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