Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / lib / activate / activate.c
blobd884150ea52042736262d4694e8b3577f451f8dd
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "lib.h"
19 #include "metadata.h"
20 #include "activate.h"
21 #include "memlock.h"
22 #include "display.h"
23 #include "fs.h"
24 #include "lvm-exec.h"
25 #include "lvm-file.h"
26 #include "lvm-string.h"
27 #include "toolcontext.h"
28 #include "dev_manager.h"
29 #include "str_list.h"
30 #include "config.h"
31 #include "filter.h"
32 #include "segtype.h"
34 #include <limits.h>
35 #include <fcntl.h>
36 #include <unistd.h>
38 #define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)
40 int lvm1_present(struct cmd_context *cmd)
42 char path[PATH_MAX];
44 if (dm_snprintf(path, sizeof(path), "%s/lvm/global", cmd->proc_dir)
45 < 0) {
46 log_error("LVM1 proc global snprintf failed");
47 return 0;
50 if (path_exists(path))
51 return 1;
52 else
53 return 0;
56 int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg,
57 struct dm_list *modules)
59 unsigned int s;
60 struct lv_segment *seg2, *snap_seg;
61 struct dm_list *snh;
63 if (seg->segtype->ops->modules_needed &&
64 !seg->segtype->ops->modules_needed(mem, seg, modules)) {
65 log_error("module string allocation failed");
66 return 0;
69 if (lv_is_origin(seg->lv))
70 dm_list_iterate(snh, &seg->lv->snapshot_segs)
71 if (!list_lv_modules(mem,
72 dm_list_struct_base(snh,
73 struct lv_segment,
74 origin_list)->cow,
75 modules))
76 return_0;
78 if (lv_is_cow(seg->lv)) {
79 snap_seg = find_cow(seg->lv);
80 if (snap_seg->segtype->ops->modules_needed &&
81 !snap_seg->segtype->ops->modules_needed(mem, snap_seg,
82 modules)) {
83 log_error("snap_seg module string allocation failed");
84 return 0;
88 for (s = 0; s < seg->area_count; s++) {
89 switch (seg_type(seg, s)) {
90 case AREA_LV:
91 seg2 = find_seg_by_le(seg_lv(seg, s), seg_le(seg, s));
92 if (seg2 && !list_segment_modules(mem, seg2, modules))
93 return_0;
94 break;
95 case AREA_PV:
96 case AREA_UNASSIGNED:
101 return 1;
104 int list_lv_modules(struct dm_pool *mem, const struct logical_volume *lv,
105 struct dm_list *modules)
107 struct lv_segment *seg;
109 dm_list_iterate_items(seg, &lv->segments)
110 if (!list_segment_modules(mem, seg, modules))
111 return_0;
113 return 1;
116 #ifndef DEVMAPPER_SUPPORT
117 void set_activation(int act)
119 static int warned = 0;
121 if (warned || !act)
122 return;
124 log_error("Compiled without libdevmapper support. "
125 "Can't enable activation.");
127 warned = 1;
129 int activation(void)
131 return 0;
133 int library_version(char *version, size_t size)
135 return 0;
137 int driver_version(char *version, size_t size)
139 return 0;
141 int target_version(const char *target_name, uint32_t *maj,
142 uint32_t *min, uint32_t *patchlevel)
144 return 0;
146 int target_present(struct cmd_context *cmd, const char *target_name,
147 int use_modprobe)
149 return 0;
151 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info,
152 int with_open_count, int with_read_ahead)
154 return 0;
156 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
157 struct lvinfo *info, int with_open_count, int with_read_ahead)
159 return 0;
161 int lv_snapshot_percent(const struct logical_volume *lv, float *percent,
162 percent_range_t *percent_range)
164 return 0;
166 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
167 int wait, float *percent, percent_range_t *percent_range,
168 uint32_t *event_nr)
170 return 0;
172 int lvs_in_vg_activated(struct volume_group *vg)
174 return 0;
176 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg)
178 return 0;
180 int lvs_in_vg_opened(struct volume_group *vg)
182 return 0;
184 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
186 return 1;
188 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s)
190 return 1;
192 int lv_resume(struct cmd_context *cmd, const char *lvid_s)
194 return 1;
196 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s)
198 return 1;
200 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
202 return 1;
204 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
205 int *activate_lv)
207 return 1;
209 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
211 return 1;
213 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
215 return 1;
218 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
220 return 1;
223 int pv_uses_vg(struct physical_volume *pv,
224 struct volume_group *vg)
226 return 0;
229 void activation_release(void)
231 return;
234 void activation_exit(void)
236 return;
239 #else /* DEVMAPPER_SUPPORT */
241 static int _activation = 1;
243 void set_activation(int act)
245 if (act == _activation)
246 return;
248 _activation = act;
249 if (_activation)
250 log_verbose("Activation enabled. Device-mapper kernel "
251 "driver will be used.");
252 else
253 log_warn("WARNING: Activation disabled. No device-mapper "
254 "interaction will be attempted.");
257 int activation(void)
259 return _activation;
262 static int _passes_activation_filter(struct cmd_context *cmd,
263 struct logical_volume *lv)
265 const struct config_node *cn;
266 struct config_value *cv;
267 char *str;
268 char path[PATH_MAX];
270 if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
271 /* If no host tags defined, activate */
272 if (dm_list_empty(&cmd->tags))
273 return 1;
275 /* If any host tag matches any LV or VG tag, activate */
276 if (str_list_match_list(&cmd->tags, &lv->tags) ||
277 str_list_match_list(&cmd->tags, &lv->vg->tags))
278 return 1;
280 /* Don't activate */
281 return 0;
284 for (cv = cn->v; cv; cv = cv->next) {
285 if (cv->type != CFG_STRING) {
286 log_error("Ignoring invalid string in config file "
287 "activation/volume_list");
288 continue;
290 str = cv->v.str;
291 if (!*str) {
292 log_error("Ignoring empty string in config file "
293 "activation/volume_list");
294 continue;
297 /* Tag? */
298 if (*str == '@') {
299 str++;
300 if (!*str) {
301 log_error("Ignoring empty tag in config file "
302 "activation/volume_list");
303 continue;
305 /* If any host tag matches any LV or VG tag, activate */
306 if (!strcmp(str, "*")) {
307 if (str_list_match_list(&cmd->tags, &lv->tags)
308 || str_list_match_list(&cmd->tags,
309 &lv->vg->tags))
310 return 1;
311 else
312 continue;
314 /* If supplied tag matches LV or VG tag, activate */
315 if (str_list_match_item(&lv->tags, str) ||
316 str_list_match_item(&lv->vg->tags, str))
317 return 1;
318 else
319 continue;
321 if (!strchr(str, '/')) {
322 /* vgname supplied */
323 if (!strcmp(str, lv->vg->name))
324 return 1;
325 else
326 continue;
328 /* vgname/lvname */
329 if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name,
330 lv->name) < 0) {
331 log_error("dm_snprintf error from %s/%s", lv->vg->name,
332 lv->name);
333 continue;
335 if (!strcmp(path, str))
336 return 1;
339 return 0;
342 int library_version(char *version, size_t size)
344 if (!activation())
345 return 0;
347 return dm_get_library_version(version, size);
350 int driver_version(char *version, size_t size)
352 if (!activation())
353 return 0;
355 log_very_verbose("Getting driver version");
357 return dm_driver_version(version, size);
360 int target_version(const char *target_name, uint32_t *maj,
361 uint32_t *min, uint32_t *patchlevel)
363 int r = 0;
364 struct dm_task *dmt;
365 struct dm_versions *target, *last_target;
367 log_very_verbose("Getting target version for %s", target_name);
368 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
369 return_0;
371 if (!dm_task_run(dmt)) {
372 log_debug("Failed to get %s target version", target_name);
373 /* Assume this was because LIST_VERSIONS isn't supported */
374 return 1;
377 target = dm_task_get_versions(dmt);
379 do {
380 last_target = target;
382 if (!strcmp(target_name, target->name)) {
383 r = 1;
384 *maj = target->version[0];
385 *min = target->version[1];
386 *patchlevel = target->version[2];
387 goto out;
390 target = (void *) target + target->next;
391 } while (last_target != target);
393 out:
394 dm_task_destroy(dmt);
396 return r;
399 int module_present(struct cmd_context *cmd, const char *target_name)
401 int ret = 0;
402 #ifdef MODPROBE_CMD
403 char module[128];
404 const char *argv[3];
406 if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) {
407 log_error("module_present module name too long: %s",
408 target_name);
409 return 0;
412 argv[0] = MODPROBE_CMD;
413 argv[1] = module;
414 argv[2] = NULL;
416 ret = exec_cmd(cmd, argv);
417 #endif
418 return ret;
421 int target_present(struct cmd_context *cmd, const char *target_name,
422 int use_modprobe)
424 uint32_t maj, min, patchlevel;
426 if (!activation())
427 return 0;
429 #ifdef MODPROBE_CMD
430 if (use_modprobe) {
431 if (target_version(target_name, &maj, &min, &patchlevel))
432 return 1;
434 if (!module_present(cmd, target_name))
435 return_0;
437 #endif
439 return target_version(target_name, &maj, &min, &patchlevel);
443 * Returns 1 if info structure populated, else 0 on failure.
445 static int _lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int with_mknodes,
446 struct lvinfo *info, int with_open_count, int with_read_ahead, unsigned by_uuid_only)
448 struct dm_info dminfo;
449 char *name = NULL;
451 if (!activation())
452 return 0;
454 if (!by_uuid_only &&
455 !(name = build_dm_name(cmd->mem, lv->vg->name, lv->name, NULL)))
456 return_0;
458 log_debug("Getting device info for %s", name);
459 if (!dev_manager_info(lv->vg->cmd->mem, name, lv, with_mknodes,
460 with_open_count, with_read_ahead, &dminfo,
461 &info->read_ahead)) {
462 if (name)
463 dm_pool_free(cmd->mem, name);
464 return_0;
467 info->exists = dminfo.exists;
468 info->suspended = dminfo.suspended;
469 info->open_count = dminfo.open_count;
470 info->major = dminfo.major;
471 info->minor = dminfo.minor;
472 info->read_only = dminfo.read_only;
473 info->live_table = dminfo.live_table;
474 info->inactive_table = dminfo.inactive_table;
476 if (name)
477 dm_pool_free(cmd->mem, name);
479 return 1;
482 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info,
483 int with_open_count, int with_read_ahead)
485 return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0);
488 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
489 struct lvinfo *info, int with_open_count, int with_read_ahead)
491 struct logical_volume *lv;
493 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
494 return 0;
496 return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0);
500 * Returns 1 if percent set, else 0 on failure.
502 int lv_snapshot_percent(const struct logical_volume *lv, float *percent,
503 percent_range_t *percent_range)
505 int r;
506 struct dev_manager *dm;
508 if (!activation())
509 return 0;
511 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
512 return_0;
514 if (!(r = dev_manager_snapshot_percent(dm, lv, percent, percent_range)))
515 stack;
517 dev_manager_destroy(dm);
519 return r;
522 /* FIXME Merge with snapshot_percent */
523 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
524 int wait, float *percent, percent_range_t *percent_range,
525 uint32_t *event_nr)
527 int r;
528 struct dev_manager *dm;
529 struct lvinfo info;
531 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
532 * it should be considered in-sync. */
533 if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
534 *percent = 100.0;
535 return 1;
538 if (!activation())
539 return 0;
541 if (!lv_info(cmd, lv, &info, 0, 0))
542 return_0;
544 if (!info.exists)
545 return 0;
547 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
548 return_0;
550 if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent,
551 percent_range, event_nr)))
552 stack;
554 dev_manager_destroy(dm);
556 return r;
559 static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv,
560 unsigned by_uuid_only)
562 struct lvinfo info;
564 if (!_lv_info(cmd, lv, 0, &info, 0, 0, by_uuid_only)) {
565 stack;
566 return -1;
569 return info.exists;
572 static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
574 struct lvinfo info;
576 if (!lv_info(cmd, lv, &info, 1, 0)) {
577 stack;
578 return -1;
581 return info.open_count;
584 static int _lv_activate_lv(struct logical_volume *lv)
586 int r;
587 struct dev_manager *dm;
589 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
590 return_0;
592 if (!(r = dev_manager_activate(dm, lv)))
593 stack;
595 dev_manager_destroy(dm);
596 return r;
599 static int _lv_preload(struct logical_volume *lv, int *flush_required)
601 int r;
602 struct dev_manager *dm;
604 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
605 return_0;
607 if (!(r = dev_manager_preload(dm, lv, flush_required)))
608 stack;
610 dev_manager_destroy(dm);
611 return r;
614 static int _lv_deactivate(struct logical_volume *lv)
616 int r;
617 struct dev_manager *dm;
619 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
620 return_0;
622 if (!(r = dev_manager_deactivate(dm, lv)))
623 stack;
625 dev_manager_destroy(dm);
626 return r;
629 static int _lv_suspend_lv(struct logical_volume *lv, int lockfs, int flush_required)
631 int r;
632 struct dev_manager *dm;
634 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
635 return_0;
637 if (!(r = dev_manager_suspend(dm, lv, lockfs, flush_required)))
638 stack;
640 dev_manager_destroy(dm);
641 return r;
645 * These two functions return the number of visible LVs in the state,
646 * or -1 on error.
648 static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only)
650 struct lv_list *lvl;
651 int count = 0;
653 if (!activation())
654 return 0;
656 dm_list_iterate_items(lvl, &vg->lvs) {
657 if (lv_is_visible(lvl->lv))
658 count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1);
661 return count;
664 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg)
666 return _lvs_in_vg_activated(vg, 1);
669 int lvs_in_vg_activated(struct volume_group *vg)
671 return _lvs_in_vg_activated(vg, 0);
674 int lvs_in_vg_opened(const struct volume_group *vg)
676 const struct lv_list *lvl;
677 int count = 0;
679 if (!activation())
680 return 0;
682 dm_list_iterate_items(lvl, &vg->lvs) {
683 if (lv_is_visible(lvl->lv))
684 count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
687 return count;
691 * Determine whether an LV is active locally or in a cluster.
692 * Assumes vg lock held.
693 * Returns:
694 * 0 - not active locally or on any node in cluster
695 * 1 - active either locally or some node in the cluster
697 int lv_is_active(struct logical_volume *lv)
699 int ret;
701 if (_lv_active(lv->vg->cmd, lv, 0))
702 return 1;
704 if (!vg_is_clustered(lv->vg))
705 return 0;
707 if ((ret = remote_lock_held(lv->lvid.s)) >= 0)
708 return ret;
711 * Old compatibility code if locking doesn't support lock query
712 * FIXME: check status to not deactivate already activate device
714 if (activate_lv_excl(lv->vg->cmd, lv)) {
715 deactivate_lv(lv->vg->cmd, lv);
716 return 0;
720 * Exclusive local activation failed so assume it is active elsewhere.
722 return 1;
726 * Returns 0 if an attempt to (un)monitor the device failed.
727 * Returns 1 otherwise.
729 int monitor_dev_for_events(struct cmd_context *cmd,
730 struct logical_volume *lv, int monitor)
732 #ifdef DMEVENTD
733 int i, pending = 0, monitored;
734 int r = 1;
735 struct dm_list *tmp, *snh, *snht;
736 struct lv_segment *seg;
737 int (*monitor_fn) (struct lv_segment *s, int e);
738 uint32_t s;
740 /* skip dmeventd code altogether */
741 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
742 return 1;
745 * Nothing to do if dmeventd configured not to be used.
747 if (monitor && !dmeventd_monitor_mode())
748 return 1;
751 * In case of a snapshot device, we monitor lv->snapshot->lv,
752 * not the actual LV itself.
754 if (lv_is_cow(lv))
755 return monitor_dev_for_events(cmd, lv->snapshot->lv, monitor);
758 * In case this LV is a snapshot origin, we instead monitor
759 * each of its respective snapshots (the origin itself does
760 * not need to be monitored).
762 * TODO: This may change when snapshots of mirrors are allowed.
764 if (lv_is_origin(lv)) {
765 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
766 if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
767 struct lv_segment, origin_list)->cow, monitor))
768 r = 0;
769 return r;
772 dm_list_iterate(tmp, &lv->segments) {
773 seg = dm_list_item(tmp, struct lv_segment);
775 /* Recurse for AREA_LV */
776 for (s = 0; s < seg->area_count; s++) {
777 if (seg_type(seg, s) != AREA_LV)
778 continue;
779 if (!monitor_dev_for_events(cmd, seg_lv(seg, s),
780 monitor)) {
781 log_error("Failed to %smonitor %s",
782 monitor ? "" : "un",
783 seg_lv(seg, s)->name);
784 r = 0;
788 if (!seg_monitored(seg) || (seg->status & PVMOVE))
789 continue;
791 monitor_fn = NULL;
793 /* Check monitoring status */
794 if (seg->segtype->ops->target_monitored)
795 monitored = seg->segtype->ops->target_monitored(seg, &pending);
796 else
797 continue; /* segtype doesn't support registration */
800 * FIXME: We should really try again if pending
802 monitored = (pending) ? 0 : monitored;
804 if (monitor) {
805 if (monitored)
806 log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
807 else if (seg->segtype->ops->target_monitor_events)
808 monitor_fn = seg->segtype->ops->target_monitor_events;
809 } else {
810 if (!monitored)
811 log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
812 else if (seg->segtype->ops->target_unmonitor_events)
813 monitor_fn = seg->segtype->ops->target_unmonitor_events;
816 /* Do [un]monitor */
817 if (!monitor_fn)
818 continue;
820 log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name);
822 /* FIXME specify events */
823 if (!monitor_fn(seg, 0)) {
824 log_error("%s/%s: %s segment monitoring function failed.",
825 lv->vg->name, lv->name, seg->segtype->name);
826 return 0;
829 /* Check [un]monitor results */
830 /* Try a couple times if pending, but not forever... */
831 for (i = 0; i < 10; i++) {
832 pending = 0;
833 monitored = seg->segtype->ops->target_monitored(seg, &pending);
834 if (pending ||
835 (!monitored && monitor) ||
836 (monitored && !monitor))
837 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
838 lv->vg->name, lv->name, monitor ? "" : "un");
839 else
840 break;
841 sleep(1);
844 r = (monitored && monitor) || (!monitored && !monitor);
847 return r;
848 #else
849 return 1;
850 #endif
853 static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
854 int error_if_not_suspended)
856 struct logical_volume *lv = NULL, *lv_pre = NULL;
857 struct lvinfo info;
858 int r = 0, lockfs = 0, flush_required = 0;
860 if (!activation())
861 return 1;
863 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
864 goto_out;
866 /* Use precommitted metadata if present */
867 if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
868 goto_out;
870 if (test_mode()) {
871 _skip("Suspending '%s'.", lv->name);
872 r = 1;
873 goto out;
876 if (!lv_info(cmd, lv, &info, 0, 0))
877 goto_out;
879 if (!info.exists || info.suspended) {
880 r = error_if_not_suspended ? 0 : 1;
881 goto out;
884 lv_calculate_readahead(lv, NULL);
886 /* If VG was precommitted, preload devices for the LV */
887 if ((lv_pre->vg->status & PRECOMMITTED)) {
888 if (!_lv_preload(lv_pre, &flush_required)) {
889 /* FIXME Revert preloading */
890 goto_out;
894 if (!monitor_dev_for_events(cmd, lv, 0))
895 /* FIXME Consider aborting here */
896 stack;
898 memlock_inc();
900 if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre))
901 lockfs = 1;
903 if (!_lv_suspend_lv(lv, lockfs, flush_required)) {
904 memlock_dec();
905 fs_unlock();
906 goto out;
909 r = 1;
910 out:
911 if (lv_pre)
912 vg_release(lv_pre->vg);
913 if (lv)
914 vg_release(lv->vg);
916 return r;
919 /* Returns success if the device is not active */
920 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s)
922 return _lv_suspend(cmd, lvid_s, 0);
925 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
927 return _lv_suspend(cmd, lvid_s, 1);
930 static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
931 int error_if_not_active)
933 struct logical_volume *lv;
934 struct lvinfo info;
935 int r = 0;
937 if (!activation())
938 return 1;
940 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
941 goto_out;
943 if (test_mode()) {
944 _skip("Resuming '%s'.", lv->name);
945 r = 1;
946 goto out;
949 if (!lv_info(cmd, lv, &info, 0, 0))
950 goto_out;
952 if (!info.exists || !info.suspended) {
953 r = error_if_not_active ? 0 : 1;
954 goto_out;
957 if (!_lv_activate_lv(lv))
958 goto_out;
960 memlock_dec();
961 fs_unlock();
963 if (!monitor_dev_for_events(cmd, lv, 1))
964 stack;
966 r = 1;
967 out:
968 if (lv)
969 vg_release(lv->vg);
971 return r;
974 /* Returns success if the device is not active */
975 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s)
977 return _lv_resume(cmd, lvid_s, 0);
980 int lv_resume(struct cmd_context *cmd, const char *lvid_s)
982 return _lv_resume(cmd, lvid_s, 1);
985 static int _lv_has_open_snapshots(struct logical_volume *lv)
987 struct lv_segment *snap_seg;
988 struct lvinfo info;
989 int r = 0;
991 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
992 if (!lv_info(lv->vg->cmd, snap_seg->cow, &info, 1, 0)) {
993 r = 1;
994 continue;
997 if (info.exists && info.open_count) {
998 log_error("LV %s/%s has open snapshot %s: "
999 "not deactivating", lv->vg->name, lv->name,
1000 snap_seg->cow->name);
1001 r = 1;
1005 return r;
1008 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
1010 struct logical_volume *lv;
1011 struct lvinfo info;
1012 int r = 0;
1014 if (!activation())
1015 return 1;
1017 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1018 goto out;
1020 if (test_mode()) {
1021 _skip("Deactivating '%s'.", lv->name);
1022 r = 1;
1023 goto out;
1026 if (!lv_info(cmd, lv, &info, 1, 0))
1027 goto_out;
1029 if (!info.exists) {
1030 r = 1;
1031 goto out;
1034 if (lv_is_visible(lv)) {
1035 if (info.open_count) {
1036 log_error("LV %s/%s in use: not deactivating",
1037 lv->vg->name, lv->name);
1038 goto out;
1040 if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1041 goto_out;
1044 lv_calculate_readahead(lv, NULL);
1046 if (!monitor_dev_for_events(cmd, lv, 0))
1047 stack;
1049 memlock_inc();
1050 r = _lv_deactivate(lv);
1051 memlock_dec();
1052 fs_unlock();
1054 if (!lv_info(cmd, lv, &info, 1, 0) || info.exists)
1055 r = 0;
1056 out:
1057 if (lv)
1058 vg_release(lv->vg);
1060 return r;
1063 /* Test if LV passes filter */
1064 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
1065 int *activate_lv)
1067 struct logical_volume *lv;
1068 int r = 0;
1070 if (!activation()) {
1071 *activate_lv = 1;
1072 return 1;
1075 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1076 goto out;
1078 if (!_passes_activation_filter(cmd, lv)) {
1079 log_verbose("Not activating %s/%s due to config file settings",
1080 lv->vg->name, lv->name);
1081 *activate_lv = 0;
1082 } else
1083 *activate_lv = 1;
1084 r = 1;
1085 out:
1086 if (lv)
1087 vg_release(lv->vg);
1089 return r;
1092 static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
1093 int exclusive, int filter)
1095 struct logical_volume *lv;
1096 struct lvinfo info;
1097 int r = 0;
1099 if (!activation())
1100 return 1;
1102 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1103 goto out;
1105 if (filter && !_passes_activation_filter(cmd, lv)) {
1106 log_verbose("Not activating %s/%s due to config file settings",
1107 lv->vg->name, lv->name);
1108 goto out;
1111 if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
1112 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1113 lv->name);
1114 goto_out;
1117 if (lv_has_unknown_segments(lv)) {
1118 log_error("Refusing activation of LV %s containing "
1119 "an unrecognised segment.", lv->name);
1120 goto_out;
1123 if (test_mode()) {
1124 _skip("Activating '%s'.", lv->name);
1125 r = 1;
1126 goto out;
1129 if (!lv_info(cmd, lv, &info, 0, 0))
1130 goto_out;
1132 if (info.exists && !info.suspended && info.live_table) {
1133 r = 1;
1134 goto out;
1137 lv_calculate_readahead(lv, NULL);
1139 if (exclusive)
1140 lv->status |= ACTIVATE_EXCL;
1142 memlock_inc();
1143 if (!(r = _lv_activate_lv(lv)))
1144 stack;
1145 memlock_dec();
1146 fs_unlock();
1148 if (r && !monitor_dev_for_events(cmd, lv, 1))
1149 stack;
1151 out:
1152 if (lv)
1153 vg_release(lv->vg);
1155 return r;
1158 /* Activate LV */
1159 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1161 if (!_lv_activate(cmd, lvid_s, exclusive, 0))
1162 return_0;
1164 return 1;
1167 /* Activate LV only if it passes filter */
1168 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1170 if (!_lv_activate(cmd, lvid_s, exclusive, 1))
1171 return_0;
1173 return 1;
1176 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1178 struct lvinfo info;
1179 int r = 1;
1181 if (!lv) {
1182 r = dm_mknodes(NULL);
1183 fs_unlock();
1184 return r;
1187 if (!_lv_info(cmd, lv, 1, &info, 0, 0, 0))
1188 return_0;
1190 if (info.exists) {
1191 if (lv_is_visible(lv))
1192 r = dev_manager_lv_mknodes(lv);
1193 } else
1194 r = dev_manager_lv_rmnodes(lv);
1196 fs_unlock();
1198 return r;
1202 * Does PV use VG somewhere in its construction?
1203 * Returns 1 on failure.
1205 int pv_uses_vg(struct physical_volume *pv,
1206 struct volume_group *vg)
1208 if (!activation())
1209 return 0;
1211 if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1212 return 0;
1214 return dev_manager_device_uses_vg(pv->dev, vg);
1217 void activation_release(void)
1219 dev_manager_release();
1222 void activation_exit(void)
1224 dev_manager_exit();
1226 #endif