ext4: Fallback to vmalloc if kmalloc can't allocate s_flex_groups array
authorTheodore Ts'o <tytso@mit.edu>
Tue, 28 Apr 2009 02:48:48 +0000 (22:48 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 28 Apr 2009 02:48:48 +0000 (22:48 -0400)
For very large filesystems, the s_flex_groups array can get quite big.
For example, a filesystem that can be resized up to 16TB will have
8192 flex groups (assuming the default flex_bg size of 16), so the
array is 96k, which is *very* marginal for kmalloc().  On the other
hand, a 160GB filesystem without the resize_inode feature will only
require 960 bytes.  So we try to allocate the array first using
kmalloc(), and if that fails, we'll try to use vmalloc() instead.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
fs/ext4/super.c

index 2958f4e6f222a8956f595145b1fe31e16e4eec9d..3f4475daa66d6024ce37cb32c2ae77da37b2d814 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/string.h>
 #include <linux/fs.h>
 #include <linux/time.h>
+#include <linux/vmalloc.h>
 #include <linux/jbd2.h>
 #include <linux/slab.h>
 #include <linux/init.h>
@@ -586,7 +587,10 @@ static void ext4_put_super(struct super_block *sb)
        for (i = 0; i < sbi->s_gdb_count; i++)
                brelse(sbi->s_group_desc[i]);
        kfree(sbi->s_group_desc);
-       kfree(sbi->s_flex_groups);
+       if (is_vmalloc_addr(sbi->s_flex_groups))
+               vfree(sbi->s_flex_groups);
+       else
+               kfree(sbi->s_flex_groups);
        percpu_counter_destroy(&sbi->s_freeblocks_counter);
        percpu_counter_destroy(&sbi->s_freeinodes_counter);
        percpu_counter_destroy(&sbi->s_dirs_counter);
@@ -1620,6 +1624,7 @@ static int ext4_fill_flex_info(struct super_block *sb)
        ext4_group_t flex_group_count;
        ext4_group_t flex_group;
        int groups_per_flex = 0;
+       size_t size;
        int i;
 
        if (!sbi->s_es->s_log_groups_per_flex) {
@@ -1634,8 +1639,13 @@ static int ext4_fill_flex_info(struct super_block *sb)
        flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
                        ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
                              EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
-       sbi->s_flex_groups = kzalloc(flex_group_count *
-                                    sizeof(struct flex_groups), GFP_KERNEL);
+       size = flex_group_count * sizeof(struct flex_groups);
+       sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
+       if (sbi->s_flex_groups == NULL) {
+               sbi->s_flex_groups = vmalloc(size);
+               if (sbi->s_flex_groups)
+                       memset(sbi->s_flex_groups, 0, size);
+       }
        if (sbi->s_flex_groups == NULL) {
                printk(KERN_ERR "EXT4-fs: not enough memory for "
                                "%u flex groups\n", flex_group_count);
@@ -2842,6 +2852,12 @@ failed_mount4:
                sbi->s_journal = NULL;
        }
 failed_mount3:
+       if (sbi->s_flex_groups) {
+               if (is_vmalloc_addr(sbi->s_flex_groups))
+                       vfree(sbi->s_flex_groups);
+               else
+                       kfree(sbi->s_flex_groups);
+       }
        percpu_counter_destroy(&sbi->s_freeblocks_counter);
        percpu_counter_destroy(&sbi->s_freeinodes_counter);
        percpu_counter_destroy(&sbi->s_dirs_counter);