1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_inode_item.h"
18 #include "xfs_quota.h"
19 #include "xfs_trace.h"
20 #include "xfs_icache.h"
21 #include "xfs_bmap_util.h"
22 #include "xfs_dquot_item.h"
23 #include "xfs_dquot.h"
24 #include "xfs_reflink.h"
25 #include "xfs_ialloc.h"
27 #include <linux/iversion.h>
30 * Allocate and initialise an xfs_inode.
40 * if this didn't occur in transactions, we could use
41 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
42 * code up to do this anyway.
44 ip = kmem_zone_alloc(xfs_inode_zone, 0);
47 if (inode_init_always(mp->m_super, VFS_I(ip))) {
48 kmem_cache_free(xfs_inode_zone, ip);
52 /* VFS doesn't initialise i_mode! */
53 VFS_I(ip)->i_mode = 0;
55 XFS_STATS_INC(mp, vn_active);
56 ASSERT(atomic_read(&ip->i_pincount) == 0);
57 ASSERT(!xfs_isiflocked(ip));
58 ASSERT(ip->i_ino == 0);
60 /* initialise the xfs inode */
63 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
66 memset(&ip->i_df, 0, sizeof(ip->i_df));
68 ip->i_delayed_blks = 0;
69 memset(&ip->i_d, 0, sizeof(ip->i_d));
72 INIT_WORK(&ip->i_ioend_work, xfs_end_io);
73 INIT_LIST_HEAD(&ip->i_ioend_list);
74 spin_lock_init(&ip->i_ioend_lock);
80 xfs_inode_free_callback(
81 struct rcu_head *head)
83 struct inode *inode = container_of(head, struct inode, i_rcu);
84 struct xfs_inode *ip = XFS_I(inode);
86 switch (VFS_I(ip)->i_mode & S_IFMT) {
90 xfs_idestroy_fork(&ip->i_df);
95 xfs_idestroy_fork(ip->i_afp);
96 kmem_cache_free(xfs_ifork_zone, ip->i_afp);
99 xfs_idestroy_fork(ip->i_cowfp);
100 kmem_cache_free(xfs_ifork_zone, ip->i_cowfp);
103 ASSERT(!test_bit(XFS_LI_IN_AIL,
104 &ip->i_itemp->ili_item.li_flags));
105 xfs_inode_item_destroy(ip);
109 kmem_cache_free(xfs_inode_zone, ip);
114 struct xfs_inode *ip)
116 /* asserts to verify all state is correct here */
117 ASSERT(atomic_read(&ip->i_pincount) == 0);
118 ASSERT(!ip->i_itemp || list_empty(&ip->i_itemp->ili_item.li_bio_list));
119 XFS_STATS_DEC(ip->i_mount, vn_active);
121 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
126 struct xfs_inode *ip)
128 ASSERT(!xfs_isiflocked(ip));
131 * Because we use RCU freeing we need to ensure the inode always
132 * appears to be reclaimed with an invalid inode number when in the
133 * free state. The ip->i_flags_lock provides the barrier against lookup
136 spin_lock(&ip->i_flags_lock);
137 ip->i_flags = XFS_IRECLAIM;
139 spin_unlock(&ip->i_flags_lock);
141 __xfs_inode_free(ip);
145 * Queue background inode reclaim work if there are reclaimable inodes and there
146 * isn't reclaim work already scheduled or in progress.
149 xfs_reclaim_work_queue(
150 struct xfs_mount *mp)
154 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
155 queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
156 msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
162 xfs_perag_set_reclaim_tag(
163 struct xfs_perag *pag)
165 struct xfs_mount *mp = pag->pag_mount;
167 lockdep_assert_held(&pag->pag_ici_lock);
168 if (pag->pag_ici_reclaimable++)
171 /* propagate the reclaim tag up into the perag radix tree */
172 spin_lock(&mp->m_perag_lock);
173 radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno,
174 XFS_ICI_RECLAIM_TAG);
175 spin_unlock(&mp->m_perag_lock);
177 /* schedule periodic background inode reclaim */
178 xfs_reclaim_work_queue(mp);
180 trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
184 xfs_perag_clear_reclaim_tag(
185 struct xfs_perag *pag)
187 struct xfs_mount *mp = pag->pag_mount;
189 lockdep_assert_held(&pag->pag_ici_lock);
190 if (--pag->pag_ici_reclaimable)
193 /* clear the reclaim tag from the perag radix tree */
194 spin_lock(&mp->m_perag_lock);
195 radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno,
196 XFS_ICI_RECLAIM_TAG);
197 spin_unlock(&mp->m_perag_lock);
198 trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
203 * We set the inode flag atomically with the radix tree tag.
204 * Once we get tag lookups on the radix tree, this inode flag
208 xfs_inode_set_reclaim_tag(
209 struct xfs_inode *ip)
211 struct xfs_mount *mp = ip->i_mount;
212 struct xfs_perag *pag;
214 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
215 spin_lock(&pag->pag_ici_lock);
216 spin_lock(&ip->i_flags_lock);
218 radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
219 XFS_ICI_RECLAIM_TAG);
220 xfs_perag_set_reclaim_tag(pag);
221 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
223 spin_unlock(&ip->i_flags_lock);
224 spin_unlock(&pag->pag_ici_lock);
229 xfs_inode_clear_reclaim_tag(
230 struct xfs_perag *pag,
233 radix_tree_tag_clear(&pag->pag_ici_root,
234 XFS_INO_TO_AGINO(pag->pag_mount, ino),
235 XFS_ICI_RECLAIM_TAG);
236 xfs_perag_clear_reclaim_tag(pag);
241 struct xfs_inode *ip)
243 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT);
244 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT);
247 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
248 if (!xfs_iflags_test(ip, XFS_INEW))
252 finish_wait(wq, &wait.wq_entry);
256 * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
257 * part of the structure. This is made more complex by the fact we store
258 * information about the on-disk values in the VFS inode and so we can't just
259 * overwrite the values unconditionally. Hence we save the parameters we
260 * need to retain across reinitialisation, and rewrite them into the VFS inode
261 * after reinitialisation even if it fails.
265 struct xfs_mount *mp,
269 uint32_t nlink = inode->i_nlink;
270 uint32_t generation = inode->i_generation;
271 uint64_t version = inode_peek_iversion(inode);
272 umode_t mode = inode->i_mode;
273 dev_t dev = inode->i_rdev;
274 kuid_t uid = inode->i_uid;
275 kgid_t gid = inode->i_gid;
277 error = inode_init_always(mp->m_super, inode);
279 set_nlink(inode, nlink);
280 inode->i_generation = generation;
281 inode_set_iversion_queried(inode, version);
282 inode->i_mode = mode;
290 * If we are allocating a new inode, then check what was returned is
291 * actually a free, empty inode. If we are not allocating an inode,
292 * then check we didn't find a free inode.
295 * 0 if the inode free state matches the lookup context
296 * -ENOENT if the inode is free and we are not allocating
297 * -EFSCORRUPTED if there is any state mismatch at all
300 xfs_iget_check_free_state(
301 struct xfs_inode *ip,
304 if (flags & XFS_IGET_CREATE) {
305 /* should be a free inode */
306 if (VFS_I(ip)->i_mode != 0) {
307 xfs_warn(ip->i_mount,
308 "Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)",
309 ip->i_ino, VFS_I(ip)->i_mode);
310 return -EFSCORRUPTED;
313 if (ip->i_d.di_nblocks != 0) {
314 xfs_warn(ip->i_mount,
315 "Corruption detected! Free inode 0x%llx has blocks allocated!",
317 return -EFSCORRUPTED;
322 /* should be an allocated inode */
323 if (VFS_I(ip)->i_mode == 0)
330 * Check the validity of the inode we just found it the cache
334 struct xfs_perag *pag,
335 struct xfs_inode *ip,
338 int lock_flags) __releases(RCU)
340 struct inode *inode = VFS_I(ip);
341 struct xfs_mount *mp = ip->i_mount;
345 * check for re-use of an inode within an RCU grace period due to the
346 * radix tree nodes not being updated yet. We monitor for this by
347 * setting the inode number to zero before freeing the inode structure.
348 * If the inode has been reallocated and set up, then the inode number
349 * will not match, so check for that, too.
351 spin_lock(&ip->i_flags_lock);
352 if (ip->i_ino != ino) {
353 trace_xfs_iget_skip(ip);
354 XFS_STATS_INC(mp, xs_ig_frecycle);
361 * If we are racing with another cache hit that is currently
362 * instantiating this inode or currently recycling it out of
363 * reclaimabe state, wait for the initialisation to complete
366 * XXX(hch): eventually we should do something equivalent to
367 * wait_on_inode to wait for these flags to be cleared
368 * instead of polling for it.
370 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
371 trace_xfs_iget_skip(ip);
372 XFS_STATS_INC(mp, xs_ig_frecycle);
378 * Check the inode free state is valid. This also detects lookup
379 * racing with unlinks.
381 error = xfs_iget_check_free_state(ip, flags);
386 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
387 * Need to carefully get it back into useable state.
389 if (ip->i_flags & XFS_IRECLAIMABLE) {
390 trace_xfs_iget_reclaim(ip);
392 if (flags & XFS_IGET_INCORE) {
398 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
399 * from stomping over us while we recycle the inode. We can't
400 * clear the radix tree reclaimable tag yet as it requires
401 * pag_ici_lock to be held exclusive.
403 ip->i_flags |= XFS_IRECLAIM;
405 spin_unlock(&ip->i_flags_lock);
408 ASSERT(!rwsem_is_locked(&inode->i_rwsem));
409 error = xfs_reinit_inode(mp, inode);
413 * Re-initializing the inode failed, and we are in deep
414 * trouble. Try to re-add it to the reclaim list.
417 spin_lock(&ip->i_flags_lock);
418 wake = !!__xfs_iflags_test(ip, XFS_INEW);
419 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
421 wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
422 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
423 trace_xfs_iget_reclaim_fail(ip);
427 spin_lock(&pag->pag_ici_lock);
428 spin_lock(&ip->i_flags_lock);
431 * Clear the per-lifetime state in the inode as we are now
432 * effectively a new inode and need to return to the initial
433 * state before reuse occurs.
435 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
436 ip->i_flags |= XFS_INEW;
437 xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
438 inode->i_state = I_NEW;
442 spin_unlock(&ip->i_flags_lock);
443 spin_unlock(&pag->pag_ici_lock);
445 /* If the VFS inode is being torn down, pause and try again. */
447 trace_xfs_iget_skip(ip);
452 /* We've got a live one. */
453 spin_unlock(&ip->i_flags_lock);
455 trace_xfs_iget_hit(ip);
459 xfs_ilock(ip, lock_flags);
461 if (!(flags & XFS_IGET_INCORE))
462 xfs_iflags_clear(ip, XFS_ISTALE);
463 XFS_STATS_INC(mp, xs_ig_found);
468 spin_unlock(&ip->i_flags_lock);
476 struct xfs_mount *mp,
477 struct xfs_perag *pag,
480 struct xfs_inode **ipp,
484 struct xfs_inode *ip;
486 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
489 ip = xfs_inode_alloc(mp, ino);
493 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, flags);
498 * For version 5 superblocks, if we are initialising a new inode and we
499 * are not utilising the XFS_MOUNT_IKEEP inode cluster mode, we can
500 * simply build the new inode core with a random generation number.
502 * For version 4 (and older) superblocks, log recovery is dependent on
503 * the di_flushiter field being initialised from the current on-disk
504 * value and hence we must also read the inode off disk even when
505 * initializing new inodes.
507 if (xfs_sb_version_has_v3inode(&mp->m_sb) &&
508 (flags & XFS_IGET_CREATE) && !(mp->m_flags & XFS_MOUNT_IKEEP)) {
509 VFS_I(ip)->i_generation = prandom_u32();
511 struct xfs_dinode *dip;
514 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0);
518 error = xfs_inode_from_disk(ip, dip);
520 xfs_buf_set_ref(bp, XFS_INO_REF);
521 xfs_trans_brelse(tp, bp);
527 trace_xfs_iget_miss(ip);
530 * Check the inode free state is valid. This also detects lookup
531 * racing with unlinks.
533 error = xfs_iget_check_free_state(ip, flags);
538 * Preload the radix tree so we can insert safely under the
539 * write spinlock. Note that we cannot sleep inside the preload
540 * region. Since we can be called from transaction context, don't
541 * recurse into the file system.
543 if (radix_tree_preload(GFP_NOFS)) {
549 * Because the inode hasn't been added to the radix-tree yet it can't
550 * be found by another thread, so we can do the non-sleeping lock here.
553 if (!xfs_ilock_nowait(ip, lock_flags))
558 * These values must be set before inserting the inode into the radix
559 * tree as the moment it is inserted a concurrent lookup (allowed by the
560 * RCU locking mechanism) can find it and that lookup must see that this
561 * is an inode currently under construction (i.e. that XFS_INEW is set).
562 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
563 * memory barrier that ensures this detection works correctly at lookup
567 if (flags & XFS_IGET_DONTCACHE)
568 d_mark_dontcache(VFS_I(ip));
572 xfs_iflags_set(ip, iflags);
574 /* insert the new inode */
575 spin_lock(&pag->pag_ici_lock);
576 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
577 if (unlikely(error)) {
578 WARN_ON(error != -EEXIST);
579 XFS_STATS_INC(mp, xs_ig_dup);
581 goto out_preload_end;
583 spin_unlock(&pag->pag_ici_lock);
584 radix_tree_preload_end();
590 spin_unlock(&pag->pag_ici_lock);
591 radix_tree_preload_end();
593 xfs_iunlock(ip, lock_flags);
595 __destroy_inode(VFS_I(ip));
601 * Look up an inode by number in the given file system. The inode is looked up
602 * in the cache held in each AG. If the inode is found in the cache, initialise
603 * the vfs inode if necessary.
605 * If it is not in core, read it in from the file system's device, add it to the
606 * cache and initialise the vfs inode.
608 * The inode is locked according to the value of the lock_flags parameter.
609 * Inode lookup is only done during metadata operations and not as part of the
610 * data IO path. Hence we only allow locking of the XFS_ILOCK during lookup.
614 struct xfs_mount *mp,
615 struct xfs_trans *tp,
619 struct xfs_inode **ipp)
621 struct xfs_inode *ip;
622 struct xfs_perag *pag;
626 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
628 /* reject inode numbers outside existing AGs */
629 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
632 XFS_STATS_INC(mp, xs_ig_attempts);
634 /* get the perag structure and ensure that it's inode capable */
635 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
636 agino = XFS_INO_TO_AGINO(mp, ino);
641 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
644 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
646 goto out_error_or_again;
649 if (flags & XFS_IGET_INCORE) {
651 goto out_error_or_again;
653 XFS_STATS_INC(mp, xs_ig_missed);
655 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
658 goto out_error_or_again;
665 * If we have a real type for an on-disk inode, we can setup the inode
666 * now. If it's a new inode being created, xfs_ialloc will handle it.
668 if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
669 xfs_setup_existing_inode(ip);
673 if (!(flags & XFS_IGET_INCORE) && error == -EAGAIN) {
682 * "Is this a cached inode that's also allocated?"
684 * Look up an inode by number in the given file system. If the inode is
685 * in cache and isn't in purgatory, return 1 if the inode is allocated
686 * and 0 if it is not. For all other cases (not in cache, being torn
687 * down, etc.), return a negative error code.
689 * The caller has to prevent inode allocation and freeing activity,
690 * presumably by locking the AGI buffer. This is to ensure that an
691 * inode cannot transition from allocated to freed until the caller is
692 * ready to allow that. If the inode is in an intermediate state (new,
693 * reclaimable, or being reclaimed), -EAGAIN will be returned; if the
694 * inode is not in the cache, -ENOENT will be returned. The caller must
695 * deal with these scenarios appropriately.
697 * This is a specialized use case for the online scrubber; if you're
698 * reading this, you probably want xfs_iget.
701 xfs_icache_inode_is_allocated(
702 struct xfs_mount *mp,
703 struct xfs_trans *tp,
707 struct xfs_inode *ip;
710 error = xfs_iget(mp, tp, ino, XFS_IGET_INCORE, 0, &ip);
714 *inuse = !!(VFS_I(ip)->i_mode);
720 * The inode lookup is done in batches to keep the amount of lock traffic and
721 * radix tree lookups to a minimum. The batch size is a trade off between
722 * lookup reduction and stack usage. This is in the reclaim path, so we can't
725 #define XFS_LOOKUP_BATCH 32
728 * Decide if the given @ip is eligible to be a part of the inode walk, and
729 * grab it if so. Returns true if it's ready to go or false if we should just
733 xfs_inode_walk_ag_grab(
734 struct xfs_inode *ip,
737 struct inode *inode = VFS_I(ip);
738 bool newinos = !!(flags & XFS_INODE_WALK_INEW_WAIT);
740 ASSERT(rcu_read_lock_held());
742 /* Check for stale RCU freed inode */
743 spin_lock(&ip->i_flags_lock);
745 goto out_unlock_noent;
747 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
748 if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) ||
749 __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM))
750 goto out_unlock_noent;
751 spin_unlock(&ip->i_flags_lock);
753 /* nothing to sync during shutdown */
754 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
757 /* If we can't grab the inode, it must on it's way to reclaim. */
765 spin_unlock(&ip->i_flags_lock);
770 * For a given per-AG structure @pag, grab, @execute, and rele all incore
771 * inodes with the given radix tree @tag.
775 struct xfs_perag *pag,
777 int (*execute)(struct xfs_inode *ip, void *args),
781 struct xfs_mount *mp = pag->pag_mount;
782 uint32_t first_index;
794 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
800 if (tag == XFS_ICI_NO_TAG)
801 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
802 (void **)batch, first_index,
805 nr_found = radix_tree_gang_lookup_tag(
807 (void **) batch, first_index,
808 XFS_LOOKUP_BATCH, tag);
816 * Grab the inodes before we drop the lock. if we found
817 * nothing, nr == 0 and the loop will be skipped.
819 for (i = 0; i < nr_found; i++) {
820 struct xfs_inode *ip = batch[i];
822 if (done || !xfs_inode_walk_ag_grab(ip, iter_flags))
826 * Update the index for the next lookup. Catch
827 * overflows into the next AG range which can occur if
828 * we have inodes in the last block of the AG and we
829 * are currently pointing to the last inode.
831 * Because we may see inodes that are from the wrong AG
832 * due to RCU freeing and reallocation, only update the
833 * index if it lies in this AG. It was a race that lead
834 * us to see this inode, so another lookup from the
835 * same index will not find it again.
837 if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
839 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
840 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
844 /* unlock now we've grabbed the inodes. */
847 for (i = 0; i < nr_found; i++) {
850 if ((iter_flags & XFS_INODE_WALK_INEW_WAIT) &&
851 xfs_iflags_test(batch[i], XFS_INEW))
852 xfs_inew_wait(batch[i]);
853 error = execute(batch[i], args);
855 if (error == -EAGAIN) {
859 if (error && last_error != -EFSCORRUPTED)
863 /* bail out if the filesystem is corrupted. */
864 if (error == -EFSCORRUPTED)
869 } while (nr_found && !done);
878 /* Fetch the next (possibly tagged) per-AG structure. */
879 static inline struct xfs_perag *
880 xfs_inode_walk_get_perag(
881 struct xfs_mount *mp,
885 if (tag == XFS_ICI_NO_TAG)
886 return xfs_perag_get(mp, agno);
887 return xfs_perag_get_tag(mp, agno, tag);
891 * Call the @execute function on all incore inodes matching the radix tree
896 struct xfs_mount *mp,
898 int (*execute)(struct xfs_inode *ip, void *args),
902 struct xfs_perag *pag;
908 while ((pag = xfs_inode_walk_get_perag(mp, ag, tag))) {
909 ag = pag->pag_agno + 1;
910 error = xfs_inode_walk_ag(pag, iter_flags, execute, args, tag);
914 if (error == -EFSCORRUPTED)
922 * Background scanning to trim post-EOF preallocated space. This is queued
923 * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
927 struct xfs_mount *mp)
930 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
931 queue_delayed_work(mp->m_eofblocks_workqueue,
932 &mp->m_eofblocks_work,
933 msecs_to_jiffies(xfs_eofb_secs * 1000));
938 xfs_eofblocks_worker(
939 struct work_struct *work)
941 struct xfs_mount *mp = container_of(to_delayed_work(work),
942 struct xfs_mount, m_eofblocks_work);
944 if (!sb_start_write_trylock(mp->m_super))
946 xfs_icache_free_eofblocks(mp, NULL);
947 sb_end_write(mp->m_super);
949 xfs_queue_eofblocks(mp);
953 * Background scanning to trim preallocated CoW space. This is queued
954 * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default).
955 * (We'll just piggyback on the post-EOF prealloc space workqueue.)
959 struct xfs_mount *mp)
962 if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG))
963 queue_delayed_work(mp->m_eofblocks_workqueue,
964 &mp->m_cowblocks_work,
965 msecs_to_jiffies(xfs_cowb_secs * 1000));
970 xfs_cowblocks_worker(
971 struct work_struct *work)
973 struct xfs_mount *mp = container_of(to_delayed_work(work),
974 struct xfs_mount, m_cowblocks_work);
976 if (!sb_start_write_trylock(mp->m_super))
978 xfs_icache_free_cowblocks(mp, NULL);
979 sb_end_write(mp->m_super);
981 xfs_queue_cowblocks(mp);
985 * Grab the inode for reclaim exclusively.
987 * We have found this inode via a lookup under RCU, so the inode may have
988 * already been freed, or it may be in the process of being recycled by
989 * xfs_iget(). In both cases, the inode will have XFS_IRECLAIM set. If the inode
990 * has been fully recycled by the time we get the i_flags_lock, XFS_IRECLAIMABLE
991 * will not be set. Hence we need to check for both these flag conditions to
992 * avoid inodes that are no longer reclaim candidates.
994 * Note: checking for other state flags here, under the i_flags_lock or not, is
995 * racy and should be avoided. Those races should be resolved only after we have
996 * ensured that we are able to reclaim this inode and the world can see that we
997 * are going to reclaim it.
999 * Return true if we grabbed it, false otherwise.
1002 xfs_reclaim_inode_grab(
1003 struct xfs_inode *ip)
1005 ASSERT(rcu_read_lock_held());
1007 spin_lock(&ip->i_flags_lock);
1008 if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
1009 __xfs_iflags_test(ip, XFS_IRECLAIM)) {
1010 /* not a reclaim candidate. */
1011 spin_unlock(&ip->i_flags_lock);
1014 __xfs_iflags_set(ip, XFS_IRECLAIM);
1015 spin_unlock(&ip->i_flags_lock);
1020 * Inode reclaim is non-blocking, so the default action if progress cannot be
1021 * made is to "requeue" the inode for reclaim by unlocking it and clearing the
1022 * XFS_IRECLAIM flag. If we are in a shutdown state, we don't care about
1023 * blocking anymore and hence we can wait for the inode to be able to reclaim
1026 * We do no IO here - if callers require inodes to be cleaned they must push the
1027 * AIL first to trigger writeback of dirty inodes. This enables writeback to be
1028 * done in the background in a non-blocking manner, and enables memory reclaim
1029 * to make progress without blocking.
1033 struct xfs_inode *ip,
1034 struct xfs_perag *pag)
1036 xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
1038 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
1040 if (!xfs_iflock_nowait(ip))
1043 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1044 xfs_iunpin_wait(ip);
1045 /* xfs_iflush_abort() drops the flush lock */
1046 xfs_iflush_abort(ip);
1049 if (xfs_ipincount(ip))
1051 if (!xfs_inode_clean(ip))
1056 ASSERT(!xfs_isiflocked(ip));
1059 * Because we use RCU freeing we need to ensure the inode always appears
1060 * to be reclaimed with an invalid inode number when in the free state.
1061 * We do this as early as possible under the ILOCK so that
1062 * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to
1063 * detect races with us here. By doing this, we guarantee that once
1064 * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that
1065 * it will see either a valid inode that will serialise correctly, or it
1066 * will see an invalid inode that it can skip.
1068 spin_lock(&ip->i_flags_lock);
1069 ip->i_flags = XFS_IRECLAIM;
1071 spin_unlock(&ip->i_flags_lock);
1073 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1075 XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
1077 * Remove the inode from the per-AG radix tree.
1079 * Because radix_tree_delete won't complain even if the item was never
1080 * added to the tree assert that it's been there before to catch
1081 * problems with the inode life time early on.
1083 spin_lock(&pag->pag_ici_lock);
1084 if (!radix_tree_delete(&pag->pag_ici_root,
1085 XFS_INO_TO_AGINO(ip->i_mount, ino)))
1087 xfs_perag_clear_reclaim_tag(pag);
1088 spin_unlock(&pag->pag_ici_lock);
1091 * Here we do an (almost) spurious inode lock in order to coordinate
1092 * with inode cache radix tree lookups. This is because the lookup
1093 * can reference the inodes in the cache without taking references.
1095 * We make that OK here by ensuring that we wait until the inode is
1096 * unlocked after the lookup before we go ahead and free it.
1098 xfs_ilock(ip, XFS_ILOCK_EXCL);
1099 xfs_qm_dqdetach(ip);
1100 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1101 ASSERT(xfs_inode_clean(ip));
1103 __xfs_inode_free(ip);
1109 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1111 xfs_iflags_clear(ip, XFS_IRECLAIM);
1115 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
1116 * corrupted, we still want to try to reclaim all the inodes. If we don't,
1117 * then a shut down during filesystem unmount reclaim walk leak all the
1118 * unreclaimed inodes.
1120 * Returns non-zero if any AGs or inodes were skipped in the reclaim pass
1121 * so that callers that want to block until all dirty inodes are written back
1122 * and reclaimed can sanely loop.
1125 xfs_reclaim_inodes_ag(
1126 struct xfs_mount *mp,
1129 struct xfs_perag *pag;
1130 xfs_agnumber_t ag = 0;
1132 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1133 unsigned long first_index = 0;
1137 ag = pag->pag_agno + 1;
1139 first_index = READ_ONCE(pag->pag_ici_reclaim_cursor);
1141 struct xfs_inode *batch[XFS_LOOKUP_BATCH];
1145 nr_found = radix_tree_gang_lookup_tag(
1147 (void **)batch, first_index,
1149 XFS_ICI_RECLAIM_TAG);
1157 * Grab the inodes before we drop the lock. if we found
1158 * nothing, nr == 0 and the loop will be skipped.
1160 for (i = 0; i < nr_found; i++) {
1161 struct xfs_inode *ip = batch[i];
1163 if (done || !xfs_reclaim_inode_grab(ip))
1167 * Update the index for the next lookup. Catch
1168 * overflows into the next AG range which can
1169 * occur if we have inodes in the last block of
1170 * the AG and we are currently pointing to the
1173 * Because we may see inodes that are from the
1174 * wrong AG due to RCU freeing and
1175 * reallocation, only update the index if it
1176 * lies in this AG. It was a race that lead us
1177 * to see this inode, so another lookup from
1178 * the same index will not find it again.
1180 if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
1183 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
1184 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
1188 /* unlock now we've grabbed the inodes. */
1191 for (i = 0; i < nr_found; i++) {
1193 xfs_reclaim_inode(batch[i], pag);
1196 *nr_to_scan -= XFS_LOOKUP_BATCH;
1198 } while (nr_found && !done && *nr_to_scan > 0);
1202 WRITE_ONCE(pag->pag_ici_reclaim_cursor, first_index);
1209 struct xfs_mount *mp)
1211 int nr_to_scan = INT_MAX;
1213 while (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
1214 xfs_ail_push_all_sync(mp->m_ail);
1215 xfs_reclaim_inodes_ag(mp, &nr_to_scan);
1220 * The shrinker infrastructure determines how many inodes we should scan for
1221 * reclaim. We want as many clean inodes ready to reclaim as possible, so we
1222 * push the AIL here. We also want to proactively free up memory if we can to
1223 * minimise the amount of work memory reclaim has to do so we kick the
1224 * background reclaim if it isn't already scheduled.
1227 xfs_reclaim_inodes_nr(
1228 struct xfs_mount *mp,
1231 /* kick background reclaimer and push the AIL */
1232 xfs_reclaim_work_queue(mp);
1233 xfs_ail_push_all(mp->m_ail);
1235 xfs_reclaim_inodes_ag(mp, &nr_to_scan);
1240 * Return the number of reclaimable inodes in the filesystem for
1241 * the shrinker to determine how much to reclaim.
1244 xfs_reclaim_inodes_count(
1245 struct xfs_mount *mp)
1247 struct xfs_perag *pag;
1248 xfs_agnumber_t ag = 0;
1249 int reclaimable = 0;
1251 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
1252 ag = pag->pag_agno + 1;
1253 reclaimable += pag->pag_ici_reclaimable;
1261 struct xfs_inode *ip,
1262 struct xfs_eofblocks *eofb)
1264 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1265 !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1268 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1269 !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1272 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1273 ip->i_d.di_projid != eofb->eof_prid)
1280 * A union-based inode filtering algorithm. Process the inode if any of the
1281 * criteria match. This is for global/internal scans only.
1284 xfs_inode_match_id_union(
1285 struct xfs_inode *ip,
1286 struct xfs_eofblocks *eofb)
1288 if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
1289 uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
1292 if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
1293 gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
1296 if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
1297 ip->i_d.di_projid == eofb->eof_prid)
1304 * Is this inode @ip eligible for eof/cow block reclamation, given some
1305 * filtering parameters @eofb? The inode is eligible if @eofb is null or
1306 * if the predicate functions match.
1309 xfs_inode_matches_eofb(
1310 struct xfs_inode *ip,
1311 struct xfs_eofblocks *eofb)
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)
1334 * This is a fast pass over the inode cache to try to get reclaim moving on as
1335 * many inodes as possible in a short period of time. It kicks itself every few
1336 * seconds, as well as being kicked by the inode cache shrinker when memory
1341 struct work_struct *work)
1343 struct xfs_mount *mp = container_of(to_delayed_work(work),
1344 struct xfs_mount, m_reclaim_work);
1345 int nr_to_scan = INT_MAX;
1347 xfs_reclaim_inodes_ag(mp, &nr_to_scan);
1348 xfs_reclaim_work_queue(mp);
1352 xfs_inode_free_eofblocks(
1353 struct xfs_inode *ip,
1356 struct xfs_eofblocks *eofb = args;
1360 wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC);
1362 if (!xfs_can_free_eofblocks(ip, false)) {
1363 /* inode could be preallocated or append-only */
1364 trace_xfs_inode_free_eofblocks_invalid(ip);
1365 xfs_inode_clear_eofblocks_tag(ip);
1370 * If the mapping is dirty the operation can block and wait for some
1371 * time. Unless we are waiting, skip it.
1373 if (!wait && mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
1376 if (!xfs_inode_matches_eofb(ip, eofb))
1380 * If the caller is waiting, return -EAGAIN to keep the background
1381 * scanner moving and revisit the inode in a subsequent pass.
1383 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1389 ret = xfs_free_eofblocks(ip);
1390 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1396 xfs_icache_free_eofblocks(
1397 struct xfs_mount *mp,
1398 struct xfs_eofblocks *eofb)
1400 return xfs_inode_walk(mp, 0, xfs_inode_free_eofblocks, eofb,
1401 XFS_ICI_EOFBLOCKS_TAG);
1405 * Run eofblocks scans on the quotas applicable to the inode. For inodes with
1406 * multiple quotas, we don't know exactly which quota caused an allocation
1407 * failure. We make a best effort by including each quota under low free space
1408 * conditions (less than 1% free space) in the scan.
1411 __xfs_inode_free_quota_eofblocks(
1412 struct xfs_inode *ip,
1413 int (*execute)(struct xfs_mount *mp,
1414 struct xfs_eofblocks *eofb))
1417 struct xfs_eofblocks eofb = {0};
1418 struct xfs_dquot *dq;
1421 * Run a sync scan to increase effectiveness and use the union filter to
1422 * cover all applicable quotas in a single scan.
1424 eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
1426 if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
1427 dq = xfs_inode_dquot(ip, XFS_DQ_USER);
1428 if (dq && xfs_dquot_lowsp(dq)) {
1429 eofb.eof_uid = VFS_I(ip)->i_uid;
1430 eofb.eof_flags |= XFS_EOF_FLAGS_UID;
1435 if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
1436 dq = xfs_inode_dquot(ip, XFS_DQ_GROUP);
1437 if (dq && xfs_dquot_lowsp(dq)) {
1438 eofb.eof_gid = VFS_I(ip)->i_gid;
1439 eofb.eof_flags |= XFS_EOF_FLAGS_GID;
1445 execute(ip->i_mount, &eofb);
1451 xfs_inode_free_quota_eofblocks(
1452 struct xfs_inode *ip)
1454 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks);
1457 static inline unsigned long
1462 case XFS_ICI_EOFBLOCKS_TAG:
1463 return XFS_IEOFBLOCKS;
1464 case XFS_ICI_COWBLOCKS_TAG:
1465 return XFS_ICOWBLOCKS;
1473 __xfs_inode_set_blocks_tag(
1475 void (*execute)(struct xfs_mount *mp),
1476 void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1477 int error, unsigned long caller_ip),
1480 struct xfs_mount *mp = ip->i_mount;
1481 struct xfs_perag *pag;
1485 * Don't bother locking the AG and looking up in the radix trees
1486 * if we already know that we have the tag set.
1488 if (ip->i_flags & xfs_iflag_for_tag(tag))
1490 spin_lock(&ip->i_flags_lock);
1491 ip->i_flags |= xfs_iflag_for_tag(tag);
1492 spin_unlock(&ip->i_flags_lock);
1494 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1495 spin_lock(&pag->pag_ici_lock);
1497 tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
1498 radix_tree_tag_set(&pag->pag_ici_root,
1499 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
1501 /* propagate the eofblocks tag up into the perag radix tree */
1502 spin_lock(&ip->i_mount->m_perag_lock);
1503 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
1504 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1506 spin_unlock(&ip->i_mount->m_perag_lock);
1508 /* kick off background trimming */
1509 execute(ip->i_mount);
1511 set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
1514 spin_unlock(&pag->pag_ici_lock);
1519 xfs_inode_set_eofblocks_tag(
1522 trace_xfs_inode_set_eofblocks_tag(ip);
1523 return __xfs_inode_set_blocks_tag(ip, xfs_queue_eofblocks,
1524 trace_xfs_perag_set_eofblocks,
1525 XFS_ICI_EOFBLOCKS_TAG);
1529 __xfs_inode_clear_blocks_tag(
1531 void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
1532 int error, unsigned long caller_ip),
1535 struct xfs_mount *mp = ip->i_mount;
1536 struct xfs_perag *pag;
1538 spin_lock(&ip->i_flags_lock);
1539 ip->i_flags &= ~xfs_iflag_for_tag(tag);
1540 spin_unlock(&ip->i_flags_lock);
1542 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1543 spin_lock(&pag->pag_ici_lock);
1545 radix_tree_tag_clear(&pag->pag_ici_root,
1546 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
1547 if (!radix_tree_tagged(&pag->pag_ici_root, tag)) {
1548 /* clear the eofblocks tag from the perag radix tree */
1549 spin_lock(&ip->i_mount->m_perag_lock);
1550 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
1551 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
1553 spin_unlock(&ip->i_mount->m_perag_lock);
1554 clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
1557 spin_unlock(&pag->pag_ici_lock);
1562 xfs_inode_clear_eofblocks_tag(
1565 trace_xfs_inode_clear_eofblocks_tag(ip);
1566 return __xfs_inode_clear_blocks_tag(ip,
1567 trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG);
1571 * Set ourselves up to free CoW blocks from this file. If it's already clean
1572 * then we can bail out quickly, but otherwise we must back off if the file
1573 * is undergoing some kind of write.
1576 xfs_prep_free_cowblocks(
1577 struct xfs_inode *ip)
1580 * Just clear the tag if we have an empty cow fork or none at all. It's
1581 * possible the inode was fully unshared since it was originally tagged.
1583 if (!xfs_inode_has_cow_data(ip)) {
1584 trace_xfs_inode_free_cowblocks_invalid(ip);
1585 xfs_inode_clear_cowblocks_tag(ip);
1590 * If the mapping is dirty or under writeback we cannot touch the
1591 * CoW fork. Leave it alone if we're in the midst of a directio.
1593 if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
1594 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
1595 mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
1596 atomic_read(&VFS_I(ip)->i_dio_count))
1603 * Automatic CoW Reservation Freeing
1605 * These functions automatically garbage collect leftover CoW reservations
1606 * that were made on behalf of a cowextsize hint when we start to run out
1607 * of quota or when the reservations sit around for too long. If the file
1608 * has dirty pages or is undergoing writeback, its CoW reservations will
1611 * The actual garbage collection piggybacks off the same code that runs
1612 * the speculative EOF preallocation garbage collector.
1615 xfs_inode_free_cowblocks(
1616 struct xfs_inode *ip,
1619 struct xfs_eofblocks *eofb = args;
1622 if (!xfs_prep_free_cowblocks(ip))
1625 if (!xfs_inode_matches_eofb(ip, eofb))
1628 /* Free the CoW blocks */
1629 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1630 xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
1633 * Check again, nobody else should be able to dirty blocks or change
1634 * the reflink iflag now that we have the first two locks held.
1636 if (xfs_prep_free_cowblocks(ip))
1637 ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
1639 xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
1640 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1646 xfs_icache_free_cowblocks(
1647 struct xfs_mount *mp,
1648 struct xfs_eofblocks *eofb)
1650 return xfs_inode_walk(mp, 0, xfs_inode_free_cowblocks, eofb,
1651 XFS_ICI_COWBLOCKS_TAG);
1655 xfs_inode_free_quota_cowblocks(
1656 struct xfs_inode *ip)
1658 return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks);
1662 xfs_inode_set_cowblocks_tag(
1665 trace_xfs_inode_set_cowblocks_tag(ip);
1666 return __xfs_inode_set_blocks_tag(ip, xfs_queue_cowblocks,
1667 trace_xfs_perag_set_cowblocks,
1668 XFS_ICI_COWBLOCKS_TAG);
1672 xfs_inode_clear_cowblocks_tag(
1675 trace_xfs_inode_clear_cowblocks_tag(ip);
1676 return __xfs_inode_clear_blocks_tag(ip,
1677 trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG);
1680 /* Disable post-EOF and CoW block auto-reclamation. */
1682 xfs_stop_block_reaping(
1683 struct xfs_mount *mp)
1685 cancel_delayed_work_sync(&mp->m_eofblocks_work);
1686 cancel_delayed_work_sync(&mp->m_cowblocks_work);
1689 /* Enable post-EOF and CoW block auto-reclamation. */
1691 xfs_start_block_reaping(
1692 struct xfs_mount *mp)
1694 xfs_queue_eofblocks(mp);
1695 xfs_queue_cowblocks(mp);