summaryrefslogtreecommitdiffstats
path: root/tools/lvchange.c
blob: 8f9b50ca27738b299c757b15e561bec4167a4559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (C) 2001  Sistina Software
 *
 * LVM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LVM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LVM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include "tools.h"

static int lvchange_single(struct cmd_context *cmd, struct logical_volume *lv);
static int lvchange_permission(struct cmd_context *cmd,
			       struct logical_volume *lv);
static int lvchange_availability(struct cmd_context *cmd,
				 struct logical_volume *lv);
static int lvchange_contiguous(struct cmd_context *cmd,
			       struct logical_volume *lv);
static int lvchange_readahead(struct cmd_context *cmd,
			      struct logical_volume *lv);
static int lvchange_persistent(struct cmd_context *cmd,
			       struct logical_volume *lv);

int lvchange(struct cmd_context *cmd, int argc, char **argv)
{
	if (!arg_count(cmd, available_ARG) && !arg_count(cmd, contiguous_ARG)
	    && !arg_count(cmd, permission_ARG) && !arg_count(cmd, readahead_ARG)
	    && !arg_count(cmd, minor_ARG) && !arg_count(cmd, persistent_ARG)) {
		log_error("One or more of -a, -C, -m, -M, -p or -r required");
		return EINVALID_CMD_LINE;
	}

	if (arg_count(cmd, ignorelockingfailure_ARG) &&
	    (arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
	     arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG))) {
		log_error("Only -a permitted with --ignorelockingfailure");
		return EINVALID_CMD_LINE;
	}

	if (!argc) {
		log_error("Please give logical volume path(s)");
		return EINVALID_CMD_LINE;
	}

	if (arg_count(cmd, minor_ARG) && argc != 1) {
		log_error("Only give one logical volume when specifying minor");
		return EINVALID_CMD_LINE;
	}

	return process_each_lv(cmd, argc, argv, LCK_VG_WRITE, &lvchange_single);
}

static int lvchange_single(struct cmd_context *cmd, struct logical_volume *lv)
{
	int doit = 0;
	int archived = 0;

	if (!(lv->vg->status & LVM_WRITE) &&
	    (arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
	     arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG))) {
		log_error("Only -a permitted with read-only volume "
			  "group \"%s\"", lv->vg->name);
		return EINVALID_CMD_LINE;
	}

	if (lv_is_origin(lv) &&
	    (arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
	     arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG))) {
		log_error("Can't change logical volume \"%s\" under snapshot",
			  lv->name);
		return ECMD_FAILED;
	}

	if (lv_is_cow(lv)) {
		log_error("Can't change snapshot logical volume \"%s\"",
			  lv->name);
		return ECMD_FAILED;
	}

	/* access permission change */
	if (arg_count(cmd, permission_ARG)) {
		if (!archive(lv->vg))
			return ECMD_FAILED;
		archived = 1;
		doit += lvchange_permission(cmd, lv);
	}

	/* allocation policy change */
	if (arg_count(cmd, contiguous_ARG)) {
		if (!archived && !archive(lv->vg))
			return ECMD_FAILED;
		archived = 1;
		doit += lvchange_contiguous(cmd, lv);
	}

	/* read ahead sector change */
	if (arg_count(cmd, readahead_ARG)) {
		if (!archived && !archive(lv->vg))
			return ECMD_FAILED;
		archived = 1;
		doit += lvchange_readahead(cmd, lv);
	}

	/* read ahead sector change */
	if (arg_count(cmd, persistent_ARG)) {
		if (!archived && !archive(lv->vg))
			return ECMD_FAILED;
		archived = 1;
		doit += lvchange_persistent(cmd, lv);
	}

	if (doit)
		log_print("Logical volume \"%s\" changed", lv->name);

	/* availability change */
	if (arg_count(cmd, available_ARG))
		if (!lvchange_availability(cmd, lv))
			return ECMD_FAILED;

	return 0;
}

static int lvchange_permission(struct cmd_context *cmd,
			       struct logical_volume *lv)
{
	int lv_access;

	lv_access = arg_int_value(cmd, permission_ARG, 0);

	if ((lv_access & LVM_WRITE) && (lv->status & LVM_WRITE)) {
		log_error("Logical volume \"%s\" is already writable",
			  lv->name);
		return 0;
	}

	if (!(lv_access & LVM_WRITE) && !(lv->status & LVM_WRITE)) {
		log_error("Logical volume \"%s\" is already read only",
			  lv->name);
		return 0;
	}

	if (lv_access & LVM_WRITE) {
		lv->status |= LVM_WRITE;
		log_verbose("Setting logical volume \"%s\" read/write",
			    lv->name);
	} else {
		lv->status &= ~LVM_WRITE;
		log_verbose("Setting logical volume \"%s\" read-only",
			    lv->name);
	}

	if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
		log_error("Failed to lock %s", lv->name);
		return 0;
	}

	log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
	if (!(lv->vg)) {
		/* FIXME: Attempt reversion? */
		unlock_lv(cmd, lv->lvid.s);
		return 0;
	}

	backup(lv->vg);

	log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
	if (!unlock_lv(cmd, lv->lvid.s)) {
		log_error("Problem reactivating %s", lv->name);
		return 0;
	}

	return 1;
}

static int lvchange_availability(struct cmd_context *cmd,
				 struct logical_volume *lv)
{
	int activate = 0;

	if (strcmp(arg_str_value(cmd, available_ARG, "n"), "n"))
		activate = 1;

	if (arg_count(cmd, minor_ARG)) {
		lv->minor = arg_int_value(cmd, minor_ARG, -1);
	}

	if (activate) {
		/* FIXME Tighter locking if lv_is_origin() */
		log_verbose("Activating logical volume \"%s\"", lv->name);
		if (!lock_vol(cmd, lv->lvid.s, LCK_LV_ACTIVATE))
			return 0;
	} else {
		log_verbose("Deactivating logical volume \"%s\"", lv->name);
		if (!lock_vol(cmd, lv->lvid.s, LCK_LV_DEACTIVATE))
			return 0;
	}

	return 1;
}

static int lvchange_contiguous(struct cmd_context *cmd,
			       struct logical_volume *lv)
{
	int lv_allocation = 0;

	if (strcmp(arg_str_value(cmd, contiguous_ARG, "n"), "n"))
		lv_allocation |= ALLOC_CONTIGUOUS;

	if ((lv_allocation & ALLOC_CONTIGUOUS) &&
	    (lv->status & ALLOC_CONTIGUOUS)) {
		log_error("Allocation policy of logical volume \"%s\" is "
			  "already contiguous", lv->name);
		return 0;
	}

	if (!(lv_allocation & ALLOC_CONTIGUOUS) &&
	    !(lv->status & ALLOC_CONTIGUOUS)) {
		log_error
		    ("Allocation policy of logical volume \"%s\" is already"
		     " not contiguous", lv->name);
		return 0;
	}

/******** FIXME lv_check_contiguous? 
	if ((lv_allocation & ALLOC_CONTIGUOUS)
		    && (ret = lv_check_contiguous(vg, lv_index + 1)) == FALSE) {
			log_error("No contiguous logical volume \"%s\"", lv->name);
			return 0;
*********/

	if (lv_allocation & ALLOC_CONTIGUOUS) {
		lv->status |= ALLOC_CONTIGUOUS;
		log_verbose("Setting contiguous allocation policy for \"%s\"",
			    lv->name);
	} else {
		lv->status &= ~ALLOC_CONTIGUOUS;
		log_verbose("Removing contiguous allocation policy for \"%s\"",
			    lv->name);
	}

	if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
		log_error("Failed to lock %s", lv->name);
		return 0;
	}

	log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
	if (!vg_write(lv->vg)) {
		/* FIXME: Attempt reversion? */
		unlock_lv(cmd, lv->lvid.s);
		return 0;
	}

	backup(lv->vg);

	if (!unlock_lv(cmd, lv->lvid.s)) {
		log_error("Problem reactivating %s", lv->name);
		return 0;
	}

	return 1;

}

static int lvchange_readahead(struct cmd_context *cmd,
			      struct logical_volume *lv)
{
	int read_ahead = 0;

	read_ahead = arg_int_value(cmd, readahead_ARG, 0);

/******* FIXME Ranges? 
	if (read_ahead < LVM_MIN_READ_AHEAD || read_ahead > LVM_MAX_READ_AHEAD) {
		log_error("read ahead sector argument is invalid");
		return 0;
	}
********/

	if (lv->read_ahead == read_ahead) {
		log_error("Read ahead is already %u for \"%s\"",
			  read_ahead, lv->name);
		return 0;
	}

	lv->read_ahead = read_ahead;

	log_verbose("Setting read ahead to %u for \"%s\"", read_ahead,
		    lv->name);

	if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
		log_error("Failed to lock %s", lv->name);
		return 0;
	}

	log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
	if (!vg_write(lv->vg)) {
		/* FIXME: Attempt reversion? */
		unlock_lv(cmd, lv->lvid.s);
		return 0;
	}

	backup(lv->vg);

	if (!unlock_lv(cmd, lv->lvid.s)) {
		log_error("Problem reactivating %s", lv->name);
		return 0;
	}

	return 1;
}

static int lvchange_persistent(struct cmd_context *cmd,
			       struct logical_volume *lv)
{

	if (!strcmp(arg_str_value(cmd, persistent_ARG, "n"), "n")) {
		if (!(lv->status & FIXED_MINOR)) {
			log_error("Minor number is already not persistent "
				  "for \"%s\"", lv->name);
			return 0;
		}
		lv->status &= ~FIXED_MINOR;
		lv->minor = -1;
		log_verbose("Disabling persistent minor for \"%s\"", lv->name);
	} else {
		if (!arg_count(cmd, minor_ARG)) {
			log_error("Minor number must be specified with -My");
			return 0;
		}
		log_verbose("Ensuring %s is inactive. Reactivate with -ay.",
			    lv->name);
		if (!lock_vol(cmd, lv->lvid.s, LCK_LV_DEACTIVATE)) {
			log_error("%s: deactivation failed", lv->name);
			return 0;
		}
		lv->status |= FIXED_MINOR;
		lv->minor = arg_int_value(cmd, minor_ARG, -1);
		log_verbose("Setting persistent minor number to %d for \"%s\"",
			    lv->minor, lv->name);
	}

	if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
		log_error("Failed to lock %s", lv->name);
		return 0;
	}

	log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
	if (!vg_write(lv->vg)) {
		/* FIXME: Attempt reversion? */
		unlock_lv(cmd, lv->lvid.s);
		return 0;
	}

	backup(lv->vg);

	if (!unlock_lv(cmd, lv->lvid.s)) {
		log_error("Problem reactivating %s", lv->name);
		return 0;
	}

	return 1;
}