blob: f28f4bad317b6792f73f45ac7ac118a1c40f1060 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong84d42ea2018-05-14 06:34:36 -07002/*
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
Darrick J. Wong84d42ea2018-05-14 06:34:36 -07004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong84d42ea2018-05-14 06:34:36 -07005 */
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_defer.h"
13#include "xfs_btree.h"
14#include "xfs_bit.h"
15#include "xfs_log_format.h"
16#include "xfs_trans.h"
17#include "xfs_sb.h"
18#include "xfs_inode.h"
19#include "xfs_icache.h"
20#include "xfs_alloc.h"
21#include "xfs_alloc_btree.h"
22#include "xfs_ialloc.h"
23#include "xfs_ialloc_btree.h"
24#include "xfs_rmap.h"
25#include "xfs_rmap_btree.h"
26#include "xfs_refcount.h"
27#include "xfs_refcount_btree.h"
28#include "xfs_extent_busy.h"
29#include "xfs_ag_resv.h"
30#include "xfs_trans_space.h"
Darrick J. Wong7e85bc62018-05-29 22:18:11 -070031#include "xfs_quota.h"
Darrick J. Wong38b62382018-10-18 17:20:35 +110032#include "xfs_attr.h"
33#include "xfs_reflink.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070034#include "scrub/xfs_scrub.h"
35#include "scrub/scrub.h"
36#include "scrub/common.h"
37#include "scrub/trace.h"
38#include "scrub/repair.h"
Darrick J. Wongbc270b52018-07-29 22:37:09 -070039#include "scrub/bitmap.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070040
41/*
42 * Attempt to repair some metadata, if the metadata is corrupt and userspace
43 * told us to fix it. This function returns -EAGAIN to mean "re-run scrub",
44 * and will set *fixed to true if it thinks it repaired anything.
45 */
46int
Darrick J. Wongb5e21962018-07-19 12:29:11 -070047xrep_attempt(
Darrick J. Wong032d91f2018-07-19 12:29:12 -070048 struct xfs_inode *ip,
Darrick J. Wong1d8a7482018-07-19 12:29:12 -070049 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -070050 bool *fixed)
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070051{
Darrick J. Wong032d91f2018-07-19 12:29:12 -070052 int error = 0;
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070053
Darrick J. Wongb5e21962018-07-19 12:29:11 -070054 trace_xrep_attempt(ip, sc->sm, error);
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070055
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070056 xchk_ag_btcur_free(&sc->sa);
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070057
58 /* Repair whatever's broken. */
59 ASSERT(sc->ops->repair);
60 error = sc->ops->repair(sc);
Darrick J. Wongb5e21962018-07-19 12:29:11 -070061 trace_xrep_done(ip, sc->sm, error);
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070062 switch (error) {
63 case 0:
64 /*
65 * Repair succeeded. Commit the fixes and perform a second
66 * scrub so that we can tell userspace if we fixed the problem.
67 */
68 sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
69 *fixed = true;
70 return -EAGAIN;
71 case -EDEADLOCK:
72 case -EAGAIN:
73 /* Tell the caller to try again having grabbed all the locks. */
74 if (!sc->try_harder) {
75 sc->try_harder = true;
76 return -EAGAIN;
77 }
78 /*
79 * We tried harder but still couldn't grab all the resources
80 * we needed to fix it. The corruption has not been fixed,
81 * so report back to userspace.
82 */
83 return -EFSCORRUPTED;
84 default:
85 return error;
86 }
87}
88
89/*
90 * Complain about unfixable problems in the filesystem. We don't log
91 * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
92 * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
93 * administrator isn't running xfs_scrub in no-repairs mode.
94 *
95 * Use this helper function because _ratelimited silently declares a static
96 * structure to track rate limiting information.
97 */
98void
Darrick J. Wongb5e21962018-07-19 12:29:11 -070099xrep_failure(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700100 struct xfs_mount *mp)
Darrick J. Wong84d42ea2018-05-14 06:34:36 -0700101{
102 xfs_alert_ratelimited(mp,
103"Corruption not fixed during online repair. Unmount and run xfs_repair.");
104}
105
106/*
107 * Repair probe -- userspace uses this to probe if we're willing to repair a
108 * given mountpoint.
109 */
110int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700111xrep_probe(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700112 struct xfs_scrub *sc)
Darrick J. Wong84d42ea2018-05-14 06:34:36 -0700113{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700114 int error = 0;
Darrick J. Wong84d42ea2018-05-14 06:34:36 -0700115
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700116 if (xchk_should_terminate(sc, &error))
Darrick J. Wong84d42ea2018-05-14 06:34:36 -0700117 return error;
118
119 return 0;
120}
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700121
122/*
123 * Roll a transaction, keeping the AG headers locked and reinitializing
124 * the btree cursors.
125 */
126int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700127xrep_roll_ag_trans(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700128 struct xfs_scrub *sc)
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700129{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700130 int error;
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700131
132 /* Keep the AG header buffers locked so we can keep going. */
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700133 if (sc->sa.agi_bp)
134 xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
135 if (sc->sa.agf_bp)
136 xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
137 if (sc->sa.agfl_bp)
138 xfs_trans_bhold(sc->tp, sc->sa.agfl_bp);
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700139
140 /* Roll the transaction. */
141 error = xfs_trans_roll(&sc->tp);
142 if (error)
143 goto out_release;
144
145 /* Join AG headers to the new transaction. */
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700146 if (sc->sa.agi_bp)
147 xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
148 if (sc->sa.agf_bp)
149 xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
150 if (sc->sa.agfl_bp)
151 xfs_trans_bjoin(sc->tp, sc->sa.agfl_bp);
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700152
153 return 0;
154
155out_release:
156 /*
157 * Rolling failed, so release the hold on the buffers. The
158 * buffers will be released during teardown on our way out
159 * of the kernel.
160 */
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700161 if (sc->sa.agi_bp)
162 xfs_trans_bhold_release(sc->tp, sc->sa.agi_bp);
163 if (sc->sa.agf_bp)
164 xfs_trans_bhold_release(sc->tp, sc->sa.agf_bp);
165 if (sc->sa.agfl_bp)
166 xfs_trans_bhold_release(sc->tp, sc->sa.agfl_bp);
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700167
168 return error;
169}
170
171/*
172 * Does the given AG have enough space to rebuild a btree? Neither AG
173 * reservation can be critical, and we must have enough space (factoring
174 * in AG reservations) to construct a whole btree.
175 */
176bool
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700177xrep_ag_has_space(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700178 struct xfs_perag *pag,
179 xfs_extlen_t nr_blocks,
180 enum xfs_ag_resv_type type)
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700181{
182 return !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
183 !xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
184 pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
185}
186
187/*
188 * Figure out how many blocks to reserve for an AG repair. We calculate the
189 * worst case estimate for the number of blocks we'd need to rebuild one of
190 * any type of per-AG btree.
191 */
192xfs_extlen_t
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700193xrep_calc_ag_resblks(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700194 struct xfs_scrub *sc)
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700195{
196 struct xfs_mount *mp = sc->mp;
197 struct xfs_scrub_metadata *sm = sc->sm;
198 struct xfs_perag *pag;
199 struct xfs_buf *bp;
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700200 xfs_agino_t icount = NULLAGINO;
201 xfs_extlen_t aglen = NULLAGBLOCK;
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700202 xfs_extlen_t usedlen;
203 xfs_extlen_t freelen;
204 xfs_extlen_t bnobt_sz;
205 xfs_extlen_t inobt_sz;
206 xfs_extlen_t rmapbt_sz;
207 xfs_extlen_t refcbt_sz;
208 int error;
209
210 if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
211 return 0;
212
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700213 pag = xfs_perag_get(mp, sm->sm_agno);
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700214 if (pag->pagi_init) {
215 /* Use in-core icount if possible. */
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700216 icount = pag->pagi_count;
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700217 } else {
218 /* Try to get the actual counters from disk. */
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700219 error = xfs_ialloc_read_agi(mp, NULL, sm->sm_agno, &bp);
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700220 if (!error) {
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700221 icount = pag->pagi_count;
222 xfs_buf_relse(bp);
223 }
224 }
225
226 /* Now grab the block counters from the AGF. */
227 error = xfs_alloc_read_agf(mp, NULL, sm->sm_agno, 0, &bp);
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700228 if (!error) {
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700229 aglen = be32_to_cpu(XFS_BUF_TO_AGF(bp)->agf_length);
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700230 freelen = be32_to_cpu(XFS_BUF_TO_AGF(bp)->agf_freeblks);
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700231 usedlen = aglen - freelen;
232 xfs_buf_relse(bp);
233 }
234 xfs_perag_put(pag);
235
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700236 /* If the icount is impossible, make some worst-case assumptions. */
237 if (icount == NULLAGINO ||
238 !xfs_verify_agino(mp, sm->sm_agno, icount)) {
239 xfs_agino_t first, last;
240
241 xfs_agino_range(mp, sm->sm_agno, &first, &last);
242 icount = last - first + 1;
243 }
244
245 /* If the block counts are impossible, make worst-case assumptions. */
246 if (aglen == NULLAGBLOCK ||
247 aglen != xfs_ag_block_count(mp, sm->sm_agno) ||
248 freelen >= aglen) {
249 aglen = xfs_ag_block_count(mp, sm->sm_agno);
250 freelen = aglen;
251 usedlen = aglen;
252 }
253
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700254 trace_xrep_calc_ag_resblks(mp, sm->sm_agno, icount, aglen,
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700255 freelen, usedlen);
256
257 /*
258 * Figure out how many blocks we'd need worst case to rebuild
259 * each type of btree. Note that we can only rebuild the
260 * bnobt/cntbt or inobt/finobt as pairs.
261 */
262 bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
263 if (xfs_sb_version_hassparseinodes(&mp->m_sb))
264 inobt_sz = xfs_iallocbt_calc_size(mp, icount /
265 XFS_INODES_PER_HOLEMASK_BIT);
266 else
267 inobt_sz = xfs_iallocbt_calc_size(mp, icount /
268 XFS_INODES_PER_CHUNK);
269 if (xfs_sb_version_hasfinobt(&mp->m_sb))
270 inobt_sz *= 2;
271 if (xfs_sb_version_hasreflink(&mp->m_sb))
272 refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
273 else
274 refcbt_sz = 0;
275 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
276 /*
277 * Guess how many blocks we need to rebuild the rmapbt.
278 * For non-reflink filesystems we can't have more records than
279 * used blocks. However, with reflink it's possible to have
280 * more than one rmap record per AG block. We don't know how
281 * many rmaps there could be in the AG, so we start off with
282 * what we hope is an generous over-estimation.
283 */
284 if (xfs_sb_version_hasreflink(&mp->m_sb))
285 rmapbt_sz = xfs_rmapbt_calc_size(mp,
286 (unsigned long long)aglen * 2);
287 else
288 rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
289 } else {
290 rmapbt_sz = 0;
291 }
292
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700293 trace_xrep_calc_ag_resblks_btsize(mp, sm->sm_agno, bnobt_sz,
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700294 inobt_sz, rmapbt_sz, refcbt_sz);
295
296 return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
297}
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700298
299/* Allocate a block in an AG. */
300int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700301xrep_alloc_ag_block(
Darrick J. Wong66e32372018-12-12 08:46:23 -0800302 struct xfs_scrub *sc,
303 const struct xfs_owner_info *oinfo,
304 xfs_fsblock_t *fsbno,
305 enum xfs_ag_resv_type resv)
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700306{
Darrick J. Wong66e32372018-12-12 08:46:23 -0800307 struct xfs_alloc_arg args = {0};
308 xfs_agblock_t bno;
309 int error;
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700310
311 switch (resv) {
312 case XFS_AG_RESV_AGFL:
313 case XFS_AG_RESV_RMAPBT:
314 error = xfs_alloc_get_freelist(sc->tp, sc->sa.agf_bp, &bno, 1);
315 if (error)
316 return error;
317 if (bno == NULLAGBLOCK)
318 return -ENOSPC;
319 xfs_extent_busy_reuse(sc->mp, sc->sa.agno, bno,
320 1, false);
321 *fsbno = XFS_AGB_TO_FSB(sc->mp, sc->sa.agno, bno);
322 if (resv == XFS_AG_RESV_RMAPBT)
323 xfs_ag_resv_rmapbt_alloc(sc->mp, sc->sa.agno);
324 return 0;
325 default:
326 break;
327 }
328
329 args.tp = sc->tp;
330 args.mp = sc->mp;
331 args.oinfo = *oinfo;
332 args.fsbno = XFS_AGB_TO_FSB(args.mp, sc->sa.agno, 0);
333 args.minlen = 1;
334 args.maxlen = 1;
335 args.prod = 1;
336 args.type = XFS_ALLOCTYPE_THIS_AG;
337 args.resv = resv;
338
339 error = xfs_alloc_vextent(&args);
340 if (error)
341 return error;
342 if (args.fsbno == NULLFSBLOCK)
343 return -ENOSPC;
344 ASSERT(args.len == 1);
345 *fsbno = args.fsbno;
346
347 return 0;
348}
349
350/* Initialize a new AG btree root block with zero entries. */
351int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700352xrep_init_btblock(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700353 struct xfs_scrub *sc,
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700354 xfs_fsblock_t fsb,
355 struct xfs_buf **bpp,
356 xfs_btnum_t btnum,
357 const struct xfs_buf_ops *ops)
358{
359 struct xfs_trans *tp = sc->tp;
360 struct xfs_mount *mp = sc->mp;
361 struct xfs_buf *bp;
362
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700363 trace_xrep_init_btblock(mp, XFS_FSB_TO_AGNO(mp, fsb),
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700364 XFS_FSB_TO_AGBNO(mp, fsb), btnum);
365
366 ASSERT(XFS_FSB_TO_AGNO(mp, fsb) == sc->sa.agno);
367 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, XFS_FSB_TO_DADDR(mp, fsb),
368 XFS_FSB_TO_BB(mp, 1), 0);
369 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
370 xfs_btree_init_block(mp, bp, btnum, 0, 0, sc->sa.agno, 0);
371 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF);
372 xfs_trans_log_buf(tp, bp, 0, bp->b_length);
373 bp->b_ops = ops;
374 *bpp = bp;
375
376 return 0;
377}
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700378
379/*
380 * Reconstructing per-AG Btrees
381 *
382 * When a space btree is corrupt, we don't bother trying to fix it. Instead,
383 * we scan secondary space metadata to derive the records that should be in
384 * the damaged btree, initialize a fresh btree root, and insert the records.
385 * Note that for rebuilding the rmapbt we scan all the primary data to
386 * generate the new records.
387 *
388 * However, that leaves the matter of removing all the metadata describing the
389 * old broken structure. For primary metadata we use the rmap data to collect
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700390 * every extent with a matching rmap owner (bitmap); we then iterate all other
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700391 * metadata structures with the same rmap owner to collect the extents that
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700392 * cannot be removed (sublist). We then subtract sublist from bitmap to
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700393 * derive the blocks that were used by the old btree. These blocks can be
394 * reaped.
395 *
396 * For rmapbt reconstructions we must use different tactics for extent
397 * collection. First we iterate all primary metadata (this excludes the old
398 * rmapbt, obviously) to generate new rmap records. The gaps in the rmap
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700399 * records are collected as bitmap. The bnobt records are collected as
400 * sublist. As with the other btrees we subtract sublist from bitmap, and the
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700401 * result (since the rmapbt lives in the free space) are the blocks from the
402 * old rmapbt.
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700403 *
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700404 * Disposal of Blocks from Old per-AG Btrees
405 *
406 * Now that we've constructed a new btree to replace the damaged one, we want
407 * to dispose of the blocks that (we think) the old btree was using.
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700408 * Previously, we used the rmapbt to collect the extents (bitmap) with the
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700409 * rmap owner corresponding to the tree we rebuilt, collected extents for any
410 * blocks with the same rmap owner that are owned by another data structure
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700411 * (sublist), and subtracted sublist from bitmap. In theory the extents
412 * remaining in bitmap are the old btree's blocks.
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700413 *
414 * Unfortunately, it's possible that the btree was crosslinked with other
415 * blocks on disk. The rmap data can tell us if there are multiple owners, so
416 * if the rmapbt says there is an owner of this block other than @oinfo, then
417 * the block is crosslinked. Remove the reverse mapping and continue.
418 *
419 * If there is one rmap record, we can free the block, which removes the
420 * reverse mapping but doesn't add the block to the free space. Our repair
421 * strategy is to hope the other metadata objects crosslinked on this block
422 * will be rebuilt (atop different blocks), thereby removing all the cross
423 * links.
424 *
425 * If there are no rmap records at all, we also free the block. If the btree
426 * being rebuilt lives in the free space (bnobt/cntbt/rmapbt) then there isn't
427 * supposed to be a rmap record and everything is ok. For other btrees there
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700428 * had to have been an rmap entry for the block to have ended up on @bitmap,
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700429 * so if it's gone now there's something wrong and the fs will shut down.
430 *
431 * Note: If there are multiple rmap records with only the same rmap owner as
432 * the btree we're trying to rebuild and the block is indeed owned by another
433 * data structure with the same rmap owner, then the block will be in sublist
434 * and therefore doesn't need disposal. If there are multiple rmap records
435 * with only the same rmap owner but the block is not owned by something with
436 * the same rmap owner, the block will be freed.
437 *
438 * The caller is responsible for locking the AG headers for the entire rebuild
439 * operation so that nothing else can sneak in and change the AG state while
440 * we're not looking. We also assume that the caller already invalidated any
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700441 * buffers associated with @bitmap.
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700442 */
443
444/*
445 * Invalidate buffers for per-AG btree blocks we're dumping. This function
446 * is not intended for use with file data repairs; we have bunmapi for that.
447 */
448int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700449xrep_invalidate_blocks(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700450 struct xfs_scrub *sc,
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700451 struct xfs_bitmap *bitmap)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700452{
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700453 struct xfs_bitmap_range *bmr;
454 struct xfs_bitmap_range *n;
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700455 struct xfs_buf *bp;
456 xfs_fsblock_t fsbno;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700457
458 /*
459 * For each block in each extent, see if there's an incore buffer for
460 * exactly that block; if so, invalidate it. The buffer cache only
461 * lets us look for one buffer at a time, so we have to look one block
462 * at a time. Avoid invalidating AG headers and post-EOFS blocks
463 * because we never own those; and if we can't TRYLOCK the buffer we
464 * assume it's owned by someone else.
465 */
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700466 for_each_xfs_bitmap_block(fsbno, bmr, n, bitmap) {
467 /* Skip AG headers and post-EOFS blocks */
468 if (!xfs_verify_fsbno(sc->mp, fsbno))
469 continue;
470 bp = xfs_buf_incore(sc->mp->m_ddev_targp,
471 XFS_FSB_TO_DADDR(sc->mp, fsbno),
472 XFS_FSB_TO_BB(sc->mp, 1), XBF_TRYLOCK);
473 if (bp) {
474 xfs_trans_bjoin(sc->tp, bp);
475 xfs_trans_binval(sc->tp, bp);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700476 }
477 }
478
479 return 0;
480}
481
482/* Ensure the freelist is the correct size. */
483int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700484xrep_fix_freelist(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700485 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700486 bool can_shrink)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700487{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700488 struct xfs_alloc_arg args = {0};
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700489
490 args.mp = sc->mp;
491 args.tp = sc->tp;
492 args.agno = sc->sa.agno;
493 args.alignment = 1;
494 args.pag = sc->sa.pag;
495
496 return xfs_alloc_fix_freelist(&args,
497 can_shrink ? 0 : XFS_ALLOC_FLAG_NOSHRINK);
498}
499
500/*
501 * Put a block back on the AGFL.
502 */
503STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700504xrep_put_freelist(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700505 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700506 xfs_agblock_t agbno)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700507{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700508 int error;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700509
510 /* Make sure there's space on the freelist. */
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700511 error = xrep_fix_freelist(sc, true);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700512 if (error)
513 return error;
514
515 /*
516 * Since we're "freeing" a lost block onto the AGFL, we have to
517 * create an rmap for the block prior to merging it or else other
518 * parts will break.
519 */
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700520 error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.agno, agbno, 1,
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800521 &XFS_RMAP_OINFO_AG);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700522 if (error)
523 return error;
524
525 /* Put the block on the AGFL. */
526 error = xfs_alloc_put_freelist(sc->tp, sc->sa.agf_bp, sc->sa.agfl_bp,
527 agbno, 0);
528 if (error)
529 return error;
530 xfs_extent_busy_insert(sc->tp, sc->sa.agno, agbno, 1,
531 XFS_EXTENT_BUSY_SKIP_DISCARD);
532
533 return 0;
534}
535
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700536/* Dispose of a single block. */
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700537STATIC int
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700538xrep_reap_block(
Darrick J. Wong66e32372018-12-12 08:46:23 -0800539 struct xfs_scrub *sc,
540 xfs_fsblock_t fsbno,
541 const struct xfs_owner_info *oinfo,
542 enum xfs_ag_resv_type resv)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700543{
Darrick J. Wong66e32372018-12-12 08:46:23 -0800544 struct xfs_btree_cur *cur;
545 struct xfs_buf *agf_bp = NULL;
546 xfs_agnumber_t agno;
547 xfs_agblock_t agbno;
548 bool has_other_rmap;
549 int error;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700550
551 agno = XFS_FSB_TO_AGNO(sc->mp, fsbno);
552 agbno = XFS_FSB_TO_AGBNO(sc->mp, fsbno);
553
554 /*
555 * If we are repairing per-inode metadata, we need to read in the AGF
556 * buffer. Otherwise, we're repairing a per-AG structure, so reuse
557 * the AGF buffer that the setup functions already grabbed.
558 */
559 if (sc->ip) {
560 error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf_bp);
561 if (error)
562 return error;
563 if (!agf_bp)
564 return -ENOMEM;
565 } else {
566 agf_bp = sc->sa.agf_bp;
567 }
568 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf_bp, agno);
569
570 /* Can we find any other rmappings? */
571 error = xfs_rmap_has_other_keys(cur, agbno, 1, oinfo, &has_other_rmap);
Darrick J. Wongef97ef22018-07-19 12:29:10 -0700572 xfs_btree_del_cursor(cur, error);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700573 if (error)
Darrick J. Wongef97ef22018-07-19 12:29:10 -0700574 goto out_free;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700575
576 /*
577 * If there are other rmappings, this block is cross linked and must
578 * not be freed. Remove the reverse mapping and move on. Otherwise,
579 * we were the only owner of the block, so free the extent, which will
580 * also remove the rmap.
581 *
582 * XXX: XFS doesn't support detecting the case where a single block
583 * metadata structure is crosslinked with a multi-block structure
584 * because the buffer cache doesn't detect aliasing problems, so we
585 * can't fix 100% of crosslinking problems (yet). The verifiers will
586 * blow on writeout, the filesystem will shut down, and the admin gets
587 * to run xfs_repair.
588 */
589 if (has_other_rmap)
590 error = xfs_rmap_free(sc->tp, agf_bp, agno, agbno, 1, oinfo);
591 else if (resv == XFS_AG_RESV_AGFL)
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700592 error = xrep_put_freelist(sc, agbno);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700593 else
594 error = xfs_free_extent(sc->tp, fsbno, 1, oinfo, resv);
595 if (agf_bp != sc->sa.agf_bp)
596 xfs_trans_brelse(sc->tp, agf_bp);
597 if (error)
598 return error;
599
600 if (sc->ip)
601 return xfs_trans_roll_inode(&sc->tp, sc->ip);
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700602 return xrep_roll_ag_trans(sc);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700603
Darrick J. Wongef97ef22018-07-19 12:29:10 -0700604out_free:
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700605 if (agf_bp != sc->sa.agf_bp)
606 xfs_trans_brelse(sc->tp, agf_bp);
607 return error;
608}
609
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700610/* Dispose of every block of every extent in the bitmap. */
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700611int
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700612xrep_reap_extents(
Darrick J. Wong66e32372018-12-12 08:46:23 -0800613 struct xfs_scrub *sc,
614 struct xfs_bitmap *bitmap,
615 const struct xfs_owner_info *oinfo,
616 enum xfs_ag_resv_type type)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700617{
Darrick J. Wong66e32372018-12-12 08:46:23 -0800618 struct xfs_bitmap_range *bmr;
619 struct xfs_bitmap_range *n;
620 xfs_fsblock_t fsbno;
621 int error = 0;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700622
623 ASSERT(xfs_sb_version_hasrmapbt(&sc->mp->m_sb));
624
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700625 for_each_xfs_bitmap_block(fsbno, bmr, n, bitmap) {
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700626 ASSERT(sc->ip != NULL ||
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700627 XFS_FSB_TO_AGNO(sc->mp, fsbno) == sc->sa.agno);
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700628 trace_xrep_dispose_btree_extent(sc->mp,
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700629 XFS_FSB_TO_AGNO(sc->mp, fsbno),
630 XFS_FSB_TO_AGBNO(sc->mp, fsbno), 1);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700631
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700632 error = xrep_reap_block(sc, fsbno, oinfo, type);
633 if (error)
634 goto out;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700635 }
636
637out:
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700638 xfs_bitmap_destroy(bitmap);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700639 return error;
640}
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700641
642/*
643 * Finding per-AG Btree Roots for AGF/AGI Reconstruction
644 *
645 * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
646 * the AG headers by using the rmap data to rummage through the AG looking for
647 * btree roots. This is not guaranteed to work if the AG is heavily damaged
648 * or the rmap data are corrupt.
649 *
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700650 * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700651 * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
652 * AGI is being rebuilt. It must maintain these locks until it's safe for
653 * other threads to change the btrees' shapes. The caller provides
654 * information about the btrees to look for by passing in an array of
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700655 * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700656 * The (root, height) fields will be set on return if anything is found. The
657 * last element of the array should have a NULL buf_ops to mark the end of the
658 * array.
659 *
660 * For every rmapbt record matching any of the rmap owners in btree_info,
661 * read each block referenced by the rmap record. If the block is a btree
662 * block from this filesystem matching any of the magic numbers and has a
663 * level higher than what we've already seen, remember the block and the
664 * height of the tree required to have such a block. When the call completes,
665 * we return the highest block we've found for each btree description; those
666 * should be the roots.
667 */
668
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700669struct xrep_findroot {
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700670 struct xfs_scrub *sc;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700671 struct xfs_buf *agfl_bp;
672 struct xfs_agf *agf;
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700673 struct xrep_find_ag_btree *btree_info;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700674};
675
676/* See if our block is in the AGFL. */
677STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700678xrep_findroot_agfl_walk(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700679 struct xfs_mount *mp,
680 xfs_agblock_t bno,
681 void *priv)
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700682{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700683 xfs_agblock_t *agbno = priv;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700684
685 return (*agbno == bno) ? XFS_BTREE_QUERY_RANGE_ABORT : 0;
686}
687
688/* Does this block match the btree information passed in? */
689STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700690xrep_findroot_block(
691 struct xrep_findroot *ri,
692 struct xrep_find_ag_btree *fab,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700693 uint64_t owner,
694 xfs_agblock_t agbno,
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100695 bool *done_with_block)
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700696{
697 struct xfs_mount *mp = ri->sc->mp;
698 struct xfs_buf *bp;
699 struct xfs_btree_block *btblock;
700 xfs_daddr_t daddr;
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100701 int block_level;
Darrick J. Wong38b62382018-10-18 17:20:35 +1100702 int error = 0;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700703
704 daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.agno, agbno);
705
706 /*
707 * Blocks in the AGFL have stale contents that might just happen to
708 * have a matching magic and uuid. We don't want to pull these blocks
709 * in as part of a tree root, so we have to filter out the AGFL stuff
710 * here. If the AGFL looks insane we'll just refuse to repair.
711 */
712 if (owner == XFS_RMAP_OWN_AG) {
713 error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700714 xrep_findroot_agfl_walk, &agbno);
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700715 if (error == XFS_BTREE_QUERY_RANGE_ABORT)
716 return 0;
717 if (error)
718 return error;
719 }
720
Darrick J. Wong38b62382018-10-18 17:20:35 +1100721 /*
722 * Read the buffer into memory so that we can see if it's a match for
723 * our btree type. We have no clue if it is beforehand, and we want to
724 * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
725 * will cause needless disk reads in subsequent calls to this function)
726 * and logging metadata verifier failures.
727 *
728 * Therefore, pass in NULL buffer ops. If the buffer was already in
729 * memory from some other caller it will already have b_ops assigned.
730 * If it was in memory from a previous unsuccessful findroot_block
731 * call, the buffer won't have b_ops but it should be clean and ready
732 * for us to try to verify if the read call succeeds. The same applies
733 * if the buffer wasn't in memory at all.
734 *
735 * Note: If we never match a btree type with this buffer, it will be
736 * left in memory with NULL b_ops. This shouldn't be a problem unless
737 * the buffer gets written.
738 */
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700739 error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
740 mp->m_bsize, 0, &bp, NULL);
741 if (error)
742 return error;
743
Darrick J. Wong38b62382018-10-18 17:20:35 +1100744 /* Ensure the block magic matches the btree type we're looking for. */
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700745 btblock = XFS_BUF_TO_BLOCK(bp);
Darrick J. Wong9228d752019-02-06 10:20:54 -0800746 ASSERT(fab->buf_ops->magic[1] != 0);
747 if (btblock->bb_magic != fab->buf_ops->magic[1])
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700748 goto out;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700749
Darrick J. Wong38b62382018-10-18 17:20:35 +1100750 /*
751 * If the buffer already has ops applied and they're not the ones for
752 * this btree type, we know this block doesn't match the btree and we
753 * can bail out.
754 *
755 * If the buffer ops match ours, someone else has already validated
756 * the block for us, so we can move on to checking if this is a root
757 * block candidate.
758 *
759 * If the buffer does not have ops, nobody has successfully validated
760 * the contents and the buffer cannot be dirty. If the magic, uuid,
761 * and structure match this btree type then we'll move on to checking
762 * if it's a root block candidate. If there is no match, bail out.
763 */
764 if (bp->b_ops) {
765 if (bp->b_ops != fab->buf_ops)
766 goto out;
767 } else {
768 ASSERT(!xfs_trans_buf_is_dirty(bp));
769 if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
770 &mp->m_sb.sb_meta_uuid))
771 goto out;
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800772 /*
773 * Read verifiers can reference b_ops, so we set the pointer
774 * here. If the verifier fails we'll reset the buffer state
775 * to what it was before we touched the buffer.
776 */
777 bp->b_ops = fab->buf_ops;
Darrick J. Wong38b62382018-10-18 17:20:35 +1100778 fab->buf_ops->verify_read(bp);
779 if (bp->b_error) {
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800780 bp->b_ops = NULL;
Darrick J. Wong38b62382018-10-18 17:20:35 +1100781 bp->b_error = 0;
782 goto out;
783 }
784
785 /*
786 * Some read verifiers will (re)set b_ops, so we must be
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800787 * careful not to change b_ops after running the verifier.
Darrick J. Wong38b62382018-10-18 17:20:35 +1100788 */
Darrick J. Wong38b62382018-10-18 17:20:35 +1100789 }
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100790
791 /*
792 * This block passes the magic/uuid and verifier tests for this btree
793 * type. We don't need the caller to try the other tree types.
794 */
795 *done_with_block = true;
796
797 /*
798 * Compare this btree block's level to the height of the current
799 * candidate root block.
800 *
801 * If the level matches the root we found previously, throw away both
802 * blocks because there can't be two candidate roots.
803 *
804 * If level is lower in the tree than the root we found previously,
805 * ignore this block.
806 */
807 block_level = xfs_btree_get_level(btblock);
808 if (block_level + 1 == fab->height) {
809 fab->root = NULLAGBLOCK;
810 goto out;
811 } else if (block_level < fab->height) {
812 goto out;
813 }
814
815 /*
816 * This is the highest block in the tree that we've found so far.
817 * Update the btree height to reflect what we've learned from this
818 * block.
819 */
820 fab->height = block_level + 1;
821
822 /*
823 * If this block doesn't have sibling pointers, then it's the new root
824 * block candidate. Otherwise, the root will be found farther up the
825 * tree.
826 */
827 if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
828 btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
829 fab->root = agbno;
830 else
831 fab->root = NULLAGBLOCK;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700832
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700833 trace_xrep_findroot_block(mp, ri->sc->sa.agno, agbno,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700834 be32_to_cpu(btblock->bb_magic), fab->height - 1);
835out:
836 xfs_trans_brelse(ri->sc->tp, bp);
837 return error;
838}
839
840/*
841 * Do any of the blocks in this rmap record match one of the btrees we're
842 * looking for?
843 */
844STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700845xrep_findroot_rmap(
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700846 struct xfs_btree_cur *cur,
847 struct xfs_rmap_irec *rec,
848 void *priv)
849{
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700850 struct xrep_findroot *ri = priv;
851 struct xrep_find_ag_btree *fab;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700852 xfs_agblock_t b;
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100853 bool done;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700854 int error = 0;
855
856 /* Ignore anything that isn't AG metadata. */
857 if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
858 return 0;
859
860 /* Otherwise scan each block + btree type. */
861 for (b = 0; b < rec->rm_blockcount; b++) {
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100862 done = false;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700863 for (fab = ri->btree_info; fab->buf_ops; fab++) {
864 if (rec->rm_owner != fab->rmap_owner)
865 continue;
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700866 error = xrep_findroot_block(ri, fab,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700867 rec->rm_owner, rec->rm_startblock + b,
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100868 &done);
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700869 if (error)
870 return error;
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100871 if (done)
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700872 break;
873 }
874 }
875
876 return 0;
877}
878
879/* Find the roots of the per-AG btrees described in btree_info. */
880int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700881xrep_find_ag_btree_roots(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700882 struct xfs_scrub *sc,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700883 struct xfs_buf *agf_bp,
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700884 struct xrep_find_ag_btree *btree_info,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700885 struct xfs_buf *agfl_bp)
886{
887 struct xfs_mount *mp = sc->mp;
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700888 struct xrep_findroot ri;
889 struct xrep_find_ag_btree *fab;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700890 struct xfs_btree_cur *cur;
891 int error;
892
893 ASSERT(xfs_buf_islocked(agf_bp));
894 ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
895
896 ri.sc = sc;
897 ri.btree_info = btree_info;
898 ri.agf = XFS_BUF_TO_AGF(agf_bp);
899 ri.agfl_bp = agfl_bp;
900 for (fab = btree_info; fab->buf_ops; fab++) {
901 ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
902 ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
903 fab->root = NULLAGBLOCK;
904 fab->height = 0;
905 }
906
907 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700908 error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700909 xfs_btree_del_cursor(cur, error);
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700910
911 return error;
912}
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700913
914/* Force a quotacheck the next time we mount. */
915void
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700916xrep_force_quotacheck(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700917 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700918 uint dqtype)
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700919{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700920 uint flag;
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700921
922 flag = xfs_quota_chkd_flag(dqtype);
923 if (!(flag & sc->mp->m_qflags))
924 return;
925
926 sc->mp->m_qflags &= ~flag;
927 spin_lock(&sc->mp->m_sb_lock);
928 sc->mp->m_sb.sb_qflags &= ~flag;
929 spin_unlock(&sc->mp->m_sb_lock);
930 xfs_log_sb(sc->tp);
931}
932
933/*
934 * Attach dquots to this inode, or schedule quotacheck to fix them.
935 *
936 * This function ensures that the appropriate dquots are attached to an inode.
937 * We cannot allow the dquot code to allocate an on-disk dquot block here
938 * because we're already in transaction context with the inode locked. The
939 * on-disk dquot should already exist anyway. If the quota code signals
940 * corruption or missing quota information, schedule quotacheck, which will
941 * repair corruptions in the quota metadata.
942 */
943int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700944xrep_ino_dqattach(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700945 struct xfs_scrub *sc)
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700946{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700947 int error;
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700948
949 error = xfs_qm_dqattach_locked(sc->ip, false);
950 switch (error) {
951 case -EFSBADCRC:
952 case -EFSCORRUPTED:
953 case -ENOENT:
954 xfs_err_ratelimited(sc->mp,
955"inode %llu repair encountered quota error %d, quotacheck forced.",
956 (unsigned long long)sc->ip->i_ino, error);
957 if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700958 xrep_force_quotacheck(sc, XFS_DQ_USER);
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700959 if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700960 xrep_force_quotacheck(sc, XFS_DQ_GROUP);
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700961 if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700962 xrep_force_quotacheck(sc, XFS_DQ_PROJ);
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700963 /* fall through */
964 case -ESRCH:
965 error = 0;
966 break;
967 default:
968 break;
969 }
970
971 return error;
972}