ext2: Use lowercase names of quota functions
[cascardo/linux.git] / fs / ext4 / mballoc.c
index deba54f..4de4209 100644 (file)
@@ -1447,7 +1447,7 @@ static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
        struct ext4_free_extent *gex = &ac->ac_g_ex;
 
        BUG_ON(ex->fe_len <= 0);
-       BUG_ON(ex->fe_len >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
+       BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
        BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
        BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
 
@@ -3086,9 +3086,12 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
        if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
                /* release all the reserved blocks if non delalloc */
                percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
-       else
+       else {
                percpu_counter_sub(&sbi->s_dirtyblocks_counter,
                                                ac->ac_b_ex.fe_len);
+               /* convert reserved quota blocks to real quota blocks */
+               vfs_dq_claim_block(ac->ac_inode, ac->ac_b_ex.fe_len);
+       }
 
        if (sbi->s_log_groups_per_flex) {
                ext4_group_t flex_group = ext4_flex_group(sbi,
@@ -3292,7 +3295,7 @@ ext4_mb_normalize_request(struct ext4_allocation_context *ac,
        }
        BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
                        start > ac->ac_o_ex.fe_logical);
-       BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
+       BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
 
        /* now prepare goal request */
 
@@ -3589,6 +3592,7 @@ static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
                        struct super_block *sb, struct ext4_prealloc_space *pa)
 {
        ext4_group_t grp;
+       ext4_fsblk_t grp_blk;
 
        if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
                return;
@@ -3603,8 +3607,12 @@ static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
        pa->pa_deleted = 1;
        spin_unlock(&pa->pa_lock);
 
-       /* -1 is to protect from crossing allocation group */
-       ext4_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
+       grp_blk = pa->pa_pstart;
+       /* If linear, pa_pstart may be in the next group when pa is used up */
+       if (pa->pa_linear)
+               grp_blk--;
+
+       ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
 
        /*
         * possible race:
@@ -3693,6 +3701,8 @@ ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
        pa->pa_free = pa->pa_len;
        atomic_set(&pa->pa_count, 1);
        spin_lock_init(&pa->pa_lock);
+       INIT_LIST_HEAD(&pa->pa_inode_list);
+       INIT_LIST_HEAD(&pa->pa_group_list);
        pa->pa_deleted = 0;
        pa->pa_linear = 0;
 
@@ -3755,6 +3765,7 @@ ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
        atomic_set(&pa->pa_count, 1);
        spin_lock_init(&pa->pa_lock);
        INIT_LIST_HEAD(&pa->pa_inode_list);
+       INIT_LIST_HEAD(&pa->pa_group_list);
        pa->pa_deleted = 0;
        pa->pa_linear = 1;
 
@@ -4476,23 +4487,26 @@ static int ext4_mb_release_context(struct ext4_allocation_context *ac)
                        pa->pa_free -= ac->ac_b_ex.fe_len;
                        pa->pa_len -= ac->ac_b_ex.fe_len;
                        spin_unlock(&pa->pa_lock);
-                       /*
-                        * We want to add the pa to the right bucket.
-                        * Remove it from the list and while adding
-                        * make sure the list to which we are adding
-                        * doesn't grow big.
-                        */
-                       if (likely(pa->pa_free)) {
-                               spin_lock(pa->pa_obj_lock);
-                               list_del_rcu(&pa->pa_inode_list);
-                               spin_unlock(pa->pa_obj_lock);
-                               ext4_mb_add_n_trim(ac);
-                       }
                }
-               ext4_mb_put_pa(ac, ac->ac_sb, pa);
        }
        if (ac->alloc_semp)
                up_read(ac->alloc_semp);
+       if (pa) {
+               /*
+                * We want to add the pa to the right bucket.
+                * Remove it from the list and while adding
+                * make sure the list to which we are adding
+                * doesn't grow big.  We need to release
+                * alloc_semp before calling ext4_mb_add_n_trim()
+                */
+               if (pa->pa_linear && likely(pa->pa_free)) {
+                       spin_lock(pa->pa_obj_lock);
+                       list_del_rcu(&pa->pa_inode_list);
+                       spin_unlock(pa->pa_obj_lock);
+                       ext4_mb_add_n_trim(ac);
+               }
+               ext4_mb_put_pa(ac, ac->ac_sb, pa);
+       }
        if (ac->ac_bitmap_page)
                page_cache_release(ac->ac_bitmap_page);
        if (ac->ac_buddy_page)
@@ -4533,7 +4547,7 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
        struct ext4_sb_info *sbi;
        struct super_block *sb;
        ext4_fsblk_t block = 0;
-       unsigned int inquota;
+       unsigned int inquota = 0;
        unsigned int reserv_blks = 0;
 
        sb = ar->inode->i_sb;
@@ -4551,9 +4565,17 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
                   (unsigned long long) ar->pleft,
                   (unsigned long long) ar->pright);
 
-       if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag) {
-               /*
-                * With delalloc we already reserved the blocks
+       /*
+        * For delayed allocation, we could skip the ENOSPC and
+        * 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)
+               ar->flags |= EXT4_MB_DELALLOC_RESERVED;
+       else {
+               /* Without delayed allocation we need to verify
+                * there is enough free blocks to do block allocation
+                * and verify allocation doesn't exceed the quota limits.
                 */
                while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
                        /* let others to free the space */
@@ -4565,19 +4587,16 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
                        return 0;
                }
                reserv_blks = ar->len;
+               while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
+                       ar->flags |= EXT4_MB_HINT_NOPREALLOC;
+                       ar->len--;
+               }
+               inquota = ar->len;
+               if (ar->len == 0) {
+                       *errp = -EDQUOT;
+                       goto out3;
+               }
        }
-       while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
-               ar->flags |= EXT4_MB_HINT_NOPREALLOC;
-               ar->len--;
-       }
-       if (ar->len == 0) {
-               *errp = -EDQUOT;
-               goto out3;
-       }
-       inquota = ar->len;
-
-       if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
-               ar->flags |= EXT4_MB_DELALLOC_RESERVED;
 
        ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
        if (!ac) {
@@ -4643,7 +4662,7 @@ repeat:
 out2:
        kmem_cache_free(ext4_ac_cachep, ac);
 out1:
-       if (ar->len < inquota)
+       if (inquota && ar->len < inquota)
                DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
 out3:
        if (!ar->len) {