Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 21 | #include "xfs_acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dinode.h" |
| 32 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_btree.h" |
| 34 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_quota.h" |
| 36 | #include "xfs_utils.h" |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 37 | #include "xfs_trans_priv.h" |
| 38 | #include "xfs_inode_item.h" |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 39 | #include "xfs_bmap.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 40 | #include "xfs_trace.h" |
Dave Chinner | 6d8b79c | 2012-10-08 21:56:09 +1100 | [diff] [blame] | 41 | #include "xfs_icache.h" |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 42 | |
| 43 | |
| 44 | /* |
| 45 | * Allocate and initialise an xfs_inode. |
| 46 | */ |
| 47 | STATIC struct xfs_inode * |
| 48 | xfs_inode_alloc( |
| 49 | struct xfs_mount *mp, |
| 50 | xfs_ino_t ino) |
| 51 | { |
| 52 | struct xfs_inode *ip; |
| 53 | |
| 54 | /* |
| 55 | * if this didn't occur in transactions, we could use |
| 56 | * KM_MAYFAIL and return NULL here on ENOMEM. Set the |
| 57 | * code up to do this anyway. |
| 58 | */ |
| 59 | ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP); |
| 60 | if (!ip) |
| 61 | return NULL; |
Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 62 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
| 63 | kmem_zone_free(xfs_inode_zone, ip); |
| 64 | return NULL; |
| 65 | } |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 66 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 67 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 68 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 69 | ASSERT(!xfs_isiflocked(ip)); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 70 | ASSERT(ip->i_ino == 0); |
Christoph Hellwig | 033da48 | 2009-10-19 04:05:26 +0000 | [diff] [blame] | 71 | |
| 72 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 73 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 74 | /* initialise the xfs inode */ |
| 75 | ip->i_ino = ino; |
| 76 | ip->i_mount = mp; |
| 77 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); |
| 78 | ip->i_afp = NULL; |
| 79 | memset(&ip->i_df, 0, sizeof(xfs_ifork_t)); |
| 80 | ip->i_flags = 0; |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 81 | ip->i_delayed_blks = 0; |
| 82 | memset(&ip->i_d, 0, sizeof(xfs_icdinode_t)); |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 83 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 84 | return ip; |
| 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 87 | STATIC void |
| 88 | xfs_inode_free_callback( |
| 89 | struct rcu_head *head) |
| 90 | { |
| 91 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 92 | struct xfs_inode *ip = XFS_I(inode); |
| 93 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 94 | kmem_zone_free(xfs_inode_zone, ip); |
| 95 | } |
| 96 | |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 97 | void |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 98 | xfs_inode_free( |
| 99 | struct xfs_inode *ip) |
| 100 | { |
| 101 | switch (ip->i_d.di_mode & S_IFMT) { |
| 102 | case S_IFREG: |
| 103 | case S_IFDIR: |
| 104 | case S_IFLNK: |
| 105 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | if (ip->i_afp) |
| 110 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 111 | |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 112 | if (ip->i_itemp) { |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 113 | ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL)); |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 114 | xfs_inode_item_destroy(ip); |
| 115 | ip->i_itemp = NULL; |
| 116 | } |
| 117 | |
| 118 | /* asserts to verify all state is correct here */ |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 119 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 120 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 121 | ASSERT(!xfs_isiflocked(ip)); |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 122 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 123 | /* |
| 124 | * Because we use RCU freeing we need to ensure the inode always |
| 125 | * appears to be reclaimed with an invalid inode number when in the |
| 126 | * free state. The ip->i_flags_lock provides the barrier against lookup |
| 127 | * races. |
| 128 | */ |
| 129 | spin_lock(&ip->i_flags_lock); |
| 130 | ip->i_flags = XFS_IRECLAIM; |
| 131 | ip->i_ino = 0; |
| 132 | spin_unlock(&ip->i_flags_lock); |
Alex Elder | 92f1c00 | 2011-01-10 21:35:55 -0600 | [diff] [blame] | 133 | |
| 134 | call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback); |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | /* |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 138 | * Check the validity of the inode we just found it the cache |
| 139 | */ |
| 140 | static int |
| 141 | xfs_iget_cache_hit( |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 142 | struct xfs_perag *pag, |
| 143 | struct xfs_inode *ip, |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 144 | xfs_ino_t ino, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 145 | int flags, |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 146 | int lock_flags) __releases(RCU) |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 147 | { |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 148 | struct inode *inode = VFS_I(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 149 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 150 | int error; |
| 151 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 152 | /* |
| 153 | * check for re-use of an inode within an RCU grace period due to the |
| 154 | * radix tree nodes not being updated yet. We monitor for this by |
| 155 | * setting the inode number to zero before freeing the inode structure. |
| 156 | * If the inode has been reallocated and set up, then the inode number |
| 157 | * will not match, so check for that, too. |
| 158 | */ |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 159 | spin_lock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 160 | if (ip->i_ino != ino) { |
| 161 | trace_xfs_iget_skip(ip); |
| 162 | XFS_STATS_INC(xs_ig_frecycle); |
| 163 | error = EAGAIN; |
| 164 | goto out_error; |
| 165 | } |
| 166 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 167 | |
| 168 | /* |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 169 | * If we are racing with another cache hit that is currently |
| 170 | * instantiating this inode or currently recycling it out of |
| 171 | * reclaimabe state, wait for the initialisation to complete |
| 172 | * before continuing. |
| 173 | * |
| 174 | * XXX(hch): eventually we should do something equivalent to |
| 175 | * wait_on_inode to wait for these flags to be cleared |
| 176 | * instead of polling for it. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 177 | */ |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 178 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 179 | trace_xfs_iget_skip(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 180 | XFS_STATS_INC(xs_ig_frecycle); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 181 | error = EAGAIN; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 182 | goto out_error; |
| 183 | } |
| 184 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 185 | /* |
| 186 | * If lookup is racing with unlink return an error immediately. |
| 187 | */ |
| 188 | if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) { |
| 189 | error = ENOENT; |
| 190 | goto out_error; |
| 191 | } |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 192 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 193 | /* |
| 194 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. |
| 195 | * Need to carefully get it back into useable state. |
| 196 | */ |
| 197 | if (ip->i_flags & XFS_IRECLAIMABLE) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 198 | trace_xfs_iget_reclaim(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 199 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 200 | /* |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 201 | * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode |
| 202 | * from stomping over us while we recycle the inode. We can't |
| 203 | * clear the radix tree reclaimable tag yet as it requires |
| 204 | * pag_ici_lock to be held exclusive. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 205 | */ |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 206 | ip->i_flags |= XFS_IRECLAIM; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 207 | |
| 208 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 209 | rcu_read_unlock(); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 210 | |
| 211 | error = -inode_init_always(mp->m_super, inode); |
| 212 | if (error) { |
| 213 | /* |
| 214 | * Re-initializing the inode failed, and we are in deep |
| 215 | * trouble. Try to re-add it to the reclaim list. |
| 216 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 217 | rcu_read_lock(); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 218 | spin_lock(&ip->i_flags_lock); |
| 219 | |
Dave Chinner | 778e24b | 2011-06-23 01:34:59 +0000 | [diff] [blame] | 220 | ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM); |
| 221 | ASSERT(ip->i_flags & XFS_IRECLAIMABLE); |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 222 | trace_xfs_iget_reclaim_fail(ip); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 223 | goto out_error; |
| 224 | } |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 225 | |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 226 | spin_lock(&pag->pag_ici_lock); |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 227 | spin_lock(&ip->i_flags_lock); |
Dave Chinner | 778e24b | 2011-06-23 01:34:59 +0000 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * Clear the per-lifetime state in the inode as we are now |
| 231 | * effectively a new inode and need to return to the initial |
| 232 | * state before reuse occurs. |
| 233 | */ |
| 234 | ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS; |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 235 | ip->i_flags |= XFS_INEW; |
| 236 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); |
Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 237 | inode->i_state = I_NEW; |
Dave Chinner | dcfcf20 | 2010-12-23 11:57:13 +1100 | [diff] [blame] | 238 | |
| 239 | ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock)); |
| 240 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
Dave Chinner | dcfcf20 | 2010-12-23 11:57:13 +1100 | [diff] [blame] | 241 | |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 242 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 243 | spin_unlock(&pag->pag_ici_lock); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 244 | } else { |
| 245 | /* If the VFS inode is being torn down, pause and try again. */ |
| 246 | if (!igrab(inode)) { |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 247 | trace_xfs_iget_skip(ip); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 248 | error = EAGAIN; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 249 | goto out_error; |
| 250 | } |
David Chinner | 6bfb3d0 | 2008-10-30 18:32:43 +1100 | [diff] [blame] | 251 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 252 | /* We've got a live one. */ |
| 253 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 254 | rcu_read_unlock(); |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 255 | trace_xfs_iget_hit(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 256 | } |
| 257 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 258 | if (lock_flags != 0) |
| 259 | xfs_ilock(ip, lock_flags); |
| 260 | |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 261 | xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 262 | XFS_STATS_INC(xs_ig_found); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 263 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 264 | return 0; |
| 265 | |
| 266 | out_error: |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 267 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 268 | rcu_read_unlock(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 269 | return error; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | static int |
| 274 | xfs_iget_cache_miss( |
| 275 | struct xfs_mount *mp, |
| 276 | struct xfs_perag *pag, |
| 277 | xfs_trans_t *tp, |
| 278 | xfs_ino_t ino, |
| 279 | struct xfs_inode **ipp, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 280 | int flags, |
Christoph Hellwig | 0c3dc2b | 2009-11-14 16:17:23 +0000 | [diff] [blame] | 281 | int lock_flags) |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 282 | { |
| 283 | struct xfs_inode *ip; |
| 284 | int error; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 285 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 286 | int iflags; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 287 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 288 | ip = xfs_inode_alloc(mp, ino); |
| 289 | if (!ip) |
| 290 | return ENOMEM; |
| 291 | |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 292 | error = xfs_iread(mp, tp, ip, flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 293 | if (error) |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 294 | goto out_destroy; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 295 | |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 296 | trace_xfs_iget_miss(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 297 | |
| 298 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
| 299 | error = ENOENT; |
| 300 | goto out_destroy; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Preload the radix tree so we can insert safely under the |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 305 | * write spinlock. Note that we cannot sleep inside the preload |
Peter Watkins | 3ba3160 | 2012-05-07 16:11:37 -0400 | [diff] [blame] | 306 | * region. Since we can be called from transaction context, don't |
| 307 | * recurse into the file system. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 308 | */ |
Peter Watkins | 3ba3160 | 2012-05-07 16:11:37 -0400 | [diff] [blame] | 309 | if (radix_tree_preload(GFP_NOFS)) { |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 310 | error = EAGAIN; |
Christoph Hellwig | ed93ec3 | 2009-03-03 14:48:35 -0500 | [diff] [blame] | 311 | goto out_destroy; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Because the inode hasn't been added to the radix-tree yet it can't |
| 316 | * be found by another thread, so we can do the non-sleeping lock here. |
| 317 | */ |
| 318 | if (lock_flags) { |
| 319 | if (!xfs_ilock_nowait(ip, lock_flags)) |
| 320 | BUG(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 321 | } |
| 322 | |
Dave Chinner | f30d500 | 2012-03-07 04:50:25 +0000 | [diff] [blame] | 323 | /* |
| 324 | * These values must be set before inserting the inode into the radix |
| 325 | * tree as the moment it is inserted a concurrent lookup (allowed by the |
| 326 | * RCU locking mechanism) can find it and that lookup must see that this |
| 327 | * is an inode currently under construction (i.e. that XFS_INEW is set). |
| 328 | * The ip->i_flags_lock that protects the XFS_INEW flag forms the |
| 329 | * memory barrier that ensures this detection works correctly at lookup |
| 330 | * time. |
| 331 | */ |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 332 | iflags = XFS_INEW; |
| 333 | if (flags & XFS_IGET_DONTCACHE) |
| 334 | iflags |= XFS_IDONTCACHE; |
Dave Chinner | f30d500 | 2012-03-07 04:50:25 +0000 | [diff] [blame] | 335 | ip->i_udquot = ip->i_gdquot = NULL; |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 336 | xfs_iflags_set(ip, iflags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 337 | |
| 338 | /* insert the new inode */ |
Dave Chinner | f30d500 | 2012-03-07 04:50:25 +0000 | [diff] [blame] | 339 | spin_lock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 340 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
| 341 | if (unlikely(error)) { |
| 342 | WARN_ON(error != -EEXIST); |
| 343 | XFS_STATS_INC(xs_ig_dup); |
| 344 | error = EAGAIN; |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 345 | goto out_preload_end; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 346 | } |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 347 | spin_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 348 | radix_tree_preload_end(); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 349 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 350 | *ipp = ip; |
| 351 | return 0; |
| 352 | |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 353 | out_preload_end: |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 354 | spin_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 355 | radix_tree_preload_end(); |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 356 | if (lock_flags) |
| 357 | xfs_iunlock(ip, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 358 | out_destroy: |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 359 | __destroy_inode(VFS_I(ip)); |
| 360 | xfs_inode_free(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 361 | return error; |
| 362 | } |
| 363 | |
| 364 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * Look up an inode by number in the given file system. |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 366 | * The inode is looked up in the cache held in each AG. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 367 | * If the inode is found in the cache, initialise the vfs inode |
| 368 | * if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | * |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 370 | * If it is not in core, read it in from the file system's device, |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 371 | * add it to the cache and initialise the vfs inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | * |
| 373 | * The inode is locked according to the value of the lock_flags parameter. |
| 374 | * This flag parameter indicates how and if the inode's IO lock and inode lock |
| 375 | * should be taken. |
| 376 | * |
| 377 | * mp -- the mount point structure for the current file system. It points |
| 378 | * to the inode hash table. |
| 379 | * tp -- a pointer to the current transaction if there is one. This is |
| 380 | * simply passed through to the xfs_iread() call. |
| 381 | * ino -- the number of the inode desired. This is the unique identifier |
| 382 | * within the file system for the inode being requested. |
| 383 | * lock_flags -- flags indicating how to lock the inode. See the comment |
| 384 | * for xfs_ilock() for a list of valid values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 386 | int |
| 387 | xfs_iget( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | xfs_mount_t *mp, |
| 389 | xfs_trans_t *tp, |
| 390 | xfs_ino_t ino, |
| 391 | uint flags, |
| 392 | uint lock_flags, |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 393 | xfs_inode_t **ipp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | int error; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 397 | xfs_perag_t *pag; |
| 398 | xfs_agino_t agino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 400 | /* |
| 401 | * xfs_reclaim_inode() uses the ILOCK to ensure an inode |
| 402 | * doesn't get freed while it's being referenced during a |
| 403 | * radix tree traversal here. It assumes this function |
| 404 | * aqcuires only the ILOCK (and therefore it has no need to |
| 405 | * involve the IOLOCK in this synchronization). |
| 406 | */ |
| 407 | ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0); |
| 408 | |
Christoph Hellwig | d276734 | 2010-10-06 18:31:23 +0000 | [diff] [blame] | 409 | /* reject inode numbers outside existing AGs */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 410 | if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount) |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 411 | return EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 413 | /* get the perag structure and ensure that it's inode capable */ |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 414 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 415 | agino = XFS_INO_TO_AGINO(mp, ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | again: |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 418 | error = 0; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 419 | rcu_read_lock(); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 420 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 422 | if (ip) { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 423 | error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 424 | if (error) |
| 425 | goto out_error_or_again; |
| 426 | } else { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 427 | rcu_read_unlock(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 428 | XFS_STATS_INC(xs_ig_missed); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 429 | |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 430 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 431 | flags, lock_flags); |
| 432 | if (error) |
| 433 | goto out_error_or_again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 435 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | *ipp = ip; |
| 438 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 439 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | * If we have a real type for an on-disk inode, we can set ops(&unlock) |
| 441 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
| 442 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 443 | if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0) |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 444 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return 0; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 446 | |
| 447 | out_error_or_again: |
| 448 | if (error == EAGAIN) { |
| 449 | delay(1); |
| 450 | goto again; |
| 451 | } |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 452 | xfs_perag_put(pag); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 453 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |