Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / tools / lvchange.c
blob284c23cf8a780ccd4bdcbd3c47809bdbade8873d
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "tools.h"
20 static int lvchange_permission(struct cmd_context *cmd,
21 struct logical_volume *lv)
23 uint32_t lv_access;
24 struct lvinfo info;
25 int r = 0;
27 lv_access = arg_uint_value(cmd, permission_ARG, 0);
29 if ((lv_access & LVM_WRITE) && (lv->status & LVM_WRITE)) {
30 log_error("Logical volume \"%s\" is already writable",
31 lv->name);
32 return 0;
35 if (!(lv_access & LVM_WRITE) && !(lv->status & LVM_WRITE)) {
36 log_error("Logical volume \"%s\" is already read only",
37 lv->name);
38 return 0;
41 if ((lv->status & MIRRORED) && (vg_is_clustered(lv->vg)) &&
42 lv_info(cmd, lv, &info, 0, 0) && info.exists) {
43 log_error("Cannot change permissions of mirror \"%s\" "
44 "while active.", lv->name);
45 return 0;
48 if (lv_access & LVM_WRITE) {
49 lv->status |= LVM_WRITE;
50 log_verbose("Setting logical volume \"%s\" read/write",
51 lv->name);
52 } else {
53 lv->status &= ~LVM_WRITE;
54 log_verbose("Setting logical volume \"%s\" read-only",
55 lv->name);
58 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
59 if (!vg_write(lv->vg))
60 return_0;
62 if (!suspend_lv(cmd, lv)) {
63 log_error("Failed to lock %s", lv->name);
64 vg_revert(lv->vg);
65 goto out;
68 if (!vg_commit(lv->vg)) {
69 resume_lv(cmd, lv);
70 goto_out;
73 log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
74 if (!resume_lv(cmd, lv)) {
75 log_error("Problem reactivating %s", lv->name);
76 goto out;
79 r = 1;
80 out:
81 backup(lv->vg);
82 return r;
85 static int lvchange_monitoring(struct cmd_context *cmd,
86 struct logical_volume *lv)
88 struct lvinfo info;
90 if (!lv_info(cmd, lv, &info, 0, 0) || !info.exists) {
91 log_error("Logical volume, %s, is not active", lv->name);
92 return 0;
95 /* do not monitor pvmove lv's */
96 if (lv->status & PVMOVE)
97 return 1;
99 if ((dmeventd_monitor_mode() != DMEVENTD_MONITOR_IGNORE) &&
100 !monitor_dev_for_events(cmd, lv, dmeventd_monitor_mode()))
101 stack;
103 return 1;
106 static int lvchange_availability(struct cmd_context *cmd,
107 struct logical_volume *lv)
109 int activate;
111 activate = arg_uint_value(cmd, available_ARG, 0);
113 if (activate == CHANGE_ALN) {
114 log_verbose("Deactivating logical volume \"%s\" locally",
115 lv->name);
116 if (!deactivate_lv_local(cmd, lv))
117 return_0;
118 } else if (activate == CHANGE_AN) {
119 log_verbose("Deactivating logical volume \"%s\"", lv->name);
120 if (!deactivate_lv(cmd, lv))
121 return_0;
122 } else {
123 if (lv_is_origin(lv) || (activate == CHANGE_AE)) {
124 log_verbose("Activating logical volume \"%s\" "
125 "exclusively", lv->name);
126 if (!activate_lv_excl(cmd, lv))
127 return_0;
128 } else if (activate == CHANGE_ALY) {
129 log_verbose("Activating logical volume \"%s\" locally",
130 lv->name);
131 if (!activate_lv_local(cmd, lv))
132 return_0;
133 } else {
134 log_verbose("Activating logical volume \"%s\"",
135 lv->name);
136 if (!activate_lv(cmd, lv))
137 return_0;
140 lv_spawn_background_polling(cmd, lv);
143 return 1;
146 static int lvchange_refresh(struct cmd_context *cmd, struct logical_volume *lv)
148 log_verbose("Refreshing logical volume \"%s\" (if active)", lv->name);
149 return lv_refresh(cmd, lv);
152 static int lvchange_resync(struct cmd_context *cmd,
153 struct logical_volume *lv)
155 int active = 0;
156 int monitored;
157 struct lvinfo info;
158 struct logical_volume *log_lv;
160 if (!(lv->status & MIRRORED)) {
161 log_error("Unable to resync %s because it is not mirrored.",
162 lv->name);
163 return 1;
166 if (lv->status & PVMOVE) {
167 log_error("Unable to resync pvmove volume %s", lv->name);
168 return 0;
171 if (lv->status & LOCKED) {
172 log_error("Unable to resync locked volume %s", lv->name);
173 return 0;
176 if (lv_info(cmd, lv, &info, 1, 0)) {
177 if (info.open_count) {
178 log_error("Can't resync open logical volume \"%s\"",
179 lv->name);
180 return 0;
183 if (info.exists) {
184 if (!arg_count(cmd, yes_ARG) &&
185 yes_no_prompt("Do you really want to deactivate "
186 "logical volume %s to resync it? [y/n]: ",
187 lv->name) == 'n') {
188 log_print("Logical volume \"%s\" not resynced",
189 lv->name);
190 return 0;
193 if (sigint_caught())
194 return 0;
196 active = 1;
200 /* Activate exclusively to ensure no nodes still have LV active */
201 monitored = dmeventd_monitor_mode();
202 init_dmeventd_monitor(0);
204 if (!deactivate_lv(cmd, lv)) {
205 log_error("Unable to deactivate %s for resync", lv->name);
206 return 0;
209 if (vg_is_clustered(lv->vg) && lv_is_active(lv)) {
210 log_error("Can't get exclusive access to clustered volume %s",
211 lv->name);
212 return 0;
215 init_dmeventd_monitor(monitored);
217 log_lv = first_seg(lv)->log_lv;
219 log_very_verbose("Starting resync of %s%s%s mirror \"%s\"",
220 (active) ? "active " : "",
221 vg_is_clustered(lv->vg) ? "clustered " : "",
222 (log_lv) ? "disk-logged" : "core-logged",
223 lv->name);
226 * If this mirror has a core log (i.e. !log_lv),
227 * then simply deactivating/activating will cause
228 * it to reset the sync status. We only need to
229 * worry about persistent logs.
231 if (!log_lv && !(lv->status & MIRROR_NOTSYNCED)) {
232 if (active && !activate_lv(cmd, lv)) {
233 log_error("Failed to reactivate %s to resynchronize "
234 "mirror", lv->name);
235 return 0;
237 return 1;
240 lv->status &= ~MIRROR_NOTSYNCED;
242 if (log_lv) {
243 /* Separate mirror log so we can clear it */
244 detach_mirror_log(first_seg(lv));
246 if (!vg_write(lv->vg)) {
247 log_error("Failed to write intermediate VG metadata.");
248 if (!attach_mirror_log(first_seg(lv), log_lv))
249 stack;
250 if (active && !activate_lv(cmd, lv))
251 stack;
252 return 0;
255 if (!vg_commit(lv->vg)) {
256 log_error("Failed to commit intermediate VG metadata.");
257 if (!attach_mirror_log(first_seg(lv), log_lv))
258 stack;
259 if (active && !activate_lv(cmd, lv))
260 stack;
261 return 0;
264 backup(lv->vg);
266 if (!activate_lv(cmd, log_lv)) {
267 log_error("Unable to activate %s for mirror log resync",
268 log_lv->name);
269 return 0;
272 log_very_verbose("Clearing log device %s", log_lv->name);
273 if (!set_lv(cmd, log_lv, log_lv->size, 0)) {
274 log_error("Unable to reset sync status for %s", lv->name);
275 if (!deactivate_lv(cmd, log_lv))
276 log_error("Failed to deactivate log LV after "
277 "wiping failed");
278 return 0;
281 if (!deactivate_lv(cmd, log_lv)) {
282 log_error("Unable to deactivate log LV %s after wiping "
283 "for resync", log_lv->name);
284 return 0;
287 /* Put mirror log back in place */
288 if (!attach_mirror_log(first_seg(lv), log_lv))
289 stack;
292 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
293 if (!vg_write(lv->vg) || !vg_commit(lv->vg)) {
294 log_error("Failed to update metadata on disk.");
295 return 0;
298 if (active && !activate_lv(cmd, lv)) {
299 log_error("Failed to reactivate %s after resync", lv->name);
300 return 0;
303 return 1;
306 static int lvchange_alloc(struct cmd_context *cmd, struct logical_volume *lv)
308 int want_contiguous = 0;
309 alloc_policy_t alloc;
311 want_contiguous = strcmp(arg_str_value(cmd, contiguous_ARG, "n"), "n");
312 alloc = want_contiguous ? ALLOC_CONTIGUOUS : ALLOC_INHERIT;
313 alloc = arg_uint_value(cmd, alloc_ARG, alloc);
315 if (alloc == lv->alloc) {
316 log_error("Allocation policy of logical volume \"%s\" is "
317 "already %s", lv->name, get_alloc_string(alloc));
318 return 0;
321 lv->alloc = alloc;
323 /* FIXME If contiguous, check existing extents already are */
325 log_verbose("Setting contiguous allocation policy for \"%s\" to %s",
326 lv->name, get_alloc_string(alloc));
328 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
330 /* No need to suspend LV for this change */
331 if (!vg_write(lv->vg) || !vg_commit(lv->vg))
332 return_0;
334 backup(lv->vg);
336 return 1;
339 static int lvchange_readahead(struct cmd_context *cmd,
340 struct logical_volume *lv)
342 unsigned read_ahead = 0;
343 unsigned pagesize = (unsigned) lvm_getpagesize() >> SECTOR_SHIFT;
344 int r = 0;
346 read_ahead = arg_uint_value(cmd, readahead_ARG, 0);
348 if (read_ahead != DM_READ_AHEAD_AUTO &&
349 (lv->vg->fid->fmt->features & FMT_RESTRICTED_READAHEAD) &&
350 (read_ahead < 2 || read_ahead > 120)) {
351 log_error("Metadata only supports readahead values between 2 and 120.");
352 return 0;
355 if (read_ahead != DM_READ_AHEAD_AUTO &&
356 read_ahead != DM_READ_AHEAD_NONE && read_ahead % pagesize) {
357 if (read_ahead < pagesize)
358 read_ahead = pagesize;
359 else
360 read_ahead = (read_ahead / pagesize) * pagesize;
361 log_warn("WARNING: Overriding readahead to %u sectors, a multiple "
362 "of %uK page size.", read_ahead, pagesize >> 1);
365 if (lv->read_ahead == read_ahead) {
366 if (read_ahead == DM_READ_AHEAD_AUTO)
367 log_error("Read ahead is already auto for \"%s\"", lv->name);
368 else
369 log_error("Read ahead is already %u for \"%s\"",
370 read_ahead, lv->name);
371 return 0;
374 lv->read_ahead = read_ahead;
376 log_verbose("Setting read ahead to %u for \"%s\"", read_ahead,
377 lv->name);
379 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
380 if (!vg_write(lv->vg))
381 return_0;
383 if (!suspend_lv(cmd, lv)) {
384 log_error("Failed to lock %s", lv->name);
385 vg_revert(lv->vg);
386 goto out;
389 if (!vg_commit(lv->vg)) {
390 resume_lv(cmd, lv);
391 goto_out;
394 log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
395 if (!resume_lv(cmd, lv)) {
396 log_error("Problem reactivating %s", lv->name);
397 goto out;
400 r = 1;
401 out:
402 backup(lv->vg);
403 return r;
406 static int lvchange_persistent(struct cmd_context *cmd,
407 struct logical_volume *lv)
409 struct lvinfo info;
410 int active = 0;
412 if (!strcmp(arg_str_value(cmd, persistent_ARG, "n"), "n")) {
413 if (!(lv->status & FIXED_MINOR)) {
414 log_error("Minor number is already not persistent "
415 "for \"%s\"", lv->name);
416 return 0;
418 lv->status &= ~FIXED_MINOR;
419 lv->minor = -1;
420 lv->major = -1;
421 log_verbose("Disabling persistent device number for \"%s\"",
422 lv->name);
423 } else {
424 if (!arg_count(cmd, minor_ARG) && lv->minor < 0) {
425 log_error("Minor number must be specified with -My");
426 return 0;
428 if (!arg_count(cmd, major_ARG) && lv->major < 0) {
429 log_error("Major number must be specified with -My");
430 return 0;
432 if (lv_info(cmd, lv, &info, 0, 0) && info.exists)
433 active = 1;
434 if (active && !arg_count(cmd, force_ARG) &&
435 yes_no_prompt("Logical volume %s will be "
436 "deactivated temporarily. "
437 "Continue? [y/n]: ", lv->name) == 'n') {
438 log_print("%s device number not changed.",
439 lv->name);
440 return 0;
443 if (sigint_caught())
444 return 0;
446 log_verbose("Ensuring %s is inactive.", lv->name);
447 if (!deactivate_lv(cmd, lv)) {
448 log_error("%s: deactivation failed", lv->name);
449 return 0;
451 lv->status |= FIXED_MINOR;
452 lv->minor = arg_int_value(cmd, minor_ARG, lv->minor);
453 lv->major = arg_int_value(cmd, major_ARG, lv->major);
454 log_verbose("Setting persistent device number to (%d, %d) "
455 "for \"%s\"", lv->major, lv->minor, lv->name);
459 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
460 if (!vg_write(lv->vg) || !vg_commit(lv->vg))
461 return_0;
463 backup(lv->vg);
465 if (active) {
466 log_verbose("Re-activating logical volume \"%s\"", lv->name);
467 if (!activate_lv(cmd, lv)) {
468 log_error("%s: reactivation failed", lv->name);
469 return 0;
473 return 1;
476 static int lvchange_tag(struct cmd_context *cmd, struct logical_volume *lv,
477 int arg)
479 const char *tag;
481 if (!(tag = arg_str_value(cmd, arg, NULL))) {
482 log_error("Failed to get tag");
483 return 0;
486 if (!(lv->vg->fid->fmt->features & FMT_TAGS)) {
487 log_error("Logical volume %s/%s does not support tags",
488 lv->vg->name, lv->name);
489 return 0;
492 if ((arg == addtag_ARG)) {
493 if (!str_list_add(cmd->mem, &lv->tags, tag)) {
494 log_error("Failed to add tag %s to %s/%s",
495 tag, lv->vg->name, lv->name);
496 return 0;
498 } else {
499 if (!str_list_del(&lv->tags, tag)) {
500 log_error("Failed to remove tag %s from %s/%s",
501 tag, lv->vg->name, lv->name);
502 return 0;
506 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
508 /* No need to suspend LV for this change */
509 if (!vg_write(lv->vg) || !vg_commit(lv->vg))
510 return_0;
512 backup(lv->vg);
514 return 1;
517 static int lvchange_single(struct cmd_context *cmd, struct logical_volume *lv,
518 void *handle __attribute((unused)))
520 int doit = 0, docmds = 0;
521 int archived = 0;
522 struct logical_volume *origin;
524 if (!(lv->vg->status & LVM_WRITE) &&
525 (arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
526 arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG) ||
527 arg_count(cmd, alloc_ARG))) {
528 log_error("Only -a permitted with read-only volume "
529 "group \"%s\"", lv->vg->name);
530 return EINVALID_CMD_LINE;
533 if (lv_is_origin(lv) &&
534 (arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
535 arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG) ||
536 arg_count(cmd, alloc_ARG))) {
537 log_error("Can't change logical volume \"%s\" under snapshot",
538 lv->name);
539 return ECMD_FAILED;
542 if (lv_is_cow(lv) && !lv_is_virtual_origin(origin_from_cow(lv)) &&
543 arg_count(cmd, available_ARG)) {
544 log_error("Can't change snapshot logical volume \"%s\"",
545 lv->name);
546 return ECMD_FAILED;
549 if (lv->status & PVMOVE) {
550 log_error("Unable to change pvmove LV %s", lv->name);
551 if (arg_count(cmd, available_ARG))
552 log_error("Use 'pvmove --abort' to abandon a pvmove");
553 return ECMD_FAILED;
556 if (lv->status & MIRROR_LOG) {
557 log_error("Unable to change mirror log LV %s directly", lv->name);
558 return ECMD_FAILED;
561 if (lv->status & MIRROR_IMAGE) {
562 log_error("Unable to change mirror image LV %s directly",
563 lv->name);
564 return ECMD_FAILED;
567 /* If LV is sparse, activate origin instead */
568 if (arg_count(cmd, available_ARG) && lv_is_cow(lv) &&
569 lv_is_virtual_origin(origin = origin_from_cow(lv)))
570 lv = origin;
572 if (!(lv_is_visible(lv)) && !lv_is_virtual_origin(lv)) {
573 log_error("Unable to change internal LV %s directly",
574 lv->name);
575 return ECMD_FAILED;
578 init_dmeventd_monitor(arg_int_value(cmd, monitor_ARG,
579 (is_static() || arg_count(cmd, ignoremonitoring_ARG)) ?
580 DMEVENTD_MONITOR_IGNORE : DEFAULT_DMEVENTD_MONITOR));
582 /* access permission change */
583 if (arg_count(cmd, permission_ARG)) {
584 if (!archive(lv->vg)) {
585 stack;
586 return ECMD_FAILED;
588 archived = 1;
589 doit += lvchange_permission(cmd, lv);
590 docmds++;
593 /* allocation policy change */
594 if (arg_count(cmd, contiguous_ARG) || arg_count(cmd, alloc_ARG)) {
595 if (!archived && !archive(lv->vg)) {
596 stack;
597 return ECMD_FAILED;
599 archived = 1;
600 doit += lvchange_alloc(cmd, lv);
601 docmds++;
604 /* read ahead sector change */
605 if (arg_count(cmd, readahead_ARG)) {
606 if (!archived && !archive(lv->vg)) {
607 stack;
608 return ECMD_FAILED;
610 archived = 1;
611 doit += lvchange_readahead(cmd, lv);
612 docmds++;
615 /* persistent device number change */
616 if (arg_count(cmd, persistent_ARG)) {
617 if (!archived && !archive(lv->vg)) {
618 stack;
619 return ECMD_FAILED;
621 archived = 1;
622 doit += lvchange_persistent(cmd, lv);
623 docmds++;
624 if (sigint_caught()) {
625 stack;
626 return ECMD_FAILED;
630 /* add tag */
631 if (arg_count(cmd, addtag_ARG)) {
632 if (!archived && !archive(lv->vg)) {
633 stack;
634 return ECMD_FAILED;
636 archived = 1;
637 doit += lvchange_tag(cmd, lv, addtag_ARG);
638 docmds++;
641 /* del tag */
642 if (arg_count(cmd, deltag_ARG)) {
643 if (!archived && !archive(lv->vg)) {
644 stack;
645 return ECMD_FAILED;
647 archived = 1;
648 doit += lvchange_tag(cmd, lv, deltag_ARG);
649 docmds++;
652 if (doit)
653 log_print("Logical volume \"%s\" changed", lv->name);
655 if (arg_count(cmd, resync_ARG))
656 if (!lvchange_resync(cmd, lv)) {
657 stack;
658 return ECMD_FAILED;
661 /* availability change */
662 if (arg_count(cmd, available_ARG)) {
663 if (!lvchange_availability(cmd, lv)) {
664 stack;
665 return ECMD_FAILED;
669 if (arg_count(cmd, refresh_ARG))
670 if (!lvchange_refresh(cmd, lv)) {
671 stack;
672 return ECMD_FAILED;
675 if (!arg_count(cmd, available_ARG) &&
676 !arg_count(cmd, refresh_ARG) &&
677 arg_count(cmd, monitor_ARG)) {
678 if (!lvchange_monitoring(cmd, lv)) {
679 stack;
680 return ECMD_FAILED;
684 if (doit != docmds) {
685 stack;
686 return ECMD_FAILED;
689 return ECMD_PROCESSED;
692 int lvchange(struct cmd_context *cmd, int argc, char **argv)
694 if (!arg_count(cmd, available_ARG) && !arg_count(cmd, contiguous_ARG)
695 && !arg_count(cmd, permission_ARG) && !arg_count(cmd, readahead_ARG)
696 && !arg_count(cmd, minor_ARG) && !arg_count(cmd, major_ARG)
697 && !arg_count(cmd, persistent_ARG) && !arg_count(cmd, addtag_ARG)
698 && !arg_count(cmd, deltag_ARG) && !arg_count(cmd, refresh_ARG)
699 && !arg_count(cmd, alloc_ARG) && !arg_count(cmd, monitor_ARG)
700 && !arg_count(cmd, resync_ARG)) {
701 log_error("Need 1 or more of -a, -C, -j, -m, -M, -p, -r, "
702 "--resync, --refresh, --alloc, --addtag, --deltag "
703 "or --monitor");
704 return EINVALID_CMD_LINE;
707 int avail_only = /* i.e. only one of -a or --refresh is given */
708 !(arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
709 arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG) ||
710 arg_count(cmd, addtag_ARG) || arg_count(cmd, deltag_ARG) ||
711 arg_count(cmd, resync_ARG) || arg_count(cmd, alloc_ARG));
713 if (arg_count(cmd, ignorelockingfailure_ARG) && !avail_only) {
714 log_error("Only -a permitted with --ignorelockingfailure");
715 return EINVALID_CMD_LINE;
718 if (avail_only)
719 cmd->handles_missing_pvs = 1;
721 if (!argc) {
722 log_error("Please give logical volume path(s)");
723 return EINVALID_CMD_LINE;
726 if ((arg_count(cmd, minor_ARG) || arg_count(cmd, major_ARG)) &&
727 !arg_count(cmd, persistent_ARG)) {
728 log_error("--major and --minor require -My");
729 return EINVALID_CMD_LINE;
732 if (arg_count(cmd, minor_ARG) && argc != 1) {
733 log_error("Only give one logical volume when specifying minor");
734 return EINVALID_CMD_LINE;
737 if (arg_count(cmd, contiguous_ARG) && arg_count(cmd, alloc_ARG)) {
738 log_error("Only one of --alloc and --contiguous permitted");
739 return EINVALID_CMD_LINE;
742 return process_each_lv(cmd, argc, argv,
743 avail_only ? 0 : READ_FOR_UPDATE, NULL,
744 &lvchange_single);