blob: be0bc11b65943c69f18e38a247e1ddd6b829741c [file] [log] [blame]
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -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_inode_fork.h"
34#include "xfs_alloc.h"
35#include "xfs_rtalloc.h"
36#include "xfs_bmap.h"
37#include "xfs_bmap_util.h"
38#include "xfs_bmap_btree.h"
39#include "xfs_rmap.h"
40#include "scrub/xfs_scrub.h"
41#include "scrub/scrub.h"
42#include "scrub/common.h"
43#include "scrub/btree.h"
44#include "scrub/trace.h"
45
46/* Set us up with an inode's bmap. */
47int
48xfs_scrub_setup_inode_bmap(
49 struct xfs_scrub_context *sc,
50 struct xfs_inode *ip)
51{
52 struct xfs_mount *mp = sc->mp;
53 int error;
54
55 error = xfs_scrub_get_inode(sc, ip);
56 if (error)
57 goto out;
58
59 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
60 xfs_ilock(sc->ip, sc->ilock_flags);
61
62 /*
63 * We don't want any ephemeral data fork updates sitting around
64 * while we inspect block mappings, so wait for directio to finish
65 * and flush dirty data if we have delalloc reservations.
66 */
67 if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
68 sc->sm->sm_type == XFS_SCRUB_TYPE_BMBTD) {
69 inode_dio_wait(VFS_I(sc->ip));
70 error = filemap_write_and_wait(VFS_I(sc->ip)->i_mapping);
71 if (error)
72 goto out;
73 }
74
75 /* Got the inode, lock it and we're ready to go. */
76 error = xfs_scrub_trans_alloc(sc->sm, mp, &sc->tp);
77 if (error)
78 goto out;
79 sc->ilock_flags |= XFS_ILOCK_EXCL;
80 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
81
82out:
83 /* scrub teardown will unlock and release the inode */
84 return error;
85}
86
87/*
88 * Inode fork block mapping (BMBT) scrubber.
89 * More complex than the others because we have to scrub
90 * all the extents regardless of whether or not the fork
91 * is in btree format.
92 */
93
94struct xfs_scrub_bmap_info {
95 struct xfs_scrub_context *sc;
96 xfs_fileoff_t lastoff;
97 bool is_rt;
98 bool is_shared;
99 int whichfork;
100};
101
102/* Scrub a single extent record. */
103STATIC int
104xfs_scrub_bmap_extent(
105 struct xfs_inode *ip,
106 struct xfs_btree_cur *cur,
107 struct xfs_scrub_bmap_info *info,
108 struct xfs_bmbt_irec *irec)
109{
110 struct xfs_mount *mp = info->sc->mp;
111 struct xfs_buf *bp = NULL;
112 int error = 0;
113
114 if (cur)
115 xfs_btree_get_block(cur, 0, &bp);
116
117 /*
118 * Check for out-of-order extents. This record could have come
119 * from the incore list, for which there is no ordering check.
120 */
121 if (irec->br_startoff < info->lastoff)
122 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
123 irec->br_startoff);
124
125 /* There should never be a "hole" extent in either extent list. */
126 if (irec->br_startblock == HOLESTARTBLOCK)
127 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
128 irec->br_startoff);
129
130 /*
131 * Check for delalloc extents. We never iterate the ones in the
132 * in-core extent scan, and we should never see these in the bmbt.
133 */
134 if (isnullstartblock(irec->br_startblock))
135 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
136 irec->br_startoff);
137
138 /* Make sure the extent points to a valid place. */
139 if (irec->br_startblock + irec->br_blockcount <= irec->br_startblock)
140 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
141 irec->br_startoff);
142 if (info->is_rt &&
143 (!xfs_verify_rtbno(mp, irec->br_startblock) ||
144 !xfs_verify_rtbno(mp, irec->br_startblock +
145 irec->br_blockcount - 1)))
146 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
147 irec->br_startoff);
148 if (!info->is_rt &&
149 (!xfs_verify_fsbno(mp, irec->br_startblock) ||
150 !xfs_verify_fsbno(mp, irec->br_startblock +
151 irec->br_blockcount - 1)))
152 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
153 irec->br_startoff);
154
155 /* We don't allow unwritten extents on attr forks. */
156 if (irec->br_state == XFS_EXT_UNWRITTEN &&
157 info->whichfork == XFS_ATTR_FORK)
158 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
159 irec->br_startoff);
160
161 info->lastoff = irec->br_startoff + irec->br_blockcount;
162 return error;
163}
164
165/* Scrub a bmbt record. */
166STATIC int
167xfs_scrub_bmapbt_rec(
168 struct xfs_scrub_btree *bs,
169 union xfs_btree_rec *rec)
170{
171 struct xfs_bmbt_rec_host ihost;
172 struct xfs_bmbt_irec irec;
173 struct xfs_scrub_bmap_info *info = bs->private;
174 struct xfs_inode *ip = bs->cur->bc_private.b.ip;
175 struct xfs_buf *bp = NULL;
176 struct xfs_btree_block *block;
177 uint64_t owner;
178 int i;
179
180 /*
181 * Check the owners of the btree blocks up to the level below
182 * the root since the verifiers don't do that.
183 */
184 if (xfs_sb_version_hascrc(&bs->cur->bc_mp->m_sb) &&
185 bs->cur->bc_ptrs[0] == 1) {
186 for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
187 block = xfs_btree_get_block(bs->cur, i, &bp);
188 owner = be64_to_cpu(block->bb_u.l.bb_owner);
189 if (owner != ip->i_ino)
190 xfs_scrub_fblock_set_corrupt(bs->sc,
191 info->whichfork, 0);
192 }
193 }
194
195 /* Set up the in-core record and scrub it. */
196 ihost.l0 = be64_to_cpu(rec->bmbt.l0);
197 ihost.l1 = be64_to_cpu(rec->bmbt.l1);
198 xfs_bmbt_get_all(&ihost, &irec);
199 return xfs_scrub_bmap_extent(ip, bs->cur, info, &irec);
200}
201
202/* Scan the btree records. */
203STATIC int
204xfs_scrub_bmap_btree(
205 struct xfs_scrub_context *sc,
206 int whichfork,
207 struct xfs_scrub_bmap_info *info)
208{
209 struct xfs_owner_info oinfo;
210 struct xfs_mount *mp = sc->mp;
211 struct xfs_inode *ip = sc->ip;
212 struct xfs_btree_cur *cur;
213 int error;
214
215 cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
216 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
217 error = xfs_scrub_btree(sc, cur, xfs_scrub_bmapbt_rec, &oinfo, info);
218 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR :
219 XFS_BTREE_NOERROR);
220 return error;
221}
222
223/*
224 * Scrub an inode fork's block mappings.
225 *
226 * First we scan every record in every btree block, if applicable.
227 * Then we unconditionally scan the incore extent cache.
228 */
229STATIC int
230xfs_scrub_bmap(
231 struct xfs_scrub_context *sc,
232 int whichfork)
233{
234 struct xfs_bmbt_irec irec;
235 struct xfs_scrub_bmap_info info = {0};
236 struct xfs_mount *mp = sc->mp;
237 struct xfs_inode *ip = sc->ip;
238 struct xfs_ifork *ifp;
239 xfs_fileoff_t endoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700240 struct xfs_iext_cursor icur;
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700241 bool found;
242 int error = 0;
243
244 ifp = XFS_IFORK_PTR(ip, whichfork);
245
246 info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
247 info.whichfork = whichfork;
248 info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
249 info.sc = sc;
250
251 switch (whichfork) {
252 case XFS_COW_FORK:
253 /* Non-existent CoW forks are ignorable. */
254 if (!ifp)
255 goto out;
256 /* No CoW forks on non-reflink inodes/filesystems. */
257 if (!xfs_is_reflink_inode(ip)) {
258 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino, NULL);
259 goto out;
260 }
261 break;
262 case XFS_ATTR_FORK:
263 if (!ifp)
264 goto out;
265 if (!xfs_sb_version_hasattr(&mp->m_sb) &&
266 !xfs_sb_version_hasattr2(&mp->m_sb))
267 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino, NULL);
268 break;
269 default:
270 ASSERT(whichfork == XFS_DATA_FORK);
271 break;
272 }
273
274 /* Check the fork values */
275 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
276 case XFS_DINODE_FMT_UUID:
277 case XFS_DINODE_FMT_DEV:
278 case XFS_DINODE_FMT_LOCAL:
279 /* No mappings to check. */
280 goto out;
281 case XFS_DINODE_FMT_EXTENTS:
282 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
283 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
284 goto out;
285 }
286 break;
287 case XFS_DINODE_FMT_BTREE:
288 if (whichfork == XFS_COW_FORK) {
289 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
290 goto out;
291 }
292
293 error = xfs_scrub_bmap_btree(sc, whichfork, &info);
294 if (error)
295 goto out;
296 break;
297 default:
298 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
299 goto out;
300 }
301
302 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
303 goto out;
304
305 /* Now try to scrub the in-memory extent list. */
306 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
307 error = xfs_iread_extents(sc->tp, ip, whichfork);
308 if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
309 goto out;
310 }
311
312 /* Find the offset of the last extent in the mapping. */
313 error = xfs_bmap_last_offset(ip, &endoff, whichfork);
314 if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
315 goto out;
316
317 /* Scrub extent records. */
318 info.lastoff = 0;
319 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700320 for (found = xfs_iext_lookup_extent(ip, ifp, 0, &icur, &irec);
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700321 found != 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700322 found = xfs_iext_next_extent(ifp, &icur, &irec)) {
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700323 if (xfs_scrub_should_terminate(sc, &error))
324 break;
325 if (isnullstartblock(irec.br_startblock))
326 continue;
327 if (irec.br_startoff >= endoff) {
328 xfs_scrub_fblock_set_corrupt(sc, whichfork,
329 irec.br_startoff);
330 goto out;
331 }
332 error = xfs_scrub_bmap_extent(ip, NULL, &info, &irec);
333 if (error)
334 goto out;
335 }
336
337out:
338 return error;
339}
340
341/* Scrub an inode's data fork. */
342int
343xfs_scrub_bmap_data(
344 struct xfs_scrub_context *sc)
345{
346 return xfs_scrub_bmap(sc, XFS_DATA_FORK);
347}
348
349/* Scrub an inode's attr fork. */
350int
351xfs_scrub_bmap_attr(
352 struct xfs_scrub_context *sc)
353{
354 return xfs_scrub_bmap(sc, XFS_ATTR_FORK);
355}
356
357/* Scrub an inode's CoW fork. */
358int
359xfs_scrub_bmap_cow(
360 struct xfs_scrub_context *sc)
361{
362 if (!xfs_is_reflink_inode(sc->ip))
363 return -ENOENT;
364
365 return xfs_scrub_bmap(sc, XFS_COW_FORK);
366}