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_list(
24 struct xfs_perag *pag,
28 struct list_head *busy_list)
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(pag->pag_mount, 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, busy_list);
66 spin_unlock(&pag->pagb_lock);
70 xfs_extent_busy_insert(
72 struct xfs_perag *pag,
77 xfs_extent_busy_insert_list(pag, bno, len, flags, &tp->t_busy);
81 xfs_extent_busy_insert_discard(
82 struct xfs_perag *pag,
85 struct list_head *busy_list)
87 xfs_extent_busy_insert_list(pag, bno, len, XFS_EXTENT_BUSY_DISCARDED,
92 * Search for a busy extent within the range of the extent we are about to
93 * allocate. You need to be holding the busy extent tree lock when calling
94 * xfs_extent_busy_search(). This function returns 0 for no overlapping busy
95 * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
96 * match. This is done so that a non-zero return indicates an overlap that
97 * will require a synchronous transaction, but it can still be
98 * used to distinguish between a partial or exact match.
101 xfs_extent_busy_search(
102 struct xfs_mount *mp,
103 struct xfs_perag *pag,
108 struct xfs_extent_busy *busyp;
111 /* find closest start bno overlap */
112 spin_lock(&pag->pagb_lock);
113 rbp = pag->pagb_tree.rb_node;
115 busyp = rb_entry(rbp, struct xfs_extent_busy, rb_node);
116 if (bno < busyp->bno) {
117 /* may overlap, but exact start block is lower */
118 if (bno + len > busyp->bno)
121 } else if (bno > busyp->bno) {
122 /* may overlap, but exact start block is higher */
123 if (bno < busyp->bno + busyp->length)
127 /* bno matches busyp, length determines exact match */
128 match = (busyp->length == len) ? 1 : -1;
132 spin_unlock(&pag->pagb_lock);
137 * The found free extent [fbno, fend] overlaps part or all of the given busy
138 * extent. If the overlap covers the beginning, the end, or all of the busy
139 * extent, the overlapping portion can be made unbusy and used for the
140 * allocation. We can't split a busy extent because we can't modify a
141 * transaction/CIL context busy list, but we can update an entry's block
144 * Returns true if the extent can safely be reused, or false if the search
145 * needs to be restarted.
148 xfs_extent_busy_update_extent(
149 struct xfs_mount *mp,
150 struct xfs_perag *pag,
151 struct xfs_extent_busy *busyp,
154 bool userdata) __releases(&pag->pagb_lock)
155 __acquires(&pag->pagb_lock)
157 xfs_agblock_t fend = fbno + flen;
158 xfs_agblock_t bbno = busyp->bno;
159 xfs_agblock_t bend = bbno + busyp->length;
162 * This extent is currently being discarded. Give the thread
163 * performing the discard a chance to mark the extent unbusy
166 if (busyp->flags & XFS_EXTENT_BUSY_DISCARDED) {
167 spin_unlock(&pag->pagb_lock);
169 spin_lock(&pag->pagb_lock);
174 * If there is a busy extent overlapping a user allocation, we have
175 * no choice but to force the log and retry the search.
177 * Fortunately this does not happen during normal operation, but
178 * only if the filesystem is very low on space and has to dip into
179 * the AGFL for normal allocations.
184 if (bbno < fbno && bend > fend) {
188 * +BBBBBBBBBBBBBBBBB+
194 * We would have to split the busy extent to be able to track
195 * it correct, which we cannot do because we would have to
196 * modify the list of busy extents attached to the transaction
197 * or CIL context, which is immutable.
199 * Force out the log to clear the busy extent and retry the
203 } else if (bbno >= fbno && bend <= fend) {
207 * +BBBBBBBBBBBBBBBBB+
208 * +-----------------+
213 * +BBBBBBBBBBBBBBBBB+
214 * +--------------------------+
219 * +BBBBBBBBBBBBBBBBB+
220 * +--------------------------+
225 * +BBBBBBBBBBBBBBBBB+
226 * +-----------------------------------+
232 * The busy extent is fully covered by the extent we are
233 * allocating, and can simply be removed from the rbtree.
234 * However we cannot remove it from the immutable list
235 * tracking busy extents in the transaction or CIL context,
236 * so set the length to zero to mark it invalid.
238 * We also need to restart the busy extent search from the
239 * tree root, because erasing the node can rearrange the
242 rb_erase(&busyp->rb_node, &pag->pagb_tree);
245 } else if (fend < bend) {
249 * +BBBBBBBBBBBBBBBBB+
255 * +BBBBBBBBBBBBBBBBB+
256 * +------------------+
261 busyp->length = bend - fend;
262 } else if (bbno < fbno) {
266 * +BBBBBBBBBBBBBBBBB+
272 * +BBBBBBBBBBBBBBBBB+
273 * +----------------------+
276 busyp->length = fbno - busyp->bno;
281 trace_xfs_extent_busy_reuse(mp, pag->pag_agno, fbno, flen);
285 spin_unlock(&pag->pagb_lock);
286 xfs_log_force(mp, XFS_LOG_SYNC);
287 trace_xfs_extent_busy_force(mp, pag->pag_agno, fbno, flen);
288 spin_lock(&pag->pagb_lock);
294 * For a given extent [fbno, flen], make sure we can reuse it safely.
297 xfs_extent_busy_reuse(
298 struct xfs_mount *mp,
299 struct xfs_perag *pag,
307 spin_lock(&pag->pagb_lock);
309 rbp = pag->pagb_tree.rb_node;
311 struct xfs_extent_busy *busyp =
312 rb_entry(rbp, struct xfs_extent_busy, rb_node);
313 xfs_agblock_t bbno = busyp->bno;
314 xfs_agblock_t bend = bbno + busyp->length;
316 if (fbno + flen <= bbno) {
319 } else if (fbno >= bend) {
324 if (!xfs_extent_busy_update_extent(mp, pag, busyp, fbno, flen,
328 spin_unlock(&pag->pagb_lock);
332 * For a given extent [fbno, flen], search the busy extent list to find a
333 * subset of the extent that is not busy. If *rlen is smaller than
334 * args->minlen no suitable extent could be found, and the higher level
335 * code needs to force out the log and retry the allocation.
337 * Return the current busy generation for the AG if the extent is busy. This
338 * value can be used to wait for at least one of the currently busy extents
339 * to be cleared. Note that the busy list is not guaranteed to be empty after
340 * the gen is woken. The state of a specific extent must always be confirmed
341 * with another call to xfs_extent_busy_trim() before it can be used.
344 xfs_extent_busy_trim(
345 struct xfs_alloc_arg *args,
357 spin_lock(&args->pag->pagb_lock);
360 rbp = args->pag->pagb_tree.rb_node;
361 while (rbp && flen >= args->minlen) {
362 struct xfs_extent_busy *busyp =
363 rb_entry(rbp, struct xfs_extent_busy, rb_node);
364 xfs_agblock_t fend = fbno + flen;
365 xfs_agblock_t bbno = busyp->bno;
366 xfs_agblock_t bend = bbno + busyp->length;
371 } else if (fbno >= bend) {
382 * +BBBBBBBBBBBBBBBBB+
388 * +BBBBBBBBBBBBBBBBB+
394 * +BBBBBBBBBBBBBBBBB+
400 * +BBBBBBBBBBBBBBBBB+
401 * +-----------------+
404 * No unbusy region in extent, return failure.
412 * +BBBBBBBBBBBBBBBBB+
413 * +----------------------+
418 * +BBBBBBBBBBBBBBBBB+
419 * +--------------------------+
422 * Needs to be trimmed to:
427 } else if (bend >= fend) {
433 * +BBBBBBBBBBBBBBBBB+
434 * +------------------+
439 * +BBBBBBBBBBBBBBBBB+
440 * +--------------------------+
443 * Needs to be trimmed to:
454 * +BBBBBBBBBBBBBBBBB+
455 * +-----------------------------------+
459 * +-------+ OR +-------+
460 * fbno fend fbno fend
462 * Backward allocation leads to significant
463 * fragmentation of directories, which degrades
464 * directory performance, therefore we always want to
465 * choose the option that produces forward allocation
467 * Preferring the lower bno extent will make the next
468 * request use "fend" as the start of the next
469 * allocation; if the segment is no longer busy at
470 * that point, we'll get a contiguous allocation, but
471 * even if it is still busy, we will get a forward
473 * We try to avoid choosing the segment at "bend",
474 * because that can lead to the next allocation
475 * taking the segment at "fbno", which would be a
476 * backward allocation. We only use the segment at
477 * "fbno" if it is much larger than the current
478 * requested size, because in that case there's a
479 * good chance subsequent allocations will be
482 if (bbno - fbno >= args->maxlen) {
483 /* left candidate fits perfect */
485 } else if (fend - bend >= args->maxlen * 4) {
486 /* right candidate has enough free space */
488 } else if (bbno - fbno >= args->minlen) {
489 /* left candidate fits minimum requirement */
500 if (fbno != *bno || flen != *len) {
501 trace_xfs_extent_busy_trim(args->mp, args->agno, *bno, *len,
505 *busy_gen = args->pag->pagb_gen;
508 spin_unlock(&args->pag->pagb_lock);
512 * Return a zero extent length as failure indications. All callers
513 * re-check if the trimmed extent satisfies the minlen requirement.
520 xfs_extent_busy_clear_one(
521 struct xfs_mount *mp,
522 struct xfs_perag *pag,
523 struct xfs_extent_busy *busyp)
526 trace_xfs_extent_busy_clear(mp, busyp->agno, busyp->bno,
528 rb_erase(&busyp->rb_node, &pag->pagb_tree);
531 list_del_init(&busyp->list);
536 xfs_extent_busy_put_pag(
537 struct xfs_perag *pag,
539 __releases(pag->pagb_lock)
543 wake_up_all(&pag->pagb_wait);
546 spin_unlock(&pag->pagb_lock);
551 * Remove all extents on the passed in list from the busy extents tree.
552 * If do_discard is set skip extents that need to be discarded, and mark
553 * these as undergoing a discard operation instead.
556 xfs_extent_busy_clear(
557 struct xfs_mount *mp,
558 struct list_head *list,
561 struct xfs_extent_busy *busyp, *n;
562 struct xfs_perag *pag = NULL;
563 xfs_agnumber_t agno = NULLAGNUMBER;
566 list_for_each_entry_safe(busyp, n, list, list) {
567 if (busyp->agno != agno) {
569 xfs_extent_busy_put_pag(pag, wakeup);
571 pag = xfs_perag_get(mp, agno);
572 spin_lock(&pag->pagb_lock);
576 if (do_discard && busyp->length &&
577 !(busyp->flags & XFS_EXTENT_BUSY_SKIP_DISCARD)) {
578 busyp->flags = XFS_EXTENT_BUSY_DISCARDED;
580 xfs_extent_busy_clear_one(mp, pag, busyp);
586 xfs_extent_busy_put_pag(pag, wakeup);
590 * Flush out all busy extents for this AG.
592 * If the current transaction is holding busy extents, the caller may not want
593 * to wait for committed busy extents to resolve. If we are being told just to
594 * try a flush or progress has been made since we last skipped a busy extent,
595 * return immediately to allow the caller to try again.
597 * If we are freeing extents, we might actually be holding the only free extents
598 * in the transaction busy list and the log force won't resolve that situation.
599 * In this case, we must return -EAGAIN to avoid a deadlock by informing the
600 * caller it needs to commit the busy extents it holds before retrying the
601 * extent free operation.
604 xfs_extent_busy_flush(
605 struct xfs_trans *tp,
606 struct xfs_perag *pag,
608 uint32_t alloc_flags)
613 error = xfs_log_force(tp->t_mountp, XFS_LOG_SYNC);
617 /* Avoid deadlocks on uncommitted busy extents. */
618 if (!list_empty(&tp->t_busy)) {
619 if (alloc_flags & XFS_ALLOC_FLAG_TRYFLUSH)
622 if (busy_gen != READ_ONCE(pag->pagb_gen))
625 if (alloc_flags & XFS_ALLOC_FLAG_FREEING)
629 /* Wait for committed busy extents to resolve. */
631 prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
632 if (busy_gen != READ_ONCE(pag->pagb_gen))
637 finish_wait(&pag->pagb_wait, &wait);
642 xfs_extent_busy_wait_all(
643 struct xfs_mount *mp)
645 struct xfs_perag *pag;
649 for_each_perag(mp, agno, pag) {
651 prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE);
652 if (RB_EMPTY_ROOT(&pag->pagb_tree))
656 finish_wait(&pag->pagb_wait, &wait);
661 * Callback for list_sort to sort busy extents by the AG they reside in.
664 xfs_extent_busy_ag_cmp(
666 const struct list_head *l1,
667 const struct list_head *l2)
669 struct xfs_extent_busy *b1 =
670 container_of(l1, struct xfs_extent_busy, list);
671 struct xfs_extent_busy *b2 =
672 container_of(l2, struct xfs_extent_busy, list);
675 diff = b1->agno - b2->agno;
677 diff = b1->bno - b2->bno;