]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
managemon: fix typo affecting incrmental assembly.
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 3d2c3f09ad67bdc75165a50f8523161efb4fdb07..aa2c8be87dfbf17763633513e6087052b72bc9e8 100644 (file)
--- a/util.c
+++ b/util.c
@@ -28,6 +28,9 @@
 #include       <sys/utsname.h>
 #include       <sys/wait.h>
 #include       <sys/un.h>
+#include       <sys/resource.h>
+#include       <sys/vfs.h>
+#include       <linux/magic.h>
 #include       <ctype.h>
 #include       <dirent.h>
 #include       <signal.h>
@@ -521,7 +524,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 +548,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 +571,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,7 +780,6 @@ int get_data_disks(int level, int layout, int raid_disks)
        return data_disks;
 }
 
-
 int devnm2devid(char *devnm)
 {
        /* First look in /sys/block/$DEVNM/dev for %d:%d
@@ -868,39 +870,6 @@ void put_md_name(char *name)
        if (strncmp(name, "/dev/.tmp.md", 12) == 0)
                unlink(name);
 }
-
-char *find_free_devnm(int use_partitions)
-{
-       static char devnm[32];
-       int devnum;
-       for (devnum = 127; devnum != 128;
-            devnum = devnum ? devnum-1 : (1<<20)-1) {
-
-               if (use_partitions)
-                       sprintf(devnm, "md_d%d", devnum);
-               else
-                       sprintf(devnm, "md%d", devnum);
-               if (mddev_busy(devnm))
-                       continue;
-               if (!conf_name_is_free(devnm))
-                       continue;
-               if (!use_udev()) {
-                       /* make sure it is new to /dev too, at least as a
-                        * non-standard */
-                       int devid = devnm2devid(devnm);
-                       if (devid) {
-                               char *dn = map_dev(major(devid),
-                                                  minor(devid), 0);
-                               if (dn && ! is_standard(dn, NULL))
-                                       continue;
-                       }
-               }
-               break;
-       }
-       if (devnum == 128)
-               return NULL;
-       return devnm;
-}
 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
 
 int dev_open(char *dev, int flags)
@@ -962,6 +931,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 +944,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 +969,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 +981,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);
@@ -1118,6 +1093,7 @@ struct supertype *dup_super(struct supertype *orig)
        st->max_devs = orig->max_devs;
        st->minor_version = orig->minor_version;
        st->ignore_hw_compat = orig->ignore_hw_compat;
+       st->data_offset = orig->data_offset;
        st->sb = NULL;
        st->info = NULL;
        return st;
@@ -1797,16 +1773,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;
@@ -1959,3 +1925,26 @@ 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);
+}
+
+int in_initrd(void)
+{
+       /* This is based on similar function in systemd. */
+       struct statfs s;
+       return  statfs("/", &s) >= 0 &&
+               (s.f_type == TMPFS_MAGIC ||
+                s.f_type == RAMFS_MAGIC);
+}