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