]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdadm.c
mdadm.c: various minor clean-ups
[thirdparty/mdadm.git] / mdadm.c
diff --git a/mdadm.c b/mdadm.c
index 1efa3e80a2eb40292f9379eb0270b17bc1584f4d..a15fd0a23698ca3969519f7dbae9f1a7bf849659 100644 (file)
--- a/mdadm.c
+++ b/mdadm.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
@@ -29,7 +29,6 @@
 #include "md_p.h"
 #include <ctype.h>
 
-
 int main(int argc, char *argv[])
 {
        int mode = 0;
@@ -73,6 +72,7 @@ int main(int argc, char *argv[])
        int test = 0;
        int export = 0;
        int assume_clean = 0;
+       char *prefer = NULL;
        char *symlinks = NULL;
        int grow_continue = 0;
        /* autof indicates whether and how to create device node.
@@ -176,7 +176,7 @@ int main(int argc, char *argv[])
 
                /*
                 * --offroot sets first char of argv[0] to @. This is used
-                * by systemd to signal that the tast was launched from
+                * by systemd to signal that the task was launched from
                 * initrd/initramfs and should be preserved during shutdown
                 */
                case OffRootOpt:
@@ -184,6 +184,13 @@ int main(int argc, char *argv[])
                        __offroot = 1;
                        continue;
 
+               case Prefer:
+                       if (prefer)
+                               free(prefer);
+                       if (asprintf(&prefer, "/%s/", optarg) <= 0)
+                               prefer = NULL;
+                       continue;
+
                case ':':
                case '?':
                        fputs(Usage, stderr);
@@ -212,15 +219,23 @@ int main(int argc, char *argv[])
                        }
                        break;
 
-               case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break;
-               case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break;
-               case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break;
-               case 'F': newmode = MONITOR;break;
+               case 'A': newmode = ASSEMBLE;
+                       shortopt = short_bitmap_auto_options;
+                       break;
+               case 'B': newmode = BUILD;
+                       shortopt = short_bitmap_auto_options;
+                       break;
+               case 'C': newmode = CREATE;
+                       shortopt = short_bitmap_auto_options;
+                       break;
+               case 'F': newmode = MONITOR;
+                       break;
                case 'G': newmode = GROW;
                        shortopt = short_bitmap_options;
                        break;
                case 'I': newmode = INCREMENTAL;
-                       shortopt = short_bitmap_auto_options; break;
+                       shortopt = short_bitmap_auto_options;
+                       break;
                case AutoDetect:
                        newmode = AUTODETECT;
                        break;
@@ -244,7 +259,7 @@ int main(int argc, char *argv[])
                case KillSubarray:
                case UpdateSubarray:
                case UdevRules:
-               case 'K':
+               case KillOpt:
                        if (!mode)
                                newmode = MISC;
                        break;
@@ -578,7 +593,7 @@ int main(int argc, char *argv[])
                        ident.raid_disks = raiddisks;
                        continue;
 
-               case O(CREATE,'x'): /* number of spare (eXtra) discs */
+               case O(CREATE,'x'): /* number of spare (eXtra) disks */
                        if (sparedisks) {
                                fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
                                        sparedisks, optarg);
@@ -825,7 +840,7 @@ int main(int argc, char *argv[])
                case O(MONITOR,'r'): /* rebuild increments */
                case O(MONITOR,Increment):
                        increments = atoi(optarg);
-                       if (increments>99 || increments<1) {
+                       if (increments > 99 || increments < 1) {
                                fprintf(stderr, Name ": please specify positive integer between 1 and 99 as rebuild increments.\n");
                                exit(2);
                        }
@@ -872,6 +887,7 @@ int main(int argc, char *argv[])
                case O(MONITOR, NoSharing):
                        spare_sharing = 0;
                        continue;
+
                        /* now the general management options.  Some are applicable
                         * to other modes. None have arguments.
                         */
@@ -924,7 +940,7 @@ int main(int argc, char *argv[])
                case O(MISC,'Q'):
                case O(MISC,'D'):
                case O(MISC,'E'):
-               case O(MISC,'K'):
+               case O(MISC,KillOpt):
                case O(MISC,'R'):
                case O(MISC,'S'):
                case O(MISC,'X'):
@@ -1035,7 +1051,7 @@ int main(int argc, char *argv[])
                case O(CREATE,Bitmap): /* here we create the bitmap */
                        if (strcmp(optarg, "none") == 0) {
                                fprintf(stderr, Name ": '--bitmap none' only"
-                                       " support for --grow\n");
+                                       " supported for --grow\n");
                                exit(2);
                        }
                        /* FALL THROUGH */
@@ -1056,15 +1072,14 @@ int main(int argc, char *argv[])
                case O(BUILD,BitmapChunk):
                case O(CREATE,BitmapChunk): /* bitmap chunksize */
                        bitmap_chunk = parse_size(optarg);
-                       if (bitmap_chunk < 0 ||
+                       if (bitmap_chunk <= 0 ||
                            bitmap_chunk & (bitmap_chunk - 1)) {
                                fprintf(stderr,
                                        Name ": invalid bitmap chunksize: %s\n",
                                        optarg);
                                exit(2);
                        }
-                       /* convert sectors to B, chunk of 0 means 512B */
-                       bitmap_chunk = bitmap_chunk ? bitmap_chunk * 512 : 512;
+                       bitmap_chunk = bitmap_chunk * 512;
                        continue;
 
                case O(GROW, WriteBehind):
@@ -1151,12 +1166,13 @@ int main(int argc, char *argv[])
         *
         * That is mosty checked in the per-mode stuff but...
         *
-        * For @,B,C  and A without -s, the first device listed must be an md device
-        * we check that here and open it.
+        * For @,B,C and A without -s, the first device listed must be
+        * an md device.  We check that here and open it.
         */
 
-       if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
-           (mode == ASSEMBLE && ! scan)) {
+       if (mode == MANAGE || mode == BUILD || mode == CREATE
+           || mode == GROW
+           || (mode == ASSEMBLE && ! scan)) {
                if (devs_found < 1) {
                        fprintf(stderr, Name ": an md device must be given in this mode\n");
                        exit(2);
@@ -1198,7 +1214,7 @@ int main(int argc, char *argv[])
        }
 
        if (raiddisks) {
-               if (raiddisks == 1 &&  !force && level != -5) {
+               if (raiddisks == 1 &&  !force && level != LEVEL_FAULTY) {
                        fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
                                "     a mistake.  If you really mean it you will need to specify --force before\n"
                                "     setting the number of drives.\n");
@@ -1219,9 +1235,10 @@ int main(int argc, char *argv[])
                require_homehost = 0;
        }
 
-       if (!((mode == MISC && devmode == 'E')
-             || (mode == MONITOR && spare_sharing == 0)) &&
-           geteuid() != 0) {
+       if ((mode == MISC && devmode == 'E')
+           || (mode == MONITOR && spare_sharing == 0))
+               /* Anyone may try this */;
+       else if (geteuid() != 0) {
                fprintf(stderr, Name ": must be super-user to perform this action\n");
                exit(1);
        }
@@ -1271,7 +1288,7 @@ int main(int argc, char *argv[])
                                      homehost, require_homehost,
                                      verbose-quiet, force,
                                      freeze_reshape);
-               else if (devs_found>0) {
+               else if (devs_found > 0) {
                        if (update && devs_found > 1) {
                                fprintf(stderr, Name ": can only update a single array at a time\n");
                                exit(1);
@@ -1499,7 +1516,7 @@ int main(int argc, char *argv[])
                                                if (devmode == 'D')
                                                        rv |= Detail(name, v,
                                                                     export, test,
-                                                                    homehost);
+                                                                    homehost, prefer);
                                                else
                                                        rv |= WaitClean(name, -1, v);
                                                put_md_name(name);
@@ -1553,9 +1570,9 @@ int main(int argc, char *argv[])
                                case 'D':
                                        rv |= Detail(dv->devname,
                                                     brief?1+verbose:0,
-                                                    export, test, homehost);
+                                                    export, test, homehost, prefer);
                                        continue;
-                               case 'K': /* Zero superblock */
+                               case KillOpt: /* Zero superblock */
                                        if (ss)
                                                rv |= Kill(dv->devname, ss, force, quiet,0);
                                        else {
@@ -1627,7 +1644,8 @@ int main(int argc, char *argv[])
                }
                rv= Monitor(devlist, mailaddr, program,
                            delay?delay:60, daemonise, scan, oneshot,
-                           dosyslog, test, pidfile, increments, spare_sharing);
+                           dosyslog, test, pidfile, increments,
+                           spare_sharing, prefer);
                break;
 
        case GROW: