LCOV - code coverage report
Current view: top level - fs/xfs - xfs_itable.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 187 195 95.9 %
Date: 2023-07-31 20:08:07 Functions: 9 9 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
       4             :  * All Rights Reserved.
       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_btree.h"
      15             : #include "xfs_ialloc.h"
      16             : #include "xfs_ialloc_btree.h"
      17             : #include "xfs_iwalk.h"
      18             : #include "xfs_itable.h"
      19             : #include "xfs_error.h"
      20             : #include "xfs_icache.h"
      21             : #include "xfs_health.h"
      22             : #include "xfs_trans.h"
      23             : 
      24             : /*
      25             :  * Bulk Stat
      26             :  * =========
      27             :  *
      28             :  * Use the inode walking functions to fill out struct xfs_bulkstat for every
      29             :  * allocated inode, then pass the stat information to some externally provided
      30             :  * iteration function.
      31             :  */
      32             : 
      33             : struct xfs_bstat_chunk {
      34             :         bulkstat_one_fmt_pf     formatter;
      35             :         struct xfs_ibulk        *breq;
      36             :         struct xfs_bulkstat     *buf;
      37             : };
      38             : 
      39             : /*
      40             :  * Fill out the bulkstat info for a single inode and report it somewhere.
      41             :  *
      42             :  * bc->breq->lastino is effectively the inode cursor as we walk through the
      43             :  * filesystem.  Therefore, we update it any time we need to move the cursor
      44             :  * forward, regardless of whether or not we're sending any bstat information
      45             :  * back to userspace.  If the inode is internal metadata or, has been freed
      46             :  * out from under us, we just simply keep going.
      47             :  *
      48             :  * However, if any other type of error happens we want to stop right where we
      49             :  * are so that userspace will call back with exact number of the bad inode and
      50             :  * we can send back an error code.
      51             :  *
      52             :  * Note that if the formatter tells us there's no space left in the buffer we
      53             :  * move the cursor forward and abort the walk.
      54             :  */
      55             : STATIC int
      56 60166854643 : xfs_bulkstat_one_int(
      57             :         struct xfs_mount        *mp,
      58             :         struct mnt_idmap        *idmap,
      59             :         struct xfs_trans        *tp,
      60             :         xfs_ino_t               ino,
      61             :         struct xfs_bstat_chunk  *bc)
      62             : {
      63 60166854643 :         struct user_namespace   *sb_userns = mp->m_super->s_user_ns;
      64 60166854643 :         struct xfs_inode        *ip;            /* incore inode pointer */
      65 60166854643 :         struct inode            *inode;
      66 60166854643 :         struct xfs_bulkstat     *buf = bc->buf;
      67 60166854643 :         xfs_extnum_t            nextents;
      68 60166854643 :         int                     error = -EINVAL;
      69 60166854643 :         vfsuid_t                vfsuid;
      70 60166854643 :         vfsgid_t                vfsgid;
      71             : 
      72 60166854643 :         if (xfs_internal_inum(mp, ino))
      73     4932660 :                 goto out_advance;
      74             : 
      75 60410562829 :         error = xfs_iget(mp, tp, ino,
      76             :                          (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
      77             :                          XFS_ILOCK_SHARED, &ip);
      78 59821021524 :         if (error == -ENOENT || error == -EINVAL)
      79     1285226 :                 goto out_advance;
      80 59819736298 :         if (error)
      81        1731 :                 goto out;
      82             : 
      83 59819734567 :         ASSERT(ip != NULL);
      84 59819734567 :         ASSERT(ip->i_imap.im_blkno != 0);
      85 59819734567 :         inode = VFS_I(ip);
      86 59819734567 :         vfsuid = i_uid_into_vfsuid(idmap, inode);
      87 60426333035 :         vfsgid = i_gid_into_vfsgid(idmap, inode);
      88             : 
      89             :         /* If this is a private inode, don't leak its details to userspace. */
      90 60434434678 :         if (IS_PRIVATE(inode)) {
      91       40975 :                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
      92       40975 :                 xfs_irele(ip);
      93       40975 :                 error = -EINVAL;
      94       40975 :                 goto out_advance;
      95             :         }
      96             : 
      97             :         /* xfs_iget returns the following without needing
      98             :          * further change.
      99             :          */
     100 60434393703 :         buf->bs_projectid = ip->i_projid;
     101 60434393703 :         buf->bs_ino = ino;
     102 60434393703 :         buf->bs_uid = from_kuid(sb_userns, vfsuid_into_kuid(vfsuid));
     103 60146251190 :         buf->bs_gid = from_kgid(sb_userns, vfsgid_into_kgid(vfsgid));
     104 60443038646 :         buf->bs_size = ip->i_disk_size;
     105             : 
     106 60443038646 :         buf->bs_nlink = inode->i_nlink;
     107 60443038646 :         buf->bs_atime = inode->i_atime.tv_sec;
     108 60443038646 :         buf->bs_atime_nsec = inode->i_atime.tv_nsec;
     109 60443038646 :         buf->bs_mtime = inode->i_mtime.tv_sec;
     110 60443038646 :         buf->bs_mtime_nsec = inode->i_mtime.tv_nsec;
     111 60443038646 :         buf->bs_ctime = inode->i_ctime.tv_sec;
     112 60443038646 :         buf->bs_ctime_nsec = inode->i_ctime.tv_nsec;
     113 60443038646 :         buf->bs_gen = inode->i_generation;
     114 60443038646 :         buf->bs_mode = inode->i_mode;
     115             : 
     116 60443038646 :         buf->bs_xflags = xfs_ip2xflags(ip);
     117 60277032387 :         buf->bs_extsize_blks = ip->i_extsize;
     118             : 
     119 60277032387 :         nextents = xfs_ifork_nextents(&ip->i_df);
     120 60277032387 :         if (!(bc->breq->flags & XFS_IBULK_NREXT64))
     121 60098693078 :                 buf->bs_extents = min(nextents, XFS_MAX_EXTCNT_DATA_FORK_SMALL);
     122             :         else
     123   178339309 :                 buf->bs_extents64 = nextents;
     124             : 
     125 60277032387 :         xfs_bulkstat_health(ip, buf);
     126 60464316788 :         buf->bs_aextents = xfs_ifork_nextents(&ip->i_af);
     127 60464316788 :         buf->bs_forkoff = xfs_inode_fork_boff(ip);
     128 60464316788 :         buf->bs_version = XFS_BULKSTAT_VERSION_V5;
     129             : 
     130 60464316788 :         if (xfs_has_v3inodes(mp)) {
     131 59945406122 :                 buf->bs_btime = ip->i_crtime.tv_sec;
     132 59945406122 :                 buf->bs_btime_nsec = ip->i_crtime.tv_nsec;
     133 59945406122 :                 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
     134        7211 :                         buf->bs_cowextsize_blks = ip->i_cowextsize;
     135             :         }
     136             : 
     137 60464316788 :         switch (ip->i_df.if_format) {
     138 22642530278 :         case XFS_DINODE_FMT_DEV:
     139 22642530278 :                 buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
     140 22642530278 :                 buf->bs_blksize = BLKDEV_IOSIZE;
     141 22642530278 :                 buf->bs_blocks = 0;
     142 22642530278 :                 break;
     143 11743526861 :         case XFS_DINODE_FMT_LOCAL:
     144 11743526861 :                 buf->bs_rdev = 0;
     145 11743526861 :                 buf->bs_blksize = mp->m_sb.sb_blocksize;
     146 11743526861 :                 buf->bs_blocks = 0;
     147 11743526861 :                 break;
     148 26078259649 :         case XFS_DINODE_FMT_EXTENTS:
     149             :         case XFS_DINODE_FMT_BTREE:
     150 26078259649 :                 buf->bs_rdev = 0;
     151 26078259649 :                 buf->bs_blksize = mp->m_sb.sb_blocksize;
     152 26078259649 :                 buf->bs_blocks = ip->i_nblocks + ip->i_delayed_blks;
     153 26078259649 :                 break;
     154             :         }
     155 60464316788 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
     156 60402244933 :         xfs_irele(ip);
     157             : 
     158 60319652119 :         error = bc->formatter(bc->breq, buf);
     159 60345931379 :         if (error == -ECANCELED)
     160   398100019 :                 goto out_advance;
     161 59947831360 :         if (error)
     162           0 :                 goto out;
     163             : 
     164 59947831360 : out_advance:
     165             :         /*
     166             :          * Advance the cursor to the inode that comes after the one we just
     167             :          * looked at.  We want the caller to move along if the bulkstat
     168             :          * information was copied successfully; if we tried to grab the inode
     169             :          * but it's no longer allocated; or if it's internal metadata.
     170             :          */
     171 60352190240 :         bc->breq->startino = ino + 1;
     172 60352191971 : out:
     173 60352191971 :         return error;
     174             : }
     175             : 
     176             : /* Bulkstat a single inode. */
     177             : int
     178     1148796 : xfs_bulkstat_one(
     179             :         struct xfs_ibulk        *breq,
     180             :         bulkstat_one_fmt_pf     formatter)
     181             : {
     182     1148796 :         struct xfs_bstat_chunk  bc = {
     183             :                 .formatter      = formatter,
     184             :                 .breq           = breq,
     185             :         };
     186     1148796 :         struct xfs_trans        *tp;
     187     1148796 :         int                     error;
     188             : 
     189     1148796 :         if (breq->idmap != &nop_mnt_idmap) {
     190           0 :                 xfs_warn_ratelimited(breq->mp,
     191             :                         "bulkstat not supported inside of idmapped mounts.");
     192           0 :                 return -EINVAL;
     193             :         }
     194             : 
     195     1148796 :         ASSERT(breq->icount == 1);
     196             : 
     197     1148796 :         bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
     198             :                         KM_MAYFAIL);
     199     1148796 :         if (!bc.buf)
     200             :                 return -ENOMEM;
     201             : 
     202             :         /*
     203             :          * Grab an empty transaction so that we can use its recursive buffer
     204             :          * locking abilities to detect cycles in the inobt without deadlocking.
     205             :          */
     206     1148796 :         error = xfs_trans_alloc_empty(breq->mp, &tp);
     207     1148797 :         if (error)
     208           0 :                 goto out;
     209             : 
     210     1148797 :         error = xfs_bulkstat_one_int(breq->mp, breq->idmap, tp,
     211             :                         breq->startino, &bc);
     212     1148795 :         xfs_trans_cancel(tp);
     213     1148798 : out:
     214     1148798 :         kmem_free(bc.buf);
     215             : 
     216             :         /*
     217             :          * If we reported one inode to userspace then we abort because we hit
     218             :          * the end of the buffer.  Don't leak that back to userspace.
     219             :          */
     220     1148798 :         if (error == -ECANCELED)
     221      553388 :                 error = 0;
     222             : 
     223             :         return error;
     224             : }
     225             : 
     226             : static int
     227 60372708890 : xfs_bulkstat_iwalk(
     228             :         struct xfs_mount        *mp,
     229             :         struct xfs_trans        *tp,
     230             :         xfs_ino_t               ino,
     231             :         void                    *data)
     232             : {
     233 60372708890 :         struct xfs_bstat_chunk  *bc = data;
     234 60372708890 :         int                     error;
     235             : 
     236 60372708890 :         error = xfs_bulkstat_one_int(mp, bc->breq->idmap, tp, ino, data);
     237             :         /* bulkstat just skips over missing inodes */
     238 60409997815 :         if (error == -ENOENT || error == -EINVAL)
     239     5663539 :                 return 0;
     240             :         return error;
     241             : }
     242             : 
     243             : /*
     244             :  * Check the incoming lastino parameter.
     245             :  *
     246             :  * We allow any inode value that could map to physical space inside the
     247             :  * filesystem because if there are no inodes there, bulkstat moves on to the
     248             :  * next chunk.  In other words, the magic agino value of zero takes us to the
     249             :  * first chunk in the AG, and an agino value past the end of the AG takes us to
     250             :  * the first chunk in the next AG.
     251             :  *
     252             :  * Therefore we can end early if the requested inode is beyond the end of the
     253             :  * filesystem or doesn't map properly.
     254             :  */
     255             : static inline bool
     256   402917841 : xfs_bulkstat_already_done(
     257             :         struct xfs_mount        *mp,
     258             :         xfs_ino_t               startino)
     259             : {
     260   402917841 :         xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, startino);
     261   402917841 :         xfs_agino_t             agino = XFS_INO_TO_AGINO(mp, startino);
     262             : 
     263   402917841 :         return agno >= mp->m_sb.sb_agcount ||
     264   402926512 :                startino != XFS_AGINO_TO_INO(mp, agno, agino);
     265             : }
     266             : 
     267             : /* Return stat information in bulk (by-inode) for the filesystem. */
     268             : int
     269   399787181 : xfs_bulkstat(
     270             :         struct xfs_ibulk        *breq,
     271             :         bulkstat_one_fmt_pf     formatter)
     272             : {
     273   399787181 :         struct xfs_bstat_chunk  bc = {
     274             :                 .formatter      = formatter,
     275             :                 .breq           = breq,
     276             :         };
     277   399787181 :         struct xfs_trans        *tp;
     278   399787181 :         unsigned int            iwalk_flags = 0;
     279   399787181 :         int                     error;
     280             : 
     281   399787181 :         if (breq->idmap != &nop_mnt_idmap) {
     282           0 :                 xfs_warn_ratelimited(breq->mp,
     283             :                         "bulkstat not supported inside of idmapped mounts.");
     284           0 :                 return -EINVAL;
     285             :         }
     286   399787181 :         if (xfs_bulkstat_already_done(breq->mp, breq->startino))
     287             :                 return 0;
     288             : 
     289   399805534 :         bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
     290             :                         KM_MAYFAIL);
     291   399800102 :         if (!bc.buf)
     292             :                 return -ENOMEM;
     293             : 
     294             :         /*
     295             :          * Grab an empty transaction so that we can use its recursive buffer
     296             :          * locking abilities to detect cycles in the inobt without deadlocking.
     297             :          */
     298   399800102 :         error = xfs_trans_alloc_empty(breq->mp, &tp);
     299   399794516 :         if (error)
     300           0 :                 goto out;
     301             : 
     302   399794516 :         if (breq->flags & XFS_IBULK_SAME_AG)
     303       15952 :                 iwalk_flags |= XFS_IWALK_SAME_AG;
     304             : 
     305   399794516 :         error = xfs_iwalk(breq->mp, tp, breq->startino, iwalk_flags,
     306             :                         xfs_bulkstat_iwalk, breq->icount, &bc);
     307   399816344 :         xfs_trans_cancel(tp);
     308   399811656 : out:
     309   399811656 :         kmem_free(bc.buf);
     310             : 
     311             :         /*
     312             :          * We found some inodes, so clear the error status and return them.
     313             :          * The lastino pointer will point directly at the inode that triggered
     314             :          * any error that occurred, so on the next call the error will be
     315             :          * triggered again and propagated to userspace as there will be no
     316             :          * formatted inodes in the buffer.
     317             :          */
     318   399815907 :         if (breq->ocount > 0)
     319   398687678 :                 error = 0;
     320             : 
     321             :         return error;
     322             : }
     323             : 
     324             : /* Convert bulkstat (v5) to bstat (v1). */
     325             : void
     326 60144702831 : xfs_bulkstat_to_bstat(
     327             :         struct xfs_mount                *mp,
     328             :         struct xfs_bstat                *bs1,
     329             :         const struct xfs_bulkstat       *bstat)
     330             : {
     331             :         /* memset is needed here because of padding holes in the structure. */
     332 60144702831 :         memset(bs1, 0, sizeof(struct xfs_bstat));
     333 60144702831 :         bs1->bs_ino = bstat->bs_ino;
     334 60144702831 :         bs1->bs_mode = bstat->bs_mode;
     335 60144702831 :         bs1->bs_nlink = bstat->bs_nlink;
     336 60144702831 :         bs1->bs_uid = bstat->bs_uid;
     337 60144702831 :         bs1->bs_gid = bstat->bs_gid;
     338 60144702831 :         bs1->bs_rdev = bstat->bs_rdev;
     339 60144702831 :         bs1->bs_blksize = bstat->bs_blksize;
     340 60144702831 :         bs1->bs_size = bstat->bs_size;
     341 60144702831 :         bs1->bs_atime.tv_sec = bstat->bs_atime;
     342 60144702831 :         bs1->bs_mtime.tv_sec = bstat->bs_mtime;
     343 60144702831 :         bs1->bs_ctime.tv_sec = bstat->bs_ctime;
     344 60144702831 :         bs1->bs_atime.tv_nsec = bstat->bs_atime_nsec;
     345 60144702831 :         bs1->bs_mtime.tv_nsec = bstat->bs_mtime_nsec;
     346 60144702831 :         bs1->bs_ctime.tv_nsec = bstat->bs_ctime_nsec;
     347 60144702831 :         bs1->bs_blocks = bstat->bs_blocks;
     348 60144702831 :         bs1->bs_xflags = bstat->bs_xflags;
     349 60144702831 :         bs1->bs_extsize = XFS_FSB_TO_B(mp, bstat->bs_extsize_blks);
     350 60144702831 :         bs1->bs_extents = bstat->bs_extents;
     351 60144702831 :         bs1->bs_gen = bstat->bs_gen;
     352 60144702831 :         bs1->bs_projid_lo = bstat->bs_projectid & 0xFFFF;
     353 60144702831 :         bs1->bs_forkoff = bstat->bs_forkoff;
     354 60144702831 :         bs1->bs_projid_hi = bstat->bs_projectid >> 16;
     355 60144702831 :         bs1->bs_sick = bstat->bs_sick;
     356 60144702831 :         bs1->bs_checked = bstat->bs_checked;
     357 60144702831 :         bs1->bs_cowextsize = XFS_FSB_TO_B(mp, bstat->bs_cowextsize_blks);
     358 60144702831 :         bs1->bs_dmevmask = 0;
     359 60144702831 :         bs1->bs_dmstate = 0;
     360 60144702831 :         bs1->bs_aextents = bstat->bs_aextents;
     361 60144702831 : }
     362             : 
     363             : struct xfs_inumbers_chunk {
     364             :         inumbers_fmt_pf         formatter;
     365             :         struct xfs_ibulk        *breq;
     366             : };
     367             : 
     368             : /*
     369             :  * INUMBERS
     370             :  * ========
     371             :  * This is how we export inode btree records to userspace, so that XFS tools
     372             :  * can figure out where inodes are allocated.
     373             :  */
     374             : 
     375             : /*
     376             :  * Format the inode group structure and report it somewhere.
     377             :  *
     378             :  * Similar to xfs_bulkstat_one_int, lastino is the inode cursor as we walk
     379             :  * through the filesystem so we move it forward unless there was a runtime
     380             :  * error.  If the formatter tells us the buffer is now full we also move the
     381             :  * cursor forward and abort the walk.
     382             :  */
     383             : STATIC int
     384     4699953 : xfs_inumbers_walk(
     385             :         struct xfs_mount        *mp,
     386             :         struct xfs_trans        *tp,
     387             :         xfs_agnumber_t          agno,
     388             :         const struct xfs_inobt_rec_incore *irec,
     389             :         void                    *data)
     390             : {
     391     4699953 :         struct xfs_inumbers     inogrp = {
     392     4699953 :                 .xi_startino    = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino),
     393     4699953 :                 .xi_alloccount  = irec->ir_count - irec->ir_freecount,
     394     4699953 :                 .xi_allocmask   = ~irec->ir_free,
     395             :                 .xi_version     = XFS_INUMBERS_VERSION_V5,
     396             :         };
     397     4699953 :         struct xfs_inumbers_chunk *ic = data;
     398     4699953 :         int                     error;
     399             : 
     400     4699953 :         error = ic->formatter(ic->breq, &inogrp);
     401     4698678 :         if (error && error != -ECANCELED)
     402             :                 return error;
     403             : 
     404     4698678 :         ic->breq->startino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino) +
     405             :                         XFS_INODES_PER_CHUNK;
     406     4698678 :         return error;
     407             : }
     408             : 
     409             : /*
     410             :  * Return inode number table for the filesystem.
     411             :  */
     412             : int
     413     3119861 : xfs_inumbers(
     414             :         struct xfs_ibulk        *breq,
     415             :         inumbers_fmt_pf         formatter)
     416             : {
     417     3119861 :         struct xfs_inumbers_chunk ic = {
     418             :                 .formatter      = formatter,
     419             :                 .breq           = breq,
     420             :         };
     421     3119861 :         struct xfs_trans        *tp;
     422     3119861 :         int                     error = 0;
     423             : 
     424     3119861 :         if (xfs_bulkstat_already_done(breq->mp, breq->startino))
     425             :                 return 0;
     426             : 
     427             :         /*
     428             :          * Grab an empty transaction so that we can use its recursive buffer
     429             :          * locking abilities to detect cycles in the inobt without deadlocking.
     430             :          */
     431     3119765 :         error = xfs_trans_alloc_empty(breq->mp, &tp);
     432     3119517 :         if (error)
     433           0 :                 goto out;
     434             : 
     435     3119517 :         error = xfs_inobt_walk(breq->mp, tp, breq->startino, breq->flags,
     436             :                         xfs_inumbers_walk, breq->icount, &ic);
     437     3119530 :         xfs_trans_cancel(tp);
     438     3119756 : out:
     439             : 
     440             :         /*
     441             :          * We found some inode groups, so clear the error status and return
     442             :          * them.  The lastino pointer will point directly at the inode that
     443             :          * triggered any error that occurred, so on the next call the error
     444             :          * will be triggered again and propagated to userspace as there will be
     445             :          * no formatted inode groups in the buffer.
     446             :          */
     447     3119756 :         if (breq->ocount > 0)
     448     2968122 :                 error = 0;
     449             : 
     450             :         return error;
     451             : }
     452             : 
     453             : /* Convert an inumbers (v5) struct to a inogrp (v1) struct. */
     454             : void
     455       11506 : xfs_inumbers_to_inogrp(
     456             :         struct xfs_inogrp               *ig1,
     457             :         const struct xfs_inumbers       *ig)
     458             : {
     459             :         /* memset is needed here because of padding holes in the structure. */
     460       11506 :         memset(ig1, 0, sizeof(struct xfs_inogrp));
     461       11506 :         ig1->xi_startino = ig->xi_startino;
     462       11506 :         ig1->xi_alloccount = ig->xi_alloccount;
     463       11506 :         ig1->xi_allocmask = ig->xi_allocmask;
     464       11506 : }

Generated by: LCOV version 1.14