^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2000-2005 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "xfs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "xfs_trans_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "xfs_inode_item.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "xfs_quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "xfs_icache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "xfs_bmap_util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "xfs_dquot_item.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "xfs_dquot.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "xfs_reflink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "xfs_ialloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Allocate and initialise an xfs_inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct xfs_inode *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) xfs_inode_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) xfs_ino_t ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct xfs_inode *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * XXX: If this didn't occur in transactions, we could drop GFP_NOFAIL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * and return NULL here on ENOMEM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ip = kmem_cache_alloc(xfs_inode_zone, GFP_KERNEL | __GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (inode_init_always(mp->m_super, VFS_I(ip))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) kmem_cache_free(xfs_inode_zone, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* VFS doesn't initialise i_mode! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) VFS_I(ip)->i_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) XFS_STATS_INC(mp, vn_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ASSERT(atomic_read(&ip->i_pincount) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ASSERT(ip->i_ino == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* initialise the xfs inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ip->i_ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ip->i_mount = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ip->i_afp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ip->i_cowfp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) memset(&ip->i_df, 0, sizeof(ip->i_df));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ip->i_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ip->i_delayed_blks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) memset(&ip->i_d, 0, sizeof(ip->i_d));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ip->i_sick = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ip->i_checked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) INIT_WORK(&ip->i_ioend_work, xfs_end_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) INIT_LIST_HEAD(&ip->i_ioend_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) spin_lock_init(&ip->i_ioend_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) xfs_inode_free_callback(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct inode *inode = container_of(head, struct inode, i_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct xfs_inode *ip = XFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) switch (VFS_I(ip)->i_mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) xfs_idestroy_fork(&ip->i_df);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (ip->i_afp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) xfs_idestroy_fork(ip->i_afp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) kmem_cache_free(xfs_ifork_zone, ip->i_afp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (ip->i_cowfp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) xfs_idestroy_fork(ip->i_cowfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) kmem_cache_free(xfs_ifork_zone, ip->i_cowfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ip->i_itemp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ASSERT(!test_bit(XFS_LI_IN_AIL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) &ip->i_itemp->ili_item.li_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) xfs_inode_item_destroy(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ip->i_itemp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) kmem_cache_free(xfs_inode_zone, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) __xfs_inode_free(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* asserts to verify all state is correct here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ASSERT(atomic_read(&ip->i_pincount) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ASSERT(!ip->i_itemp || list_empty(&ip->i_itemp->ili_item.li_bio_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) XFS_STATS_DEC(ip->i_mount, vn_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) xfs_inode_free(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ASSERT(!xfs_iflags_test(ip, XFS_IFLUSHING));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Because we use RCU freeing we need to ensure the inode always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * appears to be reclaimed with an invalid inode number when in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * free state. The ip->i_flags_lock provides the barrier against lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * races.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ip->i_flags = XFS_IRECLAIM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ip->i_ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) __xfs_inode_free(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Queue background inode reclaim work if there are reclaimable inodes and there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * isn't reclaim work already scheduled or in progress.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) xfs_reclaim_work_queue(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) xfs_perag_set_reclaim_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct xfs_perag *pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct xfs_mount *mp = pag->pag_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) lockdep_assert_held(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (pag->pag_ici_reclaimable++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* propagate the reclaim tag up into the perag radix tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) spin_lock(&mp->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) XFS_ICI_RECLAIM_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) spin_unlock(&mp->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* schedule periodic background inode reclaim */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) xfs_reclaim_work_queue(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) xfs_perag_clear_reclaim_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct xfs_perag *pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct xfs_mount *mp = pag->pag_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) lockdep_assert_held(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (--pag->pag_ici_reclaimable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* clear the reclaim tag from the perag radix tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) spin_lock(&mp->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) XFS_ICI_RECLAIM_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) spin_unlock(&mp->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * We set the inode flag atomically with the radix tree tag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Once we get tag lookups on the radix tree, this inode flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * can go away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) xfs_inode_set_reclaim_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct xfs_mount *mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) spin_lock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) XFS_ICI_RECLAIM_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) xfs_perag_set_reclaim_tag(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) spin_unlock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) xfs_inode_clear_reclaim_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) xfs_ino_t ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) radix_tree_tag_clear(&pag->pag_ici_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) XFS_INO_TO_AGINO(pag->pag_mount, ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) XFS_ICI_RECLAIM_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) xfs_perag_clear_reclaim_tag(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) xfs_inew_wait(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!xfs_iflags_test(ip, XFS_INEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) } while (true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) finish_wait(wq, &wait.wq_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * When we recycle a reclaimable inode, we need to re-initialise the VFS inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * part of the structure. This is made more complex by the fact we store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * information about the on-disk values in the VFS inode and so we can't just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * overwrite the values unconditionally. Hence we save the parameters we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * need to retain across reinitialisation, and rewrite them into the VFS inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * after reinitialisation even if it fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) xfs_reinit_inode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) uint32_t nlink = inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) uint32_t generation = inode->i_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) uint64_t version = inode_peek_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) umode_t mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) dev_t dev = inode->i_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) kuid_t uid = inode->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kgid_t gid = inode->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) error = inode_init_always(mp->m_super, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) set_nlink(inode, nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) inode->i_generation = generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) inode_set_iversion_queried(inode, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) inode->i_rdev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) inode->i_uid = uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) inode->i_gid = gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * If we are allocating a new inode, then check what was returned is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * actually a free, empty inode. If we are not allocating an inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * then check we didn't find a free inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * 0 if the inode free state matches the lookup context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * -ENOENT if the inode is free and we are not allocating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * -EFSCORRUPTED if there is any state mismatch at all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) xfs_iget_check_free_state(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (flags & XFS_IGET_CREATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* should be a free inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (VFS_I(ip)->i_mode != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) xfs_warn(ip->i_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) "Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ip->i_ino, VFS_I(ip)->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (ip->i_d.di_nblocks != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) xfs_warn(ip->i_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) "Corruption detected! Free inode 0x%llx has blocks allocated!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* should be an allocated inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (VFS_I(ip)->i_mode == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * Check the validity of the inode we just found it the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) xfs_iget_cache_hit(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) xfs_ino_t ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int lock_flags) __releases(RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct inode *inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct xfs_mount *mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * check for re-use of an inode within an RCU grace period due to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * radix tree nodes not being updated yet. We monitor for this by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * setting the inode number to zero before freeing the inode structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * If the inode has been reallocated and set up, then the inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * will not match, so check for that, too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (ip->i_ino != ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) trace_xfs_iget_skip(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) XFS_STATS_INC(mp, xs_ig_frecycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) error = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * If we are racing with another cache hit that is currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * instantiating this inode or currently recycling it out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * reclaimabe state, wait for the initialisation to complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * before continuing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * XXX(hch): eventually we should do something equivalent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * wait_on_inode to wait for these flags to be cleared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * instead of polling for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) trace_xfs_iget_skip(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) XFS_STATS_INC(mp, xs_ig_frecycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) error = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * Check the inode free state is valid. This also detects lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * racing with unlinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) error = xfs_iget_check_free_state(ip, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * If IRECLAIMABLE is set, we've torn down the VFS inode already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * Need to carefully get it back into useable state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (ip->i_flags & XFS_IRECLAIMABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) trace_xfs_iget_reclaim(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (flags & XFS_IGET_INCORE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) error = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * from stomping over us while we recycle the inode. We can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * clear the radix tree reclaimable tag yet as it requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * pag_ici_lock to be held exclusive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ip->i_flags |= XFS_IRECLAIM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ASSERT(!rwsem_is_locked(&inode->i_rwsem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) error = xfs_reinit_inode(mp, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) bool wake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * Re-initializing the inode failed, and we are in deep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * trouble. Try to re-add it to the reclaim list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) wake = !!__xfs_iflags_test(ip, XFS_INEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (wake)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) wake_up_bit(&ip->i_flags, __XFS_INEW_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) trace_xfs_iget_reclaim_fail(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) spin_lock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * Clear the per-lifetime state in the inode as we are now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * effectively a new inode and need to return to the initial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * state before reuse occurs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) ip->i_flags |= XFS_INEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) xfs_inode_clear_reclaim_tag(pag, ip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) inode->i_state = I_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ip->i_sick = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) ip->i_checked = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) spin_unlock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /* If the VFS inode is being torn down, pause and try again. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!igrab(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) trace_xfs_iget_skip(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) error = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /* We've got a live one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) trace_xfs_iget_hit(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (lock_flags != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) xfs_ilock(ip, lock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (!(flags & XFS_IGET_INCORE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) xfs_iflags_clear(ip, XFS_ISTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) XFS_STATS_INC(mp, xs_ig_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) xfs_iget_cache_miss(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) xfs_trans_t *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) xfs_ino_t ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct xfs_inode **ipp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct xfs_inode *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) ip = xfs_inode_alloc(mp, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * For version 5 superblocks, if we are initialising a new inode and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * are not utilising the XFS_MOUNT_IKEEP inode cluster mode, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * simply build the new inode core with a random generation number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * For version 4 (and older) superblocks, log recovery is dependent on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * the di_flushiter field being initialised from the current on-disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * value and hence we must also read the inode off disk even when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * initializing new inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (xfs_sb_version_has_v3inode(&mp->m_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) (flags & XFS_IGET_CREATE) && !(mp->m_flags & XFS_MOUNT_IKEEP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) VFS_I(ip)->i_generation = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) struct xfs_dinode *dip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) struct xfs_buf *bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) error = xfs_inode_from_disk(ip, dip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) xfs_buf_set_ref(bp, XFS_INO_REF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) xfs_trans_brelse(tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) trace_xfs_iget_miss(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) * Check the inode free state is valid. This also detects lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * racing with unlinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) error = xfs_iget_check_free_state(ip, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * Preload the radix tree so we can insert safely under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * write spinlock. Note that we cannot sleep inside the preload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * region. Since we can be called from transaction context, don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * recurse into the file system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (radix_tree_preload(GFP_NOFS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) error = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) goto out_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * Because the inode hasn't been added to the radix-tree yet it can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * be found by another thread, so we can do the non-sleeping lock here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (lock_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (!xfs_ilock_nowait(ip, lock_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * These values must be set before inserting the inode into the radix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * tree as the moment it is inserted a concurrent lookup (allowed by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * RCU locking mechanism) can find it and that lookup must see that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * is an inode currently under construction (i.e. that XFS_INEW is set).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * The ip->i_flags_lock that protects the XFS_INEW flag forms the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * memory barrier that ensures this detection works correctly at lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) iflags = XFS_INEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (flags & XFS_IGET_DONTCACHE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) d_mark_dontcache(VFS_I(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) ip->i_udquot = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) ip->i_gdquot = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) ip->i_pdquot = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) xfs_iflags_set(ip, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /* insert the new inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) spin_lock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (unlikely(error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) WARN_ON(error != -EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) XFS_STATS_INC(mp, xs_ig_dup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) error = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) goto out_preload_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) spin_unlock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) radix_tree_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) *ipp = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) out_preload_end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) spin_unlock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) radix_tree_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) xfs_iunlock(ip, lock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) out_destroy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) __destroy_inode(VFS_I(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) xfs_inode_free(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * Look up an inode by number in the given file system. The inode is looked up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * in the cache held in each AG. If the inode is found in the cache, initialise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * the vfs inode if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * If it is not in core, read it in from the file system's device, add it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * cache and initialise the vfs inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * The inode is locked according to the value of the lock_flags parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * Inode lookup is only done during metadata operations and not as part of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * data IO path. Hence we only allow locking of the XFS_ILOCK during lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) xfs_iget(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) xfs_ino_t ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) uint flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) uint lock_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct xfs_inode **ipp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) struct xfs_inode *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) xfs_agino_t agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /* reject inode numbers outside existing AGs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) XFS_STATS_INC(mp, xs_ig_attempts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /* get the perag structure and ensure that it's inode capable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) agino = XFS_INO_TO_AGINO(mp, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) ip = radix_tree_lookup(&pag->pag_ici_root, agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) goto out_error_or_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (flags & XFS_IGET_INCORE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) error = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) goto out_error_or_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) XFS_STATS_INC(mp, xs_ig_missed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) flags, lock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) goto out_error_or_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) *ipp = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * If we have a real type for an on-disk inode, we can setup the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * now. If it's a new inode being created, xfs_ialloc will handle it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) xfs_setup_existing_inode(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) out_error_or_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (!(flags & XFS_IGET_INCORE) && error == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) delay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * "Is this a cached inode that's also allocated?"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * Look up an inode by number in the given file system. If the inode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * in cache and isn't in purgatory, return 1 if the inode is allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * and 0 if it is not. For all other cases (not in cache, being torn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * down, etc.), return a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * The caller has to prevent inode allocation and freeing activity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * presumably by locking the AGI buffer. This is to ensure that an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * inode cannot transition from allocated to freed until the caller is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * ready to allow that. If the inode is in an intermediate state (new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * reclaimable, or being reclaimed), -EAGAIN will be returned; if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * inode is not in the cache, -ENOENT will be returned. The caller must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * deal with these scenarios appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * This is a specialized use case for the online scrubber; if you're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * reading this, you probably want xfs_iget.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) xfs_icache_inode_is_allocated(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) xfs_ino_t ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) bool *inuse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) struct xfs_inode *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) error = xfs_iget(mp, tp, ino, XFS_IGET_INCORE, 0, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) *inuse = !!(VFS_I(ip)->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) xfs_irele(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * The inode lookup is done in batches to keep the amount of lock traffic and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * radix tree lookups to a minimum. The batch size is a trade off between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * lookup reduction and stack usage. This is in the reclaim path, so we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * be too greedy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) #define XFS_LOOKUP_BATCH 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * Decide if the given @ip is eligible to be a part of the inode walk, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * grab it if so. Returns true if it's ready to go or false if we should just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * ignore it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) STATIC bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) xfs_inode_walk_ag_grab(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) struct inode *inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) bool newinos = !!(flags & XFS_INODE_WALK_INEW_WAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) ASSERT(rcu_read_lock_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) /* Check for stale RCU freed inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (!ip->i_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) goto out_unlock_noent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) goto out_unlock_noent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) /* nothing to sync during shutdown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (XFS_FORCED_SHUTDOWN(ip->i_mount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /* If we can't grab the inode, it must on it's way to reclaim. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (!igrab(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) /* inode is valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) out_unlock_noent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * For a given per-AG structure @pag, grab, @execute, and rele all incore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * inodes with the given radix tree @tag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) xfs_inode_walk_ag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct xfs_perag *pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) int iter_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) int (*execute)(struct xfs_inode *ip, void *args),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) void *args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) int tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) struct xfs_mount *mp = pag->pag_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) uint32_t first_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) int last_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) int skipped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) bool done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) int nr_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) skipped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) first_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) nr_found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) struct xfs_inode *batch[XFS_LOOKUP_BATCH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (tag == XFS_ICI_NO_TAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) (void **)batch, first_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) XFS_LOOKUP_BATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) nr_found = radix_tree_gang_lookup_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) &pag->pag_ici_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) (void **) batch, first_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) XFS_LOOKUP_BATCH, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (!nr_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * Grab the inodes before we drop the lock. if we found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * nothing, nr == 0 and the loop will be skipped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) for (i = 0; i < nr_found; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) struct xfs_inode *ip = batch[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (done || !xfs_inode_walk_ag_grab(ip, iter_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) batch[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * Update the index for the next lookup. Catch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * overflows into the next AG range which can occur if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * we have inodes in the last block of the AG and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * are currently pointing to the last inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * Because we may see inodes that are from the wrong AG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * due to RCU freeing and reallocation, only update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) * index if it lies in this AG. It was a race that lead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) * us to see this inode, so another lookup from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * same index will not find it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) /* unlock now we've grabbed the inodes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) for (i = 0; i < nr_found; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (!batch[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if ((iter_flags & XFS_INODE_WALK_INEW_WAIT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) xfs_iflags_test(batch[i], XFS_INEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) xfs_inew_wait(batch[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) error = execute(batch[i], args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) xfs_irele(batch[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (error == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) skipped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (error && last_error != -EFSCORRUPTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) last_error = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) /* bail out if the filesystem is corrupted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) if (error == -EFSCORRUPTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) } while (nr_found && !done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (skipped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) delay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) return last_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) /* Fetch the next (possibly tagged) per-AG structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) static inline struct xfs_perag *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) xfs_inode_walk_get_perag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) xfs_agnumber_t agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) int tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) if (tag == XFS_ICI_NO_TAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) return xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return xfs_perag_get_tag(mp, agno, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * Call the @execute function on all incore inodes matching the radix tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) * @tag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) xfs_inode_walk(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) int iter_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) int (*execute)(struct xfs_inode *ip, void *args),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) void *args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) int tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) int last_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) xfs_agnumber_t ag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) ag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) while ((pag = xfs_inode_walk_get_perag(mp, ag, tag))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ag = pag->pag_agno + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) error = xfs_inode_walk_ag(pag, iter_flags, execute, args, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) last_error = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) if (error == -EFSCORRUPTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return last_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) * Background scanning to trim post-EOF preallocated space. This is queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * based on the 'speculative_prealloc_lifetime' tunable (5m by default).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) xfs_queue_eofblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_EOFBLOCKS_TAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) queue_delayed_work(mp->m_eofblocks_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) &mp->m_eofblocks_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) msecs_to_jiffies(xfs_eofb_secs * 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) xfs_eofblocks_worker(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) struct xfs_mount *mp = container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) struct xfs_mount, m_eofblocks_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (!sb_start_write_trylock(mp->m_super))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) xfs_icache_free_eofblocks(mp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) sb_end_write(mp->m_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) xfs_queue_eofblocks(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * Background scanning to trim preallocated CoW space. This is queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * based on the 'speculative_cow_prealloc_lifetime' tunable (5m by default).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * (We'll just piggyback on the post-EOF prealloc space workqueue.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) xfs_queue_cowblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_COWBLOCKS_TAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) queue_delayed_work(mp->m_eofblocks_workqueue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) &mp->m_cowblocks_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) msecs_to_jiffies(xfs_cowb_secs * 1000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) xfs_cowblocks_worker(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) struct xfs_mount *mp = container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) struct xfs_mount, m_cowblocks_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) if (!sb_start_write_trylock(mp->m_super))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) xfs_icache_free_cowblocks(mp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) sb_end_write(mp->m_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) xfs_queue_cowblocks(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * Grab the inode for reclaim exclusively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * We have found this inode via a lookup under RCU, so the inode may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) * already been freed, or it may be in the process of being recycled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) * xfs_iget(). In both cases, the inode will have XFS_IRECLAIM set. If the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * has been fully recycled by the time we get the i_flags_lock, XFS_IRECLAIMABLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) * will not be set. Hence we need to check for both these flag conditions to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * avoid inodes that are no longer reclaim candidates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) * Note: checking for other state flags here, under the i_flags_lock or not, is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * racy and should be avoided. Those races should be resolved only after we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) * ensured that we are able to reclaim this inode and the world can see that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) * are going to reclaim it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * Return true if we grabbed it, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) xfs_reclaim_inode_grab(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) ASSERT(rcu_read_lock_held());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) __xfs_iflags_test(ip, XFS_IRECLAIM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) /* not a reclaim candidate. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) __xfs_iflags_set(ip, XFS_IRECLAIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * Inode reclaim is non-blocking, so the default action if progress cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) * made is to "requeue" the inode for reclaim by unlocking it and clearing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) * XFS_IRECLAIM flag. If we are in a shutdown state, we don't care about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) * blocking anymore and hence we can wait for the inode to be able to reclaim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) * We do no IO here - if callers require inodes to be cleaned they must push the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * AIL first to trigger writeback of dirty inodes. This enables writeback to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * done in the background in a non-blocking manner, and enables memory reclaim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * to make progress without blocking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) xfs_reclaim_inode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) struct xfs_perag *pag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (xfs_iflags_test_and_set(ip, XFS_IFLUSHING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) goto out_iunlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) xfs_iunpin_wait(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) xfs_iflush_abort(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) goto reclaim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (xfs_ipincount(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) goto out_clear_flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (!xfs_inode_clean(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) goto out_clear_flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) xfs_iflags_clear(ip, XFS_IFLUSHING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) reclaim:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * Because we use RCU freeing we need to ensure the inode always appears
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * to be reclaimed with an invalid inode number when in the free state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * We do this as early as possible under the ILOCK so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) * detect races with us here. By doing this, we guarantee that once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) * it will see either a valid inode that will serialise correctly, or it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * will see an invalid inode that it can skip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) ip->i_flags = XFS_IRECLAIM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) ip->i_ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) XFS_STATS_INC(ip->i_mount, xs_ig_reclaims);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) * Remove the inode from the per-AG radix tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) * Because radix_tree_delete won't complain even if the item was never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * added to the tree assert that it's been there before to catch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) * problems with the inode life time early on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) spin_lock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) if (!radix_tree_delete(&pag->pag_ici_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) XFS_INO_TO_AGINO(ip->i_mount, ino)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) xfs_perag_clear_reclaim_tag(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) spin_unlock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) * Here we do an (almost) spurious inode lock in order to coordinate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * with inode cache radix tree lookups. This is because the lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) * can reference the inodes in the cache without taking references.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) * We make that OK here by ensuring that we wait until the inode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) * unlocked after the lookup before we go ahead and free it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) xfs_qm_dqdetach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) ASSERT(xfs_inode_clean(ip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) __xfs_inode_free(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) out_clear_flush:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) xfs_iflags_clear(ip, XFS_IFLUSHING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) out_iunlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) xfs_iflags_clear(ip, XFS_IRECLAIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) * corrupted, we still want to try to reclaim all the inodes. If we don't,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) * then a shut down during filesystem unmount reclaim walk leak all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) * unreclaimed inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) * Returns non-zero if any AGs or inodes were skipped in the reclaim pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) * so that callers that want to block until all dirty inodes are written back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) * and reclaimed can sanely loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) xfs_reclaim_inodes_ag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) int *nr_to_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) xfs_agnumber_t ag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) unsigned long first_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) int done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) int nr_found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) ag = pag->pag_agno + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) first_index = READ_ONCE(pag->pag_ici_reclaim_cursor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) struct xfs_inode *batch[XFS_LOOKUP_BATCH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) nr_found = radix_tree_gang_lookup_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) &pag->pag_ici_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) (void **)batch, first_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) XFS_LOOKUP_BATCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) XFS_ICI_RECLAIM_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (!nr_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) * Grab the inodes before we drop the lock. if we found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) * nothing, nr == 0 and the loop will be skipped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) for (i = 0; i < nr_found; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) struct xfs_inode *ip = batch[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) if (done || !xfs_reclaim_inode_grab(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) batch[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * Update the index for the next lookup. Catch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * overflows into the next AG range which can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * occur if we have inodes in the last block of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * the AG and we are currently pointing to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) * last inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * Because we may see inodes that are from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * wrong AG due to RCU freeing and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) * reallocation, only update the index if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * lies in this AG. It was a race that lead us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) * to see this inode, so another lookup from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) * the same index will not find it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) if (XFS_INO_TO_AGNO(mp, ip->i_ino) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) pag->pag_agno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) /* unlock now we've grabbed the inodes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) for (i = 0; i < nr_found; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) if (batch[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) xfs_reclaim_inode(batch[i], pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) *nr_to_scan -= XFS_LOOKUP_BATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) } while (nr_found && !done && *nr_to_scan > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) first_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) WRITE_ONCE(pag->pag_ici_reclaim_cursor, first_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) xfs_reclaim_inodes(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) int nr_to_scan = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) while (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) xfs_ail_push_all_sync(mp->m_ail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) xfs_reclaim_inodes_ag(mp, &nr_to_scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) * The shrinker infrastructure determines how many inodes we should scan for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) * reclaim. We want as many clean inodes ready to reclaim as possible, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) * push the AIL here. We also want to proactively free up memory if we can to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) * minimise the amount of work memory reclaim has to do so we kick the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) * background reclaim if it isn't already scheduled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) xfs_reclaim_inodes_nr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) int nr_to_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /* kick background reclaimer and push the AIL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) xfs_reclaim_work_queue(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) xfs_ail_push_all(mp->m_ail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) xfs_reclaim_inodes_ag(mp, &nr_to_scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) * Return the number of reclaimable inodes in the filesystem for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) * the shrinker to determine how much to reclaim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) xfs_reclaim_inodes_count(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) xfs_agnumber_t ag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) int reclaimable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) ag = pag->pag_agno + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) reclaimable += pag->pag_ici_reclaimable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) return reclaimable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) STATIC bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) xfs_inode_match_id(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) struct xfs_eofblocks *eofb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) ip->i_d.di_projid != eofb->eof_prid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) * A union-based inode filtering algorithm. Process the inode if any of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) * criteria match. This is for global/internal scans only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) STATIC bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) xfs_inode_match_id_union(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) struct xfs_eofblocks *eofb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) ip->i_d.di_projid == eofb->eof_prid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) * Is this inode @ip eligible for eof/cow block reclamation, given some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) * filtering parameters @eofb? The inode is eligible if @eofb is null or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) * if the predicate functions match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) xfs_inode_matches_eofb(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) struct xfs_eofblocks *eofb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) bool match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) if (!eofb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) if (eofb->eof_flags & XFS_EOF_FLAGS_UNION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) match = xfs_inode_match_id_union(ip, eofb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) match = xfs_inode_match_id(ip, eofb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) /* skip the inode if the file size is too small */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) if ((eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) XFS_ISIZE(ip) < eofb->eof_min_file_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) * This is a fast pass over the inode cache to try to get reclaim moving on as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) * many inodes as possible in a short period of time. It kicks itself every few
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) * seconds, as well as being kicked by the inode cache shrinker when memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) * goes low.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) xfs_reclaim_worker(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) struct xfs_mount *mp = container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) struct xfs_mount, m_reclaim_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) int nr_to_scan = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) xfs_reclaim_inodes_ag(mp, &nr_to_scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) xfs_reclaim_work_queue(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) xfs_inode_free_eofblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) void *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) struct xfs_eofblocks *eofb = args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) bool wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) if (!xfs_can_free_eofblocks(ip, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) /* inode could be preallocated or append-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) trace_xfs_inode_free_eofblocks_invalid(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) xfs_inode_clear_eofblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) * If the mapping is dirty the operation can block and wait for some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) * time. Unless we are waiting, skip it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) if (!wait && mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) if (!xfs_inode_matches_eofb(ip, eofb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) * If the caller is waiting, return -EAGAIN to keep the background
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) * scanner moving and revisit the inode in a subsequent pass.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) if (wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) ret = xfs_free_eofblocks(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) xfs_iunlock(ip, XFS_IOLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) xfs_icache_free_eofblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) struct xfs_eofblocks *eofb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) return xfs_inode_walk(mp, 0, xfs_inode_free_eofblocks, eofb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) XFS_ICI_EOFBLOCKS_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) * Run eofblocks scans on the quotas applicable to the inode. For inodes with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) * multiple quotas, we don't know exactly which quota caused an allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) * failure. We make a best effort by including each quota under low free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) * conditions (less than 1% free space) in the scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) __xfs_inode_free_quota_eofblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) int (*execute)(struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) struct xfs_eofblocks *eofb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) int scan = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) struct xfs_eofblocks eofb = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) struct xfs_dquot *dq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) * Run a sync scan to increase effectiveness and use the union filter to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) * cover all applicable quotas in a single scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) eofb.eof_flags = XFS_EOF_FLAGS_UNION|XFS_EOF_FLAGS_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) if (XFS_IS_UQUOTA_ENFORCED(ip->i_mount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) dq = xfs_inode_dquot(ip, XFS_DQTYPE_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) if (dq && xfs_dquot_lowsp(dq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) eofb.eof_uid = VFS_I(ip)->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) eofb.eof_flags |= XFS_EOF_FLAGS_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) scan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) if (XFS_IS_GQUOTA_ENFORCED(ip->i_mount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) dq = xfs_inode_dquot(ip, XFS_DQTYPE_GROUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) if (dq && xfs_dquot_lowsp(dq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) eofb.eof_gid = VFS_I(ip)->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) eofb.eof_flags |= XFS_EOF_FLAGS_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) scan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) if (scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) execute(ip->i_mount, &eofb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) xfs_inode_free_quota_eofblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_eofblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) static inline unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) xfs_iflag_for_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) int tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) switch (tag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) case XFS_ICI_EOFBLOCKS_TAG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) return XFS_IEOFBLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) case XFS_ICI_COWBLOCKS_TAG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) return XFS_ICOWBLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) ASSERT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) __xfs_inode_set_blocks_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) xfs_inode_t *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) void (*execute)(struct xfs_mount *mp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) void (*set_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) int error, unsigned long caller_ip),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) int tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) struct xfs_mount *mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) int tagged;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) * Don't bother locking the AG and looking up in the radix trees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) * if we already know that we have the tag set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) if (ip->i_flags & xfs_iflag_for_tag(tag))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) ip->i_flags |= xfs_iflag_for_tag(tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) spin_lock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) tagged = radix_tree_tagged(&pag->pag_ici_root, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) radix_tree_tag_set(&pag->pag_ici_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) if (!tagged) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) /* propagate the eofblocks tag up into the perag radix tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) spin_lock(&ip->i_mount->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) radix_tree_tag_set(&ip->i_mount->m_perag_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) spin_unlock(&ip->i_mount->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) /* kick off background trimming */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) execute(ip->i_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) set_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) spin_unlock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) xfs_inode_set_eofblocks_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) xfs_inode_t *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) trace_xfs_inode_set_eofblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) return __xfs_inode_set_blocks_tag(ip, xfs_queue_eofblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) trace_xfs_perag_set_eofblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) XFS_ICI_EOFBLOCKS_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) __xfs_inode_clear_blocks_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) xfs_inode_t *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) void (*clear_tp)(struct xfs_mount *mp, xfs_agnumber_t agno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) int error, unsigned long caller_ip),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) int tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) struct xfs_mount *mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) struct xfs_perag *pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) spin_lock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) ip->i_flags &= ~xfs_iflag_for_tag(tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) spin_unlock(&ip->i_flags_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) spin_lock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) radix_tree_tag_clear(&pag->pag_ici_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) if (!radix_tree_tagged(&pag->pag_ici_root, tag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) /* clear the eofblocks tag from the perag radix tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) spin_lock(&ip->i_mount->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) spin_unlock(&ip->i_mount->m_perag_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) clear_tp(ip->i_mount, pag->pag_agno, -1, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) spin_unlock(&pag->pag_ici_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) xfs_inode_clear_eofblocks_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) xfs_inode_t *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) trace_xfs_inode_clear_eofblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) return __xfs_inode_clear_blocks_tag(ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) trace_xfs_perag_clear_eofblocks, XFS_ICI_EOFBLOCKS_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) * Set ourselves up to free CoW blocks from this file. If it's already clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) * then we can bail out quickly, but otherwise we must back off if the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) * is undergoing some kind of write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) xfs_prep_free_cowblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) * Just clear the tag if we have an empty cow fork or none at all. It's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) * possible the inode was fully unshared since it was originally tagged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) if (!xfs_inode_has_cow_data(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) trace_xfs_inode_free_cowblocks_invalid(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) xfs_inode_clear_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) * If the mapping is dirty or under writeback we cannot touch the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) * CoW fork. Leave it alone if we're in the midst of a directio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) atomic_read(&VFS_I(ip)->i_dio_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) * Automatic CoW Reservation Freeing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) * These functions automatically garbage collect leftover CoW reservations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) * that were made on behalf of a cowextsize hint when we start to run out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) * of quota or when the reservations sit around for too long. If the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) * has dirty pages or is undergoing writeback, its CoW reservations will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) * be retained.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) * The actual garbage collection piggybacks off the same code that runs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) * the speculative EOF preallocation garbage collector.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) xfs_inode_free_cowblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) struct xfs_inode *ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) void *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) struct xfs_eofblocks *eofb = args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) if (!xfs_prep_free_cowblocks(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) if (!xfs_inode_matches_eofb(ip, eofb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) /* Free the CoW blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) xfs_ilock(ip, XFS_IOLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) * Check again, nobody else should be able to dirty blocks or change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) * the reflink iflag now that we have the first two locks held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (xfs_prep_free_cowblocks(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) xfs_iunlock(ip, XFS_IOLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) xfs_icache_free_cowblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) struct xfs_mount *mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) struct xfs_eofblocks *eofb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) return xfs_inode_walk(mp, 0, xfs_inode_free_cowblocks, eofb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) XFS_ICI_COWBLOCKS_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) xfs_inode_free_quota_cowblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) return __xfs_inode_free_quota_eofblocks(ip, xfs_icache_free_cowblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) xfs_inode_set_cowblocks_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) xfs_inode_t *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) trace_xfs_inode_set_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) return __xfs_inode_set_blocks_tag(ip, xfs_queue_cowblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) trace_xfs_perag_set_cowblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) XFS_ICI_COWBLOCKS_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) xfs_inode_clear_cowblocks_tag(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) xfs_inode_t *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) trace_xfs_inode_clear_cowblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) return __xfs_inode_clear_blocks_tag(ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) trace_xfs_perag_clear_cowblocks, XFS_ICI_COWBLOCKS_TAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) /* Disable post-EOF and CoW block auto-reclamation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) xfs_stop_block_reaping(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) cancel_delayed_work_sync(&mp->m_eofblocks_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) cancel_delayed_work_sync(&mp->m_cowblocks_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) /* Enable post-EOF and CoW block auto-reclamation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) xfs_start_block_reaping(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) struct xfs_mount *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) xfs_queue_eofblocks(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) xfs_queue_cowblocks(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) }