blob: 0261e1133901c9778c121cb6fc4dfe669b7594a9 [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{
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700171 struct xfs_bmbt_irec irec;
172 struct xfs_scrub_bmap_info *info = bs->private;
173 struct xfs_inode *ip = bs->cur->bc_private.b.ip;
174 struct xfs_buf *bp = NULL;
175 struct xfs_btree_block *block;
176 uint64_t owner;
177 int i;
178
179 /*
180 * Check the owners of the btree blocks up to the level below
181 * the root since the verifiers don't do that.
182 */
183 if (xfs_sb_version_hascrc(&bs->cur->bc_mp->m_sb) &&
184 bs->cur->bc_ptrs[0] == 1) {
185 for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
186 block = xfs_btree_get_block(bs->cur, i, &bp);
187 owner = be64_to_cpu(block->bb_u.l.bb_owner);
188 if (owner != ip->i_ino)
189 xfs_scrub_fblock_set_corrupt(bs->sc,
190 info->whichfork, 0);
191 }
192 }
193
194 /* Set up the in-core record and scrub it. */
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700195 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700196 return xfs_scrub_bmap_extent(ip, bs->cur, info, &irec);
197}
198
199/* Scan the btree records. */
200STATIC int
201xfs_scrub_bmap_btree(
202 struct xfs_scrub_context *sc,
203 int whichfork,
204 struct xfs_scrub_bmap_info *info)
205{
206 struct xfs_owner_info oinfo;
207 struct xfs_mount *mp = sc->mp;
208 struct xfs_inode *ip = sc->ip;
209 struct xfs_btree_cur *cur;
210 int error;
211
212 cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
213 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
214 error = xfs_scrub_btree(sc, cur, xfs_scrub_bmapbt_rec, &oinfo, info);
215 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR :
216 XFS_BTREE_NOERROR);
217 return error;
218}
219
220/*
221 * Scrub an inode fork's block mappings.
222 *
223 * First we scan every record in every btree block, if applicable.
224 * Then we unconditionally scan the incore extent cache.
225 */
226STATIC int
227xfs_scrub_bmap(
228 struct xfs_scrub_context *sc,
229 int whichfork)
230{
231 struct xfs_bmbt_irec irec;
Christoph Hellwig88aa5de2017-11-06 11:53:58 -0800232 struct xfs_scrub_bmap_info info = { NULL };
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700233 struct xfs_mount *mp = sc->mp;
234 struct xfs_inode *ip = sc->ip;
235 struct xfs_ifork *ifp;
236 xfs_fileoff_t endoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700237 struct xfs_iext_cursor icur;
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700238 int error = 0;
239
240 ifp = XFS_IFORK_PTR(ip, whichfork);
241
242 info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
243 info.whichfork = whichfork;
244 info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
245 info.sc = sc;
246
247 switch (whichfork) {
248 case XFS_COW_FORK:
249 /* Non-existent CoW forks are ignorable. */
250 if (!ifp)
251 goto out;
252 /* No CoW forks on non-reflink inodes/filesystems. */
253 if (!xfs_is_reflink_inode(ip)) {
254 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino, NULL);
255 goto out;
256 }
257 break;
258 case XFS_ATTR_FORK:
259 if (!ifp)
260 goto out;
261 if (!xfs_sb_version_hasattr(&mp->m_sb) &&
262 !xfs_sb_version_hasattr2(&mp->m_sb))
263 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino, NULL);
264 break;
265 default:
266 ASSERT(whichfork == XFS_DATA_FORK);
267 break;
268 }
269
270 /* Check the fork values */
271 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
272 case XFS_DINODE_FMT_UUID:
273 case XFS_DINODE_FMT_DEV:
274 case XFS_DINODE_FMT_LOCAL:
275 /* No mappings to check. */
276 goto out;
277 case XFS_DINODE_FMT_EXTENTS:
278 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
279 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
280 goto out;
281 }
282 break;
283 case XFS_DINODE_FMT_BTREE:
284 if (whichfork == XFS_COW_FORK) {
285 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
286 goto out;
287 }
288
289 error = xfs_scrub_bmap_btree(sc, whichfork, &info);
290 if (error)
291 goto out;
292 break;
293 default:
294 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
295 goto out;
296 }
297
298 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
299 goto out;
300
301 /* Now try to scrub the in-memory extent list. */
302 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
303 error = xfs_iread_extents(sc->tp, ip, whichfork);
304 if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
305 goto out;
306 }
307
308 /* Find the offset of the last extent in the mapping. */
309 error = xfs_bmap_last_offset(ip, &endoff, whichfork);
310 if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
311 goto out;
312
313 /* Scrub extent records. */
314 info.lastoff = 0;
315 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong2b9e9b52018-01-08 10:49:03 -0800316 for_each_xfs_iext(ifp, &icur, &irec) {
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700317 if (xfs_scrub_should_terminate(sc, &error))
318 break;
319 if (isnullstartblock(irec.br_startblock))
320 continue;
321 if (irec.br_startoff >= endoff) {
322 xfs_scrub_fblock_set_corrupt(sc, whichfork,
323 irec.br_startoff);
324 goto out;
325 }
326 error = xfs_scrub_bmap_extent(ip, NULL, &info, &irec);
327 if (error)
328 goto out;
329 }
330
331out:
332 return error;
333}
334
335/* Scrub an inode's data fork. */
336int
337xfs_scrub_bmap_data(
338 struct xfs_scrub_context *sc)
339{
340 return xfs_scrub_bmap(sc, XFS_DATA_FORK);
341}
342
343/* Scrub an inode's attr fork. */
344int
345xfs_scrub_bmap_attr(
346 struct xfs_scrub_context *sc)
347{
348 return xfs_scrub_bmap(sc, XFS_ATTR_FORK);
349}
350
351/* Scrub an inode's CoW fork. */
352int
353xfs_scrub_bmap_cow(
354 struct xfs_scrub_context *sc)
355{
356 if (!xfs_is_reflink_inode(sc->ip))
357 return -ENOENT;
358
359 return xfs_scrub_bmap(sc, XFS_COW_FORK);
360}