xref: /openbmc/linux/fs/xfs/xfs_rtalloc.c (revision 49c23519)
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 && nmap < 1)
844 			error = -ENOSPC;
845 		if (error)
846 			goto out_trans_cancel;
847 		/*
848 		 * Free any blocks freed up in the transaction, then commit.
849 		 */
850 		error = xfs_trans_commit(tp);
851 		if (error)
852 			return error;
853 		/*
854 		 * Now we need to clear the allocated blocks.
855 		 * Do this one block per transaction, to keep it simple.
856 		 */
857 		for (bno = map.br_startoff, fsbno = map.br_startblock;
858 		     bno < map.br_startoff + map.br_blockcount;
859 		     bno++, fsbno++) {
860 			/*
861 			 * Reserve log for one block zeroing.
862 			 */
863 			error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero,
864 					0, 0, 0, &tp);
865 			if (error)
866 				return error;
867 			/*
868 			 * Lock the bitmap inode.
869 			 */
870 			xfs_ilock(ip, XFS_ILOCK_EXCL);
871 			xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
872 			/*
873 			 * Get a buffer for the block.
874 			 */
875 			d = XFS_FSB_TO_DADDR(mp, fsbno);
876 			error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
877 					mp->m_bsize, 0, &bp);
878 			if (error)
879 				goto out_trans_cancel;
880 
881 			xfs_trans_buf_set_type(tp, bp, buf_type);
882 			bp->b_ops = &xfs_rtbuf_ops;
883 			memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
884 			xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
885 			/*
886 			 * Commit the transaction.
887 			 */
888 			error = xfs_trans_commit(tp);
889 			if (error)
890 				return error;
891 		}
892 		/*
893 		 * Go on to the next extent, if any.
894 		 */
895 		oblocks = map.br_startoff + map.br_blockcount;
896 	}
897 
898 	return 0;
899 
900 out_trans_cancel:
901 	xfs_trans_cancel(tp);
902 	return error;
903 }
904 
905 static void
906 xfs_alloc_rsum_cache(
907 	xfs_mount_t	*mp,		/* file system mount structure */
908 	xfs_extlen_t	rbmblocks)	/* number of rt bitmap blocks */
909 {
910 	/*
911 	 * The rsum cache is initialized to all zeroes, which is trivially a
912 	 * lower bound on the minimum level with any free extents. We can
913 	 * continue without the cache if it couldn't be allocated.
914 	 */
915 	mp->m_rsum_cache = kvzalloc(rbmblocks, GFP_KERNEL);
916 	if (!mp->m_rsum_cache)
917 		xfs_warn(mp, "could not allocate realtime summary cache");
918 }
919 
920 /*
921  * Visible (exported) functions.
922  */
923 
924 /*
925  * Grow the realtime area of the filesystem.
926  */
927 int
928 xfs_growfs_rt(
929 	xfs_mount_t	*mp,		/* mount point for filesystem */
930 	xfs_growfs_rt_t	*in)		/* growfs rt input struct */
931 {
932 	xfs_rtblock_t	bmbno;		/* bitmap block number */
933 	struct xfs_buf	*bp;		/* temporary buffer */
934 	int		error;		/* error return value */
935 	xfs_mount_t	*nmp;		/* new (fake) mount structure */
936 	xfs_rfsblock_t	nrblocks;	/* new number of realtime blocks */
937 	xfs_extlen_t	nrbmblocks;	/* new number of rt bitmap blocks */
938 	xfs_rtblock_t	nrextents;	/* new number of realtime extents */
939 	uint8_t		nrextslog;	/* new log2 of sb_rextents */
940 	xfs_extlen_t	nrsumblocks;	/* new number of summary blocks */
941 	uint		nrsumlevels;	/* new rt summary levels */
942 	uint		nrsumsize;	/* new size of rt summary, bytes */
943 	xfs_sb_t	*nsbp;		/* new superblock */
944 	xfs_extlen_t	rbmblocks;	/* current number of rt bitmap blocks */
945 	xfs_extlen_t	rsumblocks;	/* current number of rt summary blks */
946 	xfs_sb_t	*sbp;		/* old superblock */
947 	xfs_fsblock_t	sumbno;		/* summary block number */
948 	uint8_t		*rsum_cache;	/* old summary cache */
949 
950 	sbp = &mp->m_sb;
951 
952 	if (!capable(CAP_SYS_ADMIN))
953 		return -EPERM;
954 
955 	/* Needs to have been mounted with an rt device. */
956 	if (!XFS_IS_REALTIME_MOUNT(mp))
957 		return -EINVAL;
958 	/*
959 	 * Mount should fail if the rt bitmap/summary files don't load, but
960 	 * we'll check anyway.
961 	 */
962 	if (!mp->m_rbmip || !mp->m_rsumip)
963 		return -EINVAL;
964 
965 	/* Shrink not supported. */
966 	if (in->newblocks <= sbp->sb_rblocks)
967 		return -EINVAL;
968 
969 	/* Can only change rt extent size when adding rt volume. */
970 	if (sbp->sb_rblocks > 0 && in->extsize != sbp->sb_rextsize)
971 		return -EINVAL;
972 
973 	/* Range check the extent size. */
974 	if (XFS_FSB_TO_B(mp, in->extsize) > XFS_MAX_RTEXTSIZE ||
975 	    XFS_FSB_TO_B(mp, in->extsize) < XFS_MIN_RTEXTSIZE)
976 		return -EINVAL;
977 
978 	/* Unsupported realtime features. */
979 	if (xfs_has_rmapbt(mp) || xfs_has_reflink(mp) || xfs_has_quota(mp))
980 		return -EOPNOTSUPP;
981 
982 	nrblocks = in->newblocks;
983 	error = xfs_sb_validate_fsb_count(sbp, nrblocks);
984 	if (error)
985 		return error;
986 	/*
987 	 * Read in the last block of the device, make sure it exists.
988 	 */
989 	error = xfs_buf_read_uncached(mp->m_rtdev_targp,
990 				XFS_FSB_TO_BB(mp, nrblocks - 1),
991 				XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
992 	if (error)
993 		return error;
994 	xfs_buf_relse(bp);
995 
996 	/*
997 	 * Calculate new parameters.  These are the final values to be reached.
998 	 */
999 	nrextents = nrblocks;
1000 	do_div(nrextents, in->extsize);
1001 	if (!xfs_validate_rtextents(nrextents))
1002 		return -EINVAL;
1003 	nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1004 	nrextslog = xfs_compute_rextslog(nrextents);
1005 	nrsumlevels = nrextslog + 1;
1006 	nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1007 	nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1008 	nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1009 	/*
1010 	 * New summary size can't be more than half the size of
1011 	 * the log.  This prevents us from getting a log overflow,
1012 	 * since we'll log basically the whole summary file at once.
1013 	 */
1014 	if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1015 		return -EINVAL;
1016 	/*
1017 	 * Get the old block counts for bitmap and summary inodes.
1018 	 * These can't change since other growfs callers are locked out.
1019 	 */
1020 	rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_disk_size);
1021 	rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_disk_size);
1022 	/*
1023 	 * Allocate space to the bitmap and summary files, as necessary.
1024 	 */
1025 	error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
1026 	if (error)
1027 		return error;
1028 	error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
1029 	if (error)
1030 		return error;
1031 
1032 	rsum_cache = mp->m_rsum_cache;
1033 	if (nrbmblocks != sbp->sb_rbmblocks)
1034 		xfs_alloc_rsum_cache(mp, nrbmblocks);
1035 
1036 	/*
1037 	 * Allocate a new (fake) mount/sb.
1038 	 */
1039 	nmp = kmem_alloc(sizeof(*nmp), 0);
1040 	/*
1041 	 * Loop over the bitmap blocks.
1042 	 * We will do everything one bitmap block at a time.
1043 	 * Skip the current block if it is exactly full.
1044 	 * This also deals with the case where there were no rtextents before.
1045 	 */
1046 	for (bmbno = sbp->sb_rbmblocks -
1047 		     ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1048 	     bmbno < nrbmblocks;
1049 	     bmbno++) {
1050 		struct xfs_trans	*tp;
1051 		xfs_rfsblock_t		nrblocks_step;
1052 
1053 		*nmp = *mp;
1054 		nsbp = &nmp->m_sb;
1055 		/*
1056 		 * Calculate new sb and mount fields for this round.
1057 		 */
1058 		nsbp->sb_rextsize = in->extsize;
1059 		nsbp->sb_rbmblocks = bmbno + 1;
1060 		nrblocks_step = (bmbno + 1) * NBBY * nsbp->sb_blocksize *
1061 				nsbp->sb_rextsize;
1062 		nsbp->sb_rblocks = min(nrblocks, nrblocks_step);
1063 		nsbp->sb_rextents = nsbp->sb_rblocks;
1064 		do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1065 		ASSERT(nsbp->sb_rextents != 0);
1066 		nsbp->sb_rextslog = xfs_compute_rextslog(nsbp->sb_rextents);
1067 		nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1068 		nrsumsize =
1069 			(uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1070 			nsbp->sb_rbmblocks;
1071 		nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1072 		nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1073 		/* recompute growfsrt reservation from new rsumsize */
1074 		xfs_trans_resv_calc(nmp, &nmp->m_resv);
1075 
1076 		/*
1077 		 * Start a transaction, get the log reservation.
1078 		 */
1079 		error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtfree, 0, 0, 0,
1080 				&tp);
1081 		if (error)
1082 			break;
1083 		/*
1084 		 * Lock out other callers by grabbing the bitmap inode lock.
1085 		 */
1086 		xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
1087 		xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
1088 		/*
1089 		 * Update the bitmap inode's size ondisk and incore.  We need
1090 		 * to update the incore size so that inode inactivation won't
1091 		 * punch what it thinks are "posteof" blocks.
1092 		 */
1093 		mp->m_rbmip->i_disk_size =
1094 			nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1095 		i_size_write(VFS_I(mp->m_rbmip), mp->m_rbmip->i_disk_size);
1096 		xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1097 		/*
1098 		 * Get the summary inode into the transaction.
1099 		 */
1100 		xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
1101 		xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1102 		/*
1103 		 * Update the summary inode's size.  We need to update the
1104 		 * incore size so that inode inactivation won't punch what it
1105 		 * thinks are "posteof" blocks.
1106 		 */
1107 		mp->m_rsumip->i_disk_size = nmp->m_rsumsize;
1108 		i_size_write(VFS_I(mp->m_rsumip), mp->m_rsumip->i_disk_size);
1109 		xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1110 		/*
1111 		 * Copy summary data from old to new sizes.
1112 		 * Do this when the real size (not block-aligned) changes.
1113 		 */
1114 		if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1115 		    mp->m_rsumlevels != nmp->m_rsumlevels) {
1116 			error = xfs_rtcopy_summary(mp, nmp, tp);
1117 			if (error)
1118 				goto error_cancel;
1119 		}
1120 		/*
1121 		 * Update superblock fields.
1122 		 */
1123 		if (nsbp->sb_rextsize != sbp->sb_rextsize)
1124 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
1125 				nsbp->sb_rextsize - sbp->sb_rextsize);
1126 		if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
1127 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
1128 				nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
1129 		if (nsbp->sb_rblocks != sbp->sb_rblocks)
1130 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
1131 				nsbp->sb_rblocks - sbp->sb_rblocks);
1132 		if (nsbp->sb_rextents != sbp->sb_rextents)
1133 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
1134 				nsbp->sb_rextents - sbp->sb_rextents);
1135 		if (nsbp->sb_rextslog != sbp->sb_rextslog)
1136 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
1137 				nsbp->sb_rextslog - sbp->sb_rextslog);
1138 		/*
1139 		 * Free new extent.
1140 		 */
1141 		bp = NULL;
1142 		error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
1143 			nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
1144 		if (error) {
1145 error_cancel:
1146 			xfs_trans_cancel(tp);
1147 			break;
1148 		}
1149 		/*
1150 		 * Mark more blocks free in the superblock.
1151 		 */
1152 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
1153 			nsbp->sb_rextents - sbp->sb_rextents);
1154 		/*
1155 		 * Update mp values into the real mp structure.
1156 		 */
1157 		mp->m_rsumlevels = nrsumlevels;
1158 		mp->m_rsumsize = nrsumsize;
1159 		/* recompute growfsrt reservation from new rsumsize */
1160 		xfs_trans_resv_calc(mp, &mp->m_resv);
1161 
1162 		error = xfs_trans_commit(tp);
1163 		if (error)
1164 			break;
1165 
1166 		/* Ensure the mount RT feature flag is now set. */
1167 		mp->m_features |= XFS_FEAT_REALTIME;
1168 	}
1169 	if (error)
1170 		goto out_free;
1171 
1172 	/* Update secondary superblocks now the physical grow has completed */
1173 	error = xfs_update_secondary_sbs(mp);
1174 
1175 out_free:
1176 	/*
1177 	 * Free the fake mp structure.
1178 	 */
1179 	kmem_free(nmp);
1180 
1181 	/*
1182 	 * If we had to allocate a new rsum_cache, we either need to free the
1183 	 * old one (if we succeeded) or free the new one and restore the old one
1184 	 * (if there was an error).
1185 	 */
1186 	if (rsum_cache != mp->m_rsum_cache) {
1187 		if (error) {
1188 			kmem_free(mp->m_rsum_cache);
1189 			mp->m_rsum_cache = rsum_cache;
1190 		} else {
1191 			kmem_free(rsum_cache);
1192 		}
1193 	}
1194 
1195 	return error;
1196 }
1197 
1198 /*
1199  * Allocate an extent in the realtime subvolume, with the usual allocation
1200  * parameters.  The length units are all in realtime extents, as is the
1201  * result block number.
1202  */
1203 int					/* error */
1204 xfs_rtallocate_extent(
1205 	xfs_trans_t	*tp,		/* transaction pointer */
1206 	xfs_rtblock_t	bno,		/* starting block number to allocate */
1207 	xfs_extlen_t	minlen,		/* minimum length to allocate */
1208 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
1209 	xfs_extlen_t	*len,		/* out: actual length allocated */
1210 	int		wasdel,		/* was a delayed allocation extent */
1211 	xfs_extlen_t	prod,		/* extent product factor */
1212 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
1213 {
1214 	xfs_mount_t	*mp = tp->t_mountp;
1215 	int		error;		/* error value */
1216 	xfs_rtblock_t	r;		/* result allocated block */
1217 	xfs_fsblock_t	sb;		/* summary file block number */
1218 	struct xfs_buf	*sumbp;		/* summary file block buffer */
1219 
1220 	ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1221 	ASSERT(minlen > 0 && minlen <= maxlen);
1222 
1223 	/*
1224 	 * If prod is set then figure out what to do to minlen and maxlen.
1225 	 */
1226 	if (prod > 1) {
1227 		xfs_extlen_t	i;
1228 
1229 		if ((i = maxlen % prod))
1230 			maxlen -= i;
1231 		if ((i = minlen % prod))
1232 			minlen += prod - i;
1233 		if (maxlen < minlen) {
1234 			*rtblock = NULLRTBLOCK;
1235 			return 0;
1236 		}
1237 	}
1238 
1239 retry:
1240 	sumbp = NULL;
1241 	if (bno == 0) {
1242 		error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
1243 				&sumbp,	&sb, prod, &r);
1244 	} else {
1245 		error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
1246 				len, &sumbp, &sb, prod, &r);
1247 	}
1248 
1249 	if (error)
1250 		return error;
1251 
1252 	/*
1253 	 * If it worked, update the superblock.
1254 	 */
1255 	if (r != NULLRTBLOCK) {
1256 		long	slen = (long)*len;
1257 
1258 		ASSERT(*len >= minlen && *len <= maxlen);
1259 		if (wasdel)
1260 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
1261 		else
1262 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
1263 	} else if (prod > 1) {
1264 		prod = 1;
1265 		goto retry;
1266 	}
1267 
1268 	*rtblock = r;
1269 	return 0;
1270 }
1271 
1272 /*
1273  * Initialize realtime fields in the mount structure.
1274  */
1275 int				/* error */
1276 xfs_rtmount_init(
1277 	struct xfs_mount	*mp)	/* file system mount structure */
1278 {
1279 	struct xfs_buf		*bp;	/* buffer for last block of subvolume */
1280 	struct xfs_sb		*sbp;	/* filesystem superblock copy in mount */
1281 	xfs_daddr_t		d;	/* address of last block of subvolume */
1282 	int			error;
1283 
1284 	sbp = &mp->m_sb;
1285 	if (sbp->sb_rblocks == 0)
1286 		return 0;
1287 	if (mp->m_rtdev_targp == NULL) {
1288 		xfs_warn(mp,
1289 	"Filesystem has a realtime volume, use rtdev=device option");
1290 		return -ENODEV;
1291 	}
1292 	mp->m_rsumlevels = sbp->sb_rextslog + 1;
1293 	mp->m_rsumsize =
1294 		(uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
1295 		sbp->sb_rbmblocks;
1296 	mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
1297 	mp->m_rbmip = mp->m_rsumip = NULL;
1298 	/*
1299 	 * Check that the realtime section is an ok size.
1300 	 */
1301 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1302 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
1303 		xfs_warn(mp, "realtime mount -- %llu != %llu",
1304 			(unsigned long long) XFS_BB_TO_FSB(mp, d),
1305 			(unsigned long long) mp->m_sb.sb_rblocks);
1306 		return -EFBIG;
1307 	}
1308 	error = xfs_buf_read_uncached(mp->m_rtdev_targp,
1309 					d - XFS_FSB_TO_BB(mp, 1),
1310 					XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
1311 	if (error) {
1312 		xfs_warn(mp, "realtime device size check failed");
1313 		return error;
1314 	}
1315 	xfs_buf_relse(bp);
1316 	return 0;
1317 }
1318 
1319 static int
1320 xfs_rtalloc_count_frextent(
1321 	struct xfs_mount		*mp,
1322 	struct xfs_trans		*tp,
1323 	const struct xfs_rtalloc_rec	*rec,
1324 	void				*priv)
1325 {
1326 	uint64_t			*valp = priv;
1327 
1328 	*valp += rec->ar_extcount;
1329 	return 0;
1330 }
1331 
1332 /*
1333  * Reinitialize the number of free realtime extents from the realtime bitmap.
1334  * Callers must ensure that there is no other activity in the filesystem.
1335  */
1336 int
1337 xfs_rtalloc_reinit_frextents(
1338 	struct xfs_mount	*mp)
1339 {
1340 	uint64_t		val = 0;
1341 	int			error;
1342 
1343 	xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
1344 	error = xfs_rtalloc_query_all(mp, NULL, xfs_rtalloc_count_frextent,
1345 			&val);
1346 	xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
1347 	if (error)
1348 		return error;
1349 
1350 	spin_lock(&mp->m_sb_lock);
1351 	mp->m_sb.sb_frextents = val;
1352 	spin_unlock(&mp->m_sb_lock);
1353 	percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
1354 	return 0;
1355 }
1356 
1357 /*
1358  * Read in the bmbt of an rt metadata inode so that we never have to load them
1359  * at runtime.  This enables the use of shared ILOCKs for rtbitmap scans.  Use
1360  * an empty transaction to avoid deadlocking on loops in the bmbt.
1361  */
1362 static inline int
1363 xfs_rtmount_iread_extents(
1364 	struct xfs_inode	*ip,
1365 	unsigned int		lock_class)
1366 {
1367 	struct xfs_trans	*tp;
1368 	int			error;
1369 
1370 	error = xfs_trans_alloc_empty(ip->i_mount, &tp);
1371 	if (error)
1372 		return error;
1373 
1374 	xfs_ilock(ip, XFS_ILOCK_EXCL | lock_class);
1375 
1376 	error = xfs_iread_extents(tp, ip, XFS_DATA_FORK);
1377 	if (error)
1378 		goto out_unlock;
1379 
1380 	if (xfs_inode_has_attr_fork(ip)) {
1381 		error = xfs_iread_extents(tp, ip, XFS_ATTR_FORK);
1382 		if (error)
1383 			goto out_unlock;
1384 	}
1385 
1386 out_unlock:
1387 	xfs_iunlock(ip, XFS_ILOCK_EXCL | lock_class);
1388 	xfs_trans_cancel(tp);
1389 	return error;
1390 }
1391 
1392 /*
1393  * Get the bitmap and summary inodes and the summary cache into the mount
1394  * structure at mount time.
1395  */
1396 int					/* error */
1397 xfs_rtmount_inodes(
1398 	xfs_mount_t	*mp)		/* file system mount structure */
1399 {
1400 	int		error;		/* error return value */
1401 	xfs_sb_t	*sbp;
1402 
1403 	sbp = &mp->m_sb;
1404 	error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
1405 	if (error)
1406 		return error;
1407 	ASSERT(mp->m_rbmip != NULL);
1408 
1409 	error = xfs_rtmount_iread_extents(mp->m_rbmip, XFS_ILOCK_RTBITMAP);
1410 	if (error)
1411 		goto out_rele_bitmap;
1412 
1413 	error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
1414 	if (error)
1415 		goto out_rele_bitmap;
1416 	ASSERT(mp->m_rsumip != NULL);
1417 
1418 	error = xfs_rtmount_iread_extents(mp->m_rsumip, XFS_ILOCK_RTSUM);
1419 	if (error)
1420 		goto out_rele_summary;
1421 
1422 	xfs_alloc_rsum_cache(mp, sbp->sb_rbmblocks);
1423 	return 0;
1424 
1425 out_rele_summary:
1426 	xfs_irele(mp->m_rsumip);
1427 out_rele_bitmap:
1428 	xfs_irele(mp->m_rbmip);
1429 	return error;
1430 }
1431 
1432 void
1433 xfs_rtunmount_inodes(
1434 	struct xfs_mount	*mp)
1435 {
1436 	kmem_free(mp->m_rsum_cache);
1437 	if (mp->m_rbmip)
1438 		xfs_irele(mp->m_rbmip);
1439 	if (mp->m_rsumip)
1440 		xfs_irele(mp->m_rsumip);
1441 }
1442 
1443 /*
1444  * Pick an extent for allocation at the start of a new realtime file.
1445  * Use the sequence number stored in the atime field of the bitmap inode.
1446  * Translate this to a fraction of the rtextents, and return the product
1447  * of rtextents and the fraction.
1448  * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
1449  */
1450 int					/* error */
1451 xfs_rtpick_extent(
1452 	xfs_mount_t	*mp,		/* file system mount point */
1453 	xfs_trans_t	*tp,		/* transaction pointer */
1454 	xfs_extlen_t	len,		/* allocation length (rtextents) */
1455 	xfs_rtblock_t	*pick)		/* result rt extent */
1456 {
1457 	xfs_rtblock_t	b;		/* result block */
1458 	int		log2;		/* log of sequence number */
1459 	uint64_t	resid;		/* residual after log removed */
1460 	uint64_t	seq;		/* sequence number of file creation */
1461 	uint64_t	*seqp;		/* pointer to seqno in inode */
1462 
1463 	ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
1464 
1465 	seqp = (uint64_t *)&VFS_I(mp->m_rbmip)->i_atime;
1466 	if (!(mp->m_rbmip->i_diflags & XFS_DIFLAG_NEWRTBM)) {
1467 		mp->m_rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1468 		*seqp = 0;
1469 	}
1470 	seq = *seqp;
1471 	if ((log2 = xfs_highbit64(seq)) == -1)
1472 		b = 0;
1473 	else {
1474 		resid = seq - (1ULL << log2);
1475 		b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
1476 		    (log2 + 1);
1477 		if (b >= mp->m_sb.sb_rextents)
1478 			div64_u64_rem(b, mp->m_sb.sb_rextents, &b);
1479 		if (b + len > mp->m_sb.sb_rextents)
1480 			b = mp->m_sb.sb_rextents - len;
1481 	}
1482 	*seqp = seq + 1;
1483 	xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1484 	*pick = b;
1485 	return 0;
1486 }
1487