2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
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.
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
23 const char *command_name(struct cmd_context *cmd)
25 return cmd->command->name;
29 * Strip dev_dir if present
31 char *skip_dev_dir(struct cmd_context *cmd, const char *vg_name,
32 unsigned *dev_dir_found)
34 const char *dmdir = dm_dir();
35 size_t dmdir_len = strlen(dmdir), vglv_sz;
36 char *vgname, *lvname, *layer, *vglv;
38 /* FIXME Do this properly */
39 if (*vg_name == '/') {
40 while (*vg_name == '/')
45 /* Reformat string if /dev/mapper found */
46 if (!strncmp(vg_name, dmdir, dmdir_len) && vg_name[dmdir_len] == '/') {
50 while (*vg_name == '/')
53 if (!dm_split_lvm_name(cmd->mem, vg_name, &vgname, &lvname, &layer) ||
55 log_error("skip_dev_dir: Couldn't split up device name %s",
57 return (char *) vg_name;
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,
64 log_error("vg/lv string alloc failed");
65 return (char *) vg_name;
70 if (!strncmp(vg_name, cmd->dev_dir, strlen(cmd->dev_dir))) {
73 vg_name += strlen(cmd->dev_dir);
74 while (*vg_name == '/')
76 } else if (dev_dir_found)
79 return (char *) vg_name;
83 * Metadata iteration functions
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,
91 process_single_lv_fn_t process_single_lv)
93 int ret_max = ECMD_PROCESSED;
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;
103 if (!vg_check_status(vg, EXPORTED_VG))
106 if (tags && !dm_list_empty(tags))
109 if (arg_lvnames && !dm_list_empty(arg_lvnames))
112 /* Process all LVs in this VG if no restrictions given */
113 if (!tags_supplied && !lvargs_supplied)
116 /* Or if VG tags match */
117 if (!process_lv && tags_supplied &&
118 str_list_match_list(tags, &vg->tags, NULL)) {
122 dm_list_iterate_items(lvl, &vg->lvs) {
123 if (lvl->lv->status & SNAPSHOT)
126 if (lv_is_virtual_origin(lvl->lv) && !arg_count(cmd, all_ARG))
130 * Only let hidden LVs through it --all was used or the LVs
131 * were specifically named on the command line.
133 if (!lvargs_supplied && !lv_is_visible(lvl->lv) && !arg_count(cmd, all_ARG))
136 /* Should we process this LV? */
143 if (!process_lv && tags_supplied &&
144 str_list_match_list(tags, &lvl->lv->tags, NULL)) {
149 if (lvargs_supplied &&
150 str_list_match_item(arg_lvnames, lvl->lv->name)) {
158 lvl->lv->vg->cmd_missing_vgs = 0;
159 ret = process_single_lv(cmd, lvl->lv, handle);
160 if (ret != ECMD_PROCESSED && failed_lvnames) {
161 lv_name = dm_pool_strdup(cmd->mem, lvl->lv->name);
163 !str_list_add(cmd->mem, failed_lvnames, lv_name)) {
164 log_error("Allocation failed for str_list.");
167 if (lvl->lv->vg->cmd_missing_vgs)
168 ret = ECMD_PROCESSED;
176 if (lvargs_supplied && lvargs_matched != dm_list_size(arg_lvnames)) {
177 log_error("One or more specified logical volume(s) not found.");
178 if (ret_max < ECMD_FAILED)
179 ret_max = ECMD_FAILED;
185 int process_each_lv(struct cmd_context *cmd, int argc, char **argv,
186 uint32_t flags, void *handle,
187 process_single_lv_fn_t process_single_lv)
190 int ret_max = ECMD_PROCESSED;
193 struct dm_list *tags_arg;
194 struct dm_list *vgnames; /* VGs to process */
195 struct str_list *sll, *strl;
196 struct cmd_vg *cvl_vg;
197 struct dm_list cmd_vgs;
198 struct dm_list failed_lvnames;
199 struct dm_list tags, lvnames;
200 struct dm_list arg_lvnames; /* Cmdline vgname or vgname/lvname */
201 struct dm_list arg_vgnames;
208 dm_list_init(&arg_lvnames);
209 dm_list_init(&failed_lvnames);
212 log_verbose("Using logical volume(s) on command line");
213 dm_list_init(&arg_vgnames);
215 for (; opt < argc; opt++) {
216 const char *lv_name = argv[opt];
217 const char *tmp_lv_name;
219 unsigned dev_dir_found = 0;
221 /* Do we have a tag or vgname or lvname? */
224 if (*vgname == '@') {
225 if (!validate_tag(vgname + 1)) {
226 log_error("Skipping invalid tag %s",
230 if (!str_list_add(cmd->mem, &tags,
231 dm_pool_strdup(cmd->mem,
233 log_error("strlist allocation failed");
239 /* FIXME Jumbled parsing */
240 vgname = skip_dev_dir(cmd, vgname, &dev_dir_found);
242 if (*vgname == '/') {
243 log_error("\"%s\": Invalid path for Logical "
244 "Volume", argv[opt]);
245 if (ret_max < ECMD_FAILED)
246 ret_max = ECMD_FAILED;
250 if ((tmp_lv_name = strchr(vgname, '/'))) {
252 lv_name = tmp_lv_name;
253 while (*lv_name == '/')
255 if (!(vgname = extract_vgname(cmd, vgname))) {
256 if (ret_max < ECMD_FAILED)
257 ret_max = ECMD_FAILED;
260 } else if (!dev_dir_found &&
261 (vgname_def = default_vgname(cmd))) {
266 if (!str_list_add(cmd->mem, &arg_vgnames,
267 dm_pool_strdup(cmd->mem, vgname))) {
268 log_error("strlist allocation failed");
273 if (!str_list_add(cmd->mem, &arg_lvnames,
274 dm_pool_strdup(cmd->mem,
276 log_error("strlist allocation failed");
280 vglv_sz = strlen(vgname) + strlen(lv_name) + 2;
281 if (!(vglv = dm_pool_alloc(cmd->mem, vglv_sz)) ||
282 dm_snprintf(vglv, vglv_sz, "%s/%s", vgname,
284 log_error("vg/lv string alloc failed");
287 if (!str_list_add(cmd->mem, &arg_lvnames, vglv)) {
288 log_error("strlist allocation failed");
293 vgnames = &arg_vgnames;
296 if (!argc || !dm_list_empty(&tags)) {
297 log_verbose("Finding all logical volumes");
298 if (!(vgnames = get_vgnames(cmd, 0)) || dm_list_empty(vgnames)) {
299 log_error("No volume groups found");
304 dm_list_iterate_items(strl, vgnames) {
306 dm_list_init(&cmd_vgs);
307 if (!(cvl_vg = cmd_vg_add(cmd->mem, &cmd_vgs,
308 vgname, NULL, flags))) {
313 if (!cmd_vg_read(cmd, &cmd_vgs)) {
314 free_cmd_vgs(&cmd_vgs);
315 if (ret_max < ECMD_FAILED) {
316 log_error("Skipping volume group %s", vgname);
317 ret_max = ECMD_FAILED;
324 dm_list_init(&lvnames); /* LVs to be processed in this VG */
325 dm_list_iterate_items(sll, &arg_lvnames) {
326 const char *vg_name = sll->str;
327 const char *lv_name = strchr(vg_name, '/');
329 if ((!lv_name && !strcmp(vg_name, vgname))) {
330 /* Process all LVs in this VG */
332 dm_list_init(&lvnames);
334 } else if (!strncmp(vg_name, vgname, strlen(vgname)) && lv_name &&
335 strlen(vgname) == (size_t) (lv_name - vg_name)) {
336 if (!str_list_add(cmd->mem, &lvnames,
337 dm_pool_strdup(cmd->mem,
339 log_error("strlist allocation failed");
340 free_cmd_vgs(&cmd_vgs);
346 while (!sigint_caught()) {
347 ret = process_each_lv_in_vg(cmd, cvl_vg->vg, &lvnames,
348 tags_arg, &failed_lvnames,
349 handle, process_single_lv);
350 if (ret != ECMD_PROCESSED ||
351 dm_list_empty(&failed_lvnames))
354 /* Try again with failed LVs in this VG */
355 dm_list_init(&lvnames);
356 dm_list_splice(&lvnames, &failed_lvnames);
358 free_cmd_vgs(&cmd_vgs);
359 if (!cmd_vg_read(cmd, &cmd_vgs)) {
360 ret = ECMD_FAILED; /* break */
367 free_cmd_vgs(&cmd_vgs);
368 /* FIXME: logic for breaking command is not consistent */
376 int process_each_segment_in_pv(struct cmd_context *cmd,
377 struct volume_group *vg,
378 struct physical_volume *pv,
380 process_single_pvseg_fn_t process_single_pvseg)
382 struct pv_segment *pvseg;
384 const char *vg_name = NULL;
385 int ret_max = ECMD_PROCESSED;
387 struct volume_group *old_vg = vg;
388 struct pv_segment _free_pv_segment = { .pv = pv };
390 if (is_pv(pv) && !vg && !is_orphan(pv)) {
391 vg_name = pv_vg_name(pv);
393 vg = vg_read(cmd, vg_name, NULL, 0);
394 if (vg_read_error(vg)) {
396 log_error("Skipping volume group %s", vg_name);
401 * Replace possibly incomplete PV structure with new one
402 * allocated in vg_read_internal() path.
404 if (!(pvl = find_pv_in_vg(vg, pv_dev_name(pv)))) {
405 log_error("Unable to find %s in volume group %s",
406 pv_dev_name(pv), vg_name);
407 unlock_and_free_vg(cmd, vg, vg_name);
414 if (dm_list_empty(&pv->segments)) {
415 ret = process_single_pvseg(cmd, NULL, &_free_pv_segment, handle);
419 dm_list_iterate_items(pvseg, &pv->segments) {
420 ret = process_single_pvseg(cmd, vg, pvseg, handle);
428 unlock_vg(cmd, vg_name);
435 int process_each_segment_in_lv(struct cmd_context *cmd,
436 struct logical_volume *lv,
438 process_single_seg_fn_t process_single_seg)
440 struct lv_segment *seg;
441 int ret_max = ECMD_PROCESSED;
444 dm_list_iterate_items(seg, &lv->segments) {
445 ret = process_single_seg(cmd, seg, handle);
448 /* FIXME: logic for breaking command is not consistent */
456 static int _process_one_vg(struct cmd_context *cmd, const char *vg_name,
458 struct dm_list *tags, struct dm_list *arg_vgnames,
459 uint32_t flags, void *handle, int ret_max,
460 process_single_vg_fn_t process_single_vg)
462 struct dm_list cmd_vgs;
463 struct cmd_vg *cvl_vg;
466 log_verbose("Finding volume group \"%s\"", vg_name);
468 dm_list_init(&cmd_vgs);
469 if (!(cvl_vg = cmd_vg_add(cmd->mem, &cmd_vgs, vg_name, vgid, flags)))
473 /* FIXME: consistent handling of command break */
474 if (sigint_caught()) {
478 if (!cmd_vg_read(cmd, &cmd_vgs))
479 /* Allow FAILED_INCONSISTENT through only for vgcfgrestore */
480 if (vg_read_error(cvl_vg->vg) &&
481 (!((flags & READ_ALLOW_INCONSISTENT) &&
482 (vg_read_error(cvl_vg->vg) == FAILED_INCONSISTENT)))) {
487 if (!dm_list_empty(tags) &&
488 /* Only process if a tag matches or it's on arg_vgnames */
489 !str_list_match_item(arg_vgnames, vg_name) &&
490 !str_list_match_list(tags, &cvl_vg->vg->tags, NULL))
493 ret = process_single_vg(cmd, vg_name, cvl_vg->vg, handle);
495 if (vg_read_error(cvl_vg->vg)) /* FAILED_INCONSISTENT */
498 if (!cvl_vg->vg->cmd_missing_vgs)
501 free_cmd_vgs(&cmd_vgs);
504 free_cmd_vgs(&cmd_vgs);
506 return (ret > ret_max) ? ret : ret_max;
509 int process_each_vg(struct cmd_context *cmd, int argc, char **argv,
510 uint32_t flags, void *handle,
511 process_single_vg_fn_t process_single_vg)
514 int ret_max = ECMD_PROCESSED;
517 struct dm_list *vgnames, *vgids;
518 struct dm_list arg_vgnames, tags;
520 const char *vg_name, *vgid;
523 dm_list_init(&arg_vgnames);
526 log_verbose("Using volume group(s) on command line");
528 for (; opt < argc; opt++) {
530 if (*vg_name == '@') {
531 if (!validate_tag(vg_name + 1)) {
532 log_error("Skipping invalid tag %s",
534 if (ret_max < EINVALID_CMD_LINE)
535 ret_max = EINVALID_CMD_LINE;
538 if (!str_list_add(cmd->mem, &tags,
539 dm_pool_strdup(cmd->mem,
541 log_error("strlist allocation failed");
547 vg_name = skip_dev_dir(cmd, vg_name, NULL);
548 if (strchr(vg_name, '/')) {
549 log_error("Invalid volume group name: %s",
551 if (ret_max < EINVALID_CMD_LINE)
552 ret_max = EINVALID_CMD_LINE;
555 if (!str_list_add(cmd->mem, &arg_vgnames,
556 dm_pool_strdup(cmd->mem, vg_name))) {
557 log_error("strlist allocation failed");
562 vgnames = &arg_vgnames;
565 if (!argc || !dm_list_empty(&tags)) {
566 log_verbose("Finding all volume groups");
567 if (!(vgids = get_vgids(cmd, 0)) || dm_list_empty(vgids)) {
568 log_error("No volume groups found");
571 dm_list_iterate_items(sl, vgids) {
573 if (!(vgid) || !(vg_name = vgname_from_vgid(cmd->mem, vgid)))
575 ret_max = _process_one_vg(cmd, vg_name, vgid, &tags,
578 ret_max, process_single_vg);
583 dm_list_iterate_items(sl, vgnames) {
585 if (is_orphan_vg(vg_name))
586 continue; /* FIXME Unnecessary? */
587 ret_max = _process_one_vg(cmd, vg_name, NULL, &tags,
590 ret_max, process_single_vg);
599 int process_each_pv_in_vg(struct cmd_context *cmd, struct volume_group *vg,
600 const struct dm_list *tags, void *handle,
601 process_single_pv_fn_t process_single_pv)
603 int ret_max = ECMD_PROCESSED;
607 dm_list_iterate_items(pvl, &vg->pvs) {
608 if (tags && !dm_list_empty(tags) &&
609 !str_list_match_list(tags, &pvl->pv->tags, NULL)) {
612 if ((ret = process_single_pv(cmd, vg, pvl->pv, handle)) > ret_max)
621 static int _process_all_devs(struct cmd_context *cmd, void *handle,
622 process_single_pv_fn_t process_single_pv)
624 struct physical_volume *pv;
625 struct physical_volume pv_dummy;
626 struct dev_iter *iter;
629 int ret_max = ECMD_PROCESSED;
632 if (!scan_vgs_for_pvs(cmd, 1)) {
637 if (!(iter = dev_iter_create(cmd->filter, 1))) {
638 log_error("dev_iter creation failed");
642 while ((dev = dev_iter_get(iter))) {
643 if (!(pv = pv_read(cmd, dev_name(dev), NULL, NULL, 0, 0))) {
644 memset(&pv_dummy, 0, sizeof(pv_dummy));
645 dm_list_init(&pv_dummy.tags);
646 dm_list_init(&pv_dummy.segments);
651 ret = process_single_pv(cmd, NULL, pv, handle);
658 dev_iter_destroy(iter);
664 * If the lock_type is LCK_VG_READ (used only in reporting commands),
665 * we lock VG_GLOBAL to enable use of metadata cache.
666 * This can pause alongide pvscan or vgscan process for a while.
668 int process_each_pv(struct cmd_context *cmd, int argc, char **argv,
669 struct volume_group *vg, uint32_t flags,
670 int scan_label_only, void *handle,
671 process_single_pv_fn_t process_single_pv)
674 int ret_max = ECMD_PROCESSED;
676 int lock_global = !(flags & READ_WITHOUT_LOCK) && !(flags & READ_FOR_UPDATE);
679 struct physical_volume *pv;
680 struct dm_list *pvslist, *vgnames;
682 struct str_list *sll;
683 char *at_sign, *tagname;
689 if (lock_global && !lock_vol(cmd, VG_GLOBAL, LCK_VG_READ)) {
690 log_error("Unable to obtain global lock.");
695 log_verbose("Using physical volume(s) on command line");
696 for (; opt < argc; opt++) {
697 unescape_colons_and_at_signs(argv[opt], NULL, &at_sign);
698 if (at_sign && (at_sign == argv[opt])) {
699 tagname = at_sign + 1;
701 if (!validate_tag(tagname)) {
702 log_error("Skipping invalid tag %s",
704 if (ret_max < EINVALID_CMD_LINE)
705 ret_max = EINVALID_CMD_LINE;
708 if (!str_list_add(cmd->mem, &tags,
709 dm_pool_strdup(cmd->mem,
711 log_error("strlist allocation failed");
717 if (!(pvl = find_pv_in_vg(vg, argv[opt]))) {
718 log_error("Physical Volume \"%s\" not "
719 "found in Volume Group "
722 ret_max = ECMD_FAILED;
729 if (!(pv = pv_read(cmd, argv[opt], &mdas,
730 NULL, 1, scan_label_only))) {
731 log_error("Failed to read physical "
732 "volume \"%s\"", argv[opt]);
733 ret_max = ECMD_FAILED;
738 * If a PV has no MDAs it may appear to be an
739 * orphan until the metadata is read off
740 * another PV in the same VG. Detecting this
741 * means checking every VG by scanning every
744 if (!scanned && is_orphan(pv) &&
745 !dm_list_size(&mdas)) {
746 if (!scan_label_only &&
747 !scan_vgs_for_pvs(cmd, 1)) {
749 ret_max = ECMD_FAILED;
753 if (!(pv = pv_read(cmd, argv[opt],
756 log_error("Failed to read "
758 "\"%s\"", argv[opt]);
759 ret_max = ECMD_FAILED;
765 ret = process_single_pv(cmd, vg, pv, handle);
771 if (!dm_list_empty(&tags) && (vgnames = get_vgnames(cmd, 1)) &&
772 !dm_list_empty(vgnames)) {
773 dm_list_iterate_items(sll, vgnames) {
774 vg = vg_read(cmd, sll->str, NULL, flags);
775 if (vg_read_error(vg)) {
776 ret_max = ECMD_FAILED;
782 ret = process_each_pv_in_vg(cmd, vg, &tags,
786 unlock_and_free_vg(cmd, vg, sll->str);
796 log_verbose("Using all physical volume(s) in "
798 ret = process_each_pv_in_vg(cmd, vg, NULL, handle,
804 } else if (arg_count(cmd, all_ARG)) {
805 ret = _process_all_devs(cmd, handle, process_single_pv);
811 log_verbose("Scanning for physical volume names");
813 if (!(pvslist = get_pvs(cmd)))
816 dm_list_iterate_items(pvl, pvslist) {
817 ret = process_single_pv(cmd, NULL, pvl->pv,
828 unlock_vg(cmd, VG_GLOBAL);
832 unlock_vg(cmd, VG_GLOBAL);
838 * Determine volume group name from a logical volume name
840 const char *extract_vgname(struct cmd_context *cmd, const char *lv_name)
842 const char *vg_name = lv_name;
844 char *dev_dir = cmd->dev_dir;
847 if (vg_name && strchr(vg_name, '/')) {
848 /* Strip dev_dir (optional) */
849 if (*vg_name == '/') {
850 while (*vg_name == '/')
854 if (!strncmp(vg_name, dev_dir, strlen(dev_dir))) {
855 vg_name += strlen(dev_dir);
856 while (*vg_name == '/')
859 if (*vg_name == '/') {
860 log_error("\"%s\": Invalid path for Logical "
865 /* Require exactly one set of consecutive slashes */
866 if ((st = strchr(vg_name, '/')))
870 if (!st || strchr(st, '/')) {
871 log_error("\"%s\": Invalid path for Logical Volume",
876 vg_name = dm_pool_strdup(cmd->mem, vg_name);
878 log_error("Allocation of vg_name failed");
882 *strchr(vg_name, '/') = '\0';
886 if (!(vg_name = default_vgname(cmd))) {
888 log_error("Path required for Logical Volume \"%s\"",
897 * Extract default volume group name from environment
899 char *default_vgname(struct cmd_context *cmd)
903 /* Take default VG from environment? */
904 vg_path = getenv("LVM_VG_NAME");
908 vg_path = skip_dev_dir(cmd, vg_path, NULL);
910 if (strchr(vg_path, '/')) {
911 log_error("Environment Volume Group in LVM_VG_NAME invalid: "
916 return dm_pool_strdup(cmd->mem, vg_path);
920 * Process physical extent range specifiers
922 static int _add_pe_range(struct dm_pool *mem, const char *pvname,
923 struct dm_list *pe_ranges, uint32_t start, uint32_t count)
925 struct pe_range *per;
927 log_debug("Adding PE range: start PE %" PRIu32 " length %" PRIu32
928 " on %s", start, count, pvname);
930 /* Ensure no overlap with existing areas */
931 dm_list_iterate_items(per, pe_ranges) {
932 if (((start < per->start) && (start + count - 1 >= per->start))
933 || ((start >= per->start) &&
934 (per->start + per->count - 1) >= start)) {
935 log_error("Overlapping PE ranges specified (%" PRIu32
936 "-%" PRIu32 ", %" PRIu32 "-%" PRIu32 ")"
938 start, start + count - 1, per->start,
939 per->start + per->count - 1, pvname);
944 if (!(per = dm_pool_alloc(mem, sizeof(*per)))) {
945 log_error("Allocation of list failed");
951 dm_list_add(pe_ranges, &per->list);
956 static int xstrtouint32(const char *s, char **p, int base, uint32_t *result)
961 ul = strtoul(s, p, base);
962 if (errno || *p == s || (uint32_t) ul != ul)
968 static int _parse_pes(struct dm_pool *mem, char *c, struct dm_list *pe_ranges,
969 const char *pvname, uint32_t size)
974 /* Default to whole PV */
976 if (!_add_pe_range(mem, pvname, pe_ranges, UINT32_C(0), size))
987 /* Disallow :: and :\0 */
988 if (*c == ':' || !*c)
991 /* Default to whole range */
995 /* Start extent given? */
997 if (xstrtouint32(c, &endptr, 10, &start))
1000 /* Just one number given? */
1001 if (!*c || *c == ':')
1008 if (xstrtouint32(c, &endptr, 10, &end))
1013 if (*c && *c != ':')
1016 if ((start > end) || (end > size - 1)) {
1017 log_error("PE range error: start extent %" PRIu32 " to "
1018 "end extent %" PRIu32, start, end);
1022 if (!_add_pe_range(mem, pvname, pe_ranges, start, end - start + 1))
1030 log_error("Physical extent parsing error at %s", c);
1034 static int _create_pv_entry(struct dm_pool *mem, struct pv_list *pvl,
1035 char *colon, int allocatable_only, struct dm_list *r)
1038 struct pv_list *new_pvl = NULL, *pvl2;
1039 struct dm_list *pe_ranges;
1041 pvname = pv_dev_name(pvl->pv);
1042 if (allocatable_only && !(pvl->pv->status & ALLOCATABLE_PV)) {
1043 log_error("Physical volume %s not allocatable", pvname);
1047 if (allocatable_only && is_missing_pv(pvl->pv)) {
1048 log_error("Physical volume %s is missing", pvname);
1052 if (allocatable_only &&
1053 (pvl->pv->pe_count == pvl->pv->pe_alloc_count)) {
1054 log_error("No free extents on physical volume \"%s\"", pvname);
1058 dm_list_iterate_items(pvl2, r)
1059 if (pvl->pv->dev == pvl2->pv->dev) {
1065 if (!(new_pvl = dm_pool_alloc(mem, sizeof(*new_pvl)))) {
1066 log_error("Unable to allocate physical volume list.");
1070 memcpy(new_pvl, pvl, sizeof(*new_pvl));
1072 if (!(pe_ranges = dm_pool_alloc(mem, sizeof(*pe_ranges)))) {
1073 log_error("Allocation of pe_ranges list failed");
1076 dm_list_init(pe_ranges);
1077 new_pvl->pe_ranges = pe_ranges;
1078 dm_list_add(r, &new_pvl->list);
1081 /* Determine selected physical extents */
1082 if (!_parse_pes(mem, colon, new_pvl->pe_ranges, pv_dev_name(pvl->pv),
1089 struct dm_list *create_pv_list(struct dm_pool *mem, struct volume_group *vg, int argc,
1090 char **argv, int allocatable_only)
1093 struct pv_list *pvl;
1094 struct dm_list tags, arg_pvnames;
1095 char *pvname = NULL;
1096 char *colon, *at_sign, *tagname;
1099 /* Build up list of PVs */
1100 if (!(r = dm_pool_alloc(mem, sizeof(*r)))) {
1101 log_error("Allocation of list failed");
1106 dm_list_init(&tags);
1107 dm_list_init(&arg_pvnames);
1109 for (i = 0; i < argc; i++) {
1110 unescape_colons_and_at_signs(argv[i], &colon, &at_sign);
1112 if (at_sign && (at_sign == argv[i])) {
1113 tagname = at_sign + 1;
1114 if (!validate_tag(tagname)) {
1115 log_error("Skipping invalid tag %s", tagname);
1118 dm_list_iterate_items(pvl, &vg->pvs) {
1119 if (str_list_match_item(&pvl->pv->tags,
1121 if (!_create_pv_entry(mem, pvl, NULL,
1132 if (colon && !(pvname = dm_pool_strndup(mem, pvname,
1133 (unsigned) (colon - pvname)))) {
1134 log_error("Failed to clone PV name");
1138 if (!(pvl = find_pv_in_vg(vg, pvname))) {
1139 log_error("Physical Volume \"%s\" not found in "
1140 "Volume Group \"%s\"", pvname, vg->name);
1143 if (!_create_pv_entry(mem, pvl, colon, allocatable_only, r))
1147 if (dm_list_empty(r))
1148 log_error("No specified PVs have space available");
1150 return dm_list_empty(r) ? NULL : r;
1153 struct dm_list *clone_pv_list(struct dm_pool *mem, struct dm_list *pvsl)
1156 struct pv_list *pvl, *new_pvl;
1158 /* Build up list of PVs */
1159 if (!(r = dm_pool_alloc(mem, sizeof(*r)))) {
1160 log_error("Allocation of list failed");
1165 dm_list_iterate_items(pvl, pvsl) {
1166 if (!(new_pvl = dm_pool_zalloc(mem, sizeof(*new_pvl)))) {
1167 log_error("Unable to allocate physical volume list.");
1171 memcpy(new_pvl, pvl, sizeof(*new_pvl));
1172 dm_list_add(r, &new_pvl->list);
1178 void vgcreate_params_set_defaults(struct vgcreate_params *vp_def,
1179 struct volume_group *vg)
1182 vp_def->vg_name = NULL;
1183 vp_def->extent_size = vg->extent_size;
1184 vp_def->max_pv = vg->max_pv;
1185 vp_def->max_lv = vg->max_lv;
1186 vp_def->alloc = vg->alloc;
1187 vp_def->clustered = vg_is_clustered(vg);
1188 vp_def->vgmetadatacopies = vg->mda_copies;
1190 vp_def->vg_name = NULL;
1191 vp_def->extent_size = DEFAULT_EXTENT_SIZE * 2;
1192 vp_def->max_pv = DEFAULT_MAX_PV;
1193 vp_def->max_lv = DEFAULT_MAX_LV;
1194 vp_def->alloc = DEFAULT_ALLOC_POLICY;
1195 vp_def->clustered = DEFAULT_CLUSTERED;
1196 vp_def->vgmetadatacopies = DEFAULT_VGMETADATACOPIES;
1201 * Set members of struct vgcreate_params from cmdline arguments.
1202 * Do preliminary validation with arg_*() interface.
1203 * Further, more generic validation is done in validate_vgcreate_params().
1204 * This function is to remain in tools directory.
1206 int vgcreate_params_set_from_args(struct cmd_context *cmd,
1207 struct vgcreate_params *vp_new,
1208 struct vgcreate_params *vp_def)
1210 vp_new->vg_name = skip_dev_dir(cmd, vp_def->vg_name, NULL);
1211 vp_new->max_lv = arg_uint_value(cmd, maxlogicalvolumes_ARG,
1213 vp_new->max_pv = arg_uint_value(cmd, maxphysicalvolumes_ARG,
1215 vp_new->alloc = arg_uint_value(cmd, alloc_ARG, vp_def->alloc);
1217 /* Units of 512-byte sectors */
1218 vp_new->extent_size =
1219 arg_uint_value(cmd, physicalextentsize_ARG, vp_def->extent_size);
1221 if (arg_count(cmd, clustered_ARG))
1223 !strcmp(arg_str_value(cmd, clustered_ARG,
1224 vp_def->clustered ? "y":"n"), "y");
1226 /* Default depends on current locking type */
1227 vp_new->clustered = locking_is_clustered();
1229 if (arg_sign_value(cmd, physicalextentsize_ARG, 0) == SIGN_MINUS) {
1230 log_error("Physical extent size may not be negative");
1234 if (arg_sign_value(cmd, maxlogicalvolumes_ARG, 0) == SIGN_MINUS) {
1235 log_error("Max Logical Volumes may not be negative");
1239 if (arg_sign_value(cmd, maxphysicalvolumes_ARG, 0) == SIGN_MINUS) {
1240 log_error("Max Physical Volumes may not be negative");
1244 if (arg_count(cmd, metadatacopies_ARG)) {
1245 vp_new->vgmetadatacopies = arg_int_value(cmd, metadatacopies_ARG,
1246 DEFAULT_VGMETADATACOPIES);
1247 } else if (arg_count(cmd, vgmetadatacopies_ARG)) {
1248 vp_new->vgmetadatacopies = arg_int_value(cmd, vgmetadatacopies_ARG,
1249 DEFAULT_VGMETADATACOPIES);
1251 vp_new->vgmetadatacopies = find_config_tree_int(cmd,
1252 "metadata/vgmetadatacopies",
1253 DEFAULT_VGMETADATACOPIES);
1259 int lv_refresh(struct cmd_context *cmd, struct logical_volume *lv)
1263 if (!cmd->partial_activation && (lv->status & PARTIAL_LV)) {
1264 log_error("Refusing refresh of partial LV %s. Use --partial to override.",
1269 r = suspend_lv(cmd, lv);
1273 r = resume_lv(cmd, lv);
1278 * check if snapshot merge should be polled
1279 * - unfortunately: even though the dev_manager will clear
1280 * the lv's merge attributes if a merge is not possible;
1281 * it is clearing a different instance of the lv (as
1282 * retrieved with lv_from_lvid)
1283 * - fortunately: polldaemon will immediately shutdown if the
1284 * origin doesn't have a status with a snapshot percentage
1286 if (background_polling() && lv_is_origin(lv) && lv_is_merging_origin(lv))
1287 lv_spawn_background_polling(cmd, lv);
1293 int vg_refresh_visible(struct cmd_context *cmd, struct volume_group *vg)
1295 struct lv_list *lvl;
1298 dm_list_iterate_items(lvl, &vg->lvs)
1299 if (lv_is_visible(lvl->lv))
1300 if (!lv_refresh(cmd, lvl->lv))
1306 void lv_spawn_background_polling(struct cmd_context *cmd,
1307 struct logical_volume *lv)
1311 if ((lv->status & PVMOVE) &&
1312 (pvname = get_pvmove_pvname_from_lv_mirr(lv))) {
1313 log_verbose("Spawning background pvmove process for %s",
1315 pvmove_poll(cmd, pvname, 1);
1316 } else if ((lv->status & LOCKED) &&
1317 (pvname = get_pvmove_pvname_from_lv(lv))) {
1318 log_verbose("Spawning background pvmove process for %s",
1320 pvmove_poll(cmd, pvname, 1);
1323 if (lv->status & (CONVERTING|MERGING)) {
1324 log_verbose("Spawning background lvconvert process for %s",
1326 lvconvert_poll(cmd, lv, 1);
1331 * Intial sanity checking of non-recovery related command-line arguments.
1334 * pp: structure allocated by caller, fields written / validated here
1336 int pvcreate_params_validate(struct cmd_context *cmd,
1337 int argc, char **argv,
1338 struct pvcreate_params *pp)
1341 log_error("Please enter a physical volume path");
1345 pp->yes = arg_count(cmd, yes_ARG);
1346 pp->force = arg_count(cmd, force_ARG);
1348 if (arg_int_value(cmd, labelsector_ARG, 0) >= LABEL_SCAN_SECTORS) {
1349 log_error("labelsector must be less than %lu",
1350 LABEL_SCAN_SECTORS);
1353 pp->labelsector = arg_int64_value(cmd, labelsector_ARG,
1354 DEFAULT_LABELSECTOR);
1357 if (!(cmd->fmt->features & FMT_MDAS) &&
1358 (arg_count(cmd, pvmetadatacopies_ARG) ||
1359 arg_count(cmd, metadatasize_ARG) ||
1360 arg_count(cmd, dataalignment_ARG) ||
1361 arg_count(cmd, dataalignmentoffset_ARG))) {
1362 log_error("Metadata and data alignment parameters only "
1363 "apply to text format.");
1367 if (arg_count(cmd, pvmetadatacopies_ARG) &&
1368 arg_int_value(cmd, pvmetadatacopies_ARG, -1) > 2) {
1369 log_error("Metadatacopies may only be 0, 1 or 2");
1373 if (arg_count(cmd, metadataignore_ARG)) {
1374 pp->metadataignore = !strcmp(arg_str_value(cmd,
1376 DEFAULT_PVMETADATAIGNORE_STR),
1379 pp->metadataignore = !strcmp(find_config_tree_str(cmd,
1380 "metadata/pvmetadataignore",
1381 DEFAULT_PVMETADATAIGNORE_STR),
1384 if (arg_count(cmd, pvmetadatacopies_ARG) &&
1385 !arg_int_value(cmd, pvmetadatacopies_ARG, -1) &&
1386 pp->metadataignore) {
1387 log_error("metadataignore only applies to metadatacopies > 0");
1391 if (arg_count(cmd, zero_ARG))
1392 pp->zero = strcmp(arg_str_value(cmd, zero_ARG, "y"), "n");
1394 if (arg_sign_value(cmd, dataalignment_ARG, 0) == SIGN_MINUS) {
1395 log_error("Physical volume data alignment may not be negative");
1398 pp->data_alignment = arg_uint64_value(cmd, dataalignment_ARG, UINT64_C(0));
1400 if (pp->data_alignment > ULONG_MAX) {
1401 log_error("Physical volume data alignment is too big.");
1405 if (pp->data_alignment && pp->pe_start) {
1406 if (pp->pe_start % pp->data_alignment)
1407 log_warn("WARNING: Ignoring data alignment %" PRIu64
1408 " incompatible with --restorefile value (%"
1409 PRIu64").", pp->data_alignment, pp->pe_start);
1410 pp->data_alignment = 0;
1413 if (arg_sign_value(cmd, dataalignmentoffset_ARG, 0) == SIGN_MINUS) {
1414 log_error("Physical volume data alignment offset may not be negative");
1417 pp->data_alignment_offset = arg_uint64_value(cmd, dataalignmentoffset_ARG, UINT64_C(0));
1419 if (pp->data_alignment_offset > ULONG_MAX) {
1420 log_error("Physical volume data alignment offset is too big.");
1424 if (pp->data_alignment_offset && pp->pe_start) {
1425 log_warn("WARNING: Ignoring data alignment offset %" PRIu64
1426 " incompatible with --restorefile value (%"
1427 PRIu64").", pp->data_alignment_offset, pp->pe_start);
1428 pp->data_alignment_offset = 0;
1431 if (arg_sign_value(cmd, metadatasize_ARG, 0) == SIGN_MINUS) {
1432 log_error("Metadata size may not be negative");
1436 pp->pvmetadatasize = arg_uint64_value(cmd, metadatasize_ARG, UINT64_C(0));
1437 if (!pp->pvmetadatasize)
1438 pp->pvmetadatasize = find_config_tree_int(cmd,
1439 "metadata/pvmetadatasize",
1440 DEFAULT_PVMETADATASIZE);
1442 pp->pvmetadatacopies = arg_int_value(cmd, pvmetadatacopies_ARG, -1);
1443 if (pp->pvmetadatacopies < 0)
1444 pp->pvmetadatacopies = find_config_tree_int(cmd,
1445 "metadata/pvmetadatacopies",
1446 DEFAULT_PVMETADATACOPIES);
1451 int get_activation_monitoring_mode(struct cmd_context *cmd,
1452 struct volume_group *vg,
1453 int *monitoring_mode)
1455 *monitoring_mode = DEFAULT_DMEVENTD_MONITOR;
1457 if (arg_count(cmd, monitor_ARG) &&
1458 (arg_count(cmd, ignoremonitoring_ARG) ||
1459 arg_count(cmd, sysinit_ARG))) {
1460 log_error("--ignoremonitoring or --sysinit option not allowed with --monitor option");
1464 if (arg_count(cmd, monitor_ARG))
1465 *monitoring_mode = arg_int_value(cmd, monitor_ARG,
1466 DEFAULT_DMEVENTD_MONITOR);
1467 else if (is_static() || arg_count(cmd, ignoremonitoring_ARG) ||
1468 arg_count(cmd, sysinit_ARG) ||
1469 !find_config_tree_bool(cmd, "activation/monitoring",
1470 DEFAULT_DMEVENTD_MONITOR))
1471 *monitoring_mode = DMEVENTD_MONITOR_IGNORE;
1473 if (vg && vg_is_clustered(vg) &&
1474 *monitoring_mode == DMEVENTD_MONITOR_IGNORE) {
1475 log_error("%s is incompatible with clustered Volume Group "
1476 "\"%s\": Skipping.",
1477 (arg_count(cmd, ignoremonitoring_ARG) ?
1478 "--ignoremonitoring" : "activation/monitoring=0"),
1487 * Generic stripe parameter checks.
1489 static int _validate_stripe_params(struct cmd_context *cmd, uint32_t *stripes,
1490 uint32_t *stripe_size)
1492 if (*stripes == 1 && *stripe_size) {
1493 log_print("Ignoring stripesize argument with single stripe");
1497 if (*stripes > 1 && !*stripe_size) {
1498 *stripe_size = find_config_tree_int(cmd, "metadata/stripesize", DEFAULT_STRIPESIZE) * 2;
1499 log_print("Using default stripesize %s",
1500 display_size(cmd, (uint64_t) *stripe_size));
1503 if (*stripes < 1 || *stripes > MAX_STRIPES) {
1504 log_error("Number of stripes (%d) must be between %d and %d",
1505 *stripes, 1, MAX_STRIPES);
1509 if (*stripes > 1 && (*stripe_size < STRIPE_SIZE_MIN ||
1510 *stripe_size & (*stripe_size - 1))) {
1511 log_error("Invalid stripe size %s",
1512 display_size(cmd, (uint64_t) *stripe_size));
1520 * The stripe size is limited by the size of a uint32_t, but since the
1521 * value given by the user is doubled, and the final result must be a
1522 * power of 2, we must divide UINT_MAX by four and add 1 (to round it
1523 * up to the power of 2)
1525 int get_stripe_params(struct cmd_context *cmd, uint32_t *stripes, uint32_t *stripe_size)
1527 /* stripes_long_ARG takes precedence (for lvconvert) */
1528 *stripes = arg_uint_value(cmd, arg_count(cmd, stripes_long_ARG) ? stripes_long_ARG : stripes_ARG, 1);
1530 *stripe_size = arg_uint_value(cmd, stripesize_ARG, 0);
1532 if (arg_sign_value(cmd, stripesize_ARG, 0) == SIGN_MINUS) {
1533 log_error("Negative stripesize is invalid");
1537 if(*stripe_size > STRIPE_SIZE_LIMIT * 2) {
1538 log_error("Stripe size cannot be larger than %s",
1539 display_size(cmd, (uint64_t) STRIPE_SIZE_LIMIT));
1544 return _validate_stripe_params(cmd, stripes, stripe_size);