OSDN Git Service

910e0bf85bd78b491e4a52ed03a09faa8f203012
[tomoyo/tomoyo-test1.git] / fs / xfs / scrub / dir.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
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
22 /* Set us up to scrub directories. */
23 int
24 xchk_setup_directory(
25         struct xfs_scrub        *sc,
26         struct xfs_inode        *ip)
27 {
28         return xchk_setup_inode_contents(sc, ip, 0);
29 }
30
31 /* Directories */
32
33 /* Scrub a directory entry. */
34
35 struct xchk_dir_ctx {
36         /* VFS fill-directory iterator */
37         struct dir_context      dir_iter;
38
39         struct xfs_scrub        *sc;
40 };
41
42 /* Check that an inode's mode matches a given DT_ type. */
43 STATIC int
44 xchk_dir_check_ftype(
45         struct xchk_dir_ctx     *sdc,
46         xfs_fileoff_t           offset,
47         xfs_ino_t               inum,
48         int                     dtype)
49 {
50         struct xfs_mount        *mp = sdc->sc->mp;
51         struct xfs_inode        *ip;
52         int                     ino_dtype;
53         int                     error = 0;
54
55         if (!xfs_sb_version_hasftype(&mp->m_sb)) {
56                 if (dtype != DT_UNKNOWN && dtype != DT_DIR)
57                         xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
58                                         offset);
59                 goto out;
60         }
61
62         /*
63          * Grab the inode pointed to by the dirent.  We release the
64          * inode before we cancel the scrub transaction.  Since we're
65          * don't know a priori that releasing the inode won't trigger
66          * eofblocks cleanup (which allocates what would be a nested
67          * transaction), we can't use DONTCACHE here because DONTCACHE
68          * inodes can trigger immediate inactive cleanup of the inode.
69          */
70         error = xfs_iget(mp, sdc->sc->tp, inum, 0, 0, &ip);
71         if (!xchk_fblock_xref_process_error(sdc->sc, XFS_DATA_FORK, offset,
72                         &error))
73                 goto out;
74
75         /* Convert mode to the DT_* values that dir_emit uses. */
76         ino_dtype = xfs_dir3_get_dtype(mp,
77                         xfs_mode_to_ftype(VFS_I(ip)->i_mode));
78         if (ino_dtype != dtype)
79                 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
80         xfs_irele(ip);
81 out:
82         return error;
83 }
84
85 /*
86  * Scrub a single directory entry.
87  *
88  * We use the VFS directory iterator (i.e. readdir) to call this
89  * function for every directory entry in a directory.  Once we're here,
90  * we check the inode number to make sure it's sane, then we check that
91  * we can look up this filename.  Finally, we check the ftype.
92  */
93 STATIC int
94 xchk_dir_actor(
95         struct dir_context      *dir_iter,
96         const char              *name,
97         int                     namelen,
98         loff_t                  pos,
99         u64                     ino,
100         unsigned                type)
101 {
102         struct xfs_mount        *mp;
103         struct xfs_inode        *ip;
104         struct xchk_dir_ctx     *sdc;
105         struct xfs_name         xname;
106         xfs_ino_t               lookup_ino;
107         xfs_dablk_t             offset;
108         int                     error = 0;
109
110         sdc = container_of(dir_iter, struct xchk_dir_ctx, dir_iter);
111         ip = sdc->sc->ip;
112         mp = ip->i_mount;
113         offset = xfs_dir2_db_to_da(mp->m_dir_geo,
114                         xfs_dir2_dataptr_to_db(mp->m_dir_geo, pos));
115
116         if (xchk_should_terminate(sdc->sc, &error))
117                 return error;
118
119         /* Does this inode number make sense? */
120         if (!xfs_verify_dir_ino(mp, ino)) {
121                 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
122                 goto out;
123         }
124
125         /* Does this name make sense? */
126         if (!xfs_dir2_namecheck(name, namelen)) {
127                 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
128                 goto out;
129         }
130
131         if (!strncmp(".", name, namelen)) {
132                 /* If this is "." then check that the inum matches the dir. */
133                 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
134                         xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
135                                         offset);
136                 if (ino != ip->i_ino)
137                         xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
138                                         offset);
139         } else if (!strncmp("..", name, namelen)) {
140                 /*
141                  * If this is ".." in the root inode, check that the inum
142                  * matches this dir.
143                  */
144                 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
145                         xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
146                                         offset);
147                 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
148                         xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
149                                         offset);
150         }
151
152         /* Verify that we can look up this name by hash. */
153         xname.name = name;
154         xname.len = namelen;
155         xname.type = XFS_DIR3_FT_UNKNOWN;
156
157         error = xfs_dir_lookup(sdc->sc->tp, ip, &xname, &lookup_ino, NULL);
158         if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
159                         &error))
160                 goto out;
161         if (lookup_ino != ino) {
162                 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
163                 goto out;
164         }
165
166         /* Verify the file type.  This function absorbs error codes. */
167         error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type);
168         if (error)
169                 goto out;
170 out:
171         /*
172          * A negative error code returned here is supposed to cause the
173          * dir_emit caller (xfs_readdir) to abort the directory iteration
174          * and return zero to xchk_directory.
175          */
176         if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
177                 return -EFSCORRUPTED;
178         return error;
179 }
180
181 /* Scrub a directory btree record. */
182 STATIC int
183 xchk_dir_rec(
184         struct xchk_da_btree            *ds,
185         int                             level)
186 {
187         struct xfs_da_state_blk         *blk = &ds->state->path.blk[level];
188         struct xfs_mount                *mp = ds->state->mp;
189         struct xfs_inode                *dp = ds->dargs.dp;
190         struct xfs_da_geometry          *geo = mp->m_dir_geo;
191         struct xfs_dir2_data_entry      *dent;
192         struct xfs_buf                  *bp;
193         struct xfs_dir2_leaf_entry      *ent;
194         unsigned int                    end;
195         unsigned int                    iter_off;
196         xfs_ino_t                       ino;
197         xfs_dablk_t                     rec_bno;
198         xfs_dir2_db_t                   db;
199         xfs_dir2_data_aoff_t            off;
200         xfs_dir2_dataptr_t              ptr;
201         xfs_dahash_t                    calc_hash;
202         xfs_dahash_t                    hash;
203         struct xfs_dir3_icleaf_hdr      hdr;
204         unsigned int                    tag;
205         int                             error;
206
207         ASSERT(blk->magic == XFS_DIR2_LEAF1_MAGIC ||
208                blk->magic == XFS_DIR2_LEAFN_MAGIC);
209
210         xfs_dir2_leaf_hdr_from_disk(mp, &hdr, blk->bp->b_addr);
211         ent = hdr.ents + blk->index;
212
213         /* Check the hash of the entry. */
214         error = xchk_da_btree_hash(ds, level, &ent->hashval);
215         if (error)
216                 goto out;
217
218         /* Valid hash pointer? */
219         ptr = be32_to_cpu(ent->address);
220         if (ptr == 0)
221                 return 0;
222
223         /* Find the directory entry's location. */
224         db = xfs_dir2_dataptr_to_db(geo, ptr);
225         off = xfs_dir2_dataptr_to_off(geo, ptr);
226         rec_bno = xfs_dir2_db_to_da(geo, db);
227
228         if (rec_bno >= geo->leafblk) {
229                 xchk_da_set_corrupt(ds, level);
230                 goto out;
231         }
232         error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno, -2, &bp);
233         if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
234                         &error))
235                 goto out;
236         if (!bp) {
237                 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
238                 goto out;
239         }
240         xchk_buffer_recheck(ds->sc, bp);
241
242         if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
243                 goto out_relse;
244
245         dent = bp->b_addr + off;
246
247         /* Make sure we got a real directory entry. */
248         iter_off = geo->data_entry_offset;
249         end = xfs_dir3_data_end_offset(geo, bp->b_addr);
250         if (!end) {
251                 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
252                 goto out_relse;
253         }
254         for (;;) {
255                 struct xfs_dir2_data_entry      *dep = bp->b_addr + iter_off;
256                 struct xfs_dir2_data_unused     *dup = bp->b_addr + iter_off;
257
258                 if (iter_off >= end) {
259                         xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
260                         goto out_relse;
261                 }
262
263                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
264                         iter_off += be16_to_cpu(dup->length);
265                         continue;
266                 }
267                 if (dep == dent)
268                         break;
269                 iter_off += xfs_dir2_data_entsize(mp, dep->namelen);
270         }
271
272         /* Retrieve the entry, sanity check it, and compare hashes. */
273         ino = be64_to_cpu(dent->inumber);
274         hash = be32_to_cpu(ent->hashval);
275         tag = be16_to_cpup(xfs_dir2_data_entry_tag_p(mp, dent));
276         if (!xfs_verify_dir_ino(mp, ino) || tag != off)
277                 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
278         if (dent->namelen == 0) {
279                 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
280                 goto out_relse;
281         }
282         calc_hash = xfs_da_hashname(dent->name, dent->namelen);
283         if (calc_hash != hash)
284                 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
285
286 out_relse:
287         xfs_trans_brelse(ds->dargs.trans, bp);
288 out:
289         return error;
290 }
291
292 /*
293  * Is this unused entry either in the bestfree or smaller than all of
294  * them?  We've already checked that the bestfrees are sorted longest to
295  * shortest, and that there aren't any bogus entries.
296  */
297 STATIC void
298 xchk_directory_check_free_entry(
299         struct xfs_scrub                *sc,
300         xfs_dablk_t                     lblk,
301         struct xfs_dir2_data_free       *bf,
302         struct xfs_dir2_data_unused     *dup)
303 {
304         struct xfs_dir2_data_free       *dfp;
305         unsigned int                    dup_length;
306
307         dup_length = be16_to_cpu(dup->length);
308
309         /* Unused entry is shorter than any of the bestfrees */
310         if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
311                 return;
312
313         for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
314                 if (dup_length == be16_to_cpu(dfp->length))
315                         return;
316
317         /* Unused entry should be in the bestfrees but wasn't found. */
318         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
319 }
320
321 /* Check free space info in a directory data block. */
322 STATIC int
323 xchk_directory_data_bestfree(
324         struct xfs_scrub                *sc,
325         xfs_dablk_t                     lblk,
326         bool                            is_block)
327 {
328         struct xfs_dir2_data_unused     *dup;
329         struct xfs_dir2_data_free       *dfp;
330         struct xfs_buf                  *bp;
331         struct xfs_dir2_data_free       *bf;
332         struct xfs_mount                *mp = sc->mp;
333         u16                             tag;
334         unsigned int                    nr_bestfrees = 0;
335         unsigned int                    nr_frees = 0;
336         unsigned int                    smallest_bestfree;
337         int                             newlen;
338         unsigned int                    offset;
339         unsigned int                    end;
340         int                             error;
341
342         if (is_block) {
343                 /* dir block format */
344                 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
345                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
346                 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
347         } else {
348                 /* dir data format */
349                 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, -1, &bp);
350         }
351         if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
352                 goto out;
353         xchk_buffer_recheck(sc, bp);
354
355         /* XXX: Check xfs_dir3_data_hdr.pad is zero once we start setting it. */
356
357         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
358                 goto out_buf;
359
360         /* Do the bestfrees correspond to actual free space? */
361         bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
362         smallest_bestfree = UINT_MAX;
363         for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
364                 offset = be16_to_cpu(dfp->offset);
365                 if (offset == 0)
366                         continue;
367                 if (offset >= mp->m_dir_geo->blksize) {
368                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
369                         goto out_buf;
370                 }
371                 dup = bp->b_addr + offset;
372                 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
373
374                 /* bestfree doesn't match the entry it points at? */
375                 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
376                     be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
377                     tag != offset) {
378                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
379                         goto out_buf;
380                 }
381
382                 /* bestfree records should be ordered largest to smallest */
383                 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
384                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
385                         goto out_buf;
386                 }
387
388                 smallest_bestfree = be16_to_cpu(dfp->length);
389                 nr_bestfrees++;
390         }
391
392         /* Make sure the bestfrees are actually the best free spaces. */
393         offset = mp->m_dir_geo->data_entry_offset;
394         end = xfs_dir3_data_end_offset(mp->m_dir_geo, bp->b_addr);
395
396         /* Iterate the entries, stopping when we hit or go past the end. */
397         while (offset < end) {
398                 dup = bp->b_addr + offset;
399
400                 /* Skip real entries */
401                 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
402                         struct xfs_dir2_data_entry *dep = bp->b_addr + offset;
403
404                         newlen = xfs_dir2_data_entsize(mp, dep->namelen);
405                         if (newlen <= 0) {
406                                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
407                                                 lblk);
408                                 goto out_buf;
409                         }
410                         offset += newlen;
411                         continue;
412                 }
413
414                 /* Spot check this free entry */
415                 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
416                 if (tag != offset) {
417                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
418                         goto out_buf;
419                 }
420
421                 /*
422                  * Either this entry is a bestfree or it's smaller than
423                  * any of the bestfrees.
424                  */
425                 xchk_directory_check_free_entry(sc, lblk, bf, dup);
426                 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
427                         goto out_buf;
428
429                 /* Move on. */
430                 newlen = be16_to_cpu(dup->length);
431                 if (newlen <= 0) {
432                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
433                         goto out_buf;
434                 }
435                 offset += newlen;
436                 if (offset <= end)
437                         nr_frees++;
438         }
439
440         /* We're required to fill all the space. */
441         if (offset != end)
442                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
443
444         /* Did we see at least as many free slots as there are bestfrees? */
445         if (nr_frees < nr_bestfrees)
446                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
447 out_buf:
448         xfs_trans_brelse(sc->tp, bp);
449 out:
450         return error;
451 }
452
453 /*
454  * Does the free space length in the free space index block ($len) match
455  * the longest length in the directory data block's bestfree array?
456  * Assume that we've already checked that the data block's bestfree
457  * array is in order.
458  */
459 STATIC void
460 xchk_directory_check_freesp(
461         struct xfs_scrub                *sc,
462         xfs_dablk_t                     lblk,
463         struct xfs_buf                  *dbp,
464         unsigned int                    len)
465 {
466         struct xfs_dir2_data_free       *dfp;
467
468         dfp = xfs_dir2_data_bestfree_p(sc->mp, dbp->b_addr);
469
470         if (len != be16_to_cpu(dfp->length))
471                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
472
473         if (len > 0 && be16_to_cpu(dfp->offset) == 0)
474                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
475 }
476
477 /* Check free space info in a directory leaf1 block. */
478 STATIC int
479 xchk_directory_leaf1_bestfree(
480         struct xfs_scrub                *sc,
481         struct xfs_da_args              *args,
482         xfs_dablk_t                     lblk)
483 {
484         struct xfs_dir3_icleaf_hdr      leafhdr;
485         struct xfs_dir2_leaf_tail       *ltp;
486         struct xfs_dir2_leaf            *leaf;
487         struct xfs_buf                  *dbp;
488         struct xfs_buf                  *bp;
489         struct xfs_da_geometry          *geo = sc->mp->m_dir_geo;
490         __be16                          *bestp;
491         __u16                           best;
492         __u32                           hash;
493         __u32                           lasthash = 0;
494         __u32                           bestcount;
495         unsigned int                    stale = 0;
496         int                             i;
497         int                             error;
498
499         /* Read the free space block. */
500         error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, &bp);
501         if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
502                 goto out;
503         xchk_buffer_recheck(sc, bp);
504
505         leaf = bp->b_addr;
506         xfs_dir2_leaf_hdr_from_disk(sc->ip->i_mount, &leafhdr, leaf);
507         ltp = xfs_dir2_leaf_tail_p(geo, leaf);
508         bestcount = be32_to_cpu(ltp->bestcount);
509         bestp = xfs_dir2_leaf_bests_p(ltp);
510
511         if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
512                 struct xfs_dir3_leaf_hdr        *hdr3 = bp->b_addr;
513
514                 if (hdr3->pad != cpu_to_be32(0))
515                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
516         }
517
518         /*
519          * There should be as many bestfree slots as there are dir data
520          * blocks that can fit under i_size.
521          */
522         if (bestcount != xfs_dir2_byte_to_db(geo, sc->ip->i_d.di_size)) {
523                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
524                 goto out;
525         }
526
527         /* Is the leaf count even remotely sane? */
528         if (leafhdr.count > geo->leaf_max_ents) {
529                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
530                 goto out;
531         }
532
533         /* Leaves and bests don't overlap in leaf format. */
534         if ((char *)&leafhdr.ents[leafhdr.count] > (char *)bestp) {
535                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
536                 goto out;
537         }
538
539         /* Check hash value order, count stale entries.  */
540         for (i = 0; i < leafhdr.count; i++) {
541                 hash = be32_to_cpu(leafhdr.ents[i].hashval);
542                 if (i > 0 && lasthash > hash)
543                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
544                 lasthash = hash;
545                 if (leafhdr.ents[i].address ==
546                     cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
547                         stale++;
548         }
549         if (leafhdr.stale != stale)
550                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
551         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
552                 goto out;
553
554         /* Check all the bestfree entries. */
555         for (i = 0; i < bestcount; i++, bestp++) {
556                 best = be16_to_cpu(*bestp);
557                 if (best == NULLDATAOFF)
558                         continue;
559                 error = xfs_dir3_data_read(sc->tp, sc->ip,
560                                 i * args->geo->fsbcount, -1, &dbp);
561                 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
562                                 &error))
563                         break;
564                 xchk_directory_check_freesp(sc, lblk, dbp, best);
565                 xfs_trans_brelse(sc->tp, dbp);
566                 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
567                         goto out;
568         }
569 out:
570         return error;
571 }
572
573 /* Check free space info in a directory freespace block. */
574 STATIC int
575 xchk_directory_free_bestfree(
576         struct xfs_scrub                *sc,
577         struct xfs_da_args              *args,
578         xfs_dablk_t                     lblk)
579 {
580         struct xfs_dir3_icfree_hdr      freehdr;
581         struct xfs_buf                  *dbp;
582         struct xfs_buf                  *bp;
583         __u16                           best;
584         unsigned int                    stale = 0;
585         int                             i;
586         int                             error;
587
588         /* Read the free space block */
589         error = xfs_dir2_free_read(sc->tp, sc->ip, lblk, &bp);
590         if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
591                 goto out;
592         xchk_buffer_recheck(sc, bp);
593
594         if (xfs_sb_version_hascrc(&sc->mp->m_sb)) {
595                 struct xfs_dir3_free_hdr        *hdr3 = bp->b_addr;
596
597                 if (hdr3->pad != cpu_to_be32(0))
598                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
599         }
600
601         /* Check all the entries. */
602         xfs_dir2_free_hdr_from_disk(sc->ip->i_mount, &freehdr, bp->b_addr);
603         for (i = 0; i < freehdr.nvalid; i++) {
604                 best = be16_to_cpu(freehdr.bests[i]);
605                 if (best == NULLDATAOFF) {
606                         stale++;
607                         continue;
608                 }
609                 error = xfs_dir3_data_read(sc->tp, sc->ip,
610                                 (freehdr.firstdb + i) * args->geo->fsbcount,
611                                 -1, &dbp);
612                 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
613                                 &error))
614                         break;
615                 xchk_directory_check_freesp(sc, lblk, dbp, best);
616                 xfs_trans_brelse(sc->tp, dbp);
617         }
618
619         if (freehdr.nused + stale != freehdr.nvalid)
620                 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
621 out:
622         return error;
623 }
624
625 /* Check free space information in directories. */
626 STATIC int
627 xchk_directory_blocks(
628         struct xfs_scrub        *sc)
629 {
630         struct xfs_bmbt_irec    got;
631         struct xfs_da_args      args;
632         struct xfs_ifork        *ifp;
633         struct xfs_mount        *mp = sc->mp;
634         xfs_fileoff_t           leaf_lblk;
635         xfs_fileoff_t           free_lblk;
636         xfs_fileoff_t           lblk;
637         struct xfs_iext_cursor  icur;
638         xfs_dablk_t             dabno;
639         bool                    found;
640         int                     is_block = 0;
641         int                     error;
642
643         /* Ignore local format directories. */
644         if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
645             sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
646                 return 0;
647
648         ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK);
649         lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
650         leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
651         free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
652
653         /* Is this a block dir? */
654         args.dp = sc->ip;
655         args.geo = mp->m_dir_geo;
656         args.trans = sc->tp;
657         error = xfs_dir2_isblock(&args, &is_block);
658         if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
659                 goto out;
660
661         /* Iterate all the data extents in the directory... */
662         found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
663         while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
664                 /* Block directories only have a single block at offset 0. */
665                 if (is_block &&
666                     (got.br_startoff > 0 ||
667                      got.br_blockcount != args.geo->fsbcount)) {
668                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
669                                         got.br_startoff);
670                         break;
671                 }
672
673                 /* No more data blocks... */
674                 if (got.br_startoff >= leaf_lblk)
675                         break;
676
677                 /*
678                  * Check each data block's bestfree data.
679                  *
680                  * Iterate all the fsbcount-aligned block offsets in
681                  * this directory.  The directory block reading code is
682                  * smart enough to do its own bmap lookups to handle
683                  * discontiguous directory blocks.  When we're done
684                  * with the extent record, re-query the bmap at the
685                  * next fsbcount-aligned offset to avoid redundant
686                  * block checks.
687                  */
688                 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
689                                 args.geo->fsbcount);
690                      lblk < got.br_startoff + got.br_blockcount;
691                      lblk += args.geo->fsbcount) {
692                         error = xchk_directory_data_bestfree(sc, lblk,
693                                         is_block);
694                         if (error)
695                                 goto out;
696                 }
697                 dabno = got.br_startoff + got.br_blockcount;
698                 lblk = roundup(dabno, args.geo->fsbcount);
699                 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
700         }
701
702         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
703                 goto out;
704
705         /* Look for a leaf1 block, which has free info. */
706         if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
707             got.br_startoff == leaf_lblk &&
708             got.br_blockcount == args.geo->fsbcount &&
709             !xfs_iext_next_extent(ifp, &icur, &got)) {
710                 if (is_block) {
711                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
712                         goto out;
713                 }
714                 error = xchk_directory_leaf1_bestfree(sc, &args,
715                                 leaf_lblk);
716                 if (error)
717                         goto out;
718         }
719
720         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
721                 goto out;
722
723         /* Scan for free blocks */
724         lblk = free_lblk;
725         found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
726         while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
727                 /*
728                  * Dirs can't have blocks mapped above 2^32.
729                  * Single-block dirs shouldn't even be here.
730                  */
731                 lblk = got.br_startoff;
732                 if (lblk & ~0xFFFFFFFFULL) {
733                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
734                         goto out;
735                 }
736                 if (is_block) {
737                         xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
738                         goto out;
739                 }
740
741                 /*
742                  * Check each dir free block's bestfree data.
743                  *
744                  * Iterate all the fsbcount-aligned block offsets in
745                  * this directory.  The directory block reading code is
746                  * smart enough to do its own bmap lookups to handle
747                  * discontiguous directory blocks.  When we're done
748                  * with the extent record, re-query the bmap at the
749                  * next fsbcount-aligned offset to avoid redundant
750                  * block checks.
751                  */
752                 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
753                                 args.geo->fsbcount);
754                      lblk < got.br_startoff + got.br_blockcount;
755                      lblk += args.geo->fsbcount) {
756                         error = xchk_directory_free_bestfree(sc, &args,
757                                         lblk);
758                         if (error)
759                                 goto out;
760                 }
761                 dabno = got.br_startoff + got.br_blockcount;
762                 lblk = roundup(dabno, args.geo->fsbcount);
763                 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
764         }
765 out:
766         return error;
767 }
768
769 /* Scrub a whole directory. */
770 int
771 xchk_directory(
772         struct xfs_scrub        *sc)
773 {
774         struct xchk_dir_ctx     sdc = {
775                 .dir_iter.actor = xchk_dir_actor,
776                 .dir_iter.pos = 0,
777                 .sc = sc,
778         };
779         size_t                  bufsize;
780         loff_t                  oldpos;
781         int                     error = 0;
782
783         if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
784                 return -ENOENT;
785
786         /* Plausible size? */
787         if (sc->ip->i_d.di_size < xfs_dir2_sf_hdr_size(0)) {
788                 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
789                 goto out;
790         }
791
792         /* Check directory tree structure */
793         error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
794         if (error)
795                 return error;
796
797         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
798                 return error;
799
800         /* Check the freespace. */
801         error = xchk_directory_blocks(sc);
802         if (error)
803                 return error;
804
805         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
806                 return error;
807
808         /*
809          * Check that every dirent we see can also be looked up by hash.
810          * Userspace usually asks for a 32k buffer, so we will too.
811          */
812         bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
813                         sc->ip->i_d.di_size);
814
815         /*
816          * Look up every name in this directory by hash.
817          *
818          * Use the xfs_readdir function to call xchk_dir_actor on
819          * every directory entry in this directory.  In _actor, we check
820          * the name, inode number, and ftype (if applicable) of the
821          * entry.  xfs_readdir uses the VFS filldir functions to provide
822          * iteration context.
823          *
824          * The VFS grabs a read or write lock via i_rwsem before it reads
825          * or writes to a directory.  If we've gotten this far we've
826          * already obtained IOLOCK_EXCL, which (since 4.10) is the same as
827          * getting a write lock on i_rwsem.  Therefore, it is safe for us
828          * to drop the ILOCK here in order to reuse the _readdir and
829          * _dir_lookup routines, which do their own ILOCK locking.
830          */
831         oldpos = 0;
832         sc->ilock_flags &= ~XFS_ILOCK_EXCL;
833         xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
834         while (true) {
835                 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
836                 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
837                                 &error))
838                         goto out;
839                 if (oldpos == sdc.dir_iter.pos)
840                         break;
841                 oldpos = sdc.dir_iter.pos;
842         }
843
844 out:
845         return error;
846 }