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