Move recently merged /sys/dev/ lookup into stat2devnum.
authorNeilBrown <neilb@suse.de>
Mon, 3 Nov 2008 23:35:08 +0000 (10:35 +1100)
committerNeilBrown <neilb@suse.de>
Mon, 3 Nov 2008 23:35:08 +0000 (10:35 +1100)
But sysfs_init and stat2devnum try to convert stat information
into an md devnum.  Combine all the value of both pieces of code
into stat2devnum and have sysfs_init call that.

Signed-off-by: NeilBrown <neilb@suse.de>
sysfs.c
util.c

diff --git a/sysfs.c b/sysfs.c
index 8bcdaa5..00f42ea 100644 (file)
--- a/sysfs.c
+++ b/sysfs.c
@@ -82,49 +82,21 @@ int sysfs_open(int devnum, char *devname, char *attr)
 void sysfs_init(struct mdinfo *mdi, int fd, int devnum)
 {
        if (fd >= 0) {
-               struct stat stb;
                mdu_version_t vers;
-               if (fstat(fd, &stb))
-                       return;
                if (ioctl(fd, RAID_VERSION, &vers) != 0)
                        return;
-               if (major(stb.st_rdev) == MD_MAJOR)
-                       sprintf(mdi->sys_name, "md%d", (int)minor(stb.st_rdev));
-               else if (major(stb.st_rdev) == get_mdp_major())
-                       sprintf(mdi->sys_name, "md_d%d",
-                               (int)minor(stb.st_rdev)>>MdpMinorShift);
-               else {
-                       /* must be an extended-minor partition. Look at the
-                        * /sys/dev/block/%d:%d link which must look like
-                        * ../../block/mdXXX/mdXXXpYY
-                        */
-                       char path[30];
-                       char link[200];
-                       char *cp;
-                       int n;
-                       sprintf(path, "/sys/dev/block/%d:%d", major(stb.st_rdev),
-                               minor(stb.st_rdev));
-                       n = readlink(path, link, sizeof(link)-1);
-                       if (n <= 0)
-                               return;
-                       link[n] = 0;
-                       cp = strrchr(link, '/');
-                       if (cp) *cp = 0;
-                       cp = strchr(link, '/');
-                       if (cp && strncmp(cp, "/md", 3) == 0)
-                               strcpy(mdi->sys_name, cp+1);
-                       else
-                               return;
-               }
-       } else {
-               if (devnum >= 0)
-                       sprintf(mdi->sys_name, "md%d", devnum);
-               else
-                       sprintf(mdi->sys_name, "md_d%d",
-                               -1-devnum);
+               devnum = fd2devnum(fd);
        }
+       if (devnum == NoMdDev)
+               return;
+       if (devnum >= 0)
+               sprintf(mdi->sys_name, "md%d", devnum);
+       else
+               sprintf(mdi->sys_name, "md_d%d",
+                       -1-devnum);
 }
 
+
 struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options)
 {
        /* Longest possible name in sysfs, mounted at /sys, is
diff --git a/util.c b/util.c
index ab2d7e9..f073bce 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1116,13 +1116,34 @@ int devname2devnum(char *name)
 
 int stat2devnum(struct stat *st)
 {
+       char path[30];
+       char link[200];
+       char *cp;
+       int n;
+
        if ((S_IFMT & st->st_mode) == S_IFBLK) {
                if (major(st->st_rdev) == MD_MAJOR)
                        return minor(st->st_rdev);
-               else
-                       return -1- (minor(st->st_rdev)>>6);
+               else if (major(st->st_rdev) == get_mdp_major())
+                       return -1- (minor(st->st_rdev)>>MdpMinorShift);
+
+               /* must be an extended-minor partition. Look at the
+                * /sys/dev/block/%d:%d link which must look like
+                * ../../block/mdXXX/mdXXXpYY
+                */
+               sprintf(path, "/sys/dev/block/%d:%d", major(st->st_rdev),
+                       minor(st->st_rdev));
+               n = readlink(path, link, sizeof(link)-1);
+               if (n <= 0)
+                       return NoMdDev;
+               link[n] = 0;
+               cp = strrchr(link, '/');
+               if (cp) *cp = 0;
+               cp = strchr(link, '/');
+               if (cp && strncmp(cp, "/md", 3) == 0)
+                       return devname2devnum(cp+1);
        }
-       return -1;
+       return NoMdDev;
 
 }
 
@@ -1131,7 +1152,7 @@ int fd2devnum(int fd)
        struct stat stb;
        if (fstat(fd, &stb) == 0)
                return stat2devnum(&stb);
-       return -1;
+       return NoMdDev;
 }
 
 int mdmon_running(int devnum)