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