2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 static int lvchange_permission(struct cmd_context *cmd,
19 struct logical_volume *lv)
25 lv_access = arg_uint_value(cmd, permission_ARG, 0);
27 if ((lv_access & LVM_WRITE) && (lv->status & LVM_WRITE)) {
28 log_error("Logical volume \"%s\" is already writable",
33 if (!(lv_access & LVM_WRITE) && !(lv->status & LVM_WRITE)) {
34 log_error("Logical volume \"%s\" is already read only",
39 if ((lv->status & MIRRORED) && (vg_is_clustered(lv->vg)) &&
40 lv_info(cmd, lv, 0, &info, 0, 0) && info.exists) {
41 log_error("Cannot change permissions of mirror \"%s\" "
42 "while active.", lv->name);
46 if (lv_access & LVM_WRITE) {
47 lv->status |= LVM_WRITE;
48 log_verbose("Setting logical volume \"%s\" read/write",
51 lv->status &= ~LVM_WRITE;
52 log_verbose("Setting logical volume \"%s\" read-only",
56 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
57 if (!vg_write(lv->vg))
60 if (!suspend_lv(cmd, lv)) {
61 log_error("Failed to lock %s", lv->name);
66 if (!vg_commit(lv->vg)) {
67 if (!resume_lv(cmd, lv))
72 log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
73 if (!resume_lv(cmd, lv)) {
74 log_error("Problem reactivating %s", lv->name);
84 static int lvchange_monitoring(struct cmd_context *cmd,
85 struct logical_volume *lv)
89 if (!lv_info(cmd, lv, 0, &info, 0, 0) || !info.exists) {
90 log_error("Logical volume, %s, is not active", lv->name);
94 /* do not monitor pvmove lv's */
95 if (lv->status & PVMOVE)
98 if ((dmeventd_monitor_mode() != DMEVENTD_MONITOR_IGNORE) &&
99 !monitor_dev_for_events(cmd, lv, 0, dmeventd_monitor_mode()))
105 static int lvchange_background_polling(struct cmd_context *cmd,
106 struct logical_volume *lv)
110 if (!lv_info(cmd, lv, 0, &info, 0, 0) || !info.exists) {
111 log_error("Logical volume, %s, is not active", lv->name);
115 if (background_polling())
116 lv_spawn_background_polling(cmd, lv);
121 static int lvchange_availability(struct cmd_context *cmd,
122 struct logical_volume *lv)
126 activate = arg_uint_value(cmd, available_ARG, 0);
128 if (activate == CHANGE_ALN) {
129 log_verbose("Deactivating logical volume \"%s\" locally",
131 if (!deactivate_lv_local(cmd, lv))
133 } else if (activate == CHANGE_AN) {
134 log_verbose("Deactivating logical volume \"%s\"", lv->name);
135 if (!deactivate_lv(cmd, lv))
138 if (lv_is_origin(lv) || (activate == CHANGE_AE)) {
139 log_verbose("Activating logical volume \"%s\" "
140 "exclusively", lv->name);
141 if (!activate_lv_excl(cmd, lv))
143 } else if (activate == CHANGE_ALY) {
144 log_verbose("Activating logical volume \"%s\" locally",
146 if (!activate_lv_local(cmd, lv))
149 log_verbose("Activating logical volume \"%s\"",
151 if (!activate_lv(cmd, lv))
155 if (background_polling())
156 lv_spawn_background_polling(cmd, lv);
162 static int lvchange_refresh(struct cmd_context *cmd, struct logical_volume *lv)
164 log_verbose("Refreshing logical volume \"%s\" (if active)", lv->name);
166 return lv_refresh(cmd, lv);
169 static int lvchange_resync(struct cmd_context *cmd,
170 struct logical_volume *lv)
175 struct logical_volume *log_lv;
177 if (!(lv->status & MIRRORED)) {
178 log_error("Unable to resync %s because it is not mirrored.",
183 if (lv->status & PVMOVE) {
184 log_error("Unable to resync pvmove volume %s", lv->name);
188 if (lv->status & LOCKED) {
189 log_error("Unable to resync locked volume %s", lv->name);
193 if (lv_info(cmd, lv, 0, &info, 1, 0)) {
194 if (info.open_count) {
195 log_error("Can't resync open logical volume \"%s\"",
201 if (!arg_count(cmd, yes_ARG) &&
202 yes_no_prompt("Do you really want to deactivate "
203 "logical volume %s to resync it? [y/n]: ",
205 log_error("Logical volume \"%s\" not resynced",
217 /* Activate exclusively to ensure no nodes still have LV active */
218 monitored = dmeventd_monitor_mode();
219 init_dmeventd_monitor(0);
221 if (!deactivate_lv(cmd, lv)) {
222 log_error("Unable to deactivate %s for resync", lv->name);
226 if (vg_is_clustered(lv->vg) && lv_is_active(lv)) {
227 log_error("Can't get exclusive access to clustered volume %s",
232 init_dmeventd_monitor(monitored);
234 log_lv = first_seg(lv)->log_lv;
236 log_very_verbose("Starting resync of %s%s%s mirror \"%s\"",
237 (active) ? "active " : "",
238 vg_is_clustered(lv->vg) ? "clustered " : "",
239 (log_lv) ? "disk-logged" : "core-logged",
243 * If this mirror has a core log (i.e. !log_lv),
244 * then simply deactivating/activating will cause
245 * it to reset the sync status. We only need to
246 * worry about persistent logs.
248 if (!log_lv && !(lv->status & MIRROR_NOTSYNCED)) {
249 if (active && !activate_lv(cmd, lv)) {
250 log_error("Failed to reactivate %s to resynchronize "
257 lv->status &= ~MIRROR_NOTSYNCED;
260 /* Separate mirror log so we can clear it */
261 detach_mirror_log(first_seg(lv));
263 if (!vg_write(lv->vg)) {
264 log_error("Failed to write intermediate VG metadata.");
265 if (!attach_mirror_log(first_seg(lv), log_lv))
267 if (active && !activate_lv(cmd, lv))
272 if (!vg_commit(lv->vg)) {
273 log_error("Failed to commit intermediate VG metadata.");
274 if (!attach_mirror_log(first_seg(lv), log_lv))
276 if (active && !activate_lv(cmd, lv))
283 if (!activate_lv(cmd, log_lv)) {
284 log_error("Unable to activate %s for mirror log resync",
289 log_very_verbose("Clearing log device %s", log_lv->name);
290 if (!set_lv(cmd, log_lv, log_lv->size, 0)) {
291 log_error("Unable to reset sync status for %s", lv->name);
292 if (!deactivate_lv(cmd, log_lv))
293 log_error("Failed to deactivate log LV after "
298 if (!deactivate_lv(cmd, log_lv)) {
299 log_error("Unable to deactivate log LV %s after wiping "
300 "for resync", log_lv->name);
304 /* Put mirror log back in place */
305 if (!attach_mirror_log(first_seg(lv), log_lv))
309 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
310 if (!vg_write(lv->vg) || !vg_commit(lv->vg)) {
311 log_error("Failed to update metadata on disk.");
315 if (active && !activate_lv(cmd, lv)) {
316 log_error("Failed to reactivate %s after resync", lv->name);
323 static int lvchange_alloc(struct cmd_context *cmd, struct logical_volume *lv)
325 int want_contiguous = 0;
326 alloc_policy_t alloc;
328 want_contiguous = strcmp(arg_str_value(cmd, contiguous_ARG, "n"), "n");
329 alloc = want_contiguous ? ALLOC_CONTIGUOUS : ALLOC_INHERIT;
330 alloc = arg_uint_value(cmd, alloc_ARG, alloc);
332 if (alloc == lv->alloc) {
333 log_error("Allocation policy of logical volume \"%s\" is "
334 "already %s", lv->name, get_alloc_string(alloc));
340 /* FIXME If contiguous, check existing extents already are */
342 log_verbose("Setting contiguous allocation policy for \"%s\" to %s",
343 lv->name, get_alloc_string(alloc));
345 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
347 /* No need to suspend LV for this change */
348 if (!vg_write(lv->vg) || !vg_commit(lv->vg))
356 static int lvchange_readahead(struct cmd_context *cmd,
357 struct logical_volume *lv)
359 unsigned read_ahead = 0;
360 unsigned pagesize = (unsigned) lvm_getpagesize() >> SECTOR_SHIFT;
363 read_ahead = arg_uint_value(cmd, readahead_ARG, 0);
365 if (read_ahead != DM_READ_AHEAD_AUTO &&
366 (lv->vg->fid->fmt->features & FMT_RESTRICTED_READAHEAD) &&
367 (read_ahead < 2 || read_ahead > 120)) {
368 log_error("Metadata only supports readahead values between 2 and 120.");
372 if (read_ahead != DM_READ_AHEAD_AUTO &&
373 read_ahead != DM_READ_AHEAD_NONE && read_ahead % pagesize) {
374 if (read_ahead < pagesize)
375 read_ahead = pagesize;
377 read_ahead = (read_ahead / pagesize) * pagesize;
378 log_warn("WARNING: Overriding readahead to %u sectors, a multiple "
379 "of %uK page size.", read_ahead, pagesize >> 1);
382 if (lv->read_ahead == read_ahead) {
383 if (read_ahead == DM_READ_AHEAD_AUTO)
384 log_error("Read ahead is already auto for \"%s\"", lv->name);
386 log_error("Read ahead is already %u for \"%s\"",
387 read_ahead, lv->name);
391 lv->read_ahead = read_ahead;
393 log_verbose("Setting read ahead to %u for \"%s\"", read_ahead,
396 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
397 if (!vg_write(lv->vg))
400 if (!suspend_lv(cmd, lv)) {
401 log_error("Failed to lock %s", lv->name);
406 if (!vg_commit(lv->vg)) {
407 if (!resume_lv(cmd, lv))
412 log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
413 if (!resume_lv(cmd, lv)) {
414 log_error("Problem reactivating %s", lv->name);
424 static int lvchange_persistent(struct cmd_context *cmd,
425 struct logical_volume *lv)
430 if (!strcmp(arg_str_value(cmd, persistent_ARG, "n"), "n")) {
431 if (!(lv->status & FIXED_MINOR)) {
432 log_error("Minor number is already not persistent "
433 "for \"%s\"", lv->name);
436 lv->status &= ~FIXED_MINOR;
439 log_verbose("Disabling persistent device number for \"%s\"",
442 if (!arg_count(cmd, minor_ARG) && lv->minor < 0) {
443 log_error("Minor number must be specified with -My");
446 if (!arg_count(cmd, major_ARG) && lv->major < 0) {
447 log_error("Major number must be specified with -My");
450 if (lv_info(cmd, lv, 0, &info, 0, 0) && info.exists)
452 if (active && !arg_count(cmd, force_ARG) &&
453 yes_no_prompt("Logical volume %s will be "
454 "deactivated temporarily. "
455 "Continue? [y/n]: ", lv->name) == 'n') {
456 log_error("%s device number not changed.",
464 log_verbose("Ensuring %s is inactive.", lv->name);
465 if (!deactivate_lv(cmd, lv)) {
466 log_error("%s: deactivation failed", lv->name);
469 lv->status |= FIXED_MINOR;
470 lv->minor = arg_int_value(cmd, minor_ARG, lv->minor);
471 lv->major = arg_int_value(cmd, major_ARG, lv->major);
472 log_verbose("Setting persistent device number to (%d, %d) "
473 "for \"%s\"", lv->major, lv->minor, lv->name);
477 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
478 if (!vg_write(lv->vg) || !vg_commit(lv->vg))
484 log_verbose("Re-activating logical volume \"%s\"", lv->name);
485 if (!activate_lv(cmd, lv)) {
486 log_error("%s: reactivation failed", lv->name);
494 static int lvchange_tag(struct cmd_context *cmd, struct logical_volume *lv,
498 struct arg_value_group_list *current_group;
500 dm_list_iterate_items(current_group, &cmd->arg_value_groups) {
501 if (!grouped_arg_is_set(current_group->arg_values, arg))
504 if (!(tag = grouped_arg_str_value(current_group->arg_values, arg, NULL))) {
505 log_error("Failed to get tag");
509 if (!lv_change_tag(lv, tag, arg == addtag_ARG))
513 log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
515 /* No need to suspend LV for this change */
516 if (!vg_write(lv->vg) || !vg_commit(lv->vg))
524 static int lvchange_single(struct cmd_context *cmd, struct logical_volume *lv,
525 void *handle __attribute__((unused)))
527 int doit = 0, docmds = 0;
528 int dmeventd_mode, archived = 0;
529 struct logical_volume *origin;
531 if (!(lv->vg->status & LVM_WRITE) &&
532 (arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
533 arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG) ||
534 arg_count(cmd, alloc_ARG))) {
535 log_error("Only -a permitted with read-only volume "
536 "group \"%s\"", lv->vg->name);
537 return EINVALID_CMD_LINE;
540 if (lv_is_origin(lv) &&
541 (arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
542 arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG) ||
543 arg_count(cmd, alloc_ARG))) {
544 log_error("Can't change logical volume \"%s\" under snapshot",
549 if (lv_is_cow(lv) && !lv_is_virtual_origin(origin_from_cow(lv)) &&
550 arg_count(cmd, available_ARG)) {
551 log_error("Can't change snapshot logical volume \"%s\"",
556 if (lv->status & PVMOVE) {
557 log_error("Unable to change pvmove LV %s", lv->name);
558 if (arg_count(cmd, available_ARG))
559 log_error("Use 'pvmove --abort' to abandon a pvmove");
563 if (lv->status & MIRROR_LOG) {
564 log_error("Unable to change mirror log LV %s directly", lv->name);
568 if (lv->status & MIRROR_IMAGE) {
569 log_error("Unable to change mirror image LV %s directly",
574 /* If LV is sparse, activate origin instead */
575 if (arg_count(cmd, available_ARG) && lv_is_cow(lv) &&
576 lv_is_virtual_origin(origin = origin_from_cow(lv)))
579 if (!(lv_is_visible(lv)) && !lv_is_virtual_origin(lv)) {
580 log_error("Unable to change internal LV %s directly",
585 if (!get_activation_monitoring_mode(cmd, lv->vg, &dmeventd_mode))
588 init_dmeventd_monitor(dmeventd_mode);
591 * FIXME: DEFAULT_BACKGROUND_POLLING should be "unspecified".
592 * If --poll is explicitly provided use it; otherwise polling
593 * should only be started if the LV is not already active. So:
594 * 1) change the activation code to say if the LV was actually activated
595 * 2) make polling of an LV tightly coupled with LV activation
597 * Do not initiate any polling if --sysinit option is used.
599 init_background_polling(arg_count(cmd, sysinit_ARG) ? 0 :
600 arg_int_value(cmd, poll_ARG,
601 DEFAULT_BACKGROUND_POLLING));
603 /* access permission change */
604 if (arg_count(cmd, permission_ARG)) {
605 if (!archive(lv->vg)) {
610 doit += lvchange_permission(cmd, lv);
614 /* allocation policy change */
615 if (arg_count(cmd, contiguous_ARG) || arg_count(cmd, alloc_ARG)) {
616 if (!archived && !archive(lv->vg)) {
621 doit += lvchange_alloc(cmd, lv);
625 /* read ahead sector change */
626 if (arg_count(cmd, readahead_ARG)) {
627 if (!archived && !archive(lv->vg)) {
632 doit += lvchange_readahead(cmd, lv);
636 /* persistent device number change */
637 if (arg_count(cmd, persistent_ARG)) {
638 if (!archived && !archive(lv->vg)) {
643 doit += lvchange_persistent(cmd, lv);
645 if (sigint_caught()) {
652 if (arg_count(cmd, addtag_ARG)) {
653 if (!archived && !archive(lv->vg)) {
658 doit += lvchange_tag(cmd, lv, addtag_ARG);
663 if (arg_count(cmd, deltag_ARG)) {
664 if (!archived && !archive(lv->vg)) {
669 doit += lvchange_tag(cmd, lv, deltag_ARG);
674 log_print("Logical volume \"%s\" changed", lv->name);
676 if (arg_count(cmd, resync_ARG))
677 if (!lvchange_resync(cmd, lv)) {
682 /* availability change */
683 if (arg_count(cmd, available_ARG)) {
684 if (!lvchange_availability(cmd, lv)) {
690 if (arg_count(cmd, refresh_ARG))
691 if (!lvchange_refresh(cmd, lv)) {
696 if (!arg_count(cmd, available_ARG) &&
697 !arg_count(cmd, refresh_ARG) &&
698 arg_count(cmd, monitor_ARG)) {
699 if (!lvchange_monitoring(cmd, lv)) {
705 if (!arg_count(cmd, available_ARG) &&
706 !arg_count(cmd, refresh_ARG) &&
707 arg_count(cmd, poll_ARG)) {
708 if (!lvchange_background_polling(cmd, lv)) {
714 if (doit != docmds) {
719 return ECMD_PROCESSED;
722 int lvchange(struct cmd_context *cmd, int argc, char **argv)
724 int update = /* options other than -a, --refresh, --monitor or --poll */
725 arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
726 arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG) ||
727 arg_count(cmd, addtag_ARG) || arg_count(cmd, deltag_ARG) ||
728 arg_count(cmd, resync_ARG) || arg_count(cmd, alloc_ARG);
731 !arg_count(cmd, available_ARG) && !arg_count(cmd, refresh_ARG) &&
732 !arg_count(cmd, monitor_ARG) && !arg_count(cmd, poll_ARG) &&
733 /* for persistent_ARG */
734 !arg_count(cmd, minor_ARG) && !arg_count(cmd, major_ARG)) {
735 log_error("Need 1 or more of -a, -C, -j, -m, -M, -p, -r, "
736 "--resync, --refresh, --alloc, --addtag, --deltag, "
737 "--monitor or --poll");
738 return EINVALID_CMD_LINE;
741 if (arg_count(cmd, available_ARG) && arg_count(cmd, refresh_ARG)) {
742 log_error("Only one of -a and --refresh permitted.");
743 return EINVALID_CMD_LINE;
746 if ((arg_count(cmd, ignorelockingfailure_ARG) ||
747 arg_count(cmd, sysinit_ARG)) && update) {
748 log_error("Only -a permitted with --ignorelockingfailure and --sysinit");
749 return EINVALID_CMD_LINE;
753 cmd->handles_missing_pvs = 1;
756 log_error("Please give logical volume path(s)");
757 return EINVALID_CMD_LINE;
760 if ((arg_count(cmd, minor_ARG) || arg_count(cmd, major_ARG)) &&
761 !arg_count(cmd, persistent_ARG)) {
762 log_error("--major and --minor require -My");
763 return EINVALID_CMD_LINE;
766 if (arg_count(cmd, minor_ARG) && argc != 1) {
767 log_error("Only give one logical volume when specifying minor");
768 return EINVALID_CMD_LINE;
771 if (arg_count(cmd, contiguous_ARG) && arg_count(cmd, alloc_ARG)) {
772 log_error("Only one of --alloc and --contiguous permitted");
773 return EINVALID_CMD_LINE;
776 if (arg_count(cmd, poll_ARG) && arg_count(cmd, sysinit_ARG)) {
777 log_error("Only one of --poll and --sysinit permitted");
778 return EINVALID_CMD_LINE;
781 return process_each_lv(cmd, argc, argv,
782 update ? READ_FOR_UPDATE : 0, NULL,