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