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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans.h" |
| 26 | #include "xfs_sb.h" |
| 27 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dir2.h" |
| 29 | #include "xfs_dmapi.h" |
| 30 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_dinode.h" |
| 37 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 38 | #include "xfs_btree.h" |
| 39 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_quota.h" |
| 41 | #include "xfs_utils.h" |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 42 | #include "xfs_trans_priv.h" |
| 43 | #include "xfs_inode_item.h" |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 44 | #include "xfs_bmap.h" |
| 45 | #include "xfs_btree_trace.h" |
| 46 | #include "xfs_dir2_trace.h" |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | * Allocate and initialise an xfs_inode. |
| 51 | */ |
| 52 | STATIC struct xfs_inode * |
| 53 | xfs_inode_alloc( |
| 54 | struct xfs_mount *mp, |
| 55 | xfs_ino_t ino) |
| 56 | { |
| 57 | struct xfs_inode *ip; |
| 58 | |
| 59 | /* |
| 60 | * if this didn't occur in transactions, we could use |
| 61 | * KM_MAYFAIL and return NULL here on ENOMEM. Set the |
| 62 | * code up to do this anyway. |
| 63 | */ |
| 64 | ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP); |
| 65 | if (!ip) |
| 66 | return NULL; |
Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 67 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
| 68 | kmem_zone_free(xfs_inode_zone, ip); |
| 69 | return NULL; |
| 70 | } |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 71 | |
| 72 | ASSERT(atomic_read(&ip->i_iocount) == 0); |
| 73 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 74 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
| 75 | ASSERT(completion_done(&ip->i_flush)); |
| 76 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 77 | /* initialise the xfs inode */ |
| 78 | ip->i_ino = ino; |
| 79 | ip->i_mount = mp; |
| 80 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); |
| 81 | ip->i_afp = NULL; |
| 82 | memset(&ip->i_df, 0, sizeof(xfs_ifork_t)); |
| 83 | ip->i_flags = 0; |
| 84 | ip->i_update_core = 0; |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 85 | ip->i_delayed_blks = 0; |
| 86 | memset(&ip->i_d, 0, sizeof(xfs_icdinode_t)); |
| 87 | ip->i_size = 0; |
| 88 | ip->i_new_size = 0; |
| 89 | |
| 90 | /* |
| 91 | * Initialize inode's trace buffers. |
| 92 | */ |
| 93 | #ifdef XFS_INODE_TRACE |
| 94 | ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS); |
| 95 | #endif |
| 96 | #ifdef XFS_BMAP_TRACE |
| 97 | ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS); |
| 98 | #endif |
| 99 | #ifdef XFS_BTREE_TRACE |
| 100 | ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS); |
| 101 | #endif |
| 102 | #ifdef XFS_RW_TRACE |
| 103 | ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS); |
| 104 | #endif |
| 105 | #ifdef XFS_ILOCK_TRACE |
| 106 | ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS); |
| 107 | #endif |
| 108 | #ifdef XFS_DIR2_TRACE |
| 109 | ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS); |
| 110 | #endif |
Dave Chinner | 705db3f | 2009-04-06 18:40:17 +0200 | [diff] [blame] | 111 | |
| 112 | /* prevent anyone from using this yet */ |
| 113 | VFS_I(ip)->i_state = I_NEW|I_LOCK; |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 114 | |
| 115 | return ip; |
| 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 118 | STATIC void |
| 119 | xfs_inode_free( |
| 120 | struct xfs_inode *ip) |
| 121 | { |
| 122 | switch (ip->i_d.di_mode & S_IFMT) { |
| 123 | case S_IFREG: |
| 124 | case S_IFDIR: |
| 125 | case S_IFLNK: |
| 126 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | if (ip->i_afp) |
| 131 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 132 | |
| 133 | #ifdef XFS_INODE_TRACE |
| 134 | ktrace_free(ip->i_trace); |
| 135 | #endif |
| 136 | #ifdef XFS_BMAP_TRACE |
| 137 | ktrace_free(ip->i_xtrace); |
| 138 | #endif |
| 139 | #ifdef XFS_BTREE_TRACE |
| 140 | ktrace_free(ip->i_btrace); |
| 141 | #endif |
| 142 | #ifdef XFS_RW_TRACE |
| 143 | ktrace_free(ip->i_rwtrace); |
| 144 | #endif |
| 145 | #ifdef XFS_ILOCK_TRACE |
| 146 | ktrace_free(ip->i_lock_trace); |
| 147 | #endif |
| 148 | #ifdef XFS_DIR2_TRACE |
| 149 | ktrace_free(ip->i_dir_trace); |
| 150 | #endif |
| 151 | |
| 152 | if (ip->i_itemp) { |
| 153 | /* |
| 154 | * Only if we are shutting down the fs will we see an |
| 155 | * inode still in the AIL. If it is there, we should remove |
| 156 | * it to prevent a use-after-free from occurring. |
| 157 | */ |
| 158 | xfs_log_item_t *lip = &ip->i_itemp->ili_item; |
| 159 | struct xfs_ail *ailp = lip->li_ailp; |
| 160 | |
| 161 | ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) || |
| 162 | XFS_FORCED_SHUTDOWN(ip->i_mount)); |
| 163 | if (lip->li_flags & XFS_LI_IN_AIL) { |
| 164 | spin_lock(&ailp->xa_lock); |
| 165 | if (lip->li_flags & XFS_LI_IN_AIL) |
| 166 | xfs_trans_ail_delete(ailp, lip); |
| 167 | else |
| 168 | spin_unlock(&ailp->xa_lock); |
| 169 | } |
| 170 | xfs_inode_item_destroy(ip); |
| 171 | ip->i_itemp = NULL; |
| 172 | } |
| 173 | |
| 174 | /* asserts to verify all state is correct here */ |
| 175 | ASSERT(atomic_read(&ip->i_iocount) == 0); |
| 176 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 177 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
| 178 | ASSERT(completion_done(&ip->i_flush)); |
| 179 | |
| 180 | kmem_zone_free(xfs_inode_zone, ip); |
| 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 184 | * Check the validity of the inode we just found it the cache |
| 185 | */ |
| 186 | static int |
| 187 | xfs_iget_cache_hit( |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 188 | struct xfs_perag *pag, |
| 189 | struct xfs_inode *ip, |
| 190 | int flags, |
| 191 | int lock_flags) __releases(pag->pag_ici_lock) |
| 192 | { |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 193 | struct inode *inode = VFS_I(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 194 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 195 | int error; |
| 196 | |
| 197 | spin_lock(&ip->i_flags_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 198 | |
| 199 | /* |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 200 | * If we are racing with another cache hit that is currently |
| 201 | * instantiating this inode or currently recycling it out of |
| 202 | * reclaimabe state, wait for the initialisation to complete |
| 203 | * before continuing. |
| 204 | * |
| 205 | * XXX(hch): eventually we should do something equivalent to |
| 206 | * wait_on_inode to wait for these flags to be cleared |
| 207 | * instead of polling for it. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 208 | */ |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 209 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 210 | XFS_STATS_INC(xs_ig_frecycle); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 211 | error = EAGAIN; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 212 | goto out_error; |
| 213 | } |
| 214 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 215 | /* |
| 216 | * If lookup is racing with unlink return an error immediately. |
| 217 | */ |
| 218 | if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) { |
| 219 | error = ENOENT; |
| 220 | goto out_error; |
| 221 | } |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 222 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 223 | /* |
| 224 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. |
| 225 | * Need to carefully get it back into useable state. |
| 226 | */ |
| 227 | if (ip->i_flags & XFS_IRECLAIMABLE) { |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 228 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); |
| 229 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 230 | /* |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 231 | * We need to set XFS_INEW atomically with clearing the |
| 232 | * reclaimable tag so that we do have an indicator of the |
| 233 | * inode still being initialized. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 234 | */ |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 235 | ip->i_flags |= XFS_INEW; |
| 236 | ip->i_flags &= ~XFS_IRECLAIMABLE; |
| 237 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); |
| 238 | |
| 239 | spin_unlock(&ip->i_flags_lock); |
| 240 | read_unlock(&pag->pag_ici_lock); |
| 241 | |
| 242 | error = -inode_init_always(mp->m_super, inode); |
| 243 | if (error) { |
| 244 | /* |
| 245 | * Re-initializing the inode failed, and we are in deep |
| 246 | * trouble. Try to re-add it to the reclaim list. |
| 247 | */ |
| 248 | read_lock(&pag->pag_ici_lock); |
| 249 | spin_lock(&ip->i_flags_lock); |
| 250 | |
| 251 | ip->i_flags &= ~XFS_INEW; |
| 252 | ip->i_flags |= XFS_IRECLAIMABLE; |
| 253 | __xfs_inode_set_reclaim_tag(pag, ip); |
| 254 | goto out_error; |
| 255 | } |
| 256 | inode->i_state = I_LOCK|I_NEW; |
| 257 | } else { |
| 258 | /* If the VFS inode is being torn down, pause and try again. */ |
| 259 | if (!igrab(inode)) { |
| 260 | error = EAGAIN; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 261 | goto out_error; |
| 262 | } |
David Chinner | 6bfb3d0 | 2008-10-30 18:32:43 +1100 | [diff] [blame] | 263 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 264 | /* We've got a live one. */ |
| 265 | spin_unlock(&ip->i_flags_lock); |
| 266 | read_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 267 | } |
| 268 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 269 | if (lock_flags != 0) |
| 270 | xfs_ilock(ip, lock_flags); |
| 271 | |
| 272 | xfs_iflags_clear(ip, XFS_ISTALE); |
| 273 | xfs_itrace_exit_tag(ip, "xfs_iget.found"); |
| 274 | XFS_STATS_INC(xs_ig_found); |
| 275 | return 0; |
| 276 | |
| 277 | out_error: |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 278 | spin_unlock(&ip->i_flags_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 279 | read_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 280 | return error; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | static int |
| 285 | xfs_iget_cache_miss( |
| 286 | struct xfs_mount *mp, |
| 287 | struct xfs_perag *pag, |
| 288 | xfs_trans_t *tp, |
| 289 | xfs_ino_t ino, |
| 290 | struct xfs_inode **ipp, |
| 291 | xfs_daddr_t bno, |
| 292 | int flags, |
| 293 | int lock_flags) __releases(pag->pag_ici_lock) |
| 294 | { |
| 295 | struct xfs_inode *ip; |
| 296 | int error; |
| 297 | unsigned long first_index, mask; |
| 298 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
| 299 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 300 | ip = xfs_inode_alloc(mp, ino); |
| 301 | if (!ip) |
| 302 | return ENOMEM; |
| 303 | |
| 304 | error = xfs_iread(mp, tp, ip, bno, flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 305 | if (error) |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 306 | goto out_destroy; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 307 | |
| 308 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); |
| 309 | |
| 310 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
| 311 | error = ENOENT; |
| 312 | goto out_destroy; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Preload the radix tree so we can insert safely under the |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 317 | * write spinlock. Note that we cannot sleep inside the preload |
| 318 | * region. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 319 | */ |
| 320 | if (radix_tree_preload(GFP_KERNEL)) { |
| 321 | error = EAGAIN; |
Christoph Hellwig | ed93ec3 | 2009-03-03 14:48:35 -0500 | [diff] [blame] | 322 | goto out_destroy; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Because the inode hasn't been added to the radix-tree yet it can't |
| 327 | * be found by another thread, so we can do the non-sleeping lock here. |
| 328 | */ |
| 329 | if (lock_flags) { |
| 330 | if (!xfs_ilock_nowait(ip, lock_flags)) |
| 331 | BUG(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 332 | } |
| 333 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 334 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
| 335 | first_index = agino & mask; |
| 336 | write_lock(&pag->pag_ici_lock); |
| 337 | |
| 338 | /* insert the new inode */ |
| 339 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
| 340 | if (unlikely(error)) { |
| 341 | WARN_ON(error != -EEXIST); |
| 342 | XFS_STATS_INC(xs_ig_dup); |
| 343 | error = EAGAIN; |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 344 | goto out_preload_end; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* These values _must_ be set before releasing the radix tree lock! */ |
| 348 | ip->i_udquot = ip->i_gdquot = NULL; |
| 349 | xfs_iflags_set(ip, XFS_INEW); |
| 350 | |
| 351 | write_unlock(&pag->pag_ici_lock); |
| 352 | radix_tree_preload_end(); |
| 353 | *ipp = ip; |
| 354 | return 0; |
| 355 | |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 356 | out_preload_end: |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 357 | write_unlock(&pag->pag_ici_lock); |
| 358 | radix_tree_preload_end(); |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 359 | if (lock_flags) |
| 360 | xfs_iunlock(ip, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 361 | out_destroy: |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 362 | __destroy_inode(VFS_I(ip)); |
| 363 | xfs_inode_free(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 364 | return error; |
| 365 | } |
| 366 | |
| 367 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | * Look up an inode by number in the given file system. |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 369 | * The inode is looked up in the cache held in each AG. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 370 | * If the inode is found in the cache, initialise the vfs inode |
| 371 | * if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | * |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 373 | * 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] | 374 | * add it to the cache and initialise the vfs inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | * |
| 376 | * The inode is locked according to the value of the lock_flags parameter. |
| 377 | * This flag parameter indicates how and if the inode's IO lock and inode lock |
| 378 | * should be taken. |
| 379 | * |
| 380 | * mp -- the mount point structure for the current file system. It points |
| 381 | * to the inode hash table. |
| 382 | * tp -- a pointer to the current transaction if there is one. This is |
| 383 | * simply passed through to the xfs_iread() call. |
| 384 | * ino -- the number of the inode desired. This is the unique identifier |
| 385 | * within the file system for the inode being requested. |
| 386 | * lock_flags -- flags indicating how to lock the inode. See the comment |
| 387 | * for xfs_ilock() for a list of valid values. |
| 388 | * bno -- the block number starting the buffer containing the inode, |
| 389 | * if known (as by bulkstat), else 0. |
| 390 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 391 | int |
| 392 | xfs_iget( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | xfs_mount_t *mp, |
| 394 | xfs_trans_t *tp, |
| 395 | xfs_ino_t ino, |
| 396 | uint flags, |
| 397 | uint lock_flags, |
| 398 | xfs_inode_t **ipp, |
| 399 | xfs_daddr_t bno) |
| 400 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | int error; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 403 | xfs_perag_t *pag; |
| 404 | xfs_agino_t agino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 406 | /* the radix tree exists only in inode capable AGs */ |
| 407 | if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi) |
| 408 | return EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 410 | /* get the perag structure and ensure that it's inode capable */ |
| 411 | pag = xfs_get_perag(mp, ino); |
| 412 | if (!pag->pagi_inodeok) |
| 413 | return EINVAL; |
| 414 | ASSERT(pag->pag_ici_init); |
| 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; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 419 | read_lock(&pag->pag_ici_lock); |
| 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) { |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 423 | error = xfs_iget_cache_hit(pag, ip, 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 { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 427 | read_unlock(&pag->pag_ici_lock); |
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 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 430 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno, |
| 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 | } |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 435 | xfs_put_perag(mp, 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 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 439 | ASSERT(ip->i_df.if_ext_max == |
| 440 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t)); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 441 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | * If we have a real type for an on-disk inode, we can set ops(&unlock) |
| 443 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
| 444 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 445 | 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] | 446 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return 0; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 448 | |
| 449 | out_error_or_again: |
| 450 | if (error == EAGAIN) { |
| 451 | delay(1); |
| 452 | goto again; |
| 453 | } |
| 454 | xfs_put_perag(mp, pag); |
| 455 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | /* |
| 459 | * Decrement reference count of an inode structure and unlock it. |
| 460 | * |
| 461 | * ip -- the inode being released |
| 462 | * lock_flags -- this parameter indicates the inode's locks to be |
| 463 | * to be released. See the comment on xfs_iunlock() for a list |
| 464 | * of valid values. |
| 465 | */ |
| 466 | void |
| 467 | xfs_iput(xfs_inode_t *ip, |
| 468 | uint lock_flags) |
| 469 | { |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 470 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | xfs_iunlock(ip, lock_flags); |
Christoph Hellwig | 10090be | 2007-10-11 18:11:03 +1000 | [diff] [blame] | 472 | IRELE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* |
| 476 | * Special iput for brand-new inodes that are still locked |
| 477 | */ |
| 478 | void |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 479 | xfs_iput_new( |
| 480 | xfs_inode_t *ip, |
| 481 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 483 | struct inode *inode = VFS_I(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 485 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | if ((ip->i_d.di_mode == 0)) { |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 488 | ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE)); |
Christoph Hellwig | 10090be | 2007-10-11 18:11:03 +1000 | [diff] [blame] | 489 | make_bad_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | if (inode->i_state & I_NEW) |
| 492 | unlock_new_inode(inode); |
| 493 | if (lock_flags) |
| 494 | xfs_iunlock(ip, lock_flags); |
Christoph Hellwig | 10090be | 2007-10-11 18:11:03 +1000 | [diff] [blame] | 495 | IRELE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | /* |
Christoph Hellwig | 5cafdeb | 2008-12-03 12:20:25 +0100 | [diff] [blame] | 499 | * This is called free all the memory associated with an inode. |
| 500 | * It must free the inode itself and any buffers allocated for |
| 501 | * if_extents/if_data and if_broot. It must also free the lock |
| 502 | * associated with the inode. |
| 503 | * |
| 504 | * Note: because we don't initialise everything on reallocation out |
| 505 | * of the zone, we must ensure we nullify everything correctly before |
| 506 | * freeing the structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | */ |
| 508 | void |
Christoph Hellwig | 5cafdeb | 2008-12-03 12:20:25 +0100 | [diff] [blame] | 509 | xfs_ireclaim( |
| 510 | struct xfs_inode *ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
Christoph Hellwig | 5cafdeb | 2008-12-03 12:20:25 +0100 | [diff] [blame] | 512 | struct xfs_mount *mp = ip->i_mount; |
| 513 | struct xfs_perag *pag; |
| 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | XFS_STATS_INC(xs_ig_reclaims); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /* |
Christoph Hellwig | 5cafdeb | 2008-12-03 12:20:25 +0100 | [diff] [blame] | 518 | * Remove the inode from the per-AG radix tree. It doesn't matter |
| 519 | * if it was never added to it because radix_tree_delete can deal |
| 520 | * with that case just fine. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | */ |
Christoph Hellwig | 5cafdeb | 2008-12-03 12:20:25 +0100 | [diff] [blame] | 522 | pag = xfs_get_perag(mp, ip->i_ino); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 523 | write_lock(&pag->pag_ici_lock); |
| 524 | radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino)); |
| 525 | write_unlock(&pag->pag_ici_lock); |
| 526 | xfs_put_perag(mp, pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Christoph Hellwig | 5cafdeb | 2008-12-03 12:20:25 +0100 | [diff] [blame] | 528 | /* |
| 529 | * Here we do an (almost) spurious inode lock in order to coordinate |
| 530 | * with inode cache radix tree lookups. This is because the lookup |
| 531 | * can reference the inodes in the cache without taking references. |
| 532 | * |
| 533 | * We make that OK here by ensuring that we wait until the inode is |
| 534 | * unlocked after the lookup before we go ahead and free it. We get |
| 535 | * both the ilock and the iolock because the code may need to drop the |
| 536 | * ilock one but will still hold the iolock. |
| 537 | */ |
| 538 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 539 | xfs_qm_dqdetach(ip); |
Christoph Hellwig | 5cafdeb | 2008-12-03 12:20:25 +0100 | [diff] [blame] | 540 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 541 | |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 542 | xfs_inode_free(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
| 546 | * This is a wrapper routine around the xfs_ilock() routine |
| 547 | * used to centralize some grungy code. It is used in places |
| 548 | * that wish to lock the inode solely for reading the extents. |
| 549 | * The reason these places can't just call xfs_ilock(SHARED) |
| 550 | * is that the inode lock also guards to bringing in of the |
| 551 | * extents from disk for a file in b-tree format. If the inode |
| 552 | * is in b-tree format, then we need to lock the inode exclusively |
| 553 | * until the extents are read in. Locking it exclusively all |
| 554 | * the time would limit our parallelism unnecessarily, though. |
| 555 | * What we do instead is check to see if the extents have been |
| 556 | * read in yet, and only lock the inode exclusively if they |
| 557 | * have not. |
| 558 | * |
| 559 | * The function returns a value which should be given to the |
| 560 | * corresponding xfs_iunlock_map_shared(). This value is |
| 561 | * the mode in which the lock was actually taken. |
| 562 | */ |
| 563 | uint |
| 564 | xfs_ilock_map_shared( |
| 565 | xfs_inode_t *ip) |
| 566 | { |
| 567 | uint lock_mode; |
| 568 | |
| 569 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && |
| 570 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { |
| 571 | lock_mode = XFS_ILOCK_EXCL; |
| 572 | } else { |
| 573 | lock_mode = XFS_ILOCK_SHARED; |
| 574 | } |
| 575 | |
| 576 | xfs_ilock(ip, lock_mode); |
| 577 | |
| 578 | return lock_mode; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). |
| 583 | * All it does is call xfs_iunlock() with the given lock_mode. |
| 584 | */ |
| 585 | void |
| 586 | xfs_iunlock_map_shared( |
| 587 | xfs_inode_t *ip, |
| 588 | unsigned int lock_mode) |
| 589 | { |
| 590 | xfs_iunlock(ip, lock_mode); |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * The xfs inode contains 2 locks: a multi-reader lock called the |
| 595 | * i_iolock and a multi-reader lock called the i_lock. This routine |
| 596 | * allows either or both of the locks to be obtained. |
| 597 | * |
| 598 | * The 2 locks should always be ordered so that the IO lock is |
| 599 | * obtained first in order to prevent deadlock. |
| 600 | * |
| 601 | * ip -- the inode being locked |
| 602 | * lock_flags -- this parameter indicates the inode's locks |
| 603 | * to be locked. It can be: |
| 604 | * XFS_IOLOCK_SHARED, |
| 605 | * XFS_IOLOCK_EXCL, |
| 606 | * XFS_ILOCK_SHARED, |
| 607 | * XFS_ILOCK_EXCL, |
| 608 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, |
| 609 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, |
| 610 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, |
| 611 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
| 612 | */ |
| 613 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 614 | xfs_ilock( |
| 615 | xfs_inode_t *ip, |
| 616 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
| 618 | /* |
| 619 | * You can't set both SHARED and EXCL for the same lock, |
| 620 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 621 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 622 | */ |
| 623 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 624 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 625 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 626 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 627 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 629 | if (lock_flags & XFS_IOLOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 630 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 631 | else if (lock_flags & XFS_IOLOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 632 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 633 | |
| 634 | if (lock_flags & XFS_ILOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 635 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 636 | else if (lock_flags & XFS_ILOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 637 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * This is just like xfs_ilock(), except that the caller |
| 644 | * is guaranteed not to sleep. It returns 1 if it gets |
| 645 | * the requested locks and 0 otherwise. If the IO lock is |
| 646 | * obtained but the inode lock cannot be, then the IO lock |
| 647 | * is dropped before returning. |
| 648 | * |
| 649 | * ip -- the inode being locked |
| 650 | * lock_flags -- this parameter indicates the inode's locks to be |
| 651 | * to be locked. See the comment for xfs_ilock() for a list |
| 652 | * of valid values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | */ |
| 654 | int |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 655 | xfs_ilock_nowait( |
| 656 | xfs_inode_t *ip, |
| 657 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | /* |
| 660 | * You can't set both SHARED and EXCL for the same lock, |
| 661 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 662 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 663 | */ |
| 664 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 665 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 666 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 667 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 668 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (lock_flags & XFS_IOLOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 671 | if (!mrtryupdate(&ip->i_iolock)) |
| 672 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 674 | if (!mrtryaccess(&ip->i_iolock)) |
| 675 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | if (lock_flags & XFS_ILOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 678 | if (!mrtryupdate(&ip->i_lock)) |
| 679 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 681 | if (!mrtryaccess(&ip->i_lock)) |
| 682 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address); |
| 685 | return 1; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 686 | |
| 687 | out_undo_iolock: |
| 688 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 689 | mrunlock_excl(&ip->i_iolock); |
| 690 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 691 | mrunlock_shared(&ip->i_iolock); |
| 692 | out: |
| 693 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* |
| 697 | * xfs_iunlock() is used to drop the inode locks acquired with |
| 698 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass |
| 699 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so |
| 700 | * that we know which locks to drop. |
| 701 | * |
| 702 | * ip -- the inode being unlocked |
| 703 | * lock_flags -- this parameter indicates the inode's locks to be |
| 704 | * to be unlocked. See the comment for xfs_ilock() for a list |
| 705 | * of valid values for this parameter. |
| 706 | * |
| 707 | */ |
| 708 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 709 | xfs_iunlock( |
| 710 | xfs_inode_t *ip, |
| 711 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | { |
| 713 | /* |
| 714 | * You can't set both SHARED and EXCL for the same lock, |
| 715 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 716 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 717 | */ |
| 718 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 719 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 720 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 721 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 722 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY | |
| 723 | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | ASSERT(lock_flags != 0); |
| 725 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 726 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 727 | mrunlock_excl(&ip->i_iolock); |
| 728 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 729 | mrunlock_shared(&ip->i_iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 731 | if (lock_flags & XFS_ILOCK_EXCL) |
| 732 | mrunlock_excl(&ip->i_lock); |
| 733 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 734 | mrunlock_shared(&ip->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 736 | if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) && |
| 737 | !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | /* |
| 739 | * Let the AIL know that this item has been unlocked in case |
| 740 | * it is in the AIL and anyone is waiting on it. Don't do |
| 741 | * this if the caller has asked us not to. |
| 742 | */ |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 743 | xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp, |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 744 | (xfs_log_item_t*)(ip->i_itemp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address); |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * give up write locks. the i/o lock cannot be held nested |
| 751 | * if it is being demoted. |
| 752 | */ |
| 753 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 754 | xfs_ilock_demote( |
| 755 | xfs_inode_t *ip, |
| 756 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | { |
| 758 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); |
| 759 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); |
| 760 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 761 | if (lock_flags & XFS_ILOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | mrdemote(&ip->i_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 763 | if (lock_flags & XFS_IOLOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | mrdemote(&ip->i_iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 767 | #ifdef DEBUG |
| 768 | /* |
| 769 | * Debug-only routine, without additional rw_semaphore APIs, we can |
| 770 | * now only answer requests regarding whether we hold the lock for write |
| 771 | * (reader state is outside our visibility, we only track writer state). |
| 772 | * |
| 773 | * Note: this means !xfs_isilocked would give false positives, so don't do that. |
| 774 | */ |
| 775 | int |
| 776 | xfs_isilocked( |
| 777 | xfs_inode_t *ip, |
| 778 | uint lock_flags) |
| 779 | { |
| 780 | if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) == |
| 781 | XFS_ILOCK_EXCL) { |
| 782 | if (!ip->i_lock.mr_writer) |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) == |
| 787 | XFS_IOLOCK_EXCL) { |
| 788 | if (!ip->i_iolock.mr_writer) |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | return 1; |
| 793 | } |
| 794 | #endif |
| 795 | |
Christoph Hellwig | 5a8d0f3 | 2008-12-03 12:20:40 +0100 | [diff] [blame] | 796 | #ifdef XFS_INODE_TRACE |
| 797 | |
| 798 | #define KTRACE_ENTER(ip, vk, s, line, ra) \ |
| 799 | ktrace_enter((ip)->i_trace, \ |
| 800 | /* 0 */ (void *)(__psint_t)(vk), \ |
| 801 | /* 1 */ (void *)(s), \ |
| 802 | /* 2 */ (void *)(__psint_t) line, \ |
| 803 | /* 3 */ (void *)(__psint_t)atomic_read(&VFS_I(ip)->i_count), \ |
| 804 | /* 4 */ (void *)(ra), \ |
| 805 | /* 5 */ NULL, \ |
| 806 | /* 6 */ (void *)(__psint_t)current_cpu(), \ |
| 807 | /* 7 */ (void *)(__psint_t)current_pid(), \ |
| 808 | /* 8 */ (void *)__return_address, \ |
| 809 | /* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL) |
| 810 | |
| 811 | /* |
| 812 | * Vnode tracing code. |
| 813 | */ |
| 814 | void |
| 815 | _xfs_itrace_entry(xfs_inode_t *ip, const char *func, inst_t *ra) |
| 816 | { |
| 817 | KTRACE_ENTER(ip, INODE_KTRACE_ENTRY, func, 0, ra); |
| 818 | } |
| 819 | |
| 820 | void |
| 821 | _xfs_itrace_exit(xfs_inode_t *ip, const char *func, inst_t *ra) |
| 822 | { |
| 823 | KTRACE_ENTER(ip, INODE_KTRACE_EXIT, func, 0, ra); |
| 824 | } |
| 825 | |
| 826 | void |
| 827 | xfs_itrace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra) |
| 828 | { |
| 829 | KTRACE_ENTER(ip, INODE_KTRACE_HOLD, file, line, ra); |
| 830 | } |
| 831 | |
| 832 | void |
| 833 | _xfs_itrace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra) |
| 834 | { |
| 835 | KTRACE_ENTER(ip, INODE_KTRACE_REF, file, line, ra); |
| 836 | } |
| 837 | |
| 838 | void |
| 839 | xfs_itrace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra) |
| 840 | { |
| 841 | KTRACE_ENTER(ip, INODE_KTRACE_RELE, file, line, ra); |
| 842 | } |
| 843 | #endif /* XFS_INODE_TRACE */ |