]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdadm.c
Write_rules(): Avoid stack corruption if using extremely long udev pathname
[thirdparty/mdadm.git] / mdadm.c
diff --git a/mdadm.c b/mdadm.c
index dd5311d5912c7a503452b33f36aaf8d6cc70ee9e..56de7b7e8df4cd75cd5a2dfd6368317663b02221 100644 (file)
--- a/mdadm.c
+++ b/mdadm.c
@@ -74,6 +74,7 @@ int main(int argc, char *argv[])
        int export = 0;
        int assume_clean = 0;
        char *symlinks = NULL;
+       int grow_continue = 0;
        /* autof indicates whether and how to create device node.
         * bottom 3 bits are style.  Rest (when shifted) are number of parts
         * 0  - unset
@@ -103,15 +104,17 @@ int main(int argc, char *argv[])
        char *shortopt = short_options;
        int dosyslog = 0;
        int rebuild_map = 0;
-       int auto_update_home = 0;
        char *subarray = NULL;
        char *remove_path = NULL;
+       char *udev_filename = NULL;
 
        int print_help = 0;
        FILE *outf;
 
        int mdfd = -1;
 
+       int freeze_reshape = 0;
+
        srandom(time(0) ^ getpid());
 
        ident.uuid_set=0;
@@ -234,6 +237,7 @@ int main(int argc, char *argv[])
                                }
                                subarray = optarg;
                        }
+               case UdevRules:
                case 'K': if (!mode) newmode = MISC; break;
                case NoSharing: newmode = MONITOR; break;
                }
@@ -252,6 +256,12 @@ int main(int argc, char *argv[])
                        exit(2);
                } else if (!mode && newmode) {
                        mode = newmode;
+                       if (mode == MISC && devs_found) {
+                               fprintf(stderr, Name ": No action given for %s in --misc mode\n",
+                                       devlist->devname);
+                               fprintf(stderr,"       Action options must come before device names\n");
+                               exit(2);
+                       }
                } else {
                        /* special case of -c --help */
                        if ((opt == 'c' || opt == ConfigFile) &&
@@ -354,20 +364,15 @@ int main(int argc, char *argv[])
                                exit(2);
                        }
                        chunk = parse_size(optarg);
-                       if (chunk < 8 || ((chunk-1)&chunk)) {
+                       if (chunk < 8 || (chunk&1)) {
                                fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
                                        optarg);
                                exit(2);
                        }
-                       /* Covert sectors to K */
+                       /* Convert sectors to K */
                        chunk /= 2;
                        continue;
 
-#if 0
-               case O(ASSEMBLE,AutoHomeHost):
-                       auto_update_home = 1;
-                       continue;
-#endif
                case O(INCREMENTAL, 'e'):
                case O(CREATE,'e'):
                case O(ASSEMBLE,'e'):
@@ -540,6 +545,7 @@ int main(int argc, char *argv[])
 
                case O(CREATE,AssumeClean):
                case O(BUILD,AssumeClean): /* assume clean */
+               case O(GROW,AssumeClean):
                        assume_clean = 1;
                        continue;
 
@@ -606,10 +612,15 @@ int main(int argc, char *argv[])
                case O(ASSEMBLE,Force): /* force assembly */
                case O(MISC,'f'): /* force zero */
                case O(MISC,Force): /* force zero */
+               case O(MANAGE,Force): /* add device which is too large */
                        force=1;
                        continue;
-
                        /* now for the Assemble options */
+               case O(ASSEMBLE, FreezeReshape):   /* Freeze reshape during
+                                                   * initrd phase */
+               case O(INCREMENTAL, FreezeReshape):
+                       freeze_reshape = 1;
+                       continue;
                case O(CREATE,'u'): /* uuid of array */
                case O(ASSEMBLE,'u'): /* uuid of array */
                        if (ident.uuid_set) {
@@ -666,12 +677,14 @@ int main(int argc, char *argv[])
                case O(ASSEMBLE,'U'): /* update the superblock */
                case O(MISC,'U'):
                        if (update) {
-                               fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
+                               fprintf(stderr, Name ": Can only update one aspect"
+                                       " of superblock, both %s and %s given.\n",
                                        update, optarg);
                                exit(2);
                        }
                        if (mode == MISC && !subarray) {
-                               fprintf(stderr, Name ": Only subarrays can be updated in misc mode\n");
+                               fprintf(stderr, Name ": Only subarrays can be"
+                                       " updated in misc mode\n");
                                exit(2);
                        }
                        update = optarg;
@@ -695,13 +708,17 @@ int main(int argc, char *argv[])
                                continue;
                        if (strcmp(update, "byteorder")==0) {
                                if (ss) {
-                                       fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n");
+                                       fprintf(stderr,
+                                               Name ": must not set metadata"
+                                               " type with --update=byteorder.\n");
                                        exit(2);
                                }
                                for(i=0; !ss && superlist[i]; i++)
-                                       ss = superlist[i]->match_metadata_desc("0.swap");
+                                       ss = superlist[i]->match_metadata_desc(
+                                               "0.swap");
                                if (!ss) {
-                                       fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n");
+                                       fprintf(stderr, Name ": INTERNAL ERROR"
+                                               " cannot find 0.swap\n");
                                        exit(2);
                                }
 
@@ -723,6 +740,27 @@ int main(int argc, char *argv[])
                "     'no-bitmap'\n");
                        exit(outf == stdout ? 0 : 2);
 
+               case O(MANAGE,'U'):
+                       /* update=devicesize is allowed with --re-add */
+                       if (devmode != 'a' || re_add != 1) {
+                               fprintf(stderr, Name "--update in Manage mode only"
+                                       " allowed with --re-add.\n");
+                               exit(1);
+                       }
+                       if (update) {
+                               fprintf(stderr, Name ": Can only update one aspect"
+                                       " of superblock, both %s and %s given.\n",
+                                       update, optarg);
+                               exit(2);
+                       }
+                       update = optarg;
+                       if (strcmp(update, "devicesize") != 0) {
+                               fprintf(stderr, Name ": only 'devicesize' can be"
+                                       " updated with --re-add\n");
+                               exit(2);
+                       }
+                       continue;
+
                case O(INCREMENTAL,NoDegraded):
                        fprintf(stderr, Name ": --no-degraded is deprecated in Incremental mode\n");
                case O(ASSEMBLE,NoDegraded): /* --no-degraded */
@@ -810,6 +848,7 @@ int main(int argc, char *argv[])
                        continue;
                case O(MONITOR,'1'): /* oneshot */
                        oneshot = 1;
+                       spare_sharing = 0;
                        continue;
                case O(MONITOR,'t'): /* test */
                        test = 1;
@@ -902,6 +941,20 @@ int main(int argc, char *argv[])
                        }
                        devmode = opt;
                        continue;
+               case O(MISC, UdevRules):
+                      if (devmode && devmode != opt) {
+                               fprintf(stderr, Name ": --udev-rules must"
+                                      " be the only option.\n");
+                      } else {
+                              if (udev_filename)
+                                      fprintf(stderr, Name ": only specify one udev "
+                                              "rule filename. %s ignored.\n",
+                                              optarg);
+                              else
+                                      udev_filename = optarg;
+                      }
+                      devmode = opt;
+                      continue;
                case O(MISC,'t'):
                        test = 1;
                        continue;
@@ -944,6 +997,11 @@ int main(int argc, char *argv[])
                        backup_file = optarg;
                        continue;
 
+               case O(GROW, Continue):
+                       /* Continue interrupted grow
+                        */
+                       grow_continue = 1;
+                       continue;
                case O(ASSEMBLE, InvalidBackup):
                        /* Acknowledge that the backupfile is invalid, but ask
                         * to continue anyway
@@ -1130,12 +1188,16 @@ int main(int argc, char *argv[])
 
        if (homehost == NULL)
                homehost = conf_get_homehost(&require_homehost);
-       if (homehost == NULL || strcmp(homehost, "<system>")==0) {
+       if (homehost == NULL || strcasecmp(homehost, "<system>")==0) {
                if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
                        sys_hostname[sizeof(sys_hostname)-1] = 0;
                        homehost = sys_hostname;
                }
        }
+       if (homehost && (!homehost[0] || strcasecmp(homehost, "<none>") == 0)) {
+               homehost = NULL;
+               require_homehost = 0;
+       }
 
        if ((mode != MISC || devmode != 'E') &&
            geteuid() != 0) {
@@ -1153,7 +1215,8 @@ int main(int argc, char *argv[])
                        rv = Manage_ro(devlist->devname, mdfd, readonly);
                if (!rv && devs_found>1)
                        rv = Manage_subdevs(devlist->devname, mdfd,
-                                           devlist->next, verbose-quiet, test);
+                                           devlist->next, verbose-quiet, test,
+                                           update, force);
                if (!rv && readonly < 0)
                        rv = Manage_ro(devlist->devname, mdfd, readonly);
                if (!rv && runstop)
@@ -1177,14 +1240,16 @@ int main(int argc, char *argv[])
                                               NULL, backup_file, invalid_backup,
                                               readonly, runstop, update,
                                               homehost, require_homehost,
-                                              verbose-quiet, force);
+                                              verbose-quiet, force,
+                                              freeze_reshape);
                        }
                } else if (!scan)
                        rv = Assemble(ss, devlist->devname, &ident,
                                      devlist->next, backup_file, invalid_backup,
                                      readonly, runstop, update,
                                      homehost, require_homehost,
-                                     verbose-quiet, force);
+                                     verbose-quiet, force,
+                                     freeze_reshape);
                else if (devs_found>0) {
                        if (update && devs_found > 1) {
                                fprintf(stderr, Name ": can only update a single array at a time\n");
@@ -1208,11 +1273,13 @@ int main(int argc, char *argv[])
                                               NULL, backup_file, invalid_backup,
                                               readonly, runstop, update,
                                               homehost, require_homehost,
-                                              verbose-quiet, force);
+                                              verbose-quiet, force,
+                                              freeze_reshape);
                        }
                } else {
                        struct mddev_ident *a, *array_list =  conf_get_ident(NULL);
                        struct mddev_dev *devlist = conf_get_devs();
+                       struct map_ent *map = NULL;
                        int cnt = 0;
                        int failures, successes;
                        if (devlist == NULL) {
@@ -1232,6 +1299,10 @@ int main(int argc, char *argv[])
                                if (a->autof == 0)
                                        a->autof = autof;
                        }
+                       if (map_lock(&map))
+                               fprintf(stderr, Name " %s: failed to get "
+                                       "exclusive lock on mapfile\n",
+                                       __func__);
                        do {
                                failures = 0;
                                successes = 0;
@@ -1249,7 +1320,8 @@ int main(int argc, char *argv[])
                                                     NULL, NULL, 0,
                                                     readonly, runstop, NULL,
                                                     homehost, require_homehost,
-                                                    verbose-quiet, force);
+                                                    verbose-quiet, force,
+                                                    freeze_reshape);
                                        if (r == 0) {
                                                a->assembled = 1;
                                                successes++;
@@ -1274,45 +1346,20 @@ int main(int argc, char *argv[])
                                                rv2 = Assemble(ss, NULL,
                                                               &ident,
                                                               devlist, NULL, 0,
-                                                              readonly, runstop, NULL,
-                                                              homehost, require_homehost,
-                                                              verbose-quiet, force);
+                                                              readonly,
+                                                              runstop, NULL,
+                                                              homehost,
+                                                              require_homehost,
+                                                              verbose-quiet,
+                                                              force,
+                                                              freeze_reshape);
                                                if (rv2==0) {
                                                        cnt++;
                                                        acnt++;
                                                }
-                                               if (rv2 == 1)
-                                                       /* found something so even though assembly failed  we
-                                                        * want to avoid auto-updates
-                                                        */
-                                                       auto_update_home = 0;
                                        } while (rv2!=2);
                                        /* Incase there are stacked devices, we need to go around again */
                                } while (acnt);
-#if 0
-                               if (cnt == 0 && auto_update_home && homehost) {
-                                       /* Nothing found, maybe we need to bootstrap homehost info */
-                                       do {
-                                               acnt = 0;
-                                               do {
-                                                       rv2 = Assemble(
-                                                               ss, NULL,
-                                                               &ident,
-                                                               NULL, NULL, 0,
-                                                               readonly, runstop,
-                                                               "homehost",
-                                                               homehost,
-                                                               require_homehost,
-                                                               verbose-quiet, force);
-                                                       if (rv2==0) {
-                                                               cnt++;
-                                                               acnt++;
-                                                       }
-                                               } while (rv2!=2);
-                                               /* Incase there are stacked devices, we need to go around again */
-                                       } while (acnt);
-                               }
-#endif
                                if (cnt == 0 && rv == 0) {
                                        fprintf(stderr, Name ": No arrays found in config file or automatically\n");
                                        rv = 1;
@@ -1322,6 +1369,7 @@ int main(int argc, char *argv[])
                                fprintf(stderr, Name ": No arrays found in config file\n");
                                rv = 1;
                        }
+                       map_unlock(&map);
                }
                break;
        case BUILD:
@@ -1465,6 +1513,8 @@ int main(int argc, char *argv[])
                                                free_mdstat(ms);
                                        } while (!last && err);
                                        if (err) rv |= 1;
+                               } else if (devmode == UdevRules) {
+                                       rv = Write_rules(udev_filename);
                                } else {
                                        fprintf(stderr, Name ": No devices given.\n");
                                        exit(2);
@@ -1584,22 +1634,26 @@ int main(int argc, char *argv[])
                                break;
                        }
                }
-               if (devs_found > 1) {
-
+               if (devs_found > 1 && raiddisks == 0) {
                        /* must be '-a'. */
-                       if (size >= 0 || raiddisks || chunk || layout_str != NULL || bitmap_file) {
-                               fprintf(stderr, Name ": --add cannot be used with other geometry changes in --grow mode\n");
+                       if (size >= 0 || chunk || layout_str != NULL || bitmap_file) {
+                               fprintf(stderr, Name ": --add cannot be used with "
+                                       "other geometry changes in --grow mode\n");
                                rv = 1;
                                break;
                        }
                        for (dv=devlist->next; dv ; dv=dv->next) {
-                               rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
+                               rv = Grow_Add_device(devlist->devname, mdfd,
+                                                    dv->devname);
                                if (rv)
                                        break;
                        }
                } else if (bitmap_file) {
-                       if (size >= 0 || raiddisks || chunk || layout_str != NULL) {
-                               fprintf(stderr, Name ": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
+                       if (size >= 0 || raiddisks || chunk ||
+                           layout_str != NULL || devs_found > 1) {
+                               fprintf(stderr, Name ": --bitmap changes cannot be "
+                                       "used with other geometry changes "
+                                       "in --grow mode\n");
                                rv = 1;
                                break;
                        }
@@ -1607,11 +1661,16 @@ int main(int argc, char *argv[])
                                delay = DEFAULT_BITMAP_DELAY;
                        rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file,
                                            bitmap_chunk, delay, write_behind, force);
-               } else if (size >= 0 || raiddisks != 0 || layout_str != NULL
+               } else if (grow_continue)
+                       rv = Grow_continue_command(devlist->devname,
+                                                  mdfd, backup_file,
+                                                  verbose);
+               else if (size >= 0 || raiddisks != 0 || layout_str != NULL
                           || chunk != 0 || level != UnSet) {
                        rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file,
                                          size, level, layout_str, chunk, raiddisks,
-                                         force);
+                                         devlist->next,
+                                         assume_clean, force);
                } else if (array_size < 0)
                        fprintf(stderr, Name ": no changes to --grow\n");
                break;
@@ -1652,7 +1711,8 @@ int main(int argc, char *argv[])
                else
                        rv = Incremental(devlist->devname, verbose-quiet,
                                         runstop, ss, homehost,
-                                        require_homehost, autof);
+                                        require_homehost, autof,
+                                        freeze_reshape);
                break;
        case AUTODETECT:
                autodetect();