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