ext4: replace i_delalloc_reserved_flag with EXT4_STATE_DELALLOC_RESERVED
[cascardo/linux.git] / fs / ext4 / mballoc.c
index 5b4d4e3..d47a80e 100644 (file)
@@ -2608,18 +2608,12 @@ int ext4_mb_release(struct super_block *sb)
 static inline int ext4_issue_discard(struct super_block *sb,
                ext4_group_t block_group, ext4_grpblk_t block, int count)
 {
-       int ret;
        ext4_fsblk_t discard_block;
 
        discard_block = block + ext4_group_first_block_no(sb, block_group);
        trace_ext4_discard_blocks(sb,
                        (unsigned long long) discard_block, count);
-       ret = sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
-       if (ret == -EOPNOTSUPP) {
-               ext4_warning(sb, "discard not supported, disabling");
-               clear_opt(EXT4_SB(sb)->s_mount_opt, DISCARD);
-       }
-       return ret;
+       return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
 }
 
 /*
@@ -2631,7 +2625,7 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
        struct super_block *sb = journal->j_private;
        struct ext4_buddy e4b;
        struct ext4_group_info *db;
-       int err, count = 0, count2 = 0;
+       int err, ret, count = 0, count2 = 0;
        struct ext4_free_data *entry;
        struct list_head *l, *ltmp;
 
@@ -2641,9 +2635,15 @@ static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
                mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
                         entry->count, entry->group, entry);
 
-               if (test_opt(sb, DISCARD))
-                       ext4_issue_discard(sb, entry->group,
+               if (test_opt(sb, DISCARD)) {
+                       ret = ext4_issue_discard(sb, entry->group,
                                        entry->start_blk, entry->count);
+                       if (unlikely(ret == -EOPNOTSUPP)) {
+                               ext4_warning(sb, "discard not supported, "
+                                                "disabling");
+                               clear_opt(sb, DISCARD);
+                       }
+               }
 
                err = ext4_mb_load_buddy(sb, entry->group, &e4b);
                /* we expect to find existing buddy because it's pinned */
@@ -4283,7 +4283,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
         * EDQUOT check, as blocks and quotas have been already
         * reserved when data being copied into pagecache.
         */
-       if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
+       if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
                ar->flags |= EXT4_MB_DELALLOC_RESERVED;
        else {
                /* Without delayed allocation we need to verify
@@ -4380,7 +4380,8 @@ out:
        if (inquota && ar->len < inquota)
                dquot_free_block(ar->inode, inquota - ar->len);
        if (!ar->len) {
-               if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
+               if (!ext4_test_inode_state(ar->inode,
+                                          EXT4_STATE_DELALLOC_RESERVED))
                        /* release all the reserved blocks if non delalloc */
                        percpu_counter_sub(&sbi->s_dirtyblocks_counter,
                                                reserv_blks);
@@ -4626,7 +4627,11 @@ do_more:
                 * blocks being freed are metadata. these blocks shouldn't
                 * be used until this transaction is committed
                 */
-               new_entry  = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
+               new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
+               if (!new_entry) {
+                       err = -ENOMEM;
+                       goto error_return;
+               }
                new_entry->start_blk = bit;
                new_entry->group  = block_group;
                new_entry->count = count;
@@ -4718,8 +4723,6 @@ static int ext4_trim_extent(struct super_block *sb, int start, int count,
        ext4_unlock_group(sb, group);
 
        ret = ext4_issue_discard(sb, group, start, count);
-       if (ret)
-               ext4_std_error(sb, ret);
 
        ext4_lock_group(sb, group);
        mb_free_blocks(NULL, e4b, start, ex.fe_len);
@@ -4815,6 +4818,7 @@ 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;
@@ -4826,6 +4830,11 @@ 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;