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
)) {
122 dm_list_iterate_items(lvl
, &vg
->lvs
) {
123 if (lvl
->lv
->status
& SNAPSHOT
)
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
)))
132 if (lv_is_virtual_origin(lvl
->lv
) && !arg_count(cmd
, all_ARG
))
136 * Only let hidden LVs through it --all was used or the LVs
137 * were specifically named on the command line.
139 if (!lvargs_supplied
&& !lv_is_visible(lvl
->lv
) && !arg_count(cmd
, all_ARG
))
142 /* Should we process this LV? */
149 if (!process_lv
&& tags_supplied
&&
150 str_list_match_list(tags
, &lvl
->lv
->tags
, NULL
)) {
155 if (lvargs_supplied
&&
156 str_list_match_item(arg_lvnames
, lvl
->lv
->name
)) {
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
);
169 !str_list_add(cmd
->mem
, failed_lvnames
, lv_name
)) {
170 log_error("Allocation failed for str_list.");
173 if (lvl
->lv
->vg
->cmd_missing_vgs
)
174 ret
= ECMD_PROCESSED
;
178 if (sigint_caught()) {
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
;
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
)
198 int ret_max
= ECMD_PROCESSED
;
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
;
216 dm_list_init(&arg_lvnames
);
217 dm_list_init(&failed_lvnames
);
220 log_verbose("Using logical volume(s) on command line");
221 dm_list_init(&arg_vgnames
);
223 for (; opt
< argc
; opt
++) {
224 const char *lv_name
= argv
[opt
];
225 const char *tmp_lv_name
;
227 unsigned dev_dir_found
= 0;
229 /* Do we have a tag or vgname or lvname? */
232 if (*vgname
== '@') {
233 if (!validate_tag(vgname
+ 1)) {
234 log_error("Skipping invalid tag %s",
238 if (!str_list_add(cmd
->mem
, &tags
,
239 dm_pool_strdup(cmd
->mem
,
241 log_error("strlist allocation failed");
247 /* FIXME Jumbled parsing */
248 vgname
= skip_dev_dir(cmd
, vgname
, &dev_dir_found
);
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
;
258 if ((tmp_lv_name
= strchr(vgname
, '/'))) {
260 lv_name
= tmp_lv_name
;
261 while (*lv_name
== '/')
263 if (!(vgname
= extract_vgname(cmd
, vgname
))) {
264 if (ret_max
< ECMD_FAILED
) {
266 ret_max
= ECMD_FAILED
;
270 } else if (!dev_dir_found
&&
271 (vgname_def
= default_vgname(cmd
))) {
276 if (!str_list_add(cmd
->mem
, &arg_vgnames
,
277 dm_pool_strdup(cmd
->mem
, vgname
))) {
278 log_error("strlist allocation failed");
283 if (!str_list_add(cmd
->mem
, &arg_lvnames
,
284 dm_pool_strdup(cmd
->mem
,
286 log_error("strlist allocation failed");
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
,
294 log_error("vg/lv string alloc failed");
297 if (!str_list_add(cmd
->mem
, &arg_lvnames
, vglv
)) {
298 log_error("strlist allocation failed");
303 vgnames
= &arg_vgnames
;
306 if (!argc
|| !dm_list_empty(&tags
)) {
307 log_verbose("Finding all logical volumes");
308 if (!lvmetad_vg_list_to_lvmcache(cmd
))
310 if (!(vgnames
= get_vgnames(cmd
, 0)) || dm_list_empty(vgnames
)) {
311 log_error("No volume groups found");
316 dm_list_iterate_items(strl
, vgnames
) {
318 dm_list_init(&cmd_vgs
);
319 if (!(cvl_vg
= cmd_vg_add(cmd
->mem
, &cmd_vgs
,
320 vgname
, NULL
, flags
))) {
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
;
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
, '/');
341 if ((!lv_name
&& !strcmp(vg_name
, vgname
))) {
342 /* Process all LVs in this VG */
344 dm_list_init(&lvnames
);
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
,
351 log_error("strlist allocation failed");
352 free_cmd_vgs(&cmd_vgs
);
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
) {
367 if (dm_list_empty(&failed_lvnames
))
370 /* Try again with failed LVs in this VG */
371 dm_list_init(&lvnames
);
372 dm_list_splice(&lvnames
, &failed_lvnames
);
374 free_cmd_vgs(&cmd_vgs
);
375 if (!cmd_vg_read(cmd
, &cmd_vgs
)) {
377 ret
= ECMD_FAILED
; /* break */
384 free_cmd_vgs(&cmd_vgs
);
385 /* FIXME: logic for breaking command is not consistent */
386 if (sigint_caught()) {
395 int process_each_segment_in_pv(struct cmd_context
*cmd
,
396 struct volume_group
*vg
,
397 struct physical_volume
*pv
,
399 process_single_pvseg_fn_t process_single_pvseg
)
401 struct pv_segment
*pvseg
;
403 const char *vg_name
= NULL
;
404 int ret_max
= ECMD_PROCESSED
;
406 struct volume_group
*old_vg
= vg
;
407 struct pv_segment _free_pv_segment
= { .pv
= pv
};
409 if (is_pv(pv
) && !vg
&& !is_orphan(pv
)) {
410 vg_name
= pv_vg_name(pv
);
412 vg
= vg_read(cmd
, vg_name
, NULL
, 0);
413 if (vg_read_error(vg
)) {
415 log_error("Skipping volume group %s", vg_name
);
420 * Replace possibly incomplete PV structure with new one
421 * allocated in vg_read_internal() path.
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
);
433 if (dm_list_empty(&pv
->segments
)) {
434 ret
= process_single_pvseg(cmd
, NULL
, &_free_pv_segment
, handle
);
438 dm_list_iterate_items(pvseg
, &pv
->segments
) {
439 ret
= process_single_pvseg(cmd
, vg
, pvseg
, handle
);
447 unlock_vg(cmd
, vg_name
);
454 int process_each_segment_in_lv(struct cmd_context
*cmd
,
455 struct logical_volume
*lv
,
457 process_single_seg_fn_t process_single_seg
)
459 struct lv_segment
*seg
;
460 int ret_max
= ECMD_PROCESSED
;
463 dm_list_iterate_items(seg
, &lv
->segments
) {
464 ret
= process_single_seg(cmd
, seg
, handle
);
467 /* FIXME: logic for breaking command is not consistent */
475 static int _process_one_vg(struct cmd_context
*cmd
, const char *vg_name
,
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
)
481 struct dm_list cmd_vgs
;
482 struct cmd_vg
*cvl_vg
;
485 log_verbose("Finding volume group \"%s\"", vg_name
);
487 dm_list_init(&cmd_vgs
);
488 if (!(cvl_vg
= cmd_vg_add(cmd
->mem
, &cmd_vgs
, vg_name
, vgid
, flags
)))
492 /* FIXME: consistent handling of command break */
493 if (sigint_caught()) {
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
)))) {
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
))
512 ret
= process_single_vg(cmd
, vg_name
, cvl_vg
->vg
, handle
);
514 if (vg_read_error(cvl_vg
->vg
)) /* FAILED_INCONSISTENT */
517 if (!cvl_vg
->vg
->cmd_missing_vgs
)
520 free_cmd_vgs(&cmd_vgs
);
523 free_cmd_vgs(&cmd_vgs
);
525 return (ret
> ret_max
) ? ret
: ret_max
;
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
)
533 int ret_max
= ECMD_PROCESSED
;
536 struct dm_list
*vgnames
, *vgids
;
537 struct dm_list arg_vgnames
, tags
;
539 const char *vg_name
, *vgid
;
542 dm_list_init(&arg_vgnames
);
545 log_verbose("Using volume group(s) on command line");
547 for (; opt
< argc
; opt
++) {
549 if (*vg_name
== '@') {
550 if (!validate_tag(vg_name
+ 1)) {
551 log_error("Skipping invalid tag %s",
553 if (ret_max
< EINVALID_CMD_LINE
)
554 ret_max
= EINVALID_CMD_LINE
;
557 if (!str_list_add(cmd
->mem
, &tags
,
558 dm_pool_strdup(cmd
->mem
,
560 log_error("strlist allocation failed");
566 vg_name
= skip_dev_dir(cmd
, vg_name
, NULL
);
567 if (strchr(vg_name
, '/')) {
568 log_error("Invalid volume group name: %s",
570 if (ret_max
< EINVALID_CMD_LINE
)
571 ret_max
= EINVALID_CMD_LINE
;
574 if (!str_list_add(cmd
->mem
, &arg_vgnames
,
575 dm_pool_strdup(cmd
->mem
, vg_name
))) {
576 log_error("strlist allocation failed");
581 vgnames
= &arg_vgnames
;
584 if (!argc
|| !dm_list_empty(&tags
)) {
585 log_verbose("Finding all volume groups");
586 if (!lvmetad_vg_list_to_lvmcache(cmd
))
588 if (!(vgids
= get_vgids(cmd
, 0)) || dm_list_empty(vgids
)) {
589 log_error("No volume groups found");
592 dm_list_iterate_items(sl
, vgids
) {
594 if (!(vgid
) || !(vg_name
= lvmcache_vgname_from_vgid(cmd
->mem
, vgid
)))
596 ret_max
= _process_one_vg(cmd
, vg_name
, vgid
, &tags
,
599 ret_max
, process_single_vg
);
604 dm_list_iterate_items(sl
, vgnames
) {
606 if (is_orphan_vg(vg_name
))
607 continue; /* FIXME Unnecessary? */
608 ret_max
= _process_one_vg(cmd
, vg_name
, NULL
, &tags
,
611 ret_max
, process_single_vg
);
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
)
624 int ret_max
= ECMD_PROCESSED
;
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
)) {
633 if ((ret
= process_single_pv(cmd
, vg
, pvl
->pv
, handle
)) > ret_max
)
642 static int _process_all_devs(struct cmd_context
*cmd
, void *handle
,
643 process_single_pv_fn_t process_single_pv
)
645 struct physical_volume
*pv
;
646 struct physical_volume pv_dummy
;
647 struct dev_iter
*iter
;
650 int ret_max
= ECMD_PROCESSED
;
653 if (!scan_vgs_for_pvs(cmd
, 1)) {
658 if (!(iter
= dev_iter_create(cmd
->filter
, 1))) {
659 log_error("dev_iter creation failed");
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
);
672 ret
= process_single_pv(cmd
, NULL
, pv
, handle
);
682 dev_iter_destroy(iter
);
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.
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
)
698 int ret_max
= ECMD_PROCESSED
;
700 int lock_global
= !(flags
& READ_WITHOUT_LOCK
) && !(flags
& READ_FOR_UPDATE
);
703 struct physical_volume
*pv
;
704 struct dm_list
*pvslist
, *vgnames
;
706 struct str_list
*sll
;
707 char *at_sign
, *tagname
;
712 if (lock_global
&& !lock_vol(cmd
, VG_GLOBAL
, LCK_VG_READ
)) {
713 log_error("Unable to obtain global lock.");
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;
724 if (!validate_tag(tagname
)) {
725 log_error("Skipping invalid tag %s",
727 if (ret_max
< EINVALID_CMD_LINE
)
728 ret_max
= EINVALID_CMD_LINE
;
731 if (!str_list_add(cmd
->mem
, &tags
,
732 dm_pool_strdup(cmd
->mem
,
734 log_error("strlist allocation failed");
740 if (!(pvl
= find_pv_in_vg(vg
, argv
[opt
]))) {
741 log_error("Physical Volume \"%s\" not "
742 "found in Volume Group "
745 ret_max
= ECMD_FAILED
;
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
;
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
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)) {
771 ret_max
= ECMD_FAILED
;
776 if (!(pv
= pv_read(cmd
, argv
[opt
],
779 log_error("Failed to read "
781 "\"%s\"", argv
[opt
]);
782 ret_max
= ECMD_FAILED
;
788 ret
= process_single_pv(cmd
, vg
, pv
, handle
);
791 * Free PV only if we called pv_read before,
792 * otherwise the PV structure is part of the VG.
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
;
813 ret
= process_each_pv_in_vg(cmd
, vg
, &tags
,
817 unlock_and_release_vg(cmd
, vg
, sll
->str
);
827 log_verbose("Using all physical volume(s) in "
829 ret
= process_each_pv_in_vg(cmd
, vg
, NULL
, handle
,
835 } else if (arg_count(cmd
, all_ARG
)) {
836 ret
= _process_all_devs(cmd
, handle
, process_single_pv
);
842 log_verbose("Scanning for physical volume names");
844 lvmcache_seed_infos_from_lvmetad(cmd
);
845 if (!(pvslist
= get_pvs(cmd
)))
848 dm_list_iterate_items(pvl
, pvslist
) {
849 ret
= process_single_pv(cmd
, NULL
, pvl
->pv
,
851 free_pv_fid(pvl
->pv
);
861 unlock_vg(cmd
, VG_GLOBAL
);
865 unlock_vg(cmd
, VG_GLOBAL
);
871 * Determine volume group name from a logical volume name
873 const char *extract_vgname(struct cmd_context
*cmd
, const char *lv_name
)
875 const char *vg_name
= lv_name
;
877 char *dev_dir
= cmd
->dev_dir
;
880 if (vg_name
&& strchr(vg_name
, '/')) {
881 /* Strip dev_dir (optional) */
882 if (*vg_name
== '/') {
883 while (*vg_name
== '/')
887 if (!strncmp(vg_name
, dev_dir
, strlen(dev_dir
))) {
888 vg_name
+= strlen(dev_dir
);
889 while (*vg_name
== '/')
892 if (*vg_name
== '/') {
893 log_error("\"%s\": Invalid path for Logical "
898 /* Require exactly one set of consecutive slashes */
899 if ((st
= strchr(vg_name
, '/')))
903 if (!st
|| strchr(st
, '/')) {
904 log_error("\"%s\": Invalid path for Logical Volume",
909 vg_name
= dm_pool_strdup(cmd
->mem
, vg_name
);
911 log_error("Allocation of vg_name failed");
915 *strchr(vg_name
, '/') = '\0';
919 if (!(vg_name
= default_vgname(cmd
))) {
921 log_error("Path required for Logical Volume \"%s\"",
930 * Extract default volume group name from environment
932 char *default_vgname(struct cmd_context
*cmd
)
936 /* Take default VG from environment? */
937 vg_path
= getenv("LVM_VG_NAME");
941 vg_path
= skip_dev_dir(cmd
, vg_path
, NULL
);
943 if (strchr(vg_path
, '/')) {
944 log_error("Environment Volume Group in LVM_VG_NAME invalid: "
949 return dm_pool_strdup(cmd
->mem
, vg_path
);
953 * Process physical extent range specifiers
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
)
958 struct pe_range
*per
;
960 log_debug("Adding PE range: start PE %" PRIu32
" length %" PRIu32
961 " on %s", start
, count
, pvname
);
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
")"
971 start
, start
+ count
- 1, per
->start
,
972 per
->start
+ per
->count
- 1, pvname
);
977 if (!(per
= dm_pool_alloc(mem
, sizeof(*per
)))) {
978 log_error("Allocation of list failed");
984 dm_list_add(pe_ranges
, &per
->list
);
989 static int xstrtouint32(const char *s
, char **p
, int base
, uint32_t *result
)
994 ul
= strtoul(s
, p
, base
);
995 if (errno
|| *p
== s
|| (uint32_t) ul
!= ul
)
1001 static int _parse_pes(struct dm_pool
*mem
, char *c
, struct dm_list
*pe_ranges
,
1002 const char *pvname
, uint32_t size
)
1005 uint32_t start
, end
;
1007 /* Default to whole PV */
1009 if (!_add_pe_range(mem
, pvname
, pe_ranges
, UINT32_C(0), size
))
1020 /* Disallow :: and :\0 */
1021 if (*c
== ':' || !*c
)
1024 /* Default to whole range */
1025 start
= UINT32_C(0);
1028 /* Start extent given? */
1030 if (xstrtouint32(c
, &endptr
, 10, &start
))
1033 /* Just one number given? */
1034 if (!*c
|| *c
== ':')
1041 if (xstrtouint32(c
, &endptr
, 10, &end
))
1046 if (*c
&& *c
!= ':')
1049 if ((start
> end
) || (end
> size
- 1)) {
1050 log_error("PE range error: start extent %" PRIu32
" to "
1051 "end extent %" PRIu32
, start
, end
);
1055 if (!_add_pe_range(mem
, pvname
, pe_ranges
, start
, end
- start
+ 1))
1063 log_error("Physical extent parsing error at %s", c
);
1067 static int _create_pv_entry(struct dm_pool
*mem
, struct pv_list
*pvl
,
1068 char *colon
, int allocatable_only
, struct dm_list
*r
)
1071 struct pv_list
*new_pvl
= NULL
, *pvl2
;
1072 struct dm_list
*pe_ranges
;
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
);
1080 if (allocatable_only
&& is_missing_pv(pvl
->pv
)) {
1081 log_error("Physical volume %s is missing", pvname
);
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
);
1091 dm_list_iterate_items(pvl2
, r
)
1092 if (pvl
->pv
->dev
== pvl2
->pv
->dev
) {
1098 if (!(new_pvl
= dm_pool_alloc(mem
, sizeof(*new_pvl
)))) {
1099 log_error("Unable to allocate physical volume list.");
1103 memcpy(new_pvl
, pvl
, sizeof(*new_pvl
));
1105 if (!(pe_ranges
= dm_pool_alloc(mem
, sizeof(*pe_ranges
)))) {
1106 log_error("Allocation of pe_ranges list failed");
1109 dm_list_init(pe_ranges
);
1110 new_pvl
->pe_ranges
= pe_ranges
;
1111 dm_list_add(r
, &new_pvl
->list
);
1114 /* Determine selected physical extents */
1115 if (!_parse_pes(mem
, colon
, new_pvl
->pe_ranges
, pv_dev_name(pvl
->pv
),
1122 struct dm_list
*create_pv_list(struct dm_pool
*mem
, struct volume_group
*vg
, int argc
,
1123 char **argv
, int allocatable_only
)
1126 struct pv_list
*pvl
;
1127 struct dm_list tags
, arg_pvnames
;
1128 char *pvname
= NULL
;
1129 char *colon
, *at_sign
, *tagname
;
1132 /* Build up list of PVs */
1133 if (!(r
= dm_pool_alloc(mem
, sizeof(*r
)))) {
1134 log_error("Allocation of list failed");
1139 dm_list_init(&tags
);
1140 dm_list_init(&arg_pvnames
);
1142 for (i
= 0; i
< argc
; i
++) {
1143 dm_unescape_colons_and_at_signs(argv
[i
], &colon
, &at_sign
);
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
);
1151 dm_list_iterate_items(pvl
, &vg
->pvs
) {
1152 if (str_list_match_item(&pvl
->pv
->tags
,
1154 if (!_create_pv_entry(mem
, pvl
, NULL
,
1165 if (colon
&& !(pvname
= dm_pool_strndup(mem
, pvname
,
1166 (unsigned) (colon
- pvname
)))) {
1167 log_error("Failed to clone PV name");
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
);
1176 if (!_create_pv_entry(mem
, pvl
, colon
, allocatable_only
, r
))
1180 if (dm_list_empty(r
))
1181 log_error("No specified PVs have space available");
1183 return dm_list_empty(r
) ? NULL
: r
;
1186 struct dm_list
*clone_pv_list(struct dm_pool
*mem
, struct dm_list
*pvsl
)
1189 struct pv_list
*pvl
, *new_pvl
;
1191 /* Build up list of PVs */
1192 if (!(r
= dm_pool_alloc(mem
, sizeof(*r
)))) {
1193 log_error("Allocation of list failed");
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.");
1204 memcpy(new_pvl
, pvl
, sizeof(*new_pvl
));
1205 dm_list_add(r
, &new_pvl
->list
);
1211 void vgcreate_params_set_defaults(struct vgcreate_params
*vp_def
,
1212 struct volume_group
*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
;
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
;
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.
1239 int vgcreate_params_set_from_args(struct cmd_context
*cmd
,
1240 struct vgcreate_params
*vp_new
,
1241 struct vgcreate_params
*vp_def
)
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
,
1246 vp_new
->max_pv
= arg_uint_value(cmd
, maxphysicalvolumes_ARG
,
1248 vp_new
->alloc
= (alloc_policy_t
) arg_uint_value(cmd
, alloc_ARG
, vp_def
->alloc
);
1250 /* Units of 512-byte sectors */
1251 vp_new
->extent_size
=
1252 arg_uint_value(cmd
, physicalextentsize_ARG
, vp_def
->extent_size
);
1254 if (arg_count(cmd
, clustered_ARG
))
1256 !strcmp(arg_str_value(cmd
, clustered_ARG
,
1257 vp_def
->clustered
? "y":"n"), "y");
1259 /* Default depends on current locking type */
1260 vp_new
->clustered
= locking_is_clustered();
1262 if (arg_sign_value(cmd
, physicalextentsize_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1263 log_error("Physical extent size may not be negative");
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
));
1273 if (arg_sign_value(cmd
, maxlogicalvolumes_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1274 log_error("Max Logical Volumes may not be negative");
1278 if (arg_sign_value(cmd
, maxphysicalvolumes_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1279 log_error("Max Physical Volumes may not be negative");
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
);
1290 vp_new
->vgmetadatacopies
= find_config_tree_int(cmd
,
1291 "metadata/vgmetadatacopies",
1292 DEFAULT_VGMETADATACOPIES
);
1298 int lv_refresh(struct cmd_context
*cmd
, struct logical_volume
*lv
)
1302 if (!cmd
->partial_activation
&& (lv
->status
& PARTIAL_LV
)) {
1303 log_error("Refusing refresh of partial LV %s. Use --partial to override.",
1308 r
= suspend_lv(cmd
, lv
);
1312 r
= resume_lv(cmd
, lv
);
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
1325 if (background_polling() && lv_is_origin(lv
) && lv_is_merging_origin(lv
))
1326 lv_spawn_background_polling(cmd
, lv
);
1332 int vg_refresh_visible(struct cmd_context
*cmd
, struct volume_group
*vg
)
1334 struct lv_list
*lvl
;
1338 dm_list_iterate_items(lvl
, &vg
->lvs
) {
1339 if (sigint_caught())
1342 if (lv_is_visible(lvl
->lv
))
1343 if (!lv_refresh(cmd
, lvl
->lv
))
1352 void lv_spawn_background_polling(struct cmd_context
*cmd
,
1353 struct logical_volume
*lv
)
1357 if ((lv
->status
& PVMOVE
) &&
1358 (pvname
= get_pvmove_pvname_from_lv_mirr(lv
))) {
1359 log_verbose("Spawning background pvmove process for %s",
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",
1366 pvmove_poll(cmd
, pvname
, 1);
1369 if (lv
->status
& (CONVERTING
|MERGING
)) {
1370 log_verbose("Spawning background lvconvert process for %s",
1372 lvconvert_poll(cmd
, lv
, 1);
1377 * Intial sanity checking of non-recovery related command-line arguments.
1380 * pp: structure allocated by caller, fields written / validated here
1382 int pvcreate_params_validate(struct cmd_context
*cmd
,
1383 int argc
, char **argv
,
1384 struct pvcreate_params
*pp
)
1387 log_error("Please enter a physical volume path");
1391 pp
->yes
= arg_count(cmd
, yes_ARG
);
1392 pp
->force
= (force_t
) arg_count(cmd
, force_ARG
);
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
);
1399 pp
->labelsector
= arg_int64_value(cmd
, labelsector_ARG
,
1400 DEFAULT_LABELSECTOR
);
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.");
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");
1419 if (arg_count(cmd
, metadataignore_ARG
)) {
1420 pp
->metadataignore
= !strcmp(arg_str_value(cmd
,
1422 DEFAULT_PVMETADATAIGNORE_STR
),
1425 pp
->metadataignore
= !strcmp(find_config_tree_str(cmd
,
1426 "metadata/pvmetadataignore",
1427 DEFAULT_PVMETADATAIGNORE_STR
),
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");
1437 if (arg_count(cmd
, zero_ARG
))
1438 pp
->zero
= strcmp(arg_str_value(cmd
, zero_ARG
, "y"), "n");
1440 if (arg_sign_value(cmd
, dataalignment_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1441 log_error("Physical volume data alignment may not be negative");
1444 pp
->data_alignment
= arg_uint64_value(cmd
, dataalignment_ARG
, UINT64_C(0));
1446 if (pp
->data_alignment
> UINT32_MAX
) {
1447 log_error("Physical volume data alignment is too big.");
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;
1459 if (arg_sign_value(cmd
, dataalignmentoffset_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1460 log_error("Physical volume data alignment offset may not be negative");
1463 pp
->data_alignment_offset
= arg_uint64_value(cmd
, dataalignmentoffset_ARG
, UINT64_C(0));
1465 if (pp
->data_alignment_offset
> UINT32_MAX
) {
1466 log_error("Physical volume data alignment offset is too big.");
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;
1477 if (arg_sign_value(cmd
, metadatasize_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1478 log_error("Metadata size may not be negative");
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
);
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
);
1497 int get_activation_monitoring_mode(struct cmd_context
*cmd
,
1498 int *monitoring_mode
)
1500 *monitoring_mode
= DEFAULT_DMEVENTD_MONITOR
;
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");
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
;
1522 * Generic stripe parameter checks.
1524 static int _validate_stripe_params(struct cmd_context
*cmd
, uint32_t *stripes
,
1525 uint32_t *stripe_size
)
1527 if (*stripes
== 1 && *stripe_size
) {
1528 log_print("Ignoring stripesize argument with single stripe");
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
));
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
);
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
));
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)
1560 int get_stripe_params(struct cmd_context
*cmd
, uint32_t *stripes
, uint32_t *stripe_size
)
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);
1565 *stripe_size
= arg_uint_value(cmd
, stripesize_ARG
, 0);
1567 if (arg_sign_value(cmd
, stripesize_ARG
, SIGN_NONE
) == SIGN_MINUS
) {
1568 log_error("Negative stripesize is invalid");
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
));
1579 return _validate_stripe_params(cmd
, stripes
, stripe_size
);
1582 /* FIXME move to lib */
1583 static int _pv_change_tag(struct physical_volume
*pv
, const char *tag
, int 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
));
1592 str_list_del(&pv
->tags
, tag
);
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
)
1602 struct arg_value_group_list
*current_group
;
1604 dm_list_iterate_items(current_group
, &cmd
->arg_value_groups
) {
1605 if (!grouped_arg_is_set(current_group
->arg_values
, arg
))
1608 if (!(tag
= grouped_arg_str_value(current_group
->arg_values
, arg
, NULL
))) {
1609 log_error("Failed to get tag");
1613 if (vg
&& !vg_change_tag(vg
, tag
, arg
== addtag_ARG
))
1615 else if (lv
&& !lv_change_tag(lv
, tag
, arg
== addtag_ARG
))
1617 else if (pv
&& !_pv_change_tag(pv
, tag
, arg
== addtag_ARG
))
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
)
1627 return (uint32_t)(((uint64_t)percents
* (uint64_t)count
+
1628 ((roundup
) ? 99 : 0)) / 100);