blob: 318dd97c70b565d22a1506b6a9017e116eb9288f [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"
Darrick J. Wong80e4e122017-10-17 21:37:42 -070033#include "xfs_icache.h"
34#include "xfs_itable.h"
Darrick J. Wongdcb660f2017-10-17 21:37:36 -070035#include "xfs_alloc.h"
36#include "xfs_alloc_btree.h"
37#include "xfs_bmap.h"
38#include "xfs_bmap_btree.h"
39#include "xfs_ialloc.h"
40#include "xfs_ialloc_btree.h"
41#include "xfs_refcount.h"
42#include "xfs_refcount_btree.h"
43#include "xfs_rmap.h"
44#include "xfs_rmap_btree.h"
Darrick J. Wong3daa6642017-10-17 21:37:40 -070045#include "xfs_log.h"
46#include "xfs_trans_priv.h"
Darrick J. Wongdcb660f2017-10-17 21:37:36 -070047#include "scrub/xfs_scrub.h"
48#include "scrub/scrub.h"
49#include "scrub/common.h"
50#include "scrub/trace.h"
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -070051#include "scrub/btree.h"
Darrick J. Wongdcb660f2017-10-17 21:37:36 -070052
53/* Common code for the metadata scrubbers. */
54
Darrick J. Wong4700d222017-10-17 21:37:36 -070055/*
56 * Handling operational errors.
57 *
58 * The *_process_error() family of functions are used to process error return
59 * codes from functions called as part of a scrub operation.
60 *
61 * If there's no error, we return true to tell the caller that it's ok
62 * to move on to the next check in its list.
63 *
64 * For non-verifier errors (e.g. ENOMEM) we return false to tell the
65 * caller that something bad happened, and we preserve *error so that
66 * the caller can return the *error up the stack to userspace.
67 *
68 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting
69 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words,
70 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT,
71 * not via return codes. We return false to tell the caller that
72 * something bad happened. Since the error has been cleared, the caller
73 * will (presumably) return that zero and scrubbing will move on to
74 * whatever's next.
75 *
76 * ftrace can be used to record the precise metadata location and the
77 * approximate code location of the failed operation.
78 */
79
80/* Check for operational errors. */
81bool
82xfs_scrub_process_error(
83 struct xfs_scrub_context *sc,
84 xfs_agnumber_t agno,
85 xfs_agblock_t bno,
86 int *error)
87{
88 switch (*error) {
89 case 0:
90 return true;
91 case -EDEADLOCK:
92 /* Used to restart an op with deadlock avoidance. */
93 trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
94 break;
95 case -EFSBADCRC:
96 case -EFSCORRUPTED:
97 /* Note the badness but don't abort. */
98 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
99 *error = 0;
100 /* fall through */
101 default:
102 trace_xfs_scrub_op_error(sc, agno, bno, *error,
103 __return_address);
104 break;
105 }
106 return false;
107}
108
109/* Check for operational errors for a file offset. */
110bool
111xfs_scrub_fblock_process_error(
112 struct xfs_scrub_context *sc,
113 int whichfork,
114 xfs_fileoff_t offset,
115 int *error)
116{
117 switch (*error) {
118 case 0:
119 return true;
120 case -EDEADLOCK:
121 /* Used to restart an op with deadlock avoidance. */
122 trace_xfs_scrub_deadlock_retry(sc->ip, sc->sm, *error);
123 break;
124 case -EFSBADCRC:
125 case -EFSCORRUPTED:
126 /* Note the badness but don't abort. */
127 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
128 *error = 0;
129 /* fall through */
130 default:
131 trace_xfs_scrub_file_op_error(sc, whichfork, offset, *error,
132 __return_address);
133 break;
134 }
135 return false;
136}
137
138/*
139 * Handling scrub corruption/optimization/warning checks.
140 *
141 * The *_set_{corrupt,preen,warning}() family of functions are used to
142 * record the presence of metadata that is incorrect (corrupt), could be
143 * optimized somehow (preen), or should be flagged for administrative
144 * review but is not incorrect (warn).
145 *
146 * ftrace can be used to record the precise metadata location and
147 * approximate code location of the failed check.
148 */
149
150/* Record a block which could be optimized. */
151void
152xfs_scrub_block_set_preen(
153 struct xfs_scrub_context *sc,
154 struct xfs_buf *bp)
155{
156 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
157 trace_xfs_scrub_block_preen(sc, bp->b_bn, __return_address);
158}
159
160/*
161 * Record an inode which could be optimized. The trace data will
162 * include the block given by bp if bp is given; otherwise it will use
163 * the block location of the inode record itself.
164 */
165void
166xfs_scrub_ino_set_preen(
167 struct xfs_scrub_context *sc,
168 struct xfs_buf *bp)
169{
170 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN;
171 trace_xfs_scrub_ino_preen(sc, sc->ip->i_ino, bp ? bp->b_bn : 0,
172 __return_address);
173}
174
175/* Record a corrupt block. */
176void
177xfs_scrub_block_set_corrupt(
178 struct xfs_scrub_context *sc,
179 struct xfs_buf *bp)
180{
181 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
182 trace_xfs_scrub_block_error(sc, bp->b_bn, __return_address);
183}
184
185/*
186 * Record a corrupt inode. The trace data will include the block given
187 * by bp if bp is given; otherwise it will use the block location of the
188 * inode record itself.
189 */
190void
191xfs_scrub_ino_set_corrupt(
192 struct xfs_scrub_context *sc,
193 xfs_ino_t ino,
194 struct xfs_buf *bp)
195{
196 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
197 trace_xfs_scrub_ino_error(sc, ino, bp ? bp->b_bn : 0, __return_address);
198}
199
200/* Record corruption in a block indexed by a file fork. */
201void
202xfs_scrub_fblock_set_corrupt(
203 struct xfs_scrub_context *sc,
204 int whichfork,
205 xfs_fileoff_t offset)
206{
207 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
208 trace_xfs_scrub_fblock_error(sc, whichfork, offset, __return_address);
209}
210
211/*
212 * Warn about inodes that need administrative review but is not
213 * incorrect.
214 */
215void
216xfs_scrub_ino_set_warning(
217 struct xfs_scrub_context *sc,
218 struct xfs_buf *bp)
219{
220 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
221 trace_xfs_scrub_ino_warning(sc, sc->ip->i_ino, bp ? bp->b_bn : 0,
222 __return_address);
223}
224
225/* Warn about a block indexed by a file fork that needs review. */
226void
227xfs_scrub_fblock_set_warning(
228 struct xfs_scrub_context *sc,
229 int whichfork,
230 xfs_fileoff_t offset)
231{
232 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING;
233 trace_xfs_scrub_fblock_warning(sc, whichfork, offset, __return_address);
234}
235
236/* Signal an incomplete scrub. */
237void
238xfs_scrub_set_incomplete(
239 struct xfs_scrub_context *sc)
240{
241 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE;
242 trace_xfs_scrub_incomplete(sc, __return_address);
243}
244
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700245/*
246 * AG scrubbing
247 *
248 * These helpers facilitate locking an allocation group's header
249 * buffers, setting up cursors for all btrees that are present, and
250 * cleaning everything up once we're through.
251 */
252
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700253/* Decide if we want to return an AG header read failure. */
254static inline bool
255want_ag_read_header_failure(
256 struct xfs_scrub_context *sc,
257 unsigned int type)
258{
259 /* Return all AG header read failures when scanning btrees. */
260 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF &&
Darrick J. Wonga12890a2017-10-17 21:37:39 -0700261 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL &&
262 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI)
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700263 return true;
264 /*
265 * If we're scanning a given type of AG header, we only want to
266 * see read failures from that specific header. We'd like the
267 * other headers to cross-check them, but this isn't required.
268 */
269 if (sc->sm->sm_type == type)
270 return true;
271 return false;
272}
273
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700274/*
275 * Grab all the headers for an AG.
276 *
277 * The headers should be released by xfs_scrub_ag_free, but as a fail
278 * safe we attach all the buffers we grab to the scrub transaction so
279 * they'll all be freed when we cancel it.
280 */
281int
282xfs_scrub_ag_read_headers(
283 struct xfs_scrub_context *sc,
284 xfs_agnumber_t agno,
285 struct xfs_buf **agi,
286 struct xfs_buf **agf,
287 struct xfs_buf **agfl)
288{
289 struct xfs_mount *mp = sc->mp;
290 int error;
291
292 error = xfs_ialloc_read_agi(mp, sc->tp, agno, agi);
Darrick J. Wonga12890a2017-10-17 21:37:39 -0700293 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI))
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700294 goto out;
295
296 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, agf);
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700297 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF))
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700298 goto out;
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700299
300 error = xfs_alloc_read_agfl(mp, sc->tp, agno, agfl);
Darrick J. Wongab9d5dc2017-10-17 21:37:39 -0700301 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL))
Darrick J. Wongb6c1beb2017-10-17 21:37:38 -0700302 goto out;
303
304out:
305 return error;
306}
307
308/* Release all the AG btree cursors. */
309void
310xfs_scrub_ag_btcur_free(
311 struct xfs_scrub_ag *sa)
312{
313 if (sa->refc_cur)
314 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR);
315 if (sa->rmap_cur)
316 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR);
317 if (sa->fino_cur)
318 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR);
319 if (sa->ino_cur)
320 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR);
321 if (sa->cnt_cur)
322 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR);
323 if (sa->bno_cur)
324 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR);
325
326 sa->refc_cur = NULL;
327 sa->rmap_cur = NULL;
328 sa->fino_cur = NULL;
329 sa->ino_cur = NULL;
330 sa->bno_cur = NULL;
331 sa->cnt_cur = NULL;
332}
333
334/* Initialize all the btree cursors for an AG. */
335int
336xfs_scrub_ag_btcur_init(
337 struct xfs_scrub_context *sc,
338 struct xfs_scrub_ag *sa)
339{
340 struct xfs_mount *mp = sc->mp;
341 xfs_agnumber_t agno = sa->agno;
342
343 if (sa->agf_bp) {
344 /* Set up a bnobt cursor for cross-referencing. */
345 sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
346 agno, XFS_BTNUM_BNO);
347 if (!sa->bno_cur)
348 goto err;
349
350 /* Set up a cntbt cursor for cross-referencing. */
351 sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp,
352 agno, XFS_BTNUM_CNT);
353 if (!sa->cnt_cur)
354 goto err;
355 }
356
357 /* Set up a inobt cursor for cross-referencing. */
358 if (sa->agi_bp) {
359 sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
360 agno, XFS_BTNUM_INO);
361 if (!sa->ino_cur)
362 goto err;
363 }
364
365 /* Set up a finobt cursor for cross-referencing. */
366 if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb)) {
367 sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp,
368 agno, XFS_BTNUM_FINO);
369 if (!sa->fino_cur)
370 goto err;
371 }
372
373 /* Set up a rmapbt cursor for cross-referencing. */
374 if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb)) {
375 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp,
376 agno);
377 if (!sa->rmap_cur)
378 goto err;
379 }
380
381 /* Set up a refcountbt cursor for cross-referencing. */
382 if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb)) {
383 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp,
384 sa->agf_bp, agno, NULL);
385 if (!sa->refc_cur)
386 goto err;
387 }
388
389 return 0;
390err:
391 return -ENOMEM;
392}
393
394/* Release the AG header context and btree cursors. */
395void
396xfs_scrub_ag_free(
397 struct xfs_scrub_context *sc,
398 struct xfs_scrub_ag *sa)
399{
400 xfs_scrub_ag_btcur_free(sa);
401 if (sa->agfl_bp) {
402 xfs_trans_brelse(sc->tp, sa->agfl_bp);
403 sa->agfl_bp = NULL;
404 }
405 if (sa->agf_bp) {
406 xfs_trans_brelse(sc->tp, sa->agf_bp);
407 sa->agf_bp = NULL;
408 }
409 if (sa->agi_bp) {
410 xfs_trans_brelse(sc->tp, sa->agi_bp);
411 sa->agi_bp = NULL;
412 }
413 sa->agno = NULLAGNUMBER;
414}
415
416/*
417 * For scrub, grab the AGI and the AGF headers, in that order. Locking
418 * order requires us to get the AGI before the AGF. We use the
419 * transaction to avoid deadlocking on crosslinked metadata buffers;
420 * either the caller passes one in (bmap scrub) or we have to create a
421 * transaction ourselves.
422 */
423int
424xfs_scrub_ag_init(
425 struct xfs_scrub_context *sc,
426 xfs_agnumber_t agno,
427 struct xfs_scrub_ag *sa)
428{
429 int error;
430
431 sa->agno = agno;
432 error = xfs_scrub_ag_read_headers(sc, agno, &sa->agi_bp,
433 &sa->agf_bp, &sa->agfl_bp);
434 if (error)
435 return error;
436
437 return xfs_scrub_ag_btcur_init(sc, sa);
438}
439
Darrick J. Wongdcb660f2017-10-17 21:37:36 -0700440/* Per-scrubber setup functions */
441
442/* Set us up with a transaction and an empty context. */
443int
444xfs_scrub_setup_fs(
445 struct xfs_scrub_context *sc,
446 struct xfs_inode *ip)
447{
448 return xfs_scrub_trans_alloc(sc->sm, sc->mp, &sc->tp);
449}
Darrick J. Wongefa7a992017-10-17 21:37:40 -0700450
451/* Set us up with AG headers and btree cursors. */
452int
453xfs_scrub_setup_ag_btree(
454 struct xfs_scrub_context *sc,
455 struct xfs_inode *ip,
456 bool force_log)
457{
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700458 struct xfs_mount *mp = sc->mp;
Darrick J. Wongefa7a992017-10-17 21:37:40 -0700459 int error;
460
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700461 /*
462 * If the caller asks us to checkpont the log, do so. This
463 * expensive operation should be performed infrequently and only
464 * as a last resort. Any caller that sets force_log should
465 * document why they need to do so.
466 */
467 if (force_log) {
468 error = xfs_scrub_checkpoint_log(mp);
469 if (error)
470 return error;
471 }
472
Darrick J. Wongefa7a992017-10-17 21:37:40 -0700473 error = xfs_scrub_setup_ag_header(sc, ip);
474 if (error)
475 return error;
476
477 return xfs_scrub_ag_init(sc, sc->sm->sm_agno, &sc->sa);
478}
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700479
480/* Push everything out of the log onto disk. */
481int
482xfs_scrub_checkpoint_log(
483 struct xfs_mount *mp)
484{
485 int error;
486
487 error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
488 if (error)
489 return error;
490 xfs_ail_push_all_sync(mp->m_ail);
491 return 0;
492}
Darrick J. Wong80e4e122017-10-17 21:37:42 -0700493
494/*
495 * Given an inode and the scrub control structure, grab either the
496 * inode referenced in the control structure or the inode passed in.
497 * The inode is not locked.
498 */
499int
500xfs_scrub_get_inode(
501 struct xfs_scrub_context *sc,
502 struct xfs_inode *ip_in)
503{
504 struct xfs_mount *mp = sc->mp;
505 struct xfs_inode *ip = NULL;
506 int error;
507
508 /*
509 * If userspace passed us an AG number or a generation number
510 * without an inode number, they haven't got a clue so bail out
511 * immediately.
512 */
513 if (sc->sm->sm_agno || (sc->sm->sm_gen && !sc->sm->sm_ino))
514 return -EINVAL;
515
516 /* We want to scan the inode we already had opened. */
517 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) {
518 sc->ip = ip_in;
519 return 0;
520 }
521
522 /* Look up the inode, see if the generation number matches. */
523 if (xfs_internal_inum(mp, sc->sm->sm_ino))
524 return -ENOENT;
525 error = xfs_iget(mp, NULL, sc->sm->sm_ino,
526 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip);
527 if (error == -ENOENT || error == -EINVAL) {
528 /* inode doesn't exist... */
529 return -ENOENT;
530 } else if (error) {
531 trace_xfs_scrub_op_error(sc,
532 XFS_INO_TO_AGNO(mp, sc->sm->sm_ino),
533 XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino),
534 error, __return_address);
535 return error;
536 }
537 if (VFS_I(ip)->i_generation != sc->sm->sm_gen) {
538 iput(VFS_I(ip));
539 return -ENOENT;
540 }
541
542 sc->ip = ip;
543 return 0;
544}
Darrick J. Wonga5c46e52017-10-17 21:37:44 -0700545
546/* Set us up to scrub a file's contents. */
547int
548xfs_scrub_setup_inode_contents(
549 struct xfs_scrub_context *sc,
550 struct xfs_inode *ip,
551 unsigned int resblks)
552{
553 struct xfs_mount *mp = sc->mp;
554 int error;
555
556 error = xfs_scrub_get_inode(sc, ip);
557 if (error)
558 return error;
559
560 /* Got the inode, lock it and we're ready to go. */
561 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
562 xfs_ilock(sc->ip, sc->ilock_flags);
563 error = xfs_scrub_trans_alloc(sc->sm, mp, &sc->tp);
564 if (error)
565 goto out;
566 sc->ilock_flags |= XFS_ILOCK_EXCL;
567 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
568
569out:
570 /* scrub teardown will unlock and release the inode for us */
571 return error;
572}