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