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