]> sourceware.org Git - lvm2.git/blame - lib/activate/activate.c
Thin updated support for thin pool percent
[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{
5d5c80ac 398 const struct dm_config_node *cn;
a18dcfb5
AK
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 712/*
63368983 713 * Returns data or metadata percent usage, depends on metadata 0/1.
34507894
ZK
714 * Returns 1 if percent set, else 0 on failure.
715 */
63368983
ZK
716int lv_thin_pool_percent(const struct logical_volume *lv, int metadata,
717 percent_t *percent)
34507894
ZK
718{
719 int r;
720 struct dev_manager *dm;
721
722 if (!activation())
723 return 0;
724
63368983
ZK
725 log_debug("Checking thin %sdata percent for LV %s/%s",
726 (metadata) ? "meta" : "", lv->vg->name, lv->name);
34507894
ZK
727
728 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
729 return_0;
730
63368983 731 if (!(r = dev_manager_thin_pool_percent(dm, lv, metadata, percent)))
34507894
ZK
732 stack;
733
734 dev_manager_destroy(dm);
735
736 return r;
737}
738
ab9663f3 739static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv)
2ba80b43 740{
199e490e 741 struct lvinfo info;
2ba80b43 742
2d6fcbf6 743 if (!lv_info(cmd, lv, 0, &info, 0, 0)) {
2ba80b43 744 stack;
de6c9183 745 return -1;
2ba80b43
JT
746 }
747
de6c9183 748 return info.exists;
2ba80b43
JT
749}
750
f894b4b1 751static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv)
5986ec94 752{
199e490e 753 struct lvinfo info;
5986ec94 754
2d6fcbf6 755 if (!lv_info(cmd, lv, 0, &info, 1, 0)) {
5986ec94 756 stack;
de6c9183 757 return -1;
5986ec94
JT
758 }
759
de6c9183 760 return info.open_count;
5986ec94
JT
761}
762
81beded3 763static int _lv_activate_lv(struct logical_volume *lv, struct lv_activate_opts *laopts)
b1713d28 764{
6d52fb46 765 int r;
de6c9183 766 struct dev_manager *dm;
b1713d28 767
df390f17 768 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
5f4b2acf 769 return_0;
ae2bb665 770
81beded3 771 if (!(r = dev_manager_activate(dm, lv, laopts)))
6d52fb46 772 stack;
ae2bb665 773
de6c9183 774 dev_manager_destroy(dm);
ae2bb665 775 return r;
b1713d28 776}
a381c45a 777
81beded3
ZK
778static int _lv_preload(struct logical_volume *lv, struct lv_activate_opts *laopts,
779 int *flush_required)
0a5e4a14 780{
a18dcfb5 781 int r = 0;
de6c9183 782 struct dev_manager *dm;
a18dcfb5
AK
783 int old_readonly = laopts->read_only;
784
785 laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);
37ed70b9 786
df390f17 787 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
a18dcfb5 788 goto_out;
5f4b2acf 789
81beded3 790 if (!(r = dev_manager_preload(dm, lv, laopts, flush_required)))
6d52fb46 791 stack;
5f4b2acf
AK
792
793 dev_manager_destroy(dm);
a18dcfb5
AK
794
795 laopts->read_only = old_readonly;
796out:
5f4b2acf
AK
797 return r;
798}
799
800static int _lv_deactivate(struct logical_volume *lv)
801{
802 int r;
803 struct dev_manager *dm;
804
df390f17 805 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
5f4b2acf 806 return_0;
37ed70b9 807
de6c9183 808 if (!(r = dev_manager_deactivate(dm, lv)))
37ed70b9 809 stack;
37ed70b9 810
de6c9183
JT
811 dev_manager_destroy(dm);
812 return r;
37ed70b9
JT
813}
814
81beded3
ZK
815static int _lv_suspend_lv(struct logical_volume *lv, struct lv_activate_opts *laopts,
816 int lockfs, int flush_required)
4a624ca0 817{
20c5fcf7
AK
818 int r;
819 struct dev_manager *dm;
c2d72fd4 820
a18dcfb5
AK
821 laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);
822
df390f17
AK
823 /*
824 * When we are asked to manipulate (normally suspend/resume) the PVMOVE
825 * device directly, we don't want to touch the devices that use it.
826 */
827 if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
5f4b2acf 828 return_0;
0a5e4a14 829
81beded3 830 if (!(r = dev_manager_suspend(dm, lv, laopts, lockfs, flush_required)))
37ed70b9 831 stack;
0a5e4a14 832
20c5fcf7
AK
833 dev_manager_destroy(dm);
834 return r;
6d52fb46 835}
4a624ca0 836
8c013da4 837/*
f75c11ed 838 * These two functions return the number of visible LVs in the state,
7df72b3c 839 * or -1 on error. FIXME Check this.
8c013da4 840 */
65752052 841int lvs_in_vg_activated(struct volume_group *vg)
f047219b 842{
60f13f01 843 struct lv_list *lvl;
94b8220f 844 int count = 0;
37ed70b9 845
d1d9800e
AK
846 if (!activation())
847 return 0;
848
7df72b3c 849 dm_list_iterate_items(lvl, &vg->lvs)
59d8429c 850 if (lv_is_visible(lvl->lv))
ab9663f3 851 count += (_lv_active(vg->cmd, lvl->lv) == 1);
7df72b3c
AK
852
853 log_debug("Counted %d active LVs in VG %s", count, vg->name);
37ed70b9
JT
854
855 return count;
f047219b 856}
2ba80b43 857
08c9ff43 858int lvs_in_vg_opened(const struct volume_group *vg)
2ba80b43 859{
08c9ff43 860 const struct lv_list *lvl;
94b8220f 861 int count = 0;
2ba80b43 862
d1d9800e
AK
863 if (!activation())
864 return 0;
865
7df72b3c 866 dm_list_iterate_items(lvl, &vg->lvs)
87371d48 867 if (lv_is_visible(lvl->lv))
f894b4b1 868 count += (_lv_open_count(vg->cmd, lvl->lv) > 0);
7df72b3c
AK
869
870 log_debug("Counted %d open LVs in VG %s", count, vg->name);
2ba80b43
JT
871
872 return count;
873}
413cc918 874
5ca6698f 875/*
27ff8813
JEB
876 * _lv_is_active
877 * @lv: logical volume being queried
878 * @locally: set if active locally (when provided)
879 * @exclusive: set if active exclusively (when provided)
880 *
5ca6698f 881 * Determine whether an LV is active locally or in a cluster.
27ff8813
JEB
882 * In addition to the return code which indicates whether or
883 * not the LV is active somewhere, two other values are set
884 * to yield more information about the status of the activation:
885 * return locally exclusively status
886 * ====== ======= =========== ======
887 * 0 0 0 not active
888 * 1 0 0 active remotely
889 * 1 0 1 exclusive remotely
890 * 1 1 0 active locally and possibly remotely
891 * 1 1 1 exclusive locally (or local && !cluster)
892 * The VG lock must be held to call this function.
893 *
894 * Returns: 0 or 1
5ca6698f 895 */
27ff8813
JEB
896static int _lv_is_active(struct logical_volume *lv,
897 int *locally, int *exclusive)
5ca6698f 898{
27ff8813
JEB
899 int r, l, e; /* remote, local, and exclusive */
900
901 r = l = e = 0;
6ac30c94 902
ab9663f3 903 if (_lv_active(lv->vg->cmd, lv))
27ff8813 904 l = 1;
5ca6698f 905
27ff8813
JEB
906 if (!vg_is_clustered(lv->vg)) {
907 e = 1; /* exclusive by definition */
908 goto out;
909 }
910
911 /* Active locally, and the caller doesn't care about exclusive */
912 if (l && !exclusive)
913 goto out;
5ca6698f 914
27ff8813
JEB
915 if ((r = remote_lock_held(lv->lvid.s, &e)) >= 0)
916 goto out;
6ac30c94
MB
917
918 /*
27ff8813
JEB
919 * If lock query is not supported (due to interfacing with old
920 * code), then we cannot evaluate exclusivity properly.
921 *
922 * Old users of this function will never be affected by this,
923 * since they are only concerned about active vs. not active.
924 * New users of this function who specifically ask for 'exclusive'
925 * will be given an error message.
6ac30c94 926 */
27ff8813
JEB
927 if (l) {
928 if (exclusive)
929 log_error("Unable to determine exclusivity of %s",
930 lv->name);
931 goto out;
932 }
933
f3d8974d 934 /* FIXME: Is this fallback alright? */
6ac30c94 935 if (activate_lv_excl(lv->vg->cmd, lv)) {
56b3d204
MS
936 if (!deactivate_lv(lv->vg->cmd, lv))
937 stack;
f3d8974d 938 /* FIXME: locally & exclusive are undefined. */
25a2e7b8 939 return 0;
6ac30c94 940 }
f3d8974d 941 /* FIXME: Check exclusive value here. */
27ff8813
JEB
942out:
943 if (locally)
944 *locally = l;
945 if (exclusive)
946 *exclusive = e;
947
948 log_very_verbose("%s/%s is %sactive%s%s",
949 lv->vg->name, lv->name,
950 (r || l) ? "" : "not ",
951 (exclusive && e) ? " exclusive" : "",
952 e ? (l ? " locally" : " remotely") : "");
953
954 return r || l;
955}
956
957int lv_is_active(struct logical_volume *lv)
958{
959 return _lv_is_active(lv, NULL, NULL);
960}
961
9e277b9e 962int lv_is_active_but_not_locally(struct logical_volume *lv)
27ff8813
JEB
963{
964 int l;
9e277b9e 965 return _lv_is_active(lv, &l, NULL) && !l;
27ff8813 966}
27ff8813 967
b19f0121
JEB
968int lv_is_active_exclusive(struct logical_volume *lv)
969{
970 int e;
971
972 return _lv_is_active(lv, NULL, &e) && e;
973}
974
27ff8813
JEB
975int lv_is_active_exclusive_locally(struct logical_volume *lv)
976{
977 int l, e;
7df72b3c 978
27ff8813
JEB
979 return _lv_is_active(lv, &l, &e) && l && e;
980}
981
982int lv_is_active_exclusive_remotely(struct logical_volume *lv)
983{
984 int l, e;
7df72b3c 985
27ff8813 986 return _lv_is_active(lv, &l, &e) && !l && e;
5ca6698f
DW
987}
988
d1e8046f
AK
989#ifdef DMEVENTD
990static struct dm_event_handler *_create_dm_event_handler(struct cmd_context *cmd, const char *dmuuid, const char *dso,
991 const int timeout, enum dm_event_mask mask)
992{
993 struct dm_event_handler *dmevh;
994
995 if (!(dmevh = dm_event_handler_create()))
996 return_NULL;
997
998 if (dm_event_handler_set_dmeventd_path(dmevh, find_config_tree_str(cmd, "dmeventd/executable", NULL)))
999 goto_bad;
1000
1001 if (dm_event_handler_set_dso(dmevh, dso))
1002 goto_bad;
1003
1004 if (dm_event_handler_set_uuid(dmevh, dmuuid))
1005 goto_bad;
1006
1007 dm_event_handler_set_timeout(dmevh, timeout);
1008 dm_event_handler_set_event_mask(dmevh, mask);
1009
1010 return dmevh;
1011
1012bad:
1013 dm_event_handler_destroy(dmevh);
1014 return NULL;
1015}
1016
1017char *get_monitor_dso_path(struct cmd_context *cmd, const char *libpath)
1018{
1019 char *path;
1020
1021 if (!(path = dm_pool_alloc(cmd->mem, PATH_MAX))) {
1022 log_error("Failed to allocate dmeventd library path.");
1023 return NULL;
1024 }
1025
1026 get_shared_library_path(cmd, libpath, path, PATH_MAX);
1027
1028 return path;
1029}
1030
2bc1d759
ZK
1031static char *_build_target_uuid(struct cmd_context *cmd, struct logical_volume *lv)
1032{
1033 const char *layer;
1034
1035 if (lv_is_thin_pool(lv))
1036 layer = "tpool"; /* Monitor "tpool" for the "thin pool". */
1037 else if (lv_is_origin(lv))
1038 layer = "real"; /* Monitor "real" for "snapshot-origin". */
1039 else
1040 layer = NULL;
1041
1042 return build_dm_uuid(cmd->mem, lv->lvid.s, layer);
1043}
1044
f92b4f94
AK
1045int target_registered_with_dmeventd(struct cmd_context *cmd, const char *dso,
1046 struct logical_volume *lv, int *pending)
d1e8046f
AK
1047{
1048 char *uuid;
1049 enum dm_event_mask evmask = 0;
1050 struct dm_event_handler *dmevh;
d1e8046f
AK
1051 *pending = 0;
1052
1053 if (!dso)
1054 return_0;
1055
2bc1d759 1056 if (!(uuid = _build_target_uuid(cmd, lv)))
d1e8046f
AK
1057 return_0;
1058
1059 if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, 0, DM_EVENT_ALL_ERRORS)))
1060 return_0;
1061
1062 if (dm_event_get_registered_device(dmevh, 0)) {
1063 dm_event_handler_destroy(dmevh);
1064 return 0;
1065 }
1066
1067 evmask = dm_event_handler_get_event_mask(dmevh);
1068 if (evmask & DM_EVENT_REGISTRATION_PENDING) {
1069 *pending = 1;
1070 evmask &= ~DM_EVENT_REGISTRATION_PENDING;
1071 }
1072
1073 dm_event_handler_destroy(dmevh);
1074
1075 return evmask;
1076}
1077
f92b4f94 1078int target_register_events(struct cmd_context *cmd, const char *dso, struct logical_volume *lv,
d1e8046f
AK
1079 int evmask __attribute__((unused)), int set, int timeout)
1080{
1081 char *uuid;
1082 struct dm_event_handler *dmevh;
1083 int r;
1084
1085 if (!dso)
1086 return_0;
1087
f92b4f94 1088 /* We always monitor the "real" device, never the "snapshot-origin" itself. */
2bc1d759 1089 if (!(uuid = _build_target_uuid(cmd, lv)))
d1e8046f
AK
1090 return_0;
1091
1092 if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, timeout,
1093 DM_EVENT_ALL_ERRORS | (timeout ? DM_EVENT_TIMEOUT : 0))))
1094 return_0;
1095
1096 r = set ? dm_event_register_handler(dmevh) : dm_event_unregister_handler(dmevh);
1097
1098 dm_event_handler_destroy(dmevh);
1099
1100 if (!r)
1101 return_0;
1102
1103 log_info("%s %s for events", set ? "Monitored" : "Unmonitored", uuid);
1104
1105 return 1;
1106}
1107
1108#endif
1109
3e3d5d85 1110/*
8a37910d
AK
1111 * Returns 0 if an attempt to (un)monitor the device failed.
1112 * Returns 1 otherwise.
3e3d5d85 1113 */
2d6fcbf6 1114int monitor_dev_for_events(struct cmd_context *cmd, struct logical_volume *lv,
81beded3 1115 const struct lv_activate_opts *laopts, int monitor)
15d91f5a 1116{
ed09d7e3 1117#ifdef DMEVENTD
8a37910d
AK
1118 int i, pending = 0, monitored;
1119 int r = 1;
2c44337b 1120 struct dm_list *tmp, *snh, *snht;
15d91f5a 1121 struct lv_segment *seg;
7a369d37 1122 struct lv_segment *log_seg;
24f4552b 1123 int (*monitor_fn) (struct lv_segment *s, int e);
4e9083db 1124 uint32_t s;
81beded3
ZK
1125 static const struct lv_activate_opts zlaopts = { 0 };
1126
1127 if (!laopts)
1128 laopts = &zlaopts;
15d91f5a 1129
20db8ffc
AK
1130 /* skip dmeventd code altogether */
1131 if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE)
1132 return 1;
1133
8a37910d
AK
1134 /*
1135 * Nothing to do if dmeventd configured not to be used.
1136 */
1137 if (monitor && !dmeventd_monitor_mode())
3e3d5d85
AK
1138 return 1;
1139
67961c7c
PR
1140 /*
1141 * In case of a snapshot device, we monitor lv->snapshot->lv,
1142 * not the actual LV itself.
1143 */
c6168a14
ZK
1144 if (lv_is_cow(lv) && (laopts->no_merging || !lv_is_merging_cow(lv)))
1145 return monitor_dev_for_events(cmd, lv->snapshot->lv, NULL, monitor);
67961c7c
PR
1146
1147 /*
1148 * In case this LV is a snapshot origin, we instead monitor
f92b4f94
AK
1149 * each of its respective snapshots. The origin itself may
1150 * also need to be monitored if it is a mirror, for example.
67961c7c 1151 */
81beded3 1152 if (!laopts->origin_only && lv_is_origin(lv))
2c44337b
AK
1153 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs)
1154 if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh,
81beded3 1155 struct lv_segment, origin_list)->cow, NULL, monitor))
0c06de63 1156 r = 0;
67961c7c 1157
7a369d37
JEB
1158 /*
1159 * If the volume is mirrored and its log is also mirrored, monitor
1160 * the log volume as well.
1161 */
1162 if ((seg = first_seg(lv)) != NULL && seg->log_lv != NULL &&
1163 (log_seg = first_seg(seg->log_lv)) != NULL &&
1164 seg_is_mirrored(log_seg))
81beded3 1165 if (!monitor_dev_for_events(cmd, seg->log_lv, NULL, monitor))
7a369d37
JEB
1166 r = 0;
1167
2c44337b
AK
1168 dm_list_iterate(tmp, &lv->segments) {
1169 seg = dm_list_item(tmp, struct lv_segment);
15d91f5a 1170
4e9083db
AK
1171 /* Recurse for AREA_LV */
1172 for (s = 0; s < seg->area_count; s++) {
1173 if (seg_type(seg, s) != AREA_LV)
1174 continue;
81beded3 1175 if (!monitor_dev_for_events(cmd, seg_lv(seg, s), NULL,
4e9083db
AK
1176 monitor)) {
1177 log_error("Failed to %smonitor %s",
1178 monitor ? "" : "un",
1179 seg_lv(seg, s)->name);
1180 r = 0;
1181 }
1182 }
1183
8ef6eb30
AK
1184 if (!seg_monitored(seg) || (seg->status & PVMOVE))
1185 continue;
8a37910d
AK
1186
1187 monitor_fn = NULL;
ed09d7e3 1188
8ef6eb30 1189 /* Check monitoring status */
8a37910d
AK
1190 if (seg->segtype->ops->target_monitored)
1191 monitored = seg->segtype->ops->target_monitored(seg, &pending);
8ef6eb30
AK
1192 else
1193 continue; /* segtype doesn't support registration */
1194
1195 /*
1196 * FIXME: We should really try again if pending
1197 */
8a37910d 1198 monitored = (pending) ? 0 : monitored;
8ef6eb30 1199
8a37910d
AK
1200 if (monitor) {
1201 if (monitored)
8ef6eb30 1202 log_verbose("%s/%s already monitored.", lv->vg->name, lv->name);
8a37910d
AK
1203 else if (seg->segtype->ops->target_monitor_events)
1204 monitor_fn = seg->segtype->ops->target_monitor_events;
8ef6eb30 1205 } else {
8a37910d 1206 if (!monitored)
8ef6eb30 1207 log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name);
8a37910d
AK
1208 else if (seg->segtype->ops->target_unmonitor_events)
1209 monitor_fn = seg->segtype->ops->target_unmonitor_events;
8ef6eb30 1210 }
15d91f5a 1211
8ef6eb30 1212 /* Do [un]monitor */
8a37910d 1213 if (!monitor_fn)
3e3d5d85 1214 continue;
e24e7130 1215
85a80e05
AK
1216 log_verbose("%sonitoring %s/%s%s", monitor ? "M" : "Not m", lv->vg->name, lv->name,
1217 test_mode() ? " [Test mode: skipping this]" : "");
1218
1219 /* FIXME Test mode should really continue a bit further. */
1220 if (test_mode())
1221 continue;
8a37910d 1222
3e3d5d85 1223 /* FIXME specify events */
57fc4cc0 1224 if (!monitor_fn(seg, 0)) {
8a37910d
AK
1225 log_error("%s/%s: %s segment monitoring function failed.",
1226 lv->vg->name, lv->name, seg->segtype->name);
1227 return 0;
e24e7130 1228 }
3e3d5d85 1229
8ef6eb30
AK
1230 /* Check [un]monitor results */
1231 /* Try a couple times if pending, but not forever... */
1232 for (i = 0; i < 10; i++) {
1233 pending = 0;
8a37910d 1234 monitored = seg->segtype->ops->target_monitored(seg, &pending);
8ef6eb30 1235 if (pending ||
8a37910d
AK
1236 (!monitored && monitor) ||
1237 (monitored && !monitor))
1238 log_very_verbose("%s/%s %smonitoring still pending: waiting...",
1239 lv->vg->name, lv->name, monitor ? "" : "un");
8ef6eb30
AK
1240 else
1241 break;
1242 sleep(1);
1243 }
1244
85a80e05
AK
1245 if (r)
1246 r = (monitored && monitor) || (!monitored && !monitor);
15d91f5a 1247 }
e6493477 1248
3e3d5d85
AK
1249 return r;
1250#else
15d91f5a 1251 return 1;
3e3d5d85 1252#endif
15d91f5a 1253}
15d91f5a 1254
0f2a4ca2
AK
1255struct detached_lv_data {
1256 struct logical_volume *lv_pre;
1257 struct lv_activate_opts *laopts;
1258 int *flush_required;
1259};
1260
1261static int _preload_detached_lv(struct cmd_context *cmd, struct logical_volume *lv, void *data)
1262{
1263 struct detached_lv_data *detached = data;
1264 struct lv_list *lvl_pre;
1265
1266 if ((lvl_pre = find_lv_in_vg(detached->lv_pre->vg, lv->name))) {
ee840ff1 1267 if (lv_is_visible(lvl_pre->lv) && lv_is_active(lv) && (!lv_is_cow(lv) || !lv_is_cow(lvl_pre->lv)) &&
0f2a4ca2
AK
1268 !_lv_preload(lvl_pre->lv, detached->laopts, detached->flush_required))
1269 return_0;
1270 }
1271
1272 return 1;
1273}
1274
658b5812 1275static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s,
81beded3 1276 struct lv_activate_opts *laopts, int error_if_not_suspended)
413cc918 1277{
df390f17
AK
1278 struct logical_volume *lv = NULL, *lv_pre = NULL, *pvmove_lv = NULL;
1279 struct lv_list *lvl_pre;
1280 struct seg_list *sl;
ee840ff1 1281 struct lv_segment *snap_seg;
199e490e 1282 struct lvinfo info;
eb91c4ee 1283 int r = 0, lockfs = 0, flush_required = 0;
0f2a4ca2 1284 struct detached_lv_data detached;
413cc918 1285
d1d9800e
AK
1286 if (!activation())
1287 return 1;
1288
8b888354 1289 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1290 goto_out;
8b888354 1291
7a593325 1292 /* Use precommitted metadata if present */
8b888354 1293 if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1)))
095bbca6 1294 goto_out;
413cc918 1295
2d6fcbf6
AK
1296 /* Ignore origin_only unless LV is origin in both old and new metadata */
1297 if (!lv_is_origin(lv) || !lv_is_origin(lv_pre))
81beded3 1298 laopts->origin_only = 0;
2d6fcbf6 1299
20c5fcf7 1300 if (test_mode()) {
81beded3 1301 _skip("Suspending %s%s.", lv->name, laopts->origin_only ? " origin without snapshots" : "");
095bbca6
MB
1302 r = 1;
1303 goto out;
20c5fcf7
AK
1304 }
1305
81beded3 1306 if (!lv_info(cmd, lv, laopts->origin_only, &info, 0, 0))
095bbca6 1307 goto_out;
41967a02 1308
095bbca6 1309 if (!info.exists || info.suspended) {
63ae0d14
MB
1310 if (!error_if_not_suspended) {
1311 r = 1;
1312 if (info.suspended)
df390f17 1313 critical_section_inc(cmd, "already suspended");
63ae0d14 1314 }
095bbca6
MB
1315 goto out;
1316 }
914c9723 1317
9249fb12
ZK
1318 if (!lv_read_replicator_vgs(lv))
1319 goto_out;
1320
c1fdeec9
MB
1321 lv_calculate_readahead(lv, NULL);
1322
df390f17 1323 /*
3a8eb387 1324 * Preload devices for the LV.
df390f17
AK
1325 * If the PVMOVE LV is being removed, it's only present in the old
1326 * metadata and not the new, so we must explicitly add the new
1327 * tables for all the changed LVs here, as the relationships
1328 * are not found by walking the new metadata.
1329 */
3a8eb387
AK
1330 if (!(lv_pre->status & LOCKED) &&
1331 (lv->status & LOCKED) &&
1332 (pvmove_lv = find_pvmove_lv_in_lv(lv))) {
1333 /* Preload all the LVs above the PVMOVE LV */
1334 dm_list_iterate_items(sl, &pvmove_lv->segs_using_this_lv) {
1335 if (!(lvl_pre = find_lv_in_vg(lv_pre->vg, sl->seg->lv->name))) {
ee840ff1 1336 log_error(INTERNAL_ERROR "LV %s missing from preload metadata", sl->seg->lv->name);
df390f17
AK
1337 goto out;
1338 }
81beded3 1339 if (!_lv_preload(lvl_pre->lv, laopts, &flush_required))
df390f17 1340 goto_out;
3a8eb387
AK
1341 }
1342 /* Now preload the PVMOVE LV itself */
1343 if (!(lvl_pre = find_lv_in_vg(lv_pre->vg, pvmove_lv->name))) {
ee840ff1 1344 log_error(INTERNAL_ERROR "LV %s missing from preload metadata", pvmove_lv->name);
3a8eb387
AK
1345 goto out;
1346 }
1347 if (!_lv_preload(lvl_pre->lv, laopts, &flush_required))
1348 goto_out;
1349 } else {
1350 if (!_lv_preload(lv_pre, laopts, &flush_required))
1351 /* FIXME Revert preloading */
1352 goto_out;
0f2a4ca2 1353
3a8eb387
AK
1354 /*
1355 * Search for existing LVs that have become detached and preload them.
1356 */
1357 detached.lv_pre = lv_pre;
1358 detached.laopts = laopts;
1359 detached.flush_required = &flush_required;
0f2a4ca2 1360
3a8eb387
AK
1361 if (!for_each_sub_lv(cmd, lv, &_preload_detached_lv, &detached))
1362 goto_out;
ee840ff1
AK
1363
1364 /*
1365 * Preload any snapshots that are being removed.
1366 */
1367 if (!laopts->origin_only && lv_is_origin(lv)) {
1368 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
a73e9a6c
AK
1369 if (!(lvl_pre = find_lv_in_vg_by_lvid(lv_pre->vg, &snap_seg->cow->lvid))) {
1370 log_error(INTERNAL_ERROR "LV %s (%s) missing from preload metadata",
1371 snap_seg->cow->name, snap_seg->cow->lvid.id[1].uuid);
ee840ff1
AK
1372 goto out;
1373 }
1374 if (!lv_is_cow(lvl_pre->lv) &&
1375 !_lv_preload(lvl_pre->lv, laopts, &flush_required))
1376 goto_out;
1377 }
1378 }
5f4b2acf
AK
1379 }
1380
81beded3 1381 if (!monitor_dev_for_events(cmd, lv, laopts, 0))
e24e7130 1382 /* FIXME Consider aborting here */
ed09d7e3
AK
1383 stack;
1384
df390f17
AK
1385 critical_section_inc(cmd, "suspending");
1386 if (pvmove_lv)
1387 critical_section_inc(cmd, "suspending pvmove LV");
9cd3426d 1388
81beded3 1389 if (!laopts->origin_only &&
2d6fcbf6 1390 (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)))
9cd3426d
AK
1391 lockfs = 1;
1392
df390f17
AK
1393 /*
1394 * Suspending an LV directly above a PVMOVE LV also
1395 * suspends other LVs using that same PVMOVE LV.
1396 * FIXME Remove this and delay the 'clear node' until
1397 * after the code knows whether there's a different
1398 * inactive table to load or not instead so lv_suspend
1399 * can be called separately for each LV safely.
1400 */
1401 if ((lv_pre->vg->status & PRECOMMITTED) &&
1402 (lv_pre->status & LOCKED) && find_pvmove_lv_in_lv(lv_pre)) {
81beded3 1403 if (!_lv_suspend_lv(lv_pre, laopts, lockfs, flush_required)) {
df390f17
AK
1404 critical_section_dec(cmd, "failed precommitted suspend");
1405 if (pvmove_lv)
1406 critical_section_dec(cmd, "failed precommitted suspend (pvmove)");
1407 goto_out;
1408 }
1409 } else {
1410 /* Normal suspend */
81beded3 1411 if (!_lv_suspend_lv(lv, laopts, lockfs, flush_required)) {
df390f17
AK
1412 critical_section_dec(cmd, "failed suspend");
1413 if (pvmove_lv)
1414 critical_section_dec(cmd, "failed suspend (pvmove)");
1415 goto_out;
1416 }
914c9723 1417 }
8c013da4 1418
095bbca6
MB
1419 r = 1;
1420out:
1421 if (lv_pre)
077a6755 1422 release_vg(lv_pre->vg);
9249fb12
ZK
1423 if (lv) {
1424 lv_release_replicator_vgs(lv);
077a6755 1425 release_vg(lv->vg);
9249fb12 1426 }
095bbca6
MB
1427
1428 return r;
413cc918
AK
1429}
1430
658b5812 1431/* Returns success if the device is not active */
2d6fcbf6 1432int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
658b5812 1433{
81beded3
ZK
1434 struct lv_activate_opts laopts = { .origin_only = origin_only };
1435
1436 return _lv_suspend(cmd, lvid_s, &laopts, 0);
658b5812
AK
1437}
1438
2d6fcbf6
AK
1439/* No longer used */
1440/***********
658b5812
AK
1441int lv_suspend(struct cmd_context *cmd, const char *lvid_s)
1442{
1443 return _lv_suspend(cmd, lvid_s, 1);
1444}
2d6fcbf6 1445***********/
658b5812 1446
c054e7cc
JEB
1447 /*
1448 * _lv_resume
1449 * @cmd
1450 * @lvid_s
1451 * @origin_only
1452 * @exclusive: This parameter only has an affect in cluster-context.
1453 * It forces local target type to be used (instead of
1454 * cluster-aware type).
1455 * @error_if_not_active
1456 */
658b5812 1457static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
81beded3 1458 struct lv_activate_opts *laopts, int error_if_not_active)
413cc918
AK
1459{
1460 struct logical_volume *lv;
199e490e 1461 struct lvinfo info;
095bbca6 1462 int r = 0;
413cc918 1463
d1d9800e
AK
1464 if (!activation())
1465 return 1;
1466
7a593325 1467 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
75b37a11 1468 goto_out;
413cc918 1469
2d6fcbf6 1470 if (!lv_is_origin(lv))
81beded3 1471 laopts->origin_only = 0;
2d6fcbf6 1472
20c5fcf7 1473 if (test_mode()) {
10d0d9c7
AK
1474 _skip("Resuming %s%s%s.", lv->name, laopts->origin_only ? " without snapshots" : "",
1475 laopts->revert ? " (reverting)" : "");
095bbca6
MB
1476 r = 1;
1477 goto out;
20c5fcf7
AK
1478 }
1479
10d0d9c7 1480 log_debug("Resuming LV %s/%s%s%s%s.", lv->vg->name, lv->name,
7df72b3c 1481 error_if_not_active ? "" : " if active",
10d0d9c7
AK
1482 laopts->origin_only ? " without snapshots" : "",
1483 laopts->revert ? " (reverting)" : "");
7df72b3c 1484
81beded3 1485 if (!lv_info(cmd, lv, laopts->origin_only, &info, 0, 0))
095bbca6 1486 goto_out;
41967a02 1487
095bbca6 1488 if (!info.exists || !info.suspended) {
4ec2ae86
ZK
1489 if (error_if_not_active)
1490 goto_out;
1491 r = 1;
df390f17
AK
1492 if (!info.suspended)
1493 critical_section_dec(cmd, "already resumed");
4ec2ae86 1494 goto out;
095bbca6 1495 }
914c9723 1496
a18dcfb5
AK
1497 laopts->read_only = _passes_readonly_filter(cmd, lv);
1498
81beded3 1499 if (!_lv_activate_lv(lv, laopts))
75b37a11 1500 goto_out;
914c9723 1501
df390f17 1502 critical_section_dec(cmd, "resumed");
413cc918 1503
81beded3 1504 if (!monitor_dev_for_events(cmd, lv, laopts, 1))
ed09d7e3 1505 stack;
15d91f5a 1506
095bbca6
MB
1507 r = 1;
1508out:
1509 if (lv)
077a6755 1510 release_vg(lv->vg);
095bbca6
MB
1511
1512 return r;
413cc918
AK
1513}
1514
658b5812 1515/* Returns success if the device is not active */
c054e7cc 1516int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
10d0d9c7 1517 unsigned origin_only, unsigned exclusive, unsigned revert)
658b5812 1518{
81beded3
ZK
1519 struct lv_activate_opts laopts = {
1520 .origin_only = origin_only,
1521 /*
1522 * When targets are activated exclusively in a cluster, the
1523 * non-clustered target should be used. This only happens
1524 * if exclusive is set.
1525 */
10d0d9c7
AK
1526 .exclusive = exclusive,
1527 .revert = revert
81beded3
ZK
1528 };
1529
1530 return _lv_resume(cmd, lvid_s, &laopts, 0);
658b5812
AK
1531}
1532
2d6fcbf6 1533int lv_resume(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
658b5812 1534{
81beded3
ZK
1535 struct lv_activate_opts laopts = { .origin_only = origin_only, };
1536
1537 return _lv_resume(cmd, lvid_s, &laopts, 1);
658b5812
AK
1538}
1539
64a95010
AK
1540static int _lv_has_open_snapshots(struct logical_volume *lv)
1541{
1542 struct lv_segment *snap_seg;
1543 struct lvinfo info;
1544 int r = 0;
1545
1546 dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) {
2d6fcbf6 1547 if (!lv_info(lv->vg->cmd, snap_seg->cow, 0, &info, 1, 0)) {
64a95010
AK
1548 r = 1;
1549 continue;
1550 }
1551
1552 if (info.exists && info.open_count) {
1553 log_error("LV %s/%s has open snapshot %s: "
1554 "not deactivating", lv->vg->name, lv->name,
1555 snap_seg->cow->name);
1556 r = 1;
1557 }
1558 }
1559
1560 return r;
1561}
1562
be326a2f 1563int lv_deactivate(struct cmd_context *cmd, const char *lvid_s)
f4cbeaf0
AK
1564{
1565 struct logical_volume *lv;
199e490e 1566 struct lvinfo info;
095bbca6 1567 int r = 0;
f4cbeaf0 1568
d1d9800e
AK
1569 if (!activation())
1570 return 1;
1571
7a593325 1572 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1573 goto out;
f4cbeaf0 1574
20c5fcf7
AK
1575 if (test_mode()) {
1576 _skip("Deactivating '%s'.", lv->name);
095bbca6
MB
1577 r = 1;
1578 goto out;
20c5fcf7
AK
1579 }
1580
7df72b3c
AK
1581 log_debug("Deactivating %s/%s.", lv->vg->name, lv->name);
1582
2d6fcbf6 1583 if (!lv_info(cmd, lv, 0, &info, 1, 0))
095bbca6 1584 goto_out;
41967a02 1585
095bbca6
MB
1586 if (!info.exists) {
1587 r = 1;
1588 goto out;
1589 }
f4cbeaf0 1590
64a95010 1591 if (lv_is_visible(lv)) {
125712be
PR
1592 if (!lv_check_not_in_use(cmd, lv, &info))
1593 goto_out;
1594
64a95010
AK
1595 if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1596 goto_out;
0cf96f33
AK
1597 }
1598
9249fb12
ZK
1599 if (!lv_read_replicator_vgs(lv))
1600 goto_out;
1601
c1fdeec9
MB
1602 lv_calculate_readahead(lv, NULL);
1603
81beded3 1604 if (!monitor_dev_for_events(cmd, lv, NULL, 0))
ed09d7e3 1605 stack;
15d91f5a 1606
df390f17 1607 critical_section_inc(cmd, "deactivating");
914c9723 1608 r = _lv_deactivate(lv);
df390f17 1609 critical_section_dec(cmd, "deactivated");
914c9723 1610
401a40d9 1611 if (!lv_info(cmd, lv, 0, &info, 0, 0) || info.exists)
89a6cdfd 1612 r = 0;
095bbca6 1613out:
9249fb12
ZK
1614 if (lv) {
1615 lv_release_replicator_vgs(lv);
077a6755 1616 release_vg(lv->vg);
9249fb12 1617 }
095bbca6 1618
914c9723 1619 return r;
f4cbeaf0
AK
1620}
1621
658b5812
AK
1622/* Test if LV passes filter */
1623int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s,
1624 int *activate_lv)
1625{
1626 struct logical_volume *lv;
095bbca6 1627 int r = 0;
658b5812 1628
095bbca6
MB
1629 if (!activation()) {
1630 *activate_lv = 1;
1631 return 1;
1632 }
658b5812 1633
424dd43e 1634 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1635 goto out;
658b5812
AK
1636
1637 if (!_passes_activation_filter(cmd, lv)) {
f7e3a19f
PR
1638 log_verbose("Not activating %s/%s since it does not pass "
1639 "activation filter.", lv->vg->name, lv->name);
658b5812 1640 *activate_lv = 0;
095bbca6
MB
1641 } else
1642 *activate_lv = 1;
1643 r = 1;
1644out:
1645 if (lv)
077a6755 1646 release_vg(lv->vg);
658b5812 1647
095bbca6 1648 return r;
658b5812
AK
1649}
1650
07d31831 1651static int _lv_activate(struct cmd_context *cmd, const char *lvid_s,
81beded3 1652 struct lv_activate_opts *laopts, int filter)
f4cbeaf0
AK
1653{
1654 struct logical_volume *lv;
199e490e 1655 struct lvinfo info;
095bbca6 1656 int r = 0;
f4cbeaf0 1657
d1d9800e
AK
1658 if (!activation())
1659 return 1;
1660
424dd43e 1661 if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
095bbca6 1662 goto out;
f4cbeaf0 1663
658b5812 1664 if (filter && !_passes_activation_filter(cmd, lv)) {
f7e3a19f
PR
1665 log_error("Not activating %s/%s since it does not pass "
1666 "activation filter.", lv->vg->name, lv->name);
095bbca6 1667 goto out;
de17d760
AK
1668 }
1669
cda35408 1670 if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) {
8c5bcdab
AK
1671 log_error("Refusing activation of partial LV %s. Use --partial to override.",
1672 lv->name);
095bbca6 1673 goto_out;
8c5bcdab
AK
1674 }
1675
b4048242
PR
1676 if (lv_has_unknown_segments(lv)) {
1677 log_error("Refusing activation of LV %s containing "
1678 "an unrecognised segment.", lv->name);
1679 goto_out;
1680 }
1681
20c5fcf7
AK
1682 if (test_mode()) {
1683 _skip("Activating '%s'.", lv->name);
095bbca6
MB
1684 r = 1;
1685 goto out;
20c5fcf7
AK
1686 }
1687
a18dcfb5
AK
1688 if (filter)
1689 laopts->read_only = _passes_readonly_filter(cmd, lv);
1690
1691 log_debug("Activating %s/%s%s%s.", lv->vg->name, lv->name,
1692 laopts->exclusive ? " exclusively" : "",
1693 laopts->read_only ? " read-only" : "");
7df72b3c 1694
2d6fcbf6 1695 if (!lv_info(cmd, lv, 0, &info, 0, 0))
095bbca6 1696 goto_out;
8c013da4 1697
a18dcfb5
AK
1698 /*
1699 * Nothing to do?
1700 */
1701 if (info.exists && !info.suspended && info.live_table &&
1702 (info.read_only == read_only_lv(lv, laopts))) {
095bbca6
MB
1703 r = 1;
1704 goto out;
1705 }
f4cbeaf0 1706
9249fb12
ZK
1707 if (!lv_read_replicator_vgs(lv))
1708 goto_out;
1709
c1fdeec9
MB
1710 lv_calculate_readahead(lv, NULL);
1711
df390f17 1712 critical_section_inc(cmd, "activating");
81beded3 1713 if (!(r = _lv_activate_lv(lv, laopts)))
75b37a11 1714 stack;
df390f17 1715 critical_section_dec(cmd, "activated");
914c9723 1716
81beded3 1717 if (r && !monitor_dev_for_events(cmd, lv, laopts, 1))
ed09d7e3 1718 stack;
15d91f5a 1719
095bbca6 1720out:
9249fb12
ZK
1721 if (lv) {
1722 lv_release_replicator_vgs(lv);
077a6755 1723 release_vg(lv->vg);
9249fb12 1724 }
095bbca6 1725
914c9723 1726 return r;
f4cbeaf0 1727}
199e490e 1728
658b5812 1729/* Activate LV */
07d31831 1730int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive)
658b5812 1731{
81beded3
ZK
1732 struct lv_activate_opts laopts = { .exclusive = exclusive };
1733
1734 if (!_lv_activate(cmd, lvid_s, &laopts, 0))
75b37a11
AK
1735 return_0;
1736
1737 return 1;
658b5812
AK
1738}
1739
1740/* Activate LV only if it passes filter */
07d31831 1741int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive)
658b5812 1742{
81beded3
ZK
1743 struct lv_activate_opts laopts = { .exclusive = exclusive };
1744
1745 if (!_lv_activate(cmd, lvid_s, &laopts, 1))
75b37a11
AK
1746 return_0;
1747
1748 return 1;
658b5812
AK
1749}
1750
f7dd6d84
AK
1751int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv)
1752{
f7dd6d84
AK
1753 int r = 1;
1754
8b076648 1755 if (!lv) {
2262b320 1756 r = dm_mknodes(NULL);
8b076648
AK
1757 fs_unlock();
1758 return r;
1759 }
1760
ab9663f3
MB
1761 if (!activation())
1762 return 1;
f7dd6d84 1763
ab9663f3 1764 r = dev_manager_mknodes(lv);
f7dd6d84
AK
1765
1766 fs_unlock();
1767
1768 return r;
1769}
1770
352a99b9
AK
1771/*
1772 * Does PV use VG somewhere in its construction?
1773 * Returns 1 on failure.
1774 */
898e6f8e 1775int pv_uses_vg(struct physical_volume *pv,
3e3d5d85 1776 struct volume_group *vg)
352a99b9 1777{
352a99b9
AK
1778 if (!activation())
1779 return 0;
1780
1781 if (!dm_is_dm_major(MAJOR(pv->dev->dev)))
1782 return 0;
1783
898e6f8e 1784 return dev_manager_device_uses_vg(pv->dev, vg);
352a99b9
AK
1785}
1786
2293567c
AK
1787void activation_release(void)
1788{
1789 dev_manager_release();
1790}
1791
914c9723
AK
1792void activation_exit(void)
1793{
1794 dev_manager_exit();
1795}
199e490e 1796#endif
This page took 0.327035 seconds and 5 git commands to generate.