Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000,2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 7 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 10 | #include "xfs_log_format.h" |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 11 | #include "xfs_trans_resv.h" |
| 12 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 14 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 15 | #include "xfs_trans_priv.h" |
| 16 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Jeff Layton | f0e2828 | 2017-12-11 06:35:19 -0500 | [diff] [blame] | 18 | #include <linux/iversion.h> |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | /* |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 21 | * Add a locked inode to the transaction. |
| 22 | * |
| 23 | * The inode must be locked, and it cannot be associated with any transaction. |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 24 | * If lock_flags is non-zero the inode will be unlocked on transaction commit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
| 26 | void |
| 27 | xfs_trans_ijoin( |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 28 | struct xfs_trans *tp, |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 29 | struct xfs_inode *ip, |
| 30 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Christoph Hellwig | fd9cbe5 | 2020-04-30 12:52:19 -0700 | [diff] [blame] | 32 | struct xfs_inode_log_item *iip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 34 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | if (ip->i_itemp == NULL) |
| 36 | xfs_inode_item_init(ip, ip->i_mount); |
| 37 | iip = ip->i_itemp; |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 38 | |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 39 | ASSERT(iip->ili_lock_flags == 0); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 40 | iip->ili_lock_flags = lock_flags; |
Dave Chinner | 96355d5a | 2020-06-29 14:48:45 -0700 | [diff] [blame] | 41 | ASSERT(!xfs_iflags_test(ip, XFS_ISTALE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Get a log_item_desc to point at the new item. |
| 45 | */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 46 | xfs_trans_add_item(tp, &iip->ili_item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 50 | * Transactional inode timestamp update. Requires the inode to be locked and |
| 51 | * joined to the transaction supplied. Relies on the transaction subsystem to |
| 52 | * track dirty state and update/writeback the inode accordingly. |
| 53 | */ |
| 54 | void |
| 55 | xfs_trans_ichgtime( |
| 56 | struct xfs_trans *tp, |
| 57 | struct xfs_inode *ip, |
| 58 | int flags) |
| 59 | { |
| 60 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | 8d2d878 | 2019-11-12 08:20:42 -0800 | [diff] [blame] | 61 | struct timespec64 tv; |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 62 | |
| 63 | ASSERT(tp); |
| 64 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 65 | |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 66 | tv = current_time(inode); |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 67 | |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 68 | if (flags & XFS_ICHGTIME_MOD) |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 69 | inode->i_mtime = tv; |
Dave Chinner | 3987848 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 70 | if (flags & XFS_ICHGTIME_CHG) |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 71 | inode->i_ctime = tv; |
Christoph Hellwig | 8d2d878 | 2019-11-12 08:20:42 -0800 | [diff] [blame] | 72 | if (flags & XFS_ICHGTIME_CREATE) |
| 73 | ip->i_d.di_crtime = tv; |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 77 | * This is called to mark the fields indicated in fieldmask as needing to be |
| 78 | * logged when the transaction is committed. The inode must already be |
| 79 | * associated with the given transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | * |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 81 | * The values for fieldmask are defined in xfs_inode_item.h. We always log all |
| 82 | * of the core inode if any of it has changed, and we always log all of the |
| 83 | * inline data/extents/b-tree root if any of them has changed. |
| 84 | * |
| 85 | * Grab and pin the cluster buffer associated with this inode to avoid RMW |
| 86 | * cycles at inode writeback time. Avoid the need to add error handling to every |
| 87 | * xfs_trans_log_inode() call by shutting down on read error. This will cause |
| 88 | * transactions to fail and everything to error out, just like if we return a |
| 89 | * read error in a dirty transaction and cancel it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | */ |
| 91 | void |
| 92 | xfs_trans_log_inode( |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 93 | struct xfs_trans *tp, |
| 94 | struct xfs_inode *ip, |
| 95 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 97 | struct xfs_inode_log_item *iip = ip->i_itemp; |
| 98 | struct inode *inode = VFS_I(ip); |
| 99 | uint iversion_flags = 0; |
Christoph Hellwig | c3b1b13 | 2018-03-06 17:04:00 -0800 | [diff] [blame] | 100 | |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 101 | ASSERT(iip); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 102 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Dave Chinner | 96355d5a | 2020-06-29 14:48:45 -0700 | [diff] [blame] | 103 | ASSERT(!xfs_iflags_test(ip, XFS_ISTALE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 105 | tp->t_flags |= XFS_TRANS_DIRTY; |
| 106 | |
Dave Chinner | dc037ad | 2013-06-27 16:04:59 +1000 | [diff] [blame] | 107 | /* |
Christoph Hellwig | c3b1b13 | 2018-03-06 17:04:00 -0800 | [diff] [blame] | 108 | * Don't bother with i_lock for the I_DIRTY_TIME check here, as races |
| 109 | * don't matter - we either will need an extra transaction in 24 hours |
| 110 | * to log the timestamps, or will clear already cleared fields in the |
| 111 | * worst case. |
| 112 | */ |
| 113 | if (inode->i_state & (I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED)) { |
| 114 | spin_lock(&inode->i_lock); |
| 115 | inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED); |
| 116 | spin_unlock(&inode->i_lock); |
| 117 | } |
| 118 | |
| 119 | /* |
Dave Chinner | dc037ad | 2013-06-27 16:04:59 +1000 | [diff] [blame] | 120 | * First time we log the inode in a transaction, bump the inode change |
Jeff Layton | d17260f | 2017-12-11 06:35:23 -0500 | [diff] [blame] | 121 | * counter if it is configured for this to occur. While we have the |
| 122 | * inode locked exclusively for metadata modification, we can usually |
| 123 | * avoid setting XFS_ILOG_CORE if no one has queried the value since |
| 124 | * the last time it was incremented. If we have XFS_ILOG_CORE already |
| 125 | * set however, then go ahead and bump the i_version counter |
| 126 | * unconditionally. |
Dave Chinner | dc037ad | 2013-06-27 16:04:59 +1000 | [diff] [blame] | 127 | */ |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 128 | if (!test_and_set_bit(XFS_LI_DIRTY, &iip->ili_item.li_flags)) { |
| 129 | if (IS_I_VERSION(inode) && |
| 130 | inode_maybe_inc_iversion(inode, flags & XFS_ILOG_CORE)) |
| 131 | iversion_flags = XFS_ILOG_CORE; |
Dave Chinner | dc037ad | 2013-06-27 16:04:59 +1000 | [diff] [blame] | 132 | } |
| 133 | |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Record the specific change for fdatasync optimisation. This allows |
| 136 | * fdatasync to skip log forces for inodes that are only timestamp |
| 137 | * dirty. |
| 138 | */ |
| 139 | spin_lock(&iip->ili_lock); |
| 140 | iip->ili_fsync_fields |= flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 142 | if (!iip->ili_item.li_buf) { |
| 143 | struct xfs_buf *bp; |
| 144 | int error; |
| 145 | |
| 146 | /* |
| 147 | * We hold the ILOCK here, so this inode is not going to be |
| 148 | * flushed while we are here. Further, because there is no |
| 149 | * buffer attached to the item, we know that there is no IO in |
| 150 | * progress, so nothing will clear the ili_fields while we read |
| 151 | * in the buffer. Hence we can safely drop the spin lock and |
| 152 | * read the buffer knowing that the state will not change from |
| 153 | * here. |
| 154 | */ |
| 155 | spin_unlock(&iip->ili_lock); |
| 156 | error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, NULL, |
| 157 | &bp, 0); |
| 158 | if (error) { |
| 159 | xfs_force_shutdown(ip->i_mount, SHUTDOWN_META_IO_ERROR); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * We need an explicit buffer reference for the log item but |
| 165 | * don't want the buffer to remain attached to the transaction. |
Dave Chinner | 48d55e2 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 166 | * Hold the buffer but release the transaction reference once |
| 167 | * we've attached the inode log item to the buffer log item |
| 168 | * list. |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 169 | */ |
| 170 | xfs_buf_hold(bp); |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 171 | spin_lock(&iip->ili_lock); |
| 172 | iip->ili_item.li_buf = bp; |
Dave Chinner | 48d55e2 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 173 | bp->b_flags |= _XBF_INODES; |
| 174 | list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list); |
| 175 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 298f7be | 2020-06-29 14:49:15 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | /* |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 179 | * Always OR in the bits from the ili_last_fields field. This is to |
| 180 | * coordinate with the xfs_iflush() and xfs_iflush_done() routines in |
| 181 | * the eventual clearing of the ili_fields bits. See the big comment in |
| 182 | * xfs_iflush() for an explanation of this coordination mechanism. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | */ |
Dave Chinner | 1319ebe | 2020-06-29 14:48:46 -0700 | [diff] [blame] | 184 | iip->ili_fields |= (flags | iip->ili_last_fields | iversion_flags); |
| 185 | spin_unlock(&iip->ili_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 187 | |
| 188 | int |
| 189 | xfs_trans_roll_inode( |
| 190 | struct xfs_trans **tpp, |
| 191 | struct xfs_inode *ip) |
| 192 | { |
| 193 | int error; |
| 194 | |
| 195 | xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE); |
| 196 | error = xfs_trans_roll(tpp); |
| 197 | if (!error) |
| 198 | xfs_trans_ijoin(*tpp, ip, 0); |
| 199 | return error; |
| 200 | } |