]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
xfs: implement busy extent tracking for rtgroups
authorDarrick J. Wong <djwong@kernel.org>
Mon, 4 Nov 2024 04:19:36 +0000 (20:19 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 5 Nov 2024 21:38:44 +0000 (13:38 -0800)
For rtgroups filesystems, track newly freed (rt) space through the log
until the rt EFIs have been committed to disk.  This way we ensure that
space cannot be reused until all traces of the old owner are gone.

As a fringe benefit, we now support -o discard on the realtime device.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/libxfs/xfs_rtbitmap.c
fs/xfs/libxfs/xfs_rtgroup.h
fs/xfs/xfs_extent_busy.c
fs/xfs/xfs_rtalloc.c
fs/xfs/xfs_trace.h

index 30220bf8c3f430bcb05b307800ad84e9a39454b1..4ddfb7e395b38af2d3ee8f9de109053183c8903a 100644 (file)
@@ -24,6 +24,7 @@
 #include "xfs_errortag.h"
 #include "xfs_log.h"
 #include "xfs_buf_item.h"
+#include "xfs_extent_busy.h"
 
 /*
  * Realtime allocator bitmap functions shared with userspace.
@@ -1120,6 +1121,7 @@ xfs_rtfree_blocks(
 {
        struct xfs_mount        *mp = tp->t_mountp;
        xfs_extlen_t            mod;
+       int                     error;
 
        ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
 
@@ -1135,8 +1137,16 @@ xfs_rtfree_blocks(
                return -EIO;
        }
 
-       return xfs_rtfree_extent(tp, rtg, xfs_rtb_to_rtx(mp, rtbno),
+       error = xfs_rtfree_extent(tp, rtg, xfs_rtb_to_rtx(mp, rtbno),
                        xfs_extlen_to_rtxlen(mp, rtlen));
+       if (error)
+               return error;
+
+       if (xfs_has_rtgroups(mp))
+               xfs_extent_busy_insert(tp, rtg_group(rtg),
+                               xfs_rtb_to_rgbno(mp, rtbno), rtlen, 0);
+
+       return 0;
 }
 
 /* Find all the free records within a given range. */
index 1e51dc62d1143ec82b0107382c95a49cdd903dfc..7e7e491ff06fa5bbe0dbbe8c437e23e866b1077d 100644 (file)
@@ -155,6 +155,19 @@ xfs_rtbno_is_group_start(
        return (rtbno & mp->m_groups[XG_TYPE_RTG].blkmask) == 0;
 }
 
+/* Convert an rtgroups rt extent number into an rgbno. */
+static inline xfs_rgblock_t
+xfs_rtx_to_rgbno(
+       struct xfs_rtgroup      *rtg,
+       xfs_rtxnum_t            rtx)
+{
+       struct xfs_mount        *mp = rtg_mount(rtg);
+
+       if (likely(mp->m_rtxblklog >= 0))
+               return rtx << mp->m_rtxblklog;
+       return rtx * mp->m_sb.sb_rextsize;
+}
+
 static inline xfs_daddr_t
 xfs_rtb_to_daddr(
        struct xfs_mount        *mp,
index 457a27ab837599b4c13f2dbbeaa4a0bfa40922bc..ea43c9a6e54c12bdb36a3614d8f2054aeceb7151 100644 (file)
@@ -18,6 +18,7 @@
 #include "xfs_trans.h"
 #include "xfs_log.h"
 #include "xfs_ag.h"
+#include "xfs_rtgroup.h"
 
 struct xfs_extent_busy_tree {
        spinlock_t              eb_lock;
@@ -665,9 +666,14 @@ xfs_extent_busy_wait_all(
        struct xfs_mount        *mp)
 {
        struct xfs_perag        *pag = NULL;
+       struct xfs_rtgroup      *rtg = NULL;
 
        while ((pag = xfs_perag_next(mp, pag)))
                xfs_extent_busy_wait_group(pag_group(pag));
+
+       if (xfs_has_rtgroups(mp))
+               while ((rtg = xfs_rtgroup_next(mp, rtg)))
+                       xfs_extent_busy_wait_group(rtg_group(rtg));
 }
 
 /*
index 8520d72afac0d9768e9a71a00b0a5710e1d55791..7ecea7623a151a1dbc619b2537e1a4058f83d869 100644 (file)
@@ -29,6 +29,7 @@
 #include "xfs_metafile.h"
 #include "xfs_rtgroup.h"
 #include "xfs_error.h"
+#include "xfs_trace.h"
 
 /*
  * Return whether there are any free extents in the size range given
@@ -1659,6 +1660,114 @@ xfs_rtalloc_align_minmax(
        *raminlen = newminlen;
 }
 
+/* Given a free extent, find any part of it that isn't busy, if possible. */
+STATIC bool
+xfs_rtalloc_check_busy(
+       struct xfs_rtalloc_args *args,
+       xfs_rtxnum_t            start,
+       xfs_rtxlen_t            minlen_rtx,
+       xfs_rtxlen_t            maxlen_rtx,
+       xfs_rtxlen_t            len_rtx,
+       xfs_rtxlen_t            prod,
+       xfs_rtxnum_t            rtx,
+       xfs_rtxlen_t            *reslen,
+       xfs_rtxnum_t            *resrtx,
+       unsigned                *busy_gen)
+{
+       struct xfs_rtgroup      *rtg = args->rtg;
+       struct xfs_mount        *mp = rtg_mount(rtg);
+       xfs_agblock_t           rgbno = xfs_rtx_to_rgbno(rtg, rtx);
+       xfs_rgblock_t           min_rgbno = xfs_rtx_to_rgbno(rtg, start);
+       xfs_extlen_t            minlen = xfs_rtxlen_to_extlen(mp, minlen_rtx);
+       xfs_extlen_t            len = xfs_rtxlen_to_extlen(mp, len_rtx);
+       xfs_extlen_t            diff;
+       bool                    busy;
+
+       busy = xfs_extent_busy_trim(rtg_group(rtg), minlen,
+                       xfs_rtxlen_to_extlen(mp, maxlen_rtx), &rgbno, &len,
+                       busy_gen);
+
+       /*
+        * If we have a largish extent that happens to start before min_rgbno,
+        * see if we can shift it into range...
+        */
+       if (rgbno < min_rgbno && rgbno + len > min_rgbno) {
+               diff = min_rgbno - rgbno;
+               if (len > diff) {
+                       rgbno += diff;
+                       len -= diff;
+               }
+       }
+
+       if (prod > 1 && len >= minlen) {
+               xfs_rgblock_t   aligned_rgbno = roundup(rgbno, prod);
+
+               diff = aligned_rgbno - rgbno;
+
+               *resrtx = xfs_rgbno_to_rtx(mp, aligned_rgbno);
+               *reslen = xfs_extlen_to_rtxlen(mp,
+                               diff >= len ? 0 : len - diff);
+       } else {
+               *resrtx = xfs_rgbno_to_rtx(mp, rgbno);
+               *reslen = xfs_extlen_to_rtxlen(mp, len);
+       }
+
+       return busy;
+}
+
+/*
+ * Adjust the given free extent so that it isn't busy, or flush the log and
+ * wait for the space to become unbusy.  Only needed for rtgroups.
+ */
+STATIC int
+xfs_rtallocate_adjust_for_busy(
+       struct xfs_rtalloc_args *args,
+       xfs_rtxnum_t            start,
+       xfs_rtxlen_t            minlen,
+       xfs_rtxlen_t            maxlen,
+       xfs_rtxlen_t            *len,
+       xfs_rtxlen_t            prod,
+       xfs_rtxnum_t            *rtx)
+{
+       xfs_rtxnum_t            resrtx;
+       xfs_rtxlen_t            reslen;
+       unsigned                busy_gen;
+       bool                    busy;
+       int                     error;
+
+again:
+       busy = xfs_rtalloc_check_busy(args, start, minlen, maxlen, *len, prod,
+                       *rtx, &reslen, &resrtx, &busy_gen);
+       if (!busy)
+               return 0;
+
+       if (reslen < minlen || (start != 0 && resrtx != *rtx)) {
+               /*
+                * Enough of the extent was busy that we cannot satisfy the
+                * allocation, or this is a near allocation and the start of
+                * the extent is busy.  Flush the log and wait for the busy
+                * situation to resolve.
+                */
+               trace_xfs_rtalloc_extent_busy(args->rtg, start, minlen, maxlen,
+                               *len, prod, *rtx, busy_gen);
+
+               error = xfs_extent_busy_flush(args->tp, rtg_group(args->rtg),
+                               busy_gen, 0);
+               if (error)
+                       return error;
+
+               goto again;
+       }
+
+       /* Some of the free space wasn't busy, hand that back to the caller. */
+       trace_xfs_rtalloc_extent_busy_trim(args->rtg, *rtx, *len, resrtx,
+                       reslen);
+       *len = reslen;
+       *rtx = resrtx;
+
+       return 0;
+}
+
 static int
 xfs_rtallocate_rtg(
        struct xfs_trans        *tp,
@@ -1740,15 +1849,19 @@ xfs_rtallocate_rtg(
        }
 
        if (error) {
-               if (xfs_has_rtgroups(args.mp)) {
-                       xfs_rtgroup_unlock(args.rtg, XFS_RTGLOCK_BITMAP);
-                       *rtlocked = false;
-               }
+               if (xfs_has_rtgroups(args.mp))
+                       goto out_unlock;
                goto out_release;
        }
 
-       if (xfs_has_rtgroups(args.mp))
+       if (xfs_has_rtgroups(args.mp)) {
+               error = xfs_rtallocate_adjust_for_busy(&args, start, minlen,
+                               maxlen, &len, prod, &rtx);
+               if (error)
+                       goto out_unlock;
+
                xfs_rtgroup_trans_join(tp, args.rtg, XFS_RTGLOCK_BITMAP);
+       }
 
        error = xfs_rtallocate_range(&args, rtx, len);
        if (error)
@@ -1764,6 +1877,10 @@ out_release:
        xfs_rtgroup_rele(args.rtg);
        xfs_rtbuf_cache_relse(&args);
        return error;
+out_unlock:
+       xfs_rtgroup_unlock(args.rtg, XFS_RTGLOCK_BITMAP);
+       *rtlocked = false;
+       goto out_release;
 }
 
 static int
index 5f7b461286ab671a3ac73b8e7757472608b9dec6..d81b5b69a6f3b865de7ded0f58e906208da93364 100644 (file)
@@ -97,6 +97,7 @@ struct xfs_extent_free_item;
 struct xfs_rmap_intent;
 struct xfs_refcount_intent;
 struct xfs_metadir_update;
+struct xfs_rtgroup;
 
 #define XFS_ATTR_FILTER_FLAGS \
        { XFS_ATTR_ROOT,        "ROOT" }, \
@@ -1744,6 +1745,80 @@ TRACE_EVENT(xfs_extent_busy_trim,
                  __entry->tlen)
 );
 
+#ifdef CONFIG_XFS_RT
+TRACE_EVENT(xfs_rtalloc_extent_busy,
+       TP_PROTO(struct xfs_rtgroup *rtg, xfs_rtxnum_t start,
+                xfs_rtxlen_t minlen, xfs_rtxlen_t maxlen,
+                xfs_rtxlen_t len, xfs_rtxlen_t prod, xfs_rtxnum_t rtx,
+                unsigned busy_gen),
+       TP_ARGS(rtg, start, minlen, maxlen, len, prod, rtx, busy_gen),
+       TP_STRUCT__entry(
+               __field(dev_t, dev)
+               __field(xfs_rgnumber_t, rgno)
+               __field(xfs_rtxnum_t, start)
+               __field(xfs_rtxlen_t, minlen)
+               __field(xfs_rtxlen_t, maxlen)
+               __field(xfs_rtxlen_t, mod)
+               __field(xfs_rtxlen_t, prod)
+               __field(xfs_rtxlen_t, len)
+               __field(xfs_rtxnum_t, rtx)
+               __field(unsigned, busy_gen)
+       ),
+       TP_fast_assign(
+               __entry->dev = rtg_mount(rtg)->m_super->s_dev;
+               __entry->rgno = rtg_rgno(rtg);
+               __entry->start = start;
+               __entry->minlen = minlen;
+               __entry->maxlen = maxlen;
+               __entry->prod = prod;
+               __entry->len = len;
+               __entry->rtx = rtx;
+               __entry->busy_gen = busy_gen;
+       ),
+       TP_printk("dev %d:%d rgno 0x%x startrtx 0x%llx minlen %u maxlen %u "
+                 "prod %u len %u rtx 0%llx busy_gen 0x%x",
+                 MAJOR(__entry->dev), MINOR(__entry->dev),
+                 __entry->rgno,
+                 __entry->start,
+                 __entry->minlen,
+                 __entry->maxlen,
+                 __entry->prod,
+                 __entry->len,
+                 __entry->rtx,
+                 __entry->busy_gen)
+)
+
+TRACE_EVENT(xfs_rtalloc_extent_busy_trim,
+       TP_PROTO(struct xfs_rtgroup *rtg, xfs_rtxnum_t old_rtx,
+                xfs_rtxlen_t old_len, xfs_rtxnum_t new_rtx,
+                xfs_rtxlen_t new_len),
+       TP_ARGS(rtg, old_rtx, old_len, new_rtx, new_len),
+       TP_STRUCT__entry(
+               __field(dev_t, dev)
+               __field(xfs_rgnumber_t, rgno)
+               __field(xfs_rtxnum_t, old_rtx)
+               __field(xfs_rtxnum_t, new_rtx)
+               __field(xfs_rtxlen_t, old_len)
+               __field(xfs_rtxlen_t, new_len)
+       ),
+       TP_fast_assign(
+               __entry->dev = rtg_mount(rtg)->m_super->s_dev;
+               __entry->rgno = rtg_rgno(rtg);
+               __entry->old_rtx = old_rtx;
+               __entry->old_len = old_len;
+               __entry->new_rtx = new_rtx;
+               __entry->new_len = new_len;
+       ),
+       TP_printk("dev %d:%d rgno 0x%x rtx 0x%llx rtxcount 0x%x -> rtx 0x%llx rtxcount 0x%x",
+                 MAJOR(__entry->dev), MINOR(__entry->dev),
+                 __entry->rgno,
+                 __entry->old_rtx,
+                 __entry->old_len,
+                 __entry->new_rtx,
+                 __entry->new_len)
+);
+#endif /* CONFIG_XFS_RT */
+
 DECLARE_EVENT_CLASS(xfs_agf_class,
        TP_PROTO(struct xfs_mount *mp, struct xfs_agf *agf, int flags,
                 unsigned long caller_ip),