blob: 4c184f70d43ca010ffc8238bb7a963d2718f4243 [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"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110036
David Chinnera167b172008-10-30 17:06:18 +110037#include <linux/kthread.h>
38#include <linux/freezer.h>
39
Dave Chinner33479e02012-10-08 21:56:11 +110040STATIC void __xfs_inode_clear_reclaim_tag(struct xfs_mount *mp,
41 struct xfs_perag *pag, struct xfs_inode *ip);
42
43/*
44 * Allocate and initialise an xfs_inode.
45 */
Dave Chinner638f44162013-08-30 10:23:45 +100046struct xfs_inode *
Dave Chinner33479e02012-10-08 21:56:11 +110047xfs_inode_alloc(
48 struct xfs_mount *mp,
49 xfs_ino_t ino)
50{
51 struct xfs_inode *ip;
52
53 /*
54 * if this didn't occur in transactions, we could use
55 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
56 * code up to do this anyway.
57 */
58 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
59 if (!ip)
60 return NULL;
61 if (inode_init_always(mp->m_super, VFS_I(ip))) {
62 kmem_zone_free(xfs_inode_zone, ip);
63 return NULL;
64 }
65
Bill O'Donnellff6d6af2015-10-12 18:21:22 +110066 XFS_STATS_INC(mp, vn_active);
Dave Chinner33479e02012-10-08 21:56:11 +110067 ASSERT(atomic_read(&ip->i_pincount) == 0);
68 ASSERT(!spin_is_locked(&ip->i_flags_lock));
69 ASSERT(!xfs_isiflocked(ip));
70 ASSERT(ip->i_ino == 0);
71
72 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
73
74 /* initialise the xfs inode */
75 ip->i_ino = ino;
76 ip->i_mount = mp;
77 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
78 ip->i_afp = NULL;
79 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
80 ip->i_flags = 0;
81 ip->i_delayed_blks = 0;
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +110082 memset(&ip->i_d, 0, sizeof(ip->i_d));
Dave Chinner33479e02012-10-08 21:56:11 +110083
84 return ip;
85}
86
87STATIC void
88xfs_inode_free_callback(
89 struct rcu_head *head)
90{
91 struct inode *inode = container_of(head, struct inode, i_rcu);
92 struct xfs_inode *ip = XFS_I(inode);
93
94 kmem_zone_free(xfs_inode_zone, ip);
95}
96
Dave Chinner638f44162013-08-30 10:23:45 +100097void
Dave Chinner33479e02012-10-08 21:56:11 +110098xfs_inode_free(
99 struct xfs_inode *ip)
100{
101 switch (ip->i_d.di_mode & S_IFMT) {
102 case S_IFREG:
103 case S_IFDIR:
104 case S_IFLNK:
105 xfs_idestroy_fork(ip, XFS_DATA_FORK);
106 break;
107 }
108
109 if (ip->i_afp)
110 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
111
112 if (ip->i_itemp) {
113 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
114 xfs_inode_item_destroy(ip);
115 ip->i_itemp = NULL;
116 }
117
Dave Chinner33479e02012-10-08 21:56:11 +1100118 /*
119 * Because we use RCU freeing we need to ensure the inode always
120 * appears to be reclaimed with an invalid inode number when in the
121 * free state. The ip->i_flags_lock provides the barrier against lookup
122 * races.
123 */
124 spin_lock(&ip->i_flags_lock);
125 ip->i_flags = XFS_IRECLAIM;
126 ip->i_ino = 0;
127 spin_unlock(&ip->i_flags_lock);
128
Dave Chinnerb313a5f2013-09-24 16:01:14 +1000129 /* asserts to verify all state is correct here */
130 ASSERT(atomic_read(&ip->i_pincount) == 0);
131 ASSERT(!xfs_isiflocked(ip));
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100132 XFS_STATS_DEC(ip->i_mount, vn_active);
Dave Chinnerb313a5f2013-09-24 16:01:14 +1000133
Dave Chinner33479e02012-10-08 21:56:11 +1100134 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
135}
136
137/*
Dave Chinner50997472016-02-09 16:54:58 +1100138 * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
139 * part of the structure. This is made more complex by the fact we store
140 * information about the on-disk values in the VFS inode and so we can't just
141 * overwrite it's values unconditionally. Hence we save the parameters we
142 * need to retain across reinitialisation, and rewrite them into the VFS inode
143 * after resetting it's state even if resetting fails.
144 */
145static int
146xfs_reinit_inode(
147 struct xfs_mount *mp,
148 struct inode *inode)
149{
150 int error;
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100151 uint32_t nlink = inode->i_nlink;
Dave Chinner50997472016-02-09 16:54:58 +1100152
153 error = inode_init_always(mp->m_super, inode);
154
Dave Chinner54d7b5c2016-02-09 16:54:58 +1100155 set_nlink(inode, nlink);
Dave Chinner50997472016-02-09 16:54:58 +1100156 return error;
157}
158
159/*
Dave Chinner33479e02012-10-08 21:56:11 +1100160 * Check the validity of the inode we just found it the cache
161 */
162static int
163xfs_iget_cache_hit(
164 struct xfs_perag *pag,
165 struct xfs_inode *ip,
166 xfs_ino_t ino,
167 int flags,
168 int lock_flags) __releases(RCU)
169{
170 struct inode *inode = VFS_I(ip);
171 struct xfs_mount *mp = ip->i_mount;
172 int error;
173
174 /*
175 * check for re-use of an inode within an RCU grace period due to the
176 * radix tree nodes not being updated yet. We monitor for this by
177 * setting the inode number to zero before freeing the inode structure.
178 * If the inode has been reallocated and set up, then the inode number
179 * will not match, so check for that, too.
180 */
181 spin_lock(&ip->i_flags_lock);
182 if (ip->i_ino != ino) {
183 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100184 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000185 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100186 goto out_error;
187 }
188
189
190 /*
191 * If we are racing with another cache hit that is currently
192 * instantiating this inode or currently recycling it out of
193 * reclaimabe state, wait for the initialisation to complete
194 * before continuing.
195 *
196 * XXX(hch): eventually we should do something equivalent to
197 * wait_on_inode to wait for these flags to be cleared
198 * instead of polling for it.
199 */
200 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
201 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100202 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000203 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100204 goto out_error;
205 }
206
207 /*
208 * If lookup is racing with unlink return an error immediately.
209 */
210 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000211 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100212 goto out_error;
213 }
214
215 /*
216 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
217 * Need to carefully get it back into useable state.
218 */
219 if (ip->i_flags & XFS_IRECLAIMABLE) {
220 trace_xfs_iget_reclaim(ip);
221
222 /*
223 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
224 * from stomping over us while we recycle the inode. We can't
225 * clear the radix tree reclaimable tag yet as it requires
226 * pag_ici_lock to be held exclusive.
227 */
228 ip->i_flags |= XFS_IRECLAIM;
229
230 spin_unlock(&ip->i_flags_lock);
231 rcu_read_unlock();
232
Dave Chinner50997472016-02-09 16:54:58 +1100233 error = xfs_reinit_inode(mp, inode);
Dave Chinner33479e02012-10-08 21:56:11 +1100234 if (error) {
235 /*
236 * Re-initializing the inode failed, and we are in deep
237 * trouble. Try to re-add it to the reclaim list.
238 */
239 rcu_read_lock();
240 spin_lock(&ip->i_flags_lock);
241
242 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
243 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
244 trace_xfs_iget_reclaim_fail(ip);
245 goto out_error;
246 }
247
248 spin_lock(&pag->pag_ici_lock);
249 spin_lock(&ip->i_flags_lock);
250
251 /*
252 * Clear the per-lifetime state in the inode as we are now
253 * effectively a new inode and need to return to the initial
254 * state before reuse occurs.
255 */
256 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
257 ip->i_flags |= XFS_INEW;
258 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
259 inode->i_state = I_NEW;
260
261 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
262 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
263
264 spin_unlock(&ip->i_flags_lock);
265 spin_unlock(&pag->pag_ici_lock);
266 } else {
267 /* If the VFS inode is being torn down, pause and try again. */
268 if (!igrab(inode)) {
269 trace_xfs_iget_skip(ip);
Dave Chinner24513372014-06-25 14:58:08 +1000270 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100271 goto out_error;
272 }
273
274 /* We've got a live one. */
275 spin_unlock(&ip->i_flags_lock);
276 rcu_read_unlock();
277 trace_xfs_iget_hit(ip);
278 }
279
280 if (lock_flags != 0)
281 xfs_ilock(ip, lock_flags);
282
283 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100284 XFS_STATS_INC(mp, xs_ig_found);
Dave Chinner33479e02012-10-08 21:56:11 +1100285
286 return 0;
287
288out_error:
289 spin_unlock(&ip->i_flags_lock);
290 rcu_read_unlock();
291 return error;
292}
293
294
295static int
296xfs_iget_cache_miss(
297 struct xfs_mount *mp,
298 struct xfs_perag *pag,
299 xfs_trans_t *tp,
300 xfs_ino_t ino,
301 struct xfs_inode **ipp,
302 int flags,
303 int lock_flags)
304{
305 struct xfs_inode *ip;
306 int error;
307 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
308 int iflags;
309
310 ip = xfs_inode_alloc(mp, ino);
311 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +1000312 return -ENOMEM;
Dave Chinner33479e02012-10-08 21:56:11 +1100313
314 error = xfs_iread(mp, tp, ip, flags);
315 if (error)
316 goto out_destroy;
317
318 trace_xfs_iget_miss(ip);
319
320 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000321 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100322 goto out_destroy;
323 }
324
325 /*
326 * Preload the radix tree so we can insert safely under the
327 * write spinlock. Note that we cannot sleep inside the preload
328 * region. Since we can be called from transaction context, don't
329 * recurse into the file system.
330 */
331 if (radix_tree_preload(GFP_NOFS)) {
Dave Chinner24513372014-06-25 14:58:08 +1000332 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100333 goto out_destroy;
334 }
335
336 /*
337 * Because the inode hasn't been added to the radix-tree yet it can't
338 * be found by another thread, so we can do the non-sleeping lock here.
339 */
340 if (lock_flags) {
341 if (!xfs_ilock_nowait(ip, lock_flags))
342 BUG();
343 }
344
345 /*
346 * These values must be set before inserting the inode into the radix
347 * tree as the moment it is inserted a concurrent lookup (allowed by the
348 * RCU locking mechanism) can find it and that lookup must see that this
349 * is an inode currently under construction (i.e. that XFS_INEW is set).
350 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
351 * memory barrier that ensures this detection works correctly at lookup
352 * time.
353 */
354 iflags = XFS_INEW;
355 if (flags & XFS_IGET_DONTCACHE)
356 iflags |= XFS_IDONTCACHE;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500357 ip->i_udquot = NULL;
358 ip->i_gdquot = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500359 ip->i_pdquot = NULL;
Dave Chinner33479e02012-10-08 21:56:11 +1100360 xfs_iflags_set(ip, iflags);
361
362 /* insert the new inode */
363 spin_lock(&pag->pag_ici_lock);
364 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
365 if (unlikely(error)) {
366 WARN_ON(error != -EEXIST);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100367 XFS_STATS_INC(mp, xs_ig_dup);
Dave Chinner24513372014-06-25 14:58:08 +1000368 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100369 goto out_preload_end;
370 }
371 spin_unlock(&pag->pag_ici_lock);
372 radix_tree_preload_end();
373
374 *ipp = ip;
375 return 0;
376
377out_preload_end:
378 spin_unlock(&pag->pag_ici_lock);
379 radix_tree_preload_end();
380 if (lock_flags)
381 xfs_iunlock(ip, lock_flags);
382out_destroy:
383 __destroy_inode(VFS_I(ip));
384 xfs_inode_free(ip);
385 return error;
386}
387
388/*
389 * Look up an inode by number in the given file system.
390 * The inode is looked up in the cache held in each AG.
391 * If the inode is found in the cache, initialise the vfs inode
392 * if necessary.
393 *
394 * If it is not in core, read it in from the file system's device,
395 * add it to the cache and initialise the vfs inode.
396 *
397 * The inode is locked according to the value of the lock_flags parameter.
398 * This flag parameter indicates how and if the inode's IO lock and inode lock
399 * should be taken.
400 *
401 * mp -- the mount point structure for the current file system. It points
402 * to the inode hash table.
403 * tp -- a pointer to the current transaction if there is one. This is
404 * simply passed through to the xfs_iread() call.
405 * ino -- the number of the inode desired. This is the unique identifier
406 * within the file system for the inode being requested.
407 * lock_flags -- flags indicating how to lock the inode. See the comment
408 * for xfs_ilock() for a list of valid values.
409 */
410int
411xfs_iget(
412 xfs_mount_t *mp,
413 xfs_trans_t *tp,
414 xfs_ino_t ino,
415 uint flags,
416 uint lock_flags,
417 xfs_inode_t **ipp)
418{
419 xfs_inode_t *ip;
420 int error;
421 xfs_perag_t *pag;
422 xfs_agino_t agino;
423
424 /*
425 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
426 * doesn't get freed while it's being referenced during a
427 * radix tree traversal here. It assumes this function
428 * aqcuires only the ILOCK (and therefore it has no need to
429 * involve the IOLOCK in this synchronization).
430 */
431 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
432
433 /* reject inode numbers outside existing AGs */
434 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +1000435 return -EINVAL;
Dave Chinner33479e02012-10-08 21:56:11 +1100436
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100437 XFS_STATS_INC(mp, xs_ig_attempts);
Lucas Stach8774cf82015-08-28 14:50:56 +1000438
Dave Chinner33479e02012-10-08 21:56:11 +1100439 /* get the perag structure and ensure that it's inode capable */
440 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
441 agino = XFS_INO_TO_AGINO(mp, ino);
442
443again:
444 error = 0;
445 rcu_read_lock();
446 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
447
448 if (ip) {
449 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
450 if (error)
451 goto out_error_or_again;
452 } else {
453 rcu_read_unlock();
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100454 XFS_STATS_INC(mp, xs_ig_missed);
Dave Chinner33479e02012-10-08 21:56:11 +1100455
456 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
457 flags, lock_flags);
458 if (error)
459 goto out_error_or_again;
460 }
461 xfs_perag_put(pag);
462
463 *ipp = ip;
464
465 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100466 * If we have a real type for an on-disk inode, we can setup the inode
Dave Chinner33479e02012-10-08 21:56:11 +1100467 * now. If it's a new inode being created, xfs_ialloc will handle it.
468 */
469 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Dave Chinner58c90472015-02-23 22:38:08 +1100470 xfs_setup_existing_inode(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100471 return 0;
472
473out_error_or_again:
Dave Chinner24513372014-06-25 14:58:08 +1000474 if (error == -EAGAIN) {
Dave Chinner33479e02012-10-08 21:56:11 +1100475 delay(1);
476 goto again;
477 }
478 xfs_perag_put(pag);
479 return error;
480}
481
Dave Chinner78ae5252010-09-28 12:28:19 +1000482/*
483 * The inode lookup is done in batches to keep the amount of lock traffic and
484 * radix tree lookups to a minimum. The batch size is a trade off between
485 * lookup reduction and stack usage. This is in the reclaim path, so we can't
486 * be too greedy.
487 */
488#define XFS_LOOKUP_BATCH 32
489
Dave Chinnere13de952010-09-28 12:28:06 +1000490STATIC int
491xfs_inode_ag_walk_grab(
492 struct xfs_inode *ip)
493{
494 struct inode *inode = VFS_I(ip);
495
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100496 ASSERT(rcu_read_lock_held());
497
498 /*
499 * check for stale RCU freed inode
500 *
501 * If the inode has been reallocated, it doesn't matter if it's not in
502 * the AG we are walking - we are walking for writeback, so if it
503 * passes all the "valid inode" checks and is dirty, then we'll write
504 * it back anyway. If it has been reallocated and still being
505 * initialised, the XFS_INEW check below will catch it.
506 */
507 spin_lock(&ip->i_flags_lock);
508 if (!ip->i_ino)
509 goto out_unlock_noent;
510
511 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
512 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
513 goto out_unlock_noent;
514 spin_unlock(&ip->i_flags_lock);
515
Dave Chinnere13de952010-09-28 12:28:06 +1000516 /* nothing to sync during shutdown */
517 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000518 return -EFSCORRUPTED;
Dave Chinnere13de952010-09-28 12:28:06 +1000519
Dave Chinnere13de952010-09-28 12:28:06 +1000520 /* If we can't grab the inode, it must on it's way to reclaim. */
521 if (!igrab(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000522 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000523
Dave Chinnere13de952010-09-28 12:28:06 +1000524 /* inode is valid */
525 return 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100526
527out_unlock_noent:
528 spin_unlock(&ip->i_flags_lock);
Dave Chinner24513372014-06-25 14:58:08 +1000529 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000530}
531
Dave Chinner75f3cb12009-06-08 15:35:14 +0200532STATIC int
533xfs_inode_ag_walk(
534 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +0000535 struct xfs_perag *pag,
Eric Sandeene0094002014-04-14 19:04:19 +1000536 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500537 void *args),
538 int flags,
539 void *args,
540 int tag)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200541{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200542 uint32_t first_index;
543 int last_error = 0;
544 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000545 int done;
Dave Chinner78ae5252010-09-28 12:28:19 +1000546 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200547
548restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000549 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200550 skipped = 0;
551 first_index = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000552 nr_found = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200553 do {
Dave Chinner78ae5252010-09-28 12:28:19 +1000554 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
Dave Chinner75f3cb12009-06-08 15:35:14 +0200555 int error = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000556 int i;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200557
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100558 rcu_read_lock();
Brian Fostera454f742012-11-06 09:50:39 -0500559
560 if (tag == -1)
561 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
Dave Chinner78ae5252010-09-28 12:28:19 +1000562 (void **)batch, first_index,
563 XFS_LOOKUP_BATCH);
Brian Fostera454f742012-11-06 09:50:39 -0500564 else
565 nr_found = radix_tree_gang_lookup_tag(
566 &pag->pag_ici_root,
567 (void **) batch, first_index,
568 XFS_LOOKUP_BATCH, tag);
569
Dave Chinner65d0f202010-09-24 18:40:15 +1000570 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100571 rcu_read_unlock();
Dave Chinner75f3cb12009-06-08 15:35:14 +0200572 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000573 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200574
Dave Chinner65d0f202010-09-24 18:40:15 +1000575 /*
Dave Chinner78ae5252010-09-28 12:28:19 +1000576 * Grab the inodes before we drop the lock. if we found
577 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000578 */
Dave Chinner78ae5252010-09-28 12:28:19 +1000579 for (i = 0; i < nr_found; i++) {
580 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000581
Dave Chinner78ae5252010-09-28 12:28:19 +1000582 if (done || xfs_inode_ag_walk_grab(ip))
583 batch[i] = NULL;
584
585 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100586 * Update the index for the next lookup. Catch
587 * overflows into the next AG range which can occur if
588 * we have inodes in the last block of the AG and we
589 * are currently pointing to the last inode.
590 *
591 * Because we may see inodes that are from the wrong AG
592 * due to RCU freeing and reallocation, only update the
593 * index if it lies in this AG. It was a race that lead
594 * us to see this inode, so another lookup from the
595 * same index will not find it again.
Dave Chinner78ae5252010-09-28 12:28:19 +1000596 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100597 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
598 continue;
Dave Chinner78ae5252010-09-28 12:28:19 +1000599 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
600 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
601 done = 1;
Dave Chinnere13de952010-09-28 12:28:06 +1000602 }
Dave Chinner78ae5252010-09-28 12:28:19 +1000603
604 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100605 rcu_read_unlock();
Dave Chinnere13de952010-09-28 12:28:06 +1000606
Dave Chinner78ae5252010-09-28 12:28:19 +1000607 for (i = 0; i < nr_found; i++) {
608 if (!batch[i])
609 continue;
Eric Sandeene0094002014-04-14 19:04:19 +1000610 error = execute(batch[i], flags, args);
Dave Chinner78ae5252010-09-28 12:28:19 +1000611 IRELE(batch[i]);
Dave Chinner24513372014-06-25 14:58:08 +1000612 if (error == -EAGAIN) {
Dave Chinner78ae5252010-09-28 12:28:19 +1000613 skipped++;
614 continue;
615 }
Dave Chinner24513372014-06-25 14:58:08 +1000616 if (error && last_error != -EFSCORRUPTED)
Dave Chinner78ae5252010-09-28 12:28:19 +1000617 last_error = error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200618 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000619
620 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +1000621 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200622 break;
623
Dave Chinner8daaa832011-07-08 14:14:46 +1000624 cond_resched();
625
Dave Chinner78ae5252010-09-28 12:28:19 +1000626 } while (nr_found && !done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200627
628 if (skipped) {
629 delay(1);
630 goto restart;
631 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200632 return last_error;
633}
634
Brian Foster579b62f2012-11-06 09:50:47 -0500635/*
636 * Background scanning to trim post-EOF preallocated space. This is queued
Dwight Engenb9fe5052013-08-15 14:08:02 -0400637 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
Brian Foster579b62f2012-11-06 09:50:47 -0500638 */
639STATIC void
640xfs_queue_eofblocks(
641 struct xfs_mount *mp)
642{
643 rcu_read_lock();
644 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
645 queue_delayed_work(mp->m_eofblocks_workqueue,
646 &mp->m_eofblocks_work,
647 msecs_to_jiffies(xfs_eofb_secs * 1000));
648 rcu_read_unlock();
649}
650
651void
652xfs_eofblocks_worker(
653 struct work_struct *work)
654{
655 struct xfs_mount *mp = container_of(to_delayed_work(work),
656 struct xfs_mount, m_eofblocks_work);
657 xfs_icache_free_eofblocks(mp, NULL);
658 xfs_queue_eofblocks(mp);
659}
660
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200661int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200662xfs_inode_ag_iterator(
663 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000664 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500665 void *args),
666 int flags,
667 void *args)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200668{
Dave Chinner16fd5362010-07-20 09:43:39 +1000669 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200670 int error = 0;
671 int last_error = 0;
672 xfs_agnumber_t ag;
673
Dave Chinner16fd5362010-07-20 09:43:39 +1000674 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000675 while ((pag = xfs_perag_get(mp, ag))) {
676 ag = pag->pag_agno + 1;
Brian Fostera454f742012-11-06 09:50:39 -0500677 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
678 xfs_perag_put(pag);
679 if (error) {
680 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000681 if (error == -EFSCORRUPTED)
Brian Fostera454f742012-11-06 09:50:39 -0500682 break;
683 }
684 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000685 return last_error;
Brian Fostera454f742012-11-06 09:50:39 -0500686}
687
688int
689xfs_inode_ag_iterator_tag(
690 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000691 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500692 void *args),
693 int flags,
694 void *args,
695 int tag)
696{
697 struct xfs_perag *pag;
698 int error = 0;
699 int last_error = 0;
700 xfs_agnumber_t ag;
701
702 ag = 0;
703 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
704 ag = pag->pag_agno + 1;
705 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
Dave Chinner5017e972010-01-11 11:47:40 +0000706 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200707 if (error) {
708 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000709 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200710 break;
711 }
712 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000713 return last_error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200714}
715
David Chinner76bf1052008-10-30 17:16:21 +1100716/*
Dave Chinnera7b339f2011-04-08 12:45:07 +1000717 * Queue a new inode reclaim pass if there are reclaimable inodes and there
718 * isn't a reclaim pass already in progress. By default it runs every 5s based
Dave Chinner58896082012-10-08 21:56:05 +1100719 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
Dave Chinnera7b339f2011-04-08 12:45:07 +1000720 * tunable, but that can be done if this method proves to be ineffective or too
721 * aggressive.
722 */
723static void
Dave Chinner58896082012-10-08 21:56:05 +1100724xfs_reclaim_work_queue(
Dave Chinnera7b339f2011-04-08 12:45:07 +1000725 struct xfs_mount *mp)
David Chinnera167b172008-10-30 17:06:18 +1100726{
David Chinnera167b172008-10-30 17:06:18 +1100727
Dave Chinnera7b339f2011-04-08 12:45:07 +1000728 rcu_read_lock();
729 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
Dave Chinner58896082012-10-08 21:56:05 +1100730 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
Dave Chinnera7b339f2011-04-08 12:45:07 +1000731 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
David Chinnera167b172008-10-30 17:06:18 +1100732 }
Dave Chinnera7b339f2011-04-08 12:45:07 +1000733 rcu_read_unlock();
734}
David Chinnera167b172008-10-30 17:06:18 +1100735
Dave Chinnera7b339f2011-04-08 12:45:07 +1000736/*
737 * This is a fast pass over the inode cache to try to get reclaim moving on as
738 * many inodes as possible in a short period of time. It kicks itself every few
739 * seconds, as well as being kicked by the inode cache shrinker when memory
740 * goes low. It scans as quickly as possible avoiding locked inodes or those
741 * already being flushed, and once done schedules a future pass.
742 */
Dave Chinner33c7a2b2012-10-08 21:55:59 +1100743void
Dave Chinnera7b339f2011-04-08 12:45:07 +1000744xfs_reclaim_worker(
745 struct work_struct *work)
746{
747 struct xfs_mount *mp = container_of(to_delayed_work(work),
748 struct xfs_mount, m_reclaim_work);
749
750 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
Dave Chinner58896082012-10-08 21:56:05 +1100751 xfs_reclaim_work_queue(mp);
Dave Chinnera7b339f2011-04-08 12:45:07 +1000752}
753
Dave Chinner33479e02012-10-08 21:56:11 +1100754static void
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400755__xfs_inode_set_reclaim_tag(
756 struct xfs_perag *pag,
757 struct xfs_inode *ip)
758{
759 radix_tree_tag_set(&pag->pag_ici_root,
760 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
761 XFS_ICI_RECLAIM_TAG);
Dave Chinner16fd5362010-07-20 09:43:39 +1000762
763 if (!pag->pag_ici_reclaimable) {
764 /* propagate the reclaim tag up into the perag radix tree */
765 spin_lock(&ip->i_mount->m_perag_lock);
766 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
767 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
768 XFS_ICI_RECLAIM_TAG);
769 spin_unlock(&ip->i_mount->m_perag_lock);
Dave Chinnera7b339f2011-04-08 12:45:07 +1000770
771 /* schedule periodic background inode reclaim */
Dave Chinner58896082012-10-08 21:56:05 +1100772 xfs_reclaim_work_queue(ip->i_mount);
Dave Chinnera7b339f2011-04-08 12:45:07 +1000773
Dave Chinner16fd5362010-07-20 09:43:39 +1000774 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
775 -1, _RET_IP_);
776 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000777 pag->pag_ici_reclaimable++;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400778}
779
David Chinner11654512008-10-30 17:37:49 +1100780/*
781 * We set the inode flag atomically with the radix tree tag.
782 * Once we get tag lookups on the radix tree, this inode flag
783 * can go away.
784 */
David Chinner396beb82008-10-30 17:37:26 +1100785void
786xfs_inode_set_reclaim_tag(
787 xfs_inode_t *ip)
788{
Dave Chinner5017e972010-01-11 11:47:40 +0000789 struct xfs_mount *mp = ip->i_mount;
790 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100791
Dave Chinner5017e972010-01-11 11:47:40 +0000792 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
Dave Chinner1a427ab2010-12-16 17:08:41 +1100793 spin_lock(&pag->pag_ici_lock);
David Chinner396beb82008-10-30 17:37:26 +1100794 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400795 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100796 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100797 spin_unlock(&ip->i_flags_lock);
Dave Chinner1a427ab2010-12-16 17:08:41 +1100798 spin_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000799 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100800}
801
Johannes Weiner081003f2010-10-01 07:43:54 +0000802STATIC void
803__xfs_inode_clear_reclaim(
David Chinner396beb82008-10-30 17:37:26 +1100804 xfs_perag_t *pag,
805 xfs_inode_t *ip)
806{
Dave Chinner9bf729c2010-04-29 09:55:50 +1000807 pag->pag_ici_reclaimable--;
Dave Chinner16fd5362010-07-20 09:43:39 +1000808 if (!pag->pag_ici_reclaimable) {
809 /* clear the reclaim tag from the perag radix tree */
810 spin_lock(&ip->i_mount->m_perag_lock);
811 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
812 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
813 XFS_ICI_RECLAIM_TAG);
814 spin_unlock(&ip->i_mount->m_perag_lock);
815 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
816 -1, _RET_IP_);
817 }
David Chinner396beb82008-10-30 17:37:26 +1100818}
819
Dave Chinner33479e02012-10-08 21:56:11 +1100820STATIC void
Johannes Weiner081003f2010-10-01 07:43:54 +0000821__xfs_inode_clear_reclaim_tag(
822 xfs_mount_t *mp,
823 xfs_perag_t *pag,
824 xfs_inode_t *ip)
825{
826 radix_tree_tag_clear(&pag->pag_ici_root,
827 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
828 __xfs_inode_clear_reclaim(pag, ip);
829}
830
Dave Chinner777df5a2010-02-06 12:37:26 +1100831/*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000832 * Grab the inode for reclaim exclusively.
833 * Return 0 if we grabbed it, non-zero otherwise.
834 */
835STATIC int
836xfs_reclaim_inode_grab(
837 struct xfs_inode *ip,
838 int flags)
839{
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100840 ASSERT(rcu_read_lock_held());
841
842 /* quick check for stale RCU freed inode */
843 if (!ip->i_ino)
844 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000845
846 /*
Christoph Hellwig474fce02011-12-18 20:00:09 +0000847 * If we are asked for non-blocking operation, do unlocked checks to
848 * see if the inode already is being flushed or in reclaim to avoid
849 * lock traffic.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000850 */
851 if ((flags & SYNC_TRYLOCK) &&
Christoph Hellwig474fce02011-12-18 20:00:09 +0000852 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
Dave Chinnere3a20c02010-09-24 19:51:50 +1000853 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000854
855 /*
856 * The radix tree lock here protects a thread in xfs_iget from racing
857 * with us starting reclaim on the inode. Once we have the
858 * XFS_IRECLAIM flag set it will not touch us.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100859 *
860 * Due to RCU lookup, we may find inodes that have been freed and only
861 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
862 * aren't candidates for reclaim at all, so we must check the
863 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000864 */
865 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100866 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
867 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
868 /* not a reclaim candidate. */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000869 spin_unlock(&ip->i_flags_lock);
870 return 1;
871 }
872 __xfs_iflags_set(ip, XFS_IRECLAIM);
873 spin_unlock(&ip->i_flags_lock);
874 return 0;
875}
876
877/*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000878 * Inodes in different states need to be treated differently. The following
879 * table lists the inode states and the reclaim actions necessary:
Dave Chinner777df5a2010-02-06 12:37:26 +1100880 *
881 * inode state iflush ret required action
882 * --------------- ---------- ---------------
883 * bad - reclaim
884 * shutdown EIO unpin and reclaim
885 * clean, unpinned 0 reclaim
886 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100887 * clean, pinned(*) 0 requeue
888 * stale, pinned EAGAIN requeue
Christoph Hellwig8a480882012-04-23 15:58:35 +1000889 * dirty, async - requeue
890 * dirty, sync 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100891 *
892 * (*) dgc: I don't think the clean, pinned state is possible but it gets
893 * handled anyway given the order of checks implemented.
894 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100895 * Also, because we get the flush lock first, we know that any inode that has
896 * been flushed delwri has had the flush completed by the time we check that
Christoph Hellwig8a480882012-04-23 15:58:35 +1000897 * the inode is clean.
Dave Chinnerc8543632010-02-06 12:39:36 +1100898 *
Christoph Hellwig8a480882012-04-23 15:58:35 +1000899 * Note that because the inode is flushed delayed write by AIL pushing, the
900 * flush lock may already be held here and waiting on it can result in very
901 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
902 * the caller should push the AIL first before trying to reclaim inodes to
903 * minimise the amount of time spent waiting. For background relaim, we only
904 * bother to reclaim clean inodes anyway.
Dave Chinnerc8543632010-02-06 12:39:36 +1100905 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100906 * Hence the order of actions after gaining the locks should be:
907 * bad => reclaim
908 * shutdown => unpin and reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000909 * pinned, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100910 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100911 * stale => reclaim
912 * clean => reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000913 * dirty, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100914 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100915 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200916STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000917xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200918 struct xfs_inode *ip,
919 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000920 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100921{
Christoph Hellwig4c468192012-04-23 15:58:36 +1000922 struct xfs_buf *bp = NULL;
923 int error;
Dave Chinner777df5a2010-02-06 12:37:26 +1100924
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100925restart:
926 error = 0;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000927 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100928 if (!xfs_iflock_nowait(ip)) {
929 if (!(sync_mode & SYNC_WAIT))
930 goto out;
931 xfs_iflock(ip);
932 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000933
Dave Chinner777df5a2010-02-06 12:37:26 +1100934 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
935 xfs_iunpin_wait(ip);
Dave Chinner04913fd2012-04-23 15:58:41 +1000936 xfs_iflush_abort(ip, false);
Dave Chinner777df5a2010-02-06 12:37:26 +1100937 goto reclaim;
938 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100939 if (xfs_ipincount(ip)) {
Christoph Hellwig8a480882012-04-23 15:58:35 +1000940 if (!(sync_mode & SYNC_WAIT))
941 goto out_ifunlock;
Dave Chinner777df5a2010-02-06 12:37:26 +1100942 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100943 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100944 if (xfs_iflags_test(ip, XFS_ISTALE))
945 goto reclaim;
946 if (xfs_inode_clean(ip))
947 goto reclaim;
948
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100949 /*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000950 * Never flush out dirty data during non-blocking reclaim, as it would
951 * just contend with AIL pushing trying to do the same job.
952 */
953 if (!(sync_mode & SYNC_WAIT))
954 goto out_ifunlock;
955
956 /*
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100957 * Now we have an inode that needs flushing.
958 *
Christoph Hellwig4c468192012-04-23 15:58:36 +1000959 * Note that xfs_iflush will never block on the inode buffer lock, as
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100960 * xfs_ifree_cluster() can lock the inode buffer before it locks the
Christoph Hellwig4c468192012-04-23 15:58:36 +1000961 * ip->i_lock, and we are doing the exact opposite here. As a result,
Christoph Hellwig475ee412012-07-03 12:21:22 -0400962 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
963 * result in an ABBA deadlock with xfs_ifree_cluster().
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100964 *
965 * As xfs_ifree_cluser() must gather all inodes that are active in the
966 * cache to mark them stale, if we hit this case we don't actually want
967 * to do IO here - we want the inode marked stale so we can simply
Christoph Hellwig4c468192012-04-23 15:58:36 +1000968 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
969 * inode, back off and try again. Hopefully the next pass through will
970 * see the stale flag set on the inode.
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100971 */
Christoph Hellwig4c468192012-04-23 15:58:36 +1000972 error = xfs_iflush(ip, &bp);
Dave Chinner24513372014-06-25 14:58:08 +1000973 if (error == -EAGAIN) {
Christoph Hellwig8a480882012-04-23 15:58:35 +1000974 xfs_iunlock(ip, XFS_ILOCK_EXCL);
975 /* backoff longer than in xfs_ifree_cluster */
976 delay(2);
977 goto restart;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000978 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100979
Christoph Hellwig4c468192012-04-23 15:58:36 +1000980 if (!error) {
981 error = xfs_bwrite(bp);
982 xfs_buf_relse(bp);
983 }
984
985 xfs_iflock(ip);
Dave Chinner777df5a2010-02-06 12:37:26 +1100986reclaim:
987 xfs_ifunlock(ip);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000988 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000989
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100990 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000991 /*
992 * Remove the inode from the per-AG radix tree.
993 *
994 * Because radix_tree_delete won't complain even if the item was never
995 * added to the tree assert that it's been there before to catch
996 * problems with the inode life time early on.
997 */
Dave Chinner1a427ab2010-12-16 17:08:41 +1100998 spin_lock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000999 if (!radix_tree_delete(&pag->pag_ici_root,
1000 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
1001 ASSERT(0);
Johannes Weiner081003f2010-10-01 07:43:54 +00001002 __xfs_inode_clear_reclaim(pag, ip);
Dave Chinner1a427ab2010-12-16 17:08:41 +11001003 spin_unlock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001004
1005 /*
1006 * Here we do an (almost) spurious inode lock in order to coordinate
1007 * with inode cache radix tree lookups. This is because the lookup
1008 * can reference the inodes in the cache without taking references.
1009 *
1010 * We make that OK here by ensuring that we wait until the inode is
Alex Elderad637a12012-02-16 22:01:00 +00001011 * unlocked after the lookup before we go ahead and free it.
Dave Chinner2f11fea2010-07-20 17:53:25 +10001012 */
Alex Elderad637a12012-02-16 22:01:00 +00001013 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001014 xfs_qm_dqdetach(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001015 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001016
1017 xfs_inode_free(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001018 return error;
Christoph Hellwig8a480882012-04-23 15:58:35 +10001019
1020out_ifunlock:
1021 xfs_ifunlock(ip);
1022out:
1023 xfs_iflags_clear(ip, XFS_IRECLAIM);
1024 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1025 /*
Dave Chinner24513372014-06-25 14:58:08 +10001026 * We could return -EAGAIN here to make reclaim rescan the inode tree in
Christoph Hellwig8a480882012-04-23 15:58:35 +10001027 * a short while. However, this just burns CPU time scanning the tree
Dave Chinner58896082012-10-08 21:56:05 +11001028 * waiting for IO to complete and the reclaim work never goes back to
1029 * the idle state. Instead, return 0 to let the next scheduled
1030 * background reclaim attempt to reclaim the inode again.
Christoph Hellwig8a480882012-04-23 15:58:35 +10001031 */
1032 return 0;
David Chinner7a3be022008-10-30 17:37:37 +11001033}
1034
Dave Chinner65d0f202010-09-24 18:40:15 +10001035/*
1036 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1037 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1038 * then a shut down during filesystem unmount reclaim walk leak all the
1039 * unreclaimed inodes.
1040 */
Dave Chinner33479e02012-10-08 21:56:11 +11001041STATIC int
Dave Chinner65d0f202010-09-24 18:40:15 +10001042xfs_reclaim_inodes_ag(
1043 struct xfs_mount *mp,
1044 int flags,
1045 int *nr_to_scan)
1046{
1047 struct xfs_perag *pag;
1048 int error = 0;
1049 int last_error = 0;
1050 xfs_agnumber_t ag;
Dave Chinner69b491c2010-09-27 11:09:51 +10001051 int trylock = flags & SYNC_TRYLOCK;
1052 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +10001053
Dave Chinner69b491c2010-09-27 11:09:51 +10001054restart:
Dave Chinner65d0f202010-09-24 18:40:15 +10001055 ag = 0;
Dave Chinner69b491c2010-09-27 11:09:51 +10001056 skipped = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001057 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1058 unsigned long first_index = 0;
1059 int done = 0;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001060 int nr_found = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001061
1062 ag = pag->pag_agno + 1;
1063
Dave Chinner69b491c2010-09-27 11:09:51 +10001064 if (trylock) {
1065 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1066 skipped++;
Dave Chinnerf83282a2010-11-08 08:55:04 +00001067 xfs_perag_put(pag);
Dave Chinner69b491c2010-09-27 11:09:51 +10001068 continue;
1069 }
1070 first_index = pag->pag_ici_reclaim_cursor;
1071 } else
1072 mutex_lock(&pag->pag_ici_reclaim_lock);
1073
Dave Chinner65d0f202010-09-24 18:40:15 +10001074 do {
Dave Chinnere3a20c02010-09-24 19:51:50 +10001075 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1076 int i;
Dave Chinner65d0f202010-09-24 18:40:15 +10001077
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001078 rcu_read_lock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001079 nr_found = radix_tree_gang_lookup_tag(
1080 &pag->pag_ici_root,
1081 (void **)batch, first_index,
1082 XFS_LOOKUP_BATCH,
Dave Chinner65d0f202010-09-24 18:40:15 +10001083 XFS_ICI_RECLAIM_TAG);
1084 if (!nr_found) {
Dave Chinnerb2232212011-05-06 02:54:04 +00001085 done = 1;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001086 rcu_read_unlock();
Dave Chinner65d0f202010-09-24 18:40:15 +10001087 break;
1088 }
1089
1090 /*
Dave Chinnere3a20c02010-09-24 19:51:50 +10001091 * Grab the inodes before we drop the lock. if we found
1092 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +10001093 */
Dave Chinnere3a20c02010-09-24 19:51:50 +10001094 for (i = 0; i < nr_found; i++) {
1095 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +10001096
Dave Chinnere3a20c02010-09-24 19:51:50 +10001097 if (done || xfs_reclaim_inode_grab(ip, flags))
1098 batch[i] = NULL;
Dave Chinner65d0f202010-09-24 18:40:15 +10001099
Dave Chinnere3a20c02010-09-24 19:51:50 +10001100 /*
1101 * Update the index for the next lookup. Catch
1102 * overflows into the next AG range which can
1103 * occur if we have inodes in the last block of
1104 * the AG and we are currently pointing to the
1105 * last inode.
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001106 *
1107 * Because we may see inodes that are from the
1108 * wrong AG due to RCU freeing and
1109 * reallocation, only update the index if it
1110 * lies in this AG. It was a race that lead us
1111 * to see this inode, so another lookup from
1112 * the same index will not find it again.
Dave Chinnere3a20c02010-09-24 19:51:50 +10001113 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001114 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1115 pag->pag_agno)
1116 continue;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001117 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1118 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1119 done = 1;
1120 }
1121
1122 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001123 rcu_read_unlock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001124
1125 for (i = 0; i < nr_found; i++) {
1126 if (!batch[i])
1127 continue;
1128 error = xfs_reclaim_inode(batch[i], pag, flags);
Dave Chinner24513372014-06-25 14:58:08 +10001129 if (error && last_error != -EFSCORRUPTED)
Dave Chinnere3a20c02010-09-24 19:51:50 +10001130 last_error = error;
1131 }
1132
1133 *nr_to_scan -= XFS_LOOKUP_BATCH;
1134
Dave Chinner8daaa832011-07-08 14:14:46 +10001135 cond_resched();
1136
Dave Chinnere3a20c02010-09-24 19:51:50 +10001137 } while (nr_found && !done && *nr_to_scan > 0);
Dave Chinner65d0f202010-09-24 18:40:15 +10001138
Dave Chinner69b491c2010-09-27 11:09:51 +10001139 if (trylock && !done)
1140 pag->pag_ici_reclaim_cursor = first_index;
1141 else
1142 pag->pag_ici_reclaim_cursor = 0;
1143 mutex_unlock(&pag->pag_ici_reclaim_lock);
Dave Chinner65d0f202010-09-24 18:40:15 +10001144 xfs_perag_put(pag);
1145 }
Dave Chinner69b491c2010-09-27 11:09:51 +10001146
1147 /*
1148 * if we skipped any AG, and we still have scan count remaining, do
1149 * another pass this time using blocking reclaim semantics (i.e
1150 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1151 * ensure that when we get more reclaimers than AGs we block rather
1152 * than spin trying to execute reclaim.
1153 */
Dave Chinner8daaa832011-07-08 14:14:46 +10001154 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
Dave Chinner69b491c2010-09-27 11:09:51 +10001155 trylock = 0;
1156 goto restart;
1157 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001158 return last_error;
Dave Chinner65d0f202010-09-24 18:40:15 +10001159}
1160
David Chinnerfce08f22008-10-30 17:37:03 +11001161int
David Chinner1dc33182008-10-30 17:37:15 +11001162xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +11001163 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +11001164 int mode)
1165{
Dave Chinner65d0f202010-09-24 18:40:15 +10001166 int nr_to_scan = INT_MAX;
1167
1168 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001169}
1170
1171/*
Dave Chinner8daaa832011-07-08 14:14:46 +10001172 * Scan a certain number of inodes for reclaim.
Dave Chinnera7b339f2011-04-08 12:45:07 +10001173 *
1174 * When called we make sure that there is a background (fast) inode reclaim in
Dave Chinner8daaa832011-07-08 14:14:46 +10001175 * progress, while we will throttle the speed of reclaim via doing synchronous
Dave Chinnera7b339f2011-04-08 12:45:07 +10001176 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1177 * them to be cleaned, which we hope will not be very long due to the
1178 * background walker having already kicked the IO off on those dirty inodes.
Dave Chinner9bf729c2010-04-29 09:55:50 +10001179 */
Dave Chinner0a234c62013-08-28 10:17:57 +10001180long
Dave Chinner8daaa832011-07-08 14:14:46 +10001181xfs_reclaim_inodes_nr(
1182 struct xfs_mount *mp,
1183 int nr_to_scan)
Dave Chinner9bf729c2010-04-29 09:55:50 +10001184{
Dave Chinner8daaa832011-07-08 14:14:46 +10001185 /* kick background reclaimer and push the AIL */
Dave Chinner58896082012-10-08 21:56:05 +11001186 xfs_reclaim_work_queue(mp);
Dave Chinner8daaa832011-07-08 14:14:46 +10001187 xfs_ail_push_all(mp->m_ail);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001188
Dave Chinner0a234c62013-08-28 10:17:57 +10001189 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
Dave Chinner8daaa832011-07-08 14:14:46 +10001190}
Dave Chinnera7b339f2011-04-08 12:45:07 +10001191
Dave Chinner8daaa832011-07-08 14:14:46 +10001192/*
1193 * Return the number of reclaimable inodes in the filesystem for
1194 * the shrinker to determine how much to reclaim.
1195 */
1196int
1197xfs_reclaim_inodes_count(
1198 struct xfs_mount *mp)
1199{
1200 struct xfs_perag *pag;
1201 xfs_agnumber_t ag = 0;
1202 int reclaimable = 0;
Dave Chinner9bf729c2010-04-29 09:55:50 +10001203
Dave Chinner65d0f202010-09-24 18:40:15 +10001204 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1205 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +10001206 reclaimable += pag->pag_ici_reclaimable;
1207 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001208 }
Dave Chinner9bf729c2010-04-29 09:55:50 +10001209 return reclaimable;
1210}
1211
Brian Foster41176a62012-11-06 09:50:42 -05001212STATIC int
Brian Foster3e3f9f52012-11-07 12:21:13 -05001213xfs_inode_match_id(
1214 struct xfs_inode *ip,
1215 struct xfs_eofblocks *eofb)
1216{
Dwight Engenb9fe5052013-08-15 14:08:02 -04001217 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1218 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
Brian Foster1b556042012-11-06 09:50:45 -05001219 return 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001220
Dwight Engenb9fe5052013-08-15 14:08:02 -04001221 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1222 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
Brian Foster1b556042012-11-06 09:50:45 -05001223 return 0;
1224
Dwight Engenb9fe5052013-08-15 14:08:02 -04001225 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
Brian Foster1b556042012-11-06 09:50:45 -05001226 xfs_get_projid(ip) != eofb->eof_prid)
1227 return 0;
1228
1229 return 1;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001230}
1231
Brian Fosterf4526392014-07-24 19:44:28 +10001232/*
1233 * A union-based inode filtering algorithm. Process the inode if any of the
1234 * criteria match. This is for global/internal scans only.
1235 */
1236STATIC int
1237xfs_inode_match_id_union(
1238 struct xfs_inode *ip,
1239 struct xfs_eofblocks *eofb)
1240{
1241 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1242 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1243 return 1;
1244
1245 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1246 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1247 return 1;
1248
1249 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1250 xfs_get_projid(ip) == eofb->eof_prid)
1251 return 1;
1252
1253 return 0;
1254}
1255
Brian Foster3e3f9f52012-11-07 12:21:13 -05001256STATIC int
Brian Foster41176a62012-11-06 09:50:42 -05001257xfs_inode_free_eofblocks(
1258 struct xfs_inode *ip,
Brian Foster41176a62012-11-06 09:50:42 -05001259 int flags,
1260 void *args)
1261{
1262 int ret;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001263 struct xfs_eofblocks *eofb = args;
Brian Foster5400da72014-07-24 19:40:22 +10001264 bool need_iolock = true;
Brian Fosterf4526392014-07-24 19:44:28 +10001265 int match;
Brian Foster5400da72014-07-24 19:40:22 +10001266
1267 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
Brian Foster41176a62012-11-06 09:50:42 -05001268
1269 if (!xfs_can_free_eofblocks(ip, false)) {
1270 /* inode could be preallocated or append-only */
1271 trace_xfs_inode_free_eofblocks_invalid(ip);
1272 xfs_inode_clear_eofblocks_tag(ip);
1273 return 0;
1274 }
1275
1276 /*
1277 * If the mapping is dirty the operation can block and wait for some
1278 * time. Unless we are waiting, skip it.
1279 */
1280 if (!(flags & SYNC_WAIT) &&
1281 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1282 return 0;
1283
Brian Foster00ca79a2012-11-07 12:21:14 -05001284 if (eofb) {
Brian Fosterf4526392014-07-24 19:44:28 +10001285 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1286 match = xfs_inode_match_id_union(ip, eofb);
1287 else
1288 match = xfs_inode_match_id(ip, eofb);
1289 if (!match)
Brian Foster00ca79a2012-11-07 12:21:14 -05001290 return 0;
1291
1292 /* skip the inode if the file size is too small */
1293 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1294 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1295 return 0;
Brian Foster5400da72014-07-24 19:40:22 +10001296
1297 /*
1298 * A scan owner implies we already hold the iolock. Skip it in
1299 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1300 * the possibility of EAGAIN being returned.
1301 */
1302 if (eofb->eof_scan_owner == ip->i_ino)
1303 need_iolock = false;
Brian Foster00ca79a2012-11-07 12:21:14 -05001304 }
Brian Foster3e3f9f52012-11-07 12:21:13 -05001305
Brian Foster5400da72014-07-24 19:40:22 +10001306 ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
Brian Foster41176a62012-11-06 09:50:42 -05001307
1308 /* don't revisit the inode if we're not waiting */
Dave Chinner24513372014-06-25 14:58:08 +10001309 if (ret == -EAGAIN && !(flags & SYNC_WAIT))
Brian Foster41176a62012-11-06 09:50:42 -05001310 ret = 0;
1311
1312 return ret;
1313}
1314
1315int
1316xfs_icache_free_eofblocks(
1317 struct xfs_mount *mp,
Brian Foster8ca149d2012-11-07 12:21:12 -05001318 struct xfs_eofblocks *eofb)
Brian Foster41176a62012-11-06 09:50:42 -05001319{
Brian Foster8ca149d2012-11-07 12:21:12 -05001320 int flags = SYNC_TRYLOCK;
1321
1322 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1323 flags = SYNC_WAIT;
1324
Brian Foster41176a62012-11-06 09:50:42 -05001325 return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
Brian Foster8ca149d2012-11-07 12:21:12 -05001326 eofb, XFS_ICI_EOFBLOCKS_TAG);
Brian Foster41176a62012-11-06 09:50:42 -05001327}
1328
Brian Fosterdc06f3982014-07-24 19:49:28 +10001329/*
1330 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1331 * multiple quotas, we don't know exactly which quota caused an allocation
1332 * failure. We make a best effort by including each quota under low free space
1333 * conditions (less than 1% free space) in the scan.
1334 */
1335int
1336xfs_inode_free_quota_eofblocks(
1337 struct xfs_inode *ip)
1338{
1339 int scan = 0;
1340 struct xfs_eofblocks eofb = {0};
1341 struct xfs_dquot *dq;
1342
1343 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1344
1345 /*
1346 * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1347 * can repeatedly trylock on the inode we're currently processing. We
1348 * run a sync scan to increase effectiveness and use the union filter to
1349 * cover all applicable quotas in a single scan.
1350 */
1351 eofb.eof_scan_owner = ip->i_ino;
1352 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1353
1354 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1355 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1356 if (dq && xfs_dquot_lowsp(dq)) {
1357 eofb.eof_uid = VFS_I(ip)->i_uid;
1358 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1359 scan = 1;
1360 }
1361 }
1362
1363 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1364 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1365 if (dq && xfs_dquot_lowsp(dq)) {
1366 eofb.eof_gid = VFS_I(ip)->i_gid;
1367 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1368 scan = 1;
1369 }
1370 }
1371
1372 if (scan)
1373 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
1374
1375 return scan;
1376}
1377
Brian Foster27b52862012-11-06 09:50:38 -05001378void
1379xfs_inode_set_eofblocks_tag(
1380 xfs_inode_t *ip)
1381{
1382 struct xfs_mount *mp = ip->i_mount;
1383 struct xfs_perag *pag;
1384 int tagged;
1385
1386 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1387 spin_lock(&pag->pag_ici_lock);
1388 trace_xfs_inode_set_eofblocks_tag(ip);
1389
1390 tagged = radix_tree_tagged(&pag->pag_ici_root,
1391 XFS_ICI_EOFBLOCKS_TAG);
1392 radix_tree_tag_set(&pag->pag_ici_root,
1393 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1394 XFS_ICI_EOFBLOCKS_TAG);
1395 if (!tagged) {
1396 /* propagate the eofblocks tag up into the perag radix tree */
1397 spin_lock(&ip->i_mount->m_perag_lock);
1398 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1399 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1400 XFS_ICI_EOFBLOCKS_TAG);
1401 spin_unlock(&ip->i_mount->m_perag_lock);
1402
Brian Foster579b62f2012-11-06 09:50:47 -05001403 /* kick off background trimming */
1404 xfs_queue_eofblocks(ip->i_mount);
1405
Brian Foster27b52862012-11-06 09:50:38 -05001406 trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
1407 -1, _RET_IP_);
1408 }
1409
1410 spin_unlock(&pag->pag_ici_lock);
1411 xfs_perag_put(pag);
1412}
1413
1414void
1415xfs_inode_clear_eofblocks_tag(
1416 xfs_inode_t *ip)
1417{
1418 struct xfs_mount *mp = ip->i_mount;
1419 struct xfs_perag *pag;
1420
1421 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1422 spin_lock(&pag->pag_ici_lock);
1423 trace_xfs_inode_clear_eofblocks_tag(ip);
1424
1425 radix_tree_tag_clear(&pag->pag_ici_root,
1426 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1427 XFS_ICI_EOFBLOCKS_TAG);
1428 if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
1429 /* clear the eofblocks tag from the perag radix tree */
1430 spin_lock(&ip->i_mount->m_perag_lock);
1431 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1432 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1433 XFS_ICI_EOFBLOCKS_TAG);
1434 spin_unlock(&ip->i_mount->m_perag_lock);
1435 trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
1436 -1, _RET_IP_);
1437 }
1438
1439 spin_unlock(&pag->pag_ici_lock);
1440 xfs_perag_put(pag);
1441}
1442