]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Subject: Make wait_for and open_dev_excl faster
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index a5eb8ed5c356814b3b765a04a95161011ffa6c26..a9aaea48d7644565228958f236d3a028c752b72f 100644 (file)
--- a/util.c
+++ b/util.c
@@ -28,6 +28,7 @@
 #include       <sys/utsname.h>
 #include       <sys/wait.h>
 #include       <sys/un.h>
+#include       <sys/resource.h>
 #include       <ctype.h>
 #include       <dirent.h>
 #include       <signal.h>
@@ -521,7 +522,7 @@ int check_ext2(int fd, char *name)
        size = sb[4]|(sb[5]|(sb[6]|sb[7]<<8)<<8)<<8;
        pr_err("%s appears to contain an ext2fs file system\n",
                name);
-       fprintf(stderr,"    size=%dK  mtime=%s",
+       cont_err("size=%dK  mtime=%s",
                size*(1<<bsize), ctime(&mtime));
        return 1;
 }
@@ -545,7 +546,7 @@ int check_reiser(int fd, char *name)
                return 0;
        pr_err("%s appears to contain a reiserfs file system\n",name);
        size = sb[0]|(sb[1]|(sb[2]|sb[3]<<8)<<8)<<8;
-       fprintf(stderr, "    size = %luK\n", size*4);
+       cont_err("size = %luK\n", size*4);
 
        return 1;
 }
@@ -568,8 +569,8 @@ int check_raid(int fd, char *name)
        crtime = info.array.ctime;
        level = map_num(pers, info.array.level);
        if (!level) level = "-unknown-";
-       fprintf(stderr, "    level=%s devices=%d ctime=%s",
-               level, info.array.raid_disks, ctime(&crtime));
+       cont_err("level=%s devices=%d ctime=%s",
+                level, info.array.raid_disks, ctime(&crtime));
        return 1;
 }
 
@@ -777,8 +778,6 @@ int get_data_disks(int level, int layout, int raid_disks)
        return data_disks;
 }
 
-#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
-
 int devnm2devid(char *devnm)
 {
        /* First look in /sys/block/$DEVNM/dev for %d:%d
@@ -815,6 +814,7 @@ int devnm2devid(char *devnm)
        return 0;
 }
 
+#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
 char *get_md_name(char *devnm)
 {
        /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
@@ -962,6 +962,7 @@ int open_dev_excl(char *devnm)
        int i;
        int flags = O_RDWR;
        int devid = devnm2devid(devnm);
+       long delay = 1000;
 
        sprintf(buf, "%d:%d", major(devid), minor(devid));
        for (i = 0 ; i < 25 ; i++) {
@@ -974,7 +975,9 @@ int open_dev_excl(char *devnm)
                }
                if (errno != EBUSY)
                        return fd;
-               usleep(200000);
+               usleep(delay);
+               if (delay < 200000)
+                       delay *= 2;
        }
        return -1;
 }
@@ -997,6 +1000,7 @@ void wait_for(char *dev, int fd)
 {
        int i;
        struct stat stb_want;
+       long delay = 1000;
 
        if (fstat(fd, &stb_want) != 0 ||
            (stb_want.st_mode & S_IFMT) != S_IFBLK)
@@ -1008,7 +1012,9 @@ void wait_for(char *dev, int fd)
                    (stb.st_mode & S_IFMT) == S_IFBLK &&
                    (stb.st_rdev == stb_want.st_rdev))
                        return;
-               usleep(200000);
+               usleep(delay);
+               if (delay < 200000)
+                       delay *= 2;
        }
        if (i == 25)
                dprintf("%s: timeout waiting for %s\n", __func__, dev);
@@ -1745,6 +1751,11 @@ int start_mdmon(char *devnm)
                        else
                                skipped = 0;
 
+               /* Don't want to see error messages from systemctl.
+                * If the service doesn't exist, we start mdmon ourselves.
+                */
+               close(2);
+               open("/dev/null", O_WRONLY);
                snprintf(pathbuf, sizeof(pathbuf), "mdmon@%s.service",
                         devnm);
                status = execl("/usr/bin/systemctl", "systemctl", "start",
@@ -1792,16 +1803,6 @@ int start_mdmon(char *devnm)
        return 0;
 }
 
-int check_env(char *name)
-{
-       char *val = getenv(name);
-
-       if (val && atoi(val) == 1)
-               return 1;
-
-       return 0;
-}
-
 __u32 random32(void)
 {
        __u32 rv;
@@ -1954,3 +1955,17 @@ int compare_paths (char* path1, char* path2)
                return 0;
        return 1;
 }
+
+/* Make sure we can open as many devices as needed */
+void enable_fds(int devices)
+{
+       unsigned int fds = 20 + devices;
+       struct rlimit lim;
+       if (getrlimit(RLIMIT_NOFILE, &lim) != 0
+           || lim.rlim_cur >= fds)
+               return;
+       if (lim.rlim_max < fds)
+               lim.rlim_max = fds;
+       lim.rlim_cur = fds;
+       setrlimit(RLIMIT_NOFILE, &lim);
+}