blob: 86a4911520cc5cf9cfb1803c9da96bc34533f50d [file] [log] [blame]
David Chinnerfe4fa4b2008-10-30 17:06:08 +11001/*
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 Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110023#include "xfs_sb.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110024#include "xfs_mount.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110025#include "xfs_inode.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110026#include "xfs_error.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_trans.h"
28#include "xfs_trans_priv.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110029#include "xfs_inode_item.h"
Christoph Hellwig7d095252009-06-08 15:33:32 +020030#include "xfs_quota.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000031#include "xfs_trace.h"
Dave Chinner6d8b79c2012-10-08 21:56:09 +110032#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100033#include "xfs_bmap_util.h"
Brian Fosterdc06f3982014-07-24 19:49:28 +100034#include "xfs_dquot_item.h"
35#include "xfs_dquot.h"
Darrick J. Wong83104d42016-10-03 09:11:46 -070036#include "xfs_reflink.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110037
David Chinnera167b172008-10-30 17:06:18 +110038#include <linux/kthread.h>
39#include <linux/freezer.h>
40
Dave Chinner33479e02012-10-08 21:56:11 +110041/*
42 * Allocate and initialise an xfs_inode.
43 */
Dave Chinner638f44162013-08-30 10:23:45 +100044struct xfs_inode *
Dave Chinner33479e02012-10-08 21:56:11 +110045xfs_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 Chinnerc19b3b052016-02-09 16:54:58 +110064 /* VFS doesn't initialise i_mode! */
65 VFS_I(ip)->i_mode = 0;
66
Bill O'Donnellff6d6af2015-10-12 18:21:22 +110067 XFS_STATS_INC(mp, vn_active);
Dave Chinner33479e02012-10-08 21:56:11 +110068 ASSERT(atomic_read(&ip->i_pincount) == 0);
Dave Chinner33479e02012-10-08 21:56:11 +110069 ASSERT(!xfs_isiflocked(ip));
70 ASSERT(ip->i_ino == 0);
71
72 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
73
74 /* initialise the xfs inode */
75 ip->i_ino = ino;
76 ip->i_mount = mp;
77 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
78 ip->i_afp = NULL;
Darrick J. Wong3993bae2016-10-03 09:11:32 -070079 ip->i_cowfp = NULL;
80 ip->i_cnextents = 0;
81 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
Dave Chinner33479e02012-10-08 21:56:11 +110082 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
83 ip->i_flags = 0;
84 ip->i_delayed_blks = 0;
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +110085 memset(&ip->i_d, 0, sizeof(ip->i_d));
Dave Chinner33479e02012-10-08 21:56:11 +110086
87 return ip;
88}
89
90STATIC void
91xfs_inode_free_callback(
92 struct rcu_head *head)
93{
94 struct inode *inode = container_of(head, struct inode, i_rcu);
95 struct xfs_inode *ip = XFS_I(inode);
96
Dave Chinnerc19b3b052016-02-09 16:54:58 +110097 switch (VFS_I(ip)->i_mode & S_IFMT) {
Dave Chinner33479e02012-10-08 21:56:11 +110098 case S_IFREG:
99 case S_IFDIR:
100 case S_IFLNK:
101 xfs_idestroy_fork(ip, XFS_DATA_FORK);
102 break;
103 }
104
105 if (ip->i_afp)
106 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700107 if (ip->i_cowfp)
108 xfs_idestroy_fork(ip, XFS_COW_FORK);
Dave Chinner33479e02012-10-08 21:56:11 +1100109
110 if (ip->i_itemp) {
111 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
112 xfs_inode_item_destroy(ip);
113 ip->i_itemp = NULL;
114 }
115
Dave Chinner1f2dcfe2016-05-18 14:01:53 +1000116 kmem_zone_free(xfs_inode_zone, ip);
117}
118
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000119static void
120__xfs_inode_free(
121 struct xfs_inode *ip)
122{
123 /* asserts to verify all state is correct here */
124 ASSERT(atomic_read(&ip->i_pincount) == 0);
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000125 XFS_STATS_DEC(ip->i_mount, vn_active);
126
127 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
128}
129
Dave Chinner1f2dcfe2016-05-18 14:01:53 +1000130void
131xfs_inode_free(
132 struct xfs_inode *ip)
133{
Brian Fosterb49ef752017-01-09 16:38:38 +0100134 ASSERT(!xfs_isiflocked(ip));
135
Dave Chinner33479e02012-10-08 21:56:11 +1100136 /*
137 * Because we use RCU freeing we need to ensure the inode always
138 * appears to be reclaimed with an invalid inode number when in the
139 * free state. The ip->i_flags_lock provides the barrier against lookup
140 * races.
141 */
142 spin_lock(&ip->i_flags_lock);
143 ip->i_flags = XFS_IRECLAIM;
144 ip->i_ino = 0;
145 spin_unlock(&ip->i_flags_lock);
146
Dave Chinner8a17d7d2016-05-18 14:09:12 +1000147 __xfs_inode_free(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100148}
149
150/*
Dave Chinnerad438c42016-05-18 14:20:08 +1000151 * Queue a new inode reclaim pass if there are reclaimable inodes and there
152 * isn't a reclaim pass already in progress. By default it runs every 5s based
153 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
154 * tunable, but that can be done if this method proves to be ineffective or too
155 * aggressive.
156 */
157static void
158xfs_reclaim_work_queue(
159 struct xfs_mount *mp)
160{
161
162 rcu_read_lock();
163 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
164 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
165 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
166 }
167 rcu_read_unlock();
168}
169
170/*
171 * This is a fast pass over the inode cache to try to get reclaim moving on as
172 * many inodes as possible in a short period of time. It kicks itself every few
173 * seconds, as well as being kicked by the inode cache shrinker when memory
174 * goes low. It scans as quickly as possible avoiding locked inodes or those
175 * already being flushed, and once done schedules a future pass.
176 */
177void
178xfs_reclaim_worker(
179 struct work_struct *work)
180{
181 struct xfs_mount *mp = container_of(to_delayed_work(work),
182 struct xfs_mount, m_reclaim_work);
183
184 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
185 xfs_reclaim_work_queue(mp);
186}
187
188static void
189xfs_perag_set_reclaim_tag(
190 struct xfs_perag *pag)
191{
192 struct xfs_mount *mp = pag->pag_mount;
193
Brian Foster85ab1b22017-06-08 08:23:07 -0700194 lockdep_assert_held(&pag->pag_ici_lock);
Dave Chinnerad438c42016-05-18 14:20:08 +1000195 if (pag->pag_ici_reclaimable++)
196 return;
197
198 /* propagate the reclaim tag up into the perag radix tree */
199 spin_lock(&mp->m_perag_lock);
200 radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno,
201 XFS_ICI_RECLAIM_TAG);
202 spin_unlock(&mp->m_perag_lock);
203
204 /* schedule periodic background inode reclaim */
205 xfs_reclaim_work_queue(mp);
206
207 trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
208}
209
210static void
211xfs_perag_clear_reclaim_tag(
212 struct xfs_perag *pag)
213{
214 struct xfs_mount *mp = pag->pag_mount;
215
Brian Foster85ab1b22017-06-08 08:23:07 -0700216 lockdep_assert_held(&pag->pag_ici_lock);
Dave Chinnerad438c42016-05-18 14:20:08 +1000217 if (--pag->pag_ici_reclaimable)
218 return;
219
220 /* clear the reclaim tag from the perag radix tree */
221 spin_lock(&mp->m_perag_lock);
222 radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno,
223 XFS_ICI_RECLAIM_TAG);
224 spin_unlock(&mp->m_perag_lock);
225 trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
226}
227
228
229/*
230 * We set the inode flag atomically with the radix tree tag.
231 * Once we get tag lookups on the radix tree, this inode flag
232 * can go away.
233 */
234void
235xfs_inode_set_reclaim_tag(
236 struct xfs_inode *ip)
237{
238 struct xfs_mount *mp = ip->i_mount;
239 struct xfs_perag *pag;
240
241 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
242 spin_lock(&pag->pag_ici_lock);
243 spin_lock(&ip->i_flags_lock);
244
245 radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
246 XFS_ICI_RECLAIM_TAG);
247 xfs_perag_set_reclaim_tag(pag);
248 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
249
250 spin_unlock(&ip->i_flags_lock);
251 spin_unlock(&pag->pag_ici_lock);
252 xfs_perag_put(pag);
253}
254
255STATIC void
256xfs_inode_clear_reclaim_tag(
257 struct xfs_perag *pag,
258 xfs_ino_t ino)
259{
260 radix_tree_tag_clear(&pag->pag_ici_root,
261 XFS_INO_TO_AGINO(pag->pag_mount, ino),
262 XFS_ICI_RECLAIM_TAG);
263 xfs_perag_clear_reclaim_tag(pag);
264}
265
Brian Foster2ea882d2017-04-26 08:30:39 -0700266static void
267xfs_inew_wait(
268 struct xfs_inode *ip)
269{
270 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT);
271 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT);
272
273 do {
274 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
275 if (!xfs_iflags_test(ip, XFS_INEW))
276 break;
277 schedule();
278 } while (true);
279 finish_wait(wq, &wait.wait);
280}
281
Dave Chinnerad438c42016-05-18 14:20:08 +1000282/*
Dave Chinner50997472016-02-09 16:54:58 +1100283 * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
284 * part of the structure. This is made more complex by the fact we store
285 * information about the on-disk values in the VFS inode and so we can't just
Dave Chinner83e06f22016-02-09 16:54:58 +1100286 * overwrite the values unconditionally. Hence we save the parameters we
Dave Chinner50997472016-02-09 16:54:58 +1100287 * need to retain across reinitialisation, and rewrite them into the VFS inode
Dave Chinner83e06f22016-02-09 16:54:58 +1100288 * after reinitialisation even if it fails.
Dave Chinner50997472016-02-09 16:54:58 +1100289 */
290static int
291xfs_reinit_inode(
292 struct xfs_mount *mp,
293 struct inode *inode)
294{
295 int error;
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100296 uint32_t nlink = inode->i_nlink;
Dave Chinner9e9a2672016-02-09 16:54:58 +1100297 uint32_t generation = inode->i_generation;
Dave Chinner83e06f22016-02-09 16:54:58 +1100298 uint64_t version = inode->i_version;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100299 umode_t mode = inode->i_mode;
Dave Chinner50997472016-02-09 16:54:58 +1100300
301 error = inode_init_always(mp->m_super, inode);
302
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100303 set_nlink(inode, nlink);
Dave Chinner9e9a2672016-02-09 16:54:58 +1100304 inode->i_generation = generation;
Dave Chinner83e06f22016-02-09 16:54:58 +1100305 inode->i_version = version;
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100306 inode->i_mode = mode;
Dave Chinner50997472016-02-09 16:54:58 +1100307 return error;
308}
309
310/*
Dave Chinner33479e02012-10-08 21:56:11 +1100311 * Check the validity of the inode we just found it the cache
312 */
313static int
314xfs_iget_cache_hit(
315 struct xfs_perag *pag,
316 struct xfs_inode *ip,
317 xfs_ino_t ino,
318 int flags,
319 int lock_flags) __releases(RCU)
320{
321 struct inode *inode = VFS_I(ip);
322 struct xfs_mount *mp = ip->i_mount;
323 int error;
324
325 /*
326 * check for re-use of an inode within an RCU grace period due to the
327 * radix tree nodes not being updated yet. We monitor for this by
328 * setting the inode number to zero before freeing the inode structure.
329 * If the inode has been reallocated and set up, then the inode number
330 * will not match, so check for that, too.
331 */
332 spin_lock(&ip->i_flags_lock);
333 if (ip->i_ino != ino) {
334 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100335 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000336 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100337 goto out_error;
338 }
339
340
341 /*
342 * If we are racing with another cache hit that is currently
343 * instantiating this inode or currently recycling it out of
344 * reclaimabe state, wait for the initialisation to complete
345 * before continuing.
346 *
347 * XXX(hch): eventually we should do something equivalent to
348 * wait_on_inode to wait for these flags to be cleared
349 * instead of polling for it.
350 */
351 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
352 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100353 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000354 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100355 goto out_error;
356 }
357
358 /*
359 * If lookup is racing with unlink return an error immediately.
360 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100361 if (VFS_I(ip)->i_mode == 0 && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000362 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100363 goto out_error;
364 }
365
366 /*
367 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
368 * Need to carefully get it back into useable state.
369 */
370 if (ip->i_flags & XFS_IRECLAIMABLE) {
371 trace_xfs_iget_reclaim(ip);
372
373 /*
374 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
375 * from stomping over us while we recycle the inode. We can't
376 * clear the radix tree reclaimable tag yet as it requires
377 * pag_ici_lock to be held exclusive.
378 */
379 ip->i_flags |= XFS_IRECLAIM;
380
381 spin_unlock(&ip->i_flags_lock);
382 rcu_read_unlock();
383
Dave Chinner50997472016-02-09 16:54:58 +1100384 error = xfs_reinit_inode(mp, inode);
Dave Chinner33479e02012-10-08 21:56:11 +1100385 if (error) {
Brian Fostere86b6162017-04-26 08:30:39 -0700386 bool wake;
Dave Chinner33479e02012-10-08 21:56:11 +1100387 /*
388 * Re-initializing the inode failed, and we are in deep
389 * trouble. Try to re-add it to the reclaim list.
390 */
391 rcu_read_lock();
392 spin_lock(&ip->i_flags_lock);
Brian Fostere86b6162017-04-26 08:30:39 -0700393 wake = !!__xfs_iflags_test(ip, XFS_INEW);
Dave Chinner33479e02012-10-08 21:56:11 +1100394 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
Brian Fostere86b6162017-04-26 08:30:39 -0700395 if (wake)
396 wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
Dave Chinner33479e02012-10-08 21:56:11 +1100397 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
398 trace_xfs_iget_reclaim_fail(ip);
399 goto out_error;
400 }
401
402 spin_lock(&pag->pag_ici_lock);
403 spin_lock(&ip->i_flags_lock);
404
405 /*
406 * Clear the per-lifetime state in the inode as we are now
407 * effectively a new inode and need to return to the initial
408 * state before reuse occurs.
409 */
410 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
411 ip->i_flags |= XFS_INEW;
Dave Chinner545c0882016-05-18 14:11:41 +1000412 xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
Dave Chinner33479e02012-10-08 21:56:11 +1100413 inode->i_state = I_NEW;
414
415 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
416 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
417
418 spin_unlock(&ip->i_flags_lock);
419 spin_unlock(&pag->pag_ici_lock);
420 } else {
421 /* If the VFS inode is being torn down, pause and try again. */
422 if (!igrab(inode)) {
423 trace_xfs_iget_skip(ip);
Dave Chinner24513372014-06-25 14:58:08 +1000424 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100425 goto out_error;
426 }
427
428 /* We've got a live one. */
429 spin_unlock(&ip->i_flags_lock);
430 rcu_read_unlock();
431 trace_xfs_iget_hit(ip);
432 }
433
434 if (lock_flags != 0)
435 xfs_ilock(ip, lock_flags);
436
437 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100438 XFS_STATS_INC(mp, xs_ig_found);
Dave Chinner33479e02012-10-08 21:56:11 +1100439
440 return 0;
441
442out_error:
443 spin_unlock(&ip->i_flags_lock);
444 rcu_read_unlock();
445 return error;
446}
447
448
449static int
450xfs_iget_cache_miss(
451 struct xfs_mount *mp,
452 struct xfs_perag *pag,
453 xfs_trans_t *tp,
454 xfs_ino_t ino,
455 struct xfs_inode **ipp,
456 int flags,
457 int lock_flags)
458{
459 struct xfs_inode *ip;
460 int error;
461 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
462 int iflags;
463
464 ip = xfs_inode_alloc(mp, ino);
465 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +1000466 return -ENOMEM;
Dave Chinner33479e02012-10-08 21:56:11 +1100467
468 error = xfs_iread(mp, tp, ip, flags);
469 if (error)
470 goto out_destroy;
471
472 trace_xfs_iget_miss(ip);
473
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100474 if ((VFS_I(ip)->i_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000475 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100476 goto out_destroy;
477 }
478
479 /*
480 * Preload the radix tree so we can insert safely under the
481 * write spinlock. Note that we cannot sleep inside the preload
482 * region. Since we can be called from transaction context, don't
483 * recurse into the file system.
484 */
485 if (radix_tree_preload(GFP_NOFS)) {
Dave Chinner24513372014-06-25 14:58:08 +1000486 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100487 goto out_destroy;
488 }
489
490 /*
491 * Because the inode hasn't been added to the radix-tree yet it can't
492 * be found by another thread, so we can do the non-sleeping lock here.
493 */
494 if (lock_flags) {
495 if (!xfs_ilock_nowait(ip, lock_flags))
496 BUG();
497 }
498
499 /*
500 * These values must be set before inserting the inode into the radix
501 * tree as the moment it is inserted a concurrent lookup (allowed by the
502 * RCU locking mechanism) can find it and that lookup must see that this
503 * is an inode currently under construction (i.e. that XFS_INEW is set).
504 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
505 * memory barrier that ensures this detection works correctly at lookup
506 * time.
507 */
508 iflags = XFS_INEW;
509 if (flags & XFS_IGET_DONTCACHE)
510 iflags |= XFS_IDONTCACHE;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500511 ip->i_udquot = NULL;
512 ip->i_gdquot = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500513 ip->i_pdquot = NULL;
Dave Chinner33479e02012-10-08 21:56:11 +1100514 xfs_iflags_set(ip, iflags);
515
516 /* insert the new inode */
517 spin_lock(&pag->pag_ici_lock);
518 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
519 if (unlikely(error)) {
520 WARN_ON(error != -EEXIST);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100521 XFS_STATS_INC(mp, xs_ig_dup);
Dave Chinner24513372014-06-25 14:58:08 +1000522 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100523 goto out_preload_end;
524 }
525 spin_unlock(&pag->pag_ici_lock);
526 radix_tree_preload_end();
527
528 *ipp = ip;
529 return 0;
530
531out_preload_end:
532 spin_unlock(&pag->pag_ici_lock);
533 radix_tree_preload_end();
534 if (lock_flags)
535 xfs_iunlock(ip, lock_flags);
536out_destroy:
537 __destroy_inode(VFS_I(ip));
538 xfs_inode_free(ip);
539 return error;
540}
541
542/*
543 * Look up an inode by number in the given file system.
544 * The inode is looked up in the cache held in each AG.
545 * If the inode is found in the cache, initialise the vfs inode
546 * if necessary.
547 *
548 * If it is not in core, read it in from the file system's device,
549 * add it to the cache and initialise the vfs inode.
550 *
551 * The inode is locked according to the value of the lock_flags parameter.
552 * This flag parameter indicates how and if the inode's IO lock and inode lock
553 * should be taken.
554 *
555 * mp -- the mount point structure for the current file system. It points
556 * to the inode hash table.
557 * tp -- a pointer to the current transaction if there is one. This is
558 * simply passed through to the xfs_iread() call.
559 * ino -- the number of the inode desired. This is the unique identifier
560 * within the file system for the inode being requested.
561 * lock_flags -- flags indicating how to lock the inode. See the comment
562 * for xfs_ilock() for a list of valid values.
563 */
564int
565xfs_iget(
566 xfs_mount_t *mp,
567 xfs_trans_t *tp,
568 xfs_ino_t ino,
569 uint flags,
570 uint lock_flags,
571 xfs_inode_t **ipp)
572{
573 xfs_inode_t *ip;
574 int error;
575 xfs_perag_t *pag;
576 xfs_agino_t agino;
577
578 /*
579 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
580 * doesn't get freed while it's being referenced during a
581 * radix tree traversal here. It assumes this function
582 * aqcuires only the ILOCK (and therefore it has no need to
583 * involve the IOLOCK in this synchronization).
584 */
585 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
586
587 /* reject inode numbers outside existing AGs */
588 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +1000589 return -EINVAL;
Dave Chinner33479e02012-10-08 21:56:11 +1100590
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100591 XFS_STATS_INC(mp, xs_ig_attempts);
Lucas Stach8774cf82015-08-28 14:50:56 +1000592
Dave Chinner33479e02012-10-08 21:56:11 +1100593 /* get the perag structure and ensure that it's inode capable */
594 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
595 agino = XFS_INO_TO_AGINO(mp, ino);
596
597again:
598 error = 0;
599 rcu_read_lock();
600 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
601
602 if (ip) {
603 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
604 if (error)
605 goto out_error_or_again;
606 } else {
607 rcu_read_unlock();
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100608 XFS_STATS_INC(mp, xs_ig_missed);
Dave Chinner33479e02012-10-08 21:56:11 +1100609
610 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
611 flags, lock_flags);
612 if (error)
613 goto out_error_or_again;
614 }
615 xfs_perag_put(pag);
616
617 *ipp = ip;
618
619 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100620 * If we have a real type for an on-disk inode, we can setup the inode
Dave Chinner33479e02012-10-08 21:56:11 +1100621 * now. If it's a new inode being created, xfs_ialloc will handle it.
622 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100623 if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
Dave Chinner58c90472015-02-23 22:38:08 +1100624 xfs_setup_existing_inode(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100625 return 0;
626
627out_error_or_again:
Dave Chinner24513372014-06-25 14:58:08 +1000628 if (error == -EAGAIN) {
Dave Chinner33479e02012-10-08 21:56:11 +1100629 delay(1);
630 goto again;
631 }
632 xfs_perag_put(pag);
633 return error;
634}
635
Dave Chinner78ae5252010-09-28 12:28:19 +1000636/*
637 * The inode lookup is done in batches to keep the amount of lock traffic and
638 * radix tree lookups to a minimum. The batch size is a trade off between
639 * lookup reduction and stack usage. This is in the reclaim path, so we can't
640 * be too greedy.
641 */
642#define XFS_LOOKUP_BATCH 32
643
Dave Chinnere13de952010-09-28 12:28:06 +1000644STATIC int
645xfs_inode_ag_walk_grab(
Brian Foster2ea882d2017-04-26 08:30:39 -0700646 struct xfs_inode *ip,
647 int flags)
Dave Chinnere13de952010-09-28 12:28:06 +1000648{
649 struct inode *inode = VFS_I(ip);
Brian Foster2ea882d2017-04-26 08:30:39 -0700650 bool newinos = !!(flags & XFS_AGITER_INEW_WAIT);
Dave Chinnere13de952010-09-28 12:28:06 +1000651
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100652 ASSERT(rcu_read_lock_held());
653
654 /*
655 * check for stale RCU freed inode
656 *
657 * If the inode has been reallocated, it doesn't matter if it's not in
658 * the AG we are walking - we are walking for writeback, so if it
659 * passes all the "valid inode" checks and is dirty, then we'll write
660 * it back anyway. If it has been reallocated and still being
661 * initialised, the XFS_INEW check below will catch it.
662 */
663 spin_lock(&ip->i_flags_lock);
664 if (!ip->i_ino)
665 goto out_unlock_noent;
666
667 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
Brian Foster2ea882d2017-04-26 08:30:39 -0700668 if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) ||
669 __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM))
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100670 goto out_unlock_noent;
671 spin_unlock(&ip->i_flags_lock);
672
Dave Chinnere13de952010-09-28 12:28:06 +1000673 /* nothing to sync during shutdown */
674 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000675 return -EFSCORRUPTED;
Dave Chinnere13de952010-09-28 12:28:06 +1000676
Dave Chinnere13de952010-09-28 12:28:06 +1000677 /* If we can't grab the inode, it must on it's way to reclaim. */
678 if (!igrab(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000679 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000680
Dave Chinnere13de952010-09-28 12:28:06 +1000681 /* inode is valid */
682 return 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100683
684out_unlock_noent:
685 spin_unlock(&ip->i_flags_lock);
Dave Chinner24513372014-06-25 14:58:08 +1000686 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000687}
688
Dave Chinner75f3cb12009-06-08 15:35:14 +0200689STATIC int
690xfs_inode_ag_walk(
691 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +0000692 struct xfs_perag *pag,
Eric Sandeene0094002014-04-14 19:04:19 +1000693 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500694 void *args),
695 int flags,
696 void *args,
Brian Foster2ea882d2017-04-26 08:30:39 -0700697 int tag,
698 int iter_flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200699{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200700 uint32_t first_index;
701 int last_error = 0;
702 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000703 int done;
Dave Chinner78ae5252010-09-28 12:28:19 +1000704 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200705
706restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000707 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200708 skipped = 0;
709 first_index = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000710 nr_found = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200711 do {
Dave Chinner78ae5252010-09-28 12:28:19 +1000712 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
Dave Chinner75f3cb12009-06-08 15:35:14 +0200713 int error = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000714 int i;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200715
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100716 rcu_read_lock();
Brian Fostera454f742012-11-06 09:50:39 -0500717
718 if (tag == -1)
719 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
Dave Chinner78ae5252010-09-28 12:28:19 +1000720 (void **)batch, first_index,
721 XFS_LOOKUP_BATCH);
Brian Fostera454f742012-11-06 09:50:39 -0500722 else
723 nr_found = radix_tree_gang_lookup_tag(
724 &pag->pag_ici_root,
725 (void **) batch, first_index,
726 XFS_LOOKUP_BATCH, tag);
727
Dave Chinner65d0f202010-09-24 18:40:15 +1000728 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100729 rcu_read_unlock();
Dave Chinner75f3cb12009-06-08 15:35:14 +0200730 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000731 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200732
Dave Chinner65d0f202010-09-24 18:40:15 +1000733 /*
Dave Chinner78ae5252010-09-28 12:28:19 +1000734 * Grab the inodes before we drop the lock. if we found
735 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000736 */
Dave Chinner78ae5252010-09-28 12:28:19 +1000737 for (i = 0; i < nr_found; i++) {
738 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000739
Brian Foster2ea882d2017-04-26 08:30:39 -0700740 if (done || xfs_inode_ag_walk_grab(ip, iter_flags))
Dave Chinner78ae5252010-09-28 12:28:19 +1000741 batch[i] = NULL;
742
743 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100744 * Update the index for the next lookup. Catch
745 * overflows into the next AG range which can occur if
746 * we have inodes in the last block of the AG and we
747 * are currently pointing to the last inode.
748 *
749 * Because we may see inodes that are from the wrong AG
750 * due to RCU freeing and reallocation, only update the
751 * index if it lies in this AG. It was a race that lead
752 * us to see this inode, so another lookup from the
753 * same index will not find it again.
Dave Chinner78ae5252010-09-28 12:28:19 +1000754 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100755 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
756 continue;
Dave Chinner78ae5252010-09-28 12:28:19 +1000757 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
758 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
759 done = 1;
Dave Chinnere13de952010-09-28 12:28:06 +1000760 }
Dave Chinner78ae5252010-09-28 12:28:19 +1000761
762 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100763 rcu_read_unlock();
Dave Chinnere13de952010-09-28 12:28:06 +1000764
Dave Chinner78ae5252010-09-28 12:28:19 +1000765 for (i = 0; i < nr_found; i++) {
766 if (!batch[i])
767 continue;
Brian Foster2ea882d2017-04-26 08:30:39 -0700768 if ((iter_flags & XFS_AGITER_INEW_WAIT) &&
769 xfs_iflags_test(batch[i], XFS_INEW))
770 xfs_inew_wait(batch[i]);
Eric Sandeene0094002014-04-14 19:04:19 +1000771 error = execute(batch[i], flags, args);
Dave Chinner78ae5252010-09-28 12:28:19 +1000772 IRELE(batch[i]);
Dave Chinner24513372014-06-25 14:58:08 +1000773 if (error == -EAGAIN) {
Dave Chinner78ae5252010-09-28 12:28:19 +1000774 skipped++;
775 continue;
776 }
Dave Chinner24513372014-06-25 14:58:08 +1000777 if (error && last_error != -EFSCORRUPTED)
Dave Chinner78ae5252010-09-28 12:28:19 +1000778 last_error = error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200779 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000780
781 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +1000782 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200783 break;
784
Dave Chinner8daaa832011-07-08 14:14:46 +1000785 cond_resched();
786
Dave Chinner78ae5252010-09-28 12:28:19 +1000787 } while (nr_found && !done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200788
789 if (skipped) {
790 delay(1);
791 goto restart;
792 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200793 return last_error;
794}
795
Brian Foster579b62f2012-11-06 09:50:47 -0500796/*
797 * Background scanning to trim post-EOF preallocated space. This is queued
Dwight Engenb9fe5052013-08-15 14:08:02 -0400798 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
Brian Foster579b62f2012-11-06 09:50:47 -0500799 */
Brian Fosterfa5a4f52016-06-21 11:53:28 +1000800void
Brian Foster579b62f2012-11-06 09:50:47 -0500801xfs_queue_eofblocks(
802 struct xfs_mount *mp)
803{
804 rcu_read_lock();
805 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
806 queue_delayed_work(mp->m_eofblocks_workqueue,
807 &mp->m_eofblocks_work,
808 msecs_to_jiffies(xfs_eofb_secs * 1000));
809 rcu_read_unlock();
810}
811
812void
813xfs_eofblocks_worker(
814 struct work_struct *work)
815{
816 struct xfs_mount *mp = container_of(to_delayed_work(work),
817 struct xfs_mount, m_eofblocks_work);
818 xfs_icache_free_eofblocks(mp, NULL);
819 xfs_queue_eofblocks(mp);
820}
821
Darrick J. Wong83104d42016-10-03 09:11:46 -0700822/*
823 * Background scanning to trim preallocated CoW space. This is queued
824 * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default).
825 * (We'll just piggyback on the post-EOF prealloc space workqueue.)
826 */
827STATIC void
828xfs_queue_cowblocks(
829 struct xfs_mount *mp)
830{
831 rcu_read_lock();
832 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG))
833 queue_delayed_work(mp->m_eofblocks_workqueue,
834 &mp->m_cowblocks_work,
835 msecs_to_jiffies(xfs_cowb_secs * 1000));
836 rcu_read_unlock();
837}
838
839void
840xfs_cowblocks_worker(
841 struct work_struct *work)
842{
843 struct xfs_mount *mp = container_of(to_delayed_work(work),
844 struct xfs_mount, m_cowblocks_work);
845 xfs_icache_free_cowblocks(mp, NULL);
846 xfs_queue_cowblocks(mp);
847}
848
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200849int
Brian Foster2ea882d2017-04-26 08:30:39 -0700850xfs_inode_ag_iterator_flags(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200851 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000852 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500853 void *args),
854 int flags,
Brian Foster2ea882d2017-04-26 08:30:39 -0700855 void *args,
856 int iter_flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200857{
Dave Chinner16fd5362010-07-20 09:43:39 +1000858 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200859 int error = 0;
860 int last_error = 0;
861 xfs_agnumber_t ag;
862
Dave Chinner16fd5362010-07-20 09:43:39 +1000863 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000864 while ((pag = xfs_perag_get(mp, ag))) {
865 ag = pag->pag_agno + 1;
Brian Foster2ea882d2017-04-26 08:30:39 -0700866 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1,
867 iter_flags);
Brian Fostera454f742012-11-06 09:50:39 -0500868 xfs_perag_put(pag);
869 if (error) {
870 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000871 if (error == -EFSCORRUPTED)
Brian Fostera454f742012-11-06 09:50:39 -0500872 break;
873 }
874 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000875 return last_error;
Brian Fostera454f742012-11-06 09:50:39 -0500876}
877
878int
Brian Foster2ea882d2017-04-26 08:30:39 -0700879xfs_inode_ag_iterator(
880 struct xfs_mount *mp,
881 int (*execute)(struct xfs_inode *ip, int flags,
882 void *args),
883 int flags,
884 void *args)
885{
886 return xfs_inode_ag_iterator_flags(mp, execute, flags, args, 0);
887}
888
889int
Brian Fostera454f742012-11-06 09:50:39 -0500890xfs_inode_ag_iterator_tag(
891 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000892 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500893 void *args),
894 int flags,
895 void *args,
896 int tag)
897{
898 struct xfs_perag *pag;
899 int error = 0;
900 int last_error = 0;
901 xfs_agnumber_t ag;
902
903 ag = 0;
904 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
905 ag = pag->pag_agno + 1;
Brian Foster2ea882d2017-04-26 08:30:39 -0700906 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag,
907 0);
Dave Chinner5017e972010-01-11 11:47:40 +0000908 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200909 if (error) {
910 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000911 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200912 break;
913 }
914 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000915 return last_error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200916}
917
David Chinner76bf1052008-10-30 17:16:21 +1100918/*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000919 * Grab the inode for reclaim exclusively.
920 * Return 0 if we grabbed it, non-zero otherwise.
921 */
922STATIC int
923xfs_reclaim_inode_grab(
924 struct xfs_inode *ip,
925 int flags)
926{
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100927 ASSERT(rcu_read_lock_held());
928
929 /* quick check for stale RCU freed inode */
930 if (!ip->i_ino)
931 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000932
933 /*
Christoph Hellwig474fce02011-12-18 20:00:09 +0000934 * If we are asked for non-blocking operation, do unlocked checks to
935 * see if the inode already is being flushed or in reclaim to avoid
936 * lock traffic.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000937 */
938 if ((flags & SYNC_TRYLOCK) &&
Christoph Hellwig474fce02011-12-18 20:00:09 +0000939 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
Dave Chinnere3a20c02010-09-24 19:51:50 +1000940 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000941
942 /*
943 * The radix tree lock here protects a thread in xfs_iget from racing
944 * with us starting reclaim on the inode. Once we have the
945 * XFS_IRECLAIM flag set it will not touch us.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100946 *
947 * Due to RCU lookup, we may find inodes that have been freed and only
948 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
949 * aren't candidates for reclaim at all, so we must check the
950 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000951 */
952 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100953 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
954 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
955 /* not a reclaim candidate. */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000956 spin_unlock(&ip->i_flags_lock);
957 return 1;
958 }
959 __xfs_iflags_set(ip, XFS_IRECLAIM);
960 spin_unlock(&ip->i_flags_lock);
961 return 0;
962}
963
964/*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000965 * Inodes in different states need to be treated differently. The following
966 * table lists the inode states and the reclaim actions necessary:
Dave Chinner777df5a2010-02-06 12:37:26 +1100967 *
968 * inode state iflush ret required action
969 * --------------- ---------- ---------------
970 * bad - reclaim
971 * shutdown EIO unpin and reclaim
972 * clean, unpinned 0 reclaim
973 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100974 * clean, pinned(*) 0 requeue
975 * stale, pinned EAGAIN requeue
Christoph Hellwig8a480882012-04-23 15:58:35 +1000976 * dirty, async - requeue
977 * dirty, sync 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100978 *
979 * (*) dgc: I don't think the clean, pinned state is possible but it gets
980 * handled anyway given the order of checks implemented.
981 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100982 * Also, because we get the flush lock first, we know that any inode that has
983 * been flushed delwri has had the flush completed by the time we check that
Christoph Hellwig8a480882012-04-23 15:58:35 +1000984 * the inode is clean.
Dave Chinnerc8543632010-02-06 12:39:36 +1100985 *
Christoph Hellwig8a480882012-04-23 15:58:35 +1000986 * Note that because the inode is flushed delayed write by AIL pushing, the
987 * flush lock may already be held here and waiting on it can result in very
988 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
989 * the caller should push the AIL first before trying to reclaim inodes to
990 * minimise the amount of time spent waiting. For background relaim, we only
991 * bother to reclaim clean inodes anyway.
Dave Chinnerc8543632010-02-06 12:39:36 +1100992 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100993 * Hence the order of actions after gaining the locks should be:
994 * bad => reclaim
995 * shutdown => unpin and reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000996 * pinned, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100997 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100998 * stale => reclaim
999 * clean => reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +10001000 * dirty, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +11001001 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +11001002 */
Dave Chinner75f3cb12009-06-08 15:35:14 +02001003STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001004xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +02001005 struct xfs_inode *ip,
1006 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001007 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +11001008{
Christoph Hellwig4c468192012-04-23 15:58:36 +10001009 struct xfs_buf *bp = NULL;
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001010 xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
Christoph Hellwig4c468192012-04-23 15:58:36 +10001011 int error;
Dave Chinner777df5a2010-02-06 12:37:26 +11001012
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001013restart:
1014 error = 0;
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001015 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +11001016 if (!xfs_iflock_nowait(ip)) {
1017 if (!(sync_mode & SYNC_WAIT))
1018 goto out;
1019 xfs_iflock(ip);
1020 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001021
Dave Chinner777df5a2010-02-06 12:37:26 +11001022 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1023 xfs_iunpin_wait(ip);
Brian Fosterb49ef752017-01-09 16:38:38 +01001024 /* xfs_iflush_abort() drops the flush lock */
Dave Chinner04913fd2012-04-23 15:58:41 +10001025 xfs_iflush_abort(ip, false);
Dave Chinner777df5a2010-02-06 12:37:26 +11001026 goto reclaim;
1027 }
Dave Chinnerc8543632010-02-06 12:39:36 +11001028 if (xfs_ipincount(ip)) {
Christoph Hellwig8a480882012-04-23 15:58:35 +10001029 if (!(sync_mode & SYNC_WAIT))
1030 goto out_ifunlock;
Dave Chinner777df5a2010-02-06 12:37:26 +11001031 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +11001032 }
Brian Fosterb49ef752017-01-09 16:38:38 +01001033 if (xfs_iflags_test(ip, XFS_ISTALE) || xfs_inode_clean(ip)) {
1034 xfs_ifunlock(ip);
Dave Chinner777df5a2010-02-06 12:37:26 +11001035 goto reclaim;
Brian Fosterb49ef752017-01-09 16:38:38 +01001036 }
Dave Chinner777df5a2010-02-06 12:37:26 +11001037
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001038 /*
Christoph Hellwig8a480882012-04-23 15:58:35 +10001039 * Never flush out dirty data during non-blocking reclaim, as it would
1040 * just contend with AIL pushing trying to do the same job.
1041 */
1042 if (!(sync_mode & SYNC_WAIT))
1043 goto out_ifunlock;
1044
1045 /*
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001046 * Now we have an inode that needs flushing.
1047 *
Christoph Hellwig4c468192012-04-23 15:58:36 +10001048 * Note that xfs_iflush will never block on the inode buffer lock, as
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001049 * xfs_ifree_cluster() can lock the inode buffer before it locks the
Christoph Hellwig4c468192012-04-23 15:58:36 +10001050 * ip->i_lock, and we are doing the exact opposite here. As a result,
Christoph Hellwig475ee412012-07-03 12:21:22 -04001051 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
1052 * result in an ABBA deadlock with xfs_ifree_cluster().
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001053 *
1054 * As xfs_ifree_cluser() must gather all inodes that are active in the
1055 * cache to mark them stale, if we hit this case we don't actually want
1056 * to do IO here - we want the inode marked stale so we can simply
Christoph Hellwig4c468192012-04-23 15:58:36 +10001057 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
1058 * inode, back off and try again. Hopefully the next pass through will
1059 * see the stale flag set on the inode.
Dave Chinner1bfd8d02011-03-26 09:13:55 +11001060 */
Christoph Hellwig4c468192012-04-23 15:58:36 +10001061 error = xfs_iflush(ip, &bp);
Dave Chinner24513372014-06-25 14:58:08 +10001062 if (error == -EAGAIN) {
Christoph Hellwig8a480882012-04-23 15:58:35 +10001063 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1064 /* backoff longer than in xfs_ifree_cluster */
1065 delay(2);
1066 goto restart;
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001067 }
Dave Chinnerc8543632010-02-06 12:39:36 +11001068
Christoph Hellwig4c468192012-04-23 15:58:36 +10001069 if (!error) {
1070 error = xfs_bwrite(bp);
1071 xfs_buf_relse(bp);
1072 }
1073
Dave Chinner777df5a2010-02-06 12:37:26 +11001074reclaim:
Brian Fosterb49ef752017-01-09 16:38:38 +01001075 ASSERT(!xfs_isiflocked(ip));
1076
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001077 /*
1078 * Because we use RCU freeing we need to ensure the inode always appears
1079 * to be reclaimed with an invalid inode number when in the free state.
Brian Fosterb49ef752017-01-09 16:38:38 +01001080 * We do this as early as possible under the ILOCK so that
Omar Sandoval81286ad2017-09-17 14:06:58 -07001081 * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to
1082 * detect races with us here. By doing this, we guarantee that once
1083 * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that
1084 * it will see either a valid inode that will serialise correctly, or it
1085 * will see an invalid inode that it can skip.
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001086 */
1087 spin_lock(&ip->i_flags_lock);
1088 ip->i_flags = XFS_IRECLAIM;
1089 ip->i_ino = 0;
1090 spin_unlock(&ip->i_flags_lock);
1091
Dave Chinnerc8e20be2010-01-10 23:51:45 +00001092 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001093
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001094 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001095 /*
1096 * Remove the inode from the per-AG radix tree.
1097 *
1098 * Because radix_tree_delete won't complain even if the item was never
1099 * added to the tree assert that it's been there before to catch
1100 * problems with the inode life time early on.
1101 */
Dave Chinner1a427ab2010-12-16 17:08:41 +11001102 spin_lock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001103 if (!radix_tree_delete(&pag->pag_ici_root,
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001104 XFS_INO_TO_AGINO(ip->i_mount, ino)))
Dave Chinner2f11fea2010-07-20 17:53:25 +10001105 ASSERT(0);
Dave Chinner545c0882016-05-18 14:11:41 +10001106 xfs_perag_clear_reclaim_tag(pag);
Dave Chinner1a427ab2010-12-16 17:08:41 +11001107 spin_unlock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001108
1109 /*
1110 * Here we do an (almost) spurious inode lock in order to coordinate
1111 * with inode cache radix tree lookups. This is because the lookup
1112 * can reference the inodes in the cache without taking references.
1113 *
1114 * We make that OK here by ensuring that we wait until the inode is
Alex Elderad637a12012-02-16 22:01:00 +00001115 * unlocked after the lookup before we go ahead and free it.
Dave Chinner2f11fea2010-07-20 17:53:25 +10001116 */
Alex Elderad637a12012-02-16 22:01:00 +00001117 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001118 xfs_qm_dqdetach(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001119 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001120
Dave Chinner8a17d7d2016-05-18 14:09:12 +10001121 __xfs_inode_free(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001122 return error;
Christoph Hellwig8a480882012-04-23 15:58:35 +10001123
1124out_ifunlock:
1125 xfs_ifunlock(ip);
1126out:
1127 xfs_iflags_clear(ip, XFS_IRECLAIM);
1128 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1129 /*
Dave Chinner24513372014-06-25 14:58:08 +10001130 * We could return -EAGAIN here to make reclaim rescan the inode tree in
Christoph Hellwig8a480882012-04-23 15:58:35 +10001131 * a short while. However, this just burns CPU time scanning the tree
Dave Chinner58896082012-10-08 21:56:05 +11001132 * waiting for IO to complete and the reclaim work never goes back to
1133 * the idle state. Instead, return 0 to let the next scheduled
1134 * background reclaim attempt to reclaim the inode again.
Christoph Hellwig8a480882012-04-23 15:58:35 +10001135 */
1136 return 0;
David Chinner7a3be022008-10-30 17:37:37 +11001137}
1138
Dave Chinner65d0f202010-09-24 18:40:15 +10001139/*
1140 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1141 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1142 * then a shut down during filesystem unmount reclaim walk leak all the
1143 * unreclaimed inodes.
1144 */
Dave Chinner33479e02012-10-08 21:56:11 +11001145STATIC int
Dave Chinner65d0f202010-09-24 18:40:15 +10001146xfs_reclaim_inodes_ag(
1147 struct xfs_mount *mp,
1148 int flags,
1149 int *nr_to_scan)
1150{
1151 struct xfs_perag *pag;
1152 int error = 0;
1153 int last_error = 0;
1154 xfs_agnumber_t ag;
Dave Chinner69b491c2010-09-27 11:09:51 +10001155 int trylock = flags & SYNC_TRYLOCK;
1156 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +10001157
Dave Chinner69b491c2010-09-27 11:09:51 +10001158restart:
Dave Chinner65d0f202010-09-24 18:40:15 +10001159 ag = 0;
Dave Chinner69b491c2010-09-27 11:09:51 +10001160 skipped = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001161 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1162 unsigned long first_index = 0;
1163 int done = 0;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001164 int nr_found = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001165
1166 ag = pag->pag_agno + 1;
1167
Dave Chinner69b491c2010-09-27 11:09:51 +10001168 if (trylock) {
1169 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1170 skipped++;
Dave Chinnerf83282a2010-11-08 08:55:04 +00001171 xfs_perag_put(pag);
Dave Chinner69b491c2010-09-27 11:09:51 +10001172 continue;
1173 }
1174 first_index = pag->pag_ici_reclaim_cursor;
1175 } else
1176 mutex_lock(&pag->pag_ici_reclaim_lock);
1177
Dave Chinner65d0f202010-09-24 18:40:15 +10001178 do {
Dave Chinnere3a20c02010-09-24 19:51:50 +10001179 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1180 int i;
Dave Chinner65d0f202010-09-24 18:40:15 +10001181
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001182 rcu_read_lock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001183 nr_found = radix_tree_gang_lookup_tag(
1184 &pag->pag_ici_root,
1185 (void **)batch, first_index,
1186 XFS_LOOKUP_BATCH,
Dave Chinner65d0f202010-09-24 18:40:15 +10001187 XFS_ICI_RECLAIM_TAG);
1188 if (!nr_found) {
Dave Chinnerb2232212011-05-06 02:54:04 +00001189 done = 1;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001190 rcu_read_unlock();
Dave Chinner65d0f202010-09-24 18:40:15 +10001191 break;
1192 }
1193
1194 /*
Dave Chinnere3a20c02010-09-24 19:51:50 +10001195 * Grab the inodes before we drop the lock. if we found
1196 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +10001197 */
Dave Chinnere3a20c02010-09-24 19:51:50 +10001198 for (i = 0; i < nr_found; i++) {
1199 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +10001200
Dave Chinnere3a20c02010-09-24 19:51:50 +10001201 if (done || xfs_reclaim_inode_grab(ip, flags))
1202 batch[i] = NULL;
Dave Chinner65d0f202010-09-24 18:40:15 +10001203
Dave Chinnere3a20c02010-09-24 19:51:50 +10001204 /*
1205 * Update the index for the next lookup. Catch
1206 * overflows into the next AG range which can
1207 * occur if we have inodes in the last block of
1208 * the AG and we are currently pointing to the
1209 * last inode.
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001210 *
1211 * Because we may see inodes that are from the
1212 * wrong AG due to RCU freeing and
1213 * reallocation, only update the index if it
1214 * lies in this AG. It was a race that lead us
1215 * to see this inode, so another lookup from
1216 * the same index will not find it again.
Dave Chinnere3a20c02010-09-24 19:51:50 +10001217 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001218 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1219 pag->pag_agno)
1220 continue;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001221 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1222 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1223 done = 1;
1224 }
1225
1226 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001227 rcu_read_unlock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001228
1229 for (i = 0; i < nr_found; i++) {
1230 if (!batch[i])
1231 continue;
1232 error = xfs_reclaim_inode(batch[i], pag, flags);
Dave Chinner24513372014-06-25 14:58:08 +10001233 if (error && last_error != -EFSCORRUPTED)
Dave Chinnere3a20c02010-09-24 19:51:50 +10001234 last_error = error;
1235 }
1236
1237 *nr_to_scan -= XFS_LOOKUP_BATCH;
1238
Dave Chinner8daaa832011-07-08 14:14:46 +10001239 cond_resched();
1240
Dave Chinnere3a20c02010-09-24 19:51:50 +10001241 } while (nr_found && !done && *nr_to_scan > 0);
Dave Chinner65d0f202010-09-24 18:40:15 +10001242
Dave Chinner69b491c2010-09-27 11:09:51 +10001243 if (trylock && !done)
1244 pag->pag_ici_reclaim_cursor = first_index;
1245 else
1246 pag->pag_ici_reclaim_cursor = 0;
1247 mutex_unlock(&pag->pag_ici_reclaim_lock);
Dave Chinner65d0f202010-09-24 18:40:15 +10001248 xfs_perag_put(pag);
1249 }
Dave Chinner69b491c2010-09-27 11:09:51 +10001250
1251 /*
1252 * if we skipped any AG, and we still have scan count remaining, do
1253 * another pass this time using blocking reclaim semantics (i.e
1254 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1255 * ensure that when we get more reclaimers than AGs we block rather
1256 * than spin trying to execute reclaim.
1257 */
Dave Chinner8daaa832011-07-08 14:14:46 +10001258 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
Dave Chinner69b491c2010-09-27 11:09:51 +10001259 trylock = 0;
1260 goto restart;
1261 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001262 return last_error;
Dave Chinner65d0f202010-09-24 18:40:15 +10001263}
1264
David Chinnerfce08f22008-10-30 17:37:03 +11001265int
David Chinner1dc33182008-10-30 17:37:15 +11001266xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +11001267 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +11001268 int mode)
1269{
Dave Chinner65d0f202010-09-24 18:40:15 +10001270 int nr_to_scan = INT_MAX;
1271
1272 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001273}
1274
1275/*
Dave Chinner8daaa832011-07-08 14:14:46 +10001276 * Scan a certain number of inodes for reclaim.
Dave Chinnera7b339f2011-04-08 12:45:07 +10001277 *
1278 * When called we make sure that there is a background (fast) inode reclaim in
Dave Chinner8daaa832011-07-08 14:14:46 +10001279 * progress, while we will throttle the speed of reclaim via doing synchronous
Dave Chinnera7b339f2011-04-08 12:45:07 +10001280 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1281 * them to be cleaned, which we hope will not be very long due to the
1282 * background walker having already kicked the IO off on those dirty inodes.
Dave Chinner9bf729c2010-04-29 09:55:50 +10001283 */
Dave Chinner0a234c62013-08-28 10:17:57 +10001284long
Dave Chinner8daaa832011-07-08 14:14:46 +10001285xfs_reclaim_inodes_nr(
1286 struct xfs_mount *mp,
1287 int nr_to_scan)
Dave Chinner9bf729c2010-04-29 09:55:50 +10001288{
Dave Chinner8daaa832011-07-08 14:14:46 +10001289 /* kick background reclaimer and push the AIL */
Dave Chinner58896082012-10-08 21:56:05 +11001290 xfs_reclaim_work_queue(mp);
Dave Chinner8daaa832011-07-08 14:14:46 +10001291 xfs_ail_push_all(mp->m_ail);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001292
Dave Chinner0a234c62013-08-28 10:17:57 +10001293 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
Dave Chinner8daaa832011-07-08 14:14:46 +10001294}
Dave Chinnera7b339f2011-04-08 12:45:07 +10001295
Dave Chinner8daaa832011-07-08 14:14:46 +10001296/*
1297 * Return the number of reclaimable inodes in the filesystem for
1298 * the shrinker to determine how much to reclaim.
1299 */
1300int
1301xfs_reclaim_inodes_count(
1302 struct xfs_mount *mp)
1303{
1304 struct xfs_perag *pag;
1305 xfs_agnumber_t ag = 0;
1306 int reclaimable = 0;
Dave Chinner9bf729c2010-04-29 09:55:50 +10001307
Dave Chinner65d0f202010-09-24 18:40:15 +10001308 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1309 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +10001310 reclaimable += pag->pag_ici_reclaimable;
1311 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001312 }
Dave Chinner9bf729c2010-04-29 09:55:50 +10001313 return reclaimable;
1314}
1315
Brian Foster41176a62012-11-06 09:50:42 -05001316STATIC int
Brian Foster3e3f9f52012-11-07 12:21:13 -05001317xfs_inode_match_id(
1318 struct xfs_inode *ip,
1319 struct xfs_eofblocks *eofb)
1320{
Dwight Engenb9fe5052013-08-15 14:08:02 -04001321 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1322 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
Brian Foster1b556042012-11-06 09:50:45 -05001323 return 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001324
Dwight Engenb9fe5052013-08-15 14:08:02 -04001325 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1326 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
Brian Foster1b556042012-11-06 09:50:45 -05001327 return 0;
1328
Dwight Engenb9fe5052013-08-15 14:08:02 -04001329 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
Brian Foster1b556042012-11-06 09:50:45 -05001330 xfs_get_projid(ip) != eofb->eof_prid)
1331 return 0;
1332
1333 return 1;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001334}
1335
Brian Fosterf4526392014-07-24 19:44:28 +10001336/*
1337 * A union-based inode filtering algorithm. Process the inode if any of the
1338 * criteria match. This is for global/internal scans only.
1339 */
1340STATIC int
1341xfs_inode_match_id_union(
1342 struct xfs_inode *ip,
1343 struct xfs_eofblocks *eofb)
1344{
1345 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1346 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1347 return 1;
1348
1349 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1350 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1351 return 1;
1352
1353 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1354 xfs_get_projid(ip) == eofb->eof_prid)
1355 return 1;
1356
1357 return 0;
1358}
1359
Brian Foster3e3f9f52012-11-07 12:21:13 -05001360STATIC int
Brian Foster41176a62012-11-06 09:50:42 -05001361xfs_inode_free_eofblocks(
1362 struct xfs_inode *ip,
Brian Foster41176a62012-11-06 09:50:42 -05001363 int flags,
1364 void *args)
1365{
Brian Foster798b1dc2017-01-27 23:22:55 -08001366 int ret = 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001367 struct xfs_eofblocks *eofb = args;
Brian Fosterf4526392014-07-24 19:44:28 +10001368 int match;
Brian Foster5400da72014-07-24 19:40:22 +10001369
Brian Foster41176a62012-11-06 09:50:42 -05001370 if (!xfs_can_free_eofblocks(ip, false)) {
1371 /* inode could be preallocated or append-only */
1372 trace_xfs_inode_free_eofblocks_invalid(ip);
1373 xfs_inode_clear_eofblocks_tag(ip);
1374 return 0;
1375 }
1376
1377 /*
1378 * If the mapping is dirty the operation can block and wait for some
1379 * time. Unless we are waiting, skip it.
1380 */
1381 if (!(flags & SYNC_WAIT) &&
1382 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1383 return 0;
1384
Brian Foster00ca79a2012-11-07 12:21:14 -05001385 if (eofb) {
Brian Fosterf4526392014-07-24 19:44:28 +10001386 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1387 match = xfs_inode_match_id_union(ip, eofb);
1388 else
1389 match = xfs_inode_match_id(ip, eofb);
1390 if (!match)
Brian Foster00ca79a2012-11-07 12:21:14 -05001391 return 0;
1392
1393 /* skip the inode if the file size is too small */
1394 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1395 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1396 return 0;
1397 }
Brian Foster3e3f9f52012-11-07 12:21:13 -05001398
Brian Foster798b1dc2017-01-27 23:22:55 -08001399 /*
1400 * If the caller is waiting, return -EAGAIN to keep the background
1401 * scanner moving and revisit the inode in a subsequent pass.
1402 */
Brian Foster4d725d72017-01-27 23:22:56 -08001403 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
Brian Foster798b1dc2017-01-27 23:22:55 -08001404 if (flags & SYNC_WAIT)
1405 ret = -EAGAIN;
1406 return ret;
1407 }
1408 ret = xfs_free_eofblocks(ip);
Brian Foster4d725d72017-01-27 23:22:56 -08001409 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Brian Foster41176a62012-11-06 09:50:42 -05001410
1411 return ret;
1412}
1413
Darrick J. Wong83104d42016-10-03 09:11:46 -07001414static int
1415__xfs_icache_free_eofblocks(
Brian Foster41176a62012-11-06 09:50:42 -05001416 struct xfs_mount *mp,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001417 struct xfs_eofblocks *eofb,
1418 int (*execute)(struct xfs_inode *ip, int flags,
1419 void *args),
1420 int tag)
Brian Foster41176a62012-11-06 09:50:42 -05001421{
Brian Foster8ca149d2012-11-07 12:21:12 -05001422 int flags = SYNC_TRYLOCK;
1423
1424 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1425 flags = SYNC_WAIT;
1426
Darrick J. Wong83104d42016-10-03 09:11:46 -07001427 return xfs_inode_ag_iterator_tag(mp, execute, flags,
1428 eofb, tag);
1429}
1430
1431int
1432xfs_icache_free_eofblocks(
1433 struct xfs_mount *mp,
1434 struct xfs_eofblocks *eofb)
1435{
1436 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_eofblocks,
1437 XFS_ICI_EOFBLOCKS_TAG);
Brian Foster41176a62012-11-06 09:50:42 -05001438}
1439
Brian Fosterdc06f3982014-07-24 19:49:28 +10001440/*
1441 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1442 * multiple quotas, we don't know exactly which quota caused an allocation
1443 * failure. We make a best effort by including each quota under low free space
1444 * conditions (less than 1% free space) in the scan.
1445 */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001446static int
1447__xfs_inode_free_quota_eofblocks(
1448 struct xfs_inode *ip,
1449 int (*execute)(struct xfs_mount *mp,
1450 struct xfs_eofblocks *eofb))
Brian Fosterdc06f3982014-07-24 19:49:28 +10001451{
1452 int scan = 0;
1453 struct xfs_eofblocks eofb = {0};
1454 struct xfs_dquot *dq;
1455
Brian Fosterdc06f3982014-07-24 19:49:28 +10001456 /*
Brian Foster4d725d72017-01-27 23:22:56 -08001457 * Run a sync scan to increase effectiveness and use the union filter to
Brian Fosterdc06f3982014-07-24 19:49:28 +10001458 * cover all applicable quotas in a single scan.
1459 */
Brian Fosterdc06f3982014-07-24 19:49:28 +10001460 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1461
1462 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1463 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1464 if (dq && xfs_dquot_lowsp(dq)) {
1465 eofb.eof_uid = VFS_I(ip)->i_uid;
1466 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1467 scan = 1;
1468 }
1469 }
1470
1471 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1472 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1473 if (dq && xfs_dquot_lowsp(dq)) {
1474 eofb.eof_gid = VFS_I(ip)->i_gid;
1475 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1476 scan = 1;
1477 }
1478 }
1479
1480 if (scan)
Darrick J. Wong83104d42016-10-03 09:11:46 -07001481 execute(ip->i_mount, &eofb);
Brian Fosterdc06f3982014-07-24 19:49:28 +10001482
1483 return scan;
1484}
1485
Darrick J. Wong83104d42016-10-03 09:11:46 -07001486int
1487xfs_inode_free_quota_eofblocks(
1488 struct xfs_inode *ip)
1489{
1490 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks);
1491}
1492
1493static void
1494__xfs_inode_set_eofblocks_tag(
1495 xfs_inode_t *ip,
1496 void (*execute)(struct xfs_mount *mp),
1497 void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1498 int error, unsigned long caller_ip),
1499 int tag)
Brian Foster27b52862012-11-06 09:50:38 -05001500{
1501 struct xfs_mount *mp = ip->i_mount;
1502 struct xfs_perag *pag;
1503 int tagged;
1504
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001505 /*
1506 * Don't bother locking the AG and looking up in the radix trees
1507 * if we already know that we have the tag set.
1508 */
1509 if (ip->i_flags & XFS_IEOFBLOCKS)
1510 return;
1511 spin_lock(&ip->i_flags_lock);
1512 ip->i_flags |= XFS_IEOFBLOCKS;
1513 spin_unlock(&ip->i_flags_lock);
1514
Brian Foster27b52862012-11-06 09:50:38 -05001515 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1516 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001517
Darrick J. Wong83104d42016-10-03 09:11:46 -07001518 tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
Brian Foster27b52862012-11-06 09:50:38 -05001519 radix_tree_tag_set(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001520 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
Brian Foster27b52862012-11-06 09:50:38 -05001521 if (!tagged) {
1522 /* propagate the eofblocks tag up into the perag radix tree */
1523 spin_lock(&ip->i_mount->m_perag_lock);
1524 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1525 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001526 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001527 spin_unlock(&ip->i_mount->m_perag_lock);
1528
Brian Foster579b62f2012-11-06 09:50:47 -05001529 /* kick off background trimming */
Darrick J. Wong83104d42016-10-03 09:11:46 -07001530 execute(ip->i_mount);
Brian Foster579b62f2012-11-06 09:50:47 -05001531
Darrick J. Wong83104d42016-10-03 09:11:46 -07001532 set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001533 }
1534
1535 spin_unlock(&pag->pag_ici_lock);
1536 xfs_perag_put(pag);
1537}
1538
1539void
Darrick J. Wong83104d42016-10-03 09:11:46 -07001540xfs_inode_set_eofblocks_tag(
Brian Foster27b52862012-11-06 09:50:38 -05001541 xfs_inode_t *ip)
1542{
Darrick J. Wong83104d42016-10-03 09:11:46 -07001543 trace_xfs_inode_set_eofblocks_tag(ip);
1544 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_eofblocks,
1545 trace_xfs_perag_set_eofblocks,
1546 XFS_ICI_EOFBLOCKS_TAG);
1547}
1548
1549static void
1550__xfs_inode_clear_eofblocks_tag(
1551 xfs_inode_t *ip,
1552 void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1553 int error, unsigned long caller_ip),
1554 int tag)
1555{
Brian Foster27b52862012-11-06 09:50:38 -05001556 struct xfs_mount *mp = ip->i_mount;
1557 struct xfs_perag *pag;
1558
Christoph Hellwig85a6e762016-09-19 11:09:48 +10001559 spin_lock(&ip->i_flags_lock);
1560 ip->i_flags &= ~XFS_IEOFBLOCKS;
1561 spin_unlock(&ip->i_flags_lock);
1562
Brian Foster27b52862012-11-06 09:50:38 -05001563 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1564 spin_lock(&pag->pag_ici_lock);
Brian Foster27b52862012-11-06 09:50:38 -05001565
1566 radix_tree_tag_clear(&pag->pag_ici_root,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001567 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
1568 if (!radix_tree_tagged(&pag->pag_ici_root, tag)) {
Brian Foster27b52862012-11-06 09:50:38 -05001569 /* clear the eofblocks tag from the perag radix tree */
1570 spin_lock(&ip->i_mount->m_perag_lock);
1571 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1572 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
Darrick J. Wong83104d42016-10-03 09:11:46 -07001573 tag);
Brian Foster27b52862012-11-06 09:50:38 -05001574 spin_unlock(&ip->i_mount->m_perag_lock);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001575 clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
Brian Foster27b52862012-11-06 09:50:38 -05001576 }
1577
1578 spin_unlock(&pag->pag_ici_lock);
1579 xfs_perag_put(pag);
1580}
1581
Darrick J. Wong83104d42016-10-03 09:11:46 -07001582void
1583xfs_inode_clear_eofblocks_tag(
1584 xfs_inode_t *ip)
1585{
1586 trace_xfs_inode_clear_eofblocks_tag(ip);
1587 return __xfs_inode_clear_eofblocks_tag(ip,
1588 trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG);
1589}
1590
1591/*
1592 * Automatic CoW Reservation Freeing
1593 *
1594 * These functions automatically garbage collect leftover CoW reservations
1595 * that were made on behalf of a cowextsize hint when we start to run out
1596 * of quota or when the reservations sit around for too long. If the file
1597 * has dirty pages or is undergoing writeback, its CoW reservations will
1598 * be retained.
1599 *
1600 * The actual garbage collection piggybacks off the same code that runs
1601 * the speculative EOF preallocation garbage collector.
1602 */
1603STATIC int
1604xfs_inode_free_cowblocks(
1605 struct xfs_inode *ip,
1606 int flags,
1607 void *args)
1608{
1609 int ret;
1610 struct xfs_eofblocks *eofb = args;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001611 int match;
Brian Foster2f092422017-01-09 16:38:34 +01001612 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001613
Brian Foster2f092422017-01-09 16:38:34 +01001614 /*
1615 * Just clear the tag if we have an empty cow fork or none at all. It's
1616 * possible the inode was fully unshared since it was originally tagged.
1617 */
1618 if (!xfs_is_reflink_inode(ip) || !ifp->if_bytes) {
Darrick J. Wong83104d42016-10-03 09:11:46 -07001619 trace_xfs_inode_free_cowblocks_invalid(ip);
1620 xfs_inode_clear_cowblocks_tag(ip);
1621 return 0;
1622 }
1623
1624 /*
1625 * If the mapping is dirty or under writeback we cannot touch the
1626 * CoW fork. Leave it alone if we're in the midst of a directio.
1627 */
Christoph Hellwig91192ae2017-01-09 16:39:02 +01001628 if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
1629 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
Darrick J. Wong83104d42016-10-03 09:11:46 -07001630 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
1631 atomic_read(&VFS_I(ip)->i_dio_count))
1632 return 0;
1633
1634 if (eofb) {
1635 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1636 match = xfs_inode_match_id_union(ip, eofb);
1637 else
1638 match = xfs_inode_match_id(ip, eofb);
1639 if (!match)
1640 return 0;
1641
1642 /* skip the inode if the file size is too small */
1643 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1644 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1645 return 0;
Darrick J. Wong83104d42016-10-03 09:11:46 -07001646 }
1647
1648 /* Free the CoW blocks */
Brian Foster4d725d72017-01-27 23:22:56 -08001649 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1650 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001651
Christoph Hellwig3b83a022017-03-07 16:45:58 -08001652 ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001653
Brian Foster4d725d72017-01-27 23:22:56 -08001654 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1655 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001656
1657 return ret;
1658}
1659
1660int
1661xfs_icache_free_cowblocks(
1662 struct xfs_mount *mp,
1663 struct xfs_eofblocks *eofb)
1664{
1665 return __xfs_icache_free_eofblocks(mp, eofb, xfs_inode_free_cowblocks,
1666 XFS_ICI_COWBLOCKS_TAG);
1667}
1668
1669int
1670xfs_inode_free_quota_cowblocks(
1671 struct xfs_inode *ip)
1672{
1673 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks);
1674}
1675
1676void
1677xfs_inode_set_cowblocks_tag(
1678 xfs_inode_t *ip)
1679{
Brian Foster7b7381f2016-10-24 14:21:00 +11001680 trace_xfs_inode_set_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001681 return __xfs_inode_set_eofblocks_tag(ip, xfs_queue_cowblocks,
Brian Foster7b7381f2016-10-24 14:21:00 +11001682 trace_xfs_perag_set_cowblocks,
Darrick J. Wong83104d42016-10-03 09:11:46 -07001683 XFS_ICI_COWBLOCKS_TAG);
1684}
1685
1686void
1687xfs_inode_clear_cowblocks_tag(
1688 xfs_inode_t *ip)
1689{
Brian Foster7b7381f2016-10-24 14:21:00 +11001690 trace_xfs_inode_clear_cowblocks_tag(ip);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001691 return __xfs_inode_clear_eofblocks_tag(ip,
Brian Foster7b7381f2016-10-24 14:21:00 +11001692 trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG);
Darrick J. Wong83104d42016-10-03 09:11:46 -07001693}