]> sourceware.org Git - lvm2.git/blame - lib/activate/activate.c
Add activation/read_only_volume_list to override LV permission in metadata.
[lvm2.git] / lib / activate / activate.c
CommitLineData
b1713d28 1/*
67cdbd7e 2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
a18dcfb5 3 * Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
b1713d28 4 *
6606c3ae
AK
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
be684599 9 * of the GNU Lesser General Public License v.2.1.
6606c3ae 10 *
be684599 11 * You should have received a copy of the GNU Lesser General Public License
6606c3ae
AK
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
b1713d28
JT
14 */
15
d1d9800e 16#include "lib.h"
a381c45a 17#include "metadata.h"
b1713d28 18#include "activate.h"
914c9723 19#include "memlock.h"
78125be9 20#include "display.h"
f7a14956 21#include "fs.h"
f894b4b1 22#include "lvm-exec.h"
7d1552c9 23#include "lvm-file.h"
41b2fd5f 24#include "lvm-string.h"
413cc918 25#include "toolcontext.h"
de6c9183 26#include "dev_manager.h"
de17d760 27#include "str_list.h"
4922197a 28#include "config.h"
352a99b9 29#include "filter.h"
15d91f5a 30#include "segtype.h"
d1e8046f 31#include "sharedlib.h"
12137231
JT
32
33#include <limits.h>
5986ec94 34#include <fcntl.h>
914c9723 35#include <unistd.h>
12137231 36
6d52fb46 37#define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args)
b1713d28 38
7d1552c9
AK
39int lvm1_present(struct cmd_context *cmd)
40{
900f5f81 41 static char path[PATH_MAX];
7d1552c9 42
0550c1b6 43 if (dm_snprintf(path, sizeof(path), "%s/lvm/global", cmd->proc_dir)
7d1552c9
AK
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
6c81ed26 55int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg,
2c44337b 56 struct dm_list *modules)
6c81ed26
AK
57{
58 unsigned int s;
59 struct lv_segment *seg2, *snap_seg;
2c44337b 60 struct dm_list *snh;
6c81ed26
AK
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))
2c44337b 69 dm_list_iterate(snh, &seg->lv->snapshot_segs)
6c81ed26 70 if (!list_lv_modules(mem,
2c44337b 71 dm_list_struct_base(snh,
6c81ed26
AK
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
103int list_lv_modules(struct dm_pool *mem, const struct logical_volume *lv,
2c44337b 104 struct dm_list *modules)
6c81ed26
AK
105{
106 struct lv_segment *seg;
107
2c44337b 108 dm_list_iterate_items(seg, &lv->segments)
6c81ed26
AK
109 if (!list_segment_modules(mem, seg, modules))
110 return_0;
111
112 return 1;
113}
114
199e490e
AK
115#ifndef DEVMAPPER_SUPPORT
116void set_activation(int act)
117{
f2046e0a
AK
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;
199e490e
AK
127}
128int activation(void)
129{
130 return 0;
131}
132int library_version(char *version, size_t size)
133{
134 return 0;
135}
136int driver_version(char *version, size_t size)
137{
138 return 0;
139}
bbf83db1
AK
140int target_version(const char *target_name, uint32_t *maj,
141 uint32_t *min, uint32_t *patchlevel)
142{
143 return 0;
144}
ed82bfd2
AK
145int target_present(struct cmd_context *cmd, const char *target_name,
146 int use_modprobe)
d1f4953a
AK
147{
148 return 0;
149}
d1b36fbe 150int lvm_dm_prefix_check(const char *sysfs_dir, int major, int minor, const char *prefix)
07113bee
MB
151{
152 return 0;
153}
2d6fcbf6
AK
154int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, unsigned origin_only,
155 struct lvinfo *info, int with_open_count, int with_read_ahead)
199e490e
AK
156{
157 return 0;
158}
4bd9480d 159int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
2d6fcbf6 160 unsigned origin_only,
a6b22cf3 161 struct lvinfo *info, int with_open_count, int with_read_ahead)
4bd9480d
AK
162{
163 return 0;
164}
8191fe4f 165int lv_snapshot_percent(const struct logical_volume *lv, percent_t *percent)
199e490e
AK
166{
167 return 0;
168}
aec21154 169int lv_mirror_percent(struct cmd_context *cmd, const struct logical_volume *lv,
8191fe4f 170 int wait, percent_t *percent, uint32_t *event_nr)
10b29b8d
AK
171{
172 return 0;
173}
199e490e
AK
174int lvs_in_vg_activated(struct volume_group *vg)
175{
176 return 0;
177}
ab8b85fb 178int lvs_in_vg_opened(const struct volume_group *vg)
199e490e
AK
179{
180 return 0;
181}
2d6fcbf6 182/******
658b5812
AK
183int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
184{
185 return 1;
186}
2d6fcbf6 187*******/
ab8b85fb 188int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
199e490e
AK
189{
190 return 1;
191}
ab8b85fb 192int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
658b5812
AK
193{
194 return 1;
195}
ab8b85fb 196int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
10d0d9c7 197 unsigned origin_only, unsigned exclusive, unsigned revert)
199e490e
AK
198{
199 return 1;
200}
201int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
202{
203 return 1;
204}
658b5812
AK
205int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
206 int *activate_lv)
207{
208 return 1;
209}
07d31831 210int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
199e490e
AK
211{
212 return 1;
213}
07d31831 214int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
658b5812
AK
215{
216 return 1;
217}
f7dd6d84
AK
218int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
219{
220 return 1;
221}
a00cb3a6
ZK
222int lv_send_message(const struct logical_volume *lv, const char *message)
223{
224 return 0;
225}
0ce83a83 226int pv_uses_vg(struct physical_volume *pv,
3e3d5d85 227 struct volume_group *vg)
352a99b9
AK
228{
229 return 0;
230}
2293567c
AK
231void activation_release(void)
232{
2293567c 233}
914c9723
AK
234void activation_exit(void)
235{
914c9723 236}
b19f0121 237
ab8b85fb
ZK
238int lv_is_active(struct logical_volume *lv)
239{
240 return 0;
241}
b19f0121
JEB
242int lv_is_active_but_not_locally(struct logical_volume *lv)
243{
244 return 0;
245}
246int lv_is_active_exclusive(struct logical_volume *lv)
247{
248 return 0;
249}
ab8b85fb
ZK
250int lv_is_active_exclusive_locally(struct logical_volume *lv)
251{
252 return 0;
253}
254int lv_is_active_exclusive_remotely(struct logical_volume *lv)
255{
256 return 0;
257}
b19f0121 258
ab8b85fb
ZK
259int lv_check_transient(struct logical_volume *lv)
260{
261 return 1;
262}
263int monitor_dev_for_events(struct cmd_context *cmd, struct logical_volume *lv,
81beded3 264 struct lv_activate_opts *laopts, int monitor)
ab8b85fb
ZK
265{
266 return 1;
267}
199e490e
AK
268#else /* DEVMAPPER_SUPPORT */
269
d1d9800e
AK
270static int _activation = 1;
271
8ef2b021 272void set_activation(int act)
d1d9800e 273{
8ef2b021 274 if (act == _activation)
d1d9800e
AK
275 return;
276
8ef2b021 277 _activation = act;
d1d9800e
AK
278 if (_activation)
279 log_verbose("Activation enabled. Device-mapper kernel "
280 "driver will be used.");
281 else
e7ddf416 282 log_warn("WARNING: Activation disabled. No device-mapper "
bfe2b548 283 "interaction will be attempted.");
d1d9800e
AK
284}
285
8ef2b021 286int activation(void)
d1d9800e
AK
287{
288 return _activation;
289}
290
a18dcfb5
AK
291static int _passes_volumes_filter(struct cmd_context *cmd,
292 struct logical_volume *lv,
293 const struct dm_config_node *cn,
294 const char *config_path)
de17d760 295{
e59e2f7c 296 const struct dm_config_value *cv;
760d1fac 297 const char *str;
900f5f81 298 static char path[PATH_MAX];
de17d760 299
a18dcfb5
AK
300 log_verbose("%s configuration setting defined: "
301 "Checking the list to match %s/%s",
302 config_path, lv->vg->name, lv->name);
fefa4323 303
f7e3a19f 304 for (cv = cn->v; cv; cv = cv->next) {
e59e2f7c 305 if (cv->type != DM_CFG_STRING) {
a18dcfb5
AK
306 log_error("Ignoring invalid string in config file %s",
307 config_path);
de17d760
AK
308 continue;
309 }
310 str = cv->v.str;
311 if (!*str) {
a18dcfb5
AK
312 log_error("Ignoring empty string in config file %s",
313 config_path);
de17d760
AK
314 continue;
315 }
316
f7e3a19f 317
de17d760
AK
318 /* Tag? */
319 if (*str == '@') {
320 str++;
321 if (!*str) {
322 log_error("Ignoring empty tag in config file "
a18dcfb5 323 "%s", config_path);
de17d760
AK
324 continue;
325 }
326 /* If any host tag matches any LV or VG tag, activate */
327 if (!strcmp(str, "*")) {
eb82bd05 328 if (str_list_match_list(&cmd->tags, &lv->tags, NULL)
de17d760 329 || str_list_match_list(&cmd->tags,
eb82bd05 330 &lv->vg->tags, NULL))
de17d760
AK
331 return 1;
332 else
333 continue;
334 }
335 /* If supplied tag matches LV or VG tag, activate */
336 if (str_list_match_item(&lv->tags, str) ||
337 str_list_match_item(&lv->vg->tags, str))
338 return 1;
339 else
340 continue;
341 }
13835b5f 342 if (!strchr(str, '/')) {
de17d760
AK
343 /* vgname supplied */
344 if (!strcmp(str, lv->vg->name))
345 return 1;
346 else
347 continue;
348 }
349 /* vgname/lvname */
0550c1b6 350 if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name,
de17d760 351 lv->name) < 0) {
0550c1b6 352 log_error("dm_snprintf error from %s/%s", lv->vg->name,
de17d760
AK
353 lv->name);
354 continue;
355 }
356 if (!strcmp(path, str))
357 return 1;
358 }
359
a18dcfb5
AK
360 log_verbose("No item supplied in %s configuration setting "
361 "matches %s/%s", config_path, lv->vg->name, lv->name);
fefa4323 362
de17d760
AK
363 return 0;
364}
365
a18dcfb5
AK
366static int _passes_activation_filter(struct cmd_context *cmd,
367 struct logical_volume *lv)
368{
369 const struct dm_config_node *cn;
370
371 if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
372 log_verbose("activation/volume_list configuration setting "
373 "not defined: Checking only host tags for %s/%s",
374 lv->vg->name, lv->name);
375
376 /* If no host tags defined, activate */
377 if (dm_list_empty(&cmd->tags))
378 return 1;
379
380 /* If any host tag matches any LV or VG tag, activate */
381 if (str_list_match_list(&cmd->tags, &lv->tags, NULL) ||
382 str_list_match_list(&cmd->tags, &lv->vg->tags, NULL))
383 return 1;
384
385 log_verbose("No host tag matches %s/%s",
386 lv->vg->name, lv->name);
387
388 /* Don't activate */
389 return 0;
390 }
391
392 return _passes_volumes_filter(cmd, lv, cn, "activation/volume_list");
393}
394
395static int _passes_readonly_filter(struct cmd_context *cmd,
396 struct logical_volume *lv)
397{
398 struct dm_config_node *cn;
399
400 if (!(cn = find_config_tree_node(cmd, "activation/read_only_volume_list")))
401 return 0;
402
403 return _passes_volumes_filter(cmd, lv, cn, "activation/read_only_volume_list");
404}
405
fae0c576
AK
406int library_version(char *version, size_t size)
407{
d1d9800e
AK
408 if (!activation())
409 return 0;
410
f894b4b1 411 return dm_get_library_version(version, size);
fae0c576
AK
412}
413
fae0c576
AK
414int driver_version(char *version, size_t size)
415{
d1d9800e
AK
416 if (!activation())
417 return 0;
418
fae0c576 419 log_very_verbose("Getting driver version");
fae0c576 420
f894b4b1 421 return dm_driver_version(version, size);
fae0c576
AK
422}
423
bbf83db1
AK
424int target_version(const char *target_name, uint32_t *maj,
425 uint32_t *min, uint32_t *patchlevel)
d1f4953a
AK
426{
427 int r = 0;
428 struct dm_task *dmt;
429 struct dm_versions *target, *last_target;
430
d1f4953a 431 log_very_verbose("Getting target version for %s", target_name);
5f4b2acf
AK
432 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
433 return_0;
d1f4953a 434
2243718f
AK
435 if (activation_checks() && !dm_task_enable_checks(dmt))
436 goto_out;
437
d1f4953a
AK
438 if (!dm_task_run(dmt)) {
439 log_debug("Failed to get %s target version", target_name);
440 /* Assume this was because LIST_VERSIONS isn't supported */
441 return 1;
442 }
443
444 target = dm_task_get_versions(dmt);
445
446 do {
447 last_target = target;
448
449 if (!strcmp(target_name, target->name)) {
450 r = 1;
bbf83db1
AK
451 *maj = target->version[0];
452 *min = target->version[1];
453 *patchlevel = target->version[2];
d1f4953a
AK
454 goto out;
455 }
456
d40d166f 457 target = (struct dm_versions *)((char *) target + target->next);
d1f4953a
AK
458 } while (last_target != target);
459
460 out:
461 dm_task_destroy(dmt);
462
463 return r;
464}
465
a3390bb5 466int lvm_dm_prefix_check(int major, int minor, const char *prefix)
07113bee
MB
467{
468 struct dm_task *dmt;
469 const char *uuid;
470 int r;
471
472 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
d1b36fbe 473 return_0;
07113bee
MB
474
475 if (!dm_task_set_minor(dmt, minor) ||
476 !dm_task_set_major(dmt, major) ||
477 !dm_task_run(dmt) ||
478 !(uuid = dm_task_get_uuid(dmt))) {
479 dm_task_destroy(dmt);
480 return 0;
481 }
482
483 r = strncasecmp(uuid, prefix, strlen(prefix));
484 dm_task_destroy(dmt);
485
d1b36fbe 486 return r ? 0 : 1;
07113bee
MB
487}
488
ed82bfd2 489int module_present(struct cmd_context *cmd, const char *target_name)
f894b4b1 490{
5619c629 491 int ret = 0;
03b49fe1 492#ifdef MODPROBE_CMD
f894b4b1 493 char module[128];
c8669f6b 494 const char *argv[3];
5619c629
MB
495
496 if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) {
497 log_error("module_present module name too long: %s",
498 target_name);
499 return 0;
500 }
501
c8669f6b
ZK
502 argv[0] = MODPROBE_CMD;
503 argv[1] = module;
504 argv[2] = NULL;
505
b1b38215 506 ret = exec_cmd(cmd, argv, NULL, 0);
03b49fe1 507#endif
5619c629
MB
508 return ret;
509}
510
ed82bfd2
AK
511int target_present(struct cmd_context *cmd, const char *target_name,
512 int use_modprobe)
5619c629
MB
513{
514 uint32_t maj, min, patchlevel;
f894b4b1
AK
515
516 if (!activation())
517 return 0;
518
519#ifdef MODPROBE_CMD
5f4b2acf 520 if (use_modprobe) {
bbf83db1 521 if (target_version(target_name, &maj, &min, &patchlevel))
5f4b2acf 522 return 1;
f894b4b1 523
ed82bfd2 524 if (!module_present(cmd, target_name))
5f4b2acf 525 return_0;
f894b4b1
AK
526 }
527#endif
528
bbf83db1 529 return target_version(target_name, &maj, &min, &patchlevel);
f894b4b1
AK
530}
531
de6c9183
JT
532/*
533 * Returns 1 if info structure populated, else 0 on failure.
534 */
2d6fcbf6 535int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, unsigned origin_only,
ab9663f3 536 struct lvinfo *info, int with_open_count, int with_read_ahead)
37ed70b9 537{
199e490e 538 struct dm_info dminfo;
4a624ca0 539
d1d9800e
AK
540 if (!activation())
541 return 0;
56cab8cc
ZK
542 /*
543 * If open_count info is requested and we have to be sure our own udev
544 * transactions are finished
545 * For non-clustered locking type we are only interested for non-delete operation
546 * in progress - as only those could lead to opened files
547 */
548 if (with_open_count) {
549 if (locking_is_clustered())
550 sync_local_dev_names(cmd); /* Wait to have udev in sync */
f5f6dcbc 551 else if (fs_has_non_delete_ops())
56cab8cc
ZK
552 fs_unlock(); /* For non clustered - wait if there are non-delete ops */
553 }
d1d9800e 554
2d6fcbf6 555 if (!dev_manager_info(lv->vg->cmd->mem, lv, origin_only ? "real" : NULL, with_open_count,
ab9663f3 556 with_read_ahead, &dminfo, &info->read_ahead))
5f4b2acf 557 return_0;
4a624ca0 558
199e490e
AK
559 info->exists = dminfo.exists;
560 info->suspended = dminfo.suspended;
561 info->open_count = dminfo.open_count;
562 info->major = dminfo.major;
563 info->minor = dminfo.minor;
564 info->read_only = dminfo.read_only;
5f4b2acf
AK
565 info->live_table = dminfo.live_table;
566 info->inactive_table = dminfo.inactive_table;
199e490e 567
f894b4b1 568 return 1;
de6c9183 569}
a62ee8ad 570
4bd9480d 571int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
2d6fcbf6 572 unsigned origin_only,
a6b22cf3 573 struct lvinfo *info, int with_open_count, int with_read_ahead)
4bd9480d 574{
0548bcc2 575 int r;
4bd9480d
AK
576 struct logical_volume *lv;
577
7a593325 578 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
4bd9480d
AK
579 return 0;
580
2d6fcbf6
AK
581 if (!lv_is_origin(lv))
582 origin_only = 0;
583
584 r = lv_info(cmd, lv, origin_only, info, with_open_count, with_read_ahead);
077a6755 585 release_vg(lv->vg);
0548bcc2
MB
586
587 return r;
4bd9480d
AK
588}
589
125712be
PR
590int lv_check_not_in_use(struct cmd_context *cmd __attribute__((unused)),
591 struct logical_volume *lv, struct lvinfo *info)
592{
593 if (!info->exists)
594 return 1;
595
596 /* If sysfs is not used, use open_count information only. */
c3e5b497
PR
597 if (!*dm_sysfs_dir()) {
598 if (info->open_count) {
599 log_error("Logical volume %s/%s in use.",
600 lv->vg->name, lv->name);
601 return 0;
602 }
603
604 return 1;
605 }
125712be
PR
606
607 if (dm_device_has_holders(info->major, info->minor)) {
608 log_error("Logical volume %s/%s is used by another device.",
609 lv->vg->name, lv->name);
610 return 0;
611 }
612
613 if (dm_device_has_mounted_fs(info->major, info->minor)) {
614 log_error("Logical volume %s/%s contains a filesystem in use.",
615 lv->vg->name, lv->name);
616 return 0;
617 }
618
619 return 1;
620}
621
d345bf2c
PR
622/*
623 * Returns 1 if percent set, else 0 on failure.
624 */
625int lv_check_transient(struct logical_volume *lv)
626{
627 int r;
628 struct dev_manager *dm;
629
630 if (!activation())
631 return 0;
632
7df72b3c
AK
633 log_debug("Checking transient status for LV %s/%s", lv->vg->name, lv->name);
634
df390f17 635 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
d345bf2c
PR
636 return_0;
637
638 if (!(r = dev_manager_transient(dm, lv)))
639 stack;
640
641 dev_manager_destroy(dm);
642
643 return r;
644}
645
1951dba9
AL
646/*
647 * Returns 1 if percent set, else 0 on failure.
648 */
8191fe4f 649int lv_snapshot_percent(const struct logical_volume *lv, percent_t *percent)
1951dba9
AL
650{
651 int r;
652 struct dev_manager *dm;
653
d1d9800e
AK
654 if (!activation())
655 return 0;
656
7df72b3c
AK
657 log_debug("Checking snapshot percent for LV %s/%s", lv->vg->name, lv->name);
658
df390f17 659 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
5f4b2acf 660 return_0;
1951dba9 661
8191fe4f 662 if (!(r = dev_manager_snapshot_percent(dm, lv, percent)))
1951dba9 663 stack;
c826c0d1 664
1951dba9
AL
665 dev_manager_destroy(dm);
666
667 return r;
668}
669
10b29b8d 670/* FIXME Merge with snapshot_percent */
aec21154 671int lv_mirror_percent(struct cmd_context *cmd, const struct logical_volume *lv,
8191fe4f 672 int wait, percent_t *percent, uint32_t *event_nr)
10b29b8d
AK
673{
674 int r;
675 struct dev_manager *dm;
b65b777d 676 struct lvinfo info;
10b29b8d 677
876003dc
AK
678 /* If mirrored LV is temporarily shrinked to 1 area (= linear),
679 * it should be considered in-sync. */
2c44337b 680 if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) {
8191fe4f 681 *percent = PERCENT_100;
876003dc
AK
682 return 1;
683 }
684
10b29b8d
AK
685 if (!activation())
686 return 0;
687
7df72b3c
AK
688 log_debug("Checking mirror percent for LV %s/%s", lv->vg->name, lv->name);
689
2d6fcbf6 690 if (!lv_info(cmd, lv, 0, &info, 0, 0))
5f4b2acf 691 return_0;
b65b777d
AK
692
693 if (!info.exists)
694 return 0;
695
df390f17 696 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
5f4b2acf 697 return_0;
10b29b8d 698
8191fe4f 699 if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, event_nr)))
10b29b8d
AK
700 stack;
701
702 dev_manager_destroy(dm);
703
704 return r;
705}
706
4aebd52c
JEB
707int lv_raid_percent(const struct logical_volume *lv, percent_t *percent)
708{
709 return lv_mirror_percent(lv->vg->cmd, lv, 0, percent, NULL);
710}
711
34507894
ZK
712/*
713 * Returns 1 if percent set, else 0 on failure.
714 */
715int lv_thin_pool_percent(const struct logical_volume *lv, percent_t *percent)
716{
717 int r;
718 struct dev_manager *dm;
719
720 if (!activation())
721 return 0;
722
723 log_debug("Checking thin pool percent for LV %s/%s", lv->vg->name, lv->name);
724
725 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
726 return_0;
727
728 if (!(r = dev_manager_thin_pool_percent(dm, lv, percent)))
729 stack;
730
731 dev_manager_destroy(dm);
732
733 return r;
734}
735
ab9663f3 736static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv)
2ba80b43 737{
199e490e 738 struct lvinfo info;
2ba80b43 739
2d6fcbf6 740 if (!lv_info(cmd, lv, 0, &info, 0, 0)) {
2ba80b43 741 stack;
de6c9183 742 return -1;
2ba80b43
JT
743 }
744
de6c9183 745 return info.exists;
2ba80b43
JT
746}
747
f894b4b1 748static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
5986ec94 749{
199e490e 750 struct lvinfo info;
5986ec94 751
2d6fcbf6 752 if (!lv_info(cmd, lv, 0, &info, 1, 0)) {
5986ec94 753 stack;
de6c9183 754 return -1;
5986ec94
JT
755 }
756
de6c9183 757 return info.open_count;
5986ec94
JT
758}
759
81beded3 760static int _lv_activate_lv(struct logical_volume *lv, struct lv_activate_opts *laopts)
b1713d28 761{
6d52fb46 762 int r;
de6c9183 763 struct dev_manager *dm;
b1713d28 764
df390f17 765 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
5f4b2acf 766 return_0;
ae2bb665 767
81beded3 768 if (!(r = dev_manager_activate(dm, lv, laopts)))
6d52fb46 769 stack;
ae2bb665 770
de6c9183 771 dev_manager_destroy(dm);
ae2bb665 772 return r;
b1713d28 773}
a381c45a 774
81beded3
ZK
775static int _lv_preload(struct logical_volume *lv, struct lv_activate_opts *laopts,
776 int *flush_required)
0a5e4a14 777{
a18dcfb5 778 int r = 0;
de6c9183 779 struct dev_manager *dm;
a18dcfb5
AK
780 int old_readonly = laopts->read_only;
781
782 laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);
37ed70b9 783
df390f17 784 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
a18dcfb5 785 goto_out;
5f4b2acf 786
81beded3 787 if (!(r = dev_manager_preload(dm, lv, laopts, flush_required)))
6d52fb46 788 stack;
5f4b2acf
AK
789
790 dev_manager_destroy(dm);
a18dcfb5
AK
791
792 laopts->read_only = old_readonly;
793out:
5f4b2acf
AK
794 return r;
795}
796
797static int _lv_deactivate(struct logical_volume *lv)
798{
799 int r;
800 struct dev_manager *dm;
801
df390f17 802 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
5f4b2acf 803 return_0;
37ed70b9 804
de6c9183 805 if (!(r = dev_manager_deactivate(dm, lv)))
37ed70b9 806 stack;
37ed70b9 807
de6c9183
JT
808 dev_manager_destroy(dm);
809 return r;
37ed70b9
JT
810}
811
81beded3
ZK
812static int _lv_suspend_lv(struct logical_volume *lv, struct lv_activate_opts *laopts,
813 int lockfs, int flush_required)
4a624ca0 814{
20c5fcf7
AK
815 int r;
816 struct dev_manager *dm;
c2d72fd4 817
a18dcfb5
AK
818 laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);
819
df390f17
AK
820 /*
821 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
822 * device directly, we don't want to touch the devices that use it.
823 */
824 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
5f4b2acf 825 return_0;
0a5e4a14 826
81beded3 827 if (!(r = dev_manager_suspend(dm, lv, laopts, lockfs, flush_required)))
37ed70b9 828 stack;
0a5e4a14 829
20c5fcf7
AK
830 dev_manager_destroy(dm);
831 return r;
6d52fb46 832}
4a624ca0 833
8c013da4 834/*
f75c11ed 835 * These two functions return the number of visible LVs in the state,
7df72b3c 836 * or -1 on error. FIXME Check this.
8c013da4 837 */
65752052 838int lvs_in_vg_activated(struct volume_group *vg)
f047219b 839{
60f13f01 840 struct lv_list *lvl;
94b8220f 841 int count = 0;
37ed70b9 842
d1d9800e
AK
843 if (!activation())
844 return 0;
845
7df72b3c 846 dm_list_iterate_items(lvl, &vg->lvs)
59d8429c 847 if (lv_is_visible(lvl->lv))
ab9663f3 848 count += (_lv_active(vg->cmd, lvl->lv) == 1);
7df72b3c
AK
849
850 log_debug("Counted %d active LVs in VG %s", count, vg->name);
37ed70b9
JT
851
852 return count;
f047219b 853}
2ba80b43 854
08c9ff43 855int lvs_in_vg_opened(const struct volume_group *vg)
2ba80b43 856{
08c9ff43 857 const struct lv_list *lvl;
94b8220f 858 int count = 0;
2ba80b43 859
d1d9800e
AK
860 if (!activation())
861 return 0;
862
7df72b3c 863 dm_list_iterate_items(lvl, &vg->lvs)
87371d48 864 if (lv_is_visible(lvl->lv))
f894b4b1 865 count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
7df72b3c
AK
866
867 log_debug("Counted %d open LVs in VG %s", count, vg->name);
2ba80b43
JT
868
869 return count;
870}
413cc918 871
5ca6698f 872/*
27ff8813
JEB
873 * _lv_is_active
874 * @lv: logical volume being queried
875 * @locally: set if active locally (when provided)
876 * @exclusive: set if active exclusively (when provided)
877 *
5ca6698f 878 * Determine whether an LV is active locally or in a cluster.
27ff8813
JEB
879 * In addition to the return code which indicates whether or
880 * not the LV is active somewhere, two other values are set
881 * to yield more information about the status of the activation:
882 * return locally exclusively status
883 * ====== ======= =========== ======
884 * 0 0 0 not active
885 * 1 0 0 active remotely
886 * 1 0 1 exclusive remotely
887 * 1 1 0 active locally and possibly remotely
888 * 1 1 1 exclusive locally (or local && !cluster)
889 * The VG lock must be held to call this function.
890 *
891 * Returns: 0 or 1
5ca6698f 892 */
27ff8813
JEB
893static int _lv_is_active(struct logical_volume *lv,
894 int *locally, int *exclusive)
5ca6698f 895{
27ff8813
JEB
896 int r, l, e; /* remote, local, and exclusive */
897
898 r = l = e = 0;
6ac30c94 899
ab9663f3 900 if (_lv_active(lv->vg->cmd, lv))
27ff8813 901 l = 1;
5ca6698f 902
27ff8813
JEB
903 if (!vg_is_clustered(lv->vg)) {
904 e = 1; /* exclusive by definition */
905 goto out;
906 }
907
908 /* Active locally, and the caller doesn't care about exclusive */
909 if (l && !exclusive)
910 goto out;
5ca6698f 911
27ff8813
JEB
912 if ((r = remote_lock_held(lv->lvid.s, &e)) >= 0)
913 goto out;
6ac30c94
MB
914
915 /*
27ff8813
JEB
916 * If lock query is not supported (due to interfacing with old
917 * code), then we cannot evaluate exclusivity properly.
918 *
919 * Old users of this function will never be affected by this,
920 * since they are only concerned about active vs. not active.
921 * New users of this function who specifically ask for 'exclusive'
922 * will be given an error message.
6ac30c94 923 */
27ff8813
JEB
924 if (l) {
925 if (exclusive)
926 log_error("Unable to determine exclusivity of %s",
927 lv->name);
928 goto out;
929 }
930
f3d8974d 931 /* FIXME: Is this fallback alright? */
6ac30c94 932 if (activate_lv_excl(lv->vg->cmd, lv)) {
56b3d204
MS
933 if (!deactivate_lv(lv->vg->cmd, lv))
934 stack;
f3d8974d 935 /* FIXME: locally & exclusive are undefined. */
25a2e7b8 936 return 0;
6ac30c94 937 }
f3d8974d 938 /* FIXME: Check exclusive value here. */
27ff8813
JEB
939out:
940 if (locally)
941 *locally = l;
942 if (exclusive)
943 *exclusive = e;
944
945 log_very_verbose("%s/%s is %sactive%s%s",
946 lv->vg->name, lv->name,
947 (r || l) ? "" : "not ",
948 (exclusive && e) ? " exclusive" : "",
949 e ? (l ? " locally" : " remotely") : "");
950
951 return r || l;
952}
953
954int lv_is_active(struct logical_volume *lv)
955{
956 return _lv_is_active(lv, NULL, NULL);
957}
958
9e277b9e 959int lv_is_active_but_not_locally(struct logical_volume *lv)
27ff8813
JEB
960{
961 int l;
9e277b9e 962 return _lv_is_active(lv, &l, NULL) && !l;
27ff8813 963}
27ff8813 964
b19f0121
JEB
965int lv_is_active_exclusive(struct logical_volume *lv)
966{
967 int e;
968
969 return _lv_is_active(lv, NULL, &e) && e;
970}
971
27ff8813
JEB
972int lv_is_active_exclusive_locally(struct logical_volume *lv)
973{
974 int l, e;
7df72b3c 975
27ff8813
JEB
976 return _lv_is_active(lv, &l, &e) && l && e;
977}
978
979int lv_is_active_exclusive_remotely(struct logical_volume *lv)
980{
981 int l, e;
7df72b3c 982
27ff8813 983 return _lv_is_active(lv, &l, &e) && !l && e;
5ca6698f
DW
984}
985
d1e8046f
AK
986#ifdef DMEVENTD
987static struct dm_event_handler *_create_dm_event_handler(struct cmd_context *cmd, const char *dmuuid, const char *dso,
988 const int timeout, enum dm_event_mask mask)
989{
990 struct dm_event_handler *dmevh;
991
992 if (!(dmevh = dm_event_handler_create()))
993 return_NULL;
994
995 if (dm_event_handler_set_dmeventd_path(dmevh, find_config_tree_str(cmd, "dmeventd/executable", NULL)))
996 goto_bad;
997
998 if (dm_event_handler_set_dso(dmevh, dso))
999 goto_bad;
1000
1001 if (dm_event_handler_set_uuid(dmevh, dmuuid))
1002 goto_bad;
1003
1004 dm_event_handler_set_timeout(dmevh, timeout);
1005 dm_event_handler_set_event_mask(dmevh, mask);
1006
1007 return dmevh;
1008
1009bad:
1010 dm_event_handler_destroy(dmevh);
1011 return NULL;
1012}
1013
1014char *get_monitor_dso_path(struct cmd_context *cmd, const char *libpath)
1015{
1016 char *path;
1017
1018 if (!(path = dm_pool_alloc(cmd->mem, PATH_MAX))) {
1019 log_error("Failed to allocate dmeventd library path.");
1020 return NULL;
1021 }
1022
1023 get_shared_library_path(cmd, libpath, path, PATH_MAX);
1024
1025 return path;
1026}
1027
2bc1d759
ZK
1028static char *_build_target_uuid(struct cmd_context *cmd, struct logical_volume *lv)
1029{
1030 const char *layer;
1031
1032 if (lv_is_thin_pool(lv))
1033 layer = "tpool"; /* Monitor "tpool" for the "thin pool". */
1034 else if (lv_is_origin(lv))
1035 layer = "real"; /* Monitor "real" for "snapshot-origin". */
1036 else
1037 layer = NULL;
1038
1039 return build_dm_uuid(cmd->mem, lv->lvid.s, layer);
1040}
1041
f92b4f94
AK
1042int target_registered_with_dmeventd(struct cmd_context *cmd, const char *dso,
1043 struct logical_volume *lv, int *pending)
d1e8046f
AK
1044{
1045 char *uuid;
1046 enum dm_event_mask evmask = 0;
1047 struct dm_event_handler *dmevh;
d1e8046f
AK
1048 *pending = 0;
1049
1050 if (!dso)
1051 return_0;
1052
2bc1d759 1053 if (!(uuid = _build_target_uuid(cmd, lv)))
d1e8046f
AK
1054 return_0;
1055
1056 if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, 0, DM_EVENT_ALL_ERRORS)))
1057 return_0;
1058
1059 if (dm_event_get_registered_device(dmevh, 0)) {
1060 dm_event_handler_destroy(dmevh);
1061 return 0;
1062 }
1063
1064 evmask = dm_event_handler_get_event_mask(dmevh);
1065 if (evmask & DM_EVENT_REGISTRATION_PENDING) {
1066 *pending = 1;
1067 evmask &= ~DM_EVENT_REGISTRATION_PENDING;
1068 }
1069
1070 dm_event_handler_destroy(dmevh);
1071
1072 return evmask;
1073}
1074
f92b4f94 1075int target_register_events(struct cmd_context *cmd, const char *dso, struct logical_volume *lv,
d1e8046f
AK
1076 int evmask __attribute__((unused)), int set, int timeout)
1077{
1078 char *uuid;
1079 struct dm_event_handler *dmevh;
1080 int r;
1081
1082 if (!dso)
1083 return_0;
1084
f92b4f94 1085 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
2bc1d759 1086 if (!(uuid = _build_target_uuid(cmd, lv)))
d1e8046f
AK
1087 return_0;
1088
1089 if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, timeout,
1090 DM_EVENT_ALL_ERRORS | (timeout ? DM_EVENT_TIMEOUT : 0))))
1091 return_0;
1092
1093 r = set ? dm_event_register_handler(dmevh) : dm_event_unregister_handler(dmevh);
1094
1095 dm_event_handler_destroy(dmevh);
1096
1097 if (!r)
1098 return_0;
1099
1100 log_info("%s %s for events", set ? "Monitored" : "Unmonitored", uuid);
1101
1102 return 1;
1103}
1104
1105#endif
1106
3e3d5d85 1107/*
8a37910d
AK
1108 * Returns 0 if an attempt to (un)monitor the device failed.
1109 * Returns 1 otherwise.
3e3d5d85 1110 */
2d6fcbf6 1111int monitor_dev_for_events(struct cmd_context *cmd, struct logical_volume *lv,
81beded3 1112 const struct lv_activate_opts *laopts, int monitor)
15d91f5a 1113{
ed09d7e3 1114#ifdef DMEVENTD
8a37910d
AK
1115 int i, pending = 0, monitored;
1116 int r = 1;
2c44337b 1117 struct dm_list *tmp, *snh, *snht;
15d91f5a 1118 struct lv_segment *seg;
7a369d37 1119 struct lv_segment *log_seg;
24f4552b 1120 int (*monitor_fn) (struct lv_segment *s, int e);
4e9083db 1121 uint32_t s;
81beded3
ZK
1122 static const struct lv_activate_opts zlaopts = { 0 };
1123
1124 if (!laopts)
1125 laopts = &zlaopts;
15d91f5a 1126
20db8ffc
AK
1127 /* skip dmeventd code altogether */
1128 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
1129 return 1;
1130
8a37910d
AK
1131 /*
1132 * Nothing to do if dmeventd configured not to be used.
1133 */
1134 if (monitor && !dmeventd_monitor_mode())
3e3d5d85
AK
1135 return 1;
1136
67961c7c
PR
1137 /*
1138 * In case of a snapshot device, we monitor lv->snapshot->lv,
1139 * not the actual LV itself.
1140 */
c6168a14
ZK
1141 if (lv_is_cow(lv) && (laopts->no_merging || !lv_is_merging_cow(lv)))
1142 return monitor_dev_for_events(cmd, lv->snapshot->lv, NULL, monitor);
67961c7c
PR
1143
1144 /*
1145 * In case this LV is a snapshot origin, we instead monitor
f92b4f94
AK
1146 * each of its respective snapshots. The origin itself may
1147 * also need to be monitored if it is a mirror, for example.
67961c7c 1148 */
81beded3 1149 if (!laopts->origin_only && lv_is_origin(lv))
2c44337b
AK
1150 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
1151 if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
81beded3 1152 struct lv_segment, origin_list)->cow, NULL, monitor))
0c06de63 1153 r = 0;
67961c7c 1154
7a369d37
JEB
1155 /*
1156 * If the volume is mirrored and its log is also mirrored, monitor
1157 * the log volume as well.
1158 */
1159 if ((seg = first_seg(lv)) != NULL && seg->log_lv != NULL &&
1160 (log_seg = first_seg(seg->log_lv)) != NULL &&
1161 seg_is_mirrored(log_seg))
81beded3 1162 if (!monitor_dev_for_events(cmd, seg->log_lv, NULL, monitor))
7a369d37
JEB
1163 r = 0;
1164
2c44337b
AK
1165 dm_list_iterate(tmp, &lv->segments) {
1166 seg = dm_list_item(tmp, struct lv_segment);
15d91f5a 1167
4e9083db
AK
1168 /* Recurse for AREA_LV */
1169 for (s = 0; s < seg->area_count; s++) {
1170 if (seg_type(seg, s) != AREA_LV)
1171 continue;
81beded3 1172 if (!monitor_dev_for_events(cmd, seg_lv(seg, s), NULL,
4e9083db
AK
1173 monitor)) {
1174 log_error("Failed to %smonitor %s",
1175 monitor ? "" : "un",
1176 seg_lv(seg, s)->name);
1177 r = 0;
1178 }
1179 }
1180
8ef6eb30
AK
1181 if (!seg_monitored(seg) || (seg->status & PVMOVE))
1182 continue;
8a37910d
AK
1183
1184 monitor_fn = NULL;
ed09d7e3 1185
8ef6eb30 1186 /* Check monitoring status */
8a37910d
AK
1187 if (seg->segtype->ops->target_monitored)
1188 monitored = seg->segtype->ops->target_monitored(seg, &pending);
8ef6eb30
AK
1189 else
1190 continue; /* segtype doesn't support registration */
1191
1192 /*
1193 * FIXME: We should really try again if pending
1194 */
8a37910d 1195 monitored = (pending) ? 0 : monitored;
8ef6eb30 1196
8a37910d
AK
1197 if (monitor) {
1198 if (monitored)
8ef6eb30 1199 log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
8a37910d
AK
1200 else if (seg->segtype->ops->target_monitor_events)
1201 monitor_fn = seg->segtype->ops->target_monitor_events;
8ef6eb30 1202 } else {
8a37910d 1203 if (!monitored)
8ef6eb30 1204 log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
8a37910d
AK
1205 else if (seg->segtype->ops->target_unmonitor_events)
1206 monitor_fn = seg->segtype->ops->target_unmonitor_events;
8ef6eb30 1207 }
15d91f5a 1208
8ef6eb30 1209 /* Do [un]monitor */
8a37910d 1210 if (!monitor_fn)
3e3d5d85 1211 continue;
e24e7130 1212
85a80e05
AK
1213 log_verbose("%sonitoring %s/%s%s", monitor ? "M" : "Not m", lv->vg->name, lv->name,
1214 test_mode() ? " [Test mode: skipping this]" : "");
1215
1216 /* FIXME Test mode should really continue a bit further. */
1217 if (test_mode())
1218 continue;
8a37910d 1219
3e3d5d85 1220 /* FIXME specify events */
57fc4cc0 1221 if (!monitor_fn(seg, 0)) {
8a37910d
AK
1222 log_error("%s/%s: %s segment monitoring function failed.",
1223 lv->vg->name, lv->name, seg->segtype->name);
1224 return 0;
e24e7130 1225 }
3e3d5d85 1226
8ef6eb30
AK
1227 /* Check [un]monitor results */
1228 /* Try a couple times if pending, but not forever... */
1229 for (i = 0; i < 10; i++) {
1230 pending = 0;
8a37910d 1231 monitored = seg->segtype->ops->target_monitored(seg, &pending);
8ef6eb30 1232 if (pending ||
8a37910d
AK
1233 (!monitored && monitor) ||
1234 (monitored && !monitor))
1235 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
1236 lv->vg->name, lv->name, monitor ? "" : "un");
8ef6eb30
AK
1237 else
1238 break;
1239 sleep(1);
1240 }
1241
85a80e05
AK
1242 if (r)
1243 r = (monitored && monitor) || (!monitored && !monitor);
15d91f5a 1244 }
e6493477 1245
3e3d5d85
AK
1246 return r;
1247#else
15d91f5a 1248 return 1;
3e3d5d85 1249#endif
15d91f5a 1250}
15d91f5a 1251
0f2a4ca2
AK
1252struct detached_lv_data {
1253 struct logical_volume *lv_pre;
1254 struct lv_activate_opts *laopts;
1255 int *flush_required;
1256};
1257
1258static int _preload_detached_lv(struct cmd_context *cmd, struct logical_volume *lv, void *data)
1259{
1260 struct detached_lv_data *detached = data;
1261 struct lv_list *lvl_pre;
1262
1263 if ((lvl_pre = find_lv_in_vg(detached->lv_pre->vg, lv->name))) {
ee840ff1 1264 if (lv_is_visible(lvl_pre->lv) && lv_is_active(lv) && (!lv_is_cow(lv) || !lv_is_cow(lvl_pre->lv)) &&
0f2a4ca2
AK
1265 !_lv_preload(lvl_pre->lv, detached->laopts, detached->flush_required))
1266 return_0;
1267 }
1268
1269 return 1;
1270}
1271
658b5812 1272static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
81beded3 1273 struct lv_activate_opts *laopts, int error_if_not_suspended)
413cc918 1274{
df390f17
AK
1275 struct logical_volume *lv = NULL, *lv_pre = NULL, *pvmove_lv = NULL;
1276 struct lv_list *lvl_pre;
1277 struct seg_list *sl;
ee840ff1 1278 struct lv_segment *snap_seg;
199e490e 1279 struct lvinfo info;
eb91c4ee 1280 int r = 0, lockfs = 0, flush_required = 0;
0f2a4ca2 1281 struct detached_lv_data detached;
413cc918 1282
d1d9800e
AK
1283 if (!activation())
1284 return 1;
1285
8b888354 1286 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1287 goto_out;
8b888354 1288
7a593325 1289 /* Use precommitted metadata if present */
8b888354 1290 if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
095bbca6 1291 goto_out;
413cc918 1292
2d6fcbf6
AK
1293 /* Ignore origin_only unless LV is origin in both old and new metadata */
1294 if (!lv_is_origin(lv) || !lv_is_origin(lv_pre))
81beded3 1295 laopts->origin_only = 0;
2d6fcbf6 1296
20c5fcf7 1297 if (test_mode()) {
81beded3 1298 _skip("Suspending %s%s.", lv->name, laopts->origin_only ? " origin without snapshots" : "");
095bbca6
MB
1299 r = 1;
1300 goto out;
20c5fcf7
AK
1301 }
1302
81beded3 1303 if (!lv_info(cmd, lv, laopts->origin_only, &info, 0, 0))
095bbca6 1304 goto_out;
41967a02 1305
095bbca6 1306 if (!info.exists || info.suspended) {
63ae0d14
MB
1307 if (!error_if_not_suspended) {
1308 r = 1;
1309 if (info.suspended)
df390f17 1310 critical_section_inc(cmd, "already suspended");
63ae0d14 1311 }
095bbca6
MB
1312 goto out;
1313 }
914c9723 1314
9249fb12
ZK
1315 if (!lv_read_replicator_vgs(lv))
1316 goto_out;
1317
c1fdeec9
MB
1318 lv_calculate_readahead(lv, NULL);
1319
df390f17 1320 /*
3a8eb387 1321 * Preload devices for the LV.
df390f17
AK
1322 * If the PVMOVE LV is being removed, it's only present in the old
1323 * metadata and not the new, so we must explicitly add the new
1324 * tables for all the changed LVs here, as the relationships
1325 * are not found by walking the new metadata.
1326 */
3a8eb387
AK
1327 if (!(lv_pre->status & LOCKED) &&
1328 (lv->status & LOCKED) &&
1329 (pvmove_lv = find_pvmove_lv_in_lv(lv))) {
1330 /* Preload all the LVs above the PVMOVE LV */
1331 dm_list_iterate_items(sl, &pvmove_lv->segs_using_this_lv) {
1332 if (!(lvl_pre = find_lv_in_vg(lv_pre->vg, sl->seg->lv->name))) {
ee840ff1 1333 log_error(INTERNAL_ERROR "LV %s missing from preload metadata", sl->seg->lv->name);
df390f17
AK
1334 goto out;
1335 }
81beded3 1336 if (!_lv_preload(lvl_pre->lv, laopts, &flush_required))
df390f17 1337 goto_out;
3a8eb387
AK
1338 }
1339 /* Now preload the PVMOVE LV itself */
1340 if (!(lvl_pre = find_lv_in_vg(lv_pre->vg, pvmove_lv->name))) {
ee840ff1 1341 log_error(INTERNAL_ERROR "LV %s missing from preload metadata", pvmove_lv->name);
3a8eb387
AK
1342 goto out;
1343 }
1344 if (!_lv_preload(lvl_pre->lv, laopts, &flush_required))
1345 goto_out;
1346 } else {
1347 if (!_lv_preload(lv_pre, laopts, &flush_required))
1348 /* FIXME Revert preloading */
1349 goto_out;
0f2a4ca2 1350
3a8eb387
AK
1351 /*
1352 * Search for existing LVs that have become detached and preload them.
1353 */
1354 detached.lv_pre = lv_pre;
1355 detached.laopts = laopts;
1356 detached.flush_required = &flush_required;
0f2a4ca2 1357
3a8eb387
AK
1358 if (!for_each_sub_lv(cmd, lv, &_preload_detached_lv, &detached))
1359 goto_out;
ee840ff1
AK
1360
1361 /*
1362 * Preload any snapshots that are being removed.
1363 */
1364 if (!laopts->origin_only && lv_is_origin(lv)) {
1365 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
a73e9a6c
AK
1366 if (!(lvl_pre = find_lv_in_vg_by_lvid(lv_pre->vg, &snap_seg->cow->lvid))) {
1367 log_error(INTERNAL_ERROR "LV %s (%s) missing from preload metadata",
1368 snap_seg->cow->name, snap_seg->cow->lvid.id[1].uuid);
ee840ff1
AK
1369 goto out;
1370 }
1371 if (!lv_is_cow(lvl_pre->lv) &&
1372 !_lv_preload(lvl_pre->lv, laopts, &flush_required))
1373 goto_out;
1374 }
1375 }
5f4b2acf
AK
1376 }
1377
81beded3 1378 if (!monitor_dev_for_events(cmd, lv, laopts, 0))
e24e7130 1379 /* FIXME Consider aborting here */
ed09d7e3
AK
1380 stack;
1381
df390f17
AK
1382 critical_section_inc(cmd, "suspending");
1383 if (pvmove_lv)
1384 critical_section_inc(cmd, "suspending pvmove LV");
9cd3426d 1385
81beded3 1386 if (!laopts->origin_only &&
2d6fcbf6 1387 (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)))
9cd3426d
AK
1388 lockfs = 1;
1389
df390f17
AK
1390 /*
1391 * Suspending an LV directly above a PVMOVE LV also
1392 * suspends other LVs using that same PVMOVE LV.
1393 * FIXME Remove this and delay the 'clear node' until
1394 * after the code knows whether there's a different
1395 * inactive table to load or not instead so lv_suspend
1396 * can be called separately for each LV safely.
1397 */
1398 if ((lv_pre->vg->status & PRECOMMITTED) &&
1399 (lv_pre->status & LOCKED) && find_pvmove_lv_in_lv(lv_pre)) {
81beded3 1400 if (!_lv_suspend_lv(lv_pre, laopts, lockfs, flush_required)) {
df390f17
AK
1401 critical_section_dec(cmd, "failed precommitted suspend");
1402 if (pvmove_lv)
1403 critical_section_dec(cmd, "failed precommitted suspend (pvmove)");
1404 goto_out;
1405 }
1406 } else {
1407 /* Normal suspend */
81beded3 1408 if (!_lv_suspend_lv(lv, laopts, lockfs, flush_required)) {
df390f17
AK
1409 critical_section_dec(cmd, "failed suspend");
1410 if (pvmove_lv)
1411 critical_section_dec(cmd, "failed suspend (pvmove)");
1412 goto_out;
1413 }
914c9723 1414 }
8c013da4 1415
095bbca6
MB
1416 r = 1;
1417out:
1418 if (lv_pre)
077a6755 1419 release_vg(lv_pre->vg);
9249fb12
ZK
1420 if (lv) {
1421 lv_release_replicator_vgs(lv);
077a6755 1422 release_vg(lv->vg);
9249fb12 1423 }
095bbca6
MB
1424
1425 return r;
413cc918
AK
1426}
1427
658b5812 1428/* Returns success if the device is not active */
2d6fcbf6 1429int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
658b5812 1430{
81beded3
ZK
1431 struct lv_activate_opts laopts = { .origin_only = origin_only };
1432
1433 return _lv_suspend(cmd, lvid_s, &laopts, 0);
658b5812
AK
1434}
1435
2d6fcbf6
AK
1436/* No longer used */
1437/***********
658b5812
AK
1438int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1439{
1440 return _lv_suspend(cmd, lvid_s, 1);
1441}
2d6fcbf6 1442***********/
658b5812 1443
c054e7cc
JEB
1444 /*
1445 * _lv_resume
1446 * @cmd
1447 * @lvid_s
1448 * @origin_only
1449 * @exclusive: This parameter only has an affect in cluster-context.
1450 * It forces local target type to be used (instead of
1451 * cluster-aware type).
1452 * @error_if_not_active
1453 */
658b5812 1454static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
81beded3 1455 struct lv_activate_opts *laopts, int error_if_not_active)
413cc918
AK
1456{
1457 struct logical_volume *lv;
199e490e 1458 struct lvinfo info;
095bbca6 1459 int r = 0;
413cc918 1460
d1d9800e
AK
1461 if (!activation())
1462 return 1;
1463
7a593325 1464 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
75b37a11 1465 goto_out;
413cc918 1466
2d6fcbf6 1467 if (!lv_is_origin(lv))
81beded3 1468 laopts->origin_only = 0;
2d6fcbf6 1469
20c5fcf7 1470 if (test_mode()) {
10d0d9c7
AK
1471 _skip("Resuming %s%s%s.", lv->name, laopts->origin_only ? " without snapshots" : "",
1472 laopts->revert ? " (reverting)" : "");
095bbca6
MB
1473 r = 1;
1474 goto out;
20c5fcf7
AK
1475 }
1476
10d0d9c7 1477 log_debug("Resuming LV %s/%s%s%s%s.", lv->vg->name, lv->name,
7df72b3c 1478 error_if_not_active ? "" : " if active",
10d0d9c7
AK
1479 laopts->origin_only ? " without snapshots" : "",
1480 laopts->revert ? " (reverting)" : "");
7df72b3c 1481
81beded3 1482 if (!lv_info(cmd, lv, laopts->origin_only, &info, 0, 0))
095bbca6 1483 goto_out;
41967a02 1484
095bbca6 1485 if (!info.exists || !info.suspended) {
4ec2ae86
ZK
1486 if (error_if_not_active)
1487 goto_out;
1488 r = 1;
df390f17
AK
1489 if (!info.suspended)
1490 critical_section_dec(cmd, "already resumed");
4ec2ae86 1491 goto out;
095bbca6 1492 }
914c9723 1493
a18dcfb5
AK
1494 laopts->read_only = _passes_readonly_filter(cmd, lv);
1495
81beded3 1496 if (!_lv_activate_lv(lv, laopts))
75b37a11 1497 goto_out;
914c9723 1498
df390f17 1499 critical_section_dec(cmd, "resumed");
413cc918 1500
81beded3 1501 if (!monitor_dev_for_events(cmd, lv, laopts, 1))
ed09d7e3 1502 stack;
15d91f5a 1503
095bbca6
MB
1504 r = 1;
1505out:
1506 if (lv)
077a6755 1507 release_vg(lv->vg);
095bbca6
MB
1508
1509 return r;
413cc918
AK
1510}
1511
658b5812 1512/* Returns success if the device is not active */
c054e7cc 1513int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
10d0d9c7 1514 unsigned origin_only, unsigned exclusive, unsigned revert)
658b5812 1515{
81beded3
ZK
1516 struct lv_activate_opts laopts = {
1517 .origin_only = origin_only,
1518 /*
1519 * When targets are activated exclusively in a cluster, the
1520 * non-clustered target should be used. This only happens
1521 * if exclusive is set.
1522 */
10d0d9c7
AK
1523 .exclusive = exclusive,
1524 .revert = revert
81beded3
ZK
1525 };
1526
1527 return _lv_resume(cmd, lvid_s, &laopts, 0);
658b5812
AK
1528}
1529
2d6fcbf6 1530int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
658b5812 1531{
81beded3
ZK
1532 struct lv_activate_opts laopts = { .origin_only = origin_only, };
1533
1534 return _lv_resume(cmd, lvid_s, &laopts, 1);
658b5812
AK
1535}
1536
64a95010
AK
1537static int _lv_has_open_snapshots(struct logical_volume *lv)
1538{
1539 struct lv_segment *snap_seg;
1540 struct lvinfo info;
1541 int r = 0;
1542
1543 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
2d6fcbf6 1544 if (!lv_info(lv->vg->cmd, snap_seg->cow, 0, &info, 1, 0)) {
64a95010
AK
1545 r = 1;
1546 continue;
1547 }
1548
1549 if (info.exists && info.open_count) {
1550 log_error("LV %s/%s has open snapshot %s: "
1551 "not deactivating", lv->vg->name, lv->name,
1552 snap_seg->cow->name);
1553 r = 1;
1554 }
1555 }
1556
1557 return r;
1558}
1559
be326a2f 1560int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
f4cbeaf0
AK
1561{
1562 struct logical_volume *lv;
199e490e 1563 struct lvinfo info;
095bbca6 1564 int r = 0;
f4cbeaf0 1565
d1d9800e
AK
1566 if (!activation())
1567 return 1;
1568
7a593325 1569 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1570 goto out;
f4cbeaf0 1571
20c5fcf7
AK
1572 if (test_mode()) {
1573 _skip("Deactivating '%s'.", lv->name);
095bbca6
MB
1574 r = 1;
1575 goto out;
20c5fcf7
AK
1576 }
1577
7df72b3c
AK
1578 log_debug("Deactivating %s/%s.", lv->vg->name, lv->name);
1579
2d6fcbf6 1580 if (!lv_info(cmd, lv, 0, &info, 1, 0))
095bbca6 1581 goto_out;
41967a02 1582
095bbca6
MB
1583 if (!info.exists) {
1584 r = 1;
1585 goto out;
1586 }
f4cbeaf0 1587
64a95010 1588 if (lv_is_visible(lv)) {
125712be
PR
1589 if (!lv_check_not_in_use(cmd, lv, &info))
1590 goto_out;
1591
64a95010
AK
1592 if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1593 goto_out;
0cf96f33
AK
1594 }
1595
9249fb12
ZK
1596 if (!lv_read_replicator_vgs(lv))
1597 goto_out;
1598
c1fdeec9
MB
1599 lv_calculate_readahead(lv, NULL);
1600
81beded3 1601 if (!monitor_dev_for_events(cmd, lv, NULL, 0))
ed09d7e3 1602 stack;
15d91f5a 1603
df390f17 1604 critical_section_inc(cmd, "deactivating");
914c9723 1605 r = _lv_deactivate(lv);
df390f17 1606 critical_section_dec(cmd, "deactivated");
914c9723 1607
401a40d9 1608 if (!lv_info(cmd, lv, 0, &info, 0, 0) || info.exists)
89a6cdfd 1609 r = 0;
095bbca6 1610out:
9249fb12
ZK
1611 if (lv) {
1612 lv_release_replicator_vgs(lv);
077a6755 1613 release_vg(lv->vg);
9249fb12 1614 }
095bbca6 1615
914c9723 1616 return r;
f4cbeaf0
AK
1617}
1618
658b5812
AK
1619/* Test if LV passes filter */
1620int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
1621 int *activate_lv)
1622{
1623 struct logical_volume *lv;
095bbca6 1624 int r = 0;
658b5812 1625
095bbca6
MB
1626 if (!activation()) {
1627 *activate_lv = 1;
1628 return 1;
1629 }
658b5812 1630
424dd43e 1631 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1632 goto out;
658b5812
AK
1633
1634 if (!_passes_activation_filter(cmd, lv)) {
f7e3a19f
PR
1635 log_verbose("Not activating %s/%s since it does not pass "
1636 "activation filter.", lv->vg->name, lv->name);
658b5812 1637 *activate_lv = 0;
095bbca6
MB
1638 } else
1639 *activate_lv = 1;
1640 r = 1;
1641out:
1642 if (lv)
077a6755 1643 release_vg(lv->vg);
658b5812 1644
095bbca6 1645 return r;
658b5812
AK
1646}
1647
07d31831 1648static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
81beded3 1649 struct lv_activate_opts *laopts, int filter)
f4cbeaf0
AK
1650{
1651 struct logical_volume *lv;
199e490e 1652 struct lvinfo info;
095bbca6 1653 int r = 0;
f4cbeaf0 1654
d1d9800e
AK
1655 if (!activation())
1656 return 1;
1657
424dd43e 1658 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1659 goto out;
f4cbeaf0 1660
658b5812 1661 if (filter && !_passes_activation_filter(cmd, lv)) {
f7e3a19f
PR
1662 log_error("Not activating %s/%s since it does not pass "
1663 "activation filter.", lv->vg->name, lv->name);
095bbca6 1664 goto out;
de17d760
AK
1665 }
1666
cda35408 1667 if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
8c5bcdab
AK
1668 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1669 lv->name);
095bbca6 1670 goto_out;
8c5bcdab
AK
1671 }
1672
b4048242
PR
1673 if (lv_has_unknown_segments(lv)) {
1674 log_error("Refusing activation of LV %s containing "
1675 "an unrecognised segment.", lv->name);
1676 goto_out;
1677 }
1678
20c5fcf7
AK
1679 if (test_mode()) {
1680 _skip("Activating '%s'.", lv->name);
095bbca6
MB
1681 r = 1;
1682 goto out;
20c5fcf7
AK
1683 }
1684
a18dcfb5
AK
1685 if (filter)
1686 laopts->read_only = _passes_readonly_filter(cmd, lv);
1687
1688 log_debug("Activating %s/%s%s%s.", lv->vg->name, lv->name,
1689 laopts->exclusive ? " exclusively" : "",
1690 laopts->read_only ? " read-only" : "");
7df72b3c 1691
2d6fcbf6 1692 if (!lv_info(cmd, lv, 0, &info, 0, 0))
095bbca6 1693 goto_out;
8c013da4 1694
a18dcfb5
AK
1695 /*
1696 * Nothing to do?
1697 */
1698 if (info.exists && !info.suspended && info.live_table &&
1699 (info.read_only == read_only_lv(lv, laopts))) {
095bbca6
MB
1700 r = 1;
1701 goto out;
1702 }
f4cbeaf0 1703
9249fb12
ZK
1704 if (!lv_read_replicator_vgs(lv))
1705 goto_out;
1706
c1fdeec9
MB
1707 lv_calculate_readahead(lv, NULL);
1708
df390f17 1709 critical_section_inc(cmd, "activating");
81beded3 1710 if (!(r = _lv_activate_lv(lv, laopts)))
75b37a11 1711 stack;
df390f17 1712 critical_section_dec(cmd, "activated");
914c9723 1713
81beded3 1714 if (r && !monitor_dev_for_events(cmd, lv, laopts, 1))
ed09d7e3 1715 stack;
15d91f5a 1716
095bbca6 1717out:
9249fb12
ZK
1718 if (lv) {
1719 lv_release_replicator_vgs(lv);
077a6755 1720 release_vg(lv->vg);
9249fb12 1721 }
095bbca6 1722
914c9723 1723 return r;
f4cbeaf0 1724}
199e490e 1725
658b5812 1726/* Activate LV */
07d31831 1727int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
658b5812 1728{
81beded3
ZK
1729 struct lv_activate_opts laopts = { .exclusive = exclusive };
1730
1731 if (!_lv_activate(cmd, lvid_s, &laopts, 0))
75b37a11
AK
1732 return_0;
1733
1734 return 1;
658b5812
AK
1735}
1736
1737/* Activate LV only if it passes filter */
07d31831 1738int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
658b5812 1739{
81beded3
ZK
1740 struct lv_activate_opts laopts = { .exclusive = exclusive };
1741
1742 if (!_lv_activate(cmd, lvid_s, &laopts, 1))
75b37a11
AK
1743 return_0;
1744
1745 return 1;
658b5812
AK
1746}
1747
f7dd6d84
AK
1748int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1749{
f7dd6d84
AK
1750 int r = 1;
1751
8b076648 1752 if (!lv) {
2262b320 1753 r = dm_mknodes(NULL);
8b076648
AK
1754 fs_unlock();
1755 return r;
1756 }
1757
ab9663f3
MB
1758 if (!activation())
1759 return 1;
f7dd6d84 1760
ab9663f3 1761 r = dev_manager_mknodes(lv);
f7dd6d84
AK
1762
1763 fs_unlock();
1764
1765 return r;
1766}
1767
352a99b9
AK
1768/*
1769 * Does PV use VG somewhere in its construction?
1770 * Returns 1 on failure.
1771 */
898e6f8e 1772int pv_uses_vg(struct physical_volume *pv,
3e3d5d85 1773 struct volume_group *vg)
352a99b9 1774{
352a99b9
AK
1775 if (!activation())
1776 return 0;
1777
1778 if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1779 return 0;
1780
898e6f8e 1781 return dev_manager_device_uses_vg(pv->dev, vg);
352a99b9
AK
1782}
1783
2293567c
AK
1784void activation_release(void)
1785{
1786 dev_manager_release();
1787}
1788
914c9723
AK
1789void activation_exit(void)
1790{
1791 dev_manager_exit();
1792}
199e490e 1793#endif
This page took 0.342643 seconds and 5 git commands to generate.