blob: 9ca2865575ab2624150fd0e01198f904d747b544 [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 Chinnerf8d55aa2016-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;
151
152 error = inode_init_always(mp->m_super, inode);
153
154 return error;
155}
156
157/*
Dave Chinner33479e02012-10-08 21:56:11 +1100158 * Check the validity of the inode we just found it the cache
159 */
160static int
161xfs_iget_cache_hit(
162 struct xfs_perag *pag,
163 struct xfs_inode *ip,
164 xfs_ino_t ino,
165 int flags,
166 int lock_flags) __releases(RCU)
167{
168 struct inode *inode = VFS_I(ip);
169 struct xfs_mount *mp = ip->i_mount;
170 int error;
171
172 /*
173 * check for re-use of an inode within an RCU grace period due to the
174 * radix tree nodes not being updated yet. We monitor for this by
175 * setting the inode number to zero before freeing the inode structure.
176 * If the inode has been reallocated and set up, then the inode number
177 * will not match, so check for that, too.
178 */
179 spin_lock(&ip->i_flags_lock);
180 if (ip->i_ino != ino) {
181 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100182 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000183 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100184 goto out_error;
185 }
186
187
188 /*
189 * If we are racing with another cache hit that is currently
190 * instantiating this inode or currently recycling it out of
191 * reclaimabe state, wait for the initialisation to complete
192 * before continuing.
193 *
194 * XXX(hch): eventually we should do something equivalent to
195 * wait_on_inode to wait for these flags to be cleared
196 * instead of polling for it.
197 */
198 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
199 trace_xfs_iget_skip(ip);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100200 XFS_STATS_INC(mp, xs_ig_frecycle);
Dave Chinner24513372014-06-25 14:58:08 +1000201 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100202 goto out_error;
203 }
204
205 /*
206 * If lookup is racing with unlink return an error immediately.
207 */
208 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000209 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100210 goto out_error;
211 }
212
213 /*
214 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
215 * Need to carefully get it back into useable state.
216 */
217 if (ip->i_flags & XFS_IRECLAIMABLE) {
218 trace_xfs_iget_reclaim(ip);
219
220 /*
221 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
222 * from stomping over us while we recycle the inode. We can't
223 * clear the radix tree reclaimable tag yet as it requires
224 * pag_ici_lock to be held exclusive.
225 */
226 ip->i_flags |= XFS_IRECLAIM;
227
228 spin_unlock(&ip->i_flags_lock);
229 rcu_read_unlock();
230
Dave Chinner50997472016-02-09 16:54:58 +1100231 error = xfs_reinit_inode(mp, inode);
Dave Chinner33479e02012-10-08 21:56:11 +1100232 if (error) {
233 /*
234 * Re-initializing the inode failed, and we are in deep
235 * trouble. Try to re-add it to the reclaim list.
236 */
237 rcu_read_lock();
238 spin_lock(&ip->i_flags_lock);
239
240 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
241 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
242 trace_xfs_iget_reclaim_fail(ip);
243 goto out_error;
244 }
245
246 spin_lock(&pag->pag_ici_lock);
247 spin_lock(&ip->i_flags_lock);
248
249 /*
250 * Clear the per-lifetime state in the inode as we are now
251 * effectively a new inode and need to return to the initial
252 * state before reuse occurs.
253 */
254 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
255 ip->i_flags |= XFS_INEW;
256 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
257 inode->i_state = I_NEW;
258
259 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
260 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
261
262 spin_unlock(&ip->i_flags_lock);
263 spin_unlock(&pag->pag_ici_lock);
264 } else {
265 /* If the VFS inode is being torn down, pause and try again. */
266 if (!igrab(inode)) {
267 trace_xfs_iget_skip(ip);
Dave Chinner24513372014-06-25 14:58:08 +1000268 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100269 goto out_error;
270 }
271
272 /* We've got a live one. */
273 spin_unlock(&ip->i_flags_lock);
274 rcu_read_unlock();
275 trace_xfs_iget_hit(ip);
276 }
277
278 if (lock_flags != 0)
279 xfs_ilock(ip, lock_flags);
280
281 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100282 XFS_STATS_INC(mp, xs_ig_found);
Dave Chinner33479e02012-10-08 21:56:11 +1100283
284 return 0;
285
286out_error:
287 spin_unlock(&ip->i_flags_lock);
288 rcu_read_unlock();
289 return error;
290}
291
292
293static int
294xfs_iget_cache_miss(
295 struct xfs_mount *mp,
296 struct xfs_perag *pag,
297 xfs_trans_t *tp,
298 xfs_ino_t ino,
299 struct xfs_inode **ipp,
300 int flags,
301 int lock_flags)
302{
303 struct xfs_inode *ip;
304 int error;
305 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
306 int iflags;
307
308 ip = xfs_inode_alloc(mp, ino);
309 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +1000310 return -ENOMEM;
Dave Chinner33479e02012-10-08 21:56:11 +1100311
312 error = xfs_iread(mp, tp, ip, flags);
313 if (error)
314 goto out_destroy;
315
316 trace_xfs_iget_miss(ip);
317
318 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
Dave Chinner24513372014-06-25 14:58:08 +1000319 error = -ENOENT;
Dave Chinner33479e02012-10-08 21:56:11 +1100320 goto out_destroy;
321 }
322
323 /*
324 * Preload the radix tree so we can insert safely under the
325 * write spinlock. Note that we cannot sleep inside the preload
326 * region. Since we can be called from transaction context, don't
327 * recurse into the file system.
328 */
329 if (radix_tree_preload(GFP_NOFS)) {
Dave Chinner24513372014-06-25 14:58:08 +1000330 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100331 goto out_destroy;
332 }
333
334 /*
335 * Because the inode hasn't been added to the radix-tree yet it can't
336 * be found by another thread, so we can do the non-sleeping lock here.
337 */
338 if (lock_flags) {
339 if (!xfs_ilock_nowait(ip, lock_flags))
340 BUG();
341 }
342
343 /*
344 * These values must be set before inserting the inode into the radix
345 * tree as the moment it is inserted a concurrent lookup (allowed by the
346 * RCU locking mechanism) can find it and that lookup must see that this
347 * is an inode currently under construction (i.e. that XFS_INEW is set).
348 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
349 * memory barrier that ensures this detection works correctly at lookup
350 * time.
351 */
352 iflags = XFS_INEW;
353 if (flags & XFS_IGET_DONTCACHE)
354 iflags |= XFS_IDONTCACHE;
Chandra Seetharaman113a5682013-06-27 17:25:07 -0500355 ip->i_udquot = NULL;
356 ip->i_gdquot = NULL;
Chandra Seetharaman92f8ff72013-07-11 00:00:40 -0500357 ip->i_pdquot = NULL;
Dave Chinner33479e02012-10-08 21:56:11 +1100358 xfs_iflags_set(ip, iflags);
359
360 /* insert the new inode */
361 spin_lock(&pag->pag_ici_lock);
362 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
363 if (unlikely(error)) {
364 WARN_ON(error != -EEXIST);
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100365 XFS_STATS_INC(mp, xs_ig_dup);
Dave Chinner24513372014-06-25 14:58:08 +1000366 error = -EAGAIN;
Dave Chinner33479e02012-10-08 21:56:11 +1100367 goto out_preload_end;
368 }
369 spin_unlock(&pag->pag_ici_lock);
370 radix_tree_preload_end();
371
372 *ipp = ip;
373 return 0;
374
375out_preload_end:
376 spin_unlock(&pag->pag_ici_lock);
377 radix_tree_preload_end();
378 if (lock_flags)
379 xfs_iunlock(ip, lock_flags);
380out_destroy:
381 __destroy_inode(VFS_I(ip));
382 xfs_inode_free(ip);
383 return error;
384}
385
386/*
387 * Look up an inode by number in the given file system.
388 * The inode is looked up in the cache held in each AG.
389 * If the inode is found in the cache, initialise the vfs inode
390 * if necessary.
391 *
392 * If it is not in core, read it in from the file system's device,
393 * add it to the cache and initialise the vfs inode.
394 *
395 * The inode is locked according to the value of the lock_flags parameter.
396 * This flag parameter indicates how and if the inode's IO lock and inode lock
397 * should be taken.
398 *
399 * mp -- the mount point structure for the current file system. It points
400 * to the inode hash table.
401 * tp -- a pointer to the current transaction if there is one. This is
402 * simply passed through to the xfs_iread() call.
403 * ino -- the number of the inode desired. This is the unique identifier
404 * within the file system for the inode being requested.
405 * lock_flags -- flags indicating how to lock the inode. See the comment
406 * for xfs_ilock() for a list of valid values.
407 */
408int
409xfs_iget(
410 xfs_mount_t *mp,
411 xfs_trans_t *tp,
412 xfs_ino_t ino,
413 uint flags,
414 uint lock_flags,
415 xfs_inode_t **ipp)
416{
417 xfs_inode_t *ip;
418 int error;
419 xfs_perag_t *pag;
420 xfs_agino_t agino;
421
422 /*
423 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
424 * doesn't get freed while it's being referenced during a
425 * radix tree traversal here. It assumes this function
426 * aqcuires only the ILOCK (and therefore it has no need to
427 * involve the IOLOCK in this synchronization).
428 */
429 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
430
431 /* reject inode numbers outside existing AGs */
432 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
Dave Chinner24513372014-06-25 14:58:08 +1000433 return -EINVAL;
Dave Chinner33479e02012-10-08 21:56:11 +1100434
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100435 XFS_STATS_INC(mp, xs_ig_attempts);
Lucas Stach8774cf82015-08-28 14:50:56 +1000436
Dave Chinner33479e02012-10-08 21:56:11 +1100437 /* get the perag structure and ensure that it's inode capable */
438 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
439 agino = XFS_INO_TO_AGINO(mp, ino);
440
441again:
442 error = 0;
443 rcu_read_lock();
444 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
445
446 if (ip) {
447 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
448 if (error)
449 goto out_error_or_again;
450 } else {
451 rcu_read_unlock();
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100452 XFS_STATS_INC(mp, xs_ig_missed);
Dave Chinner33479e02012-10-08 21:56:11 +1100453
454 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
455 flags, lock_flags);
456 if (error)
457 goto out_error_or_again;
458 }
459 xfs_perag_put(pag);
460
461 *ipp = ip;
462
463 /*
Dave Chinner58c90472015-02-23 22:38:08 +1100464 * If we have a real type for an on-disk inode, we can setup the inode
Dave Chinner33479e02012-10-08 21:56:11 +1100465 * now. If it's a new inode being created, xfs_ialloc will handle it.
466 */
467 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Dave Chinner58c90472015-02-23 22:38:08 +1100468 xfs_setup_existing_inode(ip);
Dave Chinner33479e02012-10-08 21:56:11 +1100469 return 0;
470
471out_error_or_again:
Dave Chinner24513372014-06-25 14:58:08 +1000472 if (error == -EAGAIN) {
Dave Chinner33479e02012-10-08 21:56:11 +1100473 delay(1);
474 goto again;
475 }
476 xfs_perag_put(pag);
477 return error;
478}
479
Dave Chinner78ae5252010-09-28 12:28:19 +1000480/*
481 * The inode lookup is done in batches to keep the amount of lock traffic and
482 * radix tree lookups to a minimum. The batch size is a trade off between
483 * lookup reduction and stack usage. This is in the reclaim path, so we can't
484 * be too greedy.
485 */
486#define XFS_LOOKUP_BATCH 32
487
Dave Chinnere13de952010-09-28 12:28:06 +1000488STATIC int
489xfs_inode_ag_walk_grab(
490 struct xfs_inode *ip)
491{
492 struct inode *inode = VFS_I(ip);
493
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100494 ASSERT(rcu_read_lock_held());
495
496 /*
497 * check for stale RCU freed inode
498 *
499 * If the inode has been reallocated, it doesn't matter if it's not in
500 * the AG we are walking - we are walking for writeback, so if it
501 * passes all the "valid inode" checks and is dirty, then we'll write
502 * it back anyway. If it has been reallocated and still being
503 * initialised, the XFS_INEW check below will catch it.
504 */
505 spin_lock(&ip->i_flags_lock);
506 if (!ip->i_ino)
507 goto out_unlock_noent;
508
509 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
510 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
511 goto out_unlock_noent;
512 spin_unlock(&ip->i_flags_lock);
513
Dave Chinnere13de952010-09-28 12:28:06 +1000514 /* nothing to sync during shutdown */
515 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000516 return -EFSCORRUPTED;
Dave Chinnere13de952010-09-28 12:28:06 +1000517
Dave Chinnere13de952010-09-28 12:28:06 +1000518 /* If we can't grab the inode, it must on it's way to reclaim. */
519 if (!igrab(inode))
Dave Chinner24513372014-06-25 14:58:08 +1000520 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000521
Dave Chinnere13de952010-09-28 12:28:06 +1000522 /* inode is valid */
523 return 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100524
525out_unlock_noent:
526 spin_unlock(&ip->i_flags_lock);
Dave Chinner24513372014-06-25 14:58:08 +1000527 return -ENOENT;
Dave Chinnere13de952010-09-28 12:28:06 +1000528}
529
Dave Chinner75f3cb12009-06-08 15:35:14 +0200530STATIC int
531xfs_inode_ag_walk(
532 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +0000533 struct xfs_perag *pag,
Eric Sandeene0094002014-04-14 19:04:19 +1000534 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500535 void *args),
536 int flags,
537 void *args,
538 int tag)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200539{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200540 uint32_t first_index;
541 int last_error = 0;
542 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +1000543 int done;
Dave Chinner78ae5252010-09-28 12:28:19 +1000544 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200545
546restart:
Dave Chinner65d0f202010-09-24 18:40:15 +1000547 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200548 skipped = 0;
549 first_index = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000550 nr_found = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200551 do {
Dave Chinner78ae5252010-09-28 12:28:19 +1000552 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
Dave Chinner75f3cb12009-06-08 15:35:14 +0200553 int error = 0;
Dave Chinner78ae5252010-09-28 12:28:19 +1000554 int i;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200555
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100556 rcu_read_lock();
Brian Fostera454f742012-11-06 09:50:39 -0500557
558 if (tag == -1)
559 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
Dave Chinner78ae5252010-09-28 12:28:19 +1000560 (void **)batch, first_index,
561 XFS_LOOKUP_BATCH);
Brian Fostera454f742012-11-06 09:50:39 -0500562 else
563 nr_found = radix_tree_gang_lookup_tag(
564 &pag->pag_ici_root,
565 (void **) batch, first_index,
566 XFS_LOOKUP_BATCH, tag);
567
Dave Chinner65d0f202010-09-24 18:40:15 +1000568 if (!nr_found) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100569 rcu_read_unlock();
Dave Chinner75f3cb12009-06-08 15:35:14 +0200570 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000571 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200572
Dave Chinner65d0f202010-09-24 18:40:15 +1000573 /*
Dave Chinner78ae5252010-09-28 12:28:19 +1000574 * Grab the inodes before we drop the lock. if we found
575 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +1000576 */
Dave Chinner78ae5252010-09-28 12:28:19 +1000577 for (i = 0; i < nr_found; i++) {
578 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +1000579
Dave Chinner78ae5252010-09-28 12:28:19 +1000580 if (done || xfs_inode_ag_walk_grab(ip))
581 batch[i] = NULL;
582
583 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100584 * Update the index for the next lookup. Catch
585 * overflows into the next AG range which can occur if
586 * we have inodes in the last block of the AG and we
587 * are currently pointing to the last inode.
588 *
589 * Because we may see inodes that are from the wrong AG
590 * due to RCU freeing and reallocation, only update the
591 * index if it lies in this AG. It was a race that lead
592 * us to see this inode, so another lookup from the
593 * same index will not find it again.
Dave Chinner78ae5252010-09-28 12:28:19 +1000594 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100595 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
596 continue;
Dave Chinner78ae5252010-09-28 12:28:19 +1000597 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
598 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
599 done = 1;
Dave Chinnere13de952010-09-28 12:28:06 +1000600 }
Dave Chinner78ae5252010-09-28 12:28:19 +1000601
602 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100603 rcu_read_unlock();
Dave Chinnere13de952010-09-28 12:28:06 +1000604
Dave Chinner78ae5252010-09-28 12:28:19 +1000605 for (i = 0; i < nr_found; i++) {
606 if (!batch[i])
607 continue;
Eric Sandeene0094002014-04-14 19:04:19 +1000608 error = execute(batch[i], flags, args);
Dave Chinner78ae5252010-09-28 12:28:19 +1000609 IRELE(batch[i]);
Dave Chinner24513372014-06-25 14:58:08 +1000610 if (error == -EAGAIN) {
Dave Chinner78ae5252010-09-28 12:28:19 +1000611 skipped++;
612 continue;
613 }
Dave Chinner24513372014-06-25 14:58:08 +1000614 if (error && last_error != -EFSCORRUPTED)
Dave Chinner78ae5252010-09-28 12:28:19 +1000615 last_error = error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200616 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000617
618 /* bail out if the filesystem is corrupted. */
Dave Chinner24513372014-06-25 14:58:08 +1000619 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200620 break;
621
Dave Chinner8daaa832011-07-08 14:14:46 +1000622 cond_resched();
623
Dave Chinner78ae5252010-09-28 12:28:19 +1000624 } while (nr_found && !done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200625
626 if (skipped) {
627 delay(1);
628 goto restart;
629 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200630 return last_error;
631}
632
Brian Foster579b62f2012-11-06 09:50:47 -0500633/*
634 * Background scanning to trim post-EOF preallocated space. This is queued
Dwight Engenb9fe5052013-08-15 14:08:02 -0400635 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
Brian Foster579b62f2012-11-06 09:50:47 -0500636 */
637STATIC void
638xfs_queue_eofblocks(
639 struct xfs_mount *mp)
640{
641 rcu_read_lock();
642 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
643 queue_delayed_work(mp->m_eofblocks_workqueue,
644 &mp->m_eofblocks_work,
645 msecs_to_jiffies(xfs_eofb_secs * 1000));
646 rcu_read_unlock();
647}
648
649void
650xfs_eofblocks_worker(
651 struct work_struct *work)
652{
653 struct xfs_mount *mp = container_of(to_delayed_work(work),
654 struct xfs_mount, m_eofblocks_work);
655 xfs_icache_free_eofblocks(mp, NULL);
656 xfs_queue_eofblocks(mp);
657}
658
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200659int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200660xfs_inode_ag_iterator(
661 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000662 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500663 void *args),
664 int flags,
665 void *args)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200666{
Dave Chinner16fd5362010-07-20 09:43:39 +1000667 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200668 int error = 0;
669 int last_error = 0;
670 xfs_agnumber_t ag;
671
Dave Chinner16fd5362010-07-20 09:43:39 +1000672 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000673 while ((pag = xfs_perag_get(mp, ag))) {
674 ag = pag->pag_agno + 1;
Brian Fostera454f742012-11-06 09:50:39 -0500675 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
676 xfs_perag_put(pag);
677 if (error) {
678 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000679 if (error == -EFSCORRUPTED)
Brian Fostera454f742012-11-06 09:50:39 -0500680 break;
681 }
682 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000683 return last_error;
Brian Fostera454f742012-11-06 09:50:39 -0500684}
685
686int
687xfs_inode_ag_iterator_tag(
688 struct xfs_mount *mp,
Eric Sandeene0094002014-04-14 19:04:19 +1000689 int (*execute)(struct xfs_inode *ip, int flags,
Brian Fostera454f742012-11-06 09:50:39 -0500690 void *args),
691 int flags,
692 void *args,
693 int tag)
694{
695 struct xfs_perag *pag;
696 int error = 0;
697 int last_error = 0;
698 xfs_agnumber_t ag;
699
700 ag = 0;
701 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
702 ag = pag->pag_agno + 1;
703 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
Dave Chinner5017e972010-01-11 11:47:40 +0000704 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200705 if (error) {
706 last_error = error;
Dave Chinner24513372014-06-25 14:58:08 +1000707 if (error == -EFSCORRUPTED)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200708 break;
709 }
710 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000711 return last_error;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200712}
713
David Chinner76bf1052008-10-30 17:16:21 +1100714/*
Dave Chinnera7b339f2011-04-08 12:45:07 +1000715 * Queue a new inode reclaim pass if there are reclaimable inodes and there
716 * isn't a reclaim pass already in progress. By default it runs every 5s based
Dave Chinner58896082012-10-08 21:56:05 +1100717 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
Dave Chinnera7b339f2011-04-08 12:45:07 +1000718 * tunable, but that can be done if this method proves to be ineffective or too
719 * aggressive.
720 */
721static void
Dave Chinner58896082012-10-08 21:56:05 +1100722xfs_reclaim_work_queue(
Dave Chinnera7b339f2011-04-08 12:45:07 +1000723 struct xfs_mount *mp)
David Chinnera167b172008-10-30 17:06:18 +1100724{
David Chinnera167b172008-10-30 17:06:18 +1100725
Dave Chinnera7b339f2011-04-08 12:45:07 +1000726 rcu_read_lock();
727 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
Dave Chinner58896082012-10-08 21:56:05 +1100728 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
Dave Chinnera7b339f2011-04-08 12:45:07 +1000729 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
David Chinnera167b172008-10-30 17:06:18 +1100730 }
Dave Chinnera7b339f2011-04-08 12:45:07 +1000731 rcu_read_unlock();
732}
David Chinnera167b172008-10-30 17:06:18 +1100733
Dave Chinnera7b339f2011-04-08 12:45:07 +1000734/*
735 * This is a fast pass over the inode cache to try to get reclaim moving on as
736 * many inodes as possible in a short period of time. It kicks itself every few
737 * seconds, as well as being kicked by the inode cache shrinker when memory
738 * goes low. It scans as quickly as possible avoiding locked inodes or those
739 * already being flushed, and once done schedules a future pass.
740 */
Dave Chinner33c7a2b2012-10-08 21:55:59 +1100741void
Dave Chinnera7b339f2011-04-08 12:45:07 +1000742xfs_reclaim_worker(
743 struct work_struct *work)
744{
745 struct xfs_mount *mp = container_of(to_delayed_work(work),
746 struct xfs_mount, m_reclaim_work);
747
748 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
Dave Chinner58896082012-10-08 21:56:05 +1100749 xfs_reclaim_work_queue(mp);
Dave Chinnera7b339f2011-04-08 12:45:07 +1000750}
751
Dave Chinner33479e02012-10-08 21:56:11 +1100752static void
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400753__xfs_inode_set_reclaim_tag(
754 struct xfs_perag *pag,
755 struct xfs_inode *ip)
756{
757 radix_tree_tag_set(&pag->pag_ici_root,
758 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
759 XFS_ICI_RECLAIM_TAG);
Dave Chinner16fd5362010-07-20 09:43:39 +1000760
761 if (!pag->pag_ici_reclaimable) {
762 /* propagate the reclaim tag up into the perag radix tree */
763 spin_lock(&ip->i_mount->m_perag_lock);
764 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
765 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
766 XFS_ICI_RECLAIM_TAG);
767 spin_unlock(&ip->i_mount->m_perag_lock);
Dave Chinnera7b339f2011-04-08 12:45:07 +1000768
769 /* schedule periodic background inode reclaim */
Dave Chinner58896082012-10-08 21:56:05 +1100770 xfs_reclaim_work_queue(ip->i_mount);
Dave Chinnera7b339f2011-04-08 12:45:07 +1000771
Dave Chinner16fd5362010-07-20 09:43:39 +1000772 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
773 -1, _RET_IP_);
774 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000775 pag->pag_ici_reclaimable++;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400776}
777
David Chinner11654512008-10-30 17:37:49 +1100778/*
779 * We set the inode flag atomically with the radix tree tag.
780 * Once we get tag lookups on the radix tree, this inode flag
781 * can go away.
782 */
David Chinner396beb82008-10-30 17:37:26 +1100783void
784xfs_inode_set_reclaim_tag(
785 xfs_inode_t *ip)
786{
Dave Chinner5017e972010-01-11 11:47:40 +0000787 struct xfs_mount *mp = ip->i_mount;
788 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100789
Dave Chinner5017e972010-01-11 11:47:40 +0000790 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
Dave Chinner1a427ab2010-12-16 17:08:41 +1100791 spin_lock(&pag->pag_ici_lock);
David Chinner396beb82008-10-30 17:37:26 +1100792 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400793 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100794 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100795 spin_unlock(&ip->i_flags_lock);
Dave Chinner1a427ab2010-12-16 17:08:41 +1100796 spin_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000797 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100798}
799
Johannes Weiner081003f2010-10-01 07:43:54 +0000800STATIC void
801__xfs_inode_clear_reclaim(
David Chinner396beb82008-10-30 17:37:26 +1100802 xfs_perag_t *pag,
803 xfs_inode_t *ip)
804{
Dave Chinner9bf729c2010-04-29 09:55:50 +1000805 pag->pag_ici_reclaimable--;
Dave Chinner16fd5362010-07-20 09:43:39 +1000806 if (!pag->pag_ici_reclaimable) {
807 /* clear the reclaim tag from the perag radix tree */
808 spin_lock(&ip->i_mount->m_perag_lock);
809 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
810 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
811 XFS_ICI_RECLAIM_TAG);
812 spin_unlock(&ip->i_mount->m_perag_lock);
813 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
814 -1, _RET_IP_);
815 }
David Chinner396beb82008-10-30 17:37:26 +1100816}
817
Dave Chinner33479e02012-10-08 21:56:11 +1100818STATIC void
Johannes Weiner081003f2010-10-01 07:43:54 +0000819__xfs_inode_clear_reclaim_tag(
820 xfs_mount_t *mp,
821 xfs_perag_t *pag,
822 xfs_inode_t *ip)
823{
824 radix_tree_tag_clear(&pag->pag_ici_root,
825 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
826 __xfs_inode_clear_reclaim(pag, ip);
827}
828
Dave Chinner777df5a2010-02-06 12:37:26 +1100829/*
Dave Chinnere3a20c02010-09-24 19:51:50 +1000830 * Grab the inode for reclaim exclusively.
831 * Return 0 if we grabbed it, non-zero otherwise.
832 */
833STATIC int
834xfs_reclaim_inode_grab(
835 struct xfs_inode *ip,
836 int flags)
837{
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100838 ASSERT(rcu_read_lock_held());
839
840 /* quick check for stale RCU freed inode */
841 if (!ip->i_ino)
842 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000843
844 /*
Christoph Hellwig474fce02011-12-18 20:00:09 +0000845 * If we are asked for non-blocking operation, do unlocked checks to
846 * see if the inode already is being flushed or in reclaim to avoid
847 * lock traffic.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000848 */
849 if ((flags & SYNC_TRYLOCK) &&
Christoph Hellwig474fce02011-12-18 20:00:09 +0000850 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
Dave Chinnere3a20c02010-09-24 19:51:50 +1000851 return 1;
Dave Chinnere3a20c02010-09-24 19:51:50 +1000852
853 /*
854 * The radix tree lock here protects a thread in xfs_iget from racing
855 * with us starting reclaim on the inode. Once we have the
856 * XFS_IRECLAIM flag set it will not touch us.
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100857 *
858 * Due to RCU lookup, we may find inodes that have been freed and only
859 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
860 * aren't candidates for reclaim at all, so we must check the
861 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
Dave Chinnere3a20c02010-09-24 19:51:50 +1000862 */
863 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100864 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
865 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
866 /* not a reclaim candidate. */
Dave Chinnere3a20c02010-09-24 19:51:50 +1000867 spin_unlock(&ip->i_flags_lock);
868 return 1;
869 }
870 __xfs_iflags_set(ip, XFS_IRECLAIM);
871 spin_unlock(&ip->i_flags_lock);
872 return 0;
873}
874
875/*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000876 * Inodes in different states need to be treated differently. The following
877 * table lists the inode states and the reclaim actions necessary:
Dave Chinner777df5a2010-02-06 12:37:26 +1100878 *
879 * inode state iflush ret required action
880 * --------------- ---------- ---------------
881 * bad - reclaim
882 * shutdown EIO unpin and reclaim
883 * clean, unpinned 0 reclaim
884 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100885 * clean, pinned(*) 0 requeue
886 * stale, pinned EAGAIN requeue
Christoph Hellwig8a480882012-04-23 15:58:35 +1000887 * dirty, async - requeue
888 * dirty, sync 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100889 *
890 * (*) dgc: I don't think the clean, pinned state is possible but it gets
891 * handled anyway given the order of checks implemented.
892 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100893 * Also, because we get the flush lock first, we know that any inode that has
894 * been flushed delwri has had the flush completed by the time we check that
Christoph Hellwig8a480882012-04-23 15:58:35 +1000895 * the inode is clean.
Dave Chinnerc8543632010-02-06 12:39:36 +1100896 *
Christoph Hellwig8a480882012-04-23 15:58:35 +1000897 * Note that because the inode is flushed delayed write by AIL pushing, the
898 * flush lock may already be held here and waiting on it can result in very
899 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
900 * the caller should push the AIL first before trying to reclaim inodes to
901 * minimise the amount of time spent waiting. For background relaim, we only
902 * bother to reclaim clean inodes anyway.
Dave Chinnerc8543632010-02-06 12:39:36 +1100903 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100904 * Hence the order of actions after gaining the locks should be:
905 * bad => reclaim
906 * shutdown => unpin and reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000907 * pinned, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100908 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100909 * stale => reclaim
910 * clean => reclaim
Christoph Hellwig8a480882012-04-23 15:58:35 +1000911 * dirty, async => requeue
Dave Chinnerc8543632010-02-06 12:39:36 +1100912 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100913 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200914STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000915xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200916 struct xfs_inode *ip,
917 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000918 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100919{
Christoph Hellwig4c468192012-04-23 15:58:36 +1000920 struct xfs_buf *bp = NULL;
921 int error;
Dave Chinner777df5a2010-02-06 12:37:26 +1100922
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100923restart:
924 error = 0;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000925 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100926 if (!xfs_iflock_nowait(ip)) {
927 if (!(sync_mode & SYNC_WAIT))
928 goto out;
929 xfs_iflock(ip);
930 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000931
Dave Chinner777df5a2010-02-06 12:37:26 +1100932 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
933 xfs_iunpin_wait(ip);
Dave Chinner04913fd2012-04-23 15:58:41 +1000934 xfs_iflush_abort(ip, false);
Dave Chinner777df5a2010-02-06 12:37:26 +1100935 goto reclaim;
936 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100937 if (xfs_ipincount(ip)) {
Christoph Hellwig8a480882012-04-23 15:58:35 +1000938 if (!(sync_mode & SYNC_WAIT))
939 goto out_ifunlock;
Dave Chinner777df5a2010-02-06 12:37:26 +1100940 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100941 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100942 if (xfs_iflags_test(ip, XFS_ISTALE))
943 goto reclaim;
944 if (xfs_inode_clean(ip))
945 goto reclaim;
946
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100947 /*
Christoph Hellwig8a480882012-04-23 15:58:35 +1000948 * Never flush out dirty data during non-blocking reclaim, as it would
949 * just contend with AIL pushing trying to do the same job.
950 */
951 if (!(sync_mode & SYNC_WAIT))
952 goto out_ifunlock;
953
954 /*
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100955 * Now we have an inode that needs flushing.
956 *
Christoph Hellwig4c468192012-04-23 15:58:36 +1000957 * Note that xfs_iflush will never block on the inode buffer lock, as
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100958 * xfs_ifree_cluster() can lock the inode buffer before it locks the
Christoph Hellwig4c468192012-04-23 15:58:36 +1000959 * ip->i_lock, and we are doing the exact opposite here. As a result,
Christoph Hellwig475ee412012-07-03 12:21:22 -0400960 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
961 * result in an ABBA deadlock with xfs_ifree_cluster().
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100962 *
963 * As xfs_ifree_cluser() must gather all inodes that are active in the
964 * cache to mark them stale, if we hit this case we don't actually want
965 * to do IO here - we want the inode marked stale so we can simply
Christoph Hellwig4c468192012-04-23 15:58:36 +1000966 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
967 * inode, back off and try again. Hopefully the next pass through will
968 * see the stale flag set on the inode.
Dave Chinner1bfd8d02011-03-26 09:13:55 +1100969 */
Christoph Hellwig4c468192012-04-23 15:58:36 +1000970 error = xfs_iflush(ip, &bp);
Dave Chinner24513372014-06-25 14:58:08 +1000971 if (error == -EAGAIN) {
Christoph Hellwig8a480882012-04-23 15:58:35 +1000972 xfs_iunlock(ip, XFS_ILOCK_EXCL);
973 /* backoff longer than in xfs_ifree_cluster */
974 delay(2);
975 goto restart;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000976 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100977
Christoph Hellwig4c468192012-04-23 15:58:36 +1000978 if (!error) {
979 error = xfs_bwrite(bp);
980 xfs_buf_relse(bp);
981 }
982
983 xfs_iflock(ip);
Dave Chinner777df5a2010-02-06 12:37:26 +1100984reclaim:
985 xfs_ifunlock(ip);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000986 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000987
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100988 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000989 /*
990 * Remove the inode from the per-AG radix tree.
991 *
992 * Because radix_tree_delete won't complain even if the item was never
993 * added to the tree assert that it's been there before to catch
994 * problems with the inode life time early on.
995 */
Dave Chinner1a427ab2010-12-16 17:08:41 +1100996 spin_lock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000997 if (!radix_tree_delete(&pag->pag_ici_root,
998 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
999 ASSERT(0);
Johannes Weiner081003f2010-10-01 07:43:54 +00001000 __xfs_inode_clear_reclaim(pag, ip);
Dave Chinner1a427ab2010-12-16 17:08:41 +11001001 spin_unlock(&pag->pag_ici_lock);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001002
1003 /*
1004 * Here we do an (almost) spurious inode lock in order to coordinate
1005 * with inode cache radix tree lookups. This is because the lookup
1006 * can reference the inodes in the cache without taking references.
1007 *
1008 * We make that OK here by ensuring that we wait until the inode is
Alex Elderad637a12012-02-16 22:01:00 +00001009 * unlocked after the lookup before we go ahead and free it.
Dave Chinner2f11fea2010-07-20 17:53:25 +10001010 */
Alex Elderad637a12012-02-16 22:01:00 +00001011 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001012 xfs_qm_dqdetach(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001013 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +10001014
1015 xfs_inode_free(ip);
Alex Elderad637a12012-02-16 22:01:00 +00001016 return error;
Christoph Hellwig8a480882012-04-23 15:58:35 +10001017
1018out_ifunlock:
1019 xfs_ifunlock(ip);
1020out:
1021 xfs_iflags_clear(ip, XFS_IRECLAIM);
1022 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1023 /*
Dave Chinner24513372014-06-25 14:58:08 +10001024 * We could return -EAGAIN here to make reclaim rescan the inode tree in
Christoph Hellwig8a480882012-04-23 15:58:35 +10001025 * a short while. However, this just burns CPU time scanning the tree
Dave Chinner58896082012-10-08 21:56:05 +11001026 * waiting for IO to complete and the reclaim work never goes back to
1027 * the idle state. Instead, return 0 to let the next scheduled
1028 * background reclaim attempt to reclaim the inode again.
Christoph Hellwig8a480882012-04-23 15:58:35 +10001029 */
1030 return 0;
David Chinner7a3be022008-10-30 17:37:37 +11001031}
1032
Dave Chinner65d0f202010-09-24 18:40:15 +10001033/*
1034 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1035 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1036 * then a shut down during filesystem unmount reclaim walk leak all the
1037 * unreclaimed inodes.
1038 */
Dave Chinner33479e02012-10-08 21:56:11 +11001039STATIC int
Dave Chinner65d0f202010-09-24 18:40:15 +10001040xfs_reclaim_inodes_ag(
1041 struct xfs_mount *mp,
1042 int flags,
1043 int *nr_to_scan)
1044{
1045 struct xfs_perag *pag;
1046 int error = 0;
1047 int last_error = 0;
1048 xfs_agnumber_t ag;
Dave Chinner69b491c2010-09-27 11:09:51 +10001049 int trylock = flags & SYNC_TRYLOCK;
1050 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +10001051
Dave Chinner69b491c2010-09-27 11:09:51 +10001052restart:
Dave Chinner65d0f202010-09-24 18:40:15 +10001053 ag = 0;
Dave Chinner69b491c2010-09-27 11:09:51 +10001054 skipped = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001055 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1056 unsigned long first_index = 0;
1057 int done = 0;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001058 int nr_found = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +10001059
1060 ag = pag->pag_agno + 1;
1061
Dave Chinner69b491c2010-09-27 11:09:51 +10001062 if (trylock) {
1063 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1064 skipped++;
Dave Chinnerf83282a2010-11-08 08:55:04 +00001065 xfs_perag_put(pag);
Dave Chinner69b491c2010-09-27 11:09:51 +10001066 continue;
1067 }
1068 first_index = pag->pag_ici_reclaim_cursor;
1069 } else
1070 mutex_lock(&pag->pag_ici_reclaim_lock);
1071
Dave Chinner65d0f202010-09-24 18:40:15 +10001072 do {
Dave Chinnere3a20c02010-09-24 19:51:50 +10001073 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1074 int i;
Dave Chinner65d0f202010-09-24 18:40:15 +10001075
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001076 rcu_read_lock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001077 nr_found = radix_tree_gang_lookup_tag(
1078 &pag->pag_ici_root,
1079 (void **)batch, first_index,
1080 XFS_LOOKUP_BATCH,
Dave Chinner65d0f202010-09-24 18:40:15 +10001081 XFS_ICI_RECLAIM_TAG);
1082 if (!nr_found) {
Dave Chinnerb2232212011-05-06 02:54:04 +00001083 done = 1;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001084 rcu_read_unlock();
Dave Chinner65d0f202010-09-24 18:40:15 +10001085 break;
1086 }
1087
1088 /*
Dave Chinnere3a20c02010-09-24 19:51:50 +10001089 * Grab the inodes before we drop the lock. if we found
1090 * nothing, nr == 0 and the loop will be skipped.
Dave Chinner65d0f202010-09-24 18:40:15 +10001091 */
Dave Chinnere3a20c02010-09-24 19:51:50 +10001092 for (i = 0; i < nr_found; i++) {
1093 struct xfs_inode *ip = batch[i];
Dave Chinner65d0f202010-09-24 18:40:15 +10001094
Dave Chinnere3a20c02010-09-24 19:51:50 +10001095 if (done || xfs_reclaim_inode_grab(ip, flags))
1096 batch[i] = NULL;
Dave Chinner65d0f202010-09-24 18:40:15 +10001097
Dave Chinnere3a20c02010-09-24 19:51:50 +10001098 /*
1099 * Update the index for the next lookup. Catch
1100 * overflows into the next AG range which can
1101 * occur if we have inodes in the last block of
1102 * the AG and we are currently pointing to the
1103 * last inode.
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001104 *
1105 * Because we may see inodes that are from the
1106 * wrong AG due to RCU freeing and
1107 * reallocation, only update the index if it
1108 * lies in this AG. It was a race that lead us
1109 * to see this inode, so another lookup from
1110 * the same index will not find it again.
Dave Chinnere3a20c02010-09-24 19:51:50 +10001111 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001112 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1113 pag->pag_agno)
1114 continue;
Dave Chinnere3a20c02010-09-24 19:51:50 +10001115 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1116 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1117 done = 1;
1118 }
1119
1120 /* unlock now we've grabbed the inodes. */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11001121 rcu_read_unlock();
Dave Chinnere3a20c02010-09-24 19:51:50 +10001122
1123 for (i = 0; i < nr_found; i++) {
1124 if (!batch[i])
1125 continue;
1126 error = xfs_reclaim_inode(batch[i], pag, flags);
Dave Chinner24513372014-06-25 14:58:08 +10001127 if (error && last_error != -EFSCORRUPTED)
Dave Chinnere3a20c02010-09-24 19:51:50 +10001128 last_error = error;
1129 }
1130
1131 *nr_to_scan -= XFS_LOOKUP_BATCH;
1132
Dave Chinner8daaa832011-07-08 14:14:46 +10001133 cond_resched();
1134
Dave Chinnere3a20c02010-09-24 19:51:50 +10001135 } while (nr_found && !done && *nr_to_scan > 0);
Dave Chinner65d0f202010-09-24 18:40:15 +10001136
Dave Chinner69b491c2010-09-27 11:09:51 +10001137 if (trylock && !done)
1138 pag->pag_ici_reclaim_cursor = first_index;
1139 else
1140 pag->pag_ici_reclaim_cursor = 0;
1141 mutex_unlock(&pag->pag_ici_reclaim_lock);
Dave Chinner65d0f202010-09-24 18:40:15 +10001142 xfs_perag_put(pag);
1143 }
Dave Chinner69b491c2010-09-27 11:09:51 +10001144
1145 /*
1146 * if we skipped any AG, and we still have scan count remaining, do
1147 * another pass this time using blocking reclaim semantics (i.e
1148 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1149 * ensure that when we get more reclaimers than AGs we block rather
1150 * than spin trying to execute reclaim.
1151 */
Dave Chinner8daaa832011-07-08 14:14:46 +10001152 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
Dave Chinner69b491c2010-09-27 11:09:51 +10001153 trylock = 0;
1154 goto restart;
1155 }
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001156 return last_error;
Dave Chinner65d0f202010-09-24 18:40:15 +10001157}
1158
David Chinnerfce08f22008-10-30 17:37:03 +11001159int
David Chinner1dc33182008-10-30 17:37:15 +11001160xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +11001161 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +11001162 int mode)
1163{
Dave Chinner65d0f202010-09-24 18:40:15 +10001164 int nr_to_scan = INT_MAX;
1165
1166 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001167}
1168
1169/*
Dave Chinner8daaa832011-07-08 14:14:46 +10001170 * Scan a certain number of inodes for reclaim.
Dave Chinnera7b339f2011-04-08 12:45:07 +10001171 *
1172 * When called we make sure that there is a background (fast) inode reclaim in
Dave Chinner8daaa832011-07-08 14:14:46 +10001173 * progress, while we will throttle the speed of reclaim via doing synchronous
Dave Chinnera7b339f2011-04-08 12:45:07 +10001174 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1175 * them to be cleaned, which we hope will not be very long due to the
1176 * background walker having already kicked the IO off on those dirty inodes.
Dave Chinner9bf729c2010-04-29 09:55:50 +10001177 */
Dave Chinner0a234c62013-08-28 10:17:57 +10001178long
Dave Chinner8daaa832011-07-08 14:14:46 +10001179xfs_reclaim_inodes_nr(
1180 struct xfs_mount *mp,
1181 int nr_to_scan)
Dave Chinner9bf729c2010-04-29 09:55:50 +10001182{
Dave Chinner8daaa832011-07-08 14:14:46 +10001183 /* kick background reclaimer and push the AIL */
Dave Chinner58896082012-10-08 21:56:05 +11001184 xfs_reclaim_work_queue(mp);
Dave Chinner8daaa832011-07-08 14:14:46 +10001185 xfs_ail_push_all(mp->m_ail);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001186
Dave Chinner0a234c62013-08-28 10:17:57 +10001187 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
Dave Chinner8daaa832011-07-08 14:14:46 +10001188}
Dave Chinnera7b339f2011-04-08 12:45:07 +10001189
Dave Chinner8daaa832011-07-08 14:14:46 +10001190/*
1191 * Return the number of reclaimable inodes in the filesystem for
1192 * the shrinker to determine how much to reclaim.
1193 */
1194int
1195xfs_reclaim_inodes_count(
1196 struct xfs_mount *mp)
1197{
1198 struct xfs_perag *pag;
1199 xfs_agnumber_t ag = 0;
1200 int reclaimable = 0;
Dave Chinner9bf729c2010-04-29 09:55:50 +10001201
Dave Chinner65d0f202010-09-24 18:40:15 +10001202 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1203 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +10001204 reclaimable += pag->pag_ici_reclaimable;
1205 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +10001206 }
Dave Chinner9bf729c2010-04-29 09:55:50 +10001207 return reclaimable;
1208}
1209
Brian Foster41176a62012-11-06 09:50:42 -05001210STATIC int
Brian Foster3e3f9f52012-11-07 12:21:13 -05001211xfs_inode_match_id(
1212 struct xfs_inode *ip,
1213 struct xfs_eofblocks *eofb)
1214{
Dwight Engenb9fe5052013-08-15 14:08:02 -04001215 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1216 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
Brian Foster1b556042012-11-06 09:50:45 -05001217 return 0;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001218
Dwight Engenb9fe5052013-08-15 14:08:02 -04001219 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1220 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
Brian Foster1b556042012-11-06 09:50:45 -05001221 return 0;
1222
Dwight Engenb9fe5052013-08-15 14:08:02 -04001223 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
Brian Foster1b556042012-11-06 09:50:45 -05001224 xfs_get_projid(ip) != eofb->eof_prid)
1225 return 0;
1226
1227 return 1;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001228}
1229
Brian Fosterf4526392014-07-24 19:44:28 +10001230/*
1231 * A union-based inode filtering algorithm. Process the inode if any of the
1232 * criteria match. This is for global/internal scans only.
1233 */
1234STATIC int
1235xfs_inode_match_id_union(
1236 struct xfs_inode *ip,
1237 struct xfs_eofblocks *eofb)
1238{
1239 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1240 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1241 return 1;
1242
1243 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1244 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1245 return 1;
1246
1247 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1248 xfs_get_projid(ip) == eofb->eof_prid)
1249 return 1;
1250
1251 return 0;
1252}
1253
Brian Foster3e3f9f52012-11-07 12:21:13 -05001254STATIC int
Brian Foster41176a62012-11-06 09:50:42 -05001255xfs_inode_free_eofblocks(
1256 struct xfs_inode *ip,
Brian Foster41176a62012-11-06 09:50:42 -05001257 int flags,
1258 void *args)
1259{
1260 int ret;
Brian Foster3e3f9f52012-11-07 12:21:13 -05001261 struct xfs_eofblocks *eofb = args;
Brian Foster5400da72014-07-24 19:40:22 +10001262 bool need_iolock = true;
Brian Fosterf4526392014-07-24 19:44:28 +10001263 int match;
Brian Foster5400da72014-07-24 19:40:22 +10001264
1265 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
Brian Foster41176a62012-11-06 09:50:42 -05001266
1267 if (!xfs_can_free_eofblocks(ip, false)) {
1268 /* inode could be preallocated or append-only */
1269 trace_xfs_inode_free_eofblocks_invalid(ip);
1270 xfs_inode_clear_eofblocks_tag(ip);
1271 return 0;
1272 }
1273
1274 /*
1275 * If the mapping is dirty the operation can block and wait for some
1276 * time. Unless we are waiting, skip it.
1277 */
1278 if (!(flags & SYNC_WAIT) &&
1279 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1280 return 0;
1281
Brian Foster00ca79a2012-11-07 12:21:14 -05001282 if (eofb) {
Brian Fosterf4526392014-07-24 19:44:28 +10001283 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1284 match = xfs_inode_match_id_union(ip, eofb);
1285 else
1286 match = xfs_inode_match_id(ip, eofb);
1287 if (!match)
Brian Foster00ca79a2012-11-07 12:21:14 -05001288 return 0;
1289
1290 /* skip the inode if the file size is too small */
1291 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1292 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1293 return 0;
Brian Foster5400da72014-07-24 19:40:22 +10001294
1295 /*
1296 * A scan owner implies we already hold the iolock. Skip it in
1297 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1298 * the possibility of EAGAIN being returned.
1299 */
1300 if (eofb->eof_scan_owner == ip->i_ino)
1301 need_iolock = false;
Brian Foster00ca79a2012-11-07 12:21:14 -05001302 }
Brian Foster3e3f9f52012-11-07 12:21:13 -05001303
Brian Foster5400da72014-07-24 19:40:22 +10001304 ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
Brian Foster41176a62012-11-06 09:50:42 -05001305
1306 /* don't revisit the inode if we're not waiting */
Dave Chinner24513372014-06-25 14:58:08 +10001307 if (ret == -EAGAIN && !(flags & SYNC_WAIT))
Brian Foster41176a62012-11-06 09:50:42 -05001308 ret = 0;
1309
1310 return ret;
1311}
1312
1313int
1314xfs_icache_free_eofblocks(
1315 struct xfs_mount *mp,
Brian Foster8ca149d2012-11-07 12:21:12 -05001316 struct xfs_eofblocks *eofb)
Brian Foster41176a62012-11-06 09:50:42 -05001317{
Brian Foster8ca149d2012-11-07 12:21:12 -05001318 int flags = SYNC_TRYLOCK;
1319
1320 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1321 flags = SYNC_WAIT;
1322
Brian Foster41176a62012-11-06 09:50:42 -05001323 return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
Brian Foster8ca149d2012-11-07 12:21:12 -05001324 eofb, XFS_ICI_EOFBLOCKS_TAG);
Brian Foster41176a62012-11-06 09:50:42 -05001325}
1326
Brian Fosterdc06f3982014-07-24 19:49:28 +10001327/*
1328 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1329 * multiple quotas, we don't know exactly which quota caused an allocation
1330 * failure. We make a best effort by including each quota under low free space
1331 * conditions (less than 1% free space) in the scan.
1332 */
1333int
1334xfs_inode_free_quota_eofblocks(
1335 struct xfs_inode *ip)
1336{
1337 int scan = 0;
1338 struct xfs_eofblocks eofb = {0};
1339 struct xfs_dquot *dq;
1340
1341 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1342
1343 /*
1344 * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1345 * can repeatedly trylock on the inode we're currently processing. We
1346 * run a sync scan to increase effectiveness and use the union filter to
1347 * cover all applicable quotas in a single scan.
1348 */
1349 eofb.eof_scan_owner = ip->i_ino;
1350 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1351
1352 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1353 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1354 if (dq && xfs_dquot_lowsp(dq)) {
1355 eofb.eof_uid = VFS_I(ip)->i_uid;
1356 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1357 scan = 1;
1358 }
1359 }
1360
1361 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1362 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1363 if (dq && xfs_dquot_lowsp(dq)) {
1364 eofb.eof_gid = VFS_I(ip)->i_gid;
1365 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1366 scan = 1;
1367 }
1368 }
1369
1370 if (scan)
1371 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
1372
1373 return scan;
1374}
1375
Brian Foster27b52862012-11-06 09:50:38 -05001376void
1377xfs_inode_set_eofblocks_tag(
1378 xfs_inode_t *ip)
1379{
1380 struct xfs_mount *mp = ip->i_mount;
1381 struct xfs_perag *pag;
1382 int tagged;
1383
1384 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1385 spin_lock(&pag->pag_ici_lock);
1386 trace_xfs_inode_set_eofblocks_tag(ip);
1387
1388 tagged = radix_tree_tagged(&pag->pag_ici_root,
1389 XFS_ICI_EOFBLOCKS_TAG);
1390 radix_tree_tag_set(&pag->pag_ici_root,
1391 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1392 XFS_ICI_EOFBLOCKS_TAG);
1393 if (!tagged) {
1394 /* propagate the eofblocks tag up into the perag radix tree */
1395 spin_lock(&ip->i_mount->m_perag_lock);
1396 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1397 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1398 XFS_ICI_EOFBLOCKS_TAG);
1399 spin_unlock(&ip->i_mount->m_perag_lock);
1400
Brian Foster579b62f2012-11-06 09:50:47 -05001401 /* kick off background trimming */
1402 xfs_queue_eofblocks(ip->i_mount);
1403
Brian Foster27b52862012-11-06 09:50:38 -05001404 trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
1405 -1, _RET_IP_);
1406 }
1407
1408 spin_unlock(&pag->pag_ici_lock);
1409 xfs_perag_put(pag);
1410}
1411
1412void
1413xfs_inode_clear_eofblocks_tag(
1414 xfs_inode_t *ip)
1415{
1416 struct xfs_mount *mp = ip->i_mount;
1417 struct xfs_perag *pag;
1418
1419 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1420 spin_lock(&pag->pag_ici_lock);
1421 trace_xfs_inode_clear_eofblocks_tag(ip);
1422
1423 radix_tree_tag_clear(&pag->pag_ici_root,
1424 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1425 XFS_ICI_EOFBLOCKS_TAG);
1426 if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
1427 /* clear the eofblocks tag from the perag radix tree */
1428 spin_lock(&ip->i_mount->m_perag_lock);
1429 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1430 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1431 XFS_ICI_EOFBLOCKS_TAG);
1432 spin_unlock(&ip->i_mount->m_perag_lock);
1433 trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
1434 -1, _RET_IP_);
1435 }
1436
1437 spin_unlock(&pag->pag_ici_lock);
1438 xfs_perag_put(pag);
1439}
1440