]> sourceware.org Git - lvm2.git/blob - lib/activate/activate.c
Major pvmove fix to issue ioctls in the correct order when multiple LVs
[lvm2.git] / lib / activate / activate.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2011 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 #include "sharedlib.h"
32
33 #include <limits.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36
37 #define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)
38
39 int lvm1_present(struct cmd_context *cmd)
40 {
41 char path[PATH_MAX];
42
43 if (dm_snprintf(path, sizeof(path), "%s/lvm/global", cmd->proc_dir)
44 < 0) {
45 log_error("LVM1 proc global snprintf failed");
46 return 0;
47 }
48
49 if (path_exists(path))
50 return 1;
51 else
52 return 0;
53 }
54
55 int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg,
56 struct dm_list *modules)
57 {
58 unsigned int s;
59 struct lv_segment *seg2, *snap_seg;
60 struct dm_list *snh;
61
62 if (seg->segtype->ops->modules_needed &&
63 !seg->segtype->ops->modules_needed(mem, seg, modules)) {
64 log_error("module string allocation failed");
65 return 0;
66 }
67
68 if (lv_is_origin(seg->lv))
69 dm_list_iterate(snh, &seg->lv->snapshot_segs)
70 if (!list_lv_modules(mem,
71 dm_list_struct_base(snh,
72 struct lv_segment,
73 origin_list)->cow,
74 modules))
75 return_0;
76
77 if (lv_is_cow(seg->lv)) {
78 snap_seg = find_cow(seg->lv);
79 if (snap_seg->segtype->ops->modules_needed &&
80 !snap_seg->segtype->ops->modules_needed(mem, snap_seg,
81 modules)) {
82 log_error("snap_seg module string allocation failed");
83 return 0;
84 }
85 }
86
87 for (s = 0; s < seg->area_count; s++) {
88 switch (seg_type(seg, s)) {
89 case AREA_LV:
90 seg2 = find_seg_by_le(seg_lv(seg, s), seg_le(seg, s));
91 if (seg2 && !list_segment_modules(mem, seg2, modules))
92 return_0;
93 break;
94 case AREA_PV:
95 case AREA_UNASSIGNED:
96 ;
97 }
98 }
99
100 return 1;
101 }
102
103 int list_lv_modules(struct dm_pool *mem, const struct logical_volume *lv,
104 struct dm_list *modules)
105 {
106 struct lv_segment *seg;
107
108 dm_list_iterate_items(seg, &lv->segments)
109 if (!list_segment_modules(mem, seg, modules))
110 return_0;
111
112 return 1;
113 }
114
115 #ifndef DEVMAPPER_SUPPORT
116 void set_activation(int act)
117 {
118 static int warned = 0;
119
120 if (warned || !act)
121 return;
122
123 log_error("Compiled without libdevmapper support. "
124 "Can't enable activation.");
125
126 warned = 1;
127 }
128 int activation(void)
129 {
130 return 0;
131 }
132 int library_version(char *version, size_t size)
133 {
134 return 0;
135 }
136 int driver_version(char *version, size_t size)
137 {
138 return 0;
139 }
140 int target_version(const char *target_name, uint32_t *maj,
141 uint32_t *min, uint32_t *patchlevel)
142 {
143 return 0;
144 }
145 int target_present(struct cmd_context *cmd, const char *target_name,
146 int use_modprobe)
147 {
148 return 0;
149 }
150 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, unsigned origin_only,
151 struct lvinfo *info, 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 unsigned origin_only,
157 struct lvinfo *info, int with_open_count, int with_read_ahead)
158 {
159 return 0;
160 }
161 int lv_snapshot_percent(const struct logical_volume *lv, percent_t *percent)
162 {
163 return 0;
164 }
165 int lv_mirror_percent(struct cmd_context *cmd, const struct logical_volume *lv,
166 int wait, percent_t *percent, 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_opened(const struct volume_group *vg)
175 {
176 return 0;
177 }
178 /******
179 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
180 {
181 return 1;
182 }
183 *******/
184 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
185 {
186 return 1;
187 }
188 int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
189 {
190 return 1;
191 }
192 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
193 unsigned origin_only, unsigned exclusive)
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 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
215 {
216 return 1;
217 }
218 int pv_uses_vg(struct physical_volume *pv,
219 struct volume_group *vg)
220 {
221 return 0;
222 }
223 void activation_release(void)
224 {
225 }
226 void activation_exit(void)
227 {
228 }
229 int lv_is_active(struct logical_volume *lv)
230 {
231 return 0;
232 }
233 int lv_is_active_exclusive_locally(struct logical_volume *lv)
234 {
235 return 0;
236 }
237 int lv_is_active_exclusive_remotely(struct logical_volume *lv)
238 {
239 return 0;
240 }
241 int lv_check_transient(struct logical_volume *lv)
242 {
243 return 1;
244 }
245 int monitor_dev_for_events(struct cmd_context *cmd, struct logical_volume *lv,
246 unsigned origin_only, int monitor)
247 {
248 return 1;
249 }
250 #else /* DEVMAPPER_SUPPORT */
251
252 static int _activation = 1;
253
254 void set_activation(int act)
255 {
256 if (act == _activation)
257 return;
258
259 _activation = act;
260 if (_activation)
261 log_verbose("Activation enabled. Device-mapper kernel "
262 "driver will be used.");
263 else
264 log_warn("WARNING: Activation disabled. No device-mapper "
265 "interaction will be attempted.");
266 }
267
268 int activation(void)
269 {
270 return _activation;
271 }
272
273 static int _passes_activation_filter(struct cmd_context *cmd,
274 struct logical_volume *lv)
275 {
276 const struct config_node *cn;
277 const struct config_value *cv;
278 const char *str;
279 char path[PATH_MAX];
280
281 if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
282 log_verbose("activation/volume_list configuration setting "
283 "not defined, checking only host tags for %s/%s",
284 lv->vg->name, lv->name);
285
286 /* If no host tags defined, activate */
287 if (dm_list_empty(&cmd->tags))
288 return 1;
289
290 /* If any host tag matches any LV or VG tag, activate */
291 if (str_list_match_list(&cmd->tags, &lv->tags, NULL) ||
292 str_list_match_list(&cmd->tags, &lv->vg->tags, NULL))
293 return 1;
294
295 log_verbose("No host tag matches %s/%s",
296 lv->vg->name, lv->name);
297
298 /* Don't activate */
299 return 0;
300 }
301 else
302 log_verbose("activation/volume_list configuration setting "
303 "defined, checking the list to match %s/%s",
304 lv->vg->name, lv->name);
305
306 for (cv = cn->v; cv; cv = cv->next) {
307 if (cv->type != CFG_STRING) {
308 log_error("Ignoring invalid string in config file "
309 "activation/volume_list");
310 continue;
311 }
312 str = cv->v.str;
313 if (!*str) {
314 log_error("Ignoring empty string in config file "
315 "activation/volume_list");
316 continue;
317 }
318
319
320 /* Tag? */
321 if (*str == '@') {
322 str++;
323 if (!*str) {
324 log_error("Ignoring empty tag in config file "
325 "activation/volume_list");
326 continue;
327 }
328 /* If any host tag matches any LV or VG tag, activate */
329 if (!strcmp(str, "*")) {
330 if (str_list_match_list(&cmd->tags, &lv->tags, NULL)
331 || str_list_match_list(&cmd->tags,
332 &lv->vg->tags, NULL))
333 return 1;
334 else
335 continue;
336 }
337 /* If supplied tag matches LV or VG tag, activate */
338 if (str_list_match_item(&lv->tags, str) ||
339 str_list_match_item(&lv->vg->tags, str))
340 return 1;
341 else
342 continue;
343 }
344 if (!strchr(str, '/')) {
345 /* vgname supplied */
346 if (!strcmp(str, lv->vg->name))
347 return 1;
348 else
349 continue;
350 }
351 /* vgname/lvname */
352 if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name,
353 lv->name) < 0) {
354 log_error("dm_snprintf error from %s/%s", lv->vg->name,
355 lv->name);
356 continue;
357 }
358 if (!strcmp(path, str))
359 return 1;
360 }
361
362 log_verbose("No item supplied in activation/volume_list configuration "
363 "setting matches %s/%s", lv->vg->name, lv->name);
364
365 return 0;
366 }
367
368 int library_version(char *version, size_t size)
369 {
370 if (!activation())
371 return 0;
372
373 return dm_get_library_version(version, size);
374 }
375
376 int driver_version(char *version, size_t size)
377 {
378 if (!activation())
379 return 0;
380
381 log_very_verbose("Getting driver version");
382
383 return dm_driver_version(version, size);
384 }
385
386 int target_version(const char *target_name, uint32_t *maj,
387 uint32_t *min, uint32_t *patchlevel)
388 {
389 int r = 0;
390 struct dm_task *dmt;
391 struct dm_versions *target, *last_target;
392
393 log_very_verbose("Getting target version for %s", target_name);
394 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
395 return_0;
396
397 if (!dm_task_run(dmt)) {
398 log_debug("Failed to get %s target version", target_name);
399 /* Assume this was because LIST_VERSIONS isn't supported */
400 return 1;
401 }
402
403 target = dm_task_get_versions(dmt);
404
405 do {
406 last_target = target;
407
408 if (!strcmp(target_name, target->name)) {
409 r = 1;
410 *maj = target->version[0];
411 *min = target->version[1];
412 *patchlevel = target->version[2];
413 goto out;
414 }
415
416 target = (struct dm_versions *)((char *) target + target->next);
417 } while (last_target != target);
418
419 out:
420 dm_task_destroy(dmt);
421
422 return r;
423 }
424
425 int module_present(struct cmd_context *cmd, const char *target_name)
426 {
427 int ret = 0;
428 #ifdef MODPROBE_CMD
429 char module[128];
430 const char *argv[3];
431
432 if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) {
433 log_error("module_present module name too long: %s",
434 target_name);
435 return 0;
436 }
437
438 argv[0] = MODPROBE_CMD;
439 argv[1] = module;
440 argv[2] = NULL;
441
442 ret = exec_cmd(cmd, argv, NULL, 0);
443 #endif
444 return ret;
445 }
446
447 int target_present(struct cmd_context *cmd, const char *target_name,
448 int use_modprobe)
449 {
450 uint32_t maj, min, patchlevel;
451
452 if (!activation())
453 return 0;
454
455 #ifdef MODPROBE_CMD
456 if (use_modprobe) {
457 if (target_version(target_name, &maj, &min, &patchlevel))
458 return 1;
459
460 if (!module_present(cmd, target_name))
461 return_0;
462 }
463 #endif
464
465 return target_version(target_name, &maj, &min, &patchlevel);
466 }
467
468 /*
469 * Returns 1 if info structure populated, else 0 on failure.
470 */
471 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, unsigned origin_only,
472 struct lvinfo *info, int with_open_count, int with_read_ahead)
473 {
474 struct dm_info dminfo;
475
476 if (!activation())
477 return 0;
478 /*
479 * If open_count info is requested and we have to be sure our own udev
480 * transactions are finished
481 * For non-clustered locking type we are only interested for non-delete operation
482 * in progress - as only those could lead to opened files
483 */
484 if (with_open_count) {
485 if (locking_is_clustered())
486 sync_local_dev_names(cmd); /* Wait to have udev in sync */
487 else if (fs_has_non_delete_ops())
488 fs_unlock(); /* For non clustered - wait if there are non-delete ops */
489 }
490
491 if (!dev_manager_info(lv->vg->cmd->mem, lv, origin_only ? "real" : NULL, with_open_count,
492 with_read_ahead, &dminfo, &info->read_ahead))
493 return_0;
494
495 info->exists = dminfo.exists;
496 info->suspended = dminfo.suspended;
497 info->open_count = dminfo.open_count;
498 info->major = dminfo.major;
499 info->minor = dminfo.minor;
500 info->read_only = dminfo.read_only;
501 info->live_table = dminfo.live_table;
502 info->inactive_table = dminfo.inactive_table;
503
504 return 1;
505 }
506
507 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
508 unsigned origin_only,
509 struct lvinfo *info, int with_open_count, int with_read_ahead)
510 {
511 int r;
512 struct logical_volume *lv;
513
514 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
515 return 0;
516
517 if (!lv_is_origin(lv))
518 origin_only = 0;
519
520 r = lv_info(cmd, lv, origin_only, info, with_open_count, with_read_ahead);
521 free_vg(lv->vg);
522
523 return r;
524 }
525
526 /*
527 * Returns 1 if percent set, else 0 on failure.
528 */
529 int lv_check_transient(struct logical_volume *lv)
530 {
531 int r;
532 struct dev_manager *dm;
533
534 if (!activation())
535 return 0;
536
537 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
538 return_0;
539
540 if (!(r = dev_manager_transient(dm, lv)))
541 stack;
542
543 dev_manager_destroy(dm);
544
545 return r;
546 }
547
548 /*
549 * Returns 1 if percent set, else 0 on failure.
550 */
551 int lv_snapshot_percent(const struct logical_volume *lv, percent_t *percent)
552 {
553 int r;
554 struct dev_manager *dm;
555
556 if (!activation())
557 return 0;
558
559 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
560 return_0;
561
562 if (!(r = dev_manager_snapshot_percent(dm, lv, percent)))
563 stack;
564
565 dev_manager_destroy(dm);
566
567 return r;
568 }
569
570 /* FIXME Merge with snapshot_percent */
571 int lv_mirror_percent(struct cmd_context *cmd, const struct logical_volume *lv,
572 int wait, percent_t *percent, uint32_t *event_nr)
573 {
574 int r;
575 struct dev_manager *dm;
576 struct lvinfo info;
577
578 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
579 * it should be considered in-sync. */
580 if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
581 *percent = PERCENT_100;
582 return 1;
583 }
584
585 if (!activation())
586 return 0;
587
588 if (!lv_info(cmd, lv, 0, &info, 0, 0))
589 return_0;
590
591 if (!info.exists)
592 return 0;
593
594 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
595 return_0;
596
597 if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, event_nr)))
598 stack;
599
600 dev_manager_destroy(dm);
601
602 return r;
603 }
604
605 static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv)
606 {
607 struct lvinfo info;
608
609 if (!lv_info(cmd, lv, 0, &info, 0, 0)) {
610 stack;
611 return -1;
612 }
613
614 return info.exists;
615 }
616
617 static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
618 {
619 struct lvinfo info;
620
621 if (!lv_info(cmd, lv, 0, &info, 1, 0)) {
622 stack;
623 return -1;
624 }
625
626 return info.open_count;
627 }
628
629 static int _lv_activate_lv(struct logical_volume *lv, unsigned origin_only)
630 {
631 int r;
632 struct dev_manager *dm;
633
634 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
635 return_0;
636
637 if (!(r = dev_manager_activate(dm, lv, origin_only)))
638 stack;
639
640 dev_manager_destroy(dm);
641 return r;
642 }
643
644 static int _lv_preload(struct logical_volume *lv, unsigned origin_only, int *flush_required)
645 {
646 int r;
647 struct dev_manager *dm;
648
649 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
650 return_0;
651
652 if (!(r = dev_manager_preload(dm, lv, origin_only, flush_required)))
653 stack;
654
655 dev_manager_destroy(dm);
656 return r;
657 }
658
659 static int _lv_deactivate(struct logical_volume *lv)
660 {
661 int r;
662 struct dev_manager *dm;
663
664 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
665 return_0;
666
667 if (!(r = dev_manager_deactivate(dm, lv)))
668 stack;
669
670 dev_manager_destroy(dm);
671 return r;
672 }
673
674 static int _lv_suspend_lv(struct logical_volume *lv, unsigned origin_only, int lockfs, int flush_required)
675 {
676 int r;
677 struct dev_manager *dm;
678
679 /*
680 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
681 * device directly, we don't want to touch the devices that use it.
682 */
683 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
684 return_0;
685
686 if (!(r = dev_manager_suspend(dm, lv, origin_only, lockfs, flush_required)))
687 stack;
688
689 dev_manager_destroy(dm);
690 return r;
691 }
692
693 /*
694 * These two functions return the number of visible LVs in the state,
695 * or -1 on error.
696 */
697 int lvs_in_vg_activated(struct volume_group *vg)
698 {
699 struct lv_list *lvl;
700 int count = 0;
701
702 if (!activation())
703 return 0;
704
705 dm_list_iterate_items(lvl, &vg->lvs) {
706 if (lv_is_visible(lvl->lv))
707 count += (_lv_active(vg->cmd, lvl->lv) == 1);
708 }
709
710 return count;
711 }
712
713 int lvs_in_vg_opened(const struct volume_group *vg)
714 {
715 const struct lv_list *lvl;
716 int count = 0;
717
718 if (!activation())
719 return 0;
720
721 dm_list_iterate_items(lvl, &vg->lvs) {
722 if (lv_is_visible(lvl->lv))
723 count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
724 }
725
726 return count;
727 }
728
729 /*
730 * _lv_is_active
731 * @lv: logical volume being queried
732 * @locally: set if active locally (when provided)
733 * @exclusive: set if active exclusively (when provided)
734 *
735 * Determine whether an LV is active locally or in a cluster.
736 * In addition to the return code which indicates whether or
737 * not the LV is active somewhere, two other values are set
738 * to yield more information about the status of the activation:
739 * return locally exclusively status
740 * ====== ======= =========== ======
741 * 0 0 0 not active
742 * 1 0 0 active remotely
743 * 1 0 1 exclusive remotely
744 * 1 1 0 active locally and possibly remotely
745 * 1 1 1 exclusive locally (or local && !cluster)
746 * The VG lock must be held to call this function.
747 *
748 * Returns: 0 or 1
749 */
750 static int _lv_is_active(struct logical_volume *lv,
751 int *locally, int *exclusive)
752 {
753 int r, l, e; /* remote, local, and exclusive */
754
755 r = l = e = 0;
756
757 if (_lv_active(lv->vg->cmd, lv))
758 l = 1;
759
760 if (!vg_is_clustered(lv->vg)) {
761 e = 1; /* exclusive by definition */
762 goto out;
763 }
764
765 /* Active locally, and the caller doesn't care about exclusive */
766 if (l && !exclusive)
767 goto out;
768
769 if ((r = remote_lock_held(lv->lvid.s, &e)) >= 0)
770 goto out;
771
772 /*
773 * If lock query is not supported (due to interfacing with old
774 * code), then we cannot evaluate exclusivity properly.
775 *
776 * Old users of this function will never be affected by this,
777 * since they are only concerned about active vs. not active.
778 * New users of this function who specifically ask for 'exclusive'
779 * will be given an error message.
780 */
781 if (l) {
782 if (exclusive)
783 log_error("Unable to determine exclusivity of %s",
784 lv->name);
785 goto out;
786 }
787
788 if (activate_lv_excl(lv->vg->cmd, lv)) {
789 if (!deactivate_lv(lv->vg->cmd, lv))
790 stack;
791 return 0;
792 }
793
794 out:
795 if (locally)
796 *locally = l;
797 if (exclusive)
798 *exclusive = e;
799
800 log_very_verbose("%s/%s is %sactive%s%s",
801 lv->vg->name, lv->name,
802 (r || l) ? "" : "not ",
803 (exclusive && e) ? " exclusive" : "",
804 e ? (l ? " locally" : " remotely") : "");
805
806 return r || l;
807 }
808
809 int lv_is_active(struct logical_volume *lv)
810 {
811 return _lv_is_active(lv, NULL, NULL);
812 }
813
814 /*
815 int lv_is_active_locally(struct logical_volume *lv)
816 {
817 int l;
818 return _lv_is_active(lv, &l, NULL) && l;
819 }
820 */
821
822 int lv_is_active_exclusive_locally(struct logical_volume *lv)
823 {
824 int l, e;
825 return _lv_is_active(lv, &l, &e) && l && e;
826 }
827
828 int lv_is_active_exclusive_remotely(struct logical_volume *lv)
829 {
830 int l, e;
831 return _lv_is_active(lv, &l, &e) && !l && e;
832 }
833
834 #ifdef DMEVENTD
835 static struct dm_event_handler *_create_dm_event_handler(struct cmd_context *cmd, const char *dmuuid, const char *dso,
836 const int timeout, enum dm_event_mask mask)
837 {
838 struct dm_event_handler *dmevh;
839
840 if (!(dmevh = dm_event_handler_create()))
841 return_NULL;
842
843 if (dm_event_handler_set_dmeventd_path(dmevh, find_config_tree_str(cmd, "dmeventd/executable", NULL)))
844 goto_bad;
845
846 if (dm_event_handler_set_dso(dmevh, dso))
847 goto_bad;
848
849 if (dm_event_handler_set_uuid(dmevh, dmuuid))
850 goto_bad;
851
852 dm_event_handler_set_timeout(dmevh, timeout);
853 dm_event_handler_set_event_mask(dmevh, mask);
854
855 return dmevh;
856
857 bad:
858 dm_event_handler_destroy(dmevh);
859 return NULL;
860 }
861
862 char *get_monitor_dso_path(struct cmd_context *cmd, const char *libpath)
863 {
864 char *path;
865
866 if (!(path = dm_pool_alloc(cmd->mem, PATH_MAX))) {
867 log_error("Failed to allocate dmeventd library path.");
868 return NULL;
869 }
870
871 get_shared_library_path(cmd, libpath, path, PATH_MAX);
872
873 return path;
874 }
875
876 int target_registered_with_dmeventd(struct cmd_context *cmd, const char *dso,
877 struct logical_volume *lv, int *pending)
878 {
879 char *uuid;
880 enum dm_event_mask evmask = 0;
881 struct dm_event_handler *dmevh;
882
883 *pending = 0;
884
885 if (!dso)
886 return_0;
887
888 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
889 if (!(uuid = build_dm_uuid(cmd->mem, lv->lvid.s, lv_is_origin(lv) ? "real" : NULL)))
890 return_0;
891
892 if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, 0, DM_EVENT_ALL_ERRORS)))
893 return_0;
894
895 if (dm_event_get_registered_device(dmevh, 0)) {
896 dm_event_handler_destroy(dmevh);
897 return 0;
898 }
899
900 evmask = dm_event_handler_get_event_mask(dmevh);
901 if (evmask & DM_EVENT_REGISTRATION_PENDING) {
902 *pending = 1;
903 evmask &= ~DM_EVENT_REGISTRATION_PENDING;
904 }
905
906 dm_event_handler_destroy(dmevh);
907
908 return evmask;
909 }
910
911 int target_register_events(struct cmd_context *cmd, const char *dso, struct logical_volume *lv,
912 int evmask __attribute__((unused)), int set, int timeout)
913 {
914 char *uuid;
915 struct dm_event_handler *dmevh;
916 int r;
917
918 if (!dso)
919 return_0;
920
921 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
922 if (!(uuid = build_dm_uuid(cmd->mem, lv->lvid.s, lv_is_origin(lv) ? "real" : NULL)))
923 return_0;
924
925 if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, timeout,
926 DM_EVENT_ALL_ERRORS | (timeout ? DM_EVENT_TIMEOUT : 0))))
927 return_0;
928
929 r = set ? dm_event_register_handler(dmevh) : dm_event_unregister_handler(dmevh);
930
931 dm_event_handler_destroy(dmevh);
932
933 if (!r)
934 return_0;
935
936 log_info("%s %s for events", set ? "Monitored" : "Unmonitored", uuid);
937
938 return 1;
939 }
940
941 #endif
942
943 /*
944 * Returns 0 if an attempt to (un)monitor the device failed.
945 * Returns 1 otherwise.
946 */
947 int monitor_dev_for_events(struct cmd_context *cmd, struct logical_volume *lv,
948 unsigned origin_only, int monitor)
949 {
950 #ifdef DMEVENTD
951 int i, pending = 0, monitored;
952 int r = 1;
953 struct dm_list *tmp, *snh, *snht;
954 struct lv_segment *seg;
955 struct lv_segment *log_seg;
956 int (*monitor_fn) (struct lv_segment *s, int e);
957 uint32_t s;
958
959 /* skip dmeventd code altogether */
960 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
961 return 1;
962
963 /*
964 * Nothing to do if dmeventd configured not to be used.
965 */
966 if (monitor && !dmeventd_monitor_mode())
967 return 1;
968
969 /*
970 * In case of a snapshot device, we monitor lv->snapshot->lv,
971 * not the actual LV itself.
972 */
973 if (lv_is_cow(lv) && !lv_is_merging_cow(lv))
974 return monitor_dev_for_events(cmd, lv->snapshot->lv, 0, monitor);
975
976 /*
977 * In case this LV is a snapshot origin, we instead monitor
978 * each of its respective snapshots. The origin itself may
979 * also need to be monitored if it is a mirror, for example.
980 */
981 if (!origin_only && lv_is_origin(lv))
982 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
983 if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
984 struct lv_segment, origin_list)->cow, 0, monitor))
985 r = 0;
986
987 /*
988 * If the volume is mirrored and its log is also mirrored, monitor
989 * the log volume as well.
990 */
991 if ((seg = first_seg(lv)) != NULL && seg->log_lv != NULL &&
992 (log_seg = first_seg(seg->log_lv)) != NULL &&
993 seg_is_mirrored(log_seg))
994 if (!monitor_dev_for_events(cmd, seg->log_lv, 0, monitor))
995 r = 0;
996
997 dm_list_iterate(tmp, &lv->segments) {
998 seg = dm_list_item(tmp, struct lv_segment);
999
1000 /* Recurse for AREA_LV */
1001 for (s = 0; s < seg->area_count; s++) {
1002 if (seg_type(seg, s) != AREA_LV)
1003 continue;
1004 if (!monitor_dev_for_events(cmd, seg_lv(seg, s), 0,
1005 monitor)) {
1006 log_error("Failed to %smonitor %s",
1007 monitor ? "" : "un",
1008 seg_lv(seg, s)->name);
1009 r = 0;
1010 }
1011 }
1012
1013 if (!seg_monitored(seg) || (seg->status & PVMOVE))
1014 continue;
1015
1016 monitor_fn = NULL;
1017
1018 /* Check monitoring status */
1019 if (seg->segtype->ops->target_monitored)
1020 monitored = seg->segtype->ops->target_monitored(seg, &pending);
1021 else
1022 continue; /* segtype doesn't support registration */
1023
1024 /*
1025 * FIXME: We should really try again if pending
1026 */
1027 monitored = (pending) ? 0 : monitored;
1028
1029 if (monitor) {
1030 if (monitored)
1031 log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
1032 else if (seg->segtype->ops->target_monitor_events)
1033 monitor_fn = seg->segtype->ops->target_monitor_events;
1034 } else {
1035 if (!monitored)
1036 log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
1037 else if (seg->segtype->ops->target_unmonitor_events)
1038 monitor_fn = seg->segtype->ops->target_unmonitor_events;
1039 }
1040
1041 /* Do [un]monitor */
1042 if (!monitor_fn)
1043 continue;
1044
1045 log_verbose("%sonitoring %s/%s%s", monitor ? "M" : "Not m", lv->vg->name, lv->name,
1046 test_mode() ? " [Test mode: skipping this]" : "");
1047
1048 /* FIXME Test mode should really continue a bit further. */
1049 if (test_mode())
1050 continue;
1051
1052 /* FIXME specify events */
1053 if (!monitor_fn(seg, 0)) {
1054 log_error("%s/%s: %s segment monitoring function failed.",
1055 lv->vg->name, lv->name, seg->segtype->name);
1056 return 0;
1057 }
1058
1059 /* Check [un]monitor results */
1060 /* Try a couple times if pending, but not forever... */
1061 for (i = 0; i < 10; i++) {
1062 pending = 0;
1063 monitored = seg->segtype->ops->target_monitored(seg, &pending);
1064 if (pending ||
1065 (!monitored && monitor) ||
1066 (monitored && !monitor))
1067 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
1068 lv->vg->name, lv->name, monitor ? "" : "un");
1069 else
1070 break;
1071 sleep(1);
1072 }
1073
1074 if (r)
1075 r = (monitored && monitor) || (!monitored && !monitor);
1076 }
1077
1078 return r;
1079 #else
1080 return 1;
1081 #endif
1082 }
1083
1084 static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
1085 unsigned origin_only, int error_if_not_suspended)
1086 {
1087 struct logical_volume *lv = NULL, *lv_pre = NULL, *pvmove_lv = NULL;
1088 struct lv_list *lvl_pre;
1089 struct seg_list *sl;
1090 struct lvinfo info;
1091 int r = 0, lockfs = 0, flush_required = 0;
1092
1093 if (!activation())
1094 return 1;
1095
1096 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1097 goto_out;
1098
1099 /* Use precommitted metadata if present */
1100 if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
1101 goto_out;
1102
1103 /* Ignore origin_only unless LV is origin in both old and new metadata */
1104 if (!lv_is_origin(lv) || !lv_is_origin(lv_pre))
1105 origin_only = 0;
1106
1107 if (test_mode()) {
1108 _skip("Suspending %s%s.", lv->name, origin_only ? " origin without snapshots" : "");
1109 r = 1;
1110 goto out;
1111 }
1112
1113 if (!lv_info(cmd, lv, origin_only, &info, 0, 0))
1114 goto_out;
1115
1116 if (!info.exists || info.suspended) {
1117 if (!error_if_not_suspended) {
1118 r = 1;
1119 if (info.suspended)
1120 critical_section_inc(cmd, "already suspended");
1121 }
1122 goto out;
1123 }
1124
1125 if (!lv_read_replicator_vgs(lv))
1126 goto_out;
1127
1128 lv_calculate_readahead(lv, NULL);
1129
1130 /*
1131 * If VG was precommitted, preload devices for the LV.
1132 * If the PVMOVE LV is being removed, it's only present in the old
1133 * metadata and not the new, so we must explicitly add the new
1134 * tables for all the changed LVs here, as the relationships
1135 * are not found by walking the new metadata.
1136 */
1137 if ((lv_pre->vg->status & PRECOMMITTED)) {
1138 if (!(lv_pre->status & LOCKED) &&
1139 (lv->status & LOCKED) &&
1140 (pvmove_lv = find_pvmove_lv_in_lv(lv))) {
1141 /* Preload all the LVs above the PVMOVE LV */
1142 dm_list_iterate_items(sl, &pvmove_lv->segs_using_this_lv) {
1143 if (!(lvl_pre = find_lv_in_vg(lv_pre->vg, sl->seg->lv->name))) {
1144 /* FIXME Internal error? */
1145 log_error("LV %s missing from preload metadata", sl->seg->lv->name);
1146 goto out;
1147 }
1148 if (!_lv_preload(lvl_pre->lv, origin_only, &flush_required))
1149 goto_out;
1150 }
1151 /* Now preload the PVMOVE LV itself */
1152 if (!(lvl_pre = find_lv_in_vg(lv_pre->vg, pvmove_lv->name))) {
1153 /* FIXME Internal error? */
1154 log_error("LV %s missing from preload metadata", pvmove_lv->name);
1155 goto out;
1156 }
1157 if (!_lv_preload(lvl_pre->lv, origin_only, &flush_required))
1158 goto_out;
1159 } else if (!_lv_preload(lv_pre, origin_only, &flush_required))
1160 /* FIXME Revert preloading */
1161 goto_out;
1162 }
1163
1164 if (!monitor_dev_for_events(cmd, lv, origin_only, 0))
1165 /* FIXME Consider aborting here */
1166 stack;
1167
1168 critical_section_inc(cmd, "suspending");
1169 if (pvmove_lv)
1170 critical_section_inc(cmd, "suspending pvmove LV");
1171
1172 if (!origin_only &&
1173 (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)))
1174 lockfs = 1;
1175
1176 /*
1177 * Suspending an LV directly above a PVMOVE LV also
1178 * suspends other LVs using that same PVMOVE LV.
1179 * FIXME Remove this and delay the 'clear node' until
1180 * after the code knows whether there's a different
1181 * inactive table to load or not instead so lv_suspend
1182 * can be called separately for each LV safely.
1183 */
1184 if ((lv_pre->vg->status & PRECOMMITTED) &&
1185 (lv_pre->status & LOCKED) && find_pvmove_lv_in_lv(lv_pre)) {
1186 if (!_lv_suspend_lv(lv_pre, origin_only, lockfs, flush_required)) {
1187 critical_section_dec(cmd, "failed precommitted suspend");
1188 if (pvmove_lv)
1189 critical_section_dec(cmd, "failed precommitted suspend (pvmove)");
1190 goto_out;
1191 }
1192 } else {
1193 /* Normal suspend */
1194 if (!_lv_suspend_lv(lv, origin_only, lockfs, flush_required)) {
1195 critical_section_dec(cmd, "failed suspend");
1196 if (pvmove_lv)
1197 critical_section_dec(cmd, "failed suspend (pvmove)");
1198 goto_out;
1199 }
1200 }
1201
1202 r = 1;
1203 out:
1204 if (lv_pre)
1205 free_vg(lv_pre->vg);
1206 if (lv) {
1207 lv_release_replicator_vgs(lv);
1208 free_vg(lv->vg);
1209 }
1210
1211 return r;
1212 }
1213
1214 /* Returns success if the device is not active */
1215 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
1216 {
1217 return _lv_suspend(cmd, lvid_s, origin_only, 0);
1218 }
1219
1220 /* No longer used */
1221 /***********
1222 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1223 {
1224 return _lv_suspend(cmd, lvid_s, 1);
1225 }
1226 ***********/
1227
1228 /*
1229 * _lv_resume
1230 * @cmd
1231 * @lvid_s
1232 * @origin_only
1233 * @exclusive: This parameter only has an affect in cluster-context.
1234 * It forces local target type to be used (instead of
1235 * cluster-aware type).
1236 * @error_if_not_active
1237 */
1238 static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
1239 unsigned origin_only, unsigned exclusive,
1240 int error_if_not_active)
1241 {
1242 struct logical_volume *lv;
1243 struct lvinfo info;
1244 int r = 0;
1245
1246 if (!activation())
1247 return 1;
1248
1249 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1250 goto_out;
1251
1252 if (!lv_is_origin(lv))
1253 origin_only = 0;
1254
1255 if (test_mode()) {
1256 _skip("Resuming %s%s.", lv->name, origin_only ? " without snapshots" : "");
1257 r = 1;
1258 goto out;
1259 }
1260
1261 if (!lv_info(cmd, lv, origin_only, &info, 0, 0))
1262 goto_out;
1263
1264 if (!info.exists || !info.suspended) {
1265 if (error_if_not_active)
1266 goto_out;
1267 r = 1;
1268 if (!info.suspended)
1269 critical_section_dec(cmd, "already resumed");
1270 goto out;
1271 }
1272
1273 /*
1274 * When targets are activated exclusively in a cluster, the
1275 * non-clustered target should be used. This only happens
1276 * if ACTIVATE_EXCL is set in lv->status.
1277 */
1278 if (exclusive)
1279 lv->status |= ACTIVATE_EXCL;
1280
1281 if (!_lv_activate_lv(lv, origin_only))
1282 goto_out;
1283
1284 critical_section_dec(cmd, "resumed");
1285
1286 if (!monitor_dev_for_events(cmd, lv, origin_only, 1))
1287 stack;
1288
1289 r = 1;
1290 out:
1291 if (lv)
1292 free_vg(lv->vg);
1293
1294 return r;
1295 }
1296
1297 /* Returns success if the device is not active */
1298 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
1299 unsigned origin_only, unsigned exclusive)
1300 {
1301 return _lv_resume(cmd, lvid_s, origin_only, exclusive, 0);
1302 }
1303
1304 int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
1305 {
1306 return _lv_resume(cmd, lvid_s, origin_only, 0, 1);
1307 }
1308
1309 static int _lv_has_open_snapshots(struct logical_volume *lv)
1310 {
1311 struct lv_segment *snap_seg;
1312 struct lvinfo info;
1313 int r = 0;
1314
1315 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
1316 if (!lv_info(lv->vg->cmd, snap_seg->cow, 0, &info, 1, 0)) {
1317 r = 1;
1318 continue;
1319 }
1320
1321 if (info.exists && info.open_count) {
1322 log_error("LV %s/%s has open snapshot %s: "
1323 "not deactivating", lv->vg->name, lv->name,
1324 snap_seg->cow->name);
1325 r = 1;
1326 }
1327 }
1328
1329 return r;
1330 }
1331
1332 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
1333 {
1334 struct logical_volume *lv;
1335 struct lvinfo info;
1336 int r = 0;
1337
1338 if (!activation())
1339 return 1;
1340
1341 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1342 goto out;
1343
1344 if (test_mode()) {
1345 _skip("Deactivating '%s'.", lv->name);
1346 r = 1;
1347 goto out;
1348 }
1349
1350 if (!lv_info(cmd, lv, 0, &info, 1, 0))
1351 goto_out;
1352
1353 if (!info.exists) {
1354 r = 1;
1355 goto out;
1356 }
1357
1358 if (lv_is_visible(lv)) {
1359 if (info.open_count) {
1360 log_error("LV %s/%s in use: not deactivating",
1361 lv->vg->name, lv->name);
1362 goto out;
1363 }
1364 if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1365 goto_out;
1366 }
1367
1368 if (!lv_read_replicator_vgs(lv))
1369 goto_out;
1370
1371 lv_calculate_readahead(lv, NULL);
1372
1373 if (!monitor_dev_for_events(cmd, lv, 0, 0))
1374 stack;
1375
1376 critical_section_inc(cmd, "deactivating");
1377 r = _lv_deactivate(lv);
1378 critical_section_dec(cmd, "deactivated");
1379
1380 if (!lv_info(cmd, lv, 0, &info, 0, 0) || info.exists)
1381 r = 0;
1382 out:
1383 if (lv) {
1384 lv_release_replicator_vgs(lv);
1385 free_vg(lv->vg);
1386 }
1387
1388 return r;
1389 }
1390
1391 /* Test if LV passes filter */
1392 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
1393 int *activate_lv)
1394 {
1395 struct logical_volume *lv;
1396 int r = 0;
1397
1398 if (!activation()) {
1399 *activate_lv = 1;
1400 return 1;
1401 }
1402
1403 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1404 goto out;
1405
1406 if (!_passes_activation_filter(cmd, lv)) {
1407 log_verbose("Not activating %s/%s since it does not pass "
1408 "activation filter.", lv->vg->name, lv->name);
1409 *activate_lv = 0;
1410 } else
1411 *activate_lv = 1;
1412 r = 1;
1413 out:
1414 if (lv)
1415 free_vg(lv->vg);
1416
1417 return r;
1418 }
1419
1420 static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
1421 int exclusive, int filter)
1422 {
1423 struct logical_volume *lv;
1424 struct lvinfo info;
1425 int r = 0;
1426
1427 if (!activation())
1428 return 1;
1429
1430 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1431 goto out;
1432
1433 if (filter && !_passes_activation_filter(cmd, lv)) {
1434 log_error("Not activating %s/%s since it does not pass "
1435 "activation filter.", lv->vg->name, lv->name);
1436 goto out;
1437 }
1438
1439 if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
1440 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1441 lv->name);
1442 goto_out;
1443 }
1444
1445 if (lv_has_unknown_segments(lv)) {
1446 log_error("Refusing activation of LV %s containing "
1447 "an unrecognised segment.", lv->name);
1448 goto_out;
1449 }
1450
1451 if (test_mode()) {
1452 _skip("Activating '%s'.", lv->name);
1453 r = 1;
1454 goto out;
1455 }
1456
1457 if (!lv_info(cmd, lv, 0, &info, 0, 0))
1458 goto_out;
1459
1460 if (info.exists && !info.suspended && info.live_table) {
1461 r = 1;
1462 goto out;
1463 }
1464
1465 if (!lv_read_replicator_vgs(lv))
1466 goto_out;
1467
1468 lv_calculate_readahead(lv, NULL);
1469
1470 if (exclusive)
1471 lv->status |= ACTIVATE_EXCL;
1472
1473 critical_section_inc(cmd, "activating");
1474 if (!(r = _lv_activate_lv(lv, 0)))
1475 stack;
1476 critical_section_dec(cmd, "activated");
1477
1478 if (r && !monitor_dev_for_events(cmd, lv, 0, 1))
1479 stack;
1480
1481 out:
1482 if (lv) {
1483 lv_release_replicator_vgs(lv);
1484 free_vg(lv->vg);
1485 }
1486
1487 return r;
1488 }
1489
1490 /* Activate LV */
1491 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1492 {
1493 if (!_lv_activate(cmd, lvid_s, exclusive, 0))
1494 return_0;
1495
1496 return 1;
1497 }
1498
1499 /* Activate LV only if it passes filter */
1500 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1501 {
1502 if (!_lv_activate(cmd, lvid_s, exclusive, 1))
1503 return_0;
1504
1505 return 1;
1506 }
1507
1508 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1509 {
1510 int r = 1;
1511
1512 if (!lv) {
1513 r = dm_mknodes(NULL);
1514 fs_unlock();
1515 return r;
1516 }
1517
1518 if (!activation())
1519 return 1;
1520
1521 r = dev_manager_mknodes(lv);
1522
1523 fs_unlock();
1524
1525 return r;
1526 }
1527
1528 /*
1529 * Does PV use VG somewhere in its construction?
1530 * Returns 1 on failure.
1531 */
1532 int pv_uses_vg(struct physical_volume *pv,
1533 struct volume_group *vg)
1534 {
1535 if (!activation())
1536 return 0;
1537
1538 if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1539 return 0;
1540
1541 return dev_manager_device_uses_vg(pv->dev, vg);
1542 }
1543
1544 void activation_release(void)
1545 {
1546 dev_manager_release();
1547 }
1548
1549 void activation_exit(void)
1550 {
1551 dev_manager_exit();
1552 }
1553 #endif
This page took 0.201059 seconds and 6 git commands to generate.