]> sourceware.org Git - lvm2.git/blob - tools/toollib.c
Reinstate accidentally-deleted line.
[lvm2.git] / tools / toollib.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 *
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 Lesser General Public License v.2.1.
10 *
11 * You should have received a copy of the GNU Lesser 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
14 */
15
16 #include "tools.h"
17 #include "lv_alloc.h"
18 #include "xlate.h"
19
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22
23 const char *command_name(struct cmd_context *cmd)
24 {
25 return cmd->command->name;
26 }
27
28 /*
29 * Strip dev_dir if present
30 */
31 const char *skip_dev_dir(struct cmd_context *cmd, const char *vg_name,
32 unsigned *dev_dir_found)
33 {
34 const char *dmdir = dm_dir();
35 size_t dmdir_len = strlen(dmdir), vglv_sz;
36 char *vgname, *lvname, *layer, *vglv;
37
38 /* FIXME Do this properly */
39 if (*vg_name == '/') {
40 while (*vg_name == '/')
41 vg_name++;
42 vg_name--;
43 }
44
45 /* Reformat string if /dev/mapper found */
46 if (!strncmp(vg_name, dmdir, dmdir_len) && vg_name[dmdir_len] == '/') {
47 if (dev_dir_found)
48 *dev_dir_found = 1;
49 vg_name += dmdir_len;
50 while (*vg_name == '/')
51 vg_name++;
52
53 if (!dm_split_lvm_name(cmd->mem, vg_name, &vgname, &lvname, &layer) ||
54 *layer) {
55 log_error("skip_dev_dir: Couldn't split up device name %s",
56 vg_name);
57 return vg_name;
58 }
59 vglv_sz = strlen(vgname) + strlen(lvname) + 2;
60 if (!(vglv = dm_pool_alloc(cmd->mem, vglv_sz)) ||
61 dm_snprintf(vglv, vglv_sz, "%s%s%s", vgname,
62 *lvname ? "/" : "",
63 lvname) < 0) {
64 log_error("vg/lv string alloc failed");
65 return vg_name;
66 }
67 return vglv;
68 }
69
70 if (!strncmp(vg_name, cmd->dev_dir, strlen(cmd->dev_dir))) {
71 if (dev_dir_found)
72 *dev_dir_found = 1;
73 vg_name += strlen(cmd->dev_dir);
74 while (*vg_name == '/')
75 vg_name++;
76 } else if (dev_dir_found)
77 *dev_dir_found = 0;
78
79 return vg_name;
80 }
81
82 /*
83 * Metadata iteration functions
84 */
85 int process_each_lv_in_vg(struct cmd_context *cmd,
86 struct volume_group *vg,
87 const struct dm_list *arg_lvnames,
88 const struct dm_list *tags,
89 struct dm_list *failed_lvnames,
90 void *handle,
91 process_single_lv_fn_t process_single_lv)
92 {
93 int ret_max = ECMD_PROCESSED;
94 int ret = 0;
95 unsigned process_all = 0;
96 unsigned process_lv = 0;
97 unsigned tags_supplied = 0;
98 unsigned lvargs_supplied = 0;
99 unsigned lvargs_matched = 0;
100 char *lv_name;
101 struct lv_list *lvl;
102
103 if (!vg_check_status(vg, EXPORTED_VG))
104 return ECMD_FAILED;
105
106 if (tags && !dm_list_empty(tags))
107 tags_supplied = 1;
108
109 if (arg_lvnames && !dm_list_empty(arg_lvnames))
110 lvargs_supplied = 1;
111
112 /* Process all LVs in this VG if no restrictions given */
113 if (!tags_supplied && !lvargs_supplied)
114 process_all = 1;
115
116 /* Or if VG tags match */
117 if (!process_lv && tags_supplied &&
118 str_list_match_list(tags, &vg->tags, NULL)) {
119 process_all = 1;
120 }
121
122 dm_list_iterate_items(lvl, &vg->lvs) {
123 if (lvl->lv->status & SNAPSHOT)
124 continue;
125
126 /* Skip availability change for non-virt snaps when processing all LVs */
127 /* FIXME: pass process_all to process_single_lv() */
128 if (process_all && arg_count(cmd, available_ARG) &&
129 lv_is_cow(lvl->lv) && !lv_is_virtual_origin(origin_from_cow(lvl->lv)))
130 continue;
131
132 if (lv_is_virtual_origin(lvl->lv) && !arg_count(cmd, all_ARG))
133 continue;
134
135 /*
136 * Only let hidden LVs through it --all was used or the LVs
137 * were specifically named on the command line.
138 */
139 if (!lvargs_supplied && !lv_is_visible(lvl->lv) && !arg_count(cmd, all_ARG))
140 continue;
141
142 /* Should we process this LV? */
143 if (process_all)
144 process_lv = 1;
145 else
146 process_lv = 0;
147
148 /* LV tag match? */
149 if (!process_lv && tags_supplied &&
150 str_list_match_list(tags, &lvl->lv->tags, NULL)) {
151 process_lv = 1;
152 }
153
154 /* LV name match? */
155 if (lvargs_supplied &&
156 str_list_match_item(arg_lvnames, lvl->lv->name)) {
157 process_lv = 1;
158 lvargs_matched++;
159 }
160
161 if (!process_lv)
162 continue;
163
164 lvl->lv->vg->cmd_missing_vgs = 0;
165 ret = process_single_lv(cmd, lvl->lv, handle);
166 if (ret != ECMD_PROCESSED && failed_lvnames) {
167 lv_name = dm_pool_strdup(cmd->mem, lvl->lv->name);
168 if (!lv_name ||
169 !str_list_add(cmd->mem, failed_lvnames, lv_name)) {
170 log_error("Allocation failed for str_list.");
171 return ECMD_FAILED;
172 }
173 if (lvl->lv->vg->cmd_missing_vgs)
174 ret = ECMD_PROCESSED;
175 }
176 if (ret > ret_max)
177 ret_max = ret;
178 if (sigint_caught()) {
179 stack;
180 return ret_max;
181 }
182 }
183
184 if (lvargs_supplied && lvargs_matched != dm_list_size(arg_lvnames)) {
185 log_error("One or more specified logical volume(s) not found.");
186 if (ret_max < ECMD_FAILED)
187 ret_max = ECMD_FAILED;
188 }
189
190 return ret_max;
191 }
192
193 int process_each_lv(struct cmd_context *cmd, int argc, char **argv,
194 uint32_t flags, void *handle,
195 process_single_lv_fn_t process_single_lv)
196 {
197 int opt = 0;
198 int ret_max = ECMD_PROCESSED;
199 int ret = 0;
200
201 struct dm_list *tags_arg;
202 struct dm_list *vgnames; /* VGs to process */
203 struct str_list *sll, *strl;
204 struct cmd_vg *cvl_vg;
205 struct dm_list cmd_vgs;
206 struct dm_list failed_lvnames;
207 struct dm_list tags, lvnames;
208 struct dm_list arg_lvnames; /* Cmdline vgname or vgname/lvname */
209 struct dm_list arg_vgnames;
210 char *vglv;
211 size_t vglv_sz;
212
213 const char *vgname;
214
215 dm_list_init(&tags);
216 dm_list_init(&arg_lvnames);
217 dm_list_init(&failed_lvnames);
218
219 if (argc) {
220 log_verbose("Using logical volume(s) on command line");
221 dm_list_init(&arg_vgnames);
222
223 for (; opt < argc; opt++) {
224 const char *lv_name = argv[opt];
225 const char *tmp_lv_name;
226 char *vgname_def;
227 unsigned dev_dir_found = 0;
228
229 /* Do we have a tag or vgname or lvname? */
230 vgname = lv_name;
231
232 if (*vgname == '@') {
233 if (!validate_tag(vgname + 1)) {
234 log_error("Skipping invalid tag %s",
235 vgname);
236 continue;
237 }
238 if (!str_list_add(cmd->mem, &tags,
239 dm_pool_strdup(cmd->mem,
240 vgname + 1))) {
241 log_error("strlist allocation failed");
242 return ECMD_FAILED;
243 }
244 continue;
245 }
246
247 /* FIXME Jumbled parsing */
248 vgname = skip_dev_dir(cmd, vgname, &dev_dir_found);
249
250 if (*vgname == '/') {
251 log_error("\"%s\": Invalid path for Logical "
252 "Volume", argv[opt]);
253 if (ret_max < ECMD_FAILED)
254 ret_max = ECMD_FAILED;
255 continue;
256 }
257 lv_name = vgname;
258 if ((tmp_lv_name = strchr(vgname, '/'))) {
259 /* Must be an LV */
260 lv_name = tmp_lv_name;
261 while (*lv_name == '/')
262 lv_name++;
263 if (!(vgname = extract_vgname(cmd, vgname))) {
264 if (ret_max < ECMD_FAILED) {
265 stack;
266 ret_max = ECMD_FAILED;
267 }
268 continue;
269 }
270 } else if (!dev_dir_found &&
271 (vgname_def = default_vgname(cmd))) {
272 vgname = vgname_def;
273 } else
274 lv_name = NULL;
275
276 if (!str_list_add(cmd->mem, &arg_vgnames,
277 dm_pool_strdup(cmd->mem, vgname))) {
278 log_error("strlist allocation failed");
279 return ECMD_FAILED;
280 }
281
282 if (!lv_name) {
283 if (!str_list_add(cmd->mem, &arg_lvnames,
284 dm_pool_strdup(cmd->mem,
285 vgname))) {
286 log_error("strlist allocation failed");
287 return ECMD_FAILED;
288 }
289 } else {
290 vglv_sz = strlen(vgname) + strlen(lv_name) + 2;
291 if (!(vglv = dm_pool_alloc(cmd->mem, vglv_sz)) ||
292 dm_snprintf(vglv, vglv_sz, "%s/%s", vgname,
293 lv_name) < 0) {
294 log_error("vg/lv string alloc failed");
295 return ECMD_FAILED;
296 }
297 if (!str_list_add(cmd->mem, &arg_lvnames, vglv)) {
298 log_error("strlist allocation failed");
299 return ECMD_FAILED;
300 }
301 }
302 }
303 vgnames = &arg_vgnames;
304 }
305
306 if (!argc || !dm_list_empty(&tags)) {
307 log_verbose("Finding all logical volumes");
308 if (!lvmetad_vg_list_to_lvmcache(cmd))
309 stack;
310 if (!(vgnames = get_vgnames(cmd, 0)) || dm_list_empty(vgnames)) {
311 log_error("No volume groups found");
312 return ret_max;
313 }
314 }
315
316 dm_list_iterate_items(strl, vgnames) {
317 vgname = strl->str;
318 dm_list_init(&cmd_vgs);
319 if (!(cvl_vg = cmd_vg_add(cmd->mem, &cmd_vgs,
320 vgname, NULL, flags))) {
321 stack;
322 return ECMD_FAILED;
323 }
324
325 if (!cmd_vg_read(cmd, &cmd_vgs)) {
326 free_cmd_vgs(&cmd_vgs);
327 if (ret_max < ECMD_FAILED) {
328 log_error("Skipping volume group %s", vgname);
329 ret_max = ECMD_FAILED;
330 } else
331 stack;
332 continue;
333 }
334
335 tags_arg = &tags;
336 dm_list_init(&lvnames); /* LVs to be processed in this VG */
337 dm_list_iterate_items(sll, &arg_lvnames) {
338 const char *vg_name = sll->str;
339 const char *lv_name = strchr(vg_name, '/');
340
341 if ((!lv_name && !strcmp(vg_name, vgname))) {
342 /* Process all LVs in this VG */
343 tags_arg = NULL;
344 dm_list_init(&lvnames);
345 break;
346 } else if (!strncmp(vg_name, vgname, strlen(vgname)) && lv_name &&
347 strlen(vgname) == (size_t) (lv_name - vg_name)) {
348 if (!str_list_add(cmd->mem, &lvnames,
349 dm_pool_strdup(cmd->mem,
350 lv_name + 1))) {
351 log_error("strlist allocation failed");
352 free_cmd_vgs(&cmd_vgs);
353 return ECMD_FAILED;
354 }
355 }
356 }
357
358 while (!sigint_caught()) {
359 ret = process_each_lv_in_vg(cmd, cvl_vg->vg, &lvnames,
360 tags_arg, &failed_lvnames,
361 handle, process_single_lv);
362 if (ret != ECMD_PROCESSED) {
363 stack;
364 break;
365 }
366
367 if (dm_list_empty(&failed_lvnames))
368 break;
369
370 /* Try again with failed LVs in this VG */
371 dm_list_init(&lvnames);
372 dm_list_splice(&lvnames, &failed_lvnames);
373
374 free_cmd_vgs(&cmd_vgs);
375 if (!cmd_vg_read(cmd, &cmd_vgs)) {
376 stack;
377 ret = ECMD_FAILED; /* break */
378 break;
379 }
380 }
381 if (ret > ret_max)
382 ret_max = ret;
383
384 free_cmd_vgs(&cmd_vgs);
385 /* FIXME: logic for breaking command is not consistent */
386 if (sigint_caught()) {
387 stack;
388 return ECMD_FAILED;
389 }
390 }
391
392 return ret_max;
393 }
394
395 int process_each_segment_in_pv(struct cmd_context *cmd,
396 struct volume_group *vg,
397 struct physical_volume *pv,
398 void *handle,
399 process_single_pvseg_fn_t process_single_pvseg)
400 {
401 struct pv_segment *pvseg;
402 struct pv_list *pvl;
403 const char *vg_name = NULL;
404 int ret_max = ECMD_PROCESSED;
405 int ret;
406 struct volume_group *old_vg = vg;
407 struct pv_segment _free_pv_segment = { .pv = pv };
408
409 if (is_pv(pv) && !vg && !is_orphan(pv)) {
410 vg_name = pv_vg_name(pv);
411
412 vg = vg_read(cmd, vg_name, NULL, 0);
413 if (vg_read_error(vg)) {
414 release_vg(vg);
415 log_error("Skipping volume group %s", vg_name);
416 return ECMD_FAILED;
417 }
418
419 /*
420 * Replace possibly incomplete PV structure with new one
421 * allocated in vg_read_internal() path.
422 */
423 if (!(pvl = find_pv_in_vg(vg, pv_dev_name(pv)))) {
424 log_error("Unable to find %s in volume group %s",
425 pv_dev_name(pv), vg_name);
426 unlock_and_release_vg(cmd, vg, vg_name);
427 return ECMD_FAILED;
428 }
429
430 pv = pvl->pv;
431 }
432
433 if (dm_list_empty(&pv->segments)) {
434 ret = process_single_pvseg(cmd, NULL, &_free_pv_segment, handle);
435 if (ret > ret_max)
436 ret_max = ret;
437 } else
438 dm_list_iterate_items(pvseg, &pv->segments) {
439 ret = process_single_pvseg(cmd, vg, pvseg, handle);
440 if (ret > ret_max)
441 ret_max = ret;
442 if (sigint_caught())
443 break;
444 }
445
446 if (vg_name)
447 unlock_vg(cmd, vg_name);
448 if (!old_vg)
449 release_vg(vg);
450
451 return ret_max;
452 }
453
454 int process_each_segment_in_lv(struct cmd_context *cmd,
455 struct logical_volume *lv,
456 void *handle,
457 process_single_seg_fn_t process_single_seg)
458 {
459 struct lv_segment *seg;
460 int ret_max = ECMD_PROCESSED;
461 int ret;
462
463 dm_list_iterate_items(seg, &lv->segments) {
464 ret = process_single_seg(cmd, seg, handle);
465 if (ret > ret_max)
466 ret_max = ret;
467 /* FIXME: logic for breaking command is not consistent */
468 if (sigint_caught())
469 return ECMD_FAILED;
470 }
471
472 return ret_max;
473 }
474
475 static int _process_one_vg(struct cmd_context *cmd, const char *vg_name,
476 const char *vgid,
477 struct dm_list *tags, struct dm_list *arg_vgnames,
478 uint32_t flags, void *handle, int ret_max,
479 process_single_vg_fn_t process_single_vg)
480 {
481 struct dm_list cmd_vgs;
482 struct cmd_vg *cvl_vg;
483 int ret = 0;
484
485 log_verbose("Finding volume group \"%s\"", vg_name);
486
487 dm_list_init(&cmd_vgs);
488 if (!(cvl_vg = cmd_vg_add(cmd->mem, &cmd_vgs, vg_name, vgid, flags)))
489 return_0;
490
491 for (;;) {
492 /* FIXME: consistent handling of command break */
493 if (sigint_caught()) {
494 ret = ECMD_FAILED;
495 break;
496 }
497 if (!cmd_vg_read(cmd, &cmd_vgs))
498 /* Allow FAILED_INCONSISTENT through only for vgcfgrestore */
499 if (vg_read_error(cvl_vg->vg) &&
500 (!((flags & READ_ALLOW_INCONSISTENT) &&
501 (vg_read_error(cvl_vg->vg) == FAILED_INCONSISTENT)))) {
502 ret = ECMD_FAILED;
503 break;
504 }
505
506 if (!dm_list_empty(tags) &&
507 /* Only process if a tag matches or it's on arg_vgnames */
508 !str_list_match_item(arg_vgnames, vg_name) &&
509 !str_list_match_list(tags, &cvl_vg->vg->tags, NULL))
510 break;
511
512 ret = process_single_vg(cmd, vg_name, cvl_vg->vg, handle);
513
514 if (vg_read_error(cvl_vg->vg)) /* FAILED_INCONSISTENT */
515 break;
516
517 if (!cvl_vg->vg->cmd_missing_vgs)
518 break;
519
520 free_cmd_vgs(&cmd_vgs);
521 }
522
523 free_cmd_vgs(&cmd_vgs);
524
525 return (ret > ret_max) ? ret : ret_max;
526 }
527
528 int process_each_vg(struct cmd_context *cmd, int argc, char **argv,
529 uint32_t flags, void *handle,
530 process_single_vg_fn_t process_single_vg)
531 {
532 int opt = 0;
533 int ret_max = ECMD_PROCESSED;
534
535 struct str_list *sl;
536 struct dm_list *vgnames, *vgids;
537 struct dm_list arg_vgnames, tags;
538
539 const char *vg_name, *vgid;
540
541 dm_list_init(&tags);
542 dm_list_init(&arg_vgnames);
543
544 if (argc) {
545 log_verbose("Using volume group(s) on command line");
546
547 for (; opt < argc; opt++) {
548 vg_name = argv[opt];
549 if (*vg_name == '@') {
550 if (!validate_tag(vg_name + 1)) {
551 log_error("Skipping invalid tag %s",
552 vg_name);
553 if (ret_max < EINVALID_CMD_LINE)
554 ret_max = EINVALID_CMD_LINE;
555 continue;
556 }
557 if (!str_list_add(cmd->mem, &tags,
558 dm_pool_strdup(cmd->mem,
559 vg_name + 1))) {
560 log_error("strlist allocation failed");
561 return ECMD_FAILED;
562 }
563 continue;
564 }
565
566 vg_name = skip_dev_dir(cmd, vg_name, NULL);
567 if (strchr(vg_name, '/')) {
568 log_error("Invalid volume group name: %s",
569 vg_name);
570 if (ret_max < EINVALID_CMD_LINE)
571 ret_max = EINVALID_CMD_LINE;
572 continue;
573 }
574 if (!str_list_add(cmd->mem, &arg_vgnames,
575 dm_pool_strdup(cmd->mem, vg_name))) {
576 log_error("strlist allocation failed");
577 return ECMD_FAILED;
578 }
579 }
580
581 vgnames = &arg_vgnames;
582 }
583
584 if (!argc || !dm_list_empty(&tags)) {
585 log_verbose("Finding all volume groups");
586 if (!lvmetad_vg_list_to_lvmcache(cmd))
587 stack;
588 if (!(vgids = get_vgids(cmd, 0)) || dm_list_empty(vgids)) {
589 log_error("No volume groups found");
590 return ret_max;
591 }
592 dm_list_iterate_items(sl, vgids) {
593 vgid = sl->str;
594 if (!(vgid) || !(vg_name = lvmcache_vgname_from_vgid(cmd->mem, vgid)))
595 continue;
596 ret_max = _process_one_vg(cmd, vg_name, vgid, &tags,
597 &arg_vgnames,
598 flags, handle,
599 ret_max, process_single_vg);
600 if (sigint_caught())
601 return ret_max;
602 }
603 } else {
604 dm_list_iterate_items(sl, vgnames) {
605 vg_name = sl->str;
606 if (is_orphan_vg(vg_name))
607 continue; /* FIXME Unnecessary? */
608 ret_max = _process_one_vg(cmd, vg_name, NULL, &tags,
609 &arg_vgnames,
610 flags, handle,
611 ret_max, process_single_vg);
612 if (sigint_caught())
613 return ret_max;
614 }
615 }
616
617 return ret_max;
618 }
619
620 int process_each_pv_in_vg(struct cmd_context *cmd, struct volume_group *vg,
621 const struct dm_list *tags, void *handle,
622 process_single_pv_fn_t process_single_pv)
623 {
624 int ret_max = ECMD_PROCESSED;
625 int ret = 0;
626 struct pv_list *pvl;
627
628 dm_list_iterate_items(pvl, &vg->pvs) {
629 if (tags && !dm_list_empty(tags) &&
630 !str_list_match_list(tags, &pvl->pv->tags, NULL)) {
631 continue;
632 }
633 if ((ret = process_single_pv(cmd, vg, pvl->pv, handle)) > ret_max)
634 ret_max = ret;
635 if (sigint_caught())
636 return ret_max;
637 }
638
639 return ret_max;
640 }
641
642 static int _process_all_devs(struct cmd_context *cmd, void *handle,
643 process_single_pv_fn_t process_single_pv)
644 {
645 struct physical_volume *pv;
646 struct physical_volume pv_dummy;
647 struct dev_iter *iter;
648 struct device *dev;
649
650 int ret_max = ECMD_PROCESSED;
651 int ret = 0;
652
653 if (!scan_vgs_for_pvs(cmd, 1)) {
654 stack;
655 return ECMD_FAILED;
656 }
657
658 if (!(iter = dev_iter_create(cmd->filter, 1))) {
659 log_error("dev_iter creation failed");
660 return ECMD_FAILED;
661 }
662
663 while ((dev = dev_iter_get(iter))) {
664 if (!(pv = pv_read(cmd, dev_name(dev), 0, 0))) {
665 memset(&pv_dummy, 0, sizeof(pv_dummy));
666 dm_list_init(&pv_dummy.tags);
667 dm_list_init(&pv_dummy.segments);
668 pv_dummy.dev = dev;
669 pv_dummy.fmt = NULL;
670 pv = &pv_dummy;
671 }
672 ret = process_single_pv(cmd, NULL, pv, handle);
673
674 free_pv_fid(pv);
675
676 if (ret > ret_max)
677 ret_max = ret;
678 if (sigint_caught())
679 break;
680 }
681
682 dev_iter_destroy(iter);
683
684 return ret_max;
685 }
686
687 /*
688 * If the lock_type is LCK_VG_READ (used only in reporting commands),
689 * we lock VG_GLOBAL to enable use of metadata cache.
690 * This can pause alongide pvscan or vgscan process for a while.
691 */
692 int process_each_pv(struct cmd_context *cmd, int argc, char **argv,
693 struct volume_group *vg, uint32_t flags,
694 int scan_label_only, void *handle,
695 process_single_pv_fn_t process_single_pv)
696 {
697 int opt = 0;
698 int ret_max = ECMD_PROCESSED;
699 int ret = 0;
700 int lock_global = !(flags & READ_WITHOUT_LOCK) && !(flags & READ_FOR_UPDATE);
701
702 struct pv_list *pvl;
703 struct physical_volume *pv;
704 struct dm_list *pvslist, *vgnames;
705 struct dm_list tags;
706 struct str_list *sll;
707 char *at_sign, *tagname;
708 int scanned = 0;
709
710 dm_list_init(&tags);
711
712 if (lock_global && !lock_vol(cmd, VG_GLOBAL, LCK_VG_READ)) {
713 log_error("Unable to obtain global lock.");
714 return ECMD_FAILED;
715 }
716
717 if (argc) {
718 log_verbose("Using physical volume(s) on command line");
719 for (; opt < argc; opt++) {
720 dm_unescape_colons_and_at_signs(argv[opt], NULL, &at_sign);
721 if (at_sign && (at_sign == argv[opt])) {
722 tagname = at_sign + 1;
723
724 if (!validate_tag(tagname)) {
725 log_error("Skipping invalid tag %s",
726 tagname);
727 if (ret_max < EINVALID_CMD_LINE)
728 ret_max = EINVALID_CMD_LINE;
729 continue;
730 }
731 if (!str_list_add(cmd->mem, &tags,
732 dm_pool_strdup(cmd->mem,
733 tagname))) {
734 log_error("strlist allocation failed");
735 goto bad;
736 }
737 continue;
738 }
739 if (vg) {
740 if (!(pvl = find_pv_in_vg(vg, argv[opt]))) {
741 log_error("Physical Volume \"%s\" not "
742 "found in Volume Group "
743 "\"%s\"", argv[opt],
744 vg->name);
745 ret_max = ECMD_FAILED;
746 continue;
747 }
748 pv = pvl->pv;
749 } else {
750 if (!(pv = pv_read(cmd, argv[opt],
751 1, scan_label_only))) {
752 log_error("Failed to read physical "
753 "volume \"%s\"", argv[opt]);
754 ret_max = ECMD_FAILED;
755 continue;
756 }
757
758 /*
759 * If a PV has no MDAs it may appear to be an
760 * orphan until the metadata is read off
761 * another PV in the same VG. Detecting this
762 * means checking every VG by scanning every
763 * PV on the system.
764 */
765 if (!scanned && is_orphan(pv) &&
766 !dm_list_size(&pv->fid->metadata_areas_in_use) &&
767 !dm_list_size(&pv->fid->metadata_areas_ignored)) {
768 if (!scan_label_only &&
769 !scan_vgs_for_pvs(cmd, 1)) {
770 stack;
771 ret_max = ECMD_FAILED;
772 continue;
773 }
774 scanned = 1;
775 free_pv_fid(pv);
776 if (!(pv = pv_read(cmd, argv[opt],
777 1,
778 scan_label_only))) {
779 log_error("Failed to read "
780 "physical volume "
781 "\"%s\"", argv[opt]);
782 ret_max = ECMD_FAILED;
783 continue;
784 }
785 }
786 }
787
788 ret = process_single_pv(cmd, vg, pv, handle);
789
790 /*
791 * Free PV only if we called pv_read before,
792 * otherwise the PV structure is part of the VG.
793 */
794 if (!vg)
795 free_pv_fid(pv);
796
797 if (ret > ret_max)
798 ret_max = ret;
799 if (sigint_caught())
800 goto out;
801 }
802 if (!dm_list_empty(&tags) && (vgnames = get_vgnames(cmd, 1)) &&
803 !dm_list_empty(vgnames)) {
804 dm_list_iterate_items(sll, vgnames) {
805 vg = vg_read(cmd, sll->str, NULL, flags);
806 if (vg_read_error(vg)) {
807 ret_max = ECMD_FAILED;
808 release_vg(vg);
809 stack;
810 continue;
811 }
812
813 ret = process_each_pv_in_vg(cmd, vg, &tags,
814 handle,
815 process_single_pv);
816
817 unlock_and_release_vg(cmd, vg, sll->str);
818
819 if (ret > ret_max)
820 ret_max = ret;
821 if (sigint_caught())
822 goto out;
823 }
824 }
825 } else {
826 if (vg) {
827 log_verbose("Using all physical volume(s) in "
828 "volume group");
829 ret = process_each_pv_in_vg(cmd, vg, NULL, handle,
830 process_single_pv);
831 if (ret > ret_max)
832 ret_max = ret;
833 if (sigint_caught())
834 goto out;
835 } else if (arg_count(cmd, all_ARG)) {
836 ret = _process_all_devs(cmd, handle, process_single_pv);
837 if (ret > ret_max)
838 ret_max = ret;
839 if (sigint_caught())
840 goto out;
841 } else {
842 log_verbose("Scanning for physical volume names");
843
844 lvmcache_seed_infos_from_lvmetad(cmd);
845 if (!(pvslist = get_pvs(cmd)))
846 goto bad;
847
848 dm_list_iterate_items(pvl, pvslist) {
849 ret = process_single_pv(cmd, NULL, pvl->pv,
850 handle);
851 free_pv_fid(pvl->pv);
852 if (ret > ret_max)
853 ret_max = ret;
854 if (sigint_caught())
855 goto out;
856 }
857 }
858 }
859 out:
860 if (lock_global)
861 unlock_vg(cmd, VG_GLOBAL);
862 return ret_max;
863 bad:
864 if (lock_global)
865 unlock_vg(cmd, VG_GLOBAL);
866
867 return ECMD_FAILED;
868 }
869
870 /*
871 * Determine volume group name from a logical volume name
872 */
873 const char *extract_vgname(struct cmd_context *cmd, const char *lv_name)
874 {
875 const char *vg_name = lv_name;
876 char *st;
877 char *dev_dir = cmd->dev_dir;
878
879 /* Path supplied? */
880 if (vg_name && strchr(vg_name, '/')) {
881 /* Strip dev_dir (optional) */
882 if (*vg_name == '/') {
883 while (*vg_name == '/')
884 vg_name++;
885 vg_name--;
886 }
887 if (!strncmp(vg_name, dev_dir, strlen(dev_dir))) {
888 vg_name += strlen(dev_dir);
889 while (*vg_name == '/')
890 vg_name++;
891 }
892 if (*vg_name == '/') {
893 log_error("\"%s\": Invalid path for Logical "
894 "Volume", lv_name);
895 return 0;
896 }
897
898 /* Require exactly one set of consecutive slashes */
899 if ((st = strchr(vg_name, '/')))
900 while (*st == '/')
901 st++;
902
903 if (!st || strchr(st, '/')) {
904 log_error("\"%s\": Invalid path for Logical Volume",
905 lv_name);
906 return 0;
907 }
908
909 vg_name = dm_pool_strdup(cmd->mem, vg_name);
910 if (!vg_name) {
911 log_error("Allocation of vg_name failed");
912 return 0;
913 }
914
915 *strchr(vg_name, '/') = '\0';
916 return vg_name;
917 }
918
919 if (!(vg_name = default_vgname(cmd))) {
920 if (lv_name)
921 log_error("Path required for Logical Volume \"%s\"",
922 lv_name);
923 return 0;
924 }
925
926 return vg_name;
927 }
928
929 /*
930 * Extract default volume group name from environment
931 */
932 char *default_vgname(struct cmd_context *cmd)
933 {
934 const char *vg_path;
935
936 /* Take default VG from environment? */
937 vg_path = getenv("LVM_VG_NAME");
938 if (!vg_path)
939 return 0;
940
941 vg_path = skip_dev_dir(cmd, vg_path, NULL);
942
943 if (strchr(vg_path, '/')) {
944 log_error("Environment Volume Group in LVM_VG_NAME invalid: "
945 "\"%s\"", vg_path);
946 return 0;
947 }
948
949 return dm_pool_strdup(cmd->mem, vg_path);
950 }
951
952 /*
953 * Process physical extent range specifiers
954 */
955 static int _add_pe_range(struct dm_pool *mem, const char *pvname,
956 struct dm_list *pe_ranges, uint32_t start, uint32_t count)
957 {
958 struct pe_range *per;
959
960 log_debug("Adding PE range: start PE %" PRIu32 " length %" PRIu32
961 " on %s", start, count, pvname);
962
963 /* Ensure no overlap with existing areas */
964 dm_list_iterate_items(per, pe_ranges) {
965 if (((start < per->start) && (start + count - 1 >= per->start))
966 || ((start >= per->start) &&
967 (per->start + per->count - 1) >= start)) {
968 log_error("Overlapping PE ranges specified (%" PRIu32
969 "-%" PRIu32 ", %" PRIu32 "-%" PRIu32 ")"
970 " on %s",
971 start, start + count - 1, per->start,
972 per->start + per->count - 1, pvname);
973 return 0;
974 }
975 }
976
977 if (!(per = dm_pool_alloc(mem, sizeof(*per)))) {
978 log_error("Allocation of list failed");
979 return 0;
980 }
981
982 per->start = start;
983 per->count = count;
984 dm_list_add(pe_ranges, &per->list);
985
986 return 1;
987 }
988
989 static int xstrtouint32(const char *s, char **p, int base, uint32_t *result)
990 {
991 unsigned long ul;
992
993 errno = 0;
994 ul = strtoul(s, p, base);
995 if (errno || *p == s || (uint32_t) ul != ul)
996 return -1;
997 *result = ul;
998 return 0;
999 }
1000
1001 static int _parse_pes(struct dm_pool *mem, char *c, struct dm_list *pe_ranges,
1002 const char *pvname, uint32_t size)
1003 {
1004 char *endptr;
1005 uint32_t start, end;
1006
1007 /* Default to whole PV */
1008 if (!c) {
1009 if (!_add_pe_range(mem, pvname, pe_ranges, UINT32_C(0), size))
1010 return_0;
1011 return 1;
1012 }
1013
1014 while (*c) {
1015 if (*c != ':')
1016 goto error;
1017
1018 c++;
1019
1020 /* Disallow :: and :\0 */
1021 if (*c == ':' || !*c)
1022 goto error;
1023
1024 /* Default to whole range */
1025 start = UINT32_C(0);
1026 end = size - 1;
1027
1028 /* Start extent given? */
1029 if (isdigit(*c)) {
1030 if (xstrtouint32(c, &endptr, 10, &start))
1031 goto error;
1032 c = endptr;
1033 /* Just one number given? */
1034 if (!*c || *c == ':')
1035 end = start;
1036 }
1037 /* Range? */
1038 if (*c == '-') {
1039 c++;
1040 if (isdigit(*c)) {
1041 if (xstrtouint32(c, &endptr, 10, &end))
1042 goto error;
1043 c = endptr;
1044 }
1045 }
1046 if (*c && *c != ':')
1047 goto error;
1048
1049 if ((start > end) || (end > size - 1)) {
1050 log_error("PE range error: start extent %" PRIu32 " to "
1051 "end extent %" PRIu32, start, end);
1052 return 0;
1053 }
1054
1055 if (!_add_pe_range(mem, pvname, pe_ranges, start, end - start + 1))
1056 return_0;
1057
1058 }
1059
1060 return 1;
1061
1062 error:
1063 log_error("Physical extent parsing error at %s", c);
1064 return 0;
1065 }
1066
1067 static int _create_pv_entry(struct dm_pool *mem, struct pv_list *pvl,
1068 char *colon, int allocatable_only, struct dm_list *r)
1069 {
1070 const char *pvname;
1071 struct pv_list *new_pvl = NULL, *pvl2;
1072 struct dm_list *pe_ranges;
1073
1074 pvname = pv_dev_name(pvl->pv);
1075 if (allocatable_only && !(pvl->pv->status & ALLOCATABLE_PV)) {
1076 log_error("Physical volume %s not allocatable", pvname);
1077 return 1;
1078 }
1079
1080 if (allocatable_only && is_missing_pv(pvl->pv)) {
1081 log_error("Physical volume %s is missing", pvname);
1082 return 1;
1083 }
1084
1085 if (allocatable_only &&
1086 (pvl->pv->pe_count == pvl->pv->pe_alloc_count)) {
1087 log_error("No free extents on physical volume \"%s\"", pvname);
1088 return 1;
1089 }
1090
1091 dm_list_iterate_items(pvl2, r)
1092 if (pvl->pv->dev == pvl2->pv->dev) {
1093 new_pvl = pvl2;
1094 break;
1095 }
1096
1097 if (!new_pvl) {
1098 if (!(new_pvl = dm_pool_alloc(mem, sizeof(*new_pvl)))) {
1099 log_error("Unable to allocate physical volume list.");
1100 return 0;
1101 }
1102
1103 memcpy(new_pvl, pvl, sizeof(*new_pvl));
1104
1105 if (!(pe_ranges = dm_pool_alloc(mem, sizeof(*pe_ranges)))) {
1106 log_error("Allocation of pe_ranges list failed");
1107 return 0;
1108 }
1109 dm_list_init(pe_ranges);
1110 new_pvl->pe_ranges = pe_ranges;
1111 dm_list_add(r, &new_pvl->list);
1112 }
1113
1114 /* Determine selected physical extents */
1115 if (!_parse_pes(mem, colon, new_pvl->pe_ranges, pv_dev_name(pvl->pv),
1116 pvl->pv->pe_count))
1117 return_0;
1118
1119 return 1;
1120 }
1121
1122 struct dm_list *create_pv_list(struct dm_pool *mem, struct volume_group *vg, int argc,
1123 char **argv, int allocatable_only)
1124 {
1125 struct dm_list *r;
1126 struct pv_list *pvl;
1127 struct dm_list tags, arg_pvnames;
1128 char *pvname = NULL;
1129 char *colon, *at_sign, *tagname;
1130 int i;
1131
1132 /* Build up list of PVs */
1133 if (!(r = dm_pool_alloc(mem, sizeof(*r)))) {
1134 log_error("Allocation of list failed");
1135 return NULL;
1136 }
1137 dm_list_init(r);
1138
1139 dm_list_init(&tags);
1140 dm_list_init(&arg_pvnames);
1141
1142 for (i = 0; i < argc; i++) {
1143 dm_unescape_colons_and_at_signs(argv[i], &colon, &at_sign);
1144
1145 if (at_sign && (at_sign == argv[i])) {
1146 tagname = at_sign + 1;
1147 if (!validate_tag(tagname)) {
1148 log_error("Skipping invalid tag %s", tagname);
1149 continue;
1150 }
1151 dm_list_iterate_items(pvl, &vg->pvs) {
1152 if (str_list_match_item(&pvl->pv->tags,
1153 tagname)) {
1154 if (!_create_pv_entry(mem, pvl, NULL,
1155 allocatable_only,
1156 r))
1157 return_NULL;
1158 }
1159 }
1160 continue;
1161 }
1162
1163 pvname = argv[i];
1164
1165 if (colon && !(pvname = dm_pool_strndup(mem, pvname,
1166 (unsigned) (colon - pvname)))) {
1167 log_error("Failed to clone PV name");
1168 return NULL;
1169 }
1170
1171 if (!(pvl = find_pv_in_vg(vg, pvname))) {
1172 log_error("Physical Volume \"%s\" not found in "
1173 "Volume Group \"%s\"", pvname, vg->name);
1174 return NULL;
1175 }
1176 if (!_create_pv_entry(mem, pvl, colon, allocatable_only, r))
1177 return_NULL;
1178 }
1179
1180 if (dm_list_empty(r))
1181 log_error("No specified PVs have space available");
1182
1183 return dm_list_empty(r) ? NULL : r;
1184 }
1185
1186 struct dm_list *clone_pv_list(struct dm_pool *mem, struct dm_list *pvsl)
1187 {
1188 struct dm_list *r;
1189 struct pv_list *pvl, *new_pvl;
1190
1191 /* Build up list of PVs */
1192 if (!(r = dm_pool_alloc(mem, sizeof(*r)))) {
1193 log_error("Allocation of list failed");
1194 return NULL;
1195 }
1196 dm_list_init(r);
1197
1198 dm_list_iterate_items(pvl, pvsl) {
1199 if (!(new_pvl = dm_pool_zalloc(mem, sizeof(*new_pvl)))) {
1200 log_error("Unable to allocate physical volume list.");
1201 return NULL;
1202 }
1203
1204 memcpy(new_pvl, pvl, sizeof(*new_pvl));
1205 dm_list_add(r, &new_pvl->list);
1206 }
1207
1208 return r;
1209 }
1210
1211 void vgcreate_params_set_defaults(struct vgcreate_params *vp_def,
1212 struct volume_group *vg)
1213 {
1214 if (vg) {
1215 vp_def->vg_name = NULL;
1216 vp_def->extent_size = vg->extent_size;
1217 vp_def->max_pv = vg->max_pv;
1218 vp_def->max_lv = vg->max_lv;
1219 vp_def->alloc = vg->alloc;
1220 vp_def->clustered = vg_is_clustered(vg);
1221 vp_def->vgmetadatacopies = vg->mda_copies;
1222 } else {
1223 vp_def->vg_name = NULL;
1224 vp_def->extent_size = DEFAULT_EXTENT_SIZE * 2;
1225 vp_def->max_pv = DEFAULT_MAX_PV;
1226 vp_def->max_lv = DEFAULT_MAX_LV;
1227 vp_def->alloc = DEFAULT_ALLOC_POLICY;
1228 vp_def->clustered = DEFAULT_CLUSTERED;
1229 vp_def->vgmetadatacopies = DEFAULT_VGMETADATACOPIES;
1230 }
1231 }
1232
1233 /*
1234 * Set members of struct vgcreate_params from cmdline arguments.
1235 * Do preliminary validation with arg_*() interface.
1236 * Further, more generic validation is done in validate_vgcreate_params().
1237 * This function is to remain in tools directory.
1238 */
1239 int vgcreate_params_set_from_args(struct cmd_context *cmd,
1240 struct vgcreate_params *vp_new,
1241 struct vgcreate_params *vp_def)
1242 {
1243 vp_new->vg_name = skip_dev_dir(cmd, vp_def->vg_name, NULL);
1244 vp_new->max_lv = arg_uint_value(cmd, maxlogicalvolumes_ARG,
1245 vp_def->max_lv);
1246 vp_new->max_pv = arg_uint_value(cmd, maxphysicalvolumes_ARG,
1247 vp_def->max_pv);
1248 vp_new->alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, vp_def->alloc);
1249
1250 /* Units of 512-byte sectors */
1251 vp_new->extent_size =
1252 arg_uint_value(cmd, physicalextentsize_ARG, vp_def->extent_size);
1253
1254 if (arg_count(cmd, clustered_ARG))
1255 vp_new->clustered =
1256 !strcmp(arg_str_value(cmd, clustered_ARG,
1257 vp_def->clustered ? "y":"n"), "y");
1258 else
1259 /* Default depends on current locking type */
1260 vp_new->clustered = locking_is_clustered();
1261
1262 if (arg_sign_value(cmd, physicalextentsize_ARG, SIGN_NONE) == SIGN_MINUS) {
1263 log_error("Physical extent size may not be negative");
1264 return 1;
1265 }
1266
1267 if (arg_uint64_value(cmd, physicalextentsize_ARG, 0) > MAX_EXTENT_SIZE) {
1268 log_error("Physical extent size cannot be larger than %s",
1269 display_size(cmd, (uint64_t) MAX_EXTENT_SIZE));
1270 return 1;
1271 }
1272
1273 if (arg_sign_value(cmd, maxlogicalvolumes_ARG, SIGN_NONE) == SIGN_MINUS) {
1274 log_error("Max Logical Volumes may not be negative");
1275 return 1;
1276 }
1277
1278 if (arg_sign_value(cmd, maxphysicalvolumes_ARG, SIGN_NONE) == SIGN_MINUS) {
1279 log_error("Max Physical Volumes may not be negative");
1280 return 1;
1281 }
1282
1283 if (arg_count(cmd, metadatacopies_ARG)) {
1284 vp_new->vgmetadatacopies = arg_int_value(cmd, metadatacopies_ARG,
1285 DEFAULT_VGMETADATACOPIES);
1286 } else if (arg_count(cmd, vgmetadatacopies_ARG)) {
1287 vp_new->vgmetadatacopies = arg_int_value(cmd, vgmetadatacopies_ARG,
1288 DEFAULT_VGMETADATACOPIES);
1289 } else {
1290 vp_new->vgmetadatacopies = find_config_tree_int(cmd,
1291 "metadata/vgmetadatacopies",
1292 DEFAULT_VGMETADATACOPIES);
1293 }
1294
1295 return 0;
1296 }
1297
1298 int lv_refresh(struct cmd_context *cmd, struct logical_volume *lv)
1299 {
1300 int r = 0;
1301
1302 if (!cmd->partial_activation && (lv->status & PARTIAL_LV)) {
1303 log_error("Refusing refresh of partial LV %s. Use --partial to override.",
1304 lv->name);
1305 goto out;
1306 }
1307
1308 r = suspend_lv(cmd, lv);
1309 if (!r)
1310 goto_out;
1311
1312 r = resume_lv(cmd, lv);
1313 if (!r)
1314 goto_out;
1315
1316 /*
1317 * check if snapshot merge should be polled
1318 * - unfortunately: even though the dev_manager will clear
1319 * the lv's merge attributes if a merge is not possible;
1320 * it is clearing a different instance of the lv (as
1321 * retrieved with lv_from_lvid)
1322 * - fortunately: polldaemon will immediately shutdown if the
1323 * origin doesn't have a status with a snapshot percentage
1324 */
1325 if (background_polling() && lv_is_origin(lv) && lv_is_merging_origin(lv))
1326 lv_spawn_background_polling(cmd, lv);
1327
1328 out:
1329 return r;
1330 }
1331
1332 int vg_refresh_visible(struct cmd_context *cmd, struct volume_group *vg)
1333 {
1334 struct lv_list *lvl;
1335 int r = 1;
1336
1337 sigint_allow();
1338 dm_list_iterate_items(lvl, &vg->lvs) {
1339 if (sigint_caught())
1340 return_0;
1341
1342 if (lv_is_visible(lvl->lv))
1343 if (!lv_refresh(cmd, lvl->lv))
1344 r = 0;
1345 }
1346
1347 sigint_restore();
1348
1349 return r;
1350 }
1351
1352 void lv_spawn_background_polling(struct cmd_context *cmd,
1353 struct logical_volume *lv)
1354 {
1355 const char *pvname;
1356
1357 if ((lv->status & PVMOVE) &&
1358 (pvname = get_pvmove_pvname_from_lv_mirr(lv))) {
1359 log_verbose("Spawning background pvmove process for %s",
1360 pvname);
1361 pvmove_poll(cmd, pvname, 1);
1362 } else if ((lv->status & LOCKED) &&
1363 (pvname = get_pvmove_pvname_from_lv(lv))) {
1364 log_verbose("Spawning background pvmove process for %s",
1365 pvname);
1366 pvmove_poll(cmd, pvname, 1);
1367 }
1368
1369 if (lv->status & (CONVERTING|MERGING)) {
1370 log_verbose("Spawning background lvconvert process for %s",
1371 lv->name);
1372 lvconvert_poll(cmd, lv, 1);
1373 }
1374 }
1375
1376 /*
1377 * Intial sanity checking of non-recovery related command-line arguments.
1378 *
1379 * Output arguments:
1380 * pp: structure allocated by caller, fields written / validated here
1381 */
1382 int pvcreate_params_validate(struct cmd_context *cmd,
1383 int argc, char **argv,
1384 struct pvcreate_params *pp)
1385 {
1386 if (!argc) {
1387 log_error("Please enter a physical volume path");
1388 return 0;
1389 }
1390
1391 pp->yes = arg_count(cmd, yes_ARG);
1392 pp->force = (force_t) arg_count(cmd, force_ARG);
1393
1394 if (arg_int_value(cmd, labelsector_ARG, 0) >= LABEL_SCAN_SECTORS) {
1395 log_error("labelsector must be less than %lu",
1396 LABEL_SCAN_SECTORS);
1397 return 0;
1398 } else {
1399 pp->labelsector = arg_int64_value(cmd, labelsector_ARG,
1400 DEFAULT_LABELSECTOR);
1401 }
1402
1403 if (!(cmd->fmt->features & FMT_MDAS) &&
1404 (arg_count(cmd, pvmetadatacopies_ARG) ||
1405 arg_count(cmd, metadatasize_ARG) ||
1406 arg_count(cmd, dataalignment_ARG) ||
1407 arg_count(cmd, dataalignmentoffset_ARG))) {
1408 log_error("Metadata and data alignment parameters only "
1409 "apply to text format.");
1410 return 0;
1411 }
1412
1413 if (arg_count(cmd, pvmetadatacopies_ARG) &&
1414 arg_int_value(cmd, pvmetadatacopies_ARG, -1) > 2) {
1415 log_error("Metadatacopies may only be 0, 1 or 2");
1416 return 0;
1417 }
1418
1419 if (arg_count(cmd, metadataignore_ARG)) {
1420 pp->metadataignore = !strcmp(arg_str_value(cmd,
1421 metadataignore_ARG,
1422 DEFAULT_PVMETADATAIGNORE_STR),
1423 "y");
1424 } else {
1425 pp->metadataignore = !strcmp(find_config_tree_str(cmd,
1426 "metadata/pvmetadataignore",
1427 DEFAULT_PVMETADATAIGNORE_STR),
1428 "y");
1429 }
1430 if (arg_count(cmd, pvmetadatacopies_ARG) &&
1431 !arg_int_value(cmd, pvmetadatacopies_ARG, -1) &&
1432 pp->metadataignore) {
1433 log_error("metadataignore only applies to metadatacopies > 0");
1434 return 0;
1435 }
1436
1437 if (arg_count(cmd, zero_ARG))
1438 pp->zero = strcmp(arg_str_value(cmd, zero_ARG, "y"), "n");
1439
1440 if (arg_sign_value(cmd, dataalignment_ARG, SIGN_NONE) == SIGN_MINUS) {
1441 log_error("Physical volume data alignment may not be negative");
1442 return 0;
1443 }
1444 pp->data_alignment = arg_uint64_value(cmd, dataalignment_ARG, UINT64_C(0));
1445
1446 if (pp->data_alignment > UINT32_MAX) {
1447 log_error("Physical volume data alignment is too big.");
1448 return 0;
1449 }
1450
1451 if (pp->data_alignment && pp->pe_start) {
1452 if (pp->pe_start % pp->data_alignment)
1453 log_warn("WARNING: Ignoring data alignment %" PRIu64
1454 " incompatible with --restorefile value (%"
1455 PRIu64").", pp->data_alignment, pp->pe_start);
1456 pp->data_alignment = 0;
1457 }
1458
1459 if (arg_sign_value(cmd, dataalignmentoffset_ARG, SIGN_NONE) == SIGN_MINUS) {
1460 log_error("Physical volume data alignment offset may not be negative");
1461 return 0;
1462 }
1463 pp->data_alignment_offset = arg_uint64_value(cmd, dataalignmentoffset_ARG, UINT64_C(0));
1464
1465 if (pp->data_alignment_offset > UINT32_MAX) {
1466 log_error("Physical volume data alignment offset is too big.");
1467 return 0;
1468 }
1469
1470 if (pp->data_alignment_offset && pp->pe_start) {
1471 log_warn("WARNING: Ignoring data alignment offset %" PRIu64
1472 " incompatible with --restorefile value (%"
1473 PRIu64").", pp->data_alignment_offset, pp->pe_start);
1474 pp->data_alignment_offset = 0;
1475 }
1476
1477 if (arg_sign_value(cmd, metadatasize_ARG, SIGN_NONE) == SIGN_MINUS) {
1478 log_error("Metadata size may not be negative");
1479 return 0;
1480 }
1481
1482 pp->pvmetadatasize = arg_uint64_value(cmd, metadatasize_ARG, UINT64_C(0));
1483 if (!pp->pvmetadatasize)
1484 pp->pvmetadatasize = find_config_tree_int(cmd,
1485 "metadata/pvmetadatasize",
1486 DEFAULT_PVMETADATASIZE);
1487
1488 pp->pvmetadatacopies = arg_int_value(cmd, pvmetadatacopies_ARG, -1);
1489 if (pp->pvmetadatacopies < 0)
1490 pp->pvmetadatacopies = find_config_tree_int(cmd,
1491 "metadata/pvmetadatacopies",
1492 DEFAULT_PVMETADATACOPIES);
1493
1494 return 1;
1495 }
1496
1497 int get_activation_monitoring_mode(struct cmd_context *cmd,
1498 int *monitoring_mode)
1499 {
1500 *monitoring_mode = DEFAULT_DMEVENTD_MONITOR;
1501
1502 if (arg_count(cmd, monitor_ARG) &&
1503 (arg_count(cmd, ignoremonitoring_ARG) ||
1504 arg_count(cmd, sysinit_ARG))) {
1505 log_error("--ignoremonitoring or --sysinit option not allowed with --monitor option");
1506 return 0;
1507 }
1508
1509 if (arg_count(cmd, monitor_ARG))
1510 *monitoring_mode = arg_int_value(cmd, monitor_ARG,
1511 DEFAULT_DMEVENTD_MONITOR);
1512 else if (is_static() || arg_count(cmd, ignoremonitoring_ARG) ||
1513 arg_count(cmd, sysinit_ARG) ||
1514 !find_config_tree_bool(cmd, "activation/monitoring",
1515 DEFAULT_DMEVENTD_MONITOR))
1516 *monitoring_mode = DMEVENTD_MONITOR_IGNORE;
1517
1518 return 1;
1519 }
1520
1521 /*
1522 * Generic stripe parameter checks.
1523 */
1524 static int _validate_stripe_params(struct cmd_context *cmd, uint32_t *stripes,
1525 uint32_t *stripe_size)
1526 {
1527 if (*stripes == 1 && *stripe_size) {
1528 log_print("Ignoring stripesize argument with single stripe");
1529 *stripe_size = 0;
1530 }
1531
1532 if (*stripes > 1 && !*stripe_size) {
1533 *stripe_size = find_config_tree_int(cmd, "metadata/stripesize", DEFAULT_STRIPESIZE) * 2;
1534 log_print("Using default stripesize %s",
1535 display_size(cmd, (uint64_t) *stripe_size));
1536 }
1537
1538 if (*stripes < 1 || *stripes > MAX_STRIPES) {
1539 log_error("Number of stripes (%d) must be between %d and %d",
1540 *stripes, 1, MAX_STRIPES);
1541 return 0;
1542 }
1543
1544 if (*stripes > 1 && (*stripe_size < STRIPE_SIZE_MIN ||
1545 *stripe_size & (*stripe_size - 1))) {
1546 log_error("Invalid stripe size %s",
1547 display_size(cmd, (uint64_t) *stripe_size));
1548 return 0;
1549 }
1550
1551 return 1;
1552 }
1553
1554 /*
1555 * The stripe size is limited by the size of a uint32_t, but since the
1556 * value given by the user is doubled, and the final result must be a
1557 * power of 2, we must divide UINT_MAX by four and add 1 (to round it
1558 * up to the power of 2)
1559 */
1560 int get_stripe_params(struct cmd_context *cmd, uint32_t *stripes, uint32_t *stripe_size)
1561 {
1562 /* stripes_long_ARG takes precedence (for lvconvert) */
1563 *stripes = arg_uint_value(cmd, arg_count(cmd, stripes_long_ARG) ? stripes_long_ARG : stripes_ARG, 1);
1564
1565 *stripe_size = arg_uint_value(cmd, stripesize_ARG, 0);
1566 if (*stripe_size) {
1567 if (arg_sign_value(cmd, stripesize_ARG, SIGN_NONE) == SIGN_MINUS) {
1568 log_error("Negative stripesize is invalid");
1569 return 0;
1570 }
1571
1572 if(arg_uint64_value(cmd, stripesize_ARG, 0) > STRIPE_SIZE_LIMIT * 2) {
1573 log_error("Stripe size cannot be larger than %s",
1574 display_size(cmd, (uint64_t) STRIPE_SIZE_LIMIT));
1575 return 0;
1576 }
1577 }
1578
1579 return _validate_stripe_params(cmd, stripes, stripe_size);
1580 }
1581
1582 /* FIXME move to lib */
1583 static int _pv_change_tag(struct physical_volume *pv, const char *tag, int addtag)
1584 {
1585 if (addtag) {
1586 if (!str_list_add(pv->fmt->cmd->mem, &pv->tags, tag)) {
1587 log_error("Failed to add tag %s to physical volume %s",
1588 tag, pv_dev_name(pv));
1589 return 0;
1590 }
1591 } else
1592 str_list_del(&pv->tags, tag);
1593
1594 return 1;
1595 }
1596
1597 /* Set exactly one of VG, LV or PV */
1598 int change_tag(struct cmd_context *cmd, struct volume_group *vg,
1599 struct logical_volume *lv, struct physical_volume *pv, int arg)
1600 {
1601 const char *tag;
1602 struct arg_value_group_list *current_group;
1603
1604 dm_list_iterate_items(current_group, &cmd->arg_value_groups) {
1605 if (!grouped_arg_is_set(current_group->arg_values, arg))
1606 continue;
1607
1608 if (!(tag = grouped_arg_str_value(current_group->arg_values, arg, NULL))) {
1609 log_error("Failed to get tag");
1610 return 0;
1611 }
1612
1613 if (vg && !vg_change_tag(vg, tag, arg == addtag_ARG))
1614 return_0;
1615 else if (lv && !lv_change_tag(lv, tag, arg == addtag_ARG))
1616 return_0;
1617 else if (pv && !_pv_change_tag(pv, tag, arg == addtag_ARG))
1618 return_0;
1619 }
1620
1621 return 1;
1622 }
1623
1624 /* Return percents of extents and avoid overflow, with optional roundup */
1625 uint32_t percent_of_extents(uint32_t percents, uint32_t count, int roundup)
1626 {
1627 return (uint32_t)(((uint64_t)percents * (uint64_t)count +
1628 ((roundup) ? 99 : 0)) / 100);
1629 }
This page took 0.102556 seconds and 5 git commands to generate.