David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | #include "xfs.h" |
| 19 | #include "xfs_fs.h" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 20 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_log_format.h" |
| 22 | #include "xfs_trans_resv.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 23 | #include "xfs_sb.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 24 | #include "xfs_mount.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 25 | #include "xfs_inode.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 26 | #include "xfs_error.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 27 | #include "xfs_trans.h" |
| 28 | #include "xfs_trans_priv.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 29 | #include "xfs_inode_item.h" |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 30 | #include "xfs_quota.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 31 | #include "xfs_trace.h" |
Dave Chinner | 6d8b79c | 2012-10-08 21:56:09 +1100 | [diff] [blame] | 32 | #include "xfs_icache.h" |
Dave Chinner | c24b5df | 2013-08-12 20:49:45 +1000 | [diff] [blame] | 33 | #include "xfs_bmap_util.h" |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 34 | #include "xfs_dquot_item.h" |
| 35 | #include "xfs_dquot.h" |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 36 | #include "xfs_reflink.h" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 37 | |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 38 | #include <linux/kthread.h> |
| 39 | #include <linux/freezer.h> |
| 40 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 41 | /* |
| 42 | * Allocate and initialise an xfs_inode. |
| 43 | */ |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame] | 44 | struct xfs_inode * |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 45 | xfs_inode_alloc( |
| 46 | struct xfs_mount *mp, |
| 47 | xfs_ino_t ino) |
| 48 | { |
| 49 | struct xfs_inode *ip; |
| 50 | |
| 51 | /* |
| 52 | * if this didn't occur in transactions, we could use |
| 53 | * KM_MAYFAIL and return NULL here on ENOMEM. Set the |
| 54 | * code up to do this anyway. |
| 55 | */ |
| 56 | ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP); |
| 57 | if (!ip) |
| 58 | return NULL; |
| 59 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
| 60 | kmem_zone_free(xfs_inode_zone, ip); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 64 | /* VFS doesn't initialise i_mode! */ |
| 65 | VFS_I(ip)->i_mode = 0; |
| 66 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 67 | XFS_STATS_INC(mp, vn_active); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 68 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 69 | ASSERT(!xfs_isiflocked(ip)); |
| 70 | ASSERT(ip->i_ino == 0); |
| 71 | |
| 72 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
| 73 | |
| 74 | /* initialise the xfs inode */ |
| 75 | ip->i_ino = ino; |
| 76 | ip->i_mount = mp; |
| 77 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); |
| 78 | ip->i_afp = NULL; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 79 | ip->i_cowfp = NULL; |
| 80 | ip->i_cnextents = 0; |
| 81 | ip->i_cformat = XFS_DINODE_FMT_EXTENTS; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 82 | memset(&ip->i_df, 0, sizeof(xfs_ifork_t)); |
| 83 | ip->i_flags = 0; |
| 84 | ip->i_delayed_blks = 0; |
Dave Chinner | f8d55aa052 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 85 | memset(&ip->i_d, 0, sizeof(ip->i_d)); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 86 | |
| 87 | return ip; |
| 88 | } |
| 89 | |
| 90 | STATIC void |
| 91 | xfs_inode_free_callback( |
| 92 | struct rcu_head *head) |
| 93 | { |
| 94 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 95 | struct xfs_inode *ip = XFS_I(inode); |
| 96 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 97 | switch (VFS_I(ip)->i_mode & S_IFMT) { |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 98 | case S_IFREG: |
| 99 | case S_IFDIR: |
| 100 | case S_IFLNK: |
| 101 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | if (ip->i_afp) |
| 106 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 107 | if (ip->i_cowfp) |
| 108 | xfs_idestroy_fork(ip, XFS_COW_FORK); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 109 | |
| 110 | if (ip->i_itemp) { |
| 111 | ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL)); |
| 112 | xfs_inode_item_destroy(ip); |
| 113 | ip->i_itemp = NULL; |
| 114 | } |
| 115 | |
Dave Chinner | 1f2dcfe | 2016-05-18 14:01:53 +1000 | [diff] [blame] | 116 | kmem_zone_free(xfs_inode_zone, ip); |
| 117 | } |
| 118 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 119 | static void |
| 120 | __xfs_inode_free( |
| 121 | struct xfs_inode *ip) |
| 122 | { |
| 123 | /* asserts to verify all state is correct here */ |
| 124 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 125 | XFS_STATS_DEC(ip->i_mount, vn_active); |
| 126 | |
| 127 | call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback); |
| 128 | } |
| 129 | |
Dave Chinner | 1f2dcfe | 2016-05-18 14:01:53 +1000 | [diff] [blame] | 130 | void |
| 131 | xfs_inode_free( |
| 132 | struct xfs_inode *ip) |
| 133 | { |
Brian Foster | b49ef75 | 2017-01-09 16:38:38 +0100 | [diff] [blame] | 134 | ASSERT(!xfs_isiflocked(ip)); |
| 135 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 136 | /* |
| 137 | * Because we use RCU freeing we need to ensure the inode always |
| 138 | * appears to be reclaimed with an invalid inode number when in the |
| 139 | * free state. The ip->i_flags_lock provides the barrier against lookup |
| 140 | * races. |
| 141 | */ |
| 142 | spin_lock(&ip->i_flags_lock); |
| 143 | ip->i_flags = XFS_IRECLAIM; |
| 144 | ip->i_ino = 0; |
| 145 | spin_unlock(&ip->i_flags_lock); |
| 146 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 147 | __xfs_inode_free(ip); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 151 | * Queue a new inode reclaim pass if there are reclaimable inodes and there |
| 152 | * isn't a reclaim pass already in progress. By default it runs every 5s based |
| 153 | * on the xfs periodic sync default of 30s. Perhaps this should have it's own |
| 154 | * tunable, but that can be done if this method proves to be ineffective or too |
| 155 | * aggressive. |
| 156 | */ |
| 157 | static void |
| 158 | xfs_reclaim_work_queue( |
| 159 | struct xfs_mount *mp) |
| 160 | { |
| 161 | |
| 162 | rcu_read_lock(); |
| 163 | if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) { |
| 164 | queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work, |
| 165 | msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10)); |
| 166 | } |
| 167 | rcu_read_unlock(); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * This is a fast pass over the inode cache to try to get reclaim moving on as |
| 172 | * many inodes as possible in a short period of time. It kicks itself every few |
| 173 | * seconds, as well as being kicked by the inode cache shrinker when memory |
| 174 | * goes low. It scans as quickly as possible avoiding locked inodes or those |
| 175 | * already being flushed, and once done schedules a future pass. |
| 176 | */ |
| 177 | void |
| 178 | xfs_reclaim_worker( |
| 179 | struct work_struct *work) |
| 180 | { |
| 181 | struct xfs_mount *mp = container_of(to_delayed_work(work), |
| 182 | struct xfs_mount, m_reclaim_work); |
| 183 | |
| 184 | xfs_reclaim_inodes(mp, SYNC_TRYLOCK); |
| 185 | xfs_reclaim_work_queue(mp); |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | xfs_perag_set_reclaim_tag( |
| 190 | struct xfs_perag *pag) |
| 191 | { |
| 192 | struct xfs_mount *mp = pag->pag_mount; |
| 193 | |
Brian Foster | 85ab1b2 | 2017-06-08 08:23:07 -0700 | [diff] [blame] | 194 | lockdep_assert_held(&pag->pag_ici_lock); |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 195 | if (pag->pag_ici_reclaimable++) |
| 196 | return; |
| 197 | |
| 198 | /* propagate the reclaim tag up into the perag radix tree */ |
| 199 | spin_lock(&mp->m_perag_lock); |
| 200 | radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno, |
| 201 | XFS_ICI_RECLAIM_TAG); |
| 202 | spin_unlock(&mp->m_perag_lock); |
| 203 | |
| 204 | /* schedule periodic background inode reclaim */ |
| 205 | xfs_reclaim_work_queue(mp); |
| 206 | |
| 207 | trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_); |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | xfs_perag_clear_reclaim_tag( |
| 212 | struct xfs_perag *pag) |
| 213 | { |
| 214 | struct xfs_mount *mp = pag->pag_mount; |
| 215 | |
Brian Foster | 85ab1b2 | 2017-06-08 08:23:07 -0700 | [diff] [blame] | 216 | lockdep_assert_held(&pag->pag_ici_lock); |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 217 | if (--pag->pag_ici_reclaimable) |
| 218 | return; |
| 219 | |
| 220 | /* clear the reclaim tag from the perag radix tree */ |
| 221 | spin_lock(&mp->m_perag_lock); |
| 222 | radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno, |
| 223 | XFS_ICI_RECLAIM_TAG); |
| 224 | spin_unlock(&mp->m_perag_lock); |
| 225 | trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /* |
| 230 | * We set the inode flag atomically with the radix tree tag. |
| 231 | * Once we get tag lookups on the radix tree, this inode flag |
| 232 | * can go away. |
| 233 | */ |
| 234 | void |
| 235 | xfs_inode_set_reclaim_tag( |
| 236 | struct xfs_inode *ip) |
| 237 | { |
| 238 | struct xfs_mount *mp = ip->i_mount; |
| 239 | struct xfs_perag *pag; |
| 240 | |
| 241 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
| 242 | spin_lock(&pag->pag_ici_lock); |
| 243 | spin_lock(&ip->i_flags_lock); |
| 244 | |
| 245 | radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino), |
| 246 | XFS_ICI_RECLAIM_TAG); |
| 247 | xfs_perag_set_reclaim_tag(pag); |
| 248 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); |
| 249 | |
| 250 | spin_unlock(&ip->i_flags_lock); |
| 251 | spin_unlock(&pag->pag_ici_lock); |
| 252 | xfs_perag_put(pag); |
| 253 | } |
| 254 | |
| 255 | STATIC void |
| 256 | xfs_inode_clear_reclaim_tag( |
| 257 | struct xfs_perag *pag, |
| 258 | xfs_ino_t ino) |
| 259 | { |
| 260 | radix_tree_tag_clear(&pag->pag_ici_root, |
| 261 | XFS_INO_TO_AGINO(pag->pag_mount, ino), |
| 262 | XFS_ICI_RECLAIM_TAG); |
| 263 | xfs_perag_clear_reclaim_tag(pag); |
| 264 | } |
| 265 | |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 266 | static void |
| 267 | xfs_inew_wait( |
| 268 | struct xfs_inode *ip) |
| 269 | { |
| 270 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT); |
| 271 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT); |
| 272 | |
| 273 | do { |
| 274 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 275 | if (!xfs_iflags_test(ip, XFS_INEW)) |
| 276 | break; |
| 277 | schedule(); |
| 278 | } while (true); |
| 279 | finish_wait(wq, &wait.wait); |
| 280 | } |
| 281 | |
Dave Chinner | ad438c4 | 2016-05-18 14:20:08 +1000 | [diff] [blame] | 282 | /* |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 283 | * When we recycle a reclaimable inode, we need to re-initialise the VFS inode |
| 284 | * part of the structure. This is made more complex by the fact we store |
| 285 | * 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] | 286 | * overwrite the values unconditionally. Hence we save the parameters we |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 287 | * need to retain across reinitialisation, and rewrite them into the VFS inode |
Dave Chinner | 83e06f2 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 288 | * after reinitialisation even if it fails. |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 289 | */ |
| 290 | static int |
| 291 | xfs_reinit_inode( |
| 292 | struct xfs_mount *mp, |
| 293 | struct inode *inode) |
| 294 | { |
| 295 | int error; |
Dave Chinner | 54d7b5c | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 296 | uint32_t nlink = inode->i_nlink; |
Dave Chinner | 9e9a267 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 297 | uint32_t generation = inode->i_generation; |
Dave Chinner | 83e06f2 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 298 | uint64_t version = inode->i_version; |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 299 | umode_t mode = inode->i_mode; |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 300 | |
| 301 | error = inode_init_always(mp->m_super, inode); |
| 302 | |
Dave Chinner | 54d7b5c | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 303 | set_nlink(inode, nlink); |
Dave Chinner | 9e9a267 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 304 | inode->i_generation = generation; |
Dave Chinner | 83e06f2 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 305 | inode->i_version = version; |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 306 | inode->i_mode = mode; |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 307 | return error; |
| 308 | } |
| 309 | |
| 310 | /* |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 311 | * Check the validity of the inode we just found it the cache |
| 312 | */ |
| 313 | static int |
| 314 | xfs_iget_cache_hit( |
| 315 | struct xfs_perag *pag, |
| 316 | struct xfs_inode *ip, |
| 317 | xfs_ino_t ino, |
| 318 | int flags, |
| 319 | int lock_flags) __releases(RCU) |
| 320 | { |
| 321 | struct inode *inode = VFS_I(ip); |
| 322 | struct xfs_mount *mp = ip->i_mount; |
| 323 | int error; |
| 324 | |
| 325 | /* |
| 326 | * check for re-use of an inode within an RCU grace period due to the |
| 327 | * radix tree nodes not being updated yet. We monitor for this by |
| 328 | * setting the inode number to zero before freeing the inode structure. |
| 329 | * If the inode has been reallocated and set up, then the inode number |
| 330 | * will not match, so check for that, too. |
| 331 | */ |
| 332 | spin_lock(&ip->i_flags_lock); |
| 333 | if (ip->i_ino != ino) { |
| 334 | trace_xfs_iget_skip(ip); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 335 | XFS_STATS_INC(mp, xs_ig_frecycle); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 336 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 337 | goto out_error; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /* |
| 342 | * If we are racing with another cache hit that is currently |
| 343 | * instantiating this inode or currently recycling it out of |
| 344 | * reclaimabe state, wait for the initialisation to complete |
| 345 | * before continuing. |
| 346 | * |
| 347 | * XXX(hch): eventually we should do something equivalent to |
| 348 | * wait_on_inode to wait for these flags to be cleared |
| 349 | * instead of polling for it. |
| 350 | */ |
| 351 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { |
| 352 | trace_xfs_iget_skip(ip); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 353 | XFS_STATS_INC(mp, xs_ig_frecycle); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 354 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 355 | goto out_error; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * If lookup is racing with unlink return an error immediately. |
| 360 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 361 | if (VFS_I(ip)->i_mode == 0 && !(flags & XFS_IGET_CREATE)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 362 | error = -ENOENT; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 363 | goto out_error; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. |
| 368 | * Need to carefully get it back into useable state. |
| 369 | */ |
| 370 | if (ip->i_flags & XFS_IRECLAIMABLE) { |
| 371 | trace_xfs_iget_reclaim(ip); |
| 372 | |
| 373 | /* |
| 374 | * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode |
| 375 | * from stomping over us while we recycle the inode. We can't |
| 376 | * clear the radix tree reclaimable tag yet as it requires |
| 377 | * pag_ici_lock to be held exclusive. |
| 378 | */ |
| 379 | ip->i_flags |= XFS_IRECLAIM; |
| 380 | |
| 381 | spin_unlock(&ip->i_flags_lock); |
| 382 | rcu_read_unlock(); |
| 383 | |
Dave Chinner | 5099747 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 384 | error = xfs_reinit_inode(mp, inode); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 385 | if (error) { |
Brian Foster | e86b616 | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 386 | bool wake; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 387 | /* |
| 388 | * Re-initializing the inode failed, and we are in deep |
| 389 | * trouble. Try to re-add it to the reclaim list. |
| 390 | */ |
| 391 | rcu_read_lock(); |
| 392 | spin_lock(&ip->i_flags_lock); |
Brian Foster | e86b616 | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 393 | wake = !!__xfs_iflags_test(ip, XFS_INEW); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 394 | ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM); |
Brian Foster | e86b616 | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 395 | if (wake) |
| 396 | wake_up_bit(&ip->i_flags, __XFS_INEW_BIT); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 397 | ASSERT(ip->i_flags & XFS_IRECLAIMABLE); |
| 398 | trace_xfs_iget_reclaim_fail(ip); |
| 399 | goto out_error; |
| 400 | } |
| 401 | |
| 402 | spin_lock(&pag->pag_ici_lock); |
| 403 | spin_lock(&ip->i_flags_lock); |
| 404 | |
| 405 | /* |
| 406 | * Clear the per-lifetime state in the inode as we are now |
| 407 | * effectively a new inode and need to return to the initial |
| 408 | * state before reuse occurs. |
| 409 | */ |
| 410 | ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS; |
| 411 | ip->i_flags |= XFS_INEW; |
Dave Chinner | 545c088 | 2016-05-18 14:11:41 +1000 | [diff] [blame] | 412 | xfs_inode_clear_reclaim_tag(pag, ip->i_ino); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 413 | inode->i_state = I_NEW; |
| 414 | |
| 415 | ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock)); |
| 416 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
| 417 | |
| 418 | spin_unlock(&ip->i_flags_lock); |
| 419 | spin_unlock(&pag->pag_ici_lock); |
| 420 | } else { |
| 421 | /* If the VFS inode is being torn down, pause and try again. */ |
| 422 | if (!igrab(inode)) { |
| 423 | trace_xfs_iget_skip(ip); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 424 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 425 | goto out_error; |
| 426 | } |
| 427 | |
| 428 | /* We've got a live one. */ |
| 429 | spin_unlock(&ip->i_flags_lock); |
| 430 | rcu_read_unlock(); |
| 431 | trace_xfs_iget_hit(ip); |
| 432 | } |
| 433 | |
| 434 | if (lock_flags != 0) |
| 435 | xfs_ilock(ip, lock_flags); |
| 436 | |
| 437 | xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 438 | XFS_STATS_INC(mp, xs_ig_found); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 439 | |
| 440 | return 0; |
| 441 | |
| 442 | out_error: |
| 443 | spin_unlock(&ip->i_flags_lock); |
| 444 | rcu_read_unlock(); |
| 445 | return error; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | static int |
| 450 | xfs_iget_cache_miss( |
| 451 | struct xfs_mount *mp, |
| 452 | struct xfs_perag *pag, |
| 453 | xfs_trans_t *tp, |
| 454 | xfs_ino_t ino, |
| 455 | struct xfs_inode **ipp, |
| 456 | int flags, |
| 457 | int lock_flags) |
| 458 | { |
| 459 | struct xfs_inode *ip; |
| 460 | int error; |
| 461 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
| 462 | int iflags; |
| 463 | |
| 464 | ip = xfs_inode_alloc(mp, ino); |
| 465 | if (!ip) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 466 | return -ENOMEM; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 467 | |
| 468 | error = xfs_iread(mp, tp, ip, flags); |
| 469 | if (error) |
| 470 | goto out_destroy; |
| 471 | |
| 472 | trace_xfs_iget_miss(ip); |
| 473 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 474 | if ((VFS_I(ip)->i_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 475 | error = -ENOENT; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 476 | goto out_destroy; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Preload the radix tree so we can insert safely under the |
| 481 | * write spinlock. Note that we cannot sleep inside the preload |
| 482 | * region. Since we can be called from transaction context, don't |
| 483 | * recurse into the file system. |
| 484 | */ |
| 485 | if (radix_tree_preload(GFP_NOFS)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 486 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 487 | goto out_destroy; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Because the inode hasn't been added to the radix-tree yet it can't |
| 492 | * be found by another thread, so we can do the non-sleeping lock here. |
| 493 | */ |
| 494 | if (lock_flags) { |
| 495 | if (!xfs_ilock_nowait(ip, lock_flags)) |
| 496 | BUG(); |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * These values must be set before inserting the inode into the radix |
| 501 | * tree as the moment it is inserted a concurrent lookup (allowed by the |
| 502 | * RCU locking mechanism) can find it and that lookup must see that this |
| 503 | * is an inode currently under construction (i.e. that XFS_INEW is set). |
| 504 | * The ip->i_flags_lock that protects the XFS_INEW flag forms the |
| 505 | * memory barrier that ensures this detection works correctly at lookup |
| 506 | * time. |
| 507 | */ |
| 508 | iflags = XFS_INEW; |
| 509 | if (flags & XFS_IGET_DONTCACHE) |
| 510 | iflags |= XFS_IDONTCACHE; |
Chandra Seetharaman | 113a568 | 2013-06-27 17:25:07 -0500 | [diff] [blame] | 511 | ip->i_udquot = NULL; |
| 512 | ip->i_gdquot = NULL; |
Chandra Seetharaman | 92f8ff7 | 2013-07-11 00:00:40 -0500 | [diff] [blame] | 513 | ip->i_pdquot = NULL; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 514 | xfs_iflags_set(ip, iflags); |
| 515 | |
| 516 | /* insert the new inode */ |
| 517 | spin_lock(&pag->pag_ici_lock); |
| 518 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
| 519 | if (unlikely(error)) { |
| 520 | WARN_ON(error != -EEXIST); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 521 | XFS_STATS_INC(mp, xs_ig_dup); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 522 | error = -EAGAIN; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 523 | goto out_preload_end; |
| 524 | } |
| 525 | spin_unlock(&pag->pag_ici_lock); |
| 526 | radix_tree_preload_end(); |
| 527 | |
| 528 | *ipp = ip; |
| 529 | return 0; |
| 530 | |
| 531 | out_preload_end: |
| 532 | spin_unlock(&pag->pag_ici_lock); |
| 533 | radix_tree_preload_end(); |
| 534 | if (lock_flags) |
| 535 | xfs_iunlock(ip, lock_flags); |
| 536 | out_destroy: |
| 537 | __destroy_inode(VFS_I(ip)); |
| 538 | xfs_inode_free(ip); |
| 539 | return error; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Look up an inode by number in the given file system. |
| 544 | * The inode is looked up in the cache held in each AG. |
| 545 | * If the inode is found in the cache, initialise the vfs inode |
| 546 | * if necessary. |
| 547 | * |
| 548 | * If it is not in core, read it in from the file system's device, |
| 549 | * add it to the cache and initialise the vfs inode. |
| 550 | * |
| 551 | * The inode is locked according to the value of the lock_flags parameter. |
| 552 | * This flag parameter indicates how and if the inode's IO lock and inode lock |
| 553 | * should be taken. |
| 554 | * |
| 555 | * mp -- the mount point structure for the current file system. It points |
| 556 | * to the inode hash table. |
| 557 | * tp -- a pointer to the current transaction if there is one. This is |
| 558 | * simply passed through to the xfs_iread() call. |
| 559 | * ino -- the number of the inode desired. This is the unique identifier |
| 560 | * within the file system for the inode being requested. |
| 561 | * lock_flags -- flags indicating how to lock the inode. See the comment |
| 562 | * for xfs_ilock() for a list of valid values. |
| 563 | */ |
| 564 | int |
| 565 | xfs_iget( |
| 566 | xfs_mount_t *mp, |
| 567 | xfs_trans_t *tp, |
| 568 | xfs_ino_t ino, |
| 569 | uint flags, |
| 570 | uint lock_flags, |
| 571 | xfs_inode_t **ipp) |
| 572 | { |
| 573 | xfs_inode_t *ip; |
| 574 | int error; |
| 575 | xfs_perag_t *pag; |
| 576 | xfs_agino_t agino; |
| 577 | |
| 578 | /* |
| 579 | * xfs_reclaim_inode() uses the ILOCK to ensure an inode |
| 580 | * doesn't get freed while it's being referenced during a |
| 581 | * radix tree traversal here. It assumes this function |
| 582 | * aqcuires only the ILOCK (and therefore it has no need to |
| 583 | * involve the IOLOCK in this synchronization). |
| 584 | */ |
| 585 | ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0); |
| 586 | |
| 587 | /* reject inode numbers outside existing AGs */ |
| 588 | 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] | 589 | return -EINVAL; |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 590 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 591 | XFS_STATS_INC(mp, xs_ig_attempts); |
Lucas Stach | 8774cf8 | 2015-08-28 14:50:56 +1000 | [diff] [blame] | 592 | |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 593 | /* get the perag structure and ensure that it's inode capable */ |
| 594 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); |
| 595 | agino = XFS_INO_TO_AGINO(mp, ino); |
| 596 | |
| 597 | again: |
| 598 | error = 0; |
| 599 | rcu_read_lock(); |
| 600 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); |
| 601 | |
| 602 | if (ip) { |
| 603 | error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags); |
| 604 | if (error) |
| 605 | goto out_error_or_again; |
| 606 | } else { |
| 607 | rcu_read_unlock(); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 608 | XFS_STATS_INC(mp, xs_ig_missed); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 609 | |
| 610 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, |
| 611 | flags, lock_flags); |
| 612 | if (error) |
| 613 | goto out_error_or_again; |
| 614 | } |
| 615 | xfs_perag_put(pag); |
| 616 | |
| 617 | *ipp = ip; |
| 618 | |
| 619 | /* |
Dave Chinner | 58c9047 | 2015-02-23 22:38:08 +1100 | [diff] [blame] | 620 | * 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] | 621 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
| 622 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 623 | 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] | 624 | xfs_setup_existing_inode(ip); |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 625 | return 0; |
| 626 | |
| 627 | out_error_or_again: |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 628 | if (error == -EAGAIN) { |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 629 | delay(1); |
| 630 | goto again; |
| 631 | } |
| 632 | xfs_perag_put(pag); |
| 633 | return error; |
| 634 | } |
| 635 | |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 636 | /* |
| 637 | * The inode lookup is done in batches to keep the amount of lock traffic and |
| 638 | * radix tree lookups to a minimum. The batch size is a trade off between |
| 639 | * lookup reduction and stack usage. This is in the reclaim path, so we can't |
| 640 | * be too greedy. |
| 641 | */ |
| 642 | #define XFS_LOOKUP_BATCH 32 |
| 643 | |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 644 | STATIC int |
| 645 | xfs_inode_ag_walk_grab( |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 646 | struct xfs_inode *ip, |
| 647 | int flags) |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 648 | { |
| 649 | struct inode *inode = VFS_I(ip); |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 650 | bool newinos = !!(flags & XFS_AGITER_INEW_WAIT); |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 651 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 652 | ASSERT(rcu_read_lock_held()); |
| 653 | |
| 654 | /* |
| 655 | * check for stale RCU freed inode |
| 656 | * |
| 657 | * If the inode has been reallocated, it doesn't matter if it's not in |
| 658 | * the AG we are walking - we are walking for writeback, so if it |
| 659 | * passes all the "valid inode" checks and is dirty, then we'll write |
| 660 | * it back anyway. If it has been reallocated and still being |
| 661 | * initialised, the XFS_INEW check below will catch it. |
| 662 | */ |
| 663 | spin_lock(&ip->i_flags_lock); |
| 664 | if (!ip->i_ino) |
| 665 | goto out_unlock_noent; |
| 666 | |
| 667 | /* avoid new or reclaimable inodes. Leave for reclaim code to flush */ |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 668 | if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) || |
| 669 | __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM)) |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 670 | goto out_unlock_noent; |
| 671 | spin_unlock(&ip->i_flags_lock); |
| 672 | |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 673 | /* nothing to sync during shutdown */ |
| 674 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 675 | return -EFSCORRUPTED; |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 676 | |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 677 | /* If we can't grab the inode, it must on it's way to reclaim. */ |
| 678 | if (!igrab(inode)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 679 | return -ENOENT; |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 680 | |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 681 | /* inode is valid */ |
| 682 | return 0; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 683 | |
| 684 | out_unlock_noent: |
| 685 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 686 | return -ENOENT; |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 687 | } |
| 688 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 689 | STATIC int |
| 690 | xfs_inode_ag_walk( |
| 691 | struct xfs_mount *mp, |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 692 | struct xfs_perag *pag, |
Eric Sandeen | e009400 | 2014-04-14 19:04:19 +1000 | [diff] [blame] | 693 | int (*execute)(struct xfs_inode *ip, int flags, |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 694 | void *args), |
| 695 | int flags, |
| 696 | void *args, |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 697 | int tag, |
| 698 | int iter_flags) |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 699 | { |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 700 | uint32_t first_index; |
| 701 | int last_error = 0; |
| 702 | int skipped; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 703 | int done; |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 704 | int nr_found; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 705 | |
| 706 | restart: |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 707 | done = 0; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 708 | skipped = 0; |
| 709 | first_index = 0; |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 710 | nr_found = 0; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 711 | do { |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 712 | struct xfs_inode *batch[XFS_LOOKUP_BATCH]; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 713 | int error = 0; |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 714 | int i; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 715 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 716 | rcu_read_lock(); |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 717 | |
| 718 | if (tag == -1) |
| 719 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 720 | (void **)batch, first_index, |
| 721 | XFS_LOOKUP_BATCH); |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 722 | else |
| 723 | nr_found = radix_tree_gang_lookup_tag( |
| 724 | &pag->pag_ici_root, |
| 725 | (void **) batch, first_index, |
| 726 | XFS_LOOKUP_BATCH, tag); |
| 727 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 728 | if (!nr_found) { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 729 | rcu_read_unlock(); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 730 | break; |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 731 | } |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 732 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 733 | /* |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 734 | * Grab the inodes before we drop the lock. if we found |
| 735 | * nothing, nr == 0 and the loop will be skipped. |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 736 | */ |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 737 | for (i = 0; i < nr_found; i++) { |
| 738 | struct xfs_inode *ip = batch[i]; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 739 | |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 740 | if (done || xfs_inode_ag_walk_grab(ip, iter_flags)) |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 741 | batch[i] = NULL; |
| 742 | |
| 743 | /* |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 744 | * Update the index for the next lookup. Catch |
| 745 | * overflows into the next AG range which can occur if |
| 746 | * we have inodes in the last block of the AG and we |
| 747 | * are currently pointing to the last inode. |
| 748 | * |
| 749 | * Because we may see inodes that are from the wrong AG |
| 750 | * due to RCU freeing and reallocation, only update the |
| 751 | * index if it lies in this AG. It was a race that lead |
| 752 | * us to see this inode, so another lookup from the |
| 753 | * same index will not find it again. |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 754 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 755 | if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno) |
| 756 | continue; |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 757 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); |
| 758 | if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) |
| 759 | done = 1; |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 760 | } |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 761 | |
| 762 | /* unlock now we've grabbed the inodes. */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 763 | rcu_read_unlock(); |
Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 764 | |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 765 | for (i = 0; i < nr_found; i++) { |
| 766 | if (!batch[i]) |
| 767 | continue; |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 768 | if ((iter_flags & XFS_AGITER_INEW_WAIT) && |
| 769 | xfs_iflags_test(batch[i], XFS_INEW)) |
| 770 | xfs_inew_wait(batch[i]); |
Eric Sandeen | e009400 | 2014-04-14 19:04:19 +1000 | [diff] [blame] | 771 | error = execute(batch[i], flags, args); |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 772 | IRELE(batch[i]); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 773 | if (error == -EAGAIN) { |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 774 | skipped++; |
| 775 | continue; |
| 776 | } |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 777 | if (error && last_error != -EFSCORRUPTED) |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 778 | last_error = error; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 779 | } |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 780 | |
| 781 | /* bail out if the filesystem is corrupted. */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 782 | if (error == -EFSCORRUPTED) |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 783 | break; |
| 784 | |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 785 | cond_resched(); |
| 786 | |
Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 787 | } while (nr_found && !done); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 788 | |
| 789 | if (skipped) { |
| 790 | delay(1); |
| 791 | goto restart; |
| 792 | } |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 793 | return last_error; |
| 794 | } |
| 795 | |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 796 | /* |
| 797 | * Background scanning to trim post-EOF preallocated space. This is queued |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 798 | * based on the 'speculative_prealloc_lifetime' tunable (5m by default). |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 799 | */ |
Brian Foster | fa5a4f5 | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 800 | void |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 801 | xfs_queue_eofblocks( |
| 802 | struct xfs_mount *mp) |
| 803 | { |
| 804 | rcu_read_lock(); |
| 805 | if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG)) |
| 806 | queue_delayed_work(mp->m_eofblocks_workqueue, |
| 807 | &mp->m_eofblocks_work, |
| 808 | msecs_to_jiffies(xfs_eofb_secs * 1000)); |
| 809 | rcu_read_unlock(); |
| 810 | } |
| 811 | |
| 812 | void |
| 813 | xfs_eofblocks_worker( |
| 814 | struct work_struct *work) |
| 815 | { |
| 816 | struct xfs_mount *mp = container_of(to_delayed_work(work), |
| 817 | struct xfs_mount, m_eofblocks_work); |
| 818 | xfs_icache_free_eofblocks(mp, NULL); |
| 819 | xfs_queue_eofblocks(mp); |
| 820 | } |
| 821 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 822 | /* |
| 823 | * Background scanning to trim preallocated CoW space. This is queued |
| 824 | * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default). |
| 825 | * (We'll just piggyback on the post-EOF prealloc space workqueue.) |
| 826 | */ |
| 827 | STATIC void |
| 828 | xfs_queue_cowblocks( |
| 829 | struct xfs_mount *mp) |
| 830 | { |
| 831 | rcu_read_lock(); |
| 832 | if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG)) |
| 833 | queue_delayed_work(mp->m_eofblocks_workqueue, |
| 834 | &mp->m_cowblocks_work, |
| 835 | msecs_to_jiffies(xfs_cowb_secs * 1000)); |
| 836 | rcu_read_unlock(); |
| 837 | } |
| 838 | |
| 839 | void |
| 840 | xfs_cowblocks_worker( |
| 841 | struct work_struct *work) |
| 842 | { |
| 843 | struct xfs_mount *mp = container_of(to_delayed_work(work), |
| 844 | struct xfs_mount, m_cowblocks_work); |
| 845 | xfs_icache_free_cowblocks(mp, NULL); |
| 846 | xfs_queue_cowblocks(mp); |
| 847 | } |
| 848 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 849 | int |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 850 | xfs_inode_ag_iterator_flags( |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 851 | struct xfs_mount *mp, |
Eric Sandeen | e009400 | 2014-04-14 19:04:19 +1000 | [diff] [blame] | 852 | int (*execute)(struct xfs_inode *ip, int flags, |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 853 | void *args), |
| 854 | int flags, |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 855 | void *args, |
| 856 | int iter_flags) |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 857 | { |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 858 | struct xfs_perag *pag; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 859 | int error = 0; |
| 860 | int last_error = 0; |
| 861 | xfs_agnumber_t ag; |
| 862 | |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 863 | ag = 0; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 864 | while ((pag = xfs_perag_get(mp, ag))) { |
| 865 | ag = pag->pag_agno + 1; |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 866 | error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1, |
| 867 | iter_flags); |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 868 | xfs_perag_put(pag); |
| 869 | if (error) { |
| 870 | last_error = error; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 871 | if (error == -EFSCORRUPTED) |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 872 | break; |
| 873 | } |
| 874 | } |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 875 | return last_error; |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | int |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 879 | xfs_inode_ag_iterator( |
| 880 | struct xfs_mount *mp, |
| 881 | int (*execute)(struct xfs_inode *ip, int flags, |
| 882 | void *args), |
| 883 | int flags, |
| 884 | void *args) |
| 885 | { |
| 886 | return xfs_inode_ag_iterator_flags(mp, execute, flags, args, 0); |
| 887 | } |
| 888 | |
| 889 | int |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 890 | xfs_inode_ag_iterator_tag( |
| 891 | struct xfs_mount *mp, |
Eric Sandeen | e009400 | 2014-04-14 19:04:19 +1000 | [diff] [blame] | 892 | int (*execute)(struct xfs_inode *ip, int flags, |
Brian Foster | a454f74 | 2012-11-06 09:50:39 -0500 | [diff] [blame] | 893 | void *args), |
| 894 | int flags, |
| 895 | void *args, |
| 896 | int tag) |
| 897 | { |
| 898 | struct xfs_perag *pag; |
| 899 | int error = 0; |
| 900 | int last_error = 0; |
| 901 | xfs_agnumber_t ag; |
| 902 | |
| 903 | ag = 0; |
| 904 | while ((pag = xfs_perag_get_tag(mp, ag, tag))) { |
| 905 | ag = pag->pag_agno + 1; |
Brian Foster | 2ea882d | 2017-04-26 08:30:39 -0700 | [diff] [blame] | 906 | error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag, |
| 907 | 0); |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 908 | xfs_perag_put(pag); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 909 | if (error) { |
| 910 | last_error = error; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 911 | if (error == -EFSCORRUPTED) |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 912 | break; |
| 913 | } |
| 914 | } |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 915 | return last_error; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 916 | } |
| 917 | |
David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 918 | /* |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 919 | * Grab the inode for reclaim exclusively. |
| 920 | * Return 0 if we grabbed it, non-zero otherwise. |
| 921 | */ |
| 922 | STATIC int |
| 923 | xfs_reclaim_inode_grab( |
| 924 | struct xfs_inode *ip, |
| 925 | int flags) |
| 926 | { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 927 | ASSERT(rcu_read_lock_held()); |
| 928 | |
| 929 | /* quick check for stale RCU freed inode */ |
| 930 | if (!ip->i_ino) |
| 931 | return 1; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 932 | |
| 933 | /* |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 934 | * If we are asked for non-blocking operation, do unlocked checks to |
| 935 | * see if the inode already is being flushed or in reclaim to avoid |
| 936 | * lock traffic. |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 937 | */ |
| 938 | if ((flags & SYNC_TRYLOCK) && |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 939 | __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM)) |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 940 | return 1; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 941 | |
| 942 | /* |
| 943 | * The radix tree lock here protects a thread in xfs_iget from racing |
| 944 | * with us starting reclaim on the inode. Once we have the |
| 945 | * XFS_IRECLAIM flag set it will not touch us. |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 946 | * |
| 947 | * Due to RCU lookup, we may find inodes that have been freed and only |
| 948 | * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that |
| 949 | * aren't candidates for reclaim at all, so we must check the |
| 950 | * XFS_IRECLAIMABLE is set first before proceeding to reclaim. |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 951 | */ |
| 952 | spin_lock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 953 | if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) || |
| 954 | __xfs_iflags_test(ip, XFS_IRECLAIM)) { |
| 955 | /* not a reclaim candidate. */ |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 956 | spin_unlock(&ip->i_flags_lock); |
| 957 | return 1; |
| 958 | } |
| 959 | __xfs_iflags_set(ip, XFS_IRECLAIM); |
| 960 | spin_unlock(&ip->i_flags_lock); |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | /* |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 965 | * Inodes in different states need to be treated differently. The following |
| 966 | * table lists the inode states and the reclaim actions necessary: |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 967 | * |
| 968 | * inode state iflush ret required action |
| 969 | * --------------- ---------- --------------- |
| 970 | * bad - reclaim |
| 971 | * shutdown EIO unpin and reclaim |
| 972 | * clean, unpinned 0 reclaim |
| 973 | * stale, unpinned 0 reclaim |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 974 | * clean, pinned(*) 0 requeue |
| 975 | * stale, pinned EAGAIN requeue |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 976 | * dirty, async - requeue |
| 977 | * dirty, sync 0 reclaim |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 978 | * |
| 979 | * (*) dgc: I don't think the clean, pinned state is possible but it gets |
| 980 | * handled anyway given the order of checks implemented. |
| 981 | * |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 982 | * Also, because we get the flush lock first, we know that any inode that has |
| 983 | * been flushed delwri has had the flush completed by the time we check that |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 984 | * the inode is clean. |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 985 | * |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 986 | * Note that because the inode is flushed delayed write by AIL pushing, the |
| 987 | * flush lock may already be held here and waiting on it can result in very |
| 988 | * long latencies. Hence for sync reclaims, where we wait on the flush lock, |
| 989 | * the caller should push the AIL first before trying to reclaim inodes to |
| 990 | * minimise the amount of time spent waiting. For background relaim, we only |
| 991 | * bother to reclaim clean inodes anyway. |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 992 | * |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 993 | * Hence the order of actions after gaining the locks should be: |
| 994 | * bad => reclaim |
| 995 | * shutdown => unpin and reclaim |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 996 | * pinned, async => requeue |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 997 | * pinned, sync => unpin |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 998 | * stale => reclaim |
| 999 | * clean => reclaim |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 1000 | * dirty, async => requeue |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 1001 | * dirty, sync => flush, wait and reclaim |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1002 | */ |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 1003 | STATIC int |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 1004 | xfs_reclaim_inode( |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 1005 | struct xfs_inode *ip, |
| 1006 | struct xfs_perag *pag, |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 1007 | int sync_mode) |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 1008 | { |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1009 | struct xfs_buf *bp = NULL; |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 1010 | xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */ |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1011 | int error; |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1012 | |
Dave Chinner | 1bfd8d0 | 2011-03-26 09:13:55 +1100 | [diff] [blame] | 1013 | restart: |
| 1014 | error = 0; |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 1015 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 1016 | if (!xfs_iflock_nowait(ip)) { |
| 1017 | if (!(sync_mode & SYNC_WAIT)) |
| 1018 | goto out; |
| 1019 | xfs_iflock(ip); |
| 1020 | } |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 1021 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1022 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 1023 | xfs_iunpin_wait(ip); |
Brian Foster | b49ef75 | 2017-01-09 16:38:38 +0100 | [diff] [blame] | 1024 | /* xfs_iflush_abort() drops the flush lock */ |
Dave Chinner | 04913fd | 2012-04-23 15:58:41 +1000 | [diff] [blame] | 1025 | xfs_iflush_abort(ip, false); |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1026 | goto reclaim; |
| 1027 | } |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 1028 | if (xfs_ipincount(ip)) { |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 1029 | if (!(sync_mode & SYNC_WAIT)) |
| 1030 | goto out_ifunlock; |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1031 | xfs_iunpin_wait(ip); |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 1032 | } |
Brian Foster | b49ef75 | 2017-01-09 16:38:38 +0100 | [diff] [blame] | 1033 | if (xfs_iflags_test(ip, XFS_ISTALE) || xfs_inode_clean(ip)) { |
| 1034 | xfs_ifunlock(ip); |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1035 | goto reclaim; |
Brian Foster | b49ef75 | 2017-01-09 16:38:38 +0100 | [diff] [blame] | 1036 | } |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1037 | |
Dave Chinner | 1bfd8d0 | 2011-03-26 09:13:55 +1100 | [diff] [blame] | 1038 | /* |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 1039 | * Never flush out dirty data during non-blocking reclaim, as it would |
| 1040 | * just contend with AIL pushing trying to do the same job. |
| 1041 | */ |
| 1042 | if (!(sync_mode & SYNC_WAIT)) |
| 1043 | goto out_ifunlock; |
| 1044 | |
| 1045 | /* |
Dave Chinner | 1bfd8d0 | 2011-03-26 09:13:55 +1100 | [diff] [blame] | 1046 | * Now we have an inode that needs flushing. |
| 1047 | * |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1048 | * Note that xfs_iflush will never block on the inode buffer lock, as |
Dave Chinner | 1bfd8d0 | 2011-03-26 09:13:55 +1100 | [diff] [blame] | 1049 | * xfs_ifree_cluster() can lock the inode buffer before it locks the |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1050 | * ip->i_lock, and we are doing the exact opposite here. As a result, |
Christoph Hellwig | 475ee41 | 2012-07-03 12:21:22 -0400 | [diff] [blame] | 1051 | * doing a blocking xfs_imap_to_bp() to get the cluster buffer would |
| 1052 | * result in an ABBA deadlock with xfs_ifree_cluster(). |
Dave Chinner | 1bfd8d0 | 2011-03-26 09:13:55 +1100 | [diff] [blame] | 1053 | * |
| 1054 | * As xfs_ifree_cluser() must gather all inodes that are active in the |
| 1055 | * cache to mark them stale, if we hit this case we don't actually want |
| 1056 | * to do IO here - we want the inode marked stale so we can simply |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1057 | * reclaim it. Hence if we get an EAGAIN error here, just unlock the |
| 1058 | * inode, back off and try again. Hopefully the next pass through will |
| 1059 | * see the stale flag set on the inode. |
Dave Chinner | 1bfd8d0 | 2011-03-26 09:13:55 +1100 | [diff] [blame] | 1060 | */ |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1061 | error = xfs_iflush(ip, &bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1062 | if (error == -EAGAIN) { |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 1063 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1064 | /* backoff longer than in xfs_ifree_cluster */ |
| 1065 | delay(2); |
| 1066 | goto restart; |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 1067 | } |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 1068 | |
Christoph Hellwig | 4c46819 | 2012-04-23 15:58:36 +1000 | [diff] [blame] | 1069 | if (!error) { |
| 1070 | error = xfs_bwrite(bp); |
| 1071 | xfs_buf_relse(bp); |
| 1072 | } |
| 1073 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 1074 | reclaim: |
Brian Foster | b49ef75 | 2017-01-09 16:38:38 +0100 | [diff] [blame] | 1075 | ASSERT(!xfs_isiflocked(ip)); |
| 1076 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 1077 | /* |
| 1078 | * Because we use RCU freeing we need to ensure the inode always appears |
| 1079 | * to be reclaimed with an invalid inode number when in the free state. |
Brian Foster | b49ef75 | 2017-01-09 16:38:38 +0100 | [diff] [blame] | 1080 | * We do this as early as possible under the ILOCK so that |
Omar Sandoval | 81286ad | 2017-09-17 14:06:58 -0700 | [diff] [blame] | 1081 | * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to |
| 1082 | * detect races with us here. By doing this, we guarantee that once |
| 1083 | * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that |
| 1084 | * it will see either a valid inode that will serialise correctly, or it |
| 1085 | * will see an invalid inode that it can skip. |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 1086 | */ |
| 1087 | spin_lock(&ip->i_flags_lock); |
| 1088 | ip->i_flags = XFS_IRECLAIM; |
| 1089 | ip->i_ino = 0; |
| 1090 | spin_unlock(&ip->i_flags_lock); |
| 1091 | |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 1092 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1093 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1094 | XFS_STATS_INC(ip->i_mount, xs_ig_reclaims); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1095 | /* |
| 1096 | * Remove the inode from the per-AG radix tree. |
| 1097 | * |
| 1098 | * Because radix_tree_delete won't complain even if the item was never |
| 1099 | * added to the tree assert that it's been there before to catch |
| 1100 | * problems with the inode life time early on. |
| 1101 | */ |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 1102 | spin_lock(&pag->pag_ici_lock); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1103 | if (!radix_tree_delete(&pag->pag_ici_root, |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 1104 | XFS_INO_TO_AGINO(ip->i_mount, ino))) |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1105 | ASSERT(0); |
Dave Chinner | 545c088 | 2016-05-18 14:11:41 +1000 | [diff] [blame] | 1106 | xfs_perag_clear_reclaim_tag(pag); |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 1107 | spin_unlock(&pag->pag_ici_lock); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * Here we do an (almost) spurious inode lock in order to coordinate |
| 1111 | * with inode cache radix tree lookups. This is because the lookup |
| 1112 | * can reference the inodes in the cache without taking references. |
| 1113 | * |
| 1114 | * 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] | 1115 | * unlocked after the lookup before we go ahead and free it. |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1116 | */ |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 1117 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1118 | xfs_qm_dqdetach(ip); |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 1119 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 1120 | |
Dave Chinner | 8a17d7d | 2016-05-18 14:09:12 +1000 | [diff] [blame] | 1121 | __xfs_inode_free(ip); |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 1122 | return error; |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 1123 | |
| 1124 | out_ifunlock: |
| 1125 | xfs_ifunlock(ip); |
| 1126 | out: |
| 1127 | xfs_iflags_clear(ip, XFS_IRECLAIM); |
| 1128 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1129 | /* |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1130 | * We could return -EAGAIN here to make reclaim rescan the inode tree in |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 1131 | * a short while. However, this just burns CPU time scanning the tree |
Dave Chinner | 5889608 | 2012-10-08 21:56:05 +1100 | [diff] [blame] | 1132 | * waiting for IO to complete and the reclaim work never goes back to |
| 1133 | * the idle state. Instead, return 0 to let the next scheduled |
| 1134 | * background reclaim attempt to reclaim the inode again. |
Christoph Hellwig | 8a48088 | 2012-04-23 15:58:35 +1000 | [diff] [blame] | 1135 | */ |
| 1136 | return 0; |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 1137 | } |
| 1138 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1139 | /* |
| 1140 | * Walk the AGs and reclaim the inodes in them. Even if the filesystem is |
| 1141 | * corrupted, we still want to try to reclaim all the inodes. If we don't, |
| 1142 | * then a shut down during filesystem unmount reclaim walk leak all the |
| 1143 | * unreclaimed inodes. |
| 1144 | */ |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 1145 | STATIC int |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1146 | xfs_reclaim_inodes_ag( |
| 1147 | struct xfs_mount *mp, |
| 1148 | int flags, |
| 1149 | int *nr_to_scan) |
| 1150 | { |
| 1151 | struct xfs_perag *pag; |
| 1152 | int error = 0; |
| 1153 | int last_error = 0; |
| 1154 | xfs_agnumber_t ag; |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1155 | int trylock = flags & SYNC_TRYLOCK; |
| 1156 | int skipped; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1157 | |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1158 | restart: |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1159 | ag = 0; |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1160 | skipped = 0; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1161 | while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { |
| 1162 | unsigned long first_index = 0; |
| 1163 | int done = 0; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1164 | int nr_found = 0; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1165 | |
| 1166 | ag = pag->pag_agno + 1; |
| 1167 | |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1168 | if (trylock) { |
| 1169 | if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) { |
| 1170 | skipped++; |
Dave Chinner | f83282a | 2010-11-08 08:55:04 +0000 | [diff] [blame] | 1171 | xfs_perag_put(pag); |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1172 | continue; |
| 1173 | } |
| 1174 | first_index = pag->pag_ici_reclaim_cursor; |
| 1175 | } else |
| 1176 | mutex_lock(&pag->pag_ici_reclaim_lock); |
| 1177 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1178 | do { |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1179 | struct xfs_inode *batch[XFS_LOOKUP_BATCH]; |
| 1180 | int i; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1181 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1182 | rcu_read_lock(); |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1183 | nr_found = radix_tree_gang_lookup_tag( |
| 1184 | &pag->pag_ici_root, |
| 1185 | (void **)batch, first_index, |
| 1186 | XFS_LOOKUP_BATCH, |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1187 | XFS_ICI_RECLAIM_TAG); |
| 1188 | if (!nr_found) { |
Dave Chinner | b223221 | 2011-05-06 02:54:04 +0000 | [diff] [blame] | 1189 | done = 1; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1190 | rcu_read_unlock(); |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1191 | break; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1195 | * Grab the inodes before we drop the lock. if we found |
| 1196 | * nothing, nr == 0 and the loop will be skipped. |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1197 | */ |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1198 | for (i = 0; i < nr_found; i++) { |
| 1199 | struct xfs_inode *ip = batch[i]; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1200 | |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1201 | if (done || xfs_reclaim_inode_grab(ip, flags)) |
| 1202 | batch[i] = NULL; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1203 | |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1204 | /* |
| 1205 | * Update the index for the next lookup. Catch |
| 1206 | * overflows into the next AG range which can |
| 1207 | * occur if we have inodes in the last block of |
| 1208 | * the AG and we are currently pointing to the |
| 1209 | * last inode. |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1210 | * |
| 1211 | * Because we may see inodes that are from the |
| 1212 | * wrong AG due to RCU freeing and |
| 1213 | * reallocation, only update the index if it |
| 1214 | * lies in this AG. It was a race that lead us |
| 1215 | * to see this inode, so another lookup from |
| 1216 | * the same index will not find it again. |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1217 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1218 | if (XFS_INO_TO_AGNO(mp, ip->i_ino) != |
| 1219 | pag->pag_agno) |
| 1220 | continue; |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1221 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); |
| 1222 | if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) |
| 1223 | done = 1; |
| 1224 | } |
| 1225 | |
| 1226 | /* unlock now we've grabbed the inodes. */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 1227 | rcu_read_unlock(); |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1228 | |
| 1229 | for (i = 0; i < nr_found; i++) { |
| 1230 | if (!batch[i]) |
| 1231 | continue; |
| 1232 | error = xfs_reclaim_inode(batch[i], pag, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1233 | if (error && last_error != -EFSCORRUPTED) |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1234 | last_error = error; |
| 1235 | } |
| 1236 | |
| 1237 | *nr_to_scan -= XFS_LOOKUP_BATCH; |
| 1238 | |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1239 | cond_resched(); |
| 1240 | |
Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1241 | } while (nr_found && !done && *nr_to_scan > 0); |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1242 | |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1243 | if (trylock && !done) |
| 1244 | pag->pag_ici_reclaim_cursor = first_index; |
| 1245 | else |
| 1246 | pag->pag_ici_reclaim_cursor = 0; |
| 1247 | mutex_unlock(&pag->pag_ici_reclaim_lock); |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1248 | xfs_perag_put(pag); |
| 1249 | } |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1250 | |
| 1251 | /* |
| 1252 | * if we skipped any AG, and we still have scan count remaining, do |
| 1253 | * another pass this time using blocking reclaim semantics (i.e |
| 1254 | * waiting on the reclaim locks and ignoring the reclaim cursors). This |
| 1255 | * ensure that when we get more reclaimers than AGs we block rather |
| 1256 | * than spin trying to execute reclaim. |
| 1257 | */ |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1258 | if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) { |
Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 1259 | trylock = 0; |
| 1260 | goto restart; |
| 1261 | } |
Eric Sandeen | b474c7a | 2014-06-22 15:04:54 +1000 | [diff] [blame] | 1262 | return last_error; |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1263 | } |
| 1264 | |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 1265 | int |
David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 1266 | xfs_reclaim_inodes( |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 1267 | xfs_mount_t *mp, |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 1268 | int mode) |
| 1269 | { |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1270 | int nr_to_scan = INT_MAX; |
| 1271 | |
| 1272 | return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1276 | * Scan a certain number of inodes for reclaim. |
Dave Chinner | a7b339f | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 1277 | * |
| 1278 | * When called we make sure that there is a background (fast) inode reclaim in |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1279 | * progress, while we will throttle the speed of reclaim via doing synchronous |
Dave Chinner | a7b339f | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 1280 | * reclaim of inodes. That means if we come across dirty inodes, we wait for |
| 1281 | * them to be cleaned, which we hope will not be very long due to the |
| 1282 | * background walker having already kicked the IO off on those dirty inodes. |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1283 | */ |
Dave Chinner | 0a234c6 | 2013-08-28 10:17:57 +1000 | [diff] [blame] | 1284 | long |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1285 | xfs_reclaim_inodes_nr( |
| 1286 | struct xfs_mount *mp, |
| 1287 | int nr_to_scan) |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1288 | { |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1289 | /* kick background reclaimer and push the AIL */ |
Dave Chinner | 5889608 | 2012-10-08 21:56:05 +1100 | [diff] [blame] | 1290 | xfs_reclaim_work_queue(mp); |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1291 | xfs_ail_push_all(mp->m_ail); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1292 | |
Dave Chinner | 0a234c6 | 2013-08-28 10:17:57 +1000 | [diff] [blame] | 1293 | return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan); |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1294 | } |
Dave Chinner | a7b339f | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 1295 | |
Dave Chinner | 8daaa83 | 2011-07-08 14:14:46 +1000 | [diff] [blame] | 1296 | /* |
| 1297 | * Return the number of reclaimable inodes in the filesystem for |
| 1298 | * the shrinker to determine how much to reclaim. |
| 1299 | */ |
| 1300 | int |
| 1301 | xfs_reclaim_inodes_count( |
| 1302 | struct xfs_mount *mp) |
| 1303 | { |
| 1304 | struct xfs_perag *pag; |
| 1305 | xfs_agnumber_t ag = 0; |
| 1306 | int reclaimable = 0; |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1307 | |
Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1308 | while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { |
| 1309 | ag = pag->pag_agno + 1; |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 1310 | reclaimable += pag->pag_ici_reclaimable; |
| 1311 | xfs_perag_put(pag); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1312 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1313 | return reclaimable; |
| 1314 | } |
| 1315 | |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1316 | STATIC int |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1317 | xfs_inode_match_id( |
| 1318 | struct xfs_inode *ip, |
| 1319 | struct xfs_eofblocks *eofb) |
| 1320 | { |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 1321 | if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) && |
| 1322 | !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid)) |
Brian Foster | 1b55604 | 2012-11-06 09:50:45 -0500 | [diff] [blame] | 1323 | return 0; |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1324 | |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 1325 | if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) && |
| 1326 | !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid)) |
Brian Foster | 1b55604 | 2012-11-06 09:50:45 -0500 | [diff] [blame] | 1327 | return 0; |
| 1328 | |
Dwight Engen | b9fe505 | 2013-08-15 14:08:02 -0400 | [diff] [blame] | 1329 | if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) && |
Brian Foster | 1b55604 | 2012-11-06 09:50:45 -0500 | [diff] [blame] | 1330 | xfs_get_projid(ip) != eofb->eof_prid) |
| 1331 | return 0; |
| 1332 | |
| 1333 | return 1; |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1334 | } |
| 1335 | |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1336 | /* |
| 1337 | * A union-based inode filtering algorithm. Process the inode if any of the |
| 1338 | * criteria match. This is for global/internal scans only. |
| 1339 | */ |
| 1340 | STATIC int |
| 1341 | xfs_inode_match_id_union( |
| 1342 | struct xfs_inode *ip, |
| 1343 | struct xfs_eofblocks *eofb) |
| 1344 | { |
| 1345 | if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) && |
| 1346 | uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid)) |
| 1347 | return 1; |
| 1348 | |
| 1349 | if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) && |
| 1350 | gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid)) |
| 1351 | return 1; |
| 1352 | |
| 1353 | if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) && |
| 1354 | xfs_get_projid(ip) == eofb->eof_prid) |
| 1355 | return 1; |
| 1356 | |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1360 | STATIC int |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1361 | xfs_inode_free_eofblocks( |
| 1362 | struct xfs_inode *ip, |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1363 | int flags, |
| 1364 | void *args) |
| 1365 | { |
Brian Foster | 798b1dc | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 1366 | int ret = 0; |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1367 | struct xfs_eofblocks *eofb = args; |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1368 | int match; |
Brian Foster | 5400da7 | 2014-07-24 19:40:22 +1000 | [diff] [blame] | 1369 | |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1370 | if (!xfs_can_free_eofblocks(ip, false)) { |
| 1371 | /* inode could be preallocated or append-only */ |
| 1372 | trace_xfs_inode_free_eofblocks_invalid(ip); |
| 1373 | xfs_inode_clear_eofblocks_tag(ip); |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | * If the mapping is dirty the operation can block and wait for some |
| 1379 | * time. Unless we are waiting, skip it. |
| 1380 | */ |
| 1381 | if (!(flags & SYNC_WAIT) && |
| 1382 | mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY)) |
| 1383 | return 0; |
| 1384 | |
Brian Foster | 00ca79a | 2012-11-07 12:21:14 -0500 | [diff] [blame] | 1385 | if (eofb) { |
Brian Foster | f452639 | 2014-07-24 19:44:28 +1000 | [diff] [blame] | 1386 | if (eofb->eof_flags & XFS_EOF_FLAGS_UNION) |
| 1387 | match = xfs_inode_match_id_union(ip, eofb); |
| 1388 | else |
| 1389 | match = xfs_inode_match_id(ip, eofb); |
| 1390 | if (!match) |
Brian Foster | 00ca79a | 2012-11-07 12:21:14 -0500 | [diff] [blame] | 1391 | return 0; |
| 1392 | |
| 1393 | /* skip the inode if the file size is too small */ |
| 1394 | if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE && |
| 1395 | XFS_ISIZE(ip) < eofb->eof_min_file_size) |
| 1396 | return 0; |
| 1397 | } |
Brian Foster | 3e3f9f5 | 2012-11-07 12:21:13 -0500 | [diff] [blame] | 1398 | |
Brian Foster | 798b1dc | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 1399 | /* |
| 1400 | * If the caller is waiting, return -EAGAIN to keep the background |
| 1401 | * scanner moving and revisit the inode in a subsequent pass. |
| 1402 | */ |
Brian Foster | 4d725d7 | 2017-01-27 23:22:56 -0800 | [diff] [blame] | 1403 | if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { |
Brian Foster | 798b1dc | 2017-01-27 23:22:55 -0800 | [diff] [blame] | 1404 | if (flags & SYNC_WAIT) |
| 1405 | ret = -EAGAIN; |
| 1406 | return ret; |
| 1407 | } |
| 1408 | ret = xfs_free_eofblocks(ip); |
Brian Foster | 4d725d7 | 2017-01-27 23:22:56 -0800 | [diff] [blame] | 1409 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1410 | |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1414 | static int |
| 1415 | __xfs_icache_free_eofblocks( |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1416 | struct xfs_mount *mp, |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1417 | struct xfs_eofblocks *eofb, |
| 1418 | int (*execute)(struct xfs_inode *ip, int flags, |
| 1419 | void *args), |
| 1420 | int tag) |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1421 | { |
Brian Foster | 8ca149d | 2012-11-07 12:21:12 -0500 | [diff] [blame] | 1422 | int flags = SYNC_TRYLOCK; |
| 1423 | |
| 1424 | if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC)) |
| 1425 | flags = SYNC_WAIT; |
| 1426 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1427 | return xfs_inode_ag_iterator_tag(mp, execute, flags, |
| 1428 | eofb, tag); |
| 1429 | } |
| 1430 | |
| 1431 | int |
| 1432 | xfs_icache_free_eofblocks( |
| 1433 | struct xfs_mount *mp, |
| 1434 | struct xfs_eofblocks *eofb) |
| 1435 | { |
| 1436 | return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_eofblocks, |
| 1437 | XFS_ICI_EOFBLOCKS_TAG); |
Brian Foster | 41176a6 | 2012-11-06 09:50:42 -0500 | [diff] [blame] | 1438 | } |
| 1439 | |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 1440 | /* |
| 1441 | * Run eofblocks scans on the quotas applicable to the inode. For inodes with |
| 1442 | * multiple quotas, we don't know exactly which quota caused an allocation |
| 1443 | * failure. We make a best effort by including each quota under low free space |
| 1444 | * conditions (less than 1% free space) in the scan. |
| 1445 | */ |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1446 | static int |
| 1447 | __xfs_inode_free_quota_eofblocks( |
| 1448 | struct xfs_inode *ip, |
| 1449 | int (*execute)(struct xfs_mount *mp, |
| 1450 | struct xfs_eofblocks *eofb)) |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 1451 | { |
| 1452 | int scan = 0; |
| 1453 | struct xfs_eofblocks eofb = {0}; |
| 1454 | struct xfs_dquot *dq; |
| 1455 | |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 1456 | /* |
Brian Foster | 4d725d7 | 2017-01-27 23:22:56 -0800 | [diff] [blame] | 1457 | * Run a sync scan to increase effectiveness and use the union filter to |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 1458 | * cover all applicable quotas in a single scan. |
| 1459 | */ |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 1460 | eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC; |
| 1461 | |
| 1462 | if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) { |
| 1463 | dq = xfs_inode_dquot(ip, XFS_DQ_USER); |
| 1464 | if (dq && xfs_dquot_lowsp(dq)) { |
| 1465 | eofb.eof_uid = VFS_I(ip)->i_uid; |
| 1466 | eofb.eof_flags |= XFS_EOF_FLAGS_UID; |
| 1467 | scan = 1; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) { |
| 1472 | dq = xfs_inode_dquot(ip, XFS_DQ_GROUP); |
| 1473 | if (dq && xfs_dquot_lowsp(dq)) { |
| 1474 | eofb.eof_gid = VFS_I(ip)->i_gid; |
| 1475 | eofb.eof_flags |= XFS_EOF_FLAGS_GID; |
| 1476 | scan = 1; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | if (scan) |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1481 | execute(ip->i_mount, &eofb); |
Brian Foster | dc06f398 | 2014-07-24 19:49:28 +1000 | [diff] [blame] | 1482 | |
| 1483 | return scan; |
| 1484 | } |
| 1485 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1486 | int |
| 1487 | xfs_inode_free_quota_eofblocks( |
| 1488 | struct xfs_inode *ip) |
| 1489 | { |
| 1490 | return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks); |
| 1491 | } |
| 1492 | |
| 1493 | static void |
| 1494 | __xfs_inode_set_eofblocks_tag( |
| 1495 | xfs_inode_t *ip, |
| 1496 | void (*execute)(struct xfs_mount *mp), |
| 1497 | void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno, |
| 1498 | int error, unsigned long caller_ip), |
| 1499 | int tag) |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1500 | { |
| 1501 | struct xfs_mount *mp = ip->i_mount; |
| 1502 | struct xfs_perag *pag; |
| 1503 | int tagged; |
| 1504 | |
Christoph Hellwig | 85a6e76 | 2016-09-19 11:09:48 +1000 | [diff] [blame] | 1505 | /* |
| 1506 | * Don't bother locking the AG and looking up in the radix trees |
| 1507 | * if we already know that we have the tag set. |
| 1508 | */ |
| 1509 | if (ip->i_flags & XFS_IEOFBLOCKS) |
| 1510 | return; |
| 1511 | spin_lock(&ip->i_flags_lock); |
| 1512 | ip->i_flags |= XFS_IEOFBLOCKS; |
| 1513 | spin_unlock(&ip->i_flags_lock); |
| 1514 | |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1515 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
| 1516 | spin_lock(&pag->pag_ici_lock); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1517 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1518 | tagged = radix_tree_tagged(&pag->pag_ici_root, tag); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1519 | radix_tree_tag_set(&pag->pag_ici_root, |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1520 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1521 | if (!tagged) { |
| 1522 | /* propagate the eofblocks tag up into the perag radix tree */ |
| 1523 | spin_lock(&ip->i_mount->m_perag_lock); |
| 1524 | radix_tree_tag_set(&ip->i_mount->m_perag_tree, |
| 1525 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1526 | tag); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1527 | spin_unlock(&ip->i_mount->m_perag_lock); |
| 1528 | |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 1529 | /* kick off background trimming */ |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1530 | execute(ip->i_mount); |
Brian Foster | 579b62f | 2012-11-06 09:50:47 -0500 | [diff] [blame] | 1531 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1532 | set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | spin_unlock(&pag->pag_ici_lock); |
| 1536 | xfs_perag_put(pag); |
| 1537 | } |
| 1538 | |
| 1539 | void |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1540 | xfs_inode_set_eofblocks_tag( |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1541 | xfs_inode_t *ip) |
| 1542 | { |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1543 | trace_xfs_inode_set_eofblocks_tag(ip); |
| 1544 | return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_eofblocks, |
| 1545 | trace_xfs_perag_set_eofblocks, |
| 1546 | XFS_ICI_EOFBLOCKS_TAG); |
| 1547 | } |
| 1548 | |
| 1549 | static void |
| 1550 | __xfs_inode_clear_eofblocks_tag( |
| 1551 | xfs_inode_t *ip, |
| 1552 | void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno, |
| 1553 | int error, unsigned long caller_ip), |
| 1554 | int tag) |
| 1555 | { |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1556 | struct xfs_mount *mp = ip->i_mount; |
| 1557 | struct xfs_perag *pag; |
| 1558 | |
Christoph Hellwig | 85a6e76 | 2016-09-19 11:09:48 +1000 | [diff] [blame] | 1559 | spin_lock(&ip->i_flags_lock); |
| 1560 | ip->i_flags &= ~XFS_IEOFBLOCKS; |
| 1561 | spin_unlock(&ip->i_flags_lock); |
| 1562 | |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1563 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
| 1564 | spin_lock(&pag->pag_ici_lock); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1565 | |
| 1566 | radix_tree_tag_clear(&pag->pag_ici_root, |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1567 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag); |
| 1568 | if (!radix_tree_tagged(&pag->pag_ici_root, tag)) { |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1569 | /* clear the eofblocks tag from the perag radix tree */ |
| 1570 | spin_lock(&ip->i_mount->m_perag_lock); |
| 1571 | radix_tree_tag_clear(&ip->i_mount->m_perag_tree, |
| 1572 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1573 | tag); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1574 | spin_unlock(&ip->i_mount->m_perag_lock); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1575 | clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_); |
Brian Foster | 27b5286 | 2012-11-06 09:50:38 -0500 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | spin_unlock(&pag->pag_ici_lock); |
| 1579 | xfs_perag_put(pag); |
| 1580 | } |
| 1581 | |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1582 | void |
| 1583 | xfs_inode_clear_eofblocks_tag( |
| 1584 | xfs_inode_t *ip) |
| 1585 | { |
| 1586 | trace_xfs_inode_clear_eofblocks_tag(ip); |
| 1587 | return __xfs_inode_clear_eofblocks_tag(ip, |
| 1588 | trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG); |
| 1589 | } |
| 1590 | |
| 1591 | /* |
| 1592 | * Automatic CoW Reservation Freeing |
| 1593 | * |
| 1594 | * These functions automatically garbage collect leftover CoW reservations |
| 1595 | * that were made on behalf of a cowextsize hint when we start to run out |
| 1596 | * of quota or when the reservations sit around for too long. If the file |
| 1597 | * has dirty pages or is undergoing writeback, its CoW reservations will |
| 1598 | * be retained. |
| 1599 | * |
| 1600 | * The actual garbage collection piggybacks off the same code that runs |
| 1601 | * the speculative EOF preallocation garbage collector. |
| 1602 | */ |
| 1603 | STATIC int |
| 1604 | xfs_inode_free_cowblocks( |
| 1605 | struct xfs_inode *ip, |
| 1606 | int flags, |
| 1607 | void *args) |
| 1608 | { |
| 1609 | int ret; |
| 1610 | struct xfs_eofblocks *eofb = args; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1611 | int match; |
Brian Foster | 2f09242 | 2017-01-09 16:38:34 +0100 | [diff] [blame] | 1612 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1613 | |
Brian Foster | 2f09242 | 2017-01-09 16:38:34 +0100 | [diff] [blame] | 1614 | /* |
| 1615 | * Just clear the tag if we have an empty cow fork or none at all. It's |
| 1616 | * possible the inode was fully unshared since it was originally tagged. |
| 1617 | */ |
| 1618 | if (!xfs_is_reflink_inode(ip) || !ifp->if_bytes) { |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1619 | trace_xfs_inode_free_cowblocks_invalid(ip); |
| 1620 | xfs_inode_clear_cowblocks_tag(ip); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | * If the mapping is dirty or under writeback we cannot touch the |
| 1626 | * CoW fork. Leave it alone if we're in the midst of a directio. |
| 1627 | */ |
Christoph Hellwig | 91192ae | 2017-01-09 16:39:02 +0100 | [diff] [blame] | 1628 | if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) || |
| 1629 | mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) || |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1630 | mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) || |
| 1631 | atomic_read(&VFS_I(ip)->i_dio_count)) |
| 1632 | return 0; |
| 1633 | |
| 1634 | if (eofb) { |
| 1635 | if (eofb->eof_flags & XFS_EOF_FLAGS_UNION) |
| 1636 | match = xfs_inode_match_id_union(ip, eofb); |
| 1637 | else |
| 1638 | match = xfs_inode_match_id(ip, eofb); |
| 1639 | if (!match) |
| 1640 | return 0; |
| 1641 | |
| 1642 | /* skip the inode if the file size is too small */ |
| 1643 | if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE && |
| 1644 | XFS_ISIZE(ip) < eofb->eof_min_file_size) |
| 1645 | return 0; |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* Free the CoW blocks */ |
Brian Foster | 4d725d7 | 2017-01-27 23:22:56 -0800 | [diff] [blame] | 1649 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
| 1650 | xfs_ilock(ip, XFS_MMAPLOCK_EXCL); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1651 | |
Christoph Hellwig | 3b83a02 | 2017-03-07 16:45:58 -0800 | [diff] [blame] | 1652 | ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1653 | |
Brian Foster | 4d725d7 | 2017-01-27 23:22:56 -0800 | [diff] [blame] | 1654 | xfs_iunlock(ip, XFS_MMAPLOCK_EXCL); |
| 1655 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1656 | |
| 1657 | return ret; |
| 1658 | } |
| 1659 | |
| 1660 | int |
| 1661 | xfs_icache_free_cowblocks( |
| 1662 | struct xfs_mount *mp, |
| 1663 | struct xfs_eofblocks *eofb) |
| 1664 | { |
| 1665 | return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_cowblocks, |
| 1666 | XFS_ICI_COWBLOCKS_TAG); |
| 1667 | } |
| 1668 | |
| 1669 | int |
| 1670 | xfs_inode_free_quota_cowblocks( |
| 1671 | struct xfs_inode *ip) |
| 1672 | { |
| 1673 | return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks); |
| 1674 | } |
| 1675 | |
| 1676 | void |
| 1677 | xfs_inode_set_cowblocks_tag( |
| 1678 | xfs_inode_t *ip) |
| 1679 | { |
Brian Foster | 7b7381f | 2016-10-24 14:21:00 +1100 | [diff] [blame] | 1680 | trace_xfs_inode_set_cowblocks_tag(ip); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1681 | return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_cowblocks, |
Brian Foster | 7b7381f | 2016-10-24 14:21:00 +1100 | [diff] [blame] | 1682 | trace_xfs_perag_set_cowblocks, |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1683 | XFS_ICI_COWBLOCKS_TAG); |
| 1684 | } |
| 1685 | |
| 1686 | void |
| 1687 | xfs_inode_clear_cowblocks_tag( |
| 1688 | xfs_inode_t *ip) |
| 1689 | { |
Brian Foster | 7b7381f | 2016-10-24 14:21:00 +1100 | [diff] [blame] | 1690 | trace_xfs_inode_clear_cowblocks_tag(ip); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1691 | return __xfs_inode_clear_eofblocks_tag(ip, |
Brian Foster | 7b7381f | 2016-10-24 14:21:00 +1100 | [diff] [blame] | 1692 | trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG); |
Darrick J. Wong | 83104d4 | 2016-10-03 09:11:46 -0700 | [diff] [blame] | 1693 | } |