import source from lvm2 2.02.79
[external/device-mapper.git] / tools / toollib.c
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
7  * This copyrighted material is made available to anyone wishing to use,
8  * modify, copy, or redistribute it subject to the terms and conditions
9  * of the GNU Lesser General Public License v.2.1.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, write to the Free Software Foundation,
13  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  */
15
16 #include "tools.h"
17 #include "lv_alloc.h"
18 #include "xlate.h"
19
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22
23 const char *command_name(struct cmd_context *cmd)
24 {
25         return cmd->command->name;
26 }
27
28 /*
29  * Strip dev_dir if present
30  */
31 char *skip_dev_dir(struct cmd_context *cmd, const char *vg_name,
32                    unsigned *dev_dir_found)
33 {
34         const char *dmdir = dm_dir();
35         size_t dmdir_len = strlen(dmdir), vglv_sz;
36         char *vgname, *lvname, *layer, *vglv;
37
38         /* FIXME Do this properly */
39         if (*vg_name == '/') {
40                 while (*vg_name == '/')
41                         vg_name++;
42                 vg_name--;
43         }
44
45         /* Reformat string if /dev/mapper found */
46         if (!strncmp(vg_name, dmdir, dmdir_len) && vg_name[dmdir_len] == '/') {
47                 if (dev_dir_found)
48                         *dev_dir_found = 1;
49                 vg_name += dmdir_len;
50                 while (*vg_name == '/')
51                         vg_name++;
52
53                 if (!dm_split_lvm_name(cmd->mem, vg_name, &vgname, &lvname, &layer) ||
54                     *layer) {
55                         log_error("skip_dev_dir: Couldn't split up device name %s",
56                                   vg_name);
57                         return (char *) vg_name;
58                 }
59                 vglv_sz = strlen(vgname) + strlen(lvname) + 2;
60                 if (!(vglv = dm_pool_alloc(cmd->mem, vglv_sz)) ||
61                     dm_snprintf(vglv, vglv_sz, "%s%s%s", vgname,
62                                  *lvname ? "/" : "",
63                                  lvname) < 0) {
64                         log_error("vg/lv string alloc failed");
65                         return (char *) vg_name;
66                 }
67                 return vglv;
68         }
69
70         if (!strncmp(vg_name, cmd->dev_dir, strlen(cmd->dev_dir))) {
71                 if (dev_dir_found)
72                         *dev_dir_found = 1;
73                 vg_name += strlen(cmd->dev_dir);
74                 while (*vg_name == '/')
75                         vg_name++;
76         } else if (dev_dir_found)
77                 *dev_dir_found = 0;
78
79         return (char *) vg_name;
80 }
81
82 /*
83  * Metadata iteration functions
84  */
85 int process_each_lv_in_vg(struct cmd_context *cmd,
86                           struct volume_group *vg,
87                           const struct dm_list *arg_lvnames,
88                           const struct dm_list *tags,
89                           struct dm_list *failed_lvnames,
90                           void *handle,
91                           process_single_lv_fn_t process_single_lv)
92 {
93         int ret_max = ECMD_PROCESSED;
94         int ret = 0;
95         unsigned process_all = 0;
96         unsigned process_lv = 0;
97         unsigned tags_supplied = 0;
98         unsigned lvargs_supplied = 0;
99         unsigned lvargs_matched = 0;
100         char *lv_name;
101         struct lv_list *lvl;
102
103         if (!vg_check_status(vg, EXPORTED_VG))
104                 return ECMD_FAILED;
105
106         if (tags && !dm_list_empty(tags))
107                 tags_supplied = 1;
108
109         if (arg_lvnames && !dm_list_empty(arg_lvnames))
110                 lvargs_supplied = 1;
111
112         /* Process all LVs in this VG if no restrictions given */
113         if (!tags_supplied && !lvargs_supplied)
114                 process_all = 1;
115
116         /* Or if VG tags match */
117         if (!process_lv && tags_supplied &&
118             str_list_match_list(tags, &vg->tags, NULL)) {
119                 process_all = 1;
120         }
121
122         dm_list_iterate_items(lvl, &vg->lvs) {
123                 if (lvl->lv->status & SNAPSHOT)
124                         continue;
125
126                 if (lv_is_virtual_origin(lvl->lv) && !arg_count(cmd, all_ARG))
127                         continue;
128
129                 /*
130                  * Only let hidden LVs through it --all was used or the LVs 
131                  * were specifically named on the command line.
132                  */
133                 if (!lvargs_supplied && !lv_is_visible(lvl->lv) && !arg_count(cmd, all_ARG))
134                         continue;
135
136                 /* Should we process this LV? */
137                 if (process_all)
138                         process_lv = 1;
139                 else
140                         process_lv = 0;
141
142                 /* LV tag match? */
143                 if (!process_lv && tags_supplied &&
144                     str_list_match_list(tags, &lvl->lv->tags, NULL)) {
145                         process_lv = 1;
146                 }
147
148                 /* LV name match? */
149                 if (lvargs_supplied &&
150                     str_list_match_item(arg_lvnames, lvl->lv->name)) {
151                         process_lv = 1;
152                         lvargs_matched++;
153                 }
154
155                 if (!process_lv)
156                         continue;
157
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);
162                         if (!lv_name ||
163                             !str_list_add(cmd->mem, failed_lvnames, lv_name)) {
164                                 log_error("Allocation failed for str_list.");
165                                 return ECMD_FAILED;
166                         }
167                         if (lvl->lv->vg->cmd_missing_vgs)
168                                 ret = ECMD_PROCESSED;
169                 }
170                 if (ret > ret_max)
171                         ret_max = ret;
172                 if (sigint_caught())
173                         return ret_max;
174         }
175
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;
180         }
181
182         return ret_max;
183 }
184
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)
188 {
189         int opt = 0;
190         int ret_max = ECMD_PROCESSED;
191         int ret = 0;
192
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;
202         char *vglv;
203         size_t vglv_sz;
204
205         const char *vgname;
206
207         dm_list_init(&tags);
208         dm_list_init(&arg_lvnames);
209         dm_list_init(&failed_lvnames);
210
211         if (argc) {
212                 log_verbose("Using logical volume(s) on command line");
213                 dm_list_init(&arg_vgnames);
214
215                 for (; opt < argc; opt++) {
216                         const char *lv_name = argv[opt];
217                         const char *tmp_lv_name;
218                         char *vgname_def;
219                         unsigned dev_dir_found = 0;
220
221                         /* Do we have a tag or vgname or lvname? */
222                         vgname = lv_name;
223
224                         if (*vgname == '@') {
225                                 if (!validate_tag(vgname + 1)) {
226                                         log_error("Skipping invalid tag %s",
227                                                   vgname);
228                                         continue;
229                                 }
230                                 if (!str_list_add(cmd->mem, &tags,
231                                                   dm_pool_strdup(cmd->mem,
232                                                               vgname + 1))) {
233                                         log_error("strlist allocation failed");
234                                         return ECMD_FAILED;
235                                 }
236                                 continue;
237                         }
238
239                         /* FIXME Jumbled parsing */
240                         vgname = skip_dev_dir(cmd, vgname, &dev_dir_found);
241
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;
247                                 continue;
248                         }
249                         lv_name = vgname;
250                         if ((tmp_lv_name = strchr(vgname, '/'))) {
251                                 /* Must be an LV */
252                                 lv_name = tmp_lv_name;
253                                 while (*lv_name == '/')
254                                         lv_name++;
255                                 if (!(vgname = extract_vgname(cmd, vgname))) {
256                                         if (ret_max < ECMD_FAILED)
257                                                 ret_max = ECMD_FAILED;
258                                         continue;
259                                 }
260                         } else if (!dev_dir_found &&
261                                    (vgname_def = default_vgname(cmd))) {
262                                 vgname = vgname_def;
263                         } else
264                                 lv_name = NULL;
265
266                         if (!str_list_add(cmd->mem, &arg_vgnames,
267                                           dm_pool_strdup(cmd->mem, vgname))) {
268                                 log_error("strlist allocation failed");
269                                 return ECMD_FAILED;
270                         }
271
272                         if (!lv_name) {
273                                 if (!str_list_add(cmd->mem, &arg_lvnames,
274                                                   dm_pool_strdup(cmd->mem,
275                                                               vgname))) {
276                                         log_error("strlist allocation failed");
277                                         return ECMD_FAILED;
278                                 }
279                         } else {
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,
283                                                  lv_name) < 0) {
284                                         log_error("vg/lv string alloc failed");
285                                         return ECMD_FAILED;
286                                 }
287                                 if (!str_list_add(cmd->mem, &arg_lvnames, vglv)) {
288                                         log_error("strlist allocation failed");
289                                         return ECMD_FAILED;
290                                 }
291                         }
292                 }
293                 vgnames = &arg_vgnames;
294         }
295
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");
300                         return ret_max;
301                 }
302         }
303
304         dm_list_iterate_items(strl, vgnames) {
305                 vgname = strl->str;
306                 dm_list_init(&cmd_vgs);
307                 if (!(cvl_vg = cmd_vg_add(cmd->mem, &cmd_vgs,
308                                           vgname, NULL, flags))) {
309                         stack;
310                         return ECMD_FAILED;
311                 }
312
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;
318                         } else
319                                 stack;
320                         continue;
321                 }
322
323                 tags_arg = &tags;
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, '/');
328
329                         if ((!lv_name && !strcmp(vg_name, vgname))) {
330                                 /* Process all LVs in this VG */
331                                 tags_arg = NULL;
332                                 dm_list_init(&lvnames);
333                                 break;
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,
338                                                                  lv_name + 1))) {
339                                         log_error("strlist allocation failed");
340                                         free_cmd_vgs(&cmd_vgs);
341                                         return ECMD_FAILED;
342                                 }
343                         }
344                 }
345
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))
352                                 break;
353
354                         /* Try again with failed LVs in this VG */
355                         dm_list_init(&lvnames);
356                         dm_list_splice(&lvnames, &failed_lvnames);
357
358                         free_cmd_vgs(&cmd_vgs);
359                         if (!cmd_vg_read(cmd, &cmd_vgs)) {
360                                 ret = ECMD_FAILED; /* break */
361                                 break;
362                         }
363                 }
364                 if (ret > ret_max)
365                         ret_max = ret;
366
367                 free_cmd_vgs(&cmd_vgs);
368                 /* FIXME: logic for breaking command is not consistent */
369                 if (sigint_caught())
370                         return ECMD_FAILED;
371         }
372
373         return ret_max;
374 }
375
376 int process_each_segment_in_pv(struct cmd_context *cmd,
377                                struct volume_group *vg,
378                                struct physical_volume *pv,
379                                void *handle,
380                                process_single_pvseg_fn_t process_single_pvseg)
381 {
382         struct pv_segment *pvseg;
383         struct pv_list *pvl;
384         const char *vg_name = NULL;
385         int ret_max = ECMD_PROCESSED;
386         int ret;
387         struct volume_group *old_vg = vg;
388         struct pv_segment _free_pv_segment = { .pv = pv };
389
390         if (is_pv(pv) && !vg && !is_orphan(pv)) {
391                 vg_name = pv_vg_name(pv);
392
393                 vg = vg_read(cmd, vg_name, NULL, 0);
394                 if (vg_read_error(vg)) {
395                         free_vg(vg);
396                         log_error("Skipping volume group %s", vg_name);
397                         return ECMD_FAILED;
398                 }
399
400                 /*
401                  * Replace possibly incomplete PV structure with new one
402                  * allocated in vg_read_internal() path.
403                  */
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);
408                          return ECMD_FAILED;
409                 }
410
411                 pv = pvl->pv;
412         }
413
414         if (dm_list_empty(&pv->segments)) {
415                 ret = process_single_pvseg(cmd, NULL, &_free_pv_segment, handle);
416                 if (ret > ret_max)
417                         ret_max = ret;
418         } else
419                 dm_list_iterate_items(pvseg, &pv->segments) {
420                         ret = process_single_pvseg(cmd, vg, pvseg, handle);
421                         if (ret > ret_max)
422                                 ret_max = ret;
423                         if (sigint_caught())
424                                 break;
425                 }
426
427         if (vg_name)
428                 unlock_vg(cmd, vg_name);
429         if (!old_vg)
430                 free_vg(vg);
431
432         return ret_max;
433 }
434
435 int process_each_segment_in_lv(struct cmd_context *cmd,
436                                struct logical_volume *lv,
437                                void *handle,
438                                process_single_seg_fn_t process_single_seg)
439 {
440         struct lv_segment *seg;
441         int ret_max = ECMD_PROCESSED;
442         int ret;
443
444         dm_list_iterate_items(seg, &lv->segments) {
445                 ret = process_single_seg(cmd, seg, handle);
446                 if (ret > ret_max)
447                         ret_max = ret;
448                 /* FIXME: logic for breaking command is not consistent */
449                 if (sigint_caught())
450                         return ECMD_FAILED;
451         }
452
453         return ret_max;
454 }
455
456 static int _process_one_vg(struct cmd_context *cmd, const char *vg_name,
457                            const char *vgid,
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)
461 {
462         struct dm_list cmd_vgs;
463         struct cmd_vg *cvl_vg;
464         int ret = 0;
465
466         log_verbose("Finding volume group \"%s\"", vg_name);
467
468         dm_list_init(&cmd_vgs);
469         if (!(cvl_vg = cmd_vg_add(cmd->mem, &cmd_vgs, vg_name, vgid, flags)))
470                 return_0;
471
472         for (;;) {
473                 /* FIXME: consistent handling of command break */
474                 if (sigint_caught()) {
475                         ret = ECMD_FAILED;
476                         break;
477                 }
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)))) {
483                                 ret = ECMD_FAILED;
484                                 break;
485                         }
486
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))
491                         break;
492
493                 ret = process_single_vg(cmd, vg_name, cvl_vg->vg, handle);
494
495                 if (vg_read_error(cvl_vg->vg)) /* FAILED_INCONSISTENT */
496                         break;
497
498                 if (!cvl_vg->vg->cmd_missing_vgs)
499                         break;
500
501                 free_cmd_vgs(&cmd_vgs);
502         }
503
504         free_cmd_vgs(&cmd_vgs);
505
506         return (ret > ret_max) ? ret : ret_max;
507 }
508
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)
512 {
513         int opt = 0;
514         int ret_max = ECMD_PROCESSED;
515
516         struct str_list *sl;
517         struct dm_list *vgnames, *vgids;
518         struct dm_list arg_vgnames, tags;
519
520         const char *vg_name, *vgid;
521
522         dm_list_init(&tags);
523         dm_list_init(&arg_vgnames);
524
525         if (argc) {
526                 log_verbose("Using volume group(s) on command line");
527
528                 for (; opt < argc; opt++) {
529                         vg_name = argv[opt];
530                         if (*vg_name == '@') {
531                                 if (!validate_tag(vg_name + 1)) {
532                                         log_error("Skipping invalid tag %s",
533                                                   vg_name);
534                                         if (ret_max < EINVALID_CMD_LINE)
535                                                 ret_max = EINVALID_CMD_LINE;
536                                         continue;
537                                 }
538                                 if (!str_list_add(cmd->mem, &tags,
539                                                   dm_pool_strdup(cmd->mem,
540                                                               vg_name + 1))) {
541                                         log_error("strlist allocation failed");
542                                         return ECMD_FAILED;
543                                 }
544                                 continue;
545                         }
546
547                         vg_name = skip_dev_dir(cmd, vg_name, NULL);
548                         if (strchr(vg_name, '/')) {
549                                 log_error("Invalid volume group name: %s",
550                                           vg_name);
551                                 if (ret_max < EINVALID_CMD_LINE)
552                                         ret_max = EINVALID_CMD_LINE;
553                                 continue;
554                         }
555                         if (!str_list_add(cmd->mem, &arg_vgnames,
556                                           dm_pool_strdup(cmd->mem, vg_name))) {
557                                 log_error("strlist allocation failed");
558                                 return ECMD_FAILED;
559                         }
560                 }
561
562                 vgnames = &arg_vgnames;
563         }
564
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");
569                         return ret_max;
570                 }
571                 dm_list_iterate_items(sl, vgids) {
572                         vgid = sl->str;
573                         if (!(vgid) || !(vg_name = vgname_from_vgid(cmd->mem, vgid)))
574                                 continue;
575                         ret_max = _process_one_vg(cmd, vg_name, vgid, &tags,
576                                                   &arg_vgnames,
577                                                   flags, handle,
578                                                   ret_max, process_single_vg);
579                         if (sigint_caught())
580                                 return ret_max;
581                 }
582         } else {
583                 dm_list_iterate_items(sl, vgnames) {
584                         vg_name = sl->str;
585                         if (is_orphan_vg(vg_name))
586                                 continue;       /* FIXME Unnecessary? */
587                         ret_max = _process_one_vg(cmd, vg_name, NULL, &tags,
588                                                   &arg_vgnames,
589                                                   flags, handle,
590                                                   ret_max, process_single_vg);
591                         if (sigint_caught())
592                                 return ret_max;
593                 }
594         }
595
596         return ret_max;
597 }
598
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)
602 {
603         int ret_max = ECMD_PROCESSED;
604         int ret = 0;
605         struct pv_list *pvl;
606
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)) {
610                         continue;
611                 }
612                 if ((ret = process_single_pv(cmd, vg, pvl->pv, handle)) > ret_max)
613                         ret_max = ret;
614                 if (sigint_caught())
615                         return ret_max;
616         }
617
618         return ret_max;
619 }
620
621 static int _process_all_devs(struct cmd_context *cmd, void *handle,
622                              process_single_pv_fn_t process_single_pv)
623 {
624         struct physical_volume *pv;
625         struct physical_volume pv_dummy;
626         struct dev_iter *iter;
627         struct device *dev;
628
629         int ret_max = ECMD_PROCESSED;
630         int ret = 0;
631
632         if (!scan_vgs_for_pvs(cmd, 1)) {
633                 stack;
634                 return ECMD_FAILED;
635         }
636
637         if (!(iter = dev_iter_create(cmd->filter, 1))) {
638                 log_error("dev_iter creation failed");
639                 return ECMD_FAILED;
640         }
641
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);
647                         pv_dummy.dev = dev;
648                         pv_dummy.fmt = NULL;
649                         pv = &pv_dummy;
650                 }
651                 ret = process_single_pv(cmd, NULL, pv, handle);
652                 if (ret > ret_max)
653                         ret_max = ret;
654                 if (sigint_caught())
655                         break;
656         }
657
658         dev_iter_destroy(iter);
659
660         return ret_max;
661 }
662
663 /*
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.
667  */
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)
672 {
673         int opt = 0;
674         int ret_max = ECMD_PROCESSED;
675         int ret = 0;
676         int lock_global = !(flags & READ_WITHOUT_LOCK) && !(flags & READ_FOR_UPDATE);
677
678         struct pv_list *pvl;
679         struct physical_volume *pv;
680         struct dm_list *pvslist, *vgnames;
681         struct dm_list tags;
682         struct str_list *sll;
683         char *at_sign, *tagname;
684         int scanned = 0;
685         struct dm_list mdas;
686
687         dm_list_init(&tags);
688
689         if (lock_global && !lock_vol(cmd, VG_GLOBAL, LCK_VG_READ)) {
690                 log_error("Unable to obtain global lock.");
691                 return ECMD_FAILED;
692         }
693
694         if (argc) {
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;
700
701                                 if (!validate_tag(tagname)) {
702                                         log_error("Skipping invalid tag %s",
703                                                   tagname);
704                                         if (ret_max < EINVALID_CMD_LINE)
705                                                 ret_max = EINVALID_CMD_LINE;
706                                         continue;
707                                 }
708                                 if (!str_list_add(cmd->mem, &tags,
709                                                   dm_pool_strdup(cmd->mem,
710                                                               tagname))) {
711                                         log_error("strlist allocation failed");
712                                         goto bad;
713                                 }
714                                 continue;
715                         }
716                         if (vg) {
717                                 if (!(pvl = find_pv_in_vg(vg, argv[opt]))) {
718                                         log_error("Physical Volume \"%s\" not "
719                                                   "found in Volume Group "
720                                                   "\"%s\"", argv[opt],
721                                                   vg->name);
722                                         ret_max = ECMD_FAILED;
723                                         continue;
724                                 }
725                                 pv = pvl->pv;
726                         } else {
727
728                                 dm_list_init(&mdas);
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;
734                                         continue;
735                                 }
736
737                                 /*
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
742                                  * PV on the system.
743                                  */
744                                 if (!scanned && is_orphan(pv) &&
745                                     !dm_list_size(&mdas)) {
746                                         if (!scan_label_only &&
747                                             !scan_vgs_for_pvs(cmd, 1)) {
748                                                 stack;
749                                                 ret_max = ECMD_FAILED;
750                                                 continue;
751                                         }
752                                         scanned = 1;
753                                         if (!(pv = pv_read(cmd, argv[opt],
754                                                            NULL, NULL, 1,
755                                                            scan_label_only))) {
756                                                 log_error("Failed to read "
757                                                           "physical volume "
758                                                           "\"%s\"", argv[opt]);
759                                                 ret_max = ECMD_FAILED;
760                                                 continue;
761                                         }
762                                 }
763                         }
764
765                         ret = process_single_pv(cmd, vg, pv, handle);
766                         if (ret > ret_max)
767                                 ret_max = ret;
768                         if (sigint_caught())
769                                 goto out;
770                 }
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;
777                                         free_vg(vg);
778                                         stack;
779                                         continue;
780                                 }
781
782                                 ret = process_each_pv_in_vg(cmd, vg, &tags,
783                                                             handle,
784                                                             process_single_pv);
785
786                                 unlock_and_free_vg(cmd, vg, sll->str);
787
788                                 if (ret > ret_max)
789                                         ret_max = ret;
790                                 if (sigint_caught())
791                                         goto out;
792                         }
793                 }
794         } else {
795                 if (vg) {
796                         log_verbose("Using all physical volume(s) in "
797                                     "volume group");
798                         ret = process_each_pv_in_vg(cmd, vg, NULL, handle,
799                                                     process_single_pv);
800                         if (ret > ret_max)
801                                 ret_max = ret;
802                         if (sigint_caught())
803                                 goto out;
804                 } else if (arg_count(cmd, all_ARG)) {
805                         ret = _process_all_devs(cmd, handle, process_single_pv);
806                         if (ret > ret_max)
807                                 ret_max = ret;
808                         if (sigint_caught())
809                                 goto out;
810                 } else {
811                         log_verbose("Scanning for physical volume names");
812
813                         if (!(pvslist = get_pvs(cmd)))
814                                 goto bad;
815
816                         dm_list_iterate_items(pvl, pvslist) {
817                                 ret = process_single_pv(cmd, NULL, pvl->pv,
818                                                      handle);
819                                 if (ret > ret_max)
820                                         ret_max = ret;
821                                 if (sigint_caught())
822                                         goto out;
823                         }
824                 }
825         }
826 out:
827         if (lock_global)
828                 unlock_vg(cmd, VG_GLOBAL);
829         return ret_max;
830 bad:
831         if (lock_global)
832                 unlock_vg(cmd, VG_GLOBAL);
833
834         return ECMD_FAILED;
835 }
836
837 /*
838  * Determine volume group name from a logical volume name
839  */
840 const char *extract_vgname(struct cmd_context *cmd, const char *lv_name)
841 {
842         const char *vg_name = lv_name;
843         char *st;
844         char *dev_dir = cmd->dev_dir;
845
846         /* Path supplied? */
847         if (vg_name && strchr(vg_name, '/')) {
848                 /* Strip dev_dir (optional) */
849                 if (*vg_name == '/') {
850                         while (*vg_name == '/')
851                                 vg_name++;
852                         vg_name--;
853                 }
854                 if (!strncmp(vg_name, dev_dir, strlen(dev_dir))) {
855                         vg_name += strlen(dev_dir);
856                         while (*vg_name == '/')
857                                 vg_name++;
858                 }
859                 if (*vg_name == '/') {
860                         log_error("\"%s\": Invalid path for Logical "
861                                   "Volume", lv_name);
862                         return 0;
863                 }
864
865                 /* Require exactly one set of consecutive slashes */
866                 if ((st = strchr(vg_name, '/')))
867                         while (*st == '/')
868                                 st++;
869
870                 if (!st || strchr(st, '/')) {
871                         log_error("\"%s\": Invalid path for Logical Volume",
872                                   lv_name);
873                         return 0;
874                 }
875
876                 vg_name = dm_pool_strdup(cmd->mem, vg_name);
877                 if (!vg_name) {
878                         log_error("Allocation of vg_name failed");
879                         return 0;
880                 }
881
882                 *strchr(vg_name, '/') = '\0';
883                 return vg_name;
884         }
885
886         if (!(vg_name = default_vgname(cmd))) {
887                 if (lv_name)
888                         log_error("Path required for Logical Volume \"%s\"",
889                                   lv_name);
890                 return 0;
891         }
892
893         return vg_name;
894 }
895
896 /*
897  * Extract default volume group name from environment
898  */
899 char *default_vgname(struct cmd_context *cmd)
900 {
901         char *vg_path;
902
903         /* Take default VG from environment? */
904         vg_path = getenv("LVM_VG_NAME");
905         if (!vg_path)
906                 return 0;
907
908         vg_path = skip_dev_dir(cmd, vg_path, NULL);
909
910         if (strchr(vg_path, '/')) {
911                 log_error("Environment Volume Group in LVM_VG_NAME invalid: "
912                           "\"%s\"", vg_path);
913                 return 0;
914         }
915
916         return dm_pool_strdup(cmd->mem, vg_path);
917 }
918
919 /*
920  * Process physical extent range specifiers
921  */
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)
924 {
925         struct pe_range *per;
926
927         log_debug("Adding PE range: start PE %" PRIu32 " length %" PRIu32
928                   " on %s", start, count, pvname);
929
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 ")"
937                                   " on %s",
938                                   start, start + count - 1, per->start,
939                                   per->start + per->count - 1, pvname);
940                         return 0;
941                 }
942         }
943
944         if (!(per = dm_pool_alloc(mem, sizeof(*per)))) {
945                 log_error("Allocation of list failed");
946                 return 0;
947         }
948
949         per->start = start;
950         per->count = count;
951         dm_list_add(pe_ranges, &per->list);
952
953         return 1;
954 }
955
956 static int xstrtouint32(const char *s, char **p, int base, uint32_t *result)
957 {
958         unsigned long ul;
959
960         errno = 0;
961         ul = strtoul(s, p, base);
962         if (errno || *p == s || (uint32_t) ul != ul)
963                 return -1;
964         *result = ul;
965         return 0;
966 }
967
968 static int _parse_pes(struct dm_pool *mem, char *c, struct dm_list *pe_ranges,
969                       const char *pvname, uint32_t size)
970 {
971         char *endptr;
972         uint32_t start, end;
973
974         /* Default to whole PV */
975         if (!c) {
976                 if (!_add_pe_range(mem, pvname, pe_ranges, UINT32_C(0), size))
977                         return_0;
978                 return 1;
979         }
980
981         while (*c) {
982                 if (*c != ':')
983                         goto error;
984
985                 c++;
986
987                 /* Disallow :: and :\0 */
988                 if (*c == ':' || !*c)
989                         goto error;
990
991                 /* Default to whole range */
992                 start = UINT32_C(0);
993                 end = size - 1;
994
995                 /* Start extent given? */
996                 if (isdigit(*c)) {
997                         if (xstrtouint32(c, &endptr, 10, &start))
998                                 goto error;
999                         c = endptr;
1000                         /* Just one number given? */
1001                         if (!*c || *c == ':')
1002                                 end = start;
1003                 }
1004                 /* Range? */
1005                 if (*c == '-') {
1006                         c++;
1007                         if (isdigit(*c)) {
1008                                 if (xstrtouint32(c, &endptr, 10, &end))
1009                                         goto error;
1010                                 c = endptr;
1011                         }
1012                 }
1013                 if (*c && *c != ':')
1014                         goto error;
1015
1016                 if ((start > end) || (end > size - 1)) {
1017                         log_error("PE range error: start extent %" PRIu32 " to "
1018                                   "end extent %" PRIu32, start, end);
1019                         return 0;
1020                 }
1021
1022                 if (!_add_pe_range(mem, pvname, pe_ranges, start, end - start + 1))
1023                         return_0;
1024
1025         }
1026
1027         return 1;
1028
1029       error:
1030         log_error("Physical extent parsing error at %s", c);
1031         return 0;
1032 }
1033
1034 static int _create_pv_entry(struct dm_pool *mem, struct pv_list *pvl,
1035                              char *colon, int allocatable_only, struct dm_list *r)
1036 {
1037         const char *pvname;
1038         struct pv_list *new_pvl = NULL, *pvl2;
1039         struct dm_list *pe_ranges;
1040
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);
1044                 return 1;
1045         }
1046
1047         if (allocatable_only && is_missing_pv(pvl->pv)) {
1048                 log_error("Physical volume %s is missing", pvname);
1049                 return 1;
1050         }
1051
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);
1055                 return 1;
1056         }
1057
1058         dm_list_iterate_items(pvl2, r)
1059                 if (pvl->pv->dev == pvl2->pv->dev) {
1060                         new_pvl = pvl2;
1061                         break;
1062                 }
1063
1064         if (!new_pvl) {
1065                 if (!(new_pvl = dm_pool_alloc(mem, sizeof(*new_pvl)))) {
1066                         log_error("Unable to allocate physical volume list.");
1067                         return 0;
1068                 }
1069
1070                 memcpy(new_pvl, pvl, sizeof(*new_pvl));
1071
1072                 if (!(pe_ranges = dm_pool_alloc(mem, sizeof(*pe_ranges)))) {
1073                         log_error("Allocation of pe_ranges list failed");
1074                         return 0;
1075                 }
1076                 dm_list_init(pe_ranges);
1077                 new_pvl->pe_ranges = pe_ranges;
1078                 dm_list_add(r, &new_pvl->list);
1079         }
1080
1081         /* Determine selected physical extents */
1082         if (!_parse_pes(mem, colon, new_pvl->pe_ranges, pv_dev_name(pvl->pv),
1083                         pvl->pv->pe_count))
1084                 return_0;
1085
1086         return 1;
1087 }
1088
1089 struct dm_list *create_pv_list(struct dm_pool *mem, struct volume_group *vg, int argc,
1090                             char **argv, int allocatable_only)
1091 {
1092         struct dm_list *r;
1093         struct pv_list *pvl;
1094         struct dm_list tags, arg_pvnames;
1095         char *pvname = NULL;
1096         char *colon, *at_sign, *tagname;
1097         int i;
1098
1099         /* Build up list of PVs */
1100         if (!(r = dm_pool_alloc(mem, sizeof(*r)))) {
1101                 log_error("Allocation of list failed");
1102                 return NULL;
1103         }
1104         dm_list_init(r);
1105
1106         dm_list_init(&tags);
1107         dm_list_init(&arg_pvnames);
1108
1109         for (i = 0; i < argc; i++) {
1110                 unescape_colons_and_at_signs(argv[i], &colon, &at_sign);
1111
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);
1116                                 continue;
1117                         }
1118                         dm_list_iterate_items(pvl, &vg->pvs) {
1119                                 if (str_list_match_item(&pvl->pv->tags,
1120                                                         tagname)) {
1121                                         if (!_create_pv_entry(mem, pvl, NULL,
1122                                                               allocatable_only,
1123                                                               r))
1124                                                 return_NULL;
1125                                 }
1126                         }
1127                         continue;
1128                 }
1129
1130                 pvname = argv[i];
1131
1132                 if (colon && !(pvname = dm_pool_strndup(mem, pvname,
1133                                         (unsigned) (colon - pvname)))) {
1134                         log_error("Failed to clone PV name");
1135                         return NULL;
1136                 }
1137
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);
1141                         return NULL;
1142                 }
1143                 if (!_create_pv_entry(mem, pvl, colon, allocatable_only, r))
1144                         return_NULL;
1145         }
1146
1147         if (dm_list_empty(r))
1148                 log_error("No specified PVs have space available");
1149
1150         return dm_list_empty(r) ? NULL : r;
1151 }
1152
1153 struct dm_list *clone_pv_list(struct dm_pool *mem, struct dm_list *pvsl)
1154 {
1155         struct dm_list *r;
1156         struct pv_list *pvl, *new_pvl;
1157
1158         /* Build up list of PVs */
1159         if (!(r = dm_pool_alloc(mem, sizeof(*r)))) {
1160                 log_error("Allocation of list failed");
1161                 return NULL;
1162         }
1163         dm_list_init(r);
1164
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.");
1168                         return NULL;
1169                 }
1170
1171                 memcpy(new_pvl, pvl, sizeof(*new_pvl));
1172                 dm_list_add(r, &new_pvl->list);
1173         }
1174
1175         return r;
1176 }
1177
1178 void vgcreate_params_set_defaults(struct vgcreate_params *vp_def,
1179                                   struct volume_group *vg)
1180 {
1181         if (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;
1189         } else {
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;
1197         }
1198 }
1199
1200 /*
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.
1205  */
1206 int vgcreate_params_set_from_args(struct cmd_context *cmd,
1207                                   struct vgcreate_params *vp_new,
1208                                   struct vgcreate_params *vp_def)
1209 {
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,
1212                                         vp_def->max_lv);
1213         vp_new->max_pv = arg_uint_value(cmd, maxphysicalvolumes_ARG,
1214                                         vp_def->max_pv);
1215         vp_new->alloc = arg_uint_value(cmd, alloc_ARG, vp_def->alloc);
1216
1217         /* Units of 512-byte sectors */
1218         vp_new->extent_size =
1219             arg_uint_value(cmd, physicalextentsize_ARG, vp_def->extent_size);
1220
1221         if (arg_count(cmd, clustered_ARG))
1222                 vp_new->clustered =
1223                         !strcmp(arg_str_value(cmd, clustered_ARG,
1224                                               vp_def->clustered ? "y":"n"), "y");
1225         else
1226                 /* Default depends on current locking type */
1227                 vp_new->clustered = locking_is_clustered();
1228
1229         if (arg_sign_value(cmd, physicalextentsize_ARG, 0) == SIGN_MINUS) {
1230                 log_error("Physical extent size may not be negative");
1231                 return 1;
1232         }
1233
1234         if (arg_sign_value(cmd, maxlogicalvolumes_ARG, 0) == SIGN_MINUS) {
1235                 log_error("Max Logical Volumes may not be negative");
1236                 return 1;
1237         }
1238
1239         if (arg_sign_value(cmd, maxphysicalvolumes_ARG, 0) == SIGN_MINUS) {
1240                 log_error("Max Physical Volumes may not be negative");
1241                 return 1;
1242         }
1243
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);
1250         } else {
1251                 vp_new->vgmetadatacopies = find_config_tree_int(cmd,
1252                                                    "metadata/vgmetadatacopies",
1253                                                    DEFAULT_VGMETADATACOPIES);
1254         }
1255
1256         return 0;
1257 }
1258
1259 int lv_refresh(struct cmd_context *cmd, struct logical_volume *lv)
1260 {
1261         int r = 0;
1262
1263         if (!cmd->partial_activation && (lv->status & PARTIAL_LV)) {
1264                 log_error("Refusing refresh of partial LV %s. Use --partial to override.",
1265                           lv->name);
1266                 goto out;
1267         }
1268
1269         r = suspend_lv(cmd, lv);
1270         if (!r)
1271                 goto_out;
1272
1273         r = resume_lv(cmd, lv);
1274         if (!r)
1275                 goto_out;
1276
1277         /*
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
1285          */
1286         if (background_polling() && lv_is_origin(lv) && lv_is_merging_origin(lv))
1287                 lv_spawn_background_polling(cmd, lv);
1288
1289 out:
1290         return r;
1291 }
1292
1293 int vg_refresh_visible(struct cmd_context *cmd, struct volume_group *vg)
1294 {
1295         struct lv_list *lvl;
1296         int r = 1;
1297
1298         dm_list_iterate_items(lvl, &vg->lvs)
1299                 if (lv_is_visible(lvl->lv))
1300                         if (!lv_refresh(cmd, lvl->lv))
1301                                 r = 0;
1302
1303         return r;
1304 }
1305
1306 void lv_spawn_background_polling(struct cmd_context *cmd,
1307                                  struct logical_volume *lv)
1308 {
1309         const char *pvname;
1310
1311         if ((lv->status & PVMOVE) &&
1312             (pvname = get_pvmove_pvname_from_lv_mirr(lv))) {
1313                 log_verbose("Spawning background pvmove process for %s",
1314                             pvname);
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",
1319                             pvname);
1320                 pvmove_poll(cmd, pvname, 1);
1321         }
1322
1323         if (lv->status & (CONVERTING|MERGING)) {
1324                 log_verbose("Spawning background lvconvert process for %s",
1325                         lv->name);
1326                 lvconvert_poll(cmd, lv, 1);
1327         }
1328 }
1329
1330 /*
1331  * Intial sanity checking of non-recovery related command-line arguments.
1332  *
1333  * Output arguments:
1334  * pp: structure allocated by caller, fields written / validated here
1335  */
1336 int pvcreate_params_validate(struct cmd_context *cmd,
1337                              int argc, char **argv,
1338                              struct pvcreate_params *pp)
1339 {
1340         if (!argc) {
1341                 log_error("Please enter a physical volume path");
1342                 return 0;
1343         }
1344
1345         pp->yes = arg_count(cmd, yes_ARG);
1346         pp->force = arg_count(cmd, force_ARG);
1347
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);
1351                 return 0;
1352         } else {
1353                 pp->labelsector = arg_int64_value(cmd, labelsector_ARG,
1354                                                   DEFAULT_LABELSECTOR);
1355         }
1356
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.");
1364                 return 0;
1365         }
1366
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");
1370                 return 0;
1371         }
1372
1373         if (arg_count(cmd, metadataignore_ARG)) {
1374                 pp->metadataignore = !strcmp(arg_str_value(cmd,
1375                                                 metadataignore_ARG,
1376                                                 DEFAULT_PVMETADATAIGNORE_STR),
1377                                          "y");
1378         } else {
1379                 pp->metadataignore = !strcmp(find_config_tree_str(cmd,
1380                                         "metadata/pvmetadataignore",
1381                                         DEFAULT_PVMETADATAIGNORE_STR),
1382                                         "y");
1383         }
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");
1388                 return 0;
1389         }
1390
1391         if (arg_count(cmd, zero_ARG))
1392                 pp->zero = strcmp(arg_str_value(cmd, zero_ARG, "y"), "n");
1393
1394         if (arg_sign_value(cmd, dataalignment_ARG, 0) == SIGN_MINUS) {
1395                 log_error("Physical volume data alignment may not be negative");
1396                 return 0;
1397         }
1398         pp->data_alignment = arg_uint64_value(cmd, dataalignment_ARG, UINT64_C(0));
1399
1400         if (pp->data_alignment > ULONG_MAX) {
1401                 log_error("Physical volume data alignment is too big.");
1402                 return 0;
1403         }
1404
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;
1411         }
1412
1413         if (arg_sign_value(cmd, dataalignmentoffset_ARG, 0) == SIGN_MINUS) {
1414                 log_error("Physical volume data alignment offset may not be negative");
1415                 return 0;
1416         }
1417         pp->data_alignment_offset = arg_uint64_value(cmd, dataalignmentoffset_ARG, UINT64_C(0));
1418
1419         if (pp->data_alignment_offset > ULONG_MAX) {
1420                 log_error("Physical volume data alignment offset is too big.");
1421                 return 0;
1422         }
1423
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;
1429         }
1430
1431         if (arg_sign_value(cmd, metadatasize_ARG, 0) == SIGN_MINUS) {
1432                 log_error("Metadata size may not be negative");
1433                 return 0;
1434         }
1435
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);
1441
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);
1447
1448         return 1;
1449 }
1450
1451 int get_activation_monitoring_mode(struct cmd_context *cmd,
1452                                    struct volume_group *vg,
1453                                    int *monitoring_mode)
1454 {
1455         *monitoring_mode = DEFAULT_DMEVENTD_MONITOR;
1456
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");
1461                 return 0;
1462         }
1463
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;
1472
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"),
1479                           vg->name);
1480                 return 0;
1481         }
1482         
1483         return 1;
1484 }
1485
1486 /*
1487  * Generic stripe parameter checks.
1488  */
1489 static int _validate_stripe_params(struct cmd_context *cmd, uint32_t *stripes,
1490                                    uint32_t *stripe_size)
1491 {
1492         if (*stripes == 1 && *stripe_size) {
1493                 log_print("Ignoring stripesize argument with single stripe");
1494                 *stripe_size = 0;
1495         }
1496
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));
1501         }
1502
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);
1506                 return 0;
1507         }
1508
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));
1513                 return 0;
1514         }
1515
1516         return 1;
1517 }
1518
1519 /*
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)
1524  */
1525 int get_stripe_params(struct cmd_context *cmd, uint32_t *stripes, uint32_t *stripe_size)
1526 {
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);
1529
1530         *stripe_size = arg_uint_value(cmd, stripesize_ARG, 0);
1531         if (*stripe_size) {
1532                 if (arg_sign_value(cmd, stripesize_ARG, 0) == SIGN_MINUS) {
1533                         log_error("Negative stripesize is invalid");
1534                         return 0;
1535                 }
1536
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));
1540                         return 0;
1541                 }
1542         }
1543
1544         return _validate_stripe_params(cmd, stripes, stripe_size);
1545 }
1546