LCOV - code coverage report
Current view: top level - fs/xfs/scrub - readdir.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 177 178 99.4 %
Date: 2023-07-31 20:08:07 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2022-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_log_format.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_mount.h"
      13             : #include "xfs_inode.h"
      14             : #include "xfs_dir2.h"
      15             : #include "xfs_dir2_priv.h"
      16             : #include "xfs_trace.h"
      17             : #include "xfs_bmap.h"
      18             : #include "xfs_trans.h"
      19             : #include "xfs_error.h"
      20             : #include "scrub/scrub.h"
      21             : #include "scrub/readdir.h"
      22             : 
      23             : /* Call a function for every entry in a shortform directory. */
      24             : STATIC int
      25   669161387 : xchk_dir_walk_sf(
      26             :         struct xfs_scrub        *sc,
      27             :         struct xfs_inode        *dp,
      28             :         xchk_dirent_fn          dirent_fn,
      29             :         void                    *priv)
      30             : {
      31   669161387 :         struct xfs_name         name = {
      32             :                 .name           = ".",
      33             :                 .len            = 1,
      34             :                 .type           = XFS_DIR3_FT_DIR,
      35             :         };
      36   669161387 :         struct xfs_mount        *mp = dp->i_mount;
      37   669161387 :         struct xfs_da_geometry  *geo = mp->m_dir_geo;
      38   669161387 :         struct xfs_dir2_sf_entry *sfep;
      39   669161387 :         struct xfs_dir2_sf_hdr  *sfp;
      40   669161387 :         xfs_ino_t               ino;
      41   669161387 :         xfs_dir2_dataptr_t      dapos;
      42   669161387 :         unsigned int            i;
      43   669161387 :         int                     error;
      44             : 
      45   669161387 :         ASSERT(dp->i_df.if_bytes == dp->i_disk_size);
      46   669161387 :         ASSERT(dp->i_df.if_u1.if_data != NULL);
      47             : 
      48   669161387 :         sfp = (struct xfs_dir2_sf_hdr *)dp->i_df.if_u1.if_data;
      49             : 
      50             :         /* dot entry */
      51   669161387 :         dapos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
      52   669161387 :                         geo->data_entry_offset);
      53             : 
      54   669161387 :         error = dirent_fn(sc, dp, dapos, &name, dp->i_ino, priv);
      55   669164215 :         if (error)
      56             :                 return error;
      57             : 
      58             :         /* dotdot entry */
      59   668738873 :         dapos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
      60   668738873 :                         geo->data_entry_offset +
      61             :                         xfs_dir2_data_entsize(mp, sizeof(".") - 1));
      62   668738873 :         ino = xfs_dir2_sf_get_parent_ino(sfp);
      63   668143045 :         name.name = "..";
      64   668143045 :         name.len = 2;
      65             : 
      66   668143045 :         error = dirent_fn(sc, dp, dapos, &name, ino, priv);
      67   668217039 :         if (error)
      68             :                 return error;
      69             : 
      70             :         /* iterate everything else */
      71   668217039 :         sfep = xfs_dir2_sf_firstentry(sfp);
      72  2918991735 :         for (i = 0; i < sfp->count; i++) {
      73  2249707425 :                 dapos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
      74             :                                 xfs_dir2_sf_get_offset(sfep));
      75  2249707425 :                 ino = xfs_dir2_sf_get_ino(mp, sfp, sfep);
      76  2247759270 :                 name.name = sfep->name;
      77  2247759270 :                 name.len = sfep->namelen;
      78  2247759270 :                 name.type = xfs_dir2_sf_get_ftype(mp, sfep);
      79             : 
      80  2250594817 :                 error = dirent_fn(sc, dp, dapos, &name, ino, priv);
      81  2250834624 :                 if (error)
      82          24 :                         return error;
      83             : 
      84  2250834600 :                 sfep = xfs_dir2_sf_nextentry(mp, sfp, sfep);
      85             :         }
      86             : 
      87             :         return 0;
      88             : }
      89             : 
      90             : /* Call a function for every entry in a block directory. */
      91             : STATIC int
      92     8798920 : xchk_dir_walk_block(
      93             :         struct xfs_scrub        *sc,
      94             :         struct xfs_inode        *dp,
      95             :         xchk_dirent_fn          dirent_fn,
      96             :         void                    *priv)
      97             : {
      98     8798920 :         struct xfs_mount        *mp = dp->i_mount;
      99     8798920 :         struct xfs_da_geometry  *geo = mp->m_dir_geo;
     100     8798920 :         struct xfs_buf          *bp;
     101     8798920 :         unsigned int            off, next_off, end;
     102     8798920 :         int                     error;
     103             : 
     104     8798920 :         error = xfs_dir3_block_read(sc->tp, dp, dp->i_ino, &bp);
     105     8798484 :         if (error)
     106             :                 return error;
     107             : 
     108             :         /* Walk each directory entry. */
     109     8798499 :         end = xfs_dir3_data_end_offset(geo, bp->b_addr);
     110   221794218 :         for (off = geo->data_entry_offset; off < end; off = next_off) {
     111   212995343 :                 struct xfs_name                 name = { };
     112   212995343 :                 struct xfs_dir2_data_unused     *dup = bp->b_addr + off;
     113   212995343 :                 struct xfs_dir2_data_entry      *dep = bp->b_addr + off;
     114   212995343 :                 xfs_ino_t                       ino;
     115   212995343 :                 xfs_dir2_dataptr_t              dapos;
     116             : 
     117             :                 /* Skip an empty entry. */
     118   212995343 :                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
     119    44448202 :                         next_off = off + be16_to_cpu(dup->length);
     120    44448202 :                         continue;
     121             :                 }
     122             : 
     123             :                 /* Otherwise, find the next entry and report it. */
     124   168547141 :                 next_off = off + xfs_dir2_data_entsize(mp, dep->namelen);
     125   168547141 :                 if (next_off > end)
     126             :                         break;
     127             : 
     128   168547141 :                 dapos = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, off);
     129   168547141 :                 ino = be64_to_cpu(dep->inumber);
     130   168547141 :                 name.name = dep->name;
     131   168547141 :                 name.len = dep->namelen;
     132   168547141 :                 name.type = xfs_dir2_data_get_ftype(mp, dep);
     133             : 
     134   168546210 :                 error = dirent_fn(sc, dp, dapos, &name, ino, priv);
     135   168547618 :                 if (error)
     136             :                         break;
     137             :         }
     138             : 
     139     8798875 :         xfs_trans_brelse(sc->tp, bp);
     140     8798875 :         return error;
     141             : }
     142             : 
     143             : /* Read a leaf-format directory buffer. */
     144             : STATIC int
     145     4087070 : xchk_read_leaf_dir_buf(
     146             :         struct xfs_trans        *tp,
     147             :         struct xfs_inode        *dp,
     148             :         struct xfs_da_geometry  *geo,
     149             :         xfs_dir2_off_t          *curoff,
     150             :         struct xfs_buf          **bpp)
     151             : {
     152     4087070 :         struct xfs_iext_cursor  icur;
     153     4087070 :         struct xfs_bmbt_irec    map;
     154     4087070 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(dp, XFS_DATA_FORK);
     155     4087070 :         xfs_dablk_t             last_da;
     156     4087070 :         xfs_dablk_t             map_off;
     157     4087070 :         xfs_dir2_off_t          new_off;
     158             : 
     159     4087070 :         *bpp = NULL;
     160             : 
     161             :         /*
     162             :          * Look for mapped directory blocks at or above the current offset.
     163             :          * Truncate down to the nearest directory block to start the scanning
     164             :          * operation.
     165             :          */
     166     4087070 :         last_da = xfs_dir2_byte_to_da(geo, XFS_DIR2_LEAF_OFFSET);
     167     4087070 :         map_off = xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, *curoff));
     168             : 
     169     4087070 :         if (!xfs_iext_lookup_extent(dp, ifp, map_off, &icur, &map))
     170             :                 return 0;
     171     4087046 :         if (map.br_startoff >= last_da)
     172             :                 return 0;
     173     3947843 :         xfs_trim_extent(&map, map_off, last_da - map_off);
     174             : 
     175             :         /* Read the directory block of that first mapping. */
     176     3947866 :         new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
     177     3947866 :         if (new_off > *curoff)
     178        6168 :                 *curoff = new_off;
     179             : 
     180     3947866 :         return xfs_dir3_data_read(tp, dp, dp->i_ino, map.br_startoff, 0, bpp);
     181             : }
     182             : 
     183             : /* Call a function for every entry in a leaf directory. */
     184             : STATIC int
     185      139227 : xchk_dir_walk_leaf(
     186             :         struct xfs_scrub        *sc,
     187             :         struct xfs_inode        *dp,
     188             :         xchk_dirent_fn          dirent_fn,
     189             :         void                    *priv)
     190             : {
     191      139227 :         struct xfs_mount        *mp = dp->i_mount;
     192      139227 :         struct xfs_da_geometry  *geo = mp->m_dir_geo;
     193      139227 :         struct xfs_buf          *bp = NULL;
     194      139227 :         xfs_dir2_off_t          curoff = 0;
     195      139227 :         unsigned int            offset = 0;
     196      139227 :         int                     error;
     197             : 
     198             :         /* Iterate every directory offset in this directory. */
     199   739377590 :         while (curoff < XFS_DIR2_LEAF_OFFSET) {
     200   739377590 :                 struct xfs_name                 name = { };
     201   739377590 :                 struct xfs_dir2_data_unused     *dup;
     202   739377590 :                 struct xfs_dir2_data_entry      *dep;
     203   739377590 :                 xfs_ino_t                       ino;
     204   739377590 :                 unsigned int                    length;
     205   739377590 :                 xfs_dir2_dataptr_t              dapos;
     206             : 
     207             :                 /*
     208             :                  * If we have no buffer, or we're off the end of the
     209             :                  * current buffer, need to get another one.
     210             :                  */
     211   739377590 :                 if (!bp || offset >= geo->blksize) {
     212     4086868 :                         if (bp) {
     213     3947643 :                                 xfs_trans_brelse(sc->tp, bp);
     214     3947861 :                                 bp = NULL;
     215             :                         }
     216             : 
     217     4087086 :                         error = xchk_read_leaf_dir_buf(sc->tp, dp, geo, &curoff,
     218             :                                         &bp);
     219     4086771 :                         if (error || !bp)
     220             :                                 break;
     221             : 
     222             :                         /*
     223             :                          * Find our position in the block.
     224             :                          */
     225     3947542 :                         offset = geo->data_entry_offset;
     226     3947542 :                         curoff += geo->data_entry_offset;
     227             :                 }
     228             : 
     229             :                 /* Skip an empty entry. */
     230   739238264 :                 dup = bp->b_addr + offset;
     231   739238264 :                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
     232    30709731 :                         length = be16_to_cpu(dup->length);
     233    30709731 :                         offset += length;
     234    30709731 :                         curoff += length;
     235    30709731 :                         continue;
     236             :                 }
     237             : 
     238             :                 /* Otherwise, find the next entry and report it. */
     239   708528533 :                 dep = bp->b_addr + offset;
     240   708528533 :                 length = xfs_dir2_data_entsize(mp, dep->namelen);
     241             : 
     242   708528533 :                 dapos = xfs_dir2_byte_to_dataptr(curoff) & 0x7fffffff;
     243   708528533 :                 ino = be64_to_cpu(dep->inumber);
     244   708528533 :                 name.name = dep->name;
     245   708528533 :                 name.len = dep->namelen;
     246   708528533 :                 name.type = xfs_dir2_data_get_ftype(mp, dep);
     247             : 
     248   708431092 :                 error = dirent_fn(sc, dp, dapos, &name, ino, priv);
     249   708528632 :                 if (error)
     250             :                         break;
     251             : 
     252             :                 /* Advance to the next entry. */
     253   708528632 :                 offset += length;
     254   708528632 :                 curoff += length;
     255             :         }
     256             : 
     257      139229 :         if (bp)
     258           0 :                 xfs_trans_brelse(sc->tp, bp);
     259      139229 :         return error;
     260             : }
     261             : 
     262             : /*
     263             :  * Call a function for every entry in a directory.
     264             :  *
     265             :  * Callers must hold the ILOCK.  File types are XFS_DIR3_FT_*.
     266             :  */
     267             : int
     268   678172567 : xchk_dir_walk(
     269             :         struct xfs_scrub        *sc,
     270             :         struct xfs_inode        *dp,
     271             :         xchk_dirent_fn          dirent_fn,
     272             :         void                    *priv)
     273             : {
     274   678172567 :         struct xfs_da_args      args = {
     275             :                 .dp             = dp,
     276   678172567 :                 .geo            = dp->i_mount->m_dir_geo,
     277   678172567 :                 .trans          = sc->tp,
     278   678172567 :                 .owner          = dp->i_ino,
     279             :         };
     280   678172567 :         bool                    isblock;
     281   678172567 :         int                     error;
     282             : 
     283  1356345134 :         if (xfs_is_shutdown(dp->i_mount))
     284             :                 return -EIO;
     285             : 
     286   678172567 :         ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
     287   678172567 :         ASSERT(xfs_isilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
     288             : 
     289   678213518 :         if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
     290   669275377 :                 return xchk_dir_walk_sf(sc, dp, dirent_fn, priv);
     291             : 
     292             :         /* dir2 functions require that the data fork is loaded */
     293     8938141 :         error = xfs_iread_extents(sc->tp, dp, XFS_DATA_FORK);
     294     8938139 :         if (error)
     295             :                 return error;
     296             : 
     297     8938120 :         error = xfs_dir2_isblock(&args, &isblock);
     298     8938152 :         if (error)
     299             :                 return error;
     300             : 
     301     8938152 :         if (isblock)
     302     8798926 :                 return xchk_dir_walk_block(sc, dp, dirent_fn, priv);
     303             : 
     304      139226 :         return xchk_dir_walk_leaf(sc, dp, dirent_fn, priv);
     305             : }
     306             : 
     307             : /*
     308             :  * Look up the inode number for an exact name in a directory.
     309             :  *
     310             :  * Callers must hold the ILOCK.  File types are XFS_DIR3_FT_*.  Names are not
     311             :  * checked for correctness.
     312             :  */
     313             : int
     314   232820334 : xchk_dir_lookup(
     315             :         struct xfs_scrub        *sc,
     316             :         struct xfs_inode        *dp,
     317             :         const struct xfs_name   *name,
     318             :         xfs_ino_t               *ino)
     319             : {
     320   465646601 :         struct xfs_da_args      args = {
     321             :                 .dp             = dp,
     322   232820334 :                 .geo            = dp->i_mount->m_dir_geo,
     323   232826267 :                 .trans          = sc->tp,
     324   232820334 :                 .name           = name->name,
     325   232820334 :                 .namelen        = name->len,
     326   232820334 :                 .filetype       = name->type,
     327   232820334 :                 .hashval        = xfs_dir2_hashname(dp->i_mount, name),
     328             :                 .whichfork      = XFS_DATA_FORK,
     329             :                 .op_flags       = XFS_DA_OP_OKNOENT,
     330   232826267 :                 .owner          = dp->i_ino,
     331             :         };
     332   232826267 :         bool                    isblock, isleaf;
     333   232826267 :         int                     error;
     334             : 
     335   465652534 :         if (xfs_is_shutdown(dp->i_mount))
     336             :                 return -EIO;
     337             : 
     338             :         /*
     339             :          * A temporary directory's block headers are written with the owner
     340             :          * set to sc->ip, so we must switch the owner here for the lookup.
     341             :          */
     342   232826267 :         if (dp == sc->tempip)
     343      129783 :                 args.owner = sc->ip->i_ino;
     344             : 
     345   232826267 :         ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
     346   232826267 :         ASSERT(xfs_isilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
     347             : 
     348   232839653 :         if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
     349   149968661 :                 error = xfs_dir2_sf_lookup(&args);
     350   149968401 :                 goto out_check_rval;
     351             :         }
     352             : 
     353             :         /* dir2 functions require that the data fork is loaded */
     354    82870992 :         error = xfs_iread_extents(sc->tp, dp, XFS_DATA_FORK);
     355    82871046 :         if (error)
     356             :                 return error;
     357             : 
     358    82871072 :         error = xfs_dir2_isblock(&args, &isblock);
     359    82870786 :         if (error)
     360             :                 return error;
     361             : 
     362    82870786 :         if (isblock) {
     363    10458816 :                 error = xfs_dir2_block_lookup(&args);
     364    10458531 :                 goto out_check_rval;
     365             :         }
     366             : 
     367    72411970 :         error = xfs_dir2_isleaf(&args, &isleaf);
     368    72412367 :         if (error)
     369             :                 return error;
     370             : 
     371    72412367 :         if (isleaf) {
     372     2032946 :                 error = xfs_dir2_leaf_lookup(&args);
     373     2032955 :                 goto out_check_rval;
     374             :         }
     375             : 
     376    70379421 :         error = xfs_dir2_node_lookup(&args);
     377             : 
     378   232841017 : out_check_rval:
     379   232841017 :         if (error == -EEXIST)
     380             :                 error = 0;
     381      129784 :         if (!error)
     382   232711233 :                 *ino = args.inumber;
     383             :         return error;
     384             : }

Generated by: LCOV version 1.14