1
2
3
4
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
33int
34xchk_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
42
43
44
45struct xchk_dir_ctx {
46
47 struct dir_context dir_iter;
48
49 struct xfs_scrub *sc;
50};
51
52
53STATIC int
54xchk_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
74
75
76
77
78
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
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);
91out:
92 return error;
93}
94
95
96
97
98
99
100
101
102
103STATIC int
104xchk_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
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 if (!strncmp(".", name, namelen)) {
133
134 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
135 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
136 offset);
137 if (ino != ip->i_ino)
138 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
139 offset);
140 } else if (!strncmp("..", name, namelen)) {
141
142
143
144
145 if (xfs_sb_version_hasftype(&mp->m_sb) && type != DT_DIR)
146 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
147 offset);
148 if (ip->i_ino == mp->m_sb.sb_rootino && ino != ip->i_ino)
149 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK,
150 offset);
151 }
152
153
154 xname.name = name;
155 xname.len = namelen;
156 xname.type = XFS_DIR3_FT_UNKNOWN;
157
158 error = xfs_dir_lookup(sdc->sc->tp, ip, &xname, &lookup_ino, NULL);
159 if (!xchk_fblock_process_error(sdc->sc, XFS_DATA_FORK, offset,
160 &error))
161 goto out;
162 if (lookup_ino != ino) {
163 xchk_fblock_set_corrupt(sdc->sc, XFS_DATA_FORK, offset);
164 goto out;
165 }
166
167
168 error = xchk_dir_check_ftype(sdc, offset, lookup_ino, type);
169 if (error)
170 goto out;
171out:
172
173
174
175
176
177 if (error == 0 && sdc->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
178 return -EFSCORRUPTED;
179 return error;
180}
181
182
183STATIC int
184xchk_dir_rec(
185 struct xchk_da_btree *ds,
186 int level,
187 void *rec)
188{
189 struct xfs_mount *mp = ds->state->mp;
190 struct xfs_dir2_leaf_entry *ent = rec;
191 struct xfs_inode *dp = ds->dargs.dp;
192 struct xfs_dir2_data_entry *dent;
193 struct xfs_buf *bp;
194 char *p, *endp;
195 xfs_ino_t ino;
196 xfs_dablk_t rec_bno;
197 xfs_dir2_db_t db;
198 xfs_dir2_data_aoff_t off;
199 xfs_dir2_dataptr_t ptr;
200 xfs_dahash_t calc_hash;
201 xfs_dahash_t hash;
202 unsigned int tag;
203 int error;
204
205
206 error = xchk_da_btree_hash(ds, level, &ent->hashval);
207 if (error)
208 goto out;
209
210
211 ptr = be32_to_cpu(ent->address);
212 if (ptr == 0)
213 return 0;
214
215
216 db = xfs_dir2_dataptr_to_db(mp->m_dir_geo, ptr);
217 off = xfs_dir2_dataptr_to_off(mp->m_dir_geo, ptr);
218 rec_bno = xfs_dir2_db_to_da(mp->m_dir_geo, db);
219
220 if (rec_bno >= mp->m_dir_geo->leafblk) {
221 xchk_da_set_corrupt(ds, level);
222 goto out;
223 }
224 error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno, -2, &bp);
225 if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
226 &error))
227 goto out;
228 if (!bp) {
229 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
230 goto out;
231 }
232 xchk_buffer_recheck(ds->sc, bp);
233
234 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
235 goto out_relse;
236
237 dent = (struct xfs_dir2_data_entry *)(((char *)bp->b_addr) + off);
238
239
240 p = (char *)mp->m_dir_inode_ops->data_entry_p(bp->b_addr);
241 endp = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
242 if (!endp) {
243 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
244 goto out_relse;
245 }
246 while (p < endp) {
247 struct xfs_dir2_data_entry *dep;
248 struct xfs_dir2_data_unused *dup;
249
250 dup = (struct xfs_dir2_data_unused *)p;
251 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
252 p += be16_to_cpu(dup->length);
253 continue;
254 }
255 dep = (struct xfs_dir2_data_entry *)p;
256 if (dep == dent)
257 break;
258 p += mp->m_dir_inode_ops->data_entsize(dep->namelen);
259 }
260 if (p >= endp) {
261 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
262 goto out_relse;
263 }
264
265
266 ino = be64_to_cpu(dent->inumber);
267 hash = be32_to_cpu(ent->hashval);
268 tag = be16_to_cpup(dp->d_ops->data_entry_tag_p(dent));
269 if (!xfs_verify_dir_ino(mp, ino) || tag != off)
270 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
271 if (dent->namelen == 0) {
272 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
273 goto out_relse;
274 }
275 calc_hash = xfs_da_hashname(dent->name, dent->namelen);
276 if (calc_hash != hash)
277 xchk_fblock_set_corrupt(ds->sc, XFS_DATA_FORK, rec_bno);
278
279out_relse:
280 xfs_trans_brelse(ds->dargs.trans, bp);
281out:
282 return error;
283}
284
285
286
287
288
289
290STATIC void
291xchk_directory_check_free_entry(
292 struct xfs_scrub *sc,
293 xfs_dablk_t lblk,
294 struct xfs_dir2_data_free *bf,
295 struct xfs_dir2_data_unused *dup)
296{
297 struct xfs_dir2_data_free *dfp;
298 unsigned int dup_length;
299
300 dup_length = be16_to_cpu(dup->length);
301
302
303 if (dup_length < be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
304 return;
305
306 for (dfp = &bf[XFS_DIR2_DATA_FD_COUNT - 1]; dfp >= bf; dfp--)
307 if (dup_length == be16_to_cpu(dfp->length))
308 return;
309
310
311 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
312}
313
314
315STATIC int
316xchk_directory_data_bestfree(
317 struct xfs_scrub *sc,
318 xfs_dablk_t lblk,
319 bool is_block)
320{
321 struct xfs_dir2_data_unused *dup;
322 struct xfs_dir2_data_free *dfp;
323 struct xfs_buf *bp;
324 struct xfs_dir2_data_free *bf;
325 struct xfs_mount *mp = sc->mp;
326 const struct xfs_dir_ops *d_ops;
327 char *ptr;
328 char *endptr;
329 u16 tag;
330 unsigned int nr_bestfrees = 0;
331 unsigned int nr_frees = 0;
332 unsigned int smallest_bestfree;
333 int newlen;
334 int offset;
335 int error;
336
337 d_ops = sc->ip->d_ops;
338
339 if (is_block) {
340
341 if (lblk != XFS_B_TO_FSBT(mp, XFS_DIR2_DATA_OFFSET))
342 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
343 error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
344 } else {
345
346 error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, -1, &bp);
347 }
348 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
349 goto out;
350 xchk_buffer_recheck(sc, bp);
351
352
353
354 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
355 goto out_buf;
356
357
358 bf = d_ops->data_bestfree_p(bp->b_addr);
359 smallest_bestfree = UINT_MAX;
360 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
361 offset = be16_to_cpu(dfp->offset);
362 if (offset == 0)
363 continue;
364 if (offset >= mp->m_dir_geo->blksize) {
365 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
366 goto out_buf;
367 }
368 dup = (struct xfs_dir2_data_unused *)(bp->b_addr + offset);
369 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
370
371
372 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG) ||
373 be16_to_cpu(dup->length) != be16_to_cpu(dfp->length) ||
374 tag != ((char *)dup - (char *)bp->b_addr)) {
375 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
376 goto out_buf;
377 }
378
379
380 if (smallest_bestfree < be16_to_cpu(dfp->length)) {
381 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
382 goto out_buf;
383 }
384
385 smallest_bestfree = be16_to_cpu(dfp->length);
386 nr_bestfrees++;
387 }
388
389
390 ptr = (char *)d_ops->data_entry_p(bp->b_addr);
391 endptr = xfs_dir3_data_endp(mp->m_dir_geo, bp->b_addr);
392
393
394 while (ptr < endptr) {
395 dup = (struct xfs_dir2_data_unused *)ptr;
396
397 if (dup->freetag != cpu_to_be16(XFS_DIR2_DATA_FREE_TAG)) {
398 struct xfs_dir2_data_entry *dep;
399
400 dep = (struct xfs_dir2_data_entry *)ptr;
401 newlen = d_ops->data_entsize(dep->namelen);
402 if (newlen <= 0) {
403 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
404 lblk);
405 goto out_buf;
406 }
407 ptr += newlen;
408 continue;
409 }
410
411
412 tag = be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup));
413 if (tag != ((char *)dup - (char *)bp->b_addr)) {
414 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
415 goto out_buf;
416 }
417
418
419
420
421
422 xchk_directory_check_free_entry(sc, lblk, bf, dup);
423 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
424 goto out_buf;
425
426
427 newlen = be16_to_cpu(dup->length);
428 if (newlen <= 0) {
429 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
430 goto out_buf;
431 }
432 ptr += newlen;
433 if (ptr <= endptr)
434 nr_frees++;
435 }
436
437
438 if (ptr != endptr)
439 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
440
441
442 if (nr_frees < nr_bestfrees)
443 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
444out_buf:
445 xfs_trans_brelse(sc->tp, bp);
446out:
447 return error;
448}
449
450
451
452
453
454
455
456STATIC void
457xchk_directory_check_freesp(
458 struct xfs_scrub *sc,
459 xfs_dablk_t lblk,
460 struct xfs_buf *dbp,
461 unsigned int len)
462{
463 struct xfs_dir2_data_free *dfp;
464
465 dfp = sc->ip->d_ops->data_bestfree_p(dbp->b_addr);
466
467 if (len != be16_to_cpu(dfp->length))
468 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
469
470 if (len > 0 && be16_to_cpu(dfp->offset) == 0)
471 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
472}
473
474
475STATIC int
476xchk_directory_leaf1_bestfree(
477 struct xfs_scrub *sc,
478 struct xfs_da_args *args,
479 xfs_dablk_t lblk)
480{
481 struct xfs_dir3_icleaf_hdr leafhdr;
482 struct xfs_dir2_leaf_entry *ents;
483 struct xfs_dir2_leaf_tail *ltp;
484 struct xfs_dir2_leaf *leaf;
485 struct xfs_buf *dbp;
486 struct xfs_buf *bp;
487 const struct xfs_dir_ops *d_ops = sc->ip->d_ops;
488 struct xfs_da_geometry *geo = sc->mp->m_dir_geo;
489 __be16 *bestp;
490 __u16 best;
491 __u32 hash;
492 __u32 lasthash = 0;
493 __u32 bestcount;
494 unsigned int stale = 0;
495 int i;
496 int error;
497
498
499 error = xfs_dir3_leaf_read(sc->tp, sc->ip, lblk, -1, &bp);
500 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
501 goto out;
502 xchk_buffer_recheck(sc, bp);
503
504 leaf = bp->b_addr;
505 d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
506 ents = d_ops->leaf_ents_p(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
520
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
528 if (leafhdr.count > d_ops->leaf_max_ents(geo)) {
529 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
530 goto out;
531 }
532
533
534 if ((char *)&ents[leafhdr.count] > (char *)bestp) {
535 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
536 goto out;
537 }
538
539
540 for (i = 0; i < leafhdr.count; i++) {
541 hash = be32_to_cpu(ents[i].hashval);
542 if (i > 0 && lasthash > hash)
543 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
544 lasthash = hash;
545 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
546 stale++;
547 }
548 if (leafhdr.stale != stale)
549 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
550 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
551 goto out;
552
553
554 for (i = 0; i < bestcount; i++, bestp++) {
555 best = be16_to_cpu(*bestp);
556 if (best == NULLDATAOFF)
557 continue;
558 error = xfs_dir3_data_read(sc->tp, sc->ip,
559 i * args->geo->fsbcount, -1, &dbp);
560 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
561 &error))
562 break;
563 xchk_directory_check_freesp(sc, lblk, dbp, best);
564 xfs_trans_brelse(sc->tp, dbp);
565 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
566 goto out;
567 }
568out:
569 return error;
570}
571
572
573STATIC int
574xchk_directory_free_bestfree(
575 struct xfs_scrub *sc,
576 struct xfs_da_args *args,
577 xfs_dablk_t lblk)
578{
579 struct xfs_dir3_icfree_hdr freehdr;
580 struct xfs_buf *dbp;
581 struct xfs_buf *bp;
582 __be16 *bestp;
583 __u16 best;
584 unsigned int stale = 0;
585 int i;
586 int error;
587
588
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
602 sc->ip->d_ops->free_hdr_from_disk(&freehdr, bp->b_addr);
603 bestp = sc->ip->d_ops->free_bests_p(bp->b_addr);
604 for (i = 0; i < freehdr.nvalid; i++, bestp++) {
605 best = be16_to_cpu(*bestp);
606 if (best == NULLDATAOFF) {
607 stale++;
608 continue;
609 }
610 error = xfs_dir3_data_read(sc->tp, sc->ip,
611 (freehdr.firstdb + i) * args->geo->fsbcount,
612 -1, &dbp);
613 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
614 &error))
615 break;
616 xchk_directory_check_freesp(sc, lblk, dbp, best);
617 xfs_trans_brelse(sc->tp, dbp);
618 }
619
620 if (freehdr.nused + stale != freehdr.nvalid)
621 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
622out:
623 return error;
624}
625
626
627STATIC int
628xchk_directory_blocks(
629 struct xfs_scrub *sc)
630{
631 struct xfs_bmbt_irec got;
632 struct xfs_da_args args;
633 struct xfs_ifork *ifp;
634 struct xfs_mount *mp = sc->mp;
635 xfs_fileoff_t leaf_lblk;
636 xfs_fileoff_t free_lblk;
637 xfs_fileoff_t lblk;
638 struct xfs_iext_cursor icur;
639 xfs_dablk_t dabno;
640 bool found;
641 int is_block = 0;
642 int error;
643
644
645 if (sc->ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
646 sc->ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
647 return 0;
648
649 ifp = XFS_IFORK_PTR(sc->ip, XFS_DATA_FORK);
650 lblk = XFS_B_TO_FSB(mp, XFS_DIR2_DATA_OFFSET);
651 leaf_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_LEAF_OFFSET);
652 free_lblk = XFS_B_TO_FSB(mp, XFS_DIR2_FREE_OFFSET);
653
654
655 args.dp = sc->ip;
656 args.geo = mp->m_dir_geo;
657 args.trans = sc->tp;
658 error = xfs_dir2_isblock(&args, &is_block);
659 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
660 goto out;
661
662
663 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
664 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
665
666 if (is_block &&
667 (got.br_startoff > 0 ||
668 got.br_blockcount != args.geo->fsbcount)) {
669 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
670 got.br_startoff);
671 break;
672 }
673
674
675 if (got.br_startoff >= leaf_lblk)
676 break;
677
678
679
680
681
682
683
684
685
686
687
688
689 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
690 args.geo->fsbcount);
691 lblk < got.br_startoff + got.br_blockcount;
692 lblk += args.geo->fsbcount) {
693 error = xchk_directory_data_bestfree(sc, lblk,
694 is_block);
695 if (error)
696 goto out;
697 }
698 dabno = got.br_startoff + got.br_blockcount;
699 lblk = roundup(dabno, args.geo->fsbcount);
700 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
701 }
702
703 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
704 goto out;
705
706
707 if (xfs_iext_lookup_extent(sc->ip, ifp, leaf_lblk, &icur, &got) &&
708 got.br_startoff == leaf_lblk &&
709 got.br_blockcount == args.geo->fsbcount &&
710 !xfs_iext_next_extent(ifp, &icur, &got)) {
711 if (is_block) {
712 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
713 goto out;
714 }
715 error = xchk_directory_leaf1_bestfree(sc, &args,
716 leaf_lblk);
717 if (error)
718 goto out;
719 }
720
721 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
722 goto out;
723
724
725 lblk = free_lblk;
726 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
727 while (found && !(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
728
729
730
731
732 lblk = got.br_startoff;
733 if (lblk & ~0xFFFFFFFFULL) {
734 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
735 goto out;
736 }
737 if (is_block) {
738 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, lblk);
739 goto out;
740 }
741
742
743
744
745
746
747
748
749
750
751
752
753 for (lblk = roundup((xfs_dablk_t)got.br_startoff,
754 args.geo->fsbcount);
755 lblk < got.br_startoff + got.br_blockcount;
756 lblk += args.geo->fsbcount) {
757 error = xchk_directory_free_bestfree(sc, &args,
758 lblk);
759 if (error)
760 goto out;
761 }
762 dabno = got.br_startoff + got.br_blockcount;
763 lblk = roundup(dabno, args.geo->fsbcount);
764 found = xfs_iext_lookup_extent(sc->ip, ifp, lblk, &icur, &got);
765 }
766out:
767 return error;
768}
769
770
771int
772xchk_directory(
773 struct xfs_scrub *sc)
774{
775 struct xchk_dir_ctx sdc = {
776 .dir_iter.actor = xchk_dir_actor,
777 .dir_iter.pos = 0,
778 .sc = sc,
779 };
780 size_t bufsize;
781 loff_t oldpos;
782 int error = 0;
783
784 if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
785 return -ENOENT;
786
787
788 if (sc->ip->i_d.di_size < xfs_dir2_sf_hdr_size(0)) {
789 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
790 goto out;
791 }
792
793
794 error = xchk_da_btree(sc, XFS_DATA_FORK, xchk_dir_rec, NULL);
795 if (error)
796 return error;
797
798 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
799 return error;
800
801
802 error = xchk_directory_blocks(sc);
803 if (error)
804 return error;
805
806 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
807 return error;
808
809
810
811
812
813 bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE,
814 sc->ip->i_d.di_size);
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832 oldpos = 0;
833 sc->ilock_flags &= ~XFS_ILOCK_EXCL;
834 xfs_iunlock(sc->ip, XFS_ILOCK_EXCL);
835 while (true) {
836 error = xfs_readdir(sc->tp, sc->ip, &sdc.dir_iter, bufsize);
837 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0,
838 &error))
839 goto out;
840 if (oldpos == sdc.dir_iter.pos)
841 break;
842 oldpos = sdc.dir_iter.pos;
843 }
844
845out:
846 return error;
847}
848