ext4: fix trimming starting with block 0 with small blocksize
[pandora-kernel.git] / fs / ext4 / mballoc.c
index d47a80e..851f49b 100644 (file)
@@ -3881,19 +3881,6 @@ repeat:
        }
 }
 
-/*
- * finds all preallocated spaces and return blocks being freed to them
- * if preallocated space becomes full (no block is used from the space)
- * then the function frees space in buddy
- * XXX: at the moment, truncate (which is the only way to free blocks)
- * discards all preallocations
- */
-static void ext4_mb_return_to_preallocation(struct inode *inode,
-                                       struct ext4_buddy *e4b,
-                                       sector_t block, int count)
-{
-       BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
-}
 #ifdef CONFIG_EXT4_DEBUG
 static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
 {
@@ -4648,7 +4635,6 @@ do_more:
                ext4_lock_group(sb, block_group);
                mb_clear_bits(bitmap_bh->b_data, bit, count);
                mb_free_blocks(inode, &e4b, bit, count);
-               ext4_mb_return_to_preallocation(inode, &e4b, block, count);
        }
 
        ret = ext4_free_blks_count(sb, gdp) + count;
@@ -4818,11 +4804,12 @@ ext4_grpblk_t ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
 int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 {
        struct ext4_buddy e4b;
-       ext4_fsblk_t blocks_count = ext4_blocks_count(EXT4_SB(sb)->s_es);
        ext4_group_t first_group, last_group;
        ext4_group_t group, ngroups = ext4_get_groups_count(sb);
        ext4_grpblk_t cnt = 0, first_block, last_block;
        uint64_t start, len, minlen, trimmed;
+       ext4_fsblk_t first_data_blk =
+                       le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
        int ret = 0;
 
        start = range->start >> sb->s_blocksize_bits;
@@ -4830,13 +4817,12 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
        minlen = range->minlen >> sb->s_blocksize_bits;
        trimmed = 0;
 
-       if (start >= blocks_count)
-               return -EINVAL;
-       if (start + len > blocks_count)
-               len = blocks_count - start;
-
        if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
                return -EINVAL;
+       if (start < first_data_blk) {
+               len -= first_data_blk - start;
+               start = first_data_blk;
+       }
 
        /* Determine first and last group to examine based on start and len */
        ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
@@ -4860,7 +4846,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
                if (len >= EXT4_BLOCKS_PER_GROUP(sb))
                        len -= (EXT4_BLOCKS_PER_GROUP(sb) - first_block);
                else
-                       last_block = len;
+                       last_block = first_block + len;
 
                if (e4b.bd_info->bb_free >= minlen) {
                        cnt = ext4_trim_all_free(sb, &e4b, first_block,