utils: teach fsck mount and remount about the root name
authorAlan Cox <alan@linux.intel.com>
Sun, 2 Sep 2018 00:05:34 +0000 (01:05 +0100)
committerAlan Cox <alan@linux.intel.com>
Sun, 2 Sep 2018 00:05:34 +0000 (01:05 +0100)
This allows us to stop all the substroot stuff as with fsck split we now have
enough space. This actually turns the whole thing into a win. We trade the
extra exec's of substroot for a fork/exec of fsck.

Not only that but with a bit of thought we can later optimise the single
case fsck to kill the fork off

Applications/util/fsck.c
Applications/util/mount.c
Applications/util/remount.c

index d44d869..b564992 100644 (file)
@@ -22,13 +22,15 @@ const char *mntpoint(const char *mount)
 {
        FILE *fp;
        struct mntent *mnt;
+       const char *p;
 
        fp = setmntent("/etc/fstab", "r");
        if (fp) {
                while (mnt = getmntent(fp)) {
+                       p = mnt_device_path(mnt);
                        if (strcmp(mnt->mnt_dir, mount) == 0) {
                                endmntent(fp);
-                               return mnt->mnt_fsname;
+                               return p;
                        }
                }
                endmntent(fp);
index b07f8c3..9101e00 100644 (file)
@@ -60,14 +60,14 @@ static int flagsof(struct mntent *mnt)
     return f;
 }
 
-static int is_mounted(struct mntent *ent)
+static int is_mounted(const char *path)
 {
     FILE *f = setmntent("/etc/mtab", "r");
     struct mntent mnt;
     static char buf[_MAX_MNTLEN];
     
     while(getmntent_r(f, &mnt, buf, _MAX_MNTLEN)) {
-        if (strcmp(mnt.mnt_fsname, ent->mnt_fsname) == 0) {
+        if (strcmp(mnt.mnt_fsname, path) == 0) {
             endmntent(f);
             return 1;
         }
@@ -78,11 +78,13 @@ static int is_mounted(struct mntent *ent)
 
 static void do_mount(struct mntent *mnt)
 {
+    const char *p;
     if (strcmp(mnt->mnt_type, "swap") == 0)
         return;
-    if (mount(mnt->mnt_fsname, mnt->mnt_dir, flagsof(mnt)) == -1) {
+    p = mnt_device_path(mnt);
+    if (mount(p, mnt->mnt_dir, flagsof(mnt)) == -1) {
         err = errno;
-        perror(mnt->mnt_fsname);
+        perror(p);
         return;
     }
     add2mtab(mnt);
@@ -91,14 +93,16 @@ static void do_mount(struct mntent *mnt)
 static void automount(char *match)
 {
     FILE *f = setmntent("/etc/fstab", "r");
+    const char *p;
     struct mntent *mnt;
 
     while (mnt = getmntent(f)) {
+        p = mnt_device_path(mnt);
         /* Warning - mnt contents go invalid if we do work on mtab so
            be careful here and use getmntent_r in is_mounted */
-        if (is_mounted(mnt))
+        if (is_mounted(p))
             continue;
-        if (match == NULL || strcmp(mnt->mnt_fsname, match) == 0 ||
+        if (match == NULL || strcmp(p, match) == 0 ||
                                 strcmp(mnt->mnt_dir, match) == 0)
                 do_mount(mnt);
         if (err)
index 432d56c..8ed911d 100644 (file)
@@ -35,44 +35,19 @@ static char *modify_opts(char *opts, int flags)
        return optbuf;
 }
 
-#define DEV_PATH "/dev/"
-
-static char *root_device(void)
-{
-    static DIR dp;
-    struct dirent *entry;
-    struct stat filestat, rootstat;
-    static char namebuf[sizeof(DEV_PATH) + MAXNAMLEN + 1];
-
-    if (stat("/", &rootstat) == 0
-       && opendir_r(&dp,DEV_PATH) != (DIR *) NULL) {
-       while ((entry = readdir(&dp)) != (struct dirent *) NULL) {
-           strcpy(namebuf, DEV_PATH);
-           strlcat(namebuf, entry->d_name, sizeof(namebuf));
-           if (stat(namebuf, &filestat) != 0)
-               continue;
-           if (!S_ISBLK(filestat.st_mode))
-               continue;
-           if (filestat.st_rdev != rootstat.st_dev)
-               continue;
-            return namebuf;
-       }
-    }
-    return NULL;
-}
-
-
 static char *getdev(char *arg, char *p)
 {
        FILE *f;
        struct mntent *mnt;
+       const char *path;
 
        f = setmntent(p, "r");
        if (f) {
                while (mnt = getmntent(f)) {
-                       if ((strcmp(mnt->mnt_fsname, arg) == 0) || (strcmp(mnt->mnt_dir, arg) == 0)) {
+                       path = mnt_device_path(mnt);
+                       if ((strcmp(path, arg) == 0) || (strcmp(mnt->mnt_dir, arg) == 0)) {
                                endmntent(f);
-                               return strdup(mnt->mnt_fsname);
+                               return strdup(path);
                        }
                }
                endmntent(f);
@@ -81,7 +56,7 @@ static char *getdev(char *arg, char *p)
           we want it to do the right thing anyway so you can clean up nicely */
        if (strcmp(arg, "/"))
                return NULL;
-       return root_device();
+       return root_device_name();
 }
 
 static void rewrite_mtab(char *name, int flags)