xfs: treat CoW fork operations as delalloc for quota accounting
authorDarrick J. Wong <darrick.wong@oracle.com>
Fri, 19 Jan 2018 17:05:48 +0000 (09:05 -0800)
committerDarrick J. Wong <darrick.wong@oracle.com>
Mon, 29 Jan 2018 15:27:23 +0000 (07:27 -0800)
Since the CoW fork only exists in memory, it is incorrect to update the
on-disk quota block counts when we modify the CoW fork.  Unlike the data
fork, even real extents in the CoW fork are only delalloc-style
reservations (on-disk they're owned by the refcountbt) so they must not
be tracked in the on disk quota info.  Ensure the i_delayed_blks
accounting reflects this too.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/libxfs/xfs_bmap.c
fs/xfs/xfs_reflink.c

index 4582f5547ae3eb7b0b9362253f86e3db26cd1754..cad21fd0c45d5b47780bdd540f4588614a4be265 100644 (file)
@@ -3343,8 +3343,35 @@ xfs_bmap_btalloc_accounting(
        struct xfs_bmalloca     *ap,
        struct xfs_alloc_arg    *args)
 {
-       if (!(ap->flags & XFS_BMAPI_COWFORK))
-               ap->ip->i_d.di_nblocks += args->len;
+       if (ap->flags & XFS_BMAPI_COWFORK) {
+               /*
+                * COW fork blocks are in-core only and thus are treated as
+                * in-core quota reservation (like delalloc blocks) even when
+                * converted to real blocks. The quota reservation is not
+                * accounted to disk until blocks are remapped to the data
+                * fork. So if these blocks were previously delalloc, we
+                * already have quota reservation and there's nothing to do
+                * yet.
+                */
+               if (ap->wasdel)
+                       return;
+
+               /*
+                * Otherwise, we've allocated blocks in a hole. The transaction
+                * has acquired in-core quota reservation for this extent.
+                * Rather than account these as real blocks, however, we reduce
+                * the transaction quota reservation based on the allocation.
+                * This essentially transfers the transaction quota reservation
+                * to that of a delalloc extent.
+                */
+               ap->ip->i_delayed_blks += args->len;
+               xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
+                               -(long)args->len);
+               return;
+       }
+
+       /* data/attr fork only */
+       ap->ip->i_d.di_nblocks += args->len;
        xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
        if (ap->wasdel)
                ap->ip->i_delayed_blks -= args->len;
@@ -4820,6 +4847,7 @@ xfs_bmap_del_extent_cow(
                xfs_iext_insert(ip, icur, &new, state);
                break;
        }
+       ip->i_delayed_blks -= del->br_blockcount;
 }
 
 /*
index 85a119e1463b83af3e61ff9689223ac2f872d3fe..c4f0aff86f96bec9fc9dfae6fe700b46e80d2bea 100644 (file)
@@ -599,10 +599,6 @@ xfs_reflink_cancel_cow_blocks(
                                        del.br_startblock, del.br_blockcount,
                                        NULL);
 
-                       /* Update quota accounting */
-                       xfs_trans_mod_dquot_byino(*tpp, ip, XFS_TRANS_DQ_BCOUNT,
-                                       -(long)del.br_blockcount);
-
                        /* Roll the transaction */
                        xfs_defer_ijoin(&dfops, ip);
                        error = xfs_defer_finish(tpp, &dfops);
@@ -613,6 +609,13 @@ xfs_reflink_cancel_cow_blocks(
 
                        /* Remove the mapping from the CoW fork. */
                        xfs_bmap_del_extent_cow(ip, &icur, &got, &del);
+
+                       /* Remove the quota reservation */
+                       error = xfs_trans_reserve_quota_nblks(NULL, ip,
+                                       -(long)del.br_blockcount, 0,
+                                       XFS_QMOPT_RES_REGBLKS);
+                       if (error)
+                               break;
                } else {
                        /* Didn't do anything, push cursor back. */
                        xfs_iext_prev(ifp, &icur);
@@ -795,6 +798,10 @@ xfs_reflink_end_cow(
                if (error)
                        goto out_defer;
 
+               /* Charge this new data fork mapping to the on-disk quota. */
+               xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_DELBCOUNT,
+                               (long)del.br_blockcount);
+
                /* Remove the mapping from the CoW fork. */
                xfs_bmap_del_extent_cow(ip, &icur, &got, &del);