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 "xfs_attr.h"
19 : #include "xfs_parent.h"
20 : #include "scrub/scrub.h"
21 : #include "scrub/common.h"
22 : #include "scrub/dabtree.h"
23 : #include "scrub/readdir.h"
24 : #include "scrub/repair.h"
25 : #include "scrub/trace.h"
26 : #include "scrub/xfile.h"
27 : #include "scrub/xfarray.h"
28 : #include "scrub/xfblob.h"
29 :
30 : /* Set us up to scrub directories. */
31 : int
32 19382237 : xchk_setup_directory(
33 : struct xfs_scrub *sc)
34 : {
35 19382237 : int error;
36 :
37 38764474 : if (xchk_could_repair(sc)) {
38 242643 : error = xrep_setup_directory(sc);
39 242595 : if (error)
40 : return error;
41 : }
42 :
43 19381708 : return xchk_setup_inode_contents(sc, 0);
44 : }
45 :
46 : /* Directories */
47 :
48 : /* Deferred directory entry that we saved for later. */
49 : struct xchk_dirent {
50 : /* Cookie for retrieval of the dirent name. */
51 : xfblob_cookie name_cookie;
52 :
53 : /* Child inode number. */
54 : xfs_ino_t ino;
55 :
56 : /* Length of the pptr name. */
57 : uint8_t namelen;
58 : };
59 :
60 : struct xchk_dir {
61 : struct xfs_scrub *sc;
62 :
63 : /* Scratch buffer for scanning pptr xattrs */
64 : struct xfs_parent_name_irec pptr;
65 :
66 : /* xattr key and da args for parent pointer validation. */
67 : struct xfs_parent_scratch pptr_scratch;
68 :
69 : /* Fixed-size array of xchk_dirent structures. */
70 : struct xfarray *dir_entries;
71 :
72 : /* Blobs containing dirent names. */
73 : struct xfblob *dir_names;
74 :
75 : /* If we've cycled the ILOCK, we must revalidate deferred dirents. */
76 : bool need_revalidate;
77 :
78 : /* Name buffer for dirent revalidation. */
79 : uint8_t namebuf[MAXNAMELEN];
80 :
81 : };
82 :
83 : /* Scrub a directory entry. */
84 :
85 : /* Check that an inode's mode matches a given XFS_DIR3_FT_* type. */
86 : STATIC void
87 186149007 : xchk_dir_check_ftype(
88 : struct xfs_scrub *sc,
89 : xfs_fileoff_t offset,
90 : struct xfs_inode *ip,
91 : int ftype)
92 : {
93 186149007 : struct xfs_mount *mp = sc->mp;
94 :
95 186149007 : if (!xfs_has_ftype(mp)) {
96 0 : if (ftype != XFS_DIR3_FT_UNKNOWN && ftype != XFS_DIR3_FT_DIR)
97 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
98 0 : return;
99 : }
100 :
101 186149007 : if (xfs_mode_to_ftype(VFS_I(ip)->i_mode) != ftype)
102 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
103 :
104 : /*
105 : * Metadata and regular inodes cannot cross trees. This property
106 : * cannot change without a full inode free and realloc cycle, so it's
107 : * safe to check this without holding locks.
108 : */
109 185849809 : if (xfs_is_metadir_inode(ip) ^ xfs_is_metadir_inode(sc->ip))
110 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
111 : }
112 :
113 : /*
114 : * Try to lock a child file for checking parent pointers. Returns the inode
115 : * flags for the locks we now hold, or zero if we failed.
116 : */
117 : STATIC unsigned int
118 141821555 : xchk_dir_lock_child(
119 : struct xfs_scrub *sc,
120 : struct xfs_inode *ip)
121 : {
122 141821555 : if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
123 : return 0;
124 :
125 141557139 : if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
126 2397 : xfs_iunlock(ip, XFS_IOLOCK_SHARED);
127 2397 : return 0;
128 : }
129 :
130 283911338 : if (!xfs_inode_has_attr_fork(ip) || !xfs_need_iread_extents(&ip->i_af))
131 141951983 : return XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED;
132 :
133 120 : xfs_iunlock(ip, XFS_ILOCK_SHARED);
134 :
135 120 : if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
136 0 : xfs_iunlock(ip, XFS_IOLOCK_SHARED);
137 0 : return 0;
138 : }
139 :
140 : return XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL;
141 : }
142 :
143 : /* Check the backwards link (parent pointer) associated with this dirent. */
144 : STATIC int
145 141843247 : xchk_dir_parent_pointer(
146 : struct xchk_dir *sd,
147 : const struct xfs_name *name,
148 : struct xfs_inode *ip)
149 : {
150 141843247 : struct xfs_scrub *sc = sd->sc;
151 141843247 : int error;
152 :
153 141843247 : sd->pptr.p_ino = sc->ip->i_ino;
154 141843247 : sd->pptr.p_gen = VFS_I(sc->ip)->i_generation;
155 141843247 : sd->pptr.p_namelen = name->len;
156 283686494 : memcpy(sd->pptr.p_name, name->name, name->len);
157 141843247 : xfs_parent_irec_hashname(sc->mp, &sd->pptr);
158 :
159 141529701 : error = xfs_parent_lookup(sc->tp, ip, &sd->pptr, &sd->pptr_scratch);
160 141640570 : if (error == -ENOATTR)
161 0 : xchk_fblock_xref_set_corrupt(sc, XFS_DATA_FORK, 0);
162 :
163 141640570 : return 0;
164 : }
165 :
166 : /* Look for a parent pointer matching this dirent, if the child isn't busy. */
167 : STATIC int
168 179122547 : xchk_dir_check_pptr_fast(
169 : struct xchk_dir *sd,
170 : xfs_dir2_dataptr_t dapos,
171 : const struct xfs_name *name,
172 : struct xfs_inode *ip)
173 : {
174 179122547 : struct xfs_scrub *sc = sd->sc;
175 179122547 : unsigned int lockmode;
176 179122547 : int error;
177 :
178 : /* dot and dotdot entries do not have parent pointers */
179 339605247 : if (xfs_dir2_samename(name, &xfs_name_dot) ||
180 160490500 : xfs_dir2_samename(name, &xfs_name_dotdot))
181 37255545 : return 0;
182 :
183 : /* Try to lock the inode. */
184 141812845 : lockmode = xchk_dir_lock_child(sc, ip);
185 141993677 : if (!lockmode) {
186 185900 : struct xchk_dirent save_de = {
187 185900 : .namelen = name->len,
188 185900 : .ino = ip->i_ino,
189 : };
190 :
191 : /* Couldn't lock the inode, so save the dirent for later. */
192 185900 : trace_xchk_dir_defer(sc->ip, name->name, name->len, ip->i_ino);
193 :
194 371802 : error = xfblob_store(sd->dir_names, &save_de.name_cookie,
195 185900 : name->name, name->len);
196 185902 : if (xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
197 : &error))
198 185900 : return error;
199 :
200 0 : error = xfarray_append(sd->dir_entries, &save_de);
201 0 : if (xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
202 : &error))
203 0 : return error;
204 :
205 : return 0;
206 : }
207 :
208 141807777 : error = xchk_dir_parent_pointer(sd, name, ip);
209 141692135 : xfs_iunlock(ip, lockmode);
210 141490641 : return error;
211 : }
212 :
213 : /*
214 : * Scrub a single directory entry.
215 : *
216 : * Check the inode number to make sure it's sane, then we check that we can
217 : * look up this filename. Finally, we check the ftype.
218 : */
219 : STATIC int
220 185909770 : xchk_dir_actor(
221 : struct xfs_scrub *sc,
222 : struct xfs_inode *dp,
223 : xfs_dir2_dataptr_t dapos,
224 : const struct xfs_name *name,
225 : xfs_ino_t ino,
226 : void *priv)
227 : {
228 185909770 : struct xfs_mount *mp = dp->i_mount;
229 185909770 : struct xfs_inode *ip;
230 185909770 : struct xchk_dir *sd = priv;
231 185909770 : xfs_ino_t lookup_ino;
232 185909770 : xfs_dablk_t offset;
233 185909770 : int error = 0;
234 :
235 185909770 : offset = xfs_dir2_db_to_da(mp->m_dir_geo,
236 : xfs_dir2_dataptr_to_db(mp->m_dir_geo, dapos));
237 :
238 185828979 : if (xchk_should_terminate(sc, &error))
239 9 : return error;
240 :
241 : /* Does this inode number make sense? */
242 185989097 : if (!xfs_verify_dir_ino(mp, ino)) {
243 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
244 0 : return -ECANCELED;
245 : }
246 :
247 : /* Does this name make sense? */
248 185656773 : if (!xfs_dir2_namecheck(name->name, name->len)) {
249 11 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
250 11 : return -ECANCELED;
251 : }
252 :
253 185646715 : if (xfs_dir2_samename(name, &xfs_name_dot)) {
254 : /* If this is "." then check that the inum matches the dir. */
255 19326528 : if (ino != dp->i_ino)
256 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
257 166198886 : } else if (xfs_dir2_samename(name, &xfs_name_dotdot)) {
258 : /*
259 : * If this is ".." in the root inode, check that the inum
260 : * matches this dir.
261 : */
262 19280931 : if (dp->i_ino == mp->m_sb.sb_rootino && ino != dp->i_ino)
263 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
264 : }
265 :
266 : /* Verify that we can look up this name by hash. */
267 185423161 : error = xchk_dir_lookup(sc, dp, name, &lookup_ino);
268 : /* ENOENT means the hash lookup failed and the dir is corrupt */
269 186151551 : if (error == -ENOENT)
270 0 : error = -EFSCORRUPTED;
271 186151551 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, offset, &error))
272 0 : goto out;
273 185724882 : if (lookup_ino != ino) {
274 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
275 0 : return -ECANCELED;
276 : }
277 :
278 : /*
279 : * Grab the inode pointed to by the dirent. We release the inode
280 : * before we cancel the scrub transaction.
281 : *
282 : * If _iget returns -EINVAL or -ENOENT then the child inode number is
283 : * garbage and the directory is corrupt. If the _iget returns
284 : * -EFSCORRUPTED or -EFSBADCRC then the child is corrupt which is a
285 : * cross referencing error. Any other error is an operational error.
286 : */
287 185724882 : error = xchk_iget(sc, ino, &ip);
288 186152598 : if (error == -EINVAL || error == -ENOENT) {
289 0 : error = -EFSCORRUPTED;
290 0 : xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error);
291 0 : goto out;
292 : }
293 186152598 : if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, offset, &error))
294 0 : goto out;
295 :
296 186039032 : xchk_dir_check_ftype(sc, offset, ip, name->type);
297 :
298 185892756 : if (xfs_has_parent(mp)) {
299 179124409 : error = xchk_dir_check_pptr_fast(sd, dapos, name, ip);
300 179227063 : if (error)
301 : goto out_rele;
302 : }
303 :
304 185995410 : out_rele:
305 185995410 : xchk_irele(sc, ip);
306 186096354 : out:
307 186096354 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
308 : return -ECANCELED;
309 186096354 : return error;
310 : }
311 :
312 : /* Scrub a directory btree record. */
313 : STATIC int
314 78354548 : xchk_dir_rec(
315 : struct xchk_da_btree *ds,
316 : int level)
317 : {
318 78354548 : struct xfs_name dname = { };
319 78354548 : struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
320 78332071 : struct xfs_mount *mp = ds->state->mp;
321 78332071 : struct xfs_inode *dp = ds->dargs.dp;
322 78332071 : struct xfs_da_geometry *geo = mp->m_dir_geo;
323 78332071 : struct xfs_dir2_data_entry *dent;
324 78332071 : struct xfs_buf *bp;
325 78332071 : struct xfs_dir2_leaf_entry *ent;
326 78332071 : unsigned int end;
327 78332071 : unsigned int iter_off;
328 78332071 : xfs_ino_t ino;
329 78332071 : xfs_dablk_t rec_bno;
330 78332071 : xfs_dir2_db_t db;
331 78332071 : xfs_dir2_data_aoff_t off;
332 78332071 : xfs_dir2_dataptr_t ptr;
333 78332071 : xfs_dahash_t calc_hash;
334 78332071 : xfs_dahash_t hash;
335 78332071 : struct xfs_dir3_icleaf_hdr hdr;
336 78332071 : unsigned int tag;
337 78332071 : int error;
338 :
339 78332071 : ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
340 : blk->magic == XFS_DIR2_LEAFN_MAGIC);
341 :
342 78332071 : xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
343 78241327 : ent = hdr.ents + blk->index;
344 :
345 : /* Check the hash of the entry. */
346 78241327 : error = xchk_da_btree_hash(ds, level, &ent->hashval);
347 78263116 : if (error)
348 0 : goto out;
349 :
350 : /* Valid hash pointer? */
351 78263116 : ptr = be32_to_cpu(ent->address);
352 78263116 : if (ptr == 0)
353 : return 0;
354 :
355 : /* Find the directory entry's location. */
356 70484083 : db = xfs_dir2_dataptr_to_db(geo, ptr);
357 70475526 : off = xfs_dir2_dataptr_to_off(geo, ptr);
358 70475526 : rec_bno = xfs_dir2_db_to_da(geo, db);
359 :
360 70482390 : if (rec_bno >= geo->leafblk) {
361 0 : xchk_da_set_corrupt(ds, level);
362 0 : goto out;
363 : }
364 70482390 : error = xfs_dir3_data_read(ds->dargs.trans, dp, ds->dargs.owner,
365 : rec_bno, XFS_DABUF_MAP_HOLE_OK, &bp);
366 70352856 : if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
367 : &error))
368 0 : goto out;
369 70283960 : if (!bp) {
370 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
371 0 : goto out;
372 : }
373 70283960 : xchk_buffer_recheck(ds->sc, bp);
374 :
375 70501800 : if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
376 0 : goto out_relse;
377 :
378 70501800 : dent = bp->b_addr + off;
379 :
380 : /* Make sure we got a real directory entry. */
381 70501800 : iter_off = geo->data_entry_offset;
382 70501800 : end = xfs_dir3_data_end_offset(geo, bp->b_addr);
383 70598772 : if (!end) {
384 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
385 0 : goto out_relse;
386 : }
387 7053941947 : for (;;) {
388 7053941947 : struct xfs_dir2_data_entry *dep = bp->b_addr + iter_off;
389 7053941947 : struct xfs_dir2_data_unused *dup = bp->b_addr + iter_off;
390 :
391 7053941947 : if (iter_off >= end) {
392 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
393 0 : goto out_relse;
394 : }
395 :
396 7053941947 : if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
397 217319394 : iter_off += be16_to_cpu(dup->length);
398 217319394 : continue;
399 : }
400 6836622553 : if (dep == dent)
401 : break;
402 13530712059 : iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
403 : }
404 :
405 : /* Retrieve the entry, sanity check it, and compare hashes. */
406 70598772 : ino = be64_to_cpu(dent->inumber);
407 70598772 : hash = be32_to_cpu(ent->hashval);
408 70598772 : tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
409 70565057 : if (!xfs_verify_dir_ino(mp, ino) || tag != off)
410 21 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
411 70527274 : if (dent->namelen == 0) {
412 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
413 0 : goto out_relse;
414 : }
415 :
416 : /* Does the directory hash match? */
417 70527274 : dname.name = dent->name;
418 70527274 : dname.len = dent->namelen;
419 70527274 : calc_hash = xfs_dir2_hashname(mp, &dname);
420 70530490 : if (calc_hash != hash)
421 0 : xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
422 :
423 70530490 : out_relse:
424 70530490 : xfs_trans_brelse(ds->dargs.trans, bp);
425 70523232 : out:
426 70523232 : return error;
427 : }
428 :
429 : /*
430 : * Is this unused entry either in the bestfree or smaller than all of
431 : * them? We've already checked that the bestfrees are sorted longest to
432 : * shortest, and that there aren't any bogus entries.
433 : */
434 : STATIC void
435 4547718 : xchk_directory_check_free_entry(
436 : struct xfs_scrub *sc,
437 : xfs_dablk_t lblk,
438 : struct xfs_dir2_data_free *bf,
439 : struct xfs_dir2_data_unused *dup)
440 : {
441 4547718 : struct xfs_dir2_data_free *dfp;
442 4547718 : unsigned int dup_length;
443 :
444 4547718 : dup_length = be16_to_cpu(dup->length);
445 :
446 : /* Unused entry is shorter than any of the bestfrees */
447 4547718 : if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
448 : return;
449 :
450 4288559 : for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
451 4288559 : if (dup_length == be16_to_cpu(dfp->length))
452 : return;
453 :
454 : /* Unused entry should be in the bestfrees but wasn't found. */
455 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
456 : }
457 :
458 : /* Check free space info in a directory data block. */
459 : STATIC int
460 771535 : xchk_directory_data_bestfree(
461 : struct xfs_scrub *sc,
462 : xfs_dablk_t lblk,
463 : bool is_block)
464 : {
465 771535 : struct xfs_dir2_data_unused *dup;
466 771535 : struct xfs_dir2_data_free *dfp;
467 771535 : struct xfs_buf *bp;
468 771535 : struct xfs_dir2_data_free *bf;
469 771535 : struct xfs_mount *mp = sc->mp;
470 771535 : u16 tag;
471 771535 : unsigned int nr_bestfrees = 0;
472 771535 : unsigned int nr_frees = 0;
473 771535 : unsigned int smallest_bestfree;
474 771535 : int newlen;
475 771535 : unsigned int offset;
476 771535 : unsigned int end;
477 771535 : int error;
478 :
479 771535 : if (is_block) {
480 : /* dir block format */
481 282168 : if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
482 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
483 282168 : error = xfs_dir3_block_read(sc->tp, sc->ip, sc->ip->i_ino, &bp);
484 : } else {
485 : /* dir data format */
486 489367 : error = xfs_dir3_data_read(sc->tp, sc->ip, sc->ip->i_ino, lblk,
487 : 0, &bp);
488 : }
489 771578 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
490 0 : goto out;
491 771541 : xchk_buffer_recheck(sc, bp);
492 :
493 : /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
494 :
495 771604 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
496 0 : goto out_buf;
497 :
498 : /* Do the bestfrees correspond to actual free space? */
499 771604 : bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
500 771604 : smallest_bestfree = UINT_MAX;
501 3858022 : for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
502 2314816 : offset = be16_to_cpu(dfp->offset);
503 2314816 : if (offset == 0)
504 1013603 : continue;
505 1301213 : if (offset >= mp->m_dir_geo->blksize) {
506 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
507 0 : goto out_buf;
508 : }
509 1301213 : dup = bp->b_addr + offset;
510 1301213 : tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
511 :
512 : /* bestfree doesn't match the entry it points at? */
513 1301213 : if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
514 1301213 : be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
515 : tag != offset) {
516 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
517 0 : goto out_buf;
518 : }
519 :
520 : /* bestfree records should be ordered largest to smallest */
521 1301213 : if (smallest_bestfree < be16_to_cpu(dfp->length)) {
522 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
523 0 : goto out_buf;
524 : }
525 :
526 1301213 : smallest_bestfree = be16_to_cpu(dfp->length);
527 1301213 : nr_bestfrees++;
528 : }
529 :
530 : /* Make sure the bestfrees are actually the best free spaces. */
531 771602 : offset = mp->m_dir_geo->data_entry_offset;
532 771602 : end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
533 :
534 : /* Iterate the entries, stopping when we hit or go past the end. */
535 81901162 : while (offset < end) {
536 81129505 : dup = bp->b_addr + offset;
537 :
538 : /* Skip real entries */
539 81129505 : if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
540 76581880 : struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
541 :
542 76581880 : newlen = xfs_dir2_data_entsize(mp, dep->namelen);
543 76581880 : if (newlen <= 0) {
544 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
545 : lblk);
546 : goto out_buf;
547 : }
548 76581880 : offset += newlen;
549 76581880 : continue;
550 : }
551 :
552 : /* Spot check this free entry */
553 4547625 : tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
554 4547625 : if (tag != offset) {
555 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
556 0 : goto out_buf;
557 : }
558 :
559 : /*
560 : * Either this entry is a bestfree or it's smaller than
561 : * any of the bestfrees.
562 : */
563 4547625 : xchk_directory_check_free_entry(sc, lblk, bf, dup);
564 4547680 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
565 0 : goto out_buf;
566 :
567 : /* Move on. */
568 4547680 : newlen = be16_to_cpu(dup->length);
569 4547680 : if (newlen <= 0) {
570 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
571 0 : goto out_buf;
572 : }
573 4547680 : offset += newlen;
574 4547680 : if (offset <= end)
575 4547682 : nr_frees++;
576 : }
577 :
578 : /* We're required to fill all the space. */
579 771649 : if (offset != end)
580 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
581 :
582 : /* Did we see at least as many free slots as there are bestfrees? */
583 771649 : if (nr_frees < nr_bestfrees)
584 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
585 771649 : out_buf:
586 771649 : xfs_trans_brelse(sc->tp, bp);
587 771576 : out:
588 771576 : return error;
589 : }
590 :
591 : /*
592 : * Does the free space length in the free space index block ($len) match
593 : * the longest length in the directory data block's bestfree array?
594 : * Assume that we've already checked that the data block's bestfree
595 : * array is in order.
596 : */
597 : STATIC void
598 489395 : xchk_directory_check_freesp(
599 : struct xfs_scrub *sc,
600 : xfs_dablk_t lblk,
601 : struct xfs_buf *dbp,
602 : unsigned int len)
603 : {
604 489395 : struct xfs_dir2_data_free *dfp;
605 :
606 489395 : dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
607 :
608 489383 : if (len != be16_to_cpu(dfp->length))
609 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
610 :
611 489383 : if (len > 0 && be16_to_cpu(dfp->offset) == 0)
612 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
613 489383 : }
614 :
615 : /* Check free space info in a directory leaf1 block. */
616 : STATIC int
617 19851 : xchk_directory_leaf1_bestfree(
618 : struct xfs_scrub *sc,
619 : struct xfs_da_args *args,
620 : xfs_dir2_db_t last_data_db,
621 : xfs_dablk_t lblk)
622 : {
623 19851 : struct xfs_dir3_icleaf_hdr leafhdr;
624 19851 : struct xfs_dir2_leaf_tail *ltp;
625 19851 : struct xfs_dir2_leaf *leaf;
626 19851 : struct xfs_buf *dbp;
627 19851 : struct xfs_buf *bp;
628 19851 : struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
629 19851 : __be16 *bestp;
630 19851 : __u16 best;
631 19851 : __u32 hash;
632 19851 : __u32 lasthash = 0;
633 19851 : __u32 bestcount;
634 19851 : unsigned int stale = 0;
635 19851 : int i;
636 19851 : int error;
637 :
638 : /* Read the free space block. */
639 19851 : error = xfs_dir3_leaf_read(sc->tp, sc->ip, sc->ip->i_ino, lblk, &bp);
640 19851 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
641 0 : return error;
642 19851 : xchk_buffer_recheck(sc, bp);
643 :
644 19851 : leaf = bp->b_addr;
645 19851 : xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
646 19851 : ltp = xfs_dir2_leaf_tail_p(geo, leaf);
647 19851 : bestcount = be32_to_cpu(ltp->bestcount);
648 19851 : bestp = xfs_dir2_leaf_bests_p(ltp);
649 :
650 19851 : if (xfs_has_crc(sc->mp)) {
651 19851 : struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
652 :
653 19851 : if (hdr3->pad != cpu_to_be32(0))
654 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
655 : }
656 :
657 : /*
658 : * There must be enough bestfree slots to cover all the directory data
659 : * blocks that we scanned. It is possible for there to be a hole
660 : * between the last data block and i_disk_size. This seems like an
661 : * oversight to the scrub author, but as we have been writing out
662 : * directories like this (and xfs_repair doesn't mind them) for years,
663 : * that's what we have to check.
664 : */
665 19851 : if (bestcount != last_data_db + 1) {
666 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
667 0 : goto out;
668 : }
669 :
670 : /* Is the leaf count even remotely sane? */
671 19851 : if (leafhdr.count > geo->leaf_max_ents) {
672 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
673 0 : goto out;
674 : }
675 :
676 : /* Leaves and bests don't overlap in leaf format. */
677 19851 : if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
678 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
679 0 : goto out;
680 : }
681 :
682 : /* Check hash value order, count stale entries. */
683 4680744 : for (i = 0; i < leafhdr.count; i++) {
684 4660894 : hash = be32_to_cpu(leafhdr.ents[i].hashval);
685 4660894 : if (i > 0 && lasthash > hash)
686 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
687 4660893 : lasthash = hash;
688 4660893 : if (leafhdr.ents[i].address ==
689 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
690 387495 : stale++;
691 : }
692 19850 : if (leafhdr.stale != stale)
693 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
694 19850 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
695 0 : goto out;
696 :
697 : /* Check all the bestfree entries. */
698 67409 : for (i = 0; i < bestcount; i++, bestp++) {
699 47558 : best = be16_to_cpu(*bestp);
700 47558 : error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
701 : xfs_dir2_db_to_da(args->geo, i),
702 : XFS_DABUF_MAP_HOLE_OK, &dbp);
703 47559 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
704 : &error))
705 : break;
706 :
707 47559 : if (!dbp) {
708 22 : if (best != NULLDATAOFF) {
709 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
710 : lblk);
711 0 : break;
712 : }
713 22 : continue;
714 : }
715 :
716 47537 : if (best == NULLDATAOFF)
717 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
718 : else
719 47537 : xchk_directory_check_freesp(sc, lblk, dbp, best);
720 47536 : xfs_trans_brelse(sc->tp, dbp);
721 47537 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
722 : break;
723 : }
724 19851 : out:
725 19851 : xfs_trans_brelse(sc->tp, bp);
726 19851 : return error;
727 : }
728 :
729 : /* Check free space info in a directory freespace block. */
730 : STATIC int
731 25923 : xchk_directory_free_bestfree(
732 : struct xfs_scrub *sc,
733 : struct xfs_da_args *args,
734 : xfs_dablk_t lblk)
735 : {
736 25923 : struct xfs_dir3_icfree_hdr freehdr;
737 25923 : struct xfs_buf *dbp;
738 25923 : struct xfs_buf *bp;
739 25923 : __u16 best;
740 25923 : unsigned int stale = 0;
741 25923 : int i;
742 25923 : int error;
743 :
744 : /* Read the free space block */
745 25923 : error = xfs_dir2_free_read(sc->tp, sc->ip, sc->ip->i_ino, lblk, &bp);
746 25923 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
747 0 : return error;
748 25922 : xchk_buffer_recheck(sc, bp);
749 :
750 25923 : if (xfs_has_crc(sc->mp)) {
751 25923 : struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
752 :
753 25923 : if (hdr3->pad != cpu_to_be32(0))
754 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
755 : }
756 :
757 : /* Check all the entries. */
758 25923 : xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
759 502833 : for (i = 0; i < freehdr.nvalid; i++) {
760 450987 : best = be16_to_cpu(freehdr.bests[i]);
761 450987 : if (best == NULLDATAOFF) {
762 9142 : stale++;
763 9142 : continue;
764 : }
765 883709 : error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
766 441845 : (freehdr.firstdb + i) * args->geo->fsbcount,
767 : 0, &dbp);
768 441864 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
769 : &error))
770 0 : goto out;
771 441852 : xchk_directory_check_freesp(sc, lblk, dbp, best);
772 441843 : xfs_trans_brelse(sc->tp, dbp);
773 : }
774 :
775 25923 : if (freehdr.nused + stale != freehdr.nvalid)
776 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
777 25923 : out:
778 25923 : xfs_trans_brelse(sc->tp, bp);
779 25923 : return error;
780 : }
781 :
782 : /* Check free space information in directories. */
783 : STATIC int
784 19355555 : xchk_directory_blocks(
785 : struct xfs_scrub *sc)
786 : {
787 19355555 : struct xfs_bmbt_irec got;
788 19355555 : struct xfs_da_args args = {
789 19355555 : .dp = sc->ip,
790 : .whichfork = XFS_DATA_FORK,
791 19355555 : .geo = sc->mp->m_dir_geo,
792 19355555 : .trans = sc->tp,
793 19355555 : .owner = sc->ip->i_ino,
794 : };
795 19355555 : struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
796 19355555 : struct xfs_mount *mp = sc->mp;
797 19355555 : xfs_fileoff_t leaf_lblk;
798 19355555 : xfs_fileoff_t free_lblk;
799 19355555 : xfs_fileoff_t lblk;
800 19355555 : struct xfs_iext_cursor icur;
801 19355555 : xfs_dablk_t dabno;
802 19355555 : xfs_dir2_db_t last_data_db = 0;
803 19355555 : bool found;
804 19355555 : bool is_block = false;
805 19355555 : int error;
806 :
807 : /* Ignore local format directories. */
808 19355555 : if (ifp->if_format != XFS_DINODE_FMT_EXTENTS &&
809 : ifp->if_format != XFS_DINODE_FMT_BTREE)
810 : return 0;
811 :
812 327915 : lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
813 327915 : leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
814 327915 : free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
815 :
816 : /* Is this a block dir? */
817 327915 : error = xfs_dir2_isblock(&args, &is_block);
818 327914 : if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
819 0 : goto out;
820 :
821 : /* Iterate all the data extents in the directory... */
822 327907 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
823 971775 : while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
824 : /* No more data blocks... */
825 689607 : if (got.br_startoff >= leaf_lblk)
826 : break;
827 :
828 : /*
829 : * Check each data block's bestfree data.
830 : *
831 : * Iterate all the fsbcount-aligned block offsets in
832 : * this directory. The directory block reading code is
833 : * smart enough to do its own bmap lookups to handle
834 : * discontiguous directory blocks. When we're done
835 : * with the extent record, re-query the bmap at the
836 : * next fsbcount-aligned offset to avoid redundant
837 : * block checks.
838 : */
839 643863 : for (lblk = roundup((xfs_dablk_t)got.br_startoff,
840 : args.geo->fsbcount);
841 1415476 : lblk < got.br_startoff + got.br_blockcount;
842 771613 : lblk += args.geo->fsbcount) {
843 771600 : last_data_db = xfs_dir2_da_to_db(args.geo, lblk);
844 771542 : error = xchk_directory_data_bestfree(sc, lblk,
845 : is_block);
846 771613 : if (error)
847 0 : goto out;
848 : }
849 643876 : dabno = got.br_startoff + got.br_blockcount;
850 643876 : lblk = roundup(dabno, args.geo->fsbcount);
851 643876 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
852 : }
853 :
854 327922 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
855 0 : goto out;
856 :
857 : /* Look for a leaf1 block, which has free info. */
858 327922 : if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
859 45744 : got.br_startoff == leaf_lblk &&
860 91487 : got.br_blockcount == args.geo->fsbcount &&
861 45743 : !xfs_iext_next_extent(ifp, &icur, &got)) {
862 19851 : if (is_block) {
863 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
864 0 : goto out;
865 : }
866 19851 : error = xchk_directory_leaf1_bestfree(sc, &args, last_data_db,
867 : leaf_lblk);
868 19850 : if (error)
869 0 : goto out;
870 : }
871 :
872 327922 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
873 0 : goto out;
874 :
875 : /* Scan for free blocks */
876 327922 : lblk = free_lblk;
877 327922 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
878 353842 : while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
879 : /*
880 : * Dirs can't have blocks mapped above 2^32.
881 : * Single-block dirs shouldn't even be here.
882 : */
883 25923 : lblk = got.br_startoff;
884 25923 : if (lblk & ~0xFFFFFFFFULL) {
885 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
886 0 : goto out;
887 : }
888 25923 : if (is_block) {
889 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
890 0 : goto out;
891 : }
892 :
893 : /*
894 : * Check each dir free block's bestfree data.
895 : *
896 : * Iterate all the fsbcount-aligned block offsets in
897 : * this directory. The directory block reading code is
898 : * smart enough to do its own bmap lookups to handle
899 : * discontiguous directory blocks. When we're done
900 : * with the extent record, re-query the bmap at the
901 : * next fsbcount-aligned offset to avoid redundant
902 : * block checks.
903 : */
904 25923 : for (lblk = roundup((xfs_dablk_t)got.br_startoff,
905 : args.geo->fsbcount);
906 51846 : lblk < got.br_startoff + got.br_blockcount;
907 25923 : lblk += args.geo->fsbcount) {
908 25923 : error = xchk_directory_free_bestfree(sc, &args,
909 : lblk);
910 25923 : if (error)
911 0 : goto out;
912 : }
913 25923 : dabno = got.br_startoff + got.br_blockcount;
914 25923 : lblk = roundup(dabno, args.geo->fsbcount);
915 25923 : found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
916 : }
917 327920 : out:
918 327920 : return error;
919 : }
920 :
921 : /*
922 : * Revalidate a dirent that we collected in the past but couldn't check because
923 : * of lock contention. Returns 0 if the dirent is still valid, -ENOENT if it
924 : * has gone away on us, or a negative errno.
925 : */
926 : STATIC int
927 0 : xchk_dir_revalidate_dirent(
928 : struct xchk_dir *sd,
929 : const struct xfs_name *xname,
930 : xfs_ino_t ino)
931 : {
932 0 : struct xfs_scrub *sc = sd->sc;
933 0 : xfs_ino_t child_ino;
934 0 : int error;
935 :
936 : /*
937 : * Look up the directory entry. If we get -ENOENT, the directory entry
938 : * went away and there's nothing to revalidate. Return any other
939 : * error.
940 : */
941 0 : error = xchk_dir_lookup(sc, sc->ip, xname, &child_ino);
942 0 : if (error)
943 : return error;
944 :
945 : /* The inode number changed, nothing to revalidate. */
946 0 : if (ino != child_ino)
947 0 : return -ENOENT;
948 :
949 : return 0;
950 : }
951 :
952 : /*
953 : * Check a directory entry's parent pointers the slow way, which means we cycle
954 : * locks a bunch and put up with revalidation until we get it done.
955 : */
956 : STATIC int
957 0 : xchk_dir_slow_dirent(
958 : struct xchk_dir *sd,
959 : struct xchk_dirent *dirent)
960 : {
961 0 : struct xfs_name xname = {
962 0 : .name = sd->namebuf,
963 0 : .len = dirent->namelen,
964 : };
965 0 : struct xfs_scrub *sc = sd->sc;
966 0 : struct xfs_inode *ip;
967 0 : unsigned int lockmode;
968 0 : int error;
969 :
970 : /* Check that the deferred dirent still exists. */
971 0 : if (sd->need_revalidate) {
972 0 : error = xchk_dir_revalidate_dirent(sd, &xname, dirent->ino);
973 0 : if (error == -ENOENT)
974 : return 0;
975 0 : if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0,
976 : &error))
977 0 : return error;
978 : }
979 :
980 0 : error = xchk_iget(sc, dirent->ino, &ip);
981 0 : if (error == -EINVAL || error == -ENOENT) {
982 0 : xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
983 0 : return 0;
984 : }
985 0 : if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
986 0 : return error;
987 :
988 : /*
989 : * If we can grab both IOLOCK and ILOCK of the alleged child, we can
990 : * proceed with the validation.
991 : */
992 0 : lockmode = xchk_dir_lock_child(sc, ip);
993 0 : if (lockmode)
994 0 : goto check_pptr;
995 :
996 : /*
997 : * We couldn't lock the child file. Drop all the locks and try to
998 : * get them again, one at a time.
999 : */
1000 0 : xchk_iunlock(sc, sc->ilock_flags);
1001 0 : sd->need_revalidate = true;
1002 :
1003 0 : trace_xchk_dir_slowpath(sc->ip, xname.name, xname.len, ip->i_ino);
1004 :
1005 0 : while (true) {
1006 0 : xchk_ilock(sc, XFS_IOLOCK_EXCL);
1007 0 : if (xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
1008 0 : xchk_ilock(sc, XFS_ILOCK_EXCL);
1009 0 : if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1010 : break;
1011 : }
1012 0 : xchk_iunlock(sc, XFS_ILOCK_EXCL);
1013 : }
1014 0 : xchk_iunlock(sc, XFS_IOLOCK_EXCL);
1015 :
1016 0 : if (xchk_should_terminate(sc, &error))
1017 0 : goto out_rele;
1018 :
1019 0 : delay(1);
1020 : }
1021 0 : lockmode = XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL;
1022 :
1023 : /* Revalidate, since we just cycled the locks. */
1024 0 : error = xchk_dir_revalidate_dirent(sd, &xname, dirent->ino);
1025 0 : if (error == -ENOENT)
1026 0 : goto out_unlock;
1027 0 : if (!xchk_fblock_xref_process_error(sc, XFS_DATA_FORK, 0, &error))
1028 0 : goto out_unlock;
1029 :
1030 0 : check_pptr:
1031 0 : error = xchk_dir_parent_pointer(sd, &xname, ip);
1032 0 : out_unlock:
1033 0 : xfs_iunlock(ip, lockmode);
1034 0 : out_rele:
1035 0 : xchk_irele(sc, ip);
1036 0 : return error;
1037 : }
1038 :
1039 : /* Check all the dirents that we deferred the first time around. */
1040 : STATIC int
1041 18683407 : xchk_dir_finish_slow_dirents(
1042 : struct xchk_dir *sd)
1043 : {
1044 18683407 : xfarray_idx_t array_cur;
1045 18683407 : int error;
1046 :
1047 18683407 : foreach_xfarray_idx(sd->dir_entries, array_cur) {
1048 0 : struct xchk_dirent dirent;
1049 :
1050 0 : if (sd->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
1051 0 : return 0;
1052 :
1053 0 : error = xfarray_load(sd->dir_entries, array_cur, &dirent);
1054 0 : if (error)
1055 0 : return error;
1056 :
1057 0 : error = xfblob_load(sd->dir_names, dirent.name_cookie,
1058 0 : sd->namebuf, dirent.namelen);
1059 0 : if (error)
1060 0 : return error;
1061 0 : sd->namebuf[MAXNAMELEN - 1] = 0;
1062 :
1063 0 : error = xchk_dir_slow_dirent(sd, &dirent);
1064 0 : if (error)
1065 0 : return error;
1066 : }
1067 :
1068 : return 0;
1069 : }
1070 :
1071 : /* Scrub a whole directory. */
1072 : int
1073 19366194 : xchk_directory(
1074 : struct xfs_scrub *sc)
1075 : {
1076 19366194 : struct xchk_dir *sd;
1077 19366194 : int error;
1078 :
1079 19366194 : if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
1080 : return -ENOENT;
1081 :
1082 : /* Plausible size? */
1083 19366194 : if (sc->ip->i_disk_size < xfs_dir2_sf_hdr_size(0)) {
1084 0 : xchk_ino_set_corrupt(sc, sc->ip->i_ino);
1085 0 : return 0;
1086 : }
1087 :
1088 : /* Check directory tree structure */
1089 19366194 : error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
1090 19318359 : if (error)
1091 : return error;
1092 :
1093 19318352 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
1094 : return 0;
1095 :
1096 : /* Check the freespace. */
1097 19319276 : error = xchk_directory_blocks(sc);
1098 19322580 : if (error)
1099 : return error;
1100 :
1101 19322580 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
1102 : return 0;
1103 :
1104 19320188 : sd = kvzalloc(sizeof(struct xchk_dir), XCHK_GFP_FLAGS);
1105 19375647 : if (!sd)
1106 : return -ENOMEM;
1107 19375647 : sd->sc = sc;
1108 :
1109 19375647 : if (xfs_has_parent(sc->mp)) {
1110 18672578 : char *descr;
1111 :
1112 : /*
1113 : * Set up some staging memory for dirents that we can't check
1114 : * due to locking contention.
1115 : */
1116 18672578 : descr = xchk_xfile_ino_descr(sc, "slow directory entries");
1117 18617463 : error = xfarray_create(descr, 0, sizeof(struct xchk_dirent),
1118 : &sd->dir_entries);
1119 18590292 : kfree(descr);
1120 18650053 : if (error)
1121 0 : goto out_sd;
1122 :
1123 18650053 : descr = xchk_xfile_ino_descr(sc, "slow directory entry names");
1124 18624455 : error = xfblob_create(descr, &sd->dir_names);
1125 18689725 : kfree(descr);
1126 18672250 : if (error)
1127 0 : goto out_entries;
1128 : }
1129 :
1130 : /* Look up every name in this directory by hash. */
1131 19375319 : error = xchk_dir_walk(sc, sc->ip, xchk_dir_actor, sd);
1132 19391167 : if (error == -ECANCELED)
1133 : error = 0;
1134 19395794 : if (error)
1135 9 : goto out_names;
1136 :
1137 19391158 : if (xfs_has_parent(sc->mp)) {
1138 18686485 : error = xchk_dir_finish_slow_dirents(sd);
1139 18672589 : if (error)
1140 0 : goto out_names;
1141 : }
1142 :
1143 19377262 : out_names:
1144 19377271 : if (sd->dir_names)
1145 18672601 : xfblob_destroy(sd->dir_names);
1146 704670 : out_entries:
1147 19381875 : if (sd->dir_entries)
1148 18677219 : xfarray_destroy(sd->dir_entries);
1149 704656 : out_sd:
1150 19386943 : kvfree(sd);
1151 19386943 : return error;
1152 : }
1153 :
1154 : /*
1155 : * Decide if this directory has been zapped to satisfy the inode and ifork
1156 : * verifiers. Checking and repairing should be postponed until the directory
1157 : * is fixed.
1158 : */
1159 : bool
1160 180163601 : xchk_dir_looks_zapped(
1161 : struct xfs_inode *dp)
1162 : {
1163 : /*
1164 : * If the dinode repair found a bad data fork, it will reset the fork
1165 : * to extents format with zero records and wait for the bmapbtd
1166 : * scrubber to reconstruct the block mappings. Directories always
1167 : * contain some content, so this is a clear sign of a zapped directory.
1168 : */
1169 180163601 : if (dp->i_df.if_format == XFS_DINODE_FMT_EXTENTS)
1170 767289 : return dp->i_df.if_nextents == 0;
1171 :
1172 : return false;
1173 : }
|