]> sourceware.org Git - lvm2.git/blob - lib/activate/activate.c
Introduce percent_range_t and centralise snapshot full/mirror in-sync checks.
[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 struct logical_volume *lv;
490
491 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
492 return 0;
493
494 return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0);
495 }
496
497 /*
498 * Returns 1 if percent set, else 0 on failure.
499 */
500 int lv_snapshot_percent(const struct logical_volume *lv, float *percent,
501 percent_range_t *percent_range)
502 {
503 int r;
504 struct dev_manager *dm;
505
506 if (!activation())
507 return 0;
508
509 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
510 return_0;
511
512 if (!(r = dev_manager_snapshot_percent(dm, lv, percent, percent_range)))
513 stack;
514
515 dev_manager_destroy(dm);
516
517 return r;
518 }
519
520 /* FIXME Merge with snapshot_percent */
521 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
522 int wait, float *percent, percent_range_t *percent_range,
523 uint32_t *event_nr)
524 {
525 int r;
526 struct dev_manager *dm;
527 struct lvinfo info;
528
529 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
530 * it should be considered in-sync. */
531 if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
532 *percent = 100.0;
533 return 1;
534 }
535
536 if (!activation())
537 return 0;
538
539 if (!lv_info(cmd, lv, &info, 0, 0))
540 return_0;
541
542 if (!info.exists)
543 return 0;
544
545 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
546 return_0;
547
548 if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent,
549 percent_range, event_nr)))
550 stack;
551
552 dev_manager_destroy(dm);
553
554 return r;
555 }
556
557 static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv,
558 unsigned by_uuid_only)
559 {
560 struct lvinfo info;
561
562 if (!_lv_info(cmd, lv, 0, &info, 0, 0, by_uuid_only)) {
563 stack;
564 return -1;
565 }
566
567 return info.exists;
568 }
569
570 static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
571 {
572 struct lvinfo info;
573
574 if (!lv_info(cmd, lv, &info, 1, 0)) {
575 stack;
576 return -1;
577 }
578
579 return info.open_count;
580 }
581
582 static int _lv_activate_lv(struct logical_volume *lv)
583 {
584 int r;
585 struct dev_manager *dm;
586
587 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
588 return_0;
589
590 if (!(r = dev_manager_activate(dm, lv)))
591 stack;
592
593 dev_manager_destroy(dm);
594 return r;
595 }
596
597 static int _lv_preload(struct logical_volume *lv, int *flush_required)
598 {
599 int r;
600 struct dev_manager *dm;
601
602 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
603 return_0;
604
605 if (!(r = dev_manager_preload(dm, lv, flush_required)))
606 stack;
607
608 dev_manager_destroy(dm);
609 return r;
610 }
611
612 static int _lv_deactivate(struct logical_volume *lv)
613 {
614 int r;
615 struct dev_manager *dm;
616
617 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
618 return_0;
619
620 if (!(r = dev_manager_deactivate(dm, lv)))
621 stack;
622
623 dev_manager_destroy(dm);
624 return r;
625 }
626
627 static int _lv_suspend_lv(struct logical_volume *lv, int lockfs, int flush_required)
628 {
629 int r;
630 struct dev_manager *dm;
631
632 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
633 return_0;
634
635 if (!(r = dev_manager_suspend(dm, lv, lockfs, flush_required)))
636 stack;
637
638 dev_manager_destroy(dm);
639 return r;
640 }
641
642 /*
643 * These two functions return the number of visible LVs in the state,
644 * or -1 on error.
645 */
646 static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only)
647 {
648 struct lv_list *lvl;
649 int count = 0;
650
651 if (!activation())
652 return 0;
653
654 dm_list_iterate_items(lvl, &vg->lvs) {
655 if (lv_is_visible(lvl->lv))
656 count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1);
657 }
658
659 return count;
660 }
661
662 int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg)
663 {
664 return _lvs_in_vg_activated(vg, 1);
665 }
666
667 int lvs_in_vg_activated(struct volume_group *vg)
668 {
669 return _lvs_in_vg_activated(vg, 0);
670 }
671
672 int lvs_in_vg_opened(const struct volume_group *vg)
673 {
674 const struct lv_list *lvl;
675 int count = 0;
676
677 if (!activation())
678 return 0;
679
680 dm_list_iterate_items(lvl, &vg->lvs) {
681 if (lv_is_visible(lvl->lv))
682 count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
683 }
684
685 return count;
686 }
687
688 /*
689 * Determine whether an LV is active locally or in a cluster.
690 * Assumes vg lock held.
691 * Returns:
692 * 0 - not active locally or on any node in cluster
693 * 1 - active either locally or some node in the cluster
694 */
695 int lv_is_active(struct logical_volume *lv)
696 {
697 int ret;
698
699 if (_lv_active(lv->vg->cmd, lv, 0))
700 return 1;
701
702 if (!vg_is_clustered(lv->vg))
703 return 0;
704
705 if ((ret = remote_lock_held(lv->lvid.s)) >= 0)
706 return ret;
707
708 /*
709 * Old compatibility code if locking doesn't support lock query
710 * FIXME: check status to not deactivate already activate device
711 */
712 if (activate_lv_excl(lv->vg->cmd, lv)) {
713 deactivate_lv(lv->vg->cmd, lv);
714 return 0;
715 }
716
717 /*
718 * Exclusive local activation failed so assume it is active elsewhere.
719 */
720 return 1;
721 }
722
723 /*
724 * Returns 0 if an attempt to (un)monitor the device failed.
725 * Returns 1 otherwise.
726 */
727 int monitor_dev_for_events(struct cmd_context *cmd,
728 struct logical_volume *lv, int monitor)
729 {
730 #ifdef DMEVENTD
731 int i, pending = 0, monitored;
732 int r = 1;
733 struct dm_list *tmp, *snh, *snht;
734 struct lv_segment *seg;
735 int (*monitor_fn) (struct lv_segment *s, int e);
736 uint32_t s;
737
738 /* skip dmeventd code altogether */
739 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
740 return 1;
741
742 /*
743 * Nothing to do if dmeventd configured not to be used.
744 */
745 if (monitor && !dmeventd_monitor_mode())
746 return 1;
747
748 /*
749 * In case of a snapshot device, we monitor lv->snapshot->lv,
750 * not the actual LV itself.
751 */
752 if (lv_is_cow(lv))
753 return monitor_dev_for_events(cmd, lv->snapshot->lv, monitor);
754
755 /*
756 * In case this LV is a snapshot origin, we instead monitor
757 * each of its respective snapshots (the origin itself does
758 * not need to be monitored).
759 *
760 * TODO: This may change when snapshots of mirrors are allowed.
761 */
762 if (lv_is_origin(lv)) {
763 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
764 if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
765 struct lv_segment, origin_list)->cow, monitor))
766 r = 0;
767 return r;
768 }
769
770 dm_list_iterate(tmp, &lv->segments) {
771 seg = dm_list_item(tmp, struct lv_segment);
772
773 /* Recurse for AREA_LV */
774 for (s = 0; s < seg->area_count; s++) {
775 if (seg_type(seg, s) != AREA_LV)
776 continue;
777 if (!monitor_dev_for_events(cmd, seg_lv(seg, s),
778 monitor)) {
779 log_error("Failed to %smonitor %s",
780 monitor ? "" : "un",
781 seg_lv(seg, s)->name);
782 r = 0;
783 }
784 }
785
786 if (!seg_monitored(seg) || (seg->status & PVMOVE))
787 continue;
788
789 monitor_fn = NULL;
790
791 /* Check monitoring status */
792 if (seg->segtype->ops->target_monitored)
793 monitored = seg->segtype->ops->target_monitored(seg, &pending);
794 else
795 continue; /* segtype doesn't support registration */
796
797 /*
798 * FIXME: We should really try again if pending
799 */
800 monitored = (pending) ? 0 : monitored;
801
802 if (monitor) {
803 if (monitored)
804 log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
805 else if (seg->segtype->ops->target_monitor_events)
806 monitor_fn = seg->segtype->ops->target_monitor_events;
807 } else {
808 if (!monitored)
809 log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
810 else if (seg->segtype->ops->target_unmonitor_events)
811 monitor_fn = seg->segtype->ops->target_unmonitor_events;
812 }
813
814 /* Do [un]monitor */
815 if (!monitor_fn)
816 continue;
817
818 log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name);
819
820 /* FIXME specify events */
821 if (!monitor_fn(seg, 0)) {
822 log_error("%s/%s: %s segment monitoring function failed.",
823 lv->vg->name, lv->name, seg->segtype->name);
824 return 0;
825 }
826
827 /* Check [un]monitor results */
828 /* Try a couple times if pending, but not forever... */
829 for (i = 0; i < 10; i++) {
830 pending = 0;
831 monitored = seg->segtype->ops->target_monitored(seg, &pending);
832 if (pending ||
833 (!monitored && monitor) ||
834 (monitored && !monitor))
835 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
836 lv->vg->name, lv->name, monitor ? "" : "un");
837 else
838 break;
839 sleep(1);
840 }
841
842 r = (monitored && monitor) || (!monitored && !monitor);
843 }
844
845 return r;
846 #else
847 return 1;
848 #endif
849 }
850
851 static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
852 int error_if_not_suspended)
853 {
854 struct logical_volume *lv = NULL, *lv_pre = NULL;
855 struct lvinfo info;
856 int r = 0, lockfs = 0, flush_required = 0;
857
858 if (!activation())
859 return 1;
860
861 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
862 goto_out;
863
864 /* Use precommitted metadata if present */
865 if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
866 goto_out;
867
868 if (test_mode()) {
869 _skip("Suspending '%s'.", lv->name);
870 r = 1;
871 goto out;
872 }
873
874 if (!lv_info(cmd, lv, &info, 0, 0))
875 goto_out;
876
877 if (!info.exists || info.suspended) {
878 r = error_if_not_suspended ? 0 : 1;
879 goto out;
880 }
881
882 lv_calculate_readahead(lv, NULL);
883
884 /* If VG was precommitted, preload devices for the LV */
885 if ((lv_pre->vg->status & PRECOMMITTED)) {
886 if (!_lv_preload(lv_pre, &flush_required)) {
887 /* FIXME Revert preloading */
888 goto_out;
889 }
890 }
891
892 if (!monitor_dev_for_events(cmd, lv, 0))
893 /* FIXME Consider aborting here */
894 stack;
895
896 memlock_inc();
897
898 if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre))
899 lockfs = 1;
900
901 if (!_lv_suspend_lv(lv, lockfs, flush_required)) {
902 memlock_dec();
903 fs_unlock();
904 goto out;
905 }
906
907 r = 1;
908 out:
909 if (lv_pre)
910 vg_release(lv_pre->vg);
911 if (lv)
912 vg_release(lv->vg);
913
914 return r;
915 }
916
917 /* Returns success if the device is not active */
918 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s)
919 {
920 return _lv_suspend(cmd, lvid_s, 0);
921 }
922
923 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
924 {
925 return _lv_suspend(cmd, lvid_s, 1);
926 }
927
928 static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
929 int error_if_not_active)
930 {
931 struct logical_volume *lv;
932 struct lvinfo info;
933 int r = 0;
934
935 if (!activation())
936 return 1;
937
938 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
939 goto out;
940
941 if (test_mode()) {
942 _skip("Resuming '%s'.", lv->name);
943 r = 1;
944 goto out;
945 }
946
947 if (!lv_info(cmd, lv, &info, 0, 0))
948 goto_out;
949
950 if (!info.exists || !info.suspended) {
951 r = error_if_not_active ? 0 : 1;
952 goto out;
953 }
954
955 if (!_lv_activate_lv(lv))
956 goto out;
957
958 memlock_dec();
959 fs_unlock();
960
961 if (!monitor_dev_for_events(cmd, lv, 1))
962 stack;
963
964 r = 1;
965 out:
966 if (lv)
967 vg_release(lv->vg);
968
969 return r;
970 }
971
972 /* Returns success if the device is not active */
973 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s)
974 {
975 return _lv_resume(cmd, lvid_s, 0);
976 }
977
978 int lv_resume(struct cmd_context *cmd, const char *lvid_s)
979 {
980 return _lv_resume(cmd, lvid_s, 1);
981 }
982
983 static int _lv_has_open_snapshots(struct logical_volume *lv)
984 {
985 struct lv_segment *snap_seg;
986 struct lvinfo info;
987 int r = 0;
988
989 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
990 if (!lv_info(lv->vg->cmd, snap_seg->cow, &info, 1, 0)) {
991 r = 1;
992 continue;
993 }
994
995 if (info.exists && info.open_count) {
996 log_error("LV %s/%s has open snapshot %s: "
997 "not deactivating", lv->vg->name, lv->name,
998 snap_seg->cow->name);
999 r = 1;
1000 }
1001 }
1002
1003 return r;
1004 }
1005
1006 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
1007 {
1008 struct logical_volume *lv;
1009 struct lvinfo info;
1010 int r = 0;
1011
1012 if (!activation())
1013 return 1;
1014
1015 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1016 goto out;
1017
1018 if (test_mode()) {
1019 _skip("Deactivating '%s'.", lv->name);
1020 r = 1;
1021 goto out;
1022 }
1023
1024 if (!lv_info(cmd, lv, &info, 1, 0))
1025 goto_out;
1026
1027 if (!info.exists) {
1028 r = 1;
1029 goto out;
1030 }
1031
1032 if (lv_is_visible(lv)) {
1033 if (info.open_count) {
1034 log_error("LV %s/%s in use: not deactivating",
1035 lv->vg->name, lv->name);
1036 goto out;
1037 }
1038 if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1039 goto_out;
1040 }
1041
1042 lv_calculate_readahead(lv, NULL);
1043
1044 if (!monitor_dev_for_events(cmd, lv, 0))
1045 stack;
1046
1047 memlock_inc();
1048 r = _lv_deactivate(lv);
1049 memlock_dec();
1050 fs_unlock();
1051
1052 if (!lv_info(cmd, lv, &info, 1, 0) || info.exists)
1053 r = 0;
1054 out:
1055 if (lv)
1056 vg_release(lv->vg);
1057
1058 return r;
1059 }
1060
1061 /* Test if LV passes filter */
1062 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
1063 int *activate_lv)
1064 {
1065 struct logical_volume *lv;
1066 int r = 0;
1067
1068 if (!activation()) {
1069 *activate_lv = 1;
1070 return 1;
1071 }
1072
1073 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1074 goto out;
1075
1076 if (!_passes_activation_filter(cmd, lv)) {
1077 log_verbose("Not activating %s/%s due to config file settings",
1078 lv->vg->name, lv->name);
1079 *activate_lv = 0;
1080 } else
1081 *activate_lv = 1;
1082 r = 1;
1083 out:
1084 if (lv)
1085 vg_release(lv->vg);
1086
1087 return r;
1088 }
1089
1090 static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
1091 int exclusive, int filter)
1092 {
1093 struct logical_volume *lv;
1094 struct lvinfo info;
1095 int r = 0;
1096
1097 if (!activation())
1098 return 1;
1099
1100 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1101 goto out;
1102
1103 if (filter && !_passes_activation_filter(cmd, lv)) {
1104 log_verbose("Not activating %s/%s due to config file settings",
1105 lv->vg->name, lv->name);
1106 goto out;
1107 }
1108
1109 if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
1110 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1111 lv->name);
1112 goto_out;
1113 }
1114
1115 if (test_mode()) {
1116 _skip("Activating '%s'.", lv->name);
1117 r = 1;
1118 goto out;
1119 }
1120
1121 if (!lv_info(cmd, lv, &info, 0, 0))
1122 goto_out;
1123
1124 if (info.exists && !info.suspended && info.live_table) {
1125 r = 1;
1126 goto out;
1127 }
1128
1129 lv_calculate_readahead(lv, NULL);
1130
1131 if (exclusive)
1132 lv->status |= ACTIVATE_EXCL;
1133
1134 memlock_inc();
1135 r = _lv_activate_lv(lv);
1136 memlock_dec();
1137 fs_unlock();
1138
1139 if (r && !monitor_dev_for_events(cmd, lv, 1))
1140 stack;
1141
1142 out:
1143 if (lv)
1144 vg_release(lv->vg);
1145
1146 return r;
1147 }
1148
1149 /* Activate LV */
1150 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1151 {
1152 return _lv_activate(cmd, lvid_s, exclusive, 0);
1153 }
1154
1155 /* Activate LV only if it passes filter */
1156 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1157 {
1158 return _lv_activate(cmd, lvid_s, exclusive, 1);
1159 }
1160
1161 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1162 {
1163 struct lvinfo info;
1164 int r = 1;
1165
1166 if (!lv) {
1167 r = dm_mknodes(NULL);
1168 fs_unlock();
1169 return r;
1170 }
1171
1172 if (!_lv_info(cmd, lv, 1, &info, 0, 0, 0))
1173 return_0;
1174
1175 if (info.exists) {
1176 if (lv_is_visible(lv))
1177 r = dev_manager_lv_mknodes(lv);
1178 } else
1179 r = dev_manager_lv_rmnodes(lv);
1180
1181 fs_unlock();
1182
1183 return r;
1184 }
1185
1186 /*
1187 * Does PV use VG somewhere in its construction?
1188 * Returns 1 on failure.
1189 */
1190 int pv_uses_vg(struct physical_volume *pv,
1191 struct volume_group *vg)
1192 {
1193 if (!activation())
1194 return 0;
1195
1196 if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1197 return 0;
1198
1199 return dev_manager_device_uses_vg(pv->dev, vg);
1200 }
1201
1202 void activation_release(void)
1203 {
1204 dev_manager_release();
1205 }
1206
1207 void activation_exit(void)
1208 {
1209 dev_manager_exit();
1210 }
1211 #endif
This page took 0.08878 seconds and 6 git commands to generate.