]>
Commit | Line | Data |
---|---|---|
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 | */ | |
33 | static char *forkname_data; | |
34 | static char *forkname_attr; | |
35 | static char *ftype_real_time; | |
36 | static char *ftype_regular; | |
37 | ||
38 | void | |
39 | dinode_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 | ||
47 | char * | |
48 | get_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 | 60 | static int |
7328ea6e CH |
61 | clear_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 | 116 | static void |
7328ea6e CH |
117 | clear_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 | 139 | static void |
7328ea6e | 140 | clear_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 | 150 | static void |
7328ea6e | 151 | clear_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 | ||
171 | static __inline int | |
6271fa06 DW |
172 | verify_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 | |
191 | static int | |
192 | process_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 | ||
219 | static int | |
220 | process_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 |
290 | static int |
291 | process_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 | 355 | static int |
2bd0ea18 NS |
356 | process_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; |
641 | done: | |
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 | */ | |
658 | int | |
659 | process_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 | */ | |
679 | int | |
680 | scan_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 |
702 | struct xfs_buf * |
703 | get_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 | 756 | static int |
2bd0ea18 NS |
757 | process_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 | 931 | static int |
2bd0ea18 NS |
932 | process_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 | 989 | static int |
2bd0ea18 | 990 | process_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 | 1030 | static int |
7328ea6e CH |
1031 | process_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 | 1103 | static int |
2bd0ea18 NS |
1104 | null_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) | |
1123 | static int | |
1124 | process_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 | ||
1215 | bad: | |
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 |
1238 | static int |
1239 | process_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 | 1344 | static int |
0459b626 | 1345 | process_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 | 1410 | static int |
7328ea6e CH |
1411 | process_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 | 1461 | static int |
5a35bf2c | 1462 | process_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 |
1503 | static inline int |
1504 | dinode_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 |
1510 | static inline void |
1511 | change_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 | ||
1524 | static int | |
1525 | check_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 | ||
1560 | static int | |
1561 | process_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 | */ | |
1668 | static int | |
1669 | process_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 | */ | |
1760 | static int | |
1761 | process_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 | */ | |
1799 | static int | |
1800 | process_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 | */ | |
1895 | static int | |
1896 | process_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 | 1915 | retry: |
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 | */ | |
2035 | static int | |
2036 | process_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; | |
7328ea6e CH |
2053 | int repair = 0; |
2054 | int err; | |
b3bcb8f0 | 2055 | int try_rebuild = -1; /* don't know yet */ |
0459b626 | 2056 | |
b3bcb8f0 | 2057 | retry: |
8c6cccd7 | 2058 | if (!dino->di_forkoff) { |
0459b626 | 2059 | *anextents = 0; |
56b2de80 | 2060 | if (dino->di_aformat != XFS_DINODE_FMT_EXTENTS) { |
5d1b7f0f | 2061 | do_warn(_("bad attribute format %d in inode %" PRIu64 ", "), |
56b2de80 | 2062 | dino->di_aformat, lino); |
0459b626 BN |
2063 | if (!no_modify) { |
2064 | do_warn(_("resetting value\n")); | |
56b2de80 | 2065 | dino->di_aformat = XFS_DINODE_FMT_EXTENTS; |
0459b626 BN |
2066 | *dirty = 1; |
2067 | } else | |
2068 | do_warn(_("would reset value\n")); | |
2069 | } | |
2070 | return 0; | |
2071 | } | |
2bd0ea18 | 2072 | |
5f70c91b | 2073 | *anextents = xfs_dfork_attr_extents(dino); |
56b2de80 | 2074 | if (*anextents > be64_to_cpu(dino->di_nblocks)) |
0459b626 BN |
2075 | *anextents = 1; |
2076 | ||
56b2de80 | 2077 | switch (dino->di_aformat) { |
0459b626 BN |
2078 | case XFS_DINODE_FMT_LOCAL: |
2079 | *anextents = 0; | |
5e656dbb BN |
2080 | *atotblocks = 0; |
2081 | err = process_lclinode(mp, agno, ino, dino, XFS_ATTR_FORK); | |
0459b626 BN |
2082 | break; |
2083 | case XFS_DINODE_FMT_EXTENTS: | |
b3bcb8f0 DW |
2084 | if (!rmapbt_suspect && try_rebuild == -1) |
2085 | try_rebuild = 1; | |
610f3285 | 2086 | ablkmap = blkmap_alloc(*anextents, XFS_ATTR_FORK); |
0459b626 BN |
2087 | *anextents = 0; |
2088 | err = process_exinode(mp, agno, ino, dino, type, dirty, | |
2089 | atotblocks, anextents, &ablkmap, | |
2bd0ea18 | 2090 | XFS_ATTR_FORK, check_dups); |
0459b626 BN |
2091 | break; |
2092 | case XFS_DINODE_FMT_BTREE: | |
b3bcb8f0 DW |
2093 | if (!rmapbt_suspect && try_rebuild == -1) |
2094 | try_rebuild = 1; | |
610f3285 | 2095 | ablkmap = blkmap_alloc(*anextents, XFS_ATTR_FORK); |
0459b626 BN |
2096 | *anextents = 0; |
2097 | err = process_btinode(mp, agno, ino, dino, type, dirty, | |
2098 | atotblocks, anextents, &ablkmap, | |
2bd0ea18 | 2099 | XFS_ATTR_FORK, check_dups); |
0459b626 BN |
2100 | break; |
2101 | default: | |
5d1b7f0f | 2102 | do_warn(_("illegal attribute format %d, ino %" PRIu64 "\n"), |
56b2de80 | 2103 | dino->di_aformat, lino); |
0459b626 BN |
2104 | err = 1; |
2105 | break; | |
2106 | } | |
2bd0ea18 | 2107 | |
0459b626 BN |
2108 | if (err) { |
2109 | /* | |
2110 | * clear the attribute fork if necessary. we can't | |
2111 | * clear the inode because we've already put the | |
2112 | * inode space info into the blockmap. | |
2113 | * | |
2114 | * XXX - put the inode onto the "move it" list and | |
2115 | * log the the attribute scrubbing | |
2116 | */ | |
164a5514 | 2117 | do_warn(_("bad attribute fork in inode %" PRIu64 "\n"), lino); |
2bd0ea18 | 2118 | |
0459b626 | 2119 | if (!no_modify) { |
b3bcb8f0 DW |
2120 | if (try_rebuild == 1) { |
2121 | do_warn( | |
2122 | _("rebuilding inode %"PRIu64" attr fork\n"), | |
2123 | lino); | |
2124 | try_rebuild = 0; | |
2125 | err = rebuild_bmap(mp, lino, XFS_ATTR_FORK, | |
2126 | be16_to_cpu(dino->di_anextents), | |
2127 | ino_bpp, dinop, dirty); | |
2128 | dino = *dinop; | |
2129 | if (!err) | |
2130 | goto retry; | |
2131 | do_warn( | |
2132 | _("inode %"PRIu64" attr fork rebuild failed, error %d"), | |
2133 | lino, err); | |
2134 | } | |
28a4f9c0 ES |
2135 | do_warn(_(", clearing attr fork\n")); |
2136 | *dirty += clear_dinode_attr(mp, dino, lino); | |
0459b626 | 2137 | ASSERT(*dirty > 0); |
b3bcb8f0 DW |
2138 | } else if (try_rebuild) { |
2139 | do_warn( | |
2140 | _("would have tried to rebuild inode %"PRIu64" attr fork or cleared it\n"), | |
2141 | lino); | |
2142 | } else { | |
0459b626 | 2143 | do_warn(_(", would clear attr fork\n")); |
2bd0ea18 NS |
2144 | } |
2145 | ||
0459b626 BN |
2146 | *atotblocks = 0; |
2147 | *anextents = 0; | |
2bd0ea18 | 2148 | blkmap_free(ablkmap); |
0459b626 | 2149 | *retval = 1; |
2bd0ea18 | 2150 | |
28a4f9c0 | 2151 | return 0; |
2bd0ea18 NS |
2152 | } |
2153 | ||
0459b626 | 2154 | if (check_dups) { |
56b2de80 | 2155 | switch (dino->di_aformat) { |
0459b626 | 2156 | case XFS_DINODE_FMT_LOCAL: |
f8149110 | 2157 | err = process_lclinode(mp, agno, ino, dino, |
5e656dbb | 2158 | XFS_ATTR_FORK); |
0459b626 BN |
2159 | break; |
2160 | case XFS_DINODE_FMT_EXTENTS: | |
2161 | err = process_exinode(mp, agno, ino, dino, | |
2162 | type, dirty, atotblocks, anextents, | |
2163 | &ablkmap, XFS_ATTR_FORK, 0); | |
2164 | break; | |
2165 | case XFS_DINODE_FMT_BTREE: | |
2166 | err = process_btinode(mp, agno, ino, dino, | |
2167 | type, dirty, atotblocks, anextents, | |
2168 | &ablkmap, XFS_ATTR_FORK, 0); | |
2169 | break; | |
2170 | default: | |
5d1b7f0f | 2171 | do_error(_("illegal attribute fmt %d, ino %" PRIu64 "\n"), |
56b2de80 | 2172 | dino->di_aformat, lino); |
2bd0ea18 | 2173 | } |
2bd0ea18 | 2174 | |
0459b626 BN |
2175 | if (no_modify && err != 0) { |
2176 | blkmap_free(ablkmap); | |
2177 | return 1; | |
2bd0ea18 | 2178 | } |
2bd0ea18 | 2179 | |
0459b626 | 2180 | ASSERT(err == 0); |
2bd0ea18 NS |
2181 | } |
2182 | ||
0459b626 BN |
2183 | /* |
2184 | * do attribute semantic-based consistency checks now | |
2185 | */ | |
2bd0ea18 | 2186 | |
0459b626 BN |
2187 | /* get this only in phase 3, not in both phase 3 and 4 */ |
2188 | if (extra_attr_check && | |
2189 | process_attributes(mp, lino, dino, ablkmap, &repair)) { | |
5d1b7f0f CH |
2190 | do_warn( |
2191 | _("problem with attribute contents in inode %" PRIu64 "\n"), | |
0459b626 BN |
2192 | lino); |
2193 | if (!repair) { | |
2194 | /* clear attributes if not done already */ | |
2195 | if (!no_modify) { | |
2196 | *dirty += clear_dinode_attr(mp, dino, lino); | |
0459b626 BN |
2197 | } else { |
2198 | do_warn(_("would clear attr fork\n")); | |
2199 | } | |
2200 | *atotblocks = 0; | |
2201 | *anextents = 0; | |
2bd0ea18 | 2202 | } |
0459b626 BN |
2203 | else { |
2204 | *dirty = 1; /* it's been repaired */ | |
2bd0ea18 NS |
2205 | } |
2206 | } | |
0459b626 BN |
2207 | blkmap_free(ablkmap); |
2208 | return 0; | |
2209 | } | |
2bd0ea18 | 2210 | |
0459b626 BN |
2211 | /* |
2212 | * check nlinks feature, if it's a version 1 inode, | |
2213 | * just leave nlinks alone. even if it's set wrong, | |
2214 | * it'll be reset when read in. | |
2215 | */ | |
2bd0ea18 | 2216 | |
0459b626 BN |
2217 | static int |
2218 | process_check_inode_nlink_version( | |
7328ea6e CH |
2219 | struct xfs_dinode *dino, |
2220 | xfs_ino_t lino) | |
0459b626 | 2221 | { |
7328ea6e | 2222 | int dirty = 0; |
2bd0ea18 | 2223 | |
2bd0ea18 | 2224 | /* |
5f6f3660 | 2225 | * if it's a version 2 inode, it should have a zero |
2bd0ea18 NS |
2226 | * onlink field, so clear it. |
2227 | */ | |
5f6f3660 | 2228 | if (dino->di_version > 1 && dino->di_onlink != 0) { |
0459b626 | 2229 | if (!no_modify) { |
5d1b7f0f CH |
2230 | do_warn( |
2231 | _("clearing obsolete nlink field in version 2 inode %" PRIu64 ", was %d, now 0\n"), | |
56b2de80 DC |
2232 | lino, be16_to_cpu(dino->di_onlink)); |
2233 | dino->di_onlink = 0; | |
0459b626 | 2234 | dirty = 1; |
2bd0ea18 | 2235 | } else { |
5d1b7f0f CH |
2236 | do_warn( |
2237 | _("would clear obsolete nlink field in version 2 inode %" PRIu64 ", currently %d\n"), | |
56b2de80 | 2238 | lino, be16_to_cpu(dino->di_onlink)); |
0459b626 BN |
2239 | } |
2240 | } | |
2241 | return dirty; | |
2242 | } | |
2243 | ||
8fec4f7c DW |
2244 | /* Check nanoseconds of a timestamp don't exceed 1 second. */ |
2245 | static void | |
2246 | check_nsec( | |
2247 | const char *name, | |
2248 | xfs_ino_t lino, | |
37c7dda1 | 2249 | struct xfs_dinode *dip, |
a252aadf | 2250 | xfs_timestamp_t *ts, |
8fec4f7c DW |
2251 | int *dirty) |
2252 | { | |
a252aadf DW |
2253 | struct xfs_legacy_timestamp *t; |
2254 | ||
37c7dda1 DW |
2255 | if (xfs_dinode_has_bigtime(dip)) |
2256 | return; | |
2257 | ||
a252aadf | 2258 | t = (struct xfs_legacy_timestamp *)ts; |
c0e58015 | 2259 | if (be32_to_cpu(t->t_nsec) < NSEC_PER_SEC) |
8fec4f7c DW |
2260 | return; |
2261 | ||
2262 | do_warn( | |
2263 | _("Bad %s nsec %u on inode %" PRIu64 ", "), name, be32_to_cpu(t->t_nsec), lino); | |
2264 | if (no_modify) { | |
2265 | do_warn(_("would reset to zero\n")); | |
2266 | } else { | |
2267 | do_warn(_("resetting to zero\n")); | |
2268 | t->t_nsec = 0; | |
2269 | *dirty = 1; | |
2270 | } | |
2271 | } | |
2272 | ||
5f062427 DW |
2273 | static void |
2274 | validate_extsize( | |
2275 | struct xfs_mount *mp, | |
2276 | struct xfs_dinode *dino, | |
2277 | xfs_ino_t lino, | |
2278 | int *dirty) | |
2279 | { | |
2280 | uint16_t flags = be16_to_cpu(dino->di_flags); | |
2281 | unsigned int value = be32_to_cpu(dino->di_extsize); | |
2282 | bool misaligned = false; | |
2283 | bool bad; | |
2284 | ||
2285 | /* | |
2286 | * XFS allows a sysadmin to change the rt extent size when adding a rt | |
2287 | * section to a filesystem after formatting. If there are any | |
2288 | * directories with extszinherit and rtinherit set, the hint could | |
2289 | * become misaligned with the new rextsize. The verifier doesn't check | |
2290 | * this, because we allow rtinherit directories even without an rt | |
2291 | * device. | |
2292 | */ | |
2293 | if ((flags & XFS_DIFLAG_EXTSZINHERIT) && | |
2294 | (flags & XFS_DIFLAG_RTINHERIT) && | |
2e62ddc4 | 2295 | xfs_extlen_to_rtxmod(mp, value) > 0) |
5f062427 DW |
2296 | misaligned = true; |
2297 | ||
2298 | /* | |
2299 | * Complain if the verifier fails. | |
2300 | * | |
2301 | * Old kernels didn't check the alignment of extsize hints when copying | |
2302 | * them to new regular realtime files. The inode verifier now checks | |
2303 | * the alignment (because misaligned hints cause misbehavior in the rt | |
2304 | * allocator), so we have to complain and fix them. | |
2305 | */ | |
2306 | bad = libxfs_inode_validate_extsize(mp, value, | |
2307 | be16_to_cpu(dino->di_mode), flags) != NULL; | |
2308 | if (bad || misaligned) { | |
2309 | do_warn( | |
2310 | _("Bad extent size hint %u on inode %" PRIu64 ", "), | |
2311 | value, lino); | |
2312 | if (!no_modify) { | |
2313 | do_warn(_("resetting to zero\n")); | |
2314 | dino->di_extsize = 0; | |
2315 | dino->di_flags &= ~cpu_to_be16(XFS_DIFLAG_EXTSIZE | | |
2316 | XFS_DIFLAG_EXTSZINHERIT); | |
2317 | *dirty = 1; | |
2318 | } else | |
2319 | do_warn(_("would reset to zero\n")); | |
2320 | } | |
2321 | } | |
2322 | ||
0459b626 BN |
2323 | /* |
2324 | * returns 0 if the inode is ok, 1 if the inode is corrupt | |
2325 | * check_dups can be set to 1 *only* when called by the | |
2326 | * first pass of the duplicate block checking of phase 4. | |
2327 | * *dirty is set > 0 if the dinode has been altered and | |
2328 | * needs to be written out. | |
2329 | * | |
2330 | * for detailed, info, look at process_dinode() comments. | |
2331 | */ | |
8b8a6b02 | 2332 | static int |
164a5514 DW |
2333 | process_dinode_int( |
2334 | struct xfs_mount *mp, | |
2335 | struct xfs_dinode **dinop, | |
2336 | xfs_agnumber_t agno, | |
2337 | xfs_agino_t ino, | |
2338 | int was_free, /* 1 if inode is currently free */ | |
2339 | int *dirty, /* out == > 0 if inode is now dirty */ | |
2340 | int *used, /* out == 1 if inode is in use */ | |
2341 | int verify_mode, /* 1 == verify but don't modify inode */ | |
2342 | int uncertain, /* 1 == inode is uncertain */ | |
2343 | int ino_discovery, /* 1 == check dirs for unknown inodes */ | |
2344 | int check_dups, /* 1 == check if inode claims duplicate blocks */ | |
2345 | int extra_attr_check, /* 1 == do attribute format and value checks */ | |
2346 | int *isa_dir, /* out == 1 if inode is a directory */ | |
2347 | xfs_ino_t *parent, /* out -- parent if ino is a dir */ | |
2348 | struct xfs_buf **ino_bpp) | |
0459b626 | 2349 | { |
5a35bf2c DC |
2350 | xfs_rfsblock_t totblocks = 0; |
2351 | xfs_rfsblock_t atotblocks = 0; | |
0459b626 BN |
2352 | int di_mode; |
2353 | int type; | |
2354 | int retval = 0; | |
3a2414fa CB |
2355 | xfs_extnum_t nextents; |
2356 | xfs_extnum_t anextents; | |
0459b626 BN |
2357 | xfs_ino_t lino; |
2358 | const int is_free = 0; | |
2359 | const int is_used = 1; | |
2360 | blkmap_t *dblkmap = NULL; | |
164a5514 | 2361 | struct xfs_dinode *dino = *dinop; |
6bd73d16 | 2362 | xfs_agino_t unlinked_ino; |
8aa34dc9 | 2363 | struct xfs_perag *pag; |
0459b626 BN |
2364 | |
2365 | *dirty = *isa_dir = 0; | |
2366 | *used = is_used; | |
2367 | type = XR_INO_UNKNOWN; | |
2368 | ||
0459b626 | 2369 | lino = XFS_AGINO_TO_INO(mp, agno, ino); |
56b2de80 | 2370 | di_mode = be16_to_cpu(dino->di_mode); |
0459b626 BN |
2371 | |
2372 | /* | |
2373 | * if in verify mode, don't modify the inode. | |
2374 | * | |
2375 | * if correcting, reset stuff that has known values | |
2376 | * | |
2377 | * if in uncertain mode, be silent on errors since we're | |
2378 | * trying to find out if these are inodes as opposed | |
2379 | * to assuming that they are. Just return the appropriate | |
2380 | * return code in that case. | |
2381 | * | |
2382 | * If uncertain is set, verify_mode MUST be set. | |
2383 | */ | |
2384 | ASSERT(uncertain == 0 || verify_mode != 0); | |
164a5514 | 2385 | ASSERT(ino_bpp != NULL || verify_mode != 0); |
0459b626 | 2386 | |
7d7c5553 ES |
2387 | /* |
2388 | * This is the only valid point to check the CRC; after this we may have | |
2389 | * made changes which invalidate it, and the CRC is only updated again | |
2390 | * when it gets written out. | |
2391 | * | |
2392 | * Of course if we make any modifications after this, the inode gets | |
2393 | * rewritten, and the CRC is updated automagically. | |
2394 | */ | |
2660e653 | 2395 | if (xfs_has_crc(mp) && |
e2f60652 | 2396 | !libxfs_verify_cksum((char *)dino, mp->m_sb.sb_inodesize, |
7d7c5553 ES |
2397 | XFS_DINODE_CRC_OFF)) { |
2398 | retval = 1; | |
2399 | if (!uncertain) | |
2400 | do_warn(_("bad CRC for inode %" PRIu64 "%c"), | |
2401 | lino, verify_mode ? '\n' : ','); | |
2402 | if (!verify_mode) { | |
2403 | if (!no_modify) { | |
2404 | do_warn(_(" will rewrite\n")); | |
2405 | *dirty = 1; | |
2406 | } else | |
2407 | do_warn(_(" would rewrite\n")); | |
2408 | } | |
2409 | } | |
2410 | ||
56b2de80 | 2411 | if (be16_to_cpu(dino->di_magic) != XFS_DINODE_MAGIC) { |
0459b626 BN |
2412 | retval = 1; |
2413 | if (!uncertain) | |
5d1b7f0f | 2414 | do_warn(_("bad magic number 0x%x on inode %" PRIu64 "%c"), |
56b2de80 | 2415 | be16_to_cpu(dino->di_magic), lino, |
0459b626 BN |
2416 | verify_mode ? '\n' : ','); |
2417 | if (!verify_mode) { | |
2418 | if (!no_modify) { | |
2419 | do_warn(_(" resetting magic number\n")); | |
56b2de80 | 2420 | dino->di_magic = cpu_to_be16(XFS_DINODE_MAGIC); |
0459b626 BN |
2421 | *dirty = 1; |
2422 | } else | |
2423 | do_warn(_(" would reset magic number\n")); | |
2424 | } | |
2425 | } | |
2426 | ||
03d8044d | 2427 | if (!libxfs_dinode_good_version(mp, dino->di_version)) { |
0459b626 BN |
2428 | retval = 1; |
2429 | if (!uncertain) | |
5d1b7f0f | 2430 | do_warn(_("bad version number 0x%x on inode %" PRIu64 "%c"), |
56b2de80 | 2431 | (__s8)dino->di_version, lino, |
0459b626 BN |
2432 | verify_mode ? '\n' : ','); |
2433 | if (!verify_mode) { | |
2434 | if (!no_modify) { | |
2435 | do_warn(_(" resetting version number\n")); | |
e0607266 | 2436 | dino->di_version = |
2660e653 | 2437 | xfs_has_crc(mp) ? 3 : 2; |
0459b626 BN |
2438 | *dirty = 1; |
2439 | } else | |
2440 | do_warn(_(" would reset version number\n")); | |
2441 | } | |
2442 | } | |
2443 | ||
6bd73d16 | 2444 | unlinked_ino = be32_to_cpu(dino->di_next_unlinked); |
8aa34dc9 DC |
2445 | pag = libxfs_perag_get(mp, agno); |
2446 | if (!xfs_verify_agino_or_null(pag, unlinked_ino)) { | |
6bd73d16 ES |
2447 | retval = 1; |
2448 | if (!uncertain) | |
2449 | do_warn(_("bad next_unlinked 0x%x on inode %" PRIu64 "%c"), | |
2450 | be32_to_cpu(dino->di_next_unlinked), lino, | |
2451 | verify_mode ? '\n' : ','); | |
2452 | if (!verify_mode) { | |
2453 | if (!no_modify) { | |
2454 | do_warn(_(" resetting next_unlinked\n")); | |
2455 | clear_dinode_unlinked(mp, dino); | |
2456 | *dirty = 1; | |
2457 | } else | |
2458 | do_warn(_(" would reset next_unlinked\n")); | |
2459 | } | |
2460 | } | |
8aa34dc9 | 2461 | libxfs_perag_put(pag); |
6bd73d16 | 2462 | |
e0607266 DC |
2463 | /* |
2464 | * We don't bother checking the CRC here - we cannot guarantee that when | |
2465 | * we are called here that the inode has not already been modified in | |
2466 | * memory and hence invalidated the CRC. | |
2467 | */ | |
2660e653 | 2468 | if (xfs_has_crc(mp)) { |
e0607266 DC |
2469 | if (be64_to_cpu(dino->di_ino) != lino) { |
2470 | if (!uncertain) | |
2471 | do_warn( | |
2472 | _("inode identifier %llu mismatch on inode %" PRIu64 "\n"), | |
51073f86 DW |
2473 | (unsigned long long)be64_to_cpu(dino->di_ino), |
2474 | lino); | |
e0607266 DC |
2475 | if (verify_mode) |
2476 | return 1; | |
2477 | goto clear_bad_out; | |
2478 | } | |
9c4e12fb ES |
2479 | if (platform_uuid_compare(&dino->di_uuid, |
2480 | &mp->m_sb.sb_meta_uuid)) { | |
e0607266 DC |
2481 | if (!uncertain) |
2482 | do_warn( | |
2483 | _("UUID mismatch on inode %" PRIu64 "\n"), lino); | |
2484 | if (verify_mode) | |
2485 | return 1; | |
2486 | goto clear_bad_out; | |
2487 | } | |
2488 | } | |
2489 | ||
0459b626 BN |
2490 | /* |
2491 | * blow out of here if the inode size is < 0 | |
2492 | */ | |
56b2de80 | 2493 | if ((xfs_fsize_t)be64_to_cpu(dino->di_size) < 0) { |
0459b626 | 2494 | if (!uncertain) |
5d1b7f0f CH |
2495 | do_warn( |
2496 | _("bad (negative) size %" PRId64 " on inode %" PRIu64 "\n"), | |
14f8b681 | 2497 | (int64_t)be64_to_cpu(dino->di_size), |
5d1b7f0f | 2498 | lino); |
0459b626 BN |
2499 | if (verify_mode) |
2500 | return 1; | |
2501 | goto clear_bad_out; | |
2502 | } | |
2503 | ||
2504 | /* | |
0724d0f4 | 2505 | * if not in verify mode, check to see if the inode and imap |
0459b626 BN |
2506 | * agree that the inode is free |
2507 | */ | |
2508 | if (!verify_mode && di_mode == 0) { | |
2509 | /* | |
2510 | * was_free value is not meaningful if we're in verify mode | |
2511 | */ | |
2512 | if (was_free) { | |
2513 | /* | |
2e1bf6c5 | 2514 | * easy case, inode free -- inode and map agree, check |
0459b626 BN |
2515 | * it just in case to ensure that format, etc. are |
2516 | * set correctly | |
2517 | */ | |
2e1bf6c5 ES |
2518 | if (libxfs_dinode_verify(mp, lino, dino) != NULL) { |
2519 | do_warn( | |
2520 | _("free inode %" PRIu64 " contains errors, "), lino); | |
2521 | if (!no_modify) { | |
0724d0f4 | 2522 | clear_dinode(mp, dino, lino); |
2e1bf6c5 | 2523 | do_warn(_("corrected\n")); |
0724d0f4 | 2524 | *dirty += 1; |
2e1bf6c5 ES |
2525 | } else { |
2526 | do_warn(_("would correct\n")); | |
2527 | } | |
2528 | } | |
0459b626 BN |
2529 | *used = is_free; |
2530 | return 0; | |
2531 | } | |
2532 | /* | |
2533 | * the inode looks free but the map says it's in use. | |
2534 | * clear the inode just to be safe and mark the inode | |
2535 | * free. | |
2536 | */ | |
5d1b7f0f CH |
2537 | do_warn( |
2538 | _("imap claims a free inode %" PRIu64 " is in use, "), lino); | |
0459b626 BN |
2539 | if (!no_modify) { |
2540 | do_warn(_("correcting imap and clearing inode\n")); | |
0724d0f4 ES |
2541 | clear_dinode(mp, dino, lino); |
2542 | *dirty += 1; | |
0459b626 BN |
2543 | retval = 1; |
2544 | } else | |
2545 | do_warn(_("would correct imap and clear inode\n")); | |
2546 | *used = is_free; | |
2547 | return retval; | |
2548 | } | |
2549 | ||
2550 | /* | |
2551 | * because of the lack of any write ordering guarantee, it's | |
2552 | * possible that the core got updated but the forks didn't. | |
2553 | * so rather than be ambitious (and probably incorrect), | |
2554 | * if there's an inconsistency, we get conservative and | |
2555 | * just pitch the file. blow off checking formats of | |
2556 | * free inodes since technically any format is legal | |
2557 | * as we reset the inode when we re-use it. | |
2558 | */ | |
56b2de80 | 2559 | if (di_mode != 0 && check_dinode_mode_format(dino) != 0) { |
0459b626 | 2560 | if (!uncertain) |
5d1b7f0f CH |
2561 | do_warn( |
2562 | _("bad inode format in inode %" PRIu64 "\n"), lino); | |
0459b626 BN |
2563 | if (verify_mode) |
2564 | return 1; | |
2565 | goto clear_bad_out; | |
2566 | } | |
2567 | ||
bd5683fe DC |
2568 | /* |
2569 | * check that we only have valid flags set, and those that are set make | |
2570 | * sense. | |
2571 | */ | |
56b2de80 DC |
2572 | if (dino->di_flags) { |
2573 | uint16_t flags = be16_to_cpu(dino->di_flags); | |
bd5683fe DC |
2574 | |
2575 | if (flags & ~XFS_DIFLAG_ANY) { | |
c98f31a5 CH |
2576 | if (!uncertain) { |
2577 | do_warn( | |
2578 | _("Bad flags set in inode %" PRIu64 "\n"), | |
2579 | lino); | |
2580 | } | |
5cd0710a | 2581 | flags &= XFS_DIFLAG_ANY; |
bd5683fe DC |
2582 | } |
2583 | ||
e8e72c93 DW |
2584 | /* need an rt-dev for the realtime flag! */ |
2585 | if ((flags & XFS_DIFLAG_REALTIME) && !rt_name) { | |
2586 | if (!uncertain) { | |
2587 | do_warn( | |
c98f31a5 | 2588 | _("inode %" PRIu64 " has RT flag set but there is no RT device\n"), |
e8e72c93 | 2589 | lino); |
bd5683fe | 2590 | } |
e8e72c93 | 2591 | flags &= ~XFS_DIFLAG_REALTIME; |
bd5683fe | 2592 | } |
fb380cb7 | 2593 | if (flags & XFS_DIFLAG_NEWRTBM) { |
bd5683fe DC |
2594 | /* must be a rt bitmap inode */ |
2595 | if (lino != mp->m_sb.sb_rbmino) { | |
c98f31a5 CH |
2596 | if (!uncertain) { |
2597 | do_warn( | |
2598 | _("inode %" PRIu64 " not rt bitmap\n"), | |
2599 | lino); | |
2600 | } | |
fb380cb7 | 2601 | flags &= ~XFS_DIFLAG_NEWRTBM; |
bd5683fe DC |
2602 | } |
2603 | } | |
2604 | if (flags & (XFS_DIFLAG_RTINHERIT | | |
2605 | XFS_DIFLAG_EXTSZINHERIT | | |
2606 | XFS_DIFLAG_PROJINHERIT | | |
2607 | XFS_DIFLAG_NOSYMLINKS)) { | |
2608 | /* must be a directory */ | |
2609 | if (di_mode && !S_ISDIR(di_mode)) { | |
c98f31a5 CH |
2610 | if (!uncertain) { |
2611 | do_warn( | |
2612 | _("directory flags set on non-directory inode %" PRIu64 "\n" ), | |
2613 | lino); | |
2614 | } | |
bd5683fe DC |
2615 | flags &= ~(XFS_DIFLAG_RTINHERIT | |
2616 | XFS_DIFLAG_EXTSZINHERIT | | |
2617 | XFS_DIFLAG_PROJINHERIT | | |
2618 | XFS_DIFLAG_NOSYMLINKS); | |
2619 | } | |
2620 | } | |
83f4b5ac | 2621 | if (flags & (XFS_DIFLAG_REALTIME | FS_XFLAG_EXTSIZE)) { |
bd5683fe DC |
2622 | /* must be a file */ |
2623 | if (di_mode && !S_ISREG(di_mode)) { | |
c98f31a5 CH |
2624 | if (!uncertain) { |
2625 | do_warn( | |
2626 | _("file flags set on non-file inode %" PRIu64 "\n"), | |
2627 | lino); | |
2628 | } | |
bd5683fe | 2629 | flags &= ~(XFS_DIFLAG_REALTIME | |
83f4b5ac | 2630 | FS_XFLAG_EXTSIZE); |
bd5683fe DC |
2631 | } |
2632 | } | |
56b2de80 | 2633 | if (!verify_mode && flags != be16_to_cpu(dino->di_flags)) { |
bd5683fe | 2634 | if (!no_modify) { |
5cd0710a | 2635 | do_warn(_("fixing bad flags.\n")); |
56b2de80 | 2636 | dino->di_flags = cpu_to_be16(flags); |
bd5683fe DC |
2637 | *dirty = 1; |
2638 | } else | |
5cd0710a | 2639 | do_warn(_("would fix bad flags.\n")); |
bd5683fe DC |
2640 | } |
2641 | } | |
2642 | ||
a406779b DW |
2643 | /* |
2644 | * check that we only have valid flags2 set, and those that are set make | |
2645 | * sense. | |
2646 | */ | |
2647 | if (dino->di_version >= 3) { | |
2648 | uint16_t flags = be16_to_cpu(dino->di_flags); | |
2649 | uint64_t flags2 = be64_to_cpu(dino->di_flags2); | |
2650 | ||
2651 | if (flags2 & ~XFS_DIFLAG2_ANY) { | |
2652 | if (!uncertain) { | |
2653 | do_warn( | |
2654 | _("Bad flags2 set in inode %" PRIu64 "\n"), | |
2655 | lino); | |
2656 | } | |
2657 | flags2 &= XFS_DIFLAG2_ANY; | |
2658 | } | |
2659 | ||
c1604fa4 DW |
2660 | if (flags2 & XFS_DIFLAG2_DAX) { |
2661 | /* must be a file or dir */ | |
2662 | if (di_mode && !(S_ISREG(di_mode) || S_ISDIR(di_mode))) { | |
2663 | if (!uncertain) { | |
2664 | do_warn( | |
2665 | _("DAX flag set on special inode %" PRIu64 "\n"), | |
2666 | lino); | |
2667 | } | |
2668 | flags2 &= ~XFS_DIFLAG2_DAX; | |
2669 | } | |
2670 | } | |
2671 | ||
a406779b | 2672 | if ((flags2 & XFS_DIFLAG2_REFLINK) && |
2660e653 | 2673 | !xfs_has_reflink(mp)) { |
a406779b DW |
2674 | if (!uncertain) { |
2675 | do_warn( | |
2676 | _("inode %" PRIu64 " is marked reflinked but file system does not support reflink\n"), | |
2677 | lino); | |
2678 | } | |
2679 | goto clear_bad_out; | |
2680 | } | |
2681 | ||
2682 | if (flags2 & XFS_DIFLAG2_REFLINK) { | |
2683 | /* must be a file */ | |
2684 | if (di_mode && !S_ISREG(di_mode)) { | |
2685 | if (!uncertain) { | |
2686 | do_warn( | |
2687 | _("reflink flag set on non-file inode %" PRIu64 "\n"), | |
2688 | lino); | |
2689 | } | |
2690 | goto clear_bad_out; | |
2691 | } | |
2692 | } | |
2693 | ||
2694 | if ((flags2 & XFS_DIFLAG2_REFLINK) && | |
2695 | (flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_RTINHERIT))) { | |
2696 | if (!uncertain) { | |
2697 | do_warn( | |
2698 | _("Cannot have a reflinked realtime inode %" PRIu64 "\n"), | |
2699 | lino); | |
2700 | } | |
2701 | goto clear_bad_out; | |
2702 | } | |
2703 | ||
42627ba6 | 2704 | if ((flags2 & XFS_DIFLAG2_COWEXTSIZE) && |
2660e653 | 2705 | !xfs_has_reflink(mp)) { |
42627ba6 DW |
2706 | if (!uncertain) { |
2707 | do_warn( | |
2708 | _("inode %" PRIu64 " has CoW extent size hint but file system does not support reflink\n"), | |
2709 | lino); | |
2710 | } | |
2711 | flags2 &= ~XFS_DIFLAG2_COWEXTSIZE; | |
2712 | } | |
2713 | ||
2714 | if (flags2 & XFS_DIFLAG2_COWEXTSIZE) { | |
2715 | /* must be a directory or file */ | |
2716 | if (di_mode && !S_ISDIR(di_mode) && !S_ISREG(di_mode)) { | |
2717 | if (!uncertain) { | |
2718 | do_warn( | |
2719 | _("CoW extent size flag set on non-file, non-directory inode %" PRIu64 "\n" ), | |
2720 | lino); | |
2721 | } | |
2722 | flags2 &= ~XFS_DIFLAG2_COWEXTSIZE; | |
2723 | } | |
2724 | } | |
2725 | ||
2726 | if ((flags2 & XFS_DIFLAG2_COWEXTSIZE) && | |
2727 | (flags & (XFS_DIFLAG_REALTIME | XFS_DIFLAG_RTINHERIT))) { | |
2728 | if (!uncertain) { | |
2729 | do_warn( | |
2730 | _("Cannot have CoW extent size hint on a realtime inode %" PRIu64 "\n"), | |
2731 | lino); | |
2732 | } | |
2733 | flags2 &= ~XFS_DIFLAG2_COWEXTSIZE; | |
2734 | } | |
2735 | ||
37c7dda1 | 2736 | if (xfs_dinode_has_bigtime(dino) && |
2660e653 | 2737 | !xfs_has_bigtime(mp)) { |
37c7dda1 DW |
2738 | if (!uncertain) { |
2739 | do_warn( | |
2740 | _("inode %" PRIu64 " is marked bigtime but file system does not support large timestamps\n"), | |
2741 | lino); | |
2742 | } | |
2743 | flags2 &= ~XFS_DIFLAG2_BIGTIME; | |
2744 | ||
2745 | if (no_modify) { | |
2746 | do_warn(_("would zero timestamps.\n")); | |
2747 | } else { | |
2748 | do_warn(_("zeroing timestamps.\n")); | |
2749 | dino->di_atime = 0; | |
2750 | dino->di_mtime = 0; | |
2751 | dino->di_ctime = 0; | |
2752 | dino->di_crtime = 0; | |
2753 | *dirty = 1; | |
2754 | } | |
2755 | } | |
2756 | ||
b83b2ec0 DW |
2757 | if (xfs_dinode_has_large_extent_counts(dino) && |
2758 | !xfs_has_large_extent_counts(mp)) { | |
2759 | if (!uncertain) { | |
2760 | do_warn( | |
2761 | _("inode %" PRIu64 " is marked large extent counts but file system does not support large extent counts\n"), | |
2762 | lino); | |
2763 | } | |
2764 | flags2 &= ~XFS_DIFLAG2_NREXT64; | |
2765 | ||
2766 | if (!no_modify) | |
2767 | *dirty = 1; | |
2768 | } | |
2769 | ||
0ec4cd64 DW |
2770 | if (xfs_dinode_has_large_extent_counts(dino)) { |
2771 | if (dino->di_nrext64_pad) { | |
2772 | if (!no_modify) { | |
2773 | do_warn(_("fixing bad nrext64_pad.\n")); | |
2774 | dino->di_nrext64_pad = 0; | |
2775 | *dirty = 1; | |
2776 | } else | |
2777 | do_warn(_("would fix bad nrext64_pad.\n")); | |
2778 | } | |
2779 | } else if (dino->di_version >= 3) { | |
2780 | if (dino->di_v3_pad) { | |
2781 | if (!no_modify) { | |
2782 | do_warn(_("fixing bad v3_pad.\n")); | |
2783 | dino->di_v3_pad = 0; | |
2784 | *dirty = 1; | |
2785 | } else | |
2786 | do_warn(_("would fix bad v3_pad.\n")); | |
2787 | } | |
2788 | } | |
2789 | ||
a406779b DW |
2790 | if (!verify_mode && flags2 != be64_to_cpu(dino->di_flags2)) { |
2791 | if (!no_modify) { | |
2792 | do_warn(_("fixing bad flags2.\n")); | |
2793 | dino->di_flags2 = cpu_to_be64(flags2); | |
2794 | *dirty = 1; | |
2795 | } else | |
2796 | do_warn(_("would fix bad flags2.\n")); | |
2797 | } | |
2798 | } | |
2799 | ||
0459b626 BN |
2800 | if (verify_mode) |
2801 | return retval; | |
2802 | ||
2803 | /* | |
2804 | * clear the next unlinked field if necessary on a good | |
2805 | * inode only during phase 4 -- when checking for inodes | |
2806 | * referencing duplicate blocks. then it's safe because | |
2807 | * we've done the inode discovery and have found all the inodes | |
2808 | * we're going to find. check_dups is set to 1 only during | |
2809 | * phase 4. Ugly. | |
2810 | */ | |
119383bf DC |
2811 | if (check_dups && be32_to_cpu(dino->di_next_unlinked) != NULLAGINO) { |
2812 | if (no_modify) { | |
2813 | do_warn( | |
2814 | _("Would clear next_unlinked in inode %" PRIu64 "\n"), lino); | |
2815 | } else { | |
2816 | clear_dinode_unlinked(mp, dino); | |
2817 | do_warn( | |
2818 | _("Cleared next_unlinked in inode %" PRIu64 "\n"), lino); | |
2819 | *dirty += 1; | |
2820 | } | |
0724d0f4 | 2821 | } |
0459b626 BN |
2822 | |
2823 | /* set type and map type info */ | |
2824 | ||
2825 | switch (di_mode & S_IFMT) { | |
2826 | case S_IFDIR: | |
2827 | type = XR_INO_DIR; | |
2828 | *isa_dir = 1; | |
2829 | break; | |
2830 | case S_IFREG: | |
56b2de80 | 2831 | if (be16_to_cpu(dino->di_flags) & XFS_DIFLAG_REALTIME) |
0459b626 BN |
2832 | type = XR_INO_RTDATA; |
2833 | else if (lino == mp->m_sb.sb_rbmino) | |
2834 | type = XR_INO_RTBITMAP; | |
2835 | else if (lino == mp->m_sb.sb_rsumino) | |
2836 | type = XR_INO_RTSUM; | |
5857dce9 ES |
2837 | else if (lino == mp->m_sb.sb_uquotino) |
2838 | type = XR_INO_UQUOTA; | |
2839 | else if (lino == mp->m_sb.sb_gquotino) | |
2840 | type = XR_INO_GQUOTA; | |
2841 | else if (lino == mp->m_sb.sb_pquotino) | |
2842 | type = XR_INO_PQUOTA; | |
0459b626 BN |
2843 | else |
2844 | type = XR_INO_DATA; | |
2845 | break; | |
2846 | case S_IFLNK: | |
2847 | type = XR_INO_SYMLINK; | |
2848 | break; | |
2849 | case S_IFCHR: | |
2850 | type = XR_INO_CHRDEV; | |
2851 | break; | |
2852 | case S_IFBLK: | |
2853 | type = XR_INO_BLKDEV; | |
2854 | break; | |
2855 | case S_IFSOCK: | |
2856 | type = XR_INO_SOCK; | |
2857 | break; | |
2858 | case S_IFIFO: | |
2859 | type = XR_INO_FIFO; | |
2860 | break; | |
2861 | default: | |
5d1b7f0f | 2862 | do_warn(_("bad inode type %#o inode %" PRIu64 "\n"), |
0459b626 BN |
2863 | di_mode & S_IFMT, lino); |
2864 | goto clear_bad_out; | |
2865 | } | |
2866 | ||
2867 | /* | |
2868 | * type checks for superblock inodes | |
2869 | */ | |
56b2de80 | 2870 | if (process_check_sb_inodes(mp, dino, lino, &type, dirty) != 0) |
0459b626 BN |
2871 | goto clear_bad_out; |
2872 | ||
5f062427 | 2873 | validate_extsize(mp, dino, lino, dirty); |
0459b626 | 2874 | |
42627ba6 DW |
2875 | /* |
2876 | * Only (regular files and directories) with COWEXTSIZE flags | |
2877 | * set can have extsize set. | |
2878 | */ | |
2879 | if (dino->di_version >= 3 && | |
db3d080b DW |
2880 | libxfs_inode_validate_cowextsize(mp, |
2881 | be32_to_cpu(dino->di_cowextsize), | |
2882 | be16_to_cpu(dino->di_mode), | |
2883 | be16_to_cpu(dino->di_flags), | |
2884 | be64_to_cpu(dino->di_flags2)) != NULL) { | |
1c744e94 | 2885 | do_warn( |
db3d080b DW |
2886 | _("Bad CoW extent size %u on inode %" PRIu64 ", "), |
2887 | be32_to_cpu(dino->di_cowextsize), lino); | |
1c744e94 | 2888 | if (!no_modify) { |
db3d080b | 2889 | do_warn(_("resetting to zero\n")); |
1c744e94 | 2890 | dino->di_flags2 &= ~cpu_to_be64(XFS_DIFLAG2_COWEXTSIZE); |
db3d080b | 2891 | dino->di_cowextsize = 0; |
1c744e94 | 2892 | *dirty = 1; |
db3d080b DW |
2893 | } else |
2894 | do_warn(_("would reset to zero\n")); | |
1c744e94 DW |
2895 | } |
2896 | ||
8fec4f7c | 2897 | /* nsec fields cannot be larger than 1 billion */ |
37c7dda1 DW |
2898 | check_nsec("atime", lino, dino, &dino->di_atime, dirty); |
2899 | check_nsec("mtime", lino, dino, &dino->di_mtime, dirty); | |
2900 | check_nsec("ctime", lino, dino, &dino->di_ctime, dirty); | |
8fec4f7c | 2901 | if (dino->di_version >= 3) |
37c7dda1 | 2902 | check_nsec("crtime", lino, dino, &dino->di_crtime, dirty); |
8fec4f7c | 2903 | |
0459b626 BN |
2904 | /* |
2905 | * general size/consistency checks: | |
2906 | */ | |
2907 | if (process_check_inode_sizes(mp, dino, lino, type) != 0) | |
2908 | goto clear_bad_out; | |
2909 | ||
2910 | /* | |
2911 | * check for illegal values of forkoff | |
2912 | */ | |
56b2de80 | 2913 | if (process_check_inode_forkoff(mp, dino, lino) != 0) |
0459b626 BN |
2914 | goto clear_bad_out; |
2915 | ||
7e174ec7 DW |
2916 | /* |
2917 | * record the state of the reflink flag | |
2918 | */ | |
2919 | if (collect_rmaps) | |
2920 | record_inode_reflink_flag(mp, dino, agno, ino, lino); | |
2921 | ||
0459b626 BN |
2922 | /* |
2923 | * check data fork -- if it's bad, clear the inode | |
2924 | */ | |
164a5514 DW |
2925 | if (process_inode_data_fork(mp, agno, ino, dinop, type, dirty, |
2926 | &totblocks, &nextents, &dblkmap, check_dups, | |
2927 | ino_bpp) != 0) | |
0459b626 | 2928 | goto bad_out; |
164a5514 | 2929 | dino = *dinop; |
0459b626 BN |
2930 | |
2931 | /* | |
2932 | * check attribute fork if necessary. attributes are | |
2933 | * always stored in the regular filesystem. | |
2934 | */ | |
164a5514 | 2935 | if (process_inode_attr_fork(mp, agno, ino, dinop, type, dirty, |
0459b626 | 2936 | &atotblocks, &anextents, check_dups, extra_attr_check, |
164a5514 | 2937 | &retval, ino_bpp)) |
0459b626 | 2938 | goto bad_out; |
164a5514 | 2939 | dino = *dinop; |
0459b626 BN |
2940 | |
2941 | /* | |
2942 | * enforce totblocks is 0 for misc types | |
2943 | */ | |
2944 | if (process_misc_ino_types_blocks(totblocks, lino, type)) | |
2945 | goto clear_bad_out; | |
2946 | ||
2947 | /* | |
2948 | * correct space counters if required | |
2949 | */ | |
56b2de80 | 2950 | if (process_inode_blocks_and_extents(dino, totblocks + atotblocks, |
0459b626 BN |
2951 | nextents, anextents, lino, dirty) != 0) |
2952 | goto clear_bad_out; | |
2953 | ||
2954 | /* | |
2955 | * do any semantic type-based checking here | |
2956 | */ | |
2957 | switch (type) { | |
2958 | case XR_INO_DIR: | |
9a048535 | 2959 | if (process_dir2(mp, lino, dino, ino_discovery, |
0459b626 | 2960 | dirty, "", parent, dblkmap)) { |
5d1b7f0f CH |
2961 | do_warn( |
2962 | _("problem with directory contents in inode %" PRIu64 "\n"), | |
2963 | lino); | |
0459b626 BN |
2964 | goto clear_bad_out; |
2965 | } | |
2966 | break; | |
2967 | case XR_INO_SYMLINK: | |
2968 | if (process_symlink(mp, lino, dino, dblkmap) != 0) { | |
5d1b7f0f CH |
2969 | do_warn( |
2970 | _("problem with symbolic link in inode %" PRIu64 "\n"), | |
0459b626 BN |
2971 | lino); |
2972 | goto clear_bad_out; | |
2bd0ea18 | 2973 | } |
0459b626 | 2974 | break; |
5857dce9 ES |
2975 | case XR_INO_UQUOTA: |
2976 | case XR_INO_GQUOTA: | |
2977 | case XR_INO_PQUOTA: | |
2978 | if (process_quota_inode(mp, lino, dino, type, dblkmap) != 0) { | |
2979 | do_warn( | |
2980 | _("problem with quota inode %" PRIu64 "\n"), lino); | |
2981 | goto clear_bad_out; | |
2982 | } | |
2983 | break; | |
0459b626 BN |
2984 | default: |
2985 | break; | |
2bd0ea18 NS |
2986 | } |
2987 | ||
55d35a39 | 2988 | blkmap_free(dblkmap); |
0459b626 BN |
2989 | |
2990 | /* | |
2991 | * check nlinks feature, if it's a version 1 inode, | |
2992 | * just leave nlinks alone. even if it's set wrong, | |
2993 | * it'll be reset when read in. | |
2994 | */ | |
56b2de80 | 2995 | *dirty += process_check_inode_nlink_version(dino, lino); |
0459b626 BN |
2996 | |
2997 | return retval; | |
2998 | ||
2999 | clear_bad_out: | |
3000 | if (!no_modify) { | |
0724d0f4 ES |
3001 | clear_dinode(mp, dino, lino); |
3002 | *dirty += 1; | |
0459b626 BN |
3003 | } |
3004 | bad_out: | |
3005 | *used = is_free; | |
3006 | *isa_dir = 0; | |
55d35a39 | 3007 | blkmap_free(dblkmap); |
0459b626 | 3008 | return 1; |
2bd0ea18 NS |
3009 | } |
3010 | ||
3011 | /* | |
3012 | * returns 1 if inode is used, 0 if free. | |
3013 | * performs any necessary salvaging actions. | |
3014 | * note that we leave the generation count alone | |
3015 | * because nothing we could set it to would be | |
3016 | * guaranteed to be correct so the best guess for | |
3017 | * the correct value is just to leave it alone. | |
3018 | * | |
3019 | * The trick is detecting empty files. For those, | |
3020 | * the core and the forks should all be in the "empty" | |
3021 | * or zero-length state -- a zero or possibly minimum length | |
3022 | * (in the case of dirs) extent list -- although inline directories | |
3023 | * and symlinks might be handled differently. So it should be | |
3024 | * possible to sanity check them against each other. | |
3025 | * | |
3026 | * If the forks are an empty extent list though, then forget it. | |
3027 | * The file is toast anyway since we can't recover its storage. | |
3028 | * | |
3029 | * Parameters: | |
3030 | * Ins: | |
3031 | * mp -- mount structure | |
3032 | * dino -- pointer to on-disk inode structure | |
3033 | * agno/ino -- inode numbers | |
3034 | * free -- whether the map thinks the inode is free (1 == free) | |
3035 | * ino_discovery -- whether we should examine directory | |
3036 | * contents to discover new inodes | |
3037 | * check_dups -- whether we should check to see if the | |
3038 | * inode references duplicate blocks | |
3039 | * if so, we compare the inode's claimed | |
3040 | * blocks against the contents of the | |
3041 | * duplicate extent list but we don't | |
3042 | * set the bitmap. If not, we set the | |
3043 | * bitmap and try and detect multiply | |
3044 | * claimed blocks using the bitmap. | |
3045 | * Outs: | |
3046 | * dirty -- whether we changed the inode (1 == yes) | |
2bd0ea18 NS |
3047 | * used -- 1 if the inode is used, 0 if free. In no modify |
3048 | * mode, whether the inode should be used or free | |
3049 | * isa_dir -- 1 if the inode is a directory, 0 if not. In | |
3050 | * no modify mode, if the inode would be a dir or not. | |
3051 | * | |
3052 | * Return value -- 0 if the inode is good, 1 if it is/was corrupt | |
3053 | */ | |
3054 | ||
3055 | int | |
0459b626 | 3056 | process_dinode( |
164a5514 DW |
3057 | struct xfs_mount *mp, |
3058 | struct xfs_dinode **dinop, | |
7328ea6e CH |
3059 | xfs_agnumber_t agno, |
3060 | xfs_agino_t ino, | |
3061 | int was_free, | |
3062 | int *dirty, | |
3063 | int *used, | |
3064 | int ino_discovery, | |
3065 | int check_dups, | |
3066 | int extra_attr_check, | |
3067 | int *isa_dir, | |
164a5514 DW |
3068 | xfs_ino_t *parent, |
3069 | struct xfs_buf **ino_bpp) | |
2bd0ea18 | 3070 | { |
7328ea6e CH |
3071 | const int verify_mode = 0; |
3072 | const int uncertain = 0; | |
2bd0ea18 NS |
3073 | |
3074 | #ifdef XR_INODE_TRACE | |
9ee7055c | 3075 | fprintf(stderr, _("processing inode %d/%d\n"), agno, ino); |
2bd0ea18 | 3076 | #endif |
164a5514 DW |
3077 | return process_dinode_int(mp, dinop, agno, ino, was_free, dirty, used, |
3078 | verify_mode, uncertain, ino_discovery, | |
3079 | check_dups, extra_attr_check, isa_dir, parent, | |
3080 | ino_bpp); | |
2bd0ea18 NS |
3081 | } |
3082 | ||
3083 | /* | |
3084 | * a more cursory check, check inode core, *DON'T* check forks | |
3085 | * this basically just verifies whether the inode is an inode | |
3086 | * and whether or not it has been totally trashed. returns 0 | |
3087 | * if the inode passes the cursory sanity check, 1 otherwise. | |
3088 | */ | |
3089 | int | |
0459b626 | 3090 | verify_dinode( |
7328ea6e CH |
3091 | xfs_mount_t *mp, |
3092 | struct xfs_dinode *dino, | |
3093 | xfs_agnumber_t agno, | |
3094 | xfs_agino_t ino) | |
2bd0ea18 | 3095 | { |
7328ea6e CH |
3096 | xfs_ino_t parent; |
3097 | int used = 0; | |
3098 | int dirty = 0; | |
3099 | int isa_dir = 0; | |
3100 | const int verify_mode = 1; | |
3101 | const int check_dups = 0; | |
3102 | const int ino_discovery = 0; | |
3103 | const int uncertain = 0; | |
0459b626 | 3104 | |
164a5514 DW |
3105 | return process_dinode_int(mp, &dino, agno, ino, 0, &dirty, &used, |
3106 | verify_mode, uncertain, ino_discovery, | |
3107 | check_dups, 0, &isa_dir, &parent, NULL); | |
2bd0ea18 NS |
3108 | } |
3109 | ||
3110 | /* | |
3111 | * like above only for inode on the uncertain list. it sets | |
3112 | * the uncertain flag which makes process_dinode_int quieter. | |
3113 | * returns 0 if the inode passes the cursory sanity check, 1 otherwise. | |
3114 | */ | |
3115 | int | |
0459b626 | 3116 | verify_uncertain_dinode( |
7328ea6e CH |
3117 | xfs_mount_t *mp, |
3118 | struct xfs_dinode *dino, | |
3119 | xfs_agnumber_t agno, | |
3120 | xfs_agino_t ino) | |
2bd0ea18 | 3121 | { |
7328ea6e CH |
3122 | xfs_ino_t parent; |
3123 | int used = 0; | |
3124 | int dirty = 0; | |
3125 | int isa_dir = 0; | |
3126 | const int verify_mode = 1; | |
3127 | const int check_dups = 0; | |
3128 | const int ino_discovery = 0; | |
3129 | const int uncertain = 1; | |
0459b626 | 3130 | |
164a5514 | 3131 | return process_dinode_int(mp, &dino, agno, ino, 0, &dirty, &used, |
0459b626 | 3132 | verify_mode, uncertain, ino_discovery, |
164a5514 | 3133 | check_dups, 0, &isa_dir, &parent, NULL); |
2bd0ea18 | 3134 | } |