xref: /linux/fs/xfs/libxfs/xfs_rtbitmap.c (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs_platform.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_space.h"
18 #include "xfs_trans.h"
19 #include "xfs_error.h"
20 #include "xfs_rtbitmap.h"
21 #include "xfs_health.h"
22 #include "xfs_sb.h"
23 #include "xfs_errortag.h"
24 #include "xfs_log.h"
25 #include "xfs_buf_item.h"
26 #include "xfs_extent_busy.h"
27 
28 /*
29  * Realtime allocator bitmap functions shared with userspace.
30  */
31 
32 static xfs_failaddr_t
33 xfs_rtbuf_verify(
34 	struct xfs_buf			*bp)
35 {
36 	struct xfs_mount		*mp = bp->b_mount;
37 	struct xfs_rtbuf_blkinfo	*hdr = bp->b_addr;
38 
39 	if (!xfs_verify_magic(bp, hdr->rt_magic))
40 		return __this_address;
41 	if (!xfs_has_rtgroups(mp))
42 		return __this_address;
43 	if (!xfs_has_crc(mp))
44 		return __this_address;
45 	if (!uuid_equal(&hdr->rt_uuid, &mp->m_sb.sb_meta_uuid))
46 		return __this_address;
47 	if (hdr->rt_blkno != cpu_to_be64(xfs_buf_daddr(bp)))
48 		return __this_address;
49 	return NULL;
50 }
51 
52 static void
53 xfs_rtbuf_verify_read(
54 	struct xfs_buf			*bp)
55 {
56 	struct xfs_mount		*mp = bp->b_mount;
57 	struct xfs_rtbuf_blkinfo	*hdr = bp->b_addr;
58 	xfs_failaddr_t			fa;
59 
60 	if (!xfs_has_rtgroups(mp))
61 		return;
62 
63 	if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr->rt_lsn))) {
64 		fa = __this_address;
65 		goto fail;
66 	}
67 
68 	if (!xfs_buf_verify_cksum(bp, XFS_RTBUF_CRC_OFF)) {
69 		fa = __this_address;
70 		goto fail;
71 	}
72 
73 	fa = xfs_rtbuf_verify(bp);
74 	if (fa)
75 		goto fail;
76 
77 	return;
78 fail:
79 	xfs_verifier_error(bp, -EFSCORRUPTED, fa);
80 }
81 
82 static void
83 xfs_rtbuf_verify_write(
84 	struct xfs_buf	*bp)
85 {
86 	struct xfs_mount		*mp = bp->b_mount;
87 	struct xfs_rtbuf_blkinfo	*hdr = bp->b_addr;
88 	struct xfs_buf_log_item		*bip = bp->b_log_item;
89 	xfs_failaddr_t			fa;
90 
91 	if (!xfs_has_rtgroups(mp))
92 		return;
93 
94 	fa = xfs_rtbuf_verify(bp);
95 	if (fa) {
96 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
97 		return;
98 	}
99 
100 	if (bip)
101 		hdr->rt_lsn = cpu_to_be64(bip->bli_item.li_lsn);
102 	xfs_buf_update_cksum(bp, XFS_RTBUF_CRC_OFF);
103 }
104 
105 const struct xfs_buf_ops xfs_rtbuf_ops = {
106 	.name = "rtbuf",
107 	.verify_read = xfs_rtbuf_verify_read,
108 	.verify_write = xfs_rtbuf_verify_write,
109 };
110 
111 const struct xfs_buf_ops xfs_rtbitmap_buf_ops = {
112 	.name		= "xfs_rtbitmap",
113 	.magic		= { 0, cpu_to_be32(XFS_RTBITMAP_MAGIC) },
114 	.verify_read	= xfs_rtbuf_verify_read,
115 	.verify_write	= xfs_rtbuf_verify_write,
116 	.verify_struct	= xfs_rtbuf_verify,
117 };
118 
119 const struct xfs_buf_ops xfs_rtsummary_buf_ops = {
120 	.name		= "xfs_rtsummary",
121 	.magic		= { 0, cpu_to_be32(XFS_RTSUMMARY_MAGIC) },
122 	.verify_read	= xfs_rtbuf_verify_read,
123 	.verify_write	= xfs_rtbuf_verify_write,
124 	.verify_struct	= xfs_rtbuf_verify,
125 };
126 
127 static const struct xfs_buf_ops *xfs_rtblock_buf_ops[XFS_RTGI_MAX] = {
128 	[XFS_RTGI_SUMMARY]	= &xfs_rtsummary_buf_ops,
129 	[XFS_RTGI_BITMAP]	= &xfs_rtbitmap_buf_ops,
130 };
131 
132 const struct xfs_buf_ops *
133 xfs_rtblock_ops(
134 	struct xfs_mount	*mp,
135 	enum xfs_rtg_inodes	type)
136 {
137 	if (!xfs_has_rtgroups(mp))
138 		return &xfs_rtbuf_ops;
139 	return xfs_rtblock_buf_ops[type];
140 }
141 
142 static enum xfs_blft xfs_rtblock_buf_types[XFS_RTGI_MAX] = {
143 	[XFS_RTGI_SUMMARY]	= XFS_BLFT_RTSUMMARY_BUF,
144 	[XFS_RTGI_BITMAP]	= XFS_BLFT_RTBITMAP_BUF,
145 };
146 
147 /* Release cached rt bitmap and summary buffers. */
148 void
149 xfs_rtbuf_cache_relse(
150 	struct xfs_rtalloc_args	*args)
151 {
152 	if (args->rbmbp) {
153 		xfs_trans_brelse(args->tp, args->rbmbp);
154 		args->rbmbp = NULL;
155 		args->rbmoff = NULLFILEOFF;
156 	}
157 	if (args->sumbp) {
158 		xfs_trans_brelse(args->tp, args->sumbp);
159 		args->sumbp = NULL;
160 		args->sumoff = NULLFILEOFF;
161 	}
162 }
163 
164 /*
165  * Get a buffer for the bitmap or summary file block specified.
166  * The buffer is returned read and locked.
167  */
168 static int
169 xfs_rtbuf_get(
170 	struct xfs_rtalloc_args	*args,
171 	xfs_fileoff_t		block,	/* block number in bitmap or summary */
172 	enum xfs_rtg_inodes	type)
173 {
174 	struct xfs_inode	*ip = args->rtg->rtg_inodes[type];
175 	struct xfs_mount	*mp = args->mp;
176 	struct xfs_buf		**cbpp;	/* cached block buffer */
177 	xfs_fileoff_t		*coffp;	/* cached block number */
178 	struct xfs_buf		*bp;	/* block buffer, result */
179 	struct xfs_bmbt_irec	map;
180 	int			nmap = 1;
181 	int			error;
182 
183 	switch (type) {
184 	case XFS_RTGI_SUMMARY:
185 		cbpp = &args->sumbp;
186 		coffp = &args->sumoff;
187 		break;
188 	case XFS_RTGI_BITMAP:
189 		cbpp = &args->rbmbp;
190 		coffp = &args->rbmoff;
191 		break;
192 	default:
193 		return -EINVAL;
194 	}
195 
196 	/*
197 	 * If we have a cached buffer, and the block number matches, use that.
198 	 */
199 	if (*cbpp && *coffp == block)
200 		return 0;
201 
202 	/*
203 	 * Otherwise we have to have to get the buffer.  If there was an old
204 	 * one, get rid of it first.
205 	 */
206 	if (*cbpp) {
207 		xfs_trans_brelse(args->tp, *cbpp);
208 		*cbpp = NULL;
209 	}
210 
211 	error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0);
212 	if (error)
213 		return error;
214 
215 	if (XFS_IS_CORRUPT(mp, nmap == 0 || !xfs_bmap_is_written_extent(&map))) {
216 		xfs_rtginode_mark_sick(args->rtg, type);
217 		return -EFSCORRUPTED;
218 	}
219 
220 	ASSERT(map.br_startblock != NULLFSBLOCK);
221 	error = xfs_trans_read_buf(mp, args->tp, mp->m_ddev_targp,
222 				   XFS_FSB_TO_DADDR(mp, map.br_startblock),
223 				   mp->m_bsize, 0, &bp,
224 				   xfs_rtblock_ops(mp, type));
225 	if (xfs_metadata_is_sick(error))
226 		xfs_rtginode_mark_sick(args->rtg, type);
227 	if (error)
228 		return error;
229 
230 	if (xfs_has_rtgroups(mp)) {
231 		struct xfs_rtbuf_blkinfo	*hdr = bp->b_addr;
232 
233 		if (hdr->rt_owner != cpu_to_be64(I_INO(ip))) {
234 			xfs_buf_mark_corrupt(bp);
235 			xfs_trans_brelse(args->tp, bp);
236 			xfs_rtginode_mark_sick(args->rtg, type);
237 			return -EFSCORRUPTED;
238 		}
239 	}
240 
241 	xfs_trans_buf_set_type(args->tp, bp, xfs_rtblock_buf_types[type]);
242 	*cbpp = bp;
243 	*coffp = block;
244 	return 0;
245 }
246 
247 int
248 xfs_rtbitmap_read_buf(
249 	struct xfs_rtalloc_args		*args,
250 	xfs_fileoff_t			block)
251 {
252 	struct xfs_mount		*mp = args->mp;
253 
254 	if (XFS_IS_CORRUPT(mp, block >= mp->m_sb.sb_rbmblocks)) {
255 		xfs_rtginode_mark_sick(args->rtg, XFS_RTGI_BITMAP);
256 		return -EFSCORRUPTED;
257 	}
258 
259 	return xfs_rtbuf_get(args, block, XFS_RTGI_BITMAP);
260 }
261 
262 int
263 xfs_rtsummary_read_buf(
264 	struct xfs_rtalloc_args		*args,
265 	xfs_fileoff_t			block)
266 {
267 	struct xfs_mount		*mp = args->mp;
268 
269 	if (XFS_IS_CORRUPT(mp, block >= mp->m_rsumblocks)) {
270 		xfs_rtginode_mark_sick(args->rtg, XFS_RTGI_SUMMARY);
271 		return -EFSCORRUPTED;
272 	}
273 	return xfs_rtbuf_get(args, block, XFS_RTGI_SUMMARY);
274 }
275 
276 /*
277  * Searching backward from start find the first block whose allocated/free state
278  * is different from start's.
279  */
280 int
281 xfs_rtfind_back(
282 	struct xfs_rtalloc_args	*args,
283 	xfs_rtxnum_t		start,	/* starting rtext to look at */
284 	xfs_rtxnum_t		*rtx)	/* out: start rtext found */
285 {
286 	struct xfs_mount	*mp = args->mp;
287 	int			bit;	/* bit number in the word */
288 	xfs_fileoff_t		block;	/* bitmap block number */
289 	int			error;	/* error value */
290 	xfs_rtxnum_t		firstbit; /* first useful bit in the word */
291 	xfs_rtxnum_t		i;	/* current bit number rel. to start */
292 	xfs_rtxnum_t		len;	/* length of inspected area */
293 	xfs_rtword_t		mask;	/* mask of relevant bits for value */
294 	xfs_rtword_t		want;	/* mask for "good" values */
295 	xfs_rtword_t		wdiff;	/* difference from wanted value */
296 	xfs_rtword_t		incore;
297 	unsigned int		word;	/* word number in the buffer */
298 
299 	/*
300 	 * Compute and read in starting bitmap block for starting block.
301 	 */
302 	block = xfs_rtx_to_rbmblock(mp, start);
303 	error = xfs_rtbitmap_read_buf(args, block);
304 	if (error)
305 		return error;
306 
307 	/*
308 	 * Get the first word's index & point to it.
309 	 */
310 	word = xfs_rtx_to_rbmword(mp, start);
311 	bit = (int)(start & (XFS_NBWORD - 1));
312 	len = start + 1;
313 	/*
314 	 * Compute match value, based on the bit at start: if 1 (free)
315 	 * then all-ones, else all-zeroes.
316 	 */
317 	incore = xfs_rtbitmap_getword(args, word);
318 	want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
319 	/*
320 	 * If the starting position is not word-aligned, deal with the
321 	 * partial word.
322 	 */
323 	if (bit < XFS_NBWORD - 1) {
324 		/*
325 		 * Calculate first (leftmost) bit number to look at,
326 		 * and mask for all the relevant bits in this word.
327 		 */
328 		firstbit = max_t(xfs_srtblock_t, bit - len + 1, 0);
329 		mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
330 			firstbit;
331 		/*
332 		 * Calculate the difference between the value there
333 		 * and what we're looking for.
334 		 */
335 		if ((wdiff = (incore ^ want) & mask)) {
336 			/*
337 			 * Different.  Mark where we are and return.
338 			 */
339 			i = bit - xfs_highbit32(wdiff);
340 			*rtx = start - i + 1;
341 			return 0;
342 		}
343 		i = bit - firstbit + 1;
344 		/*
345 		 * Go on to previous block if that's where the previous word is
346 		 * and we need the previous word.
347 		 */
348 		if (--word == -1 && i < len) {
349 			/*
350 			 * If done with this block, get the previous one.
351 			 */
352 			error = xfs_rtbitmap_read_buf(args, --block);
353 			if (error)
354 				return error;
355 
356 			word = mp->m_blockwsize - 1;
357 		}
358 	} else {
359 		/*
360 		 * Starting on a word boundary, no partial word.
361 		 */
362 		i = 0;
363 	}
364 	/*
365 	 * Loop over whole words in buffers.  When we use up one buffer
366 	 * we move on to the previous one.
367 	 */
368 	while (len - i >= XFS_NBWORD) {
369 		/*
370 		 * Compute difference between actual and desired value.
371 		 */
372 		incore = xfs_rtbitmap_getword(args, word);
373 		if ((wdiff = incore ^ want)) {
374 			/*
375 			 * Different, mark where we are and return.
376 			 */
377 			i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
378 			*rtx = start - i + 1;
379 			return 0;
380 		}
381 		i += XFS_NBWORD;
382 		/*
383 		 * Go on to previous block if that's where the previous word is
384 		 * and we need the previous word.
385 		 */
386 		if (--word == -1 && i < len) {
387 			/*
388 			 * If done with this block, get the previous one.
389 			 */
390 			error = xfs_rtbitmap_read_buf(args, --block);
391 			if (error)
392 				return error;
393 
394 			word = mp->m_blockwsize - 1;
395 		}
396 	}
397 	/*
398 	 * If not ending on a word boundary, deal with the last
399 	 * (partial) word.
400 	 */
401 	if (len - i) {
402 		/*
403 		 * Calculate first (leftmost) bit number to look at,
404 		 * and mask for all the relevant bits in this word.
405 		 */
406 		firstbit = XFS_NBWORD - (len - i);
407 		mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
408 		/*
409 		 * Compute difference between actual and desired value.
410 		 */
411 		incore = xfs_rtbitmap_getword(args, word);
412 		if ((wdiff = (incore ^ want) & mask)) {
413 			/*
414 			 * Different, mark where we are and return.
415 			 */
416 			i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
417 			*rtx = start - i + 1;
418 			return 0;
419 		} else
420 			i = len;
421 	}
422 	/*
423 	 * No match, return that we scanned the whole area.
424 	 */
425 	*rtx = start - i + 1;
426 	return 0;
427 }
428 
429 /*
430  * Searching forward from start to limit, find the first block whose
431  * allocated/free state is different from start's.
432  */
433 int
434 xfs_rtfind_forw(
435 	struct xfs_rtalloc_args	*args,
436 	xfs_rtxnum_t		start,	/* starting rtext to look at */
437 	xfs_rtxnum_t		limit,	/* last rtext to look at */
438 	xfs_rtxnum_t		*rtx)	/* out: start rtext found */
439 {
440 	struct xfs_mount	*mp = args->mp;
441 	int			bit;	/* bit number in the word */
442 	xfs_fileoff_t		block;	/* bitmap block number */
443 	int			error;
444 	xfs_rtxnum_t		i;	/* current bit number rel. to start */
445 	xfs_rtxnum_t		lastbit;/* last useful bit in the word */
446 	xfs_rtxnum_t		len;	/* length of inspected area */
447 	xfs_rtword_t		mask;	/* mask of relevant bits for value */
448 	xfs_rtword_t		want;	/* mask for "good" values */
449 	xfs_rtword_t		wdiff;	/* difference from wanted value */
450 	xfs_rtword_t		incore;
451 	unsigned int		word;	/* word number in the buffer */
452 
453 	ASSERT(start <= limit);
454 
455 	/*
456 	 * Compute and read in starting bitmap block for starting block.
457 	 */
458 	block = xfs_rtx_to_rbmblock(mp, start);
459 	error = xfs_rtbitmap_read_buf(args, block);
460 	if (error)
461 		return error;
462 
463 	/*
464 	 * Get the first word's index & point to it.
465 	 */
466 	word = xfs_rtx_to_rbmword(mp, start);
467 	bit = (int)(start & (XFS_NBWORD - 1));
468 	len = limit - start + 1;
469 	/*
470 	 * Compute match value, based on the bit at start: if 1 (free)
471 	 * then all-ones, else all-zeroes.
472 	 */
473 	incore = xfs_rtbitmap_getword(args, word);
474 	want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
475 	/*
476 	 * If the starting position is not word-aligned, deal with the
477 	 * partial word.
478 	 */
479 	if (bit) {
480 		/*
481 		 * Calculate last (rightmost) bit number to look at,
482 		 * and mask for all the relevant bits in this word.
483 		 */
484 		lastbit = min(bit + len, XFS_NBWORD);
485 		mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
486 		/*
487 		 * Calculate the difference between the value there
488 		 * and what we're looking for.
489 		 */
490 		if ((wdiff = (incore ^ want) & mask)) {
491 			/*
492 			 * Different.  Mark where we are and return.
493 			 */
494 			i = xfs_lowbit32(wdiff) - bit;
495 			*rtx = start + i - 1;
496 			return 0;
497 		}
498 		i = lastbit - bit;
499 		/*
500 		 * Go on to next block if that's where the next word is
501 		 * and we need the next word.
502 		 */
503 		if (++word == mp->m_blockwsize && i < len) {
504 			/*
505 			 * If done with this block, get the previous one.
506 			 */
507 			error = xfs_rtbitmap_read_buf(args, ++block);
508 			if (error)
509 				return error;
510 
511 			word = 0;
512 		}
513 	} else {
514 		/*
515 		 * Starting on a word boundary, no partial word.
516 		 */
517 		i = 0;
518 	}
519 	/*
520 	 * Loop over whole words in buffers.  When we use up one buffer
521 	 * we move on to the next one.
522 	 */
523 	while (len - i >= XFS_NBWORD) {
524 		/*
525 		 * Compute difference between actual and desired value.
526 		 */
527 		incore = xfs_rtbitmap_getword(args, word);
528 		if ((wdiff = incore ^ want)) {
529 			/*
530 			 * Different, mark where we are and return.
531 			 */
532 			i += xfs_lowbit32(wdiff);
533 			*rtx = start + i - 1;
534 			return 0;
535 		}
536 		i += XFS_NBWORD;
537 		/*
538 		 * Go on to next block if that's where the next word is
539 		 * and we need the next word.
540 		 */
541 		if (++word == mp->m_blockwsize && i < len) {
542 			/*
543 			 * If done with this block, get the next one.
544 			 */
545 			error = xfs_rtbitmap_read_buf(args, ++block);
546 			if (error)
547 				return error;
548 
549 			word = 0;
550 		}
551 	}
552 	/*
553 	 * If not ending on a word boundary, deal with the last
554 	 * (partial) word.
555 	 */
556 	if ((lastbit = len - i)) {
557 		/*
558 		 * Calculate mask for all the relevant bits in this word.
559 		 */
560 		mask = ((xfs_rtword_t)1 << lastbit) - 1;
561 		/*
562 		 * Compute difference between actual and desired value.
563 		 */
564 		incore = xfs_rtbitmap_getword(args, word);
565 		if ((wdiff = (incore ^ want) & mask)) {
566 			/*
567 			 * Different, mark where we are and return.
568 			 */
569 			i += xfs_lowbit32(wdiff);
570 			*rtx = start + i - 1;
571 			return 0;
572 		} else
573 			i = len;
574 	}
575 	/*
576 	 * No match, return that we scanned the whole area.
577 	 */
578 	*rtx = start + i - 1;
579 	return 0;
580 }
581 
582 /* Log rtsummary counter at @infoword. */
583 static inline void
584 xfs_trans_log_rtsummary(
585 	struct xfs_rtalloc_args	*args,
586 	unsigned int		infoword)
587 {
588 	struct xfs_buf		*bp = args->sumbp;
589 	size_t			first, last;
590 
591 	first = (void *)xfs_rsumblock_infoptr(args, infoword) - bp->b_addr;
592 	last = first + sizeof(xfs_suminfo_t) - 1;
593 
594 	xfs_trans_log_buf(args->tp, bp, first, last);
595 }
596 
597 /*
598  * Modify the summary information for a given extent size, bitmap block
599  * combination.
600  */
601 int
602 xfs_rtmodify_summary(
603 	struct xfs_rtalloc_args	*args,
604 	int			log,	/* log2 of extent size */
605 	xfs_fileoff_t		bbno,	/* bitmap block number */
606 	int			delta)	/* in/out: summary block number */
607 {
608 	struct xfs_mount	*mp = args->mp;
609 	xfs_rtsumoff_t		so = xfs_rtsumoffs(mp, log, bbno);
610 	uint8_t			*rsum_cache = args->rtg->rtg_rsum_cache;
611 	unsigned int		infoword;
612 	xfs_suminfo_t		val;
613 	int			error;
614 
615 	error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
616 	if (error)
617 		return error;
618 
619 	infoword = xfs_rtsumoffs_to_infoword(mp, so);
620 	val = xfs_suminfo_add(args, infoword, delta);
621 
622 	if (rsum_cache) {
623 		if (val == 0 && log + 1 == rsum_cache[bbno])
624 			rsum_cache[bbno] = log;
625 		if (val != 0 && log >= rsum_cache[bbno])
626 			rsum_cache[bbno] = log + 1;
627 	}
628 
629 	xfs_trans_log_rtsummary(args, infoword);
630 	return 0;
631 }
632 
633 /*
634  * Read and return the summary information for a given extent size, bitmap block
635  * combination.
636  */
637 int
638 xfs_rtget_summary(
639 	struct xfs_rtalloc_args	*args,
640 	int			log,	/* log2 of extent size */
641 	xfs_fileoff_t		bbno,	/* bitmap block number */
642 	xfs_suminfo_t		*sum)	/* out: summary info for this block */
643 {
644 	struct xfs_mount	*mp = args->mp;
645 	xfs_rtsumoff_t		so = xfs_rtsumoffs(mp, log, bbno);
646 	int			error;
647 
648 	error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
649 	if (!error)
650 		*sum = xfs_suminfo_get(args, xfs_rtsumoffs_to_infoword(mp, so));
651 	return error;
652 }
653 
654 /* Log rtbitmap block from the word @from to the byte before @next. */
655 static inline void
656 xfs_trans_log_rtbitmap(
657 	struct xfs_rtalloc_args	*args,
658 	unsigned int		from,
659 	unsigned int		next)
660 {
661 	struct xfs_buf		*bp = args->rbmbp;
662 	size_t			first, last;
663 
664 	first = (void *)xfs_rbmblock_wordptr(args, from) - bp->b_addr;
665 	last = ((void *)xfs_rbmblock_wordptr(args, next) - 1) - bp->b_addr;
666 
667 	xfs_trans_log_buf(args->tp, bp, first, last);
668 }
669 
670 /*
671  * Set the given range of bitmap bits to the given value.
672  * Do whatever I/O and logging is required.
673  */
674 int
675 xfs_rtmodify_range(
676 	struct xfs_rtalloc_args	*args,
677 	xfs_rtxnum_t		start,	/* starting rtext to modify */
678 	xfs_rtxlen_t		len,	/* length of extent to modify */
679 	int			val)	/* 1 for free, 0 for allocated */
680 {
681 	struct xfs_mount	*mp = args->mp;
682 	int			bit;	/* bit number in the word */
683 	xfs_fileoff_t		block;	/* bitmap block number */
684 	int			error;
685 	int			i;	/* current bit number rel. to start */
686 	int			lastbit; /* last useful bit in word */
687 	xfs_rtword_t		mask;	 /* mask of relevant bits for value */
688 	xfs_rtword_t		incore;
689 	unsigned int		firstword; /* first word used in the buffer */
690 	unsigned int		word;	/* word number in the buffer */
691 
692 	/*
693 	 * Compute starting bitmap block number.
694 	 */
695 	block = xfs_rtx_to_rbmblock(mp, start);
696 	/*
697 	 * Read the bitmap block, and point to its data.
698 	 */
699 	error = xfs_rtbitmap_read_buf(args, block);
700 	if (error)
701 		return error;
702 
703 	/*
704 	 * Compute the starting word's address, and starting bit.
705 	 */
706 	firstword = word = xfs_rtx_to_rbmword(mp, start);
707 	bit = (int)(start & (XFS_NBWORD - 1));
708 	/*
709 	 * 0 (allocated) => all zeroes; 1 (free) => all ones.
710 	 */
711 	val = -val;
712 	/*
713 	 * If not starting on a word boundary, deal with the first
714 	 * (partial) word.
715 	 */
716 	if (bit) {
717 		/*
718 		 * Compute first bit not changed and mask of relevant bits.
719 		 */
720 		lastbit = min(bit + len, XFS_NBWORD);
721 		mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
722 		/*
723 		 * Set/clear the active bits.
724 		 */
725 		incore = xfs_rtbitmap_getword(args, word);
726 		if (val)
727 			incore |= mask;
728 		else
729 			incore &= ~mask;
730 		xfs_rtbitmap_setword(args, word, incore);
731 		i = lastbit - bit;
732 		/*
733 		 * Go on to the next block if that's where the next word is
734 		 * and we need the next word.
735 		 */
736 		if (++word == mp->m_blockwsize && i < len) {
737 			/*
738 			 * Log the changed part of this block.
739 			 * Get the next one.
740 			 */
741 			xfs_trans_log_rtbitmap(args, firstword, word);
742 			error = xfs_rtbitmap_read_buf(args, ++block);
743 			if (error)
744 				return error;
745 
746 			firstword = word = 0;
747 		}
748 	} else {
749 		/*
750 		 * Starting on a word boundary, no partial word.
751 		 */
752 		i = 0;
753 	}
754 	/*
755 	 * Loop over whole words in buffers.  When we use up one buffer
756 	 * we move on to the next one.
757 	 */
758 	while (len - i >= XFS_NBWORD) {
759 		/*
760 		 * Set the word value correctly.
761 		 */
762 		xfs_rtbitmap_setword(args, word, val);
763 		i += XFS_NBWORD;
764 		/*
765 		 * Go on to the next block if that's where the next word is
766 		 * and we need the next word.
767 		 */
768 		if (++word == mp->m_blockwsize && i < len) {
769 			/*
770 			 * Log the changed part of this block.
771 			 * Get the next one.
772 			 */
773 			xfs_trans_log_rtbitmap(args, firstword, word);
774 			error = xfs_rtbitmap_read_buf(args, ++block);
775 			if (error)
776 				return error;
777 
778 			firstword = word = 0;
779 		}
780 	}
781 	/*
782 	 * If not ending on a word boundary, deal with the last
783 	 * (partial) word.
784 	 */
785 	if ((lastbit = len - i)) {
786 		/*
787 		 * Compute a mask of relevant bits.
788 		 */
789 		mask = ((xfs_rtword_t)1 << lastbit) - 1;
790 		/*
791 		 * Set/clear the active bits.
792 		 */
793 		incore = xfs_rtbitmap_getword(args, word);
794 		if (val)
795 			incore |= mask;
796 		else
797 			incore &= ~mask;
798 		xfs_rtbitmap_setword(args, word, incore);
799 		word++;
800 	}
801 	/*
802 	 * Log any remaining changed bytes.
803 	 */
804 	if (word > firstword)
805 		xfs_trans_log_rtbitmap(args, firstword, word);
806 	return 0;
807 }
808 
809 /*
810  * Mark an extent specified by start and len freed.
811  * Updates all the summary information as well as the bitmap.
812  */
813 int
814 xfs_rtfree_range(
815 	struct xfs_rtalloc_args	*args,
816 	xfs_rtxnum_t		start,	/* starting rtext to free */
817 	xfs_rtxlen_t		len)	/* in/out: summary block number */
818 {
819 	struct xfs_mount	*mp = args->mp;
820 	xfs_rtxnum_t		end;	/* end of the freed extent */
821 	int			error;	/* error value */
822 	xfs_rtxnum_t		postblock; /* first rtext freed > end */
823 	xfs_rtxnum_t		preblock;  /* first rtext freed < start */
824 
825 	end = start + len - 1;
826 	/*
827 	 * Modify the bitmap to mark this extent freed.
828 	 */
829 	error = xfs_rtmodify_range(args, start, len, 1);
830 	if (error) {
831 		return error;
832 	}
833 	/*
834 	 * Assume we're freeing out of the middle of an allocated extent.
835 	 * We need to find the beginning and end of the extent so we can
836 	 * properly update the summary.
837 	 */
838 	error = xfs_rtfind_back(args, start, &preblock);
839 	if (error) {
840 		return error;
841 	}
842 	/*
843 	 * Find the next allocated block (end of allocated extent).
844 	 */
845 	error = xfs_rtfind_forw(args, end, args->rtg->rtg_extents - 1,
846 			&postblock);
847 	if (error)
848 		return error;
849 	/*
850 	 * If there are blocks not being freed at the front of the
851 	 * old extent, add summary data for them to be allocated.
852 	 */
853 	if (preblock < start) {
854 		error = xfs_rtmodify_summary(args,
855 				xfs_highbit64(start - preblock),
856 				xfs_rtx_to_rbmblock(mp, preblock), -1);
857 		if (error) {
858 			return error;
859 		}
860 	}
861 	/*
862 	 * If there are blocks not being freed at the end of the
863 	 * old extent, add summary data for them to be allocated.
864 	 */
865 	if (postblock > end) {
866 		error = xfs_rtmodify_summary(args,
867 				xfs_highbit64(postblock - end),
868 				xfs_rtx_to_rbmblock(mp, end + 1), -1);
869 		if (error) {
870 			return error;
871 		}
872 	}
873 	/*
874 	 * Increment the summary information corresponding to the entire
875 	 * (new) free extent.
876 	 */
877 	return xfs_rtmodify_summary(args,
878 			xfs_highbit64(postblock + 1 - preblock),
879 			xfs_rtx_to_rbmblock(mp, preblock), 1);
880 }
881 
882 /*
883  * Check that the given range is either all allocated (val = 0) or
884  * all free (val = 1).
885  */
886 int
887 xfs_rtcheck_range(
888 	struct xfs_rtalloc_args	*args,
889 	xfs_rtxnum_t		start,	/* starting rtext number of extent */
890 	xfs_rtxlen_t		len,	/* length of extent */
891 	int			val,	/* 1 for free, 0 for allocated */
892 	xfs_rtxnum_t		*new,	/* out: first rtext not matching */
893 	int			*stat)	/* out: 1 for matches, 0 for not */
894 {
895 	struct xfs_mount	*mp = args->mp;
896 	int			bit;	/* bit number in the word */
897 	xfs_fileoff_t		block;	/* bitmap block number */
898 	int			error;
899 	xfs_rtxnum_t		i;	/* current bit number rel. to start */
900 	xfs_rtxnum_t		lastbit; /* last useful bit in word */
901 	xfs_rtword_t		mask;	/* mask of relevant bits for value */
902 	xfs_rtword_t		wdiff;	/* difference from wanted value */
903 	xfs_rtword_t		incore;
904 	unsigned int		word;	/* word number in the buffer */
905 
906 	/*
907 	 * Compute starting bitmap block number
908 	 */
909 	block = xfs_rtx_to_rbmblock(mp, start);
910 	/*
911 	 * Read the bitmap block.
912 	 */
913 	error = xfs_rtbitmap_read_buf(args, block);
914 	if (error)
915 		return error;
916 
917 	/*
918 	 * Compute the starting word's address, and starting bit.
919 	 */
920 	word = xfs_rtx_to_rbmword(mp, start);
921 	bit = (int)(start & (XFS_NBWORD - 1));
922 	/*
923 	 * 0 (allocated) => all zero's; 1 (free) => all one's.
924 	 */
925 	val = -val;
926 	/*
927 	 * If not starting on a word boundary, deal with the first
928 	 * (partial) word.
929 	 */
930 	if (bit) {
931 		/*
932 		 * Compute first bit not examined.
933 		 */
934 		lastbit = min(bit + len, XFS_NBWORD);
935 		/*
936 		 * Mask of relevant bits.
937 		 */
938 		mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
939 		/*
940 		 * Compute difference between actual and desired value.
941 		 */
942 		incore = xfs_rtbitmap_getword(args, word);
943 		if ((wdiff = (incore ^ val) & mask)) {
944 			/*
945 			 * Different, compute first wrong bit and return.
946 			 */
947 			i = xfs_lowbit32(wdiff) - bit;
948 			*new = start + i;
949 			*stat = 0;
950 			return 0;
951 		}
952 		i = lastbit - bit;
953 		/*
954 		 * Go on to next block if that's where the next word is
955 		 * and we need the next word.
956 		 */
957 		if (++word == mp->m_blockwsize && i < len) {
958 			/*
959 			 * If done with this block, get the next one.
960 			 */
961 			error = xfs_rtbitmap_read_buf(args, ++block);
962 			if (error)
963 				return error;
964 
965 			word = 0;
966 		}
967 	} else {
968 		/*
969 		 * Starting on a word boundary, no partial word.
970 		 */
971 		i = 0;
972 	}
973 	/*
974 	 * Loop over whole words in buffers.  When we use up one buffer
975 	 * we move on to the next one.
976 	 */
977 	while (len - i >= XFS_NBWORD) {
978 		/*
979 		 * Compute difference between actual and desired value.
980 		 */
981 		incore = xfs_rtbitmap_getword(args, word);
982 		if ((wdiff = incore ^ val)) {
983 			/*
984 			 * Different, compute first wrong bit and return.
985 			 */
986 			i += xfs_lowbit32(wdiff);
987 			*new = start + i;
988 			*stat = 0;
989 			return 0;
990 		}
991 		i += XFS_NBWORD;
992 		/*
993 		 * Go on to next block if that's where the next word is
994 		 * and we need the next word.
995 		 */
996 		if (++word == mp->m_blockwsize && i < len) {
997 			/*
998 			 * If done with this block, get the next one.
999 			 */
1000 			error = xfs_rtbitmap_read_buf(args, ++block);
1001 			if (error)
1002 				return error;
1003 
1004 			word = 0;
1005 		}
1006 	}
1007 	/*
1008 	 * If not ending on a word boundary, deal with the last
1009 	 * (partial) word.
1010 	 */
1011 	if ((lastbit = len - i)) {
1012 		/*
1013 		 * Mask of relevant bits.
1014 		 */
1015 		mask = ((xfs_rtword_t)1 << lastbit) - 1;
1016 		/*
1017 		 * Compute difference between actual and desired value.
1018 		 */
1019 		incore = xfs_rtbitmap_getword(args, word);
1020 		if ((wdiff = (incore ^ val) & mask)) {
1021 			/*
1022 			 * Different, compute first wrong bit and return.
1023 			 */
1024 			i += xfs_lowbit32(wdiff);
1025 			*new = start + i;
1026 			*stat = 0;
1027 			return 0;
1028 		} else
1029 			i = len;
1030 	}
1031 	/*
1032 	 * Successful, return.
1033 	 */
1034 	*new = start + i;
1035 	*stat = 1;
1036 	return 0;
1037 }
1038 
1039 #ifdef DEBUG
1040 /*
1041  * Check that the given extent (block range) is allocated already.
1042  */
1043 STATIC int
1044 xfs_rtcheck_alloc_range(
1045 	struct xfs_rtalloc_args	*args,
1046 	xfs_rtxnum_t		start,	/* starting rtext number of extent */
1047 	xfs_rtxlen_t		len)	/* length of extent */
1048 {
1049 	xfs_rtxnum_t		new;	/* dummy for xfs_rtcheck_range */
1050 	int			stat;
1051 	int			error;
1052 
1053 	error = xfs_rtcheck_range(args, start, len, 0, &new, &stat);
1054 	if (error)
1055 		return error;
1056 	ASSERT(stat);
1057 	return 0;
1058 }
1059 #else
1060 #define xfs_rtcheck_alloc_range(a,b,l)	(0)
1061 #endif
1062 /*
1063  * Free an extent in the realtime subvolume.  Length is expressed in
1064  * realtime extents, as is the block number.
1065  */
1066 int
1067 xfs_rtfree_extent(
1068 	struct xfs_trans	*tp,	/* transaction pointer */
1069 	struct xfs_rtgroup	*rtg,
1070 	xfs_rtxnum_t		start,	/* starting rtext number to free */
1071 	xfs_rtxlen_t		len)	/* length of extent freed */
1072 {
1073 	struct xfs_mount	*mp = tp->t_mountp;
1074 	struct xfs_inode	*rbmip = rtg_bitmap(rtg);
1075 	struct xfs_rtalloc_args	args = {
1076 		.mp		= mp,
1077 		.tp		= tp,
1078 		.rtg		= rtg,
1079 	};
1080 	int			error;
1081 	struct timespec64	atime;
1082 
1083 	ASSERT(rbmip->i_itemp != NULL);
1084 	xfs_assert_ilocked(rbmip, XFS_ILOCK_EXCL);
1085 
1086 	if (XFS_TEST_ERROR(mp, XFS_ERRTAG_FREE_EXTENT))
1087 		return -EIO;
1088 
1089 	error = xfs_rtcheck_alloc_range(&args, start, len);
1090 	if (error)
1091 		return error;
1092 
1093 	/*
1094 	 * Free the range of realtime blocks.
1095 	 */
1096 	error = xfs_rtfree_range(&args, start, len);
1097 	if (error)
1098 		goto out;
1099 
1100 	/*
1101 	 * Mark more blocks free in the superblock.
1102 	 */
1103 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
1104 
1105 	/*
1106 	 * If we've now freed all the blocks, reset the file sequence
1107 	 * number to 0 for pre-RTG file systems.
1108 	 */
1109 	if (!xfs_has_rtgroups(mp) &&
1110 	    tp->t_frextents_delta + mp->m_sb.sb_frextents ==
1111 	    mp->m_sb.sb_rextents) {
1112 		if (!(rbmip->i_diflags & XFS_DIFLAG_NEWRTBM))
1113 			rbmip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1114 
1115 		atime = inode_get_atime(VFS_I(rbmip));
1116 		atime.tv_sec = 0;
1117 		inode_set_atime_to_ts(VFS_I(rbmip), atime);
1118 		xfs_trans_log_inode(tp, rbmip, XFS_ILOG_CORE);
1119 	}
1120 	error = 0;
1121 out:
1122 	xfs_rtbuf_cache_relse(&args);
1123 	return error;
1124 }
1125 
1126 /*
1127  * Free some blocks in the realtime subvolume.  rtbno and rtlen are in units of
1128  * rt blocks, not rt extents; must be aligned to the rt extent size; and rtlen
1129  * cannot exceed XFS_MAX_BMBT_EXTLEN.
1130  */
1131 int
1132 xfs_rtfree_blocks(
1133 	struct xfs_trans	*tp,
1134 	struct xfs_rtgroup	*rtg,
1135 	xfs_fsblock_t		rtbno,
1136 	xfs_filblks_t		rtlen)
1137 {
1138 	struct xfs_mount	*mp = tp->t_mountp;
1139 	xfs_extlen_t		mod;
1140 	int			error;
1141 
1142 	ASSERT(!xfs_has_zoned(mp));
1143 	ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
1144 
1145 	mod = xfs_blen_to_rtxoff(mp, rtlen);
1146 	if (mod) {
1147 		ASSERT(mod == 0);
1148 		return -EIO;
1149 	}
1150 
1151 	mod = xfs_rtb_to_rtxoff(mp, rtbno);
1152 	if (mod) {
1153 		ASSERT(mod == 0);
1154 		return -EIO;
1155 	}
1156 
1157 	error = xfs_rtfree_extent(tp, rtg, xfs_rtb_to_rtx(mp, rtbno),
1158 			xfs_extlen_to_rtxlen(mp, rtlen));
1159 	if (error)
1160 		return error;
1161 
1162 	if (xfs_has_rtgroups(mp))
1163 		xfs_extent_busy_insert(tp, rtg_group(rtg),
1164 				xfs_rtb_to_rgbno(mp, rtbno), rtlen, 0);
1165 
1166 	return 0;
1167 }
1168 
1169 /* Find all the free records within a given range. */
1170 int
1171 xfs_rtalloc_query_range(
1172 	struct xfs_rtgroup		*rtg,
1173 	struct xfs_trans		*tp,
1174 	xfs_rtxnum_t			start,
1175 	xfs_rtxnum_t			end,
1176 	xfs_rtalloc_query_range_fn	fn,
1177 	void				*priv)
1178 {
1179 	struct xfs_mount		*mp = rtg_mount(rtg);
1180 	struct xfs_rtalloc_args		args = {
1181 		.rtg			= rtg,
1182 		.mp			= mp,
1183 		.tp			= tp,
1184 	};
1185 	int				error = 0;
1186 
1187 	if (start > end)
1188 		return -EINVAL;
1189 	if (start == end || start >= rtg->rtg_extents)
1190 		return 0;
1191 
1192 	end = min(end, rtg->rtg_extents - 1);
1193 
1194 	if (xfs_has_zoned(mp))
1195 		return -EINVAL;
1196 
1197 	/* Iterate the bitmap, looking for discrepancies. */
1198 	while (start <= end) {
1199 		struct xfs_rtalloc_rec	rec;
1200 		int			is_free;
1201 		xfs_rtxnum_t		rtend;
1202 
1203 		/* Is the first block free? */
1204 		error = xfs_rtcheck_range(&args, start, 1, 1, &rtend,
1205 				&is_free);
1206 		if (error)
1207 			break;
1208 
1209 		/* How long does the extent go for? */
1210 		error = xfs_rtfind_forw(&args, start, end, &rtend);
1211 		if (error)
1212 			break;
1213 
1214 		if (is_free) {
1215 			rec.ar_startext = start;
1216 			rec.ar_extcount = rtend - start + 1;
1217 
1218 			error = fn(rtg, tp, &rec, priv);
1219 			if (error)
1220 				break;
1221 		}
1222 
1223 		start = rtend + 1;
1224 	}
1225 
1226 	xfs_rtbuf_cache_relse(&args);
1227 	return error;
1228 }
1229 
1230 /* Find all the free records. */
1231 int
1232 xfs_rtalloc_query_all(
1233 	struct xfs_rtgroup		*rtg,
1234 	struct xfs_trans		*tp,
1235 	xfs_rtalloc_query_range_fn	fn,
1236 	void				*priv)
1237 {
1238 	return xfs_rtalloc_query_range(rtg, tp, 0, rtg->rtg_extents - 1, fn,
1239 			priv);
1240 }
1241 
1242 /* Is the given extent all free? */
1243 int
1244 xfs_rtalloc_extent_is_free(
1245 	struct xfs_rtgroup		*rtg,
1246 	struct xfs_trans		*tp,
1247 	xfs_rtxnum_t			start,
1248 	xfs_rtxlen_t			len,
1249 	bool				*is_free)
1250 {
1251 	struct xfs_rtalloc_args		args = {
1252 		.mp			= rtg_mount(rtg),
1253 		.rtg			= rtg,
1254 		.tp			= tp,
1255 	};
1256 	xfs_rtxnum_t			end;
1257 	int				matches;
1258 	int				error;
1259 
1260 	error = xfs_rtcheck_range(&args, start, len, 1, &end, &matches);
1261 	xfs_rtbuf_cache_relse(&args);
1262 	if (error)
1263 		return error;
1264 
1265 	*is_free = matches;
1266 	return 0;
1267 }
1268 
1269 /* Compute the number of rt extents tracked by a single bitmap block. */
1270 xfs_rtxnum_t
1271 xfs_rtbitmap_rtx_per_rbmblock(
1272 	struct xfs_mount	*mp)
1273 {
1274 	unsigned int		rbmblock_bytes = mp->m_sb.sb_blocksize;
1275 
1276 	if (xfs_has_rtgroups(mp))
1277 		rbmblock_bytes -= sizeof(struct xfs_rtbuf_blkinfo);
1278 
1279 	return rbmblock_bytes * NBBY;
1280 }
1281 
1282 /*
1283  * Compute the number of rtbitmap blocks needed to track the given number of rt
1284  * extents.
1285  */
1286 xfs_filblks_t
1287 xfs_rtbitmap_blockcount_len(
1288 	struct xfs_mount	*mp,
1289 	xfs_rtbxlen_t		rtextents)
1290 {
1291 	if (xfs_has_zoned(mp))
1292 		return 0;
1293 	return howmany_64(rtextents, xfs_rtbitmap_rtx_per_rbmblock(mp));
1294 }
1295 
1296 /* How many rt extents does each rtbitmap file track? */
1297 static inline xfs_rtbxlen_t
1298 xfs_rtbitmap_bitcount(
1299 	struct xfs_mount	*mp)
1300 {
1301 	if (!mp->m_sb.sb_rextents)
1302 		return 0;
1303 
1304 	/* rtgroup size can be nonzero even if rextents is zero */
1305 	if (xfs_has_rtgroups(mp))
1306 		return mp->m_sb.sb_rgextents;
1307 
1308 	return mp->m_sb.sb_rextents;
1309 }
1310 
1311 /*
1312  * Compute the number of rtbitmap blocks used for a given file system.
1313  */
1314 xfs_filblks_t
1315 xfs_rtbitmap_blockcount(
1316 	struct xfs_mount	*mp)
1317 {
1318 	return xfs_rtbitmap_blockcount_len(mp, xfs_rtbitmap_bitcount(mp));
1319 }
1320 
1321 /*
1322  * Compute the geometry of the rtsummary file needed to track the given rt
1323  * space.
1324  */
1325 xfs_filblks_t
1326 xfs_rtsummary_blockcount(
1327 	struct xfs_mount	*mp,
1328 	unsigned int		*rsumlevels)
1329 {
1330 	xfs_rtbxlen_t		rextents = xfs_rtbitmap_bitcount(mp);
1331 	unsigned long long	rsumwords;
1332 
1333 	if (xfs_has_zoned(mp)) {
1334 		*rsumlevels = 0;
1335 		return 0;
1336 	}
1337 
1338 	*rsumlevels = xfs_compute_rextslog(rextents) + 1;
1339 	rsumwords = xfs_rtbitmap_blockcount_len(mp, rextents) * (*rsumlevels);
1340 	return howmany_64(rsumwords, mp->m_blockwsize);
1341 }
1342 
1343 static int
1344 xfs_rtfile_alloc_blocks(
1345 	struct xfs_inode	*ip,
1346 	xfs_fileoff_t		offset_fsb,
1347 	xfs_filblks_t		count_fsb,
1348 	struct xfs_bmbt_irec	*map)
1349 {
1350 	struct xfs_mount	*mp = ip->i_mount;
1351 	struct xfs_trans	*tp;
1352 	int			nmap = 1;
1353 	int			error;
1354 
1355 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtalloc,
1356 			XFS_GROWFSRT_SPACE_RES(mp, count_fsb), 0, 0, &tp);
1357 	if (error)
1358 		return error;
1359 
1360 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1361 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1362 
1363 	error = xfs_iext_count_extend(tp, ip, XFS_DATA_FORK,
1364 				XFS_IEXT_ADD_NOSPLIT_CNT);
1365 	if (error)
1366 		goto out_trans_cancel;
1367 
1368 	error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
1369 			XFS_BMAPI_METADATA, 0, map, &nmap);
1370 	if (error)
1371 		goto out_trans_cancel;
1372 
1373 	return xfs_trans_commit(tp);
1374 
1375 out_trans_cancel:
1376 	xfs_trans_cancel(tp);
1377 	return error;
1378 }
1379 
1380 void
1381 xfs_rtfile_initialize_buf(
1382 	struct xfs_rtgroup	*rtg,
1383 	enum xfs_rtg_inodes	type,
1384 	struct xfs_buf		*bp,
1385 	struct xfs_trans	*tp)
1386 {
1387 	bp->b_ops = xfs_rtblock_ops(bp->b_mount, type);
1388 	if (tp)
1389 		xfs_trans_buf_set_type(tp, bp, xfs_rtblock_buf_types[type]);
1390 	if (xfs_has_rtgroups(bp->b_mount)) {
1391 		struct xfs_rtbuf_blkinfo	*hdr = bp->b_addr;
1392 
1393 		hdr->rt_magic = bp->b_ops->magic[1];
1394 		hdr->rt_owner = cpu_to_be64(I_INO(rtg->rtg_inodes[type]));
1395 		hdr->rt_blkno = cpu_to_be64(xfs_buf_daddr(bp));
1396 		hdr->rt_lsn = 0;
1397 		uuid_copy(&hdr->rt_uuid, &bp->b_mount->m_sb.sb_meta_uuid);
1398 	}
1399 }
1400 
1401 /* Get a buffer for the block. */
1402 static int
1403 xfs_rtfile_initialize_block(
1404 	struct xfs_rtgroup	*rtg,
1405 	enum xfs_rtg_inodes	type,
1406 	xfs_fsblock_t		fsbno,
1407 	void			*data)
1408 {
1409 	struct xfs_mount	*mp = rtg_mount(rtg);
1410 	struct xfs_inode	*ip = rtg->rtg_inodes[type];
1411 	struct xfs_trans	*tp;
1412 	struct xfs_buf		*bp;
1413 	const size_t		copylen = mp->m_blockwsize << XFS_WORDLOG;
1414 	int			error;
1415 
1416 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero, 0, 0, 0, &tp);
1417 	if (error)
1418 		return error;
1419 	xfs_ilock(ip, XFS_ILOCK_EXCL);
1420 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1421 
1422 	error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1423 			XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp);
1424 	if (error) {
1425 		xfs_trans_cancel(tp);
1426 		return error;
1427 	}
1428 
1429 	xfs_rtfile_initialize_buf(rtg, type, bp, tp);
1430 	if (data)
1431 		memcpy(xfs_rtblock_payload(bp), data, copylen);
1432 	else
1433 		memset(xfs_rtblock_payload(bp), 0, copylen);
1434 	xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
1435 	return xfs_trans_commit(tp);
1436 }
1437 
1438 /*
1439  * Allocate space to the bitmap or summary file, and zero it, for growfs.
1440  * @data must be a contiguous buffer large enough to fill all blocks in the
1441  * file; or NULL to initialize the contents to zeroes.
1442  */
1443 int
1444 xfs_rtfile_initialize_blocks(
1445 	struct xfs_rtgroup	*rtg,
1446 	enum xfs_rtg_inodes	type,
1447 	xfs_fileoff_t		offset_fsb,	/* offset to start from */
1448 	xfs_fileoff_t		end_fsb,	/* offset to allocate to */
1449 	void			*data)		/* data to fill the blocks */
1450 {
1451 	struct xfs_mount	*mp = rtg_mount(rtg);
1452 	const size_t		copylen = mp->m_blockwsize << XFS_WORDLOG;
1453 
1454 	while (offset_fsb < end_fsb) {
1455 		struct xfs_bmbt_irec	map;
1456 		xfs_filblks_t		i;
1457 		int			error;
1458 
1459 		error = xfs_rtfile_alloc_blocks(rtg->rtg_inodes[type],
1460 				offset_fsb, end_fsb - offset_fsb, &map);
1461 		if (error)
1462 			return error;
1463 
1464 		/*
1465 		 * Now we need to clear the allocated blocks.
1466 		 *
1467 		 * Do this one block per transaction, to keep it simple.
1468 		 */
1469 		for (i = 0; i < map.br_blockcount; i++) {
1470 			error = xfs_rtfile_initialize_block(rtg, type,
1471 					map.br_startblock + i, data);
1472 			if (error)
1473 				return error;
1474 			if (data)
1475 				data += copylen;
1476 		}
1477 
1478 		offset_fsb = map.br_startoff + map.br_blockcount;
1479 	}
1480 
1481 	return 0;
1482 }
1483 
1484 int
1485 xfs_rtbitmap_create(
1486 	struct xfs_rtgroup	*rtg,
1487 	struct xfs_inode	*ip,
1488 	struct xfs_trans	*tp,
1489 	bool			init)
1490 {
1491 	struct xfs_mount	*mp = rtg_mount(rtg);
1492 
1493 	ip->i_disk_size = mp->m_sb.sb_rbmblocks * mp->m_sb.sb_blocksize;
1494 	if (init && !xfs_has_rtgroups(mp)) {
1495 		ip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1496 		inode_set_atime(VFS_I(ip), 0, 0);
1497 	}
1498 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1499 	return 0;
1500 }
1501 
1502 int
1503 xfs_rtsummary_create(
1504 	struct xfs_rtgroup	*rtg,
1505 	struct xfs_inode	*ip,
1506 	struct xfs_trans	*tp,
1507 	bool			init)
1508 {
1509 	struct xfs_mount	*mp = rtg_mount(rtg);
1510 
1511 	ip->i_disk_size = mp->m_rsumblocks * mp->m_sb.sb_blocksize;
1512 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1513 	return 0;
1514 }
1515