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-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 187 195 95.9 %
Date: 2023-07-31 20:08:12 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 72152196266 : 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 72152196266 :         struct user_namespace   *sb_userns = mp->m_super->s_user_ns;
      64 72152196266 :         struct xfs_inode        *ip;            /* incore inode pointer */
      65 72152196266 :         struct inode            *inode;
      66 72152196266 :         struct xfs_bulkstat     *buf = bc->buf;
      67 72152196266 :         xfs_extnum_t            nextents;
      68 72152196266 :         int                     error = -EINVAL;
      69 72152196266 :         vfsuid_t                vfsuid;
      70 72152196266 :         vfsgid_t                vfsgid;
      71             : 
      72 72152196266 :         if (xfs_internal_inum(mp, ino))
      73     7223448 :                 goto out_advance;
      74             : 
      75 72133422910 :         error = xfs_iget(mp, tp, ino,
      76             :                          (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
      77             :                          XFS_ILOCK_SHARED, &ip);
      78 73063276634 :         if (error == -ENOENT || error == -EINVAL)
      79     2392466 :                 goto out_advance;
      80 73060884168 :         if (error)
      81        3022 :                 goto out;
      82             : 
      83 73060881146 :         ASSERT(ip != NULL);
      84 73060881146 :         ASSERT(ip->i_imap.im_blkno != 0);
      85 73060881146 :         inode = VFS_I(ip);
      86 73060881146 :         vfsuid = i_uid_into_vfsuid(idmap, inode);
      87 72762031761 :         vfsgid = i_gid_into_vfsgid(idmap, inode);
      88             : 
      89             :         /* If this is a private inode, don't leak its details to userspace. */
      90 72650574302 :         if (IS_PRIVATE(inode)) {
      91       91953 :                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
      92       91924 :                 xfs_irele(ip);
      93       91948 :                 error = -EINVAL;
      94       91948 :                 goto out_advance;
      95             :         }
      96             : 
      97             :         /* xfs_iget returns the following without needing
      98             :          * further change.
      99             :          */
     100 72650482349 :         buf->bs_projectid = ip->i_projid;
     101 72650482349 :         buf->bs_ino = ino;
     102 72650482349 :         buf->bs_uid = from_kuid(sb_userns, vfsuid_into_kuid(vfsuid));
     103 72709226862 :         buf->bs_gid = from_kgid(sb_userns, vfsgid_into_kgid(vfsgid));
     104 72776251397 :         buf->bs_size = ip->i_disk_size;
     105             : 
     106 72776251397 :         buf->bs_nlink = inode->i_nlink;
     107 72776251397 :         buf->bs_atime = inode->i_atime.tv_sec;
     108 72776251397 :         buf->bs_atime_nsec = inode->i_atime.tv_nsec;
     109 72776251397 :         buf->bs_mtime = inode->i_mtime.tv_sec;
     110 72776251397 :         buf->bs_mtime_nsec = inode->i_mtime.tv_nsec;
     111 72776251397 :         buf->bs_ctime = inode->i_ctime.tv_sec;
     112 72776251397 :         buf->bs_ctime_nsec = inode->i_ctime.tv_nsec;
     113 72776251397 :         buf->bs_gen = inode->i_generation;
     114 72776251397 :         buf->bs_mode = inode->i_mode;
     115             : 
     116 72776251397 :         buf->bs_xflags = xfs_ip2xflags(ip);
     117 72378490227 :         buf->bs_extsize_blks = ip->i_extsize;
     118             : 
     119 72378490227 :         nextents = xfs_ifork_nextents(&ip->i_df);
     120 72378490227 :         if (!(bc->breq->flags & XFS_IBULK_NREXT64))
     121 72040333025 :                 buf->bs_extents = min(nextents, XFS_MAX_EXTCNT_DATA_FORK_SMALL);
     122             :         else
     123   338157202 :                 buf->bs_extents64 = nextents;
     124             : 
     125 72378490227 :         xfs_bulkstat_health(ip, buf);
     126 73364525327 :         buf->bs_aextents = xfs_ifork_nextents(&ip->i_af);
     127 73364525327 :         buf->bs_forkoff = xfs_inode_fork_boff(ip);
     128 73364525327 :         buf->bs_version = XFS_BULKSTAT_VERSION_V5;
     129             : 
     130 73364525327 :         if (xfs_has_v3inodes(mp)) {
     131 72259098787 :                 buf->bs_btime = ip->i_crtime.tv_sec;
     132 72259098787 :                 buf->bs_btime_nsec = ip->i_crtime.tv_nsec;
     133 72259098787 :                 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
     134       17716 :                         buf->bs_cowextsize_blks = ip->i_cowextsize;
     135             :         }
     136             : 
     137 73364525327 :         switch (ip->i_df.if_format) {
     138 27265607787 :         case XFS_DINODE_FMT_DEV:
     139 27265607787 :                 buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
     140 27265607787 :                 buf->bs_blksize = BLKDEV_IOSIZE;
     141 27265607787 :                 buf->bs_blocks = 0;
     142 27265607787 :                 break;
     143 14160749828 :         case XFS_DINODE_FMT_LOCAL:
     144 14160749828 :                 buf->bs_rdev = 0;
     145 14160749828 :                 buf->bs_blksize = mp->m_sb.sb_blocksize;
     146 14160749828 :                 buf->bs_blocks = 0;
     147 14160749828 :                 break;
     148 31938167712 :         case XFS_DINODE_FMT_EXTENTS:
     149             :         case XFS_DINODE_FMT_BTREE:
     150 31938167712 :                 buf->bs_rdev = 0;
     151 31938167712 :                 buf->bs_blksize = mp->m_sb.sb_blocksize;
     152 31938167712 :                 buf->bs_blocks = ip->i_nblocks + ip->i_delayed_blks;
     153 31938167712 :                 break;
     154             :         }
     155 73364525327 :         xfs_iunlock(ip, XFS_ILOCK_SHARED);
     156 71679568588 :         xfs_irele(ip);
     157             : 
     158 72959087013 :         error = bc->formatter(bc->breq, buf);
     159 72115152039 :         if (error == -ECANCELED)
     160   484366933 :                 goto out_advance;
     161 71630785106 :         if (error)
     162           0 :                 goto out;
     163             : 
     164 71630785106 : 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 72124859901 :         bc->breq->startino = ino + 1;
     172 72124862923 : out:
     173 72124862923 :         return error;
     174             : }
     175             : 
     176             : /* Bulkstat a single inode. */
     177             : int
     178     1808270 : xfs_bulkstat_one(
     179             :         struct xfs_ibulk        *breq,
     180             :         bulkstat_one_fmt_pf     formatter)
     181             : {
     182     1808270 :         struct xfs_bstat_chunk  bc = {
     183             :                 .formatter      = formatter,
     184             :                 .breq           = breq,
     185             :         };
     186     1808270 :         struct xfs_trans        *tp;
     187     1808270 :         int                     error;
     188             : 
     189     1808270 :         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     1808270 :         ASSERT(breq->icount == 1);
     196             : 
     197     1808270 :         bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
     198             :                         KM_MAYFAIL);
     199     1808307 :         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     1808307 :         error = xfs_trans_alloc_empty(breq->mp, &tp);
     207     1808477 :         if (error)
     208           0 :                 goto out;
     209             : 
     210     1808477 :         error = xfs_bulkstat_one_int(breq->mp, breq->idmap, tp,
     211             :                         breq->startino, &bc);
     212     1808461 :         xfs_trans_cancel(tp);
     213     1808577 : out:
     214     1808577 :         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     1808553 :         if (error == -ECANCELED)
     221      890581 :                 error = 0;
     222             : 
     223             :         return error;
     224             : }
     225             : 
     226             : static int
     227 71908731841 : xfs_bulkstat_iwalk(
     228             :         struct xfs_mount        *mp,
     229             :         struct xfs_trans        *tp,
     230             :         xfs_ino_t               ino,
     231             :         void                    *data)
     232             : {
     233 71908731841 :         struct xfs_bstat_chunk  *bc = data;
     234 71908731841 :         int                     error;
     235             : 
     236 71908731841 :         error = xfs_bulkstat_one_int(mp, bc->breq->idmap, tp, ino, data);
     237             :         /* bulkstat just skips over missing inodes */
     238 72235751192 :         if (error == -ENOENT || error == -EINVAL)
     239     8789893 :                 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   492620133 : xfs_bulkstat_already_done(
     257             :         struct xfs_mount        *mp,
     258             :         xfs_ino_t               startino)
     259             : {
     260   492620133 :         xfs_agnumber_t          agno = XFS_INO_TO_AGNO(mp, startino);
     261   492620133 :         xfs_agino_t             agino = XFS_INO_TO_AGINO(mp, startino);
     262             : 
     263   492620133 :         return agno >= mp->m_sb.sb_agcount ||
     264   492600223 :                startino != XFS_AGINO_TO_INO(mp, agno, agino);
     265             : }
     266             : 
     267             : /* Return stat information in bulk (by-inode) for the filesystem. */
     268             : int
     269   486958043 : xfs_bulkstat(
     270             :         struct xfs_ibulk        *breq,
     271             :         bulkstat_one_fmt_pf     formatter)
     272             : {
     273   486958043 :         struct xfs_bstat_chunk  bc = {
     274             :                 .formatter      = formatter,
     275             :                 .breq           = breq,
     276             :         };
     277   486958043 :         struct xfs_trans        *tp;
     278   486958043 :         unsigned int            iwalk_flags = 0;
     279   486958043 :         int                     error;
     280             : 
     281   486958043 :         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   486958043 :         if (xfs_bulkstat_already_done(breq->mp, breq->startino))
     287             :                 return 0;
     288             : 
     289   486940741 :         bc.buf = kmem_zalloc(sizeof(struct xfs_bulkstat),
     290             :                         KM_MAYFAIL);
     291   486946199 :         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   486946199 :         error = xfs_trans_alloc_empty(breq->mp, &tp);
     299   486971883 :         if (error)
     300           0 :                 goto out;
     301             : 
     302   486971883 :         if (breq->flags & XFS_IBULK_SAME_AG)
     303       87736 :                 iwalk_flags |= XFS_IWALK_SAME_AG;
     304             : 
     305   486971883 :         error = xfs_iwalk(breq->mp, tp, breq->startino, iwalk_flags,
     306             :                         xfs_bulkstat_iwalk, breq->icount, &bc);
     307   487016426 :         xfs_trans_cancel(tp);
     308   487027407 : out:
     309   487027407 :         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   487032977 :         if (breq->ocount > 0)
     319   485277181 :                 error = 0;
     320             : 
     321             :         return error;
     322             : }
     323             : 
     324             : /* Convert bulkstat (v5) to bstat (v1). */
     325             : void
     326 72173953102 : 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 72173953102 :         memset(bs1, 0, sizeof(struct xfs_bstat));
     333 72173953102 :         bs1->bs_ino = bstat->bs_ino;
     334 72173953102 :         bs1->bs_mode = bstat->bs_mode;
     335 72173953102 :         bs1->bs_nlink = bstat->bs_nlink;
     336 72173953102 :         bs1->bs_uid = bstat->bs_uid;
     337 72173953102 :         bs1->bs_gid = bstat->bs_gid;
     338 72173953102 :         bs1->bs_rdev = bstat->bs_rdev;
     339 72173953102 :         bs1->bs_blksize = bstat->bs_blksize;
     340 72173953102 :         bs1->bs_size = bstat->bs_size;
     341 72173953102 :         bs1->bs_atime.tv_sec = bstat->bs_atime;
     342 72173953102 :         bs1->bs_mtime.tv_sec = bstat->bs_mtime;
     343 72173953102 :         bs1->bs_ctime.tv_sec = bstat->bs_ctime;
     344 72173953102 :         bs1->bs_atime.tv_nsec = bstat->bs_atime_nsec;
     345 72173953102 :         bs1->bs_mtime.tv_nsec = bstat->bs_mtime_nsec;
     346 72173953102 :         bs1->bs_ctime.tv_nsec = bstat->bs_ctime_nsec;
     347 72173953102 :         bs1->bs_blocks = bstat->bs_blocks;
     348 72173953102 :         bs1->bs_xflags = bstat->bs_xflags;
     349 72173953102 :         bs1->bs_extsize = XFS_FSB_TO_B(mp, bstat->bs_extsize_blks);
     350 72173953102 :         bs1->bs_extents = bstat->bs_extents;
     351 72173953102 :         bs1->bs_gen = bstat->bs_gen;
     352 72173953102 :         bs1->bs_projid_lo = bstat->bs_projectid & 0xFFFF;
     353 72173953102 :         bs1->bs_forkoff = bstat->bs_forkoff;
     354 72173953102 :         bs1->bs_projid_hi = bstat->bs_projectid >> 16;
     355 72173953102 :         bs1->bs_sick = bstat->bs_sick;
     356 72173953102 :         bs1->bs_checked = bstat->bs_checked;
     357 72173953102 :         bs1->bs_cowextsize = XFS_FSB_TO_B(mp, bstat->bs_cowextsize_blks);
     358 72173953102 :         bs1->bs_dmevmask = 0;
     359 72173953102 :         bs1->bs_dmstate = 0;
     360 72173953102 :         bs1->bs_aextents = bstat->bs_aextents;
     361 72173953102 : }
     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     8101199 : 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    16202398 :         struct xfs_inumbers     inogrp = {
     392     8101199 :                 .xi_startino    = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino),
     393     8101199 :                 .xi_alloccount  = irec->ir_count - irec->ir_freecount,
     394     8101199 :                 .xi_allocmask   = ~irec->ir_free,
     395             :                 .xi_version     = XFS_INUMBERS_VERSION_V5,
     396             :         };
     397     8101199 :         struct xfs_inumbers_chunk *ic = data;
     398     8101199 :         int                     error;
     399             : 
     400     8101199 :         error = ic->formatter(ic->breq, &inogrp);
     401     8127993 :         if (error && error != -ECANCELED)
     402             :                 return error;
     403             : 
     404     8127993 :         ic->breq->startino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino) +
     405             :                         XFS_INODES_PER_CHUNK;
     406     8127993 :         return error;
     407             : }
     408             : 
     409             : /*
     410             :  * Return inode number table for the filesystem.
     411             :  */
     412             : int
     413     5669925 : xfs_inumbers(
     414             :         struct xfs_ibulk        *breq,
     415             :         inumbers_fmt_pf         formatter)
     416             : {
     417     5669925 :         struct xfs_inumbers_chunk ic = {
     418             :                 .formatter      = formatter,
     419             :                 .breq           = breq,
     420             :         };
     421     5669925 :         struct xfs_trans        *tp;
     422     5669925 :         int                     error = 0;
     423             : 
     424     5669925 :         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     5663208 :         error = xfs_trans_alloc_empty(breq->mp, &tp);
     432     5667586 :         if (error)
     433           0 :                 goto out;
     434             : 
     435     5667586 :         error = xfs_inobt_walk(breq->mp, tp, breq->startino, breq->flags,
     436             :                         xfs_inumbers_walk, breq->icount, &ic);
     437     5669755 :         xfs_trans_cancel(tp);
     438     5661544 : 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     5661544 :         if (breq->ocount > 0)
     448     5226362 :                 error = 0;
     449             : 
     450             :         return error;
     451             : }
     452             : 
     453             : /* Convert an inumbers (v5) struct to a inogrp (v1) struct. */
     454             : void
     455       71975 : 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       71975 :         memset(ig1, 0, sizeof(struct xfs_inogrp));
     461       71975 :         ig1->xi_startino = ig->xi_startino;
     462       71975 :         ig1->xi_alloccount = ig->xi_alloccount;
     463       71975 :         ig1->xi_allocmask = ig->xi_allocmask;
     464       71975 : }

Generated by: LCOV version 1.14