xfs: attach dquots and reserve quota blocks during unwritten conversion
authorDarrick J. Wong <darrick.wong@oracle.com>
Sat, 9 Nov 2019 07:04:20 +0000 (23:04 -0800)
committerDarrick J. Wong <darrick.wong@oracle.com>
Mon, 11 Nov 2019 20:42:52 +0000 (12:42 -0800)
In xfs_iomap_write_unwritten, we need to ensure that dquots are attached
to the inode and quota blocks reserved so that we capture in the quota
counters any blocks allocated to handle a bmbt split.  This can happen
on the first unwritten extent conversion to a preallocated sparse file
on a fresh mount.

This was found by running generic/311 with quotas enabled.  The bug
seems to have been introduced in "[XFS] rework iocore infrastructure,
remove some code and make it more" from ~2002?

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

index 1471bcd6cb70d00ede0ad42d2445f325bfd2b35b..d1960ff0a396cb5f2cdf8a780f2cce3b838b562c 100644 (file)
@@ -539,6 +539,11 @@ xfs_iomap_write_unwritten(
         */
        resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
 
+       /* Attach dquots so that bmbt splits are accounted correctly. */
+       error = xfs_qm_dqattach(ip);
+       if (error)
+               return error;
+
        do {
                /*
                 * Set up a transaction to convert the range of extents
@@ -557,6 +562,11 @@ xfs_iomap_write_unwritten(
                xfs_ilock(ip, XFS_ILOCK_EXCL);
                xfs_trans_ijoin(tp, ip, 0);
 
+               error = xfs_trans_reserve_quota_nblks(tp, ip, resblks, 0,
+                               XFS_QMOPT_RES_REGBLKS);
+               if (error)
+                       goto error_on_bmapi_transaction;
+
                /*
                 * Modify the unwritten extent state of the buffer.
                 */