xref: /netbsd-src/external/gpl2/lvm2/dist/lib/activate/activate.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: activate.c,v 1.1.1.2 2009/02/18 11:16:51 haad Exp $	*/
2 
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6  *
7  * This file is part of LVM2.
8  *
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.
12  *
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
16  */
17 
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"
33 
34 #include <limits.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 
38 #define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)
39 
40 int lvm1_present(struct cmd_context *cmd)
41 {
42 	char path[PATH_MAX];
43 
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;
48 	}
49 
50 	if (path_exists(path))
51 		return 1;
52 	else
53 		return 0;
54 }
55 
56 int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg,
57 			 struct dm_list *modules)
58 {
59 	unsigned int s;
60 	struct lv_segment *seg2, *snap_seg;
61 	struct dm_list *snh;
62 
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;
67 	}
68 
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;
77 
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;
85 		}
86 	}
87 
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:
97 			;
98 		}
99 	}
100 
101 	return 1;
102 }
103 
104 int list_lv_modules(struct dm_pool *mem, const struct logical_volume *lv,
105 		    struct dm_list *modules)
106 {
107 	struct lv_segment *seg;
108 
109 	dm_list_iterate_items(seg, &lv->segments)
110 		if (!list_segment_modules(mem, seg, modules))
111 			return_0;
112 
113 	return 1;
114 }
115 
116 #ifndef DEVMAPPER_SUPPORT
117 void set_activation(int act)
118 {
119 	static int warned = 0;
120 
121 	if (warned || !act)
122 		return;
123 
124 	log_error("Compiled without libdevmapper support. "
125 		  "Can't enable activation.");
126 
127 	warned = 1;
128 }
129 int activation(void)
130 {
131 	return 0;
132 }
133 int library_version(char *version, size_t size)
134 {
135 	return 0;
136 }
137 int driver_version(char *version, size_t size)
138 {
139 	return 0;
140 }
141 int target_version(const char *target_name, uint32_t *maj,
142 		   uint32_t *min, uint32_t *patchlevel)
143 {
144 	return 0;
145 }
146 int target_present(const char *target_name, int use_modprobe)
147 {
148 	return 0;
149 }
150 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info,
151 	    int with_open_count, int with_read_ahead)
152 {
153 	return 0;
154 }
155 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
156 		    struct lvinfo *info, int with_open_count, int with_read_ahead)
157 {
158 	return 0;
159 }
160 int lv_snapshot_percent(const struct logical_volume *lv, float *percent)
161 {
162 	return 0;
163 }
164 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
165 		      int wait, float *percent, uint32_t *event_nr)
166 {
167 	return 0;
168 }
169 int lvs_in_vg_activated(struct volume_group *vg)
170 {
171 	return 0;
172 }
173 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg)
174 {
175 	return 0;
176 }
177 int lvs_in_vg_opened(struct volume_group *vg)
178 {
179 	return 0;
180 }
181 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
182 {
183 	return 1;
184 }
185 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s)
186 {
187 	return 1;
188 }
189 int lv_resume(struct cmd_context *cmd, const char *lvid_s)
190 {
191 	return 1;
192 }
193 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s)
194 {
195 	return 1;
196 }
197 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
198 {
199 	return 1;
200 }
201 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
202 			 int *activate_lv)
203 {
204 	return 1;
205 }
206 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
207 {
208 	return 1;
209 }
210 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
211 {
212 	return 1;
213 }
214 
215 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
216 {
217 	return 1;
218 }
219 
220 int pv_uses_vg(struct physical_volume *pv,
221 	       struct volume_group *vg)
222 {
223 	return 0;
224 }
225 
226 void activation_release(void)
227 {
228 	return;
229 }
230 
231 void activation_exit(void)
232 {
233 	return;
234 }
235 
236 #else				/* DEVMAPPER_SUPPORT */
237 
238 static int _activation = 1;
239 
240 void set_activation(int act)
241 {
242 	if (act == _activation)
243 		return;
244 
245 	_activation = act;
246 	if (_activation)
247 		log_verbose("Activation enabled. Device-mapper kernel "
248 			    "driver will be used.");
249 	else
250 		log_warn("WARNING: Activation disabled. No device-mapper "
251 			  "interaction will be attempted.");
252 }
253 
254 int activation(void)
255 {
256 	return _activation;
257 }
258 
259 static int _passes_activation_filter(struct cmd_context *cmd,
260 				     struct logical_volume *lv)
261 {
262 	const struct config_node *cn;
263 	struct config_value *cv;
264 	char *str;
265 	char path[PATH_MAX];
266 
267 	if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
268 		/* If no host tags defined, activate */
269 		if (dm_list_empty(&cmd->tags))
270 			return 1;
271 
272 		/* If any host tag matches any LV or VG tag, activate */
273 		if (str_list_match_list(&cmd->tags, &lv->tags) ||
274 		    str_list_match_list(&cmd->tags, &lv->vg->tags))
275 			return 1;
276 
277 		/* Don't activate */
278 		return 0;
279 	}
280 
281 	for (cv = cn->v; cv; cv = cv->next) {
282 		if (cv->type != CFG_STRING) {
283 			log_error("Ignoring invalid string in config file "
284 				  "activation/volume_list");
285 			continue;
286 		}
287 		str = cv->v.str;
288 		if (!*str) {
289 			log_error("Ignoring empty string in config file "
290 				  "activation/volume_list");
291 			continue;
292 		}
293 
294 		/* Tag? */
295 		if (*str == '@') {
296 			str++;
297 			if (!*str) {
298 				log_error("Ignoring empty tag in config file "
299 					  "activation/volume_list");
300 				continue;
301 			}
302 			/* If any host tag matches any LV or VG tag, activate */
303 			if (!strcmp(str, "*")) {
304 				if (str_list_match_list(&cmd->tags, &lv->tags)
305 				    || str_list_match_list(&cmd->tags,
306 							   &lv->vg->tags))
307 					    return 1;
308 				else
309 					continue;
310 			}
311 			/* If supplied tag matches LV or VG tag, activate */
312 			if (str_list_match_item(&lv->tags, str) ||
313 			    str_list_match_item(&lv->vg->tags, str))
314 				return 1;
315 			else
316 				continue;
317 		}
318 		if (!strchr(str, '/')) {
319 			/* vgname supplied */
320 			if (!strcmp(str, lv->vg->name))
321 				return 1;
322 			else
323 				continue;
324 		}
325 		/* vgname/lvname */
326 		if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name,
327 				 lv->name) < 0) {
328 			log_error("dm_snprintf error from %s/%s", lv->vg->name,
329 				  lv->name);
330 			continue;
331 		}
332 		if (!strcmp(path, str))
333 			return 1;
334 	}
335 
336 	return 0;
337 }
338 
339 int library_version(char *version, size_t size)
340 {
341 	if (!activation())
342 		return 0;
343 
344 	return dm_get_library_version(version, size);
345 }
346 
347 int driver_version(char *version, size_t size)
348 {
349 	if (!activation())
350 		return 0;
351 
352 	log_very_verbose("Getting driver version");
353 
354 	return dm_driver_version(version, size);
355 }
356 
357 int target_version(const char *target_name, uint32_t *maj,
358 		   uint32_t *min, uint32_t *patchlevel)
359 {
360 	int r = 0;
361 	struct dm_task *dmt;
362 	struct dm_versions *target, *last_target;
363 
364 	log_very_verbose("Getting target version for %s", target_name);
365 	if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
366 		return_0;
367 
368 	if (!dm_task_run(dmt)) {
369 		log_debug("Failed to get %s target version", target_name);
370 		/* Assume this was because LIST_VERSIONS isn't supported */
371 		return 1;
372 	}
373 
374 	target = dm_task_get_versions(dmt);
375 
376 	do {
377 		last_target = target;
378 
379 		if (!strcmp(target_name, target->name)) {
380 			r = 1;
381 			*maj = target->version[0];
382 			*min = target->version[1];
383 			*patchlevel = target->version[2];
384 			goto out;
385 		}
386 
387 		target = (void *) target + target->next;
388 	} while (last_target != target);
389 
390       out:
391 	dm_task_destroy(dmt);
392 
393 	return r;
394 }
395 
396 int module_present(const char *target_name)
397 {
398 	int ret = 0;
399 #ifdef MODPROBE_CMD
400 	char module[128];
401 
402 	if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) {
403 		log_error("module_present module name too long: %s",
404 			  target_name);
405 		return 0;
406 	}
407 
408 	ret = exec_cmd(MODPROBE_CMD, module, "", "");
409 #endif
410 	return ret;
411 }
412 
413 int target_present(const char *target_name, int use_modprobe)
414 {
415 	uint32_t maj, min, patchlevel;
416 
417 	if (!activation())
418 		return 0;
419 
420 #ifdef MODPROBE_CMD
421 	if (use_modprobe) {
422 		if (target_version(target_name, &maj, &min, &patchlevel))
423 			return 1;
424 
425 		if (!module_present(target_name))
426 			return_0;
427 	}
428 #endif
429 
430 	return target_version(target_name, &maj, &min, &patchlevel);
431 }
432 
433 /*
434  * Returns 1 if info structure populated, else 0 on failure.
435  */
436 static int _lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int with_mknodes,
437 		    struct lvinfo *info, int with_open_count, int with_read_ahead, unsigned by_uuid_only)
438 {
439 	struct dm_info dminfo;
440 	char *name = NULL;
441 
442 	if (!activation())
443 		return 0;
444 
445 	if (!by_uuid_only &&
446 	    !(name = build_dm_name(cmd->mem, lv->vg->name, lv->name, NULL)))
447 		return_0;
448 
449 	log_debug("Getting device info for %s", name);
450 	if (!dev_manager_info(lv->vg->cmd->mem, name, lv, with_mknodes,
451 			      with_open_count, with_read_ahead, &dminfo,
452 			      &info->read_ahead)) {
453 		if (name)
454 			dm_pool_free(cmd->mem, name);
455 		return_0;
456 	}
457 
458 	info->exists = dminfo.exists;
459 	info->suspended = dminfo.suspended;
460 	info->open_count = dminfo.open_count;
461 	info->major = dminfo.major;
462 	info->minor = dminfo.minor;
463 	info->read_only = dminfo.read_only;
464 	info->live_table = dminfo.live_table;
465 	info->inactive_table = dminfo.inactive_table;
466 
467 	if (name)
468 		dm_pool_free(cmd->mem, name);
469 
470 	return 1;
471 }
472 
473 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info,
474 	    int with_open_count, int with_read_ahead)
475 {
476 	return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0);
477 }
478 
479 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
480 		    struct lvinfo *info, int with_open_count, int with_read_ahead)
481 {
482 	struct logical_volume *lv;
483 
484 	if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
485 		return 0;
486 
487 	return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0);
488 }
489 
490 /*
491  * Returns 1 if percent set, else 0 on failure.
492  */
493 int lv_snapshot_percent(const struct logical_volume *lv, float *percent)
494 {
495 	int r;
496 	struct dev_manager *dm;
497 
498 	if (!activation())
499 		return 0;
500 
501 	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
502 		return_0;
503 
504 	if (!(r = dev_manager_snapshot_percent(dm, lv, percent)))
505 		stack;
506 
507 	dev_manager_destroy(dm);
508 
509 	return r;
510 }
511 
512 /* FIXME Merge with snapshot_percent */
513 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
514 		      int wait, float *percent, uint32_t *event_nr)
515 {
516 	int r;
517 	struct dev_manager *dm;
518 	struct lvinfo info;
519 
520 	/* If mirrored LV is temporarily shrinked to 1 area (= linear),
521 	 * it should be considered in-sync. */
522 	if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
523 		*percent = 100.0;
524 		return 1;
525 	}
526 
527 	if (!activation())
528 		return 0;
529 
530 	if (!lv_info(cmd, lv, &info, 0, 0))
531 		return_0;
532 
533 	if (!info.exists)
534 		return 0;
535 
536 	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
537 		return_0;
538 
539 	if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, event_nr)))
540 		stack;
541 
542 	dev_manager_destroy(dm);
543 
544 	return r;
545 }
546 
547 static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv,
548 		      unsigned by_uuid_only)
549 {
550 	struct lvinfo info;
551 
552 	if (!_lv_info(cmd, lv, 0, &info, 0, 0, by_uuid_only)) {
553 		stack;
554 		return -1;
555 	}
556 
557 	return info.exists;
558 }
559 
560 static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
561 {
562 	struct lvinfo info;
563 
564 	if (!lv_info(cmd, lv, &info, 1, 0)) {
565 		stack;
566 		return -1;
567 	}
568 
569 	return info.open_count;
570 }
571 
572 static int _lv_activate_lv(struct logical_volume *lv)
573 {
574 	int r;
575 	struct dev_manager *dm;
576 
577 	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
578 		return_0;
579 
580 	if (!(r = dev_manager_activate(dm, lv)))
581 		stack;
582 
583 	dev_manager_destroy(dm);
584 	return r;
585 }
586 
587 static int _lv_preload(struct logical_volume *lv)
588 {
589 	int r;
590 	struct dev_manager *dm;
591 
592 	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
593 		return_0;
594 
595 	if (!(r = dev_manager_preload(dm, lv)))
596 		stack;
597 
598 	dev_manager_destroy(dm);
599 	return r;
600 }
601 
602 static int _lv_deactivate(struct logical_volume *lv)
603 {
604 	int r;
605 	struct dev_manager *dm;
606 
607 	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
608 		return_0;
609 
610 	if (!(r = dev_manager_deactivate(dm, lv)))
611 		stack;
612 
613 	dev_manager_destroy(dm);
614 	return r;
615 }
616 
617 static int _lv_suspend_lv(struct logical_volume *lv, int lockfs)
618 {
619 	int r;
620 	struct dev_manager *dm;
621 
622 	if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
623 		return_0;
624 
625 	if (!(r = dev_manager_suspend(dm, lv, lockfs)))
626 		stack;
627 
628 	dev_manager_destroy(dm);
629 	return r;
630 }
631 
632 /*
633  * These two functions return the number of visible LVs in the state,
634  * or -1 on error.
635  */
636 static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only)
637 {
638 	struct lv_list *lvl;
639 	int count = 0;
640 
641 	if (!activation())
642 		return 0;
643 
644 	dm_list_iterate_items(lvl, &vg->lvs) {
645 		if (lvl->lv->status & VISIBLE_LV)
646 			count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1);
647 	}
648 
649 	return count;
650 }
651 
652 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg)
653 {
654 	return _lvs_in_vg_activated(vg, 1);
655 }
656 
657 int lvs_in_vg_activated(struct volume_group *vg)
658 {
659 	return _lvs_in_vg_activated(vg, 0);
660 }
661 
662 int lvs_in_vg_opened(const struct volume_group *vg)
663 {
664 	const struct lv_list *lvl;
665 	int count = 0;
666 
667 	if (!activation())
668 		return 0;
669 
670 	dm_list_iterate_items(lvl, &vg->lvs) {
671 		if (lv_is_displayable(lvl->lv))
672 			count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
673 	}
674 
675 	return count;
676 }
677 
678 /*
679  * Determine whether an LV is active locally or in a cluster.
680  * Assumes vg lock held.
681  * Returns:
682  * 0 - not active locally or on any node in cluster
683  * 1 - active either locally or some node in the cluster
684  */
685 int lv_is_active(struct logical_volume *lv)
686 {
687 	if (_lv_active(lv->vg->cmd, lv, 0))
688 		return 1;
689 
690 	if (!vg_is_clustered(lv->vg))
691 		return 0;
692 
693 	/*
694 	 * FIXME: Cluster does not report per-node LV activation status.
695 	 * Currently the best we can do is try exclusive local activation.
696 	 * If that succeeds, we know the LV is not active elsewhere in the
697 	 * cluster.
698 	 */
699 	if (activate_lv_excl(lv->vg->cmd, lv)) {
700 		deactivate_lv(lv->vg->cmd, lv);
701 		return 0;
702 	}
703 
704 	/*
705 	 * Exclusive local activation failed so assume it is active elsewhere.
706 	 */
707 	return 1;
708 }
709 
710 /*
711  * Returns 0 if an attempt to (un)monitor the device failed.
712  * Returns 1 otherwise.
713  */
714 int monitor_dev_for_events(struct cmd_context *cmd,
715 			    struct logical_volume *lv, int monitor)
716 {
717 #ifdef DMEVENTD
718 	int i, pending = 0, monitored;
719 	int r = 1;
720 	struct dm_list *tmp, *snh, *snht;
721 	struct lv_segment *seg;
722 	int (*monitor_fn) (struct lv_segment *s, int e);
723 	uint32_t s;
724 
725 	/* skip dmeventd code altogether */
726 	if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
727 		return 1;
728 
729 	/*
730 	 * Nothing to do if dmeventd configured not to be used.
731 	 */
732 	if (monitor && !dmeventd_monitor_mode())
733 		return 1;
734 
735 	/*
736 	 * In case of a snapshot device, we monitor lv->snapshot->lv,
737 	 * not the actual LV itself.
738 	 */
739 	if (lv_is_cow(lv))
740 		return monitor_dev_for_events(cmd, lv->snapshot->lv, monitor);
741 
742 	/*
743 	 * In case this LV is a snapshot origin, we instead monitor
744 	 * each of its respective snapshots (the origin itself does
745 	 * not need to be monitored).
746 	 *
747 	 * TODO: This may change when snapshots of mirrors are allowed.
748 	 */
749 	if (lv_is_origin(lv)) {
750 		dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
751 			if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
752 				    struct lv_segment, origin_list)->cow, monitor))
753 				r = 0;
754 		return r;
755 	}
756 
757 	dm_list_iterate(tmp, &lv->segments) {
758 		seg = dm_list_item(tmp, struct lv_segment);
759 
760 		/* Recurse for AREA_LV */
761 		for (s = 0; s < seg->area_count; s++) {
762 			if (seg_type(seg, s) != AREA_LV)
763 				continue;
764 			if (!monitor_dev_for_events(cmd, seg_lv(seg, s),
765 						    monitor)) {
766 				log_error("Failed to %smonitor %s",
767 					  monitor ? "" : "un",
768 					  seg_lv(seg, s)->name);
769 				r = 0;
770 			}
771 		}
772 
773 		if (!seg_monitored(seg) || (seg->status & PVMOVE))
774 			continue;
775 
776 		monitor_fn = NULL;
777 
778 		/* Check monitoring status */
779 		if (seg->segtype->ops->target_monitored)
780 			monitored = seg->segtype->ops->target_monitored(seg, &pending);
781 		else
782 			continue;  /* segtype doesn't support registration */
783 
784 		/*
785 		 * FIXME: We should really try again if pending
786 		 */
787 		monitored = (pending) ? 0 : monitored;
788 
789 		if (monitor) {
790 			if (monitored)
791 				log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
792 			else if (seg->segtype->ops->target_monitor_events)
793 				monitor_fn = seg->segtype->ops->target_monitor_events;
794 		} else {
795 			if (!monitored)
796 				log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
797 			else if (seg->segtype->ops->target_unmonitor_events)
798 				monitor_fn = seg->segtype->ops->target_unmonitor_events;
799 		}
800 
801 		/* Do [un]monitor */
802 		if (!monitor_fn)
803 			continue;
804 
805 		log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name);
806 
807 		/* FIXME specify events */
808 		if (!monitor_fn(seg, 0)) {
809 			log_error("%s/%s: %s segment monitoring function failed.",
810 				  lv->vg->name, lv->name, seg->segtype->name);
811 			return 0;
812 		}
813 
814 		/* Check [un]monitor results */
815 		/* Try a couple times if pending, but not forever... */
816 		for (i = 0; i < 10; i++) {
817 			pending = 0;
818 			monitored = seg->segtype->ops->target_monitored(seg, &pending);
819 			if (pending ||
820 			    (!monitored && monitor) ||
821 			    (monitored && !monitor))
822 				log_very_verbose("%s/%s %smonitoring still pending: waiting...",
823 						 lv->vg->name, lv->name, monitor ? "" : "un");
824 			else
825 				break;
826 			sleep(1);
827 		}
828 
829 		r = (monitored && monitor) || (!monitored && !monitor);
830 	}
831 
832 	return r;
833 #else
834 	return 1;
835 #endif
836 }
837 
838 static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
839 		       int error_if_not_suspended)
840 {
841 	struct logical_volume *lv, *lv_pre;
842 	struct lvinfo info;
843 	int lockfs = 0;
844 
845 	if (!activation())
846 		return 1;
847 
848 	if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
849 		return_0;
850 
851 	/* Use precommitted metadata if present */
852 	if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
853 		return_0;
854 
855 	if (test_mode()) {
856 		_skip("Suspending '%s'.", lv->name);
857 		return 1;
858 	}
859 
860 	if (!lv_info(cmd, lv, &info, 0, 0))
861 		return_0;
862 
863 	if (!info.exists || info.suspended)
864 		return error_if_not_suspended ? 0 : 1;
865 
866 	/* If VG was precommitted, preload devices for the LV */
867 	if ((lv_pre->vg->status & PRECOMMITTED)) {
868 		if (!_lv_preload(lv_pre)) {
869 			/* FIXME Revert preloading */
870 			return_0;
871 		}
872 	}
873 
874 	if (!monitor_dev_for_events(cmd, lv, 0))
875 		/* FIXME Consider aborting here */
876 		stack;
877 
878 	memlock_inc();
879 
880 	if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre))
881 		lockfs = 1;
882 
883 	if (!_lv_suspend_lv(lv, lockfs)) {
884 		memlock_dec();
885 		fs_unlock();
886 		return 0;
887 	}
888 
889 	return 1;
890 }
891 
892 /* Returns success if the device is not active */
893 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s)
894 {
895 	return _lv_suspend(cmd, lvid_s, 0);
896 }
897 
898 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
899 {
900 	return _lv_suspend(cmd, lvid_s, 1);
901 }
902 
903 static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
904 		      int error_if_not_active)
905 {
906 	struct logical_volume *lv;
907 	struct lvinfo info;
908 
909 	if (!activation())
910 		return 1;
911 
912 	if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
913 		return 0;
914 
915 	if (test_mode()) {
916 		_skip("Resuming '%s'.", lv->name);
917 		return 1;
918 	}
919 
920 	if (!lv_info(cmd, lv, &info, 0, 0))
921 		return_0;
922 
923 	if (!info.exists || !info.suspended)
924 		return error_if_not_active ? 0 : 1;
925 
926 	if (!_lv_activate_lv(lv))
927 		return 0;
928 
929 	memlock_dec();
930 	fs_unlock();
931 
932 	if (!monitor_dev_for_events(cmd, lv, 1))
933 		stack;
934 
935 	return 1;
936 }
937 
938 /* Returns success if the device is not active */
939 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s)
940 {
941 	return _lv_resume(cmd, lvid_s, 0);
942 }
943 
944 int lv_resume(struct cmd_context *cmd, const char *lvid_s)
945 {
946 	return _lv_resume(cmd, lvid_s, 1);
947 }
948 
949 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
950 {
951 	struct logical_volume *lv;
952 	struct lvinfo info;
953 	int r;
954 
955 	if (!activation())
956 		return 1;
957 
958 	if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
959 		return 0;
960 
961 	if (test_mode()) {
962 		_skip("Deactivating '%s'.", lv->name);
963 		return 1;
964 	}
965 
966 	if (!lv_info(cmd, lv, &info, 1, 0))
967 		return_0;
968 
969 	if (!info.exists)
970 		return 1;
971 
972 	if (info.open_count && (lv->status & VISIBLE_LV)) {
973 		log_error("LV %s/%s in use: not deactivating", lv->vg->name,
974 			  lv->name);
975 		return 0;
976 	}
977 
978 	if (!monitor_dev_for_events(cmd, lv, 0))
979 		stack;
980 
981 	memlock_inc();
982 	r = _lv_deactivate(lv);
983 	memlock_dec();
984 	fs_unlock();
985 
986 	return r;
987 }
988 
989 /* Test if LV passes filter */
990 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
991 			 int *activate_lv)
992 {
993 	struct logical_volume *lv;
994 
995 	if (!activation())
996 		goto activate;
997 
998 	if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
999 		return 0;
1000 
1001 	if (!_passes_activation_filter(cmd, lv)) {
1002 		log_verbose("Not activating %s/%s due to config file settings",
1003 			    lv->vg->name, lv->name);
1004 		*activate_lv = 0;
1005 		return 1;
1006 	}
1007 
1008       activate:
1009 	*activate_lv = 1;
1010 	return 1;
1011 }
1012 
1013 static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
1014 			int exclusive, int filter)
1015 {
1016 	struct logical_volume *lv;
1017 	struct lvinfo info;
1018 	int r;
1019 
1020 	if (!activation())
1021 		return 1;
1022 
1023 	if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1024 		return 0;
1025 
1026 	if (filter && !_passes_activation_filter(cmd, lv)) {
1027 		log_verbose("Not activating %s/%s due to config file settings",
1028 			    lv->vg->name, lv->name);
1029 		return 0;
1030 	}
1031 
1032 	if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
1033 		log_error("Refusing activation of partial LV %s. Use --partial to override.",
1034 			  lv->name);
1035 		return_0;
1036 	}
1037 
1038 	if (test_mode()) {
1039 		_skip("Activating '%s'.", lv->name);
1040 		return 1;
1041 	}
1042 
1043 	if (!lv_info(cmd, lv, &info, 0, 0))
1044 		return_0;
1045 
1046 	if (info.exists && !info.suspended && info.live_table)
1047 		return 1;
1048 
1049 	if (exclusive)
1050 		lv->status |= ACTIVATE_EXCL;
1051 
1052 	memlock_inc();
1053 	r = _lv_activate_lv(lv);
1054 	memlock_dec();
1055 	fs_unlock();
1056 
1057 	if (r && !monitor_dev_for_events(cmd, lv, 1))
1058 		stack;
1059 
1060 	return r;
1061 }
1062 
1063 /* Activate LV */
1064 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1065 {
1066 	return _lv_activate(cmd, lvid_s, exclusive, 0);
1067 }
1068 
1069 /* Activate LV only if it passes filter */
1070 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1071 {
1072 	return _lv_activate(cmd, lvid_s, exclusive, 1);
1073 }
1074 
1075 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1076 {
1077 	struct lvinfo info;
1078 	int r = 1;
1079 
1080 	if (!lv) {
1081 		r = dm_mknodes(NULL);
1082 		fs_unlock();
1083 		return r;
1084 	}
1085 
1086 	if (!_lv_info(cmd, lv, 1, &info, 0, 0, 0))
1087 		return_0;
1088 
1089 	if (info.exists) {
1090 		if (lv_is_visible(lv))
1091 			r = dev_manager_lv_mknodes(lv);
1092 	} else
1093 		r = dev_manager_lv_rmnodes(lv);
1094 
1095 	fs_unlock();
1096 
1097 	return r;
1098 }
1099 
1100 /*
1101  * Does PV use VG somewhere in its construction?
1102  * Returns 1 on failure.
1103  */
1104 int pv_uses_vg(struct physical_volume *pv,
1105 	       struct volume_group *vg)
1106 {
1107 	if (!activation())
1108 		return 0;
1109 
1110 	if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1111 		return 0;
1112 
1113 	return dev_manager_device_uses_vg(pv->dev, vg);
1114 }
1115 
1116 void activation_release(void)
1117 {
1118 	dev_manager_release();
1119 }
1120 
1121 void activation_exit(void)
1122 {
1123 	dev_manager_exit();
1124 }
1125 #endif
1126