blob: 39165c3556cd369ad8433ea73020639f20431fa3 [file] [log] [blame]
Darrick J. Wongdcb660f2017-10-17 21:37:36 -07001/*
2 * Copyright (C) 2017 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_trans_resv.h"
25#include "xfs_mount.h"
26#include "xfs_defer.h"
27#include "xfs_btree.h"
28#include "xfs_bit.h"
29#include "xfs_log_format.h"
30#include "xfs_trans.h"
31#include "xfs_sb.h"
32#include "xfs_inode.h"
33#include "xfs_alloc.h"
34#include "xfs_alloc_btree.h"
35#include "xfs_bmap.h"
36#include "xfs_bmap_btree.h"
37#include "xfs_ialloc.h"
38#include "xfs_ialloc_btree.h"
39#include "xfs_refcount.h"
40#include "xfs_refcount_btree.h"
41#include "xfs_rmap.h"
42#include "xfs_rmap_btree.h"
Darrick J. Wong3daa6642017-10-17 21:37:40 -070043#include "xfs_log.h"
44#include "xfs_trans_priv.h"
Darrick J. Wongdcb660f2017-10-17 21:37:36 -070045#include "scrub/xfs_scrub.h"
46#include "scrub/scrub.h"
47#include "scrub/common.h"
48#include "scrub/trace.h"
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -070049#include "scrub/btree.h"
Darrick J. Wongdcb660f2017-10-17 21:37:36 -070050
51/* Common code for the metadata scrubbers. */
52
Darrick J. Wong4700d222017-10-17 21:37:36 -070053/*
54 * Handling operational errors.
55 *
56 * The *_process_error() family of functions are used to process error return
57 * codes from functions called as part of a scrub operation.
58 *
59 * If there's no error, we return true to tell the caller that it's ok
60 * to move on to the next check in its list.
61 *
62 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
63 * caller that something bad happened, and we preserve *error so that
64 * the caller can return the *error up the stack to userspace.
65 *
66 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
67 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
68 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
69 * not via return codes. We return false to tell the caller that
70 * something bad happened. Since the error has been cleared, the caller
71 * will (presumably) return that zero and scrubbing will move on to
72 * whatever's next.
73 *
74 * ftrace can be used to record the precise metadata location and the
75 * approximate code location of the failed operation.
76 */
77
78/* Check for operational errors. */
79bool
80xfs_scrub_process_error(
81 struct xfs_scrub_context *sc,
82 xfs_agnumber_t agno,
83 xfs_agblock_t bno,
84 int *error)
85{
86 switch (*error) {
87 case 0:
88 return true;
89 case -EDEADLOCK:
90 /* Used to restart an op with deadlock avoidance. */
91 trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
92 break;
93 case -EFSBADCRC:
94 case -EFSCORRUPTED:
95 /* Note the badness but don't abort. */
96 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
97 *error = 0;
98 /* fall through */
99 default:
100 trace_xfs_scrub_op_error(sc, agno, bno, *error,
101 __return_address);
102 break;
103 }
104 return false;
105}
106
107/* Check for operational errors for a file offset. */
108bool
109xfs_scrub_fblock_process_error(
110 struct xfs_scrub_context *sc,
111 int whichfork,
112 xfs_fileoff_t offset,
113 int *error)
114{
115 switch (*error) {
116 case 0:
117 return true;
118 case -EDEADLOCK:
119 /* Used to restart an op with deadlock avoidance. */
120 trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
121 break;
122 case -EFSBADCRC:
123 case -EFSCORRUPTED:
124 /* Note the badness but don't abort. */
125 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
126 *error = 0;
127 /* fall through */
128 default:
129 trace_xfs_scrub_file_op_error(sc, whichfork, offset, *error,
130 __return_address);
131 break;
132 }
133 return false;
134}
135
136/*
137 * Handling scrub corruption/optimization/warning checks.
138 *
139 * The *_set_{corrupt,preen,warning}() family of functions are used to
140 * record the presence of metadata that is incorrect (corrupt), could be
141 * optimized somehow (preen), or should be flagged for administrative
142 * review but is not incorrect (warn).
143 *
144 * ftrace can be used to record the precise metadata location and
145 * approximate code location of the failed check.
146 */
147
148/* Record a block which could be optimized. */
149void
150xfs_scrub_block_set_preen(
151 struct xfs_scrub_context *sc,
152 struct xfs_buf *bp)
153{
154 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
155 trace_xfs_scrub_block_preen(sc, bp->b_bn, __return_address);
156}
157
158/*
159 * Record an inode which could be optimized. The trace data will
160 * include the block given by bp if bp is given; otherwise it will use
161 * the block location of the inode record itself.
162 */
163void
164xfs_scrub_ino_set_preen(
165 struct xfs_scrub_context *sc,
166 struct xfs_buf *bp)
167{
168 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
169 trace_xfs_scrub_ino_preen(sc, sc->ip->i_ino, bp ? bp->b_bn : 0,
170 __return_address);
171}
172
173/* Record a corrupt block. */
174void
175xfs_scrub_block_set_corrupt(
176 struct xfs_scrub_context *sc,
177 struct xfs_buf *bp)
178{
179 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
180 trace_xfs_scrub_block_error(sc, bp->b_bn, __return_address);
181}
182
183/*
184 * Record a corrupt inode. The trace data will include the block given
185 * by bp if bp is given; otherwise it will use the block location of the
186 * inode record itself.
187 */
188void
189xfs_scrub_ino_set_corrupt(
190 struct xfs_scrub_context *sc,
191 xfs_ino_t ino,
192 struct xfs_buf *bp)
193{
194 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
195 trace_xfs_scrub_ino_error(sc, ino, bp ? bp->b_bn : 0, __return_address);
196}
197
198/* Record corruption in a block indexed by a file fork. */
199void
200xfs_scrub_fblock_set_corrupt(
201 struct xfs_scrub_context *sc,
202 int whichfork,
203 xfs_fileoff_t offset)
204{
205 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
206 trace_xfs_scrub_fblock_error(sc, whichfork, offset, __return_address);
207}
208
209/*
210 * Warn about inodes that need administrative review but is not
211 * incorrect.
212 */
213void
214xfs_scrub_ino_set_warning(
215 struct xfs_scrub_context *sc,
216 struct xfs_buf *bp)
217{
218 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
219 trace_xfs_scrub_ino_warning(sc, sc->ip->i_ino, bp ? bp->b_bn : 0,
220 __return_address);
221}
222
223/* Warn about a block indexed by a file fork that needs review. */
224void
225xfs_scrub_fblock_set_warning(
226 struct xfs_scrub_context *sc,
227 int whichfork,
228 xfs_fileoff_t offset)
229{
230 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
231 trace_xfs_scrub_fblock_warning(sc, whichfork, offset, __return_address);
232}
233
234/* Signal an incomplete scrub. */
235void
236xfs_scrub_set_incomplete(
237 struct xfs_scrub_context *sc)
238{
239 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
240 trace_xfs_scrub_incomplete(sc, __return_address);
241}
242
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700243/*
244 * AG scrubbing
245 *
246 * These helpers facilitate locking an allocation group's header
247 * buffers, setting up cursors for all btrees that are present, and
248 * cleaning everything up once we're through.
249 */
250
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700251/* Decide if we want to return an AG header read failure. */
252static inline bool
253want_ag_read_header_failure(
254 struct xfs_scrub_context *sc,
255 unsigned int type)
256{
257 /* Return all AG header read failures when scanning btrees. */
258 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
Darrick J. Wonga12890a2017-10-17 21:37:39 -0700259 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
260 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700261 return true;
262 /*
263 * If we're scanning a given type of AG header, we only want to
264 * see read failures from that specific header. We'd like the
265 * other headers to cross-check them, but this isn't required.
266 */
267 if (sc->sm->sm_type == type)
268 return true;
269 return false;
270}
271
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700272/*
273 * Grab all the headers for an AG.
274 *
275 * The headers should be released by xfs_scrub_ag_free, but as a fail
276 * safe we attach all the buffers we grab to the scrub transaction so
277 * they'll all be freed when we cancel it.
278 */
279int
280xfs_scrub_ag_read_headers(
281 struct xfs_scrub_context *sc,
282 xfs_agnumber_t agno,
283 struct xfs_buf **agi,
284 struct xfs_buf **agf,
285 struct xfs_buf **agfl)
286{
287 struct xfs_mount *mp = sc->mp;
288 int error;
289
290 error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
Darrick J. Wonga12890a2017-10-17 21:37:39 -0700291 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700292 goto out;
293
294 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700295 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700296 goto out;
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700297
298 error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700299 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700300 goto out;
301
302out:
303 return error;
304}
305
306/* Release all the AG btree cursors. */
307void
308xfs_scrub_ag_btcur_free(
309 struct xfs_scrub_ag *sa)
310{
311 if (sa->refc_cur)
312 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
313 if (sa->rmap_cur)
314 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
315 if (sa->fino_cur)
316 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
317 if (sa->ino_cur)
318 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
319 if (sa->cnt_cur)
320 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
321 if (sa->bno_cur)
322 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
323
324 sa->refc_cur = NULL;
325 sa->rmap_cur = NULL;
326 sa->fino_cur = NULL;
327 sa->ino_cur = NULL;
328 sa->bno_cur = NULL;
329 sa->cnt_cur = NULL;
330}
331
332/* Initialize all the btree cursors for an AG. */
333int
334xfs_scrub_ag_btcur_init(
335 struct xfs_scrub_context *sc,
336 struct xfs_scrub_ag *sa)
337{
338 struct xfs_mount *mp = sc->mp;
339 xfs_agnumber_t agno = sa->agno;
340
341 if (sa->agf_bp) {
342 /* Set up a bnobt cursor for cross-referencing. */
343 sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
344 agno, XFS_BTNUM_BNO);
345 if (!sa->bno_cur)
346 goto err;
347
348 /* Set up a cntbt cursor for cross-referencing. */
349 sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
350 agno, XFS_BTNUM_CNT);
351 if (!sa->cnt_cur)
352 goto err;
353 }
354
355 /* Set up a inobt cursor for cross-referencing. */
356 if (sa->agi_bp) {
357 sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
358 agno, XFS_BTNUM_INO);
359 if (!sa->ino_cur)
360 goto err;
361 }
362
363 /* Set up a finobt cursor for cross-referencing. */
364 if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb)) {
365 sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
366 agno, XFS_BTNUM_FINO);
367 if (!sa->fino_cur)
368 goto err;
369 }
370
371 /* Set up a rmapbt cursor for cross-referencing. */
372 if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb)) {
373 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
374 agno);
375 if (!sa->rmap_cur)
376 goto err;
377 }
378
379 /* Set up a refcountbt cursor for cross-referencing. */
380 if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb)) {
381 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
382 sa->agf_bp, agno, NULL);
383 if (!sa->refc_cur)
384 goto err;
385 }
386
387 return 0;
388err:
389 return -ENOMEM;
390}
391
392/* Release the AG header context and btree cursors. */
393void
394xfs_scrub_ag_free(
395 struct xfs_scrub_context *sc,
396 struct xfs_scrub_ag *sa)
397{
398 xfs_scrub_ag_btcur_free(sa);
399 if (sa->agfl_bp) {
400 xfs_trans_brelse(sc->tp, sa->agfl_bp);
401 sa->agfl_bp = NULL;
402 }
403 if (sa->agf_bp) {
404 xfs_trans_brelse(sc->tp, sa->agf_bp);
405 sa->agf_bp = NULL;
406 }
407 if (sa->agi_bp) {
408 xfs_trans_brelse(sc->tp, sa->agi_bp);
409 sa->agi_bp = NULL;
410 }
411 sa->agno = NULLAGNUMBER;
412}
413
414/*
415 * For scrub, grab the AGI and the AGF headers, in that order. Locking
416 * order requires us to get the AGI before the AGF. We use the
417 * transaction to avoid deadlocking on crosslinked metadata buffers;
418 * either the caller passes one in (bmap scrub) or we have to create a
419 * transaction ourselves.
420 */
421int
422xfs_scrub_ag_init(
423 struct xfs_scrub_context *sc,
424 xfs_agnumber_t agno,
425 struct xfs_scrub_ag *sa)
426{
427 int error;
428
429 sa->agno = agno;
430 error = xfs_scrub_ag_read_headers(sc, agno, &sa->agi_bp,
431 &sa->agf_bp, &sa->agfl_bp);
432 if (error)
433 return error;
434
435 return xfs_scrub_ag_btcur_init(sc, sa);
436}
437
Darrick J. Wongdcb660f2017-10-17 21:37:36 -0700438/* Per-scrubber setup functions */
439
440/* Set us up with a transaction and an empty context. */
441int
442xfs_scrub_setup_fs(
443 struct xfs_scrub_context *sc,
444 struct xfs_inode *ip)
445{
446 return xfs_scrub_trans_alloc(sc->sm, sc->mp, &sc->tp);
447}
Darrick J. Wongefa7a992017-10-17 21:37:40 -0700448
449/* Set us up with AG headers and btree cursors. */
450int
451xfs_scrub_setup_ag_btree(
452 struct xfs_scrub_context *sc,
453 struct xfs_inode *ip,
454 bool force_log)
455{
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700456 struct xfs_mount *mp = sc->mp;
Darrick J. Wongefa7a992017-10-17 21:37:40 -0700457 int error;
458
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700459 /*
460 * If the caller asks us to checkpont the log, do so. This
461 * expensive operation should be performed infrequently and only
462 * as a last resort. Any caller that sets force_log should
463 * document why they need to do so.
464 */
465 if (force_log) {
466 error = xfs_scrub_checkpoint_log(mp);
467 if (error)
468 return error;
469 }
470
Darrick J. Wongefa7a992017-10-17 21:37:40 -0700471 error = xfs_scrub_setup_ag_header(sc, ip);
472 if (error)
473 return error;
474
475 return xfs_scrub_ag_init(sc, sc->sm->sm_agno, &sc->sa);
476}
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700477
478/* Push everything out of the log onto disk. */
479int
480xfs_scrub_checkpoint_log(
481 struct xfs_mount *mp)
482{
483 int error;
484
485 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
486 if (error)
487 return error;
488 xfs_ail_push_all_sync(mp->m_ail);
489 return 0;
490}