]>
Commit | Line | Data |
---|---|---|
b1713d28 | 1 | /* |
6606c3ae | 2 | * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. |
be684599 | 3 | * Copyright (C) 2004-2007 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 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, |
a6b22cf3 | 149 | int with_open_count, int with_read_ahead) |
199e490e AK |
150 | { |
151 | return 0; | |
152 | } | |
4bd9480d | 153 | int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, |
a6b22cf3 | 154 | struct lvinfo *info, int with_open_count, int with_read_ahead) |
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, |
a6b22cf3 | 428 | struct lvinfo *info, int with_open_count, int with_read_ahead, 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 | 441 | if (!dev_manager_info(lv->vg->cmd->mem, name, lv, with_mknodes, |
a6b22cf3 AK |
442 | with_open_count, with_read_ahead, &dminfo, |
443 | &info->read_ahead)) { | |
b45c9f49 AK |
444 | if (name) |
445 | dm_pool_free(cmd->mem, name); | |
5f4b2acf | 446 | return_0; |
f894b4b1 | 447 | } |
4a624ca0 | 448 | |
199e490e AK |
449 | info->exists = dminfo.exists; |
450 | info->suspended = dminfo.suspended; | |
451 | info->open_count = dminfo.open_count; | |
452 | info->major = dminfo.major; | |
453 | info->minor = dminfo.minor; | |
454 | info->read_only = dminfo.read_only; | |
5f4b2acf AK |
455 | info->live_table = dminfo.live_table; |
456 | info->inactive_table = dminfo.inactive_table; | |
199e490e | 457 | |
b45c9f49 AK |
458 | if (name) |
459 | dm_pool_free(cmd->mem, name); | |
460 | ||
f894b4b1 | 461 | return 1; |
de6c9183 | 462 | } |
a62ee8ad | 463 | |
f894b4b1 | 464 | int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, struct lvinfo *info, |
a6b22cf3 | 465 | int with_open_count, int with_read_ahead) |
8c0388e4 | 466 | { |
a6b22cf3 | 467 | return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0); |
8c0388e4 AK |
468 | } |
469 | ||
4bd9480d | 470 | int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, |
a6b22cf3 | 471 | struct lvinfo *info, int with_open_count, int with_read_ahead) |
4bd9480d AK |
472 | { |
473 | struct logical_volume *lv; | |
474 | ||
7a593325 | 475 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
4bd9480d AK |
476 | return 0; |
477 | ||
a6b22cf3 | 478 | return _lv_info(cmd, lv, 0, info, with_open_count, with_read_ahead, 0); |
4bd9480d AK |
479 | } |
480 | ||
1951dba9 AL |
481 | /* |
482 | * Returns 1 if percent set, else 0 on failure. | |
483 | */ | |
472ac5bd | 484 | int lv_snapshot_percent(const struct logical_volume *lv, float *percent) |
1951dba9 AL |
485 | { |
486 | int r; | |
487 | struct dev_manager *dm; | |
488 | ||
d1d9800e AK |
489 | if (!activation()) |
490 | return 0; | |
491 | ||
5f4b2acf AK |
492 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
493 | return_0; | |
1951dba9 | 494 | |
c826c0d1 | 495 | if (!(r = dev_manager_snapshot_percent(dm, lv, percent))) |
1951dba9 | 496 | stack; |
c826c0d1 | 497 | |
1951dba9 AL |
498 | dev_manager_destroy(dm); |
499 | ||
500 | return r; | |
501 | } | |
502 | ||
10b29b8d | 503 | /* FIXME Merge with snapshot_percent */ |
472ac5bd AK |
504 | int lv_mirror_percent(struct cmd_context *cmd, struct logical_volume *lv, |
505 | int wait, float *percent, uint32_t *event_nr) | |
10b29b8d AK |
506 | { |
507 | int r; | |
508 | struct dev_manager *dm; | |
b65b777d | 509 | struct lvinfo info; |
10b29b8d | 510 | |
876003dc AK |
511 | /* If mirrored LV is temporarily shrinked to 1 area (= linear), |
512 | * it should be considered in-sync. */ | |
513 | if (list_size(&lv->segments) == 1 && first_seg(lv)->area_count == 1) { | |
514 | *percent = 100.0; | |
515 | return 1; | |
516 | } | |
517 | ||
10b29b8d AK |
518 | if (!activation()) |
519 | return 0; | |
520 | ||
a6b22cf3 | 521 | if (!lv_info(cmd, lv, &info, 0, 0)) |
5f4b2acf | 522 | return_0; |
b65b777d AK |
523 | |
524 | if (!info.exists) | |
525 | return 0; | |
526 | ||
5f4b2acf AK |
527 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
528 | return_0; | |
10b29b8d AK |
529 | |
530 | if (!(r = dev_manager_mirror_percent(dm, lv, wait, percent, event_nr))) | |
531 | stack; | |
532 | ||
533 | dev_manager_destroy(dm); | |
534 | ||
535 | return r; | |
536 | } | |
537 | ||
b45c9f49 AK |
538 | static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv, |
539 | unsigned by_uuid_only) | |
2ba80b43 | 540 | { |
199e490e | 541 | struct lvinfo info; |
2ba80b43 | 542 | |
a6b22cf3 | 543 | if (!_lv_info(cmd, lv, 0, &info, 0, 0, by_uuid_only)) { |
2ba80b43 | 544 | stack; |
de6c9183 | 545 | return -1; |
2ba80b43 JT |
546 | } |
547 | ||
de6c9183 | 548 | return info.exists; |
2ba80b43 JT |
549 | } |
550 | ||
f894b4b1 | 551 | static int _lv_open_count(struct cmd_context *cmd, struct logical_volume *lv) |
5986ec94 | 552 | { |
199e490e | 553 | struct lvinfo info; |
5986ec94 | 554 | |
a6b22cf3 | 555 | if (!lv_info(cmd, lv, &info, 1, 0)) { |
5986ec94 | 556 | stack; |
de6c9183 | 557 | return -1; |
5986ec94 JT |
558 | } |
559 | ||
de6c9183 | 560 | return info.open_count; |
5986ec94 JT |
561 | } |
562 | ||
658b5812 | 563 | static int _lv_activate_lv(struct logical_volume *lv) |
b1713d28 | 564 | { |
6d52fb46 | 565 | int r; |
de6c9183 | 566 | struct dev_manager *dm; |
b1713d28 | 567 | |
5f4b2acf AK |
568 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
569 | return_0; | |
ae2bb665 | 570 | |
0fe3a2c5 | 571 | if (!(r = dev_manager_activate(dm, lv))) |
6d52fb46 | 572 | stack; |
ae2bb665 | 573 | |
de6c9183 | 574 | dev_manager_destroy(dm); |
ae2bb665 | 575 | return r; |
b1713d28 | 576 | } |
a381c45a | 577 | |
5f4b2acf | 578 | static int _lv_preload(struct logical_volume *lv) |
0a5e4a14 | 579 | { |
de6c9183 JT |
580 | int r; |
581 | struct dev_manager *dm; | |
37ed70b9 | 582 | |
5f4b2acf AK |
583 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
584 | return_0; | |
585 | ||
586 | if (!(r = dev_manager_preload(dm, lv))) | |
6d52fb46 | 587 | stack; |
5f4b2acf AK |
588 | |
589 | dev_manager_destroy(dm); | |
590 | return r; | |
591 | } | |
592 | ||
593 | static int _lv_deactivate(struct logical_volume *lv) | |
594 | { | |
595 | int r; | |
596 | struct dev_manager *dm; | |
597 | ||
598 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) | |
599 | return_0; | |
37ed70b9 | 600 | |
de6c9183 | 601 | if (!(r = dev_manager_deactivate(dm, lv))) |
37ed70b9 | 602 | stack; |
37ed70b9 | 603 | |
de6c9183 JT |
604 | dev_manager_destroy(dm); |
605 | return r; | |
37ed70b9 JT |
606 | } |
607 | ||
9cd3426d | 608 | static int _lv_suspend_lv(struct logical_volume *lv, int lockfs) |
4a624ca0 | 609 | { |
20c5fcf7 AK |
610 | int r; |
611 | struct dev_manager *dm; | |
c2d72fd4 | 612 | |
5f4b2acf AK |
613 | if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name))) |
614 | return_0; | |
0a5e4a14 | 615 | |
9cd3426d | 616 | if (!(r = dev_manager_suspend(dm, lv, lockfs))) |
37ed70b9 | 617 | stack; |
0a5e4a14 | 618 | |
20c5fcf7 AK |
619 | dev_manager_destroy(dm); |
620 | return r; | |
6d52fb46 | 621 | } |
4a624ca0 | 622 | |
8c013da4 | 623 | /* |
f75c11ed | 624 | * These two functions return the number of visible LVs in the state, |
8c013da4 AK |
625 | * or -1 on error. |
626 | */ | |
b45c9f49 | 627 | static int _lvs_in_vg_activated(struct volume_group *vg, unsigned by_uuid_only) |
f047219b | 628 | { |
60f13f01 | 629 | struct lv_list *lvl; |
94b8220f | 630 | int count = 0; |
37ed70b9 | 631 | |
d1d9800e AK |
632 | if (!activation()) |
633 | return 0; | |
634 | ||
60f13f01 AK |
635 | list_iterate_items(lvl, &vg->lvs) { |
636 | if (lvl->lv->status & VISIBLE_LV) | |
b45c9f49 | 637 | count += (_lv_active(vg->cmd, lvl->lv, by_uuid_only) == 1); |
37ed70b9 JT |
638 | } |
639 | ||
640 | return count; | |
f047219b | 641 | } |
2ba80b43 | 642 | |
b45c9f49 AK |
643 | int lvs_in_vg_activated_by_uuid_only(struct volume_group *vg) |
644 | { | |
645 | return _lvs_in_vg_activated(vg, 1); | |
646 | } | |
647 | ||
648 | int lvs_in_vg_activated(struct volume_group *vg) | |
649 | { | |
650 | return _lvs_in_vg_activated(vg, 0); | |
651 | } | |
652 | ||
08c9ff43 | 653 | int lvs_in_vg_opened(const struct volume_group *vg) |
2ba80b43 | 654 | { |
08c9ff43 | 655 | const struct lv_list *lvl; |
94b8220f | 656 | int count = 0; |
2ba80b43 | 657 | |
d1d9800e AK |
658 | if (!activation()) |
659 | return 0; | |
660 | ||
60f13f01 AK |
661 | list_iterate_items(lvl, &vg->lvs) { |
662 | if (lvl->lv->status & VISIBLE_LV) | |
f894b4b1 | 663 | count += (_lv_open_count(vg->cmd, lvl->lv) > 0); |
2ba80b43 JT |
664 | } |
665 | ||
666 | return count; | |
667 | } | |
413cc918 | 668 | |
3e3d5d85 | 669 | /* |
8a37910d AK |
670 | * Returns 0 if an attempt to (un)monitor the device failed. |
671 | * Returns 1 otherwise. | |
3e3d5d85 | 672 | */ |
8a37910d AK |
673 | int monitor_dev_for_events(struct cmd_context *cmd, |
674 | struct logical_volume *lv, int monitor) | |
15d91f5a | 675 | { |
ed09d7e3 | 676 | #ifdef DMEVENTD |
8a37910d AK |
677 | int i, pending = 0, monitored; |
678 | int r = 1; | |
67961c7c | 679 | struct list *tmp, *snh, *snht; |
15d91f5a | 680 | struct lv_segment *seg; |
24f4552b | 681 | int (*monitor_fn) (struct lv_segment *s, int e); |
15d91f5a | 682 | |
20db8ffc AK |
683 | /* skip dmeventd code altogether */ |
684 | if (dmeventd_monitor_mode() == DMEVENTD_MONITOR_IGNORE) | |
685 | return 1; | |
686 | ||
8a37910d AK |
687 | /* |
688 | * Nothing to do if dmeventd configured not to be used. | |
689 | */ | |
690 | if (monitor && !dmeventd_monitor_mode()) | |
3e3d5d85 AK |
691 | return 1; |
692 | ||
67961c7c PR |
693 | /* |
694 | * In case of a snapshot device, we monitor lv->snapshot->lv, | |
695 | * not the actual LV itself. | |
696 | */ | |
697 | if (lv_is_cow(lv)) | |
698 | return monitor_dev_for_events(cmd, lv->snapshot->lv, monitor); | |
699 | ||
700 | /* | |
701 | * In case this LV is a snapshot origin, we instead monitor | |
702 | * each of its respective snapshots (the origin itself does | |
703 | * not need to be monitored). | |
704 | * | |
705 | * TODO: This may change when snapshots of mirrors are allowed. | |
706 | */ | |
707 | if (lv_is_origin(lv)) { | |
708 | list_iterate_safe(snh, snht, &lv->snapshot_segs) | |
709 | if (!monitor_dev_for_events( | |
710 | cmd, list_struct_base(snh, | |
711 | struct lv_segment, origin_list)->cow, monitor)) | |
712 | r=0; | |
713 | return r; | |
714 | } | |
715 | ||
15d91f5a AK |
716 | list_iterate(tmp, &lv->segments) { |
717 | seg = list_item(tmp, struct lv_segment); | |
718 | ||
8ef6eb30 AK |
719 | if (!seg_monitored(seg) || (seg->status & PVMOVE)) |
720 | continue; | |
8a37910d AK |
721 | |
722 | monitor_fn = NULL; | |
ed09d7e3 | 723 | |
8ef6eb30 | 724 | /* Check monitoring status */ |
8a37910d AK |
725 | if (seg->segtype->ops->target_monitored) |
726 | monitored = seg->segtype->ops->target_monitored(seg, &pending); | |
8ef6eb30 AK |
727 | else |
728 | continue; /* segtype doesn't support registration */ | |
729 | ||
730 | /* | |
731 | * FIXME: We should really try again if pending | |
732 | */ | |
8a37910d | 733 | monitored = (pending) ? 0 : monitored; |
8ef6eb30 | 734 | |
8a37910d AK |
735 | if (monitor) { |
736 | if (monitored) | |
8ef6eb30 | 737 | log_verbose("%s/%s already monitored.", lv->vg->name, lv->name); |
8a37910d AK |
738 | else if (seg->segtype->ops->target_monitor_events) |
739 | monitor_fn = seg->segtype->ops->target_monitor_events; | |
8ef6eb30 | 740 | } else { |
8a37910d | 741 | if (!monitored) |
8ef6eb30 | 742 | log_verbose("%s/%s already not monitored.", lv->vg->name, lv->name); |
8a37910d AK |
743 | else if (seg->segtype->ops->target_unmonitor_events) |
744 | monitor_fn = seg->segtype->ops->target_unmonitor_events; | |
8ef6eb30 | 745 | } |
15d91f5a | 746 | |
8ef6eb30 | 747 | /* Do [un]monitor */ |
8a37910d | 748 | if (!monitor_fn) |
3e3d5d85 | 749 | continue; |
e24e7130 | 750 | |
8a37910d AK |
751 | log_verbose("%sonitoring %s/%s", monitor ? "M" : "Not m", lv->vg->name, lv->name); |
752 | ||
3e3d5d85 | 753 | /* FIXME specify events */ |
57fc4cc0 | 754 | if (!monitor_fn(seg, 0)) { |
8a37910d AK |
755 | log_error("%s/%s: %s segment monitoring function failed.", |
756 | lv->vg->name, lv->name, seg->segtype->name); | |
757 | return 0; | |
e24e7130 | 758 | } |
3e3d5d85 | 759 | |
8ef6eb30 AK |
760 | /* Check [un]monitor results */ |
761 | /* Try a couple times if pending, but not forever... */ | |
762 | for (i = 0; i < 10; i++) { | |
763 | pending = 0; | |
8a37910d | 764 | monitored = seg->segtype->ops->target_monitored(seg, &pending); |
8ef6eb30 | 765 | if (pending || |
8a37910d AK |
766 | (!monitored && monitor) || |
767 | (monitored && !monitor)) | |
768 | log_very_verbose("%s/%s %smonitoring still pending: waiting...", | |
769 | lv->vg->name, lv->name, monitor ? "" : "un"); | |
8ef6eb30 AK |
770 | else |
771 | break; | |
772 | sleep(1); | |
773 | } | |
774 | ||
8a37910d | 775 | r = (monitored && monitor) || (!monitored && !monitor); |
15d91f5a | 776 | } |
e6493477 | 777 | |
3e3d5d85 AK |
778 | return r; |
779 | #else | |
15d91f5a | 780 | return 1; |
3e3d5d85 | 781 | #endif |
15d91f5a | 782 | } |
15d91f5a | 783 | |
658b5812 AK |
784 | static int _lv_suspend(struct cmd_context *cmd, const char *lvid_s, |
785 | int error_if_not_suspended) | |
413cc918 | 786 | { |
8b888354 | 787 | struct logical_volume *lv, *lv_pre; |
199e490e | 788 | struct lvinfo info; |
9cd3426d | 789 | int lockfs = 0; |
413cc918 | 790 | |
d1d9800e AK |
791 | if (!activation()) |
792 | return 1; | |
793 | ||
8b888354 | 794 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
e408d62e | 795 | return_0; |
8b888354 | 796 | |
7a593325 | 797 | /* Use precommitted metadata if present */ |
8b888354 | 798 | if (!(lv_pre = lv_from_lvid(cmd, lvid_s, 1))) |
e408d62e | 799 | return_0; |
413cc918 | 800 | |
20c5fcf7 AK |
801 | if (test_mode()) { |
802 | _skip("Suspending '%s'.", lv->name); | |
711f7fc6 | 803 | return 1; |
20c5fcf7 AK |
804 | } |
805 | ||
a6b22cf3 | 806 | if (!lv_info(cmd, lv, &info, 0, 0)) |
5f4b2acf | 807 | return_0; |
41967a02 | 808 | |
914c9723 | 809 | if (!info.exists || info.suspended) |
658b5812 | 810 | return error_if_not_suspended ? 0 : 1; |
914c9723 | 811 | |
5f4b2acf | 812 | /* If VG was precommitted, preload devices for the LV */ |
8b888354 AK |
813 | if ((lv_pre->vg->status & PRECOMMITTED)) { |
814 | if (!_lv_preload(lv_pre)) { | |
5f4b2acf AK |
815 | /* FIXME Revert preloading */ |
816 | return_0; | |
817 | } | |
818 | } | |
819 | ||
8a37910d | 820 | if (!monitor_dev_for_events(cmd, lv, 0)) |
e24e7130 | 821 | /* FIXME Consider aborting here */ |
ed09d7e3 AK |
822 | stack; |
823 | ||
914c9723 | 824 | memlock_inc(); |
9cd3426d AK |
825 | |
826 | if (lv_is_origin(lv_pre) || lv_is_cow(lv_pre)) | |
827 | lockfs = 1; | |
828 | ||
829 | if (!_lv_suspend_lv(lv, lockfs)) { | |
914c9723 AK |
830 | memlock_dec(); |
831 | fs_unlock(); | |
832 | return 0; | |
833 | } | |
8c013da4 | 834 | |
413cc918 AK |
835 | return 1; |
836 | } | |
837 | ||
658b5812 AK |
838 | /* Returns success if the device is not active */ |
839 | int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s) | |
840 | { | |
841 | return _lv_suspend(cmd, lvid_s, 0); | |
842 | } | |
843 | ||
844 | int lv_suspend(struct cmd_context *cmd, const char *lvid_s) | |
845 | { | |
846 | return _lv_suspend(cmd, lvid_s, 1); | |
847 | } | |
848 | ||
849 | static int _lv_resume(struct cmd_context *cmd, const char *lvid_s, | |
850 | int error_if_not_active) | |
413cc918 AK |
851 | { |
852 | struct logical_volume *lv; | |
199e490e | 853 | struct lvinfo info; |
413cc918 | 854 | |
d1d9800e AK |
855 | if (!activation()) |
856 | return 1; | |
857 | ||
7a593325 | 858 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
413cc918 AK |
859 | return 0; |
860 | ||
20c5fcf7 AK |
861 | if (test_mode()) { |
862 | _skip("Resuming '%s'.", lv->name); | |
711f7fc6 | 863 | return 1; |
20c5fcf7 AK |
864 | } |
865 | ||
a6b22cf3 | 866 | if (!lv_info(cmd, lv, &info, 0, 0)) |
5f4b2acf | 867 | return_0; |
41967a02 | 868 | |
914c9723 | 869 | if (!info.exists || !info.suspended) |
658b5812 | 870 | return error_if_not_active ? 0 : 1; |
914c9723 | 871 | |
658b5812 | 872 | if (!_lv_activate_lv(lv)) |
914c9723 AK |
873 | return 0; |
874 | ||
875 | memlock_dec(); | |
876 | fs_unlock(); | |
413cc918 | 877 | |
8a37910d | 878 | if (!monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 879 | stack; |
15d91f5a | 880 | |
413cc918 AK |
881 | return 1; |
882 | } | |
883 | ||
658b5812 AK |
884 | /* Returns success if the device is not active */ |
885 | int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s) | |
886 | { | |
887 | return _lv_resume(cmd, lvid_s, 0); | |
888 | } | |
889 | ||
890 | int lv_resume(struct cmd_context *cmd, const char *lvid_s) | |
891 | { | |
892 | return _lv_resume(cmd, lvid_s, 1); | |
893 | } | |
894 | ||
be326a2f | 895 | int lv_deactivate(struct cmd_context *cmd, const char *lvid_s) |
f4cbeaf0 AK |
896 | { |
897 | struct logical_volume *lv; | |
199e490e | 898 | struct lvinfo info; |
914c9723 | 899 | int r; |
f4cbeaf0 | 900 | |
d1d9800e AK |
901 | if (!activation()) |
902 | return 1; | |
903 | ||
7a593325 | 904 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
f4cbeaf0 AK |
905 | return 0; |
906 | ||
20c5fcf7 AK |
907 | if (test_mode()) { |
908 | _skip("Deactivating '%s'.", lv->name); | |
711f7fc6 | 909 | return 1; |
20c5fcf7 AK |
910 | } |
911 | ||
a6b22cf3 | 912 | if (!lv_info(cmd, lv, &info, 1, 0)) |
5f4b2acf | 913 | return_0; |
41967a02 | 914 | |
914c9723 AK |
915 | if (!info.exists) |
916 | return 1; | |
f4cbeaf0 | 917 | |
c1f50521 | 918 | if (info.open_count && (lv->status & VISIBLE_LV)) { |
5f4b2acf | 919 | log_error("LV %s/%s in use: not deactivating", lv->vg->name, |
0cf96f33 AK |
920 | lv->name); |
921 | return 0; | |
922 | } | |
923 | ||
8a37910d | 924 | if (!monitor_dev_for_events(cmd, lv, 0)) |
ed09d7e3 | 925 | stack; |
15d91f5a | 926 | |
914c9723 AK |
927 | memlock_inc(); |
928 | r = _lv_deactivate(lv); | |
929 | memlock_dec(); | |
930 | fs_unlock(); | |
931 | ||
932 | return r; | |
f4cbeaf0 AK |
933 | } |
934 | ||
658b5812 AK |
935 | /* Test if LV passes filter */ |
936 | int lv_activation_filter(struct cmd_context *cmd, const char *lvid_s, | |
937 | int *activate_lv) | |
938 | { | |
939 | struct logical_volume *lv; | |
940 | ||
941 | if (!activation()) | |
942 | goto activate; | |
943 | ||
7a593325 | 944 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
658b5812 AK |
945 | return 0; |
946 | ||
947 | if (!_passes_activation_filter(cmd, lv)) { | |
948 | log_verbose("Not activating %s/%s due to config file settings", | |
949 | lv->vg->name, lv->name); | |
950 | *activate_lv = 0; | |
951 | return 1; | |
952 | } | |
953 | ||
954 | activate: | |
955 | *activate_lv = 1; | |
956 | return 1; | |
957 | } | |
958 | ||
07d31831 AK |
959 | static int _lv_activate(struct cmd_context *cmd, const char *lvid_s, |
960 | int exclusive, int filter) | |
f4cbeaf0 AK |
961 | { |
962 | struct logical_volume *lv; | |
199e490e | 963 | struct lvinfo info; |
914c9723 | 964 | int r; |
f4cbeaf0 | 965 | |
d1d9800e AK |
966 | if (!activation()) |
967 | return 1; | |
968 | ||
7a593325 | 969 | if (!(lv = lv_from_lvid(cmd, lvid_s, 0))) |
f4cbeaf0 AK |
970 | return 0; |
971 | ||
658b5812 | 972 | if (filter && !_passes_activation_filter(cmd, lv)) { |
de17d760 AK |
973 | log_verbose("Not activating %s/%s due to config file settings", |
974 | lv->vg->name, lv->name); | |
975 | return 0; | |
976 | } | |
977 | ||
20c5fcf7 AK |
978 | if (test_mode()) { |
979 | _skip("Activating '%s'.", lv->name); | |
711f7fc6 | 980 | return 1; |
20c5fcf7 AK |
981 | } |
982 | ||
a6b22cf3 | 983 | if (!lv_info(cmd, lv, &info, 0, 0)) |
5f4b2acf | 984 | return_0; |
8c013da4 | 985 | |
5f4b2acf | 986 | if (info.exists && !info.suspended && info.live_table) |
914c9723 | 987 | return 1; |
f4cbeaf0 | 988 | |
07d31831 AK |
989 | if (exclusive) |
990 | lv->status |= ACTIVATE_EXCL; | |
991 | ||
914c9723 | 992 | memlock_inc(); |
658b5812 | 993 | r = _lv_activate_lv(lv); |
914c9723 AK |
994 | memlock_dec(); |
995 | fs_unlock(); | |
996 | ||
5efd97f1 | 997 | if (r && !monitor_dev_for_events(cmd, lv, 1)) |
ed09d7e3 | 998 | stack; |
15d91f5a | 999 | |
914c9723 | 1000 | return r; |
f4cbeaf0 | 1001 | } |
199e490e | 1002 | |
658b5812 | 1003 | /* Activate LV */ |
07d31831 | 1004 | int lv_activate(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 1005 | { |
07d31831 | 1006 | return _lv_activate(cmd, lvid_s, exclusive, 0); |
658b5812 AK |
1007 | } |
1008 | ||
1009 | /* Activate LV only if it passes filter */ | |
07d31831 | 1010 | int lv_activate_with_filter(struct cmd_context *cmd, const char *lvid_s, int exclusive) |
658b5812 | 1011 | { |
07d31831 | 1012 | return _lv_activate(cmd, lvid_s, exclusive, 1); |
658b5812 AK |
1013 | } |
1014 | ||
f7dd6d84 AK |
1015 | int lv_mknodes(struct cmd_context *cmd, const struct logical_volume *lv) |
1016 | { | |
1017 | struct lvinfo info; | |
1018 | int r = 1; | |
1019 | ||
8b076648 | 1020 | if (!lv) { |
2262b320 | 1021 | r = dm_mknodes(NULL); |
8b076648 AK |
1022 | fs_unlock(); |
1023 | return r; | |
1024 | } | |
1025 | ||
a6b22cf3 | 1026 | if (!_lv_info(cmd, lv, 1, &info, 0, 0, 0)) |
5f4b2acf | 1027 | return_0; |
f7dd6d84 AK |
1028 | |
1029 | if (info.exists) | |
f09fe0ad | 1030 | r = dev_manager_lv_mknodes(lv); |
f7dd6d84 | 1031 | else |
f09fe0ad | 1032 | r = dev_manager_lv_rmnodes(lv); |
f7dd6d84 AK |
1033 | |
1034 | fs_unlock(); | |
1035 | ||
1036 | return r; | |
1037 | } | |
1038 | ||
352a99b9 AK |
1039 | /* |
1040 | * Does PV use VG somewhere in its construction? | |
1041 | * Returns 1 on failure. | |
1042 | */ | |
898e6f8e | 1043 | int pv_uses_vg(struct physical_volume *pv, |
3e3d5d85 | 1044 | struct volume_group *vg) |
352a99b9 | 1045 | { |
352a99b9 AK |
1046 | if (!activation()) |
1047 | return 0; | |
1048 | ||
1049 | if (!dm_is_dm_major(MAJOR(pv->dev->dev))) | |
1050 | return 0; | |
1051 | ||
898e6f8e | 1052 | return dev_manager_device_uses_vg(pv->dev, vg); |
352a99b9 AK |
1053 | } |
1054 | ||
2293567c AK |
1055 | void activation_release(void) |
1056 | { | |
1057 | dev_manager_release(); | |
1058 | } | |
1059 | ||
914c9723 AK |
1060 | void activation_exit(void) |
1061 | { | |
1062 | dev_manager_exit(); | |
1063 | } | |
199e490e | 1064 | #endif |