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 const 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",
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");
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
)
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
)) {
123 * FIXME: In case of remove it goes through deleted entries,
124 * but it works since entries are allocated from vg mem pool.
126 dm_list_iterate_items(lvl
, &vg
->lvs
) {
127 if (lvl
->lv
->status
& SNAPSHOT
)
130 /* Skip availability change for non-virt snaps when processing all LVs */
131 /* FIXME: pass process_all to process_single_lv() */
132 if (process_all
&& arg_count(cmd
, available_ARG
) &&
133 lv_is_cow(lvl
->lv
) && !lv_is_virtual_origin(origin_from_cow(lvl
->lv
)))
136 if (lv_is_virtual_origin(lvl
->lv
) && !arg_count(cmd
, all_ARG
))
140 * Only let hidden LVs through it --all was used or the LVs
141 * were specifically named on the command line.
143 if (!lvargs_supplied
&& !lv_is_visible(lvl
->lv
) && !arg_count(cmd
, all_ARG
))
146 /* Should we process this LV? */
153 if (!process_lv
&& tags_supplied
&&
154 str_list_match_list(tags
, &lvl
->lv
->tags
, NULL
)) {
159 if (lvargs_supplied
&&
160 str_list_match_item(arg_lvnames
, lvl
->lv
->name
)) {
168 lvl
->lv
->vg
->cmd_missing_vgs
= 0;
169 ret
= process_single_lv(cmd
, lvl
->lv
, handle
);
170 if (ret
!= ECMD_PROCESSED
&& failed_lvnames
) {
171 lv_name
= dm_pool_strdup(cmd
->mem
, lvl
->lv
->name
);
173 !str_list_add(cmd
->mem
, failed_lvnames
, lv_name
)) {
174 log_error("Allocation failed for str_list.");
177 if (lvl
->lv
->vg
->cmd_missing_vgs
)
178 ret
= ECMD_PROCESSED
;
182 if (sigint_caught()) {
188 if (lvargs_supplied
&& lvargs_matched
!= dm_list_size(arg_lvnames
)) {
190 * FIXME: lvm supports removal of LV with all its dependencies
191 * this leads to miscalculation that depends on the order of args.
193 log_error("One or more specified logical volume(s) not found.");
194 if (ret_max
< ECMD_FAILED
)
195 ret_max
= ECMD_FAILED
;
201 int process_each_lv(struct cmd_context
*cmd
, int argc
, char **argv
,
202 uint32_t flags
, void *handle
,
203 process_single_lv_fn_t process_single_lv
)
206 int ret_max
= ECMD_PROCESSED
;
209 struct dm_list
*tags_arg
;
210 struct dm_list
*vgnames
; /* VGs to process */
211 struct str_list
*sll
, *strl
;
212 struct cmd_vg
*cvl_vg
;
213 struct dm_list cmd_vgs
;
214 struct dm_list failed_lvnames
;
215 struct dm_list tags
, lvnames
;
216 struct dm_list arg_lvnames
; /* Cmdline vgname or vgname/lvname */
217 struct dm_list arg_vgnames
;
224 dm_list_init(&arg_lvnames
);
225 dm_list_init(&failed_lvnames
);
228 log_verbose("Using logical volume(s) on command line");
229 dm_list_init(&arg_vgnames
);
231 for (; opt
< argc
; opt
++) {
232 const char *lv_name
= argv
[opt
];
233 const char *tmp_lv_name
;
235 unsigned dev_dir_found
= 0;
237 /* Do we have a tag or vgname or lvname? */
240 if (*vgname
== '@') {
241 if (!validate_tag(vgname
+ 1)) {
242 log_error("Skipping invalid tag %s",
246 if (!str_list_add(cmd
->mem
, &tags
,
247 dm_pool_strdup(cmd
->mem
,
249 log_error("strlist allocation failed");
255 /* FIXME Jumbled parsing */
256 vgname
= skip_dev_dir(cmd
, vgname
, &dev_dir_found
);
258 if (*vgname
== '/') {
259 log_error("\"%s\": Invalid path for Logical "
260 "Volume", argv
[opt
]);
261 if (ret_max
< ECMD_FAILED
)
262 ret_max
= ECMD_FAILED
;
266 if ((tmp_lv_name
= strchr(vgname
, '/'))) {
268 lv_name
= tmp_lv_name
;
269 while (*lv_name
== '/')
271 if (!(vgname
= extract_vgname(cmd
, vgname
))) {
272 if (ret_max
< ECMD_FAILED
) {
274 ret_max
= ECMD_FAILED
;
278 } else if (!dev_dir_found
&&
279 (vgname_def
= default_vgname(cmd
))) {
284 if (!str_list_add(cmd
->mem
, &arg_vgnames
,
285 dm_pool_strdup(cmd
->mem
, vgname
))) {
286 log_error("strlist allocation failed");
291 if (!str_list_add(cmd
->mem
, &arg_lvnames
,
292 dm_pool_strdup(cmd
->mem
,
294 log_error("strlist allocation failed");
298 vglv_sz
= strlen(vgname
) + strlen(lv_name
) + 2;
299 if (!(vglv
= dm_pool_alloc(cmd
->mem
, vglv_sz
)) ||
300 dm_snprintf(vglv
, vglv_sz
, "%s/%s", vgname
,
302 log_error("vg/lv string alloc failed");
305 if (!str_list_add(cmd
->mem
, &arg_lvnames
, vglv
)) {
306 log_error("strlist allocation failed");
311 vgnames
= &arg_vgnames
;
314 if (!argc
|| !dm_list_empty(&tags
)) {
315 log_verbose("Finding all logical volumes");
316 if (!lvmetad_vg_list_to_lvmcache(cmd
))
318 if (!(vgnames
= get_vgnames(cmd
, 0)) || dm_list_empty(vgnames
)) {
319 log_error("No volume groups found");
324 dm_list_iterate_items(strl
, vgnames
) {
326 dm_list_init(&cmd_vgs
);
327 if (!(cvl_vg
= cmd_vg_add(cmd
->mem
, &cmd_vgs
,
328 vgname
, NULL
, flags
))) {
333 if (!cmd_vg_read(cmd
, &cmd_vgs
)) {
334 free_cmd_vgs(&cmd_vgs
);
335 if (ret_max
< ECMD_FAILED
) {
336 log_error("Skipping volume group %s", vgname
);
337 ret_max
= ECMD_FAILED
;
344 dm_list_init(&lvnames
); /* LVs to be processed in this VG */
345 dm_list_iterate_items(sll
, &arg_lvnames
) {
346 const char *vg_name
= sll
->str
;
347 const char *lv_name
= strchr(vg_name
, '/');
349 if ((!lv_name
&& !strcmp(vg_name
, vgname
))) {
350 /* Process all LVs in this VG */
352 dm_list_init(&lvnames
);
354 } else if (!strncmp(vg_name
, vgname
, strlen(vgname
)) && lv_name
&&
355 strlen(vgname
) == (size_t) (lv_name
- vg_name
)) {
356 if (!str_list_add(cmd
->mem
, &lvnames
,
357 dm_pool_strdup(cmd
->mem
,
359 log_error("strlist allocation failed");
360 free_cmd_vgs(&cmd_vgs
);
366 while (!sigint_caught()) {
367 ret
= process_each_lv_in_vg(cmd
, cvl_vg
->vg
, &lvnames
,
368 tags_arg
, &failed_lvnames
,
369 handle
, process_single_lv
);
370 if (ret
!= ECMD_PROCESSED
) {
375 if (dm_list_empty(&failed_lvnames
))
378 /* Try again with failed LVs in this VG */
379 dm_list_init(&lvnames
);
380 dm_list_splice(&lvnames
, &failed_lvnames
);
382 free_cmd_vgs(&cmd_vgs
);
383 if (!cmd_vg_read(cmd
, &cmd_vgs
)) {
385 ret
= ECMD_FAILED
; /* break */
392 free_cmd_vgs(&cmd_vgs
);
393 /* FIXME: logic for breaking command is not consistent */
394 if (sigint_caught()) {
403 int process_each_segment_in_pv(struct cmd_context
*cmd
,
404 struct volume_group
*vg
,
405 struct physical_volume
*pv
,
407 process_single_pvseg_fn_t process_single_pvseg
)
409 struct pv_segment
*pvseg
;
411 const char *vg_name
= NULL
;
412 int ret_max
= ECMD_PROCESSED
;
414 struct volume_group
*old_vg
= vg
;
415 struct pv_segment _free_pv_segment
= { .pv
= pv
};
417 if (is_pv(pv
) && !vg
&& !is_orphan(pv
)) {
418 vg_name
= pv_vg_name(pv
);
420 vg
= vg_read(cmd
, vg_name
, NULL
, 0);
421 if (vg_read_error(vg
)) {
423 log_error("Skipping volume group %s", vg_name
);
428 * Replace possibly incomplete PV structure with new one
429 * allocated in vg_read_internal() path.
431 if (!(pvl
= find_pv_in_vg(vg
, pv_dev_name(pv
)))) {
432 log_error("Unable to find %s in volume group %s",
433 pv_dev_name(pv
), vg_name
);
434 unlock_and_release_vg(cmd
, vg
, vg_name
);
441 if (dm_list_empty(&pv
->segments
)) {
442 ret
= process_single_pvseg(cmd
, NULL
, &_free_pv_segment
, handle
);
446 dm_list_iterate_items(pvseg
, &pv
->segments
) {
447 ret
= process_single_pvseg(cmd
, vg
, pvseg
, handle
);
455 unlock_vg(cmd
, vg_name
);
462 int process_each_segment_in_lv(struct cmd_context
*cmd
,
463 struct logical_volume
*lv
,
465 process_single_seg_fn_t process_single_seg
)
467 struct lv_segment
*seg
;
468 int ret_max
= ECMD_PROCESSED
;
471 dm_list_iterate_items(seg
, &lv
->segments
) {
472 ret
= process_single_seg(cmd
, seg
, handle
);
475 /* FIXME: logic for breaking command is not consistent */
483 static int _process_one_vg(struct cmd_context
*cmd
, const char *vg_name
,
485 struct dm_list
*tags
, struct dm_list
*arg_vgnames
,
486 uint32_t flags
, void *handle
, int ret_max
,
487 process_single_vg_fn_t process_single_vg
)
489 struct dm_list cmd_vgs
;
490 struct cmd_vg
*cvl_vg
;
493 log_verbose("Finding volume group \"%s\"", vg_name
);
495 dm_list_init(&cmd_vgs
);
496 if (!(cvl_vg
= cmd_vg_add(cmd
->mem
, &cmd_vgs
, vg_name
, vgid
, flags
)))
500 /* FIXME: consistent handling of command break */
501 if (sigint_caught()) {
505 if (!cmd_vg_read(cmd
, &cmd_vgs
))
506 /* Allow FAILED_INCONSISTENT through only for vgcfgrestore */
507 if (vg_read_error(cvl_vg
->vg
) &&
508 (!((flags
& READ_ALLOW_INCONSISTENT
) &&
509 (vg_read_error(cvl_vg
->vg
) == FAILED_INCONSISTENT
)))) {
514 if (!dm_list_empty(tags
) &&
515 /* Only process if a tag matches or it's on arg_vgnames */
516 !str_list_match_item(arg_vgnames
, vg_name
) &&
517 !str_list_match_list(tags
, &cvl_vg
->vg
->tags
, NULL
))
520 ret
= process_single_vg(cmd
, vg_name
, cvl_vg
->vg
, handle
);
522 if (vg_read_error(cvl_vg
->vg
)) /* FAILED_INCONSISTENT */
525 if (!cvl_vg
->vg
->cmd_missing_vgs
)
528 free_cmd_vgs(&cmd_vgs
);
531 free_cmd_vgs(&cmd_vgs
);
533 return (ret
> ret_max
) ? ret
: ret_max
;
536 int process_each_vg(struct cmd_context
*cmd
, int argc
, char **argv
,
537 uint32_t flags
, void *handle
,
538 process_single_vg_fn_t process_single_vg
)
541 int ret_max
= ECMD_PROCESSED
;
544 struct dm_list
*vgnames
, *vgids
;
545 struct dm_list arg_vgnames
, tags
;
547 const char *vg_name
, *vgid
;
550 dm_list_init(&arg_vgnames
);
553 log_verbose("Using volume group(s) on command line");
555 for (; opt
< argc
; opt
++) {
557 if (*vg_name
== '@') {
558 if (!validate_tag(vg_name
+ 1)) {
559 log_error("Skipping invalid tag %s",
561 if (ret_max
< EINVALID_CMD_LINE
)
562 ret_max
= EINVALID_CMD_LINE
;
565 if (!str_list_add(cmd
->mem
, &tags
,
566 dm_pool_strdup(cmd
->mem
,
568 log_error("strlist allocation failed");
574 vg_name
= skip_dev_dir(cmd
, vg_name
, NULL
);
575 if (strchr(vg_name
, '/')) {
576 log_error("Invalid volume group name: %s",
578 if (ret_max
< EINVALID_CMD_LINE
)
579 ret_max
= EINVALID_CMD_LINE
;
582 if (!str_list_add(cmd
->mem
, &arg_vgnames
,
583 dm_pool_strdup(cmd
->mem
, vg_name
))) {
584 log_error("strlist allocation failed");
589 vgnames
= &arg_vgnames
;
592 if (!argc
|| !dm_list_empty(&tags
)) {
593 log_verbose("Finding all volume groups");
594 if (!lvmetad_vg_list_to_lvmcache(cmd
))
596 if (!(vgids
= get_vgids(cmd
, 0)) || dm_list_empty(vgids
)) {
597 log_error("No volume groups found");
600 dm_list_iterate_items(sl
, vgids
) {
602 if (!(vgid
) || !(vg_name
= lvmcache_vgname_from_vgid(cmd
->mem
, vgid
)))
604 ret_max
= _process_one_vg(cmd
, vg_name
, vgid
, &tags
,
607 ret_max
, process_single_vg
);
612 dm_list_iterate_items(sl
, vgnames
) {
614 if (is_orphan_vg(vg_name
))
615 continue; /* FIXME Unnecessary? */
616 ret_max
= _process_one_vg(cmd
, vg_name
, NULL
, &tags
,
619 ret_max
, process_single_vg
);
628 int process_each_pv_in_vg(struct cmd_context
*cmd
, struct volume_group
*vg
,
629 const struct dm_list
*tags
, void *handle
,
630 process_single_pv_fn_t process_single_pv
)
632 int ret_max
= ECMD_PROCESSED
;
636 dm_list_iterate_items(pvl
, &vg
->pvs
) {
637 if (tags
&& !dm_list_empty(tags
) &&
638 !str_list_match_list(tags
, &pvl
->pv
->tags
, NULL
)) {
641 if ((ret
= process_single_pv(cmd
, vg
, pvl
->pv
, handle
)) > ret_max
)
650 static int _process_all_devs(struct cmd_context
*cmd
, void *handle
,
651 process_single_pv_fn_t process_single_pv
)
653 struct physical_volume
*pv
;
654 struct physical_volume pv_dummy
;
655 struct dev_iter
*iter
;
658 int ret_max
= ECMD_PROCESSED
;
661 if (!scan_vgs_for_pvs(cmd
, 1)) {
666 if (!(iter
= dev_iter_create(cmd
->filter
, 1))) {
667 log_error("dev_iter creation failed");
671 while ((dev
= dev_iter_get(iter
))) {
672 if (!(pv
= pv_read(cmd
, dev_name(dev
), 0, 0))) {
673 memset(&pv_dummy
, 0, sizeof(pv_dummy
));
674 dm_list_init(&pv_dummy
.tags
);
675 dm_list_init(&pv_dummy
.segments
);
680 ret
= process_single_pv(cmd
, NULL
, pv
, handle
);
690 dev_iter_destroy(iter
);
696 * If the lock_type is LCK_VG_READ (used only in reporting commands),
697 * we lock VG_GLOBAL to enable use of metadata cache.
698 * This can pause alongide pvscan or vgscan process for a while.
700 int process_each_pv(struct cmd_context
*cmd
, int argc
, char **argv
,
701 struct volume_group
*vg
, uint32_t flags
,
702 int scan_label_only
, void *handle
,
703 process_single_pv_fn_t process_single_pv
)
706 int ret_max
= ECMD_PROCESSED
;
708 int lock_global
= !(flags
& READ_WITHOUT_LOCK
) && !(flags
& READ_FOR_UPDATE
);
711 struct physical_volume
*pv
;
712 struct dm_list
*pvslist
, *vgnames
;
714 struct str_list
*sll
;
715 char *at_sign
, *tagname
;
720 if (lock_global
&& !lock_vol(cmd
, VG_GLOBAL
, LCK_VG_READ
)) {
721 log_error("Unable to obtain global lock.");
726 log_verbose("Using physical volume(s) on command line");
727 for (; opt
< argc
; opt
++) {
728 dm_unescape_colons_and_at_signs(argv
[opt
], NULL
, &at_sign
);
729 if (at_sign
&& (at_sign
== argv
[opt
])) {
730 tagname
= at_sign
+ 1;
732 if (!validate_tag(tagname
)) {
733 log_error("Skipping invalid tag %s",
735 if (ret_max
< EINVALID_CMD_LINE
)
736 ret_max
= EINVALID_CMD_LINE
;
739 if (!str_list_add(cmd
->mem
, &tags
,
740 dm_pool_strdup(cmd
->mem
,
742 log_error("strlist allocation failed");
748 if (!(pvl
= find_pv_in_vg(vg
, argv
[opt
]))) {
749 log_error("Physical Volume \"%s\" not "
750 "found in Volume Group "
753 ret_max
= ECMD_FAILED
;
758 if (!(pv
= pv_read(cmd
, argv
[opt
],
759 1, scan_label_only
))) {
760 log_error("Failed to read physical "
761 "volume \"%s\"", argv
[opt
]);
762 ret_max
= ECMD_FAILED
;
767 * If a PV has no MDAs it may appear to be an
768 * orphan until the metadata is read off
769 * another PV in the same VG. Detecting this
770 * means checking every VG by scanning every
773 if (!scanned
&& is_orphan(pv
) &&
774 !dm_list_size(&pv
->fid
->metadata_areas_in_use
) &&
775 !dm_list_size(&pv
->fid
->metadata_areas_ignored
)) {
776 if (!scan_label_only
&&
777 !scan_vgs_for_pvs(cmd
, 1)) {
779 ret_max
= ECMD_FAILED
;
784 if (!(pv
= pv_read(cmd
, argv
[opt
],
787 log_error("Failed to read "
789 "\"%s\"", argv
[opt
]);
790 ret_max
= ECMD_FAILED
;
796 ret
= process_single_pv(cmd
, vg
, pv
, handle
);
799 * Free PV only if we called pv_read before,
800 * otherwise the PV structure is part of the VG.
810 if (!dm_list_empty(&tags
) && (vgnames
= get_vgnames(cmd
, 1)) &&
811 !dm_list_empty(vgnames
)) {
812 dm_list_iterate_items(sll
, vgnames
) {
813 vg
= vg_read(cmd
, sll
->str
, NULL
, flags
);
814 if (vg_read_error(vg
)) {
815 ret_max
= ECMD_FAILED
;
821 ret
= process_each_pv_in_vg(cmd
, vg
, &tags
,
825 unlock_and_release_vg(cmd
, vg
, sll
->str
);
835 log_verbose("Using all physical volume(s) in "
837 ret
= process_each_pv_in_vg(cmd
, vg
, NULL
, handle
,
843 } else if (arg_count(cmd
, all_ARG
)) {
844 ret
= _process_all_devs(cmd
, handle
, process_single_pv
);
850 log_verbose("Scanning for physical volume names");
852 lvmcache_seed_infos_from_lvmetad(cmd
);
853 if (!(pvslist
= get_pvs(cmd
)))
856 dm_list_iterate_items(pvl
, pvslist
) {
857 ret
= process_single_pv(cmd
, NULL
, pvl
->pv
,
859 free_pv_fid(pvl
->pv
);
869 unlock_vg(cmd
, VG_GLOBAL
);
873 unlock_vg(cmd
, VG_GLOBAL
);
879 * Determine volume group name from a logical volume name
881 const char *extract_vgname(struct cmd_context
*cmd
, const char *lv_name
)
883 const char *vg_name
= lv_name
;
885 char *dev_dir
= cmd
->dev_dir
;
888 if (vg_name
&& strchr(vg_name
, '/')) {
889 /* Strip dev_dir (optional) */
890 if (*vg_name
== '/') {
891 while (*vg_name
== '/')
895 if (!strncmp(vg_name
, dev_dir
, strlen(dev_dir
))) {
896 vg_name
+= strlen(dev_dir
);
897 while (*vg_name
== '/')
900 if (*vg_name
== '/') {
901 log_error("\"%s\": Invalid path for Logical "
906 /* Require exactly one set of consecutive slashes */
907 if ((st
= strchr(vg_name
, '/')))
911 if (!st
|| strchr(st
, '/')) {
912 log_error("\"%s\": Invalid path for Logical Volume",
917 vg_name
= dm_pool_strdup(cmd
->mem
, vg_name
);
919 log_error("Allocation of vg_name failed");
923 *strchr(vg_name
, '/') = '\0';
927 if (!(vg_name
= default_vgname(cmd
))) {
929 log_error("Path required for Logical Volume \"%s\"",
938 * Extract default volume group name from environment
940 char *default_vgname(struct cmd_context
*cmd
)
944 /* Take default VG from environment? */
945 vg_path
= getenv("LVM_VG_NAME");
949 vg_path
= skip_dev_dir(cmd
, vg_path
, NULL
);
951 if (strchr(vg_path
, '/')) {
952 log_error("Environment Volume Group in LVM_VG_NAME invalid: "
957 return dm_pool_strdup(cmd
->mem
, vg_path
);
961 * Process physical extent range specifiers
963 static int _add_pe_range(struct dm_pool
*mem
, const char *pvname
,
964 struct dm_list
*pe_ranges
, uint32_t start
, uint32_t count
)
966 struct pe_range
*per
;
968 log_debug("Adding PE range: start PE %" PRIu32
" length %" PRIu32
969 " on %s", start
, count
, pvname
);
971 /* Ensure no overlap with existing areas */
972 dm_list_iterate_items(per
, pe_ranges
) {
973 if (((start
< per
->start
) && (start
+ count
- 1 >= per
->start
))
974 || ((start
>= per
->start
) &&
975 (per
->start
+ per
->count
- 1) >= start
)) {
976 log_error("Overlapping PE ranges specified (%" PRIu32
977 "-%" PRIu32
", %" PRIu32
"-%" PRIu32
")"
979 start
, start
+ count
- 1, per
->start
,
980 per
->start
+ per
->count
- 1, pvname
);
985 if (!(per
= dm_pool_alloc(mem
, sizeof(*per
)))) {
986 log_error("Allocation of list failed");
992 dm_list_add(pe_ranges
, &per
->list
);
997 static int xstrtouint32(const char *s
, char **p
, int base
, uint32_t *result
)
1002 ul
= strtoul(s
, p
, base
);
1003 if (errno
|| *p
== s
|| (uint32_t) ul
!= ul
)
1009 static int _parse_pes(struct dm_pool
*mem
, char *c
, struct dm_list
*pe_ranges
,
1010 const char *pvname
, uint32_t size
)
1013 uint32_t start
, end
;
1015 /* Default to whole PV */
1017 if (!_add_pe_range(mem
, pvname
, pe_ranges
, UINT32_C(0), size
))
1028 /* Disallow :: and :\0 */
1029 if (*c
== ':' || !*c
)
1032 /* Default to whole range */
1033 start
= UINT32_C(0);
1036 /* Start extent given? */
1038 if (xstrtouint32(c
, &endptr
, 10, &start
))
1041 /* Just one number given? */
1042 if (!*c
|| *c
== ':')
1049 if (xstrtouint32(c
, &endptr
, 10, &end
))
1054 if (*c
&& *c
!= ':')
1057 if ((start
> end
) || (end
> size
- 1)) {
1058 log_error("PE range error: start extent %" PRIu32
" to "
1059 "end extent %" PRIu32
, start
, end
);
1063 if (!_add_pe_range(mem
, pvname
, pe_ranges
, start
, end
- start
+ 1))
1071 log_error("Physical extent parsing error at %s", c
);
1075 static int _create_pv_entry(struct dm_pool
*mem
, struct pv_list
*pvl
,
1076 char *colon
, int allocatable_only
, struct dm_list
*r
)
1079 struct pv_list
*new_pvl
= NULL
, *pvl2
;
1080 struct dm_list
*pe_ranges
;
1082 pvname
= pv_dev_name(pvl
->pv
);
1083 if (allocatable_only
&& !(pvl
->pv
->status
& ALLOCATABLE_PV
)) {
1084 log_error("Physical volume %s not allocatable", pvname
);
1088 if (allocatable_only
&& is_missing_pv(pvl
->pv
)) {
1089 log_error("Physical volume %s is missing", pvname
);
1093 if (allocatable_only
&&
1094 (pvl
->pv
->pe_count
== pvl
->pv
->pe_alloc_count
)) {
1095 log_error("No free extents on physical volume \"%s\"", pvname
);
1099 dm_list_iterate_items(pvl2
, r
)
1100 if (pvl
->pv
->dev
== pvl2
->pv
->dev
) {
1106 if (!(new_pvl
= dm_pool_alloc(mem
, sizeof(*new_pvl
)))) {
1107 log_error("Unable to allocate physical volume list.");
1111 memcpy(new_pvl
, pvl
, sizeof(*new_pvl
));
1113 if (!(pe_ranges
= dm_pool_alloc(mem
, sizeof(*pe_ranges
)))) {
1114 log_error("Allocation of pe_ranges list failed");
1117 dm_list_init(pe_ranges
);
1118 new_pvl
->pe_ranges
= pe_ranges
;
1119 dm_list_add(r
, &new_pvl
->list
);
1122 /* Determine selected physical extents */
1123 if (!_parse_pes(mem
, colon
, new_pvl
->pe_ranges
, pv_dev_name(pvl
->pv
),
1130 struct dm_list
*create_pv_list(struct dm_pool
*mem
, struct volume_group
*vg
, int argc
,
1131 char **argv
, int allocatable_only
)
1134 struct pv_list
*pvl
;
1135 struct dm_list tags
, arg_pvnames
;
1136 char *pvname
= NULL
;
1137 char *colon
, *at_sign
, *tagname
;
1140 /* Build up list of PVs */
1141 if (!(r
= dm_pool_alloc(mem
, sizeof(*r
)))) {
1142 log_error("Allocation of list failed");
1147 dm_list_init(&tags
);
1148 dm_list_init(&arg_pvnames
);
1150 for (i
= 0; i
< argc
; i
++) {
1151 dm_unescape_colons_and_at_signs(argv
[i
], &colon
, &at_sign
);
1153 if (at_sign
&& (at_sign
== argv
[i
])) {
1154 tagname
= at_sign
+ 1;
1155 if (!validate_tag(tagname
)) {
1156 log_error("Skipping invalid tag %s", tagname
);
1159 dm_list_iterate_items(pvl
, &vg
->pvs
) {
1160 if (str_list_match_item(&pvl
->pv
->tags
,
1162 if (!_create_pv_entry(mem
, pvl
, NULL
,
1173 if (colon
&& !(pvname
= dm_pool_strndup(mem
, pvname
,
1174 (unsigned) (colon
- pvname
)))) {
1175 log_error("Failed to clone PV name");
1179 if (!(pvl
= find_pv_in_vg(vg
, pvname
))) {
1180 log_error("Physical Volume \"%s\" not found in "
1181 "Volume Group \"%s\"", pvname
, vg
->name
);
1184 if (!_create_pv_entry(mem
, pvl
, colon
, allocatable_only
, r
))
1188 if (dm_list_empty(r
))
1189 log_error("No specified PVs have space available");
1191 return dm_list_empty(r
) ? NULL
: r
;
1194 struct dm_list
*clone_pv_list(struct dm_pool
*mem
, struct dm_list
*pvsl
)
1197 struct pv_list
*pvl
, *new_pvl
;
1199 /* Build up list of PVs */
1200 if (!(r
= dm_pool_alloc(mem
, sizeof(*r
)))) {
1201 log_error("Allocation of list failed");
1206 dm_list_iterate_items(pvl
, pvsl
) {
1207 if (!(new_pvl
= dm_pool_zalloc(mem
, sizeof(*new_pvl
)))) {
1208 log_error("Unable to allocate physical volume list.");
1212 memcpy(new_pvl
, pvl
, sizeof(*new_pvl
));
1213 dm_list_add(r
, &new_pvl
->list
);
1219 void vgcreate_params_set_defaults(struct vgcreate_params
*vp_def
,
1220 struct volume_group
*vg
)
1223 vp_def
->vg_name
= NULL
;
1224 vp_def
->extent_size
= vg
->extent_size
;
1225 vp_def
->max_pv
= vg
->max_pv
;
1226 vp_def
->max_lv
= vg
->max_lv
;
1227 vp_def
->alloc
= vg
->alloc
;
1228 vp_def
->clustered
= vg_is_clustered(vg
);
1229 vp_def
->vgmetadatacopies
= vg
->mda_copies
;
1231 vp_def
->vg_name
= NULL
;
1232 vp_def
->extent_size
= DEFAULT_EXTENT_SIZE
* 2;
1233 vp_def
->max_pv
= DEFAULT_MAX_PV
;
1234 vp_def
->max_lv
= DEFAULT_MAX_LV
;
1235 vp_def
->alloc
= DEFAULT_ALLOC_POLICY
;
1236 vp_def
->clustered
= DEFAULT_CLUSTERED
;
1237 vp_def
->vgmetadatacopies
= DEFAULT_VGMETADATACOPIES
;
1242 * Set members of struct vgcreate_params from cmdline arguments.
1243 * Do preliminary validation with arg_*() interface.
1244 * Further, more generic validation is done in validate_vgcreate_params().
1245 * This function is to remain in tools directory.
1247 int vgcreate_params_set_from_args(struct cmd_context
*cmd
,
1248 struct vgcreate_params
*vp_new
,
1249 struct vgcreate_params
*vp_def
)
1251 vp_new
->vg_name
= skip_dev_dir(cmd
, vp_def
->vg_name
, NULL
);
1252 vp_new
->max_lv
= arg_uint_value(cmd
, maxlogicalvolumes_ARG
,
1254 vp_new
->max_pv
= arg_uint_value(cmd
, maxphysicalvolumes_ARG
,
1256 vp_new
->alloc
= (alloc_policy_t
) arg_uint_value(cmd
, alloc_ARG
, vp_def
->alloc
);
1258 /* Units of 512-byte sectors */
1259 vp_new
->extent_size
=
1260 arg_uint_value(cmd
, physicalextentsize_ARG
, vp_def
->extent_size
);
1262 if (arg_count(cmd
, clustered_ARG
))
1264 !strcmp(arg_str_value(cmd
, clustered_ARG
,
1265 vp_def
->clustered
? "y":"n"), "y");
1267 /* Default depends on current locking type */
1268 vp_new
->clustered
= locking_is_clustered();
1270 if (arg_sign_value(cmd
, physicalextentsize_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1271 log_error("Physical extent size may not be negative");
1275 if (arg_uint64_value(cmd
, physicalextentsize_ARG
, 0) > MAX_EXTENT_SIZE
) {
1276 log_error("Physical extent size cannot be larger than %s",
1277 display_size(cmd
, (uint64_t) MAX_EXTENT_SIZE
));
1281 if (arg_sign_value(cmd
, maxlogicalvolumes_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1282 log_error("Max Logical Volumes may not be negative");
1286 if (arg_sign_value(cmd
, maxphysicalvolumes_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1287 log_error("Max Physical Volumes may not be negative");
1291 if (arg_count(cmd
, metadatacopies_ARG
)) {
1292 vp_new
->vgmetadatacopies
= arg_int_value(cmd
, metadatacopies_ARG
,
1293 DEFAULT_VGMETADATACOPIES
);
1294 } else if (arg_count(cmd
, vgmetadatacopies_ARG
)) {
1295 vp_new
->vgmetadatacopies
= arg_int_value(cmd
, vgmetadatacopies_ARG
,
1296 DEFAULT_VGMETADATACOPIES
);
1298 vp_new
->vgmetadatacopies
= find_config_tree_int(cmd
,
1299 "metadata/vgmetadatacopies",
1300 DEFAULT_VGMETADATACOPIES
);
1306 int lv_refresh(struct cmd_context
*cmd
, struct logical_volume
*lv
)
1310 if (!cmd
->partial_activation
&& (lv
->status
& PARTIAL_LV
)) {
1311 log_error("Refusing refresh of partial LV %s. Use --partial to override.",
1316 r
= suspend_lv(cmd
, lv
);
1320 r
= resume_lv(cmd
, lv
);
1325 * check if snapshot merge should be polled
1326 * - unfortunately: even though the dev_manager will clear
1327 * the lv's merge attributes if a merge is not possible;
1328 * it is clearing a different instance of the lv (as
1329 * retrieved with lv_from_lvid)
1330 * - fortunately: polldaemon will immediately shutdown if the
1331 * origin doesn't have a status with a snapshot percentage
1333 if (background_polling() && lv_is_origin(lv
) && lv_is_merging_origin(lv
))
1334 lv_spawn_background_polling(cmd
, lv
);
1340 int vg_refresh_visible(struct cmd_context
*cmd
, struct volume_group
*vg
)
1342 struct lv_list
*lvl
;
1346 dm_list_iterate_items(lvl
, &vg
->lvs
) {
1347 if (sigint_caught())
1350 if (lv_is_visible(lvl
->lv
))
1351 if (!lv_refresh(cmd
, lvl
->lv
))
1360 void lv_spawn_background_polling(struct cmd_context
*cmd
,
1361 struct logical_volume
*lv
)
1365 if ((lv
->status
& PVMOVE
) &&
1366 (pvname
= get_pvmove_pvname_from_lv_mirr(lv
))) {
1367 log_verbose("Spawning background pvmove process for %s",
1369 pvmove_poll(cmd
, pvname
, 1);
1370 } else if ((lv
->status
& LOCKED
) &&
1371 (pvname
= get_pvmove_pvname_from_lv(lv
))) {
1372 log_verbose("Spawning background pvmove process for %s",
1374 pvmove_poll(cmd
, pvname
, 1);
1377 if (lv
->status
& (CONVERTING
|MERGING
)) {
1378 log_verbose("Spawning background lvconvert process for %s",
1380 lvconvert_poll(cmd
, lv
, 1);
1385 * Intial sanity checking of non-recovery related command-line arguments.
1388 * pp: structure allocated by caller, fields written / validated here
1390 int pvcreate_params_validate(struct cmd_context
*cmd
,
1391 int argc
, char **argv
,
1392 struct pvcreate_params
*pp
)
1395 log_error("Please enter a physical volume path");
1399 pp
->yes
= arg_count(cmd
, yes_ARG
);
1400 pp
->force
= (force_t
) arg_count(cmd
, force_ARG
);
1402 if (arg_int_value(cmd
, labelsector_ARG
, 0) >= LABEL_SCAN_SECTORS
) {
1403 log_error("labelsector must be less than %lu",
1404 LABEL_SCAN_SECTORS
);
1407 pp
->labelsector
= arg_int64_value(cmd
, labelsector_ARG
,
1408 DEFAULT_LABELSECTOR
);
1411 if (!(cmd
->fmt
->features
& FMT_MDAS
) &&
1412 (arg_count(cmd
, pvmetadatacopies_ARG
) ||
1413 arg_count(cmd
, metadatasize_ARG
) ||
1414 arg_count(cmd
, dataalignment_ARG
) ||
1415 arg_count(cmd
, dataalignmentoffset_ARG
))) {
1416 log_error("Metadata and data alignment parameters only "
1417 "apply to text format.");
1421 if (arg_count(cmd
, pvmetadatacopies_ARG
) &&
1422 arg_int_value(cmd
, pvmetadatacopies_ARG
, -1) > 2) {
1423 log_error("Metadatacopies may only be 0, 1 or 2");
1427 if (arg_count(cmd
, metadataignore_ARG
)) {
1428 pp
->metadataignore
= !strcmp(arg_str_value(cmd
,
1430 DEFAULT_PVMETADATAIGNORE_STR
),
1433 pp
->metadataignore
= !strcmp(find_config_tree_str(cmd
,
1434 "metadata/pvmetadataignore",
1435 DEFAULT_PVMETADATAIGNORE_STR
),
1438 if (arg_count(cmd
, pvmetadatacopies_ARG
) &&
1439 !arg_int_value(cmd
, pvmetadatacopies_ARG
, -1) &&
1440 pp
->metadataignore
) {
1441 log_error("metadataignore only applies to metadatacopies > 0");
1445 if (arg_count(cmd
, zero_ARG
))
1446 pp
->zero
= strcmp(arg_str_value(cmd
, zero_ARG
, "y"), "n");
1448 if (arg_sign_value(cmd
, dataalignment_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1449 log_error("Physical volume data alignment may not be negative");
1452 pp
->data_alignment
= arg_uint64_value(cmd
, dataalignment_ARG
, UINT64_C(0));
1454 if (pp
->data_alignment
> UINT32_MAX
) {
1455 log_error("Physical volume data alignment is too big.");
1459 if (pp
->data_alignment
&& pp
->pe_start
) {
1460 if (pp
->pe_start
% pp
->data_alignment
)
1461 log_warn("WARNING: Ignoring data alignment %" PRIu64
1462 " incompatible with --restorefile value (%"
1463 PRIu64
").", pp
->data_alignment
, pp
->pe_start
);
1464 pp
->data_alignment
= 0;
1467 if (arg_sign_value(cmd
, dataalignmentoffset_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1468 log_error("Physical volume data alignment offset may not be negative");
1471 pp
->data_alignment_offset
= arg_uint64_value(cmd
, dataalignmentoffset_ARG
, UINT64_C(0));
1473 if (pp
->data_alignment_offset
> UINT32_MAX
) {
1474 log_error("Physical volume data alignment offset is too big.");
1478 if (pp
->data_alignment_offset
&& pp
->pe_start
) {
1479 log_warn("WARNING: Ignoring data alignment offset %" PRIu64
1480 " incompatible with --restorefile value (%"
1481 PRIu64
").", pp
->data_alignment_offset
, pp
->pe_start
);
1482 pp
->data_alignment_offset
= 0;
1485 if (arg_sign_value(cmd
, metadatasize_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1486 log_error("Metadata size may not be negative");
1490 pp
->pvmetadatasize
= arg_uint64_value(cmd
, metadatasize_ARG
, UINT64_C(0));
1491 if (!pp
->pvmetadatasize
)
1492 pp
->pvmetadatasize
= find_config_tree_int(cmd
,
1493 "metadata/pvmetadatasize",
1494 DEFAULT_PVMETADATASIZE
);
1496 pp
->pvmetadatacopies
= arg_int_value(cmd
, pvmetadatacopies_ARG
, -1);
1497 if (pp
->pvmetadatacopies
< 0)
1498 pp
->pvmetadatacopies
= find_config_tree_int(cmd
,
1499 "metadata/pvmetadatacopies",
1500 DEFAULT_PVMETADATACOPIES
);
1505 int get_activation_monitoring_mode(struct cmd_context
*cmd
,
1506 int *monitoring_mode
)
1508 *monitoring_mode
= DEFAULT_DMEVENTD_MONITOR
;
1510 if (arg_count(cmd
, monitor_ARG
) &&
1511 (arg_count(cmd
, ignoremonitoring_ARG
) ||
1512 arg_count(cmd
, sysinit_ARG
))) {
1513 log_error("--ignoremonitoring or --sysinit option not allowed with --monitor option");
1517 if (arg_count(cmd
, monitor_ARG
))
1518 *monitoring_mode
= arg_int_value(cmd
, monitor_ARG
,
1519 DEFAULT_DMEVENTD_MONITOR
);
1520 else if (is_static() || arg_count(cmd
, ignoremonitoring_ARG
) ||
1521 arg_count(cmd
, sysinit_ARG
) ||
1522 !find_config_tree_bool(cmd
, "activation/monitoring",
1523 DEFAULT_DMEVENTD_MONITOR
))
1524 *monitoring_mode
= DMEVENTD_MONITOR_IGNORE
;
1530 * Generic stripe parameter checks.
1532 static int _validate_stripe_params(struct cmd_context
*cmd
, uint32_t *stripes
,
1533 uint32_t *stripe_size
)
1535 if (*stripes
== 1 && *stripe_size
) {
1536 log_print("Ignoring stripesize argument with single stripe");
1540 if (*stripes
> 1 && !*stripe_size
) {
1541 *stripe_size
= find_config_tree_int(cmd
, "metadata/stripesize", DEFAULT_STRIPESIZE
) * 2;
1542 log_print("Using default stripesize %s",
1543 display_size(cmd
, (uint64_t) *stripe_size
));
1546 if (*stripes
< 1 || *stripes
> MAX_STRIPES
) {
1547 log_error("Number of stripes (%d) must be between %d and %d",
1548 *stripes
, 1, MAX_STRIPES
);
1552 if (*stripes
> 1 && (*stripe_size
< STRIPE_SIZE_MIN
||
1553 *stripe_size
& (*stripe_size
- 1))) {
1554 log_error("Invalid stripe size %s",
1555 display_size(cmd
, (uint64_t) *stripe_size
));
1563 * The stripe size is limited by the size of a uint32_t, but since the
1564 * value given by the user is doubled, and the final result must be a
1565 * power of 2, we must divide UINT_MAX by four and add 1 (to round it
1566 * up to the power of 2)
1568 int get_stripe_params(struct cmd_context
*cmd
, uint32_t *stripes
, uint32_t *stripe_size
)
1570 /* stripes_long_ARG takes precedence (for lvconvert) */
1571 *stripes
= arg_uint_value(cmd
, arg_count(cmd
, stripes_long_ARG
) ? stripes_long_ARG
: stripes_ARG
, 1);
1573 *stripe_size
= arg_uint_value(cmd
, stripesize_ARG
, 0);
1575 if (arg_sign_value(cmd
, stripesize_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1576 log_error("Negative stripesize is invalid");
1580 if(arg_uint64_value(cmd
, stripesize_ARG
, 0) > STRIPE_SIZE_LIMIT
* 2) {
1581 log_error("Stripe size cannot be larger than %s",
1582 display_size(cmd
, (uint64_t) STRIPE_SIZE_LIMIT
));
1587 return _validate_stripe_params(cmd
, stripes
, stripe_size
);
1590 /* FIXME move to lib */
1591 static int _pv_change_tag(struct physical_volume
*pv
, const char *tag
, int addtag
)
1594 if (!str_list_add(pv
->fmt
->cmd
->mem
, &pv
->tags
, tag
)) {
1595 log_error("Failed to add tag %s to physical volume %s",
1596 tag
, pv_dev_name(pv
));
1600 str_list_del(&pv
->tags
, tag
);
1605 /* Set exactly one of VG, LV or PV */
1606 int change_tag(struct cmd_context
*cmd
, struct volume_group
*vg
,
1607 struct logical_volume
*lv
, struct physical_volume
*pv
, int arg
)
1610 struct arg_value_group_list
*current_group
;
1612 dm_list_iterate_items(current_group
, &cmd
->arg_value_groups
) {
1613 if (!grouped_arg_is_set(current_group
->arg_values
, arg
))
1616 if (!(tag
= grouped_arg_str_value(current_group
->arg_values
, arg
, NULL
))) {
1617 log_error("Failed to get tag");
1621 if (vg
&& !vg_change_tag(vg
, tag
, arg
== addtag_ARG
))
1623 else if (lv
&& !lv_change_tag(lv
, tag
, arg
== addtag_ARG
))
1625 else if (pv
&& !_pv_change_tag(pv
, tag
, arg
== addtag_ARG
))
1632 /* Return percents of extents and avoid overflow, with optional roundup */
1633 uint32_t percent_of_extents(uint32_t percents
, uint32_t count
, int roundup
)
1635 return (uint32_t)(((uint64_t)percents
* (uint64_t)count
+
1636 ((roundup
) ? 99 : 0)) / 100);