LCOV - code coverage report
Current view: top level - fs/xfs/scrub - parent_repair.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 359 518 69.3 %
Date: 2023-07-31 20:08:27 Functions: 25 30 83.3 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2020-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_bit.h"
      14             : #include "xfs_log_format.h"
      15             : #include "xfs_trans.h"
      16             : #include "xfs_sb.h"
      17             : #include "xfs_inode.h"
      18             : #include "xfs_icache.h"
      19             : #include "xfs_da_format.h"
      20             : #include "xfs_da_btree.h"
      21             : #include "xfs_dir2.h"
      22             : #include "xfs_bmap_btree.h"
      23             : #include "xfs_dir2_priv.h"
      24             : #include "xfs_trans_space.h"
      25             : #include "xfs_health.h"
      26             : #include "xfs_swapext.h"
      27             : #include "xfs_parent.h"
      28             : #include "xfs_attr.h"
      29             : #include "xfs_bmap.h"
      30             : #include "scrub/xfs_scrub.h"
      31             : #include "scrub/scrub.h"
      32             : #include "scrub/common.h"
      33             : #include "scrub/trace.h"
      34             : #include "scrub/repair.h"
      35             : #include "scrub/iscan.h"
      36             : #include "scrub/findparent.h"
      37             : #include "scrub/readdir.h"
      38             : #include "scrub/tempfile.h"
      39             : #include "scrub/tempswap.h"
      40             : #include "scrub/orphanage.h"
      41             : #include "scrub/xfile.h"
      42             : #include "scrub/xfarray.h"
      43             : #include "scrub/xfblob.h"
      44             : #include "scrub/attr_repair.h"
      45             : #include "scrub/listxattr.h"
      46             : 
      47             : /*
      48             :  * Repairing The Directory Parent Pointer
      49             :  * ======================================
      50             :  *
      51             :  * Currently, only directories support parent pointers (in the form of '..'
      52             :  * entries), so we simply scan the filesystem and update the '..' entry.
      53             :  *
      54             :  * Note that because the only parent pointer is the dotdot entry, we won't
      55             :  * touch an unhealthy directory, since the directory repair code is perfectly
      56             :  * capable of rebuilding a directory with the proper parent inode.
      57             :  *
      58             :  * See the section on locking issues in dir_repair.c for more information about
      59             :  * conflicts with the VFS.  The findparent code wll keep our incore parent
      60             :  * inode up to date.
      61             :  *
      62             :  * If parent pointers are enabled, we instead reconstruct the parent pointer
      63             :  * information by visiting every directory entry of every directory in the
      64             :  * system and translating the relevant dirents into parent pointers.  In this
      65             :  * case, it is advantageous to stash all parent pointers created from dirents
      66             :  * from a single parent file before replaying them into the temporary file.  To
      67             :  * save memory, the live filesystem scan reuses the findparent object.  Parent
      68             :  * pointer repair chooses either directory scanning or findparent, but not
      69             :  * both.
      70             :  *
      71             :  * When salvaging completes, the remaining stashed entries are replayed to the
      72             :  * temporary file.  All non-parent pointer extended attributes are copied to
      73             :  * the temporary file's extended attributes.  An atomic extent swap is used to
      74             :  * commit the new directory blocks to the directory being repaired.  This will
      75             :  * disrupt attrmulti cursors.
      76             :  */
      77             : 
      78             : /* Create a parent pointer in the tempfile. */
      79             : #define XREP_PPTR_ADD           (1)
      80             : 
      81             : /* Remove a parent pointer from the tempfile. */
      82             : #define XREP_PPTR_REMOVE        (2)
      83             : 
      84             : /* A stashed parent pointer update. */
      85             : struct xrep_pptr {
      86             :         /* Cookie for retrieval of the pptr name. */
      87             :         xfblob_cookie                   name_cookie;
      88             : 
      89             :         /* Parent pointer attr key. */
      90             :         xfs_ino_t                       p_ino;
      91             :         uint32_t                        p_gen;
      92             : 
      93             :         /* Length of the pptr name. */
      94             :         uint8_t                         namelen;
      95             : 
      96             :         /* XREP_PPTR_{ADD,REMOVE} */
      97             :         uint8_t                         action;
      98             : };
      99             : 
     100             : /*
     101             :  * Stash up to 8 pages of recovered parent pointers in pptr_recs and
     102             :  * pptr_names before we write them to the temp file.
     103             :  */
     104             : #define XREP_PARENT_MAX_STASH_BYTES     (PAGE_SIZE * 8)
     105             : 
     106             : struct xrep_parent {
     107             :         struct xfs_scrub        *sc;
     108             : 
     109             :         /* Fixed-size array of xrep_pptr structures. */
     110             :         struct xfarray          *pptr_recs;
     111             : 
     112             :         /* Blobs containing parent pointer names. */
     113             :         struct xfblob           *pptr_names;
     114             : 
     115             :         /* xattr keys */
     116             :         struct xfarray          *xattr_records;
     117             : 
     118             :         /* xattr values */
     119             :         struct xfblob           *xattr_blobs;
     120             : 
     121             :         /* Scratch buffers for saving extended attributes */
     122             :         unsigned char           *xattr_name;
     123             :         void                    *xattr_value;
     124             :         unsigned int            xattr_value_sz;
     125             : 
     126             :         /*
     127             :          * Information used to swap the attr fork, if the fs supports parent
     128             :          * pointers.
     129             :          */
     130             :         struct xrep_tempswap    tx;
     131             : 
     132             :         /*
     133             :          * Information used to scan the filesystem to find the inumber of the
     134             :          * dotdot entry for this directory.  On filesystems without parent
     135             :          * pointers, we use the findparent_* functions on this object and
     136             :          * access only the parent_ino field directly.
     137             :          *
     138             :          * When parent pointers are enabled, the directory entry scanner uses
     139             :          * the iscan, hooks, and lock fields of this object directly.
     140             :          * @pscan.lock coordinates access to pptr_recs, pptr_names, pptr, and
     141             :          * pptr_scratch.  This reduces the memory requirements of this
     142             :          * structure.
     143             :          *
     144             :          * The lock also controls access to xattr_records and xattr_blobs(?)
     145             :          */
     146             :         struct xrep_parent_scan_info pscan;
     147             : 
     148             :         /* Orphanage reparenting request. */
     149             :         struct xrep_adoption    adoption;
     150             : 
     151             :         /* Have we seen any live updates of parent pointers recently? */
     152             :         bool                    saw_pptr_updates;
     153             : 
     154             :         /* xattr key and da args for parent pointer replay. */
     155             :         struct xfs_parent_scratch pptr_scratch;
     156             : 
     157             :         /*
     158             :          * Scratch buffer for scanning dirents to create pptr xattrs.  At the
     159             :          * very end of the repair, it can also be used to compute the
     160             :          * lost+found filename if we need to reparent the file.
     161             :          */
     162             :         struct xfs_parent_name_irec pptr;
     163             : };
     164             : 
     165             : struct xrep_parent_xattr {
     166             :         /* Cookie for retrieval of the xattr name. */
     167             :         xfblob_cookie           name_cookie;
     168             : 
     169             :         /* Cookie for retrieval of the xattr value. */
     170             :         xfblob_cookie           value_cookie;
     171             : 
     172             :         /* XFS_ATTR_* flags */
     173             :         int                     flags;
     174             : 
     175             :         /* Length of the value and name. */
     176             :         uint32_t                valuelen;
     177             :         uint16_t                namelen;
     178             : };
     179             : 
     180             : /*
     181             :  * Stash up to 8 pages of attrs in xattr_records/xattr_blobs before we write
     182             :  * them to the temp file.
     183             :  */
     184             : #define XREP_PARENT_XATTR_MAX_STASH_BYTES       (PAGE_SIZE * 8)
     185             : 
     186             : /* Tear down all the incore stuff we created. */
     187             : static void
     188      138772 : xrep_parent_teardown(
     189             :         struct xrep_parent      *rp)
     190             : {
     191      138772 :         xrep_findparent_scan_teardown(&rp->pscan);
     192      138784 :         kvfree(rp->xattr_name);
     193      138784 :         rp->xattr_name = NULL;
     194      138784 :         kvfree(rp->xattr_value);
     195      138781 :         rp->xattr_value = NULL;
     196      138781 :         if (rp->xattr_blobs)
     197      138781 :                 xfblob_destroy(rp->xattr_blobs);
     198      138778 :         rp->xattr_blobs = NULL;
     199      138778 :         if (rp->xattr_records)
     200      138778 :                 xfarray_destroy(rp->xattr_records);
     201      138780 :         rp->xattr_records = NULL;
     202      138780 :         if (rp->pptr_names)
     203      138780 :                 xfblob_destroy(rp->pptr_names);
     204      138785 :         rp->pptr_names = NULL;
     205      138785 :         if (rp->pptr_recs)
     206      138785 :                 xfarray_destroy(rp->pptr_recs);
     207      138784 :         rp->pptr_recs = NULL;
     208      138784 : }
     209             : 
     210             : /* Set up for a parent repair. */
     211             : int
     212      281281 : xrep_setup_parent(
     213             :         struct xfs_scrub        *sc)
     214             : {
     215      281281 :         struct xrep_parent      *rp;
     216      281281 :         int                     error;
     217             : 
     218      281281 :         xchk_fsgates_enable(sc, XCHK_FSGATES_DIRENTS);
     219             : 
     220      281276 :         rp = kvzalloc(sizeof(struct xrep_parent), XCHK_GFP_FLAGS);
     221      281240 :         if (!rp)
     222             :                 return -ENOMEM;
     223      281240 :         rp->sc = sc;
     224      281240 :         sc->buf = rp;
     225             : 
     226      281240 :         error = xrep_tempfile_create(sc, S_IFREG);
     227      281282 :         if (error)
     228             :                 return error;
     229             : 
     230      281282 :         return xrep_orphanage_try_create(sc);
     231             : }
     232             : 
     233             : /*
     234             :  * Scan all files in the filesystem for a child dirent that we can turn into
     235             :  * the dotdot entry for this directory.
     236             :  */
     237             : STATIC int
     238           0 : xrep_parent_find_dotdot(
     239             :         struct xrep_parent      *rp)
     240             : {
     241           0 :         struct xfs_scrub        *sc = rp->sc;
     242           0 :         xfs_ino_t               ino;
     243           0 :         unsigned int            sick, checked;
     244           0 :         int                     error;
     245             : 
     246             :         /*
     247             :          * Avoid sick directories.  There shouldn't be anyone else clearing the
     248             :          * directory's sick status.
     249             :          */
     250           0 :         xfs_inode_measure_sickness(sc->ip, &sick, &checked);
     251           0 :         if (sick & XFS_SICK_INO_DIR)
     252             :                 return -EFSCORRUPTED;
     253             : 
     254           0 :         ino = xrep_findparent_self_reference(sc);
     255           0 :         if (ino != NULLFSINO) {
     256           0 :                 xrep_findparent_scan_finish_early(&rp->pscan, ino);
     257           0 :                 return 0;
     258             :         }
     259             : 
     260             :         /*
     261             :          * Drop the ILOCK on this directory so that we can scan for the dotdot
     262             :          * entry.  Figure out who is going to be the parent of this directory,
     263             :          * then retake the ILOCK so that we can salvage directory entries.
     264             :          */
     265           0 :         xchk_iunlock(sc, XFS_ILOCK_EXCL);
     266             : 
     267             :         /* Does the VFS dcache have an answer for us? */
     268           0 :         ino = xrep_findparent_from_dcache(sc);
     269           0 :         if (ino != NULLFSINO) {
     270           0 :                 error = xrep_findparent_confirm(sc, &ino);
     271           0 :                 if (!error && ino != NULLFSINO) {
     272           0 :                         xrep_findparent_scan_finish_early(&rp->pscan, ino);
     273           0 :                         goto out_relock;
     274             :                 }
     275             :         }
     276             : 
     277             :         /* Scan the entire filesystem for a parent. */
     278           0 :         error = xrep_findparent_scan(&rp->pscan);
     279           0 : out_relock:
     280           0 :         xchk_ilock(sc, XFS_ILOCK_EXCL);
     281             : 
     282           0 :         return error;
     283             : }
     284             : 
     285             : /*
     286             :  * Add this stashed incore parent pointer to the temporary file.
     287             :  * The caller must hold the tempdir's IOLOCK, must not hold any ILOCKs, and
     288             :  * must not be in transaction context.
     289             :  */
     290             : STATIC int
     291      160856 : xrep_parent_replay_update(
     292             :         struct xrep_parent      *rp,
     293             :         const struct xrep_pptr  *pptr)
     294             : {
     295      160856 :         struct xfs_scrub        *sc = rp->sc;
     296      160856 :         int                     error;
     297             : 
     298      160856 :         rp->pptr.p_ino = pptr->p_ino;
     299      160856 :         rp->pptr.p_gen = pptr->p_gen;
     300      160856 :         rp->pptr.p_namelen = pptr->namelen;
     301      160856 :         xfs_parent_irec_hashname(sc->mp, &rp->pptr);
     302             : 
     303      160849 :         switch (pptr->action) {
     304      160849 :         case XREP_PPTR_ADD:
     305             :                 /* Create parent pointer. */
     306      160849 :                 trace_xrep_parent_replay_parentadd(sc->tempip, &rp->pptr);
     307             : 
     308      160849 :                 error = xfs_parent_set(sc->tempip, sc->ip->i_ino, &rp->pptr,
     309             :                                 &rp->pptr_scratch);
     310      160857 :                 if (error)
     311           0 :                         return error;
     312             :                 break;
     313           0 :         case XREP_PPTR_REMOVE:
     314             :                 /* Remove parent pointer. */
     315           0 :                 trace_xrep_parent_replay_parentremove(sc->tempip, &rp->pptr);
     316             : 
     317           0 :                 error = xfs_parent_unset(sc->tempip, sc->ip->i_ino, &rp->pptr,
     318             :                                 &rp->pptr_scratch);
     319           0 :                 if (error)
     320           0 :                         return error;
     321             :                 break;
     322           0 :         default:
     323           0 :                 ASSERT(0);
     324           0 :                 return -EIO;
     325             :         }
     326             : 
     327             :         return 0;
     328             : }
     329             : 
     330             : /*
     331             :  * Flush stashed parent pointer updates that have been recorded by the scanner.
     332             :  * This is done to reduce the memory requirements of the parent pointer
     333             :  * rebuild, since files can have a lot of hardlinks and the fs can be busy.
     334             :  *
     335             :  * Caller must not hold transactions or ILOCKs.  Caller must hold the tempfile
     336             :  * IOLOCK.
     337             :  */
     338             : STATIC int
     339      138744 : xrep_parent_replay_updates(
     340             :         struct xrep_parent      *rp)
     341             : {
     342      138744 :         xfarray_idx_t           array_cur;
     343      138744 :         int                     error;
     344             : 
     345      138744 :         mutex_lock(&rp->pscan.lock);
     346      438344 :         foreach_xfarray_idx(rp->pptr_recs, array_cur) {
     347      160849 :                 struct xrep_pptr        pptr;
     348             : 
     349      160849 :                 error = xfarray_load(rp->pptr_recs, array_cur, &pptr);
     350      160857 :                 if (error)
     351           0 :                         goto out_unlock;
     352             : 
     353      160857 :                 error = xfblob_load(rp->pptr_names, pptr.name_cookie,
     354      160857 :                                 rp->pptr.p_name, pptr.namelen);
     355      160857 :                 if (error)
     356           0 :                         goto out_unlock;
     357      160857 :                 rp->pptr.p_name[MAXNAMELEN - 1] = 0;
     358      160857 :                 mutex_unlock(&rp->pscan.lock);
     359             : 
     360      160856 :                 error = xrep_parent_replay_update(rp, &pptr);
     361      160857 :                 if (error)
     362           0 :                         return error;
     363             : 
     364      160857 :                 mutex_lock(&rp->pscan.lock);
     365             :         }
     366             : 
     367             :         /* Empty out both arrays now that we've added the entries. */
     368      138748 :         xfarray_truncate(rp->pptr_recs);
     369      138738 :         xfblob_truncate(rp->pptr_names);
     370      138748 :         mutex_unlock(&rp->pscan.lock);
     371      138748 :         return 0;
     372             : out_unlock:
     373           0 :         mutex_unlock(&rp->pscan.lock);
     374           0 :         return error;
     375             : }
     376             : 
     377             : /*
     378             :  * Remember that we want to create a parent pointer in the tempfile.  These
     379             :  * stashed actions will be replayed later.
     380             :  */
     381             : STATIC int
     382      160881 : xrep_parent_stash_parentadd(
     383             :         struct xrep_parent      *rp,
     384             :         const struct xfs_name   *name,
     385             :         const struct xfs_inode  *dp)
     386             : {
     387      160881 :         struct xrep_pptr        pptr = {
     388             :                 .action         = XREP_PPTR_ADD,
     389      160881 :                 .namelen        = name->len,
     390      160881 :                 .p_ino          = dp->i_ino,
     391      160881 :                 .p_gen          = VFS_IC(dp)->i_generation,
     392             :         };
     393      160881 :         int                     error;
     394             : 
     395      160881 :         trace_xrep_parent_stash_parentadd(rp->sc->tempip, dp, name);
     396             : 
     397      160881 :         error = xfblob_store(rp->pptr_names, &pptr.name_cookie, name->name,
     398      160881 :                         name->len);
     399      160879 :         if (error)
     400             :                 return error;
     401             : 
     402      160878 :         return xfarray_append(rp->pptr_recs, &pptr);
     403             : }
     404             : 
     405             : /*
     406             :  * Remember that we want to remove a parent pointer from the tempfile.  These
     407             :  * stashed actions will be replayed later.
     408             :  */
     409             : STATIC int
     410           0 : xrep_parent_stash_parentremove(
     411             :         struct xrep_parent      *rp,
     412             :         const struct xfs_name   *name,
     413             :         const struct xfs_inode  *dp)
     414             : {
     415           0 :         struct xrep_pptr        pptr = {
     416             :                 .action         = XREP_PPTR_REMOVE,
     417           0 :                 .namelen        = name->len,
     418           0 :                 .p_ino          = dp->i_ino,
     419           0 :                 .p_gen          = VFS_IC(dp)->i_generation,
     420             :         };
     421           0 :         int                     error;
     422             : 
     423           0 :         trace_xrep_parent_stash_parentremove(rp->sc->tempip, dp, name);
     424             : 
     425           0 :         error = xfblob_store(rp->pptr_names, &pptr.name_cookie, name->name,
     426           0 :                         name->len);
     427           0 :         if (error)
     428             :                 return error;
     429             : 
     430           0 :         return xfarray_append(rp->pptr_recs, &pptr);
     431             : }
     432             : 
     433             : /*
     434             :  * Examine an entry of a directory.  If this dirent leads us back to the file
     435             :  * whose parent pointers we're rebuilding, add a pptr to the temporary
     436             :  * directory.
     437             :  */
     438             : STATIC int
     439  4707321243 : xrep_parent_scan_dirent(
     440             :         struct xfs_scrub        *sc,
     441             :         struct xfs_inode        *dp,
     442             :         xfs_dir2_dataptr_t      dapos,
     443             :         const struct xfs_name   *name,
     444             :         xfs_ino_t               ino,
     445             :         void                    *priv)
     446             : {
     447  4707321243 :         struct xrep_parent      *rp = priv;
     448  4707321243 :         int                     error;
     449             : 
     450             :         /* Dirent doesn't point to this directory. */
     451  4707321243 :         if (ino != rp->sc->ip->i_ino)
     452             :                 return 0;
     453             : 
     454             :         /* No weird looking names. */
     455      195711 :         if (!xfs_dir2_namecheck(name->name, name->len))
     456             :                 return -EFSCORRUPTED;
     457             : 
     458             :         /* No mismatching ftypes. */
     459      195711 :         if (name->type != xfs_mode_to_ftype(VFS_I(sc->ip)->i_mode))
     460             :                 return -EFSCORRUPTED;
     461             : 
     462             :         /* Don't pick up dot or dotdot entries; we only want child dirents. */
     463      356592 :         if (xfs_dir2_samename(name, &xfs_name_dotdot) ||
     464      160881 :             xfs_dir2_samename(name, &xfs_name_dot))
     465       34830 :                 return 0;
     466             : 
     467             :         /*
     468             :          * Transform this dirent into a parent pointer and queue it for later
     469             :          * addition to the temporary file.
     470             :          */
     471      160881 :         mutex_lock(&rp->pscan.lock);
     472      160881 :         error = xrep_parent_stash_parentadd(rp, name, dp);
     473      160880 :         mutex_unlock(&rp->pscan.lock);
     474      160880 :         return error;
     475             : }
     476             : 
     477             : /*
     478             :  * Decide if we want to look for dirents in this directory.  Skip the file
     479             :  * being repaired and any files being used to stage repairs.
     480             :  */
     481             : static inline bool
     482  6210299742 : xrep_parent_want_scan(
     483             :         struct xrep_parent      *rp,
     484             :         const struct xfs_inode  *ip)
     485             : {
     486  6210299742 :         return ip != rp->sc->ip && !xrep_is_tempfile(ip);
     487             : }
     488             : 
     489             : /*
     490             :  * Take ILOCK on a file that we want to scan.
     491             :  *
     492             :  * Select ILOCK_EXCL if the file is a directory with an unloaded data bmbt.
     493             :  * Otherwise, take ILOCK_SHARED.
     494             :  */
     495             : static inline unsigned int
     496  3108751097 : xrep_parent_scan_ilock(
     497             :         struct xrep_parent      *rp,
     498             :         struct xfs_inode        *ip)
     499             : {
     500  3108751097 :         uint                    lock_mode = XFS_ILOCK_SHARED;
     501             : 
     502             :         /* Still need to take the shared ILOCK to advance the iscan cursor. */
     503  3108751097 :         if (!xrep_parent_want_scan(rp, ip))
     504      405747 :                 goto lock;
     505             : 
     506  3728002348 :         if (S_ISDIR(VFS_I(ip)->i_mode) && xfs_need_iread_extents(&ip->i_df)) {
     507           0 :                 lock_mode = XFS_ILOCK_EXCL;
     508           0 :                 goto lock;
     509             :         }
     510             : 
     511  3108575666 : lock:
     512  3108981413 :         xfs_ilock(ip, lock_mode);
     513  3109608266 :         return lock_mode;
     514             : }
     515             : 
     516             : /*
     517             :  * Scan this file for relevant child dirents that point to the file whose
     518             :  * parent pointers we're rebuilding.
     519             :  */
     520             : STATIC int
     521  3107701615 : xrep_parent_scan_file(
     522             :         struct xrep_parent      *rp,
     523             :         struct xfs_inode        *ip)
     524             : {
     525  3107701615 :         unsigned int            lock_mode;
     526  3107701615 :         int                     error = 0;
     527             : 
     528  3107701615 :         lock_mode = xrep_parent_scan_ilock(rp, ip);
     529             : 
     530  3109462241 :         if (!xrep_parent_want_scan(rp, ip))
     531      405747 :                 goto scan_done;
     532             : 
     533  3109767393 :         if (S_ISDIR(VFS_I(ip)->i_mode)) {
     534   619700346 :                 error = xchk_dir_walk(rp->sc, ip, xrep_parent_scan_dirent, rp);
     535   619614396 :                 if (error)
     536           0 :                         goto scan_done;
     537             :         }
     538             : 
     539  3109681443 : scan_done:
     540  3110087190 :         xchk_iscan_mark_visited(&rp->pscan.iscan, ip);
     541  3108677218 :         xfs_iunlock(ip, lock_mode);
     542  3112081676 :         return error;
     543             : }
     544             : 
     545             : /* Decide if we've stashed too much pptr data in memory. */
     546             : static inline bool
     547  3112032299 : xrep_parent_want_flush_stashed(
     548             :         struct xrep_parent      *rp)
     549             : {
     550  3112032299 :         unsigned long long      bytes;
     551             : 
     552  3112032299 :         bytes = xfarray_bytes(rp->pptr_recs) + xfblob_bytes(rp->pptr_names);
     553  3112051851 :         return bytes > XREP_PARENT_MAX_STASH_BYTES;
     554             : }
     555             : 
     556             : /*
     557             :  * Scan all directories in the filesystem to look for dirents that we can turn
     558             :  * into parent pointers.
     559             :  */
     560             : STATIC int
     561      138785 : xrep_parent_scan_dirtree(
     562             :         struct xrep_parent      *rp)
     563             : {
     564      138785 :         struct xfs_scrub        *sc = rp->sc;
     565      138785 :         struct xfs_inode        *ip;
     566      138785 :         int                     error;
     567             : 
     568             :         /*
     569             :          * Filesystem scans are time consuming.  Drop the file ILOCK and all
     570             :          * other resources for the duration of the scan and hope for the best.
     571             :          * The live update hooks will keep our scan information up to date.
     572             :          */
     573      138785 :         xchk_trans_cancel(sc);
     574      138785 :         if (sc->ilock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL))
     575      138785 :                 xchk_iunlock(sc, sc->ilock_flags & (XFS_ILOCK_SHARED |
     576             :                                                     XFS_ILOCK_EXCL));
     577      138785 :         error = xchk_trans_alloc_empty(sc);
     578      138785 :         if (error)
     579             :                 return error;
     580             : 
     581  3109557047 :         while ((error = xchk_iscan_iter(&rp->pscan.iscan, &ip)) == 1) {
     582  3110541422 :                 bool            flush;
     583             : 
     584  3110541422 :                 error = xrep_parent_scan_file(rp, ip);
     585  3112188313 :                 xchk_irele(sc, ip);
     586  3111320690 :                 if (error)
     587             :                         break;
     588             : 
     589             :                 /* Flush stashed pptr updates to constrain memory usage. */
     590  3111320690 :                 mutex_lock(&rp->pscan.lock);
     591  3111942062 :                 flush = xrep_parent_want_flush_stashed(rp);
     592  3111989230 :                 mutex_unlock(&rp->pscan.lock);
     593  3108986453 :                 if (flush) {
     594           0 :                         xchk_trans_cancel(sc);
     595             : 
     596           0 :                         error = xrep_tempfile_iolock_polled(sc);
     597           0 :                         if (error)
     598             :                                 break;
     599             : 
     600           0 :                         error = xrep_parent_replay_updates(rp);
     601           0 :                         xrep_tempfile_iounlock(sc);
     602           0 :                         if (error)
     603             :                                 break;
     604             : 
     605           0 :                         error = xchk_trans_alloc_empty(sc);
     606           0 :                         if (error)
     607             :                                 break;
     608             :                 }
     609             : 
     610  3108986453 :                 if (xchk_should_terminate(sc, &error))
     611             :                         break;
     612             :         }
     613      138785 :         xchk_iscan_iter_finish(&rp->pscan.iscan);
     614      138785 :         if (error) {
     615             :                 /*
     616             :                  * If we couldn't grab an inode that was busy with a state
     617             :                  * change, change the error code so that we exit to userspace
     618             :                  * as quickly as possible.
     619             :                  */
     620          37 :                 if (error == -EBUSY)
     621             :                         return -ECANCELED;
     622          37 :                 return error;
     623             :         }
     624             : 
     625             :         /*
     626             :          * Retake sc->ip's ILOCK now that we're done flushing stashed parent
     627             :          * pointers.  We end this function with an empty transaction and the
     628             :          * ILOCK.
     629             :          */
     630      138748 :         xchk_ilock(rp->sc, XFS_ILOCK_EXCL);
     631      138748 :         return 0;
     632             : }
     633             : 
     634             : /*
     635             :  * Capture dirent updates being made by other threads which are relevant to the
     636             :  * file being repaired.
     637             :  */
     638             : STATIC int
     639    78411564 : xrep_parent_live_update(
     640             :         struct notifier_block           *nb,
     641             :         unsigned long                   action,
     642             :         void                            *data)
     643             : {
     644    78411564 :         struct xfs_dir_update_params    *p = data;
     645    78411564 :         struct xrep_parent              *rp;
     646    78411564 :         struct xfs_scrub                *sc;
     647    78411564 :         int                             error;
     648             : 
     649    78411564 :         rp = container_of(nb, struct xrep_parent, pscan.hooks.dirent_hook.nb);
     650    78411564 :         sc = rp->sc;
     651             : 
     652             :         /*
     653             :          * This thread updated a dirent that points to the file that we're
     654             :          * repairing, so stash the update for replay against the temporary
     655             :          * file.
     656             :          */
     657    78411564 :         if (p->ip->i_ino == sc->ip->i_ino &&
     658           0 :             xchk_iscan_want_live_update(&rp->pscan.iscan, p->dp->i_ino)) {
     659           0 :                 mutex_lock(&rp->pscan.lock);
     660           0 :                 if (p->delta > 0)
     661           0 :                         error = xrep_parent_stash_parentadd(rp, p->name, p->dp);
     662             :                 else
     663           0 :                         error = xrep_parent_stash_parentremove(rp, p->name,
     664             :                                         p->dp);
     665           0 :                 if (!error)
     666           0 :                         rp->saw_pptr_updates = true;
     667           0 :                 mutex_unlock(&rp->pscan.lock);
     668           0 :                 if (error)
     669           0 :                         goto out_abort;
     670             :         }
     671             : 
     672             :         return NOTIFY_DONE;
     673             : out_abort:
     674           0 :         xchk_iscan_abort(&rp->pscan.iscan);
     675           0 :         return NOTIFY_DONE;
     676             : }
     677             : 
     678             : /* Reset a directory's dotdot entry, if needed. */
     679             : STATIC int
     680       29193 : xrep_parent_reset_dotdot(
     681             :         struct xrep_parent      *rp)
     682             : {
     683       29193 :         struct xfs_scrub        *sc = rp->sc;
     684       29193 :         xfs_ino_t               ino;
     685       29193 :         unsigned int            spaceres;
     686       29193 :         int                     error = 0;
     687             : 
     688       29193 :         ASSERT(sc->ilock_flags & XFS_ILOCK_EXCL);
     689             : 
     690       29193 :         error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot, &ino);
     691       29194 :         if (error || ino == rp->pscan.parent_ino)
     692             :                 return error;
     693             : 
     694           0 :         xfs_trans_ijoin(sc->tp, sc->ip, 0);
     695             : 
     696           0 :         trace_xrep_parent_reset_dotdot(sc->ip, rp->pscan.parent_ino);
     697             : 
     698             :         /*
     699             :          * Reserve more space just in case we have to expand the dir.  We're
     700             :          * allowed to exceed quota to repair inconsistent metadata.
     701             :          */
     702           0 :         spaceres = xfs_rename_space_res(sc->mp, 0, false, xfs_name_dotdot.len,
     703             :                         false);
     704           0 :         error = xfs_trans_reserve_more_inode(sc->tp, sc->ip, spaceres, 0,
     705             :                         true);
     706           0 :         if (error)
     707             :                 return error;
     708             : 
     709           0 :         return xfs_dir_replace(sc->tp, sc->ip, &xfs_name_dotdot,
     710             :                         rp->pscan.parent_ino, spaceres);
     711             : }
     712             : 
     713             : /*
     714             :  * Move the current file to the orphanage.
     715             :  *
     716             :  * Caller must hold IOLOCK_EXCL on @sc->ip, and no other inode locks.  Upon
     717             :  * successful return, the scrub transaction will have enough extra reservation
     718             :  * to make the move; it will hold IOLOCK_EXCL and ILOCK_EXCL of @sc->ip and the
     719             :  * orphanage; and both inodes will be ijoined.
     720             :  */
     721             : STATIC int
     722           5 : xrep_parent_move_to_orphanage(
     723             :         struct xrep_parent      *rp)
     724             : {
     725           5 :         struct xfs_scrub        *sc = rp->sc;
     726           5 :         xfs_ino_t               orig_parent, new_parent;
     727           5 :         int                     error;
     728             : 
     729             :         if (S_ISDIR(VFS_I(sc->ip)->i_mode)) {
     730           5 :                 /*
     731             :                  * We are about to drop the ILOCK on sc->ip to lock the
     732             :                  * orphanage and prepare for the adoption.  Therefore, look up
     733           5 :                  * the old dotdot entry for sc->ip so that we can compare it
     734             :                  * after we re-lock sc->ip.
     735             :                  */
     736             :                 error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot,
     737             :                                 &orig_parent);
     738             :                 if (error)
     739             :                         return error;
     740           0 :         } else {
     741             :                 /*
     742           0 :                  * We haven't dropped the ILOCK since we swapped in the new
     743             :                  * parent pointers, which means that the file cannot have been
     744             :                  * moved in the directory tree, and there are no parents.
     745             :                  */
     746             :                 orig_parent = NULLFSINO;
     747             :         }
     748             : 
     749             :         /*
     750           5 :          * Because the orphanage is just another directory in the filesystem,
     751             :          * we must take its IOLOCK to coordinate with the VFS.  We cannot take
     752             :          * an IOLOCK while holding an ILOCK, so we must drop the ILOCK.  We
     753             :          * may have to drop the IOLOCK as well.
     754             :          */
     755             :         xchk_iunlock(sc, XFS_ILOCK_EXCL);
     756             : 
     757             :         error = xrep_adoption_init(sc, &rp->adoption);
     758             :         if (error)
     759           5 :                 return error;
     760             : 
     761           5 :         /* If we can take the orphanage's iolock then we're ready to move. */
     762           5 :         if (!xrep_orphanage_ilock_nowait(sc, XFS_IOLOCK_EXCL)) {
     763             :                 xchk_iunlock(sc, sc->ilock_flags);
     764             :                 error = xrep_orphanage_iolock_two(sc);
     765             :                 if (error)
     766           5 :                         goto err_adoption;
     767           0 :         }
     768           0 : 
     769           0 :         /* Prepare for the adoption and lock both down. */
     770           0 :         error = xrep_adoption_prep(&rp->adoption);
     771             :         if (error)
     772             :                 goto err_adoption;
     773             : 
     774           5 :         error = xrep_adoption_compute_name(&rp->adoption, rp->pptr.p_name);
     775           5 :         if (error)
     776           0 :                 goto err_adoption;
     777             : 
     778           5 :         /*
     779           5 :          * Now that we've reacquired the ILOCK on sc->ip, look up the dotdot
     780           0 :          * entry again.  If the parent changed or the child was unlinked while
     781             :          * the child directory was unlocked, we don't need to move the child to
     782             :          * the orphanage after all.  For a non-directory, we have to scan for
     783             :          * the first parent pointer to see if one has been added.
     784             :          */
     785             :         if (S_ISDIR(VFS_I(sc->ip)->i_mode))
     786             :                 error = xchk_dir_lookup(sc, sc->ip, &xfs_name_dotdot,
     787             :                                 &new_parent);
     788             :         else
     789           5 :                 error = xrep_findparent_from_pptrs(sc, &new_parent);
     790           0 :         if (error)
     791             :                 goto err_adoption;
     792             :         if (orig_parent != new_parent || VFS_I(sc->ip)->i_nlink == 0) {
     793           5 :                 error = 0;
     794           5 :                 goto err_adoption;
     795           0 :         }
     796           5 : 
     797           5 :         return xrep_adoption_commit(&rp->adoption);
     798           5 : err_adoption:
     799             :         xrep_adoption_cancel(&rp->adoption, error);
     800             :         return error;
     801           0 : }
     802           5 : 
     803           5 : /* Ensure that the xattr value buffer is large enough. */
     804           5 : STATIC int
     805             : xrep_parent_alloc_xattr_value(
     806             :         struct xrep_parent      *rp,
     807             :         size_t                  bufsize)
     808             : {
     809      138785 :         void                    *new_val;
     810             : 
     811             :         if (rp->xattr_value_sz >= bufsize)
     812             :                 return 0;
     813      138785 : 
     814             :         if (rp->xattr_value) {
     815      138785 :                 kvfree(rp->xattr_value);
     816             :                 rp->xattr_value = NULL;
     817             :                 rp->xattr_value_sz = 0;
     818      138785 :         }
     819           0 : 
     820           0 :         new_val = kvmalloc(bufsize, XCHK_GFP_FLAGS);
     821           0 :         if (!new_val)
     822             :                 return -ENOMEM;
     823             : 
     824      138785 :         rp->xattr_value = new_val;
     825      138785 :         rp->xattr_value_sz = bufsize;
     826             :         return 0;
     827             : }
     828      138785 : 
     829      138785 : /* Retrieve the (remote) value of a non-pptr xattr. */
     830      138785 : STATIC int
     831             : xrep_parent_fetch_xattr_remote(
     832             :         struct xrep_parent      *rp,
     833             :         struct xfs_inode        *ip,
     834             :         unsigned int            attr_flags,
     835           0 :         const unsigned char     *name,
     836             :         unsigned int            namelen,
     837             :         unsigned int            valuelen)
     838             : {
     839             :         struct xfs_scrub        *sc = rp->sc;
     840             :         struct xfs_da_args      args = {
     841             :                 .op_flags       = XFS_DA_OP_NOTIME,
     842             :                 .attr_filter    = attr_flags & XFS_ATTR_NSP_ONDISK_MASK,
     843           0 :                 .geo            = sc->mp->m_attr_geo,
     844           0 :                 .whichfork      = XFS_ATTR_FORK,
     845             :                 .dp             = ip,
     846           0 :                 .name           = name,
     847           0 :                 .namelen        = namelen,
     848             :                 .hashval        = xfs_da_hashname(name, namelen),
     849             :                 .trans          = sc->tp,
     850             :                 .valuelen       = valuelen,
     851             :                 .owner          = ip->i_ino,
     852           0 :         };
     853           0 :         int                     error;
     854             : 
     855           0 :         /*
     856             :          * If we need a larger value buffer, try to allocate one.  If that
     857           0 :          * fails, return with -EDEADLOCK to try harder.
     858             :          */
     859             :         error = xrep_parent_alloc_xattr_value(rp, valuelen);
     860             :         if (error == -ENOMEM)
     861             :                 return -EDEADLOCK;
     862             :         if (error)
     863           0 :                 return error;
     864           0 : 
     865             :         args.value = rp->xattr_value;
     866           0 :         return xfs_attr_get_ilocked(&args);
     867             : }
     868             : 
     869           0 : /* Stash non-pptr attributes for later replay into the temporary file. */
     870           0 : STATIC int
     871             : xrep_parent_stash_xattr(
     872             :         struct xfs_scrub        *sc,
     873             :         struct xfs_inode        *ip,
     874             :         unsigned int            attr_flags,
     875      176574 :         const unsigned char     *name,
     876             :         unsigned int            namelen,
     877             :         const void              *value,
     878             :         unsigned int            valuelen,
     879             :         void                    *priv)
     880             : {
     881             :         struct xrep_parent_xattr key = {
     882             :                 .valuelen       = valuelen,
     883             :                 .namelen        = namelen,
     884             :                 .flags          = attr_flags & XFS_ATTR_NSP_ONDISK_MASK,
     885      176574 :         };
     886             :         struct xrep_parent      *rp = priv;
     887             :         int                     error;
     888      176574 : 
     889             :         if (attr_flags & (XFS_ATTR_INCOMPLETE | XFS_ATTR_PARENT))
     890      176574 :                 return 0;
     891      176574 : 
     892             :         if (!value) {
     893      176574 :                 error = xrep_parent_fetch_xattr_remote(rp, ip, attr_flags,
     894             :                                 name, namelen, valuelen);
     895             :                 if (error)
     896       15719 :                         return error;
     897           0 : 
     898             :                 value = rp->xattr_value;
     899           0 :         }
     900             : 
     901             :         trace_xrep_parent_stash_xattr(rp->sc->tempip, key.flags, (void *)name,
     902           0 :                         key.namelen, key.valuelen);
     903             : 
     904             :         error = xfblob_store(rp->xattr_blobs, &key.name_cookie, name,
     905       15719 :                         key.namelen);
     906       15719 :         if (error)
     907             :                 return error;
     908       15719 : 
     909       15719 :         error = xfblob_store(rp->xattr_blobs, &key.value_cookie, value,
     910       15719 :                         key.valuelen);
     911             :         if (error)
     912             :                 return error;
     913       15719 : 
     914             :         return xfarray_append(rp->xattr_records, &key);
     915       15719 : }
     916             : 
     917             : /* Insert one xattr key/value. */
     918       15719 : STATIC int
     919             : xrep_parent_insert_xattr(
     920             :         struct xrep_parent              *rp,
     921             :         const struct xrep_parent_xattr  *key)
     922             : {
     923       15719 :         struct xfs_da_args              args = {
     924             :                 .dp                     = rp->sc->tempip,
     925             :                 .attr_filter            = key->flags,
     926             :                 .namelen                = key->namelen,
     927       15719 :                 .valuelen               = key->valuelen,
     928       15719 :                 .op_flags               = XFS_DA_OP_NOTIME,
     929       15719 :                 .owner                  = rp->sc->ip->i_ino,
     930       15719 :         };
     931       15719 :         int                             error;
     932             : 
     933       15719 :         ASSERT(!(key->flags & XFS_ATTR_PARENT));
     934             : 
     935       15719 :         /*
     936             :          * Grab pointers to the scrub buffer so that we can use them to insert
     937       15719 :          * attrs into the temp file.
     938             :          */
     939             :         args.name = rp->xattr_name;
     940             :         args.value = rp->xattr_value;
     941             : 
     942             :         /*
     943       15719 :          * The attribute name is stored near the end of the in-core buffer,
     944       15719 :          * though we reserve one more byte to ensure null termination.
     945             :          */
     946             :         rp->xattr_name[XATTR_NAME_MAX] = 0;
     947             : 
     948             :         error = xfblob_load(rp->xattr_blobs, key->name_cookie, rp->xattr_name,
     949             :                         key->namelen);
     950       15719 :         if (error)
     951             :                 return error;
     952       15719 : 
     953       15719 :         error = xfblob_free(rp->xattr_blobs, key->name_cookie);
     954       15719 :         if (error)
     955             :                 return error;
     956             : 
     957       15719 :         error = xfblob_load(rp->xattr_blobs, key->value_cookie, args.value,
     958       15719 :                         key->valuelen);
     959             :         if (error)
     960             :                 return error;
     961       15719 : 
     962       15719 :         error = xfblob_free(rp->xattr_blobs, key->value_cookie);
     963       15719 :         if (error)
     964             :                 return error;
     965             : 
     966       15719 :         rp->xattr_name[key->namelen] = 0;
     967       15719 : 
     968             :         trace_xrep_parent_insert_xattr(rp->sc->tempip, key->flags,
     969             :                         rp->xattr_name, key->namelen, key->valuelen);
     970       15719 : 
     971             :         error = xfs_attr_set(&args);
     972       15719 :         if (error) {
     973       15719 :                 ASSERT(error != -EEXIST);
     974             :                 return error;
     975       15719 :         }
     976       15719 : 
     977           0 :         return 0;
     978           0 : }
     979             : 
     980             : /*
     981             :  * Periodically flush salvaged attributes to the temporary file.  This is done
     982             :  * to reduce the memory requirements of the xattr rebuild because files can
     983             :  * contain millions of attributes.
     984             :  */
     985             : STATIC int
     986             : xrep_parent_flush_xattrs(
     987             :         struct xrep_parent      *rp)
     988             : {
     989             :         xfarray_idx_t           array_cur;
     990       12887 :         int                     error;
     991             : 
     992             :         /*
     993       12887 :          * Entering this function, the scrub context has a reference to the
     994       12887 :          * inode being repaired, the temporary file, and the empty scrub
     995             :          * transaction that we created for the xattr scan.  We hold ILOCK_EXCL
     996             :          * on the inode being repaired.
     997             :          *
     998             :          * To constrain kernel memory use, we occasionally flush salvaged
     999             :          * xattrs from the xfarray and xfblob structures into the temporary
    1000             :          * file in preparation for swapping the xattr structures at the end.
    1001             :          * Updating the temporary file requires a transaction, so we commit the
    1002             :          * scrub transaction and drop the ILOCK so that xfs_attr_set can
    1003             :          * allocate whatever transaction it wants.
    1004             :          *
    1005             :          * We still hold IOLOCK_EXCL on the inode being repaired, which
    1006             :          * prevents anyone from adding xattrs (or parent pointers) while we're
    1007             :          * flushing.
    1008             :          */
    1009             :         xchk_trans_cancel(rp->sc);
    1010             :         xchk_iunlock(rp->sc, XFS_ILOCK_EXCL);
    1011             : 
    1012             :         /*
    1013       12887 :          * Take the IOLOCK of the temporary file while we modify xattrs.  This
    1014       12887 :          * isn't strictly required because the temporary file is never revealed
    1015             :          * to userspace, but we follow the same locking rules.  We still hold
    1016             :          * sc->ip's IOLOCK.
    1017             :          */
    1018             :         error = xrep_tempfile_iolock_polled(rp->sc);
    1019             :         if (error)
    1020             :                 return error;
    1021             : 
    1022       12887 :         /* Add all the salvaged attrs to the temporary file. */
    1023       12887 :         foreach_xfarray_idx(rp->xattr_records, array_cur) {
    1024             :                 struct xrep_parent_xattr        key;
    1025             : 
    1026             :                 error = xfarray_load(rp->xattr_records, array_cur, &key);
    1027       28606 :                 if (error)
    1028       15719 :                         return error;
    1029             : 
    1030       15719 :                 error = xrep_parent_insert_xattr(rp, &key);
    1031       15719 :                 if (error)
    1032           0 :                         return error;
    1033             :         }
    1034       15719 : 
    1035       15719 :         /* Empty out both arrays now that we've added the entries. */
    1036           0 :         xfarray_truncate(rp->xattr_records);
    1037             :         xfblob_truncate(rp->xattr_blobs);
    1038             : 
    1039             :         xrep_tempfile_iounlock(rp->sc);
    1040       12887 : 
    1041       12887 :         /* Recreate the empty transaction and relock the inode. */
    1042             :         error = xchk_trans_alloc_empty(rp->sc);
    1043       12887 :         if (error)
    1044             :                 return error;
    1045             :         xchk_ilock(rp->sc, XFS_ILOCK_EXCL);
    1046       12887 :         return 0;
    1047       12887 : }
    1048             : 
    1049       12887 : /* Decide if we've stashed too much xattr data in memory. */
    1050       12887 : static inline bool
    1051             : xrep_parent_want_flush_xattrs(
    1052             :         struct xrep_parent      *rp)
    1053             : {
    1054             :         unsigned long long      bytes;
    1055           0 : 
    1056             :         bytes = xfarray_bytes(rp->xattr_records) +
    1057             :                 xfblob_bytes(rp->xattr_blobs);
    1058           0 :         return bytes > XREP_PARENT_XATTR_MAX_STASH_BYTES;
    1059             : }
    1060           0 : 
    1061           0 : /* Flush staged attributes to the temporary file if we're over the limit. */
    1062           0 : STATIC int
    1063             : xrep_parent_try_flush_xattrs(
    1064             :         struct xfs_scrub        *sc,
    1065             :         void                    *priv)
    1066             : {
    1067           0 :         struct xrep_parent      *rp = priv;
    1068             :         int                     error;
    1069             : 
    1070             :         if (!xrep_parent_want_flush_xattrs(rp))
    1071           0 :                 return 0;
    1072           0 : 
    1073             :         error = xrep_parent_flush_xattrs(rp);
    1074           0 :         if (error)
    1075             :                 return error;
    1076             : 
    1077           0 :         /*
    1078           0 :          * If there were any parent pointer updates to the xattr structure
    1079             :          * while we dropped the ILOCK, the xattr structure is now stale.
    1080             :          * Signal to the attr copy process that we need to start over, but
    1081             :          * this time without opportunistic attr flushing.
    1082             :          *
    1083             :          * This is unlikely to happen, so we're ok with restarting the copy.
    1084             :          */
    1085             :         mutex_lock(&rp->pscan.lock);
    1086             :         if (rp->saw_pptr_updates)
    1087             :                 error = -ESTALE;
    1088             :         mutex_unlock(&rp->pscan.lock);
    1089           0 :         return error;
    1090           0 : }
    1091           0 : 
    1092           0 : /* Copy all the non-pptr extended attributes into the temporary file. */
    1093           0 : STATIC int
    1094             : xrep_parent_copy_xattrs(
    1095             :         struct xrep_parent      *rp)
    1096             : {
    1097             :         struct xfs_scrub        *sc = rp->sc;
    1098      138748 :         int                     error;
    1099             : 
    1100             :         /*
    1101      138748 :          * Clear the pptr updates flag.  We hold sc->ip ILOCKed, so there
    1102      138748 :          * can't be any parent pointer updates in progress.
    1103             :          */
    1104             :         mutex_lock(&rp->pscan.lock);
    1105             :         rp->saw_pptr_updates = false;
    1106             :         mutex_unlock(&rp->pscan.lock);
    1107             : 
    1108      138748 :         /* Copy xattrs, stopping periodically to flush the incore buffers. */
    1109      138748 :         error = xchk_xattr_walk(sc, sc->ip, xrep_parent_stash_xattr,
    1110      138748 :                         xrep_parent_try_flush_xattrs, rp);
    1111             :         if (error && error != -ESTALE)
    1112             :                 return error;
    1113      138747 : 
    1114             :         if (error == -ESTALE) {
    1115      138744 :                 /*
    1116             :                  * The xattr copy collided with a parent pointer update.
    1117             :                  * Restart the copy, but this time hold the ILOCK all the way
    1118      138744 :                  * to the end to lock out any directory parent pointer updates.
    1119             :                  */
    1120             :                 error = xchk_xattr_walk(sc, sc->ip, xrep_parent_stash_xattr,
    1121             :                                 NULL, rp);
    1122             :                 if (error)
    1123             :                         return error;
    1124           0 :         }
    1125             : 
    1126           0 :         /* Flush any remaining stashed xattrs to the temporary file. */
    1127             :         if (xfarray_bytes(rp->xattr_records) == 0)
    1128             :                 return 0;
    1129             : 
    1130             :         return xrep_parent_flush_xattrs(rp);
    1131      138744 : }
    1132             : 
    1133             : /*
    1134       12887 :  * Ensure that @sc->ip and @sc->tempip both have attribute forks before we
    1135             :  * head into the attr fork swap transaction.  All files on a filesystem with
    1136             :  * parent pointers must have an attr fork because the parent pointer code
    1137             :  * does not itself add attribute forks.
    1138             :  *
    1139             :  * Note: Unlinkable unlinked files don't need one, but the overhead of having
    1140             :  * an unnecessary attr fork is not justified by the additional code complexity
    1141             :  * that would be needed to track that state correctly.
    1142             :  */
    1143             : STATIC int
    1144             : xrep_parent_ensure_attr_fork(
    1145             :         struct xrep_parent      *rp)
    1146             : {
    1147             :         struct xfs_scrub        *sc = rp->sc;
    1148      138746 :         int                     error;
    1149             : 
    1150             :         error = xfs_attr_add_fork(sc->tempip,
    1151      138746 :                         sizeof(struct xfs_attr_sf_hdr), 1);
    1152      138746 :         if (error)
    1153             :                 return error;
    1154      138746 :         return xfs_attr_add_fork(sc->ip, sizeof(struct xfs_attr_sf_hdr), 1);
    1155             : }
    1156      138747 : 
    1157             : /*
    1158      138747 :  * Finish replaying stashed parent pointer updates, allocate a transaction for
    1159             :  * swapping extents, and take the ILOCKs of both files before we commit the new
    1160             :  * attribute structure.
    1161             :  */
    1162             : STATIC int
    1163             : xrep_parent_finalize_tempfile(
    1164             :         struct xrep_parent      *rp)
    1165             : {
    1166             :         struct xfs_scrub        *sc = rp->sc;
    1167      138743 :         int                     error;
    1168             : 
    1169             :         error = xrep_parent_replay_updates(rp);
    1170      138743 :         if (error)
    1171      138743 :                 return error;
    1172             : 
    1173      138743 :         error = xrep_parent_ensure_attr_fork(rp);
    1174      138747 :         if (error)
    1175             :                 return error;
    1176             : 
    1177      138747 :         error = xrep_tempswap_trans_alloc(sc, XFS_ATTR_FORK, &rp->tx);
    1178      138747 :         if (error)
    1179             :                 return error;
    1180             : 
    1181      138747 :         /*
    1182      138746 :          * We rely on the caller's hold on @sc->ip's IOLOCK_EXCL to quiesce all
    1183             :          * possible parent pointer updates during the time when we did not hold
    1184             :          * the ILOCK.  There should not be any pptr updates to replay, but
    1185             :          * check anyway.
    1186             :          */
    1187             :         if (xfarray_length(rp->pptr_recs) != 0) {
    1188             :                 ASSERT(xfarray_length(rp->pptr_recs) == 0);
    1189             :                 return -EFSCORRUPTED;
    1190             :         }
    1191      138746 : 
    1192           0 :         return 0;
    1193           0 : }
    1194             : 
    1195             : /*
    1196             :  * Replay all the stashed parent pointers into the temporary file, copy all
    1197             :  * the non-pptr xattrs from the file being repaired into the temporary file,
    1198             :  * and swap the extents atomically.
    1199             :  */
    1200             : STATIC int
    1201             : xrep_parent_rebuild_pptrs(
    1202             :         struct xrep_parent      *rp)
    1203             : {
    1204             :         struct xfs_scrub        *sc = rp->sc;
    1205      138748 :         xfs_ino_t               parent_ino = NULLFSINO;
    1206             :         int                     error;
    1207             : 
    1208      138748 :         /*
    1209      138748 :          * Copy non-ppttr xattrs from the file being repaired into the
    1210      138748 :          * temporary file's xattr structure.  We hold sc->ip's IOLOCK, which
    1211             :          * prevents setxattr/removexattr calls from occurring, but renames
    1212             :          * update the parent pointers without holding IOLOCK.  If we detect
    1213             :          * stale attr structures, we restart the scan but only flush at the
    1214             :          * end.
    1215             :          */
    1216             :         error = xrep_parent_copy_xattrs(rp);
    1217             :         if (error)
    1218             :                 return error;
    1219             : 
    1220      138748 :         /*
    1221      138746 :          * Cancel the empty transaction that we used to walk and copy attrs,
    1222             :          * and drop the ILOCK so that we can take the IOLOCK on the temporary
    1223             :          * file.  We still hold sc->ip's IOLOCK.
    1224             :          */
    1225             :         xchk_trans_cancel(sc);
    1226             :         xchk_iunlock(sc, XFS_ILOCK_EXCL);
    1227             : 
    1228             :         error = xrep_tempfile_iolock_polled(sc);
    1229      138746 :         if (error)
    1230      138747 :                 return error;
    1231             : 
    1232      138745 :         error = xrep_parent_finalize_tempfile(rp);
    1233      138744 :         if (error)
    1234             :                 return error;
    1235             : 
    1236      138744 :         /* Last chance to abort before we start committing pptr fixes. */
    1237      138744 :         if (xchk_should_terminate(sc, &error))
    1238             :                 return error;
    1239             : 
    1240             :         if (xchk_iscan_aborted(&rp->pscan.iscan))
    1241      138742 :                 return -ECANCELED;
    1242           0 : 
    1243             :         /*
    1244      138744 :          * Swap the attr fork and junk the old attr fork contents, which are
    1245             :          * now in the tempfile.
    1246             :          */
    1247             :         error = xrep_xattr_swap(sc, &rp->tx);
    1248             :         if (error)
    1249             :                 return error;
    1250             :         error = xrep_xattr_reset_tempfile_fork(sc);
    1251      138743 :         if (error)
    1252      138746 :                 return error;
    1253             : 
    1254      138746 :         /*
    1255      138748 :          * Roll transaction to detach both inodes from the transaction, then
    1256             :          * drop the ILOCK of the temporary file since we no longer need it.
    1257             :          */
    1258             :         error = xfs_trans_roll(&sc->tp);
    1259             :         if (error)
    1260             :                 return error;
    1261             :         xrep_tempfile_iunlock(sc);
    1262      138748 : 
    1263      138747 :         /*
    1264             :          * We've committed the new parent pointers.  Find at least one parent
    1265      138747 :          * so that we can decide if we're moving this file to the orphanage.
    1266             :          * For this purpose, root directories are their own parents.
    1267             :          */
    1268             :         if (sc->ip == sc->mp->m_rootip || sc->ip == sc->mp->m_metadirip) {
    1269             :                 xrep_findparent_scan_found(&rp->pscan, sc->ip->i_ino);
    1270             :         } else {
    1271             :                 error = xrep_findparent_from_pptrs(sc, &parent_ino);
    1272      138743 :                 if (error)
    1273        1493 :                         return error;
    1274             :                 if (parent_ino != NULLFSINO)
    1275      137250 :                         xrep_findparent_scan_found(&rp->pscan, parent_ino);
    1276      137230 :         }
    1277             :         return 0;
    1278      137230 : }
    1279      137225 : 
    1280             : /*
    1281             :  * Commit the new parent pointer structure (currently only the dotdot entry) to
    1282             :  * the file that we're repairing.
    1283             :  */
    1284             : STATIC int
    1285             : xrep_parent_rebuild_tree(
    1286             :         struct xrep_parent      *rp)
    1287             : {
    1288             :         int                     error;
    1289      138747 : 
    1290             :         if (xfs_has_parent(rp->sc->mp)) {
    1291             :                 error = xrep_parent_rebuild_pptrs(rp);
    1292      138747 :                 if (error)
    1293             :                         return error;
    1294      138747 :         }
    1295      138747 : 
    1296      138733 :         if (rp->pscan.parent_ino == NULLFSINO) {
    1297             :                 if (xrep_orphanage_can_adopt(rp->sc))
    1298             :                         return xrep_parent_move_to_orphanage(rp);
    1299             :                 return -EFSCORRUPTED;
    1300      138733 :         }
    1301           5 : 
    1302             :         if (S_ISDIR(VFS_I(rp->sc->ip)->i_mode))
    1303      138728 :                 return xrep_parent_reset_dotdot(rp);
    1304             : 
    1305             :         return 0;
    1306       29194 : }
    1307             : 
    1308             : /* Set up the filesystem scan so we can look for parents. */
    1309             : STATIC int
    1310             : xrep_parent_setup_scan(
    1311      138784 :         struct xrep_parent      *rp)
    1312             : {
    1313             :         struct xfs_scrub        *sc = rp->sc;
    1314      138784 :         char                    *descr;
    1315      138784 :         struct xfs_da_geometry  *geo = sc->mp->m_attr_geo;
    1316      138784 :         int                     max_len;
    1317      138784 :         int                     error;
    1318      138784 : 
    1319             :         if (!xfs_has_parent(sc->mp))
    1320      138784 :                 return xrep_findparent_scan_start(sc, &rp->pscan);
    1321           0 : 
    1322             :         /* Buffers for copying non-pptr attrs to the tempfile */
    1323             :         rp->xattr_name = kvmalloc(XATTR_NAME_MAX + 1, XCHK_GFP_FLAGS);
    1324      138784 :         if (!rp->xattr_name)
    1325      138785 :                 return -ENOMEM;
    1326             : 
    1327             :         /*
    1328             :          * Allocate enough memory to handle loading local attr values from the
    1329             :          * xfblob data while flushing stashed attrs to the temporary file.
    1330             :          * We only realloc the buffer when salvaging remote attr values, so
    1331             :          * TRY_HARDER means we allocate the maximal attr value size.
    1332             :          */
    1333             :         if (sc->flags & XCHK_TRY_HARDER)
    1334      138785 :                 max_len = XATTR_SIZE_MAX;
    1335             :         else
    1336             :                 max_len = xfs_attr_leaf_entsize_local_max(geo->blksize);
    1337      138785 :         error = xrep_parent_alloc_xattr_value(rp, max_len);
    1338      138785 :         if (error)
    1339      138785 :                 goto out_xattr_name;
    1340           0 : 
    1341             :         /* Set up some staging memory for logging parent pointer updates. */
    1342             :         descr = xchk_xfile_ino_descr(sc, "parent pointer entries");
    1343      138785 :         error = xfarray_create(descr, 0, sizeof(struct xrep_pptr),
    1344      138785 :                         &rp->pptr_recs);
    1345             :         kfree(descr);
    1346      138785 :         if (error)
    1347      138785 :                 goto out_xattr_value;
    1348           0 : 
    1349             :         descr = xchk_xfile_ino_descr(sc, "parent pointer names");
    1350      138785 :         error = xfblob_create(descr, &rp->pptr_names);
    1351      138785 :         kfree(descr);
    1352      138785 :         if (error)
    1353      138785 :                 goto out_recs;
    1354           0 : 
    1355             :         /* Set up some storage for copying attrs before the swap */
    1356             :         descr = xchk_xfile_ino_descr(sc,
    1357      138785 :                                 "parent pointer retained xattr entries");
    1358             :         error = xfarray_create(descr, 0, sizeof(struct xrep_parent_xattr),
    1359      138785 :                         &rp->xattr_records);
    1360             :         kfree(descr);
    1361      138785 :         if (error)
    1362      138785 :                 goto out_names;
    1363           0 : 
    1364             :         descr = xchk_xfile_ino_descr(sc,
    1365      138785 :                                 "parent pointer retained xattr values");
    1366             :         error = xfblob_create(descr, &rp->xattr_blobs);
    1367      138783 :         kfree(descr);
    1368      138785 :         if (error)
    1369      138785 :                 goto out_attr_keys;
    1370           0 : 
    1371             :         error = __xrep_findparent_scan_start(sc, &rp->pscan,
    1372      138785 :                         xrep_parent_live_update);
    1373             :         if (error)
    1374      138785 :                 goto out_attr_values;
    1375           0 : 
    1376             :         return 0;
    1377             : 
    1378             : out_attr_values:
    1379             :         xfblob_destroy(rp->xattr_blobs);
    1380           0 :         rp->xattr_blobs = NULL;
    1381           0 : out_attr_keys:
    1382           0 :         xfarray_destroy(rp->xattr_records);
    1383           0 :         rp->xattr_records = NULL;
    1384           0 : out_names:
    1385           0 :         xfblob_destroy(rp->pptr_names);
    1386           0 :         rp->pptr_names = NULL;
    1387           0 : out_recs:
    1388           0 :         xfarray_destroy(rp->pptr_recs);
    1389           0 :         rp->pptr_recs = NULL;
    1390           0 : out_xattr_value:
    1391           0 :         kvfree(rp->xattr_value);
    1392           0 :         rp->xattr_value = NULL;
    1393           0 : out_xattr_name:
    1394           0 :         kvfree(rp->xattr_name);
    1395           0 :         rp->xattr_name = NULL;
    1396           0 :         return error;
    1397           0 : }
    1398             : 
    1399             : int
    1400             : xrep_parent(
    1401      138785 :         struct xfs_scrub        *sc)
    1402             : {
    1403             :         struct xrep_parent      *rp = sc->buf;
    1404      138785 :         int                     error;
    1405      138785 : 
    1406             :         /*
    1407             :          * When the parent pointers feature is enabled, repairs are committed
    1408             :          * by atomically committing a new xattr structure and reaping the old
    1409             :          * attr fork.  Reaping requires rmap to be enabled.
    1410             :          */
    1411             :         if (xfs_has_parent(sc->mp) && !xfs_has_rmapbt(sc->mp))
    1412      138785 :                 return -EOPNOTSUPP;
    1413             : 
    1414             :         error = xrep_parent_setup_scan(rp);
    1415      138785 :         if (error)
    1416      138785 :                 return error;
    1417             : 
    1418             :         if (xfs_has_parent(sc->mp))
    1419      138785 :                 error = xrep_parent_scan_dirtree(rp);
    1420      138785 :         else
    1421             :                 error = xrep_parent_find_dotdot(rp);
    1422           0 :         if (error)
    1423      138784 :                 goto out_teardown;
    1424          36 : 
    1425             :         /* Last chance to abort before we start committing dotdot fixes. */
    1426             :         if (xchk_should_terminate(sc, &error))
    1427      138748 :                 goto out_teardown;
    1428           0 : 
    1429             :         error = xrep_parent_rebuild_tree(rp);
    1430      138747 :         if (error)
    1431      138736 :                 goto out_teardown;
    1432             : 
    1433             : out_teardown:
    1434      138736 :         xrep_parent_teardown(rp);
    1435      138772 :         return error;
    1436      138783 : }

Generated by: LCOV version 1.14