summaryrefslogtreecommitdiff
path: root/fs/xfs/scrub/rtsummary.c
blob: bc5dc184f6948f0c2eed886664b2c7378f1f4fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2017-2021 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <djwong@kernel.org>
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "xfs_format.h"
#include "xfs_trans_resv.h"
#include "xfs_mount.h"
#include "xfs_btree.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
#include "xfs_rtalloc.h"
#include "xfs_inode.h"
#include "xfs_bit.h"
#include "xfs_bmap.h"
#include "scrub/scrub.h"
#include "scrub/common.h"
#include "scrub/trace.h"
#include "scrub/xfile.h"

/*
 * Realtime Summary
 * ================
 *
 * We check the realtime summary by scanning the realtime bitmap file to create
 * a new summary file incore, and then we compare the computed version against
 * the ondisk version.  We use the 'xfile' functionality to store this
 * (potentially large) amount of data in pageable memory.
 */

struct xchk_rtsum_compute {
	/* How far have we iterated through the rt extents? */
	xfs_rtblock_t		rt_extent_nr;

	/* How many free rt extents have we seen? */
	xfs_rtblock_t		rt_free_nr;

	/* block and bit offset of our current position in the rtbitmap. */
	xfs_fileoff_t		off;
	unsigned int		bit;

	/* block and bit offset of the start of the most recent free rtext. */
	xfs_fileoff_t		start_off;
	unsigned int		start_bit;

	/* Are we accumulating a free rtext? */
	bool			in_extent;
};

/* Set us up to check the rtsummary file. */
int
xchk_setup_rtsummary(
	struct xfs_scrub	*sc)
{
	struct xfs_mount	*mp = sc->mp;
	int			error;

	/*
	 * Create an xfile to construct a new rtsummary file.  The xfile allows
	 * us to avoid pinning kernel memory for this purpose.
	 */
	sc->xfile = xfile_create("rtsummary", mp->m_rsumsize);
	if (IS_ERR(sc->xfile))
		return PTR_ERR(sc->xfile);

	error = xchk_trans_alloc(sc, 0);
	if (error)
		return error;

	/* Allocate a memory buffer for the summary comparison. */
	sc->buf = kmem_alloc_large(sc->mp->m_sb.sb_blocksize, KM_MAYFAIL);
	if (!sc->buf)
		return -ENOMEM;

	error = xchk_install_inode(sc, sc->mp->m_rsumip);
	if (error)
		return error;

	/*
	 * Locking order requires us to take the rtbitmap first.  We must be
	 * careful to unlock it ourselves when we are done with the rtbitmap
	 * file since the scrub infrastructure won't do that for us...
	 */
	xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);

	/* ...and then we can lock the rtsummary inode. */
	sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
	xfs_ilock(sc->ip, sc->ilock_flags);
	return 0;
}

/* Update the summary file to reflect the free extent that we've accumulated. */
STATIC int
xchk_rtsum_record_free(
	struct xfs_scrub	*sc,
	struct xchk_rtsum_compute *state)
{
	struct xfs_mount	*mp = sc->mp;
	uint64_t		free_start;
	uint64_t		alloc_start;
	unsigned int		offs;
	unsigned int		lenlog;
	unsigned int		bitsperblock = mp->m_sb.sb_blocksize * NBBY;
	xfs_suminfo_t		v = 0;
	int			error;

	/* Compute the relevant location in the rtsum file. */
	alloc_start = state->off * bitsperblock + state->bit;
	free_start = state->start_off * bitsperblock + state->start_bit;
	lenlog = XFS_RTBLOCKLOG(alloc_start - free_start);
	offs = XFS_SUMOFFS(mp, lenlog, state->start_off);

	if (!xfs_verify_rtext(mp, free_start, alloc_start - free_start)) {
		xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
		return -EFSCORRUPTED;
	}

	/* Read current rtsummary contents. */
	error = xfile_pread(sc->xfile, &v, sizeof(xfs_suminfo_t),
			sizeof(xfs_suminfo_t) * offs);
	if (error)
		return error;

	/* Bump the summary count... */
	v++;
	trace_xchk_rtsum_record_free(mp, free_start, alloc_start - 1,
			alloc_start - free_start, lenlog, offs, v);

	/* ...and write it back. */
	error = xfile_pwrite(sc->xfile, &v, sizeof(xfs_suminfo_t),
			sizeof(xfs_suminfo_t) * offs);
	if (error)
		return error;

	state->in_extent = false;
	return 0;
}

static inline bool
xchk_rtsum_isset(
	xfs_rtword_t	*words,
	unsigned int	bit)
{
	return words[bit / (sizeof(*words) * NBBY)] &
			(1ULL << (bit % (sizeof(*words) * NBBY)));
}

/* Walk a single rtbitmap block looking for changes in the free status. */
STATIC int
xchk_rtsum_process_bmblock(
	struct xfs_scrub	*sc,
	xfs_fileoff_t		block_off,
	struct xchk_rtsum_compute *state)
{
	struct xfs_mount	*mp = sc->mp;
	struct xfs_buf		*bp;
	xfs_rtword_t		*words;
	unsigned int		bitsperblock = mp->m_sb.sb_blocksize * NBBY;
	int			error = 0;

	if (xchk_should_terminate(sc, &error))
		return error;

	error = xfs_rtbuf_get(mp, sc->tp, block_off, 0, &bp);
	if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, block_off,
				&error))
		return error;

	state->off = block_off;
	words = (xfs_rtword_t *)bp->b_addr;
	for (state->bit = 0;
	     state->bit < bitsperblock &&
	     state->rt_extent_nr < mp->m_sb.sb_rextents;
	     state->bit++, state->rt_extent_nr++) {
		if (xchk_rtsum_isset(words, state->bit)) {
			state->rt_free_nr++;
			if (!state->in_extent) {
				state->start_off = block_off;
				state->start_bit = state->bit;
				state->in_extent = true;
			}
		} else if (state->in_extent) {
			error = xchk_rtsum_record_free(sc, state);
			if (error)
				goto out_relse;
		}
	}

out_relse:
	xfs_trans_brelse(sc->tp, bp);
	return error;
}

/*
 * Compute the realtime summary from the realtime bitmap.  This is a kernel
 * port of the defunct process_rtbitmap function in xfs_repair.
 */
STATIC int
xchk_rtsum_compute(
	struct xfs_scrub	*sc)
{
	struct xchk_rtsum_compute state = { 0 };
	struct xfs_mount	*mp = sc->mp;
	unsigned long long	rtbmp_bytes;
	xfs_fileoff_t		off = 0;
	xfs_fileoff_t		end_off;
	int			error;

	rtbmp_bytes = howmany_64(mp->m_sb.sb_rextents, NBBY);
	end_off = howmany_64(rtbmp_bytes, mp->m_sb.sb_blocksize);

	/* If the bitmap size doesn't match the computed size, bail. */
	if (roundup_64(rtbmp_bytes, mp->m_sb.sb_blocksize) !=
			mp->m_rbmip->i_d.di_size)
		return -EFSCORRUPTED;

	for (off = 0; off < end_off; off++) {
		error = xchk_rtsum_process_bmblock(sc, off, &state);
		if (error)
			return error;
		if (state.rt_extent_nr == mp->m_sb.sb_rextents)
			break;
	}
	if (state.in_extent) {
		error = xchk_rtsum_record_free(sc, &state);
		if (error)
			return error;
	}

	return 0;
}

/* Compare the rtsummary file against the one we computed. */
STATIC int
xchk_rtsum_compare(
	struct xfs_scrub	*sc)
{
	struct xfs_mount	*mp = sc->mp;
	struct xfs_buf		*bp;
	struct xfs_bmbt_irec	map;
	xfs_rtblock_t		off;
	loff_t			pos;
	int			nmap;
	int			error = 0;

	for (off = 0, pos = 0;
	     pos < mp->m_rsumsize;
	     pos += mp->m_sb.sb_blocksize, off++) {
		size_t		count;

		if (xchk_should_terminate(sc, &error) ||
		    (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
			break;

		/* Make sure we have a written extent. */
		nmap = 1;
		error = xfs_bmapi_read(mp->m_rsumip, off, 1, &map, &nmap,
				XFS_DATA_FORK);
		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
			break;

		if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
			break;
		}

		/* Read a block's worth of ondisk rtsummary file. */
		error = xfs_rtbuf_get(mp, sc->tp, off, 1, &bp);
		if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
			break;

		/* Read a block's worth of computed rtsummary file. */
		count = min_t(loff_t, mp->m_rsumsize - pos,
				mp->m_sb.sb_blocksize);
		error = xfile_pread(sc->xfile, sc->buf, count, pos);
		if (error) {
			xfs_trans_brelse(sc->tp, bp);
			break;
		}

		if (memcmp(bp->b_addr, sc->buf, count) != 0)
			xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);

		xfs_trans_brelse(sc->tp, bp);
	}

	return error;
}

/* Scrub the realtime summary. */
int
xchk_rtsummary(
	struct xfs_scrub	*sc)
{
	struct xfs_mount	*mp = sc->mp;
	int			error = 0;

	/* Invoke the fork scrubber. */
	error = xchk_metadata_inode_forks(sc);
	if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
		goto out_rbm;

	/* Construct the new summary file from the rtbitmap. */
	error = xchk_rtsum_compute(sc);
	if (error == -EFSCORRUPTED) {
		/*
		 * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
		 * error since we're checking the summary file.
		 */
		xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
		error = 0;
		goto out_rbm;
	}
	if (error)
		goto out_rbm;

	/* Does the computed summary file match the actual rtsummary file? */
	error = xchk_rtsum_compare(sc);

out_rbm:
	/* Unlock the rtbitmap since we're done with it. */
	xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
	return error;
}