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