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