2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2012 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
24 #include "lvm-string.h"
25 #include "toolcontext.h"
26 #include "dev_manager.h"
31 #include "sharedlib.h"
37 #define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)
39 int lvm1_present(struct cmd_context
*cmd
)
41 static char path
[PATH_MAX
];
43 if (dm_snprintf(path
, sizeof(path
), "%s/lvm/global", cmd
->proc_dir
)
45 log_error("LVM1 proc global snprintf failed");
49 if (path_exists(path
))
55 int list_segment_modules(struct dm_pool
*mem
, const struct lv_segment
*seg
,
56 struct dm_list
*modules
)
59 struct lv_segment
*seg2
, *snap_seg
;
62 if (seg
->segtype
->ops
->modules_needed
&&
63 !seg
->segtype
->ops
->modules_needed(mem
, seg
, modules
)) {
64 log_error("module string allocation failed");
68 if (lv_is_origin(seg
->lv
))
69 dm_list_iterate(snh
, &seg
->lv
->snapshot_segs
)
70 if (!list_lv_modules(mem
,
71 dm_list_struct_base(snh
,
77 if (lv_is_cow(seg
->lv
)) {
78 snap_seg
= find_cow(seg
->lv
);
79 if (snap_seg
->segtype
->ops
->modules_needed
&&
80 !snap_seg
->segtype
->ops
->modules_needed(mem
, snap_seg
,
82 log_error("snap_seg module string allocation failed");
87 for (s
= 0; s
< seg
->area_count
; s
++) {
88 switch (seg_type(seg
, s
)) {
90 seg2
= find_seg_by_le(seg_lv(seg
, s
), seg_le(seg
, s
));
91 if (seg2
&& !list_segment_modules(mem
, seg2
, modules
))
103 int list_lv_modules(struct dm_pool
*mem
, const struct logical_volume
*lv
,
104 struct dm_list
*modules
)
106 struct lv_segment
*seg
;
108 dm_list_iterate_items(seg
, &lv
->segments
)
109 if (!list_segment_modules(mem
, seg
, modules
))
115 #ifndef DEVMAPPER_SUPPORT
116 void set_activation(int act
)
118 static int warned
= 0;
123 log_error("Compiled without libdevmapper support. "
124 "Can't enable activation.");
132 int library_version(char *version
, size_t size
)
136 int driver_version(char *version
, size_t size
)
140 int target_version(const char *target_name
, uint32_t *maj
,
141 uint32_t *min
, uint32_t *patchlevel
)
145 int target_present(struct cmd_context
*cmd
, const char *target_name
,
150 int lvm_dm_prefix_check(const char *sysfs_dir
, int major
, int minor
, const char *prefix
)
154 int lv_info(struct cmd_context
*cmd
, const struct logical_volume
*lv
, unsigned origin_only
,
155 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
159 int lv_info_by_lvid(struct cmd_context
*cmd
, const char *lvid_s
,
160 unsigned origin_only
,
161 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
165 int lv_snapshot_percent(const struct logical_volume
*lv
, percent_t
*percent
)
169 int lv_mirror_percent(struct cmd_context
*cmd
, const struct logical_volume
*lv
,
170 int wait
, percent_t
*percent
, uint32_t *event_nr
)
174 int lvs_in_vg_activated(struct volume_group
*vg
)
178 int lvs_in_vg_opened(const struct volume_group
*vg
)
183 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
, unsigned origin_only
)
192 int lv_resume(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
196 int lv_resume_if_active(struct cmd_context
*cmd
, const char *lvid_s
,
197 unsigned origin_only
, unsigned exclusive
, unsigned revert
)
201 int lv_deactivate(struct cmd_context
*cmd
, const char *lvid_s
)
205 int lv_activation_filter(struct cmd_context
*cmd
, const char *lvid_s
,
210 int lv_activate(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
214 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
)
222 int lv_send_message(const struct logical_volume
*lv
, const char *message
)
226 int pv_uses_vg(struct physical_volume
*pv
,
227 struct volume_group
*vg
)
231 void activation_release(void)
234 void activation_exit(void)
238 int lv_is_active(struct logical_volume
*lv
)
242 int lv_is_active_but_not_locally(struct logical_volume
*lv
)
246 int lv_is_active_exclusive(struct logical_volume
*lv
)
250 int lv_is_active_exclusive_locally(struct logical_volume
*lv
)
254 int lv_is_active_exclusive_remotely(struct logical_volume
*lv
)
259 int lv_check_transient(struct logical_volume
*lv
)
263 int monitor_dev_for_events(struct cmd_context
*cmd
, struct logical_volume
*lv
,
264 struct lv_activate_opts
*laopts
, int monitor
)
268 #else /* DEVMAPPER_SUPPORT */
270 static int _activation
= 1;
272 void set_activation(int act
)
274 if (act
== _activation
)
279 log_verbose("Activation enabled. Device-mapper kernel "
280 "driver will be used.");
282 log_warn("WARNING: Activation disabled. No device-mapper "
283 "interaction will be attempted.");
291 static int _passes_volumes_filter(struct cmd_context
*cmd
,
292 struct logical_volume
*lv
,
293 const struct dm_config_node
*cn
,
294 const char *config_path
)
296 const struct dm_config_value
*cv
;
298 static char path
[PATH_MAX
];
300 log_verbose("%s configuration setting defined: "
301 "Checking the list to match %s/%s",
302 config_path
, lv
->vg
->name
, lv
->name
);
304 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
305 if (cv
->type
!= DM_CFG_STRING
) {
306 log_error("Ignoring invalid string in config file %s",
312 log_error("Ignoring empty string in config file %s",
322 log_error("Ignoring empty tag in config file "
326 /* If any host tag matches any LV or VG tag, activate */
327 if (!strcmp(str
, "*")) {
328 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
)
329 || str_list_match_list(&cmd
->tags
,
330 &lv
->vg
->tags
, NULL
))
335 /* If supplied tag matches LV or VG tag, activate */
336 if (str_list_match_item(&lv
->tags
, str
) ||
337 str_list_match_item(&lv
->vg
->tags
, str
))
342 if (!strchr(str
, '/')) {
343 /* vgname supplied */
344 if (!strcmp(str
, lv
->vg
->name
))
350 if (dm_snprintf(path
, sizeof(path
), "%s/%s", lv
->vg
->name
,
352 log_error("dm_snprintf error from %s/%s", lv
->vg
->name
,
356 if (!strcmp(path
, str
))
360 log_verbose("No item supplied in %s configuration setting "
361 "matches %s/%s", config_path
, lv
->vg
->name
, lv
->name
);
366 static int _passes_activation_filter(struct cmd_context
*cmd
,
367 struct logical_volume
*lv
)
369 const struct dm_config_node
*cn
;
371 if (!(cn
= find_config_tree_node(cmd
, "activation/volume_list"))) {
372 log_verbose("activation/volume_list configuration setting "
373 "not defined: Checking only host tags for %s/%s",
374 lv
->vg
->name
, lv
->name
);
376 /* If no host tags defined, activate */
377 if (dm_list_empty(&cmd
->tags
))
380 /* If any host tag matches any LV or VG tag, activate */
381 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
) ||
382 str_list_match_list(&cmd
->tags
, &lv
->vg
->tags
, NULL
))
385 log_verbose("No host tag matches %s/%s",
386 lv
->vg
->name
, lv
->name
);
392 return _passes_volumes_filter(cmd
, lv
, cn
, "activation/volume_list");
395 static int _passes_readonly_filter(struct cmd_context
*cmd
,
396 struct logical_volume
*lv
)
398 const struct dm_config_node
*cn
;
400 if (!(cn
= find_config_tree_node(cmd
, "activation/read_only_volume_list")))
403 return _passes_volumes_filter(cmd
, lv
, cn
, "activation/read_only_volume_list");
406 int library_version(char *version
, size_t size
)
411 return dm_get_library_version(version
, size
);
414 int driver_version(char *version
, size_t size
)
419 log_very_verbose("Getting driver version");
421 return dm_driver_version(version
, size
);
424 int target_version(const char *target_name
, uint32_t *maj
,
425 uint32_t *min
, uint32_t *patchlevel
)
429 struct dm_versions
*target
, *last_target
;
431 log_very_verbose("Getting target version for %s", target_name
);
432 if (!(dmt
= dm_task_create(DM_DEVICE_LIST_VERSIONS
)))
435 if (activation_checks() && !dm_task_enable_checks(dmt
))
438 if (!dm_task_run(dmt
)) {
439 log_debug("Failed to get %s target version", target_name
);
440 /* Assume this was because LIST_VERSIONS isn't supported */
444 target
= dm_task_get_versions(dmt
);
447 last_target
= target
;
449 if (!strcmp(target_name
, target
->name
)) {
451 *maj
= target
->version
[0];
452 *min
= target
->version
[1];
453 *patchlevel
= target
->version
[2];
457 target
= (struct dm_versions
*)((char *) target
+ target
->next
);
458 } while (last_target
!= target
);
461 dm_task_destroy(dmt
);
466 int lvm_dm_prefix_check(int major
, int minor
, const char *prefix
)
472 if (!(dmt
= dm_task_create(DM_DEVICE_STATUS
)))
475 if (!dm_task_set_minor(dmt
, minor
) ||
476 !dm_task_set_major(dmt
, major
) ||
478 !(uuid
= dm_task_get_uuid(dmt
))) {
479 dm_task_destroy(dmt
);
483 r
= strncasecmp(uuid
, prefix
, strlen(prefix
));
484 dm_task_destroy(dmt
);
489 int module_present(struct cmd_context
*cmd
, const char *target_name
)
496 if (dm_snprintf(module
, sizeof(module
), "dm-%s", target_name
) < 0) {
497 log_error("module_present module name too long: %s",
502 argv
[0] = MODPROBE_CMD
;
506 ret
= exec_cmd(cmd
, argv
, NULL
, 0);
511 int target_present(struct cmd_context
*cmd
, const char *target_name
,
514 uint32_t maj
, min
, patchlevel
;
521 if (target_version(target_name
, &maj
, &min
, &patchlevel
))
524 if (!module_present(cmd
, target_name
))
529 return target_version(target_name
, &maj
, &min
, &patchlevel
);
533 * Returns 1 if info structure populated, else 0 on failure.
535 int lv_info(struct cmd_context
*cmd
, const struct logical_volume
*lv
, unsigned origin_only
,
536 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
538 struct dm_info dminfo
;
543 * If open_count info is requested and we have to be sure our own udev
544 * transactions are finished
545 * For non-clustered locking type we are only interested for non-delete operation
546 * in progress - as only those could lead to opened files
548 if (with_open_count
) {
549 if (locking_is_clustered())
550 sync_local_dev_names(cmd
); /* Wait to have udev in sync */
551 else if (fs_has_non_delete_ops())
552 fs_unlock(); /* For non clustered - wait if there are non-delete ops */
555 if (!dev_manager_info(lv
->vg
->cmd
->mem
, lv
, origin_only
? "real" : NULL
, with_open_count
,
556 with_read_ahead
, &dminfo
, &info
->read_ahead
))
559 info
->exists
= dminfo
.exists
;
560 info
->suspended
= dminfo
.suspended
;
561 info
->open_count
= dminfo
.open_count
;
562 info
->major
= dminfo
.major
;
563 info
->minor
= dminfo
.minor
;
564 info
->read_only
= dminfo
.read_only
;
565 info
->live_table
= dminfo
.live_table
;
566 info
->inactive_table
= dminfo
.inactive_table
;
571 int lv_info_by_lvid(struct cmd_context
*cmd
, const char *lvid_s
,
572 unsigned origin_only
,
573 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
576 struct logical_volume
*lv
;
578 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
581 if (!lv_is_origin(lv
))
584 r
= lv_info(cmd
, lv
, origin_only
, info
, with_open_count
, with_read_ahead
);
590 int lv_check_not_in_use(struct cmd_context
*cmd
__attribute__((unused
)),
591 struct logical_volume
*lv
, struct lvinfo
*info
)
596 /* If sysfs is not used, use open_count information only. */
597 if (!*dm_sysfs_dir()) {
598 if (info
->open_count
) {
599 log_error("Logical volume %s/%s in use.",
600 lv
->vg
->name
, lv
->name
);
607 if (dm_device_has_holders(info
->major
, info
->minor
)) {
608 log_error("Logical volume %s/%s is used by another device.",
609 lv
->vg
->name
, lv
->name
);
613 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
614 log_error("Logical volume %s/%s contains a filesystem in use.",
615 lv
->vg
->name
, lv
->name
);
623 * Returns 1 if percent set, else 0 on failure.
625 int lv_check_transient(struct logical_volume
*lv
)
628 struct dev_manager
*dm
;
633 log_debug("Checking transient status for LV %s/%s", lv
->vg
->name
, lv
->name
);
635 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
638 if (!(r
= dev_manager_transient(dm
, lv
)))
641 dev_manager_destroy(dm
);
647 * Returns 1 if percent set, else 0 on failure.
649 int lv_snapshot_percent(const struct logical_volume
*lv
, percent_t
*percent
)
652 struct dev_manager
*dm
;
657 log_debug("Checking snapshot percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
659 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
662 if (!(r
= dev_manager_snapshot_percent(dm
, lv
, percent
)))
665 dev_manager_destroy(dm
);
670 /* FIXME Merge with snapshot_percent */
671 int lv_mirror_percent(struct cmd_context
*cmd
, const struct logical_volume
*lv
,
672 int wait
, percent_t
*percent
, uint32_t *event_nr
)
675 struct dev_manager
*dm
;
678 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
679 * it should be considered in-sync. */
680 if (dm_list_size(&lv
->segments
) == 1 && first_seg(lv
)->area_count
== 1) {
681 *percent
= PERCENT_100
;
688 log_debug("Checking mirror percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
690 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
696 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
699 if (!(r
= dev_manager_mirror_percent(dm
, lv
, wait
, percent
, event_nr
)))
702 dev_manager_destroy(dm
);
707 int lv_raid_percent(const struct logical_volume
*lv
, percent_t
*percent
)
709 return lv_mirror_percent(lv
->vg
->cmd
, lv
, 0, percent
, NULL
);
713 * Returns data or metadata percent usage, depends on metadata 0/1.
714 * Returns 1 if percent set, else 0 on failure.
716 int lv_thin_pool_percent(const struct logical_volume
*lv
, int metadata
,
720 struct dev_manager
*dm
;
725 log_debug("Checking thin %sdata percent for LV %s/%s",
726 (metadata
) ? "meta" : "", lv
->vg
->name
, lv
->name
);
728 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
731 if (!(r
= dev_manager_thin_pool_percent(dm
, lv
, metadata
, percent
)))
734 dev_manager_destroy(dm
);
740 * Returns 1 if percent set, else 0 on failure.
742 int lv_thin_percent(const struct logical_volume
*lv
,
743 int mapped
, percent_t
*percent
)
746 struct dev_manager
*dm
;
751 log_debug("Checking thin percent for LV %s/%s",
752 lv
->vg
->name
, lv
->name
);
754 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
757 if (!(r
= dev_manager_thin_percent(dm
, lv
, mapped
, percent
)))
760 dev_manager_destroy(dm
);
765 static int _lv_active(struct cmd_context
*cmd
, struct logical_volume
*lv
)
769 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0)) {
777 static int _lv_open_count(struct cmd_context
*cmd
, struct logical_volume
*lv
)
781 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0)) {
786 return info
.open_count
;
789 static int _lv_activate_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
)
792 struct dev_manager
*dm
;
794 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
797 if (!(r
= dev_manager_activate(dm
, lv
, laopts
)))
800 dev_manager_destroy(dm
);
804 static int _lv_preload(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
808 struct dev_manager
*dm
;
809 int old_readonly
= laopts
->read_only
;
811 laopts
->read_only
= _passes_readonly_filter(lv
->vg
->cmd
, lv
);
813 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
816 if (!(r
= dev_manager_preload(dm
, lv
, laopts
, flush_required
)))
819 dev_manager_destroy(dm
);
821 laopts
->read_only
= old_readonly
;
826 static int _lv_deactivate(struct logical_volume
*lv
)
829 struct dev_manager
*dm
;
831 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
834 if (!(r
= dev_manager_deactivate(dm
, lv
)))
837 dev_manager_destroy(dm
);
841 static int _lv_suspend_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
842 int lockfs
, int flush_required
)
845 struct dev_manager
*dm
;
847 laopts
->read_only
= _passes_readonly_filter(lv
->vg
->cmd
, lv
);
850 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
851 * device directly, we don't want to touch the devices that use it.
853 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
856 if (!(r
= dev_manager_suspend(dm
, lv
, laopts
, lockfs
, flush_required
)))
859 dev_manager_destroy(dm
);
864 * These two functions return the number of visible LVs in the state,
865 * or -1 on error. FIXME Check this.
867 int lvs_in_vg_activated(struct volume_group
*vg
)
875 dm_list_iterate_items(lvl
, &vg
->lvs
)
876 if (lv_is_visible(lvl
->lv
))
877 count
+= (_lv_active(vg
->cmd
, lvl
->lv
) == 1);
879 log_debug("Counted %d active LVs in VG %s", count
, vg
->name
);
884 int lvs_in_vg_opened(const struct volume_group
*vg
)
886 const struct lv_list
*lvl
;
892 dm_list_iterate_items(lvl
, &vg
->lvs
)
893 if (lv_is_visible(lvl
->lv
))
894 count
+= (_lv_open_count(vg
->cmd
, lvl
->lv
) > 0);
896 log_debug("Counted %d open LVs in VG %s", count
, vg
->name
);
903 * @lv: logical volume being queried
904 * @locally: set if active locally (when provided)
905 * @exclusive: set if active exclusively (when provided)
907 * Determine whether an LV is active locally or in a cluster.
908 * In addition to the return code which indicates whether or
909 * not the LV is active somewhere, two other values are set
910 * to yield more information about the status of the activation:
911 * return locally exclusively status
912 * ====== ======= =========== ======
914 * 1 0 0 active remotely
915 * 1 0 1 exclusive remotely
916 * 1 1 0 active locally and possibly remotely
917 * 1 1 1 exclusive locally (or local && !cluster)
918 * The VG lock must be held to call this function.
922 static int _lv_is_active(struct logical_volume
*lv
,
923 int *locally
, int *exclusive
)
925 int r
, l
, e
; /* remote, local, and exclusive */
929 if (_lv_active(lv
->vg
->cmd
, lv
))
932 if (!vg_is_clustered(lv
->vg
)) {
933 e
= 1; /* exclusive by definition */
937 /* Active locally, and the caller doesn't care about exclusive */
941 if ((r
= remote_lock_held(lv
->lvid
.s
, &e
)) >= 0)
945 * If lock query is not supported (due to interfacing with old
946 * code), then we cannot evaluate exclusivity properly.
948 * Old users of this function will never be affected by this,
949 * since they are only concerned about active vs. not active.
950 * New users of this function who specifically ask for 'exclusive'
951 * will be given an error message.
955 log_error("Unable to determine exclusivity of %s",
960 /* FIXME: Is this fallback alright? */
961 if (activate_lv_excl(lv
->vg
->cmd
, lv
)) {
962 if (!deactivate_lv(lv
->vg
->cmd
, lv
))
964 /* FIXME: locally & exclusive are undefined. */
967 /* FIXME: Check exclusive value here. */
974 log_very_verbose("%s/%s is %sactive%s%s",
975 lv
->vg
->name
, lv
->name
,
976 (r
|| l
) ? "" : "not ",
977 (exclusive
&& e
) ? " exclusive" : "",
978 e
? (l
? " locally" : " remotely") : "");
983 int lv_is_active(struct logical_volume
*lv
)
985 return _lv_is_active(lv
, NULL
, NULL
);
988 int lv_is_active_but_not_locally(struct logical_volume
*lv
)
991 return _lv_is_active(lv
, &l
, NULL
) && !l
;
994 int lv_is_active_exclusive(struct logical_volume
*lv
)
998 return _lv_is_active(lv
, NULL
, &e
) && e
;
1001 int lv_is_active_exclusive_locally(struct logical_volume
*lv
)
1005 return _lv_is_active(lv
, &l
, &e
) && l
&& e
;
1008 int lv_is_active_exclusive_remotely(struct logical_volume
*lv
)
1012 return _lv_is_active(lv
, &l
, &e
) && !l
&& e
;
1016 static struct dm_event_handler
*_create_dm_event_handler(struct cmd_context
*cmd
, const char *dmuuid
, const char *dso
,
1017 const int timeout
, enum dm_event_mask mask
)
1019 struct dm_event_handler
*dmevh
;
1021 if (!(dmevh
= dm_event_handler_create()))
1024 if (dm_event_handler_set_dmeventd_path(dmevh
, find_config_tree_str(cmd
, "dmeventd/executable", NULL
)))
1027 if (dm_event_handler_set_dso(dmevh
, dso
))
1030 if (dm_event_handler_set_uuid(dmevh
, dmuuid
))
1033 dm_event_handler_set_timeout(dmevh
, timeout
);
1034 dm_event_handler_set_event_mask(dmevh
, mask
);
1039 dm_event_handler_destroy(dmevh
);
1043 char *get_monitor_dso_path(struct cmd_context
*cmd
, const char *libpath
)
1047 if (!(path
= dm_pool_alloc(cmd
->mem
, PATH_MAX
))) {
1048 log_error("Failed to allocate dmeventd library path.");
1052 get_shared_library_path(cmd
, libpath
, path
, PATH_MAX
);
1057 static char *_build_target_uuid(struct cmd_context
*cmd
, struct logical_volume
*lv
)
1061 if (lv_is_thin_pool(lv
))
1062 layer
= "tpool"; /* Monitor "tpool" for the "thin pool". */
1063 else if (lv_is_origin(lv
))
1064 layer
= "real"; /* Monitor "real" for "snapshot-origin". */
1068 return build_dm_uuid(cmd
->mem
, lv
->lvid
.s
, layer
);
1071 int target_registered_with_dmeventd(struct cmd_context
*cmd
, const char *dso
,
1072 struct logical_volume
*lv
, int *pending
)
1075 enum dm_event_mask evmask
= 0;
1076 struct dm_event_handler
*dmevh
;
1082 if (!(uuid
= _build_target_uuid(cmd
, lv
)))
1085 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, 0, DM_EVENT_ALL_ERRORS
)))
1088 if (dm_event_get_registered_device(dmevh
, 0)) {
1089 dm_event_handler_destroy(dmevh
);
1093 evmask
= dm_event_handler_get_event_mask(dmevh
);
1094 if (evmask
& DM_EVENT_REGISTRATION_PENDING
) {
1096 evmask
&= ~DM_EVENT_REGISTRATION_PENDING
;
1099 dm_event_handler_destroy(dmevh
);
1104 int target_register_events(struct cmd_context
*cmd
, const char *dso
, struct logical_volume
*lv
,
1105 int evmask
__attribute__((unused
)), int set
, int timeout
)
1108 struct dm_event_handler
*dmevh
;
1114 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
1115 if (!(uuid
= _build_target_uuid(cmd
, lv
)))
1118 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, timeout
,
1119 DM_EVENT_ALL_ERRORS
| (timeout
? DM_EVENT_TIMEOUT
: 0))))
1122 r
= set
? dm_event_register_handler(dmevh
) : dm_event_unregister_handler(dmevh
);
1124 dm_event_handler_destroy(dmevh
);
1129 log_info("%s %s for events", set
? "Monitored" : "Unmonitored", uuid
);
1137 * Returns 0 if an attempt to (un)monitor the device failed.
1138 * Returns 1 otherwise.
1140 int monitor_dev_for_events(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1141 const struct lv_activate_opts
*laopts
, int monitor
)
1144 int i
, pending
= 0, monitored
;
1146 struct dm_list
*tmp
, *snh
, *snht
;
1147 struct lv_segment
*seg
;
1148 struct lv_segment
*log_seg
;
1149 int (*monitor_fn
) (struct lv_segment
*s
, int e
);
1151 static const struct lv_activate_opts zlaopts
= { 0 };
1156 /* skip dmeventd code altogether */
1157 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE
)
1161 * Nothing to do if dmeventd configured not to be used.
1163 if (monitor
&& !dmeventd_monitor_mode())
1167 * In case of a snapshot device, we monitor lv->snapshot->lv,
1168 * not the actual LV itself.
1170 if (lv_is_cow(lv
) && (laopts
->no_merging
|| !lv_is_merging_cow(lv
)))
1171 return monitor_dev_for_events(cmd
, lv
->snapshot
->lv
, NULL
, monitor
);
1174 * In case this LV is a snapshot origin, we instead monitor
1175 * each of its respective snapshots. The origin itself may
1176 * also need to be monitored if it is a mirror, for example.
1178 if (!laopts
->origin_only
&& lv_is_origin(lv
))
1179 dm_list_iterate_safe(snh
, snht
, &lv
->snapshot_segs
)
1180 if (!monitor_dev_for_events(cmd
, dm_list_struct_base(snh
,
1181 struct lv_segment
, origin_list
)->cow
, NULL
, monitor
))
1185 * If the volume is mirrored and its log is also mirrored, monitor
1186 * the log volume as well.
1188 if ((seg
= first_seg(lv
)) != NULL
&& seg
->log_lv
!= NULL
&&
1189 (log_seg
= first_seg(seg
->log_lv
)) != NULL
&&
1190 seg_is_mirrored(log_seg
))
1191 if (!monitor_dev_for_events(cmd
, seg
->log_lv
, NULL
, monitor
))
1194 dm_list_iterate(tmp
, &lv
->segments
) {
1195 seg
= dm_list_item(tmp
, struct lv_segment
);
1197 /* Recurse for AREA_LV */
1198 for (s
= 0; s
< seg
->area_count
; s
++) {
1199 if (seg_type(seg
, s
) != AREA_LV
)
1201 if (!monitor_dev_for_events(cmd
, seg_lv(seg
, s
), NULL
,
1203 log_error("Failed to %smonitor %s",
1204 monitor
? "" : "un",
1205 seg_lv(seg
, s
)->name
);
1210 if (!seg_monitored(seg
) || (seg
->status
& PVMOVE
))
1215 /* Check monitoring status */
1216 if (seg
->segtype
->ops
->target_monitored
)
1217 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1219 continue; /* segtype doesn't support registration */
1222 * FIXME: We should really try again if pending
1224 monitored
= (pending
) ? 0 : monitored
;
1228 log_verbose("%s/%s already monitored.", lv
->vg
->name
, lv
->name
);
1229 else if (seg
->segtype
->ops
->target_monitor_events
)
1230 monitor_fn
= seg
->segtype
->ops
->target_monitor_events
;
1233 log_verbose("%s/%s already not monitored.", lv
->vg
->name
, lv
->name
);
1234 else if (seg
->segtype
->ops
->target_unmonitor_events
)
1235 monitor_fn
= seg
->segtype
->ops
->target_unmonitor_events
;
1238 /* Do [un]monitor */
1242 log_verbose("%sonitoring %s/%s%s", monitor
? "M" : "Not m", lv
->vg
->name
, lv
->name
,
1243 test_mode() ? " [Test mode: skipping this]" : "");
1245 /* FIXME Test mode should really continue a bit further. */
1249 /* FIXME specify events */
1250 if (!monitor_fn(seg
, 0)) {
1251 log_error("%s/%s: %s segment monitoring function failed.",
1252 lv
->vg
->name
, lv
->name
, seg
->segtype
->name
);
1256 /* Check [un]monitor results */
1257 /* Try a couple times if pending, but not forever... */
1258 for (i
= 0; i
< 10; i
++) {
1260 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1262 (!monitored
&& monitor
) ||
1263 (monitored
&& !monitor
))
1264 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
1265 lv
->vg
->name
, lv
->name
, monitor
? "" : "un");
1272 r
= (monitored
&& monitor
) || (!monitored
&& !monitor
);
1281 struct detached_lv_data
{
1282 struct logical_volume
*lv_pre
;
1283 struct lv_activate_opts
*laopts
;
1284 int *flush_required
;
1287 static int _preload_detached_lv(struct cmd_context
*cmd
, struct logical_volume
*lv
, void *data
)
1289 struct detached_lv_data
*detached
= data
;
1290 struct lv_list
*lvl_pre
;
1292 if ((lvl_pre
= find_lv_in_vg(detached
->lv_pre
->vg
, lv
->name
))) {
1293 if (lv_is_visible(lvl_pre
->lv
) && lv_is_active(lv
) && (!lv_is_cow(lv
) || !lv_is_cow(lvl_pre
->lv
)) &&
1294 !_lv_preload(lvl_pre
->lv
, detached
->laopts
, detached
->flush_required
))
1301 static int _lv_suspend(struct cmd_context
*cmd
, const char *lvid_s
,
1302 struct lv_activate_opts
*laopts
, int error_if_not_suspended
)
1304 struct logical_volume
*lv
= NULL
, *lv_pre
= NULL
, *pvmove_lv
= NULL
;
1305 struct lv_list
*lvl_pre
;
1306 struct seg_list
*sl
;
1307 struct lv_segment
*snap_seg
;
1309 int r
= 0, lockfs
= 0, flush_required
= 0;
1310 struct detached_lv_data detached
;
1315 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1318 /* Use precommitted metadata if present */
1319 if (!(lv_pre
= lv_from_lvid(cmd
, lvid_s
, 1)))
1322 /* Ignore origin_only unless LV is origin in both old and new metadata */
1323 if (!lv_is_origin(lv
) || !lv_is_origin(lv_pre
))
1324 laopts
->origin_only
= 0;
1327 _skip("Suspending %s%s.", lv
->name
, laopts
->origin_only
? " origin without snapshots" : "");
1332 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1335 if (!info
.exists
|| info
.suspended
) {
1336 if (!error_if_not_suspended
) {
1339 critical_section_inc(cmd
, "already suspended");
1344 if (!lv_read_replicator_vgs(lv
))
1347 lv_calculate_readahead(lv
, NULL
);
1350 * Preload devices for the LV.
1351 * If the PVMOVE LV is being removed, it's only present in the old
1352 * metadata and not the new, so we must explicitly add the new
1353 * tables for all the changed LVs here, as the relationships
1354 * are not found by walking the new metadata.
1356 if (!(lv_pre
->status
& LOCKED
) &&
1357 (lv
->status
& LOCKED
) &&
1358 (pvmove_lv
= find_pvmove_lv_in_lv(lv
))) {
1359 /* Preload all the LVs above the PVMOVE LV */
1360 dm_list_iterate_items(sl
, &pvmove_lv
->segs_using_this_lv
) {
1361 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, sl
->seg
->lv
->name
))) {
1362 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", sl
->seg
->lv
->name
);
1365 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1368 /* Now preload the PVMOVE LV itself */
1369 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, pvmove_lv
->name
))) {
1370 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", pvmove_lv
->name
);
1373 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1376 if (!_lv_preload(lv_pre
, laopts
, &flush_required
))
1377 /* FIXME Revert preloading */
1381 * Search for existing LVs that have become detached and preload them.
1383 detached
.lv_pre
= lv_pre
;
1384 detached
.laopts
= laopts
;
1385 detached
.flush_required
= &flush_required
;
1387 if (!for_each_sub_lv(cmd
, lv
, &_preload_detached_lv
, &detached
))
1391 * Preload any snapshots that are being removed.
1393 if (!laopts
->origin_only
&& lv_is_origin(lv
)) {
1394 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1395 if (!(lvl_pre
= find_lv_in_vg_by_lvid(lv_pre
->vg
, &snap_seg
->cow
->lvid
))) {
1396 log_error(INTERNAL_ERROR
"LV %s (%s) missing from preload metadata",
1397 snap_seg
->cow
->name
, snap_seg
->cow
->lvid
.id
[1].uuid
);
1400 if (!lv_is_cow(lvl_pre
->lv
) &&
1401 !_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1407 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 0))
1408 /* FIXME Consider aborting here */
1411 critical_section_inc(cmd
, "suspending");
1413 critical_section_inc(cmd
, "suspending pvmove LV");
1415 if (!laopts
->origin_only
&&
1416 (lv_is_origin(lv_pre
) || lv_is_cow(lv_pre
)))
1420 * Suspending an LV directly above a PVMOVE LV also
1421 * suspends other LVs using that same PVMOVE LV.
1422 * FIXME Remove this and delay the 'clear node' until
1423 * after the code knows whether there's a different
1424 * inactive table to load or not instead so lv_suspend
1425 * can be called separately for each LV safely.
1427 if ((lv_pre
->vg
->status
& PRECOMMITTED
) &&
1428 (lv_pre
->status
& LOCKED
) && find_pvmove_lv_in_lv(lv_pre
)) {
1429 if (!_lv_suspend_lv(lv_pre
, laopts
, lockfs
, flush_required
)) {
1430 critical_section_dec(cmd
, "failed precommitted suspend");
1432 critical_section_dec(cmd
, "failed precommitted suspend (pvmove)");
1436 /* Normal suspend */
1437 if (!_lv_suspend_lv(lv
, laopts
, lockfs
, flush_required
)) {
1438 critical_section_dec(cmd
, "failed suspend");
1440 critical_section_dec(cmd
, "failed suspend (pvmove)");
1448 release_vg(lv_pre
->vg
);
1450 lv_release_replicator_vgs(lv
);
1458 * In a cluster, set exclusive to indicate that only one node is using the
1459 * device. Any preloaded tables may then use non-clustered targets.
1461 * Returns success if the device is not active
1463 int lv_suspend_if_active(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
, unsigned exclusive
)
1465 struct lv_activate_opts laopts
= {
1466 .origin_only
= origin_only
,
1467 .exclusive
= exclusive
1470 return _lv_suspend(cmd
, lvid_s
, &laopts
, 0);
1473 /* No longer used */
1475 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1477 return _lv_suspend(cmd, lvid_s, 1);
1486 * @exclusive: This parameter only has an affect in cluster-context.
1487 * It forces local target type to be used (instead of
1488 * cluster-aware type).
1489 * @error_if_not_active
1491 static int _lv_resume(struct cmd_context
*cmd
, const char *lvid_s
,
1492 struct lv_activate_opts
*laopts
, int error_if_not_active
)
1494 struct logical_volume
*lv
;
1501 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1504 if (!lv_is_origin(lv
))
1505 laopts
->origin_only
= 0;
1508 _skip("Resuming %s%s%s.", lv
->name
, laopts
->origin_only
? " without snapshots" : "",
1509 laopts
->revert
? " (reverting)" : "");
1514 log_debug("Resuming LV %s/%s%s%s%s.", lv
->vg
->name
, lv
->name
,
1515 error_if_not_active
? "" : " if active",
1516 laopts
->origin_only
? " without snapshots" : "",
1517 laopts
->revert
? " (reverting)" : "");
1519 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1522 if (!info
.exists
|| !info
.suspended
) {
1523 if (error_if_not_active
)
1526 if (!info
.suspended
)
1527 critical_section_dec(cmd
, "already resumed");
1531 laopts
->read_only
= _passes_readonly_filter(cmd
, lv
);
1533 if (!_lv_activate_lv(lv
, laopts
))
1536 critical_section_dec(cmd
, "resumed");
1538 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1550 * In a cluster, set exclusive to indicate that only one node is using the
1551 * device. Any tables loaded may then use non-clustered targets.
1553 * Returns success if the device is not active
1555 int lv_resume_if_active(struct cmd_context
*cmd
, const char *lvid_s
,
1556 unsigned origin_only
, unsigned exclusive
,
1559 struct lv_activate_opts laopts
= {
1560 .origin_only
= origin_only
,
1561 .exclusive
= exclusive
,
1565 return _lv_resume(cmd
, lvid_s
, &laopts
, 0);
1568 int lv_resume(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
1570 struct lv_activate_opts laopts
= { .origin_only
= origin_only
, };
1572 return _lv_resume(cmd
, lvid_s
, &laopts
, 1);
1575 static int _lv_has_open_snapshots(struct logical_volume
*lv
)
1577 struct lv_segment
*snap_seg
;
1581 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1582 if (!lv_info(lv
->vg
->cmd
, snap_seg
->cow
, 0, &info
, 1, 0)) {
1587 if (info
.exists
&& info
.open_count
) {
1588 log_error("LV %s/%s has open snapshot %s: "
1589 "not deactivating", lv
->vg
->name
, lv
->name
,
1590 snap_seg
->cow
->name
);
1598 int lv_deactivate(struct cmd_context
*cmd
, const char *lvid_s
)
1600 struct logical_volume
*lv
;
1607 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1611 _skip("Deactivating '%s'.", lv
->name
);
1616 log_debug("Deactivating %s/%s.", lv
->vg
->name
, lv
->name
);
1618 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0))
1626 if (lv_is_visible(lv
)) {
1627 if (!lv_check_not_in_use(cmd
, lv
, &info
))
1630 if (lv_is_origin(lv
) && _lv_has_open_snapshots(lv
))
1634 if (!lv_read_replicator_vgs(lv
))
1637 lv_calculate_readahead(lv
, NULL
);
1639 if (!monitor_dev_for_events(cmd
, lv
, NULL
, 0))
1642 critical_section_inc(cmd
, "deactivating");
1643 r
= _lv_deactivate(lv
);
1644 critical_section_dec(cmd
, "deactivated");
1646 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0) || info
.exists
)
1650 lv_release_replicator_vgs(lv
);
1657 /* Test if LV passes filter */
1658 int lv_activation_filter(struct cmd_context
*cmd
, const char *lvid_s
,
1661 struct logical_volume
*lv
;
1664 if (!activation()) {
1669 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1672 if (!_passes_activation_filter(cmd
, lv
)) {
1673 log_verbose("Not activating %s/%s since it does not pass "
1674 "activation filter.", lv
->vg
->name
, lv
->name
);
1686 static int _lv_activate(struct cmd_context
*cmd
, const char *lvid_s
,
1687 struct lv_activate_opts
*laopts
, int filter
)
1689 struct logical_volume
*lv
;
1696 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1699 if (filter
&& !_passes_activation_filter(cmd
, lv
)) {
1700 log_error("Not activating %s/%s since it does not pass "
1701 "activation filter.", lv
->vg
->name
, lv
->name
);
1705 if ((!lv
->vg
->cmd
->partial_activation
) && (lv
->status
& PARTIAL_LV
)) {
1706 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1711 if (lv_has_unknown_segments(lv
)) {
1712 log_error("Refusing activation of LV %s containing "
1713 "an unrecognised segment.", lv
->name
);
1718 _skip("Activating '%s'.", lv
->name
);
1724 laopts
->read_only
= _passes_readonly_filter(cmd
, lv
);
1726 log_debug("Activating %s/%s%s%s.", lv
->vg
->name
, lv
->name
,
1727 laopts
->exclusive
? " exclusively" : "",
1728 laopts
->read_only
? " read-only" : "");
1730 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
1736 if (info
.exists
&& !info
.suspended
&& info
.live_table
&&
1737 (info
.read_only
== read_only_lv(lv
, laopts
))) {
1742 if (!lv_read_replicator_vgs(lv
))
1745 lv_calculate_readahead(lv
, NULL
);
1747 critical_section_inc(cmd
, "activating");
1748 if (!(r
= _lv_activate_lv(lv
, laopts
)))
1750 critical_section_dec(cmd
, "activated");
1752 if (r
&& !monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1757 lv_release_replicator_vgs(lv
);
1765 int lv_activate(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1767 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1769 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 0))
1775 /* Activate LV only if it passes filter */
1776 int lv_activate_with_filter(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1778 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1780 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 1))
1786 int lv_mknodes(struct cmd_context
*cmd
, const struct logical_volume
*lv
)
1791 r
= dm_mknodes(NULL
);
1799 r
= dev_manager_mknodes(lv
);
1807 * Does PV use VG somewhere in its construction?
1808 * Returns 1 on failure.
1810 int pv_uses_vg(struct physical_volume
*pv
,
1811 struct volume_group
*vg
)
1816 if (!dm_is_dm_major(MAJOR(pv
->dev
->dev
)))
1819 return dev_manager_device_uses_vg(pv
->dev
, vg
);
1822 void activation_release(void)
1824 dev_manager_release();
1827 void activation_exit(void)