]>
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 | 488 | { |
0548bcc2 | 489 | int r; |
4bd9480d AK |
490 | struct logical_volume *lv; |
491 | ||
7a593325 | 492 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
4bd9480d AK |
493 | return 0; |
494 | ||
0548bcc2 MB |
495 | r = _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0); |
496 | vg_release(lv->vg); | |
497 | ||
498 | return r; | |
4bd9480d AK |
499 | } |
500 | ||
1951dba9 AL |
501 | /* |
502 | * Returns 1 if percent set, else 0 on failure. | |
503 | */ | |
78ad1549 AK |
504 | int lv_snapshot_percent(const struct logical_volume *lv, float *percent, |
505 | percent_range_t *percent_range) | |
1951dba9 AL |
506 | { |
507 | int r; | |
508 | struct dev_manager *dm; | |
509 | ||
d1d9800e AK |
510 | if (!activation()) |
511 | return 0; | |
512 | ||
5f4b2acf AK |
513 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
514 | return_0; | |
1951dba9 | 515 | |
78ad1549 | 516 | if (!(r = dev_manager_snapshot_percent(dm, lv, percent, percent_range))) |
1951dba9 | 517 | stack; |
c826c0d1 | 518 | |
1951dba9 AL |
519 | dev_manager_destroy(dm); |
520 | ||
521 | return r; | |
522 | } | |
523 | ||
10b29b8d | 524 | /* FIXME Merge with snapshot_percent */ |
472ac5bd | 525 | int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv, |
78ad1549 AK |
526 | int wait, float *percent, percent_range_t *percent_range, |
527 | uint32_t *event_nr) | |
10b29b8d AK |
528 | { |
529 | int r; | |
530 | struct dev_manager *dm; | |
b65b777d | 531 | struct lvinfo info; |
10b29b8d | 532 | |
876003dc AK |
533 | /* If mirrored LV is temporarily shrinked to 1 area (= linear), |
534 | * it should be considered in-sync. */ | |
2c44337b | 535 | if (dm_list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) { |
876003dc AK |
536 | *percent = 100.0; |
537 | return 1; | |
538 | } | |
539 | ||
10b29b8d AK |
540 | if (!activation()) |
541 | return 0; | |
542 | ||
a6b22cf3 | 543 | if (!lv_info(cmd, lv, &info, 0, 0)) |
5f4b2acf | 544 | return_0; |
b65b777d AK |
545 | |
546 | if (!info.exists) | |
547 | return 0; | |
548 | ||
5f4b2acf AK |
549 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
550 | return_0; | |
10b29b8d | 551 | |
78ad1549 AK |
552 | if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, |
553 | percent_range, event_nr))) | |
10b29b8d AK |
554 | stack; |
555 | ||
556 | dev_manager_destroy(dm); | |
557 | ||
558 | return r; | |
559 | } | |
560 | ||
b45c9f49 AK |
561 | static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv, |
562 | unsigned by_uuid_only) | |
2ba80b43 | 563 | { |
199e490e | 564 | struct lvinfo info; |
2ba80b43 | 565 | |
a6b22cf3 | 566 | if (!_lv_info(cmd, lv, 0, &info, 0, 0, by_uuid_only)) { |
2ba80b43 | 567 | stack; |
de6c9183 | 568 | return -1; |
2ba80b43 JT |
569 | } |
570 | ||
de6c9183 | 571 | return info.exists; |
2ba80b43 JT |
572 | } |
573 | ||
f894b4b1 | 574 | static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv) |
5986ec94 | 575 | { |
199e490e | 576 | struct lvinfo info; |
5986ec94 | 577 | |
a6b22cf3 | 578 | if (!lv_info(cmd, lv, &info, 1, 0)) { |
5986ec94 | 579 | stack; |
de6c9183 | 580 | return -1; |
5986ec94 JT |
581 | } |
582 | ||
de6c9183 | 583 | return info.open_count; |
5986ec94 JT |
584 | } |
585 | ||
658b5812 | 586 | static int _lv_activate_lv(struct logical_volume *lv) |
b1713d28 | 587 | { |
6d52fb46 | 588 | int r; |
de6c9183 | 589 | struct dev_manager *dm; |
b1713d28 | 590 | |
5f4b2acf AK |
591 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
592 | return_0; | |
ae2bb665 | 593 | |
0fe3a2c5 | 594 | if (!(r = dev_manager_activate(dm, lv))) |
6d52fb46 | 595 | stack; |
ae2bb665 | 596 | |
de6c9183 | 597 | dev_manager_destroy(dm); |
ae2bb665 | 598 | return r; |
b1713d28 | 599 | } |
a381c45a | 600 | |
eb91c4ee | 601 | static int _lv_preload(struct logical_volume *lv, int *flush_required) |
0a5e4a14 | 602 | { |
de6c9183 JT |
603 | int r; |
604 | struct dev_manager *dm; | |
37ed70b9 | 605 | |
5f4b2acf AK |
606 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
607 | return_0; | |
608 | ||
eb91c4ee | 609 | if (!(r = dev_manager_preload(dm, lv, flush_required))) |
6d52fb46 | 610 | stack; |
5f4b2acf AK |
611 | |
612 | dev_manager_destroy(dm); | |
613 | return r; | |
614 | } | |
615 | ||
616 | static int _lv_deactivate(struct logical_volume *lv) | |
617 | { | |
618 | int r; | |
619 | struct dev_manager *dm; | |
620 | ||
621 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) | |
622 | return_0; | |
37ed70b9 | 623 | |
de6c9183 | 624 | if (!(r = dev_manager_deactivate(dm, lv))) |
37ed70b9 | 625 | stack; |
37ed70b9 | 626 | |
de6c9183 JT |
627 | dev_manager_destroy(dm); |
628 | return r; | |
37ed70b9 JT |
629 | } |
630 | ||
eb91c4ee | 631 | static int _lv_suspend_lv(struct logical_volume *lv, int lockfs, int flush_required) |
4a624ca0 | 632 | { |
20c5fcf7 AK |
633 | int r; |
634 | struct dev_manager *dm; | |
c2d72fd4 | 635 | |
5f4b2acf AK |
636 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
637 | return_0; | |
0a5e4a14 | 638 | |
eb91c4ee | 639 | if (!(r = dev_manager_suspend(dm, lv, lockfs, flush_required))) |
37ed70b9 | 640 | stack; |
0a5e4a14 | 641 | |
20c5fcf7 AK |
642 | dev_manager_destroy(dm); |
643 | return r; | |
6d52fb46 | 644 | } |
4a624ca0 | 645 | |
8c013da4 | 646 | /* |
f75c11ed | 647 | * These two functions return the number of visible LVs in the state, |
8c013da4 AK |
648 | * or -1 on error. |
649 | */ | |
b45c9f49 | 650 | static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only) |
f047219b | 651 | { |
60f13f01 | 652 | struct lv_list *lvl; |
94b8220f | 653 | int count = 0; |
37ed70b9 | 654 | |
d1d9800e AK |
655 | if (!activation()) |
656 | return 0; | |
657 | ||
2c44337b | 658 | dm_list_iterate_items(lvl, &vg->lvs) { |
59d8429c | 659 | if (lv_is_visible(lvl->lv)) |
b45c9f49 | 660 | count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1); |
37ed70b9 JT |
661 | } |
662 | ||
663 | return count; | |
f047219b | 664 | } |
2ba80b43 | 665 | |
b45c9f49 AK |
666 | int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg) |
667 | { | |
668 | return _lvs_in_vg_activated(vg, 1); | |
669 | } | |
670 | ||
671 | int lvs_in_vg_activated(struct volume_group *vg) | |
672 | { | |
673 | return _lvs_in_vg_activated(vg, 0); | |
674 | } | |
675 | ||
08c9ff43 | 676 | int lvs_in_vg_opened(const struct volume_group *vg) |
2ba80b43 | 677 | { |
08c9ff43 | 678 | const struct lv_list *lvl; |
94b8220f | 679 | int count = 0; |
2ba80b43 | 680 | |
d1d9800e AK |
681 | if (!activation()) |
682 | return 0; | |
683 | ||
2c44337b | 684 | dm_list_iterate_items(lvl, &vg->lvs) { |
afd9ba98 | 685 | if (lv_is_visible(lvl->lv)) |
f894b4b1 | 686 | count += (_lv_open_count(vg->cmd, lvl->lv) > 0); |
2ba80b43 JT |
687 | } |
688 | ||
689 | return count; | |
690 | } | |
413cc918 | 691 | |
5ca6698f DW |
692 | /* |
693 | * Determine whether an LV is active locally or in a cluster. | |
694 | * Assumes vg lock held. | |
695 | * Returns: | |
696 | * 0 - not active locally or on any node in cluster | |
697 | * 1 - active either locally or some node in the cluster | |
698 | */ | |
699 | int lv_is_active(struct logical_volume *lv) | |
700 | { | |
6ac30c94 MB |
701 | int ret; |
702 | ||
5ca6698f DW |
703 | if (_lv_active(lv->vg->cmd, lv, 0)) |
704 | return 1; | |
705 | ||
706 | if (!vg_is_clustered(lv->vg)) | |
707 | return 0; | |
708 | ||
6ac30c94 MB |
709 | if ((ret = remote_lock_held(lv->lvid.s)) >= 0) |
710 | return ret; | |
711 | ||
712 | /* | |
713 | * Old compatibility code if locking doesn't support lock query | |
714 | * FIXME: check status to not deactivate already activate device | |
715 | */ | |
716 | if (activate_lv_excl(lv->vg->cmd, lv)) { | |
717 | deactivate_lv(lv->vg->cmd, lv); | |
25a2e7b8 | 718 | return 0; |
6ac30c94 MB |
719 | } |
720 | ||
721 | /* | |
722 | * Exclusive local activation failed so assume it is active elsewhere. | |
723 | */ | |
724 | return 1; | |
5ca6698f DW |
725 | } |
726 | ||
3e3d5d85 | 727 | /* |
8a37910d AK |
728 | * Returns 0 if an attempt to (un)monitor the device failed. |
729 | * Returns 1 otherwise. | |
3e3d5d85 | 730 | */ |
8a37910d AK |
731 | int monitor_dev_for_events(struct cmd_context *cmd, |
732 | struct logical_volume *lv, int monitor) | |
15d91f5a | 733 | { |
ed09d7e3 | 734 | #ifdef DMEVENTD |
8a37910d AK |
735 | int i, pending = 0, monitored; |
736 | int r = 1; | |
2c44337b | 737 | struct dm_list *tmp, *snh, *snht; |
15d91f5a | 738 | struct lv_segment *seg; |
24f4552b | 739 | int (*monitor_fn) (struct lv_segment *s, int e); |
4e9083db | 740 | uint32_t s; |
15d91f5a | 741 | |
20db8ffc AK |
742 | /* skip dmeventd code altogether */ |
743 | if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE) | |
744 | return 1; | |
745 | ||
8a37910d AK |
746 | /* |
747 | * Nothing to do if dmeventd configured not to be used. | |
748 | */ | |
749 | if (monitor && !dmeventd_monitor_mode()) | |
3e3d5d85 AK |
750 | return 1; |
751 | ||
67961c7c PR |
752 | /* |
753 | * In case of a snapshot device, we monitor lv->snapshot->lv, | |
754 | * not the actual LV itself. | |
755 | */ | |
756 | if (lv_is_cow(lv)) | |
757 | return monitor_dev_for_events(cmd, lv->snapshot->lv, monitor); | |
758 | ||
759 | /* | |
760 | * In case this LV is a snapshot origin, we instead monitor | |
761 | * each of its respective snapshots (the origin itself does | |
762 | * not need to be monitored). | |
763 | * | |
764 | * TODO: This may change when snapshots of mirrors are allowed. | |
765 | */ | |
766 | if (lv_is_origin(lv)) { | |
2c44337b AK |
767 | dm_list_iterate_safe(snh, snht, &lv->snapshot_segs) |
768 | if (!monitor_dev_for_events(cmd, dm_list_struct_base(snh, | |
0c06de63 AK |
769 | struct lv_segment, origin_list)->cow, monitor)) |
770 | r = 0; | |
67961c7c PR |
771 | return r; |
772 | } | |
773 | ||
2c44337b AK |
774 | dm_list_iterate(tmp, &lv->segments) { |
775 | seg = dm_list_item(tmp, struct lv_segment); | |
15d91f5a | 776 | |
4e9083db AK |
777 | /* Recurse for AREA_LV */ |
778 | for (s = 0; s < seg->area_count; s++) { | |
779 | if (seg_type(seg, s) != AREA_LV) | |
780 | continue; | |
781 | if (!monitor_dev_for_events(cmd, seg_lv(seg, s), | |
782 | monitor)) { | |
783 | log_error("Failed to %smonitor %s", | |
784 | monitor ? "" : "un", | |
785 | seg_lv(seg, s)->name); | |
786 | r = 0; | |
787 | } | |
788 | } | |
789 | ||
8ef6eb30 AK |
790 | if (!seg_monitored(seg) || (seg->status & PVMOVE)) |
791 | continue; | |
8a37910d AK |
792 | |
793 | monitor_fn = NULL; | |
ed09d7e3 | 794 | |
8ef6eb30 | 795 | /* Check monitoring status */ |
8a37910d AK |
796 | if (seg->segtype->ops->target_monitored) |
797 | monitored = seg->segtype->ops->target_monitored(seg, &pending); | |
8ef6eb30 AK |
798 | else |
799 | continue; /* segtype doesn't support registration */ | |
800 | ||
801 | /* | |
802 | * FIXME: We should really try again if pending | |
803 | */ | |
8a37910d | 804 | monitored = (pending) ? 0 : monitored; |
8ef6eb30 | 805 | |
8a37910d AK |
806 | if (monitor) { |
807 | if (monitored) | |
8ef6eb30 | 808 | log_verbose("%s/%s already monitored.", lv->vg->name, lv->name); |
8a37910d AK |
809 | else if (seg->segtype->ops->target_monitor_events) |
810 | monitor_fn = seg->segtype->ops->target_monitor_events; | |
8ef6eb30 | 811 | } else { |
8a37910d | 812 | if (!monitored) |
8ef6eb30 | 813 | log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name); |
8a37910d AK |
814 | else if (seg->segtype->ops->target_unmonitor_events) |
815 | monitor_fn = seg->segtype->ops->target_unmonitor_events; | |
8ef6eb30 | 816 | } |
15d91f5a | 817 | |
8ef6eb30 | 818 | /* Do [un]monitor */ |
8a37910d | 819 | if (!monitor_fn) |
3e3d5d85 | 820 | continue; |
e24e7130 | 821 | |
8a37910d AK |
822 | log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name); |
823 | ||
3e3d5d85 | 824 | /* FIXME specify events */ |
57fc4cc0 | 825 | if (!monitor_fn(seg, 0)) { |
8a37910d AK |
826 | log_error("%s/%s: %s segment monitoring function failed.", |
827 | lv->vg->name, lv->name, seg->segtype->name); | |
828 | return 0; | |
e24e7130 | 829 | } |
3e3d5d85 | 830 | |
8ef6eb30 AK |
831 | /* Check [un]monitor results */ |
832 | /* Try a couple times if pending, but not forever... */ | |
833 | for (i = 0; i < 10; i++) { | |
834 | pending = 0; | |
8a37910d | 835 | monitored = seg->segtype->ops->target_monitored(seg, &pending); |
8ef6eb30 | 836 | if (pending || |
8a37910d AK |
837 | (!monitored && monitor) || |
838 | (monitored && !monitor)) | |
839 | log_very_verbose("%s/%s %smonitoring still pending: waiting...", | |
840 | lv->vg->name, lv->name, monitor ? "" : "un"); | |
8ef6eb30 AK |
841 | else |
842 | break; | |
843 | sleep(1); | |
844 | } | |
845 | ||
8a37910d | 846 | r = (monitored && monitor) || (!monitored && !monitor); |
15d91f5a | 847 | } |
e6493477 | 848 | |
3e3d5d85 AK |
849 | return r; |
850 | #else | |
15d91f5a | 851 | return 1; |
3e3d5d85 | 852 | #endif |
15d91f5a | 853 | } |
15d91f5a | 854 | |
658b5812 AK |
855 | static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s, |
856 | int error_if_not_suspended) | |
413cc918 | 857 | { |
095bbca6 | 858 | struct logical_volume *lv = NULL, *lv_pre = NULL; |
199e490e | 859 | struct lvinfo info; |
eb91c4ee | 860 | int r = 0, lockfs = 0, flush_required = 0; |
413cc918 | 861 | |
d1d9800e AK |
862 | if (!activation()) |
863 | return 1; | |
864 | ||
8b888354 | 865 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 866 | goto_out; |
8b888354 | 867 | |
7a593325 | 868 | /* Use precommitted metadata if present */ |
8b888354 | 869 | if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1))) |
095bbca6 | 870 | goto_out; |
413cc918 | 871 | |
20c5fcf7 AK |
872 | if (test_mode()) { |
873 | _skip("Suspending '%s'.", lv->name); | |
095bbca6 MB |
874 | r = 1; |
875 | goto out; | |
20c5fcf7 AK |
876 | } |
877 | ||
a6b22cf3 | 878 | if (!lv_info(cmd, lv, &info, 0, 0)) |
095bbca6 | 879 | goto_out; |
41967a02 | 880 | |
095bbca6 MB |
881 | if (!info.exists || info.suspended) { |
882 | r = error_if_not_suspended ? 0 : 1; | |
883 | goto out; | |
884 | } | |
914c9723 | 885 | |
c1fdeec9 MB |
886 | lv_calculate_readahead(lv, NULL); |
887 | ||
5f4b2acf | 888 | /* If VG was precommitted, preload devices for the LV */ |
8b888354 | 889 | if ((lv_pre->vg->status & PRECOMMITTED)) { |
eb91c4ee | 890 | if (!_lv_preload(lv_pre, &flush_required)) { |
5f4b2acf | 891 | /* FIXME Revert preloading */ |
095bbca6 | 892 | goto_out; |
5f4b2acf AK |
893 | } |
894 | } | |
895 | ||
8a37910d | 896 | if (!monitor_dev_for_events(cmd, lv, 0)) |
e24e7130 | 897 | /* FIXME Consider aborting here */ |
ed09d7e3 AK |
898 | stack; |
899 | ||
914c9723 | 900 | memlock_inc(); |
9cd3426d AK |
901 | |
902 | if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)) | |
903 | lockfs = 1; | |
904 | ||
eb91c4ee | 905 | if (!_lv_suspend_lv(lv, lockfs, flush_required)) { |
914c9723 AK |
906 | memlock_dec(); |
907 | fs_unlock(); | |
095bbca6 | 908 | goto out; |
914c9723 | 909 | } |
8c013da4 | 910 | |
095bbca6 MB |
911 | r = 1; |
912 | out: | |
913 | if (lv_pre) | |
914 | vg_release(lv_pre->vg); | |
915 | if (lv) | |
916 | vg_release(lv->vg); | |
917 | ||
918 | return r; | |
413cc918 AK |
919 | } |
920 | ||
658b5812 AK |
921 | /* Returns success if the device is not active */ |
922 | int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s) | |
923 | { | |
924 | return _lv_suspend(cmd, lvid_s, 0); | |
925 | } | |
926 | ||
927 | int lv_suspend(struct cmd_context *cmd, const char *lvid_s) | |
928 | { | |
929 | return _lv_suspend(cmd, lvid_s, 1); | |
930 | } | |
931 | ||
932 | static int _lv_resume(struct cmd_context *cmd, const char *lvid_s, | |
933 | int error_if_not_active) | |
413cc918 AK |
934 | { |
935 | struct logical_volume *lv; | |
199e490e | 936 | struct lvinfo info; |
095bbca6 | 937 | int r = 0; |
413cc918 | 938 | |
d1d9800e AK |
939 | if (!activation()) |
940 | return 1; | |
941 | ||
7a593325 | 942 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
75b37a11 | 943 | goto_out; |
413cc918 | 944 | |
20c5fcf7 AK |
945 | if (test_mode()) { |
946 | _skip("Resuming '%s'.", lv->name); | |
095bbca6 MB |
947 | r = 1; |
948 | goto out; | |
20c5fcf7 AK |
949 | } |
950 | ||
a6b22cf3 | 951 | if (!lv_info(cmd, lv, &info, 0, 0)) |
095bbca6 | 952 | goto_out; |
41967a02 | 953 | |
095bbca6 MB |
954 | if (!info.exists || !info.suspended) { |
955 | r = error_if_not_active ? 0 : 1; | |
75b37a11 | 956 | goto_out; |
095bbca6 | 957 | } |
914c9723 | 958 | |
658b5812 | 959 | if (!_lv_activate_lv(lv)) |
75b37a11 | 960 | goto_out; |
914c9723 AK |
961 | |
962 | memlock_dec(); | |
963 | fs_unlock(); | |
413cc918 | 964 | |
8a37910d | 965 | if (!monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 966 | stack; |
15d91f5a | 967 | |
095bbca6 MB |
968 | r = 1; |
969 | out: | |
970 | if (lv) | |
971 | vg_release(lv->vg); | |
972 | ||
973 | return r; | |
413cc918 AK |
974 | } |
975 | ||
658b5812 AK |
976 | /* Returns success if the device is not active */ |
977 | int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s) | |
978 | { | |
979 | return _lv_resume(cmd, lvid_s, 0); | |
980 | } | |
981 | ||
982 | int lv_resume(struct cmd_context *cmd, const char *lvid_s) | |
983 | { | |
984 | return _lv_resume(cmd, lvid_s, 1); | |
985 | } | |
986 | ||
64a95010 AK |
987 | static int _lv_has_open_snapshots(struct logical_volume *lv) |
988 | { | |
989 | struct lv_segment *snap_seg; | |
990 | struct lvinfo info; | |
991 | int r = 0; | |
992 | ||
993 | dm_list_iterate_items_gen(snap_seg, &lv->snapshot_segs, origin_list) { | |
994 | if (!lv_info(lv->vg->cmd, snap_seg->cow, &info, 1, 0)) { | |
995 | r = 1; | |
996 | continue; | |
997 | } | |
998 | ||
999 | if (info.exists && info.open_count) { | |
1000 | log_error("LV %s/%s has open snapshot %s: " | |
1001 | "not deactivating", lv->vg->name, lv->name, | |
1002 | snap_seg->cow->name); | |
1003 | r = 1; | |
1004 | } | |
1005 | } | |
1006 | ||
1007 | return r; | |
1008 | } | |
1009 | ||
be326a2f | 1010 | int lv_deactivate(struct cmd_context *cmd, const char *lvid_s) |
f4cbeaf0 AK |
1011 | { |
1012 | struct logical_volume *lv; | |
199e490e | 1013 | struct lvinfo info; |
095bbca6 | 1014 | int r = 0; |
f4cbeaf0 | 1015 | |
d1d9800e AK |
1016 | if (!activation()) |
1017 | return 1; | |
1018 | ||
7a593325 | 1019 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 1020 | goto out; |
f4cbeaf0 | 1021 | |
20c5fcf7 AK |
1022 | if (test_mode()) { |
1023 | _skip("Deactivating '%s'.", lv->name); | |
095bbca6 MB |
1024 | r = 1; |
1025 | goto out; | |
20c5fcf7 AK |
1026 | } |
1027 | ||
a6b22cf3 | 1028 | if (!lv_info(cmd, lv, &info, 1, 0)) |
095bbca6 | 1029 | goto_out; |
41967a02 | 1030 | |
095bbca6 MB |
1031 | if (!info.exists) { |
1032 | r = 1; | |
1033 | goto out; | |
1034 | } | |
f4cbeaf0 | 1035 | |
64a95010 AK |
1036 | if (lv_is_visible(lv)) { |
1037 | if (info.open_count) { | |
1038 | log_error("LV %s/%s in use: not deactivating", | |
1039 | lv->vg->name, lv->name); | |
1040 | goto out; | |
1041 | } | |
1042 | if (lv_is_origin(lv) && _lv_has_open_snapshots(lv)) | |
1043 | goto_out; | |
0cf96f33 AK |
1044 | } |
1045 | ||
c1fdeec9 MB |
1046 | lv_calculate_readahead(lv, NULL); |
1047 | ||
8a37910d | 1048 | if (!monitor_dev_for_events(cmd, lv, 0)) |
ed09d7e3 | 1049 | stack; |
15d91f5a | 1050 | |
914c9723 AK |
1051 | memlock_inc(); |
1052 | r = _lv_deactivate(lv); | |
1053 | memlock_dec(); | |
1054 | fs_unlock(); | |
1055 | ||
89a6cdfd MB |
1056 | if (!lv_info(cmd, lv, &info, 1, 0) || info.exists) |
1057 | r = 0; | |
095bbca6 MB |
1058 | out: |
1059 | if (lv) | |
1060 | vg_release(lv->vg); | |
1061 | ||
914c9723 | 1062 | return r; |
f4cbeaf0 AK |
1063 | } |
1064 | ||
658b5812 AK |
1065 | /* Test if LV passes filter */ |
1066 | int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s, | |
1067 | int *activate_lv) | |
1068 | { | |
1069 | struct logical_volume *lv; | |
095bbca6 | 1070 | int r = 0; |
658b5812 | 1071 | |
095bbca6 MB |
1072 | if (!activation()) { |
1073 | *activate_lv = 1; | |
1074 | return 1; | |
1075 | } | |
658b5812 | 1076 | |
424dd43e | 1077 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 1078 | goto out; |
658b5812 AK |
1079 | |
1080 | if (!_passes_activation_filter(cmd, lv)) { | |
1081 | log_verbose("Not activating %s/%s due to config file settings", | |
1082 | lv->vg->name, lv->name); | |
1083 | *activate_lv = 0; | |
095bbca6 MB |
1084 | } else |
1085 | *activate_lv = 1; | |
1086 | r = 1; | |
1087 | out: | |
1088 | if (lv) | |
1089 | vg_release(lv->vg); | |
658b5812 | 1090 | |
095bbca6 | 1091 | return r; |
658b5812 AK |
1092 | } |
1093 | ||
07d31831 AK |
1094 | static int _lv_activate(struct cmd_context *cmd, const char *lvid_s, |
1095 | int exclusive, int filter) | |
f4cbeaf0 AK |
1096 | { |
1097 | struct logical_volume *lv; | |
199e490e | 1098 | struct lvinfo info; |
095bbca6 | 1099 | int r = 0; |
f4cbeaf0 | 1100 | |
d1d9800e AK |
1101 | if (!activation()) |
1102 | return 1; | |
1103 | ||
424dd43e | 1104 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
095bbca6 | 1105 | goto out; |
f4cbeaf0 | 1106 | |
658b5812 | 1107 | if (filter && !_passes_activation_filter(cmd, lv)) { |
de17d760 AK |
1108 | log_verbose("Not activating %s/%s due to config file settings", |
1109 | lv->vg->name, lv->name); | |
095bbca6 | 1110 | goto out; |
de17d760 AK |
1111 | } |
1112 | ||
cda35408 | 1113 | if ((!lv->vg->cmd->partial_activation) && (lv->status & PARTIAL_LV)) { |
8c5bcdab AK |
1114 | log_error("Refusing activation of partial LV %s. Use --partial to override.", |
1115 | lv->name); | |
095bbca6 | 1116 | goto_out; |
8c5bcdab AK |
1117 | } |
1118 | ||
b4048242 PR |
1119 | if (lv_has_unknown_segments(lv)) { |
1120 | log_error("Refusing activation of LV %s containing " | |
1121 | "an unrecognised segment.", lv->name); | |
1122 | goto_out; | |
1123 | } | |
1124 | ||
20c5fcf7 AK |
1125 | if (test_mode()) { |
1126 | _skip("Activating '%s'.", lv->name); | |
095bbca6 MB |
1127 | r = 1; |
1128 | goto out; | |
20c5fcf7 AK |
1129 | } |
1130 | ||
a6b22cf3 | 1131 | if (!lv_info(cmd, lv, &info, 0, 0)) |
095bbca6 | 1132 | goto_out; |
8c013da4 | 1133 | |
095bbca6 MB |
1134 | if (info.exists && !info.suspended && info.live_table) { |
1135 | r = 1; | |
1136 | goto out; | |
1137 | } | |
f4cbeaf0 | 1138 | |
c1fdeec9 MB |
1139 | lv_calculate_readahead(lv, NULL); |
1140 | ||
07d31831 AK |
1141 | if (exclusive) |
1142 | lv->status |= ACTIVATE_EXCL; | |
1143 | ||
914c9723 | 1144 | memlock_inc(); |
75b37a11 AK |
1145 | if (!(r = _lv_activate_lv(lv))) |
1146 | stack; | |
914c9723 AK |
1147 | memlock_dec(); |
1148 | fs_unlock(); | |
1149 | ||
5efd97f1 | 1150 | if (r && !monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 1151 | stack; |
15d91f5a | 1152 | |
095bbca6 MB |
1153 | out: |
1154 | if (lv) | |
1155 | vg_release(lv->vg); | |
1156 | ||
914c9723 | 1157 | return r; |
f4cbeaf0 | 1158 | } |
199e490e | 1159 | |
658b5812 | 1160 | /* Activate LV */ |
07d31831 | 1161 | int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 1162 | { |
75b37a11 AK |
1163 | if (!_lv_activate(cmd, lvid_s, exclusive, 0)) |
1164 | return_0; | |
1165 | ||
1166 | return 1; | |
658b5812 AK |
1167 | } |
1168 | ||
1169 | /* Activate LV only if it passes filter */ | |
07d31831 | 1170 | int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 1171 | { |
75b37a11 AK |
1172 | if (!_lv_activate(cmd, lvid_s, exclusive, 1)) |
1173 | return_0; | |
1174 | ||
1175 | return 1; | |
658b5812 AK |
1176 | } |
1177 | ||
f7dd6d84 AK |
1178 | int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv) |
1179 | { | |
1180 | struct lvinfo info; | |
1181 | int r = 1; | |
1182 | ||
8b076648 | 1183 | if (!lv) { |
2262b320 | 1184 | r = dm_mknodes(NULL); |
8b076648 AK |
1185 | fs_unlock(); |
1186 | return r; | |
1187 | } | |
1188 | ||
a6b22cf3 | 1189 | if (!_lv_info(cmd, lv, 1, &info, 0, 0, 0)) |
5f4b2acf | 1190 | return_0; |
f7dd6d84 | 1191 | |
3ad47d16 PR |
1192 | if (info.exists) { |
1193 | if (lv_is_visible(lv)) | |
1194 | r = dev_manager_lv_mknodes(lv); | |
1195 | } else | |
f09fe0ad | 1196 | r = dev_manager_lv_rmnodes(lv); |
f7dd6d84 AK |
1197 | |
1198 | fs_unlock(); | |
1199 | ||
1200 | return r; | |
1201 | } | |
1202 | ||
352a99b9 AK |
1203 | /* |
1204 | * Does PV use VG somewhere in its construction? | |
1205 | * Returns 1 on failure. | |
1206 | */ | |
898e6f8e | 1207 | int pv_uses_vg(struct physical_volume *pv, |
3e3d5d85 | 1208 | struct volume_group *vg) |
352a99b9 | 1209 | { |
352a99b9 AK |
1210 | if (!activation()) |
1211 | return 0; | |
1212 | ||
1213 | if (!dm_is_dm_major(MAJOR(pv->dev->dev))) | |
1214 | return 0; | |
1215 | ||
898e6f8e | 1216 | return dev_manager_device_uses_vg(pv->dev, vg); |
352a99b9 AK |
1217 | } |
1218 | ||
2293567c AK |
1219 | void activation_release(void) |
1220 | { | |
1221 | dev_manager_release(); | |
1222 | } | |
1223 | ||
914c9723 AK |
1224 | void activation_exit(void) |
1225 | { | |
1226 | dev_manager_exit(); | |
1227 | } | |
199e490e | 1228 | #endif |