^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2010 David Chinner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2011 Christoph Hellwig.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "xfs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "xfs_alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "xfs_extent_busy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "xfs_log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) xfs_extent_busy_insert(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) xfs_agnumber_t agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) xfs_agblock_t bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) xfs_extlen_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct xfs_extent_busy *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct xfs_extent_busy *busyp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct rb_node **rbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) new->agno = agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) new->bno = bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) new->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) INIT_LIST_HEAD(&new->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) new->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* trace before insert to be able to see failed inserts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) trace_xfs_extent_busy(tp->t_mountp, agno, bno, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pag = xfs_perag_get(tp->t_mountp, new->agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) spin_lock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) rbp = &pag->pagb_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) while (*rbp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) parent = *rbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) busyp = rb_entry(parent, struct xfs_extent_busy, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (new->bno < busyp->bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) rbp = &(*rbp)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ASSERT(new->bno + new->length <= busyp->bno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) } else if (new->bno > busyp->bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rbp = &(*rbp)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ASSERT(bno >= busyp->bno + busyp->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) rb_link_node(&new->rb_node, parent, rbp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) rb_insert_color(&new->rb_node, &pag->pagb_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) list_add(&new->list, &tp->t_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) spin_unlock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Search for a busy extent within the range of the extent we are about to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * allocate. You need to be holding the busy extent tree lock when calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * xfs_extent_busy_search(). This function returns 0 for no overlapping busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * match. This is done so that a non-zero return indicates an overlap that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * will require a synchronous transaction, but it can still be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * used to distinguish between a partial or exact match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) xfs_extent_busy_search(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) xfs_agnumber_t agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) xfs_agblock_t bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) xfs_extlen_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct rb_node *rbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct xfs_extent_busy *busyp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int match = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) pag = xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) spin_lock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) rbp = pag->pagb_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* find closest start bno overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) while (rbp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) busyp = rb_entry(rbp, struct xfs_extent_busy, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (bno < busyp->bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* may overlap, but exact start block is lower */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (bno + len > busyp->bno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) match = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) rbp = rbp->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) } else if (bno > busyp->bno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* may overlap, but exact start block is higher */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (bno < busyp->bno + busyp->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) match = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) rbp = rbp->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* bno matches busyp, length determines exact match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) match = (busyp->length == len) ? 1 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) spin_unlock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * The found free extent [fbno, fend] overlaps part or all of the given busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * extent. If the overlap covers the beginning, the end, or all of the busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * extent, the overlapping portion can be made unbusy and used for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * allocation. We can't split a busy extent because we can't modify a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * transaction/CIL context busy list, but we can update an entry's block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * number or length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * Returns true if the extent can safely be reused, or false if the search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * needs to be restarted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) STATIC bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) xfs_extent_busy_update_extent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct xfs_extent_busy *busyp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) xfs_agblock_t fbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) xfs_extlen_t flen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) bool userdata) __releases(&pag->pagb_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) __acquires(&pag->pagb_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) xfs_agblock_t fend = fbno + flen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) xfs_agblock_t bbno = busyp->bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) xfs_agblock_t bend = bbno + busyp->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * This extent is currently being discarded. Give the thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * performing the discard a chance to mark the extent unbusy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * and retry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (busyp->flags & XFS_EXTENT_BUSY_DISCARDED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) spin_unlock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) delay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) spin_lock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * If there is a busy extent overlapping a user allocation, we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * no choice but to force the log and retry the search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Fortunately this does not happen during normal operation, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * only if the filesystem is very low on space and has to dip into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * the AGFL for normal allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (userdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) goto out_force_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (bbno < fbno && bend > fend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * Case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * +---------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * We would have to split the busy extent to be able to track
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * it correct, which we cannot do because we would have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * modify the list of busy extents attached to the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * or CIL context, which is immutable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Force out the log to clear the busy extent and retry the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto out_force_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) } else if (bbno >= fbno && bend <= fend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * Case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * +-----------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * Case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * +--------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * Case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * +--------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * Case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * +-----------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * The busy extent is fully covered by the extent we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * allocating, and can simply be removed from the rbtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * However we cannot remove it from the immutable list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * tracking busy extents in the transaction or CIL context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * so set the length to zero to mark it invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * We also need to restart the busy extent search from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * tree root, because erasing the node can rearrange the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * tree topology.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) rb_erase(&busyp->rb_node, &pag->pagb_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) busyp->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) } else if (fend < bend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * Case 6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * +---------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Case 7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * +------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) busyp->bno = fend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) } else if (bbno < fbno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * +-------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * Case 9:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) busyp->length = fbno - busyp->bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) trace_xfs_extent_busy_reuse(mp, pag->pag_agno, fbno, flen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) out_force_log:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) spin_unlock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) xfs_log_force(mp, XFS_LOG_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) trace_xfs_extent_busy_force(mp, pag->pag_agno, fbno, flen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) spin_lock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * For a given extent [fbno, flen], make sure we can reuse it safely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) xfs_extent_busy_reuse(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) xfs_agnumber_t agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) xfs_agblock_t fbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) xfs_extlen_t flen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) bool userdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct rb_node *rbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ASSERT(flen > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) pag = xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) spin_lock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) rbp = pag->pagb_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) while (rbp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct xfs_extent_busy *busyp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) rb_entry(rbp, struct xfs_extent_busy, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) xfs_agblock_t bbno = busyp->bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) xfs_agblock_t bend = bbno + busyp->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (fbno + flen <= bbno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) rbp = rbp->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) } else if (fbno >= bend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) rbp = rbp->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!xfs_extent_busy_update_extent(mp, pag, busyp, fbno, flen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) userdata))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) spin_unlock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * For a given extent [fbno, flen], search the busy extent list to find a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * subset of the extent that is not busy. If *rlen is smaller than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * args->minlen no suitable extent could be found, and the higher level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * code needs to force out the log and retry the allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * Return the current busy generation for the AG if the extent is busy. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * value can be used to wait for at least one of the currently busy extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * to be cleared. Note that the busy list is not guaranteed to be empty after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * the gen is woken. The state of a specific extent must always be confirmed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * with another call to xfs_extent_busy_trim() before it can be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) xfs_extent_busy_trim(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct xfs_alloc_arg *args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) xfs_agblock_t *bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) xfs_extlen_t *len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) unsigned *busy_gen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) xfs_agblock_t fbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) xfs_extlen_t flen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct rb_node *rbp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) ASSERT(*len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) spin_lock(&args->pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) fbno = *bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) flen = *len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) rbp = args->pag->pagb_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) while (rbp && flen >= args->minlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct xfs_extent_busy *busyp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) rb_entry(rbp, struct xfs_extent_busy, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) xfs_agblock_t fend = fbno + flen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) xfs_agblock_t bbno = busyp->bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) xfs_agblock_t bend = bbno + busyp->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (fend <= bbno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) rbp = rbp->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) } else if (fbno >= bend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) rbp = rbp->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * If this is a metadata allocation, try to reuse the busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * extent instead of trimming the allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!(args->datatype & XFS_ALLOC_USERDATA) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) !(busyp->flags & XFS_EXTENT_BUSY_DISCARDED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (!xfs_extent_busy_update_extent(args->mp, args->pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) busyp, fbno, flen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (bbno <= fbno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* start overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * Case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * +---------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * Case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * +-------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * Case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * +-------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * Case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * +-----------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * No unbusy region in extent, return failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (fend <= bend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * Case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * +----------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * Case 6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * +--------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * Needs to be trimmed to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * +-------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) fbno = bend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) } else if (bend >= fend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /* end overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * Case 7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * +------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * Case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * +--------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * Needs to be trimmed to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * +-------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) fend = bbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* middle overlap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * Case 9:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * bbno bend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * +BBBBBBBBBBBBBBBBB+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * +-----------------------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * Can be trimmed to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * +-------+ OR +-------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * fbno fend fbno fend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * Backward allocation leads to significant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * fragmentation of directories, which degrades
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * directory performance, therefore we always want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * choose the option that produces forward allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * patterns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * Preferring the lower bno extent will make the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * request use "fend" as the start of the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * allocation; if the segment is no longer busy at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * that point, we'll get a contiguous allocation, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * even if it is still busy, we will get a forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * We try to avoid choosing the segment at "bend",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * because that can lead to the next allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * taking the segment at "fbno", which would be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * backward allocation. We only use the segment at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * "fbno" if it is much larger than the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * requested size, because in that case there's a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * good chance subsequent allocations will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (bbno - fbno >= args->maxlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /* left candidate fits perfect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) fend = bbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) } else if (fend - bend >= args->maxlen * 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /* right candidate has enough free space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) fbno = bend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) } else if (bbno - fbno >= args->minlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /* left candidate fits minimum requirement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) fend = bbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) flen = fend - fbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (fbno != *bno || flen != *len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) trace_xfs_extent_busy_trim(args->mp, args->agno, *bno, *len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) fbno, flen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) *bno = fbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) *len = flen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) *busy_gen = args->pag->pagb_gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) spin_unlock(&args->pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Return a zero extent length as failure indications. All callers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * re-check if the trimmed extent satisfies the minlen requirement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) flen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) xfs_extent_busy_clear_one(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct xfs_extent_busy *busyp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (busyp->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) trace_xfs_extent_busy_clear(mp, busyp->agno, busyp->bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) busyp->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) rb_erase(&busyp->rb_node, &pag->pagb_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) list_del_init(&busyp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) kmem_free(busyp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) xfs_extent_busy_put_pag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) bool wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) __releases(pag->pagb_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (wakeup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) pag->pagb_gen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) wake_up_all(&pag->pagb_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) spin_unlock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * Remove all extents on the passed in list from the busy extents tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * If do_discard is set skip extents that need to be discarded, and mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * these as undergoing a discard operation instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) xfs_extent_busy_clear(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) struct list_head *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) bool do_discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct xfs_extent_busy *busyp, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct xfs_perag *pag = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) xfs_agnumber_t agno = NULLAGNUMBER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) bool wakeup = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) list_for_each_entry_safe(busyp, n, list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (busyp->agno != agno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) xfs_extent_busy_put_pag(pag, wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) agno = busyp->agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) pag = xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) spin_lock(&pag->pagb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) wakeup = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (do_discard && busyp->length &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) !(busyp->flags & XFS_EXTENT_BUSY_SKIP_DISCARD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) busyp->flags = XFS_EXTENT_BUSY_DISCARDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) xfs_extent_busy_clear_one(mp, pag, busyp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) wakeup = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) if (pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) xfs_extent_busy_put_pag(pag, wakeup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * Flush out all busy extents for this AG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) xfs_extent_busy_flush(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) unsigned busy_gen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) DEFINE_WAIT (wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) error = xfs_log_force(mp, XFS_LOG_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (busy_gen != READ_ONCE(pag->pagb_gen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) } while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) finish_wait(&pag->pagb_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) xfs_extent_busy_wait_all(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) DEFINE_WAIT (wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) xfs_agnumber_t agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) struct xfs_perag *pag = xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (RB_EMPTY_ROOT(&pag->pagb_tree))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) } while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) finish_wait(&pag->pagb_wait, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * Callback for list_sort to sort busy extents by the AG they reside in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) xfs_extent_busy_ag_cmp(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) struct list_head *l1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) struct list_head *l2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct xfs_extent_busy *b1 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) container_of(l1, struct xfs_extent_busy, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) struct xfs_extent_busy *b2 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) container_of(l2, struct xfs_extent_busy, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) s32 diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) diff = b1->agno - b2->agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (!diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) diff = b1->bno - b2->bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }