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