Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
4 : * Author: Darrick J. Wong <djwong@kernel.org>
5 : */
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_log_format.h"
13 : #include "xfs_trans.h"
14 : #include "xfs_inode.h"
15 : #include "xfs_icache.h"
16 : #include "xfs_dir2.h"
17 : #include "xfs_dir2_priv.h"
18 : #include "scrub/scrub.h"
19 : #include "scrub/common.h"
20 : #include "scrub/dabtree.h"
21 : #include "scrub/readdir.h"
22 :
23 : /* Set us up to scrub directories. */
24 : int
25 79705593 : xchk_setup_directory(
26 : struct xfs_scrub *sc)
27 : {
28 79705593 : return xchk_setup_inode_contents(sc, 0);
29 : }
30 :
31 : /* Directories */
32 :
33 : /* Scrub a directory entry. */
34 :
35 : /* Check that an inode's mode matches a given XFS_DIR3_FT_* type. */
36 : STATIC void
37 535148797 : xchk_dir_check_ftype(
38 : struct xfs_scrub *sc,
39 : xfs_fileoff_t offset,
40 : struct xfs_inode *ip,
41 : int ftype)
42 : {
43 535148797 : struct xfs_mount *mp = sc->mp;
44 :
45 535148797 : if (!xfs_has_ftype(mp)) {
46 0 : if (ftype != XFS_DIR3_FT_UNKNOWN && ftype != XFS_DIR3_FT_DIR)
47 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
48 0 : return;
49 : }
50 :
51 535148797 : if (xfs_mode_to_ftype(VFS_I(ip)->i_mode) != ftype)
52 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
53 : }
54 :
55 : /*
56 : * Scrub a single directory entry.
57 : *
58 : * Check the inode number to make sure it's sane, then we check that we can
59 : * look up this filename. Finally, we check the ftype.
60 : */
61 : STATIC int
62 535095695 : xchk_dir_actor(
63 : struct xfs_scrub *sc,
64 : struct xfs_inode *dp,
65 : xfs_dir2_dataptr_t dapos,
66 : const struct xfs_name *name,
67 : xfs_ino_t ino,
68 : void *priv)
69 : {
70 535095695 : struct xfs_mount *mp = dp->i_mount;
71 535095695 : struct xfs_inode *ip;
72 535095695 : xfs_ino_t lookup_ino;
73 535095695 : xfs_dablk_t offset;
74 535095695 : int error = 0;
75 :
76 535095695 : offset = xfs_dir2_db_to_da(mp->m_dir_geo,
77 : xfs_dir2_dataptr_to_db(mp->m_dir_geo, dapos));
78 :
79 535148066 : if (xchk_should_terminate(sc, &error))
80 3 : return error;
81 :
82 : /* Does this inode number make sense? */
83 535152188 : if (!xfs_verify_dir_ino(mp, ino)) {
84 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
85 0 : return -ECANCELED;
86 : }
87 :
88 : /* Does this name make sense? */
89 535160040 : if (!xfs_dir2_namecheck(name->name, name->len)) {
90 2 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
91 2 : return -ECANCELED;
92 : }
93 :
94 535163094 : if (!strncmp(".", name->name, name->len)) {
95 : /* If this is "." then check that the inum matches the dir. */
96 79708573 : if (ino != dp->i_ino)
97 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
98 455454521 : } else if (!strncmp("..", name->name, name->len)) {
99 : /*
100 : * If this is ".." in the root inode, check that the inum
101 : * matches this dir.
102 : */
103 79707565 : if (dp->i_ino == mp->m_sb.sb_rootino && ino != dp->i_ino)
104 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
105 : }
106 :
107 : /* Verify that we can look up this name by hash. */
108 535163094 : error = xchk_dir_lookup(sc, dp, name, &lookup_ino);
109 : /* ENOENT means the hash lookup failed and the dir is corrupt */
110 535128346 : if (error == -ENOENT)
111 0 : error = -EFSCORRUPTED;
112 535128346 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, offset, &error))
113 0 : goto out;
114 535133923 : if (lookup_ino != ino) {
115 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
116 0 : return -ECANCELED;
117 : }
118 :
119 : /*
120 : * Grab the inode pointed to by the dirent. We release the inode
121 : * before we cancel the scrub transaction.
122 : *
123 : * If _iget returns -EINVAL or -ENOENT then the child inode number is
124 : * garbage and the directory is corrupt. If the _iget returns
125 : * -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
126 : * cross referencing error. Any other error is an operational error.
127 : */
128 535133923 : error = xchk_iget(sc, ino, &ip);
129 535159542 : if (error == -EINVAL || error == -ENOENT) {
130 0 : error = -EFSCORRUPTED;
131 0 : xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
132 0 : goto out;
133 : }
134 535159542 : if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset, &error))
135 0 : goto out;
136 :
137 535163038 : xchk_dir_check_ftype(sc, offset, ip, name->type);
138 535154826 : xchk_irele(sc, ip);
139 535165373 : out:
140 535165373 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
141 : return -ECANCELED;
142 535165373 : return error;
143 : }
144 :
145 : /* Scrub a directory btree record. */
146 : STATIC int
147 137223058 : xchk_dir_rec(
148 : struct xchk_da_btree *ds,
149 : int level)
150 : {
151 137223058 : struct xfs_name dname = { };
152 137223058 : struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
153 137223010 : struct xfs_mount *mp = ds->state->mp;
154 137223010 : struct xfs_inode *dp = ds->dargs.dp;
155 137223010 : struct xfs_da_geometry *geo = mp->m_dir_geo;
156 137223010 : struct xfs_dir2_data_entry *dent;
157 137223010 : struct xfs_buf *bp;
158 137223010 : struct xfs_dir2_leaf_entry *ent;
159 137223010 : unsigned int end;
160 137223010 : unsigned int iter_off;
161 137223010 : xfs_ino_t ino;
162 137223010 : xfs_dablk_t rec_bno;
163 137223010 : xfs_dir2_db_t db;
164 137223010 : xfs_dir2_data_aoff_t off;
165 137223010 : xfs_dir2_dataptr_t ptr;
166 137223010 : xfs_dahash_t calc_hash;
167 137223010 : xfs_dahash_t hash;
168 137223010 : struct xfs_dir3_icleaf_hdr hdr;
169 137223010 : unsigned int tag;
170 137223010 : int error;
171 :
172 137223010 : ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
173 : blk->magic == XFS_DIR2_LEAFN_MAGIC);
174 :
175 137223010 : xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
176 137223082 : ent = hdr.ents + blk->index;
177 :
178 : /* Check the hash of the entry. */
179 137223082 : error = xchk_da_btree_hash(ds, level, &ent->hashval);
180 137224109 : if (error)
181 0 : goto out;
182 :
183 : /* Valid hash pointer? */
184 137224109 : ptr = be32_to_cpu(ent->address);
185 137224109 : if (ptr == 0)
186 : return 0;
187 :
188 : /* Find the directory entry's location. */
189 114333167 : db = xfs_dir2_dataptr_to_db(geo, ptr);
190 114333471 : off = xfs_dir2_dataptr_to_off(geo, ptr);
191 114333471 : rec_bno = xfs_dir2_db_to_da(geo, db);
192 :
193 114333475 : if (rec_bno >= geo->leafblk) {
194 0 : xchk_da_set_corrupt(ds, level);
195 0 : goto out;
196 : }
197 114333475 : error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno,
198 : XFS_DABUF_MAP_HOLE_OK, &bp);
199 114333527 : if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
200 : &error))
201 0 : goto out;
202 114332833 : if (!bp) {
203 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
204 0 : goto out;
205 : }
206 114332833 : xchk_buffer_recheck(ds->sc, bp);
207 :
208 114329835 : if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
209 0 : goto out_relse;
210 :
211 114329835 : dent = bp->b_addr + off;
212 :
213 : /* Make sure we got a real directory entry. */
214 114329835 : iter_off = geo->data_entry_offset;
215 114329835 : end = xfs_dir3_data_end_offset(geo, bp->b_addr);
216 114320760 : if (!end) {
217 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
218 0 : goto out_relse;
219 : }
220 11381368091 : for (;;) {
221 11381368091 : struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off;
222 11381368091 : struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off;
223 :
224 11381368091 : if (iter_off >= end) {
225 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
226 0 : goto out_relse;
227 : }
228 :
229 11381368091 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
230 686866459 : iter_off += be16_to_cpu(dup->length);
231 686866459 : continue;
232 : }
233 10694501632 : if (dep == dent)
234 : break;
235 21187920824 : iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
236 : }
237 :
238 : /* Retrieve the entry, sanity check it, and compare hashes. */
239 114320760 : ino = be64_to_cpu(dent->inumber);
240 114320760 : hash = be32_to_cpu(ent->hashval);
241 114320760 : tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
242 114326561 : if (!xfs_verify_dir_ino(mp, ino) || tag != off)
243 553 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
244 114330221 : if (dent->namelen == 0) {
245 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
246 0 : goto out_relse;
247 : }
248 :
249 : /* Does the directory hash match? */
250 114330221 : dname.name = dent->name;
251 114330221 : dname.len = dent->namelen;
252 114330221 : calc_hash = xfs_dir2_hashname(mp, &dname);
253 114330824 : if (calc_hash != hash)
254 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
255 :
256 114330824 : out_relse:
257 114330824 : xfs_trans_brelse(ds->dargs.trans, bp);
258 114329958 : out:
259 114329958 : return error;
260 : }
261 :
262 : /*
263 : * Is this unused entry either in the bestfree or smaller than all of
264 : * them? We've already checked that the bestfrees are sorted longest to
265 : * shortest, and that there aren't any bogus entries.
266 : */
267 : STATIC void
268 9459915 : xchk_directory_check_free_entry(
269 : struct xfs_scrub *sc,
270 : xfs_dablk_t lblk,
271 : struct xfs_dir2_data_free *bf,
272 : struct xfs_dir2_data_unused *dup)
273 : {
274 9459915 : struct xfs_dir2_data_free *dfp;
275 9459915 : unsigned int dup_length;
276 :
277 9459915 : dup_length = be16_to_cpu(dup->length);
278 :
279 : /* Unused entry is shorter than any of the bestfrees */
280 9459915 : if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
281 : return;
282 :
283 7114671 : for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
284 7114671 : if (dup_length == be16_to_cpu(dfp->length))
285 : return;
286 :
287 : /* Unused entry should be in the bestfrees but wasn't found. */
288 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
289 : }
290 :
291 : /* Check free space info in a directory data block. */
292 : STATIC int
293 808163 : xchk_directory_data_bestfree(
294 : struct xfs_scrub *sc,
295 : xfs_dablk_t lblk,
296 : bool is_block)
297 : {
298 808163 : struct xfs_dir2_data_unused *dup;
299 808163 : struct xfs_dir2_data_free *dfp;
300 808163 : struct xfs_buf *bp;
301 808163 : struct xfs_dir2_data_free *bf;
302 808163 : struct xfs_mount *mp = sc->mp;
303 808163 : u16 tag;
304 808163 : unsigned int nr_bestfrees = 0;
305 808163 : unsigned int nr_frees = 0;
306 808163 : unsigned int smallest_bestfree;
307 808163 : int newlen;
308 808163 : unsigned int offset;
309 808163 : unsigned int end;
310 808163 : int error;
311 :
312 808163 : if (is_block) {
313 : /* dir block format */
314 134379 : if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
315 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
316 134379 : error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
317 : } else {
318 : /* dir data format */
319 673784 : error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, 0, &bp);
320 : }
321 808165 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
322 0 : goto out;
323 808152 : xchk_buffer_recheck(sc, bp);
324 :
325 : /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
326 :
327 808163 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
328 0 : goto out_buf;
329 :
330 : /* Do the bestfrees correspond to actual free space? */
331 808163 : bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
332 808163 : smallest_bestfree = UINT_MAX;
333 4040816 : for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
334 2424490 : offset = be16_to_cpu(dfp->offset);
335 2424490 : if (offset == 0)
336 1011296 : continue;
337 1413194 : if (offset >= mp->m_dir_geo->blksize) {
338 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
339 0 : goto out_buf;
340 : }
341 1413194 : dup = bp->b_addr + offset;
342 1413194 : tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
343 :
344 : /* bestfree doesn't match the entry it points at? */
345 2826388 : if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
346 1413194 : be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
347 1413194 : tag != offset) {
348 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
349 0 : goto out_buf;
350 : }
351 :
352 : /* bestfree records should be ordered largest to smallest */
353 1413194 : if (smallest_bestfree < be16_to_cpu(dfp->length)) {
354 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
355 0 : goto out_buf;
356 : }
357 :
358 1413194 : smallest_bestfree = be16_to_cpu(dfp->length);
359 1413194 : nr_bestfrees++;
360 : }
361 :
362 : /* Make sure the bestfrees are actually the best free spaces. */
363 808163 : offset = mp->m_dir_geo->data_entry_offset;
364 808163 : end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
365 :
366 : /* Iterate the entries, stopping when we hit or go past the end. */
367 127571881 : while (offset < end) {
368 126763718 : dup = bp->b_addr + offset;
369 :
370 : /* Skip real entries */
371 126763718 : if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
372 117303808 : struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
373 :
374 117303808 : newlen = xfs_dir2_data_entsize(mp, dep->namelen);
375 117303808 : if (newlen <= 0) {
376 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
377 : lblk);
378 : goto out_buf;
379 : }
380 117303808 : offset += newlen;
381 117303808 : continue;
382 : }
383 :
384 : /* Spot check this free entry */
385 9459910 : tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
386 9459910 : if (tag != offset) {
387 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
388 0 : goto out_buf;
389 : }
390 :
391 : /*
392 : * Either this entry is a bestfree or it's smaller than
393 : * any of the bestfrees.
394 : */
395 9459910 : xchk_directory_check_free_entry(sc, lblk, bf, dup);
396 9459910 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
397 0 : goto out_buf;
398 :
399 : /* Move on. */
400 9459910 : newlen = be16_to_cpu(dup->length);
401 9459910 : if (newlen <= 0) {
402 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
403 0 : goto out_buf;
404 : }
405 9459910 : offset += newlen;
406 9459910 : if (offset <= end)
407 9459911 : nr_frees++;
408 : }
409 :
410 : /* We're required to fill all the space. */
411 808163 : if (offset != end)
412 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
413 :
414 : /* Did we see at least as many free slots as there are bestfrees? */
415 808163 : if (nr_frees < nr_bestfrees)
416 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
417 808163 : out_buf:
418 808163 : xfs_trans_brelse(sc->tp, bp);
419 808165 : out:
420 808165 : return error;
421 : }
422 :
423 : /*
424 : * Does the free space length in the free space index block ($len) match
425 : * the longest length in the directory data block's bestfree array?
426 : * Assume that we've already checked that the data block's bestfree
427 : * array is in order.
428 : */
429 : STATIC void
430 673786 : xchk_directory_check_freesp(
431 : struct xfs_scrub *sc,
432 : xfs_dablk_t lblk,
433 : struct xfs_buf *dbp,
434 : unsigned int len)
435 : {
436 673786 : struct xfs_dir2_data_free *dfp;
437 :
438 673786 : dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
439 :
440 673786 : if (len != be16_to_cpu(dfp->length))
441 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
442 :
443 673786 : if (len > 0 && be16_to_cpu(dfp->offset) == 0)
444 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
445 673786 : }
446 :
447 : /* Check free space info in a directory leaf1 block. */
448 : STATIC int
449 11316 : xchk_directory_leaf1_bestfree(
450 : struct xfs_scrub *sc,
451 : struct xfs_da_args *args,
452 : xfs_dir2_db_t last_data_db,
453 : xfs_dablk_t lblk)
454 : {
455 11316 : struct xfs_dir3_icleaf_hdr leafhdr;
456 11316 : struct xfs_dir2_leaf_tail *ltp;
457 11316 : struct xfs_dir2_leaf *leaf;
458 11316 : struct xfs_buf *dbp;
459 11316 : struct xfs_buf *bp;
460 11316 : struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
461 11316 : __be16 *bestp;
462 11316 : __u16 best;
463 11316 : __u32 hash;
464 11316 : __u32 lasthash = 0;
465 11316 : __u32 bestcount;
466 11316 : unsigned int stale = 0;
467 11316 : int i;
468 11316 : int error;
469 :
470 : /* Read the free space block. */
471 11316 : error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, &bp);
472 11316 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
473 0 : return error;
474 11316 : xchk_buffer_recheck(sc, bp);
475 :
476 11316 : leaf = bp->b_addr;
477 11316 : xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
478 11316 : ltp = xfs_dir2_leaf_tail_p(geo, leaf);
479 11316 : bestcount = be32_to_cpu(ltp->bestcount);
480 11316 : bestp = xfs_dir2_leaf_bests_p(ltp);
481 :
482 11316 : if (xfs_has_crc(sc->mp)) {
483 11316 : struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
484 :
485 11316 : if (hdr3->pad != cpu_to_be32(0))
486 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
487 : }
488 :
489 : /*
490 : * There must be enough bestfree slots to cover all the directory data
491 : * blocks that we scanned. It is possible for there to be a hole
492 : * between the last data block and i_disk_size. This seems like an
493 : * oversight to the scrub author, but as we have been writing out
494 : * directories like this (and xfs_repair doesn't mind them) for years,
495 : * that's what we have to check.
496 : */
497 11316 : if (bestcount != last_data_db + 1) {
498 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
499 0 : goto out;
500 : }
501 :
502 : /* Is the leaf count even remotely sane? */
503 11316 : if (leafhdr.count > geo->leaf_max_ents) {
504 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
505 0 : goto out;
506 : }
507 :
508 : /* Leaves and bests don't overlap in leaf format. */
509 11316 : if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
510 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
511 0 : goto out;
512 : }
513 :
514 : /* Check hash value order, count stale entries. */
515 3427794 : for (i = 0; i < leafhdr.count; i++) {
516 3416478 : hash = be32_to_cpu(leafhdr.ents[i].hashval);
517 3416478 : if (i > 0 && lasthash > hash)
518 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
519 3416478 : lasthash = hash;
520 3416478 : if (leafhdr.ents[i].address ==
521 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
522 710750 : stale++;
523 : }
524 11316 : if (leafhdr.stale != stale)
525 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
526 11316 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
527 0 : goto out;
528 :
529 : /* Check all the bestfree entries. */
530 33472 : for (i = 0; i < bestcount; i++, bestp++) {
531 22156 : best = be16_to_cpu(*bestp);
532 22156 : error = xfs_dir3_data_read(sc->tp, sc->ip,
533 : xfs_dir2_db_to_da(args->geo, i),
534 : XFS_DABUF_MAP_HOLE_OK,
535 : &dbp);
536 22156 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
537 : &error))
538 : break;
539 :
540 22156 : if (!dbp) {
541 267 : if (best != NULLDATAOFF) {
542 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
543 : lblk);
544 0 : break;
545 : }
546 267 : continue;
547 : }
548 :
549 21889 : if (best == NULLDATAOFF)
550 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
551 : else
552 21889 : xchk_directory_check_freesp(sc, lblk, dbp, best);
553 21889 : xfs_trans_brelse(sc->tp, dbp);
554 21889 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
555 : break;
556 : }
557 11316 : out:
558 11316 : xfs_trans_brelse(sc->tp, bp);
559 11316 : return error;
560 : }
561 :
562 : /* Check free space info in a directory freespace block. */
563 : STATIC int
564 40452 : xchk_directory_free_bestfree(
565 : struct xfs_scrub *sc,
566 : struct xfs_da_args *args,
567 : xfs_dablk_t lblk)
568 : {
569 40452 : struct xfs_dir3_icfree_hdr freehdr;
570 40452 : struct xfs_buf *dbp;
571 40452 : struct xfs_buf *bp;
572 40452 : __u16 best;
573 40452 : unsigned int stale = 0;
574 40452 : int i;
575 40452 : int error;
576 :
577 : /* Read the free space block */
578 40452 : error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
579 40452 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
580 0 : return error;
581 40452 : xchk_buffer_recheck(sc, bp);
582 :
583 40452 : if (xfs_has_crc(sc->mp)) {
584 40452 : struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
585 :
586 40452 : if (hdr3->pad != cpu_to_be32(0))
587 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
588 : }
589 :
590 : /* Check all the entries. */
591 40452 : xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
592 743406 : for (i = 0; i < freehdr.nvalid; i++) {
593 662502 : best = be16_to_cpu(freehdr.bests[i]);
594 662502 : if (best == NULLDATAOFF) {
595 10605 : stale++;
596 10605 : continue;
597 : }
598 1303794 : error = xfs_dir3_data_read(sc->tp, sc->ip,
599 651897 : (freehdr.firstdb + i) * args->geo->fsbcount,
600 : 0, &dbp);
601 651897 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
602 : &error))
603 0 : goto out;
604 651897 : xchk_directory_check_freesp(sc, lblk, dbp, best);
605 651897 : xfs_trans_brelse(sc->tp, dbp);
606 : }
607 :
608 40452 : if (freehdr.nused + stale != freehdr.nvalid)
609 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
610 40452 : out:
611 40452 : xfs_trans_brelse(sc->tp, bp);
612 40452 : return error;
613 : }
614 :
615 : /* Check free space information in directories. */
616 : STATIC int
617 79703132 : xchk_directory_blocks(
618 : struct xfs_scrub *sc)
619 : {
620 79703132 : struct xfs_bmbt_irec got;
621 79703132 : struct xfs_da_args args = {
622 79703132 : .dp = sc ->ip,
623 : .whichfork = XFS_DATA_FORK,
624 79703132 : .geo = sc->mp->m_dir_geo,
625 79703132 : .trans = sc->tp,
626 : };
627 79703132 : struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
628 79703132 : struct xfs_mount *mp = sc->mp;
629 79703132 : xfs_fileoff_t leaf_lblk;
630 79703132 : xfs_fileoff_t free_lblk;
631 79703132 : xfs_fileoff_t lblk;
632 79703132 : struct xfs_iext_cursor icur;
633 79703132 : xfs_dablk_t dabno;
634 79703132 : xfs_dir2_db_t last_data_db = 0;
635 79703132 : bool found;
636 79703132 : bool is_block = false;
637 79703132 : int error;
638 :
639 : /* Ignore local format directories. */
640 79703132 : if (ifp->if_format != XFS_DINODE_FMT_EXTENTS &&
641 : ifp->if_format != XFS_DINODE_FMT_BTREE)
642 : return 0;
643 :
644 186144 : lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
645 186144 : leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
646 186144 : free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
647 :
648 : /* Is this a block dir? */
649 186144 : error = xfs_dir2_isblock(&args, &is_block);
650 186144 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
651 0 : goto out;
652 :
653 : /* Iterate all the data extents in the directory... */
654 186144 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
655 882205 : while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
656 : /* No more data blocks... */
657 747826 : if (got.br_startoff >= leaf_lblk)
658 : break;
659 :
660 : /*
661 : * Check each data block's bestfree data.
662 : *
663 : * Iterate all the fsbcount-aligned block offsets in
664 : * this directory. The directory block reading code is
665 : * smart enough to do its own bmap lookups to handle
666 : * discontiguous directory blocks. When we're done
667 : * with the extent record, re-query the bmap at the
668 : * next fsbcount-aligned offset to avoid redundant
669 : * block checks.
670 : */
671 696061 : for (lblk = roundup((xfs_dablk_t)got.br_startoff,
672 : args.geo->fsbcount);
673 1504226 : lblk < got.br_startoff + got.br_blockcount;
674 808165 : lblk += args.geo->fsbcount) {
675 808164 : last_data_db = xfs_dir2_da_to_db(args.geo, lblk);
676 808164 : error = xchk_directory_data_bestfree(sc, lblk,
677 : is_block);
678 808165 : if (error)
679 0 : goto out;
680 : }
681 696062 : dabno = got.br_startoff + got.br_blockcount;
682 696062 : lblk = roundup(dabno, args.geo->fsbcount);
683 696062 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
684 : }
685 :
686 186144 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
687 0 : goto out;
688 :
689 : /* Look for a leaf1 block, which has free info. */
690 186144 : if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
691 51765 : got.br_startoff == leaf_lblk &&
692 103530 : got.br_blockcount == args.geo->fsbcount &&
693 51765 : !xfs_iext_next_extent(ifp, &icur, &got)) {
694 11316 : if (is_block) {
695 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
696 0 : goto out;
697 : }
698 11316 : error = xchk_directory_leaf1_bestfree(sc, &args, last_data_db,
699 : leaf_lblk);
700 11316 : if (error)
701 0 : goto out;
702 : }
703 :
704 186143 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
705 0 : goto out;
706 :
707 : /* Scan for free blocks */
708 186143 : lblk = free_lblk;
709 186143 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
710 226595 : while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
711 : /*
712 : * Dirs can't have blocks mapped above 2^32.
713 : * Single-block dirs shouldn't even be here.
714 : */
715 40452 : lblk = got.br_startoff;
716 40452 : if (lblk & ~0xFFFFFFFFULL) {
717 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
718 0 : goto out;
719 : }
720 40452 : if (is_block) {
721 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
722 0 : goto out;
723 : }
724 :
725 : /*
726 : * Check each dir free block's bestfree data.
727 : *
728 : * Iterate all the fsbcount-aligned block offsets in
729 : * this directory. The directory block reading code is
730 : * smart enough to do its own bmap lookups to handle
731 : * discontiguous directory blocks. When we're done
732 : * with the extent record, re-query the bmap at the
733 : * next fsbcount-aligned offset to avoid redundant
734 : * block checks.
735 : */
736 40452 : for (lblk = roundup((xfs_dablk_t)got.br_startoff,
737 : args.geo->fsbcount);
738 80904 : lblk < got.br_startoff + got.br_blockcount;
739 40452 : lblk += args.geo->fsbcount) {
740 40452 : error = xchk_directory_free_bestfree(sc, &args,
741 : lblk);
742 40452 : if (error)
743 0 : goto out;
744 : }
745 40452 : dabno = got.br_startoff + got.br_blockcount;
746 40452 : lblk = roundup(dabno, args.geo->fsbcount);
747 40452 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
748 : }
749 186144 : out:
750 186144 : return error;
751 : }
752 :
753 : /* Scrub a whole directory. */
754 : int
755 79708364 : xchk_directory(
756 : struct xfs_scrub *sc)
757 : {
758 79708364 : int error;
759 :
760 79708364 : if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
761 : return -ENOENT;
762 :
763 : /* Plausible size? */
764 79708364 : if (sc->ip->i_disk_size < xfs_dir2_sf_hdr_size(0)) {
765 0 : xchk_ino_set_corrupt(sc, sc->ip->i_ino);
766 0 : return 0;
767 : }
768 :
769 : /* Check directory tree structure */
770 79708364 : error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
771 79706127 : if (error)
772 : return error;
773 :
774 79706127 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
775 : return 0;
776 :
777 : /* Check the freespace. */
778 79707744 : error = xchk_directory_blocks(sc);
779 79707989 : if (error)
780 : return error;
781 :
782 79707989 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
783 : return 0;
784 :
785 : /* Look up every name in this directory by hash. */
786 79708604 : error = xchk_dir_walk(sc, sc->ip, xchk_dir_actor, NULL);
787 79706015 : if (error == -ECANCELED)
788 2 : error = 0;
789 : return error;
790 : }
|