drm/panel-edp: Add STA 116QHD024002
[drm/drm-misc.git] / fs / xfs / libxfs / xfs_rtbitmap.c
blob4ddfb7e395b38af2d3ee8f9de109053183c8903a
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"
30 * Realtime allocator bitmap functions shared with userspace.
33 static xfs_failaddr_t
34 xfs_rtbuf_verify(
35 struct xfs_buf *bp)
37 struct xfs_mount *mp = bp->b_mount;
38 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
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;
53 static void
54 xfs_rtbuf_verify_read(
55 struct xfs_buf *bp)
57 struct xfs_mount *mp = bp->b_mount;
58 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
59 xfs_failaddr_t fa;
61 if (!xfs_has_rtgroups(mp))
62 return;
64 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr->rt_lsn))) {
65 fa = __this_address;
66 goto fail;
69 if (!xfs_buf_verify_cksum(bp, XFS_RTBUF_CRC_OFF)) {
70 fa = __this_address;
71 goto fail;
74 fa = xfs_rtbuf_verify(bp);
75 if (fa)
76 goto fail;
78 return;
79 fail:
80 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
83 static void
84 xfs_rtbuf_verify_write(
85 struct xfs_buf *bp)
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;
92 if (!xfs_has_rtgroups(mp))
93 return;
95 fa = xfs_rtbuf_verify(bp);
96 if (fa) {
97 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
98 return;
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);
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,
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,
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,
128 /* Release cached rt bitmap and summary buffers. */
129 void
130 xfs_rtbuf_cache_relse(
131 struct xfs_rtalloc_args *args)
133 if (args->rbmbp) {
134 xfs_trans_brelse(args->tp, args->rbmbp);
135 args->rbmbp = NULL;
136 args->rbmoff = NULLFILEOFF;
138 if (args->sumbp) {
139 xfs_trans_brelse(args->tp, args->sumbp);
140 args->sumbp = NULL;
141 args->sumoff = NULLFILEOFF;
146 * Get a buffer for the bitmap or summary file block specified.
147 * The buffer is returned read and locked.
149 static int
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)
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;
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;
181 * If we have a cached buffer, and the block number matches, use that.
183 if (*cbpp && *coffp == block)
184 return 0;
187 * Otherwise we have to have to get the buffer. If there was an old
188 * one, get rid of it first.
190 if (*cbpp) {
191 xfs_trans_brelse(args->tp, *cbpp);
192 *cbpp = NULL;
195 error = xfs_bmapi_read(ip, block, 1, &map, &nmap, 0);
196 if (error)
197 return error;
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;
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;
214 if (xfs_has_rtgroups(mp)) {
215 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
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;
225 xfs_trans_buf_set_type(args->tp, bp, buf_type);
226 *cbpp = bp;
227 *coffp = block;
228 return 0;
232 xfs_rtbitmap_read_buf(
233 struct xfs_rtalloc_args *args,
234 xfs_fileoff_t block)
236 struct xfs_mount *mp = args->mp;
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;
243 return xfs_rtbuf_get(args, block, XFS_RTGI_BITMAP);
247 xfs_rtsummary_read_buf(
248 struct xfs_rtalloc_args *args,
249 xfs_fileoff_t block)
251 struct xfs_mount *mp = args->mp;
253 if (XFS_IS_CORRUPT(mp, block >= mp->m_rsumblocks)) {
254 xfs_rtginode_mark_sick(args->rtg, XFS_RTGI_SUMMARY);
255 return -EFSCORRUPTED;
257 return xfs_rtbuf_get(args, block, XFS_RTGI_SUMMARY);
261 * Searching backward from start find the first block whose allocated/free state
262 * is different from start's.
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 */
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 */
284 * Compute and read in starting bitmap block for starting block.
286 block = xfs_rtx_to_rbmblock(mp, start);
287 error = xfs_rtbitmap_read_buf(args, block);
288 if (error)
289 return error;
292 * Get the first word's index & point to it.
294 word = xfs_rtx_to_rbmword(mp, start);
295 bit = (int)(start & (XFS_NBWORD - 1));
296 len = start + 1;
298 * Compute match value, based on the bit at start: if 1 (free)
299 * then all-ones, else all-zeroes.
301 incore = xfs_rtbitmap_getword(args, word);
302 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
304 * If the starting position is not word-aligned, deal with the
305 * partial word.
307 if (bit < XFS_NBWORD - 1) {
309 * Calculate first (leftmost) bit number to look at,
310 * and mask for all the relevant bits in this word.
312 firstbit = max_t(xfs_srtblock_t, bit - len + 1, 0);
313 mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
314 firstbit;
316 * Calculate the difference between the value there
317 * and what we're looking for.
319 if ((wdiff = (incore ^ want) & mask)) {
321 * Different. Mark where we are and return.
323 i = bit - xfs_highbit32(wdiff);
324 *rtx = start - i + 1;
325 return 0;
327 i = bit - firstbit + 1;
329 * Go on to previous block if that's where the previous word is
330 * and we need the previous word.
332 if (--word == -1 && i < len) {
334 * If done with this block, get the previous one.
336 error = xfs_rtbitmap_read_buf(args, --block);
337 if (error)
338 return error;
340 word = mp->m_blockwsize - 1;
342 } else {
344 * Starting on a word boundary, no partial word.
346 i = 0;
349 * Loop over whole words in buffers. When we use up one buffer
350 * we move on to the previous one.
352 while (len - i >= XFS_NBWORD) {
354 * Compute difference between actual and desired value.
356 incore = xfs_rtbitmap_getword(args, word);
357 if ((wdiff = incore ^ want)) {
359 * Different, mark where we are and return.
361 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
362 *rtx = start - i + 1;
363 return 0;
365 i += XFS_NBWORD;
367 * Go on to previous block if that's where the previous word is
368 * and we need the previous word.
370 if (--word == -1 && i < len) {
372 * If done with this block, get the previous one.
374 error = xfs_rtbitmap_read_buf(args, --block);
375 if (error)
376 return error;
378 word = mp->m_blockwsize - 1;
382 * If not ending on a word boundary, deal with the last
383 * (partial) word.
385 if (len - i) {
387 * Calculate first (leftmost) bit number to look at,
388 * and mask for all the relevant bits in this word.
390 firstbit = XFS_NBWORD - (len - i);
391 mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
393 * Compute difference between actual and desired value.
395 incore = xfs_rtbitmap_getword(args, word);
396 if ((wdiff = (incore ^ want) & mask)) {
398 * Different, mark where we are and return.
400 i += XFS_NBWORD - 1 - xfs_highbit32(wdiff);
401 *rtx = start - i + 1;
402 return 0;
403 } else
404 i = len;
407 * No match, return that we scanned the whole area.
409 *rtx = start - i + 1;
410 return 0;
414 * Searching forward from start to limit, find the first block whose
415 * allocated/free state is different from start's.
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 */
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 */
437 ASSERT(start <= limit);
440 * Compute and read in starting bitmap block for starting block.
442 block = xfs_rtx_to_rbmblock(mp, start);
443 error = xfs_rtbitmap_read_buf(args, block);
444 if (error)
445 return error;
448 * Get the first word's index & point to it.
450 word = xfs_rtx_to_rbmword(mp, start);
451 bit = (int)(start & (XFS_NBWORD - 1));
452 len = limit - start + 1;
454 * Compute match value, based on the bit at start: if 1 (free)
455 * then all-ones, else all-zeroes.
457 incore = xfs_rtbitmap_getword(args, word);
458 want = (incore & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
460 * If the starting position is not word-aligned, deal with the
461 * partial word.
463 if (bit) {
465 * Calculate last (rightmost) bit number to look at,
466 * and mask for all the relevant bits in this word.
468 lastbit = min(bit + len, XFS_NBWORD);
469 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
471 * Calculate the difference between the value there
472 * and what we're looking for.
474 if ((wdiff = (incore ^ want) & mask)) {
476 * Different. Mark where we are and return.
478 i = xfs_lowbit32(wdiff) - bit;
479 *rtx = start + i - 1;
480 return 0;
482 i = lastbit - bit;
484 * Go on to next block if that's where the next word is
485 * and we need the next word.
487 if (++word == mp->m_blockwsize && i < len) {
489 * If done with this block, get the previous one.
491 error = xfs_rtbitmap_read_buf(args, ++block);
492 if (error)
493 return error;
495 word = 0;
497 } else {
499 * Starting on a word boundary, no partial word.
501 i = 0;
504 * Loop over whole words in buffers. When we use up one buffer
505 * we move on to the next one.
507 while (len - i >= XFS_NBWORD) {
509 * Compute difference between actual and desired value.
511 incore = xfs_rtbitmap_getword(args, word);
512 if ((wdiff = incore ^ want)) {
514 * Different, mark where we are and return.
516 i += xfs_lowbit32(wdiff);
517 *rtx = start + i - 1;
518 return 0;
520 i += XFS_NBWORD;
522 * Go on to next block if that's where the next word is
523 * and we need the next word.
525 if (++word == mp->m_blockwsize && i < len) {
527 * If done with this block, get the next one.
529 error = xfs_rtbitmap_read_buf(args, ++block);
530 if (error)
531 return error;
533 word = 0;
537 * If not ending on a word boundary, deal with the last
538 * (partial) word.
540 if ((lastbit = len - i)) {
542 * Calculate mask for all the relevant bits in this word.
544 mask = ((xfs_rtword_t)1 << lastbit) - 1;
546 * Compute difference between actual and desired value.
548 incore = xfs_rtbitmap_getword(args, word);
549 if ((wdiff = (incore ^ want) & mask)) {
551 * Different, mark where we are and return.
553 i += xfs_lowbit32(wdiff);
554 *rtx = start + i - 1;
555 return 0;
556 } else
557 i = len;
560 * No match, return that we scanned the whole area.
562 *rtx = start + i - 1;
563 return 0;
566 /* Log rtsummary counter at @infoword. */
567 static inline void
568 xfs_trans_log_rtsummary(
569 struct xfs_rtalloc_args *args,
570 unsigned int infoword)
572 struct xfs_buf *bp = args->sumbp;
573 size_t first, last;
575 first = (void *)xfs_rsumblock_infoptr(args, infoword) - bp->b_addr;
576 last = first + sizeof(xfs_suminfo_t) - 1;
578 xfs_trans_log_buf(args->tp, bp, first, last);
582 * Modify the summary information for a given extent size, bitmap block
583 * combination.
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 */
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;
599 error = xfs_rtsummary_read_buf(args, xfs_rtsumoffs_to_block(mp, so));
600 if (error)
601 return error;
603 infoword = xfs_rtsumoffs_to_infoword(mp, so);
604 val = xfs_suminfo_add(args, infoword, delta);
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;
613 xfs_trans_log_rtsummary(args, infoword);
614 return 0;
618 * Read and return the summary information for a given extent size, bitmap block
619 * combination.
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 */
628 struct xfs_mount *mp = args->mp;
629 xfs_rtsumoff_t so = xfs_rtsumoffs(mp, log, bbno);
630 int error;
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;
638 /* Log rtbitmap block from the word @from to the byte before @next. */
639 static inline void
640 xfs_trans_log_rtbitmap(
641 struct xfs_rtalloc_args *args,
642 unsigned int from,
643 unsigned int next)
645 struct xfs_buf *bp = args->rbmbp;
646 size_t first, last;
648 first = (void *)xfs_rbmblock_wordptr(args, from) - bp->b_addr;
649 last = ((void *)xfs_rbmblock_wordptr(args, next) - 1) - bp->b_addr;
651 xfs_trans_log_buf(args->tp, bp, first, last);
655 * Set the given range of bitmap bits to the given value.
656 * Do whatever I/O and logging is required.
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 */
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 */
677 * Compute starting bitmap block number.
679 block = xfs_rtx_to_rbmblock(mp, start);
681 * Read the bitmap block, and point to its data.
683 error = xfs_rtbitmap_read_buf(args, block);
684 if (error)
685 return error;
688 * Compute the starting word's address, and starting bit.
690 firstword = word = xfs_rtx_to_rbmword(mp, start);
691 bit = (int)(start & (XFS_NBWORD - 1));
693 * 0 (allocated) => all zeroes; 1 (free) => all ones.
695 val = -val;
697 * If not starting on a word boundary, deal with the first
698 * (partial) word.
700 if (bit) {
702 * Compute first bit not changed and mask of relevant bits.
704 lastbit = min(bit + len, XFS_NBWORD);
705 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
707 * Set/clear the active bits.
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;
717 * Go on to the next block if that's where the next word is
718 * and we need the next word.
720 if (++word == mp->m_blockwsize && i < len) {
722 * Log the changed part of this block.
723 * Get the next one.
725 xfs_trans_log_rtbitmap(args, firstword, word);
726 error = xfs_rtbitmap_read_buf(args, ++block);
727 if (error)
728 return error;
730 firstword = word = 0;
732 } else {
734 * Starting on a word boundary, no partial word.
736 i = 0;
739 * Loop over whole words in buffers. When we use up one buffer
740 * we move on to the next one.
742 while (len - i >= XFS_NBWORD) {
744 * Set the word value correctly.
746 xfs_rtbitmap_setword(args, word, val);
747 i += XFS_NBWORD;
749 * Go on to the next block if that's where the next word is
750 * and we need the next word.
752 if (++word == mp->m_blockwsize && i < len) {
754 * Log the changed part of this block.
755 * Get the next one.
757 xfs_trans_log_rtbitmap(args, firstword, word);
758 error = xfs_rtbitmap_read_buf(args, ++block);
759 if (error)
760 return error;
762 firstword = word = 0;
766 * If not ending on a word boundary, deal with the last
767 * (partial) word.
769 if ((lastbit = len - i)) {
771 * Compute a mask of relevant bits.
773 mask = ((xfs_rtword_t)1 << lastbit) - 1;
775 * Set/clear the active bits.
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++;
786 * Log any remaining changed bytes.
788 if (word > firstword)
789 xfs_trans_log_rtbitmap(args, firstword, word);
790 return 0;
794 * Mark an extent specified by start and len freed.
795 * Updates all the summary information as well as the bitmap.
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 */
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 */
809 end = start + len - 1;
811 * Modify the bitmap to mark this extent freed.
813 error = xfs_rtmodify_range(args, start, len, 1);
814 if (error) {
815 return error;
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.
822 error = xfs_rtfind_back(args, start, &preblock);
823 if (error) {
824 return error;
827 * Find the next allocated block (end of allocated extent).
829 error = xfs_rtfind_forw(args, end, args->rtg->rtg_extents - 1,
830 &postblock);
831 if (error)
832 return error;
834 * If there are blocks not being freed at the front of the
835 * old extent, add summary data for them to be allocated.
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;
846 * If there are blocks not being freed at the end of the
847 * old extent, add summary data for them to be allocated.
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;
858 * Increment the summary information corresponding to the entire
859 * (new) free extent.
861 return xfs_rtmodify_summary(args,
862 xfs_highbit64(postblock + 1 - preblock),
863 xfs_rtx_to_rbmblock(mp, preblock), 1);
867 * Check that the given range is either all allocated (val = 0) or
868 * all free (val = 1).
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 */
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 */
891 * Compute starting bitmap block number
893 block = xfs_rtx_to_rbmblock(mp, start);
895 * Read the bitmap block.
897 error = xfs_rtbitmap_read_buf(args, block);
898 if (error)
899 return error;
902 * Compute the starting word's address, and starting bit.
904 word = xfs_rtx_to_rbmword(mp, start);
905 bit = (int)(start & (XFS_NBWORD - 1));
907 * 0 (allocated) => all zero's; 1 (free) => all one's.
909 val = -val;
911 * If not starting on a word boundary, deal with the first
912 * (partial) word.
914 if (bit) {
916 * Compute first bit not examined.
918 lastbit = min(bit + len, XFS_NBWORD);
920 * Mask of relevant bits.
922 mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
924 * Compute difference between actual and desired value.
926 incore = xfs_rtbitmap_getword(args, word);
927 if ((wdiff = (incore ^ val) & mask)) {
929 * Different, compute first wrong bit and return.
931 i = xfs_lowbit32(wdiff) - bit;
932 *new = start + i;
933 *stat = 0;
934 return 0;
936 i = lastbit - bit;
938 * Go on to next block if that's where the next word is
939 * and we need the next word.
941 if (++word == mp->m_blockwsize && i < len) {
943 * If done with this block, get the next one.
945 error = xfs_rtbitmap_read_buf(args, ++block);
946 if (error)
947 return error;
949 word = 0;
951 } else {
953 * Starting on a word boundary, no partial word.
955 i = 0;
958 * Loop over whole words in buffers. When we use up one buffer
959 * we move on to the next one.
961 while (len - i >= XFS_NBWORD) {
963 * Compute difference between actual and desired value.
965 incore = xfs_rtbitmap_getword(args, word);
966 if ((wdiff = incore ^ val)) {
968 * Different, compute first wrong bit and return.
970 i += xfs_lowbit32(wdiff);
971 *new = start + i;
972 *stat = 0;
973 return 0;
975 i += XFS_NBWORD;
977 * Go on to next block if that's where the next word is
978 * and we need the next word.
980 if (++word == mp->m_blockwsize && i < len) {
982 * If done with this block, get the next one.
984 error = xfs_rtbitmap_read_buf(args, ++block);
985 if (error)
986 return error;
988 word = 0;
992 * If not ending on a word boundary, deal with the last
993 * (partial) word.
995 if ((lastbit = len - i)) {
997 * Mask of relevant bits.
999 mask = ((xfs_rtword_t)1 << lastbit) - 1;
1001 * Compute difference between actual and desired value.
1003 incore = xfs_rtbitmap_getword(args, word);
1004 if ((wdiff = (incore ^ val) & mask)) {
1006 * Different, compute first wrong bit and return.
1008 i += xfs_lowbit32(wdiff);
1009 *new = start + i;
1010 *stat = 0;
1011 return 0;
1012 } else
1013 i = len;
1016 * Successful, return.
1018 *new = start + i;
1019 *stat = 1;
1020 return 0;
1023 #ifdef DEBUG
1025 * Check that the given extent (block range) is allocated already.
1027 STATIC int
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 */
1033 xfs_rtxnum_t new; /* dummy for xfs_rtcheck_range */
1034 int stat;
1035 int error;
1037 error = xfs_rtcheck_range(args, start, len, 0, &new, &stat);
1038 if (error)
1039 return error;
1040 ASSERT(stat);
1041 return 0;
1043 #else
1044 #define xfs_rtcheck_alloc_range(a,b,l) (0)
1045 #endif
1047 * Free an extent in the realtime subvolume. Length is expressed in
1048 * realtime extents, as is the block number.
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 */
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,
1064 int error;
1065 struct timespec64 atime;
1067 ASSERT(rbmip->i_itemp != NULL);
1068 xfs_assert_ilocked(rbmip, XFS_ILOCK_EXCL);
1070 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FREE_EXTENT))
1071 return -EIO;
1073 error = xfs_rtcheck_alloc_range(&args, start, len);
1074 if (error)
1075 return error;
1078 * Free the range of realtime blocks.
1080 error = xfs_rtfree_range(&args, start, len);
1081 if (error)
1082 goto out;
1085 * Mark more blocks free in the superblock.
1087 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
1090 * If we've now freed all the blocks, reset the file sequence
1091 * number to 0 for pre-RTG file systems.
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;
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);
1104 error = 0;
1105 out:
1106 xfs_rtbuf_cache_relse(&args);
1107 return error;
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.
1116 xfs_rtfree_blocks(
1117 struct xfs_trans *tp,
1118 struct xfs_rtgroup *rtg,
1119 xfs_fsblock_t rtbno,
1120 xfs_filblks_t rtlen)
1122 struct xfs_mount *mp = tp->t_mountp;
1123 xfs_extlen_t mod;
1124 int error;
1126 ASSERT(rtlen <= XFS_MAX_BMBT_EXTLEN);
1128 mod = xfs_blen_to_rtxoff(mp, rtlen);
1129 if (mod) {
1130 ASSERT(mod == 0);
1131 return -EIO;
1134 mod = xfs_rtb_to_rtxoff(mp, rtbno);
1135 if (mod) {
1136 ASSERT(mod == 0);
1137 return -EIO;
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;
1145 if (xfs_has_rtgroups(mp))
1146 xfs_extent_busy_insert(tp, rtg_group(rtg),
1147 xfs_rtb_to_rgbno(mp, rtbno), rtlen, 0);
1149 return 0;
1152 /* Find all the free records within a given range. */
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)
1162 struct xfs_mount *mp = rtg_mount(rtg);
1163 struct xfs_rtalloc_args args = {
1164 .rtg = rtg,
1165 .mp = mp,
1166 .tp = tp,
1168 int error = 0;
1170 if (start > end)
1171 return -EINVAL;
1172 if (start == end || start >= rtg->rtg_extents)
1173 return 0;
1175 end = min(end, rtg->rtg_extents - 1);
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;
1183 /* Is the first block free? */
1184 error = xfs_rtcheck_range(&args, start, 1, 1, &rtend,
1185 &is_free);
1186 if (error)
1187 break;
1189 /* How long does the extent go for? */
1190 error = xfs_rtfind_forw(&args, start, end, &rtend);
1191 if (error)
1192 break;
1194 if (is_free) {
1195 rec.ar_startext = start;
1196 rec.ar_extcount = rtend - start + 1;
1198 error = fn(rtg, tp, &rec, priv);
1199 if (error)
1200 break;
1203 start = rtend + 1;
1206 xfs_rtbuf_cache_relse(&args);
1207 return error;
1210 /* Find all the free records. */
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)
1218 return xfs_rtalloc_query_range(rtg, tp, 0, rtg->rtg_extents - 1, fn,
1219 priv);
1222 /* Is the given extent all 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)
1231 struct xfs_rtalloc_args args = {
1232 .mp = rtg_mount(rtg),
1233 .rtg = rtg,
1234 .tp = tp,
1236 xfs_rtxnum_t end;
1237 int matches;
1238 int error;
1240 error = xfs_rtcheck_range(&args, start, len, 1, &end, &matches);
1241 xfs_rtbuf_cache_relse(&args);
1242 if (error)
1243 return error;
1245 *is_free = matches;
1246 return 0;
1249 /* Compute the number of rt extents tracked by a single bitmap block. */
1250 xfs_rtxnum_t
1251 xfs_rtbitmap_rtx_per_rbmblock(
1252 struct xfs_mount *mp)
1254 unsigned int rbmblock_bytes = mp->m_sb.sb_blocksize;
1256 if (xfs_has_rtgroups(mp))
1257 rbmblock_bytes -= sizeof(struct xfs_rtbuf_blkinfo);
1259 return rbmblock_bytes * NBBY;
1263 * Compute the number of rtbitmap blocks needed to track the given number of rt
1264 * extents.
1266 xfs_filblks_t
1267 xfs_rtbitmap_blockcount_len(
1268 struct xfs_mount *mp,
1269 xfs_rtbxlen_t rtextents)
1271 return howmany_64(rtextents, xfs_rtbitmap_rtx_per_rbmblock(mp));
1274 /* How many rt extents does each rtbitmap file track? */
1275 static inline xfs_rtbxlen_t
1276 xfs_rtbitmap_bitcount(
1277 struct xfs_mount *mp)
1279 if (!mp->m_sb.sb_rextents)
1280 return 0;
1282 /* rtgroup size can be nonzero even if rextents is zero */
1283 if (xfs_has_rtgroups(mp))
1284 return mp->m_sb.sb_rgextents;
1286 return mp->m_sb.sb_rextents;
1290 * Compute the number of rtbitmap blocks used for a given file system.
1292 xfs_filblks_t
1293 xfs_rtbitmap_blockcount(
1294 struct xfs_mount *mp)
1296 return xfs_rtbitmap_blockcount_len(mp, xfs_rtbitmap_bitcount(mp));
1300 * Compute the geometry of the rtsummary file needed to track the given rt
1301 * space.
1303 xfs_filblks_t
1304 xfs_rtsummary_blockcount(
1305 struct xfs_mount *mp,
1306 unsigned int *rsumlevels)
1308 xfs_rtbxlen_t rextents = xfs_rtbitmap_bitcount(mp);
1309 unsigned long long rsumwords;
1311 *rsumlevels = xfs_compute_rextslog(rextents) + 1;
1312 rsumwords = xfs_rtbitmap_blockcount_len(mp, rextents) * (*rsumlevels);
1313 return howmany_64(rsumwords, mp->m_blockwsize);
1316 static int
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)
1323 struct xfs_mount *mp = ip->i_mount;
1324 struct xfs_trans *tp;
1325 int nmap = 1;
1326 int error;
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;
1333 xfs_ilock(ip, XFS_ILOCK_EXCL);
1334 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
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;
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;
1346 return xfs_trans_commit(tp);
1348 out_trans_cancel:
1349 xfs_trans_cancel(tp);
1350 return error;
1353 /* Get a buffer for the block. */
1354 static int
1355 xfs_rtfile_initialize_block(
1356 struct xfs_rtgroup *rtg,
1357 enum xfs_rtg_inodes type,
1358 xfs_fsblock_t fsbno,
1359 void *data)
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;
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;
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);
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;
1389 bufdata = bp->b_addr;
1391 xfs_trans_buf_set_type(tp, bp, buf_type);
1392 bp->b_ops = xfs_rtblock_ops(mp, type);
1394 if (xfs_has_rtgroups(mp)) {
1395 struct xfs_rtbuf_blkinfo *hdr = bp->b_addr;
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);
1406 bufdata += sizeof(*hdr);
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);
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.
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 */
1430 struct xfs_mount *mp = rtg_mount(rtg);
1431 const size_t copylen = mp->m_blockwsize << XFS_WORDLOG;
1433 while (offset_fsb < end_fsb) {
1434 struct xfs_bmbt_irec map;
1435 xfs_filblks_t i;
1436 int error;
1438 error = xfs_rtfile_alloc_blocks(rtg->rtg_inodes[type],
1439 offset_fsb, end_fsb - offset_fsb, &map);
1440 if (error)
1441 return error;
1444 * Now we need to clear the allocated blocks.
1446 * Do this one block per transaction, to keep it simple.
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;
1457 offset_fsb = map.br_startoff + map.br_blockcount;
1460 return 0;
1464 xfs_rtbitmap_create(
1465 struct xfs_rtgroup *rtg,
1466 struct xfs_inode *ip,
1467 struct xfs_trans *tp,
1468 bool init)
1470 struct xfs_mount *mp = rtg_mount(rtg);
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);
1477 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1478 return 0;
1482 xfs_rtsummary_create(
1483 struct xfs_rtgroup *rtg,
1484 struct xfs_inode *ip,
1485 struct xfs_trans *tp,
1486 bool init)
1488 struct xfs_mount *mp = rtg_mount(rtg);
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;