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