import source from lvm2 2.02.79
[external/device-mapper.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 #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, 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(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)
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         const struct config_value *cv;
263         const char *str;
264         char path[PATH_MAX];
265
266         if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
267                 log_verbose("activation/volume_list configuration setting "
268                             "not defined, checking only host tags for %s/%s",
269                             lv->vg->name, lv->name);
270
271                 /* If no host tags defined, activate */
272                 if (dm_list_empty(&cmd->tags))
273                         return 1;
274
275                 /* If any host tag matches any LV or VG tag, activate */
276                 if (str_list_match_list(&cmd->tags, &lv->tags, NULL) ||
277                     str_list_match_list(&cmd->tags, &lv->vg->tags, NULL))
278                         return 1;
279
280                 log_verbose("No host tag matches %s/%s",
281                             lv->vg->name, lv->name);
282
283                 /* Don't activate */
284                 return 0;
285         }
286         else
287                 log_verbose("activation/volume_list configuration setting "
288                             "defined, checking the list to match %s/%s",
289                             lv->vg->name, lv->name);
290
291         for (cv = cn->v; cv; cv = cv->next) {
292                 if (cv->type != CFG_STRING) {
293                         log_error("Ignoring invalid string in config file "
294                                   "activation/volume_list");
295                         continue;
296                 }
297                 str = cv->v.str;
298                 if (!*str) {
299                         log_error("Ignoring empty string in config file "
300                                   "activation/volume_list");
301                         continue;
302                 }
303
304
305                 /* Tag? */
306                 if (*str == '@') {
307                         str++;
308                         if (!*str) {
309                                 log_error("Ignoring empty tag in config file "
310                                           "activation/volume_list");
311                                 continue;
312                         }
313                         /* If any host tag matches any LV or VG tag, activate */
314                         if (!strcmp(str, "*")) {
315                                 if (str_list_match_list(&cmd->tags, &lv->tags, NULL)
316                                     || str_list_match_list(&cmd->tags,
317                                                            &lv->vg->tags, NULL))
318                                             return 1;
319                                 else
320                                         continue;
321                         }
322                         /* If supplied tag matches LV or VG tag, activate */
323                         if (str_list_match_item(&lv->tags, str) ||
324                             str_list_match_item(&lv->vg->tags, str))
325                                 return 1;
326                         else
327                                 continue;
328                 }
329                 if (!strchr(str, '/')) {
330                         /* vgname supplied */
331                         if (!strcmp(str, lv->vg->name))
332                                 return 1;
333                         else
334                                 continue;
335                 }
336                 /* vgname/lvname */
337                 if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name,
338                                  lv->name) < 0) {
339                         log_error("dm_snprintf error from %s/%s", lv->vg->name,
340                                   lv->name);
341                         continue;
342                 }
343                 if (!strcmp(path, str))
344                         return 1;
345         }
346
347         log_verbose("No item supplied in activation/volume_list configuration "
348                     "setting matches %s/%s", lv->vg->name, lv->name);
349
350         return 0;
351 }
352
353 int library_version(char *version, size_t size)
354 {
355         if (!activation())
356                 return 0;
357
358         return dm_get_library_version(version, size);
359 }
360
361 int driver_version(char *version, size_t size)
362 {
363         if (!activation())
364                 return 0;
365
366         log_very_verbose("Getting driver version");
367
368         return dm_driver_version(version, size);
369 }
370
371 int target_version(const char *target_name, uint32_t *maj,
372                    uint32_t *min, uint32_t *patchlevel)
373 {
374         int r = 0;
375         struct dm_task *dmt;
376         struct dm_versions *target, *last_target;
377
378         log_very_verbose("Getting target version for %s", target_name);
379         if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
380                 return_0;
381
382         if (!dm_task_run(dmt)) {
383                 log_debug("Failed to get %s target version", target_name);
384                 /* Assume this was because LIST_VERSIONS isn't supported */
385                 return 1;
386         }
387
388         target = dm_task_get_versions(dmt);
389
390         do {
391                 last_target = target;
392
393                 if (!strcmp(target_name, target->name)) {
394                         r = 1;
395                         *maj = target->version[0];
396                         *min = target->version[1];
397                         *patchlevel = target->version[2];
398                         goto out;
399                 }
400
401                 target = (struct dm_versions *)((char *) target + target->next);
402         } while (last_target != target);
403
404       out:
405         dm_task_destroy(dmt);
406
407         return r;
408 }
409
410 int module_present(struct cmd_context *cmd, const char *target_name)
411 {
412         int ret = 0;
413 #ifdef MODPROBE_CMD
414         char module[128];
415         const char *argv[3];
416
417         if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) {
418                 log_error("module_present module name too long: %s",
419                           target_name);
420                 return 0;
421         }
422
423         argv[0] = MODPROBE_CMD;
424         argv[1] = module;
425         argv[2] = NULL;
426
427         ret = exec_cmd(cmd, argv, NULL);
428 #endif
429         return ret;
430 }
431
432 int target_present(struct cmd_context *cmd, const char *target_name,
433                    int use_modprobe)
434 {
435         uint32_t maj, min, patchlevel;
436
437         if (!activation())
438                 return 0;
439
440 #ifdef MODPROBE_CMD
441         if (use_modprobe) {
442                 if (target_version(target_name, &maj, &min, &patchlevel))
443                         return 1;
444
445                 if (!module_present(cmd, target_name))
446                         return_0;
447         }
448 #endif
449
450         return target_version(target_name, &maj, &min, &patchlevel);
451 }
452
453 /*
454  * Returns 1 if info structure populated, else 0 on failure.
455  */
456 int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, unsigned origin_only,
457             struct lvinfo *info, int with_open_count, int with_read_ahead)
458 {
459         struct dm_info dminfo;
460
461         if (!activation())
462                 return 0;
463
464         if (!dev_manager_info(lv->vg->cmd->mem, lv, origin_only ? "real" : NULL, with_open_count,
465                               with_read_ahead, &dminfo, &info->read_ahead))
466                 return_0;
467
468         info->exists = dminfo.exists;
469         info->suspended = dminfo.suspended;
470         info->open_count = dminfo.open_count;
471         info->major = dminfo.major;
472         info->minor = dminfo.minor;
473         info->read_only = dminfo.read_only;
474         info->live_table = dminfo.live_table;
475         info->inactive_table = dminfo.inactive_table;
476
477         return 1;
478 }
479
480 int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
481                     unsigned origin_only,
482                     struct lvinfo *info, int with_open_count, int with_read_ahead)
483 {
484         int r;
485         struct logical_volume *lv;
486
487         if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
488                 return 0;
489
490         if (!lv_is_origin(lv))
491                 origin_only = 0;
492
493         r = lv_info(cmd, lv, origin_only, info, with_open_count, with_read_ahead);
494         free_vg(lv->vg);
495
496         return r;
497 }
498
499 /*
500  * Returns 1 if percent set, else 0 on failure.
501  */
502 int lv_check_transient(struct logical_volume *lv)
503 {
504         int r;
505         struct dev_manager *dm;
506
507         if (!activation())
508                 return 0;
509
510         if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
511                 return_0;
512
513         if (!(r = dev_manager_transient(dm, lv)))
514                 stack;
515
516         dev_manager_destroy(dm);
517
518         return r;
519 }
520
521 /*
522  * Returns 1 if percent set, else 0 on failure.
523  */
524 int lv_snapshot_percent(const struct logical_volume *lv, percent_t *percent)
525 {
526         int r;
527         struct dev_manager *dm;
528
529         if (!activation())
530                 return 0;
531
532         if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
533                 return_0;
534
535         if (!(r = dev_manager_snapshot_percent(dm, lv, percent)))
536                 stack;
537
538         dev_manager_destroy(dm);
539
540         return r;
541 }
542
543 /* FIXME Merge with snapshot_percent */
544 int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv,
545                       int wait, percent_t *percent, uint32_t *event_nr)
546 {
547         int r;
548         struct dev_manager *dm;
549         struct lvinfo info;
550
551         /* If mirrored LV is temporarily shrinked to 1 area (= linear),
552          * it should be considered in-sync. */
553         if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
554                 *percent = PERCENT_100;
555                 return 1;
556         }
557
558         if (!activation())
559                 return 0;
560
561         if (!lv_info(cmd, lv, 0, &info, 0, 0))
562                 return_0;
563
564         if (!info.exists)
565                 return 0;
566
567         if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
568                 return_0;
569
570         if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, event_nr)))
571                 stack;
572
573         dev_manager_destroy(dm);
574
575         return r;
576 }
577
578 static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv)
579 {
580         struct lvinfo info;
581
582         if (!lv_info(cmd, lv, 0, &info, 0, 0)) {
583                 stack;
584                 return -1;
585         }
586
587         return info.exists;
588 }
589
590 static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
591 {
592         struct lvinfo info;
593
594         if (!lv_info(cmd, lv, 0, &info, 1, 0)) {
595                 stack;
596                 return -1;
597         }
598
599         return info.open_count;
600 }
601
602 static int _lv_activate_lv(struct logical_volume *lv, unsigned origin_only)
603 {
604         int r;
605         struct dev_manager *dm;
606
607         if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
608                 return_0;
609
610         if (!(r = dev_manager_activate(dm, lv, origin_only)))
611                 stack;
612
613         dev_manager_destroy(dm);
614         return r;
615 }
616
617 static int _lv_preload(struct logical_volume *lv, unsigned origin_only, int *flush_required)
618 {
619         int r;
620         struct dev_manager *dm;
621
622         if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
623                 return_0;
624
625         if (!(r = dev_manager_preload(dm, lv, origin_only, flush_required)))
626                 stack;
627
628         dev_manager_destroy(dm);
629         return r;
630 }
631
632 static int _lv_deactivate(struct logical_volume *lv)
633 {
634         int r;
635         struct dev_manager *dm;
636
637         if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
638                 return_0;
639
640         if (!(r = dev_manager_deactivate(dm, lv)))
641                 stack;
642
643         dev_manager_destroy(dm);
644         return r;
645 }
646
647 static int _lv_suspend_lv(struct logical_volume *lv, unsigned origin_only, int lockfs, int flush_required)
648 {
649         int r;
650         struct dev_manager *dm;
651
652         if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name)))
653                 return_0;
654
655         if (!(r = dev_manager_suspend(dm, lv, origin_only, lockfs, flush_required)))
656                 stack;
657
658         dev_manager_destroy(dm);
659         return r;
660 }
661
662 /*
663  * These two functions return the number of visible LVs in the state,
664  * or -1 on error.
665  */
666 int lvs_in_vg_activated(struct volume_group *vg)
667 {
668         struct lv_list *lvl;
669         int count = 0;
670
671         if (!activation())
672                 return 0;
673
674         dm_list_iterate_items(lvl, &vg->lvs) {
675                 if (lv_is_visible(lvl->lv))
676                         count += (_lv_active(vg->cmd, lvl->lv) == 1);
677         }
678
679         return count;
680 }
681
682 int lvs_in_vg_opened(const struct volume_group *vg)
683 {
684         const struct lv_list *lvl;
685         int count = 0;
686
687         if (!activation())
688                 return 0;
689
690         dm_list_iterate_items(lvl, &vg->lvs) {
691                 if (lv_is_visible(lvl->lv))
692                         count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
693         }
694
695         return count;
696 }
697
698 /*
699  * Determine whether an LV is active locally or in a cluster.
700  * Assumes vg lock held.
701  * Returns:
702  * 0 - not active locally or on any node in cluster
703  * 1 - active either locally or some node in the cluster
704  */
705 int lv_is_active(struct logical_volume *lv)
706 {
707         int ret;
708
709         if (_lv_active(lv->vg->cmd, lv))
710                 return 1;
711
712         if (!vg_is_clustered(lv->vg))
713                 return 0;
714
715         if ((ret = remote_lock_held(lv->lvid.s)) >= 0)
716                 return ret;
717
718         /*
719          * Old compatibility code if locking doesn't support lock query
720          * FIXME: check status to not deactivate already activate device
721          */
722         if (activate_lv_excl(lv->vg->cmd, lv)) {
723                 if (!deactivate_lv(lv->vg->cmd, lv))
724                         stack;
725                 return 0;
726         }
727
728         /*
729          * Exclusive local activation failed so assume it is active elsewhere.
730          */
731         return 1;
732 }
733
734 #ifdef DMEVENTD
735 static struct dm_event_handler *_create_dm_event_handler(struct cmd_context *cmd, const char *dmuuid, const char *dso,
736                                                          const int timeout, enum dm_event_mask mask)
737 {
738         struct dm_event_handler *dmevh;
739
740         if (!(dmevh = dm_event_handler_create()))
741                 return_NULL;
742
743         if (dm_event_handler_set_dmeventd_path(dmevh, find_config_tree_str(cmd, "dmeventd/executable", NULL)))
744                 goto_bad;
745
746         if (dm_event_handler_set_dso(dmevh, dso))
747                 goto_bad;
748
749         if (dm_event_handler_set_uuid(dmevh, dmuuid))
750                 goto_bad;
751
752         dm_event_handler_set_timeout(dmevh, timeout);
753         dm_event_handler_set_event_mask(dmevh, mask);
754
755         return dmevh;
756
757 bad:
758         dm_event_handler_destroy(dmevh);
759         return NULL;
760 }
761
762 char *get_monitor_dso_path(struct cmd_context *cmd, const char *libpath)
763 {
764         char *path;
765
766         if (!(path = dm_pool_alloc(cmd->mem, PATH_MAX))) {
767                 log_error("Failed to allocate dmeventd library path.");
768                 return NULL;
769         }
770
771         get_shared_library_path(cmd, libpath, path, PATH_MAX);
772
773         return path;
774 }
775
776 int target_registered_with_dmeventd(struct cmd_context *cmd, const char *dso,
777                                     struct logical_volume *lv, int *pending)
778 {
779         char *uuid;
780         enum dm_event_mask evmask = 0;
781         struct dm_event_handler *dmevh;
782
783         *pending = 0;
784
785         if (!dso)
786                 return_0;
787
788         /* We always monitor the "real" device, never the "snapshot-origin" itself. */
789         if (!(uuid = build_dm_uuid(cmd->mem, lv->lvid.s, lv_is_origin(lv) ? "real" : NULL)))
790                 return_0;
791
792         if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, 0, DM_EVENT_ALL_ERRORS)))
793                 return_0;
794
795         if (dm_event_get_registered_device(dmevh, 0)) {
796                 dm_event_handler_destroy(dmevh);
797                 return 0;
798         }
799
800         evmask = dm_event_handler_get_event_mask(dmevh);
801         if (evmask & DM_EVENT_REGISTRATION_PENDING) {
802                 *pending = 1;
803                 evmask &= ~DM_EVENT_REGISTRATION_PENDING;
804         }
805
806         dm_event_handler_destroy(dmevh);
807
808         return evmask;
809 }
810
811 int target_register_events(struct cmd_context *cmd, const char *dso, struct logical_volume *lv,
812                             int evmask __attribute__((unused)), int set, int timeout)
813 {
814         char *uuid;
815         struct dm_event_handler *dmevh;
816         int r;
817
818         if (!dso)
819                 return_0;
820
821         /* We always monitor the "real" device, never the "snapshot-origin" itself. */
822         if (!(uuid = build_dm_uuid(cmd->mem, lv->lvid.s, lv_is_origin(lv) ? "real" : NULL)))
823                 return_0;
824
825         if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, timeout,
826                                                DM_EVENT_ALL_ERRORS | (timeout ? DM_EVENT_TIMEOUT : 0))))
827                 return_0;
828
829         r = set ? dm_event_register_handler(dmevh) : dm_event_unregister_handler(dmevh);
830
831         dm_event_handler_destroy(dmevh);
832
833         if (!r)
834                 return_0;
835
836         log_info("%s %s for events", set ? "Monitored" : "Unmonitored", uuid);
837
838         return 1;
839 }
840
841 #endif
842
843 /*
844  * Returns 0 if an attempt to (un)monitor the device failed.
845  * Returns 1 otherwise.
846  */
847 int monitor_dev_for_events(struct cmd_context *cmd, struct logical_volume *lv,
848                            unsigned origin_only, int monitor)
849 {
850 #ifdef DMEVENTD
851         int i, pending = 0, monitored;
852         int r = 1;
853         struct dm_list *tmp, *snh, *snht;
854         struct lv_segment *seg;
855         struct lv_segment *log_seg;
856         int (*monitor_fn) (struct lv_segment *s, int e);
857         uint32_t s;
858
859         /* skip dmeventd code altogether */
860         if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
861                 return 1;
862
863         /*
864          * Nothing to do if dmeventd configured not to be used.
865          */
866         if (monitor && !dmeventd_monitor_mode())
867                 return 1;
868
869         /*
870          * In case of a snapshot device, we monitor lv->snapshot->lv,
871          * not the actual LV itself.
872          */
873         if (lv_is_cow(lv) && !lv_is_merging_cow(lv))
874                 return monitor_dev_for_events(cmd, lv->snapshot->lv, 0, monitor);
875
876         /*
877          * In case this LV is a snapshot origin, we instead monitor
878          * each of its respective snapshots.  The origin itself may
879          * also need to be monitored if it is a mirror, for example.
880          */
881         if (!origin_only && lv_is_origin(lv))
882                 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
883                         if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
884                                     struct lv_segment, origin_list)->cow, 0, monitor))
885                                 r = 0;
886
887         /*
888          * If the volume is mirrored and its log is also mirrored, monitor
889          * the log volume as well.
890          */
891         if ((seg = first_seg(lv)) != NULL && seg->log_lv != NULL &&
892             (log_seg = first_seg(seg->log_lv)) != NULL &&
893             seg_is_mirrored(log_seg))
894                 if (!monitor_dev_for_events(cmd, seg->log_lv, 0, monitor))
895                         r = 0;
896
897         dm_list_iterate(tmp, &lv->segments) {
898                 seg = dm_list_item(tmp, struct lv_segment);
899
900                 /* Recurse for AREA_LV */
901                 for (s = 0; s < seg->area_count; s++) {
902                         if (seg_type(seg, s) != AREA_LV)
903                                 continue;
904                         if (!monitor_dev_for_events(cmd, seg_lv(seg, s), 0,
905                                                     monitor)) {
906                                 log_error("Failed to %smonitor %s",
907                                           monitor ? "" : "un",
908                                           seg_lv(seg, s)->name);
909                                 r = 0;
910                         }
911                 }
912
913                 if (!seg_monitored(seg) || (seg->status & PVMOVE))
914                         continue;
915
916                 monitor_fn = NULL;
917
918                 /* Check monitoring status */
919                 if (seg->segtype->ops->target_monitored)
920                         monitored = seg->segtype->ops->target_monitored(seg, &pending);
921                 else
922                         continue;  /* segtype doesn't support registration */
923
924                 /*
925                  * FIXME: We should really try again if pending
926                  */
927                 monitored = (pending) ? 0 : monitored;
928
929                 if (monitor) {
930                         if (monitored)
931                                 log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
932                         else if (seg->segtype->ops->target_monitor_events)
933                                 monitor_fn = seg->segtype->ops->target_monitor_events;
934                 } else {
935                         if (!monitored)
936                                 log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
937                         else if (seg->segtype->ops->target_unmonitor_events)
938                                 monitor_fn = seg->segtype->ops->target_unmonitor_events;
939                 }
940
941                 /* Do [un]monitor */
942                 if (!monitor_fn)
943                         continue;
944
945                 log_verbose("%sonitoring %s/%s%s", monitor ? "M" : "Not m", lv->vg->name, lv->name,
946                             test_mode() ? " [Test mode: skipping this]" : "");
947
948                 /* FIXME Test mode should really continue a bit further. */
949                 if (test_mode())
950                         continue;
951
952                 /* FIXME specify events */
953                 if (!monitor_fn(seg, 0)) {
954                         log_error("%s/%s: %s segment monitoring function failed.",
955                                   lv->vg->name, lv->name, seg->segtype->name);
956                         return 0;
957                 }
958
959                 /* Check [un]monitor results */
960                 /* Try a couple times if pending, but not forever... */
961                 for (i = 0; i < 10; i++) {
962                         pending = 0;
963                         monitored = seg->segtype->ops->target_monitored(seg, &pending);
964                         if (pending ||
965                             (!monitored && monitor) ||
966                             (monitored && !monitor))
967                                 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
968                                                  lv->vg->name, lv->name, monitor ? "" : "un");
969                         else
970                                 break;
971                         sleep(1);
972                 }
973
974                 if (r)
975                         r = (monitored && monitor) || (!monitored && !monitor);
976         }
977
978         return r;
979 #else
980         return 1;
981 #endif
982 }
983
984 static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
985                        unsigned origin_only, int error_if_not_suspended)
986 {
987         struct logical_volume *lv = NULL, *lv_pre = NULL;
988         struct lvinfo info;
989         int r = 0, lockfs = 0, flush_required = 0;
990
991         if (!activation())
992                 return 1;
993
994         if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
995                 goto_out;
996
997         /* Use precommitted metadata if present */
998         if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
999                 goto_out;
1000
1001         /* Ignore origin_only unless LV is origin in both old and new metadata */
1002         if (!lv_is_origin(lv) || !lv_is_origin(lv_pre))
1003                 origin_only = 0;
1004
1005         if (test_mode()) {
1006                 _skip("Suspending %s%s.", lv->name, origin_only ? " origin without snapshots" : "");
1007                 r = 1;
1008                 goto out;
1009         }
1010
1011         if (!lv_info(cmd, lv, origin_only, &info, 0, 0))
1012                 goto_out;
1013
1014         if (!info.exists || info.suspended) {
1015                 if (!error_if_not_suspended) {
1016                         r = 1;
1017                         if (info.suspended)
1018                                 memlock_inc(cmd);
1019                 }
1020                 goto out;
1021         }
1022
1023         if (!lv_read_replicator_vgs(lv))
1024                 goto_out;
1025
1026         lv_calculate_readahead(lv, NULL);
1027
1028         /* If VG was precommitted, preload devices for the LV */
1029         if ((lv_pre->vg->status & PRECOMMITTED)) {
1030                 if (!_lv_preload(lv_pre, origin_only, &flush_required)) {
1031                         /* FIXME Revert preloading */
1032                         goto_out;
1033                 }
1034         }
1035
1036         if (!monitor_dev_for_events(cmd, lv, origin_only, 0))
1037                 /* FIXME Consider aborting here */
1038                 stack;
1039
1040         memlock_inc(cmd);
1041
1042         if (!origin_only &&
1043             (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)))
1044                 lockfs = 1;
1045
1046         if (!_lv_suspend_lv(lv, origin_only, lockfs, flush_required)) {
1047                 memlock_dec(cmd);
1048                 fs_unlock();
1049                 goto out;
1050         }
1051
1052         r = 1;
1053 out:
1054         if (lv_pre)
1055                 free_vg(lv_pre->vg);
1056         if (lv) {
1057                 lv_release_replicator_vgs(lv);
1058                 free_vg(lv->vg);
1059         }
1060
1061         return r;
1062 }
1063
1064 /* Returns success if the device is not active */
1065 int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
1066 {
1067         return _lv_suspend(cmd, lvid_s, origin_only, 0);
1068 }
1069
1070 /* No longer used */
1071 /***********
1072 int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1073 {
1074         return _lv_suspend(cmd, lvid_s, 1);
1075 }
1076 ***********/
1077
1078 static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
1079                       unsigned origin_only,
1080                       int error_if_not_active)
1081 {
1082         struct logical_volume *lv;
1083         struct lvinfo info;
1084         int r = 0;
1085
1086         if (!activation())
1087                 return 1;
1088
1089         if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1090                 goto_out;
1091
1092         if (!lv_is_origin(lv))
1093                 origin_only = 0;
1094
1095         if (test_mode()) {
1096                 _skip("Resuming %s%s.", lv->name, origin_only ? " without snapshots" : "");
1097                 r = 1;
1098                 goto out;
1099         }
1100
1101         if (!lv_info(cmd, lv, origin_only, &info, 0, 0))
1102                 goto_out;
1103
1104         if (!info.exists || !info.suspended) {
1105                 if (error_if_not_active)
1106                         goto_out;
1107                 r = 1;
1108                 goto out;
1109         }
1110
1111         if (!_lv_activate_lv(lv, origin_only))
1112                 goto_out;
1113
1114         memlock_dec(cmd);
1115         fs_unlock();
1116
1117         if (!monitor_dev_for_events(cmd, lv, origin_only, 1))
1118                 stack;
1119
1120         r = 1;
1121 out:
1122         if (lv)
1123                 free_vg(lv->vg);
1124
1125         return r;
1126 }
1127
1128 /* Returns success if the device is not active */
1129 int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
1130 {
1131         return _lv_resume(cmd, lvid_s, origin_only, 0);
1132 }
1133
1134 int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
1135 {
1136         return _lv_resume(cmd, lvid_s, origin_only, 1);
1137 }
1138
1139 static int _lv_has_open_snapshots(struct logical_volume *lv)
1140 {
1141         struct lv_segment *snap_seg;
1142         struct lvinfo info;
1143         int r = 0;
1144
1145         dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
1146                 if (!lv_info(lv->vg->cmd, snap_seg->cow, 0, &info, 1, 0)) {
1147                         r = 1;
1148                         continue;
1149                 }
1150
1151                 if (info.exists && info.open_count) {
1152                         log_error("LV %s/%s has open snapshot %s: "
1153                                   "not deactivating", lv->vg->name, lv->name,
1154                                   snap_seg->cow->name);
1155                         r = 1;
1156                 }
1157         }
1158
1159         return r;
1160 }
1161
1162 int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
1163 {
1164         struct logical_volume *lv;
1165         struct lvinfo info;
1166         int r = 0;
1167
1168         if (!activation())
1169                 return 1;
1170
1171         if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1172                 goto out;
1173
1174         if (test_mode()) {
1175                 _skip("Deactivating '%s'.", lv->name);
1176                 r = 1;
1177                 goto out;
1178         }
1179
1180         if (!lv_info(cmd, lv, 0, &info, 1, 0))
1181                 goto_out;
1182
1183         if (!info.exists) {
1184                 r = 1;
1185                 goto out;
1186         }
1187
1188         if (lv_is_visible(lv)) {
1189                 if (info.open_count) {
1190                         log_error("LV %s/%s in use: not deactivating",
1191                                   lv->vg->name, lv->name);
1192                         goto out;
1193                 }
1194                 if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1195                         goto_out;
1196         }
1197
1198         if (!lv_read_replicator_vgs(lv))
1199                 goto_out;
1200
1201         lv_calculate_readahead(lv, NULL);
1202
1203         if (!monitor_dev_for_events(cmd, lv, 0, 0))
1204                 stack;
1205
1206         memlock_inc(cmd);
1207         r = _lv_deactivate(lv);
1208         memlock_dec(cmd);
1209         fs_unlock();
1210
1211         if (!lv_info(cmd, lv, 0, &info, 1, 0) || info.exists)
1212                 r = 0;
1213 out:
1214         if (lv) {
1215                 lv_release_replicator_vgs(lv);
1216                 free_vg(lv->vg);
1217         }
1218
1219         return r;
1220 }
1221
1222 /* Test if LV passes filter */
1223 int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
1224                          int *activate_lv)
1225 {
1226         struct logical_volume *lv;
1227         int r = 0;
1228
1229         if (!activation()) {
1230                 *activate_lv = 1;
1231                 return 1;
1232         }
1233
1234         if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1235                 goto out;
1236
1237         if (!_passes_activation_filter(cmd, lv)) {
1238                 log_verbose("Not activating %s/%s since it does not pass "
1239                             "activation filter.", lv->vg->name, lv->name);
1240                 *activate_lv = 0;
1241         } else
1242                 *activate_lv = 1;
1243         r = 1;
1244 out:
1245         if (lv)
1246                 free_vg(lv->vg);
1247
1248         return r;
1249 }
1250
1251 static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
1252                         int exclusive, int filter)
1253 {
1254         struct logical_volume *lv;
1255         struct lvinfo info;
1256         int r = 0;
1257
1258         if (!activation())
1259                 return 1;
1260
1261         if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1262                 goto out;
1263
1264         if (filter && !_passes_activation_filter(cmd, lv)) {
1265                 log_error("Not activating %s/%s since it does not pass "
1266                           "activation filter.", lv->vg->name, lv->name);
1267                 goto out;
1268         }
1269
1270         if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
1271                 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1272                           lv->name);
1273                 goto_out;
1274         }
1275
1276         if (lv_has_unknown_segments(lv)) {
1277                 log_error("Refusing activation of LV %s containing "
1278                           "an unrecognised segment.", lv->name);
1279                 goto_out;
1280         }
1281
1282         if (test_mode()) {
1283                 _skip("Activating '%s'.", lv->name);
1284                 r = 1;
1285                 goto out;
1286         }
1287
1288         if (!lv_info(cmd, lv, 0, &info, 0, 0))
1289                 goto_out;
1290
1291         if (info.exists && !info.suspended && info.live_table) {
1292                 r = 1;
1293                 goto out;
1294         }
1295
1296         if (!lv_read_replicator_vgs(lv))
1297                 goto_out;
1298
1299         lv_calculate_readahead(lv, NULL);
1300
1301         if (exclusive)
1302                 lv->status |= ACTIVATE_EXCL;
1303
1304         memlock_inc(cmd);
1305         if (!(r = _lv_activate_lv(lv, 0)))
1306                 stack;
1307         memlock_dec(cmd);
1308         fs_unlock();
1309
1310         if (r && !monitor_dev_for_events(cmd, lv, 0, 1))
1311                 stack;
1312
1313 out:
1314         if (lv) {
1315                 lv_release_replicator_vgs(lv);
1316                 free_vg(lv->vg);
1317         }
1318
1319         return r;
1320 }
1321
1322 /* Activate LV */
1323 int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1324 {
1325         if (!_lv_activate(cmd, lvid_s, exclusive, 0))
1326                 return_0;
1327
1328         return 1;
1329 }
1330
1331 /* Activate LV only if it passes filter */
1332 int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
1333 {
1334         if (!_lv_activate(cmd, lvid_s, exclusive, 1))
1335                 return_0;
1336
1337         return 1;
1338 }
1339
1340 int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1341 {
1342         int r = 1;
1343
1344         if (!lv) {
1345                 r = dm_mknodes(NULL);
1346                 fs_unlock();
1347                 return r;
1348         }
1349
1350         if (!activation())
1351                 return 1;
1352
1353         r = dev_manager_mknodes(lv);
1354
1355         fs_unlock();
1356
1357         return r;
1358 }
1359
1360 /*
1361  * Does PV use VG somewhere in its construction?
1362  * Returns 1 on failure.
1363  */
1364 int pv_uses_vg(struct physical_volume *pv,
1365                struct volume_group *vg)
1366 {
1367         if (!activation())
1368                 return 0;
1369
1370         if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1371                 return 0;
1372
1373         return dev_manager_device_uses_vg(pv->dev, vg);
1374 }
1375
1376 void activation_release(void)
1377 {
1378         dev_manager_release();
1379 }
1380
1381 void activation_exit(void)
1382 {
1383         dev_manager_exit();
1384 }
1385 #endif