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