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_mount *mp,
41 struct xfs_perag *pag, struct xfs_inode *ip);
44 * Allocate and initialise an xfs_inode.
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.
58 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
61 if (inode_init_always(mp->m_super, VFS_I(ip))) {
62 kmem_zone_free(xfs_inode_zone, ip);
66 XFS_STATS_INC(mp, vn_active);
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));
132 XFS_STATS_DEC(ip->i_mount, vn_active);
134 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
138 * If we are allocating a new inode, then check what was returned is
139 * actually a free, empty inode. If we are not allocating an inode,
140 * then check we didn't find a free inode.
143 * 0 if the inode free state matches the lookup context
144 * -ENOENT if the inode is free and we are not allocating
145 * -EFSCORRUPTED if there is any state mismatch at all
148 xfs_iget_check_free_state(
149 struct xfs_inode *ip,
152 if (flags & XFS_IGET_CREATE) {
153 /* should be a free inode */
154 if (ip->i_d.di_mode != 0) {
155 xfs_warn(ip->i_mount,
156 "Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)",
157 ip->i_ino, ip->i_d.di_mode);
158 return -EFSCORRUPTED;
161 if (ip->i_d.di_nblocks != 0) {
162 xfs_warn(ip->i_mount,
163 "Corruption detected! Free inode 0x%llx has blocks allocated!",
165 return -EFSCORRUPTED;
170 /* should be an allocated inode */
171 if (ip->i_d.di_mode == 0)
178 * Check the validity of the inode we just found it the cache
182 struct xfs_perag *pag,
183 struct xfs_inode *ip,
186 int lock_flags) __releases(RCU)
188 struct inode *inode = VFS_I(ip);
189 struct xfs_mount *mp = ip->i_mount;
193 * check for re-use of an inode within an RCU grace period due to the
194 * radix tree nodes not being updated yet. We monitor for this by
195 * setting the inode number to zero before freeing the inode structure.
196 * If the inode has been reallocated and set up, then the inode number
197 * will not match, so check for that, too.
199 spin_lock(&ip->i_flags_lock);
200 if (ip->i_ino != ino) {
201 trace_xfs_iget_skip(ip);
202 XFS_STATS_INC(mp, xs_ig_frecycle);
209 * If we are racing with another cache hit that is currently
210 * instantiating this inode or currently recycling it out of
211 * reclaimabe state, wait for the initialisation to complete
214 * XXX(hch): eventually we should do something equivalent to
215 * wait_on_inode to wait for these flags to be cleared
216 * instead of polling for it.
218 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
219 trace_xfs_iget_skip(ip);
220 XFS_STATS_INC(mp, xs_ig_frecycle);
226 * Check the inode free state is valid. This also detects lookup
227 * racing with unlinks.
229 error = xfs_iget_check_free_state(ip, flags);
234 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
235 * Need to carefully get it back into useable state.
237 if (ip->i_flags & XFS_IRECLAIMABLE) {
238 trace_xfs_iget_reclaim(ip);
241 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
242 * from stomping over us while we recycle the inode. We can't
243 * clear the radix tree reclaimable tag yet as it requires
244 * pag_ici_lock to be held exclusive.
246 ip->i_flags |= XFS_IRECLAIM;
248 spin_unlock(&ip->i_flags_lock);
251 error = inode_init_always(mp->m_super, inode);
255 * Re-initializing the inode failed, and we are in deep
256 * trouble. Try to re-add it to the reclaim list.
259 spin_lock(&ip->i_flags_lock);
260 wake = !!__xfs_iflags_test(ip, XFS_INEW);
261 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
263 wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
264 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
265 trace_xfs_iget_reclaim_fail(ip);
269 spin_lock(&pag->pag_ici_lock);
270 spin_lock(&ip->i_flags_lock);
273 * Clear the per-lifetime state in the inode as we are now
274 * effectively a new inode and need to return to the initial
275 * state before reuse occurs.
277 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
278 ip->i_flags |= XFS_INEW;
279 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
280 inode->i_state = I_NEW;
282 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
283 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
285 spin_unlock(&ip->i_flags_lock);
286 spin_unlock(&pag->pag_ici_lock);
288 /* If the VFS inode is being torn down, pause and try again. */
290 trace_xfs_iget_skip(ip);
295 /* We've got a live one. */
296 spin_unlock(&ip->i_flags_lock);
298 trace_xfs_iget_hit(ip);
302 xfs_ilock(ip, lock_flags);
304 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
305 XFS_STATS_INC(mp, xs_ig_found);
310 spin_unlock(&ip->i_flags_lock);
318 struct xfs_mount *mp,
319 struct xfs_perag *pag,
322 struct xfs_inode **ipp,
326 struct xfs_inode *ip;
328 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
331 ip = xfs_inode_alloc(mp, ino);
335 error = xfs_iread(mp, tp, ip, flags);
339 trace_xfs_iget_miss(ip);
343 * Check the inode free state is valid. This also detects lookup
344 * racing with unlinks.
346 error = xfs_iget_check_free_state(ip, flags);
351 * Preload the radix tree so we can insert safely under the
352 * write spinlock. Note that we cannot sleep inside the preload
353 * region. Since we can be called from transaction context, don't
354 * recurse into the file system.
356 if (radix_tree_preload(GFP_NOFS)) {
362 * Because the inode hasn't been added to the radix-tree yet it can't
363 * be found by another thread, so we can do the non-sleeping lock here.
366 if (!xfs_ilock_nowait(ip, lock_flags))
371 * These values must be set before inserting the inode into the radix
372 * tree as the moment it is inserted a concurrent lookup (allowed by the
373 * RCU locking mechanism) can find it and that lookup must see that this
374 * is an inode currently under construction (i.e. that XFS_INEW is set).
375 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
376 * memory barrier that ensures this detection works correctly at lookup
380 if (flags & XFS_IGET_DONTCACHE)
381 iflags |= XFS_IDONTCACHE;
385 xfs_iflags_set(ip, iflags);
387 /* insert the new inode */
388 spin_lock(&pag->pag_ici_lock);
389 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
390 if (unlikely(error)) {
391 WARN_ON(error != -EEXIST);
392 XFS_STATS_INC(mp, xs_ig_dup);
394 goto out_preload_end;
396 spin_unlock(&pag->pag_ici_lock);
397 radix_tree_preload_end();
403 spin_unlock(&pag->pag_ici_lock);
404 radix_tree_preload_end();
406 xfs_iunlock(ip, lock_flags);
408 __destroy_inode(VFS_I(ip));
415 struct xfs_inode *ip)
417 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT);
418 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT);
421 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
422 if (!xfs_iflags_test(ip, XFS_INEW))
426 finish_wait(wq, &wait.wait);
430 * Look up an inode by number in the given file system.
431 * The inode is looked up in the cache held in each AG.
432 * If the inode is found in the cache, initialise the vfs inode
435 * If it is not in core, read it in from the file system's device,
436 * add it to the cache and initialise the vfs inode.
438 * The inode is locked according to the value of the lock_flags parameter.
439 * This flag parameter indicates how and if the inode's IO lock and inode lock
442 * mp -- the mount point structure for the current file system. It points
443 * to the inode hash table.
444 * tp -- a pointer to the current transaction if there is one. This is
445 * simply passed through to the xfs_iread() call.
446 * ino -- the number of the inode desired. This is the unique identifier
447 * within the file system for the inode being requested.
448 * lock_flags -- flags indicating how to lock the inode. See the comment
449 * for xfs_ilock() for a list of valid values.
466 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
467 * doesn't get freed while it's being referenced during a
468 * radix tree traversal here. It assumes this function
469 * aqcuires only the ILOCK (and therefore it has no need to
470 * involve the IOLOCK in this synchronization).
472 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
474 /* reject inode numbers outside existing AGs */
475 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
478 XFS_STATS_INC(mp, xs_ig_attempts);
480 /* get the perag structure and ensure that it's inode capable */
481 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
482 agino = XFS_INO_TO_AGINO(mp, ino);
487 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
490 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
492 goto out_error_or_again;
495 XFS_STATS_INC(mp, xs_ig_missed);
497 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
500 goto out_error_or_again;
507 * If we have a real type for an on-disk inode, we can setup the inode
508 * now. If it's a new inode being created, xfs_ialloc will handle it.
510 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
511 xfs_setup_existing_inode(ip);
515 if (error == -EAGAIN) {
524 * The inode lookup is done in batches to keep the amount of lock traffic and
525 * radix tree lookups to a minimum. The batch size is a trade off between
526 * lookup reduction and stack usage. This is in the reclaim path, so we can't
529 #define XFS_LOOKUP_BATCH 32
532 xfs_inode_ag_walk_grab(
533 struct xfs_inode *ip,
536 struct inode *inode = VFS_I(ip);
537 bool newinos = !!(flags & XFS_AGITER_INEW_WAIT);
539 ASSERT(rcu_read_lock_held());
542 * check for stale RCU freed inode
544 * If the inode has been reallocated, it doesn't matter if it's not in
545 * the AG we are walking - we are walking for writeback, so if it
546 * passes all the "valid inode" checks and is dirty, then we'll write
547 * it back anyway. If it has been reallocated and still being
548 * initialised, the XFS_INEW check below will catch it.
550 spin_lock(&ip->i_flags_lock);
552 goto out_unlock_noent;
554 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
555 if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) ||
556 __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM))
557 goto out_unlock_noent;
558 spin_unlock(&ip->i_flags_lock);
560 /* nothing to sync during shutdown */
561 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
562 return -EFSCORRUPTED;
564 /* If we can't grab the inode, it must on it's way to reclaim. */
572 spin_unlock(&ip->i_flags_lock);
578 struct xfs_mount *mp,
579 struct xfs_perag *pag,
580 int (*execute)(struct xfs_inode *ip, int flags,
587 uint32_t first_index;
599 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
606 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
607 (void **)batch, first_index,
610 nr_found = radix_tree_gang_lookup_tag(
612 (void **) batch, first_index,
613 XFS_LOOKUP_BATCH, tag);
621 * Grab the inodes before we drop the lock. if we found
622 * nothing, nr == 0 and the loop will be skipped.
624 for (i = 0; i < nr_found; i++) {
625 struct xfs_inode *ip = batch[i];
627 if (done || xfs_inode_ag_walk_grab(ip, iter_flags))
631 * Update the index for the next lookup. Catch
632 * overflows into the next AG range which can occur if
633 * we have inodes in the last block of the AG and we
634 * are currently pointing to the last inode.
636 * Because we may see inodes that are from the wrong AG
637 * due to RCU freeing and reallocation, only update the
638 * index if it lies in this AG. It was a race that lead
639 * us to see this inode, so another lookup from the
640 * same index will not find it again.
642 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
644 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
645 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
649 /* unlock now we've grabbed the inodes. */
652 for (i = 0; i < nr_found; i++) {
655 if ((iter_flags & XFS_AGITER_INEW_WAIT) &&
656 xfs_iflags_test(batch[i], XFS_INEW))
657 xfs_inew_wait(batch[i]);
658 error = execute(batch[i], flags, args);
660 if (error == -EAGAIN) {
664 if (error && last_error != -EFSCORRUPTED)
668 /* bail out if the filesystem is corrupted. */
669 if (error == -EFSCORRUPTED)
674 } while (nr_found && !done);
684 * Background scanning to trim post-EOF preallocated space. This is queued
685 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
689 struct xfs_mount *mp)
692 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
693 queue_delayed_work(mp->m_eofblocks_workqueue,
694 &mp->m_eofblocks_work,
695 msecs_to_jiffies(xfs_eofb_secs * 1000));
700 xfs_eofblocks_worker(
701 struct work_struct *work)
703 struct xfs_mount *mp = container_of(to_delayed_work(work),
704 struct xfs_mount, m_eofblocks_work);
705 xfs_icache_free_eofblocks(mp, NULL);
706 xfs_queue_eofblocks(mp);
710 xfs_inode_ag_iterator_flags(
711 struct xfs_mount *mp,
712 int (*execute)(struct xfs_inode *ip, int flags,
718 struct xfs_perag *pag;
724 while ((pag = xfs_perag_get(mp, ag))) {
725 ag = pag->pag_agno + 1;
726 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, -1,
731 if (error == -EFSCORRUPTED)
739 xfs_inode_ag_iterator(
740 struct xfs_mount *mp,
741 int (*execute)(struct xfs_inode *ip, int flags,
746 return xfs_inode_ag_iterator_flags(mp, execute, flags, args, 0);
750 xfs_inode_ag_iterator_tag(
751 struct xfs_mount *mp,
752 int (*execute)(struct xfs_inode *ip, int flags,
758 struct xfs_perag *pag;
764 while ((pag = xfs_perag_get_tag(mp, ag, tag))) {
765 ag = pag->pag_agno + 1;
766 error = xfs_inode_ag_walk(mp, pag, execute, flags, args, tag,
771 if (error == -EFSCORRUPTED)
779 * Queue a new inode reclaim pass if there are reclaimable inodes and there
780 * isn't a reclaim pass already in progress. By default it runs every 5s based
781 * on the xfs periodic sync default of 30s. Perhaps this should have it's own
782 * tunable, but that can be done if this method proves to be ineffective or too
786 xfs_reclaim_work_queue(
787 struct xfs_mount *mp)
791 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
792 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
793 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
799 * This is a fast pass over the inode cache to try to get reclaim moving on as
800 * many inodes as possible in a short period of time. It kicks itself every few
801 * seconds, as well as being kicked by the inode cache shrinker when memory
802 * goes low. It scans as quickly as possible avoiding locked inodes or those
803 * already being flushed, and once done schedules a future pass.
807 struct work_struct *work)
809 struct xfs_mount *mp = container_of(to_delayed_work(work),
810 struct xfs_mount, m_reclaim_work);
812 xfs_reclaim_inodes(mp, SYNC_TRYLOCK);
813 xfs_reclaim_work_queue(mp);
817 __xfs_inode_set_reclaim_tag(
818 struct xfs_perag *pag,
819 struct xfs_inode *ip)
821 radix_tree_tag_set(&pag->pag_ici_root,
822 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
823 XFS_ICI_RECLAIM_TAG);
825 if (!pag->pag_ici_reclaimable) {
826 /* propagate the reclaim tag up into the perag radix tree */
827 spin_lock(&ip->i_mount->m_perag_lock);
828 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
829 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
830 XFS_ICI_RECLAIM_TAG);
831 spin_unlock(&ip->i_mount->m_perag_lock);
833 /* schedule periodic background inode reclaim */
834 xfs_reclaim_work_queue(ip->i_mount);
836 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
839 pag->pag_ici_reclaimable++;
843 * We set the inode flag atomically with the radix tree tag.
844 * Once we get tag lookups on the radix tree, this inode flag
848 xfs_inode_set_reclaim_tag(
851 struct xfs_mount *mp = ip->i_mount;
852 struct xfs_perag *pag;
854 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
855 spin_lock(&pag->pag_ici_lock);
856 spin_lock(&ip->i_flags_lock);
857 __xfs_inode_set_reclaim_tag(pag, ip);
858 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
859 spin_unlock(&ip->i_flags_lock);
860 spin_unlock(&pag->pag_ici_lock);
865 __xfs_inode_clear_reclaim(
869 pag->pag_ici_reclaimable--;
870 if (!pag->pag_ici_reclaimable) {
871 /* clear the reclaim tag from the perag radix tree */
872 spin_lock(&ip->i_mount->m_perag_lock);
873 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
874 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
875 XFS_ICI_RECLAIM_TAG);
876 spin_unlock(&ip->i_mount->m_perag_lock);
877 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
883 __xfs_inode_clear_reclaim_tag(
888 radix_tree_tag_clear(&pag->pag_ici_root,
889 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
890 __xfs_inode_clear_reclaim(pag, ip);
894 * Grab the inode for reclaim exclusively.
895 * Return 0 if we grabbed it, non-zero otherwise.
898 xfs_reclaim_inode_grab(
899 struct xfs_inode *ip,
902 ASSERT(rcu_read_lock_held());
904 /* quick check for stale RCU freed inode */
909 * If we are asked for non-blocking operation, do unlocked checks to
910 * see if the inode already is being flushed or in reclaim to avoid
913 if ((flags & SYNC_TRYLOCK) &&
914 __xfs_iflags_test(ip, XFS_IFLOCK | XFS_IRECLAIM))
918 * The radix tree lock here protects a thread in xfs_iget from racing
919 * with us starting reclaim on the inode. Once we have the
920 * XFS_IRECLAIM flag set it will not touch us.
922 * Due to RCU lookup, we may find inodes that have been freed and only
923 * have XFS_IRECLAIM set. Indeed, we may see reallocated inodes that
924 * aren't candidates for reclaim at all, so we must check the
925 * XFS_IRECLAIMABLE is set first before proceeding to reclaim.
927 spin_lock(&ip->i_flags_lock);
928 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
929 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
930 /* not a reclaim candidate. */
931 spin_unlock(&ip->i_flags_lock);
934 __xfs_iflags_set(ip, XFS_IRECLAIM);
935 spin_unlock(&ip->i_flags_lock);
940 * Inodes in different states need to be treated differently. The following
941 * table lists the inode states and the reclaim actions necessary:
943 * inode state iflush ret required action
944 * --------------- ---------- ---------------
946 * shutdown EIO unpin and reclaim
947 * clean, unpinned 0 reclaim
948 * stale, unpinned 0 reclaim
949 * clean, pinned(*) 0 requeue
950 * stale, pinned EAGAIN requeue
951 * dirty, async - requeue
952 * dirty, sync 0 reclaim
954 * (*) dgc: I don't think the clean, pinned state is possible but it gets
955 * handled anyway given the order of checks implemented.
957 * Also, because we get the flush lock first, we know that any inode that has
958 * been flushed delwri has had the flush completed by the time we check that
959 * the inode is clean.
961 * Note that because the inode is flushed delayed write by AIL pushing, the
962 * flush lock may already be held here and waiting on it can result in very
963 * long latencies. Hence for sync reclaims, where we wait on the flush lock,
964 * the caller should push the AIL first before trying to reclaim inodes to
965 * minimise the amount of time spent waiting. For background relaim, we only
966 * bother to reclaim clean inodes anyway.
968 * Hence the order of actions after gaining the locks should be:
970 * shutdown => unpin and reclaim
971 * pinned, async => requeue
972 * pinned, sync => unpin
975 * dirty, async => requeue
976 * dirty, sync => flush, wait and reclaim
980 struct xfs_inode *ip,
981 struct xfs_perag *pag,
984 struct xfs_buf *bp = NULL;
989 xfs_ilock(ip, XFS_ILOCK_EXCL);
990 if (!xfs_iflock_nowait(ip)) {
991 if (!(sync_mode & SYNC_WAIT))
996 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
998 xfs_iflush_abort(ip, false);
1001 if (xfs_ipincount(ip)) {
1002 if (!(sync_mode & SYNC_WAIT))
1004 xfs_iunpin_wait(ip);
1006 if (xfs_iflags_test(ip, XFS_ISTALE))
1008 if (xfs_inode_clean(ip))
1012 * Never flush out dirty data during non-blocking reclaim, as it would
1013 * just contend with AIL pushing trying to do the same job.
1015 if (!(sync_mode & SYNC_WAIT))
1019 * Now we have an inode that needs flushing.
1021 * Note that xfs_iflush will never block on the inode buffer lock, as
1022 * xfs_ifree_cluster() can lock the inode buffer before it locks the
1023 * ip->i_lock, and we are doing the exact opposite here. As a result,
1024 * doing a blocking xfs_imap_to_bp() to get the cluster buffer would
1025 * result in an ABBA deadlock with xfs_ifree_cluster().
1027 * As xfs_ifree_cluser() must gather all inodes that are active in the
1028 * cache to mark them stale, if we hit this case we don't actually want
1029 * to do IO here - we want the inode marked stale so we can simply
1030 * reclaim it. Hence if we get an EAGAIN error here, just unlock the
1031 * inode, back off and try again. Hopefully the next pass through will
1032 * see the stale flag set on the inode.
1034 error = xfs_iflush(ip, &bp);
1035 if (error == -EAGAIN) {
1036 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1037 /* backoff longer than in xfs_ifree_cluster */
1043 error = xfs_bwrite(bp);
1050 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1052 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
1054 * Remove the inode from the per-AG radix tree.
1056 * Because radix_tree_delete won't complain even if the item was never
1057 * added to the tree assert that it's been there before to catch
1058 * problems with the inode life time early on.
1060 spin_lock(&pag->pag_ici_lock);
1061 if (!radix_tree_delete(&pag->pag_ici_root,
1062 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
1064 __xfs_inode_clear_reclaim(pag, ip);
1065 spin_unlock(&pag->pag_ici_lock);
1068 * Here we do an (almost) spurious inode lock in order to coordinate
1069 * with inode cache radix tree lookups. This is because the lookup
1070 * can reference the inodes in the cache without taking references.
1072 * We make that OK here by ensuring that we wait until the inode is
1073 * unlocked after the lookup before we go ahead and free it.
1075 xfs_ilock(ip, XFS_ILOCK_EXCL);
1076 xfs_qm_dqdetach(ip);
1077 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1085 xfs_iflags_clear(ip, XFS_IRECLAIM);
1086 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1088 * We could return -EAGAIN here to make reclaim rescan the inode tree in
1089 * a short while. However, this just burns CPU time scanning the tree
1090 * waiting for IO to complete and the reclaim work never goes back to
1091 * the idle state. Instead, return 0 to let the next scheduled
1092 * background reclaim attempt to reclaim the inode again.
1098 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1099 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1100 * then a shut down during filesystem unmount reclaim walk leak all the
1101 * unreclaimed inodes.
1104 xfs_reclaim_inodes_ag(
1105 struct xfs_mount *mp,
1109 struct xfs_perag *pag;
1113 int trylock = flags & SYNC_TRYLOCK;
1119 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1120 unsigned long first_index = 0;
1124 ag = pag->pag_agno + 1;
1127 if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) {
1132 first_index = pag->pag_ici_reclaim_cursor;
1134 mutex_lock(&pag->pag_ici_reclaim_lock);
1137 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1141 nr_found = radix_tree_gang_lookup_tag(
1143 (void **)batch, first_index,
1145 XFS_ICI_RECLAIM_TAG);
1153 * Grab the inodes before we drop the lock. if we found
1154 * nothing, nr == 0 and the loop will be skipped.
1156 for (i = 0; i < nr_found; i++) {
1157 struct xfs_inode *ip = batch[i];
1159 if (done || xfs_reclaim_inode_grab(ip, flags))
1163 * Update the index for the next lookup. Catch
1164 * overflows into the next AG range which can
1165 * occur if we have inodes in the last block of
1166 * the AG and we are currently pointing to the
1169 * Because we may see inodes that are from the
1170 * wrong AG due to RCU freeing and
1171 * reallocation, only update the index if it
1172 * lies in this AG. It was a race that lead us
1173 * to see this inode, so another lookup from
1174 * the same index will not find it again.
1176 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1179 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1180 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1184 /* unlock now we've grabbed the inodes. */
1187 for (i = 0; i < nr_found; i++) {
1190 error = xfs_reclaim_inode(batch[i], pag, flags);
1191 if (error && last_error != -EFSCORRUPTED)
1195 *nr_to_scan -= XFS_LOOKUP_BATCH;
1199 } while (nr_found && !done && *nr_to_scan > 0);
1201 if (trylock && !done)
1202 pag->pag_ici_reclaim_cursor = first_index;
1204 pag->pag_ici_reclaim_cursor = 0;
1205 mutex_unlock(&pag->pag_ici_reclaim_lock);
1210 * if we skipped any AG, and we still have scan count remaining, do
1211 * another pass this time using blocking reclaim semantics (i.e
1212 * waiting on the reclaim locks and ignoring the reclaim cursors). This
1213 * ensure that when we get more reclaimers than AGs we block rather
1214 * than spin trying to execute reclaim.
1216 if (skipped && (flags & SYNC_WAIT) && *nr_to_scan > 0) {
1228 int nr_to_scan = INT_MAX;
1230 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
1234 * Scan a certain number of inodes for reclaim.
1236 * When called we make sure that there is a background (fast) inode reclaim in
1237 * progress, while we will throttle the speed of reclaim via doing synchronous
1238 * reclaim of inodes. That means if we come across dirty inodes, we wait for
1239 * them to be cleaned, which we hope will not be very long due to the
1240 * background walker having already kicked the IO off on those dirty inodes.
1243 xfs_reclaim_inodes_nr(
1244 struct xfs_mount *mp,
1247 /* kick background reclaimer and push the AIL */
1248 xfs_reclaim_work_queue(mp);
1249 xfs_ail_push_all(mp->m_ail);
1251 return xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK | SYNC_WAIT, &nr_to_scan);
1255 * Return the number of reclaimable inodes in the filesystem for
1256 * the shrinker to determine how much to reclaim.
1259 xfs_reclaim_inodes_count(
1260 struct xfs_mount *mp)
1262 struct xfs_perag *pag;
1263 xfs_agnumber_t ag = 0;
1264 int reclaimable = 0;
1266 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1267 ag = pag->pag_agno + 1;
1268 reclaimable += pag->pag_ici_reclaimable;
1276 struct xfs_inode *ip,
1277 struct xfs_eofblocks *eofb)
1279 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1280 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1283 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1284 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1287 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1288 xfs_get_projid(ip) != eofb->eof_prid)
1295 * A union-based inode filtering algorithm. Process the inode if any of the
1296 * criteria match. This is for global/internal scans only.
1299 xfs_inode_match_id_union(
1300 struct xfs_inode *ip,
1301 struct xfs_eofblocks *eofb)
1303 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1304 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1307 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1308 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1311 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1312 xfs_get_projid(ip) == eofb->eof_prid)
1319 xfs_inode_free_eofblocks(
1320 struct xfs_inode *ip,
1325 struct xfs_eofblocks *eofb = args;
1326 bool need_iolock = true;
1329 ASSERT(!eofb || (eofb && eofb->eof_scan_owner != 0));
1331 if (!xfs_can_free_eofblocks(ip, false)) {
1332 /* inode could be preallocated or append-only */
1333 trace_xfs_inode_free_eofblocks_invalid(ip);
1334 xfs_inode_clear_eofblocks_tag(ip);
1339 * If the mapping is dirty the operation can block and wait for some
1340 * time. Unless we are waiting, skip it.
1342 if (!(flags & SYNC_WAIT) &&
1343 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1347 if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
1348 match = xfs_inode_match_id_union(ip, eofb);
1350 match = xfs_inode_match_id(ip, eofb);
1354 /* skip the inode if the file size is too small */
1355 if (eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE &&
1356 XFS_ISIZE(ip) < eofb->eof_min_file_size)
1360 * A scan owner implies we already hold the iolock. Skip it in
1361 * xfs_free_eofblocks() to avoid deadlock. This also eliminates
1362 * the possibility of EAGAIN being returned.
1364 if (eofb->eof_scan_owner == ip->i_ino)
1365 need_iolock = false;
1368 ret = xfs_free_eofblocks(ip->i_mount, ip, need_iolock);
1370 /* don't revisit the inode if we're not waiting */
1371 if (ret == -EAGAIN && !(flags & SYNC_WAIT))
1378 xfs_icache_free_eofblocks(
1379 struct xfs_mount *mp,
1380 struct xfs_eofblocks *eofb)
1382 int flags = SYNC_TRYLOCK;
1384 if (eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC))
1387 return xfs_inode_ag_iterator_tag(mp, xfs_inode_free_eofblocks, flags,
1388 eofb, XFS_ICI_EOFBLOCKS_TAG);
1392 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1393 * multiple quotas, we don't know exactly which quota caused an allocation
1394 * failure. We make a best effort by including each quota under low free space
1395 * conditions (less than 1% free space) in the scan.
1398 xfs_inode_free_quota_eofblocks(
1399 struct xfs_inode *ip)
1402 struct xfs_eofblocks eofb = {0};
1403 struct xfs_dquot *dq;
1405 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1408 * Set the scan owner to avoid a potential livelock. Otherwise, the scan
1409 * can repeatedly trylock on the inode we're currently processing. We
1410 * run a sync scan to increase effectiveness and use the union filter to
1411 * cover all applicable quotas in a single scan.
1413 eofb.eof_scan_owner = ip->i_ino;
1414 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1416 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1417 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1418 if (dq && xfs_dquot_lowsp(dq)) {
1419 eofb.eof_uid = VFS_I(ip)->i_uid;
1420 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1425 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1426 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1427 if (dq && xfs_dquot_lowsp(dq)) {
1428 eofb.eof_gid = VFS_I(ip)->i_gid;
1429 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1435 xfs_icache_free_eofblocks(ip->i_mount, &eofb);
1441 xfs_inode_set_eofblocks_tag(
1444 struct xfs_mount *mp = ip->i_mount;
1445 struct xfs_perag *pag;
1448 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1449 spin_lock(&pag->pag_ici_lock);
1450 trace_xfs_inode_set_eofblocks_tag(ip);
1452 tagged = radix_tree_tagged(&pag->pag_ici_root,
1453 XFS_ICI_EOFBLOCKS_TAG);
1454 radix_tree_tag_set(&pag->pag_ici_root,
1455 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1456 XFS_ICI_EOFBLOCKS_TAG);
1458 /* propagate the eofblocks tag up into the perag radix tree */
1459 spin_lock(&ip->i_mount->m_perag_lock);
1460 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1461 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1462 XFS_ICI_EOFBLOCKS_TAG);
1463 spin_unlock(&ip->i_mount->m_perag_lock);
1465 /* kick off background trimming */
1466 xfs_queue_eofblocks(ip->i_mount);
1468 trace_xfs_perag_set_eofblocks(ip->i_mount, pag->pag_agno,
1472 spin_unlock(&pag->pag_ici_lock);
1477 xfs_inode_clear_eofblocks_tag(
1480 struct xfs_mount *mp = ip->i_mount;
1481 struct xfs_perag *pag;
1483 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1484 spin_lock(&pag->pag_ici_lock);
1485 trace_xfs_inode_clear_eofblocks_tag(ip);
1487 radix_tree_tag_clear(&pag->pag_ici_root,
1488 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
1489 XFS_ICI_EOFBLOCKS_TAG);
1490 if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_EOFBLOCKS_TAG)) {
1491 /* clear the eofblocks tag from the perag radix tree */
1492 spin_lock(&ip->i_mount->m_perag_lock);
1493 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1494 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1495 XFS_ICI_EOFBLOCKS_TAG);
1496 spin_unlock(&ip->i_mount->m_perag_lock);
1497 trace_xfs_perag_clear_eofblocks(ip->i_mount, pag->pag_agno,
1501 spin_unlock(&pag->pag_ici_lock);