xref: /openbmc/linux/fs/xfs/xfs_rtalloc.c (revision e5242c5f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_bmap.h"
16 #include "xfs_bmap_btree.h"
17 #include "xfs_trans.h"
18 #include "xfs_trans_space.h"
19 #include "xfs_icache.h"
20 #include "xfs_rtalloc.h"
21 #include "xfs_sb.h"
22 #include "xfs_rtbitmap.h"
23 
24 /*
25  * Read and return the summary information for a given extent size,
26  * bitmap block combination.
27  * Keeps track of a current summary block, so we don't keep reading
28  * it from the buffer cache.
29  */
30 static int
31 xfs_rtget_summary(
32 	xfs_mount_t	*mp,		/* file system mount structure */
33 	xfs_trans_t	*tp,		/* transaction pointer */
34 	int		log,		/* log2 of extent size */
35 	xfs_rtblock_t	bbno,		/* bitmap block number */
36 	struct xfs_buf	**rbpp,		/* in/out: summary block buffer */
37 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
38 	xfs_suminfo_t	*sum)		/* out: summary info for this block */
39 {
40 	return xfs_rtmodify_summary_int(mp, tp, log, bbno, 0, rbpp, rsb, sum);
41 }
42 
43 /*
44  * Return whether there are any free extents in the size range given
45  * by low and high, for the bitmap block bbno.
46  */
47 STATIC int				/* error */
48 xfs_rtany_summary(
49 	xfs_mount_t	*mp,		/* file system mount structure */
50 	xfs_trans_t	*tp,		/* transaction pointer */
51 	int		low,		/* low log2 extent size */
52 	int		high,		/* high log2 extent size */
53 	xfs_rtblock_t	bbno,		/* bitmap block number */
54 	struct xfs_buf	**rbpp,		/* in/out: summary block buffer */
55 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
56 	int		*stat)		/* out: any good extents here? */
57 {
58 	int		error;		/* error value */
59 	int		log;		/* loop counter, log2 of ext. size */
60 	xfs_suminfo_t	sum;		/* summary data */
61 
62 	/* There are no extents at levels < m_rsum_cache[bbno]. */
63 	if (mp->m_rsum_cache && low < mp->m_rsum_cache[bbno])
64 		low = mp->m_rsum_cache[bbno];
65 
66 	/*
67 	 * Loop over logs of extent sizes.
68 	 */
69 	for (log = low; log <= high; log++) {
70 		/*
71 		 * Get one summary datum.
72 		 */
73 		error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
74 		if (error) {
75 			return error;
76 		}
77 		/*
78 		 * If there are any, return success.
79 		 */
80 		if (sum) {
81 			*stat = 1;
82 			goto out;
83 		}
84 	}
85 	/*
86 	 * Found nothing, return failure.
87 	 */
88 	*stat = 0;
89 out:
90 	/* There were no extents at levels < log. */
91 	if (mp->m_rsum_cache && log > mp->m_rsum_cache[bbno])
92 		mp->m_rsum_cache[bbno] = log;
93 	return 0;
94 }
95 
96 
97 /*
98  * Copy and transform the summary file, given the old and new
99  * parameters in the mount structures.
100  */
101 STATIC int				/* error */
102 xfs_rtcopy_summary(
103 	xfs_mount_t	*omp,		/* old file system mount point */
104 	xfs_mount_t	*nmp,		/* new file system mount point */
105 	xfs_trans_t	*tp)		/* transaction pointer */
106 {
107 	xfs_rtblock_t	bbno;		/* bitmap block number */
108 	struct xfs_buf	*bp;		/* summary buffer */
109 	int		error;		/* error return value */
110 	int		log;		/* summary level number (log length) */
111 	xfs_suminfo_t	sum;		/* summary data */
112 	xfs_fsblock_t	sumbno;		/* summary block number */
113 
114 	bp = NULL;
115 	for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
116 		for (bbno = omp->m_sb.sb_rbmblocks - 1;
117 		     (xfs_srtblock_t)bbno >= 0;
118 		     bbno--) {
119 			error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
120 				&sumbno, &sum);
121 			if (error)
122 				return error;
123 			if (sum == 0)
124 				continue;
125 			error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
126 				&bp, &sumbno);
127 			if (error)
128 				return error;
129 			error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
130 				&bp, &sumbno);
131 			if (error)
132 				return error;
133 			ASSERT(sum > 0);
134 		}
135 	}
136 	return 0;
137 }
138 /*
139  * Mark an extent specified by start and len allocated.
140  * Updates all the summary information as well as the bitmap.
141  */
142 STATIC int				/* error */
143 xfs_rtallocate_range(
144 	xfs_mount_t	*mp,		/* file system mount point */
145 	xfs_trans_t	*tp,		/* transaction pointer */
146 	xfs_rtblock_t	start,		/* start block to allocate */
147 	xfs_extlen_t	len,		/* length to allocate */
148 	struct xfs_buf	**rbpp,		/* in/out: summary block buffer */
149 	xfs_fsblock_t	*rsb)		/* in/out: summary block number */
150 {
151 	xfs_rtblock_t	end;		/* end of the allocated extent */
152 	int		error;		/* error value */
153 	xfs_rtblock_t	postblock = 0;	/* first block allocated > end */
154 	xfs_rtblock_t	preblock = 0;	/* first block allocated < start */
155 
156 	end = start + len - 1;
157 	/*
158 	 * Assume we're allocating out of the middle of a free extent.
159 	 * We need to find the beginning and end of the extent so we can
160 	 * properly update the summary.
161 	 */
162 	error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
163 	if (error) {
164 		return error;
165 	}
166 	/*
167 	 * Find the next allocated block (end of free extent).
168 	 */
169 	error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
170 		&postblock);
171 	if (error) {
172 		return error;
173 	}
174 	/*
175 	 * Decrement the summary information corresponding to the entire
176 	 * (old) free extent.
177 	 */
178 	error = xfs_rtmodify_summary(mp, tp,
179 		XFS_RTBLOCKLOG(postblock + 1 - preblock),
180 		XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
181 	if (error) {
182 		return error;
183 	}
184 	/*
185 	 * If there are blocks not being allocated at the front of the
186 	 * old extent, add summary data for them to be free.
187 	 */
188 	if (preblock < start) {
189 		error = xfs_rtmodify_summary(mp, tp,
190 			XFS_RTBLOCKLOG(start - preblock),
191 			XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
192 		if (error) {
193 			return error;
194 		}
195 	}
196 	/*
197 	 * If there are blocks not being allocated at the end of the
198 	 * old extent, add summary data for them to be free.
199 	 */
200 	if (postblock > end) {
201 		error = xfs_rtmodify_summary(mp, tp,
202 			XFS_RTBLOCKLOG(postblock - end),
203 			XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
204 		if (error) {
205 			return error;
206 		}
207 	}
208 	/*
209 	 * Modify the bitmap to mark this extent allocated.
210 	 */
211 	error = xfs_rtmodify_range(mp, tp, start, len, 0);
212 	return error;
213 }
214 
215 /*
216  * Make sure we don't run off the end of the rt volume.  Be careful that
217  * adjusting maxlen downwards doesn't cause us to fail the alignment checks.
218  */
219 static inline xfs_extlen_t
220 xfs_rtallocate_clamp_len(
221 	struct xfs_mount	*mp,
222 	xfs_rtblock_t		startrtx,
223 	xfs_extlen_t		rtxlen,
224 	xfs_extlen_t		prod)
225 {
226 	xfs_extlen_t		ret;
227 
228 	ret = min(mp->m_sb.sb_rextents, startrtx + rtxlen) - startrtx;
229 	return rounddown(ret, prod);
230 }
231 
232 /*
233  * Attempt to allocate an extent minlen<=len<=maxlen starting from
234  * bitmap block bbno.  If we don't get maxlen then use prod to trim
235  * the length, if given.  Returns error; returns starting block in *rtblock.
236  * The lengths are all in rtextents.
237  */
238 STATIC int				/* error */
239 xfs_rtallocate_extent_block(
240 	xfs_mount_t	*mp,		/* file system mount point */
241 	xfs_trans_t	*tp,		/* transaction pointer */
242 	xfs_rtblock_t	bbno,		/* bitmap block number */
243 	xfs_extlen_t	minlen,		/* minimum length to allocate */
244 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
245 	xfs_extlen_t	*len,		/* out: actual length allocated */
246 	xfs_rtblock_t	*nextp,		/* out: next block to try */
247 	struct xfs_buf	**rbpp,		/* in/out: summary block buffer */
248 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
249 	xfs_extlen_t	prod,		/* extent product factor */
250 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
251 {
252 	xfs_rtblock_t	besti;		/* best rtblock found so far */
253 	xfs_rtblock_t	bestlen;	/* best length found so far */
254 	xfs_rtblock_t	end;		/* last rtblock in chunk */
255 	int		error;		/* error value */
256 	xfs_rtblock_t	i;		/* current rtblock trying */
257 	xfs_rtblock_t	next;		/* next rtblock to try */
258 	int		stat;		/* status from internal calls */
259 
260 	/*
261 	 * Loop over all the extents starting in this bitmap block,
262 	 * looking for one that's long enough.
263 	 */
264 	for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
265 		end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
266 	     i <= end;
267 	     i++) {
268 		/* Make sure we don't scan off the end of the rt volume. */
269 		maxlen = xfs_rtallocate_clamp_len(mp, i, maxlen, prod);
270 
271 		/*
272 		 * See if there's a free extent of maxlen starting at i.
273 		 * If it's not so then next will contain the first non-free.
274 		 */
275 		error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
276 		if (error) {
277 			return error;
278 		}
279 		if (stat) {
280 			/*
281 			 * i for maxlen is all free, allocate and return that.
282 			 */
283 			error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
284 				rsb);
285 			if (error) {
286 				return error;
287 			}
288 			*len = maxlen;
289 			*rtblock = i;
290 			return 0;
291 		}
292 		/*
293 		 * In the case where we have a variable-sized allocation
294 		 * request, figure out how big this free piece is,
295 		 * and if it's big enough for the minimum, and the best
296 		 * so far, remember it.
297 		 */
298 		if (minlen < maxlen) {
299 			xfs_rtblock_t	thislen;	/* this extent size */
300 
301 			thislen = next - i;
302 			if (thislen >= minlen && thislen > bestlen) {
303 				besti = i;
304 				bestlen = thislen;
305 			}
306 		}
307 		/*
308 		 * If not done yet, find the start of the next free space.
309 		 */
310 		if (next < end) {
311 			error = xfs_rtfind_forw(mp, tp, next, end, &i);
312 			if (error) {
313 				return error;
314 			}
315 		} else
316 			break;
317 	}
318 	/*
319 	 * Searched the whole thing & didn't find a maxlen free extent.
320 	 */
321 	if (minlen <= maxlen && besti != -1) {
322 		xfs_extlen_t	p;	/* amount to trim length by */
323 
324 		/*
325 		 * If size should be a multiple of prod, make that so.
326 		 */
327 		if (prod > 1) {
328 			div_u64_rem(bestlen, prod, &p);
329 			if (p)
330 				bestlen -= p;
331 		}
332 
333 		/*
334 		 * Allocate besti for bestlen & return that.
335 		 */
336 		error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
337 		if (error) {
338 			return error;
339 		}
340 		*len = bestlen;
341 		*rtblock = besti;
342 		return 0;
343 	}
344 	/*
345 	 * Allocation failed.  Set *nextp to the next block to try.
346 	 */
347 	*nextp = next;
348 	*rtblock = NULLRTBLOCK;
349 	return 0;
350 }
351 
352 /*
353  * Allocate an extent of length minlen<=len<=maxlen, starting at block
354  * bno.  If we don't get maxlen then use prod to trim the length, if given.
355  * Returns error; returns starting block in *rtblock.
356  * The lengths are all in rtextents.
357  */
358 STATIC int				/* error */
359 xfs_rtallocate_extent_exact(
360 	xfs_mount_t	*mp,		/* file system mount point */
361 	xfs_trans_t	*tp,		/* transaction pointer */
362 	xfs_rtblock_t	bno,		/* starting block number to allocate */
363 	xfs_extlen_t	minlen,		/* minimum length to allocate */
364 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
365 	xfs_extlen_t	*len,		/* out: actual length allocated */
366 	struct xfs_buf	**rbpp,		/* in/out: summary block buffer */
367 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
368 	xfs_extlen_t	prod,		/* extent product factor */
369 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
370 {
371 	int		error;		/* error value */
372 	xfs_extlen_t	i;		/* extent length trimmed due to prod */
373 	int		isfree;		/* extent is free */
374 	xfs_rtblock_t	next;		/* next block to try (dummy) */
375 
376 	ASSERT(minlen % prod == 0);
377 	ASSERT(maxlen % prod == 0);
378 	/*
379 	 * Check if the range in question (for maxlen) is free.
380 	 */
381 	error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
382 	if (error) {
383 		return error;
384 	}
385 	if (isfree) {
386 		/*
387 		 * If it is, allocate it and return success.
388 		 */
389 		error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
390 		if (error) {
391 			return error;
392 		}
393 		*len = maxlen;
394 		*rtblock = bno;
395 		return 0;
396 	}
397 	/*
398 	 * If not, allocate what there is, if it's at least minlen.
399 	 */
400 	maxlen = next - bno;
401 	if (maxlen < minlen) {
402 		/*
403 		 * Failed, return failure status.
404 		 */
405 		*rtblock = NULLRTBLOCK;
406 		return 0;
407 	}
408 	/*
409 	 * Trim off tail of extent, if prod is specified.
410 	 */
411 	if (prod > 1 && (i = maxlen % prod)) {
412 		maxlen -= i;
413 		if (maxlen < minlen) {
414 			/*
415 			 * Now we can't do it, return failure status.
416 			 */
417 			*rtblock = NULLRTBLOCK;
418 			return 0;
419 		}
420 	}
421 	/*
422 	 * Allocate what we can and return it.
423 	 */
424 	error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
425 	if (error) {
426 		return error;
427 	}
428 	*len = maxlen;
429 	*rtblock = bno;
430 	return 0;
431 }
432 
433 /*
434  * Allocate an extent of length minlen<=len<=maxlen, starting as near
435  * to bno as possible.  If we don't get maxlen then use prod to trim
436  * the length, if given.  The lengths are all in rtextents.
437  */
438 STATIC int				/* error */
439 xfs_rtallocate_extent_near(
440 	xfs_mount_t	*mp,		/* file system mount point */
441 	xfs_trans_t	*tp,		/* transaction pointer */
442 	xfs_rtblock_t	bno,		/* starting block number to allocate */
443 	xfs_extlen_t	minlen,		/* minimum length to allocate */
444 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
445 	xfs_extlen_t	*len,		/* out: actual length allocated */
446 	struct xfs_buf	**rbpp,		/* in/out: summary block buffer */
447 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
448 	xfs_extlen_t	prod,		/* extent product factor */
449 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
450 {
451 	int		any;		/* any useful extents from summary */
452 	xfs_rtblock_t	bbno;		/* bitmap block number */
453 	int		error;		/* error value */
454 	int		i;		/* bitmap block offset (loop control) */
455 	int		j;		/* secondary loop control */
456 	int		log2len;	/* log2 of minlen */
457 	xfs_rtblock_t	n;		/* next block to try */
458 	xfs_rtblock_t	r;		/* result block */
459 
460 	ASSERT(minlen % prod == 0);
461 	ASSERT(maxlen % prod == 0);
462 
463 	/*
464 	 * If the block number given is off the end, silently set it to
465 	 * the last block.
466 	 */
467 	if (bno >= mp->m_sb.sb_rextents)
468 		bno = mp->m_sb.sb_rextents - 1;
469 
470 	/* Make sure we don't run off the end of the rt volume. */
471 	maxlen = xfs_rtallocate_clamp_len(mp, bno, maxlen, prod);
472 	if (maxlen < minlen) {
473 		*rtblock = NULLRTBLOCK;
474 		return 0;
475 	}
476 
477 	/*
478 	 * Try the exact allocation first.
479 	 */
480 	error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
481 		rbpp, rsb, prod, &r);
482 	if (error) {
483 		return error;
484 	}
485 	/*
486 	 * If the exact allocation worked, return that.
487 	 */
488 	if (r != NULLRTBLOCK) {
489 		*rtblock = r;
490 		return 0;
491 	}
492 	bbno = XFS_BITTOBLOCK(mp, bno);
493 	i = 0;
494 	ASSERT(minlen != 0);
495 	log2len = xfs_highbit32(minlen);
496 	/*
497 	 * Loop over all bitmap blocks (bbno + i is current block).
498 	 */
499 	for (;;) {
500 		/*
501 		 * Get summary information of extents of all useful levels
502 		 * starting in this bitmap block.
503 		 */
504 		error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
505 			bbno + i, rbpp, rsb, &any);
506 		if (error) {
507 			return error;
508 		}
509 		/*
510 		 * If there are any useful extents starting here, try
511 		 * allocating one.
512 		 */
513 		if (any) {
514 			/*
515 			 * On the positive side of the starting location.
516 			 */
517 			if (i >= 0) {
518 				/*
519 				 * Try to allocate an extent starting in
520 				 * this block.
521 				 */
522 				error = xfs_rtallocate_extent_block(mp, tp,
523 					bbno + i, minlen, maxlen, len, &n, rbpp,
524 					rsb, prod, &r);
525 				if (error) {
526 					return error;
527 				}
528 				/*
529 				 * If it worked, return it.
530 				 */
531 				if (r != NULLRTBLOCK) {
532 					*rtblock = r;
533 					return 0;
534 				}
535 			}
536 			/*
537 			 * On the negative side of the starting location.
538 			 */
539 			else {		/* i < 0 */
540 				/*
541 				 * Loop backwards through the bitmap blocks from
542 				 * the starting point-1 up to where we are now.
543 				 * There should be an extent which ends in this
544 				 * bitmap block and is long enough.
545 				 */
546 				for (j = -1; j > i; j--) {
547 					/*
548 					 * Grab the summary information for
549 					 * this bitmap block.
550 					 */
551 					error = xfs_rtany_summary(mp, tp,
552 						log2len, mp->m_rsumlevels - 1,
553 						bbno + j, rbpp, rsb, &any);
554 					if (error) {
555 						return error;
556 					}
557 					/*
558 					 * If there's no extent given in the
559 					 * summary that means the extent we
560 					 * found must carry over from an
561 					 * earlier block.  If there is an
562 					 * extent given, we've already tried
563 					 * that allocation, don't do it again.
564 					 */
565 					if (any)
566 						continue;
567 					error = xfs_rtallocate_extent_block(mp,
568 						tp, bbno + j, minlen, maxlen,
569 						len, &n, rbpp, rsb, prod, &r);
570 					if (error) {
571 						return error;
572 					}
573 					/*
574 					 * If it works, return the extent.
575 					 */
576 					if (r != NULLRTBLOCK) {
577 						*rtblock = r;
578 						return 0;
579 					}
580 				}
581 				/*
582 				 * There weren't intervening bitmap blocks
583 				 * with a long enough extent, or the
584 				 * allocation didn't work for some reason
585 				 * (i.e. it's a little * too short).
586 				 * Try to allocate from the summary block
587 				 * that we found.
588 				 */
589 				error = xfs_rtallocate_extent_block(mp, tp,
590 					bbno + i, minlen, maxlen, len, &n, rbpp,
591 					rsb, prod, &r);
592 				if (error) {
593 					return error;
594 				}
595 				/*
596 				 * If it works, return the extent.
597 				 */
598 				if (r != NULLRTBLOCK) {
599 					*rtblock = r;
600 					return 0;
601 				}
602 			}
603 		}
604 		/*
605 		 * Loop control.  If we were on the positive side, and there's
606 		 * still more blocks on the negative side, go there.
607 		 */
608 		if (i > 0 && (int)bbno - i >= 0)
609 			i = -i;
610 		/*
611 		 * If positive, and no more negative, but there are more
612 		 * positive, go there.
613 		 */
614 		else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
615 			i++;
616 		/*
617 		 * If negative or 0 (just started), and there are positive
618 		 * blocks to go, go there.  The 0 case moves to block 1.
619 		 */
620 		else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
621 			i = 1 - i;
622 		/*
623 		 * If negative or 0 and there are more negative blocks,
624 		 * go there.
625 		 */
626 		else if (i <= 0 && (int)bbno + i > 0)
627 			i--;
628 		/*
629 		 * Must be done.  Return failure.
630 		 */
631 		else
632 			break;
633 	}
634 	*rtblock = NULLRTBLOCK;
635 	return 0;
636 }
637 
638 /*
639  * Allocate an extent of length minlen<=len<=maxlen, with no position
640  * specified.  If we don't get maxlen then use prod to trim
641  * the length, if given.  The lengths are all in rtextents.
642  */
643 STATIC int				/* error */
644 xfs_rtallocate_extent_size(
645 	xfs_mount_t	*mp,		/* file system mount point */
646 	xfs_trans_t	*tp,		/* transaction pointer */
647 	xfs_extlen_t	minlen,		/* minimum length to allocate */
648 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
649 	xfs_extlen_t	*len,		/* out: actual length allocated */
650 	struct xfs_buf	**rbpp,		/* in/out: summary block buffer */
651 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
652 	xfs_extlen_t	prod,		/* extent product factor */
653 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
654 {
655 	int		error;		/* error value */
656 	int		i;		/* bitmap block number */
657 	int		l;		/* level number (loop control) */
658 	xfs_rtblock_t	n;		/* next block to be tried */
659 	xfs_rtblock_t	r;		/* result block number */
660 	xfs_suminfo_t	sum;		/* summary information for extents */
661 
662 	ASSERT(minlen % prod == 0);
663 	ASSERT(maxlen % prod == 0);
664 	ASSERT(maxlen != 0);
665 
666 	/*
667 	 * Loop over all the levels starting with maxlen.
668 	 * At each level, look at all the bitmap blocks, to see if there
669 	 * are extents starting there that are long enough (>= maxlen).
670 	 * Note, only on the initial level can the allocation fail if
671 	 * the summary says there's an extent.
672 	 */
673 	for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
674 		/*
675 		 * Loop over all the bitmap blocks.
676 		 */
677 		for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
678 			/*
679 			 * Get the summary for this level/block.
680 			 */
681 			error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
682 				&sum);
683 			if (error) {
684 				return error;
685 			}
686 			/*
687 			 * Nothing there, on to the next block.
688 			 */
689 			if (!sum)
690 				continue;
691 			/*
692 			 * Try allocating the extent.
693 			 */
694 			error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
695 				maxlen, len, &n, rbpp, rsb, prod, &r);
696 			if (error) {
697 				return error;
698 			}
699 			/*
700 			 * If it worked, return that.
701 			 */
702 			if (r != NULLRTBLOCK) {
703 				*rtblock = r;
704 				return 0;
705 			}
706 			/*
707 			 * If the "next block to try" returned from the
708 			 * allocator is beyond the next bitmap block,
709 			 * skip to that bitmap block.
710 			 */
711 			if (XFS_BITTOBLOCK(mp, n) > i + 1)
712 				i = XFS_BITTOBLOCK(mp, n) - 1;
713 		}
714 	}
715 	/*
716 	 * Didn't find any maxlen blocks.  Try smaller ones, unless
717 	 * we're asking for a fixed size extent.
718 	 */
719 	if (minlen > --maxlen) {
720 		*rtblock = NULLRTBLOCK;
721 		return 0;
722 	}
723 	ASSERT(minlen != 0);
724 	ASSERT(maxlen != 0);
725 
726 	/*
727 	 * Loop over sizes, from maxlen down to minlen.
728 	 * This time, when we do the allocations, allow smaller ones
729 	 * to succeed.
730 	 */
731 	for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
732 		/*
733 		 * Loop over all the bitmap blocks, try an allocation
734 		 * starting in that block.
735 		 */
736 		for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
737 			/*
738 			 * Get the summary information for this level/block.
739 			 */
740 			error =	xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
741 						  &sum);
742 			if (error) {
743 				return error;
744 			}
745 			/*
746 			 * If nothing there, go on to next.
747 			 */
748 			if (!sum)
749 				continue;
750 			/*
751 			 * Try the allocation.  Make sure the specified
752 			 * minlen/maxlen are in the possible range for
753 			 * this summary level.
754 			 */
755 			error = xfs_rtallocate_extent_block(mp, tp, i,
756 					XFS_RTMAX(minlen, 1 << l),
757 					XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
758 					len, &n, rbpp, rsb, prod, &r);
759 			if (error) {
760 				return error;
761 			}
762 			/*
763 			 * If it worked, return that extent.
764 			 */
765 			if (r != NULLRTBLOCK) {
766 				*rtblock = r;
767 				return 0;
768 			}
769 			/*
770 			 * If the "next block to try" returned from the
771 			 * allocator is beyond the next bitmap block,
772 			 * skip to that bitmap block.
773 			 */
774 			if (XFS_BITTOBLOCK(mp, n) > i + 1)
775 				i = XFS_BITTOBLOCK(mp, n) - 1;
776 		}
777 	}
778 	/*
779 	 * Got nothing, return failure.
780 	 */
781 	*rtblock = NULLRTBLOCK;
782 	return 0;
783 }
784 
785 /*
786  * Allocate space to the bitmap or summary file, and zero it, for growfs.
787  */
788 STATIC int
789 xfs_growfs_rt_alloc(
790 	struct xfs_mount	*mp,		/* file system mount point */
791 	xfs_extlen_t		oblocks,	/* old count of blocks */
792 	xfs_extlen_t		nblocks,	/* new count of blocks */
793 	struct xfs_inode	*ip)		/* inode (bitmap/summary) */
794 {
795 	xfs_fileoff_t		bno;		/* block number in file */
796 	struct xfs_buf		*bp;	/* temporary buffer for zeroing */
797 	xfs_daddr_t		d;		/* disk block address */
798 	int			error;		/* error return value */
799 	xfs_fsblock_t		fsbno;		/* filesystem block for bno */
800 	struct xfs_bmbt_irec	map;		/* block map output */
801 	int			nmap;		/* number of block maps */
802 	int			resblks;	/* space reservation */
803 	enum xfs_blft		buf_type;
804 	struct xfs_trans	*tp;
805 
806 	if (ip == mp->m_rsumip)
807 		buf_type = XFS_BLFT_RTSUMMARY_BUF;
808 	else
809 		buf_type = XFS_BLFT_RTBITMAP_BUF;
810 
811 	/*
812 	 * Allocate space to the file, as necessary.
813 	 */
814 	while (oblocks < nblocks) {
815 		resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
816 		/*
817 		 * Reserve space & log for one extent added to the file.
818 		 */
819 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc, resblks,
820 				0, 0, &tp);
821 		if (error)
822 			return error;
823 		/*
824 		 * Lock the inode.
825 		 */
826 		xfs_ilock(ip, XFS_ILOCK_EXCL);
827 		xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
828 
829 		error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
830 				XFS_IEXT_ADD_NOSPLIT_CNT);
831 		if (error == -EFBIG)
832 			error = xfs_iext_count_upgrade(tp, ip,
833 					XFS_IEXT_ADD_NOSPLIT_CNT);
834 		if (error)
835 			goto out_trans_cancel;
836 
837 		/*
838 		 * Allocate blocks to the bitmap file.
839 		 */
840 		nmap = 1;
841 		error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
842 					XFS_BMAPI_METADATA, 0, &map, &nmap);
843 		if (error)
844 			goto out_trans_cancel;
845 		/*
846 		 * Free any blocks freed up in the transaction, then commit.
847 		 */
848 		error = xfs_trans_commit(tp);
849 		if (error)
850 			return error;
851 		/*
852 		 * Now we need to clear the allocated blocks.
853 		 * Do this one block per transaction, to keep it simple.
854 		 */
855 		for (bno = map.br_startoff, fsbno = map.br_startblock;
856 		     bno < map.br_startoff + map.br_blockcount;
857 		     bno++, fsbno++) {
858 			/*
859 			 * Reserve log for one block zeroing.
860 			 */
861 			error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
862 					0, 0, 0, &tp);
863 			if (error)
864 				return error;
865 			/*
866 			 * Lock the bitmap inode.
867 			 */
868 			xfs_ilock(ip, XFS_ILOCK_EXCL);
869 			xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
870 			/*
871 			 * Get a buffer for the block.
872 			 */
873 			d = XFS_FSB_TO_DADDR(mp, fsbno);
874 			error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
875 					mp->m_bsize, 0, &bp);
876 			if (error)
877 				goto out_trans_cancel;
878 
879 			xfs_trans_buf_set_type(tp, bp, buf_type);
880 			bp->b_ops = &xfs_rtbuf_ops;
881 			memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
882 			xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
883 			/*
884 			 * Commit the transaction.
885 			 */
886 			error = xfs_trans_commit(tp);
887 			if (error)
888 				return error;
889 		}
890 		/*
891 		 * Go on to the next extent, if any.
892 		 */
893 		oblocks = map.br_startoff + map.br_blockcount;
894 	}
895 
896 	return 0;
897 
898 out_trans_cancel:
899 	xfs_trans_cancel(tp);
900 	return error;
901 }
902 
903 static void
904 xfs_alloc_rsum_cache(
905 	xfs_mount_t	*mp,		/* file system mount structure */
906 	xfs_extlen_t	rbmblocks)	/* number of rt bitmap blocks */
907 {
908 	/*
909 	 * The rsum cache is initialized to all zeroes, which is trivially a
910 	 * lower bound on the minimum level with any free extents. We can
911 	 * continue without the cache if it couldn't be allocated.
912 	 */
913 	mp->m_rsum_cache = kvzalloc(rbmblocks, GFP_KERNEL);
914 	if (!mp->m_rsum_cache)
915 		xfs_warn(mp, "could not allocate realtime summary cache");
916 }
917 
918 /*
919  * Visible (exported) functions.
920  */
921 
922 /*
923  * Grow the realtime area of the filesystem.
924  */
925 int
926 xfs_growfs_rt(
927 	xfs_mount_t	*mp,		/* mount point for filesystem */
928 	xfs_growfs_rt_t	*in)		/* growfs rt input struct */
929 {
930 	xfs_rtblock_t	bmbno;		/* bitmap block number */
931 	struct xfs_buf	*bp;		/* temporary buffer */
932 	int		error;		/* error return value */
933 	xfs_mount_t	*nmp;		/* new (fake) mount structure */
934 	xfs_rfsblock_t	nrblocks;	/* new number of realtime blocks */
935 	xfs_extlen_t	nrbmblocks;	/* new number of rt bitmap blocks */
936 	xfs_rtblock_t	nrextents;	/* new number of realtime extents */
937 	uint8_t		nrextslog;	/* new log2 of sb_rextents */
938 	xfs_extlen_t	nrsumblocks;	/* new number of summary blocks */
939 	uint		nrsumlevels;	/* new rt summary levels */
940 	uint		nrsumsize;	/* new size of rt summary, bytes */
941 	xfs_sb_t	*nsbp;		/* new superblock */
942 	xfs_extlen_t	rbmblocks;	/* current number of rt bitmap blocks */
943 	xfs_extlen_t	rsumblocks;	/* current number of rt summary blks */
944 	xfs_sb_t	*sbp;		/* old superblock */
945 	xfs_fsblock_t	sumbno;		/* summary block number */
946 	uint8_t		*rsum_cache;	/* old summary cache */
947 
948 	sbp = &mp->m_sb;
949 
950 	if (!capable(CAP_SYS_ADMIN))
951 		return -EPERM;
952 
953 	/* Needs to have been mounted with an rt device. */
954 	if (!XFS_IS_REALTIME_MOUNT(mp))
955 		return -EINVAL;
956 	/*
957 	 * Mount should fail if the rt bitmap/summary files don't load, but
958 	 * we'll check anyway.
959 	 */
960 	if (!mp->m_rbmip || !mp->m_rsumip)
961 		return -EINVAL;
962 
963 	/* Shrink not supported. */
964 	if (in->newblocks <= sbp->sb_rblocks)
965 		return -EINVAL;
966 
967 	/* Can only change rt extent size when adding rt volume. */
968 	if (sbp->sb_rblocks > 0 && in->extsize != sbp->sb_rextsize)
969 		return -EINVAL;
970 
971 	/* Range check the extent size. */
972 	if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
973 	    XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
974 		return -EINVAL;
975 
976 	/* Unsupported realtime features. */
977 	if (xfs_has_rmapbt(mp) || xfs_has_reflink(mp) || xfs_has_quota(mp))
978 		return -EOPNOTSUPP;
979 
980 	nrblocks = in->newblocks;
981 	error = xfs_sb_validate_fsb_count(sbp, nrblocks);
982 	if (error)
983 		return error;
984 	/*
985 	 * Read in the last block of the device, make sure it exists.
986 	 */
987 	error = xfs_buf_read_uncached(mp->m_rtdev_targp,
988 				XFS_FSB_TO_BB(mp, nrblocks - 1),
989 				XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
990 	if (error)
991 		return error;
992 	xfs_buf_relse(bp);
993 
994 	/*
995 	 * Calculate new parameters.  These are the final values to be reached.
996 	 */
997 	nrextents = nrblocks;
998 	do_div(nrextents, in->extsize);
999 	if (!xfs_validate_rtextents(nrextents))
1000 		return -EINVAL;
1001 	nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1002 	nrextslog = xfs_compute_rextslog(nrextents);
1003 	nrsumlevels = nrextslog + 1;
1004 	nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1005 	nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1006 	nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1007 	/*
1008 	 * New summary size can't be more than half the size of
1009 	 * the log.  This prevents us from getting a log overflow,
1010 	 * since we'll log basically the whole summary file at once.
1011 	 */
1012 	if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1013 		return -EINVAL;
1014 	/*
1015 	 * Get the old block counts for bitmap and summary inodes.
1016 	 * These can't change since other growfs callers are locked out.
1017 	 */
1018 	rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_disk_size);
1019 	rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_disk_size);
1020 	/*
1021 	 * Allocate space to the bitmap and summary files, as necessary.
1022 	 */
1023 	error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
1024 	if (error)
1025 		return error;
1026 	error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
1027 	if (error)
1028 		return error;
1029 
1030 	rsum_cache = mp->m_rsum_cache;
1031 	if (nrbmblocks != sbp->sb_rbmblocks)
1032 		xfs_alloc_rsum_cache(mp, nrbmblocks);
1033 
1034 	/*
1035 	 * Allocate a new (fake) mount/sb.
1036 	 */
1037 	nmp = kmem_alloc(sizeof(*nmp), 0);
1038 	/*
1039 	 * Loop over the bitmap blocks.
1040 	 * We will do everything one bitmap block at a time.
1041 	 * Skip the current block if it is exactly full.
1042 	 * This also deals with the case where there were no rtextents before.
1043 	 */
1044 	for (bmbno = sbp->sb_rbmblocks -
1045 		     ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1046 	     bmbno < nrbmblocks;
1047 	     bmbno++) {
1048 		struct xfs_trans	*tp;
1049 		xfs_rfsblock_t		nrblocks_step;
1050 
1051 		*nmp = *mp;
1052 		nsbp = &nmp->m_sb;
1053 		/*
1054 		 * Calculate new sb and mount fields for this round.
1055 		 */
1056 		nsbp->sb_rextsize = in->extsize;
1057 		nsbp->sb_rbmblocks = bmbno + 1;
1058 		nrblocks_step = (bmbno + 1) * NBBY * nsbp->sb_blocksize *
1059 				nsbp->sb_rextsize;
1060 		nsbp->sb_rblocks = min(nrblocks, nrblocks_step);
1061 		nsbp->sb_rextents = nsbp->sb_rblocks;
1062 		do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1063 		ASSERT(nsbp->sb_rextents != 0);
1064 		nsbp->sb_rextslog = xfs_compute_rextslog(nsbp->sb_rextents);
1065 		nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1066 		nrsumsize =
1067 			(uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1068 			nsbp->sb_rbmblocks;
1069 		nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1070 		nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1071 		/* recompute growfsrt reservation from new rsumsize */
1072 		xfs_trans_resv_calc(nmp, &nmp->m_resv);
1073 
1074 		/*
1075 		 * Start a transaction, get the log reservation.
1076 		 */
1077 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
1078 				&tp);
1079 		if (error)
1080 			break;
1081 		/*
1082 		 * Lock out other callers by grabbing the bitmap inode lock.
1083 		 */
1084 		xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
1085 		xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
1086 		/*
1087 		 * Update the bitmap inode's size ondisk and incore.  We need
1088 		 * to update the incore size so that inode inactivation won't
1089 		 * punch what it thinks are "posteof" blocks.
1090 		 */
1091 		mp->m_rbmip->i_disk_size =
1092 			nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1093 		i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_disk_size);
1094 		xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1095 		/*
1096 		 * Get the summary inode into the transaction.
1097 		 */
1098 		xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
1099 		xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1100 		/*
1101 		 * Update the summary inode's size.  We need to update the
1102 		 * incore size so that inode inactivation won't punch what it
1103 		 * thinks are "posteof" blocks.
1104 		 */
1105 		mp->m_rsumip->i_disk_size = nmp->m_rsumsize;
1106 		i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_disk_size);
1107 		xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1108 		/*
1109 		 * Copy summary data from old to new sizes.
1110 		 * Do this when the real size (not block-aligned) changes.
1111 		 */
1112 		if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1113 		    mp->m_rsumlevels != nmp->m_rsumlevels) {
1114 			error = xfs_rtcopy_summary(mp, nmp, tp);
1115 			if (error)
1116 				goto error_cancel;
1117 		}
1118 		/*
1119 		 * Update superblock fields.
1120 		 */
1121 		if (nsbp->sb_rextsize != sbp->sb_rextsize)
1122 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1123 				nsbp->sb_rextsize - sbp->sb_rextsize);
1124 		if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1125 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1126 				nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1127 		if (nsbp->sb_rblocks != sbp->sb_rblocks)
1128 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1129 				nsbp->sb_rblocks - sbp->sb_rblocks);
1130 		if (nsbp->sb_rextents != sbp->sb_rextents)
1131 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1132 				nsbp->sb_rextents - sbp->sb_rextents);
1133 		if (nsbp->sb_rextslog != sbp->sb_rextslog)
1134 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1135 				nsbp->sb_rextslog - sbp->sb_rextslog);
1136 		/*
1137 		 * Free new extent.
1138 		 */
1139 		bp = NULL;
1140 		error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1141 			nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
1142 		if (error) {
1143 error_cancel:
1144 			xfs_trans_cancel(tp);
1145 			break;
1146 		}
1147 		/*
1148 		 * Mark more blocks free in the superblock.
1149 		 */
1150 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1151 			nsbp->sb_rextents - sbp->sb_rextents);
1152 		/*
1153 		 * Update mp values into the real mp structure.
1154 		 */
1155 		mp->m_rsumlevels = nrsumlevels;
1156 		mp->m_rsumsize = nrsumsize;
1157 		/* recompute growfsrt reservation from new rsumsize */
1158 		xfs_trans_resv_calc(mp, &mp->m_resv);
1159 
1160 		error = xfs_trans_commit(tp);
1161 		if (error)
1162 			break;
1163 
1164 		/* Ensure the mount RT feature flag is now set. */
1165 		mp->m_features |= XFS_FEAT_REALTIME;
1166 	}
1167 	if (error)
1168 		goto out_free;
1169 
1170 	/* Update secondary superblocks now the physical grow has completed */
1171 	error = xfs_update_secondary_sbs(mp);
1172 
1173 out_free:
1174 	/*
1175 	 * Free the fake mp structure.
1176 	 */
1177 	kmem_free(nmp);
1178 
1179 	/*
1180 	 * If we had to allocate a new rsum_cache, we either need to free the
1181 	 * old one (if we succeeded) or free the new one and restore the old one
1182 	 * (if there was an error).
1183 	 */
1184 	if (rsum_cache != mp->m_rsum_cache) {
1185 		if (error) {
1186 			kmem_free(mp->m_rsum_cache);
1187 			mp->m_rsum_cache = rsum_cache;
1188 		} else {
1189 			kmem_free(rsum_cache);
1190 		}
1191 	}
1192 
1193 	return error;
1194 }
1195 
1196 /*
1197  * Allocate an extent in the realtime subvolume, with the usual allocation
1198  * parameters.  The length units are all in realtime extents, as is the
1199  * result block number.
1200  */
1201 int					/* error */
1202 xfs_rtallocate_extent(
1203 	xfs_trans_t	*tp,		/* transaction pointer */
1204 	xfs_rtblock_t	bno,		/* starting block number to allocate */
1205 	xfs_extlen_t	minlen,		/* minimum length to allocate */
1206 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
1207 	xfs_extlen_t	*len,		/* out: actual length allocated */
1208 	int		wasdel,		/* was a delayed allocation extent */
1209 	xfs_extlen_t	prod,		/* extent product factor */
1210 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
1211 {
1212 	xfs_mount_t	*mp = tp->t_mountp;
1213 	int		error;		/* error value */
1214 	xfs_rtblock_t	r;		/* result allocated block */
1215 	xfs_fsblock_t	sb;		/* summary file block number */
1216 	struct xfs_buf	*sumbp;		/* summary file block buffer */
1217 
1218 	ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1219 	ASSERT(minlen > 0 && minlen <= maxlen);
1220 
1221 	/*
1222 	 * If prod is set then figure out what to do to minlen and maxlen.
1223 	 */
1224 	if (prod > 1) {
1225 		xfs_extlen_t	i;
1226 
1227 		if ((i = maxlen % prod))
1228 			maxlen -= i;
1229 		if ((i = minlen % prod))
1230 			minlen += prod - i;
1231 		if (maxlen < minlen) {
1232 			*rtblock = NULLRTBLOCK;
1233 			return 0;
1234 		}
1235 	}
1236 
1237 retry:
1238 	sumbp = NULL;
1239 	if (bno == 0) {
1240 		error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1241 				&sumbp,	&sb, prod, &r);
1242 	} else {
1243 		error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1244 				len, &sumbp, &sb, prod, &r);
1245 	}
1246 
1247 	if (error)
1248 		return error;
1249 
1250 	/*
1251 	 * If it worked, update the superblock.
1252 	 */
1253 	if (r != NULLRTBLOCK) {
1254 		long	slen = (long)*len;
1255 
1256 		ASSERT(*len >= minlen && *len <= maxlen);
1257 		if (wasdel)
1258 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1259 		else
1260 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
1261 	} else if (prod > 1) {
1262 		prod = 1;
1263 		goto retry;
1264 	}
1265 
1266 	*rtblock = r;
1267 	return 0;
1268 }
1269 
1270 /*
1271  * Initialize realtime fields in the mount structure.
1272  */
1273 int				/* error */
1274 xfs_rtmount_init(
1275 	struct xfs_mount	*mp)	/* file system mount structure */
1276 {
1277 	struct xfs_buf		*bp;	/* buffer for last block of subvolume */
1278 	struct xfs_sb		*sbp;	/* filesystem superblock copy in mount */
1279 	xfs_daddr_t		d;	/* address of last block of subvolume */
1280 	int			error;
1281 
1282 	sbp = &mp->m_sb;
1283 	if (sbp->sb_rblocks == 0)
1284 		return 0;
1285 	if (mp->m_rtdev_targp == NULL) {
1286 		xfs_warn(mp,
1287 	"Filesystem has a realtime volume, use rtdev=device option");
1288 		return -ENODEV;
1289 	}
1290 	mp->m_rsumlevels = sbp->sb_rextslog + 1;
1291 	mp->m_rsumsize =
1292 		(uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1293 		sbp->sb_rbmblocks;
1294 	mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1295 	mp->m_rbmip = mp->m_rsumip = NULL;
1296 	/*
1297 	 * Check that the realtime section is an ok size.
1298 	 */
1299 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1300 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
1301 		xfs_warn(mp, "realtime mount -- %llu != %llu",
1302 			(unsigned long long) XFS_BB_TO_FSB(mp, d),
1303 			(unsigned long long) mp->m_sb.sb_rblocks);
1304 		return -EFBIG;
1305 	}
1306 	error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1307 					d - XFS_FSB_TO_BB(mp, 1),
1308 					XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1309 	if (error) {
1310 		xfs_warn(mp, "realtime device size check failed");
1311 		return error;
1312 	}
1313 	xfs_buf_relse(bp);
1314 	return 0;
1315 }
1316 
1317 static int
1318 xfs_rtalloc_count_frextent(
1319 	struct xfs_mount		*mp,
1320 	struct xfs_trans		*tp,
1321 	const struct xfs_rtalloc_rec	*rec,
1322 	void				*priv)
1323 {
1324 	uint64_t			*valp = priv;
1325 
1326 	*valp += rec->ar_extcount;
1327 	return 0;
1328 }
1329 
1330 /*
1331  * Reinitialize the number of free realtime extents from the realtime bitmap.
1332  * Callers must ensure that there is no other activity in the filesystem.
1333  */
1334 int
1335 xfs_rtalloc_reinit_frextents(
1336 	struct xfs_mount	*mp)
1337 {
1338 	uint64_t		val = 0;
1339 	int			error;
1340 
1341 	xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
1342 	error = xfs_rtalloc_query_all(mp, NULL, xfs_rtalloc_count_frextent,
1343 			&val);
1344 	xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
1345 	if (error)
1346 		return error;
1347 
1348 	spin_lock(&mp->m_sb_lock);
1349 	mp->m_sb.sb_frextents = val;
1350 	spin_unlock(&mp->m_sb_lock);
1351 	percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
1352 	return 0;
1353 }
1354 
1355 /*
1356  * Read in the bmbt of an rt metadata inode so that we never have to load them
1357  * at runtime.  This enables the use of shared ILOCKs for rtbitmap scans.  Use
1358  * an empty transaction to avoid deadlocking on loops in the bmbt.
1359  */
1360 static inline int
1361 xfs_rtmount_iread_extents(
1362 	struct xfs_inode	*ip,
1363 	unsigned int		lock_class)
1364 {
1365 	struct xfs_trans	*tp;
1366 	int			error;
1367 
1368 	error = xfs_trans_alloc_empty(ip->i_mount, &tp);
1369 	if (error)
1370 		return error;
1371 
1372 	xfs_ilock(ip, XFS_ILOCK_EXCL | lock_class);
1373 
1374 	error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1375 	if (error)
1376 		goto out_unlock;
1377 
1378 	if (xfs_inode_has_attr_fork(ip)) {
1379 		error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
1380 		if (error)
1381 			goto out_unlock;
1382 	}
1383 
1384 out_unlock:
1385 	xfs_iunlock(ip, XFS_ILOCK_EXCL | lock_class);
1386 	xfs_trans_cancel(tp);
1387 	return error;
1388 }
1389 
1390 /*
1391  * Get the bitmap and summary inodes and the summary cache into the mount
1392  * structure at mount time.
1393  */
1394 int					/* error */
1395 xfs_rtmount_inodes(
1396 	xfs_mount_t	*mp)		/* file system mount structure */
1397 {
1398 	int		error;		/* error return value */
1399 	xfs_sb_t	*sbp;
1400 
1401 	sbp = &mp->m_sb;
1402 	error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
1403 	if (error)
1404 		return error;
1405 	ASSERT(mp->m_rbmip != NULL);
1406 
1407 	error = xfs_rtmount_iread_extents(mp->m_rbmip, XFS_ILOCK_RTBITMAP);
1408 	if (error)
1409 		goto out_rele_bitmap;
1410 
1411 	error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
1412 	if (error)
1413 		goto out_rele_bitmap;
1414 	ASSERT(mp->m_rsumip != NULL);
1415 
1416 	error = xfs_rtmount_iread_extents(mp->m_rsumip, XFS_ILOCK_RTSUM);
1417 	if (error)
1418 		goto out_rele_summary;
1419 
1420 	xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
1421 	return 0;
1422 
1423 out_rele_summary:
1424 	xfs_irele(mp->m_rsumip);
1425 out_rele_bitmap:
1426 	xfs_irele(mp->m_rbmip);
1427 	return error;
1428 }
1429 
1430 void
1431 xfs_rtunmount_inodes(
1432 	struct xfs_mount	*mp)
1433 {
1434 	kmem_free(mp->m_rsum_cache);
1435 	if (mp->m_rbmip)
1436 		xfs_irele(mp->m_rbmip);
1437 	if (mp->m_rsumip)
1438 		xfs_irele(mp->m_rsumip);
1439 }
1440 
1441 /*
1442  * Pick an extent for allocation at the start of a new realtime file.
1443  * Use the sequence number stored in the atime field of the bitmap inode.
1444  * Translate this to a fraction of the rtextents, and return the product
1445  * of rtextents and the fraction.
1446  * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1447  */
1448 int					/* error */
1449 xfs_rtpick_extent(
1450 	xfs_mount_t	*mp,		/* file system mount point */
1451 	xfs_trans_t	*tp,		/* transaction pointer */
1452 	xfs_extlen_t	len,		/* allocation length (rtextents) */
1453 	xfs_rtblock_t	*pick)		/* result rt extent */
1454 {
1455 	xfs_rtblock_t	b;		/* result block */
1456 	int		log2;		/* log of sequence number */
1457 	uint64_t	resid;		/* residual after log removed */
1458 	uint64_t	seq;		/* sequence number of file creation */
1459 	uint64_t	*seqp;		/* pointer to seqno in inode */
1460 
1461 	ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1462 
1463 	seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
1464 	if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
1465 		mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1466 		*seqp = 0;
1467 	}
1468 	seq = *seqp;
1469 	if ((log2 = xfs_highbit64(seq)) == -1)
1470 		b = 0;
1471 	else {
1472 		resid = seq - (1ULL << log2);
1473 		b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1474 		    (log2 + 1);
1475 		if (b >= mp->m_sb.sb_rextents)
1476 			div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
1477 		if (b + len > mp->m_sb.sb_rextents)
1478 			b = mp->m_sb.sb_rextents - len;
1479 	}
1480 	*seqp = seq + 1;
1481 	xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1482 	*pick = b;
1483 	return 0;
1484 }
1485