btrfs-progs: limit the max value of leafsize and nodesize
authorRobin Dong <sanbai@taobao.com>
Fri, 28 Sep 2012 03:02:39 +0000 (21:02 -0600)
committerroot <root@localhost.localdomain>
Thu, 4 Oct 2012 20:26:34 +0000 (16:26 -0400)
Using mkfs.btrfs like:

        mkfs.btrfs -l 131072 /dev/sda

will return no error, but after mount it, the dmesg will report:

BTRFS: couldn't mount because metadata blocksize (131072) was too large

The leafsize and nodesize are equal at present, so we just use one function
"check_leaf_or_node_size" to limit leaf and node size below BTRFS_MAX_METADATA_BLOCKSIZE.

Signed-off-by: Robin Dong <sanbai@taobao.com>
Reviewed-by: David Sterba <dave@jikos.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
ctree.h
mkfs.c

diff --git a/ctree.h b/ctree.h
index 4bb66ff..293b24f 100644 (file)
--- a/ctree.h
+++ b/ctree.h
@@ -111,6 +111,12 @@ struct btrfs_trans_handle;
 #define BTRFS_DEV_ITEMS_OBJECTID 1ULL
 
 /*
+ * the max metadata block size.  This limit is somewhat artificial,
+ * but the memmove costs go through the roof for larger blocks.
+ */
+#define BTRFS_MAX_METADATA_BLOCKSIZE 65536
+
+/*
  * we can actually store much bigger names, but lets not confuse the rest
  * of linux
  */
diff --git a/mkfs.c b/mkfs.c
index 4e2535d..47f0c9c 100644 (file)
--- a/mkfs.c
+++ b/mkfs.c
@@ -1201,6 +1201,27 @@ static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
        return ret;
 }
 
+static int check_leaf_or_node_size(u32 size, u32 sectorsize)
+{
+       if (size < sectorsize) {
+               fprintf(stderr,
+                       "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
+                       size, sectorsize);
+               return -1;
+       } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
+               fprintf(stderr,
+                       "Illegal leafsize (or nodesize) %u (larger than %u)\n",
+                       size, BTRFS_MAX_METADATA_BLOCKSIZE);
+               return -1;
+       } else if (size & (sectorsize - 1)) {
+               fprintf(stderr,
+                       "Illegal leafsize (or nodesize) %u (not align to %u)\n",
+                       size, sectorsize);
+               return -1;
+       }
+       return 0;
+}
+
 int main(int ac, char **av)
 {
        char *file;
@@ -1291,14 +1312,10 @@ int main(int ac, char **av)
                }
        }
        sectorsize = max(sectorsize, (u32)getpagesize());
-       if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
-               fprintf(stderr, "Illegal leafsize %u\n", leafsize);
+       if (check_leaf_or_node_size(leafsize, sectorsize))
                exit(1);
-       }
-       if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
-               fprintf(stderr, "Illegal nodesize %u\n", nodesize);
+       if (check_leaf_or_node_size(nodesize, sectorsize))
                exit(1);
-       }
        ac = ac - optind;
        if (ac == 0)
                print_usage();