xref: /openbmc/linux/fs/xfs/scrub/rtbitmap.c (revision 3a83e4e6)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
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_rtalloc.h"
15 #include "xfs_inode.h"
16 #include "xfs_bmap.h"
17 #include "scrub/scrub.h"
18 #include "scrub/common.h"
19 
20 /* Set us up with the realtime metadata locked. */
21 int
22 xchk_setup_rt(
23 	struct xfs_scrub	*sc,
24 	struct xfs_inode	*ip)
25 {
26 	int			error;
27 
28 	error = xchk_setup_fs(sc, ip);
29 	if (error)
30 		return error;
31 
32 	sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
33 	sc->ip = sc->mp->m_rbmip;
34 	xfs_ilock(sc->ip, sc->ilock_flags);
35 
36 	return 0;
37 }
38 
39 /* Realtime bitmap. */
40 
41 /* Scrub a free extent record from the realtime bitmap. */
42 STATIC int
43 xchk_rtbitmap_rec(
44 	struct xfs_trans	*tp,
45 	struct xfs_rtalloc_rec	*rec,
46 	void			*priv)
47 {
48 	struct xfs_scrub	*sc = priv;
49 	xfs_rtblock_t		startblock;
50 	xfs_rtblock_t		blockcount;
51 
52 	startblock = rec->ar_startext * tp->t_mountp->m_sb.sb_rextsize;
53 	blockcount = rec->ar_extcount * tp->t_mountp->m_sb.sb_rextsize;
54 
55 	if (startblock + blockcount <= startblock ||
56 	    !xfs_verify_rtbno(sc->mp, startblock) ||
57 	    !xfs_verify_rtbno(sc->mp, startblock + blockcount - 1))
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
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
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_d.di_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->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 /* Scrub the realtime summary. */
129 int
130 xchk_rtsummary(
131 	struct xfs_scrub	*sc)
132 {
133 	struct xfs_inode	*rsumip = sc->mp->m_rsumip;
134 	struct xfs_inode	*old_ip = sc->ip;
135 	uint			old_ilock_flags = sc->ilock_flags;
136 	int			error = 0;
137 
138 	/*
139 	 * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
140 	 * rt summary ip in compliance with the rt inode locking rules.
141 	 *
142 	 * Since we switch sc->ip to rsumip we have to save the old ilock
143 	 * flags so that we don't mix up the inode state that @sc tracks.
144 	 */
145 	sc->ip = rsumip;
146 	sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
147 	xfs_ilock(sc->ip, sc->ilock_flags);
148 
149 	/* Invoke the fork scrubber. */
150 	error = xchk_metadata_inode_forks(sc);
151 	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
152 		goto out;
153 
154 	/* XXX: implement this some day */
155 	xchk_set_incomplete(sc);
156 out:
157 	/* Switch back to the rtbitmap inode and lock flags. */
158 	xfs_iunlock(sc->ip, sc->ilock_flags);
159 	sc->ilock_flags = old_ilock_flags;
160 	sc->ip = old_ip;
161 	return error;
162 }
163 
164 
165 /* xref check that the extent is not free in the rtbitmap */
166 void
167 xchk_xref_is_used_rt_space(
168 	struct xfs_scrub	*sc,
169 	xfs_rtblock_t		fsbno,
170 	xfs_extlen_t		len)
171 {
172 	xfs_rtblock_t		startext;
173 	xfs_rtblock_t		endext;
174 	xfs_rtblock_t		extcount;
175 	bool			is_free;
176 	int			error;
177 
178 	if (xchk_skip_xref(sc->sm))
179 		return;
180 
181 	startext = fsbno;
182 	endext = fsbno + len - 1;
183 	do_div(startext, sc->mp->m_sb.sb_rextsize);
184 	do_div(endext, sc->mp->m_sb.sb_rextsize);
185 	extcount = endext - startext + 1;
186 	xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
187 	error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
188 			&is_free);
189 	if (!xchk_should_check_xref(sc, &error, NULL))
190 		goto out_unlock;
191 	if (is_free)
192 		xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
193 out_unlock:
194 	xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
195 }
196