1 /* $NetBSD: activate.c,v 1.1.1.3 2009/12/02 00:26:22 haad Exp $ */
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 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
26 #include "lvm-string.h"
27 #include "toolcontext.h"
28 #include "dev_manager.h"
38 #define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)
40 int lvm1_present(struct cmd_context *cmd)
44 if (dm_snprintf(path, sizeof(path), "%s/lvm/global", cmd->proc_dir)
46 log_error("LVM1 proc global snprintf failed");
50 if (path_exists(path))
56 int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg,
57 struct dm_list *modules)
60 struct lv_segment *seg2, *snap_seg;
63 if (seg->segtype->ops->modules_needed &&
64 !seg->segtype->ops->modules_needed(mem, seg, modules)) {
65 log_error("module string allocation failed");
69 if (lv_is_origin(seg->lv))
70 dm_list_iterate(snh, &seg->lv->snapshot_segs)
71 if (!list_lv_modules(mem,
72 dm_list_struct_base(snh,
78 if (lv_is_cow(seg->lv)) {
79 snap_seg = find_cow(seg->lv);
80 if (snap_seg->segtype->ops->modules_needed &&
81 !snap_seg->segtype->ops->modules_needed(mem, snap_seg,
83 log_error("snap_seg module string allocation failed");
88 for (s = 0; s < seg->area_count; s++) {
89 switch (seg_type(seg, s)) {
91 seg2 = find_seg_by_le(seg_lv(seg, s), seg_le(seg, s));
92 if (seg2 && !list_segment_modules(mem, seg2, modules))
104 int list_lv_modules(struct dm_pool *mem, const struct logical_volume *lv,
105 struct dm_list *modules)
107 struct lv_segment *seg;
109 dm_list_iterate_items(seg, &lv->segments)
110 if (!list_segment_modules(mem, seg, modules))
116 #ifndef DEVMAPPER_SUPPORT
117 void set_activation(int act)
119 static int warned = 0;
124 log_error("Compiled without libdevmapper support. "
125 "Can't enable activation.");
133 int library_version(char *version, size_t size)
137 int driver_version(char *version, size_t size)
141 int target_version(const char *target_name, uint32_t *maj,
142 uint32_t *min, uint32_t *patchlevel)
146 int target_present(struct cmd_context *cmd, const char *target_name,
151 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info,
152 int with_open_count, int with_read_ahead)
156 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
157 struct lvinfo *info, int with_open_count, int with_read_ahead)
161 int lv_snapshot_percent(const struct logical_volume *lv, float *percent,
162 percent_range_t *percent_range)
166 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
167 int wait, float *percent, percent_range_t *percent_range,
172 int lvs_in_vg_activated(struct volume_group *vg)
176 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg)
180 int lvs_in_vg_opened(struct volume_group *vg)
184 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
188 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s)
192 int lv_resume(struct cmd_context *cmd, const char *lvid_s)
196 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s)
200 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
204 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
209 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
213 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
218 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
223 int pv_uses_vg(struct physical_volume *pv,
224 struct volume_group *vg)
229 void activation_release(void)
234 void activation_exit(void)
239 #else /* DEVMAPPER_SUPPORT */
241 static int _activation = 1;
243 void set_activation(int act)
245 if (act == _activation)
250 log_verbose("Activation enabled. Device-mapper kernel "
251 "driver will be used.");
253 log_warn("WARNING: Activation disabled. No device-mapper "
254 "interaction will be attempted.");
262 static int _passes_activation_filter(struct cmd_context *cmd,
263 struct logical_volume *lv)
265 const struct config_node *cn;
266 struct config_value *cv;
270 if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
271 /* If no host tags defined, activate */
272 if (dm_list_empty(&cmd->tags))
275 /* If any host tag matches any LV or VG tag, activate */
276 if (str_list_match_list(&cmd->tags, &lv->tags) ||
277 str_list_match_list(&cmd->tags, &lv->vg->tags))
284 for (cv = cn->v; cv; cv = cv->next) {
285 if (cv->type != CFG_STRING) {
286 log_error("Ignoring invalid string in config file "
287 "activation/volume_list");
292 log_error("Ignoring empty string in config file "
293 "activation/volume_list");
301 log_error("Ignoring empty tag in config file "
302 "activation/volume_list");
305 /* If any host tag matches any LV or VG tag, activate */
306 if (!strcmp(str, "*")) {
307 if (str_list_match_list(&cmd->tags, &lv->tags)
308 || str_list_match_list(&cmd->tags,
314 /* If supplied tag matches LV or VG tag, activate */
315 if (str_list_match_item(&lv->tags, str) ||
316 str_list_match_item(&lv->vg->tags, str))
321 if (!strchr(str, '/')) {
322 /* vgname supplied */
323 if (!strcmp(str, lv->vg->name))
329 if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name,
331 log_error("dm_snprintf error from %s/%s", lv->vg->name,
335 if (!strcmp(path, str))
342 int library_version(char *version, size_t size)
347 return dm_get_library_version(version, size);
350 int driver_version(char *version, size_t size)
355 log_very_verbose("Getting driver version");
357 return dm_driver_version(version, size);
360 int target_version(const char *target_name, uint32_t *maj,
361 uint32_t *min, uint32_t *patchlevel)
365 struct dm_versions *target, *last_target;
367 log_very_verbose("Getting target version for %s", target_name);
368 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
371 if (!dm_task_run(dmt)) {
372 log_debug("Failed to get %s target version", target_name);
373 /* Assume this was because LIST_VERSIONS isn't supported */
377 target = dm_task_get_versions(dmt);
380 last_target = target;
382 if (!strcmp(target_name, target->name)) {
384 *maj = target->version[0];
385 *min = target->version[1];
386 *patchlevel = target->version[2];
390 target = (void *) target + target->next;
391 } while (last_target != target);
394 dm_task_destroy(dmt);
399 int module_present(struct cmd_context *cmd, const char *target_name)
406 if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) {
407 log_error("module_present module name too long: %s",
412 argv[0] = MODPROBE_CMD;
416 ret = exec_cmd(cmd, argv);
421 int target_present(struct cmd_context *cmd, const char *target_name,
424 uint32_t maj, min, patchlevel;
431 if (target_version(target_name, &maj, &min, &patchlevel))
434 if (!module_present(cmd, target_name))
439 return target_version(target_name, &maj, &min, &patchlevel);
443 * Returns 1 if info structure populated, else 0 on failure.
445 static int _lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int with_mknodes,
446 struct lvinfo *info, int with_open_count, int with_read_ahead, unsigned by_uuid_only)
448 struct dm_info dminfo;
455 !(name = build_dm_name(cmd->mem, lv->vg->name, lv->name, NULL)))
458 log_debug("Getting device info for %s", name);
459 if (!dev_manager_info(lv->vg->cmd->mem, name, lv, with_mknodes,
460 with_open_count, with_read_ahead, &dminfo,
461 &info->read_ahead)) {
463 dm_pool_free(cmd->mem, name);
467 info->exists = dminfo.exists;
468 info->suspended = dminfo.suspended;
469 info->open_count = dminfo.open_count;
470 info->major = dminfo.major;
471 info->minor = dminfo.minor;
472 info->read_only = dminfo.read_only;
473 info->live_table = dminfo.live_table;
474 info->inactive_table = dminfo.inactive_table;
477 dm_pool_free(cmd->mem, name);
482 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info,
483 int with_open_count, int with_read_ahead)
485 return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0);
488 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
489 struct lvinfo *info, int with_open_count, int with_read_ahead)
491 struct logical_volume *lv;
493 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
496 return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0);
500 * Returns 1 if percent set, else 0 on failure.
502 int lv_snapshot_percent(const struct logical_volume *lv, float *percent,
503 percent_range_t *percent_range)
506 struct dev_manager *dm;
511 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
514 if (!(r = dev_manager_snapshot_percent(dm, lv, percent, percent_range)))
517 dev_manager_destroy(dm);
522 /* FIXME Merge with snapshot_percent */
523 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
524 int wait, float *percent, percent_range_t *percent_range,
528 struct dev_manager *dm;
531 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
532 * it should be considered in-sync. */
533 if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
541 if (!lv_info(cmd, lv, &info, 0, 0))
547 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
550 if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent,
551 percent_range, event_nr)))
554 dev_manager_destroy(dm);
559 static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv,
560 unsigned by_uuid_only)
564 if (!_lv_info(cmd, lv, 0, &info, 0, 0, by_uuid_only)) {
572 static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
576 if (!lv_info(cmd, lv, &info, 1, 0)) {
581 return info.open_count;
584 static int _lv_activate_lv(struct logical_volume *lv)
587 struct dev_manager *dm;
589 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
592 if (!(r = dev_manager_activate(dm, lv)))
595 dev_manager_destroy(dm);
599 static int _lv_preload(struct logical_volume *lv, int *flush_required)
602 struct dev_manager *dm;
604 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
607 if (!(r = dev_manager_preload(dm, lv, flush_required)))
610 dev_manager_destroy(dm);
614 static int _lv_deactivate(struct logical_volume *lv)
617 struct dev_manager *dm;
619 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
622 if (!(r = dev_manager_deactivate(dm, lv)))
625 dev_manager_destroy(dm);
629 static int _lv_suspend_lv(struct logical_volume *lv, int lockfs, int flush_required)
632 struct dev_manager *dm;
634 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
637 if (!(r = dev_manager_suspend(dm, lv, lockfs, flush_required)))
640 dev_manager_destroy(dm);
645 * These two functions return the number of visible LVs in the state,
648 static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only)
656 dm_list_iterate_items(lvl, &vg->lvs) {
657 if (lv_is_visible(lvl->lv))
658 count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1);
664 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg)
666 return _lvs_in_vg_activated(vg, 1);
669 int lvs_in_vg_activated(struct volume_group *vg)
671 return _lvs_in_vg_activated(vg, 0);
674 int lvs_in_vg_opened(const struct volume_group *vg)
676 const struct lv_list *lvl;
682 dm_list_iterate_items(lvl, &vg->lvs) {
683 if (lv_is_visible(lvl->lv))
684 count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
691 * Determine whether an LV is active locally or in a cluster.
692 * Assumes vg lock held.
694 * 0 - not active locally or on any node in cluster
695 * 1 - active either locally or some node in the cluster
697 int lv_is_active(struct logical_volume *lv)
701 if (_lv_active(lv->vg->cmd, lv, 0))
704 if (!vg_is_clustered(lv->vg))
707 if ((ret = remote_lock_held(lv->lvid.s)) >= 0)
711 * Old compatibility code if locking doesn't support lock query
712 * FIXME: check status to not deactivate already activate device
714 if (activate_lv_excl(lv->vg->cmd, lv)) {
715 deactivate_lv(lv->vg->cmd, lv);
720 * Exclusive local activation failed so assume it is active elsewhere.
726 * Returns 0 if an attempt to (un)monitor the device failed.
727 * Returns 1 otherwise.
729 int monitor_dev_for_events(struct cmd_context *cmd,
730 struct logical_volume *lv, int monitor)
733 int i, pending = 0, monitored;
735 struct dm_list *tmp, *snh, *snht;
736 struct lv_segment *seg;
737 int (*monitor_fn) (struct lv_segment *s, int e);
740 /* skip dmeventd code altogether */
741 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
745 * Nothing to do if dmeventd configured not to be used.
747 if (monitor && !dmeventd_monitor_mode())
751 * In case of a snapshot device, we monitor lv->snapshot->lv,
752 * not the actual LV itself.
755 return monitor_dev_for_events(cmd, lv->snapshot->lv, monitor);
758 * In case this LV is a snapshot origin, we instead monitor
759 * each of its respective snapshots (the origin itself does
760 * not need to be monitored).
762 * TODO: This may change when snapshots of mirrors are allowed.
764 if (lv_is_origin(lv)) {
765 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
766 if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
767 struct lv_segment, origin_list)->cow, monitor))
772 dm_list_iterate(tmp, &lv->segments) {
773 seg = dm_list_item(tmp, struct lv_segment);
775 /* Recurse for AREA_LV */
776 for (s = 0; s < seg->area_count; s++) {
777 if (seg_type(seg, s) != AREA_LV)
779 if (!monitor_dev_for_events(cmd, seg_lv(seg, s),
781 log_error("Failed to %smonitor %s",
783 seg_lv(seg, s)->name);
788 if (!seg_monitored(seg) || (seg->status & PVMOVE))
793 /* Check monitoring status */
794 if (seg->segtype->ops->target_monitored)
795 monitored = seg->segtype->ops->target_monitored(seg, &pending);
797 continue; /* segtype doesn't support registration */
800 * FIXME: We should really try again if pending
802 monitored = (pending) ? 0 : monitored;
806 log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
807 else if (seg->segtype->ops->target_monitor_events)
808 monitor_fn = seg->segtype->ops->target_monitor_events;
811 log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
812 else if (seg->segtype->ops->target_unmonitor_events)
813 monitor_fn = seg->segtype->ops->target_unmonitor_events;
820 log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name);
822 /* FIXME specify events */
823 if (!monitor_fn(seg, 0)) {
824 log_error("%s/%s: %s segment monitoring function failed.",
825 lv->vg->name, lv->name, seg->segtype->name);
829 /* Check [un]monitor results */
830 /* Try a couple times if pending, but not forever... */
831 for (i = 0; i < 10; i++) {
833 monitored = seg->segtype->ops->target_monitored(seg, &pending);
835 (!monitored && monitor) ||
836 (monitored && !monitor))
837 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
838 lv->vg->name, lv->name, monitor ? "" : "un");
844 r = (monitored && monitor) || (!monitored && !monitor);
853 static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
854 int error_if_not_suspended)
856 struct logical_volume *lv = NULL, *lv_pre = NULL;
858 int r = 0, lockfs = 0, flush_required = 0;
863 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
866 /* Use precommitted metadata if present */
867 if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
871 _skip("Suspending '%s'.", lv->name);
876 if (!lv_info(cmd, lv, &info, 0, 0))
879 if (!info.exists || info.suspended) {
880 r = error_if_not_suspended ? 0 : 1;
884 lv_calculate_readahead(lv, NULL);
886 /* If VG was precommitted, preload devices for the LV */
887 if ((lv_pre->vg->status & PRECOMMITTED)) {
888 if (!_lv_preload(lv_pre, &flush_required)) {
889 /* FIXME Revert preloading */
894 if (!monitor_dev_for_events(cmd, lv, 0))
895 /* FIXME Consider aborting here */
900 if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre))
903 if (!_lv_suspend_lv(lv, lockfs, flush_required)) {
912 vg_release(lv_pre->vg);
919 /* Returns success if the device is not active */
920 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s)
922 return _lv_suspend(cmd, lvid_s, 0);
925 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
927 return _lv_suspend(cmd, lvid_s, 1);
930 static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
931 int error_if_not_active)
933 struct logical_volume *lv;
940 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
944 _skip("Resuming '%s'.", lv->name);
949 if (!lv_info(cmd, lv, &info, 0, 0))
952 if (!info.exists || !info.suspended) {
953 r = error_if_not_active ? 0 : 1;
957 if (!_lv_activate_lv(lv))
963 if (!monitor_dev_for_events(cmd, lv, 1))
974 /* Returns success if the device is not active */
975 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s)
977 return _lv_resume(cmd, lvid_s, 0);
980 int lv_resume(struct cmd_context *cmd, const char *lvid_s)
982 return _lv_resume(cmd, lvid_s, 1);
985 static int _lv_has_open_snapshots(struct logical_volume *lv)
987 struct lv_segment *snap_seg;
991 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
992 if (!lv_info(lv->vg->cmd, snap_seg->cow, &info, 1, 0)) {
997 if (info.exists && info.open_count) {
998 log_error("LV %s/%s has open snapshot %s: "
999 "not deactivating", lv->vg->name, lv->name,
1000 snap_seg->cow->name);
1008 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
1010 struct logical_volume *lv;
1017 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1021 _skip("Deactivating '%s'.", lv->name);
1026 if (!lv_info(cmd, lv, &info, 1, 0))
1034 if (lv_is_visible(lv)) {
1035 if (info.open_count) {
1036 log_error("LV %s/%s in use: not deactivating",
1037 lv->vg->name, lv->name);
1040 if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1044 lv_calculate_readahead(lv, NULL);
1046 if (!monitor_dev_for_events(cmd, lv, 0))
1050 r = _lv_deactivate(lv);
1054 if (!lv_info(cmd, lv, &info, 1, 0) || info.exists)
1063 /* Test if LV passes filter */
1064 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
1067 struct logical_volume *lv;
1070 if (!activation()) {
1075 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1078 if (!_passes_activation_filter(cmd, lv)) {
1079 log_verbose("Not activating %s/%s due to config file settings",
1080 lv->vg->name, lv->name);
1092 static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
1093 int exclusive, int filter)
1095 struct logical_volume *lv;
1102 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1105 if (filter && !_passes_activation_filter(cmd, lv)) {
1106 log_verbose("Not activating %s/%s due to config file settings",
1107 lv->vg->name, lv->name);
1111 if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
1112 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1117 if (lv_has_unknown_segments(lv)) {
1118 log_error("Refusing activation of LV %s containing "
1119 "an unrecognised segment.", lv->name);
1124 _skip("Activating '%s'.", lv->name);
1129 if (!lv_info(cmd, lv, &info, 0, 0))
1132 if (info.exists && !info.suspended && info.live_table) {
1137 lv_calculate_readahead(lv, NULL);
1140 lv->status |= ACTIVATE_EXCL;
1143 if (!(r = _lv_activate_lv(lv)))
1148 if (r && !monitor_dev_for_events(cmd, lv, 1))
1159 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1161 if (!_lv_activate(cmd, lvid_s, exclusive, 0))
1167 /* Activate LV only if it passes filter */
1168 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1170 if (!_lv_activate(cmd, lvid_s, exclusive, 1))
1176 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1182 r = dm_mknodes(NULL);
1187 if (!_lv_info(cmd, lv, 1, &info, 0, 0, 0))
1191 if (lv_is_visible(lv))
1192 r = dev_manager_lv_mknodes(lv);
1194 r = dev_manager_lv_rmnodes(lv);
1202 * Does PV use VG somewhere in its construction?
1203 * Returns 1 on failure.
1205 int pv_uses_vg(struct physical_volume *pv,
1206 struct volume_group *vg)
1211 if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1214 return dev_manager_device_uses_vg(pv->dev, vg);
1217 void activation_release(void)
1219 dev_manager_release();
1222 void activation_exit(void)