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