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