]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/dir2.c
xfs: add CRC checking to dir2 leaf blocks
[thirdparty/xfsprogs-dev.git] / repair / dir2.c
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <libxfs.h>
20 #include "avl.h"
21 #include "globals.h"
22 #include "incore.h"
23 #include "err_protos.h"
24 #include "dinode.h"
25 #include "dir2.h"
26 #include "bmap.h"
27 #include "prefetch.h"
28 #include "progress.h"
29
30 /*
31 * Tag bad directory entries with this.
32 * We can't tag them with -1 since that will look like a
33 * data_unused_t instead of a data_entry_t.
34 */
35 #define BADFSINO ((xfs_ino_t)0xfeffffffffffffffULL)
36
37 /*
38 * Known bad inode list. These are seen when the leaf and node
39 * block linkages are incorrect.
40 */
41 typedef struct dir2_bad {
42 xfs_ino_t ino;
43 struct dir2_bad *next;
44 } dir2_bad_t;
45
46 static dir2_bad_t *dir2_bad_list;
47
48 static void
49 dir2_add_badlist(
50 xfs_ino_t ino)
51 {
52 dir2_bad_t *l;
53
54 if ((l = malloc(sizeof(dir2_bad_t))) == NULL) {
55 do_error(
56 _("malloc failed (%zu bytes) dir2_add_badlist:ino %" PRIu64 "\n"),
57 sizeof(dir2_bad_t), ino);
58 exit(1);
59 }
60 l->next = dir2_bad_list;
61 dir2_bad_list = l;
62 l->ino = ino;
63 }
64
65 int
66 dir2_is_badino(
67 xfs_ino_t ino)
68 {
69 dir2_bad_t *l;
70
71 for (l = dir2_bad_list; l; l = l->next)
72 if (l->ino == ino)
73 return 1;
74 return 0;
75 }
76
77 /*
78 * takes a name and length (name need not be null-terminated)
79 * and returns 1 if the name contains a '/' or a \0, returns 0
80 * otherwise
81 */
82 int
83 namecheck(char *name, int length)
84 {
85 char *c;
86 int i;
87
88 ASSERT(length < MAXNAMELEN);
89
90 for (c = name, i = 0; i < length; i++, c++) {
91 if (*c == '/' || *c == '\0')
92 return(1);
93 }
94
95 return(0);
96 }
97
98 /*
99 * Multibuffer handling.
100 * V2 directory blocks can be noncontiguous, needing multiple buffers.
101 */
102 static struct xfs_buf *
103 da_read_buf(
104 xfs_mount_t *mp,
105 int nex,
106 bmap_ext_t *bmp)
107 {
108 #define MAP_ARRAY_SZ 4
109 struct xfs_buf_map map_array[MAP_ARRAY_SZ];
110 struct xfs_buf_map *map;
111 struct xfs_buf *bp;
112 int i;
113
114 if (nex > MAP_ARRAY_SZ) {
115 map = calloc(nex, sizeof(*map));
116 if (map == NULL) {
117 do_error(_("couldn't malloc dir2 buffer list\n"));
118 exit(1);
119 }
120 } else {
121 /* common case avoids calloc/free */
122 map = map_array;
123 }
124 for (i = 0; i < nex; i++) {
125 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, bmp[i].startblock);
126 map[i].bm_len = XFS_FSB_TO_BB(mp, bmp[i].blockcount);
127 }
128 bp = libxfs_readbuf_map(mp->m_dev, map, nex, 0);
129 if (map != map_array)
130 free(map);
131 return bp;
132 }
133
134 /*
135 * walk tree from root to the left-most leaf block reading in
136 * blocks and setting up cursor. passes back file block number of the
137 * left-most leaf block if successful (bno). returns 1 if successful,
138 * 0 if unsuccessful.
139 */
140 static int
141 traverse_int_dir2block(xfs_mount_t *mp,
142 dir2_bt_cursor_t *da_cursor,
143 xfs_dablk_t *rbno)
144 {
145 bmap_ext_t *bmp;
146 xfs_dablk_t bno;
147 struct xfs_buf *bp;
148 int i;
149 int nex;
150 xfs_da_blkinfo_t *info;
151 xfs_da_intnode_t *node;
152 bmap_ext_t lbmp;
153
154 /*
155 * traverse down left-side of tree until we hit the
156 * left-most leaf block setting up the btree cursor along
157 * the way.
158 */
159 bno = mp->m_dirleafblk;
160 i = -1;
161 info = NULL;
162 da_cursor->active = 0;
163
164 do {
165 /*
166 * read in each block along the way and set up cursor
167 */
168 nex = blkmap_getn(da_cursor->blkmap, bno, mp->m_dirblkfsbs,
169 &bmp, &lbmp);
170
171 if (nex == 0)
172 goto error_out;
173
174 bp = da_read_buf(mp, nex, bmp);
175 if (bmp != &lbmp)
176 free(bmp);
177 if (bp == NULL) {
178 do_warn(
179 _("can't read block %u for directory inode %" PRIu64 "\n"),
180 bno, da_cursor->ino);
181 goto error_out;
182 }
183
184 info = bp->b_addr;
185
186 if (be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC) {
187 if ( i != -1 ) {
188 do_warn(
189 _("found non-root LEAFN node in inode %" PRIu64 " bno = %u\n"),
190 da_cursor->ino, bno);
191 }
192 *rbno = 0;
193 libxfs_putbuf(bp);
194 return(1);
195 } else if (be16_to_cpu(info->magic) != XFS_DA_NODE_MAGIC) {
196 libxfs_putbuf(bp);
197 do_warn(
198 _("bad dir magic number 0x%x in inode %" PRIu64 " bno = %u\n"),
199 be16_to_cpu(info->magic),
200 da_cursor->ino, bno);
201 goto error_out;
202 }
203 node = (xfs_da_intnode_t*)info;
204 if (be16_to_cpu(node->hdr.count) > mp->m_dir_node_ents) {
205 libxfs_putbuf(bp);
206 do_warn(
207 _("bad record count in inode %" PRIu64 ", count = %d, max = %d\n"), da_cursor->ino,
208 be16_to_cpu(node->hdr.count),
209 mp->m_dir_node_ents);
210 goto error_out;
211 }
212 /*
213 * maintain level counter
214 */
215 if (i == -1) {
216 i = da_cursor->active = be16_to_cpu(node->hdr.level);
217 if (i >= XFS_DA_NODE_MAXDEPTH) {
218 do_warn(
219 _("bad header depth for directory inode %" PRIu64 "\n"),
220 da_cursor->ino);
221 libxfs_putbuf(bp);
222 i = -1;
223 goto error_out;
224 }
225 } else {
226 if (be16_to_cpu(node->hdr.level) == i - 1) {
227 i--;
228 } else {
229 do_warn(
230 _("bad directory btree for directory inode %" PRIu64 "\n"),
231 da_cursor->ino);
232 libxfs_putbuf(bp);
233 goto error_out;
234 }
235 }
236
237 da_cursor->level[i].hashval =
238 be32_to_cpu(node->btree[0].hashval);
239 da_cursor->level[i].bp = bp;
240 da_cursor->level[i].bno = bno;
241 da_cursor->level[i].index = 0;
242
243 /*
244 * set up new bno for next level down
245 */
246 bno = be32_to_cpu(node->btree[0].before);
247 } while (info != NULL && i > 1);
248
249 /*
250 * now return block number and get out
251 */
252 *rbno = da_cursor->level[0].bno = bno;
253 return(1);
254
255 error_out:
256 while (i > 1 && i <= da_cursor->active) {
257 libxfs_putbuf(da_cursor->level[i].bp);
258 i++;
259 }
260
261 return(0);
262 }
263
264 /*
265 * blow out buffer for this level and all the rest above as well
266 * if error == 0, we are not expecting to encounter any unreleased
267 * buffers (e.g. if we do, it's a mistake). if error == 1, we're
268 * in an error-handling case so unreleased buffers may exist.
269 */
270 static void
271 release_dir2_cursor_int(xfs_mount_t *mp,
272 dir2_bt_cursor_t *cursor,
273 int prev_level,
274 int error)
275 {
276 int level = prev_level + 1;
277
278 if (cursor->level[level].bp != NULL) {
279 if (!error) {
280 do_warn(_("release_dir2_cursor_int got unexpected "
281 "non-null bp, dabno = %u\n"),
282 cursor->level[level].bno);
283 }
284 ASSERT(error != 0);
285
286 libxfs_putbuf(cursor->level[level].bp);
287 cursor->level[level].bp = NULL;
288 }
289
290 if (level < cursor->active)
291 release_dir2_cursor_int(mp, cursor, level, error);
292
293 return;
294 }
295
296 static void
297 release_dir2_cursor(xfs_mount_t *mp,
298 dir2_bt_cursor_t *cursor,
299 int prev_level)
300 {
301 release_dir2_cursor_int(mp, cursor, prev_level, 0);
302 }
303
304 static void
305 err_release_dir2_cursor(xfs_mount_t *mp,
306 dir2_bt_cursor_t *cursor,
307 int prev_level)
308 {
309 release_dir2_cursor_int(mp, cursor, prev_level, 1);
310 }
311
312 /*
313 * make sure that all entries in all blocks along the right side of
314 * of the tree are used and hashval's are consistent. level is the
315 * level of the descendent block. returns 0 if good (even if it had
316 * to be fixed up), and 1 if bad. The right edge of the tree is
317 * technically a block boundary. This routine should be used then
318 * instead of verify_dir2_path().
319 */
320 static int
321 verify_final_dir2_path(xfs_mount_t *mp,
322 dir2_bt_cursor_t *cursor,
323 const int p_level)
324 {
325 xfs_da_intnode_t *node;
326 int bad = 0;
327 int entry;
328 int this_level = p_level + 1;
329
330 /*
331 * the index should point to the next "unprocessed" entry
332 * in the block which should be the final (rightmost) entry
333 */
334 entry = cursor->level[this_level].index;
335 node = (xfs_da_intnode_t *)(cursor->level[this_level].bp->b_addr);
336 /*
337 * check internal block consistency on this level -- ensure
338 * that all entries are used, encountered and expected hashvals
339 * match, etc.
340 */
341 if (entry != be16_to_cpu(node->hdr.count) - 1) {
342 do_warn(
343 _("directory block used/count inconsistency - %d / %hu\n"),
344 entry, be16_to_cpu(node->hdr.count));
345 bad++;
346 }
347 /*
348 * hash values monotonically increasing ???
349 */
350 if (cursor->level[this_level].hashval >=
351 be32_to_cpu(node->btree[entry].hashval)) {
352 do_warn(_("directory/attribute block hashvalue inconsistency, "
353 "expected > %u / saw %u\n"),
354 cursor->level[this_level].hashval,
355 be32_to_cpu(node->btree[entry].hashval));
356 bad++;
357 }
358 if (be32_to_cpu(node->hdr.info.forw) != 0) {
359 do_warn(_("bad directory/attribute forward block pointer, "
360 "expected 0, saw %u\n"),
361 be32_to_cpu(node->hdr.info.forw));
362 bad++;
363 }
364 if (bad) {
365 do_warn(_("bad directory block in inode %" PRIu64 "\n"), cursor->ino);
366 return(1);
367 }
368 /*
369 * keep track of greatest block # -- that gets
370 * us the length of the directory
371 */
372 if (cursor->level[this_level].bno > cursor->greatest_bno)
373 cursor->greatest_bno = cursor->level[this_level].bno;
374
375 /*
376 * ok, now check descendant block number against this level
377 */
378 if (cursor->level[p_level].bno !=
379 be32_to_cpu(node->btree[entry].before))
380 return(1);
381
382 if (cursor->level[p_level].hashval !=
383 be32_to_cpu(node->btree[entry].hashval)) {
384 if (!no_modify) {
385 do_warn(
386 _("correcting bad hashval in non-leaf dir block\n"
387 "\tin (level %d) in inode %" PRIu64 ".\n"),
388 this_level, cursor->ino);
389 node->btree[entry].hashval = cpu_to_be32(
390 cursor->level[p_level].hashval);
391 cursor->level[this_level].dirty++;
392 } else {
393 do_warn(
394 _("would correct bad hashval in non-leaf dir block\n"
395 "\tin (level %d) in inode %" PRIu64 ".\n"),
396 this_level, cursor->ino);
397 }
398 }
399
400 /*
401 * release/write buffer
402 */
403 ASSERT(cursor->level[this_level].dirty == 0 ||
404 (cursor->level[this_level].dirty && !no_modify));
405
406 if (cursor->level[this_level].dirty && !no_modify)
407 libxfs_writebuf(cursor->level[this_level].bp, 0);
408 else
409 libxfs_putbuf(cursor->level[this_level].bp);
410
411 cursor->level[this_level].bp = NULL;
412
413 /*
414 * bail out if this is the root block (top of tree)
415 */
416 if (this_level >= cursor->active)
417 return(0);
418 /*
419 * set hashvalue to correctl reflect the now-validated
420 * last entry in this block and continue upwards validation
421 */
422 cursor->level[this_level].hashval =
423 be32_to_cpu(node->btree[entry].hashval);
424
425 return(verify_final_dir2_path(mp, cursor, this_level));
426 }
427
428 /*
429 * Verifies the path from a descendant block up to the root.
430 * Should be called when the descendant level traversal hits
431 * a block boundary before crossing the boundary (reading in a new
432 * block).
433 *
434 * the directory/attr btrees work differently to the other fs btrees.
435 * each interior block contains records that are <hashval, bno>
436 * pairs. The bno is a file bno, not a filesystem bno. The last
437 * hashvalue in the block <bno> will be <hashval>. BUT unlike
438 * the freespace btrees, the *last* value in each block gets
439 * propagated up the tree instead of the first value in each block.
440 * that is, the interior records point to child blocks and the *greatest*
441 * hash value contained by the child block is the one the block above
442 * uses as the key for the child block.
443 *
444 * level is the level of the descendent block. returns 0 if good,
445 * and 1 if bad. The descendant block may be a leaf block.
446 *
447 * the invariant here is that the values in the cursor for the
448 * levels beneath this level (this_level) and the cursor index
449 * for this level *must* be valid.
450 *
451 * that is, the hashval/bno info is accurate for all
452 * DESCENDANTS and match what the node[index] information
453 * for the current index in the cursor for this level.
454 *
455 * the index values in the cursor for the descendant level
456 * are allowed to be off by one as they will reflect the
457 * next entry at those levels to be processed.
458 *
459 * the hashvalue for the current level can't be set until
460 * we hit the last entry in the block so, it's garbage
461 * until set by this routine.
462 *
463 * bno and bp for the current block/level are always valid
464 * since they have to be set so we can get a buffer for the
465 * block.
466 */
467 static int
468 verify_dir2_path(xfs_mount_t *mp,
469 dir2_bt_cursor_t *cursor,
470 const int p_level)
471 {
472 xfs_da_intnode_t *node;
473 xfs_da_intnode_t *newnode;
474 xfs_dablk_t dabno;
475 struct xfs_buf *bp;
476 int bad;
477 int entry;
478 int this_level = p_level + 1;
479 bmap_ext_t *bmp;
480 int nex;
481 bmap_ext_t lbmp;
482
483 /*
484 * index is currently set to point to the entry that
485 * should be processed now in this level.
486 */
487 entry = cursor->level[this_level].index;
488 node = cursor->level[this_level].bp->b_addr;
489
490 /*
491 * if this block is out of entries, validate this
492 * block and move on to the next block.
493 * and update cursor value for said level
494 */
495 if (entry >= be16_to_cpu(node->hdr.count)) {
496 /*
497 * update the hash value for this level before
498 * validating it. bno value should be ok since
499 * it was set when the block was first read in.
500 */
501 cursor->level[this_level].hashval =
502 be32_to_cpu(node->btree[entry - 1].hashval);
503
504 /*
505 * keep track of greatest block # -- that gets
506 * us the length of the directory
507 */
508 if (cursor->level[this_level].bno > cursor->greatest_bno)
509 cursor->greatest_bno = cursor->level[this_level].bno;
510
511 /*
512 * validate the path for the current used-up block
513 * before we trash it
514 */
515 if (verify_dir2_path(mp, cursor, this_level))
516 return(1);
517 /*
518 * ok, now get the next buffer and check sibling pointers
519 */
520 dabno = be32_to_cpu(node->hdr.info.forw);
521 ASSERT(dabno != 0);
522 nex = blkmap_getn(cursor->blkmap, dabno, mp->m_dirblkfsbs,
523 &bmp, &lbmp);
524 if (nex == 0) {
525 do_warn(
526 _("can't get map info for block %u of directory inode %" PRIu64 "\n"),
527 dabno, cursor->ino);
528 return(1);
529 }
530
531 bp = da_read_buf(mp, nex, bmp);
532 if (bmp != &lbmp)
533 free(bmp);
534
535 if (bp == NULL) {
536 do_warn(
537 _("can't read block %u for directory inode %" PRIu64 "\n"),
538 dabno, cursor->ino);
539 return(1);
540 }
541
542 newnode = bp->b_addr;
543 /*
544 * verify magic number and back pointer, sanity-check
545 * entry count, verify level
546 */
547 bad = 0;
548 if (XFS_DA_NODE_MAGIC != be16_to_cpu(newnode->hdr.info.magic)) {
549 do_warn(
550 _("bad magic number %x in block %u for directory inode %" PRIu64 "\n"),
551 be16_to_cpu(newnode->hdr.info.magic),
552 dabno, cursor->ino);
553 bad++;
554 }
555 if (be32_to_cpu(newnode->hdr.info.back) !=
556 cursor->level[this_level].bno) {
557 do_warn(
558 _("bad back pointer in block %u for directory inode %" PRIu64 "\n"),
559 dabno, cursor->ino);
560 bad++;
561 }
562 if (be16_to_cpu(newnode->hdr.count) > mp->m_dir_node_ents) {
563 do_warn(
564 _("entry count %d too large in block %u for directory inode %" PRIu64 "\n"),
565 be16_to_cpu(newnode->hdr.count),
566 dabno, cursor->ino);
567 bad++;
568 }
569 if (be16_to_cpu(newnode->hdr.level) != this_level) {
570 do_warn(
571 _("bad level %d in block %u for directory inode %" PRIu64 "\n"),
572 be16_to_cpu(newnode->hdr.level),
573 dabno, cursor->ino);
574 bad++;
575 }
576 if (bad) {
577 libxfs_putbuf(bp);
578 return(1);
579 }
580 /*
581 * update cursor, write out the *current* level if
582 * required. don't write out the descendant level
583 */
584 ASSERT(cursor->level[this_level].dirty == 0 ||
585 (cursor->level[this_level].dirty && !no_modify));
586
587 if (cursor->level[this_level].dirty && !no_modify)
588 libxfs_writebuf(cursor->level[this_level].bp, 0);
589 else
590 libxfs_putbuf(cursor->level[this_level].bp);
591 cursor->level[this_level].bp = bp;
592 cursor->level[this_level].dirty = 0;
593 cursor->level[this_level].bno = dabno;
594 cursor->level[this_level].hashval =
595 be32_to_cpu(newnode->btree[0].hashval);
596 node = newnode;
597
598 entry = cursor->level[this_level].index = 0;
599 }
600 /*
601 * ditto for block numbers
602 */
603 if (cursor->level[p_level].bno !=
604 be32_to_cpu(node->btree[entry].before))
605 return(1);
606 /*
607 * ok, now validate last hashvalue in the descendant
608 * block against the hashval in the current entry
609 */
610 if (cursor->level[p_level].hashval !=
611 be32_to_cpu(node->btree[entry].hashval)) {
612 if (!no_modify) {
613 do_warn(
614 _("correcting bad hashval in interior dir block\n"
615 "\tin (level %d) in inode %" PRIu64 ".\n"),
616 this_level, cursor->ino);
617 node->btree[entry].hashval = cpu_to_be32(
618 cursor->level[p_level].hashval);
619 cursor->level[this_level].dirty++;
620 } else {
621 do_warn(
622 _("would correct bad hashval in interior dir block\n"
623 "\tin (level %d) in inode %" PRIu64 ".\n"),
624 this_level, cursor->ino);
625 }
626 }
627 /*
628 * increment index for this level to point to next entry
629 * (which should point to the next descendant block)
630 */
631 cursor->level[this_level].index++;
632 return(0);
633 }
634
635 /*
636 * Fix up a shortform directory which was in long form (i8count set)
637 * and is now in short form (i8count clear).
638 * Return pointer to the end of the data when done.
639 */
640 void
641 process_sf_dir2_fixi8(
642 xfs_dir2_sf_t *sfp,
643 xfs_dir2_sf_entry_t **next_sfep)
644 {
645 xfs_ino_t ino;
646 xfs_dir2_sf_t *newsfp;
647 xfs_dir2_sf_entry_t *newsfep;
648 xfs_dir2_sf_t *oldsfp;
649 xfs_dir2_sf_entry_t *oldsfep;
650 int oldsize;
651
652 newsfp = sfp;
653 oldsize = (__psint_t)*next_sfep - (__psint_t)sfp;
654 oldsfp = malloc(oldsize);
655 if (oldsfp == NULL) {
656 do_error(_("couldn't malloc dir2 shortform copy\n"));
657 exit(1);
658 }
659 memmove(oldsfp, newsfp, oldsize);
660 newsfp->hdr.count = oldsfp->hdr.count;
661 newsfp->hdr.i8count = 0;
662 ino = xfs_dir2_sf_get_parent_ino(&sfp->hdr);
663 xfs_dir2_sf_put_parent_ino(&newsfp->hdr, ino);
664 oldsfep = xfs_dir2_sf_firstentry(&oldsfp->hdr);
665 newsfep = xfs_dir2_sf_firstentry(&newsfp->hdr);
666 while ((int)((char *)oldsfep - (char *)oldsfp) < oldsize) {
667 newsfep->namelen = oldsfep->namelen;
668 xfs_dir2_sf_put_offset(newsfep,
669 xfs_dir2_sf_get_offset(oldsfep));
670 memmove(newsfep->name, oldsfep->name, newsfep->namelen);
671 ino = xfs_dir2_sfe_get_ino(&oldsfp->hdr, oldsfep);
672 xfs_dir2_sfe_put_ino(&newsfp->hdr, newsfep, ino);
673 oldsfep = xfs_dir2_sf_nextentry(&oldsfp->hdr, oldsfep);
674 newsfep = xfs_dir2_sf_nextentry(&newsfp->hdr, newsfep);
675 }
676 *next_sfep = newsfep;
677 free(oldsfp);
678 }
679
680 /*
681 * Regenerate legal (minimal) offsets for the shortform directory.
682 */
683 static void
684 process_sf_dir2_fixoff(
685 xfs_dinode_t *dip)
686 {
687 int i;
688 int offset;
689 xfs_dir2_sf_entry_t *sfep;
690 xfs_dir2_sf_t *sfp;
691
692 sfp = (xfs_dir2_sf_t *)XFS_DFORK_DPTR(dip);
693 sfep = xfs_dir2_sf_firstentry(&sfp->hdr);
694 offset = XFS_DIR2_DATA_FIRST_OFFSET;
695
696 for (i = 0; i < sfp->hdr.count; i++) {
697 xfs_dir2_sf_put_offset(sfep, offset);
698 offset += xfs_dir2_data_entsize(sfep->namelen);
699 sfep = xfs_dir2_sf_nextentry(&sfp->hdr, sfep);
700 }
701 }
702
703 /*
704 * this routine performs inode discovery and tries to fix things
705 * in place. available redundancy -- inode data size should match
706 * used directory space in inode.
707 * a non-zero return value means the directory is bogus and should be blasted.
708 */
709 /* ARGSUSED */
710 static int
711 process_sf_dir2(
712 xfs_mount_t *mp,
713 xfs_ino_t ino,
714 xfs_dinode_t *dip,
715 int ino_discovery,
716 int *dino_dirty, /* out - 1 if dinode buffer dirty */
717 char *dirname, /* directory pathname */
718 xfs_ino_t *parent, /* out - NULLFSINO if entry not exist */
719 int *repair) /* out - 1 if dir was fixed up */
720 {
721 int bad_offset;
722 int bad_sfnamelen;
723 int i;
724 int i8;
725 __int64_t ino_dir_size;
726 int ino_off;
727 ino_tree_node_t *irec_p;
728 int junkit;
729 char *junkreason = NULL;
730 xfs_ino_t lino;
731 int max_size;
732 char name[MAXNAMELEN + 1];
733 int namelen;
734 xfs_dir2_sf_entry_t *next_sfep;
735 int num_entries;
736 int offset;
737 xfs_dir2_sf_t *sfp;
738 xfs_dir2_sf_entry_t *sfep;
739 int tmp_elen;
740 int tmp_len;
741 xfs_dir2_sf_entry_t *tmp_sfep;
742 xfs_ino_t zero = 0;
743
744 sfp = (xfs_dir2_sf_t *)XFS_DFORK_DPTR(dip);
745 max_size = XFS_DFORK_DSIZE(dip, mp);
746 num_entries = sfp->hdr.count;
747 ino_dir_size = be64_to_cpu(dip->di_size);
748 offset = XFS_DIR2_DATA_FIRST_OFFSET;
749 bad_offset = *repair = 0;
750
751 ASSERT(ino_dir_size <= max_size);
752
753 /*
754 * Initialize i8 based on size of parent inode number.
755 */
756 i8 = (xfs_dir2_sf_get_parent_ino(&sfp->hdr) > XFS_DIR2_MAX_SHORT_INUM);
757
758 /*
759 * check for bad entry count
760 */
761 if (num_entries * xfs_dir2_sf_entsize(&sfp->hdr, 1) +
762 xfs_dir2_sf_hdr_size(0) > max_size || num_entries == 0)
763 num_entries = 0xFF;
764
765 /*
766 * run through entries, stop at first bad entry, don't need
767 * to check for .. since that's encoded in its own field
768 */
769 sfep = next_sfep = xfs_dir2_sf_firstentry(&sfp->hdr);
770 for (i = 0;
771 i < num_entries && ino_dir_size > (char *)next_sfep - (char *)sfp;
772 i++) {
773 tmp_sfep = NULL;
774 sfep = next_sfep;
775 junkit = 0;
776 bad_sfnamelen = 0;
777 lino = xfs_dir2_sfe_get_ino(&sfp->hdr, sfep);
778 /*
779 * if entry points to self, junk it since only '.' or '..'
780 * should do that and shortform dirs don't contain either
781 * entry. if inode number is invalid, trash entry.
782 * if entry points to special inodes, trash it.
783 * if inode is unknown but number is valid,
784 * add it to the list of uncertain inodes. don't
785 * have to worry about an entry pointing to a
786 * deleted lost+found inode because the entry was
787 * deleted at the same time that the inode was cleared.
788 */
789 if (lino == ino) {
790 junkit = 1;
791 junkreason = _("current");
792 } else if (verify_inum(mp, lino)) {
793 junkit = 1;
794 junkreason = _("invalid");
795 } else if (lino == mp->m_sb.sb_rbmino) {
796 junkit = 1;
797 junkreason = _("realtime bitmap");
798 } else if (lino == mp->m_sb.sb_rsumino) {
799 junkit = 1;
800 junkreason = _("realtime summary");
801 } else if (lino == mp->m_sb.sb_uquotino) {
802 junkit = 1;
803 junkreason = _("user quota");
804 } else if (lino == mp->m_sb.sb_gquotino) {
805 junkit = 1;
806 junkreason = _("group quota");
807 } else if ((irec_p = find_inode_rec(mp,
808 XFS_INO_TO_AGNO(mp, lino),
809 XFS_INO_TO_AGINO(mp, lino))) != NULL) {
810 /*
811 * if inode is marked free and we're in inode
812 * discovery mode, leave the entry alone for now.
813 * if the inode turns out to be used, we'll figure
814 * that out when we scan it. If the inode really
815 * is free, we'll hit this code again in phase 4
816 * after we've finished inode discovery and blow
817 * out the entry then.
818 */
819 ino_off = XFS_INO_TO_AGINO(mp, lino) -
820 irec_p->ino_startnum;
821 ASSERT(is_inode_confirmed(irec_p, ino_off));
822 if (is_inode_free(irec_p, ino_off) && !ino_discovery) {
823 junkit = 1;
824 junkreason = _("free");
825 }
826 } else if (ino_discovery) {
827 /*
828 * put the inode on the uncertain list. we'll
829 * pull the inode off the list and check it later.
830 * if the inode turns out be bogus, we'll delete
831 * this entry in phase 6.
832 */
833 add_inode_uncertain(mp, lino, 0);
834 } else {
835 /*
836 * blow the entry out. we know about all
837 * undiscovered entries now (past inode discovery
838 * phase) so this is clearly a bogus entry.
839 */
840 junkit = 1;
841 junkreason = _("non-existent");
842 }
843 namelen = sfep->namelen;
844 if (junkit)
845 do_warn(
846 _("entry \"%*.*s\" in shortform directory %" PRIu64 " references %s inode %" PRIu64 "\n"),
847 namelen, namelen, sfep->name, ino, junkreason,
848 lino);
849 if (namelen == 0) {
850 /*
851 * if we're really lucky, this is
852 * the last entry in which case we
853 * can use the dir size to set the
854 * namelen value. otherwise, forget
855 * it because we're not going to be
856 * able to find the next entry.
857 */
858 bad_sfnamelen = 1;
859
860 if (i == num_entries - 1) {
861 namelen = ino_dir_size -
862 ((__psint_t) &sfep->name[0] -
863 (__psint_t) sfp);
864 if (!no_modify) {
865 do_warn(
866 _("zero length entry in shortform dir %" PRIu64 ", resetting to %d\n"),
867 ino, namelen);
868 sfep->namelen = namelen;
869 } else {
870 do_warn(
871 _("zero length entry in shortform dir %" PRIu64 ", would set to %d\n"),
872 ino, namelen);
873 }
874 } else {
875 do_warn(
876 _("zero length entry in shortform dir %" PRIu64 ""),
877 ino);
878 if (!no_modify)
879 do_warn(_(", junking %d entries\n"),
880 num_entries - i);
881 else
882 do_warn(_(", would junk %d entries\n"),
883 num_entries - i);
884 /*
885 * don't process the rest of the directory,
886 * break out of processing looop
887 */
888 break;
889 }
890 } else if ((__psint_t) sfep - (__psint_t) sfp +
891 xfs_dir2_sf_entsize(&sfp->hdr, sfep->namelen)
892 > ino_dir_size) {
893 bad_sfnamelen = 1;
894
895 if (i == num_entries - 1) {
896 namelen = ino_dir_size -
897 ((__psint_t) &sfep->name[0] -
898 (__psint_t) sfp);
899 do_warn(
900 _("size of last entry overflows space left in in shortform dir %" PRIu64 ", "),
901 ino);
902 if (!no_modify) {
903 do_warn(_("resetting to %d\n"),
904 namelen);
905 sfep->namelen = namelen;
906 *dino_dirty = 1;
907 } else {
908 do_warn(_("would reset to %d\n"),
909 namelen);
910 }
911 } else {
912 do_warn(
913 _("size of entry #%d overflows space left in in shortform dir %" PRIu64 "\n"),
914 i, ino);
915 if (!no_modify) {
916 if (i == num_entries - 1)
917 do_warn(
918 _("junking entry #%d\n"),
919 i);
920 else
921 do_warn(
922 _("junking %d entries\n"),
923 num_entries - i);
924 } else {
925 if (i == num_entries - 1)
926 do_warn(
927 _("would junk entry #%d\n"),
928 i);
929 else
930 do_warn(
931 _("would junk %d entries\n"),
932 num_entries - i);
933 }
934
935 break;
936 }
937 }
938
939 /*
940 * check for illegal chars in name.
941 * no need to check for bad length because
942 * the length value is stored in a byte
943 * so it can't be too big, it can only wrap
944 */
945 if (namecheck((char *)&sfep->name[0], namelen)) {
946 /*
947 * junk entry
948 */
949 do_warn(
950 _("entry contains illegal character in shortform dir %" PRIu64 "\n"),
951 ino);
952 junkit = 1;
953 }
954
955 if (xfs_dir2_sf_get_offset(sfep) < offset) {
956 do_warn(
957 _("entry contains offset out of order in shortform dir %" PRIu64 "\n"),
958 ino);
959 bad_offset = 1;
960 }
961 offset = xfs_dir2_sf_get_offset(sfep) +
962 xfs_dir2_data_entsize(namelen);
963
964 /*
965 * junk the entry by copying up the rest of the
966 * fork over the current entry and decrementing
967 * the entry count. if we're in no_modify mode,
968 * just issue the warning instead. then continue
969 * the loop with the next_sfep pointer set to the
970 * correct place in the fork and other counters
971 * properly set to reflect the deletion if it
972 * happened.
973 */
974 if (junkit) {
975 memmove(name, sfep->name, namelen);
976 name[namelen] = '\0';
977
978 if (!no_modify) {
979 tmp_elen = xfs_dir2_sf_entsize(&sfp->hdr,
980 sfep->namelen);
981 be64_add_cpu(&dip->di_size, -tmp_elen);
982 ino_dir_size -= tmp_elen;
983
984 tmp_sfep = (xfs_dir2_sf_entry_t *)
985 ((__psint_t) sfep + tmp_elen);
986 tmp_len = max_size - ((__psint_t) tmp_sfep
987 - (__psint_t) sfp);
988
989 memmove(sfep, tmp_sfep, tmp_len);
990
991 sfp->hdr.count -= 1;
992 num_entries--;
993 memset((void *) ((__psint_t) sfep + tmp_len), 0,
994 tmp_elen);
995
996 /*
997 * reset the tmp value to the current
998 * pointer so we'll process the entry
999 * we just moved up
1000 */
1001 tmp_sfep = sfep;
1002
1003 /*
1004 * WARNING: drop the index i by one
1005 * so it matches the decremented count
1006 * for accurate comparisons later
1007 */
1008 i--;
1009
1010 *dino_dirty = 1;
1011 *repair = 1;
1012
1013 do_warn(
1014 _("junking entry \"%s\" in directory inode %" PRIu64 "\n"),
1015 name, ino);
1016 } else {
1017 do_warn(
1018 _("would have junked entry \"%s\" in directory inode %" PRIu64 "\n"),
1019 name, ino);
1020 }
1021 } else if (lino > XFS_DIR2_MAX_SHORT_INUM)
1022 i8++;
1023 /*
1024 * go onto next entry unless we've just junked an
1025 * entry in which the current entry pointer points
1026 * to an unprocessed entry. have to take into zero-len
1027 * entries into account in no modify mode since we
1028 * calculate size based on next_sfep.
1029 */
1030 next_sfep = (tmp_sfep == NULL)
1031 ? (xfs_dir2_sf_entry_t *) ((__psint_t) sfep
1032 + ((!bad_sfnamelen)
1033 ? xfs_dir2_sf_entsize(&sfp->hdr, sfep->namelen)
1034 : xfs_dir2_sf_entsize(&sfp->hdr, namelen)))
1035 : tmp_sfep;
1036 }
1037
1038 /* sync up sizes and entry counts */
1039
1040 if (sfp->hdr.count != i) {
1041 if (no_modify) {
1042 do_warn(
1043 _("would have corrected entry count in directory %" PRIu64 " from %d to %d\n"),
1044 ino, sfp->hdr.count, i);
1045 } else {
1046 do_warn(
1047 _("corrected entry count in directory %" PRIu64 ", was %d, now %d\n"),
1048 ino, sfp->hdr.count, i);
1049 sfp->hdr.count = i;
1050 *dino_dirty = 1;
1051 *repair = 1;
1052 }
1053 }
1054
1055 if (sfp->hdr.i8count != i8) {
1056 if (no_modify) {
1057 do_warn(
1058 _("would have corrected i8 count in directory %" PRIu64 " from %d to %d\n"),
1059 ino, sfp->hdr.i8count, i8);
1060 } else {
1061 do_warn(
1062 _("corrected i8 count in directory %" PRIu64 ", was %d, now %d\n"),
1063 ino, sfp->hdr.i8count, i8);
1064 if (i8 == 0)
1065 process_sf_dir2_fixi8(sfp, &next_sfep);
1066 else
1067 sfp->hdr.i8count = i8;
1068 *dino_dirty = 1;
1069 *repair = 1;
1070 }
1071 }
1072
1073 if ((intptr_t)next_sfep - (intptr_t)sfp != ino_dir_size) {
1074 if (no_modify) {
1075 do_warn(
1076 _("would have corrected directory %" PRIu64 " size from %" PRId64 " to %" PRIdPTR "\n"),
1077 ino, ino_dir_size,
1078 (intptr_t)next_sfep - (intptr_t)sfp);
1079 } else {
1080 do_warn(
1081 _("corrected directory %" PRIu64 " size, was %" PRId64 ", now %" PRIdPTR "\n"),
1082 ino, ino_dir_size,
1083 (intptr_t)next_sfep - (intptr_t)sfp);
1084
1085 dip->di_size = cpu_to_be64(
1086 (__psint_t)next_sfep - (__psint_t)sfp);
1087 *dino_dirty = 1;
1088 *repair = 1;
1089 }
1090 }
1091 if (offset + (sfp->hdr.count + 2) * sizeof(xfs_dir2_leaf_entry_t) +
1092 sizeof(xfs_dir2_block_tail_t) > mp->m_dirblksize) {
1093 do_warn(_("directory %" PRIu64 " offsets too high\n"), ino);
1094 bad_offset = 1;
1095 }
1096 if (bad_offset) {
1097 if (no_modify) {
1098 do_warn(
1099 _("would have corrected entry offsets in directory %" PRIu64 "\n"),
1100 ino);
1101 } else {
1102 do_warn(
1103 _("corrected entry offsets in directory %" PRIu64 "\n"),
1104 ino);
1105 process_sf_dir2_fixoff(dip);
1106 *dino_dirty = 1;
1107 *repair = 1;
1108 }
1109 }
1110
1111 /*
1112 * check parent (..) entry
1113 */
1114 *parent = xfs_dir2_sf_get_parent_ino(&sfp->hdr);
1115
1116 /*
1117 * if parent entry is bogus, null it out. we'll fix it later .
1118 */
1119 if (verify_inum(mp, *parent)) {
1120
1121 do_warn(
1122 _("bogus .. inode number (%" PRIu64 ") in directory inode %" PRIu64 ", "),
1123 *parent, ino);
1124 *parent = NULLFSINO;
1125 if (!no_modify) {
1126 do_warn(_("clearing inode number\n"));
1127
1128 xfs_dir2_sf_put_parent_ino(&sfp->hdr, zero);
1129 *dino_dirty = 1;
1130 *repair = 1;
1131 } else {
1132 do_warn(_("would clear inode number\n"));
1133 }
1134 } else if (ino == mp->m_sb.sb_rootino && ino != *parent) {
1135 /*
1136 * root directories must have .. == .
1137 */
1138 if (!no_modify) {
1139 do_warn(
1140 _("corrected root directory %" PRIu64 " .. entry, was %" PRIu64 ", now %" PRIu64 "\n"),
1141 ino, *parent, ino);
1142 *parent = ino;
1143 xfs_dir2_sf_put_parent_ino(&sfp->hdr, ino);
1144 *dino_dirty = 1;
1145 *repair = 1;
1146 } else {
1147 do_warn(
1148 _("would have corrected root directory %" PRIu64 " .. entry from %" PRIu64" to %" PRIu64 "\n"),
1149 ino, *parent, ino);
1150 }
1151 } else if (ino == *parent && ino != mp->m_sb.sb_rootino) {
1152 /*
1153 * likewise, non-root directories can't have .. pointing
1154 * to .
1155 */
1156 *parent = NULLFSINO;
1157 do_warn(
1158 _("bad .. entry in directory inode %" PRIu64 ", points to self, "),
1159 ino);
1160 if (!no_modify) {
1161 do_warn(_("clearing inode number\n"));
1162
1163 xfs_dir2_sf_put_parent_ino(&sfp->hdr, zero);
1164 *dino_dirty = 1;
1165 *repair = 1;
1166 } else {
1167 do_warn(_("would clear inode number\n"));
1168 }
1169 }
1170
1171 return(0);
1172 }
1173
1174 /*
1175 * Process one directory data block.
1176 */
1177 /* ARGSUSED */
1178 static int
1179 process_dir2_data(
1180 xfs_mount_t *mp,
1181 xfs_ino_t ino,
1182 xfs_dinode_t *dip,
1183 int ino_discovery,
1184 char *dirname, /* directory pathname */
1185 xfs_ino_t *parent, /* out - NULLFSINO if entry not exist */
1186 struct xfs_buf *bp,
1187 int *dot, /* out - 1 if there is a dot, else 0 */
1188 int *dotdot, /* out - 1 if there's a dotdot, else 0 */
1189 xfs_dablk_t da_bno,
1190 char *endptr,
1191 int *dirty)
1192 {
1193 int badbest;
1194 xfs_dir2_data_free_t *bf;
1195 int clearino;
1196 char *clearreason = NULL;
1197 xfs_dir2_data_t *d;
1198 xfs_dir2_data_entry_t *dep;
1199 xfs_dir2_data_free_t *dfp;
1200 xfs_dir2_data_unused_t *dup;
1201 int freeseen;
1202 int i;
1203 int ino_off;
1204 ino_tree_node_t *irec_p;
1205 int junkit;
1206 int lastfree;
1207 int nm_illegal;
1208 char *ptr;
1209 xfs_ino_t ent_ino;
1210
1211 d = bp->b_addr;
1212 bf = d->hdr.bestfree;
1213 ptr = (char *)d->u;
1214 badbest = lastfree = freeseen = 0;
1215 if (be16_to_cpu(bf[0].length) == 0) {
1216 badbest |= be16_to_cpu(bf[0].offset) != 0;
1217 freeseen |= 1 << 0;
1218 }
1219 if (be16_to_cpu(bf[1].length) == 0) {
1220 badbest |= be16_to_cpu(bf[1].offset) != 0;
1221 freeseen |= 1 << 1;
1222 }
1223 if (be16_to_cpu(bf[2].length) == 0) {
1224 badbest |= be16_to_cpu(bf[2].offset) != 0;
1225 freeseen |= 1 << 2;
1226 }
1227 badbest |= be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length);
1228 badbest |= be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length);
1229 while (ptr < endptr) {
1230 dup = (xfs_dir2_data_unused_t *)ptr;
1231 /*
1232 * If it's unused, look for the space in the bestfree table.
1233 * If we find it, account for that, else make sure it doesn't
1234 * need to be there.
1235 */
1236 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1237 if (ptr + be16_to_cpu(dup->length) > endptr ||
1238 be16_to_cpu(dup->length) == 0 ||
1239 (be16_to_cpu(dup->length) & (XFS_DIR2_DATA_ALIGN - 1)))
1240 break;
1241 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
1242 (char *)dup - (char *)d)
1243 break;
1244 badbest |= lastfree != 0;
1245 dfp = xfs_dir2_data_freefind(&d->hdr, dup);
1246 if (dfp) {
1247 i = dfp - bf;
1248 badbest |= (freeseen & (1 << i)) != 0;
1249 freeseen |= 1 << i;
1250 } else
1251 badbest |= be16_to_cpu(dup->length) >
1252 be16_to_cpu(bf[2].length);
1253 ptr += be16_to_cpu(dup->length);
1254 lastfree = 1;
1255 continue;
1256 }
1257 dep = (xfs_dir2_data_entry_t *)ptr;
1258 if (ptr + xfs_dir2_data_entsize(dep->namelen) > endptr)
1259 break;
1260 if (be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) !=
1261 (char *)dep - (char *)d)
1262 break;
1263 ptr += xfs_dir2_data_entsize(dep->namelen);
1264 lastfree = 0;
1265 }
1266 /*
1267 * Dropped out before we processed everything, give up.
1268 * Phase 6 will kill this block if we don't kill the inode.
1269 */
1270 if (ptr != endptr) {
1271 do_warn(_("corrupt block %u in directory inode %" PRIu64 "\n"),
1272 da_bno, ino);
1273 if (!no_modify)
1274 do_warn(_("\twill junk block\n"));
1275 else
1276 do_warn(_("\twould junk block\n"));
1277 return 1;
1278 }
1279 ptr = (char *)d->u;
1280 /*
1281 * Process the entries now.
1282 */
1283 while (ptr < endptr) {
1284 dup = (xfs_dir2_data_unused_t *)ptr;
1285 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1286 ptr += be16_to_cpu(dup->length);
1287 continue;
1288 }
1289 dep = (xfs_dir2_data_entry_t *)ptr;
1290 ent_ino = be64_to_cpu(dep->inumber);
1291 clearino = 1;
1292 clearreason = NULL;
1293 /*
1294 * We may have to blow out an entry because of bad inode
1295 * numbers. Do NOT touch the name until after we've computed
1296 * the hashvalue and done a namecheck() on the name.
1297 *
1298 * Conditions must either set clearino to zero or set
1299 * clearreason why it's being cleared.
1300 */
1301 if (!ino_discovery && ent_ino == BADFSINO) {
1302 /*
1303 * Don't do a damned thing. We already found this
1304 * (or did it ourselves) during phase 3.
1305 */
1306 clearino = 0;
1307 } else if (verify_inum(mp, ent_ino)) {
1308 /*
1309 * Bad inode number. Clear the inode number and the
1310 * entry will get removed later. We don't trash the
1311 * directory since it's still structurally intact.
1312 */
1313 clearreason = _("invalid");
1314 } else if (ent_ino == mp->m_sb.sb_rbmino) {
1315 clearreason = _("realtime bitmap");
1316 } else if (ent_ino == mp->m_sb.sb_rsumino) {
1317 clearreason = _("realtime summary");
1318 } else if (ent_ino == mp->m_sb.sb_uquotino) {
1319 clearreason = _("user quota");
1320 } else if (ent_ino == mp->m_sb.sb_gquotino) {
1321 clearreason = _("group quota");
1322 } else {
1323 irec_p = find_inode_rec(mp,
1324 XFS_INO_TO_AGNO(mp, ent_ino),
1325 XFS_INO_TO_AGINO(mp, ent_ino));
1326 if (irec_p == NULL) {
1327 if (ino_discovery) {
1328 add_inode_uncertain(mp, ent_ino, 0);
1329 clearino = 0;
1330 } else
1331 clearreason = _("non-existent");
1332 } else {
1333 /*
1334 * Inode recs should have only confirmed
1335 * inodes in them.
1336 */
1337 ino_off = XFS_INO_TO_AGINO(mp, ent_ino)
1338 - irec_p->ino_startnum;
1339 ASSERT(is_inode_confirmed(irec_p, ino_off));
1340 /*
1341 * If inode is marked free and we're in inode
1342 * discovery mode, leave the entry alone for
1343 * now. If the inode turns out to be used,
1344 * we'll figure that out when we scan it.
1345 * If the inode really is free, we'll hit this
1346 * code again in phase 4 after we've finished
1347 * inode discovery and blow out the entry then.
1348 */
1349 if (!ino_discovery && is_inode_free(irec_p,
1350 ino_off))
1351 clearreason = _("free");
1352 else
1353 clearino = 0;
1354 }
1355 }
1356 ASSERT((clearino == 0 && clearreason == NULL) ||
1357 (clearino != 0 && clearreason != NULL));
1358 if (clearino)
1359 do_warn(
1360 _("entry \"%*.*s\" at block %d offset %" PRIdPTR " in directory inode %" PRIu64
1361 " references %s inode %" PRIu64 "\n"),
1362 dep->namelen, dep->namelen, dep->name,
1363 da_bno, (intptr_t)ptr - (intptr_t)d, ino,
1364 clearreason, ent_ino);
1365 /*
1366 * If the name length is 0 (illegal) make it 1 and blast
1367 * the entry.
1368 */
1369 if (dep->namelen == 0) {
1370 do_warn(
1371 _("entry at block %u offset %" PRIdPTR " in directory inode %" PRIu64
1372 "has 0 namelength\n"),
1373 da_bno, (intptr_t)ptr - (intptr_t)d, ino);
1374 if (!no_modify)
1375 dep->namelen = 1;
1376 clearino = 1;
1377 }
1378 /*
1379 * If needed to clear the inode number, do it now.
1380 */
1381 if (clearino) {
1382 if (!no_modify) {
1383 do_warn(
1384 _("\tclearing inode number in entry at offset %" PRIdPTR "...\n"),
1385 (intptr_t)ptr - (intptr_t)d);
1386 dep->inumber = cpu_to_be64(BADFSINO);
1387 ent_ino = BADFSINO;
1388 *dirty = 1;
1389 } else {
1390 do_warn(
1391 _("\twould clear inode number in entry at offset %" PRIdPTR "...\n"),
1392 (intptr_t)ptr - (intptr_t)d);
1393 }
1394 }
1395 /*
1396 * Only complain about illegal names in phase 3 (when inode
1397 * discovery is turned on). Otherwise, we'd complain a lot
1398 * during phase 4.
1399 */
1400 junkit = ent_ino == BADFSINO;
1401 nm_illegal = namecheck((char *)dep->name, dep->namelen);
1402 if (ino_discovery && nm_illegal) {
1403 do_warn(
1404 _("entry at block %u offset %" PRIdPTR " in directory inode %" PRIu64 " has illegal name \"%*.*s\": "),
1405 da_bno, (intptr_t)ptr - (intptr_t)d, ino,
1406 dep->namelen, dep->namelen, dep->name);
1407 junkit = 1;
1408 }
1409 /*
1410 * Now we can mark entries with BADFSINO's bad.
1411 */
1412 if (!no_modify && ent_ino == BADFSINO) {
1413 dep->name[0] = '/';
1414 *dirty = 1;
1415 junkit = 0;
1416 }
1417 /*
1418 * Special .. entry processing.
1419 */
1420 if (dep->namelen == 2 &&
1421 dep->name[0] == '.' && dep->name[1] == '.') {
1422 if (!*dotdot) {
1423 (*dotdot)++;
1424 *parent = ent_ino;
1425 /*
1426 * What if .. == .? Legal only in the root
1427 * inode. Blow out entry and set parent to
1428 * NULLFSINO otherwise.
1429 */
1430 if (ino == ent_ino &&
1431 ino != mp->m_sb.sb_rootino) {
1432 *parent = NULLFSINO;
1433 do_warn(
1434 _("bad .. entry in directory inode %" PRIu64 ", points to self: "),
1435 ino);
1436 junkit = 1;
1437 }
1438 /*
1439 * We have to make sure that . == .. in the
1440 * root inode.
1441 */
1442 else if (ino != ent_ino &&
1443 ino == mp->m_sb.sb_rootino) {
1444 do_warn(
1445 _("bad .. entry in root directory inode %" PRIu64 ", was %" PRIu64 ": "),
1446 ino, ent_ino);
1447 if (!no_modify) {
1448 do_warn(_("correcting\n"));
1449 dep->inumber = cpu_to_be64(ino);
1450 *dirty = 1;
1451 } else {
1452 do_warn(_("would correct\n"));
1453 }
1454 }
1455 }
1456 /*
1457 * Can't fix the directory unless we know which ..
1458 * entry is the right one. Both have valid inode
1459 * numbers or we wouldn't be here. So since both
1460 * seem equally valid, trash this one.
1461 */
1462 else {
1463 do_warn(
1464 _("multiple .. entries in directory inode %" PRIu64 ": "),
1465 ino);
1466 junkit = 1;
1467 }
1468 }
1469 /*
1470 * Special . entry processing.
1471 */
1472 else if (dep->namelen == 1 && dep->name[0] == '.') {
1473 if (!*dot) {
1474 (*dot)++;
1475 if (ent_ino != ino) {
1476 do_warn(
1477 _("bad . entry in directory inode %" PRIu64 ", was %" PRIu64 ": "),
1478 ino, ent_ino);
1479 if (!no_modify) {
1480 do_warn(_("correcting\n"));
1481 dep->inumber = cpu_to_be64(ino);
1482 *dirty = 1;
1483 } else {
1484 do_warn(_("would correct\n"));
1485 }
1486 }
1487 } else {
1488 do_warn(
1489 _("multiple . entries in directory inode %" PRIu64 ": "),
1490 ino);
1491 junkit = 1;
1492 }
1493 }
1494 /*
1495 * All other entries -- make sure only . references self.
1496 */
1497 else if (ent_ino == ino) {
1498 do_warn(
1499 _("entry \"%*.*s\" in directory inode %" PRIu64 " points to self: "),
1500 dep->namelen, dep->namelen, dep->name, ino);
1501 junkit = 1;
1502 }
1503 /*
1504 * Clear junked entries.
1505 */
1506 if (junkit) {
1507 if (!no_modify) {
1508 dep->name[0] = '/';
1509 *dirty = 1;
1510 do_warn(_("clearing entry\n"));
1511 } else {
1512 do_warn(_("would clear entry\n"));
1513 }
1514 }
1515 /*
1516 * Advance to the next entry.
1517 */
1518 ptr += xfs_dir2_data_entsize(dep->namelen);
1519 }
1520 /*
1521 * Check the bestfree table.
1522 */
1523 if (freeseen != 7 || badbest) {
1524 do_warn(
1525 _("bad bestfree table in block %u in directory inode %" PRIu64 ": "),
1526 da_bno, ino);
1527 if (!no_modify) {
1528 do_warn(_("repairing table\n"));
1529 libxfs_dir2_data_freescan(mp, &d->hdr, &i);
1530 *dirty = 1;
1531 } else {
1532 do_warn(_("would repair table\n"));
1533 }
1534 }
1535 return 0;
1536 }
1537
1538 /*
1539 * Process a block-format directory.
1540 */
1541 /* ARGSUSED */
1542 static int
1543 process_block_dir2(
1544 xfs_mount_t *mp,
1545 xfs_ino_t ino,
1546 xfs_dinode_t *dip,
1547 int ino_discovery,
1548 int *dino_dirty, /* out - 1 if dinode buffer dirty */
1549 char *dirname, /* directory pathname */
1550 xfs_ino_t *parent, /* out - NULLFSINO if entry not exist */
1551 blkmap_t *blkmap,
1552 int *dot, /* out - 1 if there is a dot, else 0 */
1553 int *dotdot, /* out - 1 if there's a dotdot, else 0 */
1554 int *repair) /* out - 1 if something was fixed */
1555 {
1556 xfs_dir2_block_t *block;
1557 xfs_dir2_leaf_entry_t *blp;
1558 bmap_ext_t *bmp;
1559 struct xfs_buf *bp;
1560 xfs_dir2_block_tail_t *btp;
1561 int nex;
1562 int rval;
1563 bmap_ext_t lbmp;
1564 int dirty = 0;
1565
1566 *repair = *dot = *dotdot = 0;
1567 *parent = NULLFSINO;
1568 nex = blkmap_getn(blkmap, mp->m_dirdatablk, mp->m_dirblkfsbs, &bmp, &lbmp);
1569 if (nex == 0) {
1570 do_warn(
1571 _("block %u for directory inode %" PRIu64 " is missing\n"),
1572 mp->m_dirdatablk, ino);
1573 return 1;
1574 }
1575 bp = da_read_buf(mp, nex, bmp);
1576 if (bmp != &lbmp)
1577 free(bmp);
1578 if (bp == NULL) {
1579 do_warn(
1580 _("can't read block %u for directory inode %" PRIu64 "\n"),
1581 mp->m_dirdatablk, ino);
1582 return 1;
1583 }
1584 /*
1585 * Verify the block
1586 */
1587 block = bp->b_addr;
1588 if (be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)
1589 do_warn(
1590 _("bad directory block magic # %#x in block %u for directory inode %" PRIu64 "\n"),
1591 be32_to_cpu(block->hdr.magic), mp->m_dirdatablk, ino);
1592 /*
1593 * process the data area
1594 * this also checks & fixes the bestfree
1595 */
1596 btp = xfs_dir2_block_tail_p(mp, &block->hdr);
1597 blp = xfs_dir2_block_leaf_p(btp);
1598 /*
1599 * Don't let this go past the end of the block.
1600 */
1601 if ((char *)blp > (char *)btp)
1602 blp = (xfs_dir2_leaf_entry_t *)btp;
1603 rval = process_dir2_data(mp, ino, dip, ino_discovery, dirname, parent,
1604 bp, dot, dotdot, mp->m_dirdatablk, (char *)blp, &dirty);
1605 if (dirty && !no_modify) {
1606 *repair = 1;
1607 libxfs_writebuf(bp, 0);
1608 } else
1609 libxfs_putbuf(bp);
1610 return rval;
1611 }
1612
1613 /*
1614 * Validates leaf contents, node format directories only.
1615 * magic number and sibling pointers checked by caller.
1616 * Returns 0 if block is ok, 1 if the block is bad.
1617 * Looking for: out of order hash values, bad stale counts.
1618 */
1619 static int
1620 process_leaf_block_dir2(
1621 xfs_mount_t *mp,
1622 xfs_dir2_leaf_t *leaf,
1623 xfs_dablk_t da_bno,
1624 xfs_ino_t ino,
1625 xfs_dahash_t last_hashval,
1626 xfs_dahash_t *next_hashval)
1627 {
1628 int i;
1629 int stale;
1630 struct xfs_dir2_leaf_entry *ents;
1631
1632 ents = xfs_dir3_leaf_ents_p(leaf);
1633
1634 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
1635 if ((char *)&ents[i] >= (char *)leaf + mp->m_dirblksize) {
1636 do_warn(
1637 _("bad entry count in block %u of directory inode %" PRIu64 "\n"),
1638 da_bno, ino);
1639 return 1;
1640 }
1641 if (be32_to_cpu(ents[i].address) == XFS_DIR2_NULL_DATAPTR)
1642 stale++;
1643 else if (be32_to_cpu(ents[i].hashval) < last_hashval) {
1644 do_warn(
1645 _("bad hash ordering in block %u of directory inode %" PRIu64 "\n"),
1646 da_bno, ino);
1647 return 1;
1648 }
1649 *next_hashval = last_hashval = be32_to_cpu(ents[i].hashval);
1650 }
1651 if (stale != be16_to_cpu(leaf->hdr.stale)) {
1652 do_warn(
1653 _("bad stale count in block %u of directory inode %" PRIu64 "\n"),
1654 da_bno, ino);
1655 return 1;
1656 }
1657 return 0;
1658 }
1659
1660 /*
1661 * Returns 0 if the directory is ok, 1 if it has to be rebuilt.
1662 */
1663 static int
1664 process_leaf_level_dir2(
1665 xfs_mount_t *mp,
1666 dir2_bt_cursor_t *da_cursor,
1667 int *repair)
1668 {
1669 bmap_ext_t *bmp;
1670 struct xfs_buf *bp;
1671 int buf_dirty;
1672 xfs_dahash_t current_hashval;
1673 xfs_dablk_t da_bno;
1674 xfs_dahash_t greatest_hashval;
1675 xfs_ino_t ino;
1676 xfs_dir2_leaf_t *leaf;
1677 int nex;
1678 xfs_dablk_t prev_bno;
1679 bmap_ext_t lbmp;
1680
1681 da_bno = da_cursor->level[0].bno;
1682 ino = da_cursor->ino;
1683 prev_bno = 0;
1684 bmp = NULL;
1685 current_hashval = 0;
1686 greatest_hashval = 0;
1687 buf_dirty = 0;
1688
1689 do {
1690 nex = blkmap_getn(da_cursor->blkmap, da_bno, mp->m_dirblkfsbs,
1691 &bmp, &lbmp);
1692 /*
1693 * Directory code uses 0 as the NULL block pointer since 0
1694 * is the root block and no directory block pointer can point
1695 * to the root block of the btree.
1696 */
1697 ASSERT(da_bno != 0);
1698
1699 if (nex == 0) {
1700 do_warn(
1701 _("can't map block %u for directory inode %" PRIu64 "\n"),
1702 da_bno, ino);
1703 goto error_out;
1704 }
1705 bp = da_read_buf(mp, nex, bmp);
1706 if (bmp != &lbmp)
1707 free(bmp);
1708 bmp = NULL;
1709 if (bp == NULL) {
1710 do_warn(
1711 _("can't read file block %u for directory inode %" PRIu64 "\n"),
1712 da_bno, ino);
1713 goto error_out;
1714 }
1715 leaf = bp->b_addr;
1716 /*
1717 * Check magic number for leaf directory btree block.
1718 */
1719 if (be16_to_cpu(leaf->hdr.info.magic) !=
1720 XFS_DIR2_LEAFN_MAGIC) {
1721 do_warn(
1722 _("bad directory leaf magic # %#x for directory inode %" PRIu64 " block %u\n"),
1723 be16_to_cpu(leaf->hdr.info.magic),
1724 ino, da_bno);
1725 libxfs_putbuf(bp);
1726 goto error_out;
1727 }
1728 buf_dirty = 0;
1729 /*
1730 * For each block, process the block, verify its path,
1731 * then get next block. Update cursor values along the way.
1732 */
1733 if (process_leaf_block_dir2(mp, leaf, da_bno, ino,
1734 current_hashval, &greatest_hashval)) {
1735 libxfs_putbuf(bp);
1736 goto error_out;
1737 }
1738 /*
1739 * Index can be set to hdr.count so match the indices of the
1740 * interior blocks -- which at the end of the block will point
1741 * to 1 after the final real entry in the block.
1742 */
1743 da_cursor->level[0].hashval = greatest_hashval;
1744 da_cursor->level[0].bp = bp;
1745 da_cursor->level[0].bno = da_bno;
1746 da_cursor->level[0].index =
1747 be16_to_cpu(leaf->hdr.count);
1748 da_cursor->level[0].dirty = buf_dirty;
1749
1750 if (be32_to_cpu(leaf->hdr.info.back) != prev_bno) {
1751 do_warn(
1752 _("bad sibling back pointer for block %u in directory inode %" PRIu64 "\n"),
1753 da_bno, ino);
1754 libxfs_putbuf(bp);
1755 goto error_out;
1756 }
1757 prev_bno = da_bno;
1758 da_bno = be32_to_cpu(leaf->hdr.info.forw);
1759 if (da_bno != 0) {
1760 if (verify_dir2_path(mp, da_cursor, 0)) {
1761 libxfs_putbuf(bp);
1762 goto error_out;
1763 }
1764 }
1765 current_hashval = greatest_hashval;
1766 ASSERT(buf_dirty == 0 || (buf_dirty && !no_modify));
1767 if (buf_dirty && !no_modify) {
1768 *repair = 1;
1769 libxfs_writebuf(bp, 0);
1770 } else
1771 libxfs_putbuf(bp);
1772 } while (da_bno != 0);
1773 if (verify_final_dir2_path(mp, da_cursor, 0)) {
1774 /*
1775 * Verify the final path up (right-hand-side) if still ok.
1776 */
1777 do_warn(_("bad hash path in directory %" PRIu64 "\n"), ino);
1778 goto error_out;
1779 }
1780 /*
1781 * Redundant but just for testing.
1782 */
1783 release_dir2_cursor(mp, da_cursor, 0);
1784 return 0;
1785
1786 error_out:
1787 /*
1788 * Release all buffers holding interior btree blocks.
1789 */
1790 err_release_dir2_cursor(mp, da_cursor, 0);
1791 if (bmp && (bmp != &lbmp))
1792 free(bmp);
1793 return 1;
1794 }
1795
1796 /*
1797 * Return 1 if the directory's leaf/node space is corrupted and
1798 * needs to be rebuilt, 0 if it's ok.
1799 */
1800 static int
1801 process_node_dir2(
1802 xfs_mount_t *mp,
1803 xfs_ino_t ino,
1804 xfs_dinode_t *dip,
1805 blkmap_t *blkmap,
1806 int *repair)
1807 {
1808 xfs_dablk_t bno;
1809 dir2_bt_cursor_t da_cursor;
1810
1811 /*
1812 * Try again -- traverse down left-side of tree until we hit the
1813 * left-most leaf block setting up the btree cursor along the way.
1814 * Then walk the leaf blocks left-to-right, calling a parent
1815 * verification routine each time we traverse a block.
1816 */
1817 memset(&da_cursor, 0, sizeof(da_cursor));
1818 da_cursor.ino = ino;
1819 da_cursor.dip = dip;
1820 da_cursor.blkmap = blkmap;
1821
1822 /*
1823 * Now process interior node.
1824 */
1825 if (traverse_int_dir2block(mp, &da_cursor, &bno) == 0)
1826 return 1;
1827
1828 /*
1829 * Skip directories with a root marked XFS_DIR2_LEAFN_MAGIC
1830 */
1831 if (bno == 0) {
1832 release_dir2_cursor(mp, &da_cursor, 0);
1833 return 0;
1834 } else {
1835 /*
1836 * Now pass cursor and bno into leaf-block processing routine.
1837 * The leaf dir level routine checks the interior paths up to
1838 * the root including the final right-most path.
1839 */
1840 return process_leaf_level_dir2(mp, &da_cursor, repair);
1841 }
1842 }
1843
1844 /*
1845 * Process leaf and node directories.
1846 * Process the data blocks then, if it's a node directory, check
1847 * the consistency of those blocks.
1848 */
1849 static int
1850 process_leaf_node_dir2(
1851 xfs_mount_t *mp,
1852 xfs_ino_t ino,
1853 xfs_dinode_t *dip,
1854 int ino_discovery,
1855 char *dirname, /* directory pathname */
1856 xfs_ino_t *parent, /* out - NULLFSINO if entry not exist */
1857 blkmap_t *blkmap,
1858 int *dot, /* out - 1 if there is a dot, else 0 */
1859 int *dotdot, /* out - 1 if there's a dotdot, else 0 */
1860 int *repair, /* out - 1 if something was fixed */
1861 int isnode) /* node directory not leaf */
1862 {
1863 bmap_ext_t *bmp;
1864 struct xfs_buf *bp;
1865 xfs_dir2_data_t *data;
1866 xfs_dfiloff_t dbno;
1867 int good;
1868 int i;
1869 xfs_dfiloff_t ndbno;
1870 int nex;
1871 int t;
1872 bmap_ext_t lbmp;
1873 int dirty = 0;
1874
1875 *repair = *dot = *dotdot = good = 0;
1876 *parent = NULLFSINO;
1877 ndbno = NULLDFILOFF;
1878 while ((dbno = blkmap_next_off(blkmap, ndbno, &t)) < mp->m_dirleafblk) {
1879 nex = blkmap_getn(blkmap, dbno, mp->m_dirblkfsbs, &bmp, &lbmp);
1880 /* Advance through map to last dfs block in this dir block */
1881 ndbno = dbno;
1882 while (ndbno < dbno + mp->m_dirblkfsbs - 1) {
1883 ndbno = blkmap_next_off(blkmap, ndbno, &t);
1884 }
1885 if (nex == 0) {
1886 do_warn(
1887 _("block %" PRIu64 " for directory inode %" PRIu64 " is missing\n"),
1888 dbno, ino);
1889 continue;
1890 }
1891 bp = da_read_buf(mp, nex, bmp);
1892 if (bmp != &lbmp)
1893 free(bmp);
1894 if (bp == NULL) {
1895 do_warn(
1896 _("can't read block %" PRIu64 " for directory inode %" PRIu64 "\n"),
1897 dbno, ino);
1898 continue;
1899 }
1900 data = bp->b_addr;
1901 if (be32_to_cpu(data->hdr.magic) != XFS_DIR2_DATA_MAGIC)
1902 do_warn(
1903 _("bad directory block magic # %#x in block %" PRIu64 " for directory inode %" PRIu64 "\n"),
1904 be32_to_cpu(data->hdr.magic), dbno, ino);
1905 i = process_dir2_data(mp, ino, dip, ino_discovery, dirname,
1906 parent, bp, dot, dotdot, (xfs_dablk_t)dbno,
1907 (char *)data + mp->m_dirblksize, &dirty);
1908 if (i == 0)
1909 good++;
1910 if (dirty && !no_modify) {
1911 *repair = 1;
1912 libxfs_writebuf(bp, 0);
1913 } else
1914 libxfs_putbuf(bp);
1915 }
1916 if (good == 0)
1917 return 1;
1918 if (!isnode)
1919 return 0;
1920 if (dir2_is_badino(ino))
1921 return 0;
1922
1923 if (process_node_dir2(mp, ino, dip, blkmap, repair))
1924 dir2_add_badlist(ino);
1925 return 0;
1926
1927 }
1928
1929 /*
1930 * Returns 1 if things are bad (directory needs to be junked)
1931 * and 0 if things are ok. If ino_discovery is 1, add unknown
1932 * inodes to uncertain inode list.
1933 */
1934 int
1935 process_dir2(
1936 xfs_mount_t *mp,
1937 xfs_ino_t ino,
1938 xfs_dinode_t *dip,
1939 int ino_discovery,
1940 int *dino_dirty,
1941 char *dirname,
1942 xfs_ino_t *parent,
1943 blkmap_t *blkmap)
1944 {
1945 int dot;
1946 int dotdot;
1947 xfs_dfiloff_t last;
1948 int repair;
1949 int res;
1950
1951 *parent = NULLFSINO;
1952 dot = dotdot = 0;
1953 last = 0;
1954
1955 /*
1956 * branch off depending on the type of inode. This routine
1957 * is only called ONCE so all the subordinate routines will
1958 * fix '.' and junk '..' if they're bogus.
1959 */
1960 if (blkmap)
1961 last = blkmap_last_off(blkmap);
1962 if (be64_to_cpu(dip->di_size) <= XFS_DFORK_DSIZE(dip, mp) &&
1963 dip->di_format == XFS_DINODE_FMT_LOCAL) {
1964 dot = dotdot = 1;
1965 res = process_sf_dir2(mp, ino, dip, ino_discovery, dino_dirty,
1966 dirname, parent, &repair);
1967 } else if (last == mp->m_dirblkfsbs &&
1968 (dip->di_format == XFS_DINODE_FMT_EXTENTS ||
1969 dip->di_format == XFS_DINODE_FMT_BTREE)) {
1970 res = process_block_dir2(mp, ino, dip, ino_discovery,
1971 dino_dirty, dirname, parent, blkmap, &dot, &dotdot,
1972 &repair);
1973 } else if (last >= mp->m_dirleafblk + mp->m_dirblkfsbs &&
1974 (dip->di_format == XFS_DINODE_FMT_EXTENTS ||
1975 dip->di_format == XFS_DINODE_FMT_BTREE)) {
1976 res = process_leaf_node_dir2(mp, ino, dip, ino_discovery,
1977 dirname, parent, blkmap, &dot, &dotdot, &repair,
1978 last > mp->m_dirleafblk + mp->m_dirblkfsbs);
1979 } else {
1980 do_warn(_("bad size/format for directory %" PRIu64 "\n"), ino);
1981 return 1;
1982 }
1983 /*
1984 * bad . entries in all directories will be fixed up in phase 6
1985 */
1986 if (dot == 0) {
1987 do_warn(_("no . entry for directory %" PRIu64 "\n"), ino);
1988 }
1989
1990 /*
1991 * shortform dirs always have a .. entry. .. for all longform
1992 * directories will get fixed in phase 6. .. for other shortform
1993 * dirs also get fixed there. .. for a shortform root was
1994 * fixed in place since we know what it should be
1995 */
1996 if (dotdot == 0 && ino != mp->m_sb.sb_rootino) {
1997 do_warn(_("no .. entry for directory %" PRIu64 "\n"), ino);
1998 } else if (dotdot == 0 && ino == mp->m_sb.sb_rootino) {
1999 do_warn(_("no .. entry for root directory %" PRIu64 "\n"), ino);
2000 need_root_dotdot = 1;
2001 }
2002
2003 ASSERT((ino != mp->m_sb.sb_rootino && ino != *parent) ||
2004 (ino == mp->m_sb.sb_rootino &&
2005 (ino == *parent || need_root_dotdot == 1)));
2006
2007 return res;
2008 }