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
)
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 lv_info(struct cmd_context
*cmd
, const struct logical_volume
*lv
, unsigned origin_only
,
151 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
155 int lv_info_by_lvid(struct cmd_context
*cmd
, const char *lvid_s
,
156 unsigned origin_only
,
157 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
161 int lv_snapshot_percent(const struct logical_volume
*lv
, percent_t
*percent
)
165 int lv_mirror_percent(struct cmd_context
*cmd
, const struct logical_volume
*lv
,
166 int wait
, percent_t
*percent
, uint32_t *event_nr
)
170 int lvs_in_vg_activated(struct volume_group
*vg
)
174 int lvs_in_vg_opened(const struct volume_group
*vg
)
179 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
184 int lv_suspend_if_active(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
188 int lv_resume(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
192 int lv_resume_if_active(struct cmd_context
*cmd
, const char *lvid_s
,
193 unsigned origin_only
, unsigned exclusive
, unsigned revert
)
197 int lv_deactivate(struct cmd_context
*cmd
, const char *lvid_s
)
201 int lv_activation_filter(struct cmd_context
*cmd
, const char *lvid_s
,
206 int lv_activate(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
210 int lv_activate_with_filter(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
214 int lv_mknodes(struct cmd_context
*cmd
, const struct logical_volume
*lv
)
218 int lv_send_message(const struct logical_volume
*lv
, const char *message
)
222 int pv_uses_vg(struct physical_volume
*pv
,
223 struct volume_group
*vg
)
227 void activation_release(void)
230 void activation_exit(void)
234 int lv_is_active(struct logical_volume
*lv
)
238 int lv_is_active_but_not_locally(struct logical_volume
*lv
)
242 int lv_is_active_exclusive(struct logical_volume
*lv
)
246 int lv_is_active_exclusive_locally(struct logical_volume
*lv
)
250 int lv_is_active_exclusive_remotely(struct logical_volume
*lv
)
255 int lv_check_transient(struct logical_volume
*lv
)
259 int monitor_dev_for_events(struct cmd_context
*cmd
, struct logical_volume
*lv
,
260 struct lv_activate_opts
*laopts
, int monitor
)
264 #else /* DEVMAPPER_SUPPORT */
266 static int _activation
= 1;
268 void set_activation(int act
)
270 if (act
== _activation
)
275 log_verbose("Activation enabled. Device-mapper kernel "
276 "driver will be used.");
278 log_warn("WARNING: Activation disabled. No device-mapper "
279 "interaction will be attempted.");
287 static int _passes_activation_filter(struct cmd_context
*cmd
,
288 struct logical_volume
*lv
)
290 const struct dm_config_node
*cn
;
291 const struct dm_config_value
*cv
;
295 if (!(cn
= find_config_tree_node(cmd
, "activation/volume_list"))) {
296 log_verbose("activation/volume_list configuration setting "
297 "not defined, checking only host tags for %s/%s",
298 lv
->vg
->name
, lv
->name
);
300 /* If no host tags defined, activate */
301 if (dm_list_empty(&cmd
->tags
))
304 /* If any host tag matches any LV or VG tag, activate */
305 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
) ||
306 str_list_match_list(&cmd
->tags
, &lv
->vg
->tags
, NULL
))
309 log_verbose("No host tag matches %s/%s",
310 lv
->vg
->name
, lv
->name
);
316 log_verbose("activation/volume_list configuration setting "
317 "defined, checking the list to match %s/%s",
318 lv
->vg
->name
, lv
->name
);
320 for (cv
= cn
->v
; cv
; cv
= cv
->next
) {
321 if (cv
->type
!= DM_CFG_STRING
) {
322 log_error("Ignoring invalid string in config file "
323 "activation/volume_list");
328 log_error("Ignoring empty string in config file "
329 "activation/volume_list");
338 log_error("Ignoring empty tag in config file "
339 "activation/volume_list");
342 /* If any host tag matches any LV or VG tag, activate */
343 if (!strcmp(str
, "*")) {
344 if (str_list_match_list(&cmd
->tags
, &lv
->tags
, NULL
)
345 || str_list_match_list(&cmd
->tags
,
346 &lv
->vg
->tags
, NULL
))
351 /* If supplied tag matches LV or VG tag, activate */
352 if (str_list_match_item(&lv
->tags
, str
) ||
353 str_list_match_item(&lv
->vg
->tags
, str
))
358 if (!strchr(str
, '/')) {
359 /* vgname supplied */
360 if (!strcmp(str
, lv
->vg
->name
))
366 if (dm_snprintf(path
, sizeof(path
), "%s/%s", lv
->vg
->name
,
368 log_error("dm_snprintf error from %s/%s", lv
->vg
->name
,
372 if (!strcmp(path
, str
))
376 log_verbose("No item supplied in activation/volume_list configuration "
377 "setting matches %s/%s", lv
->vg
->name
, lv
->name
);
382 int library_version(char *version
, size_t size
)
387 return dm_get_library_version(version
, size
);
390 int driver_version(char *version
, size_t size
)
395 log_very_verbose("Getting driver version");
397 return dm_driver_version(version
, size
);
400 int target_version(const char *target_name
, uint32_t *maj
,
401 uint32_t *min
, uint32_t *patchlevel
)
405 struct dm_versions
*target
, *last_target
;
407 log_very_verbose("Getting target version for %s", target_name
);
408 if (!(dmt
= dm_task_create(DM_DEVICE_LIST_VERSIONS
)))
411 if (activation_checks() && !dm_task_enable_checks(dmt
))
414 if (!dm_task_run(dmt
)) {
415 log_debug("Failed to get %s target version", target_name
);
416 /* Assume this was because LIST_VERSIONS isn't supported */
420 target
= dm_task_get_versions(dmt
);
423 last_target
= target
;
425 if (!strcmp(target_name
, target
->name
)) {
427 *maj
= target
->version
[0];
428 *min
= target
->version
[1];
429 *patchlevel
= target
->version
[2];
433 target
= (struct dm_versions
*)((char *) target
+ target
->next
);
434 } while (last_target
!= target
);
437 dm_task_destroy(dmt
);
442 int module_present(struct cmd_context
*cmd
, const char *target_name
)
449 if (dm_snprintf(module
, sizeof(module
), "dm-%s", target_name
) < 0) {
450 log_error("module_present module name too long: %s",
455 argv
[0] = MODPROBE_CMD
;
459 ret
= exec_cmd(cmd
, argv
, NULL
, 0);
464 int target_present(struct cmd_context
*cmd
, const char *target_name
,
467 uint32_t maj
, min
, patchlevel
;
474 if (target_version(target_name
, &maj
, &min
, &patchlevel
))
477 if (!module_present(cmd
, target_name
))
482 return target_version(target_name
, &maj
, &min
, &patchlevel
);
486 * Returns 1 if info structure populated, else 0 on failure.
488 int lv_info(struct cmd_context
*cmd
, const struct logical_volume
*lv
, unsigned origin_only
,
489 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
491 struct dm_info dminfo
;
496 * If open_count info is requested and we have to be sure our own udev
497 * transactions are finished
498 * For non-clustered locking type we are only interested for non-delete operation
499 * in progress - as only those could lead to opened files
501 if (with_open_count
) {
502 if (locking_is_clustered())
503 sync_local_dev_names(cmd
); /* Wait to have udev in sync */
504 else if (fs_has_non_delete_ops())
505 fs_unlock(); /* For non clustered - wait if there are non-delete ops */
508 if (!dev_manager_info(lv
->vg
->cmd
->mem
, lv
, origin_only
? "real" : NULL
, with_open_count
,
509 with_read_ahead
, &dminfo
, &info
->read_ahead
))
512 info
->exists
= dminfo
.exists
;
513 info
->suspended
= dminfo
.suspended
;
514 info
->open_count
= dminfo
.open_count
;
515 info
->major
= dminfo
.major
;
516 info
->minor
= dminfo
.minor
;
517 info
->read_only
= dminfo
.read_only
;
518 info
->live_table
= dminfo
.live_table
;
519 info
->inactive_table
= dminfo
.inactive_table
;
524 int lv_info_by_lvid(struct cmd_context
*cmd
, const char *lvid_s
,
525 unsigned origin_only
,
526 struct lvinfo
*info
, int with_open_count
, int with_read_ahead
)
529 struct logical_volume
*lv
;
531 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
534 if (!lv_is_origin(lv
))
537 r
= lv_info(cmd
, lv
, origin_only
, info
, with_open_count
, with_read_ahead
);
543 int lv_check_not_in_use(struct cmd_context
*cmd
__attribute__((unused
)),
544 struct logical_volume
*lv
, struct lvinfo
*info
)
549 /* If sysfs is not used, use open_count information only. */
550 if (!*dm_sysfs_dir()) {
551 if (info
->open_count
) {
552 log_error("Logical volume %s/%s in use.",
553 lv
->vg
->name
, lv
->name
);
560 if (dm_device_has_holders(info
->major
, info
->minor
)) {
561 log_error("Logical volume %s/%s is used by another device.",
562 lv
->vg
->name
, lv
->name
);
566 if (dm_device_has_mounted_fs(info
->major
, info
->minor
)) {
567 log_error("Logical volume %s/%s contains a filesystem in use.",
568 lv
->vg
->name
, lv
->name
);
576 * Returns 1 if percent set, else 0 on failure.
578 int lv_check_transient(struct logical_volume
*lv
)
581 struct dev_manager
*dm
;
586 log_debug("Checking transient status for LV %s/%s", lv
->vg
->name
, lv
->name
);
588 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
591 if (!(r
= dev_manager_transient(dm
, lv
)))
594 dev_manager_destroy(dm
);
600 * Returns 1 if percent set, else 0 on failure.
602 int lv_snapshot_percent(const struct logical_volume
*lv
, percent_t
*percent
)
605 struct dev_manager
*dm
;
610 log_debug("Checking snapshot percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
612 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
615 if (!(r
= dev_manager_snapshot_percent(dm
, lv
, percent
)))
618 dev_manager_destroy(dm
);
623 /* FIXME Merge with snapshot_percent */
624 int lv_mirror_percent(struct cmd_context
*cmd
, const struct logical_volume
*lv
,
625 int wait
, percent_t
*percent
, uint32_t *event_nr
)
628 struct dev_manager
*dm
;
631 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
632 * it should be considered in-sync. */
633 if (dm_list_size(&lv
->segments
) == 1 && first_seg(lv
)->area_count
== 1) {
634 *percent
= PERCENT_100
;
641 log_debug("Checking mirror percent for LV %s/%s", lv
->vg
->name
, lv
->name
);
643 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
649 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
652 if (!(r
= dev_manager_mirror_percent(dm
, lv
, wait
, percent
, event_nr
)))
655 dev_manager_destroy(dm
);
660 int lv_raid_percent(const struct logical_volume
*lv
, percent_t
*percent
)
662 return lv_mirror_percent(lv
->vg
->cmd
, lv
, 0, percent
, NULL
);
665 static int _lv_active(struct cmd_context
*cmd
, struct logical_volume
*lv
)
669 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0)) {
677 static int _lv_open_count(struct cmd_context
*cmd
, struct logical_volume
*lv
)
681 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0)) {
686 return info
.open_count
;
689 static int _lv_activate_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
)
692 struct dev_manager
*dm
;
694 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
697 if (!(r
= dev_manager_activate(dm
, lv
, laopts
)))
700 dev_manager_destroy(dm
);
704 static int _lv_preload(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
708 struct dev_manager
*dm
;
710 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
713 if (!(r
= dev_manager_preload(dm
, lv
, laopts
, flush_required
)))
716 dev_manager_destroy(dm
);
720 static int _lv_deactivate(struct logical_volume
*lv
)
723 struct dev_manager
*dm
;
725 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, 1)))
728 if (!(r
= dev_manager_deactivate(dm
, lv
)))
731 dev_manager_destroy(dm
);
735 static int _lv_suspend_lv(struct logical_volume
*lv
, struct lv_activate_opts
*laopts
,
736 int lockfs
, int flush_required
)
739 struct dev_manager
*dm
;
742 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
743 * device directly, we don't want to touch the devices that use it.
745 if (!(dm
= dev_manager_create(lv
->vg
->cmd
, lv
->vg
->name
, (lv
->status
& PVMOVE
) ? 0 : 1)))
748 if (!(r
= dev_manager_suspend(dm
, lv
, laopts
, lockfs
, flush_required
)))
751 dev_manager_destroy(dm
);
756 * These two functions return the number of visible LVs in the state,
757 * or -1 on error. FIXME Check this.
759 int lvs_in_vg_activated(struct volume_group
*vg
)
767 dm_list_iterate_items(lvl
, &vg
->lvs
)
768 if (lv_is_visible(lvl
->lv
))
769 count
+= (_lv_active(vg
->cmd
, lvl
->lv
) == 1);
771 log_debug("Counted %d active LVs in VG %s", count
, vg
->name
);
776 int lvs_in_vg_opened(const struct volume_group
*vg
)
778 const struct lv_list
*lvl
;
784 dm_list_iterate_items(lvl
, &vg
->lvs
)
785 if (lv_is_visible(lvl
->lv
) && !lv_is_used_thin_pool(lvl
->lv
))
786 count
+= (_lv_open_count(vg
->cmd
, lvl
->lv
) > 0);
788 log_debug("Counted %d open LVs in VG %s", count
, vg
->name
);
795 * @lv: logical volume being queried
796 * @locally: set if active locally (when provided)
797 * @exclusive: set if active exclusively (when provided)
799 * Determine whether an LV is active locally or in a cluster.
800 * In addition to the return code which indicates whether or
801 * not the LV is active somewhere, two other values are set
802 * to yield more information about the status of the activation:
803 * return locally exclusively status
804 * ====== ======= =========== ======
806 * 1 0 0 active remotely
807 * 1 0 1 exclusive remotely
808 * 1 1 0 active locally and possibly remotely
809 * 1 1 1 exclusive locally (or local && !cluster)
810 * The VG lock must be held to call this function.
814 static int _lv_is_active(struct logical_volume
*lv
,
815 int *locally
, int *exclusive
)
817 int r
, l
, e
; /* remote, local, and exclusive */
821 if (_lv_active(lv
->vg
->cmd
, lv
))
824 if (!vg_is_clustered(lv
->vg
)) {
825 e
= 1; /* exclusive by definition */
829 /* Active locally, and the caller doesn't care about exclusive */
833 if ((r
= remote_lock_held(lv
->lvid
.s
, &e
)) >= 0)
837 * If lock query is not supported (due to interfacing with old
838 * code), then we cannot evaluate exclusivity properly.
840 * Old users of this function will never be affected by this,
841 * since they are only concerned about active vs. not active.
842 * New users of this function who specifically ask for 'exclusive'
843 * will be given an error message.
847 log_error("Unable to determine exclusivity of %s",
852 /* FIXME: Is this fallback alright? */
853 if (activate_lv_excl(lv
->vg
->cmd
, lv
)) {
854 if (!deactivate_lv(lv
->vg
->cmd
, lv
))
856 /* FIXME: locally & exclusive are undefined. */
859 /* FIXME: Check exclusive value here. */
866 log_very_verbose("%s/%s is %sactive%s%s",
867 lv
->vg
->name
, lv
->name
,
868 (r
|| l
) ? "" : "not ",
869 (exclusive
&& e
) ? " exclusive" : "",
870 e
? (l
? " locally" : " remotely") : "");
875 int lv_is_active(struct logical_volume
*lv
)
877 return _lv_is_active(lv
, NULL
, NULL
);
880 int lv_is_active_but_not_locally(struct logical_volume
*lv
)
883 return _lv_is_active(lv
, &l
, NULL
) && !l
;
886 int lv_is_active_exclusive(struct logical_volume
*lv
)
890 return _lv_is_active(lv
, NULL
, &e
) && e
;
893 int lv_is_active_exclusive_locally(struct logical_volume
*lv
)
897 return _lv_is_active(lv
, &l
, &e
) && l
&& e
;
900 int lv_is_active_exclusive_remotely(struct logical_volume
*lv
)
904 return _lv_is_active(lv
, &l
, &e
) && !l
&& e
;
908 static struct dm_event_handler
*_create_dm_event_handler(struct cmd_context
*cmd
, const char *dmuuid
, const char *dso
,
909 const int timeout
, enum dm_event_mask mask
)
911 struct dm_event_handler
*dmevh
;
913 if (!(dmevh
= dm_event_handler_create()))
916 if (dm_event_handler_set_dmeventd_path(dmevh
, find_config_tree_str(cmd
, "dmeventd/executable", NULL
)))
919 if (dm_event_handler_set_dso(dmevh
, dso
))
922 if (dm_event_handler_set_uuid(dmevh
, dmuuid
))
925 dm_event_handler_set_timeout(dmevh
, timeout
);
926 dm_event_handler_set_event_mask(dmevh
, mask
);
931 dm_event_handler_destroy(dmevh
);
935 char *get_monitor_dso_path(struct cmd_context
*cmd
, const char *libpath
)
939 if (!(path
= dm_pool_alloc(cmd
->mem
, PATH_MAX
))) {
940 log_error("Failed to allocate dmeventd library path.");
944 get_shared_library_path(cmd
, libpath
, path
, PATH_MAX
);
949 int target_registered_with_dmeventd(struct cmd_context
*cmd
, const char *dso
,
950 struct logical_volume
*lv
, int *pending
)
953 enum dm_event_mask evmask
= 0;
954 struct dm_event_handler
*dmevh
;
961 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
962 if (!(uuid
= build_dm_uuid(cmd
->mem
, lv
->lvid
.s
, lv_is_origin(lv
) ? "real" : NULL
)))
965 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, 0, DM_EVENT_ALL_ERRORS
)))
968 if (dm_event_get_registered_device(dmevh
, 0)) {
969 dm_event_handler_destroy(dmevh
);
973 evmask
= dm_event_handler_get_event_mask(dmevh
);
974 if (evmask
& DM_EVENT_REGISTRATION_PENDING
) {
976 evmask
&= ~DM_EVENT_REGISTRATION_PENDING
;
979 dm_event_handler_destroy(dmevh
);
984 int target_register_events(struct cmd_context
*cmd
, const char *dso
, struct logical_volume
*lv
,
985 int evmask
__attribute__((unused
)), int set
, int timeout
)
988 struct dm_event_handler
*dmevh
;
994 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
995 if (!(uuid
= build_dm_uuid(cmd
->mem
, lv
->lvid
.s
, lv_is_origin(lv
) ? "real" : NULL
)))
998 if (!(dmevh
= _create_dm_event_handler(cmd
, uuid
, dso
, timeout
,
999 DM_EVENT_ALL_ERRORS
| (timeout
? DM_EVENT_TIMEOUT
: 0))))
1002 r
= set
? dm_event_register_handler(dmevh
) : dm_event_unregister_handler(dmevh
);
1004 dm_event_handler_destroy(dmevh
);
1009 log_info("%s %s for events", set
? "Monitored" : "Unmonitored", uuid
);
1017 * Returns 0 if an attempt to (un)monitor the device failed.
1018 * Returns 1 otherwise.
1020 int monitor_dev_for_events(struct cmd_context
*cmd
, struct logical_volume
*lv
,
1021 const struct lv_activate_opts
*laopts
, int monitor
)
1024 int i
, pending
= 0, monitored
;
1026 struct dm_list
*tmp
, *snh
, *snht
;
1027 struct lv_segment
*seg
;
1028 struct lv_segment
*log_seg
;
1029 int (*monitor_fn
) (struct lv_segment
*s
, int e
);
1031 static const struct lv_activate_opts zlaopts
= { 0 };
1036 /* skip dmeventd code altogether */
1037 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE
)
1041 * Nothing to do if dmeventd configured not to be used.
1043 if (monitor
&& !dmeventd_monitor_mode())
1047 * In case of a snapshot device, we monitor lv->snapshot->lv,
1048 * not the actual LV itself.
1050 if (lv_is_cow(lv
) && (laopts
->no_merging
|| !lv_is_merging_cow(lv
)))
1051 return monitor_dev_for_events(cmd
, lv
->snapshot
->lv
, NULL
, monitor
);
1054 * In case this LV is a snapshot origin, we instead monitor
1055 * each of its respective snapshots. The origin itself may
1056 * also need to be monitored if it is a mirror, for example.
1058 if (!laopts
->origin_only
&& lv_is_origin(lv
))
1059 dm_list_iterate_safe(snh
, snht
, &lv
->snapshot_segs
)
1060 if (!monitor_dev_for_events(cmd
, dm_list_struct_base(snh
,
1061 struct lv_segment
, origin_list
)->cow
, NULL
, monitor
))
1065 * If the volume is mirrored and its log is also mirrored, monitor
1066 * the log volume as well.
1068 if ((seg
= first_seg(lv
)) != NULL
&& seg
->log_lv
!= NULL
&&
1069 (log_seg
= first_seg(seg
->log_lv
)) != NULL
&&
1070 seg_is_mirrored(log_seg
))
1071 if (!monitor_dev_for_events(cmd
, seg
->log_lv
, NULL
, monitor
))
1074 dm_list_iterate(tmp
, &lv
->segments
) {
1075 seg
= dm_list_item(tmp
, struct lv_segment
);
1077 /* Recurse for AREA_LV */
1078 for (s
= 0; s
< seg
->area_count
; s
++) {
1079 if (seg_type(seg
, s
) != AREA_LV
)
1081 if (!monitor_dev_for_events(cmd
, seg_lv(seg
, s
), NULL
,
1083 log_error("Failed to %smonitor %s",
1084 monitor
? "" : "un",
1085 seg_lv(seg
, s
)->name
);
1090 if (!seg_monitored(seg
) || (seg
->status
& PVMOVE
))
1095 /* Check monitoring status */
1096 if (seg
->segtype
->ops
->target_monitored
)
1097 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1099 continue; /* segtype doesn't support registration */
1102 * FIXME: We should really try again if pending
1104 monitored
= (pending
) ? 0 : monitored
;
1108 log_verbose("%s/%s already monitored.", lv
->vg
->name
, lv
->name
);
1109 else if (seg
->segtype
->ops
->target_monitor_events
)
1110 monitor_fn
= seg
->segtype
->ops
->target_monitor_events
;
1113 log_verbose("%s/%s already not monitored.", lv
->vg
->name
, lv
->name
);
1114 else if (seg
->segtype
->ops
->target_unmonitor_events
)
1115 monitor_fn
= seg
->segtype
->ops
->target_unmonitor_events
;
1118 /* Do [un]monitor */
1122 log_verbose("%sonitoring %s/%s%s", monitor
? "M" : "Not m", lv
->vg
->name
, lv
->name
,
1123 test_mode() ? " [Test mode: skipping this]" : "");
1125 /* FIXME Test mode should really continue a bit further. */
1129 /* FIXME specify events */
1130 if (!monitor_fn(seg
, 0)) {
1131 log_error("%s/%s: %s segment monitoring function failed.",
1132 lv
->vg
->name
, lv
->name
, seg
->segtype
->name
);
1136 /* Check [un]monitor results */
1137 /* Try a couple times if pending, but not forever... */
1138 for (i
= 0; i
< 10; i
++) {
1140 monitored
= seg
->segtype
->ops
->target_monitored(seg
, &pending
);
1142 (!monitored
&& monitor
) ||
1143 (monitored
&& !monitor
))
1144 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
1145 lv
->vg
->name
, lv
->name
, monitor
? "" : "un");
1152 r
= (monitored
&& monitor
) || (!monitored
&& !monitor
);
1161 struct detached_lv_data
{
1162 struct logical_volume
*lv_pre
;
1163 struct lv_activate_opts
*laopts
;
1164 int *flush_required
;
1167 static int _preload_detached_lv(struct cmd_context
*cmd
, struct logical_volume
*lv
, void *data
)
1169 struct detached_lv_data
*detached
= data
;
1170 struct lv_list
*lvl_pre
;
1172 if ((lvl_pre
= find_lv_in_vg(detached
->lv_pre
->vg
, lv
->name
))) {
1173 if (lv_is_visible(lvl_pre
->lv
) && lv_is_active(lv
) && (!lv_is_cow(lv
) || !lv_is_cow(lvl_pre
->lv
)) &&
1174 !_lv_preload(lvl_pre
->lv
, detached
->laopts
, detached
->flush_required
))
1181 static int _lv_suspend(struct cmd_context
*cmd
, const char *lvid_s
,
1182 struct lv_activate_opts
*laopts
, int error_if_not_suspended
)
1184 struct logical_volume
*lv
= NULL
, *lv_pre
= NULL
, *pvmove_lv
= NULL
;
1185 struct lv_list
*lvl_pre
;
1186 struct seg_list
*sl
;
1187 struct lv_segment
*snap_seg
;
1189 int r
= 0, lockfs
= 0, flush_required
= 0;
1190 struct detached_lv_data detached
;
1195 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1198 /* Use precommitted metadata if present */
1199 if (!(lv_pre
= lv_from_lvid(cmd
, lvid_s
, 1)))
1202 /* Ignore origin_only unless LV is origin in both old and new metadata */
1203 if (!lv_is_origin(lv
) || !lv_is_origin(lv_pre
))
1204 laopts
->origin_only
= 0;
1207 _skip("Suspending %s%s.", lv
->name
, laopts
->origin_only
? " origin without snapshots" : "");
1212 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1215 if (!info
.exists
|| info
.suspended
) {
1216 if (!error_if_not_suspended
) {
1219 critical_section_inc(cmd
, "already suspended");
1224 if (!lv_read_replicator_vgs(lv
))
1227 lv_calculate_readahead(lv
, NULL
);
1230 * Preload devices for the LV.
1231 * If the PVMOVE LV is being removed, it's only present in the old
1232 * metadata and not the new, so we must explicitly add the new
1233 * tables for all the changed LVs here, as the relationships
1234 * are not found by walking the new metadata.
1236 if (!(lv_pre
->status
& LOCKED
) &&
1237 (lv
->status
& LOCKED
) &&
1238 (pvmove_lv
= find_pvmove_lv_in_lv(lv
))) {
1239 /* Preload all the LVs above the PVMOVE LV */
1240 dm_list_iterate_items(sl
, &pvmove_lv
->segs_using_this_lv
) {
1241 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, sl
->seg
->lv
->name
))) {
1242 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", sl
->seg
->lv
->name
);
1245 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1248 /* Now preload the PVMOVE LV itself */
1249 if (!(lvl_pre
= find_lv_in_vg(lv_pre
->vg
, pvmove_lv
->name
))) {
1250 log_error(INTERNAL_ERROR
"LV %s missing from preload metadata", pvmove_lv
->name
);
1253 if (!_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1256 if (!_lv_preload(lv_pre
, laopts
, &flush_required
))
1257 /* FIXME Revert preloading */
1261 * Search for existing LVs that have become detached and preload them.
1263 detached
.lv_pre
= lv_pre
;
1264 detached
.laopts
= laopts
;
1265 detached
.flush_required
= &flush_required
;
1267 if (!for_each_sub_lv(cmd
, lv
, &_preload_detached_lv
, &detached
))
1271 * Preload any snapshots that are being removed.
1273 if (!laopts
->origin_only
&& lv_is_origin(lv
)) {
1274 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1275 if (!(lvl_pre
= find_lv_in_vg_by_lvid(lv_pre
->vg
, &snap_seg
->cow
->lvid
))) {
1276 log_error(INTERNAL_ERROR
"LV %s (%s) missing from preload metadata",
1277 snap_seg
->cow
->name
, snap_seg
->cow
->lvid
.id
[1].uuid
);
1280 if (!lv_is_cow(lvl_pre
->lv
) &&
1281 !_lv_preload(lvl_pre
->lv
, laopts
, &flush_required
))
1287 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 0))
1288 /* FIXME Consider aborting here */
1291 critical_section_inc(cmd
, "suspending");
1293 critical_section_inc(cmd
, "suspending pvmove LV");
1295 if (!laopts
->origin_only
&&
1296 (lv_is_origin(lv_pre
) || lv_is_cow(lv_pre
)))
1300 * Suspending an LV directly above a PVMOVE LV also
1301 * suspends other LVs using that same PVMOVE LV.
1302 * FIXME Remove this and delay the 'clear node' until
1303 * after the code knows whether there's a different
1304 * inactive table to load or not instead so lv_suspend
1305 * can be called separately for each LV safely.
1307 if ((lv_pre
->vg
->status
& PRECOMMITTED
) &&
1308 (lv_pre
->status
& LOCKED
) && find_pvmove_lv_in_lv(lv_pre
)) {
1309 if (!_lv_suspend_lv(lv_pre
, laopts
, lockfs
, flush_required
)) {
1310 critical_section_dec(cmd
, "failed precommitted suspend");
1312 critical_section_dec(cmd
, "failed precommitted suspend (pvmove)");
1316 /* Normal suspend */
1317 if (!_lv_suspend_lv(lv
, laopts
, lockfs
, flush_required
)) {
1318 critical_section_dec(cmd
, "failed suspend");
1320 critical_section_dec(cmd
, "failed suspend (pvmove)");
1328 release_vg(lv_pre
->vg
);
1330 lv_release_replicator_vgs(lv
);
1337 /* Returns success if the device is not active */
1338 int lv_suspend_if_active(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
1340 struct lv_activate_opts laopts
= { .origin_only
= origin_only
};
1342 return _lv_suspend(cmd
, lvid_s
, &laopts
, 0);
1345 /* No longer used */
1347 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1349 return _lv_suspend(cmd, lvid_s, 1);
1358 * @exclusive: This parameter only has an affect in cluster-context.
1359 * It forces local target type to be used (instead of
1360 * cluster-aware type).
1361 * @error_if_not_active
1363 static int _lv_resume(struct cmd_context
*cmd
, const char *lvid_s
,
1364 struct lv_activate_opts
*laopts
, int error_if_not_active
)
1366 struct logical_volume
*lv
;
1373 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1376 if (!lv_is_origin(lv
))
1377 laopts
->origin_only
= 0;
1380 _skip("Resuming %s%s%s.", lv
->name
, laopts
->origin_only
? " without snapshots" : "",
1381 laopts
->revert
? " (reverting)" : "");
1386 log_debug("Resuming LV %s/%s%s%s%s.", lv
->vg
->name
, lv
->name
,
1387 error_if_not_active
? "" : " if active",
1388 laopts
->origin_only
? " without snapshots" : "",
1389 laopts
->revert
? " (reverting)" : "");
1391 if (!lv_info(cmd
, lv
, laopts
->origin_only
, &info
, 0, 0))
1394 if (!info
.exists
|| !info
.suspended
) {
1395 if (error_if_not_active
)
1398 if (!info
.suspended
)
1399 critical_section_dec(cmd
, "already resumed");
1403 if (!_lv_activate_lv(lv
, laopts
))
1406 critical_section_dec(cmd
, "resumed");
1408 if (!monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1419 /* Returns success if the device is not active */
1420 int lv_resume_if_active(struct cmd_context
*cmd
, const char *lvid_s
,
1421 unsigned origin_only
, unsigned exclusive
, unsigned revert
)
1423 struct lv_activate_opts laopts
= {
1424 .origin_only
= origin_only
,
1426 * When targets are activated exclusively in a cluster, the
1427 * non-clustered target should be used. This only happens
1428 * if exclusive is set.
1430 .exclusive
= exclusive
,
1434 return _lv_resume(cmd
, lvid_s
, &laopts
, 0);
1437 int lv_resume(struct cmd_context
*cmd
, const char *lvid_s
, unsigned origin_only
)
1439 struct lv_activate_opts laopts
= { .origin_only
= origin_only
, };
1441 return _lv_resume(cmd
, lvid_s
, &laopts
, 1);
1444 static int _lv_has_open_snapshots(struct logical_volume
*lv
)
1446 struct lv_segment
*snap_seg
;
1450 dm_list_iterate_items_gen(snap_seg
, &lv
->snapshot_segs
, origin_list
) {
1451 if (!lv_info(lv
->vg
->cmd
, snap_seg
->cow
, 0, &info
, 1, 0)) {
1456 if (info
.exists
&& info
.open_count
) {
1457 log_error("LV %s/%s has open snapshot %s: "
1458 "not deactivating", lv
->vg
->name
, lv
->name
,
1459 snap_seg
->cow
->name
);
1467 int lv_deactivate(struct cmd_context
*cmd
, const char *lvid_s
)
1469 struct logical_volume
*lv
;
1476 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1480 _skip("Deactivating '%s'.", lv
->name
);
1485 log_debug("Deactivating %s/%s.", lv
->vg
->name
, lv
->name
);
1487 if (!lv_info(cmd
, lv
, 0, &info
, 1, 0))
1495 if (lv_is_visible(lv
)) {
1496 if (!lv_check_not_in_use(cmd
, lv
, &info
))
1499 if (lv_is_origin(lv
) && _lv_has_open_snapshots(lv
))
1503 if (!lv_read_replicator_vgs(lv
))
1506 lv_calculate_readahead(lv
, NULL
);
1508 if (!monitor_dev_for_events(cmd
, lv
, NULL
, 0))
1511 critical_section_inc(cmd
, "deactivating");
1512 r
= _lv_deactivate(lv
);
1513 critical_section_dec(cmd
, "deactivated");
1515 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0) || info
.exists
)
1519 lv_release_replicator_vgs(lv
);
1526 /* Test if LV passes filter */
1527 int lv_activation_filter(struct cmd_context
*cmd
, const char *lvid_s
,
1530 struct logical_volume
*lv
;
1533 if (!activation()) {
1538 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1541 if (!_passes_activation_filter(cmd
, lv
)) {
1542 log_verbose("Not activating %s/%s since it does not pass "
1543 "activation filter.", lv
->vg
->name
, lv
->name
);
1555 static int _lv_activate(struct cmd_context
*cmd
, const char *lvid_s
,
1556 struct lv_activate_opts
*laopts
, int filter
)
1558 struct logical_volume
*lv
;
1565 if (!(lv
= lv_from_lvid(cmd
, lvid_s
, 0)))
1568 if (filter
&& !_passes_activation_filter(cmd
, lv
)) {
1569 log_error("Not activating %s/%s since it does not pass "
1570 "activation filter.", lv
->vg
->name
, lv
->name
);
1574 if ((!lv
->vg
->cmd
->partial_activation
) && (lv
->status
& PARTIAL_LV
)) {
1575 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1580 if (lv_has_unknown_segments(lv
)) {
1581 log_error("Refusing activation of LV %s containing "
1582 "an unrecognised segment.", lv
->name
);
1587 _skip("Activating '%s'.", lv
->name
);
1592 log_debug("Activating %s/%s%s.", lv
->vg
->name
, lv
->name
,
1593 laopts
->exclusive
? " exclusively" : "");
1595 if (!lv_info(cmd
, lv
, 0, &info
, 0, 0))
1598 if (info
.exists
&& !info
.suspended
&& info
.live_table
) {
1603 if (!lv_read_replicator_vgs(lv
))
1606 lv_calculate_readahead(lv
, NULL
);
1608 critical_section_inc(cmd
, "activating");
1609 if (!(r
= _lv_activate_lv(lv
, laopts
)))
1611 critical_section_dec(cmd
, "activated");
1613 if (r
&& !monitor_dev_for_events(cmd
, lv
, laopts
, 1))
1618 lv_release_replicator_vgs(lv
);
1626 int lv_activate(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1628 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1630 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 0))
1636 /* Activate LV only if it passes filter */
1637 int lv_activate_with_filter(struct cmd_context
*cmd
, const char *lvid_s
, int exclusive
)
1639 struct lv_activate_opts laopts
= { .exclusive
= exclusive
};
1641 if (!_lv_activate(cmd
, lvid_s
, &laopts
, 1))
1647 int lv_mknodes(struct cmd_context
*cmd
, const struct logical_volume
*lv
)
1652 r
= dm_mknodes(NULL
);
1660 r
= dev_manager_mknodes(lv
);
1668 * Does PV use VG somewhere in its construction?
1669 * Returns 1 on failure.
1671 int pv_uses_vg(struct physical_volume
*pv
,
1672 struct volume_group
*vg
)
1677 if (!dm_is_dm_major(MAJOR(pv
->dev
->dev
)))
1680 return dev_manager_device_uses_vg(pv
->dev
, vg
);
1683 void activation_release(void)
1685 dev_manager_release();
1688 void activation_exit(void)