]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/dinode.c
xfs_repair: don't dirty inodes unconditionally when testing unlinked state
[thirdparty/xfsprogs-dev.git] / repair / dinode.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "libxfs.h"
8 #include "avl.h"
9 #include "globals.h"
10 #include "agheader.h"
11 #include "incore.h"
12 #include "protos.h"
13 #include "err_protos.h"
14 #include "dir2.h"
15 #include "dinode.h"
16 #include "scan.h"
17 #include "versions.h"
18 #include "attr_repair.h"
19 #include "bmap.h"
20 #include "threads.h"
21 #include "slab.h"
22 #include "rmap.h"
23
24 /*
25 * gettext lookups for translations of strings use mutexes internally to
26 * the library. Hence when we come through here doing parallel scans in
27 * multiple AGs, then all do concurrent text conversions and serialise
28 * on the translation string lookups. Let's avoid doing repeated lookups
29 * by making them static variables and only assigning the translation
30 * once.
31 */
32 static char *forkname_data;
33 static char *forkname_attr;
34 static char *ftype_real_time;
35 static char *ftype_regular;
36
37 void
38 dinode_bmbt_translation_init(void)
39 {
40 forkname_data = _("data");
41 forkname_attr = _("attr");
42 ftype_real_time = _("real-time");
43 ftype_regular = _("regular");
44 }
45
46 char *
47 get_forkname(int whichfork)
48 {
49
50 if (whichfork == XFS_DATA_FORK)
51 return forkname_data;
52 return forkname_attr;
53 }
54
55 /*
56 * inode clearing routines
57 */
58
59 static int
60 clear_dinode_attr(xfs_mount_t *mp, xfs_dinode_t *dino, xfs_ino_t ino_num)
61 {
62 ASSERT(dino->di_forkoff != 0);
63
64 if (!no_modify)
65 fprintf(stderr,
66 _("clearing inode %" PRIu64 " attributes\n"), ino_num);
67 else
68 fprintf(stderr,
69 _("would have cleared inode %" PRIu64 " attributes\n"), ino_num);
70
71 if (be16_to_cpu(dino->di_anextents) != 0) {
72 if (no_modify)
73 return(1);
74 dino->di_anextents = cpu_to_be16(0);
75 }
76
77 if (dino->di_aformat != XFS_DINODE_FMT_EXTENTS) {
78 if (no_modify)
79 return(1);
80 dino->di_aformat = XFS_DINODE_FMT_EXTENTS;
81 }
82
83 /* get rid of the fork by clearing forkoff */
84
85 /* Originally, when the attr repair code was added, the fork was cleared
86 * by turning it into shortform status. This meant clearing the
87 * hdr.totsize/count fields and also changing aformat to LOCAL
88 * (vs EXTENTS). Over various fixes, the aformat and forkoff have
89 * been updated to not show an attribute fork at all, however.
90 * It could be possible that resetting totsize/count are not needed,
91 * but just to be safe, leave it in for now.
92 */
93
94 if (!no_modify) {
95 xfs_attr_shortform_t *asf = (xfs_attr_shortform_t *)
96 XFS_DFORK_APTR(dino);
97 asf->hdr.totsize = cpu_to_be16(sizeof(xfs_attr_sf_hdr_t));
98 asf->hdr.count = 0;
99 dino->di_forkoff = 0; /* got to do this after asf is set */
100 }
101
102 /*
103 * always returns 1 since the fork gets zapped
104 */
105 return(1);
106 }
107
108 static void
109 clear_dinode_core(struct xfs_mount *mp, xfs_dinode_t *dinoc, xfs_ino_t ino_num)
110 {
111 memset(dinoc, 0, sizeof(*dinoc));
112 dinoc->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
113 if (xfs_sb_version_hascrc(&mp->m_sb))
114 dinoc->di_version = 3;
115 else
116 dinoc->di_version = 2;
117 dinoc->di_gen = cpu_to_be32(random());
118 dinoc->di_format = XFS_DINODE_FMT_EXTENTS;
119 dinoc->di_aformat = XFS_DINODE_FMT_EXTENTS;
120 /* we are done for version 1/2 inodes */
121 if (dinoc->di_version < 3)
122 return;
123 dinoc->di_ino = cpu_to_be64(ino_num);
124 platform_uuid_copy(&dinoc->di_uuid, &mp->m_sb.sb_meta_uuid);
125 return;
126 }
127
128 static void
129 clear_dinode_unlinked(xfs_mount_t *mp, xfs_dinode_t *dino)
130 {
131
132 dino->di_next_unlinked = cpu_to_be32(NULLAGINO);
133 }
134
135 /*
136 * this clears the unlinked list too so it should not be called
137 * until after the agi unlinked lists are walked in phase 3.
138 */
139 static void
140 clear_dinode(xfs_mount_t *mp, xfs_dinode_t *dino, xfs_ino_t ino_num)
141 {
142 clear_dinode_core(mp, dino, ino_num);
143 clear_dinode_unlinked(mp, dino);
144
145 /* and clear the forks */
146 memset(XFS_DFORK_DPTR(dino), 0, XFS_LITINO(mp, dino->di_version));
147 return;
148 }
149
150
151 /*
152 * misc. inode-related utility routines
153 */
154
155 /*
156 * verify_ag_bno is heavily used. In the common case, it
157 * performs just two number of compares
158 * Returns 1 for bad ag/bno pair or 0 if it's valid.
159 */
160 static __inline int
161 verify_ag_bno(xfs_sb_t *sbp,
162 xfs_agnumber_t agno,
163 xfs_agblock_t agbno)
164 {
165 if (agno < (sbp->sb_agcount - 1))
166 return (agbno >= sbp->sb_agblocks);
167 if (agno == (sbp->sb_agcount - 1))
168 return (agbno >= (sbp->sb_dblocks -
169 ((xfs_rfsblock_t)(sbp->sb_agcount - 1) *
170 sbp->sb_agblocks)));
171 return 1;
172 }
173
174 /*
175 * returns 0 if inode number is valid, 1 if bogus
176 */
177 int
178 verify_inum(xfs_mount_t *mp,
179 xfs_ino_t ino)
180 {
181 xfs_agnumber_t agno;
182 xfs_agino_t agino;
183 xfs_agblock_t agbno;
184 xfs_sb_t *sbp = &mp->m_sb;;
185
186 /* range check ag #, ag block. range-checking offset is pointless */
187
188 agno = XFS_INO_TO_AGNO(mp, ino);
189 agino = XFS_INO_TO_AGINO(mp, ino);
190 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
191 if (agbno == 0)
192 return 1;
193
194 if (ino == 0 || ino == NULLFSINO)
195 return(1);
196
197 if (ino != XFS_AGINO_TO_INO(mp, agno, agino))
198 return(1);
199
200 return verify_ag_bno(sbp, agno, agbno);
201 }
202
203 /*
204 * have a separate routine to ensure that we don't accidentally
205 * lose illegally set bits in the agino by turning it into an FSINO
206 * to feed to the above routine
207 */
208 int
209 verify_aginum(xfs_mount_t *mp,
210 xfs_agnumber_t agno,
211 xfs_agino_t agino)
212 {
213 xfs_agblock_t agbno;
214 xfs_sb_t *sbp = &mp->m_sb;;
215
216 /* range check ag #, ag block. range-checking offset is pointless */
217
218 if (agino == 0 || agino == NULLAGINO)
219 return(1);
220
221 /*
222 * agino's can't be too close to NULLAGINO because the min blocksize
223 * is 9 bits and at most 1 bit of that gets used for the inode offset
224 * so if the agino gets shifted by the # of offset bits and compared
225 * to the legal agbno values, a bogus agino will be too large. there
226 * will be extra bits set at the top that shouldn't be set.
227 */
228 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
229 if (agbno == 0)
230 return 1;
231
232 return verify_ag_bno(sbp, agno, agbno);
233 }
234
235 /*
236 * return 1 if block number is good, 0 if out of range
237 */
238 int
239 verify_dfsbno(xfs_mount_t *mp,
240 xfs_fsblock_t fsbno)
241 {
242 xfs_agnumber_t agno;
243 xfs_agblock_t agbno;
244 xfs_sb_t *sbp = &mp->m_sb;;
245
246 /* range check ag #, ag block. range-checking offset is pointless */
247
248 agno = XFS_FSB_TO_AGNO(mp, fsbno);
249 agbno = XFS_FSB_TO_AGBNO(mp, fsbno);
250
251 return verify_ag_bno(sbp, agno, agbno) == 0;
252 }
253
254 #define XR_DFSBNORANGE_VALID 0
255 #define XR_DFSBNORANGE_BADSTART 1
256 #define XR_DFSBNORANGE_BADEND 2
257 #define XR_DFSBNORANGE_OVERFLOW 3
258
259 static __inline int
260 verify_dfsbno_range(xfs_mount_t *mp,
261 xfs_fsblock_t fsbno,
262 xfs_filblks_t count)
263 {
264 xfs_agnumber_t agno;
265 xfs_agblock_t agbno;
266 xfs_sb_t *sbp = &mp->m_sb;;
267
268 /* the start and end blocks better be in the same allocation group */
269 agno = XFS_FSB_TO_AGNO(mp, fsbno);
270 if (agno != XFS_FSB_TO_AGNO(mp, fsbno + count - 1)) {
271 return XR_DFSBNORANGE_OVERFLOW;
272 }
273
274 agbno = XFS_FSB_TO_AGBNO(mp, fsbno);
275 if (verify_ag_bno(sbp, agno, agbno)) {
276 return XR_DFSBNORANGE_BADSTART;
277 }
278
279 agbno = XFS_FSB_TO_AGBNO(mp, fsbno + count - 1);
280 if (verify_ag_bno(sbp, agno, agbno)) {
281 return XR_DFSBNORANGE_BADEND;
282 }
283
284 return (XR_DFSBNORANGE_VALID);
285 }
286
287 int
288 verify_agbno(xfs_mount_t *mp,
289 xfs_agnumber_t agno,
290 xfs_agblock_t agbno)
291 {
292 xfs_sb_t *sbp = &mp->m_sb;;
293
294 /* range check ag #, ag block. range-checking offset is pointless */
295 return verify_ag_bno(sbp, agno, agbno) == 0;
296 }
297
298 static int
299 process_rt_rec(
300 xfs_mount_t *mp,
301 xfs_bmbt_irec_t *irec,
302 xfs_ino_t ino,
303 xfs_rfsblock_t *tot,
304 int check_dups)
305 {
306 xfs_fsblock_t b;
307 xfs_rtblock_t ext;
308 int state;
309 int pwe; /* partially-written extent */
310
311 /*
312 * check numeric validity of the extent
313 */
314 if (irec->br_startblock >= mp->m_sb.sb_rblocks) {
315 do_warn(
316 _("inode %" PRIu64 " - bad rt extent start block number %" PRIu64 ", offset %" PRIu64 "\n"),
317 ino,
318 irec->br_startblock,
319 irec->br_startoff);
320 return 1;
321 }
322 if (irec->br_startblock + irec->br_blockcount - 1 >= mp->m_sb.sb_rblocks) {
323 do_warn(
324 _("inode %" PRIu64 " - bad rt extent last block number %" PRIu64 ", offset %" PRIu64 "\n"),
325 ino,
326 irec->br_startblock + irec->br_blockcount - 1,
327 irec->br_startoff);
328 return 1;
329 }
330 if (irec->br_startblock + irec->br_blockcount - 1 < irec->br_startblock) {
331 do_warn(
332 _("inode %" PRIu64 " - bad rt extent overflows - start %" PRIu64 ", "
333 "end %" PRIu64 ", offset %" PRIu64 "\n"),
334 ino,
335 irec->br_startblock,
336 irec->br_startblock + irec->br_blockcount - 1,
337 irec->br_startoff);
338 return 1;
339 }
340
341 /*
342 * verify that the blocks listed in the record
343 * are multiples of an extent
344 */
345 if (xfs_sb_version_hasextflgbit(&mp->m_sb) == 0 &&
346 (irec->br_startblock % mp->m_sb.sb_rextsize != 0 ||
347 irec->br_blockcount % mp->m_sb.sb_rextsize != 0)) {
348 do_warn(
349 _("malformed rt inode extent [%" PRIu64 " %" PRIu64 "] (fs rtext size = %u)\n"),
350 irec->br_startblock,
351 irec->br_blockcount,
352 mp->m_sb.sb_rextsize);
353 return 1;
354 }
355
356 /*
357 * set the appropriate number of extents
358 * this iterates block by block, this can be optimised using extents
359 */
360 for (b = irec->br_startblock; b < irec->br_startblock +
361 irec->br_blockcount; b += mp->m_sb.sb_rextsize) {
362 ext = (xfs_rtblock_t) b / mp->m_sb.sb_rextsize;
363 pwe = xfs_sb_version_hasextflgbit(&mp->m_sb) &&
364 irec->br_state == XFS_EXT_UNWRITTEN &&
365 (b % mp->m_sb.sb_rextsize != 0);
366
367 if (check_dups == 1) {
368 if (search_rt_dup_extent(mp, ext) && !pwe) {
369 do_warn(
370 _("data fork in rt ino %" PRIu64 " claims dup rt extent,"
371 "off - %" PRIu64 ", start - %" PRIu64 ", count %" PRIu64 "\n"),
372 ino,
373 irec->br_startoff,
374 irec->br_startblock,
375 irec->br_blockcount);
376 return 1;
377 }
378 continue;
379 }
380
381 state = get_rtbmap(ext);
382 switch (state) {
383 case XR_E_FREE:
384 case XR_E_UNKNOWN:
385 set_rtbmap(ext, XR_E_INUSE);
386 break;
387 case XR_E_BAD_STATE:
388 do_error(
389 _("bad state in rt block map %" PRIu64 "\n"),
390 ext);
391 case XR_E_FS_MAP:
392 case XR_E_INO:
393 case XR_E_INUSE_FS:
394 do_error(
395 _("data fork in rt inode %" PRIu64 " found metadata block %" PRIu64 " in rt bmap\n"),
396 ino, ext);
397 case XR_E_INUSE:
398 if (pwe)
399 break;
400 /* fall through */
401 case XR_E_MULT:
402 set_rtbmap(ext, XR_E_MULT);
403 do_warn(
404 _("data fork in rt inode %" PRIu64 " claims used rt block %" PRIu64 "\n"),
405 ino, ext);
406 return 1;
407 case XR_E_FREE1:
408 default:
409 do_error(
410 _("illegal state %d in rt block map %" PRIu64 "\n"),
411 state, b);
412 }
413 }
414
415 /*
416 * bump up the block counter
417 */
418 *tot += irec->br_blockcount;
419
420 return 0;
421 }
422
423 /*
424 * return 1 if inode should be cleared, 0 otherwise
425 * if check_dups should be set to 1, that implies that
426 * the primary purpose of this call is to see if the
427 * file overlaps with any duplicate extents (in the
428 * duplicate extent list).
429 */
430 static int
431 process_bmbt_reclist_int(
432 xfs_mount_t *mp,
433 xfs_bmbt_rec_t *rp,
434 int *numrecs,
435 int type,
436 xfs_ino_t ino,
437 xfs_rfsblock_t *tot,
438 blkmap_t **blkmapp,
439 xfs_fileoff_t *first_key,
440 xfs_fileoff_t *last_key,
441 int check_dups,
442 int whichfork)
443 {
444 xfs_bmbt_irec_t irec;
445 xfs_filblks_t cp = 0; /* prev count */
446 xfs_fsblock_t sp = 0; /* prev start */
447 xfs_fileoff_t op = 0; /* prev offset */
448 xfs_fsblock_t b;
449 char *ftype;
450 char *forkname = get_forkname(whichfork);
451 int i;
452 int state;
453 xfs_agnumber_t agno;
454 xfs_agblock_t agbno;
455 xfs_agblock_t ebno;
456 xfs_extlen_t blen;
457 xfs_agnumber_t locked_agno = -1;
458 int error = 1;
459
460 if (type == XR_INO_RTDATA)
461 ftype = ftype_real_time;
462 else
463 ftype = ftype_regular;
464
465 for (i = 0; i < *numrecs; i++) {
466 libxfs_bmbt_disk_get_all((rp +i), &irec);
467 if (i == 0)
468 *last_key = *first_key = irec.br_startoff;
469 else
470 *last_key = irec.br_startoff;
471 if (i > 0 && op + cp > irec.br_startoff) {
472 do_warn(
473 _("bmap rec out of order, inode %" PRIu64" entry %d "
474 "[o s c] [%" PRIu64 " %" PRIu64 " %" PRIu64 "], "
475 "%d [%" PRIu64 " %" PRIu64 " %" PRIu64 "]\n"),
476 ino, i, irec.br_startoff, irec.br_startblock,
477 irec.br_blockcount, i - 1, op, sp, cp);
478 goto done;
479 }
480 op = irec.br_startoff;
481 cp = irec.br_blockcount;
482 sp = irec.br_startblock;
483
484 /*
485 * check numeric validity of the extent
486 */
487 if (irec.br_blockcount == 0) {
488 do_warn(
489 _("zero length extent (off = %" PRIu64 ", fsbno = %" PRIu64 ") in ino %" PRIu64 "\n"),
490 irec.br_startoff,
491 irec.br_startblock,
492 ino);
493 goto done;
494 }
495
496 if (type == XR_INO_RTDATA && whichfork == XFS_DATA_FORK) {
497 /*
498 * realtime bitmaps don't use AG locks, so returning
499 * immediately is fine for this code path.
500 */
501 if (process_rt_rec(mp, &irec, ino, tot, check_dups))
502 return 1;
503 /*
504 * skip rest of loop processing since that'irec.br_startblock
505 * all for regular file forks and attr forks
506 */
507 continue;
508 }
509
510 /*
511 * regular file data fork or attribute fork
512 */
513 switch (verify_dfsbno_range(mp, irec.br_startblock,
514 irec.br_blockcount)) {
515 case XR_DFSBNORANGE_VALID:
516 break;
517
518 case XR_DFSBNORANGE_BADSTART:
519 do_warn(
520 _("inode %" PRIu64 " - bad extent starting block number %" PRIu64 ", offset %" PRIu64 "\n"),
521 ino,
522 irec.br_startblock,
523 irec.br_startoff);
524 goto done;
525
526 case XR_DFSBNORANGE_BADEND:
527 do_warn(
528 _("inode %" PRIu64 " - bad extent last block number %" PRIu64 ", offset %" PRIu64 "\n"),
529 ino,
530 irec.br_startblock + irec.br_blockcount - 1,
531 irec.br_startoff);
532 goto done;
533
534 case XR_DFSBNORANGE_OVERFLOW:
535 do_warn(
536 _("inode %" PRIu64 " - bad extent overflows - start %" PRIu64 ", "
537 "end %" PRIu64 ", offset %" PRIu64 "\n"),
538 ino,
539 irec.br_startblock,
540 irec.br_startblock + irec.br_blockcount - 1,
541 irec.br_startoff);
542 goto done;
543 }
544 /* Ensure this extent does not extend beyond the max offset */
545 if (irec.br_startoff + irec.br_blockcount - 1 >
546 fs_max_file_offset) {
547 do_warn(
548 _("inode %" PRIu64 " - extent exceeds max offset - start %" PRIu64 ", "
549 "count %" PRIu64 ", physical block %" PRIu64 "\n"),
550 ino, irec.br_startoff, irec.br_blockcount,
551 irec.br_startblock);
552 goto done;
553 }
554
555 if (blkmapp && *blkmapp) {
556 int error2;
557 error2 = blkmap_set_ext(blkmapp, irec.br_startoff,
558 irec.br_startblock, irec.br_blockcount);
559 if (error2) {
560 /*
561 * we don't want to clear the inode due to an
562 * internal bmap tracking error, but if we've
563 * run out of memory then we simply can't
564 * validate that the filesystem is consistent.
565 * Hence just abort at this point with an ENOMEM
566 * error.
567 */
568 do_abort(
569 _("Fatal error: inode %" PRIu64 " - blkmap_set_ext(): %s\n"
570 "\t%s fork, off - %" PRIu64 ", start - %" PRIu64 ", cnt %" PRIu64 "\n"),
571 ino, strerror(error2), forkname,
572 irec.br_startoff, irec.br_startblock,
573 irec.br_blockcount);
574 }
575 }
576
577 /*
578 * Profiling shows that the following loop takes the
579 * most time in all of xfs_repair.
580 */
581 agno = XFS_FSB_TO_AGNO(mp, irec.br_startblock);
582 agbno = XFS_FSB_TO_AGBNO(mp, irec.br_startblock);
583 ebno = agbno + irec.br_blockcount;
584 if (agno != locked_agno) {
585 if (locked_agno != -1)
586 pthread_mutex_unlock(&ag_locks[locked_agno].lock);
587 pthread_mutex_lock(&ag_locks[agno].lock);
588 locked_agno = agno;
589 }
590
591 if (check_dups) {
592 /*
593 * if we're just checking the bmap for dups,
594 * return if we find one, otherwise, continue
595 * checking each entry without setting the
596 * block bitmap
597 */
598 if (!(type == XR_INO_DATA &&
599 xfs_sb_version_hasreflink(&mp->m_sb)) &&
600 search_dup_extent(agno, agbno, ebno)) {
601 do_warn(
602 _("%s fork in ino %" PRIu64 " claims dup extent, "
603 "off - %" PRIu64 ", start - %" PRIu64 ", cnt %" PRIu64 "\n"),
604 forkname, ino, irec.br_startoff,
605 irec.br_startblock,
606 irec.br_blockcount);
607 goto done;
608 }
609 *tot += irec.br_blockcount;
610 continue;
611 }
612
613 for (b = irec.br_startblock;
614 agbno < ebno;
615 b += blen, agbno += blen) {
616 state = get_bmap_ext(agno, agbno, ebno, &blen);
617 switch (state) {
618 case XR_E_FREE:
619 case XR_E_FREE1:
620 do_warn(
621 _("%s fork in ino %" PRIu64 " claims free block %" PRIu64 "\n"),
622 forkname, ino, (uint64_t) b);
623 /* fall through ... */
624 case XR_E_INUSE1: /* seen by rmap */
625 case XR_E_UNKNOWN:
626 break;
627
628 case XR_E_BAD_STATE:
629 do_error(_("bad state in block map %" PRIu64 "\n"), b);
630
631 case XR_E_FS_MAP1:
632 case XR_E_INO1:
633 case XR_E_INUSE_FS1:
634 do_warn(_("rmap claims metadata use!\n"));
635 /* fall through */
636 case XR_E_FS_MAP:
637 case XR_E_INO:
638 case XR_E_INUSE_FS:
639 case XR_E_REFC:
640 do_warn(
641 _("%s fork in inode %" PRIu64 " claims metadata block %" PRIu64 "\n"),
642 forkname, ino, b);
643 goto done;
644
645 case XR_E_INUSE:
646 case XR_E_MULT:
647 if (type == XR_INO_DATA &&
648 xfs_sb_version_hasreflink(&mp->m_sb))
649 break;
650 do_warn(
651 _("%s fork in %s inode %" PRIu64 " claims used block %" PRIu64 "\n"),
652 forkname, ftype, ino, b);
653 goto done;
654
655 case XR_E_COW:
656 do_warn(
657 _("%s fork in %s inode %" PRIu64 " claims CoW block %" PRIu64 "\n"),
658 forkname, ftype, ino, b);
659 goto done;
660
661 default:
662 do_error(
663 _("illegal state %d in block map %" PRIu64 "\n"),
664 state, b);
665 goto done;
666 }
667 }
668
669 /*
670 * Update the internal extent map only after we've checked
671 * every block in this extent. The first time we reject this
672 * data fork we'll try to rebuild the bmbt from rmap data.
673 * After a successful rebuild we'll try this scan again.
674 * (If the rebuild fails we won't come back here.)
675 */
676 agbno = XFS_FSB_TO_AGBNO(mp, irec.br_startblock);
677 ebno = agbno + irec.br_blockcount;
678 for (; agbno < ebno; agbno += blen) {
679 state = get_bmap_ext(agno, agbno, ebno, &blen);
680 switch (state) {
681 case XR_E_FREE:
682 case XR_E_FREE1:
683 case XR_E_INUSE1:
684 case XR_E_UNKNOWN:
685 set_bmap_ext(agno, agbno, blen, XR_E_INUSE);
686 break;
687 case XR_E_INUSE:
688 case XR_E_MULT:
689 set_bmap_ext(agno, agbno, blen, XR_E_MULT);
690 break;
691 default:
692 break;
693 }
694 }
695 if (collect_rmaps) { /* && !check_dups */
696 error = rmap_add_rec(mp, ino, whichfork, &irec);
697 if (error)
698 do_error(
699 _("couldn't add reverse mapping\n")
700 );
701 }
702 *tot += irec.br_blockcount;
703 }
704 error = 0;
705 done:
706 if (locked_agno != -1)
707 pthread_mutex_unlock(&ag_locks[locked_agno].lock);
708
709 if (i != *numrecs) {
710 ASSERT(i < *numrecs);
711 do_warn(_("correcting nextents for inode %" PRIu64 "\n"), ino);
712 *numrecs = i;
713 }
714
715 return error;
716 }
717
718 /*
719 * return 1 if inode should be cleared, 0 otherwise, sets block bitmap
720 * as a side-effect
721 */
722 int
723 process_bmbt_reclist(
724 xfs_mount_t *mp,
725 xfs_bmbt_rec_t *rp,
726 int *numrecs,
727 int type,
728 xfs_ino_t ino,
729 xfs_rfsblock_t *tot,
730 blkmap_t **blkmapp,
731 xfs_fileoff_t *first_key,
732 xfs_fileoff_t *last_key,
733 int whichfork)
734 {
735 return process_bmbt_reclist_int(mp, rp, numrecs, type, ino, tot,
736 blkmapp, first_key, last_key, 0, whichfork);
737 }
738
739 /*
740 * return 1 if inode should be cleared, 0 otherwise, does not set
741 * block bitmap
742 */
743 int
744 scan_bmbt_reclist(
745 xfs_mount_t *mp,
746 xfs_bmbt_rec_t *rp,
747 int *numrecs,
748 int type,
749 xfs_ino_t ino,
750 xfs_rfsblock_t *tot,
751 int whichfork)
752 {
753 xfs_fileoff_t first_key = 0;
754 xfs_fileoff_t last_key = 0;
755
756 return process_bmbt_reclist_int(mp, rp, numrecs, type, ino, tot,
757 NULL, &first_key, &last_key, 1, whichfork);
758 }
759
760 /*
761 * Grab the buffer backing an inode. This is meant for routines that
762 * work with inodes one at a time in any order (like walking the
763 * unlinked lists to look for inodes). The caller is responsible for
764 * writing/releasing the buffer.
765 */
766 struct xfs_buf *
767 get_agino_buf(
768 struct xfs_mount *mp,
769 xfs_agnumber_t agno,
770 xfs_agino_t agino,
771 struct xfs_dinode **dipp)
772 {
773 struct xfs_buf *bp;
774 int cluster_size;
775 int ino_per_cluster;
776 xfs_agino_t cluster_agino;
777 xfs_daddr_t cluster_daddr;
778 xfs_daddr_t cluster_blks;
779
780 /*
781 * Inode buffers have been read into memory in inode_cluster_size
782 * chunks (or one FSB). To find the correct buffer for an inode,
783 * we must find the buffer for its cluster, add the appropriate
784 * offset, and return that.
785 */
786 cluster_size = max(mp->m_inode_cluster_size, mp->m_sb.sb_blocksize);
787 ino_per_cluster = cluster_size / mp->m_sb.sb_inodesize;
788 cluster_agino = agino & ~(ino_per_cluster - 1);
789 cluster_blks = XFS_FSB_TO_DADDR(mp, max(1,
790 mp->m_inode_cluster_size >> mp->m_sb.sb_blocklog));
791 cluster_daddr = XFS_AGB_TO_DADDR(mp, agno,
792 XFS_AGINO_TO_AGBNO(mp, cluster_agino));
793
794 #ifdef XR_INODE_TRACE
795 printf("cluster_size %d ipc %d clusagino %d daddr %lld sectors %lld\n",
796 cluster_size, ino_per_cluster, cluster_agino, cluster_daddr,
797 cluster_blks);
798 #endif
799
800 bp = libxfs_readbuf(mp->m_dev, cluster_daddr, cluster_blks,
801 0, &xfs_inode_buf_ops);
802 if (!bp) {
803 do_warn(_("cannot read inode (%u/%u), disk block %" PRIu64 "\n"),
804 agno, cluster_agino, cluster_daddr);
805 return NULL;
806 }
807
808 *dipp = xfs_make_iptr(mp, bp, agino - cluster_agino);
809 ASSERT(!xfs_sb_version_hascrc(&mp->m_sb) ||
810 XFS_AGINO_TO_INO(mp, agno, agino) ==
811 be64_to_cpu((*dipp)->di_ino));
812 return bp;
813 }
814
815 /*
816 * higher level inode processing stuff starts here:
817 * first, one utility routine for each type of inode
818 */
819
820 /*
821 * return 1 if inode should be cleared, 0 otherwise
822 */
823 static int
824 process_btinode(
825 xfs_mount_t *mp,
826 xfs_agnumber_t agno,
827 xfs_agino_t ino,
828 xfs_dinode_t *dip,
829 int type,
830 int *dirty,
831 xfs_rfsblock_t *tot,
832 uint64_t *nex,
833 blkmap_t **blkmapp,
834 int whichfork,
835 int check_dups)
836 {
837 xfs_bmdr_block_t *dib;
838 xfs_fileoff_t last_key;
839 xfs_fileoff_t first_key = 0;
840 xfs_ino_t lino;
841 xfs_bmbt_ptr_t *pp;
842 xfs_bmbt_key_t *pkey;
843 char *forkname = get_forkname(whichfork);
844 int i;
845 int level;
846 int numrecs;
847 bmap_cursor_t cursor;
848 uint64_t magic;
849
850 dib = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
851 lino = XFS_AGINO_TO_INO(mp, agno, ino);
852 *tot = 0;
853 *nex = 0;
854
855 magic = xfs_sb_version_hascrc(&mp->m_sb) ? XFS_BMAP_CRC_MAGIC
856 : XFS_BMAP_MAGIC;
857
858 level = be16_to_cpu(dib->bb_level);
859 numrecs = be16_to_cpu(dib->bb_numrecs);
860
861 if ((level == 0) || (level > XFS_BM_MAXLEVELS(mp, whichfork))) {
862 /*
863 * XXX - if we were going to fix up the inode,
864 * we'd try to treat the fork as an interior
865 * node and see if we could get an accurate
866 * level value from one of the blocks pointed
867 * to by the pointers in the fork. For now
868 * though, we just bail (and blow out the inode).
869 */
870 do_warn(
871 _("bad level %d in inode %" PRIu64 " bmap btree root block\n"),
872 level, XFS_AGINO_TO_INO(mp, agno, ino));
873 return(1);
874 }
875 if (numrecs == 0) {
876 do_warn(
877 _("bad numrecs 0 in inode %" PRIu64 " bmap btree root block\n"),
878 XFS_AGINO_TO_INO(mp, agno, ino));
879 return(1);
880 }
881 /*
882 * use bmdr/dfork_dsize since the root block is in the data fork
883 */
884 if (XFS_BMDR_SPACE_CALC(numrecs) > XFS_DFORK_SIZE(dip, mp, whichfork)) {
885 do_warn(
886 _("indicated size of %s btree root (%d bytes) greater than space in "
887 "inode %" PRIu64 " %s fork\n"),
888 forkname, XFS_BMDR_SPACE_CALC(numrecs), lino, forkname);
889 return(1);
890 }
891
892 init_bm_cursor(&cursor, level + 1);
893
894 pp = XFS_BMDR_PTR_ADDR(dib, 1,
895 libxfs_bmdr_maxrecs(XFS_DFORK_SIZE(dip, mp, whichfork), 0));
896 pkey = XFS_BMDR_KEY_ADDR(dib, 1);
897 last_key = NULLFILEOFF;
898
899 for (i = 0; i < numrecs; i++) {
900 /*
901 * XXX - if we were going to do more to fix up the inode
902 * btree, we'd do it right here. For now, if there's a
903 * problem, we'll bail out and presumably clear the inode.
904 */
905 if (!verify_dfsbno(mp, get_unaligned_be64(&pp[i]))) {
906 do_warn(
907 _("bad bmap btree ptr 0x%" PRIx64 " in ino %" PRIu64 "\n"),
908 get_unaligned_be64(&pp[i]), lino);
909 return(1);
910 }
911
912 if (scan_lbtree(get_unaligned_be64(&pp[i]), level, scan_bmapbt,
913 type, whichfork, lino, tot, nex, blkmapp,
914 &cursor, 1, check_dups, magic,
915 &xfs_bmbt_buf_ops))
916 return(1);
917 /*
918 * fix key (offset) mismatches between the keys in root
919 * block records and the first key of each child block.
920 * fixes cases where entries have been shifted between
921 * blocks but the parent hasn't been updated
922 */
923 if (!check_dups && cursor.level[level-1].first_key !=
924 get_unaligned_be64(&pkey[i].br_startoff)) {
925 if (!no_modify) {
926 do_warn(
927 _("correcting key in bmbt root (was %" PRIu64 ", now %" PRIu64") in inode "
928 "%" PRIu64" %s fork\n"),
929 get_unaligned_be64(&pkey[i].br_startoff),
930 cursor.level[level-1].first_key,
931 XFS_AGINO_TO_INO(mp, agno, ino),
932 forkname);
933 *dirty = 1;
934 put_unaligned_be64(
935 cursor.level[level-1].first_key,
936 &pkey[i].br_startoff);
937 } else {
938 do_warn(
939 _("bad key in bmbt root (is %" PRIu64 ", would reset to %" PRIu64 ") in inode "
940 "%" PRIu64 " %s fork\n"),
941 get_unaligned_be64(&pkey[i].br_startoff),
942 cursor.level[level-1].first_key,
943 XFS_AGINO_TO_INO(mp, agno, ino),
944 forkname);
945 }
946 }
947 /*
948 * make sure that keys are in ascending order. blow out
949 * inode if the ordering doesn't hold
950 */
951 if (check_dups == 0) {
952 if (last_key != NULLFILEOFF && last_key >=
953 cursor.level[level-1].first_key) {
954 do_warn(
955 _("out of order bmbt root key %" PRIu64 " in inode %" PRIu64 " %s fork\n"),
956 first_key,
957 XFS_AGINO_TO_INO(mp, agno, ino),
958 forkname);
959 return(1);
960 }
961 last_key = cursor.level[level-1].first_key;
962 }
963 }
964 /*
965 * Ideally if all the extents are ok (perhaps after further
966 * checks below?) we'd just move this back into extents format.
967 * But for now clear it, as the kernel will choke on this
968 */
969 if (*nex <= XFS_DFORK_SIZE(dip, mp, whichfork) /
970 sizeof(xfs_bmbt_rec_t)) {
971 do_warn(
972 _("extent count for ino %" PRIu64 " %s fork too low (%" PRIu64 ") for file format\n"),
973 lino, forkname, *nex);
974 return(1);
975 }
976 /*
977 * Check that the last child block's forward sibling pointer
978 * is NULL.
979 */
980 if (check_dups == 0 &&
981 cursor.level[0].right_fsbno != NULLFSBLOCK) {
982 do_warn(
983 _("bad fwd (right) sibling pointer (saw %" PRIu64 " should be NULLFSBLOCK)\n"),
984 cursor.level[0].right_fsbno);
985 do_warn(
986 _("\tin inode %" PRIu64 " (%s fork) bmap btree block %" PRIu64 "\n"),
987 XFS_AGINO_TO_INO(mp, agno, ino), forkname,
988 cursor.level[0].fsbno);
989 return(1);
990 }
991
992 return(0);
993 }
994
995 /*
996 * return 1 if inode should be cleared, 0 otherwise
997 */
998 static int
999 process_exinode(
1000 xfs_mount_t *mp,
1001 xfs_agnumber_t agno,
1002 xfs_agino_t ino,
1003 xfs_dinode_t *dip,
1004 int type,
1005 int *dirty,
1006 xfs_rfsblock_t *tot,
1007 uint64_t *nex,
1008 blkmap_t **blkmapp,
1009 int whichfork,
1010 int check_dups)
1011 {
1012 xfs_ino_t lino;
1013 xfs_bmbt_rec_t *rp;
1014 xfs_fileoff_t first_key;
1015 xfs_fileoff_t last_key;
1016 int32_t numrecs;
1017 int ret;
1018
1019 lino = XFS_AGINO_TO_INO(mp, agno, ino);
1020 rp = (xfs_bmbt_rec_t *)XFS_DFORK_PTR(dip, whichfork);
1021 *tot = 0;
1022 numrecs = XFS_DFORK_NEXTENTS(dip, whichfork);
1023
1024 /*
1025 * We've already decided on the maximum number of extents on the inode,
1026 * and numrecs may be corrupt. Hence make sure we only allow numrecs to
1027 * be in the range of valid on-disk numbers, which is:
1028 * 0 < numrecs < 2^31 - 1
1029 */
1030 if (numrecs < 0)
1031 numrecs = *nex;
1032
1033 /*
1034 * XXX - if we were going to fix up the btree record,
1035 * we'd do it right here. For now, if there's a problem,
1036 * we'll bail out and presumably clear the inode.
1037 */
1038 if (check_dups == 0)
1039 ret = process_bmbt_reclist(mp, rp, &numrecs, type, lino,
1040 tot, blkmapp, &first_key, &last_key,
1041 whichfork);
1042 else
1043 ret = scan_bmbt_reclist(mp, rp, &numrecs, type, lino, tot,
1044 whichfork);
1045
1046 *nex = numrecs;
1047 return ret;
1048 }
1049
1050 /*
1051 * return 1 if inode should be cleared, 0 otherwise
1052 */
1053 static int
1054 process_lclinode(
1055 xfs_mount_t *mp,
1056 xfs_agnumber_t agno,
1057 xfs_agino_t ino,
1058 xfs_dinode_t *dip,
1059 int whichfork)
1060 {
1061 xfs_attr_shortform_t *asf;
1062 xfs_ino_t lino;
1063
1064 lino = XFS_AGINO_TO_INO(mp, agno, ino);
1065 if (whichfork == XFS_DATA_FORK && be64_to_cpu(dip->di_size) >
1066 XFS_DFORK_DSIZE(dip, mp)) {
1067 do_warn(
1068 _("local inode %" PRIu64 " data fork is too large (size = %lld, max = %d)\n"),
1069 lino, (unsigned long long) be64_to_cpu(dip->di_size),
1070 XFS_DFORK_DSIZE(dip, mp));
1071 return(1);
1072 } else if (whichfork == XFS_ATTR_FORK) {
1073 asf = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
1074 if (be16_to_cpu(asf->hdr.totsize) > XFS_DFORK_ASIZE(dip, mp)) {
1075 do_warn(
1076 _("local inode %" PRIu64 " attr fork too large (size %d, max = %d)\n"),
1077 lino, be16_to_cpu(asf->hdr.totsize),
1078 XFS_DFORK_ASIZE(dip, mp));
1079 return(1);
1080 }
1081 if (be16_to_cpu(asf->hdr.totsize) < sizeof(xfs_attr_sf_hdr_t)) {
1082 do_warn(
1083 _("local inode %" PRIu64 " attr too small (size = %d, min size = %zd)\n"),
1084 lino, be16_to_cpu(asf->hdr.totsize),
1085 sizeof(xfs_attr_sf_hdr_t));
1086 return(1);
1087 }
1088 }
1089
1090 return(0);
1091 }
1092
1093 static int
1094 process_symlink_extlist(xfs_mount_t *mp, xfs_ino_t lino, xfs_dinode_t *dino)
1095 {
1096 xfs_fileoff_t expected_offset;
1097 xfs_bmbt_rec_t *rp;
1098 xfs_bmbt_irec_t irec;
1099 int numrecs;
1100 int i;
1101 int max_blocks;
1102
1103 if (be64_to_cpu(dino->di_size) <= XFS_DFORK_DSIZE(dino, mp)) {
1104 if (dino->di_format == XFS_DINODE_FMT_LOCAL)
1105 return 0;
1106 do_warn(
1107 _("mismatch between format (%d) and size (%" PRId64 ") in symlink ino %" PRIu64 "\n"),
1108 dino->di_format,
1109 (int64_t)be64_to_cpu(dino->di_size), lino);
1110 return 1;
1111 }
1112 if (dino->di_format == XFS_DINODE_FMT_LOCAL) {
1113 do_warn(
1114 _("mismatch between format (%d) and size (%" PRId64 ") in symlink inode %" PRIu64 "\n"),
1115 dino->di_format,
1116 (int64_t)be64_to_cpu(dino->di_size), lino);
1117 return 1;
1118 }
1119
1120 rp = (xfs_bmbt_rec_t *)XFS_DFORK_DPTR(dino);
1121 numrecs = be32_to_cpu(dino->di_nextents);
1122
1123 /*
1124 * the max # of extents in a symlink inode is equal to the
1125 * number of max # of blocks required to store the symlink
1126 */
1127 if (numrecs > max_symlink_blocks) {
1128 do_warn(
1129 _("bad number of extents (%d) in symlink %" PRIu64 " data fork\n"),
1130 numrecs, lino);
1131 return(1);
1132 }
1133
1134 max_blocks = max_symlink_blocks;
1135 expected_offset = 0;
1136
1137 for (i = 0; i < numrecs; i++) {
1138 libxfs_bmbt_disk_get_all((rp +i), &irec);
1139 if (irec.br_startoff != expected_offset) {
1140 do_warn(
1141 _("bad extent #%d offset (%" PRIu64 ") in symlink %" PRIu64 " data fork\n"),
1142 i, irec.br_startoff, lino);
1143 return(1);
1144 }
1145 if (irec.br_blockcount == 0 || irec.br_blockcount > max_blocks) {
1146 do_warn(
1147 _("bad extent #%d count (%" PRIu64 ") in symlink %" PRIu64 " data fork\n"),
1148 i, irec.br_blockcount, lino);
1149 return(1);
1150 }
1151
1152 max_blocks -= irec.br_blockcount;
1153 expected_offset += irec.br_blockcount;
1154 }
1155
1156 return(0);
1157 }
1158
1159 /*
1160 * takes a name and length and returns 1 if the name contains
1161 * a \0, returns 0 otherwise
1162 */
1163 static int
1164 null_check(char *name, int length)
1165 {
1166 int i;
1167
1168 ASSERT(length < XFS_SYMLINK_MAXLEN);
1169
1170 for (i = 0; i < length; i++, name++) {
1171 if (*name == '\0')
1172 return(1);
1173 }
1174
1175 return(0);
1176 }
1177
1178 /*
1179 * This does /not/ do quotacheck, it validates the basic quota
1180 * inode metadata, checksums, etc.
1181 */
1182 #define uuid_equal(s,d) (platform_uuid_compare((s),(d)) == 0)
1183 static int
1184 process_quota_inode(
1185 struct xfs_mount *mp,
1186 xfs_ino_t lino,
1187 struct xfs_dinode *dino,
1188 uint ino_type,
1189 struct blkmap *blkmap)
1190 {
1191 xfs_fsblock_t fsbno;
1192 struct xfs_buf *bp;
1193 xfs_filblks_t dqchunklen;
1194 uint dqperchunk;
1195 int quota_type;
1196 char *quota_string;
1197 xfs_dqid_t dqid;
1198 xfs_fileoff_t qbno;
1199 int i;
1200 int t = 0;
1201
1202 switch (ino_type) {
1203 case XR_INO_UQUOTA:
1204 quota_type = XFS_DQ_USER;
1205 quota_string = _("User quota");
1206 break;
1207 case XR_INO_GQUOTA:
1208 quota_type = XFS_DQ_GROUP;
1209 quota_string = _("Group quota");
1210 break;
1211 case XR_INO_PQUOTA:
1212 quota_type = XFS_DQ_PROJ;
1213 quota_string = _("Project quota");
1214 break;
1215 default:
1216 ASSERT(0);
1217 }
1218
1219 dqchunklen = XFS_FSB_TO_BB(mp, XFS_DQUOT_CLUSTER_SIZE_FSB);
1220 dqperchunk = libxfs_calc_dquots_per_chunk(dqchunklen);
1221 dqid = 0;
1222 qbno = NULLFILEOFF;
1223
1224 while ((qbno = blkmap_next_off(blkmap, qbno, &t)) != NULLFILEOFF) {
1225 xfs_dqblk_t *dqb;
1226 int writebuf = 0;
1227
1228 fsbno = blkmap_get(blkmap, qbno);
1229 dqid = (xfs_dqid_t)qbno * dqperchunk;
1230
1231 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, fsbno),
1232 dqchunklen, 0, &xfs_dquot_buf_ops);
1233 if (!bp) {
1234 do_warn(
1235 _("cannot read inode %" PRIu64 ", file block %" PRIu64 ", disk block %" PRIu64 "\n"),
1236 lino, qbno, fsbno);
1237 return 1;
1238 }
1239
1240 dqb = bp->b_addr;
1241 for (i = 0; i < dqperchunk; i++, dqid++, dqb++) {
1242 int bad_dqb = 0;
1243
1244 /* We only print the first problem we find */
1245 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1246 if (!libxfs_verify_cksum((char *)dqb,
1247 sizeof(*dqb),
1248 XFS_DQUOT_CRC_OFF)) {
1249 do_warn(_("%s: bad CRC for id %u. "),
1250 quota_string, dqid);
1251 bad_dqb = 1;
1252 goto bad;
1253 }
1254
1255 if (!uuid_equal(&dqb->dd_uuid,
1256 &mp->m_sb.sb_meta_uuid)) {
1257 do_warn(_("%s: bad UUID for id %u. "),
1258 quota_string, dqid);
1259 bad_dqb = 1;
1260 goto bad;
1261 }
1262 }
1263 if (libxfs_dquot_verify(mp, &dqb->dd_diskdq, dqid,
1264 quota_type) != NULL) {
1265 do_warn(_("%s: Corrupt quota for id %u. "),
1266 quota_string, dqid);
1267 bad_dqb = 1;
1268 }
1269
1270 bad:
1271 if (bad_dqb) {
1272 if (no_modify)
1273 do_warn(_("Would correct.\n"));
1274 else {
1275 do_warn(_("Corrected.\n"));
1276 libxfs_dqblk_repair(mp, dqb,
1277 dqid, quota_type);
1278 writebuf = 1;
1279 }
1280 }
1281 }
1282
1283 if (writebuf && !no_modify)
1284 libxfs_writebuf(bp, 0);
1285 else
1286 libxfs_putbuf(bp);
1287 }
1288 return 0;
1289 }
1290
1291 static int
1292 process_symlink_remote(
1293 struct xfs_mount *mp,
1294 xfs_ino_t lino,
1295 struct xfs_dinode *dino,
1296 struct blkmap *blkmap,
1297 char *dst)
1298 {
1299 xfs_fsblock_t fsbno;
1300 struct xfs_buf *bp;
1301 char *src;
1302 int pathlen;
1303 int offset;
1304 int i;
1305
1306 offset = 0;
1307 pathlen = be64_to_cpu(dino->di_size);
1308 i = 0;
1309
1310 while (pathlen > 0) {
1311 int blk_cnt = 1;
1312 int byte_cnt;
1313 int badcrc = 0;
1314
1315 fsbno = blkmap_get(blkmap, i);
1316 if (fsbno == NULLFSBLOCK) {
1317 do_warn(
1318 _("cannot read inode %" PRIu64 ", file block %d, NULL disk block\n"),
1319 lino, i);
1320 return 1;
1321 }
1322
1323 /*
1324 * There's a symlink header for each contiguous extent. If
1325 * there are contiguous blocks, read them in one go.
1326 */
1327 while (blk_cnt <= max_symlink_blocks) {
1328 if (blkmap_get(blkmap, i + 1) != fsbno + 1)
1329 break;
1330 blk_cnt++;
1331 i++;
1332 }
1333
1334 byte_cnt = XFS_FSB_TO_B(mp, blk_cnt);
1335
1336 bp = libxfs_readbuf(mp->m_dev, XFS_FSB_TO_DADDR(mp, fsbno),
1337 BTOBB(byte_cnt), 0, &xfs_symlink_buf_ops);
1338 if (!bp) {
1339 do_warn(
1340 _("cannot read inode %" PRIu64 ", file block %d, disk block %" PRIu64 "\n"),
1341 lino, i, fsbno);
1342 return 1;
1343 }
1344 if (bp->b_error == -EFSCORRUPTED) {
1345 do_warn(
1346 _("Corrupt symlink remote block %" PRIu64 ", inode %" PRIu64 ".\n"),
1347 fsbno, lino);
1348 libxfs_putbuf(bp);
1349 return 1;
1350 }
1351 if (bp->b_error == -EFSBADCRC) {
1352 do_warn(
1353 _("Bad symlink buffer CRC, block %" PRIu64 ", inode %" PRIu64 ".\n"
1354 "Correcting CRC, but symlink may be bad.\n"), fsbno, lino);
1355 badcrc = 1;
1356 }
1357
1358 byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
1359 byte_cnt = min(pathlen, byte_cnt);
1360
1361 src = bp->b_addr;
1362 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1363 if (!libxfs_symlink_hdr_ok(lino, offset,
1364 byte_cnt, bp)) {
1365 do_warn(
1366 _("bad symlink header ino %" PRIu64 ", file block %d, disk block %" PRIu64 "\n"),
1367 lino, i, fsbno);
1368 libxfs_putbuf(bp);
1369 return 1;
1370 }
1371 src += sizeof(struct xfs_dsymlink_hdr);
1372 }
1373
1374 memmove(dst + offset, src, byte_cnt);
1375
1376 pathlen -= byte_cnt;
1377 offset += byte_cnt;
1378 i++;
1379
1380 if (badcrc && !no_modify)
1381 libxfs_writebuf(bp, 0);
1382 else
1383 libxfs_putbuf(bp);
1384 }
1385 return 0;
1386 }
1387
1388 /*
1389 * like usual, returns 0 if everything's ok and 1 if something's
1390 * bogus
1391 */
1392 static int
1393 process_symlink(
1394 xfs_mount_t *mp,
1395 xfs_ino_t lino,
1396 xfs_dinode_t *dino,
1397 blkmap_t *blkmap)
1398 {
1399 char *symlink;
1400 char data[XFS_SYMLINK_MAXLEN];
1401
1402 /*
1403 * check size against kernel symlink limits. we know
1404 * size is consistent with inode storage format -- e.g.
1405 * the inode is structurally ok so we don't have to check
1406 * for that
1407 */
1408 if (be64_to_cpu(dino->di_size) >= XFS_SYMLINK_MAXLEN) {
1409 do_warn(_("symlink in inode %" PRIu64 " too long (%llu chars)\n"),
1410 lino, (unsigned long long) be64_to_cpu(dino->di_size));
1411 return(1);
1412 }
1413
1414 if (be64_to_cpu(dino->di_size) == 0) {
1415 do_warn(_("zero size symlink in inode %" PRIu64 "\n"), lino);
1416 return 1;
1417 }
1418
1419 /*
1420 * have to check symlink component by component.
1421 * get symlink contents into data area
1422 */
1423 symlink = &data[0];
1424 if (be64_to_cpu(dino->di_size) <= XFS_DFORK_DSIZE(dino, mp)) {
1425 /*
1426 * local symlink, just copy the symlink out of the
1427 * inode into the data area
1428 */
1429 memmove(symlink, XFS_DFORK_DPTR(dino),
1430 be64_to_cpu(dino->di_size));
1431 } else {
1432 int error;
1433
1434 error = process_symlink_remote(mp, lino, dino, blkmap, symlink);
1435 if (error)
1436 return error;
1437 }
1438
1439 data[be64_to_cpu(dino->di_size)] = '\0';
1440
1441 /*
1442 * check for nulls
1443 */
1444 if (null_check(symlink, be64_to_cpu(dino->di_size))) {
1445 do_warn(
1446 _("found illegal null character in symlink inode %" PRIu64 "\n"),
1447 lino);
1448 return(1);
1449 }
1450
1451 return(0);
1452 }
1453
1454 /*
1455 * called to process the set of misc inode special inode types
1456 * that have no associated data storage (fifos, pipes, devices, etc.).
1457 */
1458 static int
1459 process_misc_ino_types(xfs_mount_t *mp,
1460 xfs_dinode_t *dino,
1461 xfs_ino_t lino,
1462 int type)
1463 {
1464 /*
1465 * disallow mountpoint inodes until such time as the
1466 * kernel actually allows them to be created (will
1467 * probably require a superblock version rev, sigh).
1468 */
1469 if (type == XR_INO_MOUNTPOINT) {
1470 do_warn(
1471 _("inode %" PRIu64 " has bad inode type (IFMNT)\n"), lino);
1472 return(1);
1473 }
1474
1475 /*
1476 * must also have a zero size
1477 */
1478 if (be64_to_cpu(dino->di_size) != 0) {
1479 switch (type) {
1480 case XR_INO_CHRDEV:
1481 do_warn(
1482 _("size of character device inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1483 (int64_t)be64_to_cpu(dino->di_size));
1484 break;
1485 case XR_INO_BLKDEV:
1486 do_warn(
1487 _("size of block device inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1488 (int64_t)be64_to_cpu(dino->di_size));
1489 break;
1490 case XR_INO_SOCK:
1491 do_warn(
1492 _("size of socket inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1493 (int64_t)be64_to_cpu(dino->di_size));
1494 break;
1495 case XR_INO_FIFO:
1496 do_warn(
1497 _("size of fifo inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1498 (int64_t)be64_to_cpu(dino->di_size));
1499 break;
1500 case XR_INO_UQUOTA:
1501 case XR_INO_GQUOTA:
1502 case XR_INO_PQUOTA:
1503 do_warn(
1504 _("size of quota inode %" PRIu64 " != 0 (%" PRId64 " bytes)\n"), lino,
1505 (int64_t)be64_to_cpu(dino->di_size));
1506 break;
1507 default:
1508 do_warn(_("Internal error - process_misc_ino_types, "
1509 "illegal type %d\n"), type);
1510 abort();
1511 }
1512
1513 return(1);
1514 }
1515
1516 return(0);
1517 }
1518
1519 static int
1520 process_misc_ino_types_blocks(xfs_rfsblock_t totblocks, xfs_ino_t lino, int type)
1521 {
1522 /*
1523 * you can not enforce all misc types have zero data fork blocks
1524 * by checking dino->di_nblocks because atotblocks (attribute
1525 * blocks) are part of nblocks. We must check this later when atotblocks
1526 * has been calculated or by doing a simple check that anExtents == 0.
1527 * We must also guarantee that totblocks is 0. Thus nblocks checking
1528 * will be done later in process_dinode_int for misc types.
1529 */
1530
1531 if (totblocks != 0) {
1532 switch (type) {
1533 case XR_INO_CHRDEV:
1534 do_warn(
1535 _("size of character device inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1536 lino, totblocks);
1537 break;
1538 case XR_INO_BLKDEV:
1539 do_warn(
1540 _("size of block device inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1541 lino, totblocks);
1542 break;
1543 case XR_INO_SOCK:
1544 do_warn(
1545 _("size of socket inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1546 lino, totblocks);
1547 break;
1548 case XR_INO_FIFO:
1549 do_warn(
1550 _("size of fifo inode %" PRIu64 " != 0 (%" PRIu64 " blocks)\n"),
1551 lino, totblocks);
1552 break;
1553 default:
1554 return(0);
1555 }
1556 return(1);
1557 }
1558 return (0);
1559 }
1560
1561 static inline int
1562 dinode_fmt(
1563 xfs_dinode_t *dino)
1564 {
1565 return be16_to_cpu(dino->di_mode) & S_IFMT;
1566 }
1567
1568 static inline void
1569 change_dinode_fmt(
1570 xfs_dinode_t *dino,
1571 int new_fmt)
1572 {
1573 int mode = be16_to_cpu(dino->di_mode);
1574
1575 ASSERT((new_fmt & ~S_IFMT) == 0);
1576
1577 mode &= ~S_IFMT;
1578 mode |= new_fmt;
1579 dino->di_mode = cpu_to_be16(mode);
1580 }
1581
1582 static int
1583 check_dinode_mode_format(
1584 xfs_dinode_t *dinoc)
1585 {
1586 if (dinoc->di_format >= XFS_DINODE_FMT_UUID)
1587 return -1; /* FMT_UUID is not used */
1588
1589 switch (dinode_fmt(dinoc)) {
1590 case S_IFIFO:
1591 case S_IFCHR:
1592 case S_IFBLK:
1593 case S_IFSOCK:
1594 return (dinoc->di_format != XFS_DINODE_FMT_DEV) ? -1 : 0;
1595
1596 case S_IFDIR:
1597 return (dinoc->di_format < XFS_DINODE_FMT_LOCAL ||
1598 dinoc->di_format > XFS_DINODE_FMT_BTREE) ? -1 : 0;
1599
1600 case S_IFREG:
1601 return (dinoc->di_format < XFS_DINODE_FMT_EXTENTS ||
1602 dinoc->di_format > XFS_DINODE_FMT_BTREE) ? -1 : 0;
1603
1604 case S_IFLNK:
1605 return (dinoc->di_format < XFS_DINODE_FMT_LOCAL ||
1606 dinoc->di_format > XFS_DINODE_FMT_EXTENTS) ? -1 : 0;
1607
1608 default: ;
1609 }
1610 return 0; /* invalid modes are checked elsewhere */
1611 }
1612
1613 /*
1614 * If inode is a superblock inode, does type check to make sure is it valid.
1615 * Returns 0 if it's valid, non-zero if it needs to be cleared.
1616 */
1617
1618 static int
1619 process_check_sb_inodes(
1620 xfs_mount_t *mp,
1621 xfs_dinode_t *dinoc,
1622 xfs_ino_t lino,
1623 int *type,
1624 int *dirty)
1625 {
1626 if (lino == mp->m_sb.sb_rootino) {
1627 if (*type != XR_INO_DIR) {
1628 do_warn(_("root inode %" PRIu64 " has bad type 0x%x\n"),
1629 lino, dinode_fmt(dinoc));
1630 *type = XR_INO_DIR;
1631 if (!no_modify) {
1632 do_warn(_("resetting to directory\n"));
1633 change_dinode_fmt(dinoc, S_IFDIR);
1634 *dirty = 1;
1635 } else
1636 do_warn(_("would reset to directory\n"));
1637 }
1638 return 0;
1639 }
1640 if (lino == mp->m_sb.sb_uquotino) {
1641 if (*type != XR_INO_UQUOTA) {
1642 do_warn(_("user quota inode %" PRIu64 " has bad type 0x%x\n"),
1643 lino, dinode_fmt(dinoc));
1644 mp->m_sb.sb_uquotino = NULLFSINO;
1645 return 1;
1646 }
1647 return 0;
1648 }
1649 if (lino == mp->m_sb.sb_gquotino) {
1650 if (*type != XR_INO_GQUOTA) {
1651 do_warn(_("group quota inode %" PRIu64 " has bad type 0x%x\n"),
1652 lino, dinode_fmt(dinoc));
1653 mp->m_sb.sb_gquotino = NULLFSINO;
1654 return 1;
1655 }
1656 return 0;
1657 }
1658 if (lino == mp->m_sb.sb_pquotino) {
1659 if (*type != XR_INO_PQUOTA) {
1660 do_warn(_("project quota inode %" PRIu64 " has bad type 0x%x\n"),
1661 lino, dinode_fmt(dinoc));
1662 mp->m_sb.sb_pquotino = NULLFSINO;
1663 return 1;
1664 }
1665 return 0;
1666 }
1667 if (lino == mp->m_sb.sb_rsumino) {
1668 if (*type != XR_INO_RTSUM) {
1669 do_warn(
1670 _("realtime summary inode %" PRIu64 " has bad type 0x%x, "),
1671 lino, dinode_fmt(dinoc));
1672 if (!no_modify) {
1673 do_warn(_("resetting to regular file\n"));
1674 change_dinode_fmt(dinoc, S_IFREG);
1675 *dirty = 1;
1676 } else {
1677 do_warn(_("would reset to regular file\n"));
1678 }
1679 }
1680 if (mp->m_sb.sb_rblocks == 0 && dinoc->di_nextents != 0) {
1681 do_warn(
1682 _("bad # of extents (%u) for realtime summary inode %" PRIu64 "\n"),
1683 be32_to_cpu(dinoc->di_nextents), lino);
1684 return 1;
1685 }
1686 return 0;
1687 }
1688 if (lino == mp->m_sb.sb_rbmino) {
1689 if (*type != XR_INO_RTBITMAP) {
1690 do_warn(
1691 _("realtime bitmap inode %" PRIu64 " has bad type 0x%x, "),
1692 lino, dinode_fmt(dinoc));
1693 if (!no_modify) {
1694 do_warn(_("resetting to regular file\n"));
1695 change_dinode_fmt(dinoc, S_IFREG);
1696 *dirty = 1;
1697 } else {
1698 do_warn(_("would reset to regular file\n"));
1699 }
1700 }
1701 if (mp->m_sb.sb_rblocks == 0 && dinoc->di_nextents != 0) {
1702 do_warn(
1703 _("bad # of extents (%u) for realtime bitmap inode %" PRIu64 "\n"),
1704 be32_to_cpu(dinoc->di_nextents), lino);
1705 return 1;
1706 }
1707 return 0;
1708 }
1709 return 0;
1710 }
1711
1712 /*
1713 * general size/consistency checks:
1714 *
1715 * if the size <= size of the data fork, directories must be
1716 * local inodes unlike regular files which would be extent inodes.
1717 * all the other mentioned types have to have a zero size value.
1718 *
1719 * if the size and format don't match, get out now rather than
1720 * risk trying to process a non-existent extents or btree
1721 * type data fork.
1722 */
1723 static int
1724 process_check_inode_sizes(
1725 xfs_mount_t *mp,
1726 xfs_dinode_t *dino,
1727 xfs_ino_t lino,
1728 int type)
1729 {
1730 xfs_fsize_t size = be64_to_cpu(dino->di_size);
1731
1732 switch (type) {
1733
1734 case XR_INO_DIR:
1735 if (size <= XFS_DFORK_DSIZE(dino, mp) &&
1736 dino->di_format != XFS_DINODE_FMT_LOCAL) {
1737 do_warn(
1738 _("mismatch between format (%d) and size (%" PRId64 ") in directory ino %" PRIu64 "\n"),
1739 dino->di_format, size, lino);
1740 return 1;
1741 }
1742 if (size > XFS_DIR2_LEAF_OFFSET) {
1743 do_warn(
1744 _("directory inode %" PRIu64 " has bad size %" PRId64 "\n"),
1745 lino, size);
1746 return 1;
1747 }
1748 break;
1749
1750 case XR_INO_SYMLINK:
1751 if (process_symlink_extlist(mp, lino, dino)) {
1752 do_warn(_("bad data fork in symlink %" PRIu64 "\n"), lino);
1753 return 1;
1754 }
1755 break;
1756
1757 case XR_INO_CHRDEV: /* fall through to FIFO case ... */
1758 case XR_INO_BLKDEV: /* fall through to FIFO case ... */
1759 case XR_INO_SOCK: /* fall through to FIFO case ... */
1760 case XR_INO_MOUNTPOINT: /* fall through to FIFO case ... */
1761 case XR_INO_FIFO:
1762 if (process_misc_ino_types(mp, dino, lino, type))
1763 return 1;
1764 break;
1765
1766 case XR_INO_UQUOTA:
1767 case XR_INO_GQUOTA:
1768 case XR_INO_PQUOTA:
1769 /* Quota inodes have same restrictions as above types */
1770 if (process_misc_ino_types(mp, dino, lino, type))
1771 return 1;
1772 break;
1773
1774 case XR_INO_RTDATA:
1775 /*
1776 * if we have no realtime blocks, any inode claiming
1777 * to be a real-time file is bogus
1778 */
1779 if (mp->m_sb.sb_rblocks == 0) {
1780 do_warn(
1781 _("found inode %" PRIu64 " claiming to be a real-time file\n"), lino);
1782 return 1;
1783 }
1784 break;
1785
1786 case XR_INO_RTBITMAP:
1787 if (size != (int64_t)mp->m_sb.sb_rbmblocks *
1788 mp->m_sb.sb_blocksize) {
1789 do_warn(
1790 _("realtime bitmap inode %" PRIu64 " has bad size %" PRId64 " (should be %" PRIu64 ")\n"),
1791 lino, size,
1792 (int64_t) mp->m_sb.sb_rbmblocks *
1793 mp->m_sb.sb_blocksize);
1794 return 1;
1795 }
1796 break;
1797
1798 case XR_INO_RTSUM:
1799 if (size != mp->m_rsumsize) {
1800 do_warn(
1801 _("realtime summary inode %" PRIu64 " has bad size %" PRId64 " (should be %d)\n"),
1802 lino, size, mp->m_rsumsize);
1803 return 1;
1804 }
1805 break;
1806
1807 default:
1808 break;
1809 }
1810 return 0;
1811 }
1812
1813 /*
1814 * check for illegal values of forkoff
1815 */
1816 static int
1817 process_check_inode_forkoff(
1818 xfs_mount_t *mp,
1819 xfs_dinode_t *dino,
1820 xfs_ino_t lino)
1821 {
1822 if (dino->di_forkoff == 0)
1823 return 0;
1824
1825 switch (dino->di_format) {
1826 case XFS_DINODE_FMT_DEV:
1827 if (dino->di_forkoff != (roundup(sizeof(xfs_dev_t), 8) >> 3)) {
1828 do_warn(
1829 _("bad attr fork offset %d in dev inode %" PRIu64 ", should be %d\n"),
1830 dino->di_forkoff, lino,
1831 (int)(roundup(sizeof(xfs_dev_t), 8) >> 3));
1832 return 1;
1833 }
1834 break;
1835 case XFS_DINODE_FMT_LOCAL: /* fall through ... */
1836 case XFS_DINODE_FMT_EXTENTS: /* fall through ... */
1837 case XFS_DINODE_FMT_BTREE:
1838 if (dino->di_forkoff >=
1839 (XFS_LITINO(mp, dino->di_version) >> 3)) {
1840 do_warn(
1841 _("bad attr fork offset %d in inode %" PRIu64 ", max=%d\n"),
1842 dino->di_forkoff, lino,
1843 XFS_LITINO(mp, dino->di_version) >> 3);
1844 return 1;
1845 }
1846 break;
1847 default:
1848 do_error(_("unexpected inode format %d\n"), dino->di_format);
1849 break;
1850 }
1851 return 0;
1852 }
1853
1854 /*
1855 * Updates the inodes block and extent counts if they are wrong
1856 */
1857 static int
1858 process_inode_blocks_and_extents(
1859 xfs_dinode_t *dino,
1860 xfs_rfsblock_t nblocks,
1861 uint64_t nextents,
1862 uint64_t anextents,
1863 xfs_ino_t lino,
1864 int *dirty)
1865 {
1866 if (nblocks != be64_to_cpu(dino->di_nblocks)) {
1867 if (!no_modify) {
1868 do_warn(
1869 _("correcting nblocks for inode %" PRIu64 ", was %llu - counted %" PRIu64 "\n"), lino,
1870 (unsigned long long) be64_to_cpu(dino->di_nblocks),
1871 nblocks);
1872 dino->di_nblocks = cpu_to_be64(nblocks);
1873 *dirty = 1;
1874 } else {
1875 do_warn(
1876 _("bad nblocks %llu for inode %" PRIu64 ", would reset to %" PRIu64 "\n"),
1877 (unsigned long long) be64_to_cpu(dino->di_nblocks),
1878 lino, nblocks);
1879 }
1880 }
1881
1882 if (nextents > MAXEXTNUM) {
1883 do_warn(
1884 _("too many data fork extents (%" PRIu64 ") in inode %" PRIu64 "\n"),
1885 nextents, lino);
1886 return 1;
1887 }
1888 if (nextents != be32_to_cpu(dino->di_nextents)) {
1889 if (!no_modify) {
1890 do_warn(
1891 _("correcting nextents for inode %" PRIu64 ", was %d - counted %" PRIu64 "\n"),
1892 lino,
1893 be32_to_cpu(dino->di_nextents),
1894 nextents);
1895 dino->di_nextents = cpu_to_be32(nextents);
1896 *dirty = 1;
1897 } else {
1898 do_warn(
1899 _("bad nextents %d for inode %" PRIu64 ", would reset to %" PRIu64 "\n"),
1900 be32_to_cpu(dino->di_nextents),
1901 lino, nextents);
1902 }
1903 }
1904
1905 if (anextents > MAXAEXTNUM) {
1906 do_warn(
1907 _("too many attr fork extents (%" PRIu64 ") in inode %" PRIu64 "\n"),
1908 anextents, lino);
1909 return 1;
1910 }
1911 if (anextents != be16_to_cpu(dino->di_anextents)) {
1912 if (!no_modify) {
1913 do_warn(
1914 _("correcting anextents for inode %" PRIu64 ", was %d - counted %" PRIu64 "\n"),
1915 lino,
1916 be16_to_cpu(dino->di_anextents), anextents);
1917 dino->di_anextents = cpu_to_be16(anextents);
1918 *dirty = 1;
1919 } else {
1920 do_warn(
1921 _("bad anextents %d for inode %" PRIu64 ", would reset to %" PRIu64 "\n"),
1922 be16_to_cpu(dino->di_anextents),
1923 lino, anextents);
1924 }
1925 }
1926
1927 /*
1928 * We are comparing different units here, but that's fine given that
1929 * an extent has to have at least a block in it.
1930 */
1931 if (nblocks < nextents + anextents) {
1932 do_warn(
1933 _("nblocks (%" PRIu64 ") smaller than nextents for inode %" PRIu64 "\n"), nblocks, lino);
1934 return 1;
1935 }
1936
1937 return 0;
1938 }
1939
1940 /*
1941 * check data fork -- if it's bad, clear the inode
1942 */
1943 static int
1944 process_inode_data_fork(
1945 xfs_mount_t *mp,
1946 xfs_agnumber_t agno,
1947 xfs_agino_t ino,
1948 xfs_dinode_t *dino,
1949 int type,
1950 int *dirty,
1951 xfs_rfsblock_t *totblocks,
1952 uint64_t *nextents,
1953 blkmap_t **dblkmap,
1954 int check_dups)
1955 {
1956 xfs_ino_t lino = XFS_AGINO_TO_INO(mp, agno, ino);
1957 int err = 0;
1958 int nex;
1959
1960 /*
1961 * extent count on disk is only valid for positive values. The kernel
1962 * uses negative values in memory. hence if we see negative numbers
1963 * here, trash it!
1964 */
1965 nex = be32_to_cpu(dino->di_nextents);
1966 if (nex < 0)
1967 *nextents = 1;
1968 else
1969 *nextents = nex;
1970
1971 if (*nextents > be64_to_cpu(dino->di_nblocks))
1972 *nextents = 1;
1973
1974
1975 if (dino->di_format != XFS_DINODE_FMT_LOCAL && type != XR_INO_RTDATA)
1976 *dblkmap = blkmap_alloc(*nextents, XFS_DATA_FORK);
1977 *nextents = 0;
1978
1979 switch (dino->di_format) {
1980 case XFS_DINODE_FMT_LOCAL:
1981 err = process_lclinode(mp, agno, ino, dino, XFS_DATA_FORK);
1982 *totblocks = 0;
1983 break;
1984 case XFS_DINODE_FMT_EXTENTS:
1985 err = process_exinode(mp, agno, ino, dino, type, dirty,
1986 totblocks, nextents, dblkmap, XFS_DATA_FORK,
1987 check_dups);
1988 break;
1989 case XFS_DINODE_FMT_BTREE:
1990 err = process_btinode(mp, agno, ino, dino, type, dirty,
1991 totblocks, nextents, dblkmap, XFS_DATA_FORK,
1992 check_dups);
1993 break;
1994 case XFS_DINODE_FMT_DEV: /* fall through */
1995 err = 0;
1996 break;
1997 default:
1998 do_error(_("unknown format %d, ino %" PRIu64 " (mode = %d)\n"),
1999 dino->di_format, lino, be16_to_cpu(dino->di_mode));
2000 }
2001
2002 if (err) {
2003 do_warn(_("bad data fork in inode %" PRIu64 "\n"), lino);
2004 if (!no_modify) {
2005 clear_dinode(mp, dino, lino);
2006 *dirty += 1;
2007 }
2008 return 1;
2009 }
2010
2011 if (check_dups) {
2012 /*
2013 * if check_dups was non-zero, we have to
2014 * re-process data fork to set bitmap since the
2015 * bitmap wasn't set the first time through
2016 */
2017 switch (dino->di_format) {
2018 case XFS_DINODE_FMT_LOCAL:
2019 err = process_lclinode(mp, agno, ino, dino,
2020 XFS_DATA_FORK);
2021 break;
2022 case XFS_DINODE_FMT_EXTENTS:
2023 err = process_exinode(mp, agno, ino, dino, type,
2024 dirty, totblocks, nextents, dblkmap,
2025 XFS_DATA_FORK, 0);
2026 break;
2027 case XFS_DINODE_FMT_BTREE:
2028 err = process_btinode(mp, agno, ino, dino, type,
2029 dirty, totblocks, nextents, dblkmap,
2030 XFS_DATA_FORK, 0);
2031 break;
2032 case XFS_DINODE_FMT_DEV: /* fall through */
2033 err = 0;
2034 break;
2035 default:
2036 do_error(_("unknown format %d, ino %" PRIu64 " (mode = %d)\n"),
2037 dino->di_format, lino,
2038 be16_to_cpu(dino->di_mode));
2039 }
2040
2041 if (no_modify && err != 0)
2042 return 1;
2043
2044 ASSERT(err == 0);
2045 }
2046 return 0;
2047 }
2048
2049 /*
2050 * Process extended attribute fork in inode
2051 */
2052 static int
2053 process_inode_attr_fork(
2054 xfs_mount_t *mp,
2055 xfs_agnumber_t agno,
2056 xfs_agino_t ino,
2057 xfs_dinode_t *dino,
2058 int type,
2059 int *dirty,
2060 xfs_rfsblock_t *atotblocks,
2061 uint64_t *anextents,
2062 int check_dups,
2063 int extra_attr_check,
2064 int *retval)
2065 {
2066 xfs_ino_t lino = XFS_AGINO_TO_INO(mp, agno, ino);
2067 blkmap_t *ablkmap = NULL;
2068 int repair = 0;
2069 int err;
2070
2071 if (!XFS_DFORK_Q(dino)) {
2072 *anextents = 0;
2073 if (dino->di_aformat != XFS_DINODE_FMT_EXTENTS) {
2074 do_warn(_("bad attribute format %d in inode %" PRIu64 ", "),
2075 dino->di_aformat, lino);
2076 if (!no_modify) {
2077 do_warn(_("resetting value\n"));
2078 dino->di_aformat = XFS_DINODE_FMT_EXTENTS;
2079 *dirty = 1;
2080 } else
2081 do_warn(_("would reset value\n"));
2082 }
2083 return 0;
2084 }
2085
2086 *anextents = be16_to_cpu(dino->di_anextents);
2087 if (*anextents > be64_to_cpu(dino->di_nblocks))
2088 *anextents = 1;
2089
2090 switch (dino->di_aformat) {
2091 case XFS_DINODE_FMT_LOCAL:
2092 *anextents = 0;
2093 *atotblocks = 0;
2094 err = process_lclinode(mp, agno, ino, dino, XFS_ATTR_FORK);
2095 break;
2096 case XFS_DINODE_FMT_EXTENTS:
2097 ablkmap = blkmap_alloc(*anextents, XFS_ATTR_FORK);
2098 *anextents = 0;
2099 err = process_exinode(mp, agno, ino, dino, type, dirty,
2100 atotblocks, anextents, &ablkmap,
2101 XFS_ATTR_FORK, check_dups);
2102 break;
2103 case XFS_DINODE_FMT_BTREE:
2104 ablkmap = blkmap_alloc(*anextents, XFS_ATTR_FORK);
2105 *anextents = 0;
2106 err = process_btinode(mp, agno, ino, dino, type, dirty,
2107 atotblocks, anextents, &ablkmap,
2108 XFS_ATTR_FORK, check_dups);
2109 break;
2110 default:
2111 do_warn(_("illegal attribute format %d, ino %" PRIu64 "\n"),
2112 dino->di_aformat, lino);
2113 err = 1;
2114 break;
2115 }
2116
2117 if (err) {
2118 /*
2119 * clear the attribute fork if necessary. we can't
2120 * clear the inode because we've already put the
2121 * inode space info into the blockmap.
2122 *
2123 * XXX - put the inode onto the "move it" list and
2124 * log the the attribute scrubbing
2125 */
2126 do_warn(_("bad attribute fork in inode %" PRIu64), lino);
2127
2128 if (!no_modify) {
2129 do_warn(_(", clearing attr fork\n"));
2130 *dirty += clear_dinode_attr(mp, dino, lino);
2131 dino->di_aformat = XFS_DINODE_FMT_LOCAL;
2132 ASSERT(*dirty > 0);
2133 } else {
2134 do_warn(_(", would clear attr fork\n"));
2135 }
2136
2137 *atotblocks = 0;
2138 *anextents = 0;
2139 blkmap_free(ablkmap);
2140 *retval = 1;
2141
2142 return 0;
2143 }
2144
2145 if (check_dups) {
2146 switch (dino->di_aformat) {
2147 case XFS_DINODE_FMT_LOCAL:
2148 err = process_lclinode(mp, agno, ino, dino,
2149 XFS_ATTR_FORK);
2150 break;
2151 case XFS_DINODE_FMT_EXTENTS:
2152 err = process_exinode(mp, agno, ino, dino,
2153 type, dirty, atotblocks, anextents,
2154 &ablkmap, XFS_ATTR_FORK, 0);
2155 break;
2156 case XFS_DINODE_FMT_BTREE:
2157 err = process_btinode(mp, agno, ino, dino,
2158 type, dirty, atotblocks, anextents,
2159 &ablkmap, XFS_ATTR_FORK, 0);
2160 break;
2161 default:
2162 do_error(_("illegal attribute fmt %d, ino %" PRIu64 "\n"),
2163 dino->di_aformat, lino);
2164 }
2165
2166 if (no_modify && err != 0) {
2167 blkmap_free(ablkmap);
2168 return 1;
2169 }
2170
2171 ASSERT(err == 0);
2172 }
2173
2174 /*
2175 * do attribute semantic-based consistency checks now
2176 */
2177
2178 /* get this only in phase 3, not in both phase 3 and 4 */
2179 if (extra_attr_check &&
2180 process_attributes(mp, lino, dino, ablkmap, &repair)) {
2181 do_warn(
2182 _("problem with attribute contents in inode %" PRIu64 "\n"),
2183 lino);
2184 if (!repair) {
2185 /* clear attributes if not done already */
2186 if (!no_modify) {
2187 *dirty += clear_dinode_attr(mp, dino, lino);
2188 dino->di_aformat = XFS_DINODE_FMT_LOCAL;
2189 } else {
2190 do_warn(_("would clear attr fork\n"));
2191 }
2192 *atotblocks = 0;
2193 *anextents = 0;
2194 }
2195 else {
2196 *dirty = 1; /* it's been repaired */
2197 }
2198 }
2199 blkmap_free(ablkmap);
2200 return 0;
2201 }
2202
2203 /*
2204 * check nlinks feature, if it's a version 1 inode,
2205 * just leave nlinks alone. even if it's set wrong,
2206 * it'll be reset when read in.
2207 */
2208
2209 static int
2210 process_check_inode_nlink_version(
2211 xfs_dinode_t *dino,
2212 xfs_ino_t lino)
2213 {
2214 int dirty = 0;
2215
2216 /*
2217 * if it's a version 2 inode, it should have a zero
2218 * onlink field, so clear it.
2219 */
2220 if (dino->di_version > 1 && dino->di_onlink != 0) {
2221 if (!no_modify) {
2222 do_warn(
2223 _("clearing obsolete nlink field in version 2 inode %" PRIu64 ", was %d, now 0\n"),
2224 lino, be16_to_cpu(dino->di_onlink));
2225 dino->di_onlink = 0;
2226 dirty = 1;
2227 } else {
2228 do_warn(
2229 _("would clear obsolete nlink field in version 2 inode %" PRIu64 ", currently %d\n"),
2230 lino, be16_to_cpu(dino->di_onlink));
2231 }
2232 }
2233 return dirty;
2234 }
2235
2236 /* Check nanoseconds of a timestamp don't exceed 1 second. */
2237 static void
2238 check_nsec(
2239 const char *name,
2240 xfs_ino_t lino,
2241 struct xfs_timestamp *t,
2242 int *dirty)
2243 {
2244 if (be32_to_cpu(t->t_nsec) < 1000000000)
2245 return;
2246
2247 do_warn(
2248 _("Bad %s nsec %u on inode %" PRIu64 ", "), name, be32_to_cpu(t->t_nsec), lino);
2249 if (no_modify) {
2250 do_warn(_("would reset to zero\n"));
2251 } else {
2252 do_warn(_("resetting to zero\n"));
2253 t->t_nsec = 0;
2254 *dirty = 1;
2255 }
2256 }
2257
2258 /*
2259 * returns 0 if the inode is ok, 1 if the inode is corrupt
2260 * check_dups can be set to 1 *only* when called by the
2261 * first pass of the duplicate block checking of phase 4.
2262 * *dirty is set > 0 if the dinode has been altered and
2263 * needs to be written out.
2264 *
2265 * for detailed, info, look at process_dinode() comments.
2266 */
2267 static int
2268 process_dinode_int(xfs_mount_t *mp,
2269 xfs_dinode_t *dino,
2270 xfs_agnumber_t agno,
2271 xfs_agino_t ino,
2272 int was_free, /* 1 if inode is currently free */
2273 int *dirty, /* out == > 0 if inode is now dirty */
2274 int *used, /* out == 1 if inode is in use */
2275 int verify_mode, /* 1 == verify but don't modify inode */
2276 int uncertain, /* 1 == inode is uncertain */
2277 int ino_discovery, /* 1 == check dirs for unknown inodes */
2278 int check_dups, /* 1 == check if inode claims
2279 * duplicate blocks */
2280 int extra_attr_check, /* 1 == do attribute format and value checks */
2281 int *isa_dir, /* out == 1 if inode is a directory */
2282 xfs_ino_t *parent) /* out -- parent if ino is a dir */
2283 {
2284 xfs_rfsblock_t totblocks = 0;
2285 xfs_rfsblock_t atotblocks = 0;
2286 int di_mode;
2287 int type;
2288 int retval = 0;
2289 uint64_t nextents;
2290 uint64_t anextents;
2291 xfs_ino_t lino;
2292 const int is_free = 0;
2293 const int is_used = 1;
2294 blkmap_t *dblkmap = NULL;
2295
2296 *dirty = *isa_dir = 0;
2297 *used = is_used;
2298 type = XR_INO_UNKNOWN;
2299
2300 lino = XFS_AGINO_TO_INO(mp, agno, ino);
2301 di_mode = be16_to_cpu(dino->di_mode);
2302
2303 /*
2304 * if in verify mode, don't modify the inode.
2305 *
2306 * if correcting, reset stuff that has known values
2307 *
2308 * if in uncertain mode, be silent on errors since we're
2309 * trying to find out if these are inodes as opposed
2310 * to assuming that they are. Just return the appropriate
2311 * return code in that case.
2312 *
2313 * If uncertain is set, verify_mode MUST be set.
2314 */
2315 ASSERT(uncertain == 0 || verify_mode != 0);
2316
2317 /*
2318 * This is the only valid point to check the CRC; after this we may have
2319 * made changes which invalidate it, and the CRC is only updated again
2320 * when it gets written out.
2321 *
2322 * Of course if we make any modifications after this, the inode gets
2323 * rewritten, and the CRC is updated automagically.
2324 */
2325 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2326 !libxfs_verify_cksum((char *)dino, mp->m_sb.sb_inodesize,
2327 XFS_DINODE_CRC_OFF)) {
2328 retval = 1;
2329 if (!uncertain)
2330 do_warn(_("bad CRC for inode %" PRIu64 "%c"),
2331 lino, verify_mode ? '\n' : ',');
2332 if (!verify_mode) {
2333 if (!no_modify) {
2334 do_warn(_(" will rewrite\n"));
2335 *dirty = 1;
2336 } else
2337 do_warn(_(" would rewrite\n"));
2338 }
2339 }
2340
2341 if (be16_to_cpu(dino->di_magic) != XFS_DINODE_MAGIC) {
2342 retval = 1;
2343 if (!uncertain)
2344 do_warn(_("bad magic number 0x%x on inode %" PRIu64 "%c"),
2345 be16_to_cpu(dino->di_magic), lino,
2346 verify_mode ? '\n' : ',');
2347 if (!verify_mode) {
2348 if (!no_modify) {
2349 do_warn(_(" resetting magic number\n"));
2350 dino->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
2351 *dirty = 1;
2352 } else
2353 do_warn(_(" would reset magic number\n"));
2354 }
2355 }
2356
2357 if (!libxfs_dinode_good_version(mp, dino->di_version)) {
2358 retval = 1;
2359 if (!uncertain)
2360 do_warn(_("bad version number 0x%x on inode %" PRIu64 "%c"),
2361 (__s8)dino->di_version, lino,
2362 verify_mode ? '\n' : ',');
2363 if (!verify_mode) {
2364 if (!no_modify) {
2365 do_warn(_(" resetting version number\n"));
2366 dino->di_version =
2367 xfs_sb_version_hascrc(&mp->m_sb) ? 3 : 2;
2368 *dirty = 1;
2369 } else
2370 do_warn(_(" would reset version number\n"));
2371 }
2372 }
2373
2374 /*
2375 * We don't bother checking the CRC here - we cannot guarantee that when
2376 * we are called here that the inode has not already been modified in
2377 * memory and hence invalidated the CRC.
2378 */
2379 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2380 if (be64_to_cpu(dino->di_ino) != lino) {
2381 if (!uncertain)
2382 do_warn(
2383 _("inode identifier %llu mismatch on inode %" PRIu64 "\n"),
2384 (unsigned long long)be64_to_cpu(dino->di_ino),
2385 lino);
2386 if (verify_mode)
2387 return 1;
2388 goto clear_bad_out;
2389 }
2390 if (platform_uuid_compare(&dino->di_uuid,
2391 &mp->m_sb.sb_meta_uuid)) {
2392 if (!uncertain)
2393 do_warn(
2394 _("UUID mismatch on inode %" PRIu64 "\n"), lino);
2395 if (verify_mode)
2396 return 1;
2397 goto clear_bad_out;
2398 }
2399 }
2400
2401 /*
2402 * blow out of here if the inode size is < 0
2403 */
2404 if ((xfs_fsize_t)be64_to_cpu(dino->di_size) < 0) {
2405 if (!uncertain)
2406 do_warn(
2407 _("bad (negative) size %" PRId64 " on inode %" PRIu64 "\n"),
2408 (int64_t)be64_to_cpu(dino->di_size),
2409 lino);
2410 if (verify_mode)
2411 return 1;
2412 goto clear_bad_out;
2413 }
2414
2415 /*
2416 * if not in verify mode, check to see if the inode and imap
2417 * agree that the inode is free
2418 */
2419 if (!verify_mode && di_mode == 0) {
2420 /*
2421 * was_free value is not meaningful if we're in verify mode
2422 */
2423 if (was_free) {
2424 /*
2425 * easy case, inode free -- inode and map agree, check
2426 * it just in case to ensure that format, etc. are
2427 * set correctly
2428 */
2429 if (libxfs_dinode_verify(mp, lino, dino) != NULL) {
2430 do_warn(
2431 _("free inode %" PRIu64 " contains errors, "), lino);
2432 if (!no_modify) {
2433 clear_dinode(mp, dino, lino);
2434 do_warn(_("corrected\n"));
2435 *dirty += 1;
2436 } else {
2437 do_warn(_("would correct\n"));
2438 }
2439 }
2440 *used = is_free;
2441 return 0;
2442 }
2443 /*
2444 * the inode looks free but the map says it's in use.
2445 * clear the inode just to be safe and mark the inode
2446 * free.
2447 */
2448 do_warn(
2449 _("imap claims a free inode %" PRIu64 " is in use, "), lino);
2450 if (!no_modify) {
2451 do_warn(_("correcting imap and clearing inode\n"));
2452 clear_dinode(mp, dino, lino);
2453 *dirty += 1;
2454 retval = 1;
2455 } else
2456 do_warn(_("would correct imap and clear inode\n"));
2457 *used = is_free;
2458 return retval;
2459 }
2460
2461 /*
2462 * because of the lack of any write ordering guarantee, it's
2463 * possible that the core got updated but the forks didn't.
2464 * so rather than be ambitious (and probably incorrect),
2465 * if there's an inconsistency, we get conservative and
2466 * just pitch the file. blow off checking formats of
2467 * free inodes since technically any format is legal
2468 * as we reset the inode when we re-use it.
2469 */
2470 if (di_mode != 0 && check_dinode_mode_format(dino) != 0) {
2471 if (!uncertain)
2472 do_warn(
2473 _("bad inode format in inode %" PRIu64 "\n"), lino);
2474 if (verify_mode)
2475 return 1;
2476 goto clear_bad_out;
2477 }
2478
2479 /*
2480 * check that we only have valid flags set, and those that are set make
2481 * sense.
2482 */
2483 if (dino->di_flags) {
2484 uint16_t flags = be16_to_cpu(dino->di_flags);
2485
2486 if (flags & ~XFS_DIFLAG_ANY) {
2487 if (!uncertain) {
2488 do_warn(
2489 _("Bad flags set in inode %" PRIu64 "\n"),
2490 lino);
2491 }
2492 flags &= XFS_DIFLAG_ANY;
2493 }
2494
2495 if (flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_RTINHERIT)) {
2496 /* need an rt-dev! */
2497 if (!rt_name) {
2498 if (!uncertain) {
2499 do_warn(
2500 _("inode %" PRIu64 " has RT flag set but there is no RT device\n"),
2501 lino);
2502 }
2503 flags &= ~(XFS_DIFLAG_REALTIME |
2504 XFS_DIFLAG_RTINHERIT);
2505 }
2506 }
2507 if (flags & XFS_DIFLAG_NEWRTBM) {
2508 /* must be a rt bitmap inode */
2509 if (lino != mp->m_sb.sb_rbmino) {
2510 if (!uncertain) {
2511 do_warn(
2512 _("inode %" PRIu64 " not rt bitmap\n"),
2513 lino);
2514 }
2515 flags &= ~XFS_DIFLAG_NEWRTBM;
2516 }
2517 }
2518 if (flags & (XFS_DIFLAG_RTINHERIT |
2519 XFS_DIFLAG_EXTSZINHERIT |
2520 XFS_DIFLAG_PROJINHERIT |
2521 XFS_DIFLAG_NOSYMLINKS)) {
2522 /* must be a directory */
2523 if (di_mode && !S_ISDIR(di_mode)) {
2524 if (!uncertain) {
2525 do_warn(
2526 _("directory flags set on non-directory inode %" PRIu64 "\n" ),
2527 lino);
2528 }
2529 flags &= ~(XFS_DIFLAG_RTINHERIT |
2530 XFS_DIFLAG_EXTSZINHERIT |
2531 XFS_DIFLAG_PROJINHERIT |
2532 XFS_DIFLAG_NOSYMLINKS);
2533 }
2534 }
2535 if (flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) {
2536 /* must be a file */
2537 if (di_mode && !S_ISREG(di_mode)) {
2538 if (!uncertain) {
2539 do_warn(
2540 _("file flags set on non-file inode %" PRIu64 "\n"),
2541 lino);
2542 }
2543 flags &= ~(XFS_DIFLAG_REALTIME |
2544 FS_XFLAG_EXTSIZE);
2545 }
2546 }
2547 if (!verify_mode && flags != be16_to_cpu(dino->di_flags)) {
2548 if (!no_modify) {
2549 do_warn(_("fixing bad flags.\n"));
2550 dino->di_flags = cpu_to_be16(flags);
2551 *dirty = 1;
2552 } else
2553 do_warn(_("would fix bad flags.\n"));
2554 }
2555 }
2556
2557 /*
2558 * check that we only have valid flags2 set, and those that are set make
2559 * sense.
2560 */
2561 if (dino->di_version >= 3) {
2562 uint16_t flags = be16_to_cpu(dino->di_flags);
2563 uint64_t flags2 = be64_to_cpu(dino->di_flags2);
2564
2565 if (flags2 & ~XFS_DIFLAG2_ANY) {
2566 if (!uncertain) {
2567 do_warn(
2568 _("Bad flags2 set in inode %" PRIu64 "\n"),
2569 lino);
2570 }
2571 flags2 &= XFS_DIFLAG2_ANY;
2572 }
2573
2574 if (flags2 & XFS_DIFLAG2_DAX) {
2575 /* must be a file or dir */
2576 if (di_mode && !(S_ISREG(di_mode) || S_ISDIR(di_mode))) {
2577 if (!uncertain) {
2578 do_warn(
2579 _("DAX flag set on special inode %" PRIu64 "\n"),
2580 lino);
2581 }
2582 flags2 &= ~XFS_DIFLAG2_DAX;
2583 }
2584 }
2585
2586 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
2587 !xfs_sb_version_hasreflink(&mp->m_sb)) {
2588 if (!uncertain) {
2589 do_warn(
2590 _("inode %" PRIu64 " is marked reflinked but file system does not support reflink\n"),
2591 lino);
2592 }
2593 goto clear_bad_out;
2594 }
2595
2596 if (flags2 & XFS_DIFLAG2_REFLINK) {
2597 /* must be a file */
2598 if (di_mode && !S_ISREG(di_mode)) {
2599 if (!uncertain) {
2600 do_warn(
2601 _("reflink flag set on non-file inode %" PRIu64 "\n"),
2602 lino);
2603 }
2604 goto clear_bad_out;
2605 }
2606 }
2607
2608 if ((flags2 & XFS_DIFLAG2_REFLINK) &&
2609 (flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_RTINHERIT))) {
2610 if (!uncertain) {
2611 do_warn(
2612 _("Cannot have a reflinked realtime inode %" PRIu64 "\n"),
2613 lino);
2614 }
2615 goto clear_bad_out;
2616 }
2617
2618 if ((flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
2619 !xfs_sb_version_hasreflink(&mp->m_sb)) {
2620 if (!uncertain) {
2621 do_warn(
2622 _("inode %" PRIu64 " has CoW extent size hint but file system does not support reflink\n"),
2623 lino);
2624 }
2625 flags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
2626 }
2627
2628 if (flags2 & XFS_DIFLAG2_COWEXTSIZE) {
2629 /* must be a directory or file */
2630 if (di_mode && !S_ISDIR(di_mode) && !S_ISREG(di_mode)) {
2631 if (!uncertain) {
2632 do_warn(
2633 _("CoW extent size flag set on non-file, non-directory inode %" PRIu64 "\n" ),
2634 lino);
2635 }
2636 flags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
2637 }
2638 }
2639
2640 if ((flags2 & XFS_DIFLAG2_COWEXTSIZE) &&
2641 (flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_RTINHERIT))) {
2642 if (!uncertain) {
2643 do_warn(
2644 _("Cannot have CoW extent size hint on a realtime inode %" PRIu64 "\n"),
2645 lino);
2646 }
2647 flags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
2648 }
2649
2650 if (!verify_mode && flags2 != be64_to_cpu(dino->di_flags2)) {
2651 if (!no_modify) {
2652 do_warn(_("fixing bad flags2.\n"));
2653 dino->di_flags2 = cpu_to_be64(flags2);
2654 *dirty = 1;
2655 } else
2656 do_warn(_("would fix bad flags2.\n"));
2657 }
2658 }
2659
2660 if (verify_mode)
2661 return retval;
2662
2663 /*
2664 * clear the next unlinked field if necessary on a good
2665 * inode only during phase 4 -- when checking for inodes
2666 * referencing duplicate blocks. then it's safe because
2667 * we've done the inode discovery and have found all the inodes
2668 * we're going to find. check_dups is set to 1 only during
2669 * phase 4. Ugly.
2670 */
2671 if (check_dups && be32_to_cpu(dino->di_next_unlinked) != NULLAGINO) {
2672 if (no_modify) {
2673 do_warn(
2674 _("Would clear next_unlinked in inode %" PRIu64 "\n"), lino);
2675 } else {
2676 clear_dinode_unlinked(mp, dino);
2677 do_warn(
2678 _("Cleared next_unlinked in inode %" PRIu64 "\n"), lino);
2679 *dirty += 1;
2680 }
2681 }
2682
2683 /* set type and map type info */
2684
2685 switch (di_mode & S_IFMT) {
2686 case S_IFDIR:
2687 type = XR_INO_DIR;
2688 *isa_dir = 1;
2689 break;
2690 case S_IFREG:
2691 if (be16_to_cpu(dino->di_flags) & XFS_DIFLAG_REALTIME)
2692 type = XR_INO_RTDATA;
2693 else if (lino == mp->m_sb.sb_rbmino)
2694 type = XR_INO_RTBITMAP;
2695 else if (lino == mp->m_sb.sb_rsumino)
2696 type = XR_INO_RTSUM;
2697 else if (lino == mp->m_sb.sb_uquotino)
2698 type = XR_INO_UQUOTA;
2699 else if (lino == mp->m_sb.sb_gquotino)
2700 type = XR_INO_GQUOTA;
2701 else if (lino == mp->m_sb.sb_pquotino)
2702 type = XR_INO_PQUOTA;
2703 else
2704 type = XR_INO_DATA;
2705 break;
2706 case S_IFLNK:
2707 type = XR_INO_SYMLINK;
2708 break;
2709 case S_IFCHR:
2710 type = XR_INO_CHRDEV;
2711 break;
2712 case S_IFBLK:
2713 type = XR_INO_BLKDEV;
2714 break;
2715 case S_IFSOCK:
2716 type = XR_INO_SOCK;
2717 break;
2718 case S_IFIFO:
2719 type = XR_INO_FIFO;
2720 break;
2721 default:
2722 do_warn(_("bad inode type %#o inode %" PRIu64 "\n"),
2723 di_mode & S_IFMT, lino);
2724 goto clear_bad_out;
2725 }
2726
2727 /*
2728 * type checks for superblock inodes
2729 */
2730 if (process_check_sb_inodes(mp, dino, lino, &type, dirty) != 0)
2731 goto clear_bad_out;
2732
2733 /*
2734 * only regular files with REALTIME or EXTSIZE flags set can have
2735 * extsize set, or directories with EXTSZINHERIT.
2736 */
2737 if (libxfs_inode_validate_extsize(mp,
2738 be32_to_cpu(dino->di_extsize),
2739 be16_to_cpu(dino->di_mode),
2740 be16_to_cpu(dino->di_flags)) != NULL) {
2741 do_warn(
2742 _("Bad extent size %u on inode %" PRIu64 ", "),
2743 be32_to_cpu(dino->di_extsize), lino);
2744 if (!no_modify) {
2745 do_warn(_("resetting to zero\n"));
2746 dino->di_extsize = 0;
2747 dino->di_flags &= ~cpu_to_be16(XFS_DIFLAG_EXTSIZE |
2748 XFS_DIFLAG_EXTSZINHERIT);
2749 *dirty = 1;
2750 } else
2751 do_warn(_("would reset to zero\n"));
2752 }
2753
2754 /*
2755 * Only (regular files and directories) with COWEXTSIZE flags
2756 * set can have extsize set.
2757 */
2758 if (dino->di_version >= 3 &&
2759 libxfs_inode_validate_cowextsize(mp,
2760 be32_to_cpu(dino->di_cowextsize),
2761 be16_to_cpu(dino->di_mode),
2762 be16_to_cpu(dino->di_flags),
2763 be64_to_cpu(dino->di_flags2)) != NULL) {
2764 do_warn(
2765 _("Bad CoW extent size %u on inode %" PRIu64 ", "),
2766 be32_to_cpu(dino->di_cowextsize), lino);
2767 if (!no_modify) {
2768 do_warn(_("resetting to zero\n"));
2769 dino->di_flags2 &= ~cpu_to_be64(XFS_DIFLAG2_COWEXTSIZE);
2770 dino->di_cowextsize = 0;
2771 *dirty = 1;
2772 } else
2773 do_warn(_("would reset to zero\n"));
2774 }
2775
2776 /* nsec fields cannot be larger than 1 billion */
2777 check_nsec("atime", lino, &dino->di_atime, dirty);
2778 check_nsec("mtime", lino, &dino->di_mtime, dirty);
2779 check_nsec("ctime", lino, &dino->di_ctime, dirty);
2780 if (dino->di_version >= 3)
2781 check_nsec("crtime", lino, &dino->di_crtime, dirty);
2782
2783 /*
2784 * general size/consistency checks:
2785 */
2786 if (process_check_inode_sizes(mp, dino, lino, type) != 0)
2787 goto clear_bad_out;
2788
2789 /*
2790 * check for illegal values of forkoff
2791 */
2792 if (process_check_inode_forkoff(mp, dino, lino) != 0)
2793 goto clear_bad_out;
2794
2795 /*
2796 * record the state of the reflink flag
2797 */
2798 if (collect_rmaps)
2799 record_inode_reflink_flag(mp, dino, agno, ino, lino);
2800
2801 /*
2802 * check data fork -- if it's bad, clear the inode
2803 */
2804 if (process_inode_data_fork(mp, agno, ino, dino, type, dirty,
2805 &totblocks, &nextents, &dblkmap, check_dups) != 0)
2806 goto bad_out;
2807
2808 /*
2809 * check attribute fork if necessary. attributes are
2810 * always stored in the regular filesystem.
2811 */
2812 if (process_inode_attr_fork(mp, agno, ino, dino, type, dirty,
2813 &atotblocks, &anextents, check_dups, extra_attr_check,
2814 &retval))
2815 goto bad_out;
2816
2817 /*
2818 * enforce totblocks is 0 for misc types
2819 */
2820 if (process_misc_ino_types_blocks(totblocks, lino, type))
2821 goto clear_bad_out;
2822
2823 /*
2824 * correct space counters if required
2825 */
2826 if (process_inode_blocks_and_extents(dino, totblocks + atotblocks,
2827 nextents, anextents, lino, dirty) != 0)
2828 goto clear_bad_out;
2829
2830 /*
2831 * do any semantic type-based checking here
2832 */
2833 switch (type) {
2834 case XR_INO_DIR:
2835 if (process_dir2(mp, lino, dino, ino_discovery,
2836 dirty, "", parent, dblkmap)) {
2837 do_warn(
2838 _("problem with directory contents in inode %" PRIu64 "\n"),
2839 lino);
2840 goto clear_bad_out;
2841 }
2842 break;
2843 case XR_INO_SYMLINK:
2844 if (process_symlink(mp, lino, dino, dblkmap) != 0) {
2845 do_warn(
2846 _("problem with symbolic link in inode %" PRIu64 "\n"),
2847 lino);
2848 goto clear_bad_out;
2849 }
2850 break;
2851 case XR_INO_UQUOTA:
2852 case XR_INO_GQUOTA:
2853 case XR_INO_PQUOTA:
2854 if (process_quota_inode(mp, lino, dino, type, dblkmap) != 0) {
2855 do_warn(
2856 _("problem with quota inode %" PRIu64 "\n"), lino);
2857 goto clear_bad_out;
2858 }
2859 break;
2860 default:
2861 break;
2862 }
2863
2864 blkmap_free(dblkmap);
2865
2866 /*
2867 * check nlinks feature, if it's a version 1 inode,
2868 * just leave nlinks alone. even if it's set wrong,
2869 * it'll be reset when read in.
2870 */
2871 *dirty += process_check_inode_nlink_version(dino, lino);
2872
2873 return retval;
2874
2875 clear_bad_out:
2876 if (!no_modify) {
2877 clear_dinode(mp, dino, lino);
2878 *dirty += 1;
2879 }
2880 bad_out:
2881 *used = is_free;
2882 *isa_dir = 0;
2883 blkmap_free(dblkmap);
2884 return 1;
2885 }
2886
2887 /*
2888 * returns 1 if inode is used, 0 if free.
2889 * performs any necessary salvaging actions.
2890 * note that we leave the generation count alone
2891 * because nothing we could set it to would be
2892 * guaranteed to be correct so the best guess for
2893 * the correct value is just to leave it alone.
2894 *
2895 * The trick is detecting empty files. For those,
2896 * the core and the forks should all be in the "empty"
2897 * or zero-length state -- a zero or possibly minimum length
2898 * (in the case of dirs) extent list -- although inline directories
2899 * and symlinks might be handled differently. So it should be
2900 * possible to sanity check them against each other.
2901 *
2902 * If the forks are an empty extent list though, then forget it.
2903 * The file is toast anyway since we can't recover its storage.
2904 *
2905 * Parameters:
2906 * Ins:
2907 * mp -- mount structure
2908 * dino -- pointer to on-disk inode structure
2909 * agno/ino -- inode numbers
2910 * free -- whether the map thinks the inode is free (1 == free)
2911 * ino_discovery -- whether we should examine directory
2912 * contents to discover new inodes
2913 * check_dups -- whether we should check to see if the
2914 * inode references duplicate blocks
2915 * if so, we compare the inode's claimed
2916 * blocks against the contents of the
2917 * duplicate extent list but we don't
2918 * set the bitmap. If not, we set the
2919 * bitmap and try and detect multiply
2920 * claimed blocks using the bitmap.
2921 * Outs:
2922 * dirty -- whether we changed the inode (1 == yes)
2923 * used -- 1 if the inode is used, 0 if free. In no modify
2924 * mode, whether the inode should be used or free
2925 * isa_dir -- 1 if the inode is a directory, 0 if not. In
2926 * no modify mode, if the inode would be a dir or not.
2927 *
2928 * Return value -- 0 if the inode is good, 1 if it is/was corrupt
2929 */
2930
2931 int
2932 process_dinode(
2933 xfs_mount_t *mp,
2934 xfs_dinode_t *dino,
2935 xfs_agnumber_t agno,
2936 xfs_agino_t ino,
2937 int was_free,
2938 int *dirty,
2939 int *used,
2940 int ino_discovery,
2941 int check_dups,
2942 int extra_attr_check,
2943 int *isa_dir,
2944 xfs_ino_t *parent)
2945 {
2946 const int verify_mode = 0;
2947 const int uncertain = 0;
2948
2949 #ifdef XR_INODE_TRACE
2950 fprintf(stderr, _("processing inode %d/%d\n"), agno, ino);
2951 #endif
2952 return process_dinode_int(mp, dino, agno, ino, was_free, dirty, used,
2953 verify_mode, uncertain, ino_discovery,
2954 check_dups, extra_attr_check, isa_dir, parent);
2955 }
2956
2957 /*
2958 * a more cursory check, check inode core, *DON'T* check forks
2959 * this basically just verifies whether the inode is an inode
2960 * and whether or not it has been totally trashed. returns 0
2961 * if the inode passes the cursory sanity check, 1 otherwise.
2962 */
2963 int
2964 verify_dinode(
2965 xfs_mount_t *mp,
2966 xfs_dinode_t *dino,
2967 xfs_agnumber_t agno,
2968 xfs_agino_t ino)
2969 {
2970 xfs_ino_t parent;
2971 int used = 0;
2972 int dirty = 0;
2973 int isa_dir = 0;
2974 const int verify_mode = 1;
2975 const int check_dups = 0;
2976 const int ino_discovery = 0;
2977 const int uncertain = 0;
2978
2979 return process_dinode_int(mp, dino, agno, ino, 0, &dirty, &used,
2980 verify_mode, uncertain, ino_discovery,
2981 check_dups, 0, &isa_dir, &parent);
2982 }
2983
2984 /*
2985 * like above only for inode on the uncertain list. it sets
2986 * the uncertain flag which makes process_dinode_int quieter.
2987 * returns 0 if the inode passes the cursory sanity check, 1 otherwise.
2988 */
2989 int
2990 verify_uncertain_dinode(
2991 xfs_mount_t *mp,
2992 xfs_dinode_t *dino,
2993 xfs_agnumber_t agno,
2994 xfs_agino_t ino)
2995 {
2996 xfs_ino_t parent;
2997 int used = 0;
2998 int dirty = 0;
2999 int isa_dir = 0;
3000 const int verify_mode = 1;
3001 const int check_dups = 0;
3002 const int ino_discovery = 0;
3003 const int uncertain = 1;
3004
3005 return process_dinode_int(mp, dino, agno, ino, 0, &dirty, &used,
3006 verify_mode, uncertain, ino_discovery,
3007 check_dups, 0, &isa_dir, &parent);
3008 }