]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Central calls to ioctl BLKGETSIZE
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 1ccf4e1623dbe1f3002d381a0a7acb8c98ade55f..ffb113c3c9d5179a872e0e55ed39b7f8f7ef0043 100644 (file)
--- a/util.c
+++ b/util.c
@@ -170,7 +170,7 @@ void remove_partitions(int fd)
 #endif
 }
 
-int enough(int level, int raid_disks, int layout,
+int enough(int level, int raid_disks, int layout, int clean,
           char *avail, int avail_disks)
 {
        int copies, first;
@@ -179,7 +179,7 @@ int enough(int level, int raid_disks, int layout,
                /* This is the tricky one - we need to check
                 * which actual disks are present.
                 */
-               copies = (layout&255)* (layout>>8);
+               copies = (layout&255)* ((layout>>8) & 255);
                first=0;
                do {
                        /* there must be one of the 'copies' form 'first' */
@@ -205,9 +205,15 @@ int enough(int level, int raid_disks, int layout,
                return avail_disks >= 1;
        case 4:
        case 5:
-               return avail_disks >= raid_disks-1;
+               if (clean)
+                       return avail_disks >= raid_disks-1;
+               else
+                       return avail_disks >= raid_disks;
        case 6:
-               return avail_disks >= raid_disks-2;
+               if (clean)
+                       return avail_disks >= raid_disks-2;
+               else
+                       return avail_disks >= raid_disks;
        default:
                return 0;
        }
@@ -241,6 +247,7 @@ int same_uuid(int a[4], int b[4], int swapuuid)
        }
 }
 
+#ifndef MDASSEMBLE
 int check_ext2(int fd, char *name)
 {
        /*
@@ -338,6 +345,7 @@ int ask(char *mesg)
        fprintf(stderr, Name ": assuming 'no'\n");
        return 0;
 }
+#endif /* MDASSEMBLE */
 
 char *map_num(mapping_t *map, int num)
 {
@@ -534,6 +542,7 @@ unsigned long calc_csum(void *super, int bytes)
        return csum;
 }
 
+#ifndef MDASSEMBLE
 char *human_size(long long bytes)
 {
        static char buf[30];
@@ -586,7 +595,9 @@ char *human_size_brief(long long bytes)
                        );
        return buf;
 }
+#endif
 
+#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
 int get_mdp_major(void)
 {
 static int mdp_major = -1;
@@ -670,6 +681,7 @@ void put_md_name(char *name)
        if (strncmp(name, "/dev/.tmp.md", 12)==0)
                unlink(name);
 }
+#endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
 
 int dev_open(char *dev, int flags)
 {
@@ -761,6 +773,28 @@ struct supertype *guess_super(int fd)
        return NULL;
 }
 
+/* Return size of device in bytes */
+int get_dev_size(int fd, char *dname, unsigned long long *sizep)
+{
+       unsigned long long ldsize;
+#ifdef BLKGETSIZE64
+       if (ioctl(fd, BLKGETSIZE64, &ldsize) != 0)
+#endif
+       {
+               unsigned long dsize;
+               if (ioctl(fd, BLKGETSIZE, &dsize) == 0) {
+                       ldsize = dsize;
+                       ldsize <<= 9;
+               } else {
+                       if (dname)
+                               fprintf(stderr, Name ": Cannot get size of %s: %s\b",
+                                       dname, strerror(errno));
+                       return 0;
+               }
+       }
+       *sizep = ldsize;
+       return 1;
+}
 
 #ifdef __TINYC__
 /* tinyc doesn't optimize this check in ioctl.h out ... */