Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Copyright (C) 2018-2023 Oracle. All Rights Reserved.
4 : * Author: Darrick J. Wong <djwong@kernel.org>
5 : */
6 : #include "xfs.h"
7 : #include "xfs_fs.h"
8 : #include "xfs_shared.h"
9 : #include "xfs_format.h"
10 : #include "xfs_trans_resv.h"
11 : #include "xfs_mount.h"
12 : #include "xfs_defer.h"
13 : #include "xfs_btree.h"
14 : #include "xfs_bit.h"
15 : #include "xfs_log_format.h"
16 : #include "xfs_trans.h"
17 : #include "xfs_sb.h"
18 : #include "xfs_inode.h"
19 : #include "xfs_icache.h"
20 : #include "xfs_inode_buf.h"
21 : #include "xfs_inode_fork.h"
22 : #include "xfs_ialloc.h"
23 : #include "xfs_da_format.h"
24 : #include "xfs_reflink.h"
25 : #include "xfs_alloc.h"
26 : #include "xfs_rmap.h"
27 : #include "xfs_rmap_btree.h"
28 : #include "xfs_bmap.h"
29 : #include "xfs_bmap_btree.h"
30 : #include "xfs_bmap_util.h"
31 : #include "xfs_dir2.h"
32 : #include "xfs_dir2_priv.h"
33 : #include "xfs_quota_defs.h"
34 : #include "xfs_quota.h"
35 : #include "xfs_ag.h"
36 : #include "xfs_attr_leaf.h"
37 : #include "xfs_log_priv.h"
38 : #include "xfs_symlink_remote.h"
39 : #include "xfs_rtbitmap.h"
40 : #include "xfs_rtgroup.h"
41 : #include "xfs_rtrmap_btree.h"
42 : #include "xfs_rtrefcount_btree.h"
43 : #include "scrub/xfs_scrub.h"
44 : #include "scrub/scrub.h"
45 : #include "scrub/common.h"
46 : #include "scrub/btree.h"
47 : #include "scrub/trace.h"
48 : #include "scrub/repair.h"
49 :
50 : /*
51 : * Inode Repair
52 : *
53 : * Roughly speaking, inode problems can be classified based on whether or not
54 : * they trip the dinode verifiers. If those trip, then we won't be able to
55 : * _iget ourselves the inode.
56 : *
57 : * Therefore, the xrep_dinode_* functions fix anything that will cause the
58 : * inode buffer verifier or the dinode verifier. The xrep_inode_* functions
59 : * fix things on live incore inodes.
60 : */
61 :
62 : /*
63 : * All the information we need to repair the ondisk inode if we can't iget the
64 : * incore inode. We don't allocate this buffer unless we're going to perform
65 : * a repair to the ondisk inode cluster buffer.
66 : */
67 : struct xrep_inode {
68 : /* Inode mapping that we saved from the initial lookup attempt. */
69 : struct xfs_imap imap;
70 :
71 : struct xfs_scrub *sc;
72 :
73 : /* Blocks in use on the data device by data extents or bmbt blocks. */
74 : xfs_rfsblock_t data_blocks;
75 :
76 : /* Blocks in use on the rt device. */
77 : xfs_rfsblock_t rt_blocks;
78 :
79 : /* Blocks in use by the attr fork. */
80 : xfs_rfsblock_t attr_blocks;
81 :
82 : /* Physical block containing data block 0. */
83 : xfs_fsblock_t block0;
84 :
85 : /* Number of data device extents for the data fork. */
86 : xfs_extnum_t data_extents;
87 :
88 : /*
89 : * Number of realtime device extents for the data fork. If
90 : * data_extents and rt_extents indicate that the data fork has extents
91 : * on both devices, we'll just back away slowly.
92 : */
93 : xfs_extnum_t rt_extents;
94 :
95 : /* Number of (data device) extents for the attr fork. */
96 : xfs_aextnum_t attr_extents;
97 : };
98 :
99 : /* Setup function for inode repair. */
100 : int
101 0 : xrep_setup_inode(
102 : struct xfs_scrub *sc,
103 : struct xfs_imap *imap)
104 : {
105 0 : struct xrep_inode *ri;
106 :
107 : /*
108 : * The only information that needs to be passed between inode scrub and
109 : * repair is the location of the ondisk metadata if iget fails. The
110 : * rest of struct xrep_inode is context data that we need to massage
111 : * the ondisk inode to the point that iget will work, which means that
112 : * we don't allocate anything at all if the incore inode is loaded.
113 : */
114 0 : if (!imap)
115 : return 0;
116 :
117 0 : sc->buf = kzalloc(sizeof(struct xrep_inode), XCHK_GFP_FLAGS);
118 0 : if (!sc->buf)
119 : return -ENOMEM;
120 :
121 0 : ri = sc->buf;
122 0 : memcpy(&ri->imap, imap, sizeof(struct xfs_imap));
123 0 : ri->sc = sc;
124 0 : ri->block0 = NULLFSBLOCK;
125 0 : return 0;
126 : }
127 :
128 : /* Make sure this inode cluster buffer can pass the inode buffer verifier. */
129 : STATIC void
130 0 : xrep_dinode_buf(
131 : struct xfs_scrub *sc,
132 : struct xfs_buf *bp)
133 : {
134 0 : struct xfs_mount *mp = sc->mp;
135 0 : struct xfs_trans *tp = sc->tp;
136 0 : struct xfs_perag *pag;
137 0 : struct xfs_dinode *dip;
138 0 : xfs_agnumber_t agno;
139 0 : xfs_agino_t agino;
140 0 : int ioff;
141 0 : int i;
142 0 : int ni;
143 0 : bool crc_ok;
144 0 : bool magic_ok;
145 0 : bool unlinked_ok;
146 :
147 0 : ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
148 0 : agno = xfs_daddr_to_agno(mp, xfs_buf_daddr(bp));
149 0 : pag = xfs_perag_get(mp, agno);
150 0 : for (i = 0; i < ni; i++) {
151 0 : ioff = i << mp->m_sb.sb_inodelog;
152 0 : dip = xfs_buf_offset(bp, ioff);
153 0 : agino = be32_to_cpu(dip->di_next_unlinked);
154 :
155 0 : unlinked_ok = magic_ok = crc_ok = false;
156 :
157 0 : if (xfs_verify_agino_or_null(pag, agino))
158 : unlinked_ok = true;
159 :
160 0 : if (dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
161 0 : xfs_dinode_good_version(mp, dip->di_version))
162 0 : magic_ok = true;
163 :
164 0 : if (xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
165 : XFS_DINODE_CRC_OFF))
166 0 : crc_ok = true;
167 :
168 0 : if (magic_ok && unlinked_ok && crc_ok)
169 0 : continue;
170 :
171 0 : if (!magic_ok) {
172 0 : dip->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
173 0 : dip->di_version = 3;
174 : }
175 0 : if (!unlinked_ok) {
176 0 : xfs_emerg(mp, "IUNLINK ino 0x%llx next_unlinked %x -> NULLAGINO", sc->sm->sm_ino, be32_to_cpu(dip->di_next_unlinked));
177 0 : dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
178 : }
179 0 : xfs_dinode_calc_crc(mp, dip);
180 0 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
181 0 : xfs_trans_log_buf(tp, bp, ioff, ioff + sizeof(*dip) - 1);
182 : }
183 0 : xfs_perag_put(pag);
184 0 : }
185 :
186 : /* Reinitialize things that never change in an inode. */
187 : STATIC void
188 0 : xrep_dinode_header(
189 : struct xfs_scrub *sc,
190 : struct xfs_dinode *dip)
191 : {
192 0 : trace_xrep_dinode_header(sc, dip);
193 :
194 0 : dip->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
195 0 : if (!xfs_dinode_good_version(sc->mp, dip->di_version))
196 0 : dip->di_version = 3;
197 0 : dip->di_ino = cpu_to_be64(sc->sm->sm_ino);
198 0 : uuid_copy(&dip->di_uuid, &sc->mp->m_sb.sb_meta_uuid);
199 0 : dip->di_gen = cpu_to_be32(sc->sm->sm_gen);
200 0 : }
201 :
202 : /* Parse enough of the directory block header to guess if this is a dir. */
203 : static inline bool
204 0 : xrep_dinode_is_dir(
205 : xfs_ino_t ino,
206 : xfs_daddr_t daddr,
207 : struct xfs_buf *bp)
208 : {
209 0 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
210 0 : struct xfs_dir2_data_free *bf;
211 0 : struct xfs_mount *mp = bp->b_mount;
212 0 : xfs_lsn_t lsn = be64_to_cpu(hdr3->lsn);
213 :
214 : /* Does the dir3 header match the filesystem? */
215 0 : if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC) &&
216 : hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
217 : return false;
218 :
219 0 : if (be64_to_cpu(hdr3->owner) != ino)
220 : return false;
221 :
222 0 : if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
223 : return false;
224 :
225 0 : if (be64_to_cpu(hdr3->blkno) != daddr)
226 : return false;
227 :
228 : /* Directory blocks are always logged and must have a valid LSN. */
229 0 : if (lsn == NULLCOMMITLSN)
230 : return false;
231 0 : if (!xlog_valid_lsn(mp->m_log, lsn))
232 : return false;
233 :
234 : /*
235 : * bestfree information lives immediately after the end of the header,
236 : * so we won't run off the end of the buffer.
237 : */
238 0 : bf = xfs_dir2_data_bestfree_p(mp, bp->b_addr);
239 0 : if (!bf[0].length && bf[0].offset)
240 : return false;
241 0 : if (!bf[1].length && bf[1].offset)
242 : return false;
243 0 : if (!bf[2].length && bf[2].offset)
244 : return false;
245 :
246 0 : if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
247 : return false;
248 0 : if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length))
249 0 : return false;
250 :
251 : return true;
252 : }
253 :
254 : /* Guess the mode of this file from the contents. */
255 : STATIC uint16_t
256 0 : xrep_dinode_guess_mode(
257 : struct xrep_inode *ri,
258 : struct xfs_dinode *dip)
259 : {
260 0 : struct xfs_buf *bp;
261 0 : struct xfs_mount *mp = ri->sc->mp;
262 0 : xfs_daddr_t daddr;
263 0 : uint64_t fsize = be64_to_cpu(dip->di_size);
264 0 : unsigned int dfork_sz = XFS_DFORK_DSIZE(dip, mp);
265 0 : uint16_t mode = S_IFREG;
266 0 : int error;
267 :
268 0 : switch (dip->di_format) {
269 0 : case XFS_DINODE_FMT_LOCAL:
270 : /*
271 : * If the data fork is local format, the size of the data area
272 : * is reasonable and is big enough to contain the entire file,
273 : * we can guess the file type from the local data.
274 : *
275 : * If there are no nulls, guess this is a symbolic link.
276 : * Otherwise, this is probably a shortform directory.
277 : */
278 0 : if (dfork_sz <= XFS_LITINO(mp) && dfork_sz >= fsize) {
279 0 : if (!memchr(XFS_DFORK_DPTR(dip), 0, fsize))
280 : return S_IFLNK;
281 0 : return S_IFDIR;
282 : }
283 :
284 : /* By default, we guess regular file. */
285 : return S_IFREG;
286 0 : case XFS_DINODE_FMT_DEV:
287 : /*
288 : * If the data fork is dev format, the size of the data area is
289 : * reasonable and large enough to store a dev_t, and the file
290 : * size is zero, this could be a blockdev, a chardev, a fifo,
291 : * or a socket. There is no solid way to distinguish between
292 : * those choices, so we guess blockdev if the device number is
293 : * nonzero and chardev if it's zero (aka whiteout).
294 : */
295 0 : if (dfork_sz <= XFS_LITINO(mp) &&
296 0 : dfork_sz >= sizeof(__be32) && fsize == 0) {
297 0 : xfs_dev_t dev = xfs_dinode_get_rdev(dip);
298 :
299 0 : return dev != 0 ? S_IFBLK : S_IFCHR;
300 : }
301 :
302 : /* By default, we guess regular file. */
303 : return S_IFREG;
304 : case XFS_DINODE_FMT_EXTENTS:
305 : case XFS_DINODE_FMT_BTREE:
306 : /* There are data blocks to examine below. */
307 0 : break;
308 : default:
309 : /* Everything else is considered a regular file. */
310 : return S_IFREG;
311 : }
312 :
313 : /* There are no zero-length directories. */
314 0 : if (fsize == 0)
315 : return S_IFREG;
316 :
317 : /*
318 : * If we didn't find a written mapping for file block zero, we'll guess
319 : * that it's a sparse regular file.
320 : */
321 0 : if (ri->block0 == NULLFSBLOCK)
322 : return S_IFREG;
323 :
324 : /* Directories can't have rt extents. */
325 0 : if (ri->rt_extents > 0)
326 : return S_IFREG;
327 :
328 : /*
329 : * Read the first block of the file. Since we have no idea what kind
330 : * of file geometry (e.g. dirblock size) we might be reading into, use
331 : * an uncached buffer so that we don't pollute the buffer cache. We
332 : * can't do uncached mapped buffers, so the best we can do is guess
333 : * from the directory header.
334 : */
335 0 : daddr = XFS_FSB_TO_DADDR(mp, ri->block0);
336 0 : error = xfs_buf_read_uncached(mp->m_ddev_targp, daddr,
337 0 : XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
338 0 : if (error)
339 : return S_IFREG;
340 :
341 0 : if (xrep_dinode_is_dir(ri->sc->sm->sm_ino, daddr, bp))
342 0 : mode = S_IFDIR;
343 :
344 0 : xfs_buf_relse(bp);
345 0 : return mode;
346 : }
347 :
348 : /* Turn di_mode into /something/ recognizable. */
349 : STATIC void
350 0 : xrep_dinode_mode(
351 : struct xrep_inode *ri,
352 : struct xfs_dinode *dip)
353 : {
354 0 : struct xfs_scrub *sc = ri->sc;
355 0 : uint16_t mode;
356 :
357 0 : trace_xrep_dinode_mode(sc, dip);
358 :
359 0 : mode = be16_to_cpu(dip->di_mode);
360 0 : if (mode == 0 || xfs_mode_to_ftype(mode) != XFS_DIR3_FT_UNKNOWN)
361 0 : return;
362 :
363 : /* bad mode, so we set it to a file that only root can read */
364 0 : mode = xrep_dinode_guess_mode(ri, dip);
365 0 : dip->di_mode = cpu_to_be16(mode);
366 0 : dip->di_uid = 0;
367 0 : dip->di_gid = 0;
368 : }
369 :
370 : /* Fix any conflicting flags that the verifiers complain about. */
371 : STATIC void
372 0 : xrep_dinode_flags(
373 : struct xfs_scrub *sc,
374 : struct xfs_dinode *dip,
375 : bool isrt)
376 : {
377 0 : struct xfs_mount *mp = sc->mp;
378 0 : uint64_t flags2;
379 0 : uint16_t mode;
380 0 : uint16_t flags;
381 :
382 0 : trace_xrep_dinode_flags(sc, dip);
383 :
384 0 : mode = be16_to_cpu(dip->di_mode);
385 0 : flags = be16_to_cpu(dip->di_flags);
386 0 : flags2 = be64_to_cpu(dip->di_flags2);
387 :
388 0 : if (isrt)
389 0 : flags |= XFS_DIFLAG_REALTIME;
390 : else
391 0 : flags &= ~XFS_DIFLAG_REALTIME;
392 :
393 0 : if (xfs_has_reflink(mp) && S_ISREG(mode))
394 0 : flags2 |= XFS_DIFLAG2_REFLINK;
395 : else
396 0 : flags2 &= ~(XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE);
397 0 : if (flags2 & XFS_DIFLAG2_REFLINK)
398 0 : flags2 &= ~XFS_DIFLAG2_DAX;
399 0 : if (!xfs_has_bigtime(mp))
400 0 : flags2 &= ~XFS_DIFLAG2_BIGTIME;
401 0 : if (!xfs_has_large_extent_counts(mp))
402 0 : flags2 &= ~XFS_DIFLAG2_NREXT64;
403 0 : if (flags2 & XFS_DIFLAG2_NREXT64)
404 0 : dip->di_nrext64_pad = 0;
405 0 : else if (dip->di_version >= 3)
406 0 : dip->di_v3_pad = 0;
407 :
408 0 : if (flags2 & XFS_DIFLAG2_METADIR) {
409 0 : xfs_failaddr_t fa;
410 :
411 0 : fa = xfs_dinode_verify_metadir(sc->mp, dip, mode, flags,
412 : flags2);
413 0 : if (fa)
414 0 : flags2 &= ~XFS_DIFLAG2_METADIR;
415 : }
416 :
417 0 : dip->di_flags = cpu_to_be16(flags);
418 0 : dip->di_flags2 = cpu_to_be64(flags2);
419 0 : }
420 :
421 : /*
422 : * Blow out symlink; now it points to the current dir. We don't have to worry
423 : * about incore state because this inode is failing the verifiers.
424 : */
425 : STATIC void
426 0 : xrep_dinode_zap_symlink(
427 : struct xfs_scrub *sc,
428 : struct xfs_dinode *dip)
429 : {
430 0 : char *p;
431 :
432 0 : trace_xrep_dinode_zap_symlink(sc, dip);
433 :
434 0 : dip->di_format = XFS_DINODE_FMT_LOCAL;
435 0 : dip->di_size = cpu_to_be64(1);
436 0 : p = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
437 0 : *p = '.';
438 0 : }
439 :
440 : /*
441 : * Blow out dir, make it point to the root. In the future repair will
442 : * reconstruct this directory for us. Note that there's no in-core directory
443 : * inode because the sf verifier tripped, so we don't have to worry about the
444 : * dentry cache.
445 : */
446 : STATIC void
447 0 : xrep_dinode_zap_dir(
448 : struct xfs_scrub *sc,
449 : struct xfs_dinode *dip)
450 : {
451 0 : struct xfs_mount *mp = sc->mp;
452 0 : struct xfs_dir2_sf_hdr *sfp;
453 0 : int i8count;
454 :
455 0 : trace_xrep_dinode_zap_dir(sc, dip);
456 :
457 0 : dip->di_format = XFS_DINODE_FMT_LOCAL;
458 0 : i8count = mp->m_sb.sb_rootino > XFS_DIR2_MAX_SHORT_INUM;
459 0 : sfp = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
460 0 : sfp->count = 0;
461 0 : sfp->i8count = i8count;
462 0 : xfs_dir2_sf_put_parent_ino(sfp, mp->m_sb.sb_rootino);
463 0 : dip->di_size = cpu_to_be64(xfs_dir2_sf_hdr_size(i8count));
464 0 : }
465 :
466 : /* Make sure we don't have a garbage file size. */
467 : STATIC void
468 0 : xrep_dinode_size(
469 : struct xfs_scrub *sc,
470 : struct xfs_dinode *dip)
471 : {
472 0 : uint64_t size;
473 0 : uint16_t mode;
474 :
475 0 : trace_xrep_dinode_size(sc, dip);
476 :
477 0 : mode = be16_to_cpu(dip->di_mode);
478 0 : size = be64_to_cpu(dip->di_size);
479 0 : switch (mode & S_IFMT) {
480 0 : case S_IFIFO:
481 : case S_IFCHR:
482 : case S_IFBLK:
483 : case S_IFSOCK:
484 : /* di_size can't be nonzero for special files */
485 0 : dip->di_size = 0;
486 0 : break;
487 0 : case S_IFREG:
488 : /* Regular files can't be larger than 2^63-1 bytes. */
489 0 : dip->di_size = cpu_to_be64(size & ~(1ULL << 63));
490 0 : break;
491 0 : case S_IFLNK:
492 : /*
493 : * Truncate ridiculously oversized symlinks. If the size is
494 : * zero, reset it to point to the current directory. Both of
495 : * these conditions trigger dinode verifier errors, so there
496 : * is no in-core state to reset.
497 : */
498 0 : if (size > XFS_SYMLINK_MAXLEN)
499 0 : dip->di_size = cpu_to_be64(XFS_SYMLINK_MAXLEN);
500 0 : else if (size == 0)
501 0 : xrep_dinode_zap_symlink(sc, dip);
502 : break;
503 0 : case S_IFDIR:
504 : /*
505 : * Directories can't have a size larger than 32G. If the size
506 : * is zero, reset it to an empty directory. Both of these
507 : * conditions trigger dinode verifier errors, so there is no
508 : * in-core state to reset.
509 : */
510 0 : if (size > XFS_DIR2_SPACE_SIZE)
511 0 : dip->di_size = cpu_to_be64(XFS_DIR2_SPACE_SIZE);
512 0 : else if (size == 0)
513 0 : xrep_dinode_zap_dir(sc, dip);
514 : break;
515 : }
516 0 : }
517 :
518 : /* Fix extent size hints. */
519 : STATIC void
520 0 : xrep_dinode_extsize_hints(
521 : struct xfs_scrub *sc,
522 : struct xfs_dinode *dip)
523 : {
524 0 : struct xfs_mount *mp = sc->mp;
525 0 : uint64_t flags2;
526 0 : uint16_t flags;
527 0 : uint16_t mode;
528 0 : xfs_failaddr_t fa;
529 :
530 0 : trace_xrep_dinode_extsize_hints(sc, dip);
531 :
532 0 : mode = be16_to_cpu(dip->di_mode);
533 0 : flags = be16_to_cpu(dip->di_flags);
534 0 : flags2 = be64_to_cpu(dip->di_flags2);
535 :
536 0 : fa = xfs_inode_validate_extsize(mp, be32_to_cpu(dip->di_extsize),
537 : mode, flags);
538 0 : if (fa) {
539 0 : dip->di_extsize = 0;
540 0 : dip->di_flags &= ~cpu_to_be16(XFS_DIFLAG_EXTSIZE |
541 : XFS_DIFLAG_EXTSZINHERIT);
542 : }
543 :
544 0 : if (dip->di_version < 3)
545 : return;
546 :
547 0 : fa = xfs_inode_validate_cowextsize(mp, be32_to_cpu(dip->di_cowextsize),
548 : mode, flags, flags2);
549 0 : if (fa) {
550 0 : dip->di_cowextsize = 0;
551 0 : dip->di_flags2 &= ~cpu_to_be64(XFS_DIFLAG2_COWEXTSIZE);
552 : }
553 : }
554 :
555 : /* Count extents and blocks for an inode given an rmap. */
556 : STATIC int
557 0 : xrep_dinode_walk_rmap(
558 : struct xfs_btree_cur *cur,
559 : const struct xfs_rmap_irec *rec,
560 : void *priv)
561 : {
562 0 : struct xrep_inode *ri = priv;
563 0 : int error = 0;
564 :
565 0 : if (xchk_should_terminate(ri->sc, &error))
566 0 : return error;
567 :
568 : /* We only care about this inode. */
569 0 : if (rec->rm_owner != ri->sc->sm->sm_ino)
570 : return 0;
571 :
572 0 : if (rec->rm_flags & XFS_RMAP_ATTR_FORK) {
573 0 : ri->attr_blocks += rec->rm_blockcount;
574 0 : if (!(rec->rm_flags & XFS_RMAP_BMBT_BLOCK))
575 0 : ri->attr_extents++;
576 :
577 0 : return 0;
578 : }
579 :
580 0 : ri->data_blocks += rec->rm_blockcount;
581 0 : if (!(rec->rm_flags & XFS_RMAP_BMBT_BLOCK)) {
582 0 : ri->data_extents++;
583 :
584 0 : if (rec->rm_offset == 0 &&
585 0 : !(rec->rm_flags & XFS_RMAP_UNWRITTEN)) {
586 0 : if (ri->block0 != NULLFSBLOCK)
587 : return -EFSCORRUPTED;
588 0 : ri->block0 = rec->rm_startblock;
589 : }
590 : }
591 :
592 : return 0;
593 : }
594 :
595 : /* Count extents and blocks for an inode from all AG rmap data. */
596 : STATIC int
597 0 : xrep_dinode_count_ag_rmaps(
598 : struct xrep_inode *ri,
599 : struct xfs_perag *pag)
600 : {
601 0 : struct xfs_btree_cur *cur;
602 0 : struct xfs_buf *agf;
603 0 : int error;
604 :
605 0 : error = xfs_alloc_read_agf(pag, ri->sc->tp, 0, &agf);
606 0 : if (error)
607 : return error;
608 :
609 0 : cur = xfs_rmapbt_init_cursor(ri->sc->mp, ri->sc->tp, agf, pag);
610 0 : error = xfs_rmap_query_all(cur, xrep_dinode_walk_rmap, ri);
611 0 : xfs_btree_del_cursor(cur, error);
612 0 : xfs_trans_brelse(ri->sc->tp, agf);
613 0 : return error;
614 : }
615 :
616 : /* Count extents and blocks for an inode given an rt rmap. */
617 : STATIC int
618 0 : xrep_dinode_walk_rtrmap(
619 : struct xfs_btree_cur *cur,
620 : const struct xfs_rmap_irec *rec,
621 : void *priv)
622 : {
623 0 : struct xrep_inode *ri = priv;
624 0 : int error = 0;
625 :
626 0 : if (xchk_should_terminate(ri->sc, &error))
627 0 : return error;
628 :
629 : /* We only care about this inode. */
630 0 : if (rec->rm_owner != ri->sc->sm->sm_ino)
631 : return 0;
632 :
633 0 : if (rec->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK))
634 : return -EFSCORRUPTED;
635 :
636 0 : ri->rt_blocks += rec->rm_blockcount;
637 0 : ri->rt_extents++;
638 0 : return 0;
639 : }
640 :
641 : /* Count extents and blocks for an inode from all realtime rmap data. */
642 : STATIC int
643 0 : xrep_dinode_count_rtgroup_rmaps(
644 : struct xrep_inode *ri,
645 : struct xfs_rtgroup *rtg)
646 : {
647 0 : struct xfs_scrub *sc = ri->sc;
648 0 : int error;
649 :
650 0 : if (!xfs_has_realtime(sc->mp) ||
651 0 : xrep_is_rtmeta_ino(sc, rtg, sc->sm->sm_ino))
652 0 : return 0;
653 :
654 0 : error = xrep_rtgroup_init(sc, rtg, &sc->sr, XFS_RTGLOCK_RMAP);
655 0 : if (error)
656 : return error;
657 :
658 0 : error = xfs_rmap_query_all(sc->sr.rmap_cur, xrep_dinode_walk_rtrmap,
659 : ri);
660 0 : xchk_rtgroup_btcur_free(&sc->sr);
661 0 : xchk_rtgroup_free(sc, &sc->sr);
662 0 : return error;
663 : }
664 :
665 : /* Count extents and blocks for a given inode from all rmap data. */
666 : STATIC int
667 0 : xrep_dinode_count_rmaps(
668 : struct xrep_inode *ri)
669 : {
670 0 : struct xfs_perag *pag;
671 0 : struct xfs_rtgroup *rtg;
672 0 : xfs_agnumber_t agno;
673 0 : xfs_rgnumber_t rgno;
674 0 : int error;
675 :
676 0 : if (!xfs_has_rmapbt(ri->sc->mp))
677 : return -EOPNOTSUPP;
678 :
679 0 : for_each_rtgroup(ri->sc->mp, rgno, rtg) {
680 0 : error = xrep_dinode_count_rtgroup_rmaps(ri, rtg);
681 0 : if (error) {
682 0 : xfs_rtgroup_rele(rtg);
683 0 : return error;
684 : }
685 : }
686 :
687 0 : for_each_perag(ri->sc->mp, agno, pag) {
688 0 : error = xrep_dinode_count_ag_rmaps(ri, pag);
689 0 : if (error) {
690 0 : xfs_perag_rele(pag);
691 0 : return error;
692 : }
693 : }
694 :
695 : /* Can't have extents on both the rt and the data device. */
696 0 : if (ri->data_extents && ri->rt_extents)
697 : return -EFSCORRUPTED;
698 :
699 0 : trace_xrep_dinode_count_rmaps(ri->sc,
700 : ri->data_blocks, ri->rt_blocks, ri->attr_blocks,
701 : ri->data_extents, ri->rt_extents, ri->attr_extents,
702 : ri->block0);
703 0 : return 0;
704 : }
705 :
706 : /* Return true if this extents-format ifork looks like garbage. */
707 : STATIC bool
708 0 : xrep_dinode_bad_extents_fork(
709 : struct xfs_scrub *sc,
710 : struct xfs_dinode *dip,
711 : int dfork_size,
712 : int whichfork)
713 : {
714 0 : struct xfs_bmbt_irec new;
715 0 : struct xfs_bmbt_rec *dp;
716 0 : bool isrt;
717 0 : int i;
718 0 : int nex;
719 0 : int fork_size;
720 :
721 0 : nex = xfs_dfork_nextents(dip, whichfork);
722 0 : fork_size = nex * sizeof(struct xfs_bmbt_rec);
723 0 : if (fork_size < 0 || fork_size > dfork_size)
724 : return true;
725 0 : if (whichfork == XFS_ATTR_FORK && nex > ((uint16_t)-1U))
726 : return true;
727 0 : dp = XFS_DFORK_PTR(dip, whichfork);
728 :
729 0 : isrt = dip->di_flags & cpu_to_be16(XFS_DIFLAG_REALTIME);
730 0 : for (i = 0; i < nex; i++, dp++) {
731 0 : xfs_failaddr_t fa;
732 :
733 0 : xfs_bmbt_disk_get_all(dp, &new);
734 0 : fa = xfs_bmap_validate_extent_raw(sc->mp, isrt, whichfork,
735 : &new);
736 0 : if (fa)
737 : return true;
738 : }
739 :
740 : return false;
741 : }
742 :
743 : /* Return true if this btree-format ifork looks like garbage. */
744 : STATIC bool
745 0 : xrep_dinode_bad_bmbt_fork(
746 : struct xfs_scrub *sc,
747 : struct xfs_dinode *dip,
748 : int dfork_size,
749 : int whichfork)
750 : {
751 0 : struct xfs_bmdr_block *dfp;
752 0 : int i;
753 0 : int dmxr;
754 0 : int nrecs;
755 0 : int level;
756 :
757 0 : if (xfs_dfork_nextents(dip, whichfork) <=
758 0 : dfork_size / sizeof(struct xfs_bmbt_rec))
759 : return true;
760 :
761 0 : if (dfork_size < sizeof(struct xfs_bmdr_block))
762 : return true;
763 :
764 0 : dfp = XFS_DFORK_PTR(dip, whichfork);
765 0 : nrecs = be16_to_cpu(dfp->bb_numrecs);
766 0 : level = be16_to_cpu(dfp->bb_level);
767 :
768 0 : if (nrecs == 0 || xfs_bmdr_space_calc(nrecs) > dfork_size)
769 : return true;
770 0 : if (level == 0 || level >= XFS_BM_MAXLEVELS(sc->mp, whichfork))
771 0 : return true;
772 :
773 0 : dmxr = xfs_bmdr_maxrecs(dfork_size, 0);
774 0 : for (i = 1; i <= nrecs; i++) {
775 0 : struct xfs_bmbt_key *fkp;
776 0 : xfs_bmbt_ptr_t *fpp;
777 0 : xfs_fileoff_t fileoff;
778 0 : xfs_fsblock_t fsbno;
779 :
780 0 : fkp = xfs_bmdr_key_addr(dfp, i);
781 0 : fileoff = be64_to_cpu(fkp->br_startoff);
782 0 : if (!xfs_verify_fileoff(sc->mp, fileoff))
783 : return true;
784 :
785 0 : fpp = xfs_bmdr_ptr_addr(dfp, i, dmxr);
786 0 : fsbno = be64_to_cpu(*fpp);
787 0 : if (!xfs_verify_fsbno(sc->mp, fsbno))
788 : return true;
789 : }
790 :
791 : return false;
792 : }
793 :
794 : /* Return true if this rmap-format ifork looks like garbage. */
795 : STATIC bool
796 0 : xrep_dinode_bad_rmapbt_fork(
797 : struct xfs_scrub *sc,
798 : struct xfs_dinode *dip,
799 : int dfork_size,
800 : int whichfork)
801 : {
802 0 : struct xfs_rtrmap_root *dfp;
803 0 : int nrecs;
804 0 : int level;
805 :
806 0 : if (whichfork != XFS_DATA_FORK)
807 : return true;
808 0 : if (dfork_size < sizeof(struct xfs_rtrmap_root))
809 : return true;
810 :
811 0 : dfp = XFS_DFORK_PTR(dip, whichfork);
812 0 : nrecs = be16_to_cpu(dfp->bb_numrecs);
813 0 : level = be16_to_cpu(dfp->bb_level);
814 :
815 0 : if (level > sc->mp->m_rtrmap_maxlevels)
816 : return true;
817 0 : if (xfs_rtrmap_droot_space_calc(level, nrecs) > dfork_size)
818 : return true;
819 0 : if (level > 0 && nrecs == 0)
820 0 : return true;
821 :
822 : return false;
823 : }
824 :
825 : /* Return true if this refcount-format ifork looks like garbage. */
826 : STATIC bool
827 0 : xrep_dinode_bad_refcountbt_fork(
828 : struct xfs_scrub *sc,
829 : struct xfs_dinode *dip,
830 : int dfork_size,
831 : int whichfork)
832 : {
833 0 : struct xfs_rtrefcount_root *dfp;
834 0 : int nrecs;
835 0 : int level;
836 :
837 0 : if (whichfork != XFS_DATA_FORK)
838 : return true;
839 0 : if (dfork_size < sizeof(struct xfs_rtrefcount_root))
840 : return true;
841 :
842 0 : dfp = XFS_DFORK_PTR(dip, whichfork);
843 0 : nrecs = be16_to_cpu(dfp->bb_numrecs);
844 0 : level = be16_to_cpu(dfp->bb_level);
845 :
846 0 : if (level > sc->mp->m_rtrefc_maxlevels)
847 : return true;
848 0 : if (xfs_rtrefcount_droot_space_calc(level, nrecs) > dfork_size)
849 : return true;
850 0 : if (level > 0 && nrecs == 0)
851 0 : return true;
852 :
853 : return false;
854 : }
855 :
856 : /*
857 : * Check the data fork for things that will fail the ifork verifiers or the
858 : * ifork formatters.
859 : */
860 : STATIC bool
861 0 : xrep_dinode_check_dfork(
862 : struct xfs_scrub *sc,
863 : struct xfs_dinode *dip,
864 : uint16_t mode)
865 : {
866 0 : void *dfork_ptr;
867 0 : int64_t data_size;
868 0 : unsigned int fmt;
869 0 : int dfork_size;
870 :
871 : /*
872 : * Verifier functions take signed int64_t, so check for bogus negative
873 : * values first.
874 : */
875 0 : data_size = be64_to_cpu(dip->di_size);
876 0 : if (data_size < 0)
877 : return true;
878 :
879 0 : fmt = XFS_DFORK_FORMAT(dip, XFS_DATA_FORK);
880 0 : switch (mode & S_IFMT) {
881 0 : case S_IFIFO:
882 : case S_IFCHR:
883 : case S_IFBLK:
884 : case S_IFSOCK:
885 0 : if (fmt != XFS_DINODE_FMT_DEV)
886 : return true;
887 : break;
888 0 : case S_IFREG:
889 0 : if (fmt == XFS_DINODE_FMT_LOCAL)
890 : return true;
891 0 : fallthrough;
892 : case S_IFLNK:
893 : case S_IFDIR:
894 0 : switch (fmt) {
895 : case XFS_DINODE_FMT_LOCAL:
896 : case XFS_DINODE_FMT_EXTENTS:
897 : case XFS_DINODE_FMT_BTREE:
898 : break;
899 : default:
900 : return true;
901 : }
902 : break;
903 : default:
904 : return true;
905 : }
906 :
907 0 : dfork_size = XFS_DFORK_SIZE(dip, sc->mp, XFS_DATA_FORK);
908 0 : dfork_ptr = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
909 :
910 0 : switch (fmt) {
911 : case XFS_DINODE_FMT_DEV:
912 : break;
913 0 : case XFS_DINODE_FMT_LOCAL:
914 : /* dir/symlink structure cannot be larger than the fork */
915 0 : if (data_size > dfork_size)
916 : return true;
917 : /* directory structure must pass verification. */
918 0 : if (S_ISDIR(mode) && xfs_dir2_sf_verify_struct(sc->mp,
919 : dfork_ptr, data_size) != NULL)
920 : return true;
921 : /* symlink structure must pass verification. */
922 0 : if (S_ISLNK(mode) && xfs_symlink_sf_verify_struct(dfork_ptr,
923 : data_size) != NULL)
924 0 : return true;
925 : break;
926 0 : case XFS_DINODE_FMT_EXTENTS:
927 0 : if (xrep_dinode_bad_extents_fork(sc, dip, dfork_size,
928 : XFS_DATA_FORK))
929 0 : return true;
930 : break;
931 0 : case XFS_DINODE_FMT_BTREE:
932 0 : if (xrep_dinode_bad_bmbt_fork(sc, dip, dfork_size,
933 : XFS_DATA_FORK))
934 0 : return true;
935 : break;
936 0 : case XFS_DINODE_FMT_RMAP:
937 0 : if (xrep_dinode_bad_rmapbt_fork(sc, dip, dfork_size,
938 : XFS_DATA_FORK))
939 0 : return true;
940 : break;
941 0 : case XFS_DINODE_FMT_REFCOUNT:
942 0 : if (xrep_dinode_bad_refcountbt_fork(sc, dip, dfork_size,
943 : XFS_DATA_FORK))
944 0 : return true;
945 : break;
946 : default:
947 : return true;
948 : }
949 :
950 : return false;
951 : }
952 :
953 : static void
954 0 : xrep_dinode_set_data_nextents(
955 : struct xfs_dinode *dip,
956 : xfs_extnum_t nextents)
957 : {
958 0 : if (xfs_dinode_has_large_extent_counts(dip))
959 0 : dip->di_big_nextents = cpu_to_be64(nextents);
960 : else
961 0 : dip->di_nextents = cpu_to_be32(nextents);
962 0 : }
963 :
964 : static void
965 0 : xrep_dinode_set_attr_nextents(
966 : struct xfs_dinode *dip,
967 : xfs_extnum_t nextents)
968 : {
969 0 : if (xfs_dinode_has_large_extent_counts(dip))
970 0 : dip->di_big_anextents = cpu_to_be32(nextents);
971 : else
972 0 : dip->di_anextents = cpu_to_be16(nextents);
973 0 : }
974 :
975 : /* Reset the data fork to something sane. */
976 : STATIC void
977 0 : xrep_dinode_zap_dfork(
978 : struct xrep_inode *ri,
979 : struct xfs_dinode *dip,
980 : uint16_t mode)
981 : {
982 0 : struct xfs_scrub *sc = ri->sc;
983 :
984 0 : trace_xrep_dinode_zap_dfork(sc, dip);
985 :
986 0 : xrep_dinode_set_data_nextents(dip, 0);
987 :
988 : /* Special files always get reset to DEV */
989 0 : switch (mode & S_IFMT) {
990 0 : case S_IFIFO:
991 : case S_IFCHR:
992 : case S_IFBLK:
993 : case S_IFSOCK:
994 0 : dip->di_format = XFS_DINODE_FMT_DEV;
995 0 : dip->di_size = 0;
996 0 : return;
997 : }
998 :
999 : /*
1000 : * If we have data extents, reset to an empty map and hope the user
1001 : * will run the bmapbtd checker next.
1002 : */
1003 0 : if (ri->data_extents || ri->rt_extents || S_ISREG(mode)) {
1004 0 : dip->di_format = XFS_DINODE_FMT_EXTENTS;
1005 0 : return;
1006 : }
1007 :
1008 : /* Otherwise, reset the local format to the minimum. */
1009 0 : switch (mode & S_IFMT) {
1010 0 : case S_IFLNK:
1011 0 : xrep_dinode_zap_symlink(sc, dip);
1012 0 : break;
1013 0 : case S_IFDIR:
1014 0 : xrep_dinode_zap_dir(sc, dip);
1015 0 : break;
1016 : }
1017 : }
1018 :
1019 : /*
1020 : * Check the attr fork for things that will fail the ifork verifiers or the
1021 : * ifork formatters.
1022 : */
1023 : STATIC bool
1024 0 : xrep_dinode_check_afork(
1025 : struct xfs_scrub *sc,
1026 : struct xfs_dinode *dip)
1027 : {
1028 0 : struct xfs_attr_shortform *afork_ptr;
1029 0 : size_t attr_size;
1030 0 : int afork_size;
1031 :
1032 0 : if (XFS_DFORK_BOFF(dip) == 0)
1033 0 : return dip->di_aformat != XFS_DINODE_FMT_EXTENTS ||
1034 0 : xfs_dfork_attr_extents(dip) != 0;
1035 :
1036 0 : afork_size = XFS_DFORK_SIZE(dip, sc->mp, XFS_ATTR_FORK);
1037 0 : afork_ptr = XFS_DFORK_PTR(dip, XFS_ATTR_FORK);
1038 :
1039 0 : switch (XFS_DFORK_FORMAT(dip, XFS_ATTR_FORK)) {
1040 0 : case XFS_DINODE_FMT_LOCAL:
1041 : /* Fork has to be large enough to extract the xattr size. */
1042 0 : if (afork_size < sizeof(struct xfs_attr_sf_hdr))
1043 : return true;
1044 :
1045 : /* xattr structure cannot be larger than the fork */
1046 0 : attr_size = be16_to_cpu(afork_ptr->hdr.totsize);
1047 0 : if (attr_size > afork_size)
1048 : return true;
1049 :
1050 : /* xattr structure must pass verification. */
1051 0 : return xfs_attr_shortform_verify_struct(afork_ptr,
1052 0 : attr_size) != NULL;
1053 0 : case XFS_DINODE_FMT_EXTENTS:
1054 0 : if (xrep_dinode_bad_extents_fork(sc, dip, afork_size,
1055 : XFS_ATTR_FORK))
1056 0 : return true;
1057 : break;
1058 0 : case XFS_DINODE_FMT_BTREE:
1059 0 : if (xrep_dinode_bad_bmbt_fork(sc, dip, afork_size,
1060 : XFS_ATTR_FORK))
1061 0 : return true;
1062 : break;
1063 0 : case XFS_DINODE_FMT_RMAP:
1064 0 : if (xrep_dinode_bad_rmapbt_fork(sc, dip, afork_size,
1065 : XFS_ATTR_FORK))
1066 0 : return true;
1067 : break;
1068 0 : case XFS_DINODE_FMT_REFCOUNT:
1069 0 : if (xrep_dinode_bad_refcountbt_fork(sc, dip, afork_size,
1070 : XFS_ATTR_FORK))
1071 0 : return true;
1072 : break;
1073 : default:
1074 : return true;
1075 : }
1076 :
1077 : return false;
1078 : }
1079 :
1080 : /*
1081 : * Reset the attr fork to empty. Since the attr fork could have contained
1082 : * ACLs, make the file readable only by root.
1083 : */
1084 : STATIC void
1085 0 : xrep_dinode_zap_afork(
1086 : struct xrep_inode *ri,
1087 : struct xfs_dinode *dip,
1088 : uint16_t mode)
1089 : {
1090 0 : struct xfs_scrub *sc = ri->sc;
1091 :
1092 0 : trace_xrep_dinode_zap_afork(sc, dip);
1093 :
1094 0 : dip->di_aformat = XFS_DINODE_FMT_EXTENTS;
1095 0 : xrep_dinode_set_attr_nextents(dip, 0);
1096 :
1097 : /*
1098 : * If the data fork is in btree format, removing the attr fork entirely
1099 : * might cause verifier failures if the next level down in the bmbt
1100 : * could now fit in the data fork area.
1101 : */
1102 0 : if (dip->di_format != XFS_DINODE_FMT_BTREE)
1103 0 : dip->di_forkoff = 0;
1104 0 : dip->di_mode = cpu_to_be16(mode & ~0777);
1105 0 : dip->di_uid = 0;
1106 0 : dip->di_gid = 0;
1107 0 : }
1108 :
1109 : /* Make sure the fork offset is a sensible value. */
1110 : STATIC void
1111 0 : xrep_dinode_ensure_forkoff(
1112 : struct xrep_inode *ri,
1113 : struct xfs_dinode *dip,
1114 : uint16_t mode)
1115 : {
1116 0 : struct xfs_bmdr_block *bmdr;
1117 0 : struct xfs_rtrmap_root *rmdr;
1118 0 : struct xfs_rtrefcount_root *rcdr;
1119 0 : struct xfs_scrub *sc = ri->sc;
1120 0 : xfs_extnum_t attr_extents, data_extents;
1121 0 : size_t bmdr_minsz = xfs_bmdr_space_calc(1);
1122 0 : unsigned int lit_sz = XFS_LITINO(sc->mp);
1123 0 : unsigned int afork_min, dfork_min;
1124 :
1125 0 : trace_xrep_dinode_ensure_forkoff(sc, dip);
1126 :
1127 : /*
1128 : * Before calling this function, xrep_dinode_core ensured that both
1129 : * forks actually fit inside their respective literal areas. If this
1130 : * was not the case, the fork was reset to FMT_EXTENTS with zero
1131 : * records. If the rmapbt scan found attr or data fork blocks, this
1132 : * will be noted in the dinode_stats, and we must leave enough room
1133 : * for the bmap repair code to reconstruct the mapping structure.
1134 : *
1135 : * First, compute the minimum space required for the attr fork.
1136 : */
1137 0 : switch (dip->di_aformat) {
1138 0 : case XFS_DINODE_FMT_LOCAL:
1139 : /*
1140 : * If we still have a shortform xattr structure at all, that
1141 : * means the attr fork area was exactly large enough to fit
1142 : * the sf structure.
1143 : */
1144 0 : afork_min = XFS_DFORK_SIZE(dip, sc->mp, XFS_ATTR_FORK);
1145 : break;
1146 0 : case XFS_DINODE_FMT_EXTENTS:
1147 0 : attr_extents = xfs_dfork_attr_extents(dip);
1148 0 : if (attr_extents) {
1149 : /*
1150 : * We must maintain sufficient space to hold the entire
1151 : * extent map array in the data fork. Note that we
1152 : * previously zapped the fork if it had no chance of
1153 : * fitting in the inode.
1154 : */
1155 0 : afork_min = sizeof(struct xfs_bmbt_rec) * attr_extents;
1156 0 : } else if (ri->attr_extents > 0) {
1157 : /*
1158 : * The attr fork thinks it has zero extents, but we
1159 : * found some xattr extents. We need to leave enough
1160 : * empty space here so that the incore attr fork will
1161 : * get created (and hence trigger the attr fork bmap
1162 : * repairer).
1163 : */
1164 : afork_min = bmdr_minsz;
1165 : } else {
1166 : /* No extents on disk or found in rmapbt. */
1167 0 : afork_min = 0;
1168 : }
1169 : break;
1170 0 : case XFS_DINODE_FMT_BTREE:
1171 : /* Must have space for btree header and key/pointers. */
1172 0 : bmdr = XFS_DFORK_PTR(dip, XFS_ATTR_FORK);
1173 0 : afork_min = xfs_bmap_broot_space(sc->mp, bmdr);
1174 0 : break;
1175 : default:
1176 : /* We should never see any other formats. */
1177 : afork_min = 0;
1178 : break;
1179 : }
1180 :
1181 : /* Compute the minimum space required for the data fork. */
1182 0 : switch (dip->di_format) {
1183 : case XFS_DINODE_FMT_DEV:
1184 : dfork_min = sizeof(__be32);
1185 : break;
1186 0 : case XFS_DINODE_FMT_UUID:
1187 0 : dfork_min = sizeof(uuid_t);
1188 0 : break;
1189 0 : case XFS_DINODE_FMT_LOCAL:
1190 : /*
1191 : * If we still have a shortform data fork at all, that means
1192 : * the data fork area was large enough to fit whatever was in
1193 : * there.
1194 : */
1195 0 : dfork_min = be64_to_cpu(dip->di_size);
1196 0 : break;
1197 0 : case XFS_DINODE_FMT_EXTENTS:
1198 0 : data_extents = xfs_dfork_data_extents(dip);
1199 0 : if (data_extents) {
1200 : /*
1201 : * We must maintain sufficient space to hold the entire
1202 : * extent map array in the data fork. Note that we
1203 : * previously zapped the fork if it had no chance of
1204 : * fitting in the inode.
1205 : */
1206 0 : dfork_min = sizeof(struct xfs_bmbt_rec) * data_extents;
1207 0 : } else if (ri->data_extents > 0 || ri->rt_extents > 0) {
1208 : /*
1209 : * The data fork thinks it has zero extents, but we
1210 : * found some data extents. We need to leave enough
1211 : * empty space here so that the the data fork bmap
1212 : * repair will recover the mappings.
1213 : */
1214 : dfork_min = bmdr_minsz;
1215 : } else {
1216 : /* No extents on disk or found in rmapbt. */
1217 0 : dfork_min = 0;
1218 : }
1219 : break;
1220 0 : case XFS_DINODE_FMT_BTREE:
1221 : /* Must have space for btree header and key/pointers. */
1222 0 : bmdr = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
1223 0 : dfork_min = xfs_bmap_broot_space(sc->mp, bmdr);
1224 0 : break;
1225 0 : case XFS_DINODE_FMT_RMAP:
1226 0 : rmdr = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
1227 0 : dfork_min = xfs_rtrmap_broot_space(sc->mp, rmdr);
1228 0 : break;
1229 0 : case XFS_DINODE_FMT_REFCOUNT:
1230 0 : rcdr = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
1231 0 : dfork_min = xfs_rtrefcount_broot_space(sc->mp, rcdr);
1232 0 : break;
1233 0 : default:
1234 0 : dfork_min = 0;
1235 0 : break;
1236 : }
1237 :
1238 : /*
1239 : * Round all values up to the nearest 8 bytes, because that is the
1240 : * precision of di_forkoff.
1241 : */
1242 0 : afork_min = roundup(afork_min, 8);
1243 0 : dfork_min = roundup(dfork_min, 8);
1244 0 : bmdr_minsz = roundup(bmdr_minsz, 8);
1245 :
1246 0 : ASSERT(dfork_min <= lit_sz);
1247 0 : ASSERT(afork_min <= lit_sz);
1248 :
1249 : /*
1250 : * If the data fork was zapped and we don't have enough space for the
1251 : * recovery fork, move the attr fork up.
1252 : */
1253 0 : if (dip->di_format == XFS_DINODE_FMT_EXTENTS &&
1254 0 : xfs_dfork_data_extents(dip) == 0 &&
1255 0 : (ri->data_extents > 0 || ri->rt_extents > 0) &&
1256 0 : bmdr_minsz > XFS_DFORK_DSIZE(dip, sc->mp)) {
1257 0 : if (bmdr_minsz + afork_min > lit_sz) {
1258 : /*
1259 : * The attr for and the stub fork we need to recover
1260 : * the data fork won't both fit. Zap the attr fork.
1261 : */
1262 0 : xrep_dinode_zap_afork(ri, dip, mode);
1263 0 : afork_min = bmdr_minsz;
1264 : } else {
1265 0 : void *before, *after;
1266 :
1267 : /* Otherwise, just slide the attr fork up. */
1268 0 : before = XFS_DFORK_APTR(dip);
1269 0 : dip->di_forkoff = bmdr_minsz >> 3;
1270 0 : after = XFS_DFORK_APTR(dip);
1271 0 : memmove(after, before, XFS_DFORK_ASIZE(dip, sc->mp));
1272 : }
1273 : }
1274 :
1275 : /*
1276 : * If the attr fork was zapped and we don't have enough space for the
1277 : * recovery fork, move the attr fork down.
1278 : */
1279 0 : if (dip->di_aformat == XFS_DINODE_FMT_EXTENTS &&
1280 0 : xfs_dfork_attr_extents(dip) == 0 &&
1281 0 : ri->attr_extents > 0 &&
1282 0 : bmdr_minsz > XFS_DFORK_ASIZE(dip, sc->mp)) {
1283 0 : if (dip->di_format == XFS_DINODE_FMT_BTREE) {
1284 : /*
1285 : * If the data fork is in btree format then we can't
1286 : * adjust forkoff because that runs the risk of
1287 : * violating the extents/btree format transition rules.
1288 : */
1289 0 : } else if (bmdr_minsz + dfork_min > lit_sz) {
1290 : /*
1291 : * If we can't move the attr fork, too bad, we lose the
1292 : * attr fork and leak its blocks.
1293 : */
1294 0 : xrep_dinode_zap_afork(ri, dip, mode);
1295 : } else {
1296 : /*
1297 : * Otherwise, just slide the attr fork down. The attr
1298 : * fork is empty, so we don't have any old contents to
1299 : * move here.
1300 : */
1301 0 : dip->di_forkoff = (lit_sz - bmdr_minsz) >> 3;
1302 : }
1303 : }
1304 0 : }
1305 :
1306 : /*
1307 : * Zap the data/attr forks if we spot anything that isn't going to pass the
1308 : * ifork verifiers or the ifork formatters, because we need to get the inode
1309 : * into good enough shape that the higher level repair functions can run.
1310 : */
1311 : STATIC void
1312 0 : xrep_dinode_zap_forks(
1313 : struct xrep_inode *ri,
1314 : struct xfs_dinode *dip)
1315 : {
1316 0 : struct xfs_scrub *sc = ri->sc;
1317 0 : xfs_extnum_t data_extents;
1318 0 : xfs_extnum_t attr_extents;
1319 0 : xfs_filblks_t nblocks;
1320 0 : uint16_t mode;
1321 0 : bool zap_datafork = false;
1322 0 : bool zap_attrfork = false;
1323 :
1324 0 : trace_xrep_dinode_zap_forks(sc, dip);
1325 :
1326 0 : mode = be16_to_cpu(dip->di_mode);
1327 :
1328 0 : data_extents = xfs_dfork_data_extents(dip);
1329 0 : attr_extents = xfs_dfork_attr_extents(dip);
1330 0 : nblocks = be64_to_cpu(dip->di_nblocks);
1331 :
1332 : /* Inode counters don't make sense? */
1333 0 : if (data_extents > nblocks)
1334 0 : zap_datafork = true;
1335 0 : if (attr_extents > nblocks)
1336 0 : zap_attrfork = true;
1337 0 : if (data_extents + attr_extents > nblocks)
1338 0 : zap_datafork = zap_attrfork = true;
1339 :
1340 0 : if (!zap_datafork)
1341 0 : zap_datafork = xrep_dinode_check_dfork(sc, dip, mode);
1342 0 : if (!zap_attrfork)
1343 0 : zap_attrfork = xrep_dinode_check_afork(sc, dip);
1344 :
1345 : /* Zap whatever's bad. */
1346 0 : if (zap_attrfork)
1347 0 : xrep_dinode_zap_afork(ri, dip, mode);
1348 0 : if (zap_datafork)
1349 0 : xrep_dinode_zap_dfork(ri, dip, mode);
1350 0 : xrep_dinode_ensure_forkoff(ri, dip, mode);
1351 0 : dip->di_nblocks = 0;
1352 0 : if (!zap_attrfork)
1353 0 : be64_add_cpu(&dip->di_nblocks, ri->attr_blocks);
1354 0 : if (!zap_datafork) {
1355 0 : be64_add_cpu(&dip->di_nblocks, ri->data_blocks);
1356 0 : be64_add_cpu(&dip->di_nblocks, ri->rt_blocks);
1357 : }
1358 0 : }
1359 :
1360 : STATIC void
1361 0 : xrep_dinode_nlinks(
1362 : struct xfs_dinode *dip)
1363 : {
1364 0 : if (dip->di_version > 1)
1365 0 : dip->di_onlink = 0;
1366 : else
1367 0 : dip->di_nlink = 0;
1368 0 : }
1369 :
1370 : /* Inode didn't pass verifiers, so fix the raw buffer and retry iget. */
1371 : STATIC int
1372 0 : xrep_dinode_core(
1373 : struct xrep_inode *ri)
1374 : {
1375 0 : struct xfs_scrub *sc = ri->sc;
1376 0 : struct xfs_buf *bp;
1377 0 : struct xfs_dinode *dip;
1378 0 : xfs_ino_t ino = sc->sm->sm_ino;
1379 0 : int error;
1380 :
1381 : /* Figure out what this inode had mapped in both forks. */
1382 0 : error = xrep_dinode_count_rmaps(ri);
1383 0 : if (error)
1384 : return error;
1385 :
1386 : /* Read the inode cluster buffer. */
1387 0 : error = xfs_trans_read_buf(sc->mp, sc->tp, sc->mp->m_ddev_targp,
1388 0 : ri->imap.im_blkno, ri->imap.im_len, XBF_UNMAPPED, &bp,
1389 : NULL);
1390 0 : if (error)
1391 : return error;
1392 :
1393 : /* Make sure we can pass the inode buffer verifier. */
1394 0 : xrep_dinode_buf(sc, bp);
1395 0 : bp->b_ops = &xfs_inode_buf_ops;
1396 :
1397 : /* Fix everything the verifier will complain about. */
1398 0 : dip = xfs_buf_offset(bp, ri->imap.im_boffset);
1399 0 : xrep_dinode_header(sc, dip);
1400 0 : xrep_dinode_nlinks(dip);
1401 0 : xrep_dinode_mode(ri, dip);
1402 0 : xrep_dinode_flags(sc, dip, ri->rt_extents > 0);
1403 0 : xrep_dinode_size(sc, dip);
1404 0 : xrep_dinode_extsize_hints(sc, dip);
1405 0 : xrep_dinode_zap_forks(ri, dip);
1406 :
1407 : /* Write out the inode. */
1408 0 : trace_xrep_dinode_fixed(sc, dip);
1409 0 : xfs_dinode_calc_crc(sc->mp, dip);
1410 0 : xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_DINO_BUF);
1411 0 : xfs_trans_log_buf(sc->tp, bp, ri->imap.im_boffset,
1412 0 : ri->imap.im_boffset + sc->mp->m_sb.sb_inodesize - 1);
1413 :
1414 : /*
1415 : * Now that we've finished rewriting anything in the ondisk metadata
1416 : * that would prevent iget from giving us an incore inode, commit the
1417 : * inode cluster buffer updates and drop the AGI buffer that we've been
1418 : * holding since scrub setup.
1419 : */
1420 0 : error = xrep_trans_commit(sc);
1421 0 : if (error)
1422 : return error;
1423 :
1424 : /* Try again to load the inode. */
1425 0 : error = xchk_iget(sc, ino, &sc->ip);
1426 0 : if (error)
1427 : return error;
1428 :
1429 0 : xchk_ilock(sc, XFS_IOLOCK_EXCL);
1430 0 : error = xchk_trans_alloc(sc, 0);
1431 0 : if (error)
1432 : return error;
1433 :
1434 0 : error = xrep_ino_dqattach(sc);
1435 0 : if (error)
1436 : return error;
1437 :
1438 0 : xchk_ilock(sc, XFS_ILOCK_EXCL);
1439 0 : return 0;
1440 : }
1441 :
1442 : /* Fix everything xfs_dinode_verify cares about. */
1443 : STATIC int
1444 0 : xrep_dinode_problems(
1445 : struct xrep_inode *ri)
1446 : {
1447 0 : struct xfs_scrub *sc = ri->sc;
1448 0 : int error;
1449 :
1450 0 : error = xrep_dinode_core(ri);
1451 0 : if (error)
1452 : return error;
1453 :
1454 : /* We had to fix a totally busted inode, schedule quotacheck. */
1455 0 : if (XFS_IS_UQUOTA_ON(sc->mp))
1456 0 : xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
1457 0 : if (XFS_IS_GQUOTA_ON(sc->mp))
1458 0 : xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
1459 0 : if (XFS_IS_PQUOTA_ON(sc->mp))
1460 0 : xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
1461 :
1462 : return 0;
1463 : }
1464 :
1465 : /*
1466 : * Fix problems that the verifiers don't care about. In general these are
1467 : * errors that don't cause problems elsewhere in the kernel that we can easily
1468 : * detect, so we don't check them all that rigorously.
1469 : */
1470 :
1471 : /* Make sure block and extent counts are ok. */
1472 : STATIC int
1473 0 : xrep_inode_blockcounts(
1474 : struct xfs_scrub *sc)
1475 : {
1476 0 : struct xfs_ifork *ifp;
1477 0 : xfs_filblks_t count;
1478 0 : xfs_filblks_t acount;
1479 0 : xfs_extnum_t nextents;
1480 0 : int error;
1481 :
1482 0 : trace_xrep_inode_blockcounts(sc);
1483 :
1484 : /* Set data fork counters from the data fork mappings. */
1485 0 : error = xchk_inode_count_blocks(sc, XFS_DATA_FORK, &nextents, &count);
1486 0 : if (error)
1487 : return error;
1488 0 : if (xfs_has_reflink(sc->mp)) {
1489 : ; /* data fork blockcount can exceed physical storage */
1490 0 : } else if (XFS_IS_REALTIME_INODE(sc->ip)) {
1491 0 : if (count >= sc->mp->m_sb.sb_rblocks)
1492 : return -EFSCORRUPTED;
1493 : } else {
1494 0 : if (count >= sc->mp->m_sb.sb_dblocks)
1495 : return -EFSCORRUPTED;
1496 : }
1497 0 : error = xrep_ino_ensure_extent_count(sc, XFS_DATA_FORK, nextents);
1498 0 : if (error)
1499 : return error;
1500 0 : sc->ip->i_df.if_nextents = nextents;
1501 :
1502 : /* Set attr fork counters from the attr fork mappings. */
1503 0 : ifp = xfs_ifork_ptr(sc->ip, XFS_ATTR_FORK);
1504 0 : if (ifp) {
1505 0 : error = xchk_inode_count_blocks(sc, XFS_ATTR_FORK, &nextents,
1506 : &acount);
1507 0 : if (error)
1508 : return error;
1509 0 : if (count >= sc->mp->m_sb.sb_dblocks)
1510 : return -EFSCORRUPTED;
1511 0 : error = xrep_ino_ensure_extent_count(sc, XFS_ATTR_FORK,
1512 : nextents);
1513 0 : if (error)
1514 : return error;
1515 0 : ifp->if_nextents = nextents;
1516 : } else {
1517 0 : acount = 0;
1518 : }
1519 :
1520 0 : sc->ip->i_nblocks = count + acount;
1521 0 : return 0;
1522 : }
1523 :
1524 : /* Check for invalid uid/gid/prid. */
1525 : STATIC void
1526 0 : xrep_inode_ids(
1527 : struct xfs_scrub *sc)
1528 : {
1529 0 : trace_xrep_inode_ids(sc);
1530 :
1531 0 : if (i_uid_read(VFS_I(sc->ip)) == -1U) {
1532 0 : i_uid_write(VFS_I(sc->ip), 0);
1533 0 : VFS_I(sc->ip)->i_mode &= ~(S_ISUID | S_ISGID);
1534 0 : if (XFS_IS_UQUOTA_ON(sc->mp))
1535 0 : xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
1536 : }
1537 :
1538 0 : if (i_gid_read(VFS_I(sc->ip)) == -1U) {
1539 0 : i_gid_write(VFS_I(sc->ip), 0);
1540 0 : VFS_I(sc->ip)->i_mode &= ~(S_ISUID | S_ISGID);
1541 0 : if (XFS_IS_GQUOTA_ON(sc->mp))
1542 0 : xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
1543 : }
1544 :
1545 0 : if (sc->ip->i_projid == -1U) {
1546 0 : sc->ip->i_projid = 0;
1547 0 : if (XFS_IS_PQUOTA_ON(sc->mp))
1548 0 : xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
1549 : }
1550 0 : }
1551 :
1552 : static inline void
1553 : xrep_clamp_nsec(
1554 : struct timespec64 *ts)
1555 : {
1556 0 : ts->tv_nsec = clamp_t(long, ts->tv_nsec, 0, NSEC_PER_SEC);
1557 : }
1558 :
1559 : /* Nanosecond counters can't have more than 1 billion. */
1560 : STATIC void
1561 0 : xrep_inode_timestamps(
1562 : struct xfs_inode *ip)
1563 : {
1564 0 : xrep_clamp_nsec(&VFS_I(ip)->i_atime);
1565 0 : xrep_clamp_nsec(&VFS_I(ip)->i_mtime);
1566 0 : xrep_clamp_nsec(&VFS_I(ip)->i_ctime);
1567 0 : xrep_clamp_nsec(&ip->i_crtime);
1568 0 : }
1569 :
1570 : /* Fix inode flags that don't make sense together. */
1571 : STATIC void
1572 0 : xrep_inode_flags(
1573 : struct xfs_scrub *sc)
1574 : {
1575 0 : uint16_t mode;
1576 :
1577 0 : trace_xrep_inode_flags(sc);
1578 :
1579 0 : mode = VFS_I(sc->ip)->i_mode;
1580 :
1581 : /* Clear junk flags */
1582 0 : if (sc->ip->i_diflags & ~XFS_DIFLAG_ANY)
1583 0 : sc->ip->i_diflags &= ~XFS_DIFLAG_ANY;
1584 :
1585 : /* NEWRTBM only applies to realtime bitmaps */
1586 0 : if (sc->ip->i_ino == sc->mp->m_sb.sb_rbmino)
1587 0 : sc->ip->i_diflags |= XFS_DIFLAG_NEWRTBM;
1588 : else
1589 0 : sc->ip->i_diflags &= ~XFS_DIFLAG_NEWRTBM;
1590 :
1591 : /* These only make sense for directories. */
1592 0 : if (!S_ISDIR(mode))
1593 0 : sc->ip->i_diflags &= ~(XFS_DIFLAG_RTINHERIT |
1594 : XFS_DIFLAG_EXTSZINHERIT |
1595 : XFS_DIFLAG_PROJINHERIT |
1596 : XFS_DIFLAG_NOSYMLINKS);
1597 :
1598 : /* These only make sense for files. */
1599 0 : if (!S_ISREG(mode))
1600 0 : sc->ip->i_diflags &= ~(XFS_DIFLAG_REALTIME |
1601 : XFS_DIFLAG_EXTSIZE);
1602 :
1603 : /* These only make sense for non-rt files. */
1604 0 : if (sc->ip->i_diflags & XFS_DIFLAG_REALTIME)
1605 0 : sc->ip->i_diflags &= ~XFS_DIFLAG_FILESTREAM;
1606 :
1607 : /* Immutable and append only? Drop the append. */
1608 0 : if ((sc->ip->i_diflags & XFS_DIFLAG_IMMUTABLE) &&
1609 : (sc->ip->i_diflags & XFS_DIFLAG_APPEND))
1610 0 : sc->ip->i_diflags &= ~XFS_DIFLAG_APPEND;
1611 :
1612 : /* Clear junk flags. */
1613 0 : if (sc->ip->i_diflags2 & ~XFS_DIFLAG2_ANY)
1614 0 : sc->ip->i_diflags2 &= ~XFS_DIFLAG2_ANY;
1615 :
1616 : /* No reflink flag unless we support it and it's a file. */
1617 0 : if (!xfs_has_reflink(sc->mp) || !S_ISREG(mode))
1618 0 : sc->ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
1619 :
1620 : /* DAX only applies to files and dirs. */
1621 0 : if (!(S_ISREG(mode) || S_ISDIR(mode)))
1622 0 : sc->ip->i_diflags2 &= ~XFS_DIFLAG2_DAX;
1623 :
1624 : /* No mixing reflink and DAX yet. */
1625 0 : if (sc->ip->i_diflags2 & XFS_DIFLAG2_REFLINK)
1626 0 : sc->ip->i_diflags2 &= ~XFS_DIFLAG2_DAX;
1627 0 : }
1628 :
1629 : /*
1630 : * Fix size problems with block/node format directories. If we fail to find
1631 : * the extent list, just bail out and let the bmapbtd repair functions clean
1632 : * up that mess.
1633 : */
1634 : STATIC void
1635 0 : xrep_inode_blockdir_size(
1636 : struct xfs_scrub *sc)
1637 : {
1638 0 : struct xfs_iext_cursor icur;
1639 0 : struct xfs_bmbt_irec got;
1640 0 : struct xfs_ifork *ifp;
1641 0 : xfs_fileoff_t off;
1642 0 : int error;
1643 :
1644 0 : trace_xrep_inode_blockdir_size(sc);
1645 :
1646 : /* Find the last block before 32G; this is the dir size. */
1647 0 : error = xfs_iread_extents(sc->tp, sc->ip, XFS_DATA_FORK);
1648 0 : if (error)
1649 0 : return;
1650 :
1651 0 : ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
1652 0 : off = XFS_B_TO_FSB(sc->mp, XFS_DIR2_SPACE_SIZE);
1653 0 : if (!xfs_iext_lookup_extent_before(sc->ip, ifp, &off, &icur, &got)) {
1654 : /* zero-extents directory? */
1655 : return;
1656 : }
1657 :
1658 0 : off = got.br_startoff + got.br_blockcount;
1659 0 : sc->ip->i_disk_size = min_t(loff_t, XFS_DIR2_SPACE_SIZE,
1660 : XFS_FSB_TO_B(sc->mp, off));
1661 : }
1662 :
1663 : /* Fix size problems with short format directories. */
1664 : STATIC void
1665 0 : xrep_inode_sfdir_size(
1666 : struct xfs_scrub *sc)
1667 : {
1668 0 : struct xfs_ifork *ifp;
1669 :
1670 0 : trace_xrep_inode_sfdir_size(sc);
1671 :
1672 0 : ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
1673 0 : sc->ip->i_disk_size = ifp->if_bytes;
1674 0 : }
1675 :
1676 : /*
1677 : * Fix any irregularities in an inode's size now that we can iterate extent
1678 : * maps and access other regular inode data.
1679 : */
1680 : STATIC void
1681 0 : xrep_inode_size(
1682 : struct xfs_scrub *sc)
1683 : {
1684 0 : trace_xrep_inode_size(sc);
1685 :
1686 : /*
1687 : * Currently we only support fixing size on extents or btree format
1688 : * directories. Files can be any size and sizes for the other inode
1689 : * special types are fixed by xrep_dinode_size.
1690 : */
1691 0 : if (!S_ISDIR(VFS_I(sc->ip)->i_mode))
1692 : return;
1693 0 : switch (sc->ip->i_df.if_format) {
1694 0 : case XFS_DINODE_FMT_EXTENTS:
1695 : case XFS_DINODE_FMT_BTREE:
1696 0 : xrep_inode_blockdir_size(sc);
1697 0 : break;
1698 0 : case XFS_DINODE_FMT_LOCAL:
1699 0 : xrep_inode_sfdir_size(sc);
1700 0 : break;
1701 : }
1702 : }
1703 :
1704 : /* Fix extent size hint problems. */
1705 : STATIC void
1706 0 : xrep_inode_extsize(
1707 : struct xfs_scrub *sc)
1708 : {
1709 : /* Fix misaligned extent size hints on a directory. */
1710 0 : if ((sc->ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
1711 0 : (sc->ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) &&
1712 0 : xfs_extlen_to_rtxmod(sc->mp, sc->ip->i_extsize) > 0) {
1713 0 : sc->ip->i_extsize = 0;
1714 0 : sc->ip->i_diflags &= ~XFS_DIFLAG_EXTSZINHERIT;
1715 : }
1716 0 : }
1717 :
1718 : /* Fix COW extent size hint problems. */
1719 : STATIC void
1720 0 : xrep_inode_cowextsize(
1721 : struct xfs_scrub *sc)
1722 : {
1723 : /* Fix misaligned CoW extent size hints on a directory. */
1724 0 : if ((sc->ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
1725 0 : (sc->ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) &&
1726 0 : sc->ip->i_extsize % sc->mp->m_sb.sb_rextsize > 0) {
1727 0 : sc->ip->i_cowextsize = 0;
1728 0 : sc->ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
1729 : }
1730 0 : }
1731 :
1732 : /* Fix any irregularities in an inode that the verifiers don't catch. */
1733 : STATIC int
1734 0 : xrep_inode_problems(
1735 : struct xfs_scrub *sc)
1736 : {
1737 0 : int error;
1738 :
1739 0 : error = xrep_inode_blockcounts(sc);
1740 0 : if (error)
1741 : return error;
1742 0 : xrep_inode_timestamps(sc->ip);
1743 0 : xrep_inode_flags(sc);
1744 0 : xrep_inode_ids(sc);
1745 0 : xrep_inode_size(sc);
1746 0 : xrep_inode_extsize(sc);
1747 0 : xrep_inode_cowextsize(sc);
1748 :
1749 0 : trace_xrep_inode_fixed(sc);
1750 0 : xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE);
1751 0 : return xrep_roll_trans(sc);
1752 : }
1753 :
1754 : /*
1755 : * Make sure this inode's unlinked list pointers are consistent with its
1756 : * link count.
1757 : */
1758 : STATIC int
1759 4034477 : xrep_inode_unlinked(
1760 : struct xfs_scrub *sc)
1761 : {
1762 4034477 : unsigned int nlink = VFS_I(sc->ip)->i_nlink;
1763 4034477 : int error;
1764 :
1765 : /*
1766 : * If this inode is linked from the directory tree and on the unlinked
1767 : * list, remove it from the unlinked list.
1768 : */
1769 4034477 : if (nlink > 0 && xfs_inode_on_unlinked_list(sc->ip)) {
1770 0 : struct xfs_perag *pag;
1771 0 : int error;
1772 :
1773 0 : pag = xfs_perag_get(sc->mp,
1774 0 : XFS_INO_TO_AGNO(sc->mp, sc->ip->i_ino));
1775 0 : error = xfs_iunlink_remove(sc->tp, pag, sc->ip);
1776 0 : xfs_perag_put(pag);
1777 0 : if (error)
1778 : return error;
1779 : }
1780 :
1781 : /*
1782 : * If this inode is not linked from the directory tree yet not on the
1783 : * unlinked list, put it on the unlinked list.
1784 : */
1785 4034477 : if (nlink == 0 && !xfs_inode_on_unlinked_list(sc->ip)) {
1786 0 : error = xfs_iunlink(sc->tp, sc->ip);
1787 0 : if (error)
1788 0 : return error;
1789 : }
1790 :
1791 : return 0;
1792 : }
1793 :
1794 : /* Repair an inode's fields. */
1795 : int
1796 4125584 : xrep_inode(
1797 : struct xfs_scrub *sc)
1798 : {
1799 4125584 : int error = 0;
1800 :
1801 : /*
1802 : * No inode? That means we failed the _iget verifiers. Repair all
1803 : * the things that the inode verifiers care about, then retry _iget.
1804 : */
1805 4125584 : if (!sc->ip) {
1806 0 : struct xrep_inode *ri = sc->buf;
1807 :
1808 0 : ASSERT(ri != NULL);
1809 :
1810 0 : error = xrep_dinode_problems(ri);
1811 0 : if (error)
1812 : return error;
1813 :
1814 : /* By this point we had better have a working incore inode. */
1815 0 : if (!sc->ip)
1816 : return -EFSCORRUPTED;
1817 : }
1818 :
1819 4125584 : xfs_trans_ijoin(sc->tp, sc->ip, 0);
1820 :
1821 : /* If we found corruption of any kind, try to fix it. */
1822 4125583 : if ((sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) ||
1823 : (sc->sm->sm_flags & XFS_SCRUB_OFLAG_XCORRUPT)) {
1824 0 : error = xrep_inode_problems(sc);
1825 0 : if (error)
1826 : return error;
1827 : }
1828 :
1829 : /* See if we can clear the reflink flag. */
1830 4125583 : if (xfs_is_reflink_inode(sc->ip))
1831 91098 : return xfs_reflink_clear_inode_flag(sc->ip, &sc->tp);
1832 :
1833 4034485 : return xrep_inode_unlinked(sc);
1834 : }
|