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