2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2011 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_activation_filter(struct cmd_context
*cmd
,
292 struct logical_volume
*lv
)
294 const struct dm_config_node
*cn
;
295 const struct dm_config_value
*cv
;
297 static char path
[PATH_MAX
];
299 if (!(cn
= find_config_tree_node(cmd
, "activation/volume_list"))) {
300 log_verbose("activation/volume_list configuration setting "
301 "not defined, checking only host tags for %s/%s",
302 lv
->vg
->name
, lv
->name
);
304 /* If no host tags defined, activate */
305 if (dm_list_empty(&cmd
->tags
))
308 /* If any host tag matches any LV or VG tag, activate */
309 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
) ||
310 str_list_match_list(&cmd
->tags
, &lv
->vg
->tags
, NULL
))
313 log_verbose("No host tag matches %s/%s",
314 lv
->vg
->name
, lv
->name
);
320 log_verbose("activation/volume_list configuration setting "
321 "defined, checking the list to match %s/%s",
322 lv
->vg
->name
, lv
->name
);
324 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
325 if (cv
->type
!= DM_CFG_STRING
) {
326 log_error("Ignoring invalid string in config file "
327 "activation/volume_list");
332 log_error("Ignoring empty string in config file "
333 "activation/volume_list");
342 log_error("Ignoring empty tag in config file "
343 "activation/volume_list");
346 /* If any host tag matches any LV or VG tag, activate */
347 if (!strcmp(str
, "*")) {
348 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
)
349 || str_list_match_list(&cmd
->tags
,
350 &lv
->vg
->tags
, NULL
))
355 /* If supplied tag matches LV or VG tag, activate */
356 if (str_list_match_item(&lv
->tags
, str
) ||
357 str_list_match_item(&lv
->vg
->tags
, str
))
362 if (!strchr(str
, '/')) {
363 /* vgname supplied */
364 if (!strcmp(str
, lv
->vg
->name
))
370 if (dm_snprintf(path
, sizeof(path
), "%s/%s", lv
->vg
->name
,
372 log_error("dm_snprintf error from %s/%s", lv
->vg
->name
,
376 if (!strcmp(path
, str
))
380 log_verbose("No item supplied in activation/volume_list configuration "
381 "setting matches %s/%s", lv
->vg
->name
, lv
->name
);
386 int library_version(char *version
, size_t size
)
391 return dm_get_library_version(version
, size
);
394 int driver_version(char *version
, size_t size
)
399 log_very_verbose("Getting driver version");
401 return dm_driver_version(version
, size
);
404 int target_version(const char *target_name
, uint32_t *maj
,
405 uint32_t *min
, uint32_t *patchlevel
)
409 struct dm_versions
*target
, *last_target
;
411 log_very_verbose("Getting target version for %s", target_name
);
412 if (!(dmt
= dm_task_create(DM_DEVICE_LIST_VERSIONS
)))
415 if (activation_checks() && !dm_task_enable_checks(dmt
))
418 if (!dm_task_run(dmt
)) {
419 log_debug("Failed to get %s target version", target_name
);
420 /* Assume this was because LIST_VERSIONS isn't supported */
424 target
= dm_task_get_versions(dmt
);
427 last_target
= target
;
429 if (!strcmp(target_name
, target
->name
)) {
431 *maj
= target
->version
[0];
432 *min
= target
->version
[1];
433 *patchlevel
= target
->version
[2];
437 target
= (struct dm_versions
*)((char *) target
+ target
->next
);
438 } while (last_target
!= target
);
441 dm_task_destroy(dmt
);
446 int lvm_dm_prefix_check(int major
, int minor
, const char *prefix
)
452 if (!(dmt
= dm_task_create(DM_DEVICE_STATUS
)))
455 if (!dm_task_set_minor(dmt
, minor
) ||
456 !dm_task_set_major(dmt
, major
) ||
458 !(uuid
= dm_task_get_uuid(dmt
))) {
459 dm_task_destroy(dmt
);
463 r
= strncasecmp(uuid
, prefix
, strlen(prefix
));
464 dm_task_destroy(dmt
);
469 int module_present(struct cmd_context
*cmd
, const char *target_name
)
476 if (dm_snprintf(module
, sizeof(module
), "dm-%s", target_name
) < 0) {
477 log_error("module_present module name too long: %s",
482 argv
[0] = MODPROBE_CMD
;
486 ret
= exec_cmd(cmd
, argv
, NULL
, 0);
491 int target_present(struct cmd_context
*cmd
, const char *target_name
,
494 uint32_t maj
, min
, patchlevel
;
501 if (target_version(target_name
, &maj
, &min
, &patchlevel
))
504 if (!module_present(cmd
, target_name
))
509 return target_version(target_name
, &maj
, &min
, &patchlevel
);
513 * Returns 1 if info structure populated, else 0 on failure.
515 int lv_info(struct cmd_context
*cmd
, const struct logical_volume
*lv
, unsigned origin_only
,
516 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
518 struct dm_info dminfo
;
523 * If open_count info is requested and we have to be sure our own udev
524 * transactions are finished
525 * For non-clustered locking type we are only interested for non-delete operation
526 * in progress - as only those could lead to opened files
528 if (with_open_count
) {
529 if (locking_is_clustered())
530 sync_local_dev_names(cmd
); /* Wait to have udev in sync */
531 else if (fs_has_non_delete_ops())
532 fs_unlock(); /* For non clustered - wait if there are non-delete ops */
535 if (!dev_manager_info(lv
->vg
->cmd
->mem
, lv
, origin_only
? "real" : NULL
, with_open_count
,
536 with_read_ahead
, &dminfo
, &info
->read_ahead
))
539 info
->exists
= dminfo
.exists
;
540 info
->suspended
= dminfo
.suspended
;
541 info
->open_count
= dminfo
.open_count
;
542 info
->major
= dminfo
.major
;
543 info
->minor
= dminfo
.minor
;
544 info
->read_only
= dminfo
.read_only
;
545 info
->live_table
= dminfo
.live_table
;
546 info
->inactive_table
= dminfo
.inactive_table
;
551 int lv_info_by_lvid(struct cmd_context
*cmd
, const char *lvid_s
,
552 unsigned origin_only
,
553 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
556 struct logical_volume
*lv
;
558 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
561 if (!lv_is_origin(lv
))
564 r
= lv_info(cmd
, lv
, origin_only
, info
, with_open_count
, with_read_ahead
);
570 int lv_check_not_in_use(struct cmd_context
*cmd
__attribute__((unused
)),
571 struct logical_volume
*lv
, struct lvinfo
*info
)
576 /* If sysfs is not used, use open_count information only. */
577 if (!*dm_sysfs_dir()) {
578 if (info
->open_count
) {
579 log_error("Logical volume %s/%s in use.",
580 lv
->vg
->name
, lv
->name
);
587 if (dm_device_has_holders(info
->major
, info
->minor
)) {
588 log_error("Logical volume %s/%s is used by another device.",
589 lv
->vg
->name
, lv
->name
);
593 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
594 log_error("Logical volume %s/%s contains a filesystem in use.",
595 lv
->vg
->name
, lv
->name
);
603 * Returns 1 if percent set, else 0 on failure.
605 int lv_check_transient(struct logical_volume
*lv
)
608 struct dev_manager
*dm
;
613 log_debug("Checking transient status for LV %s/%s", lv
->vg
->name
, lv
->name
);
615 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
618 if (!(r
= dev_manager_transient(dm
, lv
)))
621 dev_manager_destroy(dm
);
627 * Returns 1 if percent set, else 0 on failure.
629 int lv_snapshot_percent(const struct logical_volume
*lv
, percent_t
*percent
)
632 struct dev_manager
*dm
;
637 log_debug("Checking snapshot percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
639 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
642 if (!(r
= dev_manager_snapshot_percent(dm
, lv
, percent
)))
645 dev_manager_destroy(dm
);
650 /* FIXME Merge with snapshot_percent */
651 int lv_mirror_percent(struct cmd_context
*cmd
, const struct logical_volume
*lv
,
652 int wait
, percent_t
*percent
, uint32_t *event_nr
)
655 struct dev_manager
*dm
;
658 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
659 * it should be considered in-sync. */
660 if (dm_list_size(&lv
->segments
) == 1 && first_seg(lv
)->area_count
== 1) {
661 *percent
= PERCENT_100
;
668 log_debug("Checking mirror percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
670 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
676 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
679 if (!(r
= dev_manager_mirror_percent(dm
, lv
, wait
, percent
, event_nr
)))
682 dev_manager_destroy(dm
);
687 int lv_raid_percent(const struct logical_volume
*lv
, percent_t
*percent
)
689 return lv_mirror_percent(lv
->vg
->cmd
, lv
, 0, percent
, NULL
);
692 static int _lv_active(struct cmd_context
*cmd
, struct logical_volume
*lv
)
696 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0)) {
704 static int _lv_open_count(struct cmd_context
*cmd
, struct logical_volume
*lv
)
708 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0)) {
713 return info
.open_count
;
716 static int _lv_activate_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
)
719 struct dev_manager
*dm
;
721 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
724 if (!(r
= dev_manager_activate(dm
, lv
, laopts
)))
727 dev_manager_destroy(dm
);
731 static int _lv_preload(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
735 struct dev_manager
*dm
;
737 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
740 if (!(r
= dev_manager_preload(dm
, lv
, laopts
, flush_required
)))
743 dev_manager_destroy(dm
);
747 static int _lv_deactivate(struct logical_volume
*lv
)
750 struct dev_manager
*dm
;
752 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
755 if (!(r
= dev_manager_deactivate(dm
, lv
)))
758 dev_manager_destroy(dm
);
762 static int _lv_suspend_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
763 int lockfs
, int flush_required
)
766 struct dev_manager
*dm
;
769 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
770 * device directly, we don't want to touch the devices that use it.
772 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
775 if (!(r
= dev_manager_suspend(dm
, lv
, laopts
, lockfs
, flush_required
)))
778 dev_manager_destroy(dm
);
783 * These two functions return the number of visible LVs in the state,
784 * or -1 on error. FIXME Check this.
786 int lvs_in_vg_activated(struct volume_group
*vg
)
794 dm_list_iterate_items(lvl
, &vg
->lvs
)
795 if (lv_is_visible(lvl
->lv
))
796 count
+= (_lv_active(vg
->cmd
, lvl
->lv
) == 1);
798 log_debug("Counted %d active LVs in VG %s", count
, vg
->name
);
803 int lvs_in_vg_opened(const struct volume_group
*vg
)
805 const struct lv_list
*lvl
;
811 dm_list_iterate_items(lvl
, &vg
->lvs
)
812 if (lv_is_visible(lvl
->lv
))
813 count
+= (_lv_open_count(vg
->cmd
, lvl
->lv
) > 0);
815 log_debug("Counted %d open LVs in VG %s", count
, vg
->name
);
822 * @lv: logical volume being queried
823 * @locally: set if active locally (when provided)
824 * @exclusive: set if active exclusively (when provided)
826 * Determine whether an LV is active locally or in a cluster.
827 * In addition to the return code which indicates whether or
828 * not the LV is active somewhere, two other values are set
829 * to yield more information about the status of the activation:
830 * return locally exclusively status
831 * ====== ======= =========== ======
833 * 1 0 0 active remotely
834 * 1 0 1 exclusive remotely
835 * 1 1 0 active locally and possibly remotely
836 * 1 1 1 exclusive locally (or local && !cluster)
837 * The VG lock must be held to call this function.
841 static int _lv_is_active(struct logical_volume
*lv
,
842 int *locally
, int *exclusive
)
844 int r
, l
, e
; /* remote, local, and exclusive */
848 if (_lv_active(lv
->vg
->cmd
, lv
))
851 if (!vg_is_clustered(lv
->vg
)) {
852 e
= 1; /* exclusive by definition */
856 /* Active locally, and the caller doesn't care about exclusive */
860 if ((r
= remote_lock_held(lv
->lvid
.s
, &e
)) >= 0)
864 * If lock query is not supported (due to interfacing with old
865 * code), then we cannot evaluate exclusivity properly.
867 * Old users of this function will never be affected by this,
868 * since they are only concerned about active vs. not active.
869 * New users of this function who specifically ask for 'exclusive'
870 * will be given an error message.
874 log_error("Unable to determine exclusivity of %s",
879 /* FIXME: Is this fallback alright? */
880 if (activate_lv_excl(lv
->vg
->cmd
, lv
)) {
881 if (!deactivate_lv(lv
->vg
->cmd
, lv
))
883 /* FIXME: locally & exclusive are undefined. */
886 /* FIXME: Check exclusive value here. */
893 log_very_verbose("%s/%s is %sactive%s%s",
894 lv
->vg
->name
, lv
->name
,
895 (r
|| l
) ? "" : "not ",
896 (exclusive
&& e
) ? " exclusive" : "",
897 e
? (l
? " locally" : " remotely") : "");
902 int lv_is_active(struct logical_volume
*lv
)
904 return _lv_is_active(lv
, NULL
, NULL
);
907 int lv_is_active_but_not_locally(struct logical_volume
*lv
)
910 return _lv_is_active(lv
, &l
, NULL
) && !l
;
913 int lv_is_active_exclusive(struct logical_volume
*lv
)
917 return _lv_is_active(lv
, NULL
, &e
) && e
;
920 int lv_is_active_exclusive_locally(struct logical_volume
*lv
)
924 return _lv_is_active(lv
, &l
, &e
) && l
&& e
;
927 int lv_is_active_exclusive_remotely(struct logical_volume
*lv
)
931 return _lv_is_active(lv
, &l
, &e
) && !l
&& e
;
935 static struct dm_event_handler
*_create_dm_event_handler(struct cmd_context
*cmd
, const char *dmuuid
, const char *dso
,
936 const int timeout
, enum dm_event_mask mask
)
938 struct dm_event_handler
*dmevh
;
940 if (!(dmevh
= dm_event_handler_create()))
943 if (dm_event_handler_set_dmeventd_path(dmevh
, find_config_tree_str(cmd
, "dmeventd/executable", NULL
)))
946 if (dm_event_handler_set_dso(dmevh
, dso
))
949 if (dm_event_handler_set_uuid(dmevh
, dmuuid
))
952 dm_event_handler_set_timeout(dmevh
, timeout
);
953 dm_event_handler_set_event_mask(dmevh
, mask
);
958 dm_event_handler_destroy(dmevh
);
962 char *get_monitor_dso_path(struct cmd_context
*cmd
, const char *libpath
)
966 if (!(path
= dm_pool_alloc(cmd
->mem
, PATH_MAX
))) {
967 log_error("Failed to allocate dmeventd library path.");
971 get_shared_library_path(cmd
, libpath
, path
, PATH_MAX
);
976 static char *_build_target_uuid(struct cmd_context
*cmd
, struct logical_volume
*lv
)
980 if (lv_is_thin_pool(lv
))
981 layer
= "tpool"; /* Monitor "tpool" for the "thin pool". */
982 else if (lv_is_origin(lv
))
983 layer
= "real"; /* Monitor "real" for "snapshot-origin". */
987 return build_dm_uuid(cmd
->mem
, lv
->lvid
.s
, layer
);
990 int target_registered_with_dmeventd(struct cmd_context
*cmd
, const char *dso
,
991 struct logical_volume
*lv
, int *pending
)
994 enum dm_event_mask evmask
= 0;
995 struct dm_event_handler
*dmevh
;
1001 if (!(uuid
= _build_target_uuid(cmd
, lv
)))
1004 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, 0, DM_EVENT_ALL_ERRORS
)))
1007 if (dm_event_get_registered_device(dmevh
, 0)) {
1008 dm_event_handler_destroy(dmevh
);
1012 evmask
= dm_event_handler_get_event_mask(dmevh
);
1013 if (evmask
& DM_EVENT_REGISTRATION_PENDING
) {
1015 evmask
&= ~DM_EVENT_REGISTRATION_PENDING
;
1018 dm_event_handler_destroy(dmevh
);
1023 int target_register_events(struct cmd_context
*cmd
, const char *dso
, struct logical_volume
*lv
,
1024 int evmask
__attribute__((unused
)), int set
, int timeout
)
1027 struct dm_event_handler
*dmevh
;
1033 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
1034 if (!(uuid
= _build_target_uuid(cmd
, lv
)))
1037 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, timeout
,
1038 DM_EVENT_ALL_ERRORS
| (timeout
? DM_EVENT_TIMEOUT
: 0))))
1041 r
= set
? dm_event_register_handler(dmevh
) : dm_event_unregister_handler(dmevh
);
1043 dm_event_handler_destroy(dmevh
);
1048 log_info("%s %s for events", set
? "Monitored" : "Unmonitored", uuid
);
1056 * Returns 0 if an attempt to (un)monitor the device failed.
1057 * Returns 1 otherwise.
1059 int monitor_dev_for_events(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1060 const struct lv_activate_opts
*laopts
, int monitor
)
1063 int i
, pending
= 0, monitored
;
1065 struct dm_list
*tmp
, *snh
, *snht
;
1066 struct lv_segment
*seg
;
1067 struct lv_segment
*log_seg
;
1068 int (*monitor_fn
) (struct lv_segment
*s
, int e
);
1070 static const struct lv_activate_opts zlaopts
= { 0 };
1075 /* skip dmeventd code altogether */
1076 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE
)
1080 * Nothing to do if dmeventd configured not to be used.
1082 if (monitor
&& !dmeventd_monitor_mode())
1086 * In case of a snapshot device, we monitor lv->snapshot->lv,
1087 * not the actual LV itself.
1089 if (lv_is_cow(lv
) && (laopts
->no_merging
|| !lv_is_merging_cow(lv
)))
1090 return monitor_dev_for_events(cmd
, lv
->snapshot
->lv
, NULL
, monitor
);
1093 * In case this LV is a snapshot origin, we instead monitor
1094 * each of its respective snapshots. The origin itself may
1095 * also need to be monitored if it is a mirror, for example.
1097 if (!laopts
->origin_only
&& lv_is_origin(lv
))
1098 dm_list_iterate_safe(snh
, snht
, &lv
->snapshot_segs
)
1099 if (!monitor_dev_for_events(cmd
, dm_list_struct_base(snh
,
1100 struct lv_segment
, origin_list
)->cow
, NULL
, monitor
))
1104 * If the volume is mirrored and its log is also mirrored, monitor
1105 * the log volume as well.
1107 if ((seg
= first_seg(lv
)) != NULL
&& seg
->log_lv
!= NULL
&&
1108 (log_seg
= first_seg(seg
->log_lv
)) != NULL
&&
1109 seg_is_mirrored(log_seg
))
1110 if (!monitor_dev_for_events(cmd
, seg
->log_lv
, NULL
, monitor
))
1113 dm_list_iterate(tmp
, &lv
->segments
) {
1114 seg
= dm_list_item(tmp
, struct lv_segment
);
1116 /* Recurse for AREA_LV */
1117 for (s
= 0; s
< seg
->area_count
; s
++) {
1118 if (seg_type(seg
, s
) != AREA_LV
)
1120 if (!monitor_dev_for_events(cmd
, seg_lv(seg
, s
), NULL
,
1122 log_error("Failed to %smonitor %s",
1123 monitor
? "" : "un",
1124 seg_lv(seg
, s
)->name
);
1129 if (!seg_monitored(seg
) || (seg
->status
& PVMOVE
))
1134 /* Check monitoring status */
1135 if (seg
->segtype
->ops
->target_monitored
)
1136 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1138 continue; /* segtype doesn't support registration */
1141 * FIXME: We should really try again if pending
1143 monitored
= (pending
) ? 0 : monitored
;
1147 log_verbose("%s/%s already monitored.", lv
->vg
->name
, lv
->name
);
1148 else if (seg
->segtype
->ops
->target_monitor_events
)
1149 monitor_fn
= seg
->segtype
->ops
->target_monitor_events
;
1152 log_verbose("%s/%s already not monitored.", lv
->vg
->name
, lv
->name
);
1153 else if (seg
->segtype
->ops
->target_unmonitor_events
)
1154 monitor_fn
= seg
->segtype
->ops
->target_unmonitor_events
;
1157 /* Do [un]monitor */
1161 log_verbose("%sonitoring %s/%s%s", monitor
? "M" : "Not m", lv
->vg
->name
, lv
->name
,
1162 test_mode() ? " [Test mode: skipping this]" : "");
1164 /* FIXME Test mode should really continue a bit further. */
1168 /* FIXME specify events */
1169 if (!monitor_fn(seg
, 0)) {
1170 log_error("%s/%s: %s segment monitoring function failed.",
1171 lv
->vg
->name
, lv
->name
, seg
->segtype
->name
);
1175 /* Check [un]monitor results */
1176 /* Try a couple times if pending, but not forever... */
1177 for (i
= 0; i
< 10; i
++) {
1179 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1181 (!monitored
&& monitor
) ||
1182 (monitored
&& !monitor
))
1183 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
1184 lv
->vg
->name
, lv
->name
, monitor
? "" : "un");
1191 r
= (monitored
&& monitor
) || (!monitored
&& !monitor
);
1200 struct detached_lv_data
{
1201 struct logical_volume
*lv_pre
;
1202 struct lv_activate_opts
*laopts
;
1203 int *flush_required
;
1206 static int _preload_detached_lv(struct cmd_context
*cmd
, struct logical_volume
*lv
, void *data
)
1208 struct detached_lv_data
*detached
= data
;
1209 struct lv_list
*lvl_pre
;
1211 if ((lvl_pre
= find_lv_in_vg(detached
->lv_pre
->vg
, lv
->name
))) {
1212 if (lv_is_visible(lvl_pre
->lv
) && lv_is_active(lv
) && (!lv_is_cow(lv
) || !lv_is_cow(lvl_pre
->lv
)) &&
1213 !_lv_preload(lvl_pre
->lv
, detached
->laopts
, detached
->flush_required
))
1220 static int _lv_suspend(struct cmd_context
*cmd
, const char *lvid_s
,
1221 struct lv_activate_opts
*laopts
, int error_if_not_suspended
)
1223 struct logical_volume
*lv
= NULL
, *lv_pre
= NULL
, *pvmove_lv
= NULL
;
1224 struct lv_list
*lvl_pre
;
1225 struct seg_list
*sl
;
1226 struct lv_segment
*snap_seg
;
1228 int r
= 0, lockfs
= 0, flush_required
= 0;
1229 struct detached_lv_data detached
;
1234 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1237 /* Use precommitted metadata if present */
1238 if (!(lv_pre
= lv_from_lvid(cmd
, lvid_s
, 1)))
1241 /* Ignore origin_only unless LV is origin in both old and new metadata */
1242 if (!lv_is_origin(lv
) || !lv_is_origin(lv_pre
))
1243 laopts
->origin_only
= 0;
1246 _skip("Suspending %s%s.", lv
->name
, laopts
->origin_only
? " origin without snapshots" : "");
1251 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1254 if (!info
.exists
|| info
.suspended
) {
1255 if (!error_if_not_suspended
) {
1258 critical_section_inc(cmd
, "already suspended");
1263 if (!lv_read_replicator_vgs(lv
))
1266 lv_calculate_readahead(lv
, NULL
);
1269 * Preload devices for the LV.
1270 * If the PVMOVE LV is being removed, it's only present in the old
1271 * metadata and not the new, so we must explicitly add the new
1272 * tables for all the changed LVs here, as the relationships
1273 * are not found by walking the new metadata.
1275 if (!(lv_pre
->status
& LOCKED
) &&
1276 (lv
->status
& LOCKED
) &&
1277 (pvmove_lv
= find_pvmove_lv_in_lv(lv
))) {
1278 /* Preload all the LVs above the PVMOVE LV */
1279 dm_list_iterate_items(sl
, &pvmove_lv
->segs_using_this_lv
) {
1280 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, sl
->seg
->lv
->name
))) {
1281 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", sl
->seg
->lv
->name
);
1284 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1287 /* Now preload the PVMOVE LV itself */
1288 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, pvmove_lv
->name
))) {
1289 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", pvmove_lv
->name
);
1292 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1295 if (!_lv_preload(lv_pre
, laopts
, &flush_required
))
1296 /* FIXME Revert preloading */
1300 * Search for existing LVs that have become detached and preload them.
1302 detached
.lv_pre
= lv_pre
;
1303 detached
.laopts
= laopts
;
1304 detached
.flush_required
= &flush_required
;
1306 if (!for_each_sub_lv(cmd
, lv
, &_preload_detached_lv
, &detached
))
1310 * Preload any snapshots that are being removed.
1312 if (!laopts
->origin_only
&& lv_is_origin(lv
)) {
1313 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1314 if (!(lvl_pre
= find_lv_in_vg_by_lvid(lv_pre
->vg
, &snap_seg
->cow
->lvid
))) {
1315 log_error(INTERNAL_ERROR
"LV %s (%s) missing from preload metadata",
1316 snap_seg
->cow
->name
, snap_seg
->cow
->lvid
.id
[1].uuid
);
1319 if (!lv_is_cow(lvl_pre
->lv
) &&
1320 !_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1326 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 0))
1327 /* FIXME Consider aborting here */
1330 critical_section_inc(cmd
, "suspending");
1332 critical_section_inc(cmd
, "suspending pvmove LV");
1334 if (!laopts
->origin_only
&&
1335 (lv_is_origin(lv_pre
) || lv_is_cow(lv_pre
)))
1339 * Suspending an LV directly above a PVMOVE LV also
1340 * suspends other LVs using that same PVMOVE LV.
1341 * FIXME Remove this and delay the 'clear node' until
1342 * after the code knows whether there's a different
1343 * inactive table to load or not instead so lv_suspend
1344 * can be called separately for each LV safely.
1346 if ((lv_pre
->vg
->status
& PRECOMMITTED
) &&
1347 (lv_pre
->status
& LOCKED
) && find_pvmove_lv_in_lv(lv_pre
)) {
1348 if (!_lv_suspend_lv(lv_pre
, laopts
, lockfs
, flush_required
)) {
1349 critical_section_dec(cmd
, "failed precommitted suspend");
1351 critical_section_dec(cmd
, "failed precommitted suspend (pvmove)");
1355 /* Normal suspend */
1356 if (!_lv_suspend_lv(lv
, laopts
, lockfs
, flush_required
)) {
1357 critical_section_dec(cmd
, "failed suspend");
1359 critical_section_dec(cmd
, "failed suspend (pvmove)");
1367 release_vg(lv_pre
->vg
);
1369 lv_release_replicator_vgs(lv
);
1376 /* Returns success if the device is not active */
1377 int lv_suspend_if_active(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
1379 struct lv_activate_opts laopts
= { .origin_only
= origin_only
};
1381 return _lv_suspend(cmd
, lvid_s
, &laopts
, 0);
1384 /* No longer used */
1386 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1388 return _lv_suspend(cmd, lvid_s, 1);
1397 * @exclusive: This parameter only has an affect in cluster-context.
1398 * It forces local target type to be used (instead of
1399 * cluster-aware type).
1400 * @error_if_not_active
1402 static int _lv_resume(struct cmd_context
*cmd
, const char *lvid_s
,
1403 struct lv_activate_opts
*laopts
, int error_if_not_active
)
1405 struct logical_volume
*lv
;
1412 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1415 if (!lv_is_origin(lv
))
1416 laopts
->origin_only
= 0;
1419 _skip("Resuming %s%s%s.", lv
->name
, laopts
->origin_only
? " without snapshots" : "",
1420 laopts
->revert
? " (reverting)" : "");
1425 log_debug("Resuming LV %s/%s%s%s%s.", lv
->vg
->name
, lv
->name
,
1426 error_if_not_active
? "" : " if active",
1427 laopts
->origin_only
? " without snapshots" : "",
1428 laopts
->revert
? " (reverting)" : "");
1430 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1433 if (!info
.exists
|| !info
.suspended
) {
1434 if (error_if_not_active
)
1437 if (!info
.suspended
)
1438 critical_section_dec(cmd
, "already resumed");
1442 if (!_lv_activate_lv(lv
, laopts
))
1445 critical_section_dec(cmd
, "resumed");
1447 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1458 /* Returns success if the device is not active */
1459 int lv_resume_if_active(struct cmd_context
*cmd
, const char *lvid_s
,
1460 unsigned origin_only
, unsigned exclusive
, unsigned revert
)
1462 struct lv_activate_opts laopts
= {
1463 .origin_only
= origin_only
,
1465 * When targets are activated exclusively in a cluster, the
1466 * non-clustered target should be used. This only happens
1467 * if exclusive is set.
1469 .exclusive
= exclusive
,
1473 return _lv_resume(cmd
, lvid_s
, &laopts
, 0);
1476 int lv_resume(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
1478 struct lv_activate_opts laopts
= { .origin_only
= origin_only
, };
1480 return _lv_resume(cmd
, lvid_s
, &laopts
, 1);
1483 static int _lv_has_open_snapshots(struct logical_volume
*lv
)
1485 struct lv_segment
*snap_seg
;
1489 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1490 if (!lv_info(lv
->vg
->cmd
, snap_seg
->cow
, 0, &info
, 1, 0)) {
1495 if (info
.exists
&& info
.open_count
) {
1496 log_error("LV %s/%s has open snapshot %s: "
1497 "not deactivating", lv
->vg
->name
, lv
->name
,
1498 snap_seg
->cow
->name
);
1506 int lv_deactivate(struct cmd_context
*cmd
, const char *lvid_s
)
1508 struct logical_volume
*lv
;
1515 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1519 _skip("Deactivating '%s'.", lv
->name
);
1524 log_debug("Deactivating %s/%s.", lv
->vg
->name
, lv
->name
);
1526 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0))
1534 if (lv_is_visible(lv
)) {
1535 if (!lv_check_not_in_use(cmd
, lv
, &info
))
1538 if (lv_is_origin(lv
) && _lv_has_open_snapshots(lv
))
1542 if (!lv_read_replicator_vgs(lv
))
1545 lv_calculate_readahead(lv
, NULL
);
1547 if (!monitor_dev_for_events(cmd
, lv
, NULL
, 0))
1550 critical_section_inc(cmd
, "deactivating");
1551 r
= _lv_deactivate(lv
);
1552 critical_section_dec(cmd
, "deactivated");
1554 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0) || info
.exists
)
1558 lv_release_replicator_vgs(lv
);
1565 /* Test if LV passes filter */
1566 int lv_activation_filter(struct cmd_context
*cmd
, const char *lvid_s
,
1569 struct logical_volume
*lv
;
1572 if (!activation()) {
1577 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1580 if (!_passes_activation_filter(cmd
, lv
)) {
1581 log_verbose("Not activating %s/%s since it does not pass "
1582 "activation filter.", lv
->vg
->name
, lv
->name
);
1594 static int _lv_activate(struct cmd_context
*cmd
, const char *lvid_s
,
1595 struct lv_activate_opts
*laopts
, int filter
)
1597 struct logical_volume
*lv
;
1604 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1607 if (filter
&& !_passes_activation_filter(cmd
, lv
)) {
1608 log_error("Not activating %s/%s since it does not pass "
1609 "activation filter.", lv
->vg
->name
, lv
->name
);
1613 if ((!lv
->vg
->cmd
->partial_activation
) && (lv
->status
& PARTIAL_LV
)) {
1614 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1619 if (lv_has_unknown_segments(lv
)) {
1620 log_error("Refusing activation of LV %s containing "
1621 "an unrecognised segment.", lv
->name
);
1626 _skip("Activating '%s'.", lv
->name
);
1631 log_debug("Activating %s/%s%s.", lv
->vg
->name
, lv
->name
,
1632 laopts
->exclusive
? " exclusively" : "");
1634 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
1637 if (info
.exists
&& !info
.suspended
&& info
.live_table
) {
1642 if (!lv_read_replicator_vgs(lv
))
1645 lv_calculate_readahead(lv
, NULL
);
1647 critical_section_inc(cmd
, "activating");
1648 if (!(r
= _lv_activate_lv(lv
, laopts
)))
1650 critical_section_dec(cmd
, "activated");
1652 if (r
&& !monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1657 lv_release_replicator_vgs(lv
);
1665 int lv_activate(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1667 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1669 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 0))
1675 /* Activate LV only if it passes filter */
1676 int lv_activate_with_filter(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1678 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1680 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 1))
1686 int lv_mknodes(struct cmd_context
*cmd
, const struct logical_volume
*lv
)
1691 r
= dm_mknodes(NULL
);
1699 r
= dev_manager_mknodes(lv
);
1707 * Does PV use VG somewhere in its construction?
1708 * Returns 1 on failure.
1710 int pv_uses_vg(struct physical_volume
*pv
,
1711 struct volume_group
*vg
)
1716 if (!dm_is_dm_major(MAJOR(pv
->dev
->dev
)))
1719 return dev_manager_device_uses_vg(pv
->dev
, vg
);
1722 void activation_release(void)
1724 dev_manager_release();
1727 void activation_exit(void)