xfs: flush eof/cowblocks if we can't reserve quota for inode creation
authorDarrick J. Wong <djwong@kernel.org>
Sat, 23 Jan 2021 00:48:37 +0000 (16:48 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Wed, 3 Feb 2021 17:18:49 +0000 (09:18 -0800)
If an inode creation is unable to reserve enough quota to handle the
modification, try clearing whatever space the filesystem might have been
hanging onto in the hopes of speeding up the filesystem.  The flushing
behavior will become particularly important when we add deferred inode
inactivation because that will increase the amount of space that isn't
actively tied to user data.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Brian Foster <bfoster@redhat.com>
fs/xfs/xfs_icache.c
fs/xfs/xfs_icache.h
fs/xfs/xfs_trans.c

index 4a074aa12b52a3d733795f220c7b7b7270206419..df9533d6bc167fb416fb57b7655ed1556b9b2000 100644 (file)
@@ -1646,64 +1646,72 @@ xfs_start_block_reaping(
 }
 
 /*
- * Run cow/eofblocks scans on the quotas applicable to the inode. For inodes
- * with multiple quotas, we don't know exactly which quota caused an allocation
- * failure. We make a best effort by including each quota under low free space
- * conditions (less than 1% free space) in the scan.
+ * Run cow/eofblocks scans on the supplied dquots.  We don't know exactly which
+ * quota caused an allocation failure, so we make a best effort by including
+ * each quota under low free space conditions (less than 1% free space) in the
+ * scan.
  *
  * Callers must not hold any inode's ILOCK.  If requesting a synchronous scan
  * (XFS_EOF_FLAGS_SYNC), the caller also must not hold any inode's IOLOCK or
  * MMAPLOCK.
  */
 int
-xfs_blockgc_free_quota(
-       struct xfs_inode        *ip,
+xfs_blockgc_free_dquots(
+       struct xfs_mount        *mp,
+       struct xfs_dquot        *udqp,
+       struct xfs_dquot        *gdqp,
+       struct xfs_dquot        *pdqp,
        unsigned int            eof_flags)
 {
        struct xfs_eofblocks    eofb = {0};
-       struct xfs_dquot        *dq;
        bool                    do_work = false;
        int                     error;
 
+       if (!udqp && !gdqp && !pdqp)
+               return 0;
+
        /*
         * Run a scan to free blocks using the union filter to cover all
         * applicable quotas in a single scan.
         */
        eofb.eof_flags = XFS_EOF_FLAGS_UNION | eof_flags;
 
-       if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
-               dq = xfs_inode_dquot(ip, XFS_DQTYPE_USER);
-               if (dq && xfs_dquot_lowsp(dq)) {
-                       eofb.eof_uid = VFS_I(ip)->i_uid;
-                       eofb.eof_flags |= XFS_EOF_FLAGS_UID;
-                       do_work = true;
-               }
+       if (XFS_IS_UQUOTA_ENFORCED(mp) && udqp && xfs_dquot_lowsp(udqp)) {
+               eofb.eof_uid = make_kuid(mp->m_super->s_user_ns, udqp->q_id);
+               eofb.eof_flags |= XFS_EOF_FLAGS_UID;
+               do_work = true;
        }
 
-       if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
-               dq = xfs_inode_dquot(ip, XFS_DQTYPE_GROUP);
-               if (dq && xfs_dquot_lowsp(dq)) {
-                       eofb.eof_gid = VFS_I(ip)->i_gid;
-                       eofb.eof_flags |= XFS_EOF_FLAGS_GID;
-                       do_work = true;
-               }
+       if (XFS_IS_UQUOTA_ENFORCED(mp) && gdqp && xfs_dquot_lowsp(gdqp)) {
+               eofb.eof_gid = make_kgid(mp->m_super->s_user_ns, gdqp->q_id);
+               eofb.eof_flags |= XFS_EOF_FLAGS_GID;
+               do_work = true;
        }
 
-       if (XFS_IS_PQUOTA_ENFORCED(ip->i_mount)) {
-               dq = xfs_inode_dquot(ip, XFS_DQTYPE_PROJ);
-               if (dq && xfs_dquot_lowsp(dq)) {
-                       eofb.eof_prid = ip->i_d.di_projid;
-                       eofb.eof_flags |= XFS_EOF_FLAGS_PRID;
-                       do_work = true;
-               }
+       if (XFS_IS_PQUOTA_ENFORCED(mp) && pdqp && xfs_dquot_lowsp(pdqp)) {
+               eofb.eof_prid = pdqp->q_id;
+               eofb.eof_flags |= XFS_EOF_FLAGS_PRID;
+               do_work = true;
        }
 
        if (!do_work)
                return 0;
 
-       error = xfs_icache_free_eofblocks(ip->i_mount, &eofb);
+       error = xfs_icache_free_eofblocks(mp, &eofb);
        if (error)
                return error;
 
-       return xfs_icache_free_cowblocks(ip->i_mount, &eofb);
+       return xfs_icache_free_cowblocks(mp, &eofb);
+}
+
+/* Run cow/eofblocks scans on the quotas attached to the inode. */
+int
+xfs_blockgc_free_quota(
+       struct xfs_inode        *ip,
+       unsigned int            eof_flags)
+{
+       return xfs_blockgc_free_dquots(ip->i_mount,
+                       xfs_inode_dquot(ip, XFS_DQTYPE_USER),
+                       xfs_inode_dquot(ip, XFS_DQTYPE_GROUP),
+                       xfs_inode_dquot(ip, XFS_DQTYPE_PROJ), eof_flags);
 }
index d64ea8f5c58928d6b253958d9f18e95e766efbc0..5f7d7c192d1e40b8c285c44fbab0f62a21d44f77 100644 (file)
@@ -54,6 +54,9 @@ long xfs_reclaim_inodes_nr(struct xfs_mount *mp, int nr_to_scan);
 
 void xfs_inode_set_reclaim_tag(struct xfs_inode *ip);
 
+int xfs_blockgc_free_dquots(struct xfs_mount *mp, struct xfs_dquot *udqp,
+               struct xfs_dquot *gdqp, struct xfs_dquot *pdqp,
+               unsigned int eof_flags);
 int xfs_blockgc_free_quota(struct xfs_inode *ip, unsigned int eof_flags);
 
 void xfs_inode_set_eofblocks_tag(struct xfs_inode *ip);
index 4071bbed2d48cd6c7ad1daab5e24cba417ef0fdd..b294341990793ad49b41bc4d392eeaa19fd530ed 100644 (file)
@@ -1102,13 +1102,21 @@ xfs_trans_alloc_icreate(
        struct xfs_trans        **tpp)
 {
        struct xfs_trans        *tp;
+       bool                    retried = false;
        int                     error;
 
+retry:
        error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp);
        if (error)
                return error;
 
        error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks);
+       if ((error == -EDQUOT || error == -ENOSPC) && !retried) {
+               xfs_trans_cancel(tp);
+               xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0);
+               retried = true;
+               goto retry;
+       }
        if (error) {
                xfs_trans_cancel(tp);
                return error;