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 busyp->length = bend - fend;
240 } else if (bbno < fbno) {
244 * +BBBBBBBBBBBBBBBBB+
250 * +BBBBBBBBBBBBBBBBB+
251 * +----------------------+
254 busyp->length = fbno - busyp->bno;
259 trace_xfs_extent_busy_reuse(mp, pag->pag_agno, fbno, flen);
263 spin_unlock(&pag->pagb_lock);
264 xfs_log_force(mp, XFS_LOG_SYNC);
265 trace_xfs_extent_busy_force(mp, pag->pag_agno, fbno, flen);
266 spin_lock(&pag->pagb_lock);
272 * For a given extent [fbno, flen], make sure we can reuse it safely.
275 xfs_extent_busy_reuse(
276 struct xfs_mount *mp,
277 struct xfs_perag *pag,
285 spin_lock(&pag->pagb_lock);
287 rbp = pag->pagb_tree.rb_node;
289 struct xfs_extent_busy *busyp =
290 rb_entry(rbp, struct xfs_extent_busy, rb_node);
291 xfs_agblock_t bbno = busyp->bno;
292 xfs_agblock_t bend = bbno + busyp->length;
294 if (fbno + flen <= bbno) {
297 } else if (fbno >= bend) {
302 if (!xfs_extent_busy_update_extent(mp, pag, busyp, fbno, flen,
306 spin_unlock(&pag->pagb_lock);
310 * For a given extent [fbno, flen], search the busy extent list to find a
311 * subset of the extent that is not busy. If *rlen is smaller than
312 * args->minlen no suitable extent could be found, and the higher level
313 * code needs to force out the log and retry the allocation.
315 * Return the current busy generation for the AG if the extent is busy. This
316 * value can be used to wait for at least one of the currently busy extents
317 * to be cleared. Note that the busy list is not guaranteed to be empty after
318 * the gen is woken. The state of a specific extent must always be confirmed
319 * with another call to xfs_extent_busy_trim() before it can be used.
322 xfs_extent_busy_trim(
323 struct xfs_alloc_arg *args,
335 spin_lock(&args->pag->pagb_lock);
338 rbp = args->pag->pagb_tree.rb_node;
339 while (rbp && flen >= args->minlen) {
340 struct xfs_extent_busy *busyp =
341 rb_entry(rbp, struct xfs_extent_busy, rb_node);
342 xfs_agblock_t fend = fbno + flen;
343 xfs_agblock_t bbno = busyp->bno;
344 xfs_agblock_t bend = bbno + busyp->length;
349 } else if (fbno >= bend) {
360 * +BBBBBBBBBBBBBBBBB+
366 * +BBBBBBBBBBBBBBBBB+
372 * +BBBBBBBBBBBBBBBBB+
378 * +BBBBBBBBBBBBBBBBB+
379 * +-----------------+
382 * No unbusy region in extent, return failure.
390 * +BBBBBBBBBBBBBBBBB+
391 * +----------------------+
396 * +BBBBBBBBBBBBBBBBB+
397 * +--------------------------+
400 * Needs to be trimmed to:
405 } else if (bend >= fend) {
411 * +BBBBBBBBBBBBBBBBB+
412 * +------------------+
417 * +BBBBBBBBBBBBBBBBB+
418 * +--------------------------+
421 * Needs to be trimmed to:
432 * +BBBBBBBBBBBBBBBBB+
433 * +-----------------------------------+
437 * +-------+ OR +-------+
438 * fbno fend fbno fend
440 * Backward allocation leads to significant
441 * fragmentation of directories, which degrades
442 * directory performance, therefore we always want to
443 * choose the option that produces forward allocation
445 * Preferring the lower bno extent will make the next
446 * request use "fend" as the start of the next
447 * allocation; if the segment is no longer busy at
448 * that point, we'll get a contiguous allocation, but
449 * even if it is still busy, we will get a forward
451 * We try to avoid choosing the segment at "bend",
452 * because that can lead to the next allocation
453 * taking the segment at "fbno", which would be a
454 * backward allocation. We only use the segment at
455 * "fbno" if it is much larger than the current
456 * requested size, because in that case there's a
457 * good chance subsequent allocations will be
460 if (bbno - fbno >= args->maxlen) {
461 /* left candidate fits perfect */
463 } else if (fend - bend >= args->maxlen * 4) {
464 /* right candidate has enough free space */
466 } else if (bbno - fbno >= args->minlen) {
467 /* left candidate fits minimum requirement */
478 if (fbno != *bno || flen != *len) {
479 trace_xfs_extent_busy_trim(args->mp, args->agno, *bno, *len,
483 *busy_gen = args->pag->pagb_gen;
486 spin_unlock(&args->pag->pagb_lock);
490 * Return a zero extent length as failure indications. All callers
491 * re-check if the trimmed extent satisfies the minlen requirement.
498 xfs_extent_busy_clear_one(
499 struct xfs_mount *mp,
500 struct xfs_perag *pag,
501 struct xfs_extent_busy *busyp)
504 trace_xfs_extent_busy_clear(mp, busyp->agno, busyp->bno,
506 rb_erase(&busyp->rb_node, &pag->pagb_tree);
509 list_del_init(&busyp->list);
514 xfs_extent_busy_put_pag(
515 struct xfs_perag *pag,
517 __releases(pag->pagb_lock)
521 wake_up_all(&pag->pagb_wait);
524 spin_unlock(&pag->pagb_lock);
529 * Remove all extents on the passed in list from the busy extents tree.
530 * If do_discard is set skip extents that need to be discarded, and mark
531 * these as undergoing a discard operation instead.
534 xfs_extent_busy_clear(
535 struct xfs_mount *mp,
536 struct list_head *list,
539 struct xfs_extent_busy *busyp, *n;
540 struct xfs_perag *pag = NULL;
541 xfs_agnumber_t agno = NULLAGNUMBER;
544 list_for_each_entry_safe(busyp, n, list, list) {
545 if (busyp->agno != agno) {
547 xfs_extent_busy_put_pag(pag, wakeup);
549 pag = xfs_perag_get(mp, agno);
550 spin_lock(&pag->pagb_lock);
554 if (do_discard && busyp->length &&
555 !(busyp->flags & XFS_EXTENT_BUSY_SKIP_DISCARD)) {
556 busyp->flags = XFS_EXTENT_BUSY_DISCARDED;
558 xfs_extent_busy_clear_one(mp, pag, busyp);
564 xfs_extent_busy_put_pag(pag, wakeup);
568 * Flush out all busy extents for this AG.
570 * If the current transaction is holding busy extents, the caller may not want
571 * to wait for committed busy extents to resolve. If we are being told just to
572 * try a flush or progress has been made since we last skipped a busy extent,
573 * return immediately to allow the caller to try again.
575 * If we are freeing extents, we might actually be holding the only free extents
576 * in the transaction busy list and the log force won't resolve that situation.
577 * In this case, we must return -EAGAIN to avoid a deadlock by informing the
578 * caller it needs to commit the busy extents it holds before retrying the
579 * extent free operation.
582 xfs_extent_busy_flush(
583 struct xfs_trans *tp,
584 struct xfs_perag *pag,
586 uint32_t alloc_flags)
591 error = xfs_log_force(tp->t_mountp, XFS_LOG_SYNC);
595 /* Avoid deadlocks on uncommitted busy extents. */
596 if (!list_empty(&tp->t_busy)) {
597 if (alloc_flags & XFS_ALLOC_FLAG_TRYFLUSH)
600 if (busy_gen != READ_ONCE(pag->pagb_gen))
603 if (alloc_flags & XFS_ALLOC_FLAG_FREEING)
607 /* Wait for committed busy extents to resolve. */
609 prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
610 if (busy_gen != READ_ONCE(pag->pagb_gen))
615 finish_wait(&pag->pagb_wait, &wait);
620 xfs_extent_busy_wait_all(
621 struct xfs_mount *mp)
623 struct xfs_perag *pag;
627 for_each_perag(mp, agno, pag) {
629 prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
630 if (RB_EMPTY_ROOT(&pag->pagb_tree))
634 finish_wait(&pag->pagb_wait, &wait);
639 * Callback for list_sort to sort busy extents by the AG they reside in.
642 xfs_extent_busy_ag_cmp(
644 const struct list_head *l1,
645 const struct list_head *l2)
647 struct xfs_extent_busy *b1 =
648 container_of(l1, struct xfs_extent_busy, list);
649 struct xfs_extent_busy *b2 =
650 container_of(l2, struct xfs_extent_busy, list);
653 diff = b1->agno - b2->agno;
655 diff = b1->bno - b2->bno;