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
, int use_layer
,
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
, int use_layer
,
160 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
164 int lv_snapshot_percent(const struct logical_volume
*lv
, percent_t
*percent
)
168 int lv_mirror_percent(struct cmd_context
*cmd
, const struct logical_volume
*lv
,
169 int wait
, percent_t
*percent
, uint32_t *event_nr
)
173 int lvs_in_vg_activated(struct volume_group
*vg
)
177 int lvs_in_vg_opened(const struct volume_group
*vg
)
182 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
187 int lv_suspend_if_active(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
191 int lv_resume(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
195 int lv_resume_if_active(struct cmd_context
*cmd
, const char *lvid_s
,
196 unsigned origin_only
, unsigned exclusive
, unsigned revert
)
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
)
217 int lv_mknodes(struct cmd_context
*cmd
, const struct logical_volume
*lv
)
221 int lv_send_message(const struct logical_volume
*lv
, const char *message
)
225 int pv_uses_vg(struct physical_volume
*pv
,
226 struct volume_group
*vg
)
230 void activation_release(void)
233 void activation_exit(void)
237 int lv_is_active(struct logical_volume
*lv
)
241 int lv_is_active_but_not_locally(struct logical_volume
*lv
)
245 int lv_is_active_exclusive(struct logical_volume
*lv
)
249 int lv_is_active_exclusive_locally(struct logical_volume
*lv
)
253 int lv_is_active_exclusive_remotely(struct logical_volume
*lv
)
258 int lv_check_transient(struct logical_volume
*lv
)
262 int monitor_dev_for_events(struct cmd_context
*cmd
, struct logical_volume
*lv
,
263 struct lv_activate_opts
*laopts
, int monitor
)
267 #else /* DEVMAPPER_SUPPORT */
269 static int _activation
= 1;
271 void set_activation(int act
)
273 if (act
== _activation
)
278 log_verbose("Activation enabled. Device-mapper kernel "
279 "driver will be used.");
281 log_warn("WARNING: Activation disabled. No device-mapper "
282 "interaction will be attempted.");
290 static int _passes_volumes_filter(struct cmd_context
*cmd
,
291 struct logical_volume
*lv
,
292 const struct dm_config_node
*cn
,
293 const char *config_path
)
295 const struct dm_config_value
*cv
;
297 static char path
[PATH_MAX
];
299 log_verbose("%s configuration setting defined: "
300 "Checking the list to match %s/%s",
301 config_path
, lv
->vg
->name
, lv
->name
);
303 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
304 if (cv
->type
!= DM_CFG_STRING
) {
305 log_error("Ignoring invalid string in config file %s",
311 log_error("Ignoring empty string in config file %s",
321 log_error("Ignoring empty tag in config file "
325 /* If any host tag matches any LV or VG tag, activate */
326 if (!strcmp(str
, "*")) {
327 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
)
328 || str_list_match_list(&cmd
->tags
,
329 &lv
->vg
->tags
, NULL
))
334 /* If supplied tag matches LV or VG tag, activate */
335 if (str_list_match_item(&lv
->tags
, str
) ||
336 str_list_match_item(&lv
->vg
->tags
, str
))
341 if (!strchr(str
, '/')) {
342 /* vgname supplied */
343 if (!strcmp(str
, lv
->vg
->name
))
349 if (dm_snprintf(path
, sizeof(path
), "%s/%s", lv
->vg
->name
,
351 log_error("dm_snprintf error from %s/%s", lv
->vg
->name
,
355 if (!strcmp(path
, str
))
359 log_verbose("No item supplied in %s configuration setting "
360 "matches %s/%s", config_path
, lv
->vg
->name
, lv
->name
);
365 static int _passes_activation_filter(struct cmd_context
*cmd
,
366 struct logical_volume
*lv
)
368 const struct dm_config_node
*cn
;
370 if (!(cn
= find_config_tree_node(cmd
, "activation/volume_list"))) {
371 log_verbose("activation/volume_list configuration setting "
372 "not defined: Checking only host tags for %s/%s",
373 lv
->vg
->name
, lv
->name
);
375 /* If no host tags defined, activate */
376 if (dm_list_empty(&cmd
->tags
))
379 /* If any host tag matches any LV or VG tag, activate */
380 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
) ||
381 str_list_match_list(&cmd
->tags
, &lv
->vg
->tags
, NULL
))
384 log_verbose("No host tag matches %s/%s",
385 lv
->vg
->name
, lv
->name
);
391 return _passes_volumes_filter(cmd
, lv
, cn
, "activation/volume_list");
394 static int _passes_readonly_filter(struct cmd_context
*cmd
,
395 struct logical_volume
*lv
)
397 const struct dm_config_node
*cn
;
399 if (!(cn
= find_config_tree_node(cmd
, "activation/read_only_volume_list")))
402 return _passes_volumes_filter(cmd
, lv
, cn
, "activation/read_only_volume_list");
405 int library_version(char *version
, size_t size
)
410 return dm_get_library_version(version
, size
);
413 int driver_version(char *version
, size_t size
)
418 log_very_verbose("Getting driver version");
420 return dm_driver_version(version
, size
);
423 int target_version(const char *target_name
, uint32_t *maj
,
424 uint32_t *min
, uint32_t *patchlevel
)
428 struct dm_versions
*target
, *last_target
;
430 log_very_verbose("Getting target version for %s", target_name
);
431 if (!(dmt
= dm_task_create(DM_DEVICE_LIST_VERSIONS
)))
434 if (activation_checks() && !dm_task_enable_checks(dmt
))
437 if (!dm_task_run(dmt
)) {
438 log_debug("Failed to get %s target version", target_name
);
439 /* Assume this was because LIST_VERSIONS isn't supported */
443 target
= dm_task_get_versions(dmt
);
446 last_target
= target
;
448 if (!strcmp(target_name
, target
->name
)) {
450 *maj
= target
->version
[0];
451 *min
= target
->version
[1];
452 *patchlevel
= target
->version
[2];
456 target
= (struct dm_versions
*)((char *) target
+ target
->next
);
457 } while (last_target
!= target
);
460 dm_task_destroy(dmt
);
465 int lvm_dm_prefix_check(int major
, int minor
, const char *prefix
)
471 if (!(dmt
= dm_task_create(DM_DEVICE_STATUS
)))
474 if (!dm_task_set_minor(dmt
, minor
) ||
475 !dm_task_set_major(dmt
, major
) ||
477 !(uuid
= dm_task_get_uuid(dmt
))) {
478 dm_task_destroy(dmt
);
482 r
= strncasecmp(uuid
, prefix
, strlen(prefix
));
483 dm_task_destroy(dmt
);
488 int module_present(struct cmd_context
*cmd
, const char *target_name
)
495 if (dm_snprintf(module
, sizeof(module
), "dm-%s", target_name
) < 0) {
496 log_error("module_present module name too long: %s",
501 argv
[0] = MODPROBE_CMD
;
505 ret
= exec_cmd(cmd
, argv
, NULL
, 0);
510 int target_present(struct cmd_context
*cmd
, const char *target_name
,
513 uint32_t maj
, min
, patchlevel
;
520 if (target_version(target_name
, &maj
, &min
, &patchlevel
))
523 if (!module_present(cmd
, target_name
))
528 return target_version(target_name
, &maj
, &min
, &patchlevel
);
532 * Returns 1 if info structure populated, else 0 on failure.
534 int lv_info(struct cmd_context
*cmd
, const struct logical_volume
*lv
, int use_layer
,
535 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
537 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 (use_layer
&& lv_is_thin_pool(lv
))
557 else if (use_layer
&& lv_is_origin(lv
))
562 if (!dev_manager_info(lv
->vg
->cmd
->mem
, lv
, layer
, with_open_count
,
563 with_read_ahead
, &dminfo
, &info
->read_ahead
))
566 info
->exists
= dminfo
.exists
;
567 info
->suspended
= dminfo
.suspended
;
568 info
->open_count
= dminfo
.open_count
;
569 info
->major
= dminfo
.major
;
570 info
->minor
= dminfo
.minor
;
571 info
->read_only
= dminfo
.read_only
;
572 info
->live_table
= dminfo
.live_table
;
573 info
->inactive_table
= dminfo
.inactive_table
;
578 int lv_info_by_lvid(struct cmd_context
*cmd
, const char *lvid_s
, int use_layer
,
579 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
582 struct logical_volume
*lv
;
584 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
587 r
= lv_info(cmd
, lv
, use_layer
, info
, with_open_count
, with_read_ahead
);
593 int lv_check_not_in_use(struct cmd_context
*cmd
__attribute__((unused
)),
594 struct logical_volume
*lv
, struct lvinfo
*info
)
599 /* If sysfs is not used, use open_count information only. */
600 if (!*dm_sysfs_dir()) {
601 if (info
->open_count
) {
602 log_error("Logical volume %s/%s in use.",
603 lv
->vg
->name
, lv
->name
);
610 if (dm_device_has_holders(info
->major
, info
->minor
)) {
611 log_error("Logical volume %s/%s is used by another device.",
612 lv
->vg
->name
, lv
->name
);
616 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
617 log_error("Logical volume %s/%s contains a filesystem in use.",
618 lv
->vg
->name
, lv
->name
);
626 * Returns 1 if percent set, else 0 on failure.
628 int lv_check_transient(struct logical_volume
*lv
)
631 struct dev_manager
*dm
;
636 log_debug("Checking transient status for LV %s/%s", lv
->vg
->name
, lv
->name
);
638 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
641 if (!(r
= dev_manager_transient(dm
, lv
)))
644 dev_manager_destroy(dm
);
650 * Returns 1 if percent set, else 0 on failure.
652 int lv_snapshot_percent(const struct logical_volume
*lv
, percent_t
*percent
)
655 struct dev_manager
*dm
;
660 log_debug("Checking snapshot percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
662 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
665 if (!(r
= dev_manager_snapshot_percent(dm
, lv
, percent
)))
668 dev_manager_destroy(dm
);
673 /* FIXME Merge with snapshot_percent */
674 int lv_mirror_percent(struct cmd_context
*cmd
, const struct logical_volume
*lv
,
675 int wait
, percent_t
*percent
, uint32_t *event_nr
)
678 struct dev_manager
*dm
;
681 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
682 * it should be considered in-sync. */
683 if (dm_list_size(&lv
->segments
) == 1 && first_seg(lv
)->area_count
== 1) {
684 *percent
= PERCENT_100
;
691 log_debug("Checking mirror percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
693 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
699 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
702 if (!(r
= dev_manager_mirror_percent(dm
, lv
, wait
, percent
, event_nr
)))
705 dev_manager_destroy(dm
);
710 int lv_raid_percent(const struct logical_volume
*lv
, percent_t
*percent
)
712 return lv_mirror_percent(lv
->vg
->cmd
, lv
, 0, percent
, NULL
);
716 * Returns data or metadata percent usage, depends on metadata 0/1.
717 * Returns 1 if percent set, else 0 on failure.
719 int lv_thin_pool_percent(const struct logical_volume
*lv
, int metadata
,
723 struct dev_manager
*dm
;
728 log_debug("Checking thin %sdata percent for LV %s/%s",
729 (metadata
) ? "meta" : "", lv
->vg
->name
, lv
->name
);
731 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
734 if (!(r
= dev_manager_thin_pool_percent(dm
, lv
, metadata
, percent
)))
737 dev_manager_destroy(dm
);
743 * Returns 1 if percent set, else 0 on failure.
745 int lv_thin_percent(const struct logical_volume
*lv
,
746 int mapped
, percent_t
*percent
)
749 struct dev_manager
*dm
;
754 log_debug("Checking thin percent for LV %s/%s",
755 lv
->vg
->name
, lv
->name
);
757 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
760 if (!(r
= dev_manager_thin_percent(dm
, lv
, mapped
, percent
)))
763 dev_manager_destroy(dm
);
769 * Returns 1 if transaction_id set, else 0 on failure.
771 int lv_thin_pool_transaction_id(const struct logical_volume
*lv
,
772 uint64_t *transaction_id
)
775 struct dev_manager
*dm
;
776 struct dm_status_thin_pool
*status
;
781 log_debug("Checking thin percent for LV %s/%s",
782 lv
->vg
->name
, lv
->name
);
784 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
787 if (!(r
= dev_manager_thin_pool_status(dm
, lv
, &status
)))
790 *transaction_id
= status
->transaction_id
;
792 dev_manager_destroy(dm
);
797 static int _lv_active(struct cmd_context
*cmd
, struct logical_volume
*lv
)
801 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0)) {
809 static int _lv_open_count(struct cmd_context
*cmd
, struct logical_volume
*lv
)
813 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0)) {
818 return info
.open_count
;
821 static int _lv_activate_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
)
824 struct dev_manager
*dm
;
826 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
829 if (!(r
= dev_manager_activate(dm
, lv
, laopts
)))
832 dev_manager_destroy(dm
);
836 static int _lv_preload(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
840 struct dev_manager
*dm
;
841 int old_readonly
= laopts
->read_only
;
843 laopts
->read_only
= _passes_readonly_filter(lv
->vg
->cmd
, lv
);
845 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
848 if (!(r
= dev_manager_preload(dm
, lv
, laopts
, flush_required
)))
851 dev_manager_destroy(dm
);
853 laopts
->read_only
= old_readonly
;
858 static int _lv_deactivate(struct logical_volume
*lv
)
861 struct dev_manager
*dm
;
863 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
866 if (!(r
= dev_manager_deactivate(dm
, lv
)))
869 dev_manager_destroy(dm
);
873 static int _lv_suspend_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
874 int lockfs
, int flush_required
)
877 struct dev_manager
*dm
;
879 laopts
->read_only
= _passes_readonly_filter(lv
->vg
->cmd
, lv
);
882 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
883 * device directly, we don't want to touch the devices that use it.
885 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
888 if (!(r
= dev_manager_suspend(dm
, lv
, laopts
, lockfs
, flush_required
)))
891 dev_manager_destroy(dm
);
896 * These two functions return the number of visible LVs in the state,
897 * or -1 on error. FIXME Check this.
899 int lvs_in_vg_activated(struct volume_group
*vg
)
907 dm_list_iterate_items(lvl
, &vg
->lvs
)
908 if (lv_is_visible(lvl
->lv
))
909 count
+= (_lv_active(vg
->cmd
, lvl
->lv
) == 1);
911 log_debug("Counted %d active LVs in VG %s", count
, vg
->name
);
916 int lvs_in_vg_opened(const struct volume_group
*vg
)
918 const struct lv_list
*lvl
;
924 dm_list_iterate_items(lvl
, &vg
->lvs
)
925 if (lv_is_visible(lvl
->lv
))
926 count
+= (_lv_open_count(vg
->cmd
, lvl
->lv
) > 0);
928 log_debug("Counted %d open LVs in VG %s", count
, vg
->name
);
935 * @lv: logical volume being queried
936 * @locally: set if active locally (when provided)
937 * @exclusive: set if active exclusively (when provided)
939 * Determine whether an LV is active locally or in a cluster.
940 * In addition to the return code which indicates whether or
941 * not the LV is active somewhere, two other values are set
942 * to yield more information about the status of the activation:
943 * return locally exclusively status
944 * ====== ======= =========== ======
946 * 1 0 0 active remotely
947 * 1 0 1 exclusive remotely
948 * 1 1 0 active locally and possibly remotely
949 * 1 1 1 exclusive locally (or local && !cluster)
950 * The VG lock must be held to call this function.
954 static int _lv_is_active(struct logical_volume
*lv
,
955 int *locally
, int *exclusive
)
957 int r
, l
, e
; /* remote, local, and exclusive */
961 if (_lv_active(lv
->vg
->cmd
, lv
))
964 if (!vg_is_clustered(lv
->vg
)) {
966 e
= 1; /* exclusive by definition */
970 /* Active locally, and the caller doesn't care about exclusive */
974 if ((r
= remote_lock_held(lv
->lvid
.s
, &e
)) >= 0)
978 * If lock query is not supported (due to interfacing with old
979 * code), then we cannot evaluate exclusivity properly.
981 * Old users of this function will never be affected by this,
982 * since they are only concerned about active vs. not active.
983 * New users of this function who specifically ask for 'exclusive'
984 * will be given an error message.
986 log_error("Unable to determine exclusivity of %s", lv
->name
);
991 * We used to attempt activate_lv_excl_local(lv->vg->cmd, lv) here,
992 * but it's unreliable.
1001 log_very_verbose("%s/%s is %sactive%s%s",
1002 lv
->vg
->name
, lv
->name
,
1003 (r
|| l
) ? "" : "not ",
1004 (exclusive
&& e
) ? " exclusive" : "",
1005 e
? (l
? " locally" : " remotely") : "");
1010 int lv_is_active(struct logical_volume
*lv
)
1012 return _lv_is_active(lv
, NULL
, NULL
);
1015 int lv_is_active_but_not_locally(struct logical_volume
*lv
)
1018 return _lv_is_active(lv
, &l
, NULL
) && !l
;
1021 int lv_is_active_exclusive(struct logical_volume
*lv
)
1025 return _lv_is_active(lv
, NULL
, &e
) && e
;
1028 int lv_is_active_exclusive_locally(struct logical_volume
*lv
)
1032 return _lv_is_active(lv
, &l
, &e
) && l
&& e
;
1035 int lv_is_active_exclusive_remotely(struct logical_volume
*lv
)
1039 return _lv_is_active(lv
, &l
, &e
) && !l
&& e
;
1043 static struct dm_event_handler
*_create_dm_event_handler(struct cmd_context
*cmd
, const char *dmuuid
, const char *dso
,
1044 const int timeout
, enum dm_event_mask mask
)
1046 struct dm_event_handler
*dmevh
;
1048 if (!(dmevh
= dm_event_handler_create()))
1051 if (dm_event_handler_set_dmeventd_path(dmevh
, find_config_tree_str(cmd
, "dmeventd/executable", NULL
)))
1054 if (dm_event_handler_set_dso(dmevh
, dso
))
1057 if (dm_event_handler_set_uuid(dmevh
, dmuuid
))
1060 dm_event_handler_set_timeout(dmevh
, timeout
);
1061 dm_event_handler_set_event_mask(dmevh
, mask
);
1066 dm_event_handler_destroy(dmevh
);
1070 char *get_monitor_dso_path(struct cmd_context
*cmd
, const char *libpath
)
1074 if (!(path
= dm_pool_alloc(cmd
->mem
, PATH_MAX
))) {
1075 log_error("Failed to allocate dmeventd library path.");
1079 get_shared_library_path(cmd
, libpath
, path
, PATH_MAX
);
1084 static char *_build_target_uuid(struct cmd_context
*cmd
, struct logical_volume
*lv
)
1088 if (lv_is_thin_pool(lv
))
1089 layer
= "tpool"; /* Monitor "tpool" for the "thin pool". */
1090 else if (lv_is_origin(lv
))
1091 layer
= "real"; /* Monitor "real" for "snapshot-origin". */
1095 return build_dm_uuid(cmd
->mem
, lv
->lvid
.s
, layer
);
1098 int target_registered_with_dmeventd(struct cmd_context
*cmd
, const char *dso
,
1099 struct logical_volume
*lv
, int *pending
)
1102 enum dm_event_mask evmask
= 0;
1103 struct dm_event_handler
*dmevh
;
1109 if (!(uuid
= _build_target_uuid(cmd
, lv
)))
1112 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, 0, DM_EVENT_ALL_ERRORS
)))
1115 if (dm_event_get_registered_device(dmevh
, 0)) {
1116 dm_event_handler_destroy(dmevh
);
1120 evmask
= dm_event_handler_get_event_mask(dmevh
);
1121 if (evmask
& DM_EVENT_REGISTRATION_PENDING
) {
1123 evmask
&= ~DM_EVENT_REGISTRATION_PENDING
;
1126 dm_event_handler_destroy(dmevh
);
1131 int target_register_events(struct cmd_context
*cmd
, const char *dso
, struct logical_volume
*lv
,
1132 int evmask
__attribute__((unused
)), int set
, int timeout
)
1135 struct dm_event_handler
*dmevh
;
1141 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
1142 if (!(uuid
= _build_target_uuid(cmd
, lv
)))
1145 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, timeout
,
1146 DM_EVENT_ALL_ERRORS
| (timeout
? DM_EVENT_TIMEOUT
: 0))))
1149 r
= set
? dm_event_register_handler(dmevh
) : dm_event_unregister_handler(dmevh
);
1151 dm_event_handler_destroy(dmevh
);
1156 log_info("%s %s for events", set
? "Monitored" : "Unmonitored", uuid
);
1164 * Returns 0 if an attempt to (un)monitor the device failed.
1165 * Returns 1 otherwise.
1167 int monitor_dev_for_events(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1168 const struct lv_activate_opts
*laopts
, int monitor
)
1171 int i
, pending
= 0, monitored
;
1173 struct dm_list
*tmp
, *snh
, *snht
;
1174 struct lv_segment
*seg
;
1175 struct lv_segment
*log_seg
;
1176 int (*monitor_fn
) (struct lv_segment
*s
, int e
);
1178 static const struct lv_activate_opts zlaopts
= { 0 };
1183 /* skip dmeventd code altogether */
1184 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE
)
1188 * Nothing to do if dmeventd configured not to be used.
1190 if (monitor
&& !dmeventd_monitor_mode())
1194 * In case of a snapshot device, we monitor lv->snapshot->lv,
1195 * not the actual LV itself.
1197 if (lv_is_cow(lv
) && (laopts
->no_merging
|| !lv_is_merging_cow(lv
)))
1198 return monitor_dev_for_events(cmd
, lv
->snapshot
->lv
, NULL
, monitor
);
1201 * In case this LV is a snapshot origin, we instead monitor
1202 * each of its respective snapshots. The origin itself may
1203 * also need to be monitored if it is a mirror, for example.
1205 if (!laopts
->origin_only
&& lv_is_origin(lv
))
1206 dm_list_iterate_safe(snh
, snht
, &lv
->snapshot_segs
)
1207 if (!monitor_dev_for_events(cmd
, dm_list_struct_base(snh
,
1208 struct lv_segment
, origin_list
)->cow
, NULL
, monitor
))
1212 * If the volume is mirrored and its log is also mirrored, monitor
1213 * the log volume as well.
1215 if ((seg
= first_seg(lv
)) != NULL
&& seg
->log_lv
!= NULL
&&
1216 (log_seg
= first_seg(seg
->log_lv
)) != NULL
&&
1217 seg_is_mirrored(log_seg
))
1218 if (!monitor_dev_for_events(cmd
, seg
->log_lv
, NULL
, monitor
))
1221 dm_list_iterate(tmp
, &lv
->segments
) {
1222 seg
= dm_list_item(tmp
, struct lv_segment
);
1224 /* Recurse for AREA_LV */
1225 for (s
= 0; s
< seg
->area_count
; s
++) {
1226 if (seg_type(seg
, s
) != AREA_LV
)
1228 if (!monitor_dev_for_events(cmd
, seg_lv(seg
, s
), NULL
,
1230 log_error("Failed to %smonitor %s",
1231 monitor
? "" : "un",
1232 seg_lv(seg
, s
)->name
);
1237 if (!seg_monitored(seg
) || (seg
->status
& PVMOVE
))
1242 /* Check monitoring status */
1243 if (seg
->segtype
->ops
->target_monitored
)
1244 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1246 continue; /* segtype doesn't support registration */
1249 * FIXME: We should really try again if pending
1251 monitored
= (pending
) ? 0 : monitored
;
1255 log_verbose("%s/%s already monitored.", lv
->vg
->name
, lv
->name
);
1256 else if (seg
->segtype
->ops
->target_monitor_events
)
1257 monitor_fn
= seg
->segtype
->ops
->target_monitor_events
;
1260 log_verbose("%s/%s already not monitored.", lv
->vg
->name
, lv
->name
);
1261 else if (seg
->segtype
->ops
->target_unmonitor_events
)
1262 monitor_fn
= seg
->segtype
->ops
->target_unmonitor_events
;
1265 /* Do [un]monitor */
1269 log_verbose("%sonitoring %s/%s%s", monitor
? "M" : "Not m", lv
->vg
->name
, lv
->name
,
1270 test_mode() ? " [Test mode: skipping this]" : "");
1272 /* FIXME Test mode should really continue a bit further. */
1276 /* FIXME specify events */
1277 if (!monitor_fn(seg
, 0)) {
1278 log_error("%s/%s: %s segment monitoring function failed.",
1279 lv
->vg
->name
, lv
->name
, seg
->segtype
->name
);
1283 /* Check [un]monitor results */
1284 /* Try a couple times if pending, but not forever... */
1285 for (i
= 0; i
< 10; i
++) {
1287 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1289 (!monitored
&& monitor
) ||
1290 (monitored
&& !monitor
))
1291 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
1292 lv
->vg
->name
, lv
->name
, monitor
? "" : "un");
1299 r
= (monitored
&& monitor
) || (!monitored
&& !monitor
);
1308 struct detached_lv_data
{
1309 struct logical_volume
*lv_pre
;
1310 struct lv_activate_opts
*laopts
;
1311 int *flush_required
;
1314 static int _preload_detached_lv(struct cmd_context
*cmd
, struct logical_volume
*lv
, void *data
)
1316 struct detached_lv_data
*detached
= data
;
1317 struct lv_list
*lvl_pre
;
1319 if ((lvl_pre
= find_lv_in_vg(detached
->lv_pre
->vg
, lv
->name
))) {
1320 if (lv_is_visible(lvl_pre
->lv
) && lv_is_active(lv
) && (!lv_is_cow(lv
) || !lv_is_cow(lvl_pre
->lv
)) &&
1321 !_lv_preload(lvl_pre
->lv
, detached
->laopts
, detached
->flush_required
))
1328 static int _lv_suspend(struct cmd_context
*cmd
, const char *lvid_s
,
1329 struct lv_activate_opts
*laopts
, int error_if_not_suspended
)
1331 struct logical_volume
*lv
= NULL
, *lv_pre
= NULL
, *pvmove_lv
= NULL
;
1332 struct lv_list
*lvl_pre
;
1333 struct seg_list
*sl
;
1334 struct lv_segment
*snap_seg
;
1336 int r
= 0, lockfs
= 0, flush_required
= 0;
1337 struct detached_lv_data detached
;
1342 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1345 /* Use precommitted metadata if present */
1346 if (!(lv_pre
= lv_from_lvid(cmd
, lvid_s
, 1)))
1349 /* Ignore origin_only unless LV is origin in both old and new metadata */
1350 if (!lv_is_thin_volume(lv
) && !(lv_is_origin(lv
) && lv_is_origin(lv_pre
)))
1351 laopts
->origin_only
= 0;
1354 _skip("Suspending %s%s.", lv
->name
, laopts
->origin_only
? " origin without snapshots" : "");
1359 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1362 if (!info
.exists
|| info
.suspended
) {
1363 if (!error_if_not_suspended
) {
1366 critical_section_inc(cmd
, "already suspended");
1371 if (!lv_read_replicator_vgs(lv
))
1374 lv_calculate_readahead(lv
, NULL
);
1377 * Preload devices for the LV.
1378 * If the PVMOVE LV is being removed, it's only present in the old
1379 * metadata and not the new, so we must explicitly add the new
1380 * tables for all the changed LVs here, as the relationships
1381 * are not found by walking the new metadata.
1383 if (!(lv_pre
->status
& LOCKED
) &&
1384 (lv
->status
& LOCKED
) &&
1385 (pvmove_lv
= find_pvmove_lv_in_lv(lv
))) {
1386 /* Preload all the LVs above the PVMOVE LV */
1387 dm_list_iterate_items(sl
, &pvmove_lv
->segs_using_this_lv
) {
1388 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, sl
->seg
->lv
->name
))) {
1389 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", sl
->seg
->lv
->name
);
1392 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1395 /* Now preload the PVMOVE LV itself */
1396 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, pvmove_lv
->name
))) {
1397 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", pvmove_lv
->name
);
1400 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1403 if (!_lv_preload(lv_pre
, laopts
, &flush_required
))
1404 /* FIXME Revert preloading */
1408 * Search for existing LVs that have become detached and preload them.
1410 detached
.lv_pre
= lv_pre
;
1411 detached
.laopts
= laopts
;
1412 detached
.flush_required
= &flush_required
;
1414 if (!for_each_sub_lv(cmd
, lv
, &_preload_detached_lv
, &detached
))
1418 * Preload any snapshots that are being removed.
1420 if (!laopts
->origin_only
&& lv_is_origin(lv
)) {
1421 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1422 if (!(lvl_pre
= find_lv_in_vg_by_lvid(lv_pre
->vg
, &snap_seg
->cow
->lvid
))) {
1423 log_error(INTERNAL_ERROR
"LV %s (%s) missing from preload metadata",
1424 snap_seg
->cow
->name
, snap_seg
->cow
->lvid
.id
[1].uuid
);
1427 if (!lv_is_cow(lvl_pre
->lv
) &&
1428 !_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1434 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 0))
1435 /* FIXME Consider aborting here */
1438 critical_section_inc(cmd
, "suspending");
1440 critical_section_inc(cmd
, "suspending pvmove LV");
1442 if (!laopts
->origin_only
&&
1443 (lv_is_origin(lv_pre
) || lv_is_cow(lv_pre
)))
1447 * Suspending an LV directly above a PVMOVE LV also
1448 * suspends other LVs using that same PVMOVE LV.
1449 * FIXME Remove this and delay the 'clear node' until
1450 * after the code knows whether there's a different
1451 * inactive table to load or not instead so lv_suspend
1452 * can be called separately for each LV safely.
1454 if ((lv_pre
->vg
->status
& PRECOMMITTED
) &&
1455 (lv_pre
->status
& LOCKED
) && find_pvmove_lv_in_lv(lv_pre
)) {
1456 if (!_lv_suspend_lv(lv_pre
, laopts
, lockfs
, flush_required
)) {
1457 critical_section_dec(cmd
, "failed precommitted suspend");
1459 critical_section_dec(cmd
, "failed precommitted suspend (pvmove)");
1463 /* Normal suspend */
1464 if (!_lv_suspend_lv(lv
, laopts
, lockfs
, flush_required
)) {
1465 critical_section_dec(cmd
, "failed suspend");
1467 critical_section_dec(cmd
, "failed suspend (pvmove)");
1475 release_vg(lv_pre
->vg
);
1477 lv_release_replicator_vgs(lv
);
1485 * In a cluster, set exclusive to indicate that only one node is using the
1486 * device. Any preloaded tables may then use non-clustered targets.
1488 * Returns success if the device is not active
1490 int lv_suspend_if_active(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
, unsigned exclusive
)
1492 struct lv_activate_opts laopts
= {
1493 .origin_only
= origin_only
,
1494 .exclusive
= exclusive
1497 return _lv_suspend(cmd
, lvid_s
, &laopts
, 0);
1500 /* No longer used */
1502 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1504 return _lv_suspend(cmd, lvid_s, 1);
1508 static int _lv_resume(struct cmd_context
*cmd
, const char *lvid_s
,
1509 struct lv_activate_opts
*laopts
, int error_if_not_active
)
1511 struct logical_volume
*lv
;
1514 int messages_only
= 0;
1519 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1522 if (lv_is_thin_pool(lv
) && laopts
->origin_only
)
1525 if (!lv_is_origin(lv
))
1526 laopts
->origin_only
= 0;
1529 _skip("Resuming %s%s%s.", lv
->name
, laopts
->origin_only
? " without snapshots" : "",
1530 laopts
->revert
? " (reverting)" : "");
1535 log_debug("Resuming LV %s/%s%s%s%s.", lv
->vg
->name
, lv
->name
,
1536 error_if_not_active
? "" : " if active",
1537 laopts
->origin_only
? " without snapshots" : "",
1538 laopts
->revert
? " (reverting)" : "");
1540 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1543 if (!info
.exists
|| !(info
.suspended
|| messages_only
)) {
1544 if (error_if_not_active
)
1547 if (!info
.suspended
)
1548 critical_section_dec(cmd
, "already resumed");
1552 laopts
->read_only
= _passes_readonly_filter(cmd
, lv
);
1554 if (!_lv_activate_lv(lv
, laopts
))
1557 critical_section_dec(cmd
, "resumed");
1559 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1571 * In a cluster, set exclusive to indicate that only one node is using the
1572 * device. Any tables loaded may then use non-clustered targets.
1575 * @exclusive This parameter only has an affect in cluster-context.
1576 * It forces local target type to be used (instead of
1577 * cluster-aware type).
1578 * Returns success if the device is not active
1580 int lv_resume_if_active(struct cmd_context
*cmd
, const char *lvid_s
,
1581 unsigned origin_only
, unsigned exclusive
,
1584 struct lv_activate_opts laopts
= {
1585 .origin_only
= origin_only
,
1586 .exclusive
= exclusive
,
1590 return _lv_resume(cmd
, lvid_s
, &laopts
, 0);
1593 int lv_resume(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
1595 struct lv_activate_opts laopts
= { .origin_only
= origin_only
, };
1597 return _lv_resume(cmd
, lvid_s
, &laopts
, 1);
1600 static int _lv_has_open_snapshots(struct logical_volume
*lv
)
1602 struct lv_segment
*snap_seg
;
1606 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1607 if (!lv_info(lv
->vg
->cmd
, snap_seg
->cow
, 0, &info
, 1, 0)) {
1612 if (info
.exists
&& info
.open_count
) {
1613 log_error("LV %s/%s has open snapshot %s: "
1614 "not deactivating", lv
->vg
->name
, lv
->name
,
1615 snap_seg
->cow
->name
);
1623 int lv_deactivate(struct cmd_context
*cmd
, const char *lvid_s
)
1625 struct logical_volume
*lv
;
1632 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1636 _skip("Deactivating '%s'.", lv
->name
);
1641 log_debug("Deactivating %s/%s.", lv
->vg
->name
, lv
->name
);
1643 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0))
1651 if (lv_is_visible(lv
)) {
1652 if (!lv_check_not_in_use(cmd
, lv
, &info
))
1655 if (lv_is_origin(lv
) && _lv_has_open_snapshots(lv
))
1659 if (!lv_read_replicator_vgs(lv
))
1662 lv_calculate_readahead(lv
, NULL
);
1664 if (!monitor_dev_for_events(cmd
, lv
, NULL
, 0))
1667 critical_section_inc(cmd
, "deactivating");
1668 r
= _lv_deactivate(lv
);
1669 critical_section_dec(cmd
, "deactivated");
1671 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0) || info
.exists
)
1675 lv_release_replicator_vgs(lv
);
1682 /* Test if LV passes filter */
1683 int lv_activation_filter(struct cmd_context
*cmd
, const char *lvid_s
,
1686 struct logical_volume
*lv
;
1689 if (!activation()) {
1694 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1697 if (!_passes_activation_filter(cmd
, lv
)) {
1698 log_verbose("Not activating %s/%s since it does not pass "
1699 "activation filter.", lv
->vg
->name
, lv
->name
);
1711 static int _lv_activate(struct cmd_context
*cmd
, const char *lvid_s
,
1712 struct lv_activate_opts
*laopts
, int filter
)
1714 struct logical_volume
*lv
;
1721 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1724 if (filter
&& !_passes_activation_filter(cmd
, lv
)) {
1725 log_error("Not activating %s/%s since it does not pass "
1726 "activation filter.", lv
->vg
->name
, lv
->name
);
1730 if ((!lv
->vg
->cmd
->partial_activation
) && (lv
->status
& PARTIAL_LV
)) {
1731 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1736 if (lv_has_unknown_segments(lv
)) {
1737 log_error("Refusing activation of LV %s containing "
1738 "an unrecognised segment.", lv
->name
);
1743 _skip("Activating '%s'.", lv
->name
);
1749 laopts
->read_only
= _passes_readonly_filter(cmd
, lv
);
1751 log_debug("Activating %s/%s%s%s.", lv
->vg
->name
, lv
->name
,
1752 laopts
->exclusive
? " exclusively" : "",
1753 laopts
->read_only
? " read-only" : "");
1755 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
1761 if (info
.exists
&& !info
.suspended
&& info
.live_table
&&
1762 (info
.read_only
== read_only_lv(lv
, laopts
))) {
1767 if (!lv_read_replicator_vgs(lv
))
1770 lv_calculate_readahead(lv
, NULL
);
1772 critical_section_inc(cmd
, "activating");
1773 if (!(r
= _lv_activate_lv(lv
, laopts
)))
1775 critical_section_dec(cmd
, "activated");
1777 if (r
&& !monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1782 lv_release_replicator_vgs(lv
);
1790 int lv_activate(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1792 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1794 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 0))
1800 /* Activate LV only if it passes filter */
1801 int lv_activate_with_filter(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1803 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1805 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 1))
1811 int lv_mknodes(struct cmd_context
*cmd
, const struct logical_volume
*lv
)
1816 r
= dm_mknodes(NULL
);
1824 r
= dev_manager_mknodes(lv
);
1832 * Does PV use VG somewhere in its construction?
1833 * Returns 1 on failure.
1835 int pv_uses_vg(struct physical_volume
*pv
,
1836 struct volume_group
*vg
)
1841 if (!dm_is_dm_major(MAJOR(pv
->dev
->dev
)))
1844 return dev_manager_device_uses_vg(pv
->dev
, vg
);
1847 void activation_release(void)
1849 dev_manager_release();
1852 void activation_exit(void)