]>
Commit | Line | Data |
---|---|---|
b1713d28 | 1 | /* |
67cdbd7e | 2 | * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. |
c8669f6b | 3 | * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved. |
b1713d28 | 4 | * |
6606c3ae AK |
5 | * This file is part of LVM2. |
6 | * | |
7 | * This copyrighted material is made available to anyone wishing to use, | |
8 | * modify, copy, or redistribute it subject to the terms and conditions | |
be684599 | 9 | * of the GNU Lesser General Public License v.2.1. |
6606c3ae | 10 | * |
be684599 | 11 | * You should have received a copy of the GNU Lesser General Public License |
6606c3ae AK |
12 | * along with this program; if not, write to the Free Software Foundation, |
13 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
b1713d28 JT |
14 | */ |
15 | ||
d1d9800e | 16 | #include "lib.h" |
a381c45a | 17 | #include "metadata.h" |
b1713d28 | 18 | #include "activate.h" |
914c9723 | 19 | #include "memlock.h" |
78125be9 | 20 | #include "display.h" |
f7a14956 | 21 | #include "fs.h" |
f894b4b1 | 22 | #include "lvm-exec.h" |
7d1552c9 | 23 | #include "lvm-file.h" |
41b2fd5f | 24 | #include "lvm-string.h" |
413cc918 | 25 | #include "toolcontext.h" |
de6c9183 | 26 | #include "dev_manager.h" |
de17d760 | 27 | #include "str_list.h" |
4922197a | 28 | #include "config.h" |
352a99b9 | 29 | #include "filter.h" |
15d91f5a | 30 | #include "segtype.h" |
12137231 JT |
31 | |
32 | #include <limits.h> | |
5986ec94 | 33 | #include <fcntl.h> |
914c9723 | 34 | #include <unistd.h> |
12137231 | 35 | |
6d52fb46 | 36 | #define _skip(fmt, args...) log_very_verbose("Skipping: " fmt , ## args) |
b1713d28 | 37 | |
7d1552c9 AK |
38 | int lvm1_present(struct cmd_context *cmd) |
39 | { | |
40 | char path[PATH_MAX]; | |
41 | ||
0550c1b6 | 42 | if (dm_snprintf(path, sizeof(path), "%s/lvm/global", cmd->proc_dir) |
7d1552c9 AK |
43 | < 0) { |
44 | log_error("LVM1 proc global snprintf failed"); | |
45 | return 0; | |
46 | } | |
47 | ||
48 | if (path_exists(path)) | |
49 | return 1; | |
50 | else | |
51 | return 0; | |
52 | } | |
53 | ||
6c81ed26 | 54 | int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg, |
2c44337b | 55 | struct dm_list *modules) |
6c81ed26 AK |
56 | { |
57 | unsigned int s; | |
58 | struct lv_segment *seg2, *snap_seg; | |
2c44337b | 59 | struct dm_list *snh; |
6c81ed26 AK |
60 | |
61 | if (seg->segtype->ops->modules_needed && | |
62 | !seg->segtype->ops->modules_needed(mem, seg, modules)) { | |
63 | log_error("module string allocation failed"); | |
64 | return 0; | |
65 | } | |
66 | ||
67 | if (lv_is_origin(seg->lv)) | |
2c44337b | 68 | dm_list_iterate(snh, &seg->lv->snapshot_segs) |
6c81ed26 | 69 | if (!list_lv_modules(mem, |
2c44337b | 70 | dm_list_struct_base(snh, |
6c81ed26 AK |
71 | struct lv_segment, |
72 | origin_list)->cow, | |
73 | modules)) | |
74 | return_0; | |
75 | ||
76 | if (lv_is_cow(seg->lv)) { | |
77 | snap_seg = find_cow(seg->lv); | |
78 | if (snap_seg->segtype->ops->modules_needed && | |
79 | !snap_seg->segtype->ops->modules_needed(mem, snap_seg, | |
80 | modules)) { | |
81 | log_error("snap_seg module string allocation failed"); | |
82 | return 0; | |
83 | } | |
84 | } | |
85 | ||
86 | for (s = 0; s < seg->area_count; s++) { | |
87 | switch (seg_type(seg, s)) { | |
88 | case AREA_LV: | |
89 | seg2 = find_seg_by_le(seg_lv(seg, s), seg_le(seg, s)); | |
90 | if (seg2 && !list_segment_modules(mem, seg2, modules)) | |
91 | return_0; | |
92 | break; | |
93 | case AREA_PV: | |
94 | case AREA_UNASSIGNED: | |
95 | ; | |
96 | } | |
97 | } | |
98 | ||
99 | return 1; | |
100 | } | |
101 | ||
102 | int list_lv_modules(struct dm_pool *mem, const struct logical_volume *lv, | |
2c44337b | 103 | struct dm_list *modules) |
6c81ed26 AK |
104 | { |
105 | struct lv_segment *seg; | |
106 | ||
2c44337b | 107 | dm_list_iterate_items(seg, &lv->segments) |
6c81ed26 AK |
108 | if (!list_segment_modules(mem, seg, modules)) |
109 | return_0; | |
110 | ||
111 | return 1; | |
112 | } | |
113 | ||
199e490e AK |
114 | #ifndef DEVMAPPER_SUPPORT |
115 | void set_activation(int act) | |
116 | { | |
f2046e0a AK |
117 | static int warned = 0; |
118 | ||
119 | if (warned || !act) | |
120 | return; | |
121 | ||
122 | log_error("Compiled without libdevmapper support. " | |
123 | "Can't enable activation."); | |
124 | ||
125 | warned = 1; | |
199e490e AK |
126 | } |
127 | int activation(void) | |
128 | { | |
129 | return 0; | |
130 | } | |
131 | int library_version(char *version, size_t size) | |
132 | { | |
133 | return 0; | |
134 | } | |
135 | int driver_version(char *version, size_t size) | |
136 | { | |
137 | return 0; | |
138 | } | |
bbf83db1 AK |
139 | int target_version(const char *target_name, uint32_t *maj, |
140 | uint32_t *min, uint32_t *patchlevel) | |
141 | { | |
142 | return 0; | |
143 | } | |
ed82bfd2 AK |
144 | int target_present(struct cmd_context *cmd, const char *target_name, |
145 | int use_modprobe) | |
d1f4953a AK |
146 | { |
147 | return 0; | |
148 | } | |
f894b4b1 | 149 | int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info, |
a6b22cf3 | 150 | int with_open_count, int with_read_ahead) |
199e490e AK |
151 | { |
152 | return 0; | |
153 | } | |
4bd9480d | 154 | int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, |
a6b22cf3 | 155 | struct lvinfo *info, int with_open_count, int with_read_ahead) |
4bd9480d AK |
156 | { |
157 | return 0; | |
158 | } | |
78ad1549 AK |
159 | int lv_snapshot_percent(const struct logical_volume *lv, float *percent, |
160 | percent_range_t *percent_range) | |
199e490e AK |
161 | { |
162 | return 0; | |
163 | } | |
472ac5bd | 164 | int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv, |
78ad1549 AK |
165 | int wait, float *percent, percent_range_t *percent_range, |
166 | uint32_t *event_nr) | |
10b29b8d AK |
167 | { |
168 | return 0; | |
169 | } | |
199e490e AK |
170 | int lvs_in_vg_activated(struct volume_group *vg) |
171 | { | |
172 | return 0; | |
173 | } | |
1bf5aae9 | 174 | int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg) |
b45c9f49 AK |
175 | { |
176 | return 0; | |
177 | } | |
199e490e AK |
178 | int lvs_in_vg_opened(struct volume_group *vg) |
179 | { | |
180 | return 0; | |
181 | } | |
658b5812 AK |
182 | int lv_suspend(struct cmd_context *cmd, const char *lvid_s) |
183 | { | |
184 | return 1; | |
185 | } | |
199e490e AK |
186 | int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s) |
187 | { | |
188 | return 1; | |
189 | } | |
658b5812 AK |
190 | int lv_resume(struct cmd_context *cmd, const char *lvid_s) |
191 | { | |
192 | return 1; | |
193 | } | |
199e490e AK |
194 | int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s) |
195 | { | |
196 | return 1; | |
197 | } | |
198 | int lv_deactivate(struct cmd_context *cmd, const char *lvid_s) | |
199 | { | |
200 | return 1; | |
201 | } | |
658b5812 AK |
202 | int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s, |
203 | int *activate_lv) | |
204 | { | |
205 | return 1; | |
206 | } | |
07d31831 | 207 | int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
199e490e AK |
208 | { |
209 | return 1; | |
210 | } | |
07d31831 | 211 | int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 AK |
212 | { |
213 | return 1; | |
214 | } | |
f7dd6d84 AK |
215 | |
216 | int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv) | |
217 | { | |
218 | return 1; | |
219 | } | |
220 | ||
0ce83a83 | 221 | int pv_uses_vg(struct physical_volume *pv, |
3e3d5d85 | 222 | struct volume_group *vg) |
352a99b9 AK |
223 | { |
224 | return 0; | |
225 | } | |
226 | ||
2293567c AK |
227 | void activation_release(void) |
228 | { | |
229 | return; | |
230 | } | |
231 | ||
914c9723 AK |
232 | void activation_exit(void) |
233 | { | |
234 | return; | |
235 | } | |
199e490e AK |
236 | |
237 | #else /* DEVMAPPER_SUPPORT */ | |
238 | ||
d1d9800e AK |
239 | static int _activation = 1; |
240 | ||
8ef2b021 | 241 | void set_activation(int act) |
d1d9800e | 242 | { |
8ef2b021 | 243 | if (act == _activation) |
d1d9800e AK |
244 | return; |
245 | ||
8ef2b021 | 246 | _activation = act; |
d1d9800e AK |
247 | if (_activation) |
248 | log_verbose("Activation enabled. Device-mapper kernel " | |
249 | "driver will be used."); | |
250 | else | |
e7ddf416 | 251 | log_warn("WARNING: Activation disabled. No device-mapper " |
bfe2b548 | 252 | "interaction will be attempted."); |
d1d9800e AK |
253 | } |
254 | ||
8ef2b021 | 255 | int activation(void) |
d1d9800e AK |
256 | { |
257 | return _activation; | |
258 | } | |
259 | ||
de17d760 AK |
260 | static int _passes_activation_filter(struct cmd_context *cmd, |
261 | struct logical_volume *lv) | |
262 | { | |
25579907 | 263 | const struct config_node *cn; |
de17d760 AK |
264 | struct config_value *cv; |
265 | char *str; | |
266 | char path[PATH_MAX]; | |
267 | ||
2293567c | 268 | if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) { |
25579907 | 269 | /* If no host tags defined, activate */ |
2c44337b | 270 | if (dm_list_empty(&cmd->tags)) |
de17d760 AK |
271 | return 1; |
272 | ||
273 | /* If any host tag matches any LV or VG tag, activate */ | |
274 | if (str_list_match_list(&cmd->tags, &lv->tags) || | |
275 | str_list_match_list(&cmd->tags, &lv->vg->tags)) | |
276 | return 1; | |
277 | ||
278 | /* Don't activate */ | |
279 | return 0; | |
280 | } | |
281 | ||
282 | for (cv = cn->v; cv; cv = cv->next) { | |
283 | if (cv->type != CFG_STRING) { | |
284 | log_error("Ignoring invalid string in config file " | |
285 | "activation/volume_list"); | |
286 | continue; | |
287 | } | |
288 | str = cv->v.str; | |
289 | if (!*str) { | |
290 | log_error("Ignoring empty string in config file " | |
291 | "activation/volume_list"); | |
292 | continue; | |
293 | } | |
294 | ||
295 | /* Tag? */ | |
296 | if (*str == '@') { | |
297 | str++; | |
298 | if (!*str) { | |
299 | log_error("Ignoring empty tag in config file " | |
300 | "activation/volume_list"); | |
301 | continue; | |
302 | } | |
303 | /* If any host tag matches any LV or VG tag, activate */ | |
304 | if (!strcmp(str, "*")) { | |
305 | if (str_list_match_list(&cmd->tags, &lv->tags) | |
306 | || str_list_match_list(&cmd->tags, | |
307 | &lv->vg->tags)) | |
308 | return 1; | |
309 | else | |
310 | continue; | |
311 | } | |
312 | /* If supplied tag matches LV or VG tag, activate */ | |
313 | if (str_list_match_item(&lv->tags, str) || | |
314 | str_list_match_item(&lv->vg->tags, str)) | |
315 | return 1; | |
316 | else | |
317 | continue; | |
318 | } | |
13835b5f | 319 | if (!strchr(str, '/')) { |
de17d760 AK |
320 | /* vgname supplied */ |
321 | if (!strcmp(str, lv->vg->name)) | |
322 | return 1; | |
323 | else | |
324 | continue; | |
325 | } | |
326 | /* vgname/lvname */ | |
0550c1b6 | 327 | if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name, |
de17d760 | 328 | lv->name) < 0) { |
0550c1b6 | 329 | log_error("dm_snprintf error from %s/%s", lv->vg->name, |
de17d760 AK |
330 | lv->name); |
331 | continue; | |
332 | } | |
333 | if (!strcmp(path, str)) | |
334 | return 1; | |
335 | } | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
fae0c576 AK |
340 | int library_version(char *version, size_t size) |
341 | { | |
d1d9800e AK |
342 | if (!activation()) |
343 | return 0; | |
344 | ||
f894b4b1 | 345 | return dm_get_library_version(version, size); |
fae0c576 AK |
346 | } |
347 | ||
fae0c576 AK |
348 | int driver_version(char *version, size_t size) |
349 | { | |
d1d9800e AK |
350 | if (!activation()) |
351 | return 0; | |
352 | ||
fae0c576 | 353 | log_very_verbose("Getting driver version"); |
fae0c576 | 354 | |
f894b4b1 | 355 | return dm_driver_version(version, size); |
fae0c576 AK |
356 | } |
357 | ||
bbf83db1 AK |
358 | int target_version(const char *target_name, uint32_t *maj, |
359 | uint32_t *min, uint32_t *patchlevel) | |
d1f4953a AK |
360 | { |
361 | int r = 0; | |
362 | struct dm_task *dmt; | |
363 | struct dm_versions *target, *last_target; | |
364 | ||
d1f4953a | 365 | log_very_verbose("Getting target version for %s", target_name); |
5f4b2acf AK |
366 | if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS))) |
367 | return_0; | |
d1f4953a AK |
368 | |
369 | if (!dm_task_run(dmt)) { | |
370 | log_debug("Failed to get %s target version", target_name); | |
371 | /* Assume this was because LIST_VERSIONS isn't supported */ | |
372 | return 1; | |
373 | } | |
374 | ||
375 | target = dm_task_get_versions(dmt); | |
376 | ||
377 | do { | |
378 | last_target = target; | |
379 | ||
380 | if (!strcmp(target_name, target->name)) { | |
381 | r = 1; | |
bbf83db1 AK |
382 | *maj = target->version[0]; |
383 | *min = target->version[1]; | |
384 | *patchlevel = target->version[2]; | |
d1f4953a AK |
385 | goto out; |
386 | } | |
387 | ||
388 | target = (void *) target + target->next; | |
389 | } while (last_target != target); | |
390 | ||
391 | out: | |
392 | dm_task_destroy(dmt); | |
393 | ||
394 | return r; | |
395 | } | |
396 | ||
ed82bfd2 | 397 | int module_present(struct cmd_context *cmd, const char *target_name) |
f894b4b1 | 398 | { |
5619c629 | 399 | int ret = 0; |
03b49fe1 | 400 | #ifdef MODPROBE_CMD |
f894b4b1 | 401 | char module[128]; |
c8669f6b | 402 | const char *argv[3]; |
5619c629 MB |
403 | |
404 | if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) < 0) { | |
405 | log_error("module_present module name too long: %s", | |
406 | target_name); | |
407 | return 0; | |
408 | } | |
409 | ||
c8669f6b ZK |
410 | argv[0] = MODPROBE_CMD; |
411 | argv[1] = module; | |
412 | argv[2] = NULL; | |
413 | ||
ed82bfd2 | 414 | ret = exec_cmd(cmd, argv); |
03b49fe1 | 415 | #endif |
5619c629 MB |
416 | return ret; |
417 | } | |
418 | ||
ed82bfd2 AK |
419 | int target_present(struct cmd_context *cmd, const char *target_name, |
420 | int use_modprobe) | |
5619c629 MB |
421 | { |
422 | uint32_t maj, min, patchlevel; | |
f894b4b1 AK |
423 | |
424 | if (!activation()) | |
425 | return 0; | |
426 | ||
427 | #ifdef MODPROBE_CMD | |
5f4b2acf | 428 | if (use_modprobe) { |
bbf83db1 | 429 | if (target_version(target_name, &maj, &min, &patchlevel)) |
5f4b2acf | 430 | return 1; |
f894b4b1 | 431 | |
ed82bfd2 | 432 | if (!module_present(cmd, target_name)) |
5f4b2acf | 433 | return_0; |
f894b4b1 AK |
434 | } |
435 | #endif | |
436 | ||
bbf83db1 | 437 | return target_version(target_name, &maj, &min, &patchlevel); |
f894b4b1 AK |
438 | } |
439 | ||
de6c9183 JT |
440 | /* |
441 | * Returns 1 if info structure populated, else 0 on failure. | |
442 | */ | |
f894b4b1 | 443 | static int _lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int with_mknodes, |
a6b22cf3 | 444 | struct lvinfo *info, int with_open_count, int with_read_ahead, unsigned by_uuid_only) |
37ed70b9 | 445 | { |
199e490e | 446 | struct dm_info dminfo; |
b45c9f49 | 447 | char *name = NULL; |
4a624ca0 | 448 | |
d1d9800e AK |
449 | if (!activation()) |
450 | return 0; | |
451 | ||
b45c9f49 AK |
452 | if (!by_uuid_only && |
453 | !(name = build_dm_name(cmd->mem, lv->vg->name, lv->name, NULL))) | |
5f4b2acf | 454 | return_0; |
4a624ca0 | 455 | |
f894b4b1 | 456 | log_debug("Getting device info for %s", name); |
03b49fe1 | 457 | if (!dev_manager_info(lv->vg->cmd->mem, name, lv, with_mknodes, |
a6b22cf3 AK |
458 | with_open_count, with_read_ahead, &dminfo, |
459 | &info->read_ahead)) { | |
b45c9f49 AK |
460 | if (name) |
461 | dm_pool_free(cmd->mem, name); | |
5f4b2acf | 462 | return_0; |
f894b4b1 | 463 | } |
4a624ca0 | 464 | |
199e490e AK |
465 | info->exists = dminfo.exists; |
466 | info->suspended = dminfo.suspended; | |
467 | info->open_count = dminfo.open_count; | |
468 | info->major = dminfo.major; | |
469 | info->minor = dminfo.minor; | |
470 | info->read_only = dminfo.read_only; | |
5f4b2acf AK |
471 | info->live_table = dminfo.live_table; |
472 | info->inactive_table = dminfo.inactive_table; | |
199e490e | 473 | |
b45c9f49 AK |
474 | if (name) |
475 | dm_pool_free(cmd->mem, name); | |
476 | ||
f894b4b1 | 477 | return 1; |
de6c9183 | 478 | } |
a62ee8ad | 479 | |
f894b4b1 | 480 | int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info, |
a6b22cf3 | 481 | int with_open_count, int with_read_ahead) |
8c0388e4 | 482 | { |
a6b22cf3 | 483 | return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0); |
8c0388e4 AK |
484 | } |
485 | ||
4bd9480d | 486 | int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, |
a6b22cf3 | 487 | struct lvinfo *info, int with_open_count, int with_read_ahead) |
4bd9480d AK |
488 | { |
489 | struct logical_volume *lv; | |
490 | ||
7a593325 | 491 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
4bd9480d AK |
492 | return 0; |
493 | ||
a6b22cf3 | 494 | return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0); |
4bd9480d AK |
495 | } |
496 | ||
1951dba9 AL |
497 | /* |
498 | * Returns 1 if percent set, else 0 on failure. | |
499 | */ | |
78ad1549 AK |
500 | int lv_snapshot_percent(const struct logical_volume *lv, float *percent, |
501 | percent_range_t *percent_range) | |
1951dba9 AL |
502 | { |
503 | int r; | |
504 | struct dev_manager *dm; | |
505 | ||
d1d9800e AK |
506 | if (!activation()) |
507 | return 0; | |
508 | ||
5f4b2acf AK |
509 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
510 | return_0; | |
1951dba9 | 511 | |
78ad1549 | 512 | if (!(r = dev_manager_snapshot_percent(dm, lv, percent, percent_range))) |
1951dba9 | 513 | stack; |
c826c0d1 | 514 | |
1951dba9 AL |
515 | dev_manager_destroy(dm); |
516 | ||
517 | return r; | |
518 | } | |
519 | ||
10b29b8d | 520 | /* FIXME Merge with snapshot_percent */ |
472ac5bd | 521 | int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv, |
78ad1549 AK |
522 | int wait, float *percent, percent_range_t *percent_range, |
523 | uint32_t *event_nr) | |
10b29b8d AK |
524 | { |
525 | int r; | |
526 | struct dev_manager *dm; | |
b65b777d | 527 | struct lvinfo info; |
10b29b8d | 528 | |
876003dc AK |
529 | /* If mirrored LV is temporarily shrinked to 1 area (= linear), |
530 | * it should be considered in-sync. */ | |
2c44337b | 531 | if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) { |
876003dc AK |
532 | *percent = 100.0; |
533 | return 1; | |
534 | } | |
535 | ||
10b29b8d AK |
536 | if (!activation()) |
537 | return 0; | |
538 | ||
a6b22cf3 | 539 | if (!lv_info(cmd, lv, &info, 0, 0)) |
5f4b2acf | 540 | return_0; |
b65b777d AK |
541 | |
542 | if (!info.exists) | |
543 | return 0; | |
544 | ||
5f4b2acf AK |
545 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
546 | return_0; | |
10b29b8d | 547 | |
78ad1549 AK |
548 | if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, |
549 | percent_range, event_nr))) | |
10b29b8d AK |
550 | stack; |
551 | ||
552 | dev_manager_destroy(dm); | |
553 | ||
554 | return r; | |
555 | } | |
556 | ||
b45c9f49 AK |
557 | static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv, |
558 | unsigned by_uuid_only) | |
2ba80b43 | 559 | { |
199e490e | 560 | struct lvinfo info; |
2ba80b43 | 561 | |
a6b22cf3 | 562 | if (!_lv_info(cmd, lv, 0, &info, 0, 0, by_uuid_only)) { |
2ba80b43 | 563 | stack; |
de6c9183 | 564 | return -1; |
2ba80b43 JT |
565 | } |
566 | ||
de6c9183 | 567 | return info.exists; |
2ba80b43 JT |
568 | } |
569 | ||
f894b4b1 | 570 | static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv) |
5986ec94 | 571 | { |
199e490e | 572 | struct lvinfo info; |
5986ec94 | 573 | |
a6b22cf3 | 574 | if (!lv_info(cmd, lv, &info, 1, 0)) { |
5986ec94 | 575 | stack; |
de6c9183 | 576 | return -1; |
5986ec94 JT |
577 | } |
578 | ||
de6c9183 | 579 | return info.open_count; |
5986ec94 JT |
580 | } |
581 | ||
658b5812 | 582 | static int _lv_activate_lv(struct logical_volume *lv) |
b1713d28 | 583 | { |
6d52fb46 | 584 | int r; |
de6c9183 | 585 | struct dev_manager *dm; |
b1713d28 | 586 | |
5f4b2acf AK |
587 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
588 | return_0; | |
ae2bb665 | 589 | |
0fe3a2c5 | 590 | if (!(r = dev_manager_activate(dm, lv))) |
6d52fb46 | 591 | stack; |
ae2bb665 | 592 | |
de6c9183 | 593 | dev_manager_destroy(dm); |
ae2bb665 | 594 | return r; |
b1713d28 | 595 | } |
a381c45a | 596 | |
eb91c4ee | 597 | static int _lv_preload(struct logical_volume *lv, int *flush_required) |
0a5e4a14 | 598 | { |
de6c9183 JT |
599 | int r; |
600 | struct dev_manager *dm; | |
37ed70b9 | 601 | |
5f4b2acf AK |
602 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
603 | return_0; | |
604 | ||
eb91c4ee | 605 | if (!(r = dev_manager_preload(dm, lv, flush_required))) |
6d52fb46 | 606 | stack; |
5f4b2acf AK |
607 | |
608 | dev_manager_destroy(dm); | |
609 | return r; | |
610 | } | |
611 | ||
612 | static int _lv_deactivate(struct logical_volume *lv) | |
613 | { | |
614 | int r; | |
615 | struct dev_manager *dm; | |
616 | ||
617 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) | |
618 | return_0; | |
37ed70b9 | 619 | |
de6c9183 | 620 | if (!(r = dev_manager_deactivate(dm, lv))) |
37ed70b9 | 621 | stack; |
37ed70b9 | 622 | |
de6c9183 JT |
623 | dev_manager_destroy(dm); |
624 | return r; | |
37ed70b9 JT |
625 | } |
626 | ||
eb91c4ee | 627 | static int _lv_suspend_lv(struct logical_volume *lv, int lockfs, int flush_required) |
4a624ca0 | 628 | { |
20c5fcf7 AK |
629 | int r; |
630 | struct dev_manager *dm; | |
c2d72fd4 | 631 | |
5f4b2acf AK |
632 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
633 | return_0; | |
0a5e4a14 | 634 | |
eb91c4ee | 635 | if (!(r = dev_manager_suspend(dm, lv, lockfs, flush_required))) |
37ed70b9 | 636 | stack; |
0a5e4a14 | 637 | |
20c5fcf7 AK |
638 | dev_manager_destroy(dm); |
639 | return r; | |
6d52fb46 | 640 | } |
4a624ca0 | 641 | |
8c013da4 | 642 | /* |
f75c11ed | 643 | * These two functions return the number of visible LVs in the state, |
8c013da4 AK |
644 | * or -1 on error. |
645 | */ | |
b45c9f49 | 646 | static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only) |
f047219b | 647 | { |
60f13f01 | 648 | struct lv_list *lvl; |
94b8220f | 649 | int count = 0; |
37ed70b9 | 650 | |
d1d9800e AK |
651 | if (!activation()) |
652 | return 0; | |
653 | ||
2c44337b | 654 | dm_list_iterate_items(lvl, &vg->lvs) { |
59d8429c | 655 | if (lv_is_visible(lvl->lv)) |
b45c9f49 | 656 | count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1); |
37ed70b9 JT |
657 | } |
658 | ||
659 | return count; | |
f047219b | 660 | } |
2ba80b43 | 661 | |
b45c9f49 AK |
662 | int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg) |
663 | { | |
664 | return _lvs_in_vg_activated(vg, 1); | |
665 | } | |
666 | ||
667 | int lvs_in_vg_activated(struct volume_group *vg) | |
668 | { | |
669 | return _lvs_in_vg_activated(vg, 0); | |
670 | } | |
671 | ||
08c9ff43 | 672 | int lvs_in_vg_opened(const struct volume_group *vg) |
2ba80b43 | 673 | { |
08c9ff43 | 674 | const struct lv_list *lvl; |
94b8220f | 675 | int count = 0; |
2ba80b43 | 676 | |
d1d9800e AK |
677 | if (!activation()) |
678 | return 0; | |
679 | ||
2c44337b | 680 | dm_list_iterate_items(lvl, &vg->lvs) { |
afd9ba98 | 681 | if (lv_is_visible(lvl->lv)) |
f894b4b1 | 682 | count += (_lv_open_count(vg->cmd, lvl->lv) > 0); |
2ba80b43 JT |
683 | } |
684 | ||
685 | return count; | |
686 | } | |
413cc918 | 687 | |
5ca6698f DW |
688 | /* |
689 | * Determine whether an LV is active locally or in a cluster. | |
690 | * Assumes vg lock held. | |
691 | * Returns: | |
692 | * 0 - not active locally or on any node in cluster | |
693 | * 1 - active either locally or some node in the cluster | |
694 | */ | |
695 | int lv_is_active(struct logical_volume *lv) | |
696 | { | |
6ac30c94 MB |
697 | int ret; |
698 | ||
5ca6698f DW |
699 | if (_lv_active(lv->vg->cmd, lv, 0)) |
700 | return 1; | |
701 | ||
702 | if (!vg_is_clustered(lv->vg)) | |
703 | return 0; | |
704 | ||
6ac30c94 MB |
705 | if ((ret = remote_lock_held(lv->lvid.s)) >= 0) |
706 | return ret; | |
707 | ||
708 | /* | |
709 | * Old compatibility code if locking doesn't support lock query | |
710 | * FIXME: check status to not deactivate already activate device | |
711 | */ | |
712 | if (activate_lv_excl(lv->vg->cmd, lv)) { | |
713 | deactivate_lv(lv->vg->cmd, lv); | |
25a2e7b8 | 714 | return 0; |
6ac30c94 MB |
715 | } |
716 | ||
717 | /* | |
718 | * Exclusive local activation failed so assume it is active elsewhere. | |
719 | */ | |
720 | return 1; | |
5ca6698f DW |
721 | } |
722 | ||
3e3d5d85 | 723 | /* |
8a37910d AK |
724 | * Returns 0 if an attempt to (un)monitor the device failed. |
725 | * Returns 1 otherwise. | |
3e3d5d85 | 726 | */ |
8a37910d AK |
727 | int monitor_dev_for_events(struct cmd_context *cmd, |
728 | struct logical_volume *lv, int monitor) | |
15d91f5a | 729 | { |
ed09d7e3 | 730 | #ifdef DMEVENTD |
8a37910d AK |
731 | int i, pending = 0, monitored; |
732 | int r = 1; | |
2c44337b | 733 | struct dm_list *tmp, *snh, *snht; |
15d91f5a | 734 | struct lv_segment *seg; |
24f4552b | 735 | int (*monitor_fn) (struct lv_segment *s, int e); |
4e9083db | 736 | uint32_t s; |
15d91f5a | 737 | |
20db8ffc AK |
738 | /* skip dmeventd code altogether */ |
739 | if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE) | |
740 | return 1; | |
741 | ||
8a37910d AK |
742 | /* |
743 | * Nothing to do if dmeventd configured not to be used. | |
744 | */ | |
745 | if (monitor && !dmeventd_monitor_mode()) | |
3e3d5d85 AK |
746 | return 1; |
747 | ||
67961c7c PR |
748 | /* |
749 | * In case of a snapshot device, we monitor lv->snapshot->lv, | |
750 | * not the actual LV itself. | |
751 | */ | |
752 | if (lv_is_cow(lv)) | |
753 | return monitor_dev_for_events(cmd, lv->snapshot->lv, monitor); | |
754 | ||
755 | /* | |
756 | * In case this LV is a snapshot origin, we instead monitor | |
757 | * each of its respective snapshots (the origin itself does | |
758 | * not need to be monitored). | |
759 | * | |
760 | * TODO: This may change when snapshots of mirrors are allowed. | |
761 | */ | |
762 | if (lv_is_origin(lv)) { | |
2c44337b AK |
763 | dm_list_iterate_safe(snh, snht, &lv->snapshot_segs) |
764 | if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh, | |
0c06de63 AK |
765 | struct lv_segment, origin_list)->cow, monitor)) |
766 | r = 0; | |
67961c7c PR |
767 | return r; |
768 | } | |
769 | ||
2c44337b AK |
770 | dm_list_iterate(tmp, &lv->segments) { |
771 | seg = dm_list_item(tmp, struct lv_segment); | |
15d91f5a | 772 | |
4e9083db AK |
773 | /* Recurse for AREA_LV */ |
774 | for (s = 0; s < seg->area_count; s++) { | |
775 | if (seg_type(seg, s) != AREA_LV) | |
776 | continue; | |
777 | if (!monitor_dev_for_events(cmd, seg_lv(seg, s), | |
778 | monitor)) { | |
779 | log_error("Failed to %smonitor %s", | |
780 | monitor ? "" : "un", | |
781 | seg_lv(seg, s)->name); | |
782 | r = 0; | |
783 | } | |
784 | } | |
785 | ||
8ef6eb30 AK |
786 | if (!seg_monitored(seg) || (seg->status & PVMOVE)) |
787 | continue; | |
8a37910d AK |
788 | |
789 | monitor_fn = NULL; | |
ed09d7e3 | 790 | |
8ef6eb30 | 791 | /* Check monitoring status */ |
8a37910d AK |
792 | if (seg->segtype->ops->target_monitored) |
793 | monitored = seg->segtype->ops->target_monitored(seg, &pending); | |
8ef6eb30 AK |
794 | else |
795 | continue; /* segtype doesn't support registration */ | |
796 | ||
797 | /* | |
798 | * FIXME: We should really try again if pending | |
799 | */ | |
8a37910d | 800 | monitored = (pending) ? 0 : monitored; |
8ef6eb30 | 801 | |
8a37910d AK |
802 | if (monitor) { |
803 | if (monitored) | |
8ef6eb30 | 804 | log_verbose("%s/%s already monitored.", lv->vg->name, lv->name); |
8a37910d AK |
805 | else if (seg->segtype->ops->target_monitor_events) |
806 | monitor_fn = seg->segtype->ops->target_monitor_events; | |
8ef6eb30 | 807 | } else { |
8a37910d | 808 | if (!monitored) |
8ef6eb30 | 809 | log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name); |
8a37910d AK |
810 | else if (seg->segtype->ops->target_unmonitor_events) |
811 | monitor_fn = seg->segtype->ops->target_unmonitor_events; | |
8ef6eb30 | 812 | } |
15d91f5a | 813 | |
8ef6eb30 | 814 | /* Do [un]monitor */ |
8a37910d | 815 | if (!monitor_fn) |
3e3d5d85 | 816 | continue; |
e24e7130 | 817 | |
8a37910d AK |
818 | log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name); |
819 | ||
3e3d5d85 | 820 | /* FIXME specify events */ |
57fc4cc0 | 821 | if (!monitor_fn(seg, 0)) { |
8a37910d AK |
822 | log_error("%s/%s: %s segment monitoring function failed.", |
823 | lv->vg->name, lv->name, seg->segtype->name); | |
824 | return 0; | |
e24e7130 | 825 | } |
3e3d5d85 | 826 | |
8ef6eb30 AK |
827 | /* Check [un]monitor results */ |
828 | /* Try a couple times if pending, but not forever... */ | |
829 | for (i = 0; i < 10; i++) { | |
830 | pending = 0; | |
8a37910d | 831 | monitored = seg->segtype->ops->target_monitored(seg, &pending); |
8ef6eb30 | 832 | if (pending || |
8a37910d AK |
833 | (!monitored && monitor) || |
834 | (monitored && !monitor)) | |
835 | log_very_verbose("%s/%s %smonitoring still pending: waiting...", | |
836 | lv->vg->name, lv->name, monitor ? "" : "un"); | |
8ef6eb30 AK |
837 | else |
838 | break; | |
839 | sleep(1); | |
840 | } | |
841 | ||
8a37910d | 842 | r = (monitored && monitor) || (!monitored && !monitor); |
15d91f5a | 843 | } |
e6493477 | 844 | |
3e3d5d85 AK |
845 | return r; |
846 | #else | |
15d91f5a | 847 | return 1; |
3e3d5d85 | 848 | #endif |
15d91f5a | 849 | } |
15d91f5a | 850 | |
658b5812 AK |
851 | static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s, |
852 | int error_if_not_suspended) | |
413cc918 | 853 | { |
095bbca6 | 854 | struct logical_volume *lv = NULL, *lv_pre = NULL; |
199e490e | 855 | struct lvinfo info; |
eb91c4ee | 856 | int r = 0, lockfs = 0, flush_required = 0; |
413cc918 | 857 | |
d1d9800e AK |
858 | if (!activation()) |
859 | return 1; | |
860 | ||
8b888354 | 861 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 862 | goto_out; |
8b888354 | 863 | |
7a593325 | 864 | /* Use precommitted metadata if present */ |
8b888354 | 865 | if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1))) |
095bbca6 | 866 | goto_out; |
413cc918 | 867 | |
20c5fcf7 AK |
868 | if (test_mode()) { |
869 | _skip("Suspending '%s'.", lv->name); | |
095bbca6 MB |
870 | r = 1; |
871 | goto out; | |
20c5fcf7 AK |
872 | } |
873 | ||
a6b22cf3 | 874 | if (!lv_info(cmd, lv, &info, 0, 0)) |
095bbca6 | 875 | goto_out; |
41967a02 | 876 | |
095bbca6 MB |
877 | if (!info.exists || info.suspended) { |
878 | r = error_if_not_suspended ? 0 : 1; | |
879 | goto out; | |
880 | } | |
914c9723 | 881 | |
c1fdeec9 MB |
882 | lv_calculate_readahead(lv, NULL); |
883 | ||
5f4b2acf | 884 | /* If VG was precommitted, preload devices for the LV */ |
8b888354 | 885 | if ((lv_pre->vg->status & PRECOMMITTED)) { |
eb91c4ee | 886 | if (!_lv_preload(lv_pre, &flush_required)) { |
5f4b2acf | 887 | /* FIXME Revert preloading */ |
095bbca6 | 888 | goto_out; |
5f4b2acf AK |
889 | } |
890 | } | |
891 | ||
8a37910d | 892 | if (!monitor_dev_for_events(cmd, lv, 0)) |
e24e7130 | 893 | /* FIXME Consider aborting here */ |
ed09d7e3 AK |
894 | stack; |
895 | ||
914c9723 | 896 | memlock_inc(); |
9cd3426d AK |
897 | |
898 | if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)) | |
899 | lockfs = 1; | |
900 | ||
eb91c4ee | 901 | if (!_lv_suspend_lv(lv, lockfs, flush_required)) { |
914c9723 AK |
902 | memlock_dec(); |
903 | fs_unlock(); | |
095bbca6 | 904 | goto out; |
914c9723 | 905 | } |
8c013da4 | 906 | |
095bbca6 MB |
907 | r = 1; |
908 | out: | |
909 | if (lv_pre) | |
910 | vg_release(lv_pre->vg); | |
911 | if (lv) | |
912 | vg_release(lv->vg); | |
913 | ||
914 | return r; | |
413cc918 AK |
915 | } |
916 | ||
658b5812 AK |
917 | /* Returns success if the device is not active */ |
918 | int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s) | |
919 | { | |
920 | return _lv_suspend(cmd, lvid_s, 0); | |
921 | } | |
922 | ||
923 | int lv_suspend(struct cmd_context *cmd, const char *lvid_s) | |
924 | { | |
925 | return _lv_suspend(cmd, lvid_s, 1); | |
926 | } | |
927 | ||
928 | static int _lv_resume(struct cmd_context *cmd, const char *lvid_s, | |
929 | int error_if_not_active) | |
413cc918 AK |
930 | { |
931 | struct logical_volume *lv; | |
199e490e | 932 | struct lvinfo info; |
095bbca6 | 933 | int r = 0; |
413cc918 | 934 | |
d1d9800e AK |
935 | if (!activation()) |
936 | return 1; | |
937 | ||
7a593325 | 938 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
75b37a11 | 939 | goto_out; |
413cc918 | 940 | |
20c5fcf7 AK |
941 | if (test_mode()) { |
942 | _skip("Resuming '%s'.", lv->name); | |
095bbca6 MB |
943 | r = 1; |
944 | goto out; | |
20c5fcf7 AK |
945 | } |
946 | ||
a6b22cf3 | 947 | if (!lv_info(cmd, lv, &info, 0, 0)) |
095bbca6 | 948 | goto_out; |
41967a02 | 949 | |
095bbca6 MB |
950 | if (!info.exists || !info.suspended) { |
951 | r = error_if_not_active ? 0 : 1; | |
75b37a11 | 952 | goto_out; |
095bbca6 | 953 | } |
914c9723 | 954 | |
658b5812 | 955 | if (!_lv_activate_lv(lv)) |
75b37a11 | 956 | goto_out; |
914c9723 AK |
957 | |
958 | memlock_dec(); | |
959 | fs_unlock(); | |
413cc918 | 960 | |
8a37910d | 961 | if (!monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 962 | stack; |
15d91f5a | 963 | |
095bbca6 MB |
964 | r = 1; |
965 | out: | |
966 | if (lv) | |
967 | vg_release(lv->vg); | |
968 | ||
969 | return r; | |
413cc918 AK |
970 | } |
971 | ||
658b5812 AK |
972 | /* Returns success if the device is not active */ |
973 | int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s) | |
974 | { | |
975 | return _lv_resume(cmd, lvid_s, 0); | |
976 | } | |
977 | ||
978 | int lv_resume(struct cmd_context *cmd, const char *lvid_s) | |
979 | { | |
980 | return _lv_resume(cmd, lvid_s, 1); | |
981 | } | |
982 | ||
64a95010 AK |
983 | static int _lv_has_open_snapshots(struct logical_volume *lv) |
984 | { | |
985 | struct lv_segment *snap_seg; | |
986 | struct lvinfo info; | |
987 | int r = 0; | |
988 | ||
989 | dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) { | |
990 | if (!lv_info(lv->vg->cmd, snap_seg->cow, &info, 1, 0)) { | |
991 | r = 1; | |
992 | continue; | |
993 | } | |
994 | ||
995 | if (info.exists && info.open_count) { | |
996 | log_error("LV %s/%s has open snapshot %s: " | |
997 | "not deactivating", lv->vg->name, lv->name, | |
998 | snap_seg->cow->name); | |
999 | r = 1; | |
1000 | } | |
1001 | } | |
1002 | ||
1003 | return r; | |
1004 | } | |
1005 | ||
be326a2f | 1006 | int lv_deactivate(struct cmd_context *cmd, const char *lvid_s) |
f4cbeaf0 AK |
1007 | { |
1008 | struct logical_volume *lv; | |
199e490e | 1009 | struct lvinfo info; |
095bbca6 | 1010 | int r = 0; |
f4cbeaf0 | 1011 | |
d1d9800e AK |
1012 | if (!activation()) |
1013 | return 1; | |
1014 | ||
7a593325 | 1015 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 1016 | goto out; |
f4cbeaf0 | 1017 | |
20c5fcf7 AK |
1018 | if (test_mode()) { |
1019 | _skip("Deactivating '%s'.", lv->name); | |
095bbca6 MB |
1020 | r = 1; |
1021 | goto out; | |
20c5fcf7 AK |
1022 | } |
1023 | ||
a6b22cf3 | 1024 | if (!lv_info(cmd, lv, &info, 1, 0)) |
095bbca6 | 1025 | goto_out; |
41967a02 | 1026 | |
095bbca6 MB |
1027 | if (!info.exists) { |
1028 | r = 1; | |
1029 | goto out; | |
1030 | } | |
f4cbeaf0 | 1031 | |
64a95010 AK |
1032 | if (lv_is_visible(lv)) { |
1033 | if (info.open_count) { | |
1034 | log_error("LV %s/%s in use: not deactivating", | |
1035 | lv->vg->name, lv->name); | |
1036 | goto out; | |
1037 | } | |
1038 | if (lv_is_origin(lv) && _lv_has_open_snapshots(lv)) | |
1039 | goto_out; | |
0cf96f33 AK |
1040 | } |
1041 | ||
c1fdeec9 MB |
1042 | lv_calculate_readahead(lv, NULL); |
1043 | ||
8a37910d | 1044 | if (!monitor_dev_for_events(cmd, lv, 0)) |
ed09d7e3 | 1045 | stack; |
15d91f5a | 1046 | |
914c9723 AK |
1047 | memlock_inc(); |
1048 | r = _lv_deactivate(lv); | |
1049 | memlock_dec(); | |
1050 | fs_unlock(); | |
1051 | ||
89a6cdfd MB |
1052 | if (!lv_info(cmd, lv, &info, 1, 0) || info.exists) |
1053 | r = 0; | |
095bbca6 MB |
1054 | out: |
1055 | if (lv) | |
1056 | vg_release(lv->vg); | |
1057 | ||
914c9723 | 1058 | return r; |
f4cbeaf0 AK |
1059 | } |
1060 | ||
658b5812 AK |
1061 | /* Test if LV passes filter */ |
1062 | int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s, | |
1063 | int *activate_lv) | |
1064 | { | |
1065 | struct logical_volume *lv; | |
095bbca6 | 1066 | int r = 0; |
658b5812 | 1067 | |
095bbca6 MB |
1068 | if (!activation()) { |
1069 | *activate_lv = 1; | |
1070 | return 1; | |
1071 | } | |
658b5812 | 1072 | |
424dd43e | 1073 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 1074 | goto out; |
658b5812 AK |
1075 | |
1076 | if (!_passes_activation_filter(cmd, lv)) { | |
1077 | log_verbose("Not activating %s/%s due to config file settings", | |
1078 | lv->vg->name, lv->name); | |
1079 | *activate_lv = 0; | |
095bbca6 MB |
1080 | } else |
1081 | *activate_lv = 1; | |
1082 | r = 1; | |
1083 | out: | |
1084 | if (lv) | |
1085 | vg_release(lv->vg); | |
658b5812 | 1086 | |
095bbca6 | 1087 | return r; |
658b5812 AK |
1088 | } |
1089 | ||
07d31831 AK |
1090 | static int _lv_activate(struct cmd_context *cmd, const char *lvid_s, |
1091 | int exclusive, int filter) | |
f4cbeaf0 AK |
1092 | { |
1093 | struct logical_volume *lv; | |
199e490e | 1094 | struct lvinfo info; |
095bbca6 | 1095 | int r = 0; |
f4cbeaf0 | 1096 | |
d1d9800e AK |
1097 | if (!activation()) |
1098 | return 1; | |
1099 | ||
424dd43e | 1100 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 1101 | goto out; |
f4cbeaf0 | 1102 | |
658b5812 | 1103 | if (filter && !_passes_activation_filter(cmd, lv)) { |
de17d760 AK |
1104 | log_verbose("Not activating %s/%s due to config file settings", |
1105 | lv->vg->name, lv->name); | |
095bbca6 | 1106 | goto out; |
de17d760 AK |
1107 | } |
1108 | ||
cda35408 | 1109 | if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) { |
8c5bcdab AK |
1110 | log_error("Refusing activation of partial LV %s. Use --partial to override.", |
1111 | lv->name); | |
095bbca6 | 1112 | goto_out; |
8c5bcdab AK |
1113 | } |
1114 | ||
b4048242 PR |
1115 | if (lv_has_unknown_segments(lv)) { |
1116 | log_error("Refusing activation of LV %s containing " | |
1117 | "an unrecognised segment.", lv->name); | |
1118 | goto_out; | |
1119 | } | |
1120 | ||
20c5fcf7 AK |
1121 | if (test_mode()) { |
1122 | _skip("Activating '%s'.", lv->name); | |
095bbca6 MB |
1123 | r = 1; |
1124 | goto out; | |
20c5fcf7 AK |
1125 | } |
1126 | ||
a6b22cf3 | 1127 | if (!lv_info(cmd, lv, &info, 0, 0)) |
095bbca6 | 1128 | goto_out; |
8c013da4 | 1129 | |
095bbca6 MB |
1130 | if (info.exists && !info.suspended && info.live_table) { |
1131 | r = 1; | |
1132 | goto out; | |
1133 | } | |
f4cbeaf0 | 1134 | |
c1fdeec9 MB |
1135 | lv_calculate_readahead(lv, NULL); |
1136 | ||
07d31831 AK |
1137 | if (exclusive) |
1138 | lv->status |= ACTIVATE_EXCL; | |
1139 | ||
914c9723 | 1140 | memlock_inc(); |
75b37a11 AK |
1141 | if (!(r = _lv_activate_lv(lv))) |
1142 | stack; | |
914c9723 AK |
1143 | memlock_dec(); |
1144 | fs_unlock(); | |
1145 | ||
5efd97f1 | 1146 | if (r && !monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 1147 | stack; |
15d91f5a | 1148 | |
095bbca6 MB |
1149 | out: |
1150 | if (lv) | |
1151 | vg_release(lv->vg); | |
1152 | ||
914c9723 | 1153 | return r; |
f4cbeaf0 | 1154 | } |
199e490e | 1155 | |
658b5812 | 1156 | /* Activate LV */ |
07d31831 | 1157 | int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 1158 | { |
75b37a11 AK |
1159 | if (!_lv_activate(cmd, lvid_s, exclusive, 0)) |
1160 | return_0; | |
1161 | ||
1162 | return 1; | |
658b5812 AK |
1163 | } |
1164 | ||
1165 | /* Activate LV only if it passes filter */ | |
07d31831 | 1166 | int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 1167 | { |
75b37a11 AK |
1168 | if (!_lv_activate(cmd, lvid_s, exclusive, 1)) |
1169 | return_0; | |
1170 | ||
1171 | return 1; | |
658b5812 AK |
1172 | } |
1173 | ||
f7dd6d84 AK |
1174 | int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv) |
1175 | { | |
1176 | struct lvinfo info; | |
1177 | int r = 1; | |
1178 | ||
8b076648 | 1179 | if (!lv) { |
2262b320 | 1180 | r = dm_mknodes(NULL); |
8b076648 AK |
1181 | fs_unlock(); |
1182 | return r; | |
1183 | } | |
1184 | ||
a6b22cf3 | 1185 | if (!_lv_info(cmd, lv, 1, &info, 0, 0, 0)) |
5f4b2acf | 1186 | return_0; |
f7dd6d84 | 1187 | |
3ad47d16 PR |
1188 | if (info.exists) { |
1189 | if (lv_is_visible(lv)) | |
1190 | r = dev_manager_lv_mknodes(lv); | |
1191 | } else | |
f09fe0ad | 1192 | r = dev_manager_lv_rmnodes(lv); |
f7dd6d84 AK |
1193 | |
1194 | fs_unlock(); | |
1195 | ||
1196 | return r; | |
1197 | } | |
1198 | ||
352a99b9 AK |
1199 | /* |
1200 | * Does PV use VG somewhere in its construction? | |
1201 | * Returns 1 on failure. | |
1202 | */ | |
898e6f8e | 1203 | int pv_uses_vg(struct physical_volume *pv, |
3e3d5d85 | 1204 | struct volume_group *vg) |
352a99b9 | 1205 | { |
352a99b9 AK |
1206 | if (!activation()) |
1207 | return 0; | |
1208 | ||
1209 | if (!dm_is_dm_major(MAJOR(pv->dev->dev))) | |
1210 | return 0; | |
1211 | ||
898e6f8e | 1212 | return dev_manager_device_uses_vg(pv->dev, vg); |
352a99b9 AK |
1213 | } |
1214 | ||
2293567c AK |
1215 | void activation_release(void) |
1216 | { | |
1217 | dev_manager_release(); | |
1218 | } | |
1219 | ||
914c9723 AK |
1220 | void activation_exit(void) |
1221 | { | |
1222 | dev_manager_exit(); | |
1223 | } | |
199e490e | 1224 | #endif |