xref: /openbmc/linux/fs/xfs/scrub/rtbitmap.c (revision ea01221f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
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"
16 #include "xfs_bmap.h"
17 #include "xfs_sb.h"
18 #include "scrub/scrub.h"
19 #include "scrub/common.h"
20 
21 /* Set us up with the realtime metadata locked. */
22 int
xchk_setup_rtbitmap(struct xfs_scrub * sc)23 xchk_setup_rtbitmap(
24 	struct xfs_scrub	*sc)
25 {
26 	int			error;
27 
28 	error = xchk_trans_alloc(sc, 0);
29 	if (error)
30 		return error;
31 
32 	error = xchk_install_live_inode(sc, sc->mp->m_rbmip);
33 	if (error)
34 		return error;
35 
36 	xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
37 	return 0;
38 }
39 
40 /* Realtime bitmap. */
41 
42 /* Scrub a free extent record from the realtime bitmap. */
43 STATIC int
xchk_rtbitmap_rec(struct xfs_mount * mp,struct xfs_trans * tp,const struct xfs_rtalloc_rec * rec,void * priv)44 xchk_rtbitmap_rec(
45 	struct xfs_mount	*mp,
46 	struct xfs_trans	*tp,
47 	const struct xfs_rtalloc_rec *rec,
48 	void			*priv)
49 {
50 	struct xfs_scrub	*sc = priv;
51 	xfs_rtblock_t		startblock;
52 	xfs_rtblock_t		blockcount;
53 
54 	startblock = rec->ar_startext * mp->m_sb.sb_rextsize;
55 	blockcount = rec->ar_extcount * mp->m_sb.sb_rextsize;
56 
57 	if (!xfs_verify_rtext(mp, startblock, blockcount))
58 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
59 	return 0;
60 }
61 
62 /* Make sure the entire rtbitmap file is mapped with written extents. */
63 STATIC int
xchk_rtbitmap_check_extents(struct xfs_scrub * sc)64 xchk_rtbitmap_check_extents(
65 	struct xfs_scrub	*sc)
66 {
67 	struct xfs_mount	*mp = sc->mp;
68 	struct xfs_bmbt_irec	map;
69 	xfs_rtblock_t		off;
70 	int			nmap;
71 	int			error = 0;
72 
73 	for (off = 0; off < mp->m_sb.sb_rbmblocks;) {
74 		if (xchk_should_terminate(sc, &error) ||
75 		    (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
76 			break;
77 
78 		/* Make sure we have a written extent. */
79 		nmap = 1;
80 		error = xfs_bmapi_read(mp->m_rbmip, off,
81 				mp->m_sb.sb_rbmblocks - off, &map, &nmap,
82 				XFS_DATA_FORK);
83 		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
84 			break;
85 
86 		if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
87 			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
88 			break;
89 		}
90 
91 		off += map.br_blockcount;
92 	}
93 
94 	return error;
95 }
96 
97 /* Scrub the realtime bitmap. */
98 int
xchk_rtbitmap(struct xfs_scrub * sc)99 xchk_rtbitmap(
100 	struct xfs_scrub	*sc)
101 {
102 	int			error;
103 
104 	/* Is the size of the rtbitmap correct? */
105 	if (sc->mp->m_rbmip->i_disk_size !=
106 	    XFS_FSB_TO_B(sc->mp, sc->mp->m_sb.sb_rbmblocks)) {
107 		xchk_ino_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
108 		return 0;
109 	}
110 
111 	/* Invoke the fork scrubber. */
112 	error = xchk_metadata_inode_forks(sc);
113 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
114 		return error;
115 
116 	error = xchk_rtbitmap_check_extents(sc);
117 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
118 		return error;
119 
120 	error = xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtbitmap_rec, sc);
121 	if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
122 		goto out;
123 
124 out:
125 	return error;
126 }
127 
128 /* xref check that the extent is not free in the rtbitmap */
129 void
xchk_xref_is_used_rt_space(struct xfs_scrub * sc,xfs_rtblock_t fsbno,xfs_extlen_t len)130 xchk_xref_is_used_rt_space(
131 	struct xfs_scrub	*sc,
132 	xfs_rtblock_t		fsbno,
133 	xfs_extlen_t		len)
134 {
135 	xfs_rtblock_t		startext;
136 	xfs_rtblock_t		endext;
137 	xfs_rtblock_t		extcount;
138 	bool			is_free;
139 	int			error;
140 
141 	if (xchk_skip_xref(sc->sm))
142 		return;
143 
144 	startext = fsbno;
145 	endext = fsbno + len - 1;
146 	do_div(startext, sc->mp->m_sb.sb_rextsize);
147 	do_div(endext, sc->mp->m_sb.sb_rextsize);
148 	extcount = endext - startext + 1;
149 	xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
150 	error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
151 			&is_free);
152 	if (!xchk_should_check_xref(sc, &error, NULL))
153 		goto out_unlock;
154 	if (is_free)
155 		xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
156 out_unlock:
157 	xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
158 }
159