16b631c60SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-or-later 26b631c60SDarrick J. Wong /* 36b631c60SDarrick J. Wong * Copyright (c) 2021-2024 Oracle. All Rights Reserved. 46b631c60SDarrick J. Wong * Author: Darrick J. Wong <djwong@kernel.org> 56b631c60SDarrick J. Wong */ 66b631c60SDarrick J. Wong #include "xfs.h" 76b631c60SDarrick J. Wong #include "xfs_fs.h" 86b631c60SDarrick J. Wong #include "xfs_shared.h" 96b631c60SDarrick J. Wong #include "xfs_format.h" 106b631c60SDarrick J. Wong #include "xfs_trans_resv.h" 116b631c60SDarrick J. Wong #include "xfs_mount.h" 126b631c60SDarrick J. Wong #include "xfs_log_format.h" 136b631c60SDarrick J. Wong #include "xfs_trans.h" 146b631c60SDarrick J. Wong #include "xfs_inode.h" 156b631c60SDarrick J. Wong #include "xfs_icache.h" 166b631c60SDarrick J. Wong #include "xfs_bmap_util.h" 176b631c60SDarrick J. Wong #include "xfs_iwalk.h" 186b631c60SDarrick J. Wong #include "xfs_ialloc.h" 196b631c60SDarrick J. Wong #include "xfs_sb.h" 20669dfe88SDarrick J. Wong #include "xfs_ag.h" 2177ede5f4SDarrick J. Wong #include "xfs_dir2.h" 2277ede5f4SDarrick J. Wong #include "xfs_parent.h" 236b631c60SDarrick J. Wong #include "scrub/scrub.h" 246b631c60SDarrick J. Wong #include "scrub/common.h" 256b631c60SDarrick J. Wong #include "scrub/repair.h" 266b631c60SDarrick J. Wong #include "scrub/xfile.h" 276b631c60SDarrick J. Wong #include "scrub/xfarray.h" 286b631c60SDarrick J. Wong #include "scrub/iscan.h" 29e6c9e75fSDarrick J. Wong #include "scrub/orphanage.h" 306b631c60SDarrick J. Wong #include "scrub/nlinks.h" 316b631c60SDarrick J. Wong #include "scrub/trace.h" 32b1991ee3SDarrick J. Wong #include "scrub/tempfile.h" 336b631c60SDarrick J. Wong 346b631c60SDarrick J. Wong /* 356b631c60SDarrick J. Wong * Live Inode Link Count Repair 366b631c60SDarrick J. Wong * ============================ 376b631c60SDarrick J. Wong * 386b631c60SDarrick J. Wong * Use the live inode link count information that we collected to replace the 396b631c60SDarrick J. Wong * nlink values of the incore inodes. A scrub->repair cycle should have left 406b631c60SDarrick J. Wong * the live data and hooks active, so this is safe so long as we make sure the 416b631c60SDarrick J. Wong * inode is locked. 426b631c60SDarrick J. Wong */ 436b631c60SDarrick J. Wong 44e6c9e75fSDarrick J. Wong /* Set up to repair inode link counts. */ 45e6c9e75fSDarrick J. Wong int 46e6c9e75fSDarrick J. Wong xrep_setup_nlinks( 47e6c9e75fSDarrick J. Wong struct xfs_scrub *sc) 48e6c9e75fSDarrick J. Wong { 49e6c9e75fSDarrick J. Wong return xrep_orphanage_try_create(sc); 50e6c9e75fSDarrick J. Wong } 51e6c9e75fSDarrick J. Wong 52e6c9e75fSDarrick J. Wong /* 53e6c9e75fSDarrick J. Wong * Inodes that aren't the root directory or the orphanage, have a nonzero link 54e6c9e75fSDarrick J. Wong * count, and no observed parents should be moved to the orphanage. 55e6c9e75fSDarrick J. Wong */ 56e6c9e75fSDarrick J. Wong static inline bool 57e6c9e75fSDarrick J. Wong xrep_nlinks_is_orphaned( 58e6c9e75fSDarrick J. Wong struct xfs_scrub *sc, 59e6c9e75fSDarrick J. Wong struct xfs_inode *ip, 60e6c9e75fSDarrick J. Wong unsigned int actual_nlink, 61e6c9e75fSDarrick J. Wong const struct xchk_nlink *obs) 62e6c9e75fSDarrick J. Wong { 63e6c9e75fSDarrick J. Wong if (obs->parents != 0) 64e6c9e75fSDarrick J. Wong return false; 65*679b098bSDarrick J. Wong if (xchk_inode_is_dirtree_root(ip) || ip == sc->orphanage) 66e6c9e75fSDarrick J. Wong return false; 67e6c9e75fSDarrick J. Wong return actual_nlink != 0; 68e6c9e75fSDarrick J. Wong } 69e6c9e75fSDarrick J. Wong 70669dfe88SDarrick J. Wong /* Remove an inode from the unlinked list. */ 71669dfe88SDarrick J. Wong STATIC int 72669dfe88SDarrick J. Wong xrep_nlinks_iunlink_remove( 73669dfe88SDarrick J. Wong struct xfs_scrub *sc) 74669dfe88SDarrick J. Wong { 75669dfe88SDarrick J. Wong struct xfs_perag *pag; 76669dfe88SDarrick J. Wong int error; 77669dfe88SDarrick J. Wong 78669dfe88SDarrick J. Wong pag = xfs_perag_get(sc->mp, XFS_INO_TO_AGNO(sc->mp, sc->ip->i_ino)); 79669dfe88SDarrick J. Wong error = xfs_iunlink_remove(sc->tp, pag, sc->ip); 80669dfe88SDarrick J. Wong xfs_perag_put(pag); 81669dfe88SDarrick J. Wong return error; 82669dfe88SDarrick J. Wong } 83669dfe88SDarrick J. Wong 846b631c60SDarrick J. Wong /* 856b631c60SDarrick J. Wong * Correct the link count of the given inode. Because we have to grab locks 866b631c60SDarrick J. Wong * and resources in a certain order, it's possible that this will be a no-op. 876b631c60SDarrick J. Wong */ 886b631c60SDarrick J. Wong STATIC int 896b631c60SDarrick J. Wong xrep_nlinks_repair_inode( 906b631c60SDarrick J. Wong struct xchk_nlink_ctrs *xnc) 916b631c60SDarrick J. Wong { 926b631c60SDarrick J. Wong struct xchk_nlink obs; 936b631c60SDarrick J. Wong struct xfs_scrub *sc = xnc->sc; 946b631c60SDarrick J. Wong struct xfs_mount *mp = sc->mp; 956b631c60SDarrick J. Wong struct xfs_inode *ip = sc->ip; 966b631c60SDarrick J. Wong uint64_t total_links; 976b631c60SDarrick J. Wong uint64_t actual_nlink; 98e6c9e75fSDarrick J. Wong bool orphanage_available = false; 996b631c60SDarrick J. Wong bool dirty = false; 1006b631c60SDarrick J. Wong int error; 1016b631c60SDarrick J. Wong 102b1991ee3SDarrick J. Wong /* 103b1991ee3SDarrick J. Wong * Ignore temporary files being used to stage repairs, since we assume 104b1991ee3SDarrick J. Wong * they're correct for non-directories, and the directory repair code 105b1991ee3SDarrick J. Wong * doesn't bump the link counts for the children. 106b1991ee3SDarrick J. Wong */ 107b1991ee3SDarrick J. Wong if (xrep_is_tempfile(ip)) 108b1991ee3SDarrick J. Wong return 0; 109b1991ee3SDarrick J. Wong 110e6c9e75fSDarrick J. Wong /* 111e6c9e75fSDarrick J. Wong * If the filesystem has an orphanage attached to the scrub context, 112e6c9e75fSDarrick J. Wong * prepare for a link count repair that could involve @ip being adopted 113e6c9e75fSDarrick J. Wong * by the lost+found. 114e6c9e75fSDarrick J. Wong */ 115e6c9e75fSDarrick J. Wong if (xrep_orphanage_can_adopt(sc)) { 116e6c9e75fSDarrick J. Wong error = xrep_orphanage_iolock_two(sc); 117e6c9e75fSDarrick J. Wong if (error) 118e6c9e75fSDarrick J. Wong return error; 119e6c9e75fSDarrick J. Wong 120e6c9e75fSDarrick J. Wong error = xrep_adoption_trans_alloc(sc, &xnc->adoption); 121e6c9e75fSDarrick J. Wong if (error) { 122e6c9e75fSDarrick J. Wong xchk_iunlock(sc, XFS_IOLOCK_EXCL); 123e6c9e75fSDarrick J. Wong xrep_orphanage_iunlock(sc, XFS_IOLOCK_EXCL); 124e6c9e75fSDarrick J. Wong } else { 125e6c9e75fSDarrick J. Wong orphanage_available = true; 126e6c9e75fSDarrick J. Wong } 127e6c9e75fSDarrick J. Wong } 128e6c9e75fSDarrick J. Wong 129e6c9e75fSDarrick J. Wong /* 130e6c9e75fSDarrick J. Wong * Either there is no orphanage or we couldn't allocate resources for 131e6c9e75fSDarrick J. Wong * that kind of update. Let's try again with only the resources we 132e6c9e75fSDarrick J. Wong * need for a simple link count update, since that's much more common. 133e6c9e75fSDarrick J. Wong */ 134e6c9e75fSDarrick J. Wong if (!orphanage_available) { 1356b631c60SDarrick J. Wong xchk_ilock(sc, XFS_IOLOCK_EXCL); 1366b631c60SDarrick J. Wong 137e6c9e75fSDarrick J. Wong error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, 138e6c9e75fSDarrick J. Wong &sc->tp); 13966917537SDarrick J. Wong if (error) { 14066917537SDarrick J. Wong xchk_iunlock(sc, XFS_IOLOCK_EXCL); 1416b631c60SDarrick J. Wong return error; 14266917537SDarrick J. Wong } 1436b631c60SDarrick J. Wong 1446b631c60SDarrick J. Wong xchk_ilock(sc, XFS_ILOCK_EXCL); 1456b631c60SDarrick J. Wong xfs_trans_ijoin(sc->tp, ip, 0); 146e6c9e75fSDarrick J. Wong } 1476b631c60SDarrick J. Wong 1486b631c60SDarrick J. Wong mutex_lock(&xnc->lock); 1496b631c60SDarrick J. Wong 1506b631c60SDarrick J. Wong if (xchk_iscan_aborted(&xnc->collect_iscan)) { 1516b631c60SDarrick J. Wong error = -ECANCELED; 1526b631c60SDarrick J. Wong goto out_scanlock; 1536b631c60SDarrick J. Wong } 1546b631c60SDarrick J. Wong 1556b631c60SDarrick J. Wong error = xfarray_load_sparse(xnc->nlinks, ip->i_ino, &obs); 1566b631c60SDarrick J. Wong if (error) 1576b631c60SDarrick J. Wong goto out_scanlock; 1586b631c60SDarrick J. Wong 1596b631c60SDarrick J. Wong /* 1606b631c60SDarrick J. Wong * We're done accessing the shared scan data, so we can drop the lock. 1616b631c60SDarrick J. Wong * We still hold @ip's ILOCK, so its link count cannot change. 1626b631c60SDarrick J. Wong */ 1636b631c60SDarrick J. Wong mutex_unlock(&xnc->lock); 1646b631c60SDarrick J. Wong 1656b631c60SDarrick J. Wong total_links = xchk_nlink_total(ip, &obs); 1666b631c60SDarrick J. Wong actual_nlink = VFS_I(ip)->i_nlink; 1676b631c60SDarrick J. Wong 1686b631c60SDarrick J. Wong /* 1696b631c60SDarrick J. Wong * Non-directories cannot have directories pointing up to them. 1706b631c60SDarrick J. Wong * 1716b631c60SDarrick J. Wong * We previously set error to zero, but set it again because one static 1726b631c60SDarrick J. Wong * checker author fears that programmers will fail to maintain this 1736b631c60SDarrick J. Wong * invariant and built their tool to flag this as a security risk. A 1746b631c60SDarrick J. Wong * different tool author made their bot complain about the redundant 1756b631c60SDarrick J. Wong * store. This is a never-ending and stupid battle; both tools missed 1766b631c60SDarrick J. Wong * *actual bugs* elsewhere; and I no longer care. 1776b631c60SDarrick J. Wong */ 1786b631c60SDarrick J. Wong if (!S_ISDIR(VFS_I(ip)->i_mode) && obs.children != 0) { 1796b631c60SDarrick J. Wong trace_xrep_nlinks_unfixable_inode(mp, ip, &obs); 1806b631c60SDarrick J. Wong error = 0; 1816b631c60SDarrick J. Wong goto out_trans; 1826b631c60SDarrick J. Wong } 1836b631c60SDarrick J. Wong 1846b631c60SDarrick J. Wong /* 185e6c9e75fSDarrick J. Wong * Decide if we're going to move this file to the orphanage, and fix 186e6c9e75fSDarrick J. Wong * up the incore link counts if we are. 187e6c9e75fSDarrick J. Wong */ 188e6c9e75fSDarrick J. Wong if (orphanage_available && 189e6c9e75fSDarrick J. Wong xrep_nlinks_is_orphaned(sc, ip, actual_nlink, &obs)) { 190e6c9e75fSDarrick J. Wong /* Figure out what name we're going to use here. */ 191e6c9e75fSDarrick J. Wong error = xrep_adoption_compute_name(&xnc->adoption, &xnc->xname); 192e6c9e75fSDarrick J. Wong if (error) 193e6c9e75fSDarrick J. Wong goto out_trans; 194e6c9e75fSDarrick J. Wong 195e6c9e75fSDarrick J. Wong /* 196e6c9e75fSDarrick J. Wong * Reattach this file to the directory tree by moving it to 197e6c9e75fSDarrick J. Wong * the orphanage per the adoption parameters that we already 198e6c9e75fSDarrick J. Wong * computed. 199e6c9e75fSDarrick J. Wong */ 200e6c9e75fSDarrick J. Wong error = xrep_adoption_move(&xnc->adoption); 201e6c9e75fSDarrick J. Wong if (error) 202e6c9e75fSDarrick J. Wong goto out_trans; 203e6c9e75fSDarrick J. Wong 204e6c9e75fSDarrick J. Wong /* 205e6c9e75fSDarrick J. Wong * Re-read the link counts since the reparenting will have 206e6c9e75fSDarrick J. Wong * updated our scan info. 207e6c9e75fSDarrick J. Wong */ 208e6c9e75fSDarrick J. Wong mutex_lock(&xnc->lock); 209e6c9e75fSDarrick J. Wong error = xfarray_load_sparse(xnc->nlinks, ip->i_ino, &obs); 210e6c9e75fSDarrick J. Wong mutex_unlock(&xnc->lock); 211e6c9e75fSDarrick J. Wong if (error) 212e6c9e75fSDarrick J. Wong goto out_trans; 213e6c9e75fSDarrick J. Wong 214e6c9e75fSDarrick J. Wong total_links = xchk_nlink_total(ip, &obs); 215e6c9e75fSDarrick J. Wong actual_nlink = VFS_I(ip)->i_nlink; 216e6c9e75fSDarrick J. Wong dirty = true; 217e6c9e75fSDarrick J. Wong } 218e6c9e75fSDarrick J. Wong 219e6c9e75fSDarrick J. Wong /* 220669dfe88SDarrick J. Wong * If this inode is linked from the directory tree and on the unlinked 221669dfe88SDarrick J. Wong * list, remove it from the unlinked list. 2226b631c60SDarrick J. Wong */ 223669dfe88SDarrick J. Wong if (total_links > 0 && xfs_inode_on_unlinked_list(ip)) { 224669dfe88SDarrick J. Wong error = xrep_nlinks_iunlink_remove(sc); 225669dfe88SDarrick J. Wong if (error) 2266b631c60SDarrick J. Wong goto out_trans; 227669dfe88SDarrick J. Wong dirty = true; 228669dfe88SDarrick J. Wong } 229669dfe88SDarrick J. Wong 230669dfe88SDarrick J. Wong /* 231669dfe88SDarrick J. Wong * If this inode is not linked from the directory tree yet not on the 232669dfe88SDarrick J. Wong * unlinked list, put it on the unlinked list. 233669dfe88SDarrick J. Wong */ 234669dfe88SDarrick J. Wong if (total_links == 0 && !xfs_inode_on_unlinked_list(ip)) { 235669dfe88SDarrick J. Wong error = xfs_iunlink(sc->tp, ip); 236669dfe88SDarrick J. Wong if (error) 237669dfe88SDarrick J. Wong goto out_trans; 238669dfe88SDarrick J. Wong dirty = true; 2396b631c60SDarrick J. Wong } 2406b631c60SDarrick J. Wong 2416b631c60SDarrick J. Wong /* Commit the new link count if it changed. */ 2426b631c60SDarrick J. Wong if (total_links != actual_nlink) { 2436b631c60SDarrick J. Wong trace_xrep_nlinks_update_inode(mp, ip, &obs); 2446b631c60SDarrick J. Wong 2455f204051SDarrick J. Wong set_nlink(VFS_I(ip), min_t(unsigned long long, total_links, 2465f204051SDarrick J. Wong XFS_NLINK_PINNED)); 2476b631c60SDarrick J. Wong dirty = true; 2486b631c60SDarrick J. Wong } 2496b631c60SDarrick J. Wong 2506b631c60SDarrick J. Wong if (!dirty) { 2516b631c60SDarrick J. Wong error = 0; 2526b631c60SDarrick J. Wong goto out_trans; 2536b631c60SDarrick J. Wong } 2546b631c60SDarrick J. Wong 2556b631c60SDarrick J. Wong xfs_trans_log_inode(sc->tp, ip, XFS_ILOG_CORE); 2566b631c60SDarrick J. Wong 2576b631c60SDarrick J. Wong error = xrep_trans_commit(sc); 258e6c9e75fSDarrick J. Wong goto out_unlock; 2596b631c60SDarrick J. Wong 2606b631c60SDarrick J. Wong out_scanlock: 2616b631c60SDarrick J. Wong mutex_unlock(&xnc->lock); 2626b631c60SDarrick J. Wong out_trans: 2636b631c60SDarrick J. Wong xchk_trans_cancel(sc); 264e6c9e75fSDarrick J. Wong out_unlock: 265e6c9e75fSDarrick J. Wong xchk_iunlock(sc, XFS_ILOCK_EXCL); 266e6c9e75fSDarrick J. Wong if (orphanage_available) { 267e6c9e75fSDarrick J. Wong xrep_orphanage_iunlock(sc, XFS_ILOCK_EXCL); 268e6c9e75fSDarrick J. Wong xrep_orphanage_iunlock(sc, XFS_IOLOCK_EXCL); 269e6c9e75fSDarrick J. Wong } 270e6c9e75fSDarrick J. Wong xchk_iunlock(sc, XFS_IOLOCK_EXCL); 2716b631c60SDarrick J. Wong return error; 2726b631c60SDarrick J. Wong } 2736b631c60SDarrick J. Wong 2746b631c60SDarrick J. Wong /* 2756b631c60SDarrick J. Wong * Try to visit every inode in the filesystem for repairs. Move on if we can't 2766b631c60SDarrick J. Wong * grab an inode, since we're still making forward progress. 2776b631c60SDarrick J. Wong */ 2786b631c60SDarrick J. Wong static int 2796b631c60SDarrick J. Wong xrep_nlinks_iter( 2806b631c60SDarrick J. Wong struct xchk_nlink_ctrs *xnc, 2816b631c60SDarrick J. Wong struct xfs_inode **ipp) 2826b631c60SDarrick J. Wong { 2836b631c60SDarrick J. Wong int error; 2846b631c60SDarrick J. Wong 2856b631c60SDarrick J. Wong do { 2866b631c60SDarrick J. Wong error = xchk_iscan_iter(&xnc->compare_iscan, ipp); 2876b631c60SDarrick J. Wong } while (error == -EBUSY); 2886b631c60SDarrick J. Wong 2896b631c60SDarrick J. Wong return error; 2906b631c60SDarrick J. Wong } 2916b631c60SDarrick J. Wong 2926b631c60SDarrick J. Wong /* Commit the new inode link counters. */ 2936b631c60SDarrick J. Wong int 2946b631c60SDarrick J. Wong xrep_nlinks( 2956b631c60SDarrick J. Wong struct xfs_scrub *sc) 2966b631c60SDarrick J. Wong { 2976b631c60SDarrick J. Wong struct xchk_nlink_ctrs *xnc = sc->buf; 2986b631c60SDarrick J. Wong int error; 2996b631c60SDarrick J. Wong 3006b631c60SDarrick J. Wong /* 3016b631c60SDarrick J. Wong * We need ftype for an accurate count of the number of child 3026b631c60SDarrick J. Wong * subdirectory links. Child subdirectories with a back link (dotdot 303e6c9e75fSDarrick J. Wong * entry) but no forward link are moved to the orphanage, so we cannot 304e6c9e75fSDarrick J. Wong * repair the link count of the parent directory based on the back link 305e6c9e75fSDarrick J. Wong * count alone. Filesystems without ftype support are rare (old V4) so 306e6c9e75fSDarrick J. Wong * we just skip out here. 3076b631c60SDarrick J. Wong */ 3086b631c60SDarrick J. Wong if (!xfs_has_ftype(sc->mp)) 3096b631c60SDarrick J. Wong return -EOPNOTSUPP; 3106b631c60SDarrick J. Wong 3116b631c60SDarrick J. Wong /* 3126b631c60SDarrick J. Wong * Use the inobt to walk all allocated inodes to compare and fix the 3136b631c60SDarrick J. Wong * link counts. Retry iget every tenth of a second for up to 30 3146b631c60SDarrick J. Wong * seconds -- even if repair misses a few inodes, we still try to fix 3156b631c60SDarrick J. Wong * as many of them as we can. 3166b631c60SDarrick J. Wong */ 3176b631c60SDarrick J. Wong xchk_iscan_start(sc, 30000, 100, &xnc->compare_iscan); 3186b631c60SDarrick J. Wong ASSERT(sc->ip == NULL); 3196b631c60SDarrick J. Wong 3206b631c60SDarrick J. Wong while ((error = xrep_nlinks_iter(xnc, &sc->ip)) == 1) { 3216b631c60SDarrick J. Wong /* 3226b631c60SDarrick J. Wong * Commit the scrub transaction so that we can create repair 3236b631c60SDarrick J. Wong * transactions with the correct reservations. 3246b631c60SDarrick J. Wong */ 3256b631c60SDarrick J. Wong xchk_trans_cancel(sc); 3266b631c60SDarrick J. Wong 3276b631c60SDarrick J. Wong error = xrep_nlinks_repair_inode(xnc); 3286b631c60SDarrick J. Wong xchk_iscan_mark_visited(&xnc->compare_iscan, sc->ip); 3296b631c60SDarrick J. Wong xchk_irele(sc, sc->ip); 3306b631c60SDarrick J. Wong sc->ip = NULL; 3316b631c60SDarrick J. Wong if (error) 3326b631c60SDarrick J. Wong break; 3336b631c60SDarrick J. Wong 3346b631c60SDarrick J. Wong if (xchk_should_terminate(sc, &error)) 3356b631c60SDarrick J. Wong break; 3366b631c60SDarrick J. Wong 3376b631c60SDarrick J. Wong /* 3386b631c60SDarrick J. Wong * Create a new empty transaction so that we can advance the 3396b631c60SDarrick J. Wong * iscan cursor without deadlocking if the inobt has a cycle. 3406b631c60SDarrick J. Wong * We can only push the inactivation workqueues with an empty 3416b631c60SDarrick J. Wong * transaction. 3426b631c60SDarrick J. Wong */ 3436b631c60SDarrick J. Wong error = xchk_trans_alloc_empty(sc); 3446b631c60SDarrick J. Wong if (error) 3456b631c60SDarrick J. Wong break; 3466b631c60SDarrick J. Wong } 3476b631c60SDarrick J. Wong xchk_iscan_iter_finish(&xnc->compare_iscan); 3486b631c60SDarrick J. Wong xchk_iscan_teardown(&xnc->compare_iscan); 3496b631c60SDarrick J. Wong 3506b631c60SDarrick J. Wong return error; 3516b631c60SDarrick J. Wong } 352