LCOV - code coverage report
Current view: top level - fs/xfs - xfs_inode_item.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 454 493 92.1 %
Date: 2023-07-31 20:08:34 Functions: 27 28 96.4 %

          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_trans.h"
      15             : #include "xfs_inode_item.h"
      16             : #include "xfs_trace.h"
      17             : #include "xfs_trans_priv.h"
      18             : #include "xfs_buf_item.h"
      19             : #include "xfs_log.h"
      20             : #include "xfs_log_priv.h"
      21             : #include "xfs_error.h"
      22             : #include "xfs_rtbitmap.h"
      23             : 
      24             : #include <linux/iversion.h>
      25             : 
      26             : struct kmem_cache       *xfs_ili_cache;         /* inode log item */
      27             : 
      28             : static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip)
      29             : {
      30             :         return container_of(lip, struct xfs_inode_log_item, ili_item);
      31             : }
      32             : 
      33             : static uint64_t
      34  5318643959 : xfs_inode_item_sort(
      35             :         struct xfs_log_item     *lip)
      36             : {
      37  5318643959 :         return INODE_ITEM(lip)->ili_inode->i_ino;
      38             : }
      39             : 
      40             : /*
      41             :  * Prior to finally logging the inode, we have to ensure that all the
      42             :  * per-modification inode state changes are applied. This includes VFS inode
      43             :  * state updates, format conversions, verifier state synchronisation and
      44             :  * ensuring the inode buffer remains in memory whilst the inode is dirty.
      45             :  *
      46             :  * We have to be careful when we grab the inode cluster buffer due to lock
      47             :  * ordering constraints. The unlinked inode modifications (xfs_iunlink_item)
      48             :  * require AGI -> inode cluster buffer lock order. The inode cluster buffer is
      49             :  * not locked until ->precommit, so it happens after everything else has been
      50             :  * modified.
      51             :  *
      52             :  * Further, we have AGI -> AGF lock ordering, and with O_TMPFILE handling we
      53             :  * have AGI -> AGF -> iunlink item -> inode cluster buffer lock order. Hence we
      54             :  * cannot safely lock the inode cluster buffer in xfs_trans_log_inode() because
      55             :  * it can be called on a inode (e.g. via bumplink/droplink) before we take the
      56             :  * AGF lock modifying directory blocks.
      57             :  *
      58             :  * Rather than force a complete rework of all the transactions to call
      59             :  * xfs_trans_log_inode() once and once only at the end of every transaction, we
      60             :  * move the pinning of the inode cluster buffer to a ->precommit operation. This
      61             :  * matches how the xfs_iunlink_item locks the inode cluster buffer, and it
      62             :  * ensures that the inode cluster buffer locking is always done last in a
      63             :  * transaction. i.e. we ensure the lock order is always AGI -> AGF -> inode
      64             :  * cluster buffer.
      65             :  *
      66             :  * If we return the inode number as the precommit sort key then we'll also
      67             :  * guarantee that the order all inode cluster buffer locking is the same all the
      68             :  * inodes and unlink items in the transaction.
      69             :  */
      70             : static int
      71  4856011487 : xfs_inode_item_precommit(
      72             :         struct xfs_trans        *tp,
      73             :         struct xfs_log_item     *lip)
      74             : {
      75  4856011487 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
      76  4856011487 :         struct xfs_inode        *ip = iip->ili_inode;
      77  4856011487 :         struct inode            *inode = VFS_I(ip);
      78  4856011487 :         unsigned int            flags = iip->ili_dirty_flags;
      79             : 
      80             :         /*
      81             :          * Don't bother with i_lock for the I_DIRTY_TIME check here, as races
      82             :          * don't matter - we either will need an extra transaction in 24 hours
      83             :          * to log the timestamps, or will clear already cleared fields in the
      84             :          * worst case.
      85             :          */
      86  4856011487 :         if (inode->i_state & I_DIRTY_TIME) {
      87          25 :                 spin_lock(&inode->i_lock);
      88          25 :                 inode->i_state &= ~I_DIRTY_TIME;
      89          25 :                 spin_unlock(&inode->i_lock);
      90             :         }
      91             : 
      92             :         /*
      93             :          * If we're updating the inode core or the timestamps and it's possible
      94             :          * to upgrade this inode to bigtime format, do so now.
      95             :          */
      96  4856011487 :         if ((flags & (XFS_ILOG_CORE | XFS_ILOG_TIMESTAMP)) &&
      97  4383960731 :             xfs_has_bigtime(ip->i_mount) &&
      98             :             !xfs_inode_has_bigtime(ip)) {
      99         563 :                 ip->i_diflags2 |= XFS_DIFLAG2_BIGTIME;
     100         563 :                 flags |= XFS_ILOG_CORE;
     101             :         }
     102             : 
     103             :         /*
     104             :          * Inode verifiers do not check that the extent size hint is an integer
     105             :          * multiple of the rt extent size on a directory with both rtinherit
     106             :          * and extszinherit flags set.  If we're logging a directory that is
     107             :          * misconfigured in this way, clear the hint.
     108             :          */
     109  4856011487 :         if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
     110        4980 :             (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) &&
     111        4980 :             xfs_extlen_to_rtxmod(ip->i_mount, ip->i_extsize) > 0) {
     112           2 :                 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
     113             :                                    XFS_DIFLAG_EXTSZINHERIT);
     114           2 :                 ip->i_extsize = 0;
     115           2 :                 flags |= XFS_ILOG_CORE;
     116             :         }
     117             : 
     118             :         /*
     119             :          * Record the specific change for fdatasync optimisation. This allows
     120             :          * fdatasync to skip log forces for inodes that are only timestamp
     121             :          * dirty. Once we've processed the XFS_ILOG_IVERSION flag, convert it
     122             :          * to XFS_ILOG_CORE so that the actual on-disk dirty tracking
     123             :          * (ili_fields) correctly tracks that the version has changed.
     124             :          */
     125  4856011487 :         spin_lock(&iip->ili_lock);
     126  4859408832 :         iip->ili_fsync_fields |= (flags & ~XFS_ILOG_IVERSION);
     127  4859408832 :         if (flags & XFS_ILOG_IVERSION)
     128  3218767076 :                 flags = ((flags & ~XFS_ILOG_IVERSION) | XFS_ILOG_CORE);
     129             : 
     130             :         /*
     131             :          * Inode verifiers do not check that the CoW extent size hint is an
     132             :          * integer multiple of the rt extent size on a directory with both
     133             :          * rtinherit and cowextsize flags set.  If we're logging a directory
     134             :          * that is misconfigured in this way, clear the hint.
     135             :          */
     136  4859408832 :         if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
     137   417353393 :             (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) &&
     138        1630 :             xfs_extlen_to_rtxmod(ip->i_mount, ip->i_cowextsize) > 0) {
     139           1 :                 ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
     140           1 :                 ip->i_cowextsize = 0;
     141           1 :                 flags |= XFS_ILOG_CORE;
     142             :         }
     143             : 
     144  4859408832 :         if (!iip->ili_item.li_buf) {
     145   375335384 :                 struct xfs_buf  *bp;
     146   375335384 :                 int             error;
     147             : 
     148             :                 /*
     149             :                  * We hold the ILOCK here, so this inode is not going to be
     150             :                  * flushed while we are here. Further, because there is no
     151             :                  * buffer attached to the item, we know that there is no IO in
     152             :                  * progress, so nothing will clear the ili_fields while we read
     153             :                  * in the buffer. Hence we can safely drop the spin lock and
     154             :                  * read the buffer knowing that the state will not change from
     155             :                  * here.
     156             :                  */
     157   375335384 :                 spin_unlock(&iip->ili_lock);
     158   376160704 :                 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &bp);
     159   375834545 :                 if (error)
     160         331 :                         return error;
     161             : 
     162             :                 /*
     163             :                  * We need an explicit buffer reference for the log item but
     164             :                  * don't want the buffer to remain attached to the transaction.
     165             :                  * Hold the buffer but release the transaction reference once
     166             :                  * we've attached the inode log item to the buffer log item
     167             :                  * list.
     168             :                  */
     169   375834214 :                 xfs_buf_hold(bp);
     170   375871576 :                 spin_lock(&iip->ili_lock);
     171   375897664 :                 iip->ili_item.li_buf = bp;
     172   375897664 :                 bp->b_flags |= _XBF_INODES;
     173   375897664 :                 list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list);
     174   375303677 :                 xfs_trans_brelse(tp, bp);
     175             :         }
     176             : 
     177             :         /*
     178             :          * Always OR in the bits from the ili_last_fields field.  This is to
     179             :          * coordinate with the xfs_iflush() and xfs_buf_inode_iodone() routines
     180             :          * in the eventual clearing of the ili_fields bits.  See the big comment
     181             :          * in xfs_iflush() for an explanation of this coordination mechanism.
     182             :          */
     183  4859988146 :         iip->ili_fields |= (flags | iip->ili_last_fields);
     184  4859988146 :         spin_unlock(&iip->ili_lock);
     185             : 
     186             :         /*
     187             :          * We are done with the log item transaction dirty state, so clear it so
     188             :          * that it doesn't pollute future transactions.
     189             :          */
     190  4859472860 :         iip->ili_dirty_flags = 0;
     191  4859472860 :         return 0;
     192             : }
     193             : 
     194             : /*
     195             :  * The logged size of an inode fork is always the current size of the inode
     196             :  * fork. This means that when an inode fork is relogged, the size of the logged
     197             :  * region is determined by the current state, not the combination of the
     198             :  * previously logged state + the current state. This is different relogging
     199             :  * behaviour to most other log items which will retain the size of the
     200             :  * previously logged changes when smaller regions are relogged.
     201             :  *
     202             :  * Hence operations that remove data from the inode fork (e.g. shortform
     203             :  * dir/attr remove, extent form extent removal, etc), the size of the relogged
     204             :  * inode gets -smaller- rather than stays the same size as the previously logged
     205             :  * size and this can result in the committing transaction reducing the amount of
     206             :  * space being consumed by the CIL.
     207             :  */
     208             : STATIC void
     209  3455735745 : xfs_inode_item_data_fork_size(
     210             :         struct xfs_inode_log_item *iip,
     211             :         int                     *nvecs,
     212             :         int                     *nbytes)
     213             : {
     214  3455735745 :         struct xfs_inode        *ip = iip->ili_inode;
     215             : 
     216  3455735745 :         switch (ip->i_df.if_format) {
     217  1616131009 :         case XFS_DINODE_FMT_EXTENTS:
     218  1616131009 :                 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
     219   668224601 :                     ip->i_df.if_nextents > 0 &&
     220   633645154 :                     ip->i_df.if_bytes > 0) {
     221             :                         /* worst case, doesn't subtract delalloc extents */
     222   633645154 :                         *nbytes += xfs_inode_data_fork_size(ip);
     223   633645154 :                         *nvecs += 1;
     224             :                 }
     225             :                 break;
     226  1271199786 :         case XFS_DINODE_FMT_BTREE:
     227             :         case XFS_DINODE_FMT_RMAP:
     228             :         case XFS_DINODE_FMT_REFCOUNT:
     229  1271199786 :                 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
     230  1014210919 :                     ip->i_df.if_broot_bytes > 0) {
     231  1014210919 :                         *nbytes += ip->i_df.if_broot_bytes;
     232  1014210919 :                         *nvecs += 1;
     233             :                 }
     234             :                 break;
     235   455347173 :         case XFS_DINODE_FMT_LOCAL:
     236   455347173 :                 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
     237   444717510 :                     ip->i_df.if_bytes > 0) {
     238   444717510 :                         *nbytes += xlog_calc_iovec_len(ip->i_df.if_bytes);
     239   444717510 :                         *nvecs += 1;
     240             :                 }
     241             :                 break;
     242             : 
     243             :         case XFS_DINODE_FMT_DEV:
     244             :                 break;
     245           0 :         default:
     246           0 :                 ASSERT(0);
     247           0 :                 break;
     248             :         }
     249  3455735745 : }
     250             : 
     251             : STATIC void
     252  3212000951 : xfs_inode_item_attr_fork_size(
     253             :         struct xfs_inode_log_item *iip,
     254             :         int                     *nvecs,
     255             :         int                     *nbytes)
     256             : {
     257  3212000951 :         struct xfs_inode        *ip = iip->ili_inode;
     258             : 
     259  3212000951 :         switch (ip->i_af.if_format) {
     260   415715054 :         case XFS_DINODE_FMT_EXTENTS:
     261   415715054 :                 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
     262    54996663 :                     ip->i_af.if_nextents > 0 &&
     263    52863440 :                     ip->i_af.if_bytes > 0) {
     264             :                         /* worst case, doesn't subtract unused space */
     265    52863440 :                         *nbytes += xfs_inode_attr_fork_size(ip);
     266    52863440 :                         *nvecs += 1;
     267             :                 }
     268             :                 break;
     269      660279 :         case XFS_DINODE_FMT_BTREE:
     270      660279 :                 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
     271      561565 :                     ip->i_af.if_broot_bytes > 0) {
     272      561565 :                         *nbytes += ip->i_af.if_broot_bytes;
     273      561565 :                         *nvecs += 1;
     274             :                 }
     275             :                 break;
     276  2795625618 :         case XFS_DINODE_FMT_LOCAL:
     277  2795625618 :                 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
     278  1820904006 :                     ip->i_af.if_bytes > 0) {
     279  1820904006 :                         *nbytes += xlog_calc_iovec_len(ip->i_af.if_bytes);
     280  1820904006 :                         *nvecs += 1;
     281             :                 }
     282             :                 break;
     283           0 :         default:
     284           0 :                 ASSERT(0);
     285           0 :                 break;
     286             :         }
     287  3212000951 : }
     288             : 
     289             : /*
     290             :  * This returns the number of iovecs needed to log the given inode item.
     291             :  *
     292             :  * We need one iovec for the inode log format structure, one for the
     293             :  * inode core, and possibly one for the inode data/extents/b-tree root
     294             :  * and one for the inode attribute data/extents/b-tree root.
     295             :  */
     296             : STATIC void
     297  3455525643 : xfs_inode_item_size(
     298             :         struct xfs_log_item     *lip,
     299             :         int                     *nvecs,
     300             :         int                     *nbytes)
     301             : {
     302  3455525643 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     303  3455525643 :         struct xfs_inode        *ip = iip->ili_inode;
     304             : 
     305  3455525643 :         *nvecs += 2;
     306  6911051286 :         *nbytes += sizeof(struct xfs_inode_log_format) +
     307  3455525643 :                    xfs_log_dinode_size(ip->i_mount);
     308             : 
     309  3455525643 :         xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
     310  3455413542 :         if (xfs_inode_has_attr_fork(ip))
     311  3211947297 :                 xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
     312  3455285907 : }
     313             : 
     314             : STATIC void
     315  3456229797 : xfs_inode_item_format_data_fork(
     316             :         struct xfs_inode_log_item *iip,
     317             :         struct xfs_inode_log_format *ilf,
     318             :         struct xfs_log_vec      *lv,
     319             :         struct xfs_log_iovec    **vecp)
     320             : {
     321  3456229797 :         struct xfs_inode        *ip = iip->ili_inode;
     322  3456229797 :         size_t                  data_bytes;
     323             : 
     324  3456229797 :         switch (ip->i_df.if_format) {
     325  1616566430 :         case XFS_DINODE_FMT_EXTENTS:
     326  1616566430 :                 iip->ili_fields &=
     327             :                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
     328             : 
     329  1616566430 :                 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
     330   668275091 :                     ip->i_df.if_nextents > 0 &&
     331   633695258 :                     ip->i_df.if_bytes > 0) {
     332   633698247 :                         struct xfs_bmbt_rec *p;
     333             : 
     334   633698247 :                         ASSERT(xfs_iext_count(&ip->i_df) > 0);
     335             : 
     336   633688174 :                         p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
     337   633701763 :                         data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
     338   633652875 :                         xlog_finish_iovec(lv, *vecp, data_bytes);
     339             : 
     340   633632070 :                         ASSERT(data_bytes <= ip->i_df.if_bytes);
     341             : 
     342   633632070 :                         ilf->ilf_dsize = data_bytes;
     343   633632070 :                         ilf->ilf_size++;
     344             :                 } else {
     345   982868183 :                         iip->ili_fields &= ~XFS_ILOG_DEXT;
     346             :                 }
     347             :                 break;
     348  1271237765 :         case XFS_DINODE_FMT_BTREE:
     349             :         case XFS_DINODE_FMT_RMAP:
     350             :         case XFS_DINODE_FMT_REFCOUNT:
     351  1271237765 :                 iip->ili_fields &=
     352             :                         ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
     353             : 
     354  1271237765 :                 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
     355  1014235993 :                     ip->i_df.if_broot_bytes > 0) {
     356  1014235993 :                         ASSERT(ip->i_df.if_broot != NULL);
     357  1014235993 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
     358  1014235993 :                                         ip->i_df.if_broot,
     359  1014235993 :                                         ip->i_df.if_broot_bytes);
     360  1014234762 :                         ilf->ilf_dsize = ip->i_df.if_broot_bytes;
     361  1014234762 :                         ilf->ilf_size++;
     362             :                 } else {
     363   257001772 :                         ASSERT(!(iip->ili_fields &
     364             :                                  XFS_ILOG_DBROOT));
     365   257001772 :                         iip->ili_fields &= ~XFS_ILOG_DBROOT;
     366             :                 }
     367             :                 break;
     368   455357481 :         case XFS_DINODE_FMT_LOCAL:
     369   455357481 :                 iip->ili_fields &=
     370             :                         ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
     371   455357481 :                 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
     372   444730667 :                     ip->i_df.if_bytes > 0) {
     373   444730667 :                         ASSERT(ip->i_df.if_u1.if_data != NULL);
     374   444730667 :                         ASSERT(ip->i_disk_size > 0);
     375   444730667 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
     376   444730667 :                                         ip->i_df.if_u1.if_data,
     377   444730667 :                                         ip->i_df.if_bytes);
     378   444727508 :                         ilf->ilf_dsize = (unsigned)ip->i_df.if_bytes;
     379   444727508 :                         ilf->ilf_size++;
     380             :                 } else {
     381    10626814 :                         iip->ili_fields &= ~XFS_ILOG_DDATA;
     382             :                 }
     383             :                 break;
     384   113068121 :         case XFS_DINODE_FMT_DEV:
     385   113068121 :                 iip->ili_fields &=
     386             :                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
     387   113068121 :                 if (iip->ili_fields & XFS_ILOG_DEV)
     388    57323350 :                         ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev);
     389             :                 break;
     390           0 :         default:
     391           0 :                 ASSERT(0);
     392           0 :                 break;
     393             :         }
     394  3456159230 : }
     395             : 
     396             : STATIC void
     397  3212453275 : xfs_inode_item_format_attr_fork(
     398             :         struct xfs_inode_log_item *iip,
     399             :         struct xfs_inode_log_format *ilf,
     400             :         struct xfs_log_vec      *lv,
     401             :         struct xfs_log_iovec    **vecp)
     402             : {
     403  3212453275 :         struct xfs_inode        *ip = iip->ili_inode;
     404  3212453275 :         size_t                  data_bytes;
     405             : 
     406  3212453275 :         switch (ip->i_af.if_format) {
     407   415779641 :         case XFS_DINODE_FMT_EXTENTS:
     408   415779641 :                 iip->ili_fields &=
     409             :                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
     410             : 
     411   415779641 :                 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
     412    54998344 :                     ip->i_af.if_nextents > 0 &&
     413    52865076 :                     ip->i_af.if_bytes > 0) {
     414    52865106 :                         struct xfs_bmbt_rec *p;
     415             : 
     416    52865106 :                         ASSERT(xfs_iext_count(&ip->i_af) ==
     417             :                                 ip->i_af.if_nextents);
     418             : 
     419    52865107 :                         p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
     420    52865060 :                         data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
     421    52863119 :                         xlog_finish_iovec(lv, *vecp, data_bytes);
     422             : 
     423    52862382 :                         ilf->ilf_asize = data_bytes;
     424    52862382 :                         ilf->ilf_size++;
     425             :                 } else {
     426   362914535 :                         iip->ili_fields &= ~XFS_ILOG_AEXT;
     427             :                 }
     428             :                 break;
     429      660275 :         case XFS_DINODE_FMT_BTREE:
     430      660275 :                 iip->ili_fields &=
     431             :                         ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
     432             : 
     433      660275 :                 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
     434      561561 :                     ip->i_af.if_broot_bytes > 0) {
     435      561561 :                         ASSERT(ip->i_af.if_broot != NULL);
     436             : 
     437      561561 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
     438      561561 :                                         ip->i_af.if_broot,
     439      561561 :                                         ip->i_af.if_broot_bytes);
     440      561569 :                         ilf->ilf_asize = ip->i_af.if_broot_bytes;
     441      561569 :                         ilf->ilf_size++;
     442             :                 } else {
     443       98714 :                         iip->ili_fields &= ~XFS_ILOG_ABROOT;
     444             :                 }
     445             :                 break;
     446  2796013359 :         case XFS_DINODE_FMT_LOCAL:
     447  2796013359 :                 iip->ili_fields &=
     448             :                         ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
     449             : 
     450  2796013359 :                 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
     451  1821210945 :                     ip->i_af.if_bytes > 0) {
     452  1821210945 :                         ASSERT(ip->i_af.if_u1.if_data != NULL);
     453  1821210945 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
     454  1821210945 :                                         ip->i_af.if_u1.if_data,
     455  1821210945 :                                         ip->i_af.if_bytes);
     456  1820990195 :                         ilf->ilf_asize = (unsigned)ip->i_af.if_bytes;
     457  1820990195 :                         ilf->ilf_size++;
     458             :                 } else {
     459   974802414 :                         iip->ili_fields &= ~XFS_ILOG_ADATA;
     460             :                 }
     461             :                 break;
     462           0 :         default:
     463           0 :                 ASSERT(0);
     464           0 :                 break;
     465             :         }
     466  3212229809 : }
     467             : 
     468             : /*
     469             :  * Convert an incore timestamp to a log timestamp.  Note that the log format
     470             :  * specifies host endian format!
     471             :  */
     472             : static inline xfs_log_timestamp_t
     473             : xfs_inode_to_log_dinode_ts(
     474             :         struct xfs_inode                *ip,
     475             :         const struct timespec64         tv)
     476             : {
     477 13823626308 :         struct xfs_log_legacy_timestamp *lits;
     478 13823626308 :         xfs_log_timestamp_t             its;
     479             : 
     480 13823626308 :         if (xfs_inode_has_bigtime(ip))
     481 13820087504 :                 return xfs_inode_encode_bigtime(tv);
     482             : 
     483     3596439 :         lits = (struct xfs_log_legacy_timestamp *)&its;
     484     3596439 :         lits->t_sec = tv.tv_sec;
     485     3596439 :         lits->t_nsec = tv.tv_nsec;
     486             : 
     487     3596439 :         return its;
     488             : }
     489             : 
     490             : /*
     491             :  * The legacy DMAPI fields are only present in the on-disk and in-log inodes,
     492             :  * but not in the in-memory one.  But we are guaranteed to have an inode buffer
     493             :  * in memory when logging an inode, so we can just copy it from the on-disk
     494             :  * inode to the in-log inode here so that recovery of file system with these
     495             :  * fields set to non-zero values doesn't lose them.  For all other cases we zero
     496             :  * the fields.
     497             :  */
     498             : static void
     499  3456157480 : xfs_copy_dm_fields_to_log_dinode(
     500             :         struct xfs_inode        *ip,
     501             :         struct xfs_log_dinode   *to)
     502             : {
     503  3456157480 :         struct xfs_dinode       *dip;
     504             : 
     505  3456157480 :         dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf,
     506  3456157480 :                              ip->i_imap.im_boffset);
     507             : 
     508  6912827231 :         if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) {
     509           5 :                 to->di_dmevmask = be32_to_cpu(dip->di_dmevmask);
     510           5 :                 to->di_dmstate = be16_to_cpu(dip->di_dmstate);
     511             :         } else {
     512  3456621721 :                 to->di_dmevmask = 0;
     513  3456621721 :                 to->di_dmstate = 0;
     514             :         }
     515  3456621726 : }
     516             : 
     517             : static inline void
     518  3456029930 : xfs_inode_to_log_dinode_iext_counters(
     519             :         struct xfs_inode        *ip,
     520             :         struct xfs_log_dinode   *to)
     521             : {
     522  3456029930 :         if (xfs_inode_has_large_extent_counts(ip)) {
     523  3455061536 :                 to->di_big_nextents = xfs_ifork_nextents(&ip->i_df);
     524  3455061536 :                 to->di_big_anextents = xfs_ifork_nextents(&ip->i_af);
     525  3455061536 :                 to->di_nrext64_pad = 0;
     526             :         } else {
     527      968394 :                 to->di_nextents = xfs_ifork_nextents(&ip->i_df);
     528     1936788 :                 to->di_anextents = xfs_ifork_nextents(&ip->i_af);
     529             :         }
     530  3456029930 : }
     531             : 
     532             : static void
     533  3455973629 : xfs_inode_to_log_dinode(
     534             :         struct xfs_inode        *ip,
     535             :         struct xfs_log_dinode   *to,
     536             :         xfs_lsn_t               lsn)
     537             : {
     538  3455973629 :         struct inode            *inode = VFS_I(ip);
     539             : 
     540  3455973629 :         to->di_magic = XFS_DINODE_MAGIC;
     541  3455973629 :         to->di_format = xfs_ifork_format(&ip->i_df);
     542  3455973629 :         to->di_uid = i_uid_read(inode);
     543  3455780104 :         to->di_gid = i_gid_read(inode);
     544  3455734038 :         to->di_projid_lo = ip->i_projid & 0xffff;
     545  3455734038 :         to->di_projid_hi = ip->i_projid >> 16;
     546             : 
     547  3455734038 :         memset(to->di_pad3, 0, sizeof(to->di_pad3));
     548  3455734038 :         to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode->i_atime);
     549  3455734038 :         to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode->i_mtime);
     550  3455734038 :         to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode->i_ctime);
     551  3455734038 :         to->di_nlink = inode->i_nlink;
     552  3455734038 :         to->di_gen = inode->i_generation;
     553  3455734038 :         to->di_mode = inode->i_mode;
     554             : 
     555  3455734038 :         to->di_size = ip->i_disk_size;
     556  3455734038 :         to->di_nblocks = ip->i_nblocks;
     557  3455734038 :         to->di_extsize = ip->i_extsize;
     558  3455734038 :         to->di_forkoff = ip->i_forkoff;
     559  3455734038 :         to->di_aformat = xfs_ifork_format(&ip->i_af);
     560  3455734038 :         to->di_flags = ip->i_diflags;
     561             : 
     562  3455734038 :         xfs_copy_dm_fields_to_log_dinode(ip, to);
     563             : 
     564             :         /* log a dummy value to ensure log structure is fully initialised */
     565  3456428866 :         to->di_next_unlinked = NULLAGINO;
     566             : 
     567  3456428866 :         if (xfs_has_v3inodes(ip->i_mount)) {
     568  3456424194 :                 to->di_version = 3;
     569  3456424194 :                 to->di_changecount = inode_peek_iversion(inode);
     570  3456424194 :                 to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime);
     571  3456424194 :                 to->di_flags2 = ip->i_diflags2;
     572  3456424194 :                 to->di_cowextsize = ip->i_cowextsize;
     573  3456424194 :                 to->di_ino = ip->i_ino;
     574  3456424194 :                 to->di_lsn = lsn;
     575  3456424194 :                 memset(to->di_pad2, 0, sizeof(to->di_pad2));
     576  3456424194 :                 uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
     577  3455952038 :                 to->di_v3_pad = 0;
     578             :         } else {
     579        4672 :                 to->di_version = 2;
     580        4672 :                 to->di_flushiter = ip->i_flushiter;
     581        9344 :                 memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
     582             :         }
     583             : 
     584  3455956710 :         xfs_inode_to_log_dinode_iext_counters(ip, to);
     585  3456051615 : }
     586             : 
     587             : /*
     588             :  * Format the inode core. Current timestamp data is only in the VFS inode
     589             :  * fields, so we need to grab them from there. Hence rather than just copying
     590             :  * the XFS inode core structure, format the fields directly into the iovec.
     591             :  */
     592             : static void
     593  3455686349 : xfs_inode_item_format_core(
     594             :         struct xfs_inode        *ip,
     595             :         struct xfs_log_vec      *lv,
     596             :         struct xfs_log_iovec    **vecp)
     597             : {
     598  3455686349 :         struct xfs_log_dinode   *dic;
     599             : 
     600  3455686349 :         dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
     601  3455731651 :         xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
     602  3455964074 :         xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_mount));
     603  3455951708 : }
     604             : 
     605             : /*
     606             :  * This is called to fill in the vector of log iovecs for the given inode
     607             :  * log item.  It fills the first item with an inode log format structure,
     608             :  * the second with the on-disk inode structure, and a possible third and/or
     609             :  * fourth with the inode data/extents/b-tree root and inode attributes
     610             :  * data/extents/b-tree root.
     611             :  *
     612             :  * Note: Always use the 64 bit inode log format structure so we don't
     613             :  * leave an uninitialised hole in the format item on 64 bit systems. Log
     614             :  * recovery on 32 bit systems handles this just fine, so there's no reason
     615             :  * for not using an initialising the properly padded structure all the time.
     616             :  */
     617             : STATIC void
     618  3456212384 : xfs_inode_item_format(
     619             :         struct xfs_log_item     *lip,
     620             :         struct xfs_log_vec      *lv)
     621             : {
     622  3456212384 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     623  3456212384 :         struct xfs_inode        *ip = iip->ili_inode;
     624  3456212384 :         struct xfs_log_iovec    *vecp = NULL;
     625  3456212384 :         struct xfs_inode_log_format *ilf;
     626             : 
     627  3456212384 :         ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
     628  3455676606 :         ilf->ilf_type = XFS_LI_INODE;
     629  3455676606 :         ilf->ilf_ino = ip->i_ino;
     630  3455676606 :         ilf->ilf_blkno = ip->i_imap.im_blkno;
     631  3455676606 :         ilf->ilf_len = ip->i_imap.im_len;
     632  3455676606 :         ilf->ilf_boffset = ip->i_imap.im_boffset;
     633  3455676606 :         ilf->ilf_fields = XFS_ILOG_CORE;
     634  3455676606 :         ilf->ilf_size = 2; /* format + core */
     635             : 
     636             :         /*
     637             :          * make sure we don't leak uninitialised data into the log in the case
     638             :          * when we don't log every field in the inode.
     639             :          */
     640  3455676606 :         ilf->ilf_dsize = 0;
     641  3455676606 :         ilf->ilf_asize = 0;
     642  3455676606 :         ilf->ilf_pad = 0;
     643  3455676606 :         memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
     644             : 
     645  3455676606 :         xlog_finish_iovec(lv, vecp, sizeof(*ilf));
     646             : 
     647  3455664212 :         xfs_inode_item_format_core(ip, lv, &vecp);
     648  3455967851 :         xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
     649  3456029565 :         if (xfs_inode_has_attr_fork(ip)) {
     650  3212303567 :                 xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
     651             :         } else {
     652   243725998 :                 iip->ili_fields &=
     653             :                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT);
     654             :         }
     655             : 
     656             :         /* update the format with the exact fields we actually logged */
     657  3455859879 :         ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
     658  3455859879 : }
     659             : 
     660             : /*
     661             :  * This is called to pin the inode associated with the inode log
     662             :  * item in memory so it cannot be written out.
     663             :  */
     664             : STATIC void
     665   721814489 : xfs_inode_item_pin(
     666             :         struct xfs_log_item     *lip)
     667             : {
     668   721814489 :         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
     669             : 
     670   721814489 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     671   721819465 :         ASSERT(lip->li_buf);
     672             : 
     673   721819465 :         trace_xfs_inode_pin(ip, _RET_IP_);
     674   721765629 :         atomic_inc(&ip->i_pincount);
     675   721978872 : }
     676             : 
     677             : 
     678             : /*
     679             :  * This is called to unpin the inode associated with the inode log
     680             :  * item which was previously pinned with a call to xfs_inode_item_pin().
     681             :  *
     682             :  * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0.
     683             :  *
     684             :  * Note that unpin can race with inode cluster buffer freeing marking the buffer
     685             :  * stale. In that case, flush completions are run from the buffer unpin call,
     686             :  * which may happen before the inode is unpinned. If we lose the race, there
     687             :  * will be no buffer attached to the log item, but the inode will be marked
     688             :  * XFS_ISTALE.
     689             :  */
     690             : STATIC void
     691   722088741 : xfs_inode_item_unpin(
     692             :         struct xfs_log_item     *lip,
     693             :         int                     remove)
     694             : {
     695   722088741 :         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
     696             : 
     697   722088741 :         trace_xfs_inode_unpin(ip, _RET_IP_);
     698   722109429 :         ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
     699   722088741 :         ASSERT(atomic_read(&ip->i_pincount) > 0);
     700   722088741 :         if (atomic_dec_and_test(&ip->i_pincount))
     701   691371220 :                 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
     702   722088741 : }
     703             : 
     704             : STATIC uint
     705   194857616 : xfs_inode_item_push(
     706             :         struct xfs_log_item     *lip,
     707             :         struct list_head        *buffer_list)
     708             :                 __releases(&lip->li_ailp->ail_lock)
     709             :                 __acquires(&lip->li_ailp->ail_lock)
     710             : {
     711   194857616 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     712   194857616 :         struct xfs_inode        *ip = iip->ili_inode;
     713   194857616 :         struct xfs_buf          *bp = lip->li_buf;
     714   194857616 :         uint                    rval = XFS_ITEM_SUCCESS;
     715   194857616 :         int                     error;
     716             : 
     717   194857616 :         if (!bp || (ip->i_flags & XFS_ISTALE)) {
     718             :                 /*
     719             :                  * Inode item/buffer is being aborted due to cluster
     720             :                  * buffer deletion. Trigger a log force to have that operation
     721             :                  * completed and items removed from the AIL before the next push
     722             :                  * attempt.
     723             :                  */
     724             :                 return XFS_ITEM_PINNED;
     725             :         }
     726             : 
     727   194771815 :         if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp))
     728             :                 return XFS_ITEM_PINNED;
     729             : 
     730   379253188 :         if (xfs_iflags_test(ip, XFS_IFLUSHING))
     731             :                 return XFS_ITEM_FLUSHING;
     732             : 
     733    55290133 :         if (!xfs_buf_trylock(bp))
     734             :                 return XFS_ITEM_LOCKED;
     735             : 
     736    55147167 :         spin_unlock(&lip->li_ailp->ail_lock);
     737             : 
     738             :         /*
     739             :          * We need to hold a reference for flushing the cluster buffer as it may
     740             :          * fail the buffer without IO submission. In which case, we better get a
     741             :          * reference for that completion because otherwise we don't get a
     742             :          * reference for IO until we queue the buffer for delwri submission.
     743             :          */
     744    55147167 :         xfs_buf_hold(bp);
     745    55147167 :         error = xfs_iflush_cluster(bp);
     746    55147167 :         if (!error) {
     747    54604137 :                 if (!xfs_buf_delwri_queue(bp, buffer_list))
     748     1733511 :                         rval = XFS_ITEM_FLUSHING;
     749    54604137 :                 xfs_buf_relse(bp);
     750             :         } else {
     751             :                 /*
     752             :                  * Release the buffer if we were unable to flush anything. On
     753             :                  * any other error, the buffer has already been released.
     754             :                  */
     755      543030 :                 if (error == -EAGAIN)
     756      228241 :                         xfs_buf_relse(bp);
     757             :                 rval = XFS_ITEM_LOCKED;
     758             :         }
     759             : 
     760    55147167 :         spin_lock(&lip->li_ailp->ail_lock);
     761    55147167 :         return rval;
     762             : }
     763             : 
     764             : /*
     765             :  * Unlock the inode associated with the inode log item.
     766             :  */
     767             : STATIC void
     768  5562545909 : xfs_inode_item_release(
     769             :         struct xfs_log_item     *lip)
     770             : {
     771  5562545909 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     772  5562545909 :         struct xfs_inode        *ip = iip->ili_inode;
     773  5562545909 :         unsigned short          lock_flags;
     774             : 
     775  5562545909 :         ASSERT(ip->i_itemp != NULL);
     776  5562545909 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     777             : 
     778  5562427498 :         lock_flags = iip->ili_lock_flags;
     779  5562427498 :         iip->ili_lock_flags = 0;
     780  5562427498 :         if (lock_flags)
     781  1041575160 :                 xfs_iunlock(ip, lock_flags);
     782  5562505183 : }
     783             : 
     784             : /*
     785             :  * This is called to find out where the oldest active copy of the inode log
     786             :  * item in the on disk log resides now that the last log write of it completed
     787             :  * at the given lsn.  Since we always re-log all dirty data in an inode, the
     788             :  * latest copy in the on disk log is the only one that matters.  Therefore,
     789             :  * simply return the given lsn.
     790             :  *
     791             :  * If the inode has been marked stale because the cluster is being freed, we
     792             :  * don't want to (re-)insert this inode into the AIL. There is a race condition
     793             :  * where the cluster buffer may be unpinned before the inode is inserted into
     794             :  * the AIL during transaction committed processing. If the buffer is unpinned
     795             :  * before the inode item has been committed and inserted, then it is possible
     796             :  * for the buffer to be written and IO completes before the inode is inserted
     797             :  * into the AIL. In that case, we'd be inserting a clean, stale inode into the
     798             :  * AIL which will never get removed. It will, however, get reclaimed which
     799             :  * triggers an assert in xfs_inode_free() complaining about freein an inode
     800             :  * still in the AIL.
     801             :  *
     802             :  * To avoid this, just unpin the inode directly and return a LSN of -1 so the
     803             :  * transaction committed code knows that it does not need to do any further
     804             :  * processing on the item.
     805             :  */
     806             : STATIC xfs_lsn_t
     807   722088741 : xfs_inode_item_committed(
     808             :         struct xfs_log_item     *lip,
     809             :         xfs_lsn_t               lsn)
     810             : {
     811   722088741 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     812   722088741 :         struct xfs_inode        *ip = iip->ili_inode;
     813             : 
     814  1444177482 :         if (xfs_iflags_test(ip, XFS_ISTALE)) {
     815    15042687 :                 xfs_inode_item_unpin(lip, 0);
     816    15042687 :                 return -1;
     817             :         }
     818             :         return lsn;
     819             : }
     820             : 
     821             : STATIC void
     822  4942604106 : xfs_inode_item_committing(
     823             :         struct xfs_log_item     *lip,
     824             :         xfs_csn_t               seq)
     825             : {
     826  4942604106 :         INODE_ITEM(lip)->ili_commit_seq = seq;
     827  4942604106 :         return xfs_inode_item_release(lip);
     828             : }
     829             : 
     830             : static const struct xfs_item_ops xfs_inode_item_ops = {
     831             :         .iop_sort       = xfs_inode_item_sort,
     832             :         .iop_precommit  = xfs_inode_item_precommit,
     833             :         .iop_size       = xfs_inode_item_size,
     834             :         .iop_format     = xfs_inode_item_format,
     835             :         .iop_pin        = xfs_inode_item_pin,
     836             :         .iop_unpin      = xfs_inode_item_unpin,
     837             :         .iop_release    = xfs_inode_item_release,
     838             :         .iop_committed  = xfs_inode_item_committed,
     839             :         .iop_push       = xfs_inode_item_push,
     840             :         .iop_committing = xfs_inode_item_committing,
     841             : };
     842             : 
     843             : 
     844             : /*
     845             :  * Initialize the inode log item for a newly allocated (in-core) inode.
     846             :  */
     847             : void
     848    83484296 : xfs_inode_item_init(
     849             :         struct xfs_inode        *ip,
     850             :         struct xfs_mount        *mp)
     851             : {
     852    83484296 :         struct xfs_inode_log_item *iip;
     853             : 
     854    83484296 :         ASSERT(ip->i_itemp == NULL);
     855    83484296 :         iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
     856             :                                               GFP_KERNEL | __GFP_NOFAIL);
     857             : 
     858    83777891 :         iip->ili_inode = ip;
     859    83777891 :         spin_lock_init(&iip->ili_lock);
     860    83675336 :         xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
     861             :                                                 &xfs_inode_item_ops);
     862    83327449 : }
     863             : 
     864             : /*
     865             :  * Free the inode log item and any memory hanging off of it.
     866             :  */
     867             : void
     868    83910613 : xfs_inode_item_destroy(
     869             :         struct xfs_inode        *ip)
     870             : {
     871    83910613 :         struct xfs_inode_log_item *iip = ip->i_itemp;
     872             : 
     873    83910613 :         ASSERT(iip->ili_item.li_buf == NULL);
     874             : 
     875    83910613 :         ip->i_itemp = NULL;
     876    83910613 :         kmem_free(iip->ili_item.li_lv_shadow);
     877    83975559 :         kmem_cache_free(xfs_ili_cache, iip);
     878    84010519 : }
     879             : 
     880             : 
     881             : /*
     882             :  * We only want to pull the item from the AIL if it is actually there
     883             :  * and its location in the log has not changed since we started the
     884             :  * flush.  Thus, we only bother if the inode's lsn has not changed.
     885             :  */
     886             : static void
     887    54563015 : xfs_iflush_ail_updates(
     888             :         struct xfs_ail          *ailp,
     889             :         struct list_head        *list)
     890             : {
     891    54563015 :         struct xfs_log_item     *lip;
     892    54563015 :         xfs_lsn_t               tail_lsn = 0;
     893             : 
     894             :         /* this is an opencoded batch version of xfs_trans_ail_delete */
     895    54563015 :         spin_lock(&ailp->ail_lock);
     896   415876658 :         list_for_each_entry(lip, list, li_bio_list) {
     897   361313643 :                 xfs_lsn_t       lsn;
     898             : 
     899   361313643 :                 clear_bit(XFS_LI_FAILED, &lip->li_flags);
     900   361313643 :                 if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
     901          97 :                         continue;
     902             : 
     903             :                 /*
     904             :                  * dgc: Not sure how this happens, but it happens very
     905             :                  * occassionaly via generic/388.  xfs_iflush_abort() also
     906             :                  * silently handles this same "under writeback but not in AIL at
     907             :                  * shutdown" condition via xfs_trans_ail_delete().
     908             :                  */
     909   361313546 :                 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) {
     910           0 :                         ASSERT(xlog_is_shutdown(lip->li_log));
     911           0 :                         continue;
     912             :                 }
     913             : 
     914   361313546 :                 lsn = xfs_ail_delete_one(ailp, lip);
     915   361313546 :                 if (!tail_lsn && lsn)
     916      757886 :                         tail_lsn = lsn;
     917             :         }
     918    54563015 :         xfs_ail_update_finish(ailp, tail_lsn);
     919    54563015 : }
     920             : 
     921             : /*
     922             :  * Walk the list of inodes that have completed their IOs. If they are clean
     923             :  * remove them from the list and dissociate them from the buffer. Buffers that
     924             :  * are still dirty remain linked to the buffer and on the list. Caller must
     925             :  * handle them appropriately.
     926             :  */
     927             : static void
     928    55847549 : xfs_iflush_finish(
     929             :         struct xfs_buf          *bp,
     930             :         struct list_head        *list)
     931             : {
     932    55847549 :         struct xfs_log_item     *lip, *n;
     933             : 
     934   418951230 :         list_for_each_entry_safe(lip, n, list, li_bio_list) {
     935   363103681 :                 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     936   363103681 :                 bool    drop_buffer = false;
     937             : 
     938   363103681 :                 spin_lock(&iip->ili_lock);
     939             : 
     940             :                 /*
     941             :                  * Remove the reference to the cluster buffer if the inode is
     942             :                  * clean in memory and drop the buffer reference once we've
     943             :                  * dropped the locks we hold.
     944             :                  */
     945   363103681 :                 ASSERT(iip->ili_item.li_buf == bp);
     946   363103681 :                 if (!iip->ili_fields) {
     947   356846684 :                         iip->ili_item.li_buf = NULL;
     948   356846684 :                         list_del_init(&lip->li_bio_list);
     949   356846684 :                         drop_buffer = true;
     950             :                 }
     951   363103681 :                 iip->ili_last_fields = 0;
     952   363103681 :                 iip->ili_flush_lsn = 0;
     953   363103681 :                 spin_unlock(&iip->ili_lock);
     954   363103681 :                 xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
     955   363103681 :                 if (drop_buffer)
     956   356846684 :                         xfs_buf_rele(bp);
     957             :         }
     958    55847549 : }
     959             : 
     960             : /*
     961             :  * Inode buffer IO completion routine.  It is responsible for removing inodes
     962             :  * attached to the buffer from the AIL if they have not been re-logged and
     963             :  * completing the inode flush.
     964             :  */
     965             : void
     966    55847549 : xfs_buf_inode_iodone(
     967             :         struct xfs_buf          *bp)
     968             : {
     969    55847549 :         struct xfs_log_item     *lip, *n;
     970    55847549 :         LIST_HEAD(flushed_inodes);
     971    55847549 :         LIST_HEAD(ail_updates);
     972             : 
     973             :         /*
     974             :          * Pull the attached inodes from the buffer one at a time and take the
     975             :          * appropriate action on them.
     976             :          */
     977   458051422 :         list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
     978   402203873 :                 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     979             : 
     980   804407746 :                 if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
     981    17431585 :                         xfs_iflush_abort(iip->ili_inode);
     982    17431585 :                         continue;
     983             :                 }
     984   384772288 :                 if (!iip->ili_last_fields)
     985    21668607 :                         continue;
     986             : 
     987             :                 /* Do an unlocked check for needing the AIL lock. */
     988   364893719 :                 if (iip->ili_flush_lsn == lip->li_lsn ||
     989     1790038 :                     test_bit(XFS_LI_FAILED, &lip->li_flags))
     990   361313643 :                         list_move_tail(&lip->li_bio_list, &ail_updates);
     991             :                 else
     992     1790038 :                         list_move_tail(&lip->li_bio_list, &flushed_inodes);
     993             :         }
     994             : 
     995    55847549 :         if (!list_empty(&ail_updates)) {
     996    54563015 :                 xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
     997    54563015 :                 list_splice_tail(&ail_updates, &flushed_inodes);
     998             :         }
     999             : 
    1000    55847549 :         xfs_iflush_finish(bp, &flushed_inodes);
    1001    55847549 :         if (!list_empty(&flushed_inodes))
    1002     3636798 :                 list_splice_tail(&flushed_inodes, &bp->b_li_list);
    1003    55847549 : }
    1004             : 
    1005             : void
    1006         179 : xfs_buf_inode_io_fail(
    1007             :         struct xfs_buf          *bp)
    1008             : {
    1009         179 :         struct xfs_log_item     *lip;
    1010             : 
    1011         537 :         list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
    1012         358 :                 set_bit(XFS_LI_FAILED, &lip->li_flags);
    1013         179 : }
    1014             : 
    1015             : /*
    1016             :  * Clear the inode logging fields so no more flushes are attempted.  If we are
    1017             :  * on a buffer list, it is now safe to remove it because the buffer is
    1018             :  * guaranteed to be locked. The caller will drop the reference to the buffer
    1019             :  * the log item held.
    1020             :  */
    1021             : static void
    1022    22259758 : xfs_iflush_abort_clean(
    1023             :         struct xfs_inode_log_item *iip)
    1024             : {
    1025    22259758 :         iip->ili_last_fields = 0;
    1026    22259758 :         iip->ili_fields = 0;
    1027    22259758 :         iip->ili_fsync_fields = 0;
    1028    22259758 :         iip->ili_flush_lsn = 0;
    1029    22259758 :         iip->ili_item.li_buf = NULL;
    1030    22259758 :         list_del_init(&iip->ili_item.li_bio_list);
    1031    22259759 : }
    1032             : 
    1033             : /*
    1034             :  * Abort flushing the inode from a context holding the cluster buffer locked.
    1035             :  *
    1036             :  * This is the normal runtime method of aborting writeback of an inode that is
    1037             :  * attached to a cluster buffer. It occurs when the inode and the backing
    1038             :  * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster
    1039             :  * flushing or buffer IO completion encounters a log shutdown situation.
    1040             :  *
    1041             :  * If we need to abort inode writeback and we don't already hold the buffer
    1042             :  * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be
    1043             :  * necessary in a shutdown situation.
    1044             :  */
    1045             : void
    1046    22259740 : xfs_iflush_abort(
    1047             :         struct xfs_inode        *ip)
    1048             : {
    1049    22259740 :         struct xfs_inode_log_item *iip = ip->i_itemp;
    1050    22259740 :         struct xfs_buf          *bp;
    1051             : 
    1052    22259740 :         if (!iip) {
    1053             :                 /* clean inode, nothing to do */
    1054           0 :                 xfs_iflags_clear(ip, XFS_IFLUSHING);
    1055           0 :                 return;
    1056             :         }
    1057             : 
    1058             :         /*
    1059             :          * Remove the inode item from the AIL before we clear its internal
    1060             :          * state. Whilst the inode is in the AIL, it should have a valid buffer
    1061             :          * pointer for push operations to access - it is only safe to remove the
    1062             :          * inode from the buffer once it has been removed from the AIL.
    1063             :          *
    1064             :          * We also clear the failed bit before removing the item from the AIL
    1065             :          * as xfs_trans_ail_delete()->xfs_clear_li_failed() will release buffer
    1066             :          * references the inode item owns and needs to hold until we've fully
    1067             :          * aborted the inode log item and detached it from the buffer.
    1068             :          */
    1069    22259740 :         clear_bit(XFS_LI_FAILED, &iip->ili_item.li_flags);
    1070    22259759 :         xfs_trans_ail_delete(&iip->ili_item, 0);
    1071             : 
    1072             :         /*
    1073             :          * Grab the inode buffer so can we release the reference the inode log
    1074             :          * item holds on it.
    1075             :          */
    1076    22259760 :         spin_lock(&iip->ili_lock);
    1077    22259758 :         bp = iip->ili_item.li_buf;
    1078    22259758 :         xfs_iflush_abort_clean(iip);
    1079    22259758 :         spin_unlock(&iip->ili_lock);
    1080             : 
    1081    22259756 :         xfs_iflags_clear(ip, XFS_IFLUSHING);
    1082    22259760 :         if (bp)
    1083    19728641 :                 xfs_buf_rele(bp);
    1084             : }
    1085             : 
    1086             : /*
    1087             :  * Abort an inode flush in the case of a shutdown filesystem. This can be called
    1088             :  * from anywhere with just an inode reference and does not require holding the
    1089             :  * inode cluster buffer locked. If the inode is attached to a cluster buffer,
    1090             :  * it will grab and lock it safely, then abort the inode flush.
    1091             :  */
    1092             : void
    1093   325411207 : xfs_iflush_shutdown_abort(
    1094             :         struct xfs_inode        *ip)
    1095             : {
    1096   325411207 :         struct xfs_inode_log_item *iip = ip->i_itemp;
    1097   325411207 :         struct xfs_buf          *bp;
    1098             : 
    1099   325411207 :         if (!iip) {
    1100             :                 /* clean inode, nothing to do */
    1101   322788398 :                 xfs_iflags_clear(ip, XFS_IFLUSHING);
    1102   322788398 :                 return;
    1103             :         }
    1104             : 
    1105     2622809 :         spin_lock(&iip->ili_lock);
    1106     2622809 :         bp = iip->ili_item.li_buf;
    1107     2622809 :         if (!bp) {
    1108     2531119 :                 spin_unlock(&iip->ili_lock);
    1109     2531119 :                 xfs_iflush_abort(ip);
    1110     2531119 :                 return;
    1111             :         }
    1112             : 
    1113             :         /*
    1114             :          * We have to take a reference to the buffer so that it doesn't get
    1115             :          * freed when we drop the ili_lock and then wait to lock the buffer.
    1116             :          * We'll clean up the extra reference after we pick up the ili_lock
    1117             :          * again.
    1118             :          */
    1119       91690 :         xfs_buf_hold(bp);
    1120       91690 :         spin_unlock(&iip->ili_lock);
    1121       91690 :         xfs_buf_lock(bp);
    1122             : 
    1123       91690 :         spin_lock(&iip->ili_lock);
    1124       91690 :         if (!iip->ili_item.li_buf) {
    1125             :                 /*
    1126             :                  * Raced with another removal, hold the only reference
    1127             :                  * to bp now. Inode should not be in the AIL now, so just clean
    1128             :                  * up and return;
    1129             :                  */
    1130           0 :                 ASSERT(list_empty(&iip->ili_item.li_bio_list));
    1131           0 :                 ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags));
    1132           0 :                 xfs_iflush_abort_clean(iip);
    1133           0 :                 spin_unlock(&iip->ili_lock);
    1134           0 :                 xfs_iflags_clear(ip, XFS_IFLUSHING);
    1135           0 :                 xfs_buf_relse(bp);
    1136           0 :                 return;
    1137             :         }
    1138             : 
    1139             :         /*
    1140             :          * Got two references to bp. The first will get dropped by
    1141             :          * xfs_iflush_abort() when the item is removed from the buffer list, but
    1142             :          * we can't drop our reference until _abort() returns because we have to
    1143             :          * unlock the buffer as well. Hence we abort and then unlock and release
    1144             :          * our reference to the buffer.
    1145             :          */
    1146       91690 :         ASSERT(iip->ili_item.li_buf == bp);
    1147       91690 :         spin_unlock(&iip->ili_lock);
    1148       91690 :         xfs_iflush_abort(ip);
    1149       91690 :         xfs_buf_relse(bp);
    1150             : }
    1151             : 
    1152             : 
    1153             : /*
    1154             :  * convert an xfs_inode_log_format struct from the old 32 bit version
    1155             :  * (which can have different field alignments) to the native 64 bit version
    1156             :  */
    1157             : int
    1158           0 : xfs_inode_item_format_convert(
    1159             :         struct xfs_log_iovec            *buf,
    1160             :         struct xfs_inode_log_format     *in_f)
    1161             : {
    1162           0 :         struct xfs_inode_log_format_32  *in_f32 = buf->i_addr;
    1163             : 
    1164           0 :         if (buf->i_len != sizeof(*in_f32)) {
    1165           0 :                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
    1166           0 :                 return -EFSCORRUPTED;
    1167             :         }
    1168             : 
    1169           0 :         in_f->ilf_type = in_f32->ilf_type;
    1170           0 :         in_f->ilf_size = in_f32->ilf_size;
    1171           0 :         in_f->ilf_fields = in_f32->ilf_fields;
    1172           0 :         in_f->ilf_asize = in_f32->ilf_asize;
    1173           0 :         in_f->ilf_dsize = in_f32->ilf_dsize;
    1174           0 :         in_f->ilf_ino = in_f32->ilf_ino;
    1175           0 :         memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u));
    1176           0 :         in_f->ilf_blkno = in_f32->ilf_blkno;
    1177           0 :         in_f->ilf_len = in_f32->ilf_len;
    1178           0 :         in_f->ilf_boffset = in_f32->ilf_boffset;
    1179           0 :         return 0;
    1180             : }

Generated by: LCOV version 1.14