Ext2: free memory allocated and forget buffer head when io error happens
authorWang Shilong <wangsl-fnst@cn.fujitsu.com>
Sat, 12 Jan 2013 09:19:32 +0000 (01:19 -0800)
committerJan Kara <jack@suse.cz>
Mon, 21 Jan 2013 10:19:56 +0000 (11:19 +0100)
Add a necessary check when an io error happens.
If io error happens,free the memory allocated and forget
buffer head.

Signed-off-by: Wang Shilong <wangsl-fnst@cn.fujitsu.com>
Signed-off-by: Jan Kara <jack@suse.cz>
fs/ext2/inode.c

index 6363ac6..c3881e5 100644 (file)
@@ -495,6 +495,10 @@ static int ext2_alloc_branch(struct inode *inode,
                 * parent to disk.
                 */
                bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
+               if (unlikely(!bh)) {
+                       err = -ENOMEM;
+                       goto failed;
+               }
                branch[n].bh = bh;
                lock_buffer(bh);
                memset(bh->b_data, 0, blocksize);
@@ -523,6 +527,14 @@ static int ext2_alloc_branch(struct inode *inode,
        }
        *blks = num;
        return err;
+
+failed:
+       for (i = 1; i < n; i++)
+               bforget(branch[i].bh);
+       for (i = 0; i < indirect_blks; i++)
+               ext2_free_blocks(inode, new_blocks[i], 1);
+       ext2_free_blocks(inode, new_blocks[i], num);
+       return err;
 }
 
 /**