Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 5 | */ |
| 6 | #include "xfs.h" |
| 7 | #include "xfs_fs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [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" |
| 11 | #include "xfs_trans_resv.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 12 | #include "xfs_sb.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 13 | #include "xfs_mount.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 14 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 15 | #include "xfs_trans.h" |
| 16 | #include "xfs_trans_priv.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 17 | #include "xfs_inode_item.h" |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 18 | #include "xfs_quota.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 19 | #include "xfs_trace.h" |
Dave Chinner | 6d8b79c | 2012-10-08 21:56:09 +1100 | [diff] [blame] | 20 | #include "xfs_icache.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 21 | #include "xfs_bmap_util.h" |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 22 | #include "xfs_dquot_item.h" |
| 23 | #include "xfs_dquot.h" |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 24 | #include "xfs_reflink.h" |
Christoph Hellwig | bb8a66a | 2020-05-14 14:01:19 -0700 | [diff] [blame] | 25 | #include "xfs_ialloc.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 26 | |
Jeff Layton | f0e2828 | 2017-12-11 06:35:19 -0500 | [diff] [blame] | 27 | #include <linux/iversion.h> |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 28 | |
Darrick J. Wong | c809d7e | 2021-06-01 13:49:52 -0700 | [diff] [blame] | 29 | /* Radix tree tags for incore inode tree. */ |
| 30 | |
| 31 | /* inode is to be reclaimed */ |
| 32 | #define XFS_ICI_RECLAIM_TAG 0 |
| 33 | /* Inode has speculative preallocations (posteof or cow) to clean. */ |
| 34 | #define XFS_ICI_BLOCKGC_TAG 1 |
| 35 | |
| 36 | /* |
| 37 | * The goal for walking incore inodes. These can correspond with incore inode |
| 38 | * radix tree tags when convenient. Avoid existing XFS_IWALK namespace. |
| 39 | */ |
| 40 | enum xfs_icwalk_goal { |
| 41 | /* Goals that are not related to tags; these must be < 0. */ |
| 42 | XFS_ICWALK_DQRELE = -1, |
| 43 | |
| 44 | /* Goals directly associated with tagged inodes. */ |
| 45 | XFS_ICWALK_BLOCKGC = XFS_ICI_BLOCKGC_TAG, |
| 46 | }; |
| 47 | |
| 48 | #define XFS_ICWALK_NULL_TAG (-1U) |
| 49 | |
| 50 | /* Compute the inode radix tree tag for this goal. */ |
| 51 | static inline unsigned int |
| 52 | xfs_icwalk_tag(enum xfs_icwalk_goal goal) |
| 53 | { |
| 54 | return goal < 0 ? XFS_ICWALK_NULL_TAG : goal; |
| 55 | } |
| 56 | |
Darrick J. Wong | 7fdff52 | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 57 | static int xfs_icwalk(struct xfs_mount *mp, |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 58 | enum xfs_icwalk_goal goal, void *args); |
Darrick J. Wong | 7fdff52 | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 59 | static int xfs_icwalk_ag(struct xfs_perag *pag, |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 60 | enum xfs_icwalk_goal goal, void *args); |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 61 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 62 | /* |
Darrick J. Wong | 1ad2cfe | 2021-05-31 11:31:57 -0700 | [diff] [blame] | 63 | * Private inode cache walk flags for struct xfs_eofblocks. Must not coincide |
| 64 | * with XFS_EOF_FLAGS_*. |
| 65 | */ |
| 66 | #define XFS_ICWALK_FLAG_DROP_UDQUOT (1U << 31) |
| 67 | #define XFS_ICWALK_FLAG_DROP_GDQUOT (1U << 30) |
| 68 | #define XFS_ICWALK_FLAG_DROP_PDQUOT (1U << 29) |
| 69 | |
| 70 | #define XFS_ICWALK_PRIVATE_FLAGS (XFS_ICWALK_FLAG_DROP_UDQUOT | \ |
| 71 | XFS_ICWALK_FLAG_DROP_GDQUOT | \ |
| 72 | XFS_ICWALK_FLAG_DROP_PDQUOT) |
| 73 | |
| 74 | /* |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 75 | * Allocate and initialise an xfs_inode. |
| 76 | */ |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame] | 77 | struct xfs_inode * |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 78 | xfs_inode_alloc( |
| 79 | struct xfs_mount *mp, |
| 80 | xfs_ino_t ino) |
| 81 | { |
| 82 | struct xfs_inode *ip; |
| 83 | |
| 84 | /* |
Carlos Maiolino | 3050bd0 | 2020-07-22 09:23:04 -0700 | [diff] [blame] | 85 | * XXX: If this didn't occur in transactions, we could drop GFP_NOFAIL |
| 86 | * and return NULL here on ENOMEM. |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 87 | */ |
Carlos Maiolino | 3050bd0 | 2020-07-22 09:23:04 -0700 | [diff] [blame] | 88 | ip = kmem_cache_alloc(xfs_inode_zone, GFP_KERNEL | __GFP_NOFAIL); |
| 89 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 90 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 91 | kmem_cache_free(xfs_inode_zone, ip); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 95 | /* VFS doesn't initialise i_mode! */ |
| 96 | VFS_I(ip)->i_mode = 0; |
| 97 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 98 | XFS_STATS_INC(mp, vn_active); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 99 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 100 | ASSERT(ip->i_ino == 0); |
| 101 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 102 | /* initialise the xfs inode */ |
| 103 | ip->i_ino = ino; |
| 104 | ip->i_mount = mp; |
| 105 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); |
| 106 | ip->i_afp = NULL; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 107 | ip->i_cowfp = NULL; |
Christoph Hellwig | 3ba738d | 2018-07-17 16:51:50 -0700 | [diff] [blame] | 108 | memset(&ip->i_df, 0, sizeof(ip->i_df)); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 109 | ip->i_flags = 0; |
| 110 | ip->i_delayed_blks = 0; |
Christoph Hellwig | 3e09ab8 | 2021-03-29 11:11:45 -0700 | [diff] [blame] | 111 | ip->i_diflags2 = mp->m_ino_geo.new_diflags2; |
Christoph Hellwig | 6e73a54 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 112 | ip->i_nblocks = 0; |
Christoph Hellwig | 7821ea3 | 2021-03-29 11:11:44 -0700 | [diff] [blame] | 113 | ip->i_forkoff = 0; |
Darrick J. Wong | 6772c1f | 2019-04-12 07:40:25 -0700 | [diff] [blame] | 114 | ip->i_sick = 0; |
| 115 | ip->i_checked = 0; |
Darrick J. Wong | cb357bf | 2019-04-15 13:13:20 -0700 | [diff] [blame] | 116 | INIT_WORK(&ip->i_ioend_work, xfs_end_io); |
| 117 | INIT_LIST_HEAD(&ip->i_ioend_list); |
| 118 | spin_lock_init(&ip->i_ioend_lock); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 119 | |
| 120 | return ip; |
| 121 | } |
| 122 | |
| 123 | STATIC void |
| 124 | xfs_inode_free_callback( |
| 125 | struct rcu_head *head) |
| 126 | { |
| 127 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 128 | struct xfs_inode *ip = XFS_I(inode); |
| 129 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 130 | switch (VFS_I(ip)->i_mode & S_IFMT) { |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 131 | case S_IFREG: |
| 132 | case S_IFDIR: |
| 133 | case S_IFLNK: |
Christoph Hellwig | ef83851 | 2020-05-18 10:29:27 -0700 | [diff] [blame] | 134 | xfs_idestroy_fork(&ip->i_df); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | |
Christoph Hellwig | ef83851 | 2020-05-18 10:29:27 -0700 | [diff] [blame] | 138 | if (ip->i_afp) { |
| 139 | xfs_idestroy_fork(ip->i_afp); |
| 140 | kmem_cache_free(xfs_ifork_zone, ip->i_afp); |
| 141 | } |
| 142 | if (ip->i_cowfp) { |
| 143 | xfs_idestroy_fork(ip->i_cowfp); |
| 144 | kmem_cache_free(xfs_ifork_zone, ip->i_cowfp); |
| 145 | } |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 146 | if (ip->i_itemp) { |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 147 | ASSERT(!test_bit(XFS_LI_IN_AIL, |
| 148 | &ip->i_itemp->ili_item.li_flags)); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 149 | xfs_inode_item_destroy(ip); |
| 150 | ip->i_itemp = NULL; |
| 151 | } |
| 152 | |
Carlos Maiolino | 377bcd5 | 2019-11-14 12:43:04 -0800 | [diff] [blame] | 153 | kmem_cache_free(xfs_inode_zone, ip); |
Dave Chinner | 1f2dcfe | 2016-05-18 14:01:53 +1000 | [diff] [blame] | 154 | } |
| 155 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 156 | static void |
| 157 | __xfs_inode_free( |
| 158 | struct xfs_inode *ip) |
| 159 | { |
| 160 | /* asserts to verify all state is correct here */ |
| 161 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
Dave Chinner | 48d55e2 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 162 | ASSERT(!ip->i_itemp || list_empty(&ip->i_itemp->ili_item.li_bio_list)); |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 163 | XFS_STATS_DEC(ip->i_mount, vn_active); |
| 164 | |
| 165 | call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback); |
| 166 | } |
| 167 | |
Dave Chinner | 1f2dcfe | 2016-05-18 14:01:53 +1000 | [diff] [blame] | 168 | void |
| 169 | xfs_inode_free( |
| 170 | struct xfs_inode *ip) |
| 171 | { |
Dave Chinner | 718ecc5 | 2020-08-17 16:41:01 -0700 | [diff] [blame] | 172 | ASSERT(!xfs_iflags_test(ip, XFS_IFLUSHING)); |
Brian Foster | 98efe8a | 2016-11-10 08:23:22 +1100 | [diff] [blame] | 173 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 174 | /* |
| 175 | * Because we use RCU freeing we need to ensure the inode always |
| 176 | * appears to be reclaimed with an invalid inode number when in the |
| 177 | * free state. The ip->i_flags_lock provides the barrier against lookup |
| 178 | * races. |
| 179 | */ |
| 180 | spin_lock(&ip->i_flags_lock); |
| 181 | ip->i_flags = XFS_IRECLAIM; |
| 182 | ip->i_ino = 0; |
| 183 | spin_unlock(&ip->i_flags_lock); |
| 184 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 185 | __xfs_inode_free(ip); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 189 | * Queue background inode reclaim work if there are reclaimable inodes and there |
| 190 | * isn't reclaim work already scheduled or in progress. |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 191 | */ |
| 192 | static void |
| 193 | xfs_reclaim_work_queue( |
| 194 | struct xfs_mount *mp) |
| 195 | { |
| 196 | |
| 197 | rcu_read_lock(); |
| 198 | if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) { |
| 199 | queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work, |
| 200 | msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10)); |
| 201 | } |
| 202 | rcu_read_unlock(); |
| 203 | } |
| 204 | |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 205 | static void |
| 206 | xfs_perag_set_reclaim_tag( |
| 207 | struct xfs_perag *pag) |
| 208 | { |
| 209 | struct xfs_mount *mp = pag->pag_mount; |
| 210 | |
Brian Foster | 95989c4 | 2017-06-08 08:23:07 -0700 | [diff] [blame] | 211 | lockdep_assert_held(&pag->pag_ici_lock); |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 212 | if (pag->pag_ici_reclaimable++) |
| 213 | return; |
| 214 | |
| 215 | /* propagate the reclaim tag up into the perag radix tree */ |
| 216 | spin_lock(&mp->m_perag_lock); |
| 217 | radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno, |
| 218 | XFS_ICI_RECLAIM_TAG); |
| 219 | spin_unlock(&mp->m_perag_lock); |
| 220 | |
| 221 | /* schedule periodic background inode reclaim */ |
| 222 | xfs_reclaim_work_queue(mp); |
| 223 | |
| 224 | trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_); |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | xfs_perag_clear_reclaim_tag( |
| 229 | struct xfs_perag *pag) |
| 230 | { |
| 231 | struct xfs_mount *mp = pag->pag_mount; |
| 232 | |
Brian Foster | 95989c4 | 2017-06-08 08:23:07 -0700 | [diff] [blame] | 233 | lockdep_assert_held(&pag->pag_ici_lock); |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 234 | if (--pag->pag_ici_reclaimable) |
| 235 | return; |
| 236 | |
| 237 | /* clear the reclaim tag from the perag radix tree */ |
| 238 | spin_lock(&mp->m_perag_lock); |
| 239 | radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno, |
| 240 | XFS_ICI_RECLAIM_TAG); |
| 241 | spin_unlock(&mp->m_perag_lock); |
| 242 | trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /* |
| 247 | * We set the inode flag atomically with the radix tree tag. |
| 248 | * Once we get tag lookups on the radix tree, this inode flag |
| 249 | * can go away. |
| 250 | */ |
| 251 | void |
| 252 | xfs_inode_set_reclaim_tag( |
| 253 | struct xfs_inode *ip) |
| 254 | { |
| 255 | struct xfs_mount *mp = ip->i_mount; |
| 256 | struct xfs_perag *pag; |
| 257 | |
| 258 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
| 259 | spin_lock(&pag->pag_ici_lock); |
| 260 | spin_lock(&ip->i_flags_lock); |
| 261 | |
| 262 | radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino), |
| 263 | XFS_ICI_RECLAIM_TAG); |
| 264 | xfs_perag_set_reclaim_tag(pag); |
| 265 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); |
| 266 | |
| 267 | spin_unlock(&ip->i_flags_lock); |
| 268 | spin_unlock(&pag->pag_ici_lock); |
| 269 | xfs_perag_put(pag); |
| 270 | } |
| 271 | |
| 272 | STATIC void |
| 273 | xfs_inode_clear_reclaim_tag( |
| 274 | struct xfs_perag *pag, |
| 275 | xfs_ino_t ino) |
| 276 | { |
| 277 | radix_tree_tag_clear(&pag->pag_ici_root, |
| 278 | XFS_INO_TO_AGINO(pag->pag_mount, ino), |
| 279 | XFS_ICI_RECLAIM_TAG); |
| 280 | xfs_perag_clear_reclaim_tag(pag); |
| 281 | } |
| 282 | |
Darrick J. Wong | 7fdff52 | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 283 | static inline void |
Brian Foster | ae2c4ac | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 284 | xfs_inew_wait( |
| 285 | struct xfs_inode *ip) |
| 286 | { |
| 287 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT); |
| 288 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT); |
| 289 | |
| 290 | do { |
Ingo Molnar | 2141713 | 2017-03-05 11:25:39 +0100 | [diff] [blame] | 291 | prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); |
Brian Foster | ae2c4ac | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 292 | if (!xfs_iflags_test(ip, XFS_INEW)) |
| 293 | break; |
| 294 | schedule(); |
| 295 | } while (true); |
Ingo Molnar | 2141713 | 2017-03-05 11:25:39 +0100 | [diff] [blame] | 296 | finish_wait(wq, &wait.wq_entry); |
Brian Foster | ae2c4ac | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 299 | /* |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 300 | * When we recycle a reclaimable inode, we need to re-initialise the VFS inode |
| 301 | * part of the structure. This is made more complex by the fact we store |
| 302 | * information about the on-disk values in the VFS inode and so we can't just |
Dave Chinner | 83e06f2 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 303 | * overwrite the values unconditionally. Hence we save the parameters we |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 304 | * need to retain across reinitialisation, and rewrite them into the VFS inode |
Dave Chinner | 83e06f2 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 305 | * after reinitialisation even if it fails. |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 306 | */ |
| 307 | static int |
| 308 | xfs_reinit_inode( |
| 309 | struct xfs_mount *mp, |
| 310 | struct inode *inode) |
| 311 | { |
| 312 | int error; |
Dave Chinner | 54d7b5c | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 313 | uint32_t nlink = inode->i_nlink; |
Dave Chinner | 9e9a267 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 314 | uint32_t generation = inode->i_generation; |
Jeff Layton | f0e2828 | 2017-12-11 06:35:19 -0500 | [diff] [blame] | 315 | uint64_t version = inode_peek_iversion(inode); |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 316 | umode_t mode = inode->i_mode; |
Amir Goldstein | acd1d71 | 2018-01-26 11:24:40 -0800 | [diff] [blame] | 317 | dev_t dev = inode->i_rdev; |
Christoph Hellwig | 3d8f282 | 2020-02-21 08:31:26 -0800 | [diff] [blame] | 318 | kuid_t uid = inode->i_uid; |
| 319 | kgid_t gid = inode->i_gid; |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 320 | |
| 321 | error = inode_init_always(mp->m_super, inode); |
| 322 | |
Dave Chinner | 54d7b5c | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 323 | set_nlink(inode, nlink); |
Dave Chinner | 9e9a267 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 324 | inode->i_generation = generation; |
Jeff Layton | f0e2828 | 2017-12-11 06:35:19 -0500 | [diff] [blame] | 325 | inode_set_iversion_queried(inode, version); |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 326 | inode->i_mode = mode; |
Amir Goldstein | acd1d71 | 2018-01-26 11:24:40 -0800 | [diff] [blame] | 327 | inode->i_rdev = dev; |
Christoph Hellwig | 3d8f282 | 2020-02-21 08:31:26 -0800 | [diff] [blame] | 328 | inode->i_uid = uid; |
| 329 | inode->i_gid = gid; |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 330 | return error; |
| 331 | } |
| 332 | |
| 333 | /* |
Dave Chinner | afca6c5 | 2018-04-17 17:17:34 -0700 | [diff] [blame] | 334 | * If we are allocating a new inode, then check what was returned is |
| 335 | * actually a free, empty inode. If we are not allocating an inode, |
| 336 | * then check we didn't find a free inode. |
| 337 | * |
| 338 | * Returns: |
| 339 | * 0 if the inode free state matches the lookup context |
| 340 | * -ENOENT if the inode is free and we are not allocating |
| 341 | * -EFSCORRUPTED if there is any state mismatch at all |
| 342 | */ |
| 343 | static int |
| 344 | xfs_iget_check_free_state( |
| 345 | struct xfs_inode *ip, |
| 346 | int flags) |
| 347 | { |
| 348 | if (flags & XFS_IGET_CREATE) { |
| 349 | /* should be a free inode */ |
| 350 | if (VFS_I(ip)->i_mode != 0) { |
| 351 | xfs_warn(ip->i_mount, |
| 352 | "Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)", |
| 353 | ip->i_ino, VFS_I(ip)->i_mode); |
| 354 | return -EFSCORRUPTED; |
| 355 | } |
| 356 | |
Christoph Hellwig | 6e73a54 | 2021-03-29 11:11:40 -0700 | [diff] [blame] | 357 | if (ip->i_nblocks != 0) { |
Dave Chinner | afca6c5 | 2018-04-17 17:17:34 -0700 | [diff] [blame] | 358 | xfs_warn(ip->i_mount, |
| 359 | "Corruption detected! Free inode 0x%llx has blocks allocated!", |
| 360 | ip->i_ino); |
| 361 | return -EFSCORRUPTED; |
| 362 | } |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* should be an allocated inode */ |
| 367 | if (VFS_I(ip)->i_mode == 0) |
| 368 | return -ENOENT; |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | /* |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 374 | * Check the validity of the inode we just found it the cache |
| 375 | */ |
| 376 | static int |
| 377 | xfs_iget_cache_hit( |
| 378 | struct xfs_perag *pag, |
| 379 | struct xfs_inode *ip, |
| 380 | xfs_ino_t ino, |
| 381 | int flags, |
| 382 | int lock_flags) __releases(RCU) |
| 383 | { |
| 384 | struct inode *inode = VFS_I(ip); |
| 385 | struct xfs_mount *mp = ip->i_mount; |
| 386 | int error; |
| 387 | |
| 388 | /* |
| 389 | * check for re-use of an inode within an RCU grace period due to the |
| 390 | * radix tree nodes not being updated yet. We monitor for this by |
| 391 | * setting the inode number to zero before freeing the inode structure. |
| 392 | * If the inode has been reallocated and set up, then the inode number |
| 393 | * will not match, so check for that, too. |
| 394 | */ |
| 395 | spin_lock(&ip->i_flags_lock); |
| 396 | if (ip->i_ino != ino) { |
| 397 | trace_xfs_iget_skip(ip); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 398 | XFS_STATS_INC(mp, xs_ig_frecycle); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 399 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 400 | goto out_error; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /* |
| 405 | * If we are racing with another cache hit that is currently |
| 406 | * instantiating this inode or currently recycling it out of |
| 407 | * reclaimabe state, wait for the initialisation to complete |
| 408 | * before continuing. |
| 409 | * |
| 410 | * XXX(hch): eventually we should do something equivalent to |
| 411 | * wait_on_inode to wait for these flags to be cleared |
| 412 | * instead of polling for it. |
| 413 | */ |
| 414 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { |
| 415 | trace_xfs_iget_skip(ip); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 416 | XFS_STATS_INC(mp, xs_ig_frecycle); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 417 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 418 | goto out_error; |
| 419 | } |
| 420 | |
| 421 | /* |
Dave Chinner | afca6c5 | 2018-04-17 17:17:34 -0700 | [diff] [blame] | 422 | * Check the inode free state is valid. This also detects lookup |
| 423 | * racing with unlinks. |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 424 | */ |
Dave Chinner | afca6c5 | 2018-04-17 17:17:34 -0700 | [diff] [blame] | 425 | error = xfs_iget_check_free_state(ip, flags); |
| 426 | if (error) |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 427 | goto out_error; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. |
| 431 | * Need to carefully get it back into useable state. |
| 432 | */ |
| 433 | if (ip->i_flags & XFS_IRECLAIMABLE) { |
| 434 | trace_xfs_iget_reclaim(ip); |
| 435 | |
Darrick J. Wong | 378f681 | 2017-06-19 08:58:56 -0700 | [diff] [blame] | 436 | if (flags & XFS_IGET_INCORE) { |
| 437 | error = -EAGAIN; |
| 438 | goto out_error; |
| 439 | } |
| 440 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 441 | /* |
| 442 | * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode |
| 443 | * from stomping over us while we recycle the inode. We can't |
| 444 | * clear the radix tree reclaimable tag yet as it requires |
| 445 | * pag_ici_lock to be held exclusive. |
| 446 | */ |
| 447 | ip->i_flags |= XFS_IRECLAIM; |
| 448 | |
| 449 | spin_unlock(&ip->i_flags_lock); |
| 450 | rcu_read_unlock(); |
| 451 | |
Ira Weiny | d45344d | 2020-04-22 21:50:57 -0700 | [diff] [blame] | 452 | ASSERT(!rwsem_is_locked(&inode->i_rwsem)); |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 453 | error = xfs_reinit_inode(mp, inode); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 454 | if (error) { |
Brian Foster | 756baca | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 455 | bool wake; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 456 | /* |
| 457 | * Re-initializing the inode failed, and we are in deep |
| 458 | * trouble. Try to re-add it to the reclaim list. |
| 459 | */ |
| 460 | rcu_read_lock(); |
| 461 | spin_lock(&ip->i_flags_lock); |
Brian Foster | 756baca | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 462 | wake = !!__xfs_iflags_test(ip, XFS_INEW); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 463 | ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM); |
Brian Foster | 756baca | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 464 | if (wake) |
| 465 | wake_up_bit(&ip->i_flags, __XFS_INEW_BIT); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 466 | ASSERT(ip->i_flags & XFS_IRECLAIMABLE); |
| 467 | trace_xfs_iget_reclaim_fail(ip); |
| 468 | goto out_error; |
| 469 | } |
| 470 | |
| 471 | spin_lock(&pag->pag_ici_lock); |
| 472 | spin_lock(&ip->i_flags_lock); |
| 473 | |
| 474 | /* |
| 475 | * Clear the per-lifetime state in the inode as we are now |
| 476 | * effectively a new inode and need to return to the initial |
| 477 | * state before reuse occurs. |
| 478 | */ |
| 479 | ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS; |
| 480 | ip->i_flags |= XFS_INEW; |
Dave Chinner | 545c088 | 2016-05-18 14:11:41 +1000 | [diff] [blame] | 481 | xfs_inode_clear_reclaim_tag(pag, ip->i_ino); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 482 | inode->i_state = I_NEW; |
Darrick J. Wong | 6772c1f | 2019-04-12 07:40:25 -0700 | [diff] [blame] | 483 | ip->i_sick = 0; |
| 484 | ip->i_checked = 0; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 485 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 486 | spin_unlock(&ip->i_flags_lock); |
| 487 | spin_unlock(&pag->pag_ici_lock); |
| 488 | } else { |
| 489 | /* If the VFS inode is being torn down, pause and try again. */ |
| 490 | if (!igrab(inode)) { |
| 491 | trace_xfs_iget_skip(ip); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 492 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 493 | goto out_error; |
| 494 | } |
| 495 | |
| 496 | /* We've got a live one. */ |
| 497 | spin_unlock(&ip->i_flags_lock); |
| 498 | rcu_read_unlock(); |
| 499 | trace_xfs_iget_hit(ip); |
| 500 | } |
| 501 | |
| 502 | if (lock_flags != 0) |
| 503 | xfs_ilock(ip, lock_flags); |
| 504 | |
Darrick J. Wong | 378f681 | 2017-06-19 08:58:56 -0700 | [diff] [blame] | 505 | if (!(flags & XFS_IGET_INCORE)) |
Ira Weiny | dae2f8e | 2020-04-30 07:41:37 -0700 | [diff] [blame] | 506 | xfs_iflags_clear(ip, XFS_ISTALE); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 507 | XFS_STATS_INC(mp, xs_ig_found); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 508 | |
| 509 | return 0; |
| 510 | |
| 511 | out_error: |
| 512 | spin_unlock(&ip->i_flags_lock); |
| 513 | rcu_read_unlock(); |
| 514 | return error; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | static int |
| 519 | xfs_iget_cache_miss( |
| 520 | struct xfs_mount *mp, |
| 521 | struct xfs_perag *pag, |
| 522 | xfs_trans_t *tp, |
| 523 | xfs_ino_t ino, |
| 524 | struct xfs_inode **ipp, |
| 525 | int flags, |
| 526 | int lock_flags) |
| 527 | { |
| 528 | struct xfs_inode *ip; |
| 529 | int error; |
| 530 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
| 531 | int iflags; |
| 532 | |
| 533 | ip = xfs_inode_alloc(mp, ino); |
| 534 | if (!ip) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 535 | return -ENOMEM; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 536 | |
Christoph Hellwig | bb8a66a | 2020-05-14 14:01:19 -0700 | [diff] [blame] | 537 | error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, flags); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 538 | if (error) |
| 539 | goto out_destroy; |
| 540 | |
Christoph Hellwig | bb8a66a | 2020-05-14 14:01:19 -0700 | [diff] [blame] | 541 | /* |
| 542 | * For version 5 superblocks, if we are initialising a new inode and we |
| 543 | * are not utilising the XFS_MOUNT_IKEEP inode cluster mode, we can |
| 544 | * simply build the new inode core with a random generation number. |
| 545 | * |
| 546 | * For version 4 (and older) superblocks, log recovery is dependent on |
Christoph Hellwig | 965e0a1 | 2021-03-29 11:11:42 -0700 | [diff] [blame] | 547 | * the i_flushiter field being initialised from the current on-disk |
Christoph Hellwig | bb8a66a | 2020-05-14 14:01:19 -0700 | [diff] [blame] | 548 | * value and hence we must also read the inode off disk even when |
| 549 | * initializing new inodes. |
| 550 | */ |
| 551 | if (xfs_sb_version_has_v3inode(&mp->m_sb) && |
| 552 | (flags & XFS_IGET_CREATE) && !(mp->m_flags & XFS_MOUNT_IKEEP)) { |
| 553 | VFS_I(ip)->i_generation = prandom_u32(); |
| 554 | } else { |
Christoph Hellwig | bb8a66a | 2020-05-14 14:01:19 -0700 | [diff] [blame] | 555 | struct xfs_buf *bp; |
| 556 | |
Christoph Hellwig | af9dcdd | 2021-03-29 11:11:37 -0700 | [diff] [blame] | 557 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp); |
Christoph Hellwig | bb8a66a | 2020-05-14 14:01:19 -0700 | [diff] [blame] | 558 | if (error) |
| 559 | goto out_destroy; |
| 560 | |
Christoph Hellwig | af9dcdd | 2021-03-29 11:11:37 -0700 | [diff] [blame] | 561 | error = xfs_inode_from_disk(ip, |
| 562 | xfs_buf_offset(bp, ip->i_imap.im_boffset)); |
Christoph Hellwig | bb8a66a | 2020-05-14 14:01:19 -0700 | [diff] [blame] | 563 | if (!error) |
| 564 | xfs_buf_set_ref(bp, XFS_INO_REF); |
| 565 | xfs_trans_brelse(tp, bp); |
| 566 | |
| 567 | if (error) |
| 568 | goto out_destroy; |
| 569 | } |
| 570 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 571 | trace_xfs_iget_miss(ip); |
| 572 | |
Dave Chinner | ee45700 | 2018-03-23 10:22:53 -0700 | [diff] [blame] | 573 | /* |
Dave Chinner | afca6c5 | 2018-04-17 17:17:34 -0700 | [diff] [blame] | 574 | * Check the inode free state is valid. This also detects lookup |
| 575 | * racing with unlinks. |
Dave Chinner | ee45700 | 2018-03-23 10:22:53 -0700 | [diff] [blame] | 576 | */ |
Dave Chinner | afca6c5 | 2018-04-17 17:17:34 -0700 | [diff] [blame] | 577 | error = xfs_iget_check_free_state(ip, flags); |
| 578 | if (error) |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 579 | goto out_destroy; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * Preload the radix tree so we can insert safely under the |
| 583 | * write spinlock. Note that we cannot sleep inside the preload |
| 584 | * region. Since we can be called from transaction context, don't |
| 585 | * recurse into the file system. |
| 586 | */ |
| 587 | if (radix_tree_preload(GFP_NOFS)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 588 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 589 | goto out_destroy; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Because the inode hasn't been added to the radix-tree yet it can't |
| 594 | * be found by another thread, so we can do the non-sleeping lock here. |
| 595 | */ |
| 596 | if (lock_flags) { |
| 597 | if (!xfs_ilock_nowait(ip, lock_flags)) |
| 598 | BUG(); |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * These values must be set before inserting the inode into the radix |
| 603 | * tree as the moment it is inserted a concurrent lookup (allowed by the |
| 604 | * RCU locking mechanism) can find it and that lookup must see that this |
| 605 | * is an inode currently under construction (i.e. that XFS_INEW is set). |
| 606 | * The ip->i_flags_lock that protects the XFS_INEW flag forms the |
| 607 | * memory barrier that ensures this detection works correctly at lookup |
| 608 | * time. |
| 609 | */ |
| 610 | iflags = XFS_INEW; |
| 611 | if (flags & XFS_IGET_DONTCACHE) |
Ira Weiny | 2c567af | 2020-04-30 07:41:37 -0700 | [diff] [blame] | 612 | d_mark_dontcache(VFS_I(ip)); |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 613 | ip->i_udquot = NULL; |
| 614 | ip->i_gdquot = NULL; |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 615 | ip->i_pdquot = NULL; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 616 | xfs_iflags_set(ip, iflags); |
| 617 | |
| 618 | /* insert the new inode */ |
| 619 | spin_lock(&pag->pag_ici_lock); |
| 620 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
| 621 | if (unlikely(error)) { |
| 622 | WARN_ON(error != -EEXIST); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 623 | XFS_STATS_INC(mp, xs_ig_dup); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 624 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 625 | goto out_preload_end; |
| 626 | } |
| 627 | spin_unlock(&pag->pag_ici_lock); |
| 628 | radix_tree_preload_end(); |
| 629 | |
| 630 | *ipp = ip; |
| 631 | return 0; |
| 632 | |
| 633 | out_preload_end: |
| 634 | spin_unlock(&pag->pag_ici_lock); |
| 635 | radix_tree_preload_end(); |
| 636 | if (lock_flags) |
| 637 | xfs_iunlock(ip, lock_flags); |
| 638 | out_destroy: |
| 639 | __destroy_inode(VFS_I(ip)); |
| 640 | xfs_inode_free(ip); |
| 641 | return error; |
| 642 | } |
| 643 | |
| 644 | /* |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 645 | * Look up an inode by number in the given file system. The inode is looked up |
| 646 | * in the cache held in each AG. If the inode is found in the cache, initialise |
| 647 | * the vfs inode if necessary. |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 648 | * |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 649 | * If it is not in core, read it in from the file system's device, add it to the |
| 650 | * cache and initialise the vfs inode. |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 651 | * |
| 652 | * The inode is locked according to the value of the lock_flags parameter. |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 653 | * Inode lookup is only done during metadata operations and not as part of the |
| 654 | * data IO path. Hence we only allow locking of the XFS_ILOCK during lookup. |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 655 | */ |
| 656 | int |
| 657 | xfs_iget( |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 658 | struct xfs_mount *mp, |
| 659 | struct xfs_trans *tp, |
| 660 | xfs_ino_t ino, |
| 661 | uint flags, |
| 662 | uint lock_flags, |
| 663 | struct xfs_inode **ipp) |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 664 | { |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 665 | struct xfs_inode *ip; |
| 666 | struct xfs_perag *pag; |
| 667 | xfs_agino_t agino; |
| 668 | int error; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 669 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 670 | ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0); |
| 671 | |
| 672 | /* reject inode numbers outside existing AGs */ |
| 673 | if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 674 | return -EINVAL; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 675 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 676 | XFS_STATS_INC(mp, xs_ig_attempts); |
Lucas Stach | 8774cf8 | 2015-08-28 14:50:56 +1000 | [diff] [blame] | 677 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 678 | /* get the perag structure and ensure that it's inode capable */ |
| 679 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); |
| 680 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 681 | |
| 682 | again: |
| 683 | error = 0; |
| 684 | rcu_read_lock(); |
| 685 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); |
| 686 | |
| 687 | if (ip) { |
| 688 | error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags); |
| 689 | if (error) |
| 690 | goto out_error_or_again; |
| 691 | } else { |
| 692 | rcu_read_unlock(); |
Darrick J. Wong | 378f681 | 2017-06-19 08:58:56 -0700 | [diff] [blame] | 693 | if (flags & XFS_IGET_INCORE) { |
Darrick J. Wong | ed438b4 | 2017-10-17 21:37:32 -0700 | [diff] [blame] | 694 | error = -ENODATA; |
Darrick J. Wong | 378f681 | 2017-06-19 08:58:56 -0700 | [diff] [blame] | 695 | goto out_error_or_again; |
| 696 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 697 | XFS_STATS_INC(mp, xs_ig_missed); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 698 | |
| 699 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, |
| 700 | flags, lock_flags); |
| 701 | if (error) |
| 702 | goto out_error_or_again; |
| 703 | } |
| 704 | xfs_perag_put(pag); |
| 705 | |
| 706 | *ipp = ip; |
| 707 | |
| 708 | /* |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 709 | * If we have a real type for an on-disk inode, we can setup the inode |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 710 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
| 711 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 712 | if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0) |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 713 | xfs_setup_existing_inode(ip); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 714 | return 0; |
| 715 | |
| 716 | out_error_or_again: |
Darrick J. Wong | 378f681 | 2017-06-19 08:58:56 -0700 | [diff] [blame] | 717 | if (!(flags & XFS_IGET_INCORE) && error == -EAGAIN) { |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 718 | delay(1); |
| 719 | goto again; |
| 720 | } |
| 721 | xfs_perag_put(pag); |
| 722 | return error; |
| 723 | } |
| 724 | |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 725 | /* |
Darrick J. Wong | 378f681 | 2017-06-19 08:58:56 -0700 | [diff] [blame] | 726 | * "Is this a cached inode that's also allocated?" |
| 727 | * |
| 728 | * Look up an inode by number in the given file system. If the inode is |
| 729 | * in cache and isn't in purgatory, return 1 if the inode is allocated |
| 730 | * and 0 if it is not. For all other cases (not in cache, being torn |
| 731 | * down, etc.), return a negative error code. |
| 732 | * |
| 733 | * The caller has to prevent inode allocation and freeing activity, |
| 734 | * presumably by locking the AGI buffer. This is to ensure that an |
| 735 | * inode cannot transition from allocated to freed until the caller is |
| 736 | * ready to allow that. If the inode is in an intermediate state (new, |
| 737 | * reclaimable, or being reclaimed), -EAGAIN will be returned; if the |
| 738 | * inode is not in the cache, -ENOENT will be returned. The caller must |
| 739 | * deal with these scenarios appropriately. |
| 740 | * |
| 741 | * This is a specialized use case for the online scrubber; if you're |
| 742 | * reading this, you probably want xfs_iget. |
| 743 | */ |
| 744 | int |
| 745 | xfs_icache_inode_is_allocated( |
| 746 | struct xfs_mount *mp, |
| 747 | struct xfs_trans *tp, |
| 748 | xfs_ino_t ino, |
| 749 | bool *inuse) |
| 750 | { |
| 751 | struct xfs_inode *ip; |
| 752 | int error; |
| 753 | |
| 754 | error = xfs_iget(mp, tp, ino, XFS_IGET_INCORE, 0, &ip); |
| 755 | if (error) |
| 756 | return error; |
| 757 | |
| 758 | *inuse = !!(VFS_I(ip)->i_mode); |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 759 | xfs_irele(ip); |
Darrick J. Wong | 378f681 | 2017-06-19 08:58:56 -0700 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /* |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 764 | * The inode lookup is done in batches to keep the amount of lock traffic and |
| 765 | * radix tree lookups to a minimum. The batch size is a trade off between |
| 766 | * lookup reduction and stack usage. This is in the reclaim path, so we can't |
| 767 | * be too greedy. |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 768 | * |
Darrick J. Wong | c1115c0 | 2021-06-01 22:41:25 -0700 | [diff] [blame] | 769 | * XXX: This will be moved closer to xfs_icwalk* once we get rid of the |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 770 | * separate reclaim walk functions. |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 771 | */ |
| 772 | #define XFS_LOOKUP_BATCH 32 |
| 773 | |
Darrick J. Wong | 1ad2cfe | 2021-05-31 11:31:57 -0700 | [diff] [blame] | 774 | #ifdef CONFIG_XFS_QUOTA |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 775 | /* Decide if we want to grab this inode to drop its dquots. */ |
| 776 | static bool |
| 777 | xfs_dqrele_igrab( |
| 778 | struct xfs_inode *ip) |
| 779 | { |
| 780 | bool ret = false; |
| 781 | |
| 782 | ASSERT(rcu_read_lock_held()); |
| 783 | |
| 784 | /* Check for stale RCU freed inode */ |
| 785 | spin_lock(&ip->i_flags_lock); |
| 786 | if (!ip->i_ino) |
| 787 | goto out_unlock; |
| 788 | |
| 789 | /* |
| 790 | * Skip inodes that are anywhere in the reclaim machinery because we |
| 791 | * drop dquots before tagging an inode for reclamation. |
| 792 | */ |
| 793 | if (ip->i_flags & (XFS_IRECLAIM | XFS_IRECLAIMABLE)) |
| 794 | goto out_unlock; |
| 795 | |
| 796 | /* |
| 797 | * The inode looks alive; try to grab a VFS reference so that it won't |
| 798 | * get destroyed. If we got the reference, return true to say that |
| 799 | * we grabbed the inode. |
| 800 | * |
| 801 | * If we can't get the reference, then we know the inode had its VFS |
| 802 | * state torn down and hasn't yet entered the reclaim machinery. Since |
| 803 | * we also know that dquots are detached from an inode before it enters |
| 804 | * reclaim, we can skip the inode. |
| 805 | */ |
| 806 | ret = igrab(VFS_I(ip)) != NULL; |
| 807 | |
| 808 | out_unlock: |
| 809 | spin_unlock(&ip->i_flags_lock); |
| 810 | return ret; |
| 811 | } |
| 812 | |
Darrick J. Wong | 1ad2cfe | 2021-05-31 11:31:57 -0700 | [diff] [blame] | 813 | /* Drop this inode's dquots. */ |
| 814 | static int |
| 815 | xfs_dqrele_inode( |
| 816 | struct xfs_inode *ip, |
| 817 | void *priv) |
| 818 | { |
| 819 | struct xfs_eofblocks *eofb = priv; |
| 820 | |
Darrick J. Wong | 9d2793c | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 821 | if (xfs_iflags_test(ip, XFS_INEW)) |
| 822 | xfs_inew_wait(ip); |
| 823 | |
Darrick J. Wong | 1ad2cfe | 2021-05-31 11:31:57 -0700 | [diff] [blame] | 824 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 825 | if (eofb->eof_flags & XFS_ICWALK_FLAG_DROP_UDQUOT) { |
| 826 | xfs_qm_dqrele(ip->i_udquot); |
| 827 | ip->i_udquot = NULL; |
| 828 | } |
| 829 | if (eofb->eof_flags & XFS_ICWALK_FLAG_DROP_GDQUOT) { |
| 830 | xfs_qm_dqrele(ip->i_gdquot); |
| 831 | ip->i_gdquot = NULL; |
| 832 | } |
| 833 | if (eofb->eof_flags & XFS_ICWALK_FLAG_DROP_PDQUOT) { |
| 834 | xfs_qm_dqrele(ip->i_pdquot); |
| 835 | ip->i_pdquot = NULL; |
| 836 | } |
| 837 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Detach all dquots from incore inodes if we can. The caller must already |
| 843 | * have dropped the relevant XFS_[UGP]QUOTA_ACTIVE flags so that dquots will |
| 844 | * not get reattached. |
| 845 | */ |
| 846 | int |
| 847 | xfs_dqrele_all_inodes( |
| 848 | struct xfs_mount *mp, |
| 849 | unsigned int qflags) |
| 850 | { |
| 851 | struct xfs_eofblocks eofb = { .eof_flags = 0 }; |
| 852 | |
| 853 | if (qflags & XFS_UQUOTA_ACCT) |
| 854 | eofb.eof_flags |= XFS_ICWALK_FLAG_DROP_UDQUOT; |
| 855 | if (qflags & XFS_GQUOTA_ACCT) |
| 856 | eofb.eof_flags |= XFS_ICWALK_FLAG_DROP_GDQUOT; |
| 857 | if (qflags & XFS_PQUOTA_ACCT) |
| 858 | eofb.eof_flags |= XFS_ICWALK_FLAG_DROP_PDQUOT; |
| 859 | |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 860 | return xfs_icwalk(mp, XFS_ICWALK_DQRELE, &eofb); |
Darrick J. Wong | 1ad2cfe | 2021-05-31 11:31:57 -0700 | [diff] [blame] | 861 | } |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 862 | #else |
| 863 | # define xfs_dqrele_igrab(ip) (false) |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 864 | # define xfs_dqrele_inode(ip, priv) (0) |
Darrick J. Wong | 1ad2cfe | 2021-05-31 11:31:57 -0700 | [diff] [blame] | 865 | #endif /* CONFIG_XFS_QUOTA */ |
| 866 | |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 867 | /* |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 868 | * Grab the inode for reclaim exclusively. |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 869 | * |
| 870 | * We have found this inode via a lookup under RCU, so the inode may have |
| 871 | * already been freed, or it may be in the process of being recycled by |
| 872 | * xfs_iget(). In both cases, the inode will have XFS_IRECLAIM set. If the inode |
| 873 | * has been fully recycled by the time we get the i_flags_lock, XFS_IRECLAIMABLE |
| 874 | * will not be set. Hence we need to check for both these flag conditions to |
| 875 | * avoid inodes that are no longer reclaim candidates. |
| 876 | * |
| 877 | * Note: checking for other state flags here, under the i_flags_lock or not, is |
| 878 | * racy and should be avoided. Those races should be resolved only after we have |
| 879 | * ensured that we are able to reclaim this inode and the world can see that we |
| 880 | * are going to reclaim it. |
| 881 | * |
| 882 | * Return true if we grabbed it, false otherwise. |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 883 | */ |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 884 | static bool |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 885 | xfs_reclaim_inode_grab( |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 886 | struct xfs_inode *ip) |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 887 | { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 888 | ASSERT(rcu_read_lock_held()); |
| 889 | |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 890 | spin_lock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 891 | if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) || |
| 892 | __xfs_iflags_test(ip, XFS_IRECLAIM)) { |
| 893 | /* not a reclaim candidate. */ |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 894 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 895 | return false; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 896 | } |
| 897 | __xfs_iflags_set(ip, XFS_IRECLAIM); |
| 898 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 899 | return true; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 903 | * Inode reclaim is non-blocking, so the default action if progress cannot be |
| 904 | * made is to "requeue" the inode for reclaim by unlocking it and clearing the |
| 905 | * XFS_IRECLAIM flag. If we are in a shutdown state, we don't care about |
| 906 | * blocking anymore and hence we can wait for the inode to be able to reclaim |
| 907 | * it. |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 908 | * |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 909 | * We do no IO here - if callers require inodes to be cleaned they must push the |
| 910 | * AIL first to trigger writeback of dirty inodes. This enables writeback to be |
| 911 | * done in the background in a non-blocking manner, and enables memory reclaim |
| 912 | * to make progress without blocking. |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 913 | */ |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 914 | static void |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 915 | xfs_reclaim_inode( |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 916 | struct xfs_inode *ip, |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 917 | struct xfs_perag *pag) |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 918 | { |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 919 | xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */ |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 920 | |
Dave Chinner | 9552e14 | 2020-06-29 14:49:17 -0700 | [diff] [blame] | 921 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) |
Dave Chinner | 617825f | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 922 | goto out; |
Dave Chinner | 718ecc5 | 2020-08-17 16:41:01 -0700 | [diff] [blame] | 923 | if (xfs_iflags_test_and_set(ip, XFS_IFLUSHING)) |
Dave Chinner | 9552e14 | 2020-06-29 14:49:17 -0700 | [diff] [blame] | 924 | goto out_iunlock; |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 925 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 926 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 927 | xfs_iunpin_wait(ip); |
Brian Foster | 88fc187 | 2020-05-06 13:27:40 -0700 | [diff] [blame] | 928 | xfs_iflush_abort(ip); |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 929 | goto reclaim; |
| 930 | } |
Dave Chinner | 617825f | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 931 | if (xfs_ipincount(ip)) |
Dave Chinner | 718ecc5 | 2020-08-17 16:41:01 -0700 | [diff] [blame] | 932 | goto out_clear_flush; |
Dave Chinner | 617825f | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 933 | if (!xfs_inode_clean(ip)) |
Dave Chinner | 718ecc5 | 2020-08-17 16:41:01 -0700 | [diff] [blame] | 934 | goto out_clear_flush; |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 935 | |
Dave Chinner | 718ecc5 | 2020-08-17 16:41:01 -0700 | [diff] [blame] | 936 | xfs_iflags_clear(ip, XFS_IFLUSHING); |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 937 | reclaim: |
Brian Foster | 98efe8a | 2016-11-10 08:23:22 +1100 | [diff] [blame] | 938 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 939 | /* |
| 940 | * Because we use RCU freeing we need to ensure the inode always appears |
| 941 | * to be reclaimed with an invalid inode number when in the free state. |
Brian Foster | 98efe8a | 2016-11-10 08:23:22 +1100 | [diff] [blame] | 942 | * We do this as early as possible under the ILOCK so that |
Omar Sandoval | f2e9ad2 | 2017-08-25 10:05:26 -0700 | [diff] [blame] | 943 | * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to |
| 944 | * detect races with us here. By doing this, we guarantee that once |
| 945 | * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that |
| 946 | * it will see either a valid inode that will serialise correctly, or it |
| 947 | * will see an invalid inode that it can skip. |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 948 | */ |
| 949 | spin_lock(&ip->i_flags_lock); |
| 950 | ip->i_flags = XFS_IRECLAIM; |
| 951 | ip->i_ino = 0; |
| 952 | spin_unlock(&ip->i_flags_lock); |
| 953 | |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 954 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 955 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 956 | XFS_STATS_INC(ip->i_mount, xs_ig_reclaims); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 957 | /* |
| 958 | * Remove the inode from the per-AG radix tree. |
| 959 | * |
| 960 | * Because radix_tree_delete won't complain even if the item was never |
| 961 | * added to the tree assert that it's been there before to catch |
| 962 | * problems with the inode life time early on. |
| 963 | */ |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 964 | spin_lock(&pag->pag_ici_lock); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 965 | if (!radix_tree_delete(&pag->pag_ici_root, |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 966 | XFS_INO_TO_AGINO(ip->i_mount, ino))) |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 967 | ASSERT(0); |
Dave Chinner | 545c088 | 2016-05-18 14:11:41 +1000 | [diff] [blame] | 968 | xfs_perag_clear_reclaim_tag(pag); |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 969 | spin_unlock(&pag->pag_ici_lock); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 970 | |
| 971 | /* |
| 972 | * Here we do an (almost) spurious inode lock in order to coordinate |
| 973 | * with inode cache radix tree lookups. This is because the lookup |
| 974 | * can reference the inodes in the cache without taking references. |
| 975 | * |
| 976 | * We make that OK here by ensuring that we wait until the inode is |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 977 | * unlocked after the lookup before we go ahead and free it. |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 978 | */ |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 979 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Darrick J. Wong | 3ea06d7 | 2021-05-31 11:31:57 -0700 | [diff] [blame] | 980 | ASSERT(!ip->i_udquot && !ip->i_gdquot && !ip->i_pdquot); |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 981 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 96355d5a | 2020-06-29 14:48:45 -0700 | [diff] [blame] | 982 | ASSERT(xfs_inode_clean(ip)); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 983 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 984 | __xfs_inode_free(ip); |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 985 | return; |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 986 | |
Dave Chinner | 718ecc5 | 2020-08-17 16:41:01 -0700 | [diff] [blame] | 987 | out_clear_flush: |
| 988 | xfs_iflags_clear(ip, XFS_IFLUSHING); |
Dave Chinner | 9552e14 | 2020-06-29 14:49:17 -0700 | [diff] [blame] | 989 | out_iunlock: |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 990 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9552e14 | 2020-06-29 14:49:17 -0700 | [diff] [blame] | 991 | out: |
Dave Chinner | 617825f | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 992 | xfs_iflags_clear(ip, XFS_IRECLAIM); |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 993 | } |
| 994 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 995 | /* |
| 996 | * Walk the AGs and reclaim the inodes in them. Even if the filesystem is |
| 997 | * corrupted, we still want to try to reclaim all the inodes. If we don't, |
| 998 | * then a shut down during filesystem unmount reclaim walk leak all the |
| 999 | * unreclaimed inodes. |
Dave Chinner | 617825f | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 1000 | * |
| 1001 | * Returns non-zero if any AGs or inodes were skipped in the reclaim pass |
| 1002 | * so that callers that want to block until all dirty inodes are written back |
| 1003 | * and reclaimed can sanely loop. |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1004 | */ |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1005 | static void |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1006 | xfs_reclaim_inodes_ag( |
| 1007 | struct xfs_mount *mp, |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1008 | int *nr_to_scan) |
| 1009 | { |
| 1010 | struct xfs_perag *pag; |
Dave Chinner | 0e8e2c6 | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 1011 | xfs_agnumber_t ag = 0; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1012 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1013 | while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { |
| 1014 | unsigned long first_index = 0; |
| 1015 | int done = 0; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1016 | int nr_found = 0; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1017 | |
| 1018 | ag = pag->pag_agno + 1; |
| 1019 | |
Dave Chinner | 0e8e2c6 | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 1020 | first_index = READ_ONCE(pag->pag_ici_reclaim_cursor); |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1021 | do { |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1022 | struct xfs_inode *batch[XFS_LOOKUP_BATCH]; |
| 1023 | int i; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1024 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1025 | rcu_read_lock(); |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1026 | nr_found = radix_tree_gang_lookup_tag( |
| 1027 | &pag->pag_ici_root, |
| 1028 | (void **)batch, first_index, |
| 1029 | XFS_LOOKUP_BATCH, |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1030 | XFS_ICI_RECLAIM_TAG); |
| 1031 | if (!nr_found) { |
Dave Chinner | b223221 | 2011-05-06 02:54:04 +0000 | [diff] [blame] | 1032 | done = 1; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1033 | rcu_read_unlock(); |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1038 | * Grab the inodes before we drop the lock. if we found |
| 1039 | * nothing, nr == 0 and the loop will be skipped. |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1040 | */ |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1041 | for (i = 0; i < nr_found; i++) { |
| 1042 | struct xfs_inode *ip = batch[i]; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1043 | |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 1044 | if (done || !xfs_reclaim_inode_grab(ip)) |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1045 | batch[i] = NULL; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1046 | |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1047 | /* |
| 1048 | * Update the index for the next lookup. Catch |
| 1049 | * overflows into the next AG range which can |
| 1050 | * occur if we have inodes in the last block of |
| 1051 | * the AG and we are currently pointing to the |
| 1052 | * last inode. |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1053 | * |
| 1054 | * Because we may see inodes that are from the |
| 1055 | * wrong AG due to RCU freeing and |
| 1056 | * reallocation, only update the index if it |
| 1057 | * lies in this AG. It was a race that lead us |
| 1058 | * to see this inode, so another lookup from |
| 1059 | * the same index will not find it again. |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1060 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1061 | if (XFS_INO_TO_AGNO(mp, ip->i_ino) != |
| 1062 | pag->pag_agno) |
| 1063 | continue; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1064 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); |
| 1065 | if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) |
| 1066 | done = 1; |
| 1067 | } |
| 1068 | |
| 1069 | /* unlock now we've grabbed the inodes. */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1070 | rcu_read_unlock(); |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1071 | |
| 1072 | for (i = 0; i < nr_found; i++) { |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1073 | if (batch[i]) |
| 1074 | xfs_reclaim_inode(batch[i], pag); |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | *nr_to_scan -= XFS_LOOKUP_BATCH; |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1078 | cond_resched(); |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1079 | } while (nr_found && !done && *nr_to_scan > 0); |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1080 | |
Dave Chinner | 0e8e2c6 | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 1081 | if (done) |
| 1082 | first_index = 0; |
| 1083 | WRITE_ONCE(pag->pag_ici_reclaim_cursor, first_index); |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1084 | xfs_perag_put(pag); |
| 1085 | } |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1086 | } |
| 1087 | |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1088 | void |
David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 1089 | xfs_reclaim_inodes( |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1090 | struct xfs_mount *mp) |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 1091 | { |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1092 | int nr_to_scan = INT_MAX; |
| 1093 | |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1094 | while (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) { |
Dave Chinner | 617825f | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 1095 | xfs_ail_push_all_sync(mp->m_ail); |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1096 | xfs_reclaim_inodes_ag(mp, &nr_to_scan); |
Zheng Bin | 0f4ec0f1 | 2020-09-09 09:29:16 -0700 | [diff] [blame] | 1097 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /* |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 1101 | * The shrinker infrastructure determines how many inodes we should scan for |
| 1102 | * reclaim. We want as many clean inodes ready to reclaim as possible, so we |
| 1103 | * push the AIL here. We also want to proactively free up memory if we can to |
| 1104 | * minimise the amount of work memory reclaim has to do so we kick the |
| 1105 | * background reclaim if it isn't already scheduled. |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1106 | */ |
Dave Chinner | 0a234c6 | 2013-08-28 10:17:57 +1000 | [diff] [blame] | 1107 | long |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1108 | xfs_reclaim_inodes_nr( |
| 1109 | struct xfs_mount *mp, |
| 1110 | int nr_to_scan) |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1111 | { |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1112 | /* kick background reclaimer and push the AIL */ |
Dave Chinner | 5889608 | 2012-10-08 21:56:05 +1100 | [diff] [blame] | 1113 | xfs_reclaim_work_queue(mp); |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1114 | xfs_ail_push_all(mp->m_ail); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1115 | |
Dave Chinner | 50718b8 | 2020-07-01 10:21:05 -0700 | [diff] [blame] | 1116 | xfs_reclaim_inodes_ag(mp, &nr_to_scan); |
Dave Chinner | 617825f | 2020-06-29 14:49:16 -0700 | [diff] [blame] | 1117 | return 0; |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1118 | } |
Dave Chinner | a7b339f | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 1119 | |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1120 | /* |
| 1121 | * Return the number of reclaimable inodes in the filesystem for |
| 1122 | * the shrinker to determine how much to reclaim. |
| 1123 | */ |
| 1124 | int |
| 1125 | xfs_reclaim_inodes_count( |
| 1126 | struct xfs_mount *mp) |
| 1127 | { |
| 1128 | struct xfs_perag *pag; |
| 1129 | xfs_agnumber_t ag = 0; |
| 1130 | int reclaimable = 0; |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1131 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1132 | while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { |
| 1133 | ag = pag->pag_agno + 1; |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 1134 | reclaimable += pag->pag_ici_reclaimable; |
| 1135 | xfs_perag_put(pag); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1136 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1137 | return reclaimable; |
| 1138 | } |
| 1139 | |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1140 | STATIC bool |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1141 | xfs_inode_match_id( |
| 1142 | struct xfs_inode *ip, |
| 1143 | struct xfs_eofblocks *eofb) |
| 1144 | { |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 1145 | if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) && |
| 1146 | !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid)) |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1147 | return false; |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1148 | |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 1149 | if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) && |
| 1150 | !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid)) |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1151 | return false; |
Brian Foster | 1b55604 | 2012-11-06 09:50:45 -0500 | [diff] [blame] | 1152 | |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 1153 | if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) && |
Christoph Hellwig | ceaf603 | 2021-03-29 11:11:39 -0700 | [diff] [blame] | 1154 | ip->i_projid != eofb->eof_prid) |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1155 | return false; |
Brian Foster | 1b55604 | 2012-11-06 09:50:45 -0500 | [diff] [blame] | 1156 | |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1157 | return true; |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1158 | } |
| 1159 | |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1160 | /* |
| 1161 | * A union-based inode filtering algorithm. Process the inode if any of the |
| 1162 | * criteria match. This is for global/internal scans only. |
| 1163 | */ |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1164 | STATIC bool |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1165 | xfs_inode_match_id_union( |
| 1166 | struct xfs_inode *ip, |
| 1167 | struct xfs_eofblocks *eofb) |
| 1168 | { |
| 1169 | if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) && |
| 1170 | uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid)) |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1171 | return true; |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1172 | |
| 1173 | if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) && |
| 1174 | gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid)) |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1175 | return true; |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1176 | |
| 1177 | if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) && |
Christoph Hellwig | ceaf603 | 2021-03-29 11:11:39 -0700 | [diff] [blame] | 1178 | ip->i_projid == eofb->eof_prid) |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1179 | return true; |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1180 | |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1181 | return false; |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1182 | } |
| 1183 | |
Darrick J. Wong | a91bf99 | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1184 | /* |
| 1185 | * Is this inode @ip eligible for eof/cow block reclamation, given some |
| 1186 | * filtering parameters @eofb? The inode is eligible if @eofb is null or |
| 1187 | * if the predicate functions match. |
| 1188 | */ |
| 1189 | static bool |
| 1190 | xfs_inode_matches_eofb( |
| 1191 | struct xfs_inode *ip, |
| 1192 | struct xfs_eofblocks *eofb) |
| 1193 | { |
Darrick J. Wong | 39b1cfd | 2020-05-21 13:08:49 -0700 | [diff] [blame] | 1194 | bool match; |
Darrick J. Wong | a91bf99 | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1195 | |
| 1196 | if (!eofb) |
| 1197 | return true; |
| 1198 | |
| 1199 | if (eofb->eof_flags & XFS_EOF_FLAGS_UNION) |
| 1200 | match = xfs_inode_match_id_union(ip, eofb); |
| 1201 | else |
| 1202 | match = xfs_inode_match_id(ip, eofb); |
| 1203 | if (!match) |
| 1204 | return false; |
| 1205 | |
| 1206 | /* skip the inode if the file size is too small */ |
| 1207 | if ((eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE) && |
| 1208 | XFS_ISIZE(ip) < eofb->eof_min_file_size) |
| 1209 | return false; |
| 1210 | |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1214 | /* |
| 1215 | * This is a fast pass over the inode cache to try to get reclaim moving on as |
| 1216 | * many inodes as possible in a short period of time. It kicks itself every few |
| 1217 | * seconds, as well as being kicked by the inode cache shrinker when memory |
Dave Chinner | 02511a5 | 2020-06-29 14:49:18 -0700 | [diff] [blame] | 1218 | * goes low. |
Dave Chinner | 4d0bab3 | 2020-07-01 10:21:28 -0700 | [diff] [blame] | 1219 | */ |
| 1220 | void |
| 1221 | xfs_reclaim_worker( |
| 1222 | struct work_struct *work) |
| 1223 | { |
| 1224 | struct xfs_mount *mp = container_of(to_delayed_work(work), |
| 1225 | struct xfs_mount, m_reclaim_work); |
| 1226 | int nr_to_scan = INT_MAX; |
| 1227 | |
| 1228 | xfs_reclaim_inodes_ag(mp, &nr_to_scan); |
| 1229 | xfs_reclaim_work_queue(mp); |
| 1230 | } |
| 1231 | |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1232 | STATIC int |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1233 | xfs_inode_free_eofblocks( |
| 1234 | struct xfs_inode *ip, |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1235 | void *args, |
| 1236 | unsigned int *lockflags) |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1237 | { |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1238 | struct xfs_eofblocks *eofb = args; |
| 1239 | bool wait; |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1240 | |
| 1241 | wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC); |
Brian Foster | 5400da7 | 2014-07-24 19:40:22 +1000 | [diff] [blame] | 1242 | |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1243 | if (!xfs_iflags_test(ip, XFS_IEOFBLOCKS)) |
| 1244 | return 0; |
| 1245 | |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1246 | /* |
| 1247 | * If the mapping is dirty the operation can block and wait for some |
| 1248 | * time. Unless we are waiting, skip it. |
| 1249 | */ |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1250 | if (!wait && mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY)) |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1251 | return 0; |
| 1252 | |
Darrick J. Wong | a91bf99 | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1253 | if (!xfs_inode_matches_eofb(ip, eofb)) |
| 1254 | return 0; |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1255 | |
Brian Foster | a36b926 | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 1256 | /* |
| 1257 | * If the caller is waiting, return -EAGAIN to keep the background |
| 1258 | * scanner moving and revisit the inode in a subsequent pass. |
| 1259 | */ |
Brian Foster | c315509 | 2017-01-27 23:22:56 -0800 | [diff] [blame] | 1260 | if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1261 | if (wait) |
| 1262 | return -EAGAIN; |
| 1263 | return 0; |
Brian Foster | a36b926 | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 1264 | } |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1265 | *lockflags |= XFS_IOLOCK_EXCL; |
Darrick J. Wong | 390600f | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1266 | |
Darrick J. Wong | 2b156ff | 2021-03-23 16:59:31 -0700 | [diff] [blame] | 1267 | if (xfs_can_free_eofblocks(ip, false)) |
| 1268 | return xfs_free_eofblocks(ip); |
| 1269 | |
| 1270 | /* inode could be preallocated or append-only */ |
| 1271 | trace_xfs_inode_free_eofblocks_invalid(ip); |
| 1272 | xfs_inode_clear_eofblocks_tag(ip); |
| 1273 | return 0; |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1274 | } |
| 1275 | |
Darrick J. Wong | f929656 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1276 | /* |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1277 | * Background scanning to trim preallocated space. This is queued based on the |
| 1278 | * 'speculative_prealloc_lifetime' tunable (5m by default). |
Darrick J. Wong | f929656 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1279 | */ |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1280 | static inline void |
| 1281 | xfs_blockgc_queue( |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1282 | struct xfs_perag *pag) |
Darrick J. Wong | f929656 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1283 | { |
| 1284 | rcu_read_lock(); |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1285 | if (radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG)) |
Darrick J. Wong | 3fef46f | 2021-03-22 09:51:55 -0700 | [diff] [blame] | 1286 | queue_delayed_work(pag->pag_mount->m_gc_workqueue, |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1287 | &pag->pag_blockgc_work, |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1288 | msecs_to_jiffies(xfs_blockgc_secs * 1000)); |
Darrick J. Wong | f929656 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1289 | rcu_read_unlock(); |
| 1290 | } |
| 1291 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1292 | static void |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1293 | xfs_blockgc_set_iflag( |
| 1294 | struct xfs_inode *ip, |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1295 | unsigned long iflag) |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1296 | { |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1297 | struct xfs_mount *mp = ip->i_mount; |
| 1298 | struct xfs_perag *pag; |
| 1299 | int tagged; |
| 1300 | |
| 1301 | ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1302 | |
Christoph Hellwig | 85a6e76 | 2016-09-19 11:09:48 +1000 | [diff] [blame] | 1303 | /* |
| 1304 | * Don't bother locking the AG and looking up in the radix trees |
| 1305 | * if we already know that we have the tag set. |
| 1306 | */ |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1307 | if (ip->i_flags & iflag) |
Christoph Hellwig | 85a6e76 | 2016-09-19 11:09:48 +1000 | [diff] [blame] | 1308 | return; |
| 1309 | spin_lock(&ip->i_flags_lock); |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1310 | ip->i_flags |= iflag; |
Christoph Hellwig | 85a6e76 | 2016-09-19 11:09:48 +1000 | [diff] [blame] | 1311 | spin_unlock(&ip->i_flags_lock); |
| 1312 | |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1313 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
| 1314 | spin_lock(&pag->pag_ici_lock); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1315 | |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1316 | tagged = radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1317 | radix_tree_tag_set(&pag->pag_ici_root, |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1318 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), |
| 1319 | XFS_ICI_BLOCKGC_TAG); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1320 | if (!tagged) { |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1321 | /* propagate the blockgc tag up into the perag radix tree */ |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1322 | spin_lock(&ip->i_mount->m_perag_lock); |
| 1323 | radix_tree_tag_set(&ip->i_mount->m_perag_tree, |
| 1324 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1325 | XFS_ICI_BLOCKGC_TAG); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1326 | spin_unlock(&ip->i_mount->m_perag_lock); |
| 1327 | |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 1328 | /* kick off background trimming */ |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1329 | xfs_blockgc_queue(pag); |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 1330 | |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1331 | trace_xfs_perag_set_blockgc(ip->i_mount, pag->pag_agno, -1, |
| 1332 | _RET_IP_); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | spin_unlock(&pag->pag_ici_lock); |
| 1336 | xfs_perag_put(pag); |
| 1337 | } |
| 1338 | |
| 1339 | void |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1340 | xfs_inode_set_eofblocks_tag( |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1341 | xfs_inode_t *ip) |
| 1342 | { |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1343 | trace_xfs_inode_set_eofblocks_tag(ip); |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1344 | return xfs_blockgc_set_iflag(ip, XFS_IEOFBLOCKS); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | static void |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1348 | xfs_blockgc_clear_iflag( |
| 1349 | struct xfs_inode *ip, |
| 1350 | unsigned long iflag) |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1351 | { |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1352 | struct xfs_mount *mp = ip->i_mount; |
| 1353 | struct xfs_perag *pag; |
| 1354 | bool clear_tag; |
| 1355 | |
| 1356 | ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1357 | |
Christoph Hellwig | 85a6e76 | 2016-09-19 11:09:48 +1000 | [diff] [blame] | 1358 | spin_lock(&ip->i_flags_lock); |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1359 | ip->i_flags &= ~iflag; |
| 1360 | clear_tag = (ip->i_flags & (XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0; |
Christoph Hellwig | 85a6e76 | 2016-09-19 11:09:48 +1000 | [diff] [blame] | 1361 | spin_unlock(&ip->i_flags_lock); |
| 1362 | |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1363 | if (!clear_tag) |
| 1364 | return; |
| 1365 | |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1366 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
| 1367 | spin_lock(&pag->pag_ici_lock); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1368 | |
| 1369 | radix_tree_tag_clear(&pag->pag_ici_root, |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1370 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), |
| 1371 | XFS_ICI_BLOCKGC_TAG); |
| 1372 | if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG)) { |
| 1373 | /* clear the blockgc tag from the perag radix tree */ |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1374 | spin_lock(&ip->i_mount->m_perag_lock); |
| 1375 | radix_tree_tag_clear(&ip->i_mount->m_perag_tree, |
| 1376 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1377 | XFS_ICI_BLOCKGC_TAG); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1378 | spin_unlock(&ip->i_mount->m_perag_lock); |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1379 | trace_xfs_perag_clear_blockgc(ip->i_mount, pag->pag_agno, -1, |
| 1380 | _RET_IP_); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | spin_unlock(&pag->pag_ici_lock); |
| 1384 | xfs_perag_put(pag); |
| 1385 | } |
| 1386 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1387 | void |
| 1388 | xfs_inode_clear_eofblocks_tag( |
| 1389 | xfs_inode_t *ip) |
| 1390 | { |
| 1391 | trace_xfs_inode_clear_eofblocks_tag(ip); |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1392 | return xfs_blockgc_clear_iflag(ip, XFS_IEOFBLOCKS); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /* |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 1396 | * Set ourselves up to free CoW blocks from this file. If it's already clean |
| 1397 | * then we can bail out quickly, but otherwise we must back off if the file |
| 1398 | * is undergoing some kind of write. |
| 1399 | */ |
| 1400 | static bool |
| 1401 | xfs_prep_free_cowblocks( |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame] | 1402 | struct xfs_inode *ip) |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 1403 | { |
| 1404 | /* |
| 1405 | * Just clear the tag if we have an empty cow fork or none at all. It's |
| 1406 | * possible the inode was fully unshared since it was originally tagged. |
| 1407 | */ |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame] | 1408 | if (!xfs_inode_has_cow_data(ip)) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 1409 | trace_xfs_inode_free_cowblocks_invalid(ip); |
| 1410 | xfs_inode_clear_cowblocks_tag(ip); |
| 1411 | return false; |
| 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * If the mapping is dirty or under writeback we cannot touch the |
| 1416 | * CoW fork. Leave it alone if we're in the midst of a directio. |
| 1417 | */ |
| 1418 | if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) || |
| 1419 | mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) || |
| 1420 | mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) || |
| 1421 | atomic_read(&VFS_I(ip)->i_dio_count)) |
| 1422 | return false; |
| 1423 | |
| 1424 | return true; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1428 | * Automatic CoW Reservation Freeing |
| 1429 | * |
| 1430 | * These functions automatically garbage collect leftover CoW reservations |
| 1431 | * that were made on behalf of a cowextsize hint when we start to run out |
| 1432 | * of quota or when the reservations sit around for too long. If the file |
| 1433 | * has dirty pages or is undergoing writeback, its CoW reservations will |
| 1434 | * be retained. |
| 1435 | * |
| 1436 | * The actual garbage collection piggybacks off the same code that runs |
| 1437 | * the speculative EOF preallocation garbage collector. |
| 1438 | */ |
| 1439 | STATIC int |
| 1440 | xfs_inode_free_cowblocks( |
| 1441 | struct xfs_inode *ip, |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1442 | void *args, |
| 1443 | unsigned int *lockflags) |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1444 | { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 1445 | struct xfs_eofblocks *eofb = args; |
Darrick J. Wong | f41a071 | 2021-01-22 16:48:35 -0800 | [diff] [blame] | 1446 | bool wait; |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 1447 | int ret = 0; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1448 | |
Darrick J. Wong | f41a071 | 2021-01-22 16:48:35 -0800 | [diff] [blame] | 1449 | wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC); |
| 1450 | |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1451 | if (!xfs_iflags_test(ip, XFS_ICOWBLOCKS)) |
| 1452 | return 0; |
| 1453 | |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame] | 1454 | if (!xfs_prep_free_cowblocks(ip)) |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1455 | return 0; |
| 1456 | |
Darrick J. Wong | a91bf99 | 2020-05-21 13:08:48 -0700 | [diff] [blame] | 1457 | if (!xfs_inode_matches_eofb(ip, eofb)) |
| 1458 | return 0; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1459 | |
Darrick J. Wong | f41a071 | 2021-01-22 16:48:35 -0800 | [diff] [blame] | 1460 | /* |
| 1461 | * If the caller is waiting, return -EAGAIN to keep the background |
| 1462 | * scanner moving and revisit the inode in a subsequent pass. |
| 1463 | */ |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1464 | if (!(*lockflags & XFS_IOLOCK_EXCL) && |
| 1465 | !xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { |
Darrick J. Wong | f41a071 | 2021-01-22 16:48:35 -0800 | [diff] [blame] | 1466 | if (wait) |
| 1467 | return -EAGAIN; |
| 1468 | return 0; |
| 1469 | } |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1470 | *lockflags |= XFS_IOLOCK_EXCL; |
| 1471 | |
Darrick J. Wong | f41a071 | 2021-01-22 16:48:35 -0800 | [diff] [blame] | 1472 | if (!xfs_ilock_nowait(ip, XFS_MMAPLOCK_EXCL)) { |
| 1473 | if (wait) |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1474 | return -EAGAIN; |
| 1475 | return 0; |
Darrick J. Wong | f41a071 | 2021-01-22 16:48:35 -0800 | [diff] [blame] | 1476 | } |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1477 | *lockflags |= XFS_MMAPLOCK_EXCL; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1478 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 1479 | /* |
| 1480 | * Check again, nobody else should be able to dirty blocks or change |
| 1481 | * the reflink iflag now that we have the first two locks held. |
| 1482 | */ |
Christoph Hellwig | 51d6269 | 2018-07-17 16:51:51 -0700 | [diff] [blame] | 1483 | if (xfs_prep_free_cowblocks(ip)) |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 1484 | ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1485 | return ret; |
| 1486 | } |
| 1487 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1488 | void |
| 1489 | xfs_inode_set_cowblocks_tag( |
| 1490 | xfs_inode_t *ip) |
| 1491 | { |
Brian Foster | 7b7381f | 2016-10-24 14:21:00 +1100 | [diff] [blame] | 1492 | trace_xfs_inode_set_cowblocks_tag(ip); |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1493 | return xfs_blockgc_set_iflag(ip, XFS_ICOWBLOCKS); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | void |
| 1497 | xfs_inode_clear_cowblocks_tag( |
| 1498 | xfs_inode_t *ip) |
| 1499 | { |
Brian Foster | 7b7381f | 2016-10-24 14:21:00 +1100 | [diff] [blame] | 1500 | trace_xfs_inode_clear_cowblocks_tag(ip); |
Darrick J. Wong | ce2d3bb | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1501 | return xfs_blockgc_clear_iflag(ip, XFS_ICOWBLOCKS); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1502 | } |
Darrick J. Wong | d6b636e | 2018-05-09 10:03:56 -0700 | [diff] [blame] | 1503 | |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1504 | #define for_each_perag_tag(mp, next_agno, pag, tag) \ |
| 1505 | for ((next_agno) = 0, (pag) = xfs_perag_get_tag((mp), 0, (tag)); \ |
| 1506 | (pag) != NULL; \ |
| 1507 | (next_agno) = (pag)->pag_agno + 1, \ |
| 1508 | xfs_perag_put(pag), \ |
| 1509 | (pag) = xfs_perag_get_tag((mp), (next_agno), (tag))) |
| 1510 | |
| 1511 | |
Darrick J. Wong | d6b636e | 2018-05-09 10:03:56 -0700 | [diff] [blame] | 1512 | /* Disable post-EOF and CoW block auto-reclamation. */ |
| 1513 | void |
Darrick J. Wong | c9a6526 | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1514 | xfs_blockgc_stop( |
Darrick J. Wong | d6b636e | 2018-05-09 10:03:56 -0700 | [diff] [blame] | 1515 | struct xfs_mount *mp) |
| 1516 | { |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1517 | struct xfs_perag *pag; |
| 1518 | xfs_agnumber_t agno; |
| 1519 | |
| 1520 | for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG) |
| 1521 | cancel_delayed_work_sync(&pag->pag_blockgc_work); |
Darrick J. Wong | d6b636e | 2018-05-09 10:03:56 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /* Enable post-EOF and CoW block auto-reclamation. */ |
| 1525 | void |
Darrick J. Wong | c9a6526 | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1526 | xfs_blockgc_start( |
Darrick J. Wong | d6b636e | 2018-05-09 10:03:56 -0700 | [diff] [blame] | 1527 | struct xfs_mount *mp) |
| 1528 | { |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1529 | struct xfs_perag *pag; |
| 1530 | xfs_agnumber_t agno; |
| 1531 | |
| 1532 | for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG) |
| 1533 | xfs_blockgc_queue(pag); |
Darrick J. Wong | d6b636e | 2018-05-09 10:03:56 -0700 | [diff] [blame] | 1534 | } |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1535 | |
Darrick J. Wong | d20d5ed | 2021-06-01 23:01:44 -0700 | [diff] [blame^] | 1536 | /* Don't try to run block gc on an inode that's in any of these states. */ |
| 1537 | #define XFS_BLOCKGC_NOGRAB_IFLAGS (XFS_INEW | \ |
| 1538 | XFS_IRECLAIMABLE | \ |
| 1539 | XFS_IRECLAIM) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1540 | /* |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 1541 | * Decide if the given @ip is eligible for garbage collection of speculative |
| 1542 | * preallocations, and grab it if so. Returns true if it's ready to go or |
| 1543 | * false if we should just ignore it. |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1544 | */ |
| 1545 | static bool |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 1546 | xfs_blockgc_igrab( |
Darrick J. Wong | 7fdff52 | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 1547 | struct xfs_inode *ip) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1548 | { |
| 1549 | struct inode *inode = VFS_I(ip); |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1550 | |
| 1551 | ASSERT(rcu_read_lock_held()); |
| 1552 | |
| 1553 | /* Check for stale RCU freed inode */ |
| 1554 | spin_lock(&ip->i_flags_lock); |
| 1555 | if (!ip->i_ino) |
| 1556 | goto out_unlock_noent; |
| 1557 | |
Darrick J. Wong | d20d5ed | 2021-06-01 23:01:44 -0700 | [diff] [blame^] | 1558 | if (ip->i_flags & XFS_BLOCKGC_NOGRAB_IFLAGS) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1559 | goto out_unlock_noent; |
| 1560 | spin_unlock(&ip->i_flags_lock); |
| 1561 | |
| 1562 | /* nothing to sync during shutdown */ |
| 1563 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 1564 | return false; |
| 1565 | |
| 1566 | /* If we can't grab the inode, it must on it's way to reclaim. */ |
| 1567 | if (!igrab(inode)) |
| 1568 | return false; |
| 1569 | |
| 1570 | /* inode is valid */ |
| 1571 | return true; |
| 1572 | |
| 1573 | out_unlock_noent: |
| 1574 | spin_unlock(&ip->i_flags_lock); |
| 1575 | return false; |
| 1576 | } |
| 1577 | |
Darrick J. Wong | 4195675 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1578 | /* Scan one incore inode for block preallocations that we can remove. */ |
| 1579 | static int |
| 1580 | xfs_blockgc_scan_inode( |
| 1581 | struct xfs_inode *ip, |
| 1582 | void *args) |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1583 | { |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1584 | unsigned int lockflags = 0; |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1585 | int error; |
| 1586 | |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1587 | error = xfs_inode_free_eofblocks(ip, args, &lockflags); |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1588 | if (error) |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1589 | goto unlock; |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1590 | |
Darrick J. Wong | 0fa4a10 | 2021-01-25 21:09:49 -0800 | [diff] [blame] | 1591 | error = xfs_inode_free_cowblocks(ip, args, &lockflags); |
| 1592 | unlock: |
| 1593 | if (lockflags) |
| 1594 | xfs_iunlock(ip, lockflags); |
| 1595 | return error; |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1598 | /* Background worker that trims preallocated space. */ |
| 1599 | void |
| 1600 | xfs_blockgc_worker( |
| 1601 | struct work_struct *work) |
| 1602 | { |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1603 | struct xfs_perag *pag = container_of(to_delayed_work(work), |
| 1604 | struct xfs_perag, pag_blockgc_work); |
| 1605 | struct xfs_mount *mp = pag->pag_mount; |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1606 | int error; |
| 1607 | |
| 1608 | if (!sb_start_write_trylock(mp->m_super)) |
| 1609 | return; |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1610 | error = xfs_icwalk_ag(pag, XFS_ICWALK_BLOCKGC, NULL); |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1611 | if (error) |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1612 | xfs_info(mp, "AG %u preallocation gc worker failed, err=%d", |
| 1613 | pag->pag_agno, error); |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1614 | sb_end_write(mp->m_super); |
Darrick J. Wong | 894ecac | 2021-01-22 16:48:44 -0800 | [diff] [blame] | 1615 | xfs_blockgc_queue(pag); |
Darrick J. Wong | 9669f51 | 2021-01-22 16:48:43 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1618 | /* |
| 1619 | * Try to free space in the filesystem by purging eofblocks and cowblocks. |
| 1620 | */ |
| 1621 | int |
| 1622 | xfs_blockgc_free_space( |
| 1623 | struct xfs_mount *mp, |
| 1624 | struct xfs_eofblocks *eofb) |
| 1625 | { |
| 1626 | trace_xfs_blockgc_free_space(mp, eofb, _RET_IP_); |
| 1627 | |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1628 | return xfs_icwalk(mp, XFS_ICWALK_BLOCKGC, eofb); |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1631 | /* |
Darrick J. Wong | c237dd7 | 2021-01-22 16:48:37 -0800 | [diff] [blame] | 1632 | * Run cow/eofblocks scans on the supplied dquots. We don't know exactly which |
| 1633 | * quota caused an allocation failure, so we make a best effort by including |
| 1634 | * each quota under low free space conditions (less than 1% free space) in the |
| 1635 | * scan. |
Darrick J. Wong | 111068f | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1636 | * |
| 1637 | * Callers must not hold any inode's ILOCK. If requesting a synchronous scan |
| 1638 | * (XFS_EOF_FLAGS_SYNC), the caller also must not hold any inode's IOLOCK or |
| 1639 | * MMAPLOCK. |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1640 | */ |
Darrick J. Wong | 111068f | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1641 | int |
Darrick J. Wong | c237dd7 | 2021-01-22 16:48:37 -0800 | [diff] [blame] | 1642 | xfs_blockgc_free_dquots( |
| 1643 | struct xfs_mount *mp, |
| 1644 | struct xfs_dquot *udqp, |
| 1645 | struct xfs_dquot *gdqp, |
| 1646 | struct xfs_dquot *pdqp, |
Darrick J. Wong | 111068f | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1647 | unsigned int eof_flags) |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1648 | { |
| 1649 | struct xfs_eofblocks eofb = {0}; |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1650 | bool do_work = false; |
| 1651 | |
Darrick J. Wong | c237dd7 | 2021-01-22 16:48:37 -0800 | [diff] [blame] | 1652 | if (!udqp && !gdqp && !pdqp) |
| 1653 | return 0; |
| 1654 | |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1655 | /* |
Darrick J. Wong | 111068f | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1656 | * Run a scan to free blocks using the union filter to cover all |
| 1657 | * applicable quotas in a single scan. |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1658 | */ |
Darrick J. Wong | 111068f | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1659 | eofb.eof_flags = XFS_EOF_FLAGS_UNION | eof_flags; |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1660 | |
Darrick J. Wong | c237dd7 | 2021-01-22 16:48:37 -0800 | [diff] [blame] | 1661 | if (XFS_IS_UQUOTA_ENFORCED(mp) && udqp && xfs_dquot_lowsp(udqp)) { |
| 1662 | eofb.eof_uid = make_kuid(mp->m_super->s_user_ns, udqp->q_id); |
| 1663 | eofb.eof_flags |= XFS_EOF_FLAGS_UID; |
| 1664 | do_work = true; |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Darrick J. Wong | c237dd7 | 2021-01-22 16:48:37 -0800 | [diff] [blame] | 1667 | if (XFS_IS_UQUOTA_ENFORCED(mp) && gdqp && xfs_dquot_lowsp(gdqp)) { |
| 1668 | eofb.eof_gid = make_kgid(mp->m_super->s_user_ns, gdqp->q_id); |
| 1669 | eofb.eof_flags |= XFS_EOF_FLAGS_GID; |
| 1670 | do_work = true; |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
Darrick J. Wong | c237dd7 | 2021-01-22 16:48:37 -0800 | [diff] [blame] | 1673 | if (XFS_IS_PQUOTA_ENFORCED(mp) && pdqp && xfs_dquot_lowsp(pdqp)) { |
| 1674 | eofb.eof_prid = pdqp->q_id; |
| 1675 | eofb.eof_flags |= XFS_EOF_FLAGS_PRID; |
| 1676 | do_work = true; |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | if (!do_work) |
Darrick J. Wong | 111068f | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1680 | return 0; |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1681 | |
Darrick J. Wong | 85c5b27 | 2021-01-22 16:48:39 -0800 | [diff] [blame] | 1682 | return xfs_blockgc_free_space(mp, &eofb); |
Darrick J. Wong | c237dd7 | 2021-01-22 16:48:37 -0800 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | /* Run cow/eofblocks scans on the quotas attached to the inode. */ |
| 1686 | int |
| 1687 | xfs_blockgc_free_quota( |
| 1688 | struct xfs_inode *ip, |
| 1689 | unsigned int eof_flags) |
| 1690 | { |
| 1691 | return xfs_blockgc_free_dquots(ip->i_mount, |
| 1692 | xfs_inode_dquot(ip, XFS_DQTYPE_USER), |
| 1693 | xfs_inode_dquot(ip, XFS_DQTYPE_GROUP), |
| 1694 | xfs_inode_dquot(ip, XFS_DQTYPE_PROJ), eof_flags); |
Darrick J. Wong | 3d4feec | 2021-01-22 16:48:36 -0800 | [diff] [blame] | 1695 | } |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1696 | |
| 1697 | /* XFS Inode Cache Walking Code */ |
| 1698 | |
| 1699 | /* |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 1700 | * Decide if we want to grab this inode in anticipation of doing work towards |
| 1701 | * the goal. If selected, the VFS must hold a reference to this inode, which |
| 1702 | * will be released after processing. |
| 1703 | */ |
| 1704 | static inline bool |
| 1705 | xfs_icwalk_igrab( |
| 1706 | enum xfs_icwalk_goal goal, |
Darrick J. Wong | 7fdff52 | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 1707 | struct xfs_inode *ip) |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 1708 | { |
| 1709 | switch (goal) { |
| 1710 | case XFS_ICWALK_DQRELE: |
| 1711 | return xfs_dqrele_igrab(ip); |
| 1712 | case XFS_ICWALK_BLOCKGC: |
Darrick J. Wong | 7fdff52 | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 1713 | return xfs_blockgc_igrab(ip); |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 1714 | default: |
| 1715 | return false; |
| 1716 | } |
| 1717 | } |
| 1718 | |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1719 | /* Process an inode and release it. Return -EAGAIN to skip an inode. */ |
| 1720 | static inline int |
| 1721 | xfs_icwalk_process_inode( |
| 1722 | enum xfs_icwalk_goal goal, |
| 1723 | struct xfs_inode *ip, |
| 1724 | void *args) |
| 1725 | { |
| 1726 | int error; |
| 1727 | |
| 1728 | switch (goal) { |
| 1729 | case XFS_ICWALK_DQRELE: |
| 1730 | error = xfs_dqrele_inode(ip, args); |
| 1731 | break; |
| 1732 | case XFS_ICWALK_BLOCKGC: |
| 1733 | error = xfs_blockgc_scan_inode(ip, args); |
| 1734 | break; |
| 1735 | } |
| 1736 | xfs_irele(ip); |
| 1737 | return error; |
| 1738 | } |
| 1739 | |
Darrick J. Wong | b9baaef | 2021-05-31 11:31:58 -0700 | [diff] [blame] | 1740 | /* |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1741 | * For a given per-AG structure @pag and a goal, grab qualifying inodes and |
| 1742 | * process them in some manner. |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1743 | */ |
| 1744 | static int |
Darrick J. Wong | c1115c0 | 2021-06-01 22:41:25 -0700 | [diff] [blame] | 1745 | xfs_icwalk_ag( |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1746 | struct xfs_perag *pag, |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1747 | enum xfs_icwalk_goal goal, |
| 1748 | void *args) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1749 | { |
| 1750 | struct xfs_mount *mp = pag->pag_mount; |
| 1751 | uint32_t first_index; |
| 1752 | int last_error = 0; |
| 1753 | int skipped; |
| 1754 | bool done; |
| 1755 | int nr_found; |
| 1756 | |
| 1757 | restart: |
| 1758 | done = false; |
| 1759 | skipped = 0; |
| 1760 | first_index = 0; |
| 1761 | nr_found = 0; |
| 1762 | do { |
| 1763 | struct xfs_inode *batch[XFS_LOOKUP_BATCH]; |
Darrick J. Wong | c809d7e | 2021-06-01 13:49:52 -0700 | [diff] [blame] | 1764 | unsigned int tag = xfs_icwalk_tag(goal); |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1765 | int error = 0; |
| 1766 | int i; |
| 1767 | |
| 1768 | rcu_read_lock(); |
| 1769 | |
Darrick J. Wong | c809d7e | 2021-06-01 13:49:52 -0700 | [diff] [blame] | 1770 | if (tag == XFS_ICWALK_NULL_TAG) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1771 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, |
| 1772 | (void **)batch, first_index, |
| 1773 | XFS_LOOKUP_BATCH); |
| 1774 | else |
| 1775 | nr_found = radix_tree_gang_lookup_tag( |
| 1776 | &pag->pag_ici_root, |
| 1777 | (void **) batch, first_index, |
| 1778 | XFS_LOOKUP_BATCH, tag); |
| 1779 | |
| 1780 | if (!nr_found) { |
| 1781 | rcu_read_unlock(); |
| 1782 | break; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * Grab the inodes before we drop the lock. if we found |
| 1787 | * nothing, nr == 0 and the loop will be skipped. |
| 1788 | */ |
| 1789 | for (i = 0; i < nr_found; i++) { |
| 1790 | struct xfs_inode *ip = batch[i]; |
| 1791 | |
Darrick J. Wong | 7fdff52 | 2021-05-31 11:31:59 -0700 | [diff] [blame] | 1792 | if (done || !xfs_icwalk_igrab(goal, ip)) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1793 | batch[i] = NULL; |
| 1794 | |
| 1795 | /* |
| 1796 | * Update the index for the next lookup. Catch |
| 1797 | * overflows into the next AG range which can occur if |
| 1798 | * we have inodes in the last block of the AG and we |
| 1799 | * are currently pointing to the last inode. |
| 1800 | * |
| 1801 | * Because we may see inodes that are from the wrong AG |
| 1802 | * due to RCU freeing and reallocation, only update the |
| 1803 | * index if it lies in this AG. It was a race that lead |
| 1804 | * us to see this inode, so another lookup from the |
| 1805 | * same index will not find it again. |
| 1806 | */ |
| 1807 | if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno) |
| 1808 | continue; |
| 1809 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); |
| 1810 | if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) |
| 1811 | done = true; |
| 1812 | } |
| 1813 | |
| 1814 | /* unlock now we've grabbed the inodes. */ |
| 1815 | rcu_read_unlock(); |
| 1816 | |
| 1817 | for (i = 0; i < nr_found; i++) { |
| 1818 | if (!batch[i]) |
| 1819 | continue; |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1820 | error = xfs_icwalk_process_inode(goal, batch[i], args); |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1821 | if (error == -EAGAIN) { |
| 1822 | skipped++; |
| 1823 | continue; |
| 1824 | } |
| 1825 | if (error && last_error != -EFSCORRUPTED) |
| 1826 | last_error = error; |
| 1827 | } |
| 1828 | |
| 1829 | /* bail out if the filesystem is corrupted. */ |
| 1830 | if (error == -EFSCORRUPTED) |
| 1831 | break; |
| 1832 | |
| 1833 | cond_resched(); |
| 1834 | |
| 1835 | } while (nr_found && !done); |
| 1836 | |
| 1837 | if (skipped) { |
| 1838 | delay(1); |
| 1839 | goto restart; |
| 1840 | } |
| 1841 | return last_error; |
| 1842 | } |
| 1843 | |
| 1844 | /* Fetch the next (possibly tagged) per-AG structure. */ |
| 1845 | static inline struct xfs_perag * |
Darrick J. Wong | c1115c0 | 2021-06-01 22:41:25 -0700 | [diff] [blame] | 1846 | xfs_icwalk_get_perag( |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1847 | struct xfs_mount *mp, |
| 1848 | xfs_agnumber_t agno, |
Darrick J. Wong | c809d7e | 2021-06-01 13:49:52 -0700 | [diff] [blame] | 1849 | enum xfs_icwalk_goal goal) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1850 | { |
Darrick J. Wong | c809d7e | 2021-06-01 13:49:52 -0700 | [diff] [blame] | 1851 | unsigned int tag = xfs_icwalk_tag(goal); |
| 1852 | |
| 1853 | if (tag == XFS_ICWALK_NULL_TAG) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1854 | return xfs_perag_get(mp, agno); |
| 1855 | return xfs_perag_get_tag(mp, agno, tag); |
| 1856 | } |
| 1857 | |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1858 | /* Walk all incore inodes to achieve a given goal. */ |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1859 | static int |
Darrick J. Wong | c1115c0 | 2021-06-01 22:41:25 -0700 | [diff] [blame] | 1860 | xfs_icwalk( |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1861 | struct xfs_mount *mp, |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1862 | enum xfs_icwalk_goal goal, |
| 1863 | void *args) |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1864 | { |
| 1865 | struct xfs_perag *pag; |
| 1866 | int error = 0; |
| 1867 | int last_error = 0; |
| 1868 | xfs_agnumber_t agno = 0; |
| 1869 | |
Darrick J. Wong | c809d7e | 2021-06-01 13:49:52 -0700 | [diff] [blame] | 1870 | while ((pag = xfs_icwalk_get_perag(mp, agno, goal))) { |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1871 | agno = pag->pag_agno + 1; |
Darrick J. Wong | f427cf5 | 2021-05-31 11:32:00 -0700 | [diff] [blame] | 1872 | error = xfs_icwalk_ag(pag, goal, args); |
Darrick J. Wong | df60019 | 2021-06-01 13:29:41 -0700 | [diff] [blame] | 1873 | xfs_perag_put(pag); |
| 1874 | if (error) { |
| 1875 | last_error = error; |
| 1876 | if (error == -EFSCORRUPTED) |
| 1877 | break; |
| 1878 | } |
| 1879 | } |
| 1880 | return last_error; |
| 1881 | BUILD_BUG_ON(XFS_ICWALK_PRIVATE_FLAGS & XFS_EOF_FLAGS_VALID); |
| 1882 | } |