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"
24 #include "xfs_mount.h"
25 #include "xfs_inode.h"
26 #include "xfs_error.h"
27 #include "xfs_trans.h"
28 #include "xfs_trans_priv.h"
29 #include "xfs_inode_item.h"
30 #include "xfs_quota.h"
31 #include "xfs_trace.h"
32 #include "xfs_icache.h"
33 #include "xfs_bmap_util.h"
34 #include "xfs_dquot_item.h"
35 #include "xfs_dquot.h"
37 #include <linux/kthread.h>
38 #include <linux/freezer.h>
40 STATIC void xfs_inode_clear_reclaim_tag(struct xfs_perag *pag, xfs_ino_t ino);
43 * Allocate and initialise an xfs_inode.
53 * if this didn't occur in transactions, we could use
54 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
55 * code up to do this anyway.
57 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
60 if (inode_init_always(mp->m_super, VFS_I(ip))) {
61 kmem_zone_free(xfs_inode_zone, ip);
65 /* VFS doesn't initialise i_mode! */
66 VFS_I(ip)->i_mode = 0;
68 XFS_STATS_INC(mp, vn_active);
69 ASSERT(atomic_read(&ip->i_pincount) == 0);
70 ASSERT(!spin_is_locked(&ip->i_flags_lock));
71 ASSERT(!xfs_isiflocked(ip));
72 ASSERT(ip->i_ino == 0);
74 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
76 /* initialise the xfs inode */
79 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
81 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
83 ip->i_delayed_blks = 0;
84 memset(&ip->i_d, 0, sizeof(ip->i_d));
90 xfs_inode_free_callback(
91 struct rcu_head *head)
93 struct inode *inode = container_of(head, struct inode, i_rcu);
94 struct xfs_inode *ip = XFS_I(inode);
96 switch (VFS_I(ip)->i_mode & S_IFMT) {
100 xfs_idestroy_fork(ip, XFS_DATA_FORK);
105 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
108 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
109 xfs_inode_item_destroy(ip);
113 kmem_zone_free(xfs_inode_zone, ip);
118 struct xfs_inode *ip)
120 /* asserts to verify all state is correct here */
121 ASSERT(atomic_read(&ip->i_pincount) == 0);
122 ASSERT(!xfs_isiflocked(ip));
123 XFS_STATS_DEC(ip->i_mount, vn_active);
125 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
130 struct xfs_inode *ip)
133 * Because we use RCU freeing we need to ensure the inode always
134 * appears to be reclaimed with an invalid inode number when in the
135 * free state. The ip->i_flags_lock provides the barrier against lookup
138 spin_lock(&ip->i_flags_lock);
139 ip->i_flags = XFS_IRECLAIM;
141 spin_unlock(&ip->i_flags_lock);
143 __xfs_inode_free(ip);
147 * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
148 * part of the structure. This is made more complex by the fact we store
149 * information about the on-disk values in the VFS inode and so we can't just
150 * overwrite the values unconditionally. Hence we save the parameters we
151 * need to retain across reinitialisation, and rewrite them into the VFS inode
152 * after reinitialisation even if it fails.
156 struct xfs_mount *mp,
160 uint32_t nlink = inode->i_nlink;
161 uint32_t generation = inode->i_generation;
162 uint64_t version = inode->i_version;
163 umode_t mode = inode->i_mode;
165 error = inode_init_always(mp->m_super, inode);
167 set_nlink(inode, nlink);
168 inode->i_generation = generation;
169 inode->i_version = version;
170 inode->i_mode = mode;
175 * Check the validity of the inode we just found it the cache
179 struct xfs_perag *pag,
180 struct xfs_inode *ip,
183 int lock_flags) __releases(RCU)
185 struct inode *inode = VFS_I(ip);
186 struct xfs_mount *mp = ip->i_mount;
190 * check for re-use of an inode within an RCU grace period due to the
191 * radix tree nodes not being updated yet. We monitor for this by
192 * setting the inode number to zero before freeing the inode structure.
193 * If the inode has been reallocated and set up, then the inode number
194 * will not match, so check for that, too.
196 spin_lock(&ip->i_flags_lock);
197 if (ip->i_ino != ino) {
198 trace_xfs_iget_skip(ip);
199 XFS_STATS_INC(mp, xs_ig_frecycle);
206 * If we are racing with another cache hit that is currently
207 * instantiating this inode or currently recycling it out of
208 * reclaimabe state, wait for the initialisation to complete
211 * XXX(hch): eventually we should do something equivalent to
212 * wait_on_inode to wait for these flags to be cleared
213 * instead of polling for it.
215 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
216 trace_xfs_iget_skip(ip);
217 XFS_STATS_INC(mp, xs_ig_frecycle);
223 * If lookup is racing with unlink return an error immediately.
225 if (VFS_I(ip)->i_mode == 0 && !(flags & XFS_IGET_CREATE)) {
231 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
232 * Need to carefully get it back into useable state.
234 if (ip->i_flags & XFS_IRECLAIMABLE) {
235 trace_xfs_iget_reclaim(ip);
238 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
239 * from stomping over us while we recycle the inode. We can't
240 * clear the radix tree reclaimable tag yet as it requires
241 * pag_ici_lock to be held exclusive.
243 ip->i_flags |= XFS_IRECLAIM;
245 spin_unlock(&ip->i_flags_lock);
248 error = xfs_reinit_inode(mp, inode);
251 * Re-initializing the inode failed, and we are in deep
252 * trouble. Try to re-add it to the reclaim list.
255 spin_lock(&ip->i_flags_lock);
257 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
258 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
259 trace_xfs_iget_reclaim_fail(ip);
263 spin_lock(&pag->pag_ici_lock);
264 spin_lock(&ip->i_flags_lock);
267 * Clear the per-lifetime state in the inode as we are now
268 * effectively a new inode and need to return to the initial
269 * state before reuse occurs.
271 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
272 ip->i_flags |= XFS_INEW;
273 xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
274 inode->i_state = I_NEW;
276 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
277 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
279 spin_unlock(&ip->i_flags_lock);
280 spin_unlock(&pag->pag_ici_lock);
282 /* If the VFS inode is being torn down, pause and try again. */
284 trace_xfs_iget_skip(ip);
289 /* We've got a live one. */
290 spin_unlock(&ip->i_flags_lock);
292 trace_xfs_iget_hit(ip);
296 xfs_ilock(ip, lock_flags);
298 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
299 XFS_STATS_INC(mp, xs_ig_found);
304 spin_unlock(&ip->i_flags_lock);
312 struct xfs_mount *mp,
313 struct xfs_perag *pag,
316 struct xfs_inode **ipp,
320 struct xfs_inode *ip;
322 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
325 ip = xfs_inode_alloc(mp, ino);
329 error = xfs_iread(mp, tp, ip, flags);
333 trace_xfs_iget_miss(ip);
335 if ((VFS_I(ip)->i_mode == 0) && !(flags & XFS_IGET_CREATE)) {
341 * Preload the radix tree so we can insert safely under the
342 * write spinlock. Note that we cannot sleep inside the preload
343 * region. Since we can be called from transaction context, don't
344 * recurse into the file system.
346 if (radix_tree_preload(GFP_NOFS)) {
352 * Because the inode hasn't been added to the radix-tree yet it can't
353 * be found by another thread, so we can do the non-sleeping lock here.
356 if (!xfs_ilock_nowait(ip, lock_flags))
361 * These values must be set before inserting the inode into the radix
362 * tree as the moment it is inserted a concurrent lookup (allowed by the
363 * RCU locking mechanism) can find it and that lookup must see that this
364 * is an inode currently under construction (i.e. that XFS_INEW is set).
365 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
366 * memory barrier that ensures this detection works correctly at lookup
370 if (flags & XFS_IGET_DONTCACHE)
371 iflags |= XFS_IDONTCACHE;
375 xfs_iflags_set(ip, iflags);
377 /* insert the new inode */
378 spin_lock(&pag->pag_ici_lock);
379 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
380 if (unlikely(error)) {
381 WARN_ON(error != -EEXIST);
382 XFS_STATS_INC(mp, xs_ig_dup);
384 goto out_preload_end;
386 spin_unlock(&pag->pag_ici_lock);
387 radix_tree_preload_end();
393 spin_unlock(&pag->pag_ici_lock);
394 radix_tree_preload_end();
396 xfs_iunlock(ip, lock_flags);
398 __destroy_inode(VFS_I(ip));
404 * Look up an inode by number in the given file system.
405 * The inode is looked up in the cache held in each AG.
406 * If the inode is found in the cache, initialise the vfs inode
409 * If it is not in core, read it in from the file system's device,
410 * add it to the cache and initialise the vfs inode.
412 * The inode is locked according to the value of the lock_flags parameter.
413 * This flag parameter indicates how and if the inode's IO lock and inode lock
416 * mp -- the mount point structure for the current file system. It points
417 * to the inode hash table.
418 * tp -- a pointer to the current transaction if there is one. This is
419 * simply passed through to the xfs_iread() call.
420 * ino -- the number of the inode desired. This is the unique identifier
421 * within the file system for the inode being requested.
422 * lock_flags -- flags indicating how to lock the inode. See the comment
423 * for xfs_ilock() for a list of valid values.
440 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
441 * doesn't get freed while it's being referenced during a
442 * radix tree traversal here. It assumes this function
443 * aqcuires only the ILOCK (and therefore it has no need to
444 * involve the IOLOCK in this synchronization).
446 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
448 /* reject inode numbers outside existing AGs */
449 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
452 XFS_STATS_INC(mp, xs_ig_attempts);
454 /* get the perag structure and ensure that it's inode capable */
455 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
456 agino = XFS_INO_TO_AGINO(mp, ino);
461 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
464 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
466 goto out_error_or_again;
469 XFS_STATS_INC(mp, xs_ig_missed);
471 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
474 goto out_error_or_again;
481 * If we have a real type for an on-disk inode, we can setup the inode
482 * now. If it's a new inode being created, xfs_ialloc will handle it.
484 if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
485 xfs_setup_existing_inode(ip);
489 if (error == -EAGAIN) {
498 * The inode lookup is done in batches to keep the amount of lock traffic and
499 * radix tree lookups to a minimum. The batch size is a trade off between
500 * lookup reduction and stack usage. This is in the reclaim path, so we can't
503 #define XFS_LOOKUP_BATCH 32
506 xfs_inode_ag_walk_grab(
507 struct xfs_inode *ip)
509 struct inode *inode = VFS_I(ip);
511 ASSERT(rcu_read_lock_held());
514 * check for stale RCU freed inode
516 * If the inode has been reallocated, it doesn't matter if it's not in
517 * the AG we are walking - we are walking for writeback, so if it
518 * passes all the "valid inode" checks and is dirty, then we'll write
519 * it back anyway. If it has been reallocated and still being
520 * initialised, the XFS_INEW check below will catch it.
522 spin_lock(&ip->i_flags_lock);
524 goto out_unlock_noent;
526 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
527 if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
528 goto out_unlock_noent;
529 spin_unlock(&ip->i_flags_lock);
531 /* nothing to sync during shutdown */
532 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
533 return -EFSCORRUPTED;
535 /* If we can't grab the inode, it must on it's way to reclaim. */
543 spin_unlock(&ip->i_flags_lock);
549 struct xfs_mount *mp,
550 struct xfs_perag *pag,
551 int (*execute)(struct xfs_inode *ip, int flags,
557 uint32_t first_index;
569 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
576 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
577 (void **)batch, first_index,
580 nr_found = radix_tree_gang_lookup_tag(
582 (void **) batch, first_index,
583 XFS_LOOKUP_BATCH, tag);
591 * Grab the inodes before we drop the lock. if we found
592 * nothing, nr == 0 and the loop will be skipped.
594 for (i = 0; i < nr_found; i++) {
595 struct xfs_inode *ip = batch[i];
597 if (done || xfs_inode_ag_walk_grab(ip))
601 * Update the index for the next lookup. Catch
602 * overflows into the next AG range which can occur if
603 * we have inodes in the last block of the AG and we
604 * are currently pointing to the last inode.
606 * Because we may see inodes that are from the wrong AG
607 * due to RCU freeing and reallocation, only update the
608 * index if it lies in this AG. It was a race that lead
609 * us to see this inode, so another lookup from the
610 * same index will not find it again.
612 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
614 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
615 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
619 /* unlock now we've grabbed the inodes. */
622 for (i = 0; i < nr_found; i++) {
625 error = execute(batch[i], flags, args);
627 if (error == -EAGAIN) {
631 if (error && last_error != -EFSCORRUPTED)
635 /* bail out if the filesystem is corrupted. */
636 if (error == -EFSCORRUPTED)
641 } while (nr_found && !done);
651 * Background scanning to trim post-EOF preallocated space. This is queued
652 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
656 struct xfs_mount *mp)
659 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
660 queue_delayed_work(mp->m_eofblocks_workqueue,
661 &mp->m_eofblocks_work,
662 msecs_to_jiffies(xfs_eofb_secs * 1000));
667 xfs_eofblocks_worker(
668 struct work_struct *work)
670 struct xfs_mount *mp = container_of(to_delayed_work(work),
671 struct xfs_mount, m_eofblocks_work);
672 xfs_icache_free_eofblocks(mp, NULL);
673 xfs_queue_eofblocks(mp);
677 xfs_inode_ag_iterator(
678 struct xfs_mount *mp,
679 int (*execute)(struct xfs_inode *ip, int flags,
684 struct xfs_perag *pag;
690 while ((pag = xfs_perag_get(mp, ag))) {
691 ag = pag->pag_agno + 1;
692 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1);
696 if (error == -EFSCORRUPTED)
704 xfs_inode_ag_iterator_tag(
705 struct xfs_mount *mp,
706 int (*execute)(struct xfs_inode *ip, int flags,
712 struct xfs_perag *pag;
718 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
719 ag = pag->pag_agno + 1;
720 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag);
724 if (error == -EFSCORRUPTED)
732 * Queue a new inode reclaim pass if there are reclaimable inodes and there
733 * isn't a reclaim pass already in progress. By default it runs every 5s based
734 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
735 * tunable, but that can be done if this method proves to be ineffective or too
739 xfs_reclaim_work_queue(
740 struct xfs_mount *mp)
744 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
745 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
746 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
752 * This is a fast pass over the inode cache to try to get reclaim moving on as
753 * many inodes as possible in a short period of time. It kicks itself every few
754 * seconds, as well as being kicked by the inode cache shrinker when memory
755 * goes low. It scans as quickly as possible avoiding locked inodes or those
756 * already being flushed, and once done schedules a future pass.
760 struct work_struct *work)
762 struct xfs_mount *mp = container_of(to_delayed_work(work),
763 struct xfs_mount, m_reclaim_work);
765 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
766 xfs_reclaim_work_queue(mp);
770 xfs_perag_set_reclaim_tag(
771 struct xfs_perag *pag)
773 struct xfs_mount *mp = pag->pag_mount;
775 ASSERT(spin_is_locked(&pag->pag_ici_lock));
776 if (pag->pag_ici_reclaimable++)
779 /* propagate the reclaim tag up into the perag radix tree */
780 spin_lock(&mp->m_perag_lock);
781 radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno,
782 XFS_ICI_RECLAIM_TAG);
783 spin_unlock(&mp->m_perag_lock);
785 /* schedule periodic background inode reclaim */
786 xfs_reclaim_work_queue(mp);
788 trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
792 xfs_perag_clear_reclaim_tag(
793 struct xfs_perag *pag)
795 struct xfs_mount *mp = pag->pag_mount;
797 ASSERT(spin_is_locked(&pag->pag_ici_lock));
798 if (--pag->pag_ici_reclaimable)
801 /* clear the reclaim tag from the perag radix tree */
802 spin_lock(&mp->m_perag_lock);
803 radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno,
804 XFS_ICI_RECLAIM_TAG);
805 spin_unlock(&mp->m_perag_lock);
806 trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
811 * We set the inode flag atomically with the radix tree tag.
812 * Once we get tag lookups on the radix tree, this inode flag
816 xfs_inode_set_reclaim_tag(
817 struct xfs_inode *ip)
819 struct xfs_mount *mp = ip->i_mount;
820 struct xfs_perag *pag;
822 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
823 spin_lock(&pag->pag_ici_lock);
824 spin_lock(&ip->i_flags_lock);
826 radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
827 XFS_ICI_RECLAIM_TAG);
828 xfs_perag_set_reclaim_tag(pag);
829 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
831 spin_unlock(&ip->i_flags_lock);
832 spin_unlock(&pag->pag_ici_lock);
837 xfs_inode_clear_reclaim_tag(
838 struct xfs_perag *pag,
841 radix_tree_tag_clear(&pag->pag_ici_root,
842 XFS_INO_TO_AGINO(pag->pag_mount, ino),
843 XFS_ICI_RECLAIM_TAG);
844 xfs_perag_clear_reclaim_tag(pag);
848 * Grab the inode for reclaim exclusively.
849 * Return 0 if we grabbed it, non-zero otherwise.
852 xfs_reclaim_inode_grab(
853 struct xfs_inode *ip,
856 ASSERT(rcu_read_lock_held());
858 /* quick check for stale RCU freed inode */
863 * If we are asked for non-blocking operation, do unlocked checks to
864 * see if the inode already is being flushed or in reclaim to avoid
867 if ((flags & SYNC_TRYLOCK) &&
868 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
872 * The radix tree lock here protects a thread in xfs_iget from racing
873 * with us starting reclaim on the inode. Once we have the
874 * XFS_IRECLAIM flag set it will not touch us.
876 * Due to RCU lookup, we may find inodes that have been freed and only
877 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
878 * aren't candidates for reclaim at all, so we must check the
879 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
881 spin_lock(&ip->i_flags_lock);
882 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
883 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
884 /* not a reclaim candidate. */
885 spin_unlock(&ip->i_flags_lock);
888 __xfs_iflags_set(ip, XFS_IRECLAIM);
889 spin_unlock(&ip->i_flags_lock);
894 * Inodes in different states need to be treated differently. The following
895 * table lists the inode states and the reclaim actions necessary:
897 * inode state iflush ret required action
898 * --------------- ---------- ---------------
900 * shutdown EIO unpin and reclaim
901 * clean, unpinned 0 reclaim
902 * stale, unpinned 0 reclaim
903 * clean, pinned(*) 0 requeue
904 * stale, pinned EAGAIN requeue
905 * dirty, async - requeue
906 * dirty, sync 0 reclaim
908 * (*) dgc: I don't think the clean, pinned state is possible but it gets
909 * handled anyway given the order of checks implemented.
911 * Also, because we get the flush lock first, we know that any inode that has
912 * been flushed delwri has had the flush completed by the time we check that
913 * the inode is clean.
915 * Note that because the inode is flushed delayed write by AIL pushing, the
916 * flush lock may already be held here and waiting on it can result in very
917 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
918 * the caller should push the AIL first before trying to reclaim inodes to
919 * minimise the amount of time spent waiting. For background relaim, we only
920 * bother to reclaim clean inodes anyway.
922 * Hence the order of actions after gaining the locks should be:
924 * shutdown => unpin and reclaim
925 * pinned, async => requeue
926 * pinned, sync => unpin
929 * dirty, async => requeue
930 * dirty, sync => flush, wait and reclaim
934 struct xfs_inode *ip,
935 struct xfs_perag *pag,
938 struct xfs_buf *bp = NULL;
939 xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
944 xfs_ilock(ip, XFS_ILOCK_EXCL);
945 if (!xfs_iflock_nowait(ip)) {
946 if (!(sync_mode & SYNC_WAIT))
951 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
953 xfs_iflush_abort(ip, false);
956 if (xfs_ipincount(ip)) {
957 if (!(sync_mode & SYNC_WAIT))
961 if (xfs_iflags_test(ip, XFS_ISTALE))
963 if (xfs_inode_clean(ip))
967 * Never flush out dirty data during non-blocking reclaim, as it would
968 * just contend with AIL pushing trying to do the same job.
970 if (!(sync_mode & SYNC_WAIT))
974 * Now we have an inode that needs flushing.
976 * Note that xfs_iflush will never block on the inode buffer lock, as
977 * xfs_ifree_cluster() can lock the inode buffer before it locks the
978 * ip->i_lock, and we are doing the exact opposite here. As a result,
979 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
980 * result in an ABBA deadlock with xfs_ifree_cluster().
982 * As xfs_ifree_cluser() must gather all inodes that are active in the
983 * cache to mark them stale, if we hit this case we don't actually want
984 * to do IO here - we want the inode marked stale so we can simply
985 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
986 * inode, back off and try again. Hopefully the next pass through will
987 * see the stale flag set on the inode.
989 error = xfs_iflush(ip, &bp);
990 if (error == -EAGAIN) {
991 xfs_iunlock(ip, XFS_ILOCK_EXCL);
992 /* backoff longer than in xfs_ifree_cluster */
998 error = xfs_bwrite(bp);
1005 * Because we use RCU freeing we need to ensure the inode always appears
1006 * to be reclaimed with an invalid inode number when in the free state.
1007 * We do this as early as possible under the ILOCK and flush lock so
1008 * that xfs_iflush_cluster() can be guaranteed to detect races with us
1009 * here. By doing this, we guarantee that once xfs_iflush_cluster has
1010 * locked both the XFS_ILOCK and the flush lock that it will see either
1011 * a valid, flushable inode that will serialise correctly against the
1012 * locks below, or it will see a clean (and invalid) inode that it can
1015 spin_lock(&ip->i_flags_lock);
1016 ip->i_flags = XFS_IRECLAIM;
1018 spin_unlock(&ip->i_flags_lock);
1021 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1023 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
1025 * Remove the inode from the per-AG radix tree.
1027 * Because radix_tree_delete won't complain even if the item was never
1028 * added to the tree assert that it's been there before to catch
1029 * problems with the inode life time early on.
1031 spin_lock(&pag->pag_ici_lock);
1032 if (!radix_tree_delete(&pag->pag_ici_root,
1033 XFS_INO_TO_AGINO(ip->i_mount, ino)))
1035 xfs_perag_clear_reclaim_tag(pag);
1036 spin_unlock(&pag->pag_ici_lock);
1039 * Here we do an (almost) spurious inode lock in order to coordinate
1040 * with inode cache radix tree lookups. This is because the lookup
1041 * can reference the inodes in the cache without taking references.
1043 * We make that OK here by ensuring that we wait until the inode is
1044 * unlocked after the lookup before we go ahead and free it.
1046 xfs_ilock(ip, XFS_ILOCK_EXCL);
1047 xfs_qm_dqdetach(ip);
1048 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1050 __xfs_inode_free(ip);
1056 xfs_iflags_clear(ip, XFS_IRECLAIM);
1057 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1059 * We could return -EAGAIN here to make reclaim rescan the inode tree in
1060 * a short while. However, this just burns CPU time scanning the tree
1061 * waiting for IO to complete and the reclaim work never goes back to
1062 * the idle state. Instead, return 0 to let the next scheduled
1063 * background reclaim attempt to reclaim the inode again.
1069 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1070 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1071 * then a shut down during filesystem unmount reclaim walk leak all the
1072 * unreclaimed inodes.
1075 xfs_reclaim_inodes_ag(
1076 struct xfs_mount *mp,
1080 struct xfs_perag *pag;
1084 int trylock = flags & SYNC_TRYLOCK;
1090 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1091 unsigned long first_index = 0;
1095 ag = pag->pag_agno + 1;
1098 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1103 first_index = pag->pag_ici_reclaim_cursor;
1105 mutex_lock(&pag->pag_ici_reclaim_lock);
1108 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1112 nr_found = radix_tree_gang_lookup_tag(
1114 (void **)batch, first_index,
1116 XFS_ICI_RECLAIM_TAG);
1124 * Grab the inodes before we drop the lock. if we found
1125 * nothing, nr == 0 and the loop will be skipped.
1127 for (i = 0; i < nr_found; i++) {
1128 struct xfs_inode *ip = batch[i];
1130 if (done || xfs_reclaim_inode_grab(ip, flags))
1134 * Update the index for the next lookup. Catch
1135 * overflows into the next AG range which can
1136 * occur if we have inodes in the last block of
1137 * the AG and we are currently pointing to the
1140 * Because we may see inodes that are from the
1141 * wrong AG due to RCU freeing and
1142 * reallocation, only update the index if it
1143 * lies in this AG. It was a race that lead us
1144 * to see this inode, so another lookup from
1145 * the same index will not find it again.
1147 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1150 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1151 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1155 /* unlock now we've grabbed the inodes. */
1158 for (i = 0; i < nr_found; i++) {
1161 error = xfs_reclaim_inode(batch[i], pag, flags);
1162 if (error && last_error != -EFSCORRUPTED)
1166 *nr_to_scan -= XFS_LOOKUP_BATCH;
1170 } while (nr_found && !done && *nr_to_scan > 0);
1172 if (trylock && !done)
1173 pag->pag_ici_reclaim_cursor = first_index;
1175 pag->pag_ici_reclaim_cursor = 0;
1176 mutex_unlock(&pag->pag_ici_reclaim_lock);
1181 * if we skipped any AG, and we still have scan count remaining, do
1182 * another pass this time using blocking reclaim semantics (i.e
1183 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1184 * ensure that when we get more reclaimers than AGs we block rather
1185 * than spin trying to execute reclaim.
1187 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
1199 int nr_to_scan = INT_MAX;
1201 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
1205 * Scan a certain number of inodes for reclaim.
1207 * When called we make sure that there is a background (fast) inode reclaim in
1208 * progress, while we will throttle the speed of reclaim via doing synchronous
1209 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1210 * them to be cleaned, which we hope will not be very long due to the
1211 * background walker having already kicked the IO off on those dirty inodes.
1214 xfs_reclaim_inodes_nr(
1215 struct xfs_mount *mp,
1218 /* kick background reclaimer and push the AIL */
1219 xfs_reclaim_work_queue(mp);
1220 xfs_ail_push_all(mp->m_ail);
1222 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
1226 * Return the number of reclaimable inodes in the filesystem for
1227 * the shrinker to determine how much to reclaim.
1230 xfs_reclaim_inodes_count(
1231 struct xfs_mount *mp)
1233 struct xfs_perag *pag;
1234 xfs_agnumber_t ag = 0;
1235 int reclaimable = 0;
1237 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1238 ag = pag->pag_agno + 1;
1239 reclaimable += pag->pag_ici_reclaimable;
1247 struct xfs_inode *ip,
1248 struct xfs_eofblocks *eofb)
1250 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1251 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1254 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1255 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1258 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1259 xfs_get_projid(ip) != eofb->eof_prid)
1266 * A union-based inode filtering algorithm. Process the inode if any of the
1267 * criteria match. This is for global/internal scans only.
1270 xfs_inode_match_id_union(
1271 struct xfs_inode *ip,
1272 struct xfs_eofblocks *eofb)
1274 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1275 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1278 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1279 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1282 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1283 xfs_get_projid(ip) == eofb->eof_prid)
1290 xfs_inode_free_eofblocks(
1291 struct xfs_inode *ip,
1296 struct xfs_eofblocks *eofb = args;
1297 bool need_iolock = true;
1300 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
1302 if (!xfs_can_free_eofblocks(ip, false)) {
1303 /* inode could be preallocated or append-only */
1304 trace_xfs_inode_free_eofblocks_invalid(ip);
1305 xfs_inode_clear_eofblocks_tag(ip);
1310 * If the mapping is dirty the operation can block and wait for some
1311 * time. Unless we are waiting, skip it.
1313 if (!(flags & SYNC_WAIT) &&
1314 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1318 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1319 match = xfs_inode_match_id_union(ip, eofb);
1321 match = xfs_inode_match_id(ip, eofb);
1325 /* skip the inode if the file size is too small */
1326 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1327 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1331 * A scan owner implies we already hold the iolock. Skip it in
1332 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1333 * the possibility of EAGAIN being returned.
1335 if (eofb->eof_scan_owner == ip->i_ino)
1336 need_iolock = false;
1339 ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
1341 /* don't revisit the inode if we're not waiting */
1342 if (ret == -EAGAIN && !(flags & SYNC_WAIT))
1349 xfs_icache_free_eofblocks(
1350 struct xfs_mount *mp,
1351 struct xfs_eofblocks *eofb)
1353 int flags = SYNC_TRYLOCK;
1355 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1358 return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
1359 eofb, XFS_ICI_EOFBLOCKS_TAG);
1363 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1364 * multiple quotas, we don't know exactly which quota caused an allocation
1365 * failure. We make a best effort by including each quota under low free space
1366 * conditions (less than 1% free space) in the scan.
1369 xfs_inode_free_quota_eofblocks(
1370 struct xfs_inode *ip)
1373 struct xfs_eofblocks eofb = {0};
1374 struct xfs_dquot *dq;
1376 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1379 * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1380 * can repeatedly trylock on the inode we're currently processing. We
1381 * run a sync scan to increase effectiveness and use the union filter to
1382 * cover all applicable quotas in a single scan.
1384 eofb.eof_scan_owner = ip->i_ino;
1385 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1387 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1388 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1389 if (dq && xfs_dquot_lowsp(dq)) {
1390 eofb.eof_uid = VFS_I(ip)->i_uid;
1391 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1396 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1397 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1398 if (dq && xfs_dquot_lowsp(dq)) {
1399 eofb.eof_gid = VFS_I(ip)->i_gid;
1400 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1406 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
1412 xfs_inode_set_eofblocks_tag(
1415 struct xfs_mount *mp = ip->i_mount;
1416 struct xfs_perag *pag;
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_set_eofblocks_tag(ip);
1423 tagged = radix_tree_tagged(&pag->pag_ici_root,
1424 XFS_ICI_EOFBLOCKS_TAG);
1425 radix_tree_tag_set(&pag->pag_ici_root,
1426 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1427 XFS_ICI_EOFBLOCKS_TAG);
1429 /* propagate the eofblocks tag up into the perag radix tree */
1430 spin_lock(&ip->i_mount->m_perag_lock);
1431 radix_tree_tag_set(&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);
1436 /* kick off background trimming */
1437 xfs_queue_eofblocks(ip->i_mount);
1439 trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
1443 spin_unlock(&pag->pag_ici_lock);
1448 xfs_inode_clear_eofblocks_tag(
1451 struct xfs_mount *mp = ip->i_mount;
1452 struct xfs_perag *pag;
1454 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1455 spin_lock(&pag->pag_ici_lock);
1456 trace_xfs_inode_clear_eofblocks_tag(ip);
1458 radix_tree_tag_clear(&pag->pag_ici_root,
1459 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1460 XFS_ICI_EOFBLOCKS_TAG);
1461 if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
1462 /* clear the eofblocks tag from the perag radix tree */
1463 spin_lock(&ip->i_mount->m_perag_lock);
1464 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1465 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1466 XFS_ICI_EOFBLOCKS_TAG);
1467 spin_unlock(&ip->i_mount->m_perag_lock);
1468 trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
1472 spin_unlock(&pag->pag_ici_lock);