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-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 440 493 89.2 %
Date: 2023-07-31 20:08:27 Functions: 26 28 92.9 %

          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  2416109990 : xfs_inode_item_sort(
      35             :         struct xfs_log_item     *lip)
      36             : {
      37  2416109990 :         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  2708568431 : xfs_inode_item_precommit(
      72             :         struct xfs_trans        *tp,
      73             :         struct xfs_log_item     *lip)
      74             : {
      75  2708568431 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
      76  2708568431 :         struct xfs_inode        *ip = iip->ili_inode;
      77  2708568431 :         struct inode            *inode = VFS_I(ip);
      78  2708568431 :         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  2708568431 :         if (inode->i_state & I_DIRTY_TIME) {
      87           4 :                 spin_lock(&inode->i_lock);
      88           4 :                 inode->i_state &= ~I_DIRTY_TIME;
      89           4 :                 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  2708568431 :         if ((flags & (XFS_ILOG_CORE | XFS_ILOG_TIMESTAMP)) &&
      97  2445970052 :             xfs_has_bigtime(ip->i_mount) &&
      98             :             !xfs_inode_has_bigtime(ip)) {
      99          22 :                 ip->i_diflags2 |= XFS_DIFLAG2_BIGTIME;
     100          22 :                 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  2708568431 :         if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
     110           0 :             (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) &&
     111           0 :             xfs_extlen_to_rtxmod(ip->i_mount, ip->i_extsize) > 0) {
     112           0 :                 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE |
     113             :                                    XFS_DIFLAG_EXTSZINHERIT);
     114           0 :                 ip->i_extsize = 0;
     115           0 :                 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  2708568431 :         spin_lock(&iip->ili_lock);
     126  2709394600 :         iip->ili_fsync_fields |= (flags & ~XFS_ILOG_IVERSION);
     127  2709394600 :         if (flags & XFS_ILOG_IVERSION)
     128  1754713685 :                 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  2709394600 :         if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
     137   221908537 :             (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) &&
     138           0 :             xfs_extlen_to_rtxmod(ip->i_mount, ip->i_cowextsize) > 0) {
     139           0 :                 ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE;
     140           0 :                 ip->i_cowextsize = 0;
     141           0 :                 flags |= XFS_ILOG_CORE;
     142             :         }
     143             : 
     144  2709394600 :         if (!iip->ili_item.li_buf) {
     145   282778532 :                 struct xfs_buf  *bp;
     146   282778532 :                 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   282778532 :                 spin_unlock(&iip->ili_lock);
     158   282773026 :                 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &bp);
     159   282759449 :                 if (error)
     160         305 :                         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   282759144 :                 xfs_buf_hold(bp);
     170   282782558 :                 spin_lock(&iip->ili_lock);
     171   282784954 :                 iip->ili_item.li_buf = bp;
     172   282784954 :                 bp->b_flags |= _XBF_INODES;
     173   282784954 :                 list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list);
     174   282774864 :                 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  2709406084 :         iip->ili_fields |= (flags | iip->ili_last_fields);
     184  2709406084 :         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  2708520824 :         iip->ili_dirty_flags = 0;
     191  2708520824 :         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  1863092098 : xfs_inode_item_data_fork_size(
     210             :         struct xfs_inode_log_item *iip,
     211             :         int                     *nvecs,
     212             :         int                     *nbytes)
     213             : {
     214  1863092098 :         struct xfs_inode        *ip = iip->ili_inode;
     215             : 
     216  1863092098 :         switch (ip->i_df.if_format) {
     217   978269609 :         case XFS_DINODE_FMT_EXTENTS:
     218   978269609 :                 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
     219   335052873 :                     ip->i_df.if_nextents > 0 &&
     220   311586021 :                     ip->i_df.if_bytes > 0) {
     221             :                         /* worst case, doesn't subtract delalloc extents */
     222   311586021 :                         *nbytes += xfs_inode_data_fork_size(ip);
     223   311586021 :                         *nvecs += 1;
     224             :                 }
     225             :                 break;
     226   528621379 :         case XFS_DINODE_FMT_BTREE:
     227             :         case XFS_DINODE_FMT_RMAP:
     228             :         case XFS_DINODE_FMT_REFCOUNT:
     229   528621379 :                 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
     230   334815446 :                     ip->i_df.if_broot_bytes > 0) {
     231   334815446 :                         *nbytes += ip->i_df.if_broot_bytes;
     232   334815446 :                         *nvecs += 1;
     233             :                 }
     234             :                 break;
     235   287929187 :         case XFS_DINODE_FMT_LOCAL:
     236   287929187 :                 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
     237   281486410 :                     ip->i_df.if_bytes > 0) {
     238   281486410 :                         *nbytes += xlog_calc_iovec_len(ip->i_df.if_bytes);
     239   281486410 :                         *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  1863092098 : }
     250             : 
     251             : STATIC void
     252  1781876948 : xfs_inode_item_attr_fork_size(
     253             :         struct xfs_inode_log_item *iip,
     254             :         int                     *nvecs,
     255             :         int                     *nbytes)
     256             : {
     257  1781876948 :         struct xfs_inode        *ip = iip->ili_inode;
     258             : 
     259  1781876948 :         switch (ip->i_af.if_format) {
     260   368147502 :         case XFS_DINODE_FMT_EXTENTS:
     261   368147502 :                 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
     262    44887756 :                     ip->i_af.if_nextents > 0 &&
     263    42710350 :                     ip->i_af.if_bytes > 0) {
     264             :                         /* worst case, doesn't subtract unused space */
     265    42710350 :                         *nbytes += xfs_inode_attr_fork_size(ip);
     266    42710350 :                         *nvecs += 1;
     267             :                 }
     268             :                 break;
     269      122317 :         case XFS_DINODE_FMT_BTREE:
     270      122317 :                 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
     271      106386 :                     ip->i_af.if_broot_bytes > 0) {
     272      106386 :                         *nbytes += ip->i_af.if_broot_bytes;
     273      106386 :                         *nvecs += 1;
     274             :                 }
     275             :                 break;
     276  1413607129 :         case XFS_DINODE_FMT_LOCAL:
     277  1413607129 :                 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
     278   857530390 :                     ip->i_af.if_bytes > 0) {
     279   857530390 :                         *nbytes += xlog_calc_iovec_len(ip->i_af.if_bytes);
     280   857530390 :                         *nvecs += 1;
     281             :                 }
     282             :                 break;
     283           0 :         default:
     284           0 :                 ASSERT(0);
     285           0 :                 break;
     286             :         }
     287  1781876948 : }
     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  1862998981 : xfs_inode_item_size(
     298             :         struct xfs_log_item     *lip,
     299             :         int                     *nvecs,
     300             :         int                     *nbytes)
     301             : {
     302  1862998981 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     303  1862998981 :         struct xfs_inode        *ip = iip->ili_inode;
     304             : 
     305  1862998981 :         *nvecs += 2;
     306  3725997962 :         *nbytes += sizeof(struct xfs_inode_log_format) +
     307  1862998981 :                    xfs_log_dinode_size(ip->i_mount);
     308             : 
     309  1862998981 :         xfs_inode_item_data_fork_size(iip, nvecs, nbytes);
     310  1863032485 :         if (xfs_inode_has_attr_fork(ip))
     311  1781871007 :                 xfs_inode_item_attr_fork_size(iip, nvecs, nbytes);
     312  1863015897 : }
     313             : 
     314             : STATIC void
     315  1863192704 : 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  1863192704 :         struct xfs_inode        *ip = iip->ili_inode;
     322  1863192704 :         size_t                  data_bytes;
     323             : 
     324  1863192704 :         switch (ip->i_df.if_format) {
     325   978365759 :         case XFS_DINODE_FMT_EXTENTS:
     326   978365759 :                 iip->ili_fields &=
     327             :                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
     328             : 
     329   978365759 :                 if ((iip->ili_fields & XFS_ILOG_DEXT) &&
     330   335071632 :                     ip->i_df.if_nextents > 0 &&
     331   311597411 :                     ip->i_df.if_bytes > 0) {
     332   311601282 :                         struct xfs_bmbt_rec *p;
     333             : 
     334   311601282 :                         ASSERT(xfs_iext_count(&ip->i_df) > 0);
     335             : 
     336   311600663 :                         p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT);
     337   311605984 :                         data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK);
     338   311604360 :                         xlog_finish_iovec(lv, *vecp, data_bytes);
     339             : 
     340   311603434 :                         ASSERT(data_bytes <= ip->i_df.if_bytes);
     341             : 
     342   311603434 :                         ilf->ilf_dsize = data_bytes;
     343   311603434 :                         ilf->ilf_size++;
     344             :                 } else {
     345   666764477 :                         iip->ili_fields &= ~XFS_ILOG_DEXT;
     346             :                 }
     347             :                 break;
     348   528621938 :         case XFS_DINODE_FMT_BTREE:
     349             :         case XFS_DINODE_FMT_RMAP:
     350             :         case XFS_DINODE_FMT_REFCOUNT:
     351   528621938 :                 iip->ili_fields &=
     352             :                         ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV);
     353             : 
     354   528621938 :                 if ((iip->ili_fields & XFS_ILOG_DBROOT) &&
     355   334815259 :                     ip->i_df.if_broot_bytes > 0) {
     356   334815259 :                         ASSERT(ip->i_df.if_broot != NULL);
     357   334815259 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT,
     358   334815259 :                                         ip->i_df.if_broot,
     359   334815259 :                                         ip->i_df.if_broot_bytes);
     360   334814965 :                         ilf->ilf_dsize = ip->i_df.if_broot_bytes;
     361   334814965 :                         ilf->ilf_size++;
     362             :                 } else {
     363   193806679 :                         ASSERT(!(iip->ili_fields &
     364             :                                  XFS_ILOG_DBROOT));
     365   193806679 :                         iip->ili_fields &= ~XFS_ILOG_DBROOT;
     366             :                 }
     367             :                 break;
     368   287932831 :         case XFS_DINODE_FMT_LOCAL:
     369   287932831 :                 iip->ili_fields &=
     370             :                         ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV);
     371   287932831 :                 if ((iip->ili_fields & XFS_ILOG_DDATA) &&
     372   281489285 :                     ip->i_df.if_bytes > 0) {
     373   281489285 :                         ASSERT(ip->i_df.if_u1.if_data != NULL);
     374   281489285 :                         ASSERT(ip->i_disk_size > 0);
     375   281489285 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL,
     376   281489285 :                                         ip->i_df.if_u1.if_data,
     377   281489285 :                                         ip->i_df.if_bytes);
     378   281490196 :                         ilf->ilf_dsize = (unsigned)ip->i_df.if_bytes;
     379   281490196 :                         ilf->ilf_size++;
     380             :                 } else {
     381     6443546 :                         iip->ili_fields &= ~XFS_ILOG_DDATA;
     382             :                 }
     383             :                 break;
     384    68272176 :         case XFS_DINODE_FMT_DEV:
     385    68272176 :                 iip->ili_fields &=
     386             :                         ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT);
     387    68272176 :                 if (iip->ili_fields & XFS_ILOG_DEV)
     388    28437235 :                         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  1863195473 : }
     395             : 
     396             : STATIC void
     397  1781924226 : 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  1781924226 :         struct xfs_inode        *ip = iip->ili_inode;
     404  1781924226 :         size_t                  data_bytes;
     405             : 
     406  1781924226 :         switch (ip->i_af.if_format) {
     407   368149863 :         case XFS_DINODE_FMT_EXTENTS:
     408   368149863 :                 iip->ili_fields &=
     409             :                         ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT);
     410             : 
     411   368149863 :                 if ((iip->ili_fields & XFS_ILOG_AEXT) &&
     412    44887820 :                     ip->i_af.if_nextents > 0 &&
     413    42710454 :                     ip->i_af.if_bytes > 0) {
     414    42710434 :                         struct xfs_bmbt_rec *p;
     415             : 
     416    42710434 :                         ASSERT(xfs_iext_count(&ip->i_af) ==
     417             :                                 ip->i_af.if_nextents);
     418             : 
     419    42710421 :                         p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT);
     420    42710436 :                         data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK);
     421    42710394 :                         xlog_finish_iovec(lv, *vecp, data_bytes);
     422             : 
     423    42710412 :                         ilf->ilf_asize = data_bytes;
     424    42710412 :                         ilf->ilf_size++;
     425             :                 } else {
     426   325439429 :                         iip->ili_fields &= ~XFS_ILOG_AEXT;
     427             :                 }
     428             :                 break;
     429      122317 :         case XFS_DINODE_FMT_BTREE:
     430      122317 :                 iip->ili_fields &=
     431             :                         ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT);
     432             : 
     433      122317 :                 if ((iip->ili_fields & XFS_ILOG_ABROOT) &&
     434      106386 :                     ip->i_af.if_broot_bytes > 0) {
     435      106386 :                         ASSERT(ip->i_af.if_broot != NULL);
     436             : 
     437      106386 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT,
     438      106386 :                                         ip->i_af.if_broot,
     439      106386 :                                         ip->i_af.if_broot_bytes);
     440      106386 :                         ilf->ilf_asize = ip->i_af.if_broot_bytes;
     441      106386 :                         ilf->ilf_size++;
     442             :                 } else {
     443       15931 :                         iip->ili_fields &= ~XFS_ILOG_ABROOT;
     444             :                 }
     445             :                 break;
     446  1413652046 :         case XFS_DINODE_FMT_LOCAL:
     447  1413652046 :                 iip->ili_fields &=
     448             :                         ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT);
     449             : 
     450  1413652046 :                 if ((iip->ili_fields & XFS_ILOG_ADATA) &&
     451   857544521 :                     ip->i_af.if_bytes > 0) {
     452   857544521 :                         ASSERT(ip->i_af.if_u1.if_data != NULL);
     453   857544521 :                         xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL,
     454   857544521 :                                         ip->i_af.if_u1.if_data,
     455   857544521 :                                         ip->i_af.if_bytes);
     456   857549068 :                         ilf->ilf_asize = (unsigned)ip->i_af.if_bytes;
     457   857549068 :                         ilf->ilf_size++;
     458             :                 } else {
     459   556107525 :                         iip->ili_fields &= ~XFS_ILOG_ADATA;
     460             :                 }
     461             :                 break;
     462           0 :         default:
     463           0 :                 ASSERT(0);
     464           0 :                 break;
     465             :         }
     466  1781928751 : }
     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  7452547384 :         struct xfs_log_legacy_timestamp *lits;
     478  7452547384 :         xfs_log_timestamp_t             its;
     479             : 
     480  7452547384 :         if (xfs_inode_has_bigtime(ip))
     481  7451879348 :                 return xfs_inode_encode_bigtime(tv);
     482             : 
     483      676694 :         lits = (struct xfs_log_legacy_timestamp *)&its;
     484      676694 :         lits->t_sec = tv.tv_sec;
     485      676694 :         lits->t_nsec = tv.tv_nsec;
     486             : 
     487      676694 :         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  1863074863 : xfs_copy_dm_fields_to_log_dinode(
     500             :         struct xfs_inode        *ip,
     501             :         struct xfs_log_dinode   *to)
     502             : {
     503  1863074863 :         struct xfs_dinode       *dip;
     504             : 
     505  1863074863 :         dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf,
     506  1863074863 :                              ip->i_imap.im_boffset);
     507             : 
     508  3726216361 :         if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) {
     509           1 :                 to->di_dmevmask = be32_to_cpu(dip->di_dmevmask);
     510           2 :                 to->di_dmstate = be16_to_cpu(dip->di_dmstate);
     511             :         } else {
     512  1863142394 :                 to->di_dmevmask = 0;
     513  1863142394 :                 to->di_dmstate = 0;
     514             :         }
     515  1863142395 : }
     516             : 
     517             : static inline void
     518  1863127703 : xfs_inode_to_log_dinode_iext_counters(
     519             :         struct xfs_inode        *ip,
     520             :         struct xfs_log_dinode   *to)
     521             : {
     522  1863127703 :         if (xfs_inode_has_large_extent_counts(ip)) {
     523  1862965864 :                 to->di_big_nextents = xfs_ifork_nextents(&ip->i_df);
     524  1862965864 :                 to->di_big_anextents = xfs_ifork_nextents(&ip->i_af);
     525  1862965864 :                 to->di_nrext64_pad = 0;
     526             :         } else {
     527      161839 :                 to->di_nextents = xfs_ifork_nextents(&ip->i_df);
     528      323678 :                 to->di_anextents = xfs_ifork_nextents(&ip->i_af);
     529             :         }
     530  1863127703 : }
     531             : 
     532             : static void
     533  1863052926 : xfs_inode_to_log_dinode(
     534             :         struct xfs_inode        *ip,
     535             :         struct xfs_log_dinode   *to,
     536             :         xfs_lsn_t               lsn)
     537             : {
     538  1863052926 :         struct inode            *inode = VFS_I(ip);
     539             : 
     540  1863052926 :         to->di_magic = XFS_DINODE_MAGIC;
     541  1863052926 :         to->di_format = xfs_ifork_format(&ip->i_df);
     542  1863052926 :         to->di_uid = i_uid_read(inode);
     543  1863149111 :         to->di_gid = i_gid_read(inode);
     544  1863141070 :         to->di_projid_lo = ip->i_projid & 0xffff;
     545  1863141070 :         to->di_projid_hi = ip->i_projid >> 16;
     546             : 
     547  1863141070 :         memset(to->di_pad3, 0, sizeof(to->di_pad3));
     548  1863141070 :         to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode->i_atime);
     549  1863141070 :         to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode->i_mtime);
     550  1863141070 :         to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode->i_ctime);
     551  1863141070 :         to->di_nlink = inode->i_nlink;
     552  1863141070 :         to->di_gen = inode->i_generation;
     553  1863141070 :         to->di_mode = inode->i_mode;
     554             : 
     555  1863141070 :         to->di_size = ip->i_disk_size;
     556  1863141070 :         to->di_nblocks = ip->i_nblocks;
     557  1863141070 :         to->di_extsize = ip->i_extsize;
     558  1863141070 :         to->di_forkoff = ip->i_forkoff;
     559  1863141070 :         to->di_aformat = xfs_ifork_format(&ip->i_af);
     560  1863141070 :         to->di_flags = ip->i_diflags;
     561             : 
     562  1863141070 :         xfs_copy_dm_fields_to_log_dinode(ip, to);
     563             : 
     564             :         /* log a dummy value to ensure log structure is fully initialised */
     565  1863125031 :         to->di_next_unlinked = NULLAGINO;
     566             : 
     567  1863125031 :         if (xfs_has_v3inodes(ip->i_mount)) {
     568  1863124174 :                 to->di_version = 3;
     569  1863124174 :                 to->di_changecount = inode_peek_iversion(inode);
     570  1863124174 :                 to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime);
     571  1863124174 :                 to->di_flags2 = ip->i_diflags2;
     572  1863124174 :                 to->di_cowextsize = ip->i_cowextsize;
     573  1863124174 :                 to->di_ino = ip->i_ino;
     574  1863124174 :                 to->di_lsn = lsn;
     575  1863124174 :                 memset(to->di_pad2, 0, sizeof(to->di_pad2));
     576  1863124174 :                 uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid);
     577  1863135061 :                 to->di_v3_pad = 0;
     578             :         } else {
     579         857 :                 to->di_version = 2;
     580         857 :                 to->di_flushiter = ip->i_flushiter;
     581        1714 :                 memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad));
     582             :         }
     583             : 
     584  1863135918 :         xfs_inode_to_log_dinode_iext_counters(ip, to);
     585  1862993507 : }
     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  1863030727 : xfs_inode_item_format_core(
     594             :         struct xfs_inode        *ip,
     595             :         struct xfs_log_vec      *lv,
     596             :         struct xfs_log_iovec    **vecp)
     597             : {
     598  1863030727 :         struct xfs_log_dinode   *dic;
     599             : 
     600  1863030727 :         dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE);
     601  1863080623 :         xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn);
     602  1863095462 :         xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_mount));
     603  1863101598 : }
     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  1862959227 : xfs_inode_item_format(
     619             :         struct xfs_log_item     *lip,
     620             :         struct xfs_log_vec      *lv)
     621             : {
     622  1862959227 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     623  1862959227 :         struct xfs_inode        *ip = iip->ili_inode;
     624  1862959227 :         struct xfs_log_iovec    *vecp = NULL;
     625  1862959227 :         struct xfs_inode_log_format *ilf;
     626             : 
     627  1862959227 :         ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT);
     628  1862993490 :         ilf->ilf_type = XFS_LI_INODE;
     629  1862993490 :         ilf->ilf_ino = ip->i_ino;
     630  1862993490 :         ilf->ilf_blkno = ip->i_imap.im_blkno;
     631  1862993490 :         ilf->ilf_len = ip->i_imap.im_len;
     632  1862993490 :         ilf->ilf_boffset = ip->i_imap.im_boffset;
     633  1862993490 :         ilf->ilf_fields = XFS_ILOG_CORE;
     634  1862993490 :         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  1862993490 :         ilf->ilf_dsize = 0;
     641  1862993490 :         ilf->ilf_asize = 0;
     642  1862993490 :         ilf->ilf_pad = 0;
     643  1862993490 :         memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u));
     644             : 
     645  1862993490 :         xlog_finish_iovec(lv, vecp, sizeof(*ilf));
     646             : 
     647  1863030068 :         xfs_inode_item_format_core(ip, lv, &vecp);
     648  1863144125 :         xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp);
     649  1863161394 :         if (xfs_inode_has_attr_fork(ip)) {
     650  1781908655 :                 xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp);
     651             :         } else {
     652    81252739 :                 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  1863151742 :         ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP);
     658  1863151742 : }
     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   525061442 : xfs_inode_item_pin(
     666             :         struct xfs_log_item     *lip)
     667             : {
     668   525061442 :         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
     669             : 
     670   525061442 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     671   525061738 :         ASSERT(lip->li_buf);
     672             : 
     673   525061738 :         trace_xfs_inode_pin(ip, _RET_IP_);
     674   525062607 :         atomic_inc(&ip->i_pincount);
     675   525063023 : }
     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   525071333 : xfs_inode_item_unpin(
     692             :         struct xfs_log_item     *lip,
     693             :         int                     remove)
     694             : {
     695   525071333 :         struct xfs_inode        *ip = INODE_ITEM(lip)->ili_inode;
     696             : 
     697   525071333 :         trace_xfs_inode_unpin(ip, _RET_IP_);
     698   525074913 :         ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE));
     699   525071333 :         ASSERT(atomic_read(&ip->i_pincount) > 0);
     700  1050142666 :         if (atomic_dec_and_test(&ip->i_pincount))
     701   508534990 :                 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT);
     702   525071333 : }
     703             : 
     704             : STATIC uint
     705   143159429 : 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   143159429 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     712   143159429 :         struct xfs_inode        *ip = iip->ili_inode;
     713   143159429 :         struct xfs_buf          *bp = lip->li_buf;
     714   143159429 :         uint                    rval = XFS_ITEM_SUCCESS;
     715   143159429 :         int                     error;
     716             : 
     717   143159429 :         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   143146320 :         if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp))
     728             :                 return XFS_ITEM_PINNED;
     729             : 
     730   282322026 :         if (xfs_iflags_test(ip, XFS_IFLUSHING))
     731             :                 return XFS_ITEM_FLUSHING;
     732             : 
     733    47319877 :         if (!xfs_buf_trylock(bp))
     734             :                 return XFS_ITEM_LOCKED;
     735             : 
     736    47283597 :         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    47283597 :         xfs_buf_hold(bp);
     745    47283597 :         error = xfs_iflush_cluster(bp);
     746    47283597 :         if (!error) {
     747    46941300 :                 if (!xfs_buf_delwri_queue(bp, buffer_list))
     748      875603 :                         rval = XFS_ITEM_FLUSHING;
     749    46941300 :                 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      342297 :                 if (error == -EAGAIN)
     756       72740 :                         xfs_buf_relse(bp);
     757             :                 rval = XFS_ITEM_LOCKED;
     758             :         }
     759             : 
     760    47283597 :         spin_lock(&lip->li_ailp->ail_lock);
     761    47283597 :         return rval;
     762             : }
     763             : 
     764             : /*
     765             :  * Unlock the inode associated with the inode log item.
     766             :  */
     767             : STATIC void
     768  2972470950 : xfs_inode_item_release(
     769             :         struct xfs_log_item     *lip)
     770             : {
     771  2972470950 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     772  2972470950 :         struct xfs_inode        *ip = iip->ili_inode;
     773  2972470950 :         unsigned short          lock_flags;
     774             : 
     775  2972470950 :         ASSERT(ip->i_itemp != NULL);
     776  2972470950 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
     777             : 
     778  2972579163 :         lock_flags = iip->ili_lock_flags;
     779  2972579163 :         iip->ili_lock_flags = 0;
     780  2972579163 :         if (lock_flags)
     781   361451747 :                 xfs_iunlock(ip, lock_flags);
     782  2972577424 : }
     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   525071332 : xfs_inode_item_committed(
     808             :         struct xfs_log_item     *lip,
     809             :         xfs_lsn_t               lsn)
     810             : {
     811   525071332 :         struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     812   525071332 :         struct xfs_inode        *ip = iip->ili_inode;
     813             : 
     814  1050142665 :         if (xfs_iflags_test(ip, XFS_ISTALE)) {
     815     2775642 :                 xfs_inode_item_unpin(lip, 0);
     816     2775642 :                 return -1;
     817             :         }
     818             :         return lsn;
     819             : }
     820             : 
     821             : STATIC void
     822  2506035387 : xfs_inode_item_committing(
     823             :         struct xfs_log_item     *lip,
     824             :         xfs_csn_t               seq)
     825             : {
     826  2506035387 :         INODE_ITEM(lip)->ili_commit_seq = seq;
     827  2506035387 :         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    55161138 : xfs_inode_item_init(
     849             :         struct xfs_inode        *ip,
     850             :         struct xfs_mount        *mp)
     851             : {
     852    55161138 :         struct xfs_inode_log_item *iip;
     853             : 
     854    55161138 :         ASSERT(ip->i_itemp == NULL);
     855    55161138 :         iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache,
     856             :                                               GFP_KERNEL | __GFP_NOFAIL);
     857             : 
     858    55160007 :         iip->ili_inode = ip;
     859    55160007 :         spin_lock_init(&iip->ili_lock);
     860    55161021 :         xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE,
     861             :                                                 &xfs_inode_item_ops);
     862    55160687 : }
     863             : 
     864             : /*
     865             :  * Free the inode log item and any memory hanging off of it.
     866             :  */
     867             : void
     868    54869431 : xfs_inode_item_destroy(
     869             :         struct xfs_inode        *ip)
     870             : {
     871    54869431 :         struct xfs_inode_log_item *iip = ip->i_itemp;
     872             : 
     873    54869431 :         ASSERT(iip->ili_item.li_buf == NULL);
     874             : 
     875    54869431 :         ip->i_itemp = NULL;
     876    54869431 :         kmem_free(iip->ili_item.li_lv_shadow);
     877    55012665 :         kmem_cache_free(xfs_ili_cache, iip);
     878    55095144 : }
     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    46872974 : xfs_iflush_ail_updates(
     888             :         struct xfs_ail          *ailp,
     889             :         struct list_head        *list)
     890             : {
     891    46872974 :         struct xfs_log_item     *lip;
     892    46872974 :         xfs_lsn_t               tail_lsn = 0;
     893             : 
     894             :         /* this is an opencoded batch version of xfs_trans_ail_delete */
     895    46872974 :         spin_lock(&ailp->ail_lock);
     896   329150937 :         list_for_each_entry(lip, list, li_bio_list) {
     897   282277963 :                 xfs_lsn_t       lsn;
     898             : 
     899   282277963 :                 clear_bit(XFS_LI_FAILED, &lip->li_flags);
     900   282277963 :                 if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn)
     901         376 :                         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   282277587 :                 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   282277587 :                 lsn = xfs_ail_delete_one(ailp, lip);
     915   282277587 :                 if (!tail_lsn && lsn)
     916      490419 :                         tail_lsn = lsn;
     917             :         }
     918    46872974 :         xfs_ail_update_finish(ailp, tail_lsn);
     919    46872974 : }
     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    47473036 : xfs_iflush_finish(
     929             :         struct xfs_buf          *bp,
     930             :         struct list_head        *list)
     931             : {
     932    47473036 :         struct xfs_log_item     *lip, *n;
     933             : 
     934   333007717 :         list_for_each_entry_safe(lip, n, list, li_bio_list) {
     935   285534681 :                 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     936   285534681 :                 bool    drop_buffer = false;
     937             : 
     938   285534681 :                 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   285534681 :                 ASSERT(iip->ili_item.li_buf == bp);
     946   285534681 :                 if (!iip->ili_fields) {
     947   278563399 :                         iip->ili_item.li_buf = NULL;
     948   278563399 :                         list_del_init(&lip->li_bio_list);
     949   278563399 :                         drop_buffer = true;
     950             :                 }
     951   285534681 :                 iip->ili_last_fields = 0;
     952   285534681 :                 iip->ili_flush_lsn = 0;
     953   285534681 :                 spin_unlock(&iip->ili_lock);
     954   285534681 :                 xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING);
     955   285534681 :                 if (drop_buffer)
     956   278563399 :                         xfs_buf_rele(bp);
     957             :         }
     958    47473036 : }
     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    47473036 : xfs_buf_inode_iodone(
     967             :         struct xfs_buf          *bp)
     968             : {
     969    47473036 :         struct xfs_log_item     *lip, *n;
     970    47473036 :         LIST_HEAD(flushed_inodes);
     971    47473036 :         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   350497452 :         list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) {
     978   303024416 :                 struct xfs_inode_log_item *iip = INODE_ITEM(lip);
     979             : 
     980   606048831 :                 if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) {
     981     3176327 :                         xfs_iflush_abort(iip->ili_inode);
     982     3176327 :                         continue;
     983             :                 }
     984   299848088 :                 if (!iip->ili_last_fields)
     985    14313416 :                         continue;
     986             : 
     987             :                 /* Do an unlocked check for needing the AIL lock. */
     988   288791390 :                 if (iip->ili_flush_lsn == lip->li_lsn ||
     989     3256718 :                     test_bit(XFS_LI_FAILED, &lip->li_flags))
     990   282277954 :                         list_move_tail(&lip->li_bio_list, &ail_updates);
     991             :                 else
     992     3256718 :                         list_move_tail(&lip->li_bio_list, &flushed_inodes);
     993             :         }
     994             : 
     995    47473036 :         if (!list_empty(&ail_updates)) {
     996    46872974 :                 xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates);
     997    46872974 :                 list_splice_tail(&ail_updates, &flushed_inodes);
     998             :         }
     999             : 
    1000    47473036 :         xfs_iflush_finish(bp, &flushed_inodes);
    1001    47473036 :         if (!list_empty(&flushed_inodes))
    1002     3785772 :                 list_splice_tail(&flushed_inodes, &bp->b_li_list);
    1003    47473036 : }
    1004             : 
    1005             : void
    1006           0 : xfs_buf_inode_io_fail(
    1007             :         struct xfs_buf          *bp)
    1008             : {
    1009           0 :         struct xfs_log_item     *lip;
    1010             : 
    1011           0 :         list_for_each_entry(lip, &bp->b_li_list, li_bio_list)
    1012           0 :                 set_bit(XFS_LI_FAILED, &lip->li_flags);
    1013           0 : }
    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     5333016 : xfs_iflush_abort_clean(
    1023             :         struct xfs_inode_log_item *iip)
    1024             : {
    1025     5333016 :         iip->ili_last_fields = 0;
    1026     5333016 :         iip->ili_fields = 0;
    1027     5333016 :         iip->ili_fsync_fields = 0;
    1028     5333016 :         iip->ili_flush_lsn = 0;
    1029     5333016 :         iip->ili_item.li_buf = NULL;
    1030     5333016 :         list_del_init(&iip->ili_item.li_bio_list);
    1031     5333016 : }
    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     5333016 : xfs_iflush_abort(
    1047             :         struct xfs_inode        *ip)
    1048             : {
    1049     5333016 :         struct xfs_inode_log_item *iip = ip->i_itemp;
    1050     5333016 :         struct xfs_buf          *bp;
    1051             : 
    1052     5333016 :         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     5333016 :         clear_bit(XFS_LI_FAILED, &iip->ili_item.li_flags);
    1070     5333016 :         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     5333016 :         spin_lock(&iip->ili_lock);
    1077     5333016 :         bp = iip->ili_item.li_buf;
    1078     5333016 :         xfs_iflush_abort_clean(iip);
    1079     5333016 :         spin_unlock(&iip->ili_lock);
    1080             : 
    1081     5333016 :         xfs_iflags_clear(ip, XFS_IFLUSHING);
    1082     5333016 :         if (bp)
    1083     4227827 :                 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   305761830 : xfs_iflush_shutdown_abort(
    1094             :         struct xfs_inode        *ip)
    1095             : {
    1096   305761830 :         struct xfs_inode_log_item *iip = ip->i_itemp;
    1097   305761830 :         struct xfs_buf          *bp;
    1098             : 
    1099   305761830 :         if (!iip) {
    1100             :                 /* clean inode, nothing to do */
    1101   304619274 :                 xfs_iflags_clear(ip, XFS_IFLUSHING);
    1102   304619274 :                 return;
    1103             :         }
    1104             : 
    1105     1142556 :         spin_lock(&iip->ili_lock);
    1106     1142556 :         bp = iip->ili_item.li_buf;
    1107     1142556 :         if (!bp) {
    1108     1105189 :                 spin_unlock(&iip->ili_lock);
    1109     1105189 :                 xfs_iflush_abort(ip);
    1110     1105189 :                 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       37367 :         xfs_buf_hold(bp);
    1120       37367 :         spin_unlock(&iip->ili_lock);
    1121       37367 :         xfs_buf_lock(bp);
    1122             : 
    1123       37367 :         spin_lock(&iip->ili_lock);
    1124       37367 :         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       37367 :         ASSERT(iip->ili_item.li_buf == bp);
    1147       37367 :         spin_unlock(&iip->ili_lock);
    1148       37367 :         xfs_iflush_abort(ip);
    1149       37367 :         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