1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans.h"
14 #include "xfs_rtbitmap.h"
15 #include "xfs_inode.h"
19 #include "scrub/scrub.h"
20 #include "scrub/common.h"
21 #include "scrub/repair.h"
22 #include "scrub/rtbitmap.h"
24 /* Set us up with the realtime metadata locked. */
29 struct xfs_mount
*mp
= sc
->mp
;
30 struct xchk_rtbitmap
*rtb
;
33 rtb
= kzalloc(sizeof(struct xchk_rtbitmap
), XCHK_GFP_FLAGS
);
38 error
= xchk_rtgroup_init(sc
, sc
->sm
->sm_agno
, &sc
->sr
);
42 if (xchk_could_repair(sc
)) {
43 error
= xrep_setup_rtbitmap(sc
, rtb
);
48 error
= xchk_trans_alloc(sc
, rtb
->resblks
);
52 error
= xchk_install_live_inode(sc
,
53 sc
->sr
.rtg
->rtg_inodes
[XFS_RTGI_BITMAP
]);
57 error
= xchk_ino_dqattach(sc
);
62 * Now that we've locked the rtbitmap, we can't race with growfsrt
63 * trying to expand the bitmap or change the size of the rt volume.
64 * Hence it is safe to compute and check the geometry values.
66 xchk_rtgroup_lock(&sc
->sr
, XFS_RTGLOCK_BITMAP
);
67 if (mp
->m_sb
.sb_rblocks
) {
68 rtb
->rextents
= xfs_blen_to_rtbxlen(mp
, mp
->m_sb
.sb_rblocks
);
69 rtb
->rextslog
= xfs_compute_rextslog(rtb
->rextents
);
70 rtb
->rbmblocks
= xfs_rtbitmap_blockcount(mp
);
76 /* Realtime bitmap. */
78 /* Scrub a free extent record from the realtime bitmap. */
81 struct xfs_rtgroup
*rtg
,
83 const struct xfs_rtalloc_rec
*rec
,
86 struct xfs_scrub
*sc
= priv
;
87 xfs_rtblock_t startblock
;
88 xfs_filblks_t blockcount
;
90 startblock
= xfs_rtx_to_rtb(rtg
, rec
->ar_startext
);
91 blockcount
= xfs_rtxlen_to_extlen(rtg_mount(rtg
), rec
->ar_extcount
);
93 if (!xfs_verify_rtbext(rtg_mount(rtg
), startblock
, blockcount
))
94 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, 0);
98 /* Make sure the entire rtbitmap file is mapped with written extents. */
100 xchk_rtbitmap_check_extents(
101 struct xfs_scrub
*sc
)
103 struct xfs_bmbt_irec map
;
104 struct xfs_iext_cursor icur
;
105 struct xfs_mount
*mp
= sc
->mp
;
106 struct xfs_inode
*ip
= sc
->ip
;
107 xfs_fileoff_t off
= 0;
108 xfs_fileoff_t endoff
;
111 /* Mappings may not cross or lie beyond EOF. */
112 endoff
= XFS_B_TO_FSB(mp
, ip
->i_disk_size
);
113 if (xfs_iext_lookup_extent(ip
, &ip
->i_df
, endoff
, &icur
, &map
)) {
114 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, endoff
);
118 while (off
< endoff
) {
121 if (xchk_should_terminate(sc
, &error
) ||
122 (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
))
125 /* Make sure we have a written extent. */
126 error
= xfs_bmapi_read(ip
, off
, endoff
- off
, &map
, &nmap
,
128 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, off
, &error
))
131 if (nmap
!= 1 || !xfs_bmap_is_written_extent(&map
)) {
132 xchk_fblock_set_corrupt(sc
, XFS_DATA_FORK
, off
);
136 off
+= map
.br_blockcount
;
142 /* Scrub the realtime bitmap. */
145 struct xfs_scrub
*sc
)
147 struct xfs_mount
*mp
= sc
->mp
;
148 struct xfs_rtgroup
*rtg
= sc
->sr
.rtg
;
149 struct xfs_inode
*rbmip
= rtg
->rtg_inodes
[XFS_RTGI_BITMAP
];
150 struct xchk_rtbitmap
*rtb
= sc
->buf
;
153 /* Is sb_rextents correct? */
154 if (mp
->m_sb
.sb_rextents
!= rtb
->rextents
) {
155 xchk_ino_set_corrupt(sc
, rbmip
->i_ino
);
159 /* Is sb_rextslog correct? */
160 if (mp
->m_sb
.sb_rextslog
!= rtb
->rextslog
) {
161 xchk_ino_set_corrupt(sc
, rbmip
->i_ino
);
166 * Is sb_rbmblocks large enough to handle the current rt volume? In no
167 * case can we exceed 4bn bitmap blocks since the super field is a u32.
169 if (rtb
->rbmblocks
> U32_MAX
) {
170 xchk_ino_set_corrupt(sc
, rbmip
->i_ino
);
173 if (mp
->m_sb
.sb_rbmblocks
!= rtb
->rbmblocks
) {
174 xchk_ino_set_corrupt(sc
, rbmip
->i_ino
);
178 /* The bitmap file length must be aligned to an fsblock. */
179 if (rbmip
->i_disk_size
& mp
->m_blockmask
) {
180 xchk_ino_set_corrupt(sc
, rbmip
->i_ino
);
185 * Is the bitmap file itself large enough to handle the rt volume?
186 * growfsrt expands the bitmap file before updating sb_rextents, so the
187 * file can be larger than sb_rbmblocks.
189 if (rbmip
->i_disk_size
< XFS_FSB_TO_B(mp
, rtb
->rbmblocks
)) {
190 xchk_ino_set_corrupt(sc
, rbmip
->i_ino
);
194 /* Invoke the fork scrubber. */
195 error
= xchk_metadata_inode_forks(sc
);
196 if (error
|| (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
))
199 error
= xchk_rtbitmap_check_extents(sc
);
200 if (error
|| (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
))
203 error
= xfs_rtalloc_query_all(rtg
, sc
->tp
, xchk_rtbitmap_rec
, sc
);
204 if (!xchk_fblock_process_error(sc
, XFS_DATA_FORK
, 0, &error
))
210 /* xref check that the extent is not free in the rtbitmap */
212 xchk_xref_is_used_rt_space(
213 struct xfs_scrub
*sc
,
217 struct xfs_rtgroup
*rtg
= sc
->sr
.rtg
;
218 struct xfs_inode
*rbmip
= rtg
->rtg_inodes
[XFS_RTGI_BITMAP
];
219 xfs_rtxnum_t startext
;
224 if (xchk_skip_xref(sc
->sm
))
227 startext
= xfs_rtb_to_rtx(sc
->mp
, rtbno
);
228 endext
= xfs_rtb_to_rtx(sc
->mp
, rtbno
+ len
- 1);
229 error
= xfs_rtalloc_extent_is_free(rtg
, sc
->tp
, startext
,
230 endext
- startext
+ 1, &is_free
);
231 if (!xchk_should_check_xref(sc
, &error
, NULL
))
234 xchk_ino_xref_set_corrupt(sc
, rbmip
->i_ino
);