xref: /openbmc/linux/fs/xfs/xfs_rtalloc.c (revision a4fbe6ab1e7abecf42b75e9c73701ed33b4ab03b)
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_mount.h"
28 #include "xfs_inode.h"
29 #include "xfs_bmap.h"
30 #include "xfs_bmap_util.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc.h"
33 #include "xfs_error.h"
34 #include "xfs_trans.h"
35 #include "xfs_trans_space.h"
36 #include "xfs_trace.h"
37 #include "xfs_buf.h"
38 #include "xfs_icache.h"
39 #include "xfs_dinode.h"
40 
41 
42 /*
43  * Prototypes for internal functions.
44  */
45 
46 
47 STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
48 		xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
49 STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
50 		xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
51 STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
52 		xfs_extlen_t, int, xfs_rtblock_t *, int *);
53 STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
54 		xfs_rtblock_t, xfs_rtblock_t *);
55 STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
56 		xfs_rtblock_t, xfs_rtblock_t *);
57 STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
58 		xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
59 STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
60 		xfs_extlen_t, int);
61 STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
62 		xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
63 
64 /*
65  * Internal functions.
66  */
67 
68 /*
69  * Allocate space to the bitmap or summary file, and zero it, for growfs.
70  */
71 STATIC int				/* error */
72 xfs_growfs_rt_alloc(
73 	xfs_mount_t	*mp,		/* file system mount point */
74 	xfs_extlen_t	oblocks,	/* old count of blocks */
75 	xfs_extlen_t	nblocks,	/* new count of blocks */
76 	xfs_inode_t	*ip)		/* inode (bitmap/summary) */
77 {
78 	xfs_fileoff_t	bno;		/* block number in file */
79 	xfs_buf_t	*bp;		/* temporary buffer for zeroing */
80 	int		committed;	/* transaction committed flag */
81 	xfs_daddr_t	d;		/* disk block address */
82 	int		error;		/* error return value */
83 	xfs_fsblock_t	firstblock;	/* first block allocated in xaction */
84 	xfs_bmap_free_t	flist;		/* list of freed blocks */
85 	xfs_fsblock_t	fsbno;		/* filesystem block for bno */
86 	xfs_bmbt_irec_t	map;		/* block map output */
87 	int		nmap;		/* number of block maps */
88 	int		resblks;	/* space reservation */
89 
90 	/*
91 	 * Allocate space to the file, as necessary.
92 	 */
93 	while (oblocks < nblocks) {
94 		int		cancelflags = 0;
95 		xfs_trans_t	*tp;
96 
97 		tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
98 		resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
99 		/*
100 		 * Reserve space & log for one extent added to the file.
101 		 */
102 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growdata,
103 					  resblks, 0);
104 		if (error)
105 			goto error_cancel;
106 		cancelflags = XFS_TRANS_RELEASE_LOG_RES;
107 		/*
108 		 * Lock the inode.
109 		 */
110 		xfs_ilock(ip, XFS_ILOCK_EXCL);
111 		xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
112 
113 		xfs_bmap_init(&flist, &firstblock);
114 		/*
115 		 * Allocate blocks to the bitmap file.
116 		 */
117 		nmap = 1;
118 		cancelflags |= XFS_TRANS_ABORT;
119 		error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
120 					XFS_BMAPI_METADATA, &firstblock,
121 					resblks, &map, &nmap, &flist);
122 		if (!error && nmap < 1)
123 			error = XFS_ERROR(ENOSPC);
124 		if (error)
125 			goto error_cancel;
126 		/*
127 		 * Free any blocks freed up in the transaction, then commit.
128 		 */
129 		error = xfs_bmap_finish(&tp, &flist, &committed);
130 		if (error)
131 			goto error_cancel;
132 		error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
133 		if (error)
134 			goto error;
135 		/*
136 		 * Now we need to clear the allocated blocks.
137 		 * Do this one block per transaction, to keep it simple.
138 		 */
139 		cancelflags = 0;
140 		for (bno = map.br_startoff, fsbno = map.br_startblock;
141 		     bno < map.br_startoff + map.br_blockcount;
142 		     bno++, fsbno++) {
143 			tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
144 			/*
145 			 * Reserve log for one block zeroing.
146 			 */
147 			error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
148 						  0, 0);
149 			if (error)
150 				goto error_cancel;
151 			/*
152 			 * Lock the bitmap inode.
153 			 */
154 			xfs_ilock(ip, XFS_ILOCK_EXCL);
155 			xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
156 			/*
157 			 * Get a buffer for the block.
158 			 */
159 			d = XFS_FSB_TO_DADDR(mp, fsbno);
160 			bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
161 				mp->m_bsize, 0);
162 			if (bp == NULL) {
163 				error = XFS_ERROR(EIO);
164 error_cancel:
165 				xfs_trans_cancel(tp, cancelflags);
166 				goto error;
167 			}
168 			memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
169 			xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
170 			/*
171 			 * Commit the transaction.
172 			 */
173 			error = xfs_trans_commit(tp, 0);
174 			if (error)
175 				goto error;
176 		}
177 		/*
178 		 * Go on to the next extent, if any.
179 		 */
180 		oblocks = map.br_startoff + map.br_blockcount;
181 	}
182 	return 0;
183 
184 error:
185 	return error;
186 }
187 
188 /*
189  * Attempt to allocate an extent minlen<=len<=maxlen starting from
190  * bitmap block bbno.  If we don't get maxlen then use prod to trim
191  * the length, if given.  Returns error; returns starting block in *rtblock.
192  * The lengths are all in rtextents.
193  */
194 STATIC int				/* error */
195 xfs_rtallocate_extent_block(
196 	xfs_mount_t	*mp,		/* file system mount point */
197 	xfs_trans_t	*tp,		/* transaction pointer */
198 	xfs_rtblock_t	bbno,		/* bitmap block number */
199 	xfs_extlen_t	minlen,		/* minimum length to allocate */
200 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
201 	xfs_extlen_t	*len,		/* out: actual length allocated */
202 	xfs_rtblock_t	*nextp,		/* out: next block to try */
203 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
204 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
205 	xfs_extlen_t	prod,		/* extent product factor */
206 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
207 {
208 	xfs_rtblock_t	besti;		/* best rtblock found so far */
209 	xfs_rtblock_t	bestlen;	/* best length found so far */
210 	xfs_rtblock_t	end;		/* last rtblock in chunk */
211 	int		error;		/* error value */
212 	xfs_rtblock_t	i;		/* current rtblock trying */
213 	xfs_rtblock_t	next;		/* next rtblock to try */
214 	int		stat;		/* status from internal calls */
215 
216 	/*
217 	 * Loop over all the extents starting in this bitmap block,
218 	 * looking for one that's long enough.
219 	 */
220 	for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
221 		end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
222 	     i <= end;
223 	     i++) {
224 		/*
225 		 * See if there's a free extent of maxlen starting at i.
226 		 * If it's not so then next will contain the first non-free.
227 		 */
228 		error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
229 		if (error) {
230 			return error;
231 		}
232 		if (stat) {
233 			/*
234 			 * i for maxlen is all free, allocate and return that.
235 			 */
236 			error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
237 				rsb);
238 			if (error) {
239 				return error;
240 			}
241 			*len = maxlen;
242 			*rtblock = i;
243 			return 0;
244 		}
245 		/*
246 		 * In the case where we have a variable-sized allocation
247 		 * request, figure out how big this free piece is,
248 		 * and if it's big enough for the minimum, and the best
249 		 * so far, remember it.
250 		 */
251 		if (minlen < maxlen) {
252 			xfs_rtblock_t	thislen;	/* this extent size */
253 
254 			thislen = next - i;
255 			if (thislen >= minlen && thislen > bestlen) {
256 				besti = i;
257 				bestlen = thislen;
258 			}
259 		}
260 		/*
261 		 * If not done yet, find the start of the next free space.
262 		 */
263 		if (next < end) {
264 			error = xfs_rtfind_forw(mp, tp, next, end, &i);
265 			if (error) {
266 				return error;
267 			}
268 		} else
269 			break;
270 	}
271 	/*
272 	 * Searched the whole thing & didn't find a maxlen free extent.
273 	 */
274 	if (minlen < maxlen && besti != -1) {
275 		xfs_extlen_t	p;	/* amount to trim length by */
276 
277 		/*
278 		 * If size should be a multiple of prod, make that so.
279 		 */
280 		if (prod > 1 && (p = do_mod(bestlen, prod)))
281 			bestlen -= p;
282 		/*
283 		 * Allocate besti for bestlen & return that.
284 		 */
285 		error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
286 		if (error) {
287 			return error;
288 		}
289 		*len = bestlen;
290 		*rtblock = besti;
291 		return 0;
292 	}
293 	/*
294 	 * Allocation failed.  Set *nextp to the next block to try.
295 	 */
296 	*nextp = next;
297 	*rtblock = NULLRTBLOCK;
298 	return 0;
299 }
300 
301 /*
302  * Allocate an extent of length minlen<=len<=maxlen, starting at block
303  * bno.  If we don't get maxlen then use prod to trim the length, if given.
304  * Returns error; returns starting block in *rtblock.
305  * The lengths are all in rtextents.
306  */
307 STATIC int				/* error */
308 xfs_rtallocate_extent_exact(
309 	xfs_mount_t	*mp,		/* file system mount point */
310 	xfs_trans_t	*tp,		/* transaction pointer */
311 	xfs_rtblock_t	bno,		/* starting block number to allocate */
312 	xfs_extlen_t	minlen,		/* minimum length to allocate */
313 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
314 	xfs_extlen_t	*len,		/* out: actual length allocated */
315 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
316 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
317 	xfs_extlen_t	prod,		/* extent product factor */
318 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
319 {
320 	int		error;		/* error value */
321 	xfs_extlen_t	i;		/* extent length trimmed due to prod */
322 	int		isfree;		/* extent is free */
323 	xfs_rtblock_t	next;		/* next block to try (dummy) */
324 
325 	ASSERT(minlen % prod == 0 && maxlen % prod == 0);
326 	/*
327 	 * Check if the range in question (for maxlen) is free.
328 	 */
329 	error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
330 	if (error) {
331 		return error;
332 	}
333 	if (isfree) {
334 		/*
335 		 * If it is, allocate it and return success.
336 		 */
337 		error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
338 		if (error) {
339 			return error;
340 		}
341 		*len = maxlen;
342 		*rtblock = bno;
343 		return 0;
344 	}
345 	/*
346 	 * If not, allocate what there is, if it's at least minlen.
347 	 */
348 	maxlen = next - bno;
349 	if (maxlen < minlen) {
350 		/*
351 		 * Failed, return failure status.
352 		 */
353 		*rtblock = NULLRTBLOCK;
354 		return 0;
355 	}
356 	/*
357 	 * Trim off tail of extent, if prod is specified.
358 	 */
359 	if (prod > 1 && (i = maxlen % prod)) {
360 		maxlen -= i;
361 		if (maxlen < minlen) {
362 			/*
363 			 * Now we can't do it, return failure status.
364 			 */
365 			*rtblock = NULLRTBLOCK;
366 			return 0;
367 		}
368 	}
369 	/*
370 	 * Allocate what we can and return it.
371 	 */
372 	error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
373 	if (error) {
374 		return error;
375 	}
376 	*len = maxlen;
377 	*rtblock = bno;
378 	return 0;
379 }
380 
381 /*
382  * Allocate an extent of length minlen<=len<=maxlen, starting as near
383  * to bno as possible.  If we don't get maxlen then use prod to trim
384  * the length, if given.  The lengths are all in rtextents.
385  */
386 STATIC int				/* error */
387 xfs_rtallocate_extent_near(
388 	xfs_mount_t	*mp,		/* file system mount point */
389 	xfs_trans_t	*tp,		/* transaction pointer */
390 	xfs_rtblock_t	bno,		/* starting block number to allocate */
391 	xfs_extlen_t	minlen,		/* minimum length to allocate */
392 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
393 	xfs_extlen_t	*len,		/* out: actual length allocated */
394 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
395 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
396 	xfs_extlen_t	prod,		/* extent product factor */
397 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
398 {
399 	int		any;		/* any useful extents from summary */
400 	xfs_rtblock_t	bbno;		/* bitmap block number */
401 	int		error;		/* error value */
402 	int		i;		/* bitmap block offset (loop control) */
403 	int		j;		/* secondary loop control */
404 	int		log2len;	/* log2 of minlen */
405 	xfs_rtblock_t	n;		/* next block to try */
406 	xfs_rtblock_t	r;		/* result block */
407 
408 	ASSERT(minlen % prod == 0 && maxlen % prod == 0);
409 	/*
410 	 * If the block number given is off the end, silently set it to
411 	 * the last block.
412 	 */
413 	if (bno >= mp->m_sb.sb_rextents)
414 		bno = mp->m_sb.sb_rextents - 1;
415 	/*
416 	 * Try the exact allocation first.
417 	 */
418 	error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
419 		rbpp, rsb, prod, &r);
420 	if (error) {
421 		return error;
422 	}
423 	/*
424 	 * If the exact allocation worked, return that.
425 	 */
426 	if (r != NULLRTBLOCK) {
427 		*rtblock = r;
428 		return 0;
429 	}
430 	bbno = XFS_BITTOBLOCK(mp, bno);
431 	i = 0;
432 	ASSERT(minlen != 0);
433 	log2len = xfs_highbit32(minlen);
434 	/*
435 	 * Loop over all bitmap blocks (bbno + i is current block).
436 	 */
437 	for (;;) {
438 		/*
439 		 * Get summary information of extents of all useful levels
440 		 * starting in this bitmap block.
441 		 */
442 		error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
443 			bbno + i, rbpp, rsb, &any);
444 		if (error) {
445 			return error;
446 		}
447 		/*
448 		 * If there are any useful extents starting here, try
449 		 * allocating one.
450 		 */
451 		if (any) {
452 			/*
453 			 * On the positive side of the starting location.
454 			 */
455 			if (i >= 0) {
456 				/*
457 				 * Try to allocate an extent starting in
458 				 * this block.
459 				 */
460 				error = xfs_rtallocate_extent_block(mp, tp,
461 					bbno + i, minlen, maxlen, len, &n, rbpp,
462 					rsb, prod, &r);
463 				if (error) {
464 					return error;
465 				}
466 				/*
467 				 * If it worked, return it.
468 				 */
469 				if (r != NULLRTBLOCK) {
470 					*rtblock = r;
471 					return 0;
472 				}
473 			}
474 			/*
475 			 * On the negative side of the starting location.
476 			 */
477 			else {		/* i < 0 */
478 				/*
479 				 * Loop backwards through the bitmap blocks from
480 				 * the starting point-1 up to where we are now.
481 				 * There should be an extent which ends in this
482 				 * bitmap block and is long enough.
483 				 */
484 				for (j = -1; j > i; j--) {
485 					/*
486 					 * Grab the summary information for
487 					 * this bitmap block.
488 					 */
489 					error = xfs_rtany_summary(mp, tp,
490 						log2len, mp->m_rsumlevels - 1,
491 						bbno + j, rbpp, rsb, &any);
492 					if (error) {
493 						return error;
494 					}
495 					/*
496 					 * If there's no extent given in the
497 					 * summary that means the extent we
498 					 * found must carry over from an
499 					 * earlier block.  If there is an
500 					 * extent given, we've already tried
501 					 * that allocation, don't do it again.
502 					 */
503 					if (any)
504 						continue;
505 					error = xfs_rtallocate_extent_block(mp,
506 						tp, bbno + j, minlen, maxlen,
507 						len, &n, rbpp, rsb, prod, &r);
508 					if (error) {
509 						return error;
510 					}
511 					/*
512 					 * If it works, return the extent.
513 					 */
514 					if (r != NULLRTBLOCK) {
515 						*rtblock = r;
516 						return 0;
517 					}
518 				}
519 				/*
520 				 * There weren't intervening bitmap blocks
521 				 * with a long enough extent, or the
522 				 * allocation didn't work for some reason
523 				 * (i.e. it's a little * too short).
524 				 * Try to allocate from the summary block
525 				 * that we found.
526 				 */
527 				error = xfs_rtallocate_extent_block(mp, tp,
528 					bbno + i, minlen, maxlen, len, &n, rbpp,
529 					rsb, prod, &r);
530 				if (error) {
531 					return error;
532 				}
533 				/*
534 				 * If it works, return the extent.
535 				 */
536 				if (r != NULLRTBLOCK) {
537 					*rtblock = r;
538 					return 0;
539 				}
540 			}
541 		}
542 		/*
543 		 * Loop control.  If we were on the positive side, and there's
544 		 * still more blocks on the negative side, go there.
545 		 */
546 		if (i > 0 && (int)bbno - i >= 0)
547 			i = -i;
548 		/*
549 		 * If positive, and no more negative, but there are more
550 		 * positive, go there.
551 		 */
552 		else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
553 			i++;
554 		/*
555 		 * If negative or 0 (just started), and there are positive
556 		 * blocks to go, go there.  The 0 case moves to block 1.
557 		 */
558 		else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
559 			i = 1 - i;
560 		/*
561 		 * If negative or 0 and there are more negative blocks,
562 		 * go there.
563 		 */
564 		else if (i <= 0 && (int)bbno + i > 0)
565 			i--;
566 		/*
567 		 * Must be done.  Return failure.
568 		 */
569 		else
570 			break;
571 	}
572 	*rtblock = NULLRTBLOCK;
573 	return 0;
574 }
575 
576 /*
577  * Allocate an extent of length minlen<=len<=maxlen, with no position
578  * specified.  If we don't get maxlen then use prod to trim
579  * the length, if given.  The lengths are all in rtextents.
580  */
581 STATIC int				/* error */
582 xfs_rtallocate_extent_size(
583 	xfs_mount_t	*mp,		/* file system mount point */
584 	xfs_trans_t	*tp,		/* transaction pointer */
585 	xfs_extlen_t	minlen,		/* minimum length to allocate */
586 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
587 	xfs_extlen_t	*len,		/* out: actual length allocated */
588 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
589 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
590 	xfs_extlen_t	prod,		/* extent product factor */
591 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
592 {
593 	int		error;		/* error value */
594 	int		i;		/* bitmap block number */
595 	int		l;		/* level number (loop control) */
596 	xfs_rtblock_t	n;		/* next block to be tried */
597 	xfs_rtblock_t	r;		/* result block number */
598 	xfs_suminfo_t	sum;		/* summary information for extents */
599 
600 	ASSERT(minlen % prod == 0 && maxlen % prod == 0);
601 	ASSERT(maxlen != 0);
602 
603 	/*
604 	 * Loop over all the levels starting with maxlen.
605 	 * At each level, look at all the bitmap blocks, to see if there
606 	 * are extents starting there that are long enough (>= maxlen).
607 	 * Note, only on the initial level can the allocation fail if
608 	 * the summary says there's an extent.
609 	 */
610 	for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
611 		/*
612 		 * Loop over all the bitmap blocks.
613 		 */
614 		for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
615 			/*
616 			 * Get the summary for this level/block.
617 			 */
618 			error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
619 				&sum);
620 			if (error) {
621 				return error;
622 			}
623 			/*
624 			 * Nothing there, on to the next block.
625 			 */
626 			if (!sum)
627 				continue;
628 			/*
629 			 * Try allocating the extent.
630 			 */
631 			error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
632 				maxlen, len, &n, rbpp, rsb, prod, &r);
633 			if (error) {
634 				return error;
635 			}
636 			/*
637 			 * If it worked, return that.
638 			 */
639 			if (r != NULLRTBLOCK) {
640 				*rtblock = r;
641 				return 0;
642 			}
643 			/*
644 			 * If the "next block to try" returned from the
645 			 * allocator is beyond the next bitmap block,
646 			 * skip to that bitmap block.
647 			 */
648 			if (XFS_BITTOBLOCK(mp, n) > i + 1)
649 				i = XFS_BITTOBLOCK(mp, n) - 1;
650 		}
651 	}
652 	/*
653 	 * Didn't find any maxlen blocks.  Try smaller ones, unless
654 	 * we're asking for a fixed size extent.
655 	 */
656 	if (minlen > --maxlen) {
657 		*rtblock = NULLRTBLOCK;
658 		return 0;
659 	}
660 	ASSERT(minlen != 0);
661 	ASSERT(maxlen != 0);
662 
663 	/*
664 	 * Loop over sizes, from maxlen down to minlen.
665 	 * This time, when we do the allocations, allow smaller ones
666 	 * to succeed.
667 	 */
668 	for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
669 		/*
670 		 * Loop over all the bitmap blocks, try an allocation
671 		 * starting in that block.
672 		 */
673 		for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
674 			/*
675 			 * Get the summary information for this level/block.
676 			 */
677 			error =	xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
678 						  &sum);
679 			if (error) {
680 				return error;
681 			}
682 			/*
683 			 * If nothing there, go on to next.
684 			 */
685 			if (!sum)
686 				continue;
687 			/*
688 			 * Try the allocation.  Make sure the specified
689 			 * minlen/maxlen are in the possible range for
690 			 * this summary level.
691 			 */
692 			error = xfs_rtallocate_extent_block(mp, tp, i,
693 					XFS_RTMAX(minlen, 1 << l),
694 					XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
695 					len, &n, rbpp, rsb, prod, &r);
696 			if (error) {
697 				return error;
698 			}
699 			/*
700 			 * If it worked, return that extent.
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 	 * Got nothing, return failure.
717 	 */
718 	*rtblock = NULLRTBLOCK;
719 	return 0;
720 }
721 
722 /*
723  * Mark an extent specified by start and len allocated.
724  * Updates all the summary information as well as the bitmap.
725  */
726 STATIC int				/* error */
727 xfs_rtallocate_range(
728 	xfs_mount_t	*mp,		/* file system mount point */
729 	xfs_trans_t	*tp,		/* transaction pointer */
730 	xfs_rtblock_t	start,		/* start block to allocate */
731 	xfs_extlen_t	len,		/* length to allocate */
732 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
733 	xfs_fsblock_t	*rsb)		/* in/out: summary block number */
734 {
735 	xfs_rtblock_t	end;		/* end of the allocated extent */
736 	int		error;		/* error value */
737 	xfs_rtblock_t	postblock = 0;	/* first block allocated > end */
738 	xfs_rtblock_t	preblock = 0;	/* first block allocated < start */
739 
740 	end = start + len - 1;
741 	/*
742 	 * Assume we're allocating out of the middle of a free extent.
743 	 * We need to find the beginning and end of the extent so we can
744 	 * properly update the summary.
745 	 */
746 	error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
747 	if (error) {
748 		return error;
749 	}
750 	/*
751 	 * Find the next allocated block (end of free extent).
752 	 */
753 	error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
754 		&postblock);
755 	if (error) {
756 		return error;
757 	}
758 	/*
759 	 * Decrement the summary information corresponding to the entire
760 	 * (old) free extent.
761 	 */
762 	error = xfs_rtmodify_summary(mp, tp,
763 		XFS_RTBLOCKLOG(postblock + 1 - preblock),
764 		XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
765 	if (error) {
766 		return error;
767 	}
768 	/*
769 	 * If there are blocks not being allocated at the front of the
770 	 * old extent, add summary data for them to be free.
771 	 */
772 	if (preblock < start) {
773 		error = xfs_rtmodify_summary(mp, tp,
774 			XFS_RTBLOCKLOG(start - preblock),
775 			XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
776 		if (error) {
777 			return error;
778 		}
779 	}
780 	/*
781 	 * If there are blocks not being allocated at the end of the
782 	 * old extent, add summary data for them to be free.
783 	 */
784 	if (postblock > end) {
785 		error = xfs_rtmodify_summary(mp, tp,
786 			XFS_RTBLOCKLOG(postblock - end),
787 			XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
788 		if (error) {
789 			return error;
790 		}
791 	}
792 	/*
793 	 * Modify the bitmap to mark this extent allocated.
794 	 */
795 	error = xfs_rtmodify_range(mp, tp, start, len, 0);
796 	return error;
797 }
798 
799 /*
800  * Return whether there are any free extents in the size range given
801  * by low and high, for the bitmap block bbno.
802  */
803 STATIC int				/* error */
804 xfs_rtany_summary(
805 	xfs_mount_t	*mp,		/* file system mount structure */
806 	xfs_trans_t	*tp,		/* transaction pointer */
807 	int		low,		/* low log2 extent size */
808 	int		high,		/* high log2 extent size */
809 	xfs_rtblock_t	bbno,		/* bitmap block number */
810 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
811 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
812 	int		*stat)		/* out: any good extents here? */
813 {
814 	int		error;		/* error value */
815 	int		log;		/* loop counter, log2 of ext. size */
816 	xfs_suminfo_t	sum;		/* summary data */
817 
818 	/*
819 	 * Loop over logs of extent sizes.  Order is irrelevant.
820 	 */
821 	for (log = low; log <= high; log++) {
822 		/*
823 		 * Get one summary datum.
824 		 */
825 		error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
826 		if (error) {
827 			return error;
828 		}
829 		/*
830 		 * If there are any, return success.
831 		 */
832 		if (sum) {
833 			*stat = 1;
834 			return 0;
835 		}
836 	}
837 	/*
838 	 * Found nothing, return failure.
839 	 */
840 	*stat = 0;
841 	return 0;
842 }
843 
844 /*
845  * Get a buffer for the bitmap or summary file block specified.
846  * The buffer is returned read and locked.
847  */
848 STATIC int				/* error */
849 xfs_rtbuf_get(
850 	xfs_mount_t	*mp,		/* file system mount structure */
851 	xfs_trans_t	*tp,		/* transaction pointer */
852 	xfs_rtblock_t	block,		/* block number in bitmap or summary */
853 	int		issum,		/* is summary not bitmap */
854 	xfs_buf_t	**bpp)		/* output: buffer for the block */
855 {
856 	xfs_buf_t	*bp;		/* block buffer, result */
857 	xfs_inode_t	*ip;		/* bitmap or summary inode */
858 	xfs_bmbt_irec_t	map;
859 	int		nmap = 1;
860 	int		error;		/* error value */
861 
862 	ip = issum ? mp->m_rsumip : mp->m_rbmip;
863 
864 	error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
865 	if (error)
866 		return error;
867 
868 	ASSERT(map.br_startblock != NULLFSBLOCK);
869 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
870 				   XFS_FSB_TO_DADDR(mp, map.br_startblock),
871 				   mp->m_bsize, 0, &bp, NULL);
872 	if (error)
873 		return error;
874 	ASSERT(!xfs_buf_geterror(bp));
875 	*bpp = bp;
876 	return 0;
877 }
878 
879 #ifdef DEBUG
880 /*
881  * Check that the given extent (block range) is allocated already.
882  */
883 STATIC int				/* error */
884 xfs_rtcheck_alloc_range(
885 	xfs_mount_t	*mp,		/* file system mount point */
886 	xfs_trans_t	*tp,		/* transaction pointer */
887 	xfs_rtblock_t	bno,		/* starting block number of extent */
888 	xfs_extlen_t	len,		/* length of extent */
889 	int		*stat)		/* out: 1 for allocated, 0 for not */
890 {
891 	xfs_rtblock_t	new;		/* dummy for xfs_rtcheck_range */
892 
893 	return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
894 }
895 #endif
896 
897 /*
898  * Check that the given range is either all allocated (val = 0) or
899  * all free (val = 1).
900  */
901 STATIC int				/* error */
902 xfs_rtcheck_range(
903 	xfs_mount_t	*mp,		/* file system mount point */
904 	xfs_trans_t	*tp,		/* transaction pointer */
905 	xfs_rtblock_t	start,		/* starting block number of extent */
906 	xfs_extlen_t	len,		/* length of extent */
907 	int		val,		/* 1 for free, 0 for allocated */
908 	xfs_rtblock_t	*new,		/* out: first block not matching */
909 	int		*stat)		/* out: 1 for matches, 0 for not */
910 {
911 	xfs_rtword_t	*b;		/* current word in buffer */
912 	int		bit;		/* bit number in the word */
913 	xfs_rtblock_t	block;		/* bitmap block number */
914 	xfs_buf_t	*bp;		/* buf for the block */
915 	xfs_rtword_t	*bufp;		/* starting word in buffer */
916 	int		error;		/* error value */
917 	xfs_rtblock_t	i;		/* current bit number rel. to start */
918 	xfs_rtblock_t	lastbit;	/* last useful bit in word */
919 	xfs_rtword_t	mask;		/* mask of relevant bits for value */
920 	xfs_rtword_t	wdiff;		/* difference from wanted value */
921 	int		word;		/* word number in the buffer */
922 
923 	/*
924 	 * Compute starting bitmap block number
925 	 */
926 	block = XFS_BITTOBLOCK(mp, start);
927 	/*
928 	 * Read the bitmap block.
929 	 */
930 	error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
931 	if (error) {
932 		return error;
933 	}
934 	bufp = bp->b_addr;
935 	/*
936 	 * Compute the starting word's address, and starting bit.
937 	 */
938 	word = XFS_BITTOWORD(mp, start);
939 	b = &bufp[word];
940 	bit = (int)(start & (XFS_NBWORD - 1));
941 	/*
942 	 * 0 (allocated) => all zero's; 1 (free) => all one's.
943 	 */
944 	val = -val;
945 	/*
946 	 * If not starting on a word boundary, deal with the first
947 	 * (partial) word.
948 	 */
949 	if (bit) {
950 		/*
951 		 * Compute first bit not examined.
952 		 */
953 		lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
954 		/*
955 		 * Mask of relevant bits.
956 		 */
957 		mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
958 		/*
959 		 * Compute difference between actual and desired value.
960 		 */
961 		if ((wdiff = (*b ^ val) & mask)) {
962 			/*
963 			 * Different, compute first wrong bit and return.
964 			 */
965 			xfs_trans_brelse(tp, bp);
966 			i = XFS_RTLOBIT(wdiff) - bit;
967 			*new = start + i;
968 			*stat = 0;
969 			return 0;
970 		}
971 		i = lastbit - bit;
972 		/*
973 		 * Go on to next block if that's where the next word is
974 		 * and we need the next word.
975 		 */
976 		if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
977 			/*
978 			 * If done with this block, get the next one.
979 			 */
980 			xfs_trans_brelse(tp, bp);
981 			error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
982 			if (error) {
983 				return error;
984 			}
985 			b = bufp = bp->b_addr;
986 			word = 0;
987 		} else {
988 			/*
989 			 * Go on to the next word in the buffer.
990 			 */
991 			b++;
992 		}
993 	} else {
994 		/*
995 		 * Starting on a word boundary, no partial word.
996 		 */
997 		i = 0;
998 	}
999 	/*
1000 	 * Loop over whole words in buffers.  When we use up one buffer
1001 	 * we move on to the next one.
1002 	 */
1003 	while (len - i >= XFS_NBWORD) {
1004 		/*
1005 		 * Compute difference between actual and desired value.
1006 		 */
1007 		if ((wdiff = *b ^ val)) {
1008 			/*
1009 			 * Different, compute first wrong bit and return.
1010 			 */
1011 			xfs_trans_brelse(tp, bp);
1012 			i += XFS_RTLOBIT(wdiff);
1013 			*new = start + i;
1014 			*stat = 0;
1015 			return 0;
1016 		}
1017 		i += XFS_NBWORD;
1018 		/*
1019 		 * Go on to next block if that's where the next word is
1020 		 * and we need the next word.
1021 		 */
1022 		if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1023 			/*
1024 			 * If done with this block, get the next one.
1025 			 */
1026 			xfs_trans_brelse(tp, bp);
1027 			error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1028 			if (error) {
1029 				return error;
1030 			}
1031 			b = bufp = bp->b_addr;
1032 			word = 0;
1033 		} else {
1034 			/*
1035 			 * Go on to the next word in the buffer.
1036 			 */
1037 			b++;
1038 		}
1039 	}
1040 	/*
1041 	 * If not ending on a word boundary, deal with the last
1042 	 * (partial) word.
1043 	 */
1044 	if ((lastbit = len - i)) {
1045 		/*
1046 		 * Mask of relevant bits.
1047 		 */
1048 		mask = ((xfs_rtword_t)1 << lastbit) - 1;
1049 		/*
1050 		 * Compute difference between actual and desired value.
1051 		 */
1052 		if ((wdiff = (*b ^ val) & mask)) {
1053 			/*
1054 			 * Different, compute first wrong bit and return.
1055 			 */
1056 			xfs_trans_brelse(tp, bp);
1057 			i += XFS_RTLOBIT(wdiff);
1058 			*new = start + i;
1059 			*stat = 0;
1060 			return 0;
1061 		} else
1062 			i = len;
1063 	}
1064 	/*
1065 	 * Successful, return.
1066 	 */
1067 	xfs_trans_brelse(tp, bp);
1068 	*new = start + i;
1069 	*stat = 1;
1070 	return 0;
1071 }
1072 
1073 /*
1074  * Copy and transform the summary file, given the old and new
1075  * parameters in the mount structures.
1076  */
1077 STATIC int				/* error */
1078 xfs_rtcopy_summary(
1079 	xfs_mount_t	*omp,		/* old file system mount point */
1080 	xfs_mount_t	*nmp,		/* new file system mount point */
1081 	xfs_trans_t	*tp)		/* transaction pointer */
1082 {
1083 	xfs_rtblock_t	bbno;		/* bitmap block number */
1084 	xfs_buf_t	*bp;		/* summary buffer */
1085 	int		error;		/* error return value */
1086 	int		log;		/* summary level number (log length) */
1087 	xfs_suminfo_t	sum;		/* summary data */
1088 	xfs_fsblock_t	sumbno;		/* summary block number */
1089 
1090 	bp = NULL;
1091 	for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
1092 		for (bbno = omp->m_sb.sb_rbmblocks - 1;
1093 		     (xfs_srtblock_t)bbno >= 0;
1094 		     bbno--) {
1095 			error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
1096 				&sumbno, &sum);
1097 			if (error)
1098 				return error;
1099 			if (sum == 0)
1100 				continue;
1101 			error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
1102 				&bp, &sumbno);
1103 			if (error)
1104 				return error;
1105 			error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
1106 				&bp, &sumbno);
1107 			if (error)
1108 				return error;
1109 			ASSERT(sum > 0);
1110 		}
1111 	}
1112 	return 0;
1113 }
1114 
1115 /*
1116  * Searching backward from start to limit, find the first block whose
1117  * allocated/free state is different from start's.
1118  */
1119 STATIC int				/* error */
1120 xfs_rtfind_back(
1121 	xfs_mount_t	*mp,		/* file system mount point */
1122 	xfs_trans_t	*tp,		/* transaction pointer */
1123 	xfs_rtblock_t	start,		/* starting block to look at */
1124 	xfs_rtblock_t	limit,		/* last block to look at */
1125 	xfs_rtblock_t	*rtblock)	/* out: start block found */
1126 {
1127 	xfs_rtword_t	*b;		/* current word in buffer */
1128 	int		bit;		/* bit number in the word */
1129 	xfs_rtblock_t	block;		/* bitmap block number */
1130 	xfs_buf_t	*bp;		/* buf for the block */
1131 	xfs_rtword_t	*bufp;		/* starting word in buffer */
1132 	int		error;		/* error value */
1133 	xfs_rtblock_t	firstbit;	/* first useful bit in the word */
1134 	xfs_rtblock_t	i;		/* current bit number rel. to start */
1135 	xfs_rtblock_t	len;		/* length of inspected area */
1136 	xfs_rtword_t	mask;		/* mask of relevant bits for value */
1137 	xfs_rtword_t	want;		/* mask for "good" values */
1138 	xfs_rtword_t	wdiff;		/* difference from wanted value */
1139 	int		word;		/* word number in the buffer */
1140 
1141 	/*
1142 	 * Compute and read in starting bitmap block for starting block.
1143 	 */
1144 	block = XFS_BITTOBLOCK(mp, start);
1145 	error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1146 	if (error) {
1147 		return error;
1148 	}
1149 	bufp = bp->b_addr;
1150 	/*
1151 	 * Get the first word's index & point to it.
1152 	 */
1153 	word = XFS_BITTOWORD(mp, start);
1154 	b = &bufp[word];
1155 	bit = (int)(start & (XFS_NBWORD - 1));
1156 	len = start - limit + 1;
1157 	/*
1158 	 * Compute match value, based on the bit at start: if 1 (free)
1159 	 * then all-ones, else all-zeroes.
1160 	 */
1161 	want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1162 	/*
1163 	 * If the starting position is not word-aligned, deal with the
1164 	 * partial word.
1165 	 */
1166 	if (bit < XFS_NBWORD - 1) {
1167 		/*
1168 		 * Calculate first (leftmost) bit number to look at,
1169 		 * and mask for all the relevant bits in this word.
1170 		 */
1171 		firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
1172 		mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
1173 			firstbit;
1174 		/*
1175 		 * Calculate the difference between the value there
1176 		 * and what we're looking for.
1177 		 */
1178 		if ((wdiff = (*b ^ want) & mask)) {
1179 			/*
1180 			 * Different.  Mark where we are and return.
1181 			 */
1182 			xfs_trans_brelse(tp, bp);
1183 			i = bit - XFS_RTHIBIT(wdiff);
1184 			*rtblock = start - i + 1;
1185 			return 0;
1186 		}
1187 		i = bit - firstbit + 1;
1188 		/*
1189 		 * Go on to previous block if that's where the previous word is
1190 		 * and we need the previous word.
1191 		 */
1192 		if (--word == -1 && i < len) {
1193 			/*
1194 			 * If done with this block, get the previous one.
1195 			 */
1196 			xfs_trans_brelse(tp, bp);
1197 			error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1198 			if (error) {
1199 				return error;
1200 			}
1201 			bufp = bp->b_addr;
1202 			word = XFS_BLOCKWMASK(mp);
1203 			b = &bufp[word];
1204 		} else {
1205 			/*
1206 			 * Go on to the previous word in the buffer.
1207 			 */
1208 			b--;
1209 		}
1210 	} else {
1211 		/*
1212 		 * Starting on a word boundary, no partial word.
1213 		 */
1214 		i = 0;
1215 	}
1216 	/*
1217 	 * Loop over whole words in buffers.  When we use up one buffer
1218 	 * we move on to the previous one.
1219 	 */
1220 	while (len - i >= XFS_NBWORD) {
1221 		/*
1222 		 * Compute difference between actual and desired value.
1223 		 */
1224 		if ((wdiff = *b ^ want)) {
1225 			/*
1226 			 * Different, mark where we are and return.
1227 			 */
1228 			xfs_trans_brelse(tp, bp);
1229 			i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1230 			*rtblock = start - i + 1;
1231 			return 0;
1232 		}
1233 		i += XFS_NBWORD;
1234 		/*
1235 		 * Go on to previous block if that's where the previous word is
1236 		 * and we need the previous word.
1237 		 */
1238 		if (--word == -1 && i < len) {
1239 			/*
1240 			 * If done with this block, get the previous one.
1241 			 */
1242 			xfs_trans_brelse(tp, bp);
1243 			error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
1244 			if (error) {
1245 				return error;
1246 			}
1247 			bufp = bp->b_addr;
1248 			word = XFS_BLOCKWMASK(mp);
1249 			b = &bufp[word];
1250 		} else {
1251 			/*
1252 			 * Go on to the previous word in the buffer.
1253 			 */
1254 			b--;
1255 		}
1256 	}
1257 	/*
1258 	 * If not ending on a word boundary, deal with the last
1259 	 * (partial) word.
1260 	 */
1261 	if (len - i) {
1262 		/*
1263 		 * Calculate first (leftmost) bit number to look at,
1264 		 * and mask for all the relevant bits in this word.
1265 		 */
1266 		firstbit = XFS_NBWORD - (len - i);
1267 		mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
1268 		/*
1269 		 * Compute difference between actual and desired value.
1270 		 */
1271 		if ((wdiff = (*b ^ want) & mask)) {
1272 			/*
1273 			 * Different, mark where we are and return.
1274 			 */
1275 			xfs_trans_brelse(tp, bp);
1276 			i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
1277 			*rtblock = start - i + 1;
1278 			return 0;
1279 		} else
1280 			i = len;
1281 	}
1282 	/*
1283 	 * No match, return that we scanned the whole area.
1284 	 */
1285 	xfs_trans_brelse(tp, bp);
1286 	*rtblock = start - i + 1;
1287 	return 0;
1288 }
1289 
1290 /*
1291  * Searching forward from start to limit, find the first block whose
1292  * allocated/free state is different from start's.
1293  */
1294 STATIC int				/* error */
1295 xfs_rtfind_forw(
1296 	xfs_mount_t	*mp,		/* file system mount point */
1297 	xfs_trans_t	*tp,		/* transaction pointer */
1298 	xfs_rtblock_t	start,		/* starting block to look at */
1299 	xfs_rtblock_t	limit,		/* last block to look at */
1300 	xfs_rtblock_t	*rtblock)	/* out: start block found */
1301 {
1302 	xfs_rtword_t	*b;		/* current word in buffer */
1303 	int		bit;		/* bit number in the word */
1304 	xfs_rtblock_t	block;		/* bitmap block number */
1305 	xfs_buf_t	*bp;		/* buf for the block */
1306 	xfs_rtword_t	*bufp;		/* starting word in buffer */
1307 	int		error;		/* error value */
1308 	xfs_rtblock_t	i;		/* current bit number rel. to start */
1309 	xfs_rtblock_t	lastbit;	/* last useful bit in the word */
1310 	xfs_rtblock_t	len;		/* length of inspected area */
1311 	xfs_rtword_t	mask;		/* mask of relevant bits for value */
1312 	xfs_rtword_t	want;		/* mask for "good" values */
1313 	xfs_rtword_t	wdiff;		/* difference from wanted value */
1314 	int		word;		/* word number in the buffer */
1315 
1316 	/*
1317 	 * Compute and read in starting bitmap block for starting block.
1318 	 */
1319 	block = XFS_BITTOBLOCK(mp, start);
1320 	error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1321 	if (error) {
1322 		return error;
1323 	}
1324 	bufp = bp->b_addr;
1325 	/*
1326 	 * Get the first word's index & point to it.
1327 	 */
1328 	word = XFS_BITTOWORD(mp, start);
1329 	b = &bufp[word];
1330 	bit = (int)(start & (XFS_NBWORD - 1));
1331 	len = limit - start + 1;
1332 	/*
1333 	 * Compute match value, based on the bit at start: if 1 (free)
1334 	 * then all-ones, else all-zeroes.
1335 	 */
1336 	want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
1337 	/*
1338 	 * If the starting position is not word-aligned, deal with the
1339 	 * partial word.
1340 	 */
1341 	if (bit) {
1342 		/*
1343 		 * Calculate last (rightmost) bit number to look at,
1344 		 * and mask for all the relevant bits in this word.
1345 		 */
1346 		lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1347 		mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1348 		/*
1349 		 * Calculate the difference between the value there
1350 		 * and what we're looking for.
1351 		 */
1352 		if ((wdiff = (*b ^ want) & mask)) {
1353 			/*
1354 			 * Different.  Mark where we are and return.
1355 			 */
1356 			xfs_trans_brelse(tp, bp);
1357 			i = XFS_RTLOBIT(wdiff) - bit;
1358 			*rtblock = start + i - 1;
1359 			return 0;
1360 		}
1361 		i = lastbit - bit;
1362 		/*
1363 		 * Go on to next block if that's where the next word is
1364 		 * and we need the next word.
1365 		 */
1366 		if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1367 			/*
1368 			 * If done with this block, get the previous one.
1369 			 */
1370 			xfs_trans_brelse(tp, bp);
1371 			error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1372 			if (error) {
1373 				return error;
1374 			}
1375 			b = bufp = bp->b_addr;
1376 			word = 0;
1377 		} else {
1378 			/*
1379 			 * Go on to the previous word in the buffer.
1380 			 */
1381 			b++;
1382 		}
1383 	} else {
1384 		/*
1385 		 * Starting on a word boundary, no partial word.
1386 		 */
1387 		i = 0;
1388 	}
1389 	/*
1390 	 * Loop over whole words in buffers.  When we use up one buffer
1391 	 * we move on to the next one.
1392 	 */
1393 	while (len - i >= XFS_NBWORD) {
1394 		/*
1395 		 * Compute difference between actual and desired value.
1396 		 */
1397 		if ((wdiff = *b ^ want)) {
1398 			/*
1399 			 * Different, mark where we are and return.
1400 			 */
1401 			xfs_trans_brelse(tp, bp);
1402 			i += XFS_RTLOBIT(wdiff);
1403 			*rtblock = start + i - 1;
1404 			return 0;
1405 		}
1406 		i += XFS_NBWORD;
1407 		/*
1408 		 * Go on to next block if that's where the next word is
1409 		 * and we need the next word.
1410 		 */
1411 		if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1412 			/*
1413 			 * If done with this block, get the next one.
1414 			 */
1415 			xfs_trans_brelse(tp, bp);
1416 			error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1417 			if (error) {
1418 				return error;
1419 			}
1420 			b = bufp = bp->b_addr;
1421 			word = 0;
1422 		} else {
1423 			/*
1424 			 * Go on to the next word in the buffer.
1425 			 */
1426 			b++;
1427 		}
1428 	}
1429 	/*
1430 	 * If not ending on a word boundary, deal with the last
1431 	 * (partial) word.
1432 	 */
1433 	if ((lastbit = len - i)) {
1434 		/*
1435 		 * Calculate mask for all the relevant bits in this word.
1436 		 */
1437 		mask = ((xfs_rtword_t)1 << lastbit) - 1;
1438 		/*
1439 		 * Compute difference between actual and desired value.
1440 		 */
1441 		if ((wdiff = (*b ^ want) & mask)) {
1442 			/*
1443 			 * Different, mark where we are and return.
1444 			 */
1445 			xfs_trans_brelse(tp, bp);
1446 			i += XFS_RTLOBIT(wdiff);
1447 			*rtblock = start + i - 1;
1448 			return 0;
1449 		} else
1450 			i = len;
1451 	}
1452 	/*
1453 	 * No match, return that we scanned the whole area.
1454 	 */
1455 	xfs_trans_brelse(tp, bp);
1456 	*rtblock = start + i - 1;
1457 	return 0;
1458 }
1459 
1460 /*
1461  * Mark an extent specified by start and len freed.
1462  * Updates all the summary information as well as the bitmap.
1463  */
1464 STATIC int				/* error */
1465 xfs_rtfree_range(
1466 	xfs_mount_t	*mp,		/* file system mount point */
1467 	xfs_trans_t	*tp,		/* transaction pointer */
1468 	xfs_rtblock_t	start,		/* starting block to free */
1469 	xfs_extlen_t	len,		/* length to free */
1470 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
1471 	xfs_fsblock_t	*rsb)		/* in/out: summary block number */
1472 {
1473 	xfs_rtblock_t	end;		/* end of the freed extent */
1474 	int		error;		/* error value */
1475 	xfs_rtblock_t	postblock;	/* first block freed > end */
1476 	xfs_rtblock_t	preblock;	/* first block freed < start */
1477 
1478 	end = start + len - 1;
1479 	/*
1480 	 * Modify the bitmap to mark this extent freed.
1481 	 */
1482 	error = xfs_rtmodify_range(mp, tp, start, len, 1);
1483 	if (error) {
1484 		return error;
1485 	}
1486 	/*
1487 	 * Assume we're freeing out of the middle of an allocated extent.
1488 	 * We need to find the beginning and end of the extent so we can
1489 	 * properly update the summary.
1490 	 */
1491 	error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
1492 	if (error) {
1493 		return error;
1494 	}
1495 	/*
1496 	 * Find the next allocated block (end of allocated extent).
1497 	 */
1498 	error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
1499 		&postblock);
1500 	if (error)
1501 		return error;
1502 	/*
1503 	 * If there are blocks not being freed at the front of the
1504 	 * old extent, add summary data for them to be allocated.
1505 	 */
1506 	if (preblock < start) {
1507 		error = xfs_rtmodify_summary(mp, tp,
1508 			XFS_RTBLOCKLOG(start - preblock),
1509 			XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
1510 		if (error) {
1511 			return error;
1512 		}
1513 	}
1514 	/*
1515 	 * If there are blocks not being freed at the end of the
1516 	 * old extent, add summary data for them to be allocated.
1517 	 */
1518 	if (postblock > end) {
1519 		error = xfs_rtmodify_summary(mp, tp,
1520 			XFS_RTBLOCKLOG(postblock - end),
1521 			XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
1522 		if (error) {
1523 			return error;
1524 		}
1525 	}
1526 	/*
1527 	 * Increment the summary information corresponding to the entire
1528 	 * (new) free extent.
1529 	 */
1530 	error = xfs_rtmodify_summary(mp, tp,
1531 		XFS_RTBLOCKLOG(postblock + 1 - preblock),
1532 		XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
1533 	return error;
1534 }
1535 
1536 /*
1537  * Read and return the summary information for a given extent size,
1538  * bitmap block combination.
1539  * Keeps track of a current summary block, so we don't keep reading
1540  * it from the buffer cache.
1541  */
1542 STATIC int				/* error */
1543 xfs_rtget_summary(
1544 	xfs_mount_t	*mp,		/* file system mount structure */
1545 	xfs_trans_t	*tp,		/* transaction pointer */
1546 	int		log,		/* log2 of extent size */
1547 	xfs_rtblock_t	bbno,		/* bitmap block number */
1548 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
1549 	xfs_fsblock_t	*rsb,		/* in/out: summary block number */
1550 	xfs_suminfo_t	*sum)		/* out: summary info for this block */
1551 {
1552 	xfs_buf_t	*bp;		/* buffer for summary block */
1553 	int		error;		/* error value */
1554 	xfs_fsblock_t	sb;		/* summary fsblock */
1555 	int		so;		/* index into the summary file */
1556 	xfs_suminfo_t	*sp;		/* pointer to returned data */
1557 
1558 	/*
1559 	 * Compute entry number in the summary file.
1560 	 */
1561 	so = XFS_SUMOFFS(mp, log, bbno);
1562 	/*
1563 	 * Compute the block number in the summary file.
1564 	 */
1565 	sb = XFS_SUMOFFSTOBLOCK(mp, so);
1566 	/*
1567 	 * If we have an old buffer, and the block number matches, use that.
1568 	 */
1569 	if (rbpp && *rbpp && *rsb == sb)
1570 		bp = *rbpp;
1571 	/*
1572 	 * Otherwise we have to get the buffer.
1573 	 */
1574 	else {
1575 		/*
1576 		 * If there was an old one, get rid of it first.
1577 		 */
1578 		if (rbpp && *rbpp)
1579 			xfs_trans_brelse(tp, *rbpp);
1580 		error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1581 		if (error) {
1582 			return error;
1583 		}
1584 		/*
1585 		 * Remember this buffer and block for the next call.
1586 		 */
1587 		if (rbpp) {
1588 			*rbpp = bp;
1589 			*rsb = sb;
1590 		}
1591 	}
1592 	/*
1593 	 * Point to the summary information & copy it out.
1594 	 */
1595 	sp = XFS_SUMPTR(mp, bp, so);
1596 	*sum = *sp;
1597 	/*
1598 	 * Drop the buffer if we're not asked to remember it.
1599 	 */
1600 	if (!rbpp)
1601 		xfs_trans_brelse(tp, bp);
1602 	return 0;
1603 }
1604 
1605 /*
1606  * Set the given range of bitmap bits to the given value.
1607  * Do whatever I/O and logging is required.
1608  */
1609 STATIC int				/* error */
1610 xfs_rtmodify_range(
1611 	xfs_mount_t	*mp,		/* file system mount point */
1612 	xfs_trans_t	*tp,		/* transaction pointer */
1613 	xfs_rtblock_t	start,		/* starting block to modify */
1614 	xfs_extlen_t	len,		/* length of extent to modify */
1615 	int		val)		/* 1 for free, 0 for allocated */
1616 {
1617 	xfs_rtword_t	*b;		/* current word in buffer */
1618 	int		bit;		/* bit number in the word */
1619 	xfs_rtblock_t	block;		/* bitmap block number */
1620 	xfs_buf_t	*bp;		/* buf for the block */
1621 	xfs_rtword_t	*bufp;		/* starting word in buffer */
1622 	int		error;		/* error value */
1623 	xfs_rtword_t	*first;		/* first used word in the buffer */
1624 	int		i;		/* current bit number rel. to start */
1625 	int		lastbit;	/* last useful bit in word */
1626 	xfs_rtword_t	mask;		/* mask o frelevant bits for value */
1627 	int		word;		/* word number in the buffer */
1628 
1629 	/*
1630 	 * Compute starting bitmap block number.
1631 	 */
1632 	block = XFS_BITTOBLOCK(mp, start);
1633 	/*
1634 	 * Read the bitmap block, and point to its data.
1635 	 */
1636 	error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
1637 	if (error) {
1638 		return error;
1639 	}
1640 	bufp = bp->b_addr;
1641 	/*
1642 	 * Compute the starting word's address, and starting bit.
1643 	 */
1644 	word = XFS_BITTOWORD(mp, start);
1645 	first = b = &bufp[word];
1646 	bit = (int)(start & (XFS_NBWORD - 1));
1647 	/*
1648 	 * 0 (allocated) => all zeroes; 1 (free) => all ones.
1649 	 */
1650 	val = -val;
1651 	/*
1652 	 * If not starting on a word boundary, deal with the first
1653 	 * (partial) word.
1654 	 */
1655 	if (bit) {
1656 		/*
1657 		 * Compute first bit not changed and mask of relevant bits.
1658 		 */
1659 		lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
1660 		mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
1661 		/*
1662 		 * Set/clear the active bits.
1663 		 */
1664 		if (val)
1665 			*b |= mask;
1666 		else
1667 			*b &= ~mask;
1668 		i = lastbit - bit;
1669 		/*
1670 		 * Go on to the next block if that's where the next word is
1671 		 * and we need the next word.
1672 		 */
1673 		if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1674 			/*
1675 			 * Log the changed part of this block.
1676 			 * Get the next one.
1677 			 */
1678 			xfs_trans_log_buf(tp, bp,
1679 				(uint)((char *)first - (char *)bufp),
1680 				(uint)((char *)b - (char *)bufp));
1681 			error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1682 			if (error) {
1683 				return error;
1684 			}
1685 			first = b = bufp = bp->b_addr;
1686 			word = 0;
1687 		} else {
1688 			/*
1689 			 * Go on to the next word in the buffer
1690 			 */
1691 			b++;
1692 		}
1693 	} else {
1694 		/*
1695 		 * Starting on a word boundary, no partial word.
1696 		 */
1697 		i = 0;
1698 	}
1699 	/*
1700 	 * Loop over whole words in buffers.  When we use up one buffer
1701 	 * we move on to the next one.
1702 	 */
1703 	while (len - i >= XFS_NBWORD) {
1704 		/*
1705 		 * Set the word value correctly.
1706 		 */
1707 		*b = val;
1708 		i += XFS_NBWORD;
1709 		/*
1710 		 * Go on to the next block if that's where the next word is
1711 		 * and we need the next word.
1712 		 */
1713 		if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
1714 			/*
1715 			 * Log the changed part of this block.
1716 			 * Get the next one.
1717 			 */
1718 			xfs_trans_log_buf(tp, bp,
1719 				(uint)((char *)first - (char *)bufp),
1720 				(uint)((char *)b - (char *)bufp));
1721 			error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
1722 			if (error) {
1723 				return error;
1724 			}
1725 			first = b = bufp = bp->b_addr;
1726 			word = 0;
1727 		} else {
1728 			/*
1729 			 * Go on to the next word in the buffer
1730 			 */
1731 			b++;
1732 		}
1733 	}
1734 	/*
1735 	 * If not ending on a word boundary, deal with the last
1736 	 * (partial) word.
1737 	 */
1738 	if ((lastbit = len - i)) {
1739 		/*
1740 		 * Compute a mask of relevant bits.
1741 		 */
1742 		bit = 0;
1743 		mask = ((xfs_rtword_t)1 << lastbit) - 1;
1744 		/*
1745 		 * Set/clear the active bits.
1746 		 */
1747 		if (val)
1748 			*b |= mask;
1749 		else
1750 			*b &= ~mask;
1751 		b++;
1752 	}
1753 	/*
1754 	 * Log any remaining changed bytes.
1755 	 */
1756 	if (b > first)
1757 		xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
1758 			(uint)((char *)b - (char *)bufp - 1));
1759 	return 0;
1760 }
1761 
1762 /*
1763  * Read and modify the summary information for a given extent size,
1764  * bitmap block combination.
1765  * Keeps track of a current summary block, so we don't keep reading
1766  * it from the buffer cache.
1767  */
1768 STATIC int				/* error */
1769 xfs_rtmodify_summary(
1770 	xfs_mount_t	*mp,		/* file system mount point */
1771 	xfs_trans_t	*tp,		/* transaction pointer */
1772 	int		log,		/* log2 of extent size */
1773 	xfs_rtblock_t	bbno,		/* bitmap block number */
1774 	int		delta,		/* change to make to summary info */
1775 	xfs_buf_t	**rbpp,		/* in/out: summary block buffer */
1776 	xfs_fsblock_t	*rsb)		/* in/out: summary block number */
1777 {
1778 	xfs_buf_t	*bp;		/* buffer for the summary block */
1779 	int		error;		/* error value */
1780 	xfs_fsblock_t	sb;		/* summary fsblock */
1781 	int		so;		/* index into the summary file */
1782 	xfs_suminfo_t	*sp;		/* pointer to returned data */
1783 
1784 	/*
1785 	 * Compute entry number in the summary file.
1786 	 */
1787 	so = XFS_SUMOFFS(mp, log, bbno);
1788 	/*
1789 	 * Compute the block number in the summary file.
1790 	 */
1791 	sb = XFS_SUMOFFSTOBLOCK(mp, so);
1792 	/*
1793 	 * If we have an old buffer, and the block number matches, use that.
1794 	 */
1795 	if (rbpp && *rbpp && *rsb == sb)
1796 		bp = *rbpp;
1797 	/*
1798 	 * Otherwise we have to get the buffer.
1799 	 */
1800 	else {
1801 		/*
1802 		 * If there was an old one, get rid of it first.
1803 		 */
1804 		if (rbpp && *rbpp)
1805 			xfs_trans_brelse(tp, *rbpp);
1806 		error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
1807 		if (error) {
1808 			return error;
1809 		}
1810 		/*
1811 		 * Remember this buffer and block for the next call.
1812 		 */
1813 		if (rbpp) {
1814 			*rbpp = bp;
1815 			*rsb = sb;
1816 		}
1817 	}
1818 	/*
1819 	 * Point to the summary information, modify and log it.
1820 	 */
1821 	sp = XFS_SUMPTR(mp, bp, so);
1822 	*sp += delta;
1823 	xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)bp->b_addr),
1824 		(uint)((char *)sp - (char *)bp->b_addr + sizeof(*sp) - 1));
1825 	return 0;
1826 }
1827 
1828 /*
1829  * Visible (exported) functions.
1830  */
1831 
1832 /*
1833  * Grow the realtime area of the filesystem.
1834  */
1835 int
1836 xfs_growfs_rt(
1837 	xfs_mount_t	*mp,		/* mount point for filesystem */
1838 	xfs_growfs_rt_t	*in)		/* growfs rt input struct */
1839 {
1840 	xfs_rtblock_t	bmbno;		/* bitmap block number */
1841 	xfs_buf_t	*bp;		/* temporary buffer */
1842 	int		error;		/* error return value */
1843 	xfs_mount_t	*nmp;		/* new (fake) mount structure */
1844 	xfs_drfsbno_t	nrblocks;	/* new number of realtime blocks */
1845 	xfs_extlen_t	nrbmblocks;	/* new number of rt bitmap blocks */
1846 	xfs_drtbno_t	nrextents;	/* new number of realtime extents */
1847 	uint8_t		nrextslog;	/* new log2 of sb_rextents */
1848 	xfs_extlen_t	nrsumblocks;	/* new number of summary blocks */
1849 	uint		nrsumlevels;	/* new rt summary levels */
1850 	uint		nrsumsize;	/* new size of rt summary, bytes */
1851 	xfs_sb_t	*nsbp;		/* new superblock */
1852 	xfs_extlen_t	rbmblocks;	/* current number of rt bitmap blocks */
1853 	xfs_extlen_t	rsumblocks;	/* current number of rt summary blks */
1854 	xfs_sb_t	*sbp;		/* old superblock */
1855 	xfs_fsblock_t	sumbno;		/* summary block number */
1856 
1857 	sbp = &mp->m_sb;
1858 	/*
1859 	 * Initial error checking.
1860 	 */
1861 	if (!capable(CAP_SYS_ADMIN))
1862 		return XFS_ERROR(EPERM);
1863 	if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
1864 	    (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
1865 	    (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
1866 		return XFS_ERROR(EINVAL);
1867 	if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
1868 		return error;
1869 	/*
1870 	 * Read in the last block of the device, make sure it exists.
1871 	 */
1872 	bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
1873 				XFS_FSB_TO_BB(mp, nrblocks - 1),
1874 				XFS_FSB_TO_BB(mp, 1), 0, NULL);
1875 	if (!bp)
1876 		return EIO;
1877 	if (bp->b_error) {
1878 		error = bp->b_error;
1879 		xfs_buf_relse(bp);
1880 		return error;
1881 	}
1882 	xfs_buf_relse(bp);
1883 
1884 	/*
1885 	 * Calculate new parameters.  These are the final values to be reached.
1886 	 */
1887 	nrextents = nrblocks;
1888 	do_div(nrextents, in->extsize);
1889 	nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
1890 	nrextslog = xfs_highbit32(nrextents);
1891 	nrsumlevels = nrextslog + 1;
1892 	nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
1893 	nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1894 	nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1895 	/*
1896 	 * New summary size can't be more than half the size of
1897 	 * the log.  This prevents us from getting a log overflow,
1898 	 * since we'll log basically the whole summary file at once.
1899 	 */
1900 	if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
1901 		return XFS_ERROR(EINVAL);
1902 	/*
1903 	 * Get the old block counts for bitmap and summary inodes.
1904 	 * These can't change since other growfs callers are locked out.
1905 	 */
1906 	rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
1907 	rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
1908 	/*
1909 	 * Allocate space to the bitmap and summary files, as necessary.
1910 	 */
1911 	error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
1912 	if (error)
1913 		return error;
1914 	error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
1915 	if (error)
1916 		return error;
1917 	/*
1918 	 * Allocate a new (fake) mount/sb.
1919 	 */
1920 	nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
1921 	/*
1922 	 * Loop over the bitmap blocks.
1923 	 * We will do everything one bitmap block at a time.
1924 	 * Skip the current block if it is exactly full.
1925 	 * This also deals with the case where there were no rtextents before.
1926 	 */
1927 	for (bmbno = sbp->sb_rbmblocks -
1928 		     ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
1929 	     bmbno < nrbmblocks;
1930 	     bmbno++) {
1931 		xfs_trans_t	*tp;
1932 		int		cancelflags = 0;
1933 
1934 		*nmp = *mp;
1935 		nsbp = &nmp->m_sb;
1936 		/*
1937 		 * Calculate new sb and mount fields for this round.
1938 		 */
1939 		nsbp->sb_rextsize = in->extsize;
1940 		nsbp->sb_rbmblocks = bmbno + 1;
1941 		nsbp->sb_rblocks =
1942 			XFS_RTMIN(nrblocks,
1943 				  nsbp->sb_rbmblocks * NBBY *
1944 				  nsbp->sb_blocksize * nsbp->sb_rextsize);
1945 		nsbp->sb_rextents = nsbp->sb_rblocks;
1946 		do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
1947 		ASSERT(nsbp->sb_rextents != 0);
1948 		nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
1949 		nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
1950 		nrsumsize =
1951 			(uint)sizeof(xfs_suminfo_t) * nrsumlevels *
1952 			nsbp->sb_rbmblocks;
1953 		nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
1954 		nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
1955 		/*
1956 		 * Start a transaction, get the log reservation.
1957 		 */
1958 		tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
1959 		error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtfree,
1960 					  0, 0);
1961 		if (error)
1962 			goto error_cancel;
1963 		/*
1964 		 * Lock out other callers by grabbing the bitmap inode lock.
1965 		 */
1966 		xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
1967 		xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
1968 		/*
1969 		 * Update the bitmap inode's size.
1970 		 */
1971 		mp->m_rbmip->i_d.di_size =
1972 			nsbp->sb_rbmblocks * nsbp->sb_blocksize;
1973 		xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
1974 		cancelflags |= XFS_TRANS_ABORT;
1975 		/*
1976 		 * Get the summary inode into the transaction.
1977 		 */
1978 		xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
1979 		xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
1980 		/*
1981 		 * Update the summary inode's size.
1982 		 */
1983 		mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
1984 		xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
1985 		/*
1986 		 * Copy summary data from old to new sizes.
1987 		 * Do this when the real size (not block-aligned) changes.
1988 		 */
1989 		if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
1990 		    mp->m_rsumlevels != nmp->m_rsumlevels) {
1991 			error = xfs_rtcopy_summary(mp, nmp, tp);
1992 			if (error)
1993 				goto error_cancel;
1994 		}
1995 		/*
1996 		 * Update superblock fields.
1997 		 */
1998 		if (nsbp->sb_rextsize != sbp->sb_rextsize)
1999 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
2000 				nsbp->sb_rextsize - sbp->sb_rextsize);
2001 		if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
2002 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
2003 				nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
2004 		if (nsbp->sb_rblocks != sbp->sb_rblocks)
2005 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
2006 				nsbp->sb_rblocks - sbp->sb_rblocks);
2007 		if (nsbp->sb_rextents != sbp->sb_rextents)
2008 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
2009 				nsbp->sb_rextents - sbp->sb_rextents);
2010 		if (nsbp->sb_rextslog != sbp->sb_rextslog)
2011 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
2012 				nsbp->sb_rextslog - sbp->sb_rextslog);
2013 		/*
2014 		 * Free new extent.
2015 		 */
2016 		bp = NULL;
2017 		error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
2018 			nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
2019 		if (error) {
2020 error_cancel:
2021 			xfs_trans_cancel(tp, cancelflags);
2022 			break;
2023 		}
2024 		/*
2025 		 * Mark more blocks free in the superblock.
2026 		 */
2027 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
2028 			nsbp->sb_rextents - sbp->sb_rextents);
2029 		/*
2030 		 * Update mp values into the real mp structure.
2031 		 */
2032 		mp->m_rsumlevels = nrsumlevels;
2033 		mp->m_rsumsize = nrsumsize;
2034 
2035 		error = xfs_trans_commit(tp, 0);
2036 		if (error)
2037 			break;
2038 	}
2039 
2040 	/*
2041 	 * Free the fake mp structure.
2042 	 */
2043 	kmem_free(nmp);
2044 
2045 	return error;
2046 }
2047 
2048 /*
2049  * Allocate an extent in the realtime subvolume, with the usual allocation
2050  * parameters.  The length units are all in realtime extents, as is the
2051  * result block number.
2052  */
2053 int					/* error */
2054 xfs_rtallocate_extent(
2055 	xfs_trans_t	*tp,		/* transaction pointer */
2056 	xfs_rtblock_t	bno,		/* starting block number to allocate */
2057 	xfs_extlen_t	minlen,		/* minimum length to allocate */
2058 	xfs_extlen_t	maxlen,		/* maximum length to allocate */
2059 	xfs_extlen_t	*len,		/* out: actual length allocated */
2060 	xfs_alloctype_t	type,		/* allocation type XFS_ALLOCTYPE... */
2061 	int		wasdel,		/* was a delayed allocation extent */
2062 	xfs_extlen_t	prod,		/* extent product factor */
2063 	xfs_rtblock_t	*rtblock)	/* out: start block allocated */
2064 {
2065 	xfs_mount_t	*mp = tp->t_mountp;
2066 	int		error;		/* error value */
2067 	xfs_rtblock_t	r;		/* result allocated block */
2068 	xfs_fsblock_t	sb;		/* summary file block number */
2069 	xfs_buf_t	*sumbp;		/* summary file block buffer */
2070 
2071 	ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
2072 	ASSERT(minlen > 0 && minlen <= maxlen);
2073 
2074 	/*
2075 	 * If prod is set then figure out what to do to minlen and maxlen.
2076 	 */
2077 	if (prod > 1) {
2078 		xfs_extlen_t	i;
2079 
2080 		if ((i = maxlen % prod))
2081 			maxlen -= i;
2082 		if ((i = minlen % prod))
2083 			minlen += prod - i;
2084 		if (maxlen < minlen) {
2085 			*rtblock = NULLRTBLOCK;
2086 			return 0;
2087 		}
2088 	}
2089 
2090 	sumbp = NULL;
2091 	/*
2092 	 * Allocate by size, or near another block, or exactly at some block.
2093 	 */
2094 	switch (type) {
2095 	case XFS_ALLOCTYPE_ANY_AG:
2096 		error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
2097 				&sumbp,	&sb, prod, &r);
2098 		break;
2099 	case XFS_ALLOCTYPE_NEAR_BNO:
2100 		error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
2101 				len, &sumbp, &sb, prod, &r);
2102 		break;
2103 	case XFS_ALLOCTYPE_THIS_BNO:
2104 		error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
2105 				len, &sumbp, &sb, prod, &r);
2106 		break;
2107 	default:
2108 		error = EIO;
2109 		ASSERT(0);
2110 	}
2111 	if (error)
2112 		return error;
2113 
2114 	/*
2115 	 * If it worked, update the superblock.
2116 	 */
2117 	if (r != NULLRTBLOCK) {
2118 		long	slen = (long)*len;
2119 
2120 		ASSERT(*len >= minlen && *len <= maxlen);
2121 		if (wasdel)
2122 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
2123 		else
2124 			xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
2125 	}
2126 	*rtblock = r;
2127 	return 0;
2128 }
2129 
2130 /*
2131  * Free an extent in the realtime subvolume.  Length is expressed in
2132  * realtime extents, as is the block number.
2133  */
2134 int					/* error */
2135 xfs_rtfree_extent(
2136 	xfs_trans_t	*tp,		/* transaction pointer */
2137 	xfs_rtblock_t	bno,		/* starting block number to free */
2138 	xfs_extlen_t	len)		/* length of extent freed */
2139 {
2140 	int		error;		/* error value */
2141 	xfs_mount_t	*mp;		/* file system mount structure */
2142 	xfs_fsblock_t	sb;		/* summary file block number */
2143 	xfs_buf_t	*sumbp;		/* summary file block buffer */
2144 
2145 	mp = tp->t_mountp;
2146 
2147 	ASSERT(mp->m_rbmip->i_itemp != NULL);
2148 	ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
2149 
2150 #ifdef DEBUG
2151 	/*
2152 	 * Check to see that this whole range is currently allocated.
2153 	 */
2154 	{
2155 		int	stat;		/* result from checking range */
2156 
2157 		error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
2158 		if (error) {
2159 			return error;
2160 		}
2161 		ASSERT(stat);
2162 	}
2163 #endif
2164 	sumbp = NULL;
2165 	/*
2166 	 * Free the range of realtime blocks.
2167 	 */
2168 	error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
2169 	if (error) {
2170 		return error;
2171 	}
2172 	/*
2173 	 * Mark more blocks free in the superblock.
2174 	 */
2175 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
2176 	/*
2177 	 * If we've now freed all the blocks, reset the file sequence
2178 	 * number to 0.
2179 	 */
2180 	if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
2181 	    mp->m_sb.sb_rextents) {
2182 		if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
2183 			mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2184 		*(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
2185 		xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2186 	}
2187 	return 0;
2188 }
2189 
2190 /*
2191  * Initialize realtime fields in the mount structure.
2192  */
2193 int				/* error */
2194 xfs_rtmount_init(
2195 	xfs_mount_t	*mp)	/* file system mount structure */
2196 {
2197 	xfs_buf_t	*bp;	/* buffer for last block of subvolume */
2198 	xfs_daddr_t	d;	/* address of last block of subvolume */
2199 	xfs_sb_t	*sbp;	/* filesystem superblock copy in mount */
2200 
2201 	sbp = &mp->m_sb;
2202 	if (sbp->sb_rblocks == 0)
2203 		return 0;
2204 	if (mp->m_rtdev_targp == NULL) {
2205 		xfs_warn(mp,
2206 	"Filesystem has a realtime volume, use rtdev=device option");
2207 		return XFS_ERROR(ENODEV);
2208 	}
2209 	mp->m_rsumlevels = sbp->sb_rextslog + 1;
2210 	mp->m_rsumsize =
2211 		(uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
2212 		sbp->sb_rbmblocks;
2213 	mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
2214 	mp->m_rbmip = mp->m_rsumip = NULL;
2215 	/*
2216 	 * Check that the realtime section is an ok size.
2217 	 */
2218 	d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
2219 	if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
2220 		xfs_warn(mp, "realtime mount -- %llu != %llu",
2221 			(unsigned long long) XFS_BB_TO_FSB(mp, d),
2222 			(unsigned long long) mp->m_sb.sb_rblocks);
2223 		return XFS_ERROR(EFBIG);
2224 	}
2225 	bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
2226 					d - XFS_FSB_TO_BB(mp, 1),
2227 					XFS_FSB_TO_BB(mp, 1), 0, NULL);
2228 	if (!bp || bp->b_error) {
2229 		xfs_warn(mp, "realtime device size check failed");
2230 		if (bp)
2231 			xfs_buf_relse(bp);
2232 		return EIO;
2233 	}
2234 	xfs_buf_relse(bp);
2235 	return 0;
2236 }
2237 
2238 /*
2239  * Get the bitmap and summary inodes into the mount structure
2240  * at mount time.
2241  */
2242 int					/* error */
2243 xfs_rtmount_inodes(
2244 	xfs_mount_t	*mp)		/* file system mount structure */
2245 {
2246 	int		error;		/* error return value */
2247 	xfs_sb_t	*sbp;
2248 
2249 	sbp = &mp->m_sb;
2250 	if (sbp->sb_rbmino == NULLFSINO)
2251 		return 0;
2252 	error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
2253 	if (error)
2254 		return error;
2255 	ASSERT(mp->m_rbmip != NULL);
2256 	ASSERT(sbp->sb_rsumino != NULLFSINO);
2257 	error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
2258 	if (error) {
2259 		IRELE(mp->m_rbmip);
2260 		return error;
2261 	}
2262 	ASSERT(mp->m_rsumip != NULL);
2263 	return 0;
2264 }
2265 
2266 void
2267 xfs_rtunmount_inodes(
2268 	struct xfs_mount	*mp)
2269 {
2270 	if (mp->m_rbmip)
2271 		IRELE(mp->m_rbmip);
2272 	if (mp->m_rsumip)
2273 		IRELE(mp->m_rsumip);
2274 }
2275 
2276 /*
2277  * Pick an extent for allocation at the start of a new realtime file.
2278  * Use the sequence number stored in the atime field of the bitmap inode.
2279  * Translate this to a fraction of the rtextents, and return the product
2280  * of rtextents and the fraction.
2281  * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
2282  */
2283 int					/* error */
2284 xfs_rtpick_extent(
2285 	xfs_mount_t	*mp,		/* file system mount point */
2286 	xfs_trans_t	*tp,		/* transaction pointer */
2287 	xfs_extlen_t	len,		/* allocation length (rtextents) */
2288 	xfs_rtblock_t	*pick)		/* result rt extent */
2289 {
2290 	xfs_rtblock_t	b;		/* result block */
2291 	int		log2;		/* log of sequence number */
2292 	__uint64_t	resid;		/* residual after log removed */
2293 	__uint64_t	seq;		/* sequence number of file creation */
2294 	__uint64_t	*seqp;		/* pointer to seqno in inode */
2295 
2296 	ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
2297 
2298 	seqp = (__uint64_t *)&mp->m_rbmip->i_d.di_atime;
2299 	if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
2300 		mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
2301 		*seqp = 0;
2302 	}
2303 	seq = *seqp;
2304 	if ((log2 = xfs_highbit64(seq)) == -1)
2305 		b = 0;
2306 	else {
2307 		resid = seq - (1ULL << log2);
2308 		b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
2309 		    (log2 + 1);
2310 		if (b >= mp->m_sb.sb_rextents)
2311 			b = do_mod(b, mp->m_sb.sb_rextents);
2312 		if (b + len > mp->m_sb.sb_rextents)
2313 			b = mp->m_sb.sb_rextents - len;
2314 	}
2315 	*seqp = seq + 1;
2316 	xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
2317 	*pick = b;
2318 	return 0;
2319 }
2320