1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * Copyright (c) 2010 David Chinner.
5 * Copyright (c) 2011 Christoph Hellwig.
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_shared.h"
13 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_alloc.h"
16 #include "xfs_extent_busy.h"
17 #include "xfs_trace.h"
18 #include "xfs_trans.h"
23 xfs_extent_busy_insert(
25 struct xfs_perag *pag,
30 struct xfs_extent_busy *new;
31 struct xfs_extent_busy *busyp;
33 struct rb_node *parent = NULL;
35 new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0);
36 new->agno = pag->pag_agno;
39 INIT_LIST_HEAD(&new->list);
42 /* trace before insert to be able to see failed inserts */
43 trace_xfs_extent_busy(tp->t_mountp, pag->pag_agno, bno, len);
45 spin_lock(&pag->pagb_lock);
46 rbp = &pag->pagb_tree.rb_node;
49 busyp = rb_entry(parent, struct xfs_extent_busy, rb_node);
51 if (new->bno < busyp->bno) {
52 rbp = &(*rbp)->rb_left;
53 ASSERT(new->bno + new->length <= busyp->bno);
54 } else if (new->bno > busyp->bno) {
55 rbp = &(*rbp)->rb_right;
56 ASSERT(bno >= busyp->bno + busyp->length);
62 rb_link_node(&new->rb_node, parent, rbp);
63 rb_insert_color(&new->rb_node, &pag->pagb_tree);
65 list_add(&new->list, &tp->t_busy);
66 spin_unlock(&pag->pagb_lock);
70 * Search for a busy extent within the range of the extent we are about to
71 * allocate. You need to be holding the busy extent tree lock when calling
72 * xfs_extent_busy_search(). This function returns 0 for no overlapping busy
73 * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
74 * match. This is done so that a non-zero return indicates an overlap that
75 * will require a synchronous transaction, but it can still be
76 * used to distinguish between a partial or exact match.
79 xfs_extent_busy_search(
81 struct xfs_perag *pag,
86 struct xfs_extent_busy *busyp;
89 /* find closest start bno overlap */
90 spin_lock(&pag->pagb_lock);
91 rbp = pag->pagb_tree.rb_node;
93 busyp = rb_entry(rbp, struct xfs_extent_busy, rb_node);
94 if (bno < busyp->bno) {
95 /* may overlap, but exact start block is lower */
96 if (bno + len > busyp->bno)
99 } else if (bno > busyp->bno) {
100 /* may overlap, but exact start block is higher */
101 if (bno < busyp->bno + busyp->length)
105 /* bno matches busyp, length determines exact match */
106 match = (busyp->length == len) ? 1 : -1;
110 spin_unlock(&pag->pagb_lock);
115 * The found free extent [fbno, fend] overlaps part or all of the given busy
116 * extent. If the overlap covers the beginning, the end, or all of the busy
117 * extent, the overlapping portion can be made unbusy and used for the
118 * allocation. We can't split a busy extent because we can't modify a
119 * transaction/CIL context busy list, but we can update an entry's block
122 * Returns true if the extent can safely be reused, or false if the search
123 * needs to be restarted.
126 xfs_extent_busy_update_extent(
127 struct xfs_mount *mp,
128 struct xfs_perag *pag,
129 struct xfs_extent_busy *busyp,
132 bool userdata) __releases(&pag->pagb_lock)
133 __acquires(&pag->pagb_lock)
135 xfs_agblock_t fend = fbno + flen;
136 xfs_agblock_t bbno = busyp->bno;
137 xfs_agblock_t bend = bbno + busyp->length;
140 * This extent is currently being discarded. Give the thread
141 * performing the discard a chance to mark the extent unbusy
144 if (busyp->flags & XFS_EXTENT_BUSY_DISCARDED) {
145 spin_unlock(&pag->pagb_lock);
147 spin_lock(&pag->pagb_lock);
152 * If there is a busy extent overlapping a user allocation, we have
153 * no choice but to force the log and retry the search.
155 * Fortunately this does not happen during normal operation, but
156 * only if the filesystem is very low on space and has to dip into
157 * the AGFL for normal allocations.
162 if (bbno < fbno && bend > fend) {
166 * +BBBBBBBBBBBBBBBBB+
172 * We would have to split the busy extent to be able to track
173 * it correct, which we cannot do because we would have to
174 * modify the list of busy extents attached to the transaction
175 * or CIL context, which is immutable.
177 * Force out the log to clear the busy extent and retry the
181 } else if (bbno >= fbno && bend <= fend) {
185 * +BBBBBBBBBBBBBBBBB+
186 * +-----------------+
191 * +BBBBBBBBBBBBBBBBB+
192 * +--------------------------+
197 * +BBBBBBBBBBBBBBBBB+
198 * +--------------------------+
203 * +BBBBBBBBBBBBBBBBB+
204 * +-----------------------------------+
210 * The busy extent is fully covered by the extent we are
211 * allocating, and can simply be removed from the rbtree.
212 * However we cannot remove it from the immutable list
213 * tracking busy extents in the transaction or CIL context,
214 * so set the length to zero to mark it invalid.
216 * We also need to restart the busy extent search from the
217 * tree root, because erasing the node can rearrange the
220 rb_erase(&busyp->rb_node, &pag->pagb_tree);
223 } else if (fend < bend) {
227 * +BBBBBBBBBBBBBBBBB+
233 * +BBBBBBBBBBBBBBBBB+
234 * +------------------+
239 } else if (bbno < fbno) {
243 * +BBBBBBBBBBBBBBBBB+
249 * +BBBBBBBBBBBBBBBBB+
250 * +----------------------+
253 busyp->length = fbno - busyp->bno;
258 trace_xfs_extent_busy_reuse(mp, pag->pag_agno, fbno, flen);
262 spin_unlock(&pag->pagb_lock);
263 xfs_log_force(mp, XFS_LOG_SYNC);
264 trace_xfs_extent_busy_force(mp, pag->pag_agno, fbno, flen);
265 spin_lock(&pag->pagb_lock);
271 * For a given extent [fbno, flen], make sure we can reuse it safely.
274 xfs_extent_busy_reuse(
275 struct xfs_mount *mp,
276 struct xfs_perag *pag,
284 spin_lock(&pag->pagb_lock);
286 rbp = pag->pagb_tree.rb_node;
288 struct xfs_extent_busy *busyp =
289 rb_entry(rbp, struct xfs_extent_busy, rb_node);
290 xfs_agblock_t bbno = busyp->bno;
291 xfs_agblock_t bend = bbno + busyp->length;
293 if (fbno + flen <= bbno) {
296 } else if (fbno >= bend) {
301 if (!xfs_extent_busy_update_extent(mp, pag, busyp, fbno, flen,
305 spin_unlock(&pag->pagb_lock);
309 * For a given extent [fbno, flen], search the busy extent list to find a
310 * subset of the extent that is not busy. If *rlen is smaller than
311 * args->minlen no suitable extent could be found, and the higher level
312 * code needs to force out the log and retry the allocation.
314 * Return the current busy generation for the AG if the extent is busy. This
315 * value can be used to wait for at least one of the currently busy extents
316 * to be cleared. Note that the busy list is not guaranteed to be empty after
317 * the gen is woken. The state of a specific extent must always be confirmed
318 * with another call to xfs_extent_busy_trim() before it can be used.
321 xfs_extent_busy_trim(
322 struct xfs_alloc_arg *args,
334 spin_lock(&args->pag->pagb_lock);
337 rbp = args->pag->pagb_tree.rb_node;
338 while (rbp && flen >= args->minlen) {
339 struct xfs_extent_busy *busyp =
340 rb_entry(rbp, struct xfs_extent_busy, rb_node);
341 xfs_agblock_t fend = fbno + flen;
342 xfs_agblock_t bbno = busyp->bno;
343 xfs_agblock_t bend = bbno + busyp->length;
348 } else if (fbno >= bend) {
359 * +BBBBBBBBBBBBBBBBB+
365 * +BBBBBBBBBBBBBBBBB+
371 * +BBBBBBBBBBBBBBBBB+
377 * +BBBBBBBBBBBBBBBBB+
378 * +-----------------+
381 * No unbusy region in extent, return failure.
389 * +BBBBBBBBBBBBBBBBB+
390 * +----------------------+
395 * +BBBBBBBBBBBBBBBBB+
396 * +--------------------------+
399 * Needs to be trimmed to:
404 } else if (bend >= fend) {
410 * +BBBBBBBBBBBBBBBBB+
411 * +------------------+
416 * +BBBBBBBBBBBBBBBBB+
417 * +--------------------------+
420 * Needs to be trimmed to:
431 * +BBBBBBBBBBBBBBBBB+
432 * +-----------------------------------+
436 * +-------+ OR +-------+
437 * fbno fend fbno fend
439 * Backward allocation leads to significant
440 * fragmentation of directories, which degrades
441 * directory performance, therefore we always want to
442 * choose the option that produces forward allocation
444 * Preferring the lower bno extent will make the next
445 * request use "fend" as the start of the next
446 * allocation; if the segment is no longer busy at
447 * that point, we'll get a contiguous allocation, but
448 * even if it is still busy, we will get a forward
450 * We try to avoid choosing the segment at "bend",
451 * because that can lead to the next allocation
452 * taking the segment at "fbno", which would be a
453 * backward allocation. We only use the segment at
454 * "fbno" if it is much larger than the current
455 * requested size, because in that case there's a
456 * good chance subsequent allocations will be
459 if (bbno - fbno >= args->maxlen) {
460 /* left candidate fits perfect */
462 } else if (fend - bend >= args->maxlen * 4) {
463 /* right candidate has enough free space */
465 } else if (bbno - fbno >= args->minlen) {
466 /* left candidate fits minimum requirement */
477 if (fbno != *bno || flen != *len) {
478 trace_xfs_extent_busy_trim(args->mp, args->agno, *bno, *len,
482 *busy_gen = args->pag->pagb_gen;
485 spin_unlock(&args->pag->pagb_lock);
489 * Return a zero extent length as failure indications. All callers
490 * re-check if the trimmed extent satisfies the minlen requirement.
497 xfs_extent_busy_clear_one(
498 struct xfs_mount *mp,
499 struct xfs_perag *pag,
500 struct xfs_extent_busy *busyp)
503 trace_xfs_extent_busy_clear(mp, busyp->agno, busyp->bno,
505 rb_erase(&busyp->rb_node, &pag->pagb_tree);
508 list_del_init(&busyp->list);
513 xfs_extent_busy_put_pag(
514 struct xfs_perag *pag,
516 __releases(pag->pagb_lock)
520 wake_up_all(&pag->pagb_wait);
523 spin_unlock(&pag->pagb_lock);
528 * Remove all extents on the passed in list from the busy extents tree.
529 * If do_discard is set skip extents that need to be discarded, and mark
530 * these as undergoing a discard operation instead.
533 xfs_extent_busy_clear(
534 struct xfs_mount *mp,
535 struct list_head *list,
538 struct xfs_extent_busy *busyp, *n;
539 struct xfs_perag *pag = NULL;
540 xfs_agnumber_t agno = NULLAGNUMBER;
543 list_for_each_entry_safe(busyp, n, list, list) {
544 if (busyp->agno != agno) {
546 xfs_extent_busy_put_pag(pag, wakeup);
548 pag = xfs_perag_get(mp, agno);
549 spin_lock(&pag->pagb_lock);
553 if (do_discard && busyp->length &&
554 !(busyp->flags & XFS_EXTENT_BUSY_SKIP_DISCARD)) {
555 busyp->flags = XFS_EXTENT_BUSY_DISCARDED;
557 xfs_extent_busy_clear_one(mp, pag, busyp);
563 xfs_extent_busy_put_pag(pag, wakeup);
567 * Flush out all busy extents for this AG.
570 xfs_extent_busy_flush(
571 struct xfs_mount *mp,
572 struct xfs_perag *pag,
578 error = xfs_log_force(mp, XFS_LOG_SYNC);
583 prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
584 if (busy_gen != READ_ONCE(pag->pagb_gen))
589 finish_wait(&pag->pagb_wait, &wait);
593 xfs_extent_busy_wait_all(
594 struct xfs_mount *mp)
596 struct xfs_perag *pag;
600 for_each_perag(mp, agno, pag) {
602 prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
603 if (RB_EMPTY_ROOT(&pag->pagb_tree))
607 finish_wait(&pag->pagb_wait, &wait);
612 * Callback for list_sort to sort busy extents by the AG they reside in.
615 xfs_extent_busy_ag_cmp(
617 const struct list_head *l1,
618 const struct list_head *l2)
620 struct xfs_extent_busy *b1 =
621 container_of(l1, struct xfs_extent_busy, list);
622 struct xfs_extent_busy *b2 =
623 container_of(l2, struct xfs_extent_busy, list);
626 diff = b1->agno - b2->agno;
628 diff = b1->bno - b2->bno;