]>
Commit | Line | Data |
---|---|---|
b1713d28 | 1 | /* |
6606c3ae | 2 | * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. |
5f4b2acf | 3 | * Copyright (C) 2004-2005 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 | |
9 | * of the GNU General Public License v.2. | |
10 | * | |
11 | * You should have received a copy of the GNU General Public License | |
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 AK |
54 | int list_segment_modules(struct dm_pool *mem, const struct lv_segment *seg, |
55 | struct list *modules) | |
56 | { | |
57 | unsigned int s; | |
58 | struct lv_segment *seg2, *snap_seg; | |
59 | struct list *snh; | |
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)) | |
68 | list_iterate(snh, &seg->lv->snapshot_segs) | |
69 | if (!list_lv_modules(mem, | |
70 | list_struct_base(snh, | |
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, | |
103 | struct list *modules) | |
104 | { | |
105 | struct lv_segment *seg; | |
106 | ||
107 | list_iterate_items(seg, &lv->segments) | |
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 | } | |
0ce83a83 | 144 | int target_present(const char *target_name, int use_modprobe) |
d1f4953a AK |
145 | { |
146 | return 0; | |
147 | } | |
f894b4b1 | 148 | int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info, |
e9c761b8 | 149 | int with_open_count) |
199e490e AK |
150 | { |
151 | return 0; | |
152 | } | |
4bd9480d | 153 | int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, |
e9c761b8 | 154 | struct lvinfo *info, int with_open_count) |
4bd9480d AK |
155 | { |
156 | return 0; | |
157 | } | |
472ac5bd | 158 | int lv_snapshot_percent(const struct logical_volume *lv, float *percent) |
199e490e AK |
159 | { |
160 | return 0; | |
161 | } | |
472ac5bd AK |
162 | int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv, |
163 | int wait, float *percent, uint32_t *event_nr) | |
10b29b8d AK |
164 | { |
165 | return 0; | |
166 | } | |
199e490e AK |
167 | int lvs_in_vg_activated(struct volume_group *vg) |
168 | { | |
169 | return 0; | |
170 | } | |
1bf5aae9 | 171 | int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg) |
b45c9f49 AK |
172 | { |
173 | return 0; | |
174 | } | |
199e490e AK |
175 | int lvs_in_vg_opened(struct volume_group *vg) |
176 | { | |
177 | return 0; | |
178 | } | |
658b5812 AK |
179 | int lv_suspend(struct cmd_context *cmd, const char *lvid_s) |
180 | { | |
181 | return 1; | |
182 | } | |
199e490e AK |
183 | int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s) |
184 | { | |
185 | return 1; | |
186 | } | |
658b5812 AK |
187 | int lv_resume(struct cmd_context *cmd, const char *lvid_s) |
188 | { | |
189 | return 1; | |
190 | } | |
199e490e AK |
191 | int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s) |
192 | { | |
193 | return 1; | |
194 | } | |
195 | int lv_deactivate(struct cmd_context *cmd, const char *lvid_s) | |
196 | { | |
197 | return 1; | |
198 | } | |
658b5812 AK |
199 | int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s, |
200 | int *activate_lv) | |
201 | { | |
202 | return 1; | |
203 | } | |
07d31831 | 204 | int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
199e490e AK |
205 | { |
206 | return 1; | |
207 | } | |
07d31831 | 208 | int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 AK |
209 | { |
210 | return 1; | |
211 | } | |
f7dd6d84 AK |
212 | |
213 | int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv) | |
214 | { | |
215 | return 1; | |
216 | } | |
217 | ||
0ce83a83 | 218 | int pv_uses_vg(struct physical_volume *pv, |
3e3d5d85 | 219 | struct volume_group *vg) |
352a99b9 AK |
220 | { |
221 | return 0; | |
222 | } | |
223 | ||
2293567c AK |
224 | void activation_release(void) |
225 | { | |
226 | return; | |
227 | } | |
228 | ||
914c9723 AK |
229 | void activation_exit(void) |
230 | { | |
231 | return; | |
232 | } | |
199e490e AK |
233 | |
234 | #else /* DEVMAPPER_SUPPORT */ | |
235 | ||
d1d9800e AK |
236 | static int _activation = 1; |
237 | ||
8ef2b021 | 238 | void set_activation(int act) |
d1d9800e | 239 | { |
8ef2b021 | 240 | if (act == _activation) |
d1d9800e AK |
241 | return; |
242 | ||
8ef2b021 | 243 | _activation = act; |
d1d9800e AK |
244 | if (_activation) |
245 | log_verbose("Activation enabled. Device-mapper kernel " | |
246 | "driver will be used."); | |
247 | else | |
e7ddf416 | 248 | log_warn("WARNING: Activation disabled. No device-mapper " |
bfe2b548 | 249 | "interaction will be attempted."); |
d1d9800e AK |
250 | } |
251 | ||
8ef2b021 | 252 | int activation(void) |
d1d9800e AK |
253 | { |
254 | return _activation; | |
255 | } | |
256 | ||
de17d760 AK |
257 | static int _passes_activation_filter(struct cmd_context *cmd, |
258 | struct logical_volume *lv) | |
259 | { | |
25579907 | 260 | const struct config_node *cn; |
de17d760 AK |
261 | struct config_value *cv; |
262 | char *str; | |
263 | char path[PATH_MAX]; | |
264 | ||
2293567c | 265 | if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) { |
25579907 | 266 | /* If no host tags defined, activate */ |
de17d760 AK |
267 | if (list_empty(&cmd->tags)) |
268 | return 1; | |
269 | ||
270 | /* If any host tag matches any LV or VG tag, activate */ | |
271 | if (str_list_match_list(&cmd->tags, &lv->tags) || | |
272 | str_list_match_list(&cmd->tags, &lv->vg->tags)) | |
273 | return 1; | |
274 | ||
275 | /* Don't activate */ | |
276 | return 0; | |
277 | } | |
278 | ||
279 | for (cv = cn->v; cv; cv = cv->next) { | |
280 | if (cv->type != CFG_STRING) { | |
281 | log_error("Ignoring invalid string in config file " | |
282 | "activation/volume_list"); | |
283 | continue; | |
284 | } | |
285 | str = cv->v.str; | |
286 | if (!*str) { | |
287 | log_error("Ignoring empty string in config file " | |
288 | "activation/volume_list"); | |
289 | continue; | |
290 | } | |
291 | ||
292 | /* Tag? */ | |
293 | if (*str == '@') { | |
294 | str++; | |
295 | if (!*str) { | |
296 | log_error("Ignoring empty tag in config file " | |
297 | "activation/volume_list"); | |
298 | continue; | |
299 | } | |
300 | /* If any host tag matches any LV or VG tag, activate */ | |
301 | if (!strcmp(str, "*")) { | |
302 | if (str_list_match_list(&cmd->tags, &lv->tags) | |
303 | || str_list_match_list(&cmd->tags, | |
304 | &lv->vg->tags)) | |
305 | return 1; | |
306 | else | |
307 | continue; | |
308 | } | |
309 | /* If supplied tag matches LV or VG tag, activate */ | |
310 | if (str_list_match_item(&lv->tags, str) || | |
311 | str_list_match_item(&lv->vg->tags, str)) | |
312 | return 1; | |
313 | else | |
314 | continue; | |
315 | } | |
13835b5f | 316 | if (!strchr(str, '/')) { |
de17d760 AK |
317 | /* vgname supplied */ |
318 | if (!strcmp(str, lv->vg->name)) | |
319 | return 1; | |
320 | else | |
321 | continue; | |
322 | } | |
323 | /* vgname/lvname */ | |
0550c1b6 | 324 | if (dm_snprintf(path, sizeof(path), "%s/%s", lv->vg->name, |
de17d760 | 325 | lv->name) < 0) { |
0550c1b6 | 326 | log_error("dm_snprintf error from %s/%s", lv->vg->name, |
de17d760 AK |
327 | lv->name); |
328 | continue; | |
329 | } | |
330 | if (!strcmp(path, str)) | |
331 | return 1; | |
332 | } | |
333 | ||
334 | return 0; | |
335 | } | |
336 | ||
fae0c576 AK |
337 | int library_version(char *version, size_t size) |
338 | { | |
d1d9800e AK |
339 | if (!activation()) |
340 | return 0; | |
341 | ||
f894b4b1 | 342 | return dm_get_library_version(version, size); |
fae0c576 AK |
343 | } |
344 | ||
fae0c576 AK |
345 | int driver_version(char *version, size_t size) |
346 | { | |
d1d9800e AK |
347 | if (!activation()) |
348 | return 0; | |
349 | ||
fae0c576 | 350 | log_very_verbose("Getting driver version"); |
fae0c576 | 351 | |
f894b4b1 | 352 | return dm_driver_version(version, size); |
fae0c576 AK |
353 | } |
354 | ||
bbf83db1 AK |
355 | int target_version(const char *target_name, uint32_t *maj, |
356 | uint32_t *min, uint32_t *patchlevel) | |
d1f4953a AK |
357 | { |
358 | int r = 0; | |
359 | struct dm_task *dmt; | |
360 | struct dm_versions *target, *last_target; | |
361 | ||
d1f4953a | 362 | log_very_verbose("Getting target version for %s", target_name); |
5f4b2acf AK |
363 | if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS))) |
364 | return_0; | |
d1f4953a AK |
365 | |
366 | if (!dm_task_run(dmt)) { | |
367 | log_debug("Failed to get %s target version", target_name); | |
368 | /* Assume this was because LIST_VERSIONS isn't supported */ | |
369 | return 1; | |
370 | } | |
371 | ||
372 | target = dm_task_get_versions(dmt); | |
373 | ||
374 | do { | |
375 | last_target = target; | |
376 | ||
377 | if (!strcmp(target_name, target->name)) { | |
378 | r = 1; | |
bbf83db1 AK |
379 | *maj = target->version[0]; |
380 | *min = target->version[1]; | |
381 | *patchlevel = target->version[2]; | |
d1f4953a AK |
382 | goto out; |
383 | } | |
384 | ||
385 | target = (void *) target + target->next; | |
386 | } while (last_target != target); | |
387 | ||
388 | out: | |
389 | dm_task_destroy(dmt); | |
390 | ||
391 | return r; | |
392 | } | |
393 | ||
5f4b2acf | 394 | int target_present(const char *target_name, int use_modprobe) |
f894b4b1 | 395 | { |
bbf83db1 | 396 | uint32_t maj, min, patchlevel; |
03b49fe1 | 397 | #ifdef MODPROBE_CMD |
f894b4b1 | 398 | char module[128]; |
03b49fe1 | 399 | #endif |
f894b4b1 AK |
400 | |
401 | if (!activation()) | |
402 | return 0; | |
403 | ||
404 | #ifdef MODPROBE_CMD | |
5f4b2acf | 405 | if (use_modprobe) { |
bbf83db1 | 406 | if (target_version(target_name, &maj, &min, &patchlevel)) |
5f4b2acf | 407 | return 1; |
f894b4b1 | 408 | |
0550c1b6 | 409 | if (dm_snprintf(module, sizeof(module), "dm-%s", target_name) |
5f4b2acf AK |
410 | < 0) { |
411 | log_error("target_present module name too long: %s", | |
412 | target_name); | |
413 | return 0; | |
414 | } | |
f894b4b1 | 415 | |
5f4b2acf AK |
416 | if (!exec_cmd(MODPROBE_CMD, module, "", "")) |
417 | return_0; | |
f894b4b1 AK |
418 | } |
419 | #endif | |
420 | ||
bbf83db1 | 421 | return target_version(target_name, &maj, &min, &patchlevel); |
f894b4b1 AK |
422 | } |
423 | ||
de6c9183 JT |
424 | /* |
425 | * Returns 1 if info structure populated, else 0 on failure. | |
426 | */ | |
f894b4b1 | 427 | static int _lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int with_mknodes, |
b45c9f49 | 428 | struct lvinfo *info, int with_open_count, unsigned by_uuid_only) |
37ed70b9 | 429 | { |
199e490e | 430 | struct dm_info dminfo; |
b45c9f49 | 431 | char *name = NULL; |
4a624ca0 | 432 | |
d1d9800e AK |
433 | if (!activation()) |
434 | return 0; | |
435 | ||
b45c9f49 AK |
436 | if (!by_uuid_only && |
437 | !(name = build_dm_name(cmd->mem, lv->vg->name, lv->name, NULL))) | |
5f4b2acf | 438 | return_0; |
4a624ca0 | 439 | |
f894b4b1 | 440 | log_debug("Getting device info for %s", name); |
03b49fe1 AK |
441 | if (!dev_manager_info(lv->vg->cmd->mem, name, lv, with_mknodes, |
442 | with_open_count, &dminfo)) { | |
b45c9f49 AK |
443 | if (name) |
444 | dm_pool_free(cmd->mem, name); | |
5f4b2acf | 445 | return_0; |
f894b4b1 | 446 | } |
4a624ca0 | 447 | |
199e490e AK |
448 | info->exists = dminfo.exists; |
449 | info->suspended = dminfo.suspended; | |
450 | info->open_count = dminfo.open_count; | |
451 | info->major = dminfo.major; | |
452 | info->minor = dminfo.minor; | |
453 | info->read_only = dminfo.read_only; | |
5f4b2acf AK |
454 | info->live_table = dminfo.live_table; |
455 | info->inactive_table = dminfo.inactive_table; | |
199e490e | 456 | |
b45c9f49 AK |
457 | if (name) |
458 | dm_pool_free(cmd->mem, name); | |
459 | ||
f894b4b1 | 460 | return 1; |
de6c9183 | 461 | } |
a62ee8ad | 462 | |
f894b4b1 | 463 | int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info, |
e9c761b8 | 464 | int with_open_count) |
8c0388e4 | 465 | { |
b45c9f49 | 466 | return _lv_info(cmd, lv, 0, info, with_open_count, 0); |
8c0388e4 AK |
467 | } |
468 | ||
4bd9480d | 469 | int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, |
e9c761b8 | 470 | struct lvinfo *info, int with_open_count) |
4bd9480d AK |
471 | { |
472 | struct logical_volume *lv; | |
473 | ||
7a593325 | 474 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
4bd9480d AK |
475 | return 0; |
476 | ||
b45c9f49 | 477 | return _lv_info(cmd, lv, 0, info, with_open_count, 0); |
4bd9480d AK |
478 | } |
479 | ||
1951dba9 AL |
480 | /* |
481 | * Returns 1 if percent set, else 0 on failure. | |
482 | */ | |
472ac5bd | 483 | int lv_snapshot_percent(const struct logical_volume *lv, float *percent) |
1951dba9 AL |
484 | { |
485 | int r; | |
486 | struct dev_manager *dm; | |
487 | ||
d1d9800e AK |
488 | if (!activation()) |
489 | return 0; | |
490 | ||
5f4b2acf AK |
491 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
492 | return_0; | |
1951dba9 | 493 | |
c826c0d1 | 494 | if (!(r = dev_manager_snapshot_percent(dm, lv, percent))) |
1951dba9 | 495 | stack; |
c826c0d1 | 496 | |
1951dba9 AL |
497 | dev_manager_destroy(dm); |
498 | ||
499 | return r; | |
500 | } | |
501 | ||
10b29b8d | 502 | /* FIXME Merge with snapshot_percent */ |
472ac5bd AK |
503 | int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv, |
504 | int wait, float *percent, uint32_t *event_nr) | |
10b29b8d AK |
505 | { |
506 | int r; | |
507 | struct dev_manager *dm; | |
b65b777d | 508 | struct lvinfo info; |
10b29b8d AK |
509 | |
510 | if (!activation()) | |
511 | return 0; | |
512 | ||
5f4b2acf AK |
513 | if (!lv_info(cmd, lv, &info, 0)) |
514 | return_0; | |
b65b777d AK |
515 | |
516 | if (!info.exists) | |
517 | return 0; | |
518 | ||
5f4b2acf AK |
519 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
520 | return_0; | |
10b29b8d AK |
521 | |
522 | if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, event_nr))) | |
523 | stack; | |
524 | ||
525 | dev_manager_destroy(dm); | |
526 | ||
527 | return r; | |
528 | } | |
529 | ||
b45c9f49 AK |
530 | static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv, |
531 | unsigned by_uuid_only) | |
2ba80b43 | 532 | { |
199e490e | 533 | struct lvinfo info; |
2ba80b43 | 534 | |
b45c9f49 | 535 | if (!_lv_info(cmd, lv, 0, &info, 0, by_uuid_only)) { |
2ba80b43 | 536 | stack; |
de6c9183 | 537 | return -1; |
2ba80b43 JT |
538 | } |
539 | ||
de6c9183 | 540 | return info.exists; |
2ba80b43 JT |
541 | } |
542 | ||
f894b4b1 | 543 | static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv) |
5986ec94 | 544 | { |
199e490e | 545 | struct lvinfo info; |
5986ec94 | 546 | |
f894b4b1 | 547 | if (!lv_info(cmd, lv, &info, 1)) { |
5986ec94 | 548 | stack; |
de6c9183 | 549 | return -1; |
5986ec94 JT |
550 | } |
551 | ||
de6c9183 | 552 | return info.open_count; |
5986ec94 JT |
553 | } |
554 | ||
658b5812 | 555 | static int _lv_activate_lv(struct logical_volume *lv) |
b1713d28 | 556 | { |
6d52fb46 | 557 | int r; |
de6c9183 | 558 | struct dev_manager *dm; |
b1713d28 | 559 | |
5f4b2acf AK |
560 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
561 | return_0; | |
ae2bb665 | 562 | |
0fe3a2c5 | 563 | if (!(r = dev_manager_activate(dm, lv))) |
6d52fb46 | 564 | stack; |
ae2bb665 | 565 | |
de6c9183 | 566 | dev_manager_destroy(dm); |
ae2bb665 | 567 | return r; |
b1713d28 | 568 | } |
a381c45a | 569 | |
5f4b2acf | 570 | static int _lv_preload(struct logical_volume *lv) |
0a5e4a14 | 571 | { |
de6c9183 JT |
572 | int r; |
573 | struct dev_manager *dm; | |
37ed70b9 | 574 | |
5f4b2acf AK |
575 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
576 | return_0; | |
577 | ||
578 | if (!(r = dev_manager_preload(dm, lv))) | |
6d52fb46 | 579 | stack; |
5f4b2acf AK |
580 | |
581 | dev_manager_destroy(dm); | |
582 | return r; | |
583 | } | |
584 | ||
585 | static int _lv_deactivate(struct logical_volume *lv) | |
586 | { | |
587 | int r; | |
588 | struct dev_manager *dm; | |
589 | ||
590 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) | |
591 | return_0; | |
37ed70b9 | 592 | |
de6c9183 | 593 | if (!(r = dev_manager_deactivate(dm, lv))) |
37ed70b9 | 594 | stack; |
37ed70b9 | 595 | |
de6c9183 JT |
596 | dev_manager_destroy(dm); |
597 | return r; | |
37ed70b9 JT |
598 | } |
599 | ||
9cd3426d | 600 | static int _lv_suspend_lv(struct logical_volume *lv, int lockfs) |
4a624ca0 | 601 | { |
20c5fcf7 AK |
602 | int r; |
603 | struct dev_manager *dm; | |
c2d72fd4 | 604 | |
5f4b2acf AK |
605 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
606 | return_0; | |
0a5e4a14 | 607 | |
9cd3426d | 608 | if (!(r = dev_manager_suspend(dm, lv, lockfs))) |
37ed70b9 | 609 | stack; |
0a5e4a14 | 610 | |
20c5fcf7 AK |
611 | dev_manager_destroy(dm); |
612 | return r; | |
6d52fb46 | 613 | } |
4a624ca0 | 614 | |
8c013da4 | 615 | /* |
f75c11ed | 616 | * These two functions return the number of visible LVs in the state, |
8c013da4 AK |
617 | * or -1 on error. |
618 | */ | |
b45c9f49 | 619 | static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only) |
f047219b | 620 | { |
60f13f01 | 621 | struct lv_list *lvl; |
94b8220f | 622 | int count = 0; |
37ed70b9 | 623 | |
d1d9800e AK |
624 | if (!activation()) |
625 | return 0; | |
626 | ||
60f13f01 AK |
627 | list_iterate_items(lvl, &vg->lvs) { |
628 | if (lvl->lv->status & VISIBLE_LV) | |
b45c9f49 | 629 | count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1); |
37ed70b9 JT |
630 | } |
631 | ||
632 | return count; | |
f047219b | 633 | } |
2ba80b43 | 634 | |
b45c9f49 AK |
635 | int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg) |
636 | { | |
637 | return _lvs_in_vg_activated(vg, 1); | |
638 | } | |
639 | ||
640 | int lvs_in_vg_activated(struct volume_group *vg) | |
641 | { | |
642 | return _lvs_in_vg_activated(vg, 0); | |
643 | } | |
644 | ||
08c9ff43 | 645 | int lvs_in_vg_opened(const struct volume_group *vg) |
2ba80b43 | 646 | { |
08c9ff43 | 647 | const struct lv_list *lvl; |
94b8220f | 648 | int count = 0; |
2ba80b43 | 649 | |
d1d9800e AK |
650 | if (!activation()) |
651 | return 0; | |
652 | ||
60f13f01 AK |
653 | list_iterate_items(lvl, &vg->lvs) { |
654 | if (lvl->lv->status & VISIBLE_LV) | |
f894b4b1 | 655 | count += (_lv_open_count(vg->cmd, lvl->lv) > 0); |
2ba80b43 JT |
656 | } |
657 | ||
658 | return count; | |
659 | } | |
413cc918 | 660 | |
3e3d5d85 | 661 | /* |
8a37910d AK |
662 | * Returns 0 if an attempt to (un)monitor the device failed. |
663 | * Returns 1 otherwise. | |
3e3d5d85 | 664 | */ |
8a37910d AK |
665 | int monitor_dev_for_events(struct cmd_context *cmd, |
666 | struct logical_volume *lv, int monitor) | |
15d91f5a | 667 | { |
ed09d7e3 | 668 | #ifdef DMEVENTD |
8a37910d AK |
669 | int i, pending = 0, monitored; |
670 | int r = 1; | |
15d91f5a AK |
671 | struct list *tmp; |
672 | struct lv_segment *seg; | |
24f4552b | 673 | int (*monitor_fn) (struct lv_segment *s, int e); |
15d91f5a | 674 | |
20db8ffc AK |
675 | /* skip dmeventd code altogether */ |
676 | if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE) | |
677 | return 1; | |
678 | ||
8a37910d AK |
679 | /* |
680 | * Nothing to do if dmeventd configured not to be used. | |
681 | */ | |
682 | if (monitor && !dmeventd_monitor_mode()) | |
3e3d5d85 AK |
683 | return 1; |
684 | ||
15d91f5a AK |
685 | list_iterate(tmp, &lv->segments) { |
686 | seg = list_item(tmp, struct lv_segment); | |
687 | ||
8ef6eb30 AK |
688 | if (!seg_monitored(seg) || (seg->status & PVMOVE)) |
689 | continue; | |
8a37910d AK |
690 | |
691 | monitor_fn = NULL; | |
ed09d7e3 | 692 | |
8ef6eb30 | 693 | /* Check monitoring status */ |
8a37910d AK |
694 | if (seg->segtype->ops->target_monitored) |
695 | monitored = seg->segtype->ops->target_monitored(seg, &pending); | |
8ef6eb30 AK |
696 | else |
697 | continue; /* segtype doesn't support registration */ | |
698 | ||
699 | /* | |
700 | * FIXME: We should really try again if pending | |
701 | */ | |
8a37910d | 702 | monitored = (pending) ? 0 : monitored; |
8ef6eb30 | 703 | |
8a37910d AK |
704 | if (monitor) { |
705 | if (monitored) | |
8ef6eb30 | 706 | log_verbose("%s/%s already monitored.", lv->vg->name, lv->name); |
8a37910d AK |
707 | else if (seg->segtype->ops->target_monitor_events) |
708 | monitor_fn = seg->segtype->ops->target_monitor_events; | |
8ef6eb30 | 709 | } else { |
8a37910d | 710 | if (!monitored) |
8ef6eb30 | 711 | log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name); |
8a37910d AK |
712 | else if (seg->segtype->ops->target_unmonitor_events) |
713 | monitor_fn = seg->segtype->ops->target_unmonitor_events; | |
8ef6eb30 | 714 | } |
15d91f5a | 715 | |
8ef6eb30 | 716 | /* Do [un]monitor */ |
8a37910d | 717 | if (!monitor_fn) |
3e3d5d85 | 718 | continue; |
e24e7130 | 719 | |
8a37910d AK |
720 | log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name); |
721 | ||
3e3d5d85 | 722 | /* FIXME specify events */ |
57fc4cc0 | 723 | if (!monitor_fn(seg, 0)) { |
8a37910d AK |
724 | log_error("%s/%s: %s segment monitoring function failed.", |
725 | lv->vg->name, lv->name, seg->segtype->name); | |
726 | return 0; | |
e24e7130 | 727 | } |
3e3d5d85 | 728 | |
8ef6eb30 AK |
729 | /* Check [un]monitor results */ |
730 | /* Try a couple times if pending, but not forever... */ | |
731 | for (i = 0; i < 10; i++) { | |
732 | pending = 0; | |
8a37910d | 733 | monitored = seg->segtype->ops->target_monitored(seg, &pending); |
8ef6eb30 | 734 | if (pending || |
8a37910d AK |
735 | (!monitored && monitor) || |
736 | (monitored && !monitor)) | |
737 | log_very_verbose("%s/%s %smonitoring still pending: waiting...", | |
738 | lv->vg->name, lv->name, monitor ? "" : "un"); | |
8ef6eb30 AK |
739 | else |
740 | break; | |
741 | sleep(1); | |
742 | } | |
743 | ||
8a37910d | 744 | r = (monitored && monitor) || (!monitored && !monitor); |
15d91f5a | 745 | } |
e6493477 | 746 | |
3e3d5d85 AK |
747 | return r; |
748 | #else | |
15d91f5a | 749 | return 1; |
3e3d5d85 | 750 | #endif |
15d91f5a | 751 | } |
15d91f5a | 752 | |
658b5812 AK |
753 | static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s, |
754 | int error_if_not_suspended) | |
413cc918 | 755 | { |
8b888354 | 756 | struct logical_volume *lv, *lv_pre; |
199e490e | 757 | struct lvinfo info; |
9cd3426d | 758 | int lockfs = 0; |
413cc918 | 759 | |
d1d9800e AK |
760 | if (!activation()) |
761 | return 1; | |
762 | ||
8b888354 | 763 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
e408d62e | 764 | return_0; |
8b888354 | 765 | |
7a593325 | 766 | /* Use precommitted metadata if present */ |
8b888354 | 767 | if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1))) |
e408d62e | 768 | return_0; |
413cc918 | 769 | |
20c5fcf7 AK |
770 | if (test_mode()) { |
771 | _skip("Suspending '%s'.", lv->name); | |
711f7fc6 | 772 | return 1; |
20c5fcf7 AK |
773 | } |
774 | ||
5f4b2acf AK |
775 | if (!lv_info(cmd, lv, &info, 0)) |
776 | return_0; | |
41967a02 | 777 | |
914c9723 | 778 | if (!info.exists || info.suspended) |
658b5812 | 779 | return error_if_not_suspended ? 0 : 1; |
914c9723 | 780 | |
5f4b2acf | 781 | /* If VG was precommitted, preload devices for the LV */ |
8b888354 AK |
782 | if ((lv_pre->vg->status & PRECOMMITTED)) { |
783 | if (!_lv_preload(lv_pre)) { | |
5f4b2acf AK |
784 | /* FIXME Revert preloading */ |
785 | return_0; | |
786 | } | |
787 | } | |
788 | ||
8a37910d | 789 | if (!monitor_dev_for_events(cmd, lv, 0)) |
e24e7130 | 790 | /* FIXME Consider aborting here */ |
ed09d7e3 AK |
791 | stack; |
792 | ||
914c9723 | 793 | memlock_inc(); |
9cd3426d AK |
794 | |
795 | if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)) | |
796 | lockfs = 1; | |
797 | ||
798 | if (!_lv_suspend_lv(lv, lockfs)) { | |
914c9723 AK |
799 | memlock_dec(); |
800 | fs_unlock(); | |
801 | return 0; | |
802 | } | |
8c013da4 | 803 | |
413cc918 AK |
804 | return 1; |
805 | } | |
806 | ||
658b5812 AK |
807 | /* Returns success if the device is not active */ |
808 | int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s) | |
809 | { | |
810 | return _lv_suspend(cmd, lvid_s, 0); | |
811 | } | |
812 | ||
813 | int lv_suspend(struct cmd_context *cmd, const char *lvid_s) | |
814 | { | |
815 | return _lv_suspend(cmd, lvid_s, 1); | |
816 | } | |
817 | ||
818 | static int _lv_resume(struct cmd_context *cmd, const char *lvid_s, | |
819 | int error_if_not_active) | |
413cc918 AK |
820 | { |
821 | struct logical_volume *lv; | |
199e490e | 822 | struct lvinfo info; |
413cc918 | 823 | |
d1d9800e AK |
824 | if (!activation()) |
825 | return 1; | |
826 | ||
7a593325 | 827 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
413cc918 AK |
828 | return 0; |
829 | ||
20c5fcf7 AK |
830 | if (test_mode()) { |
831 | _skip("Resuming '%s'.", lv->name); | |
711f7fc6 | 832 | return 1; |
20c5fcf7 AK |
833 | } |
834 | ||
5f4b2acf AK |
835 | if (!lv_info(cmd, lv, &info, 0)) |
836 | return_0; | |
41967a02 | 837 | |
914c9723 | 838 | if (!info.exists || !info.suspended) |
658b5812 | 839 | return error_if_not_active ? 0 : 1; |
914c9723 | 840 | |
658b5812 | 841 | if (!_lv_activate_lv(lv)) |
914c9723 AK |
842 | return 0; |
843 | ||
844 | memlock_dec(); | |
845 | fs_unlock(); | |
413cc918 | 846 | |
8a37910d | 847 | if (!monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 848 | stack; |
15d91f5a | 849 | |
413cc918 AK |
850 | return 1; |
851 | } | |
852 | ||
658b5812 AK |
853 | /* Returns success if the device is not active */ |
854 | int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s) | |
855 | { | |
856 | return _lv_resume(cmd, lvid_s, 0); | |
857 | } | |
858 | ||
859 | int lv_resume(struct cmd_context *cmd, const char *lvid_s) | |
860 | { | |
861 | return _lv_resume(cmd, lvid_s, 1); | |
862 | } | |
863 | ||
be326a2f | 864 | int lv_deactivate(struct cmd_context *cmd, const char *lvid_s) |
f4cbeaf0 AK |
865 | { |
866 | struct logical_volume *lv; | |
199e490e | 867 | struct lvinfo info; |
914c9723 | 868 | int r; |
f4cbeaf0 | 869 | |
d1d9800e AK |
870 | if (!activation()) |
871 | return 1; | |
872 | ||
7a593325 | 873 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
f4cbeaf0 AK |
874 | return 0; |
875 | ||
20c5fcf7 AK |
876 | if (test_mode()) { |
877 | _skip("Deactivating '%s'.", lv->name); | |
711f7fc6 | 878 | return 1; |
20c5fcf7 AK |
879 | } |
880 | ||
5f4b2acf AK |
881 | if (!lv_info(cmd, lv, &info, 1)) |
882 | return_0; | |
41967a02 | 883 | |
914c9723 AK |
884 | if (!info.exists) |
885 | return 1; | |
f4cbeaf0 | 886 | |
c1f50521 | 887 | if (info.open_count && (lv->status & VISIBLE_LV)) { |
5f4b2acf | 888 | log_error("LV %s/%s in use: not deactivating", lv->vg->name, |
0cf96f33 AK |
889 | lv->name); |
890 | return 0; | |
891 | } | |
892 | ||
8a37910d | 893 | if (!monitor_dev_for_events(cmd, lv, 0)) |
ed09d7e3 | 894 | stack; |
15d91f5a | 895 | |
914c9723 AK |
896 | memlock_inc(); |
897 | r = _lv_deactivate(lv); | |
898 | memlock_dec(); | |
899 | fs_unlock(); | |
900 | ||
901 | return r; | |
f4cbeaf0 AK |
902 | } |
903 | ||
658b5812 AK |
904 | /* Test if LV passes filter */ |
905 | int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s, | |
906 | int *activate_lv) | |
907 | { | |
908 | struct logical_volume *lv; | |
909 | ||
910 | if (!activation()) | |
911 | goto activate; | |
912 | ||
7a593325 | 913 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
658b5812 AK |
914 | return 0; |
915 | ||
916 | if (!_passes_activation_filter(cmd, lv)) { | |
917 | log_verbose("Not activating %s/%s due to config file settings", | |
918 | lv->vg->name, lv->name); | |
919 | *activate_lv = 0; | |
920 | return 1; | |
921 | } | |
922 | ||
923 | activate: | |
924 | *activate_lv = 1; | |
925 | return 1; | |
926 | } | |
927 | ||
07d31831 AK |
928 | static int _lv_activate(struct cmd_context *cmd, const char *lvid_s, |
929 | int exclusive, int filter) | |
f4cbeaf0 AK |
930 | { |
931 | struct logical_volume *lv; | |
199e490e | 932 | struct lvinfo info; |
914c9723 | 933 | int r; |
f4cbeaf0 | 934 | |
d1d9800e AK |
935 | if (!activation()) |
936 | return 1; | |
937 | ||
7a593325 | 938 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
f4cbeaf0 AK |
939 | return 0; |
940 | ||
658b5812 | 941 | if (filter && !_passes_activation_filter(cmd, lv)) { |
de17d760 AK |
942 | log_verbose("Not activating %s/%s due to config file settings", |
943 | lv->vg->name, lv->name); | |
944 | return 0; | |
945 | } | |
946 | ||
20c5fcf7 AK |
947 | if (test_mode()) { |
948 | _skip("Activating '%s'.", lv->name); | |
711f7fc6 | 949 | return 1; |
20c5fcf7 AK |
950 | } |
951 | ||
5f4b2acf AK |
952 | if (!lv_info(cmd, lv, &info, 0)) |
953 | return_0; | |
8c013da4 | 954 | |
5f4b2acf | 955 | if (info.exists && !info.suspended && info.live_table) |
914c9723 | 956 | return 1; |
f4cbeaf0 | 957 | |
07d31831 AK |
958 | if (exclusive) |
959 | lv->status |= ACTIVATE_EXCL; | |
960 | ||
914c9723 | 961 | memlock_inc(); |
658b5812 | 962 | r = _lv_activate_lv(lv); |
914c9723 AK |
963 | memlock_dec(); |
964 | fs_unlock(); | |
965 | ||
5efd97f1 | 966 | if (r && !monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 967 | stack; |
15d91f5a | 968 | |
914c9723 | 969 | return r; |
f4cbeaf0 | 970 | } |
199e490e | 971 | |
658b5812 | 972 | /* Activate LV */ |
07d31831 | 973 | int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 974 | { |
07d31831 | 975 | return _lv_activate(cmd, lvid_s, exclusive, 0); |
658b5812 AK |
976 | } |
977 | ||
978 | /* Activate LV only if it passes filter */ | |
07d31831 | 979 | int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 980 | { |
07d31831 | 981 | return _lv_activate(cmd, lvid_s, exclusive, 1); |
658b5812 AK |
982 | } |
983 | ||
f7dd6d84 AK |
984 | int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv) |
985 | { | |
986 | struct lvinfo info; | |
987 | int r = 1; | |
988 | ||
8b076648 | 989 | if (!lv) { |
2262b320 | 990 | r = dm_mknodes(NULL); |
8b076648 AK |
991 | fs_unlock(); |
992 | return r; | |
993 | } | |
994 | ||
b45c9f49 | 995 | if (!_lv_info(cmd, lv, 1, &info, 0, 0)) |
5f4b2acf | 996 | return_0; |
f7dd6d84 AK |
997 | |
998 | if (info.exists) | |
f09fe0ad | 999 | r = dev_manager_lv_mknodes(lv); |
f7dd6d84 | 1000 | else |
f09fe0ad | 1001 | r = dev_manager_lv_rmnodes(lv); |
f7dd6d84 AK |
1002 | |
1003 | fs_unlock(); | |
1004 | ||
1005 | return r; | |
1006 | } | |
1007 | ||
352a99b9 AK |
1008 | /* |
1009 | * Does PV use VG somewhere in its construction? | |
1010 | * Returns 1 on failure. | |
1011 | */ | |
898e6f8e | 1012 | int pv_uses_vg(struct physical_volume *pv, |
3e3d5d85 | 1013 | struct volume_group *vg) |
352a99b9 | 1014 | { |
352a99b9 AK |
1015 | if (!activation()) |
1016 | return 0; | |
1017 | ||
1018 | if (!dm_is_dm_major(MAJOR(pv->dev->dev))) | |
1019 | return 0; | |
1020 | ||
898e6f8e | 1021 | return dev_manager_device_uses_vg(pv->dev, vg); |
352a99b9 AK |
1022 | } |
1023 | ||
2293567c AK |
1024 | void activation_release(void) |
1025 | { | |
1026 | dev_manager_release(); | |
1027 | } | |
1028 | ||
914c9723 AK |
1029 | void activation_exit(void) |
1030 | { | |
1031 | dev_manager_exit(); | |
1032 | } | |
199e490e | 1033 | #endif |