ext4: fix error processing in mb_free_blocks
[pandora-kernel.git] / fs / ext4 / mballoc.c
index c9900aa..b882868 100644 (file)
@@ -381,22 +381,28 @@ static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
 
 static inline int mb_find_next_zero_bit(void *addr, int max, int start)
 {
-       int fix = 0;
+       int fix = 0, ret, tmpmax;
        addr = mb_correct_addr_and_bit(&fix, addr);
-       max += fix;
+       tmpmax = max + fix;
        start += fix;
 
-       return ext4_find_next_zero_bit(addr, max, start) - fix;
+       ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
+       if (ret > max)
+               return max;
+       return ret;
 }
 
 static inline int mb_find_next_bit(void *addr, int max, int start)
 {
-       int fix = 0;
+       int fix = 0, ret, tmpmax;
        addr = mb_correct_addr_and_bit(&fix, addr);
-       max += fix;
+       tmpmax = max + fix;
        start += fix;
 
-       return ext4_find_next_bit(addr, max, start) - fix;
+       ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
+       if (ret > max)
+               return max;
+       return ret;
 }
 
 static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
@@ -803,6 +809,7 @@ static int ext4_mb_init_cache(struct page *page, char *incore)
                if (!buffer_uptodate(bh[i]))
                        goto out;
 
+       err = 0;
        first_block = page->index * blocks_per_page;
        for (i = 0; i < blocks_per_page; i++) {
                int group;
@@ -883,6 +890,7 @@ ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
        int pnum;
        int poff;
        struct page *page;
+       int ret;
 
        mb_debug("load group %lu\n", group);
 
@@ -914,15 +922,21 @@ ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
                if (page) {
                        BUG_ON(page->mapping != inode->i_mapping);
                        if (!PageUptodate(page)) {
-                               ext4_mb_init_cache(page, NULL);
+                               ret = ext4_mb_init_cache(page, NULL);
+                               if (ret) {
+                                       unlock_page(page);
+                                       goto err;
+                               }
                                mb_cmp_bitmaps(e4b, page_address(page) +
                                               (poff * sb->s_blocksize));
                        }
                        unlock_page(page);
                }
        }
-       if (page == NULL || !PageUptodate(page))
+       if (page == NULL || !PageUptodate(page)) {
+               ret = -EIO;
                goto err;
+       }
        e4b->bd_bitmap_page = page;
        e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
        mark_page_accessed(page);
@@ -938,14 +952,20 @@ ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
                page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
                if (page) {
                        BUG_ON(page->mapping != inode->i_mapping);
-                       if (!PageUptodate(page))
-                               ext4_mb_init_cache(page, e4b->bd_bitmap);
-
+                       if (!PageUptodate(page)) {
+                               ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
+                               if (ret) {
+                                       unlock_page(page);
+                                       goto err;
+                               }
+                       }
                        unlock_page(page);
                }
        }
-       if (page == NULL || !PageUptodate(page))
+       if (page == NULL || !PageUptodate(page)) {
+               ret = -EIO;
                goto err;
+       }
        e4b->bd_buddy_page = page;
        e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
        mark_page_accessed(page);
@@ -962,7 +982,7 @@ err:
                page_cache_release(e4b->bd_buddy_page);
        e4b->bd_buddy = NULL;
        e4b->bd_bitmap = NULL;
-       return -EIO;
+       return ret;
 }
 
 static void ext4_mb_release_desc(struct ext4_buddy *e4b)
@@ -1031,7 +1051,7 @@ static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
        }
 }
 
-static int mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
+static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
                          int first, int count)
 {
        int block = 0;
@@ -1071,11 +1091,12 @@ static int mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
                        blocknr += block;
                        blocknr +=
                            le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
-
+                       ext4_unlock_group(sb, e4b->bd_group);
                        ext4_error(sb, __func__, "double-free of inode"
                                   " %lu's block %llu(bit %u in group %lu)\n",
                                   inode ? inode->i_ino : 0, blocknr, block,
                                   e4b->bd_group);
+                       ext4_lock_group(sb, e4b->bd_group);
                }
                mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
                e4b->bd_info->bb_counters[order]++;
@@ -1113,8 +1134,6 @@ static int mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
                } while (1);
        }
        mb_check_buddy(e4b);
-
-       return 0;
 }
 
 static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
@@ -1730,10 +1749,6 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
                ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
                spin_unlock(&sbi->s_md_lock);
        }
-
-       /* searching for the right group start from the goal value specified */
-       group = ac->ac_g_ex.fe_group;
-
        /* Let's just scan groups to find more-less suitable blocks */
        cr = ac->ac_2order ? 0 : 1;
        /*
@@ -1743,6 +1758,12 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
 repeat:
        for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
                ac->ac_criteria = cr;
+               /*
+                * searching for the right group start
+                * from the goal value specified
+                */
+               group = ac->ac_g_ex.fe_group;
+
                for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
                        struct ext4_group_info *grp;
                        struct ext4_group_desc *desc;
@@ -1963,6 +1984,8 @@ static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
        int rc;
        int size;
 
+       if (unlikely(sbi->s_mb_history == NULL))
+               return -ENOMEM;
        s = kmalloc(sizeof(*s), GFP_KERNEL);
        if (s == NULL)
                return -ENOMEM;
@@ -2165,9 +2188,7 @@ static void ext4_mb_history_init(struct super_block *sb)
        sbi->s_mb_history_cur = 0;
        spin_lock_init(&sbi->s_mb_history_lock);
        i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
-       sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
-       if (likely(sbi->s_mb_history != NULL))
-               memset(sbi->s_mb_history, 0, i);
+       sbi->s_mb_history = kzalloc(i, GFP_KERNEL);
        /* if we can't allocate history, then we simple won't use it */
 }
 
@@ -2281,7 +2302,6 @@ static int ext4_mb_init_backend(struct super_block *sb)
                        i++;
                        goto err_freebuddy;
                }
-               memset(meta_group_info[j], 0, len);
                set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
                        &(meta_group_info[j]->bb_state));
 
@@ -2305,7 +2325,7 @@ static int ext4_mb_init_backend(struct super_block *sb)
                        meta_group_info[j]->bb_bitmap =
                                kmalloc(sb->s_blocksize, GFP_KERNEL);
                        BUG_ON(meta_group_info[j]->bb_bitmap == NULL);
-                       bh = read_block_bitmap(sb, i);
+                       bh = ext4_read_block_bitmap(sb, i);
                        BUG_ON(bh == NULL);
                        memcpy(meta_group_info[j]->bb_bitmap, bh->b_data,
                                        sb->s_blocksize);
@@ -2336,6 +2356,7 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
        unsigned i;
        unsigned offset;
        unsigned max;
+       int ret;
 
        if (!test_opt(sb, MBALLOC))
                return 0;
@@ -2370,12 +2391,12 @@ int ext4_mb_init(struct super_block *sb, int needs_recovery)
        } while (i <= sb->s_blocksize_bits + 1);
 
        /* init file for buddy data */
-       i = ext4_mb_init_backend(sb);
-       if (i) {
+       ret = ext4_mb_init_backend(sb);
+       if (ret != 0) {
                clear_opt(sbi->s_mount_opt, MBALLOC);
                kfree(sbi->s_mb_offsets);
                kfree(sbi->s_mb_maxs);
-               return i;
+               return ret;
        }
 
        spin_lock_init(&sbi->s_md_lock);
@@ -2548,8 +2569,7 @@ ext4_mb_free_committed_blocks(struct super_block *sb)
                ext4_lock_group(sb, md->group);
                for (i = 0; i < md->num; i++) {
                        mb_debug(" %u", md->blocks[i]);
-                       err = mb_free_blocks(NULL, &e4b, md->blocks[i], 1);
-                       BUG_ON(err != 0);
+                       mb_free_blocks(NULL, &e4b, md->blocks[i], 1);
                }
                mb_debug("\n");
                ext4_unlock_group(sb, md->group);
@@ -2575,25 +2595,24 @@ ext4_mb_free_committed_blocks(struct super_block *sb)
 
 
 
-#define MB_PROC_VALUE_READ(name)                               \
-static int ext4_mb_read_##name(char *page, char **start,       \
-               off_t off, int count, int *eof, void *data)     \
+#define MB_PROC_FOPS(name)                                     \
+static int ext4_mb_##name##_proc_show(struct seq_file *m, void *v)     \
 {                                                              \
-       struct ext4_sb_info *sbi = data;                        \
-       int len;                                                \
-       *eof = 1;                                               \
-       if (off != 0)                                           \
-               return 0;                                       \
-       len = sprintf(page, "%ld\n", sbi->s_mb_##name);         \
-       *start = page;                                          \
-       return len;                                             \
-}
-
-#define MB_PROC_VALUE_WRITE(name)                              \
-static int ext4_mb_write_##name(struct file *file,             \
-               const char __user *buf, unsigned long cnt, void *data)  \
+       struct ext4_sb_info *sbi = m->private;                  \
+                                                               \
+       seq_printf(m, "%ld\n", sbi->s_mb_##name);               \
+       return 0;                                               \
+}                                                              \
+                                                               \
+static int ext4_mb_##name##_proc_open(struct inode *inode, struct file *file)\
 {                                                              \
-       struct ext4_sb_info *sbi = data;                        \
+       return single_open(file, ext4_mb_##name##_proc_show, PDE(inode)->data);\
+}                                                              \
+                                                               \
+static ssize_t ext4_mb_##name##_proc_write(struct file *file,  \
+               const char __user *buf, size_t cnt, loff_t *ppos)       \
+{                                                              \
+       struct ext4_sb_info *sbi = PDE(file->f_path.dentry->d_inode)->data;\
        char str[32];                                           \
        long value;                                             \
        if (cnt >= sizeof(str))                                 \
@@ -2605,31 +2624,32 @@ static int ext4_mb_write_##name(struct file *file,              \
                return -ERANGE;                                 \
        sbi->s_mb_##name = value;                               \
        return cnt;                                             \
-}
+}                                                              \
+                                                               \
+static const struct file_operations ext4_mb_##name##_proc_fops = {     \
+       .owner          = THIS_MODULE,                          \
+       .open           = ext4_mb_##name##_proc_open,           \
+       .read           = seq_read,                             \
+       .llseek         = seq_lseek,                            \
+       .release        = single_release,                       \
+       .write          = ext4_mb_##name##_proc_write,          \
+};
 
-MB_PROC_VALUE_READ(stats);
-MB_PROC_VALUE_WRITE(stats);
-MB_PROC_VALUE_READ(max_to_scan);
-MB_PROC_VALUE_WRITE(max_to_scan);
-MB_PROC_VALUE_READ(min_to_scan);
-MB_PROC_VALUE_WRITE(min_to_scan);
-MB_PROC_VALUE_READ(order2_reqs);
-MB_PROC_VALUE_WRITE(order2_reqs);
-MB_PROC_VALUE_READ(stream_request);
-MB_PROC_VALUE_WRITE(stream_request);
-MB_PROC_VALUE_READ(group_prealloc);
-MB_PROC_VALUE_WRITE(group_prealloc);
+MB_PROC_FOPS(stats);
+MB_PROC_FOPS(max_to_scan);
+MB_PROC_FOPS(min_to_scan);
+MB_PROC_FOPS(order2_reqs);
+MB_PROC_FOPS(stream_request);
+MB_PROC_FOPS(group_prealloc);
 
 #define        MB_PROC_HANDLER(name, var)                                      \
 do {                                                                   \
-       proc = create_proc_entry(name, mode, sbi->s_mb_proc);           \
+       proc = proc_create_data(name, mode, sbi->s_mb_proc,             \
+                               &ext4_mb_##var##_proc_fops, sbi);       \
        if (proc == NULL) {                                             \
                printk(KERN_ERR "EXT4-fs: can't to create %s\n", name); \
                goto err_out;                                           \
        }                                                               \
-       proc->data = sbi;                                               \
-       proc->read_proc  = ext4_mb_read_##var ;                         \
-       proc->write_proc = ext4_mb_write_##var;                         \
 } while (0)
 
 static int ext4_mb_init_per_dev_proc(struct super_block *sb)
@@ -2639,6 +2659,10 @@ static int ext4_mb_init_per_dev_proc(struct super_block *sb)
        struct proc_dir_entry *proc;
        char devname[64];
 
+       if (proc_root_ext4 == NULL) {
+               sbi->s_mb_proc = NULL;
+               return -EINVAL;
+       }
        bdevname(sb->s_bdev, devname);
        sbi->s_mb_proc = proc_mkdir(devname, proc_root_ext4);
 
@@ -2747,7 +2771,7 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
 
 
        err = -EIO;
-       bitmap_bh = read_block_bitmap(sb, ac->ac_b_ex.fe_group);
+       bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
        if (!bitmap_bh)
                goto out_err;
 
@@ -3473,8 +3497,6 @@ ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
                if (bit >= end)
                        break;
                next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
-               if (next > end)
-                       next = end;
                start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
                                le32_to_cpu(sbi->s_es->s_first_data_block);
                mb_debug("    free preallocated %u/%u in group %u\n",
@@ -3569,7 +3591,7 @@ ext4_mb_discard_group_preallocations(struct super_block *sb,
        if (list_empty(&grp->bb_prealloc_list))
                return 0;
 
-       bitmap_bh = read_block_bitmap(sb, group);
+       bitmap_bh = ext4_read_block_bitmap(sb, group);
        if (bitmap_bh == NULL) {
                /* error handling here */
                ext4_mb_release_desc(&e4b);
@@ -3743,7 +3765,7 @@ repeat:
                err = ext4_mb_load_buddy(sb, group, &e4b);
                BUG_ON(err != 0); /* error handling here */
 
-               bitmap_bh = read_block_bitmap(sb, group);
+               bitmap_bh = ext4_read_block_bitmap(sb, group);
                if (bitmap_bh == NULL) {
                        /* error handling here */
                        ext4_mb_release_desc(&e4b);
@@ -4242,7 +4264,7 @@ do_more:
                overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
                count -= overflow;
        }
-       bitmap_bh = read_block_bitmap(sb, block_group);
+       bitmap_bh = ext4_read_block_bitmap(sb, block_group);
        if (!bitmap_bh)
                goto error_return;
        gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
@@ -4309,10 +4331,9 @@ do_more:
                ext4_mb_free_metadata(handle, &e4b, block_group, bit, count);
        } else {
                ext4_lock_group(sb, block_group);
-               err = mb_free_blocks(inode, &e4b, bit, count);
+               mb_free_blocks(inode, &e4b, bit, count);
                ext4_mb_return_to_preallocation(inode, &e4b, block, count);
                ext4_unlock_group(sb, block_group);
-               BUG_ON(err != 0);
        }
 
        spin_lock(sb_bgl_lock(sbi, block_group));