2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_error.h"
28 #include "xfs_trans.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_quota.h"
32 #include "xfs_trace.h"
33 #include "xfs_icache.h"
34 #include "xfs_bmap_util.h"
35 #include "xfs_dquot_item.h"
36 #include "xfs_dquot.h"
38 #include <linux/kthread.h>
39 #include <linux/freezer.h>
41 STATIC void __xfs_inode_clear_reclaim_tag(struct xfs_mount *mp,
42 struct xfs_perag *pag, struct xfs_inode *ip);
45 * Allocate and initialise an xfs_inode.
55 * if this didn't occur in transactions, we could use
56 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
57 * code up to do this anyway.
59 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
62 if (inode_init_always(mp->m_super, VFS_I(ip))) {
63 kmem_zone_free(xfs_inode_zone, ip);
67 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);
72 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
74 /* initialise the xfs inode */
77 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
79 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
81 ip->i_delayed_blks = 0;
82 memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
88 xfs_inode_free_callback(
89 struct rcu_head *head)
91 struct inode *inode = container_of(head, struct inode, i_rcu);
92 struct xfs_inode *ip = XFS_I(inode);
94 kmem_zone_free(xfs_inode_zone, ip);
101 switch (ip->i_d.di_mode & S_IFMT) {
105 xfs_idestroy_fork(ip, XFS_DATA_FORK);
110 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
113 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
114 xfs_inode_item_destroy(ip);
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
124 spin_lock(&ip->i_flags_lock);
125 ip->i_flags = XFS_IRECLAIM;
127 spin_unlock(&ip->i_flags_lock);
129 /* asserts to verify all state is correct here */
130 ASSERT(atomic_read(&ip->i_pincount) == 0);
131 ASSERT(!xfs_isiflocked(ip));
133 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
137 * Check the validity of the inode we just found it the cache
141 struct xfs_perag *pag,
142 struct xfs_inode *ip,
145 int lock_flags) __releases(RCU)
147 struct inode *inode = VFS_I(ip);
148 struct xfs_mount *mp = ip->i_mount;
152 * check for re-use of an inode within an RCU grace period due to the
153 * radix tree nodes not being updated yet. We monitor for this by
154 * setting the inode number to zero before freeing the inode structure.
155 * If the inode has been reallocated and set up, then the inode number
156 * will not match, so check for that, too.
158 spin_lock(&ip->i_flags_lock);
159 if (ip->i_ino != ino) {
160 trace_xfs_iget_skip(ip);
161 XFS_STATS_INC(xs_ig_frecycle);
168 * If we are racing with another cache hit that is currently
169 * instantiating this inode or currently recycling it out of
170 * reclaimabe state, wait for the initialisation to complete
173 * XXX(hch): eventually we should do something equivalent to
174 * wait_on_inode to wait for these flags to be cleared
175 * instead of polling for it.
177 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
178 trace_xfs_iget_skip(ip);
179 XFS_STATS_INC(xs_ig_frecycle);
185 * If lookup is racing with unlink return an error immediately.
187 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
193 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
194 * Need to carefully get it back into useable state.
196 if (ip->i_flags & XFS_IRECLAIMABLE) {
197 trace_xfs_iget_reclaim(ip);
200 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
201 * from stomping over us while we recycle the inode. We can't
202 * clear the radix tree reclaimable tag yet as it requires
203 * pag_ici_lock to be held exclusive.
205 ip->i_flags |= XFS_IRECLAIM;
207 spin_unlock(&ip->i_flags_lock);
210 error = inode_init_always(mp->m_super, inode);
213 * Re-initializing the inode failed, and we are in deep
214 * trouble. Try to re-add it to the reclaim list.
217 spin_lock(&ip->i_flags_lock);
219 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
220 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
221 trace_xfs_iget_reclaim_fail(ip);
225 spin_lock(&pag->pag_ici_lock);
226 spin_lock(&ip->i_flags_lock);
229 * Clear the per-lifetime state in the inode as we are now
230 * effectively a new inode and need to return to the initial
231 * state before reuse occurs.
233 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
234 ip->i_flags |= XFS_INEW;
235 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
236 inode->i_state = I_NEW;
238 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
239 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
241 spin_unlock(&ip->i_flags_lock);
242 spin_unlock(&pag->pag_ici_lock);
244 /* If the VFS inode is being torn down, pause and try again. */
246 trace_xfs_iget_skip(ip);
251 /* We've got a live one. */
252 spin_unlock(&ip->i_flags_lock);
254 trace_xfs_iget_hit(ip);
258 xfs_ilock(ip, lock_flags);
260 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
261 XFS_STATS_INC(xs_ig_found);
266 spin_unlock(&ip->i_flags_lock);
274 struct xfs_mount *mp,
275 struct xfs_perag *pag,
278 struct xfs_inode **ipp,
282 struct xfs_inode *ip;
284 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
287 ip = xfs_inode_alloc(mp, ino);
291 error = xfs_iread(mp, tp, ip, flags);
295 trace_xfs_iget_miss(ip);
297 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
303 * Preload the radix tree so we can insert safely under the
304 * write spinlock. Note that we cannot sleep inside the preload
305 * region. Since we can be called from transaction context, don't
306 * recurse into the file system.
308 if (radix_tree_preload(GFP_NOFS)) {
314 * Because the inode hasn't been added to the radix-tree yet it can't
315 * be found by another thread, so we can do the non-sleeping lock here.
318 if (!xfs_ilock_nowait(ip, lock_flags))
323 * These values must be set before inserting the inode into the radix
324 * tree as the moment it is inserted a concurrent lookup (allowed by the
325 * RCU locking mechanism) can find it and that lookup must see that this
326 * is an inode currently under construction (i.e. that XFS_INEW is set).
327 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
328 * memory barrier that ensures this detection works correctly at lookup
332 if (flags & XFS_IGET_DONTCACHE)
333 iflags |= XFS_IDONTCACHE;
337 xfs_iflags_set(ip, iflags);
339 /* insert the new inode */
340 spin_lock(&pag->pag_ici_lock);
341 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
342 if (unlikely(error)) {
343 WARN_ON(error != -EEXIST);
344 XFS_STATS_INC(xs_ig_dup);
346 goto out_preload_end;
348 spin_unlock(&pag->pag_ici_lock);
349 radix_tree_preload_end();
355 spin_unlock(&pag->pag_ici_lock);
356 radix_tree_preload_end();
358 xfs_iunlock(ip, lock_flags);
360 __destroy_inode(VFS_I(ip));
366 * Look up an inode by number in the given file system.
367 * The inode is looked up in the cache held in each AG.
368 * If the inode is found in the cache, initialise the vfs inode
371 * If it is not in core, read it in from the file system's device,
372 * add it to the cache and initialise the vfs inode.
374 * The inode is locked according to the value of the lock_flags parameter.
375 * This flag parameter indicates how and if the inode's IO lock and inode lock
378 * mp -- the mount point structure for the current file system. It points
379 * to the inode hash table.
380 * tp -- a pointer to the current transaction if there is one. This is
381 * simply passed through to the xfs_iread() call.
382 * ino -- the number of the inode desired. This is the unique identifier
383 * within the file system for the inode being requested.
384 * lock_flags -- flags indicating how to lock the inode. See the comment
385 * for xfs_ilock() for a list of valid values.
402 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
403 * doesn't get freed while it's being referenced during a
404 * radix tree traversal here. It assumes this function
405 * aqcuires only the ILOCK (and therefore it has no need to
406 * involve the IOLOCK in this synchronization).
408 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
410 /* reject inode numbers outside existing AGs */
411 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
414 /* get the perag structure and ensure that it's inode capable */
415 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
416 agino = XFS_INO_TO_AGINO(mp, ino);
421 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
424 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
426 goto out_error_or_again;
429 XFS_STATS_INC(xs_ig_missed);
431 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
434 goto out_error_or_again;
441 * If we have a real type for an on-disk inode, we can set ops(&unlock)
442 * now. If it's a new inode being created, xfs_ialloc will handle it.
444 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
449 if (error == -EAGAIN) {
458 * The inode lookup is done in batches to keep the amount of lock traffic and
459 * radix tree lookups to a minimum. The batch size is a trade off between
460 * lookup reduction and stack usage. This is in the reclaim path, so we can't
463 #define XFS_LOOKUP_BATCH 32
466 xfs_inode_ag_walk_grab(
467 struct xfs_inode *ip)
469 struct inode *inode = VFS_I(ip);
471 ASSERT(rcu_read_lock_held());
474 * check for stale RCU freed inode
476 * If the inode has been reallocated, it doesn't matter if it's not in
477 * the AG we are walking - we are walking for writeback, so if it
478 * passes all the "valid inode" checks and is dirty, then we'll write
479 * it back anyway. If it has been reallocated and still being
480 * initialised, the XFS_INEW check below will catch it.
482 spin_lock(&ip->i_flags_lock);
484 goto out_unlock_noent;
486 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
487 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
488 goto out_unlock_noent;
489 spin_unlock(&ip->i_flags_lock);
491 /* nothing to sync during shutdown */
492 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
493 return -EFSCORRUPTED;
495 /* If we can't grab the inode, it must on it's way to reclaim. */
503 spin_unlock(&ip->i_flags_lock);
509 struct xfs_mount *mp,
510 struct xfs_perag *pag,
511 int (*execute)(struct xfs_inode *ip, int flags,
517 uint32_t first_index;
529 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
536 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
537 (void **)batch, first_index,
540 nr_found = radix_tree_gang_lookup_tag(
542 (void **) batch, first_index,
543 XFS_LOOKUP_BATCH, tag);
551 * Grab the inodes before we drop the lock. if we found
552 * nothing, nr == 0 and the loop will be skipped.
554 for (i = 0; i < nr_found; i++) {
555 struct xfs_inode *ip = batch[i];
557 if (done || xfs_inode_ag_walk_grab(ip))
561 * Update the index for the next lookup. Catch
562 * overflows into the next AG range which can occur if
563 * we have inodes in the last block of the AG and we
564 * are currently pointing to the last inode.
566 * Because we may see inodes that are from the wrong AG
567 * due to RCU freeing and reallocation, only update the
568 * index if it lies in this AG. It was a race that lead
569 * us to see this inode, so another lookup from the
570 * same index will not find it again.
572 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
574 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
575 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
579 /* unlock now we've grabbed the inodes. */
582 for (i = 0; i < nr_found; i++) {
585 error = execute(batch[i], flags, args);
587 if (error == -EAGAIN) {
591 if (error && last_error != -EFSCORRUPTED)
595 /* bail out if the filesystem is corrupted. */
596 if (error == -EFSCORRUPTED)
601 } while (nr_found && !done);
611 * Background scanning to trim post-EOF preallocated space. This is queued
612 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
616 struct xfs_mount *mp)
619 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
620 queue_delayed_work(mp->m_eofblocks_workqueue,
621 &mp->m_eofblocks_work,
622 msecs_to_jiffies(xfs_eofb_secs * 1000));
627 xfs_eofblocks_worker(
628 struct work_struct *work)
630 struct xfs_mount *mp = container_of(to_delayed_work(work),
631 struct xfs_mount, m_eofblocks_work);
632 xfs_icache_free_eofblocks(mp, NULL);
633 xfs_queue_eofblocks(mp);
637 xfs_inode_ag_iterator(
638 struct xfs_mount *mp,
639 int (*execute)(struct xfs_inode *ip, int flags,
644 struct xfs_perag *pag;
650 while ((pag = xfs_perag_get(mp, ag))) {
651 ag = pag->pag_agno + 1;
652 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
656 if (error == -EFSCORRUPTED)
664 xfs_inode_ag_iterator_tag(
665 struct xfs_mount *mp,
666 int (*execute)(struct xfs_inode *ip, int flags,
672 struct xfs_perag *pag;
678 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
679 ag = pag->pag_agno + 1;
680 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
684 if (error == -EFSCORRUPTED)
692 * Queue a new inode reclaim pass if there are reclaimable inodes and there
693 * isn't a reclaim pass already in progress. By default it runs every 5s based
694 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
695 * tunable, but that can be done if this method proves to be ineffective or too
699 xfs_reclaim_work_queue(
700 struct xfs_mount *mp)
704 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
705 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
706 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
712 * This is a fast pass over the inode cache to try to get reclaim moving on as
713 * many inodes as possible in a short period of time. It kicks itself every few
714 * seconds, as well as being kicked by the inode cache shrinker when memory
715 * goes low. It scans as quickly as possible avoiding locked inodes or those
716 * already being flushed, and once done schedules a future pass.
720 struct work_struct *work)
722 struct xfs_mount *mp = container_of(to_delayed_work(work),
723 struct xfs_mount, m_reclaim_work);
725 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
726 xfs_reclaim_work_queue(mp);
730 __xfs_inode_set_reclaim_tag(
731 struct xfs_perag *pag,
732 struct xfs_inode *ip)
734 radix_tree_tag_set(&pag->pag_ici_root,
735 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
736 XFS_ICI_RECLAIM_TAG);
738 if (!pag->pag_ici_reclaimable) {
739 /* propagate the reclaim tag up into the perag radix tree */
740 spin_lock(&ip->i_mount->m_perag_lock);
741 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
742 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
743 XFS_ICI_RECLAIM_TAG);
744 spin_unlock(&ip->i_mount->m_perag_lock);
746 /* schedule periodic background inode reclaim */
747 xfs_reclaim_work_queue(ip->i_mount);
749 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
752 pag->pag_ici_reclaimable++;
756 * We set the inode flag atomically with the radix tree tag.
757 * Once we get tag lookups on the radix tree, this inode flag
761 xfs_inode_set_reclaim_tag(
764 struct xfs_mount *mp = ip->i_mount;
765 struct xfs_perag *pag;
767 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
768 spin_lock(&pag->pag_ici_lock);
769 spin_lock(&ip->i_flags_lock);
770 __xfs_inode_set_reclaim_tag(pag, ip);
771 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
772 spin_unlock(&ip->i_flags_lock);
773 spin_unlock(&pag->pag_ici_lock);
778 __xfs_inode_clear_reclaim(
782 pag->pag_ici_reclaimable--;
783 if (!pag->pag_ici_reclaimable) {
784 /* clear the reclaim tag from the perag radix tree */
785 spin_lock(&ip->i_mount->m_perag_lock);
786 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
787 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
788 XFS_ICI_RECLAIM_TAG);
789 spin_unlock(&ip->i_mount->m_perag_lock);
790 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
796 __xfs_inode_clear_reclaim_tag(
801 radix_tree_tag_clear(&pag->pag_ici_root,
802 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
803 __xfs_inode_clear_reclaim(pag, ip);
807 * Grab the inode for reclaim exclusively.
808 * Return 0 if we grabbed it, non-zero otherwise.
811 xfs_reclaim_inode_grab(
812 struct xfs_inode *ip,
815 ASSERT(rcu_read_lock_held());
817 /* quick check for stale RCU freed inode */
822 * If we are asked for non-blocking operation, do unlocked checks to
823 * see if the inode already is being flushed or in reclaim to avoid
826 if ((flags & SYNC_TRYLOCK) &&
827 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
831 * The radix tree lock here protects a thread in xfs_iget from racing
832 * with us starting reclaim on the inode. Once we have the
833 * XFS_IRECLAIM flag set it will not touch us.
835 * Due to RCU lookup, we may find inodes that have been freed and only
836 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
837 * aren't candidates for reclaim at all, so we must check the
838 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
840 spin_lock(&ip->i_flags_lock);
841 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
842 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
843 /* not a reclaim candidate. */
844 spin_unlock(&ip->i_flags_lock);
847 __xfs_iflags_set(ip, XFS_IRECLAIM);
848 spin_unlock(&ip->i_flags_lock);
853 * Inodes in different states need to be treated differently. The following
854 * table lists the inode states and the reclaim actions necessary:
856 * inode state iflush ret required action
857 * --------------- ---------- ---------------
859 * shutdown EIO unpin and reclaim
860 * clean, unpinned 0 reclaim
861 * stale, unpinned 0 reclaim
862 * clean, pinned(*) 0 requeue
863 * stale, pinned EAGAIN requeue
864 * dirty, async - requeue
865 * dirty, sync 0 reclaim
867 * (*) dgc: I don't think the clean, pinned state is possible but it gets
868 * handled anyway given the order of checks implemented.
870 * Also, because we get the flush lock first, we know that any inode that has
871 * been flushed delwri has had the flush completed by the time we check that
872 * the inode is clean.
874 * Note that because the inode is flushed delayed write by AIL pushing, the
875 * flush lock may already be held here and waiting on it can result in very
876 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
877 * the caller should push the AIL first before trying to reclaim inodes to
878 * minimise the amount of time spent waiting. For background relaim, we only
879 * bother to reclaim clean inodes anyway.
881 * Hence the order of actions after gaining the locks should be:
883 * shutdown => unpin and reclaim
884 * pinned, async => requeue
885 * pinned, sync => unpin
888 * dirty, async => requeue
889 * dirty, sync => flush, wait and reclaim
893 struct xfs_inode *ip,
894 struct xfs_perag *pag,
897 struct xfs_buf *bp = NULL;
902 xfs_ilock(ip, XFS_ILOCK_EXCL);
903 if (!xfs_iflock_nowait(ip)) {
904 if (!(sync_mode & SYNC_WAIT))
909 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
911 xfs_iflush_abort(ip, false);
914 if (xfs_ipincount(ip)) {
915 if (!(sync_mode & SYNC_WAIT))
919 if (xfs_iflags_test(ip, XFS_ISTALE))
921 if (xfs_inode_clean(ip))
925 * Never flush out dirty data during non-blocking reclaim, as it would
926 * just contend with AIL pushing trying to do the same job.
928 if (!(sync_mode & SYNC_WAIT))
932 * Now we have an inode that needs flushing.
934 * Note that xfs_iflush will never block on the inode buffer lock, as
935 * xfs_ifree_cluster() can lock the inode buffer before it locks the
936 * ip->i_lock, and we are doing the exact opposite here. As a result,
937 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
938 * result in an ABBA deadlock with xfs_ifree_cluster().
940 * As xfs_ifree_cluser() must gather all inodes that are active in the
941 * cache to mark them stale, if we hit this case we don't actually want
942 * to do IO here - we want the inode marked stale so we can simply
943 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
944 * inode, back off and try again. Hopefully the next pass through will
945 * see the stale flag set on the inode.
947 error = xfs_iflush(ip, &bp);
948 if (error == -EAGAIN) {
949 xfs_iunlock(ip, XFS_ILOCK_EXCL);
950 /* backoff longer than in xfs_ifree_cluster */
956 error = xfs_bwrite(bp);
963 xfs_iunlock(ip, XFS_ILOCK_EXCL);
965 XFS_STATS_INC(xs_ig_reclaims);
967 * Remove the inode from the per-AG radix tree.
969 * Because radix_tree_delete won't complain even if the item was never
970 * added to the tree assert that it's been there before to catch
971 * problems with the inode life time early on.
973 spin_lock(&pag->pag_ici_lock);
974 if (!radix_tree_delete(&pag->pag_ici_root,
975 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
977 __xfs_inode_clear_reclaim(pag, ip);
978 spin_unlock(&pag->pag_ici_lock);
981 * Here we do an (almost) spurious inode lock in order to coordinate
982 * with inode cache radix tree lookups. This is because the lookup
983 * can reference the inodes in the cache without taking references.
985 * We make that OK here by ensuring that we wait until the inode is
986 * unlocked after the lookup before we go ahead and free it.
988 xfs_ilock(ip, XFS_ILOCK_EXCL);
990 xfs_iunlock(ip, XFS_ILOCK_EXCL);
998 xfs_iflags_clear(ip, XFS_IRECLAIM);
999 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1001 * We could return -EAGAIN here to make reclaim rescan the inode tree in
1002 * a short while. However, this just burns CPU time scanning the tree
1003 * waiting for IO to complete and the reclaim work never goes back to
1004 * the idle state. Instead, return 0 to let the next scheduled
1005 * background reclaim attempt to reclaim the inode again.
1011 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1012 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1013 * then a shut down during filesystem unmount reclaim walk leak all the
1014 * unreclaimed inodes.
1017 xfs_reclaim_inodes_ag(
1018 struct xfs_mount *mp,
1022 struct xfs_perag *pag;
1026 int trylock = flags & SYNC_TRYLOCK;
1032 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1033 unsigned long first_index = 0;
1037 ag = pag->pag_agno + 1;
1040 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1045 first_index = pag->pag_ici_reclaim_cursor;
1047 mutex_lock(&pag->pag_ici_reclaim_lock);
1050 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1054 nr_found = radix_tree_gang_lookup_tag(
1056 (void **)batch, first_index,
1058 XFS_ICI_RECLAIM_TAG);
1066 * Grab the inodes before we drop the lock. if we found
1067 * nothing, nr == 0 and the loop will be skipped.
1069 for (i = 0; i < nr_found; i++) {
1070 struct xfs_inode *ip = batch[i];
1072 if (done || xfs_reclaim_inode_grab(ip, flags))
1076 * Update the index for the next lookup. Catch
1077 * overflows into the next AG range which can
1078 * occur if we have inodes in the last block of
1079 * the AG and we are currently pointing to the
1082 * Because we may see inodes that are from the
1083 * wrong AG due to RCU freeing and
1084 * reallocation, only update the index if it
1085 * lies in this AG. It was a race that lead us
1086 * to see this inode, so another lookup from
1087 * the same index will not find it again.
1089 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1092 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1093 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1097 /* unlock now we've grabbed the inodes. */
1100 for (i = 0; i < nr_found; i++) {
1103 error = xfs_reclaim_inode(batch[i], pag, flags);
1104 if (error && last_error != -EFSCORRUPTED)
1108 *nr_to_scan -= XFS_LOOKUP_BATCH;
1112 } while (nr_found && !done && *nr_to_scan > 0);
1114 if (trylock && !done)
1115 pag->pag_ici_reclaim_cursor = first_index;
1117 pag->pag_ici_reclaim_cursor = 0;
1118 mutex_unlock(&pag->pag_ici_reclaim_lock);
1123 * if we skipped any AG, and we still have scan count remaining, do
1124 * another pass this time using blocking reclaim semantics (i.e
1125 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1126 * ensure that when we get more reclaimers than AGs we block rather
1127 * than spin trying to execute reclaim.
1129 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
1141 int nr_to_scan = INT_MAX;
1143 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
1147 * Scan a certain number of inodes for reclaim.
1149 * When called we make sure that there is a background (fast) inode reclaim in
1150 * progress, while we will throttle the speed of reclaim via doing synchronous
1151 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1152 * them to be cleaned, which we hope will not be very long due to the
1153 * background walker having already kicked the IO off on those dirty inodes.
1156 xfs_reclaim_inodes_nr(
1157 struct xfs_mount *mp,
1160 /* kick background reclaimer and push the AIL */
1161 xfs_reclaim_work_queue(mp);
1162 xfs_ail_push_all(mp->m_ail);
1164 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
1168 * Return the number of reclaimable inodes in the filesystem for
1169 * the shrinker to determine how much to reclaim.
1172 xfs_reclaim_inodes_count(
1173 struct xfs_mount *mp)
1175 struct xfs_perag *pag;
1176 xfs_agnumber_t ag = 0;
1177 int reclaimable = 0;
1179 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1180 ag = pag->pag_agno + 1;
1181 reclaimable += pag->pag_ici_reclaimable;
1189 struct xfs_inode *ip,
1190 struct xfs_eofblocks *eofb)
1192 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1193 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1196 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1197 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1200 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1201 xfs_get_projid(ip) != eofb->eof_prid)
1208 * A union-based inode filtering algorithm. Process the inode if any of the
1209 * criteria match. This is for global/internal scans only.
1212 xfs_inode_match_id_union(
1213 struct xfs_inode *ip,
1214 struct xfs_eofblocks *eofb)
1216 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1217 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1220 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1221 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1224 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1225 xfs_get_projid(ip) == eofb->eof_prid)
1232 xfs_inode_free_eofblocks(
1233 struct xfs_inode *ip,
1238 struct xfs_eofblocks *eofb = args;
1239 bool need_iolock = true;
1242 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
1244 if (!xfs_can_free_eofblocks(ip, false)) {
1245 /* inode could be preallocated or append-only */
1246 trace_xfs_inode_free_eofblocks_invalid(ip);
1247 xfs_inode_clear_eofblocks_tag(ip);
1252 * If the mapping is dirty the operation can block and wait for some
1253 * time. Unless we are waiting, skip it.
1255 if (!(flags & SYNC_WAIT) &&
1256 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1260 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1261 match = xfs_inode_match_id_union(ip, eofb);
1263 match = xfs_inode_match_id(ip, eofb);
1267 /* skip the inode if the file size is too small */
1268 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1269 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1273 * A scan owner implies we already hold the iolock. Skip it in
1274 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1275 * the possibility of EAGAIN being returned.
1277 if (eofb->eof_scan_owner == ip->i_ino)
1278 need_iolock = false;
1281 ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
1283 /* don't revisit the inode if we're not waiting */
1284 if (ret == -EAGAIN && !(flags & SYNC_WAIT))
1291 xfs_icache_free_eofblocks(
1292 struct xfs_mount *mp,
1293 struct xfs_eofblocks *eofb)
1295 int flags = SYNC_TRYLOCK;
1297 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1300 return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
1301 eofb, XFS_ICI_EOFBLOCKS_TAG);
1305 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1306 * multiple quotas, we don't know exactly which quota caused an allocation
1307 * failure. We make a best effort by including each quota under low free space
1308 * conditions (less than 1% free space) in the scan.
1311 xfs_inode_free_quota_eofblocks(
1312 struct xfs_inode *ip)
1315 struct xfs_eofblocks eofb = {0};
1316 struct xfs_dquot *dq;
1318 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1321 * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1322 * can repeatedly trylock on the inode we're currently processing. We
1323 * run a sync scan to increase effectiveness and use the union filter to
1324 * cover all applicable quotas in a single scan.
1326 eofb.eof_scan_owner = ip->i_ino;
1327 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1329 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1330 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1331 if (dq && xfs_dquot_lowsp(dq)) {
1332 eofb.eof_uid = VFS_I(ip)->i_uid;
1333 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1338 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1339 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1340 if (dq && xfs_dquot_lowsp(dq)) {
1341 eofb.eof_gid = VFS_I(ip)->i_gid;
1342 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1348 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
1354 xfs_inode_set_eofblocks_tag(
1357 struct xfs_mount *mp = ip->i_mount;
1358 struct xfs_perag *pag;
1361 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1362 spin_lock(&pag->pag_ici_lock);
1363 trace_xfs_inode_set_eofblocks_tag(ip);
1365 tagged = radix_tree_tagged(&pag->pag_ici_root,
1366 XFS_ICI_EOFBLOCKS_TAG);
1367 radix_tree_tag_set(&pag->pag_ici_root,
1368 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1369 XFS_ICI_EOFBLOCKS_TAG);
1371 /* propagate the eofblocks tag up into the perag radix tree */
1372 spin_lock(&ip->i_mount->m_perag_lock);
1373 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1374 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1375 XFS_ICI_EOFBLOCKS_TAG);
1376 spin_unlock(&ip->i_mount->m_perag_lock);
1378 /* kick off background trimming */
1379 xfs_queue_eofblocks(ip->i_mount);
1381 trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
1385 spin_unlock(&pag->pag_ici_lock);
1390 xfs_inode_clear_eofblocks_tag(
1393 struct xfs_mount *mp = ip->i_mount;
1394 struct xfs_perag *pag;
1396 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1397 spin_lock(&pag->pag_ici_lock);
1398 trace_xfs_inode_clear_eofblocks_tag(ip);
1400 radix_tree_tag_clear(&pag->pag_ici_root,
1401 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1402 XFS_ICI_EOFBLOCKS_TAG);
1403 if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
1404 /* clear the eofblocks tag from the perag radix tree */
1405 spin_lock(&ip->i_mount->m_perag_lock);
1406 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1407 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1408 XFS_ICI_EOFBLOCKS_TAG);
1409 spin_unlock(&ip->i_mount->m_perag_lock);
1410 trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
1414 spin_unlock(&pag->pag_ici_lock);