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