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_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_dinode.h" |
| 33 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_btree.h" |
| 35 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_quota.h" |
| 37 | #include "xfs_utils.h" |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 38 | #include "xfs_trans_priv.h" |
| 39 | #include "xfs_inode_item.h" |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 40 | #include "xfs_bmap.h" |
| 41 | #include "xfs_btree_trace.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 42 | #include "xfs_trace.h" |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 43 | |
| 44 | |
| 45 | /* |
Dave Chinner | dcfcf20 | 2010-12-23 11:57:13 +1100 | [diff] [blame] | 46 | * Define xfs inode iolock lockdep classes. We need to ensure that all active |
| 47 | * inodes are considered the same for lockdep purposes, including inodes that |
| 48 | * are recycled through the XFS_IRECLAIMABLE state. This is the the only way to |
| 49 | * guarantee the locks are considered the same when there are multiple lock |
| 50 | * initialisation siteѕ. Also, define a reclaimable inode class so it is |
| 51 | * obvious in lockdep reports which class the report is against. |
| 52 | */ |
| 53 | static struct lock_class_key xfs_iolock_active; |
| 54 | struct lock_class_key xfs_iolock_reclaimable; |
| 55 | |
| 56 | /* |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 57 | * Allocate and initialise an xfs_inode. |
| 58 | */ |
| 59 | STATIC struct xfs_inode * |
| 60 | xfs_inode_alloc( |
| 61 | struct xfs_mount *mp, |
| 62 | xfs_ino_t ino) |
| 63 | { |
| 64 | struct xfs_inode *ip; |
| 65 | |
| 66 | /* |
| 67 | * if this didn't occur in transactions, we could use |
| 68 | * KM_MAYFAIL and return NULL here on ENOMEM. Set the |
| 69 | * code up to do this anyway. |
| 70 | */ |
| 71 | ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP); |
| 72 | if (!ip) |
| 73 | return NULL; |
Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 74 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
| 75 | kmem_zone_free(xfs_inode_zone, ip); |
| 76 | return NULL; |
| 77 | } |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 78 | |
| 79 | ASSERT(atomic_read(&ip->i_iocount) == 0); |
| 80 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 81 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
| 82 | ASSERT(completion_done(&ip->i_flush)); |
Christoph Hellwig | 033da48 | 2009-10-19 04:05:26 +0000 | [diff] [blame] | 83 | |
| 84 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
Dave Chinner | dcfcf20 | 2010-12-23 11:57:13 +1100 | [diff] [blame] | 85 | lockdep_set_class_and_name(&ip->i_iolock.mr_lock, |
| 86 | &xfs_iolock_active, "xfs_iolock_active"); |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 87 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 88 | /* initialise the xfs inode */ |
| 89 | ip->i_ino = ino; |
| 90 | ip->i_mount = mp; |
| 91 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); |
| 92 | ip->i_afp = NULL; |
| 93 | memset(&ip->i_df, 0, sizeof(xfs_ifork_t)); |
| 94 | ip->i_flags = 0; |
| 95 | ip->i_update_core = 0; |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 96 | ip->i_delayed_blks = 0; |
| 97 | memset(&ip->i_d, 0, sizeof(xfs_icdinode_t)); |
| 98 | ip->i_size = 0; |
| 99 | ip->i_new_size = 0; |
| 100 | |
Dave Chinner | 705db3f | 2009-04-06 18:40:17 +0200 | [diff] [blame] | 101 | /* prevent anyone from using this yet */ |
Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 102 | VFS_I(ip)->i_state = I_NEW; |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 103 | |
| 104 | return ip; |
| 105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 107 | void |
Dave Chinner | d95b7aa | 2010-12-16 16:41:39 +1100 | [diff] [blame^] | 108 | __xfs_inode_free( |
| 109 | struct rcu_head *head) |
| 110 | { |
| 111 | struct inode *inode = container_of((void *)head, |
| 112 | struct inode, i_dentry); |
| 113 | struct xfs_inode *ip = XFS_I(inode); |
| 114 | |
| 115 | INIT_LIST_HEAD(&inode->i_dentry); |
| 116 | kmem_zone_free(xfs_inode_zone, ip); |
| 117 | } |
| 118 | |
| 119 | void |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 120 | xfs_inode_free( |
| 121 | struct xfs_inode *ip) |
| 122 | { |
| 123 | switch (ip->i_d.di_mode & S_IFMT) { |
| 124 | case S_IFREG: |
| 125 | case S_IFDIR: |
| 126 | case S_IFLNK: |
| 127 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | if (ip->i_afp) |
| 132 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 133 | |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 134 | if (ip->i_itemp) { |
| 135 | /* |
| 136 | * Only if we are shutting down the fs will we see an |
| 137 | * inode still in the AIL. If it is there, we should remove |
| 138 | * it to prevent a use-after-free from occurring. |
| 139 | */ |
| 140 | xfs_log_item_t *lip = &ip->i_itemp->ili_item; |
| 141 | struct xfs_ail *ailp = lip->li_ailp; |
| 142 | |
| 143 | ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) || |
| 144 | XFS_FORCED_SHUTDOWN(ip->i_mount)); |
| 145 | if (lip->li_flags & XFS_LI_IN_AIL) { |
| 146 | spin_lock(&ailp->xa_lock); |
| 147 | if (lip->li_flags & XFS_LI_IN_AIL) |
| 148 | xfs_trans_ail_delete(ailp, lip); |
| 149 | else |
| 150 | spin_unlock(&ailp->xa_lock); |
| 151 | } |
| 152 | xfs_inode_item_destroy(ip); |
| 153 | ip->i_itemp = NULL; |
| 154 | } |
| 155 | |
| 156 | /* asserts to verify all state is correct here */ |
| 157 | ASSERT(atomic_read(&ip->i_iocount) == 0); |
| 158 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 159 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
| 160 | ASSERT(completion_done(&ip->i_flush)); |
| 161 | |
Dave Chinner | d95b7aa | 2010-12-16 16:41:39 +1100 | [diff] [blame^] | 162 | call_rcu((struct rcu_head *)&VFS_I(ip)->i_dentry, __xfs_inode_free); |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 163 | } |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 166 | * Check the validity of the inode we just found it the cache |
| 167 | */ |
| 168 | static int |
| 169 | xfs_iget_cache_hit( |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 170 | struct xfs_perag *pag, |
| 171 | struct xfs_inode *ip, |
| 172 | int flags, |
| 173 | int lock_flags) __releases(pag->pag_ici_lock) |
| 174 | { |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 175 | struct inode *inode = VFS_I(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 176 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 177 | int error; |
| 178 | |
| 179 | spin_lock(&ip->i_flags_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 180 | |
| 181 | /* |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 182 | * If we are racing with another cache hit that is currently |
| 183 | * instantiating this inode or currently recycling it out of |
| 184 | * reclaimabe state, wait for the initialisation to complete |
| 185 | * before continuing. |
| 186 | * |
| 187 | * XXX(hch): eventually we should do something equivalent to |
| 188 | * wait_on_inode to wait for these flags to be cleared |
| 189 | * instead of polling for it. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 190 | */ |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 191 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 192 | trace_xfs_iget_skip(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 193 | XFS_STATS_INC(xs_ig_frecycle); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 194 | error = EAGAIN; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 195 | goto out_error; |
| 196 | } |
| 197 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 198 | /* |
| 199 | * If lookup is racing with unlink return an error immediately. |
| 200 | */ |
| 201 | if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) { |
| 202 | error = ENOENT; |
| 203 | goto out_error; |
| 204 | } |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 205 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 206 | /* |
| 207 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. |
| 208 | * Need to carefully get it back into useable state. |
| 209 | */ |
| 210 | if (ip->i_flags & XFS_IRECLAIMABLE) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 211 | trace_xfs_iget_reclaim(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 212 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 213 | /* |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 214 | * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode |
| 215 | * from stomping over us while we recycle the inode. We can't |
| 216 | * clear the radix tree reclaimable tag yet as it requires |
| 217 | * pag_ici_lock to be held exclusive. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 218 | */ |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 219 | ip->i_flags |= XFS_IRECLAIM; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 220 | |
| 221 | spin_unlock(&ip->i_flags_lock); |
| 222 | read_unlock(&pag->pag_ici_lock); |
| 223 | |
| 224 | error = -inode_init_always(mp->m_super, inode); |
| 225 | if (error) { |
| 226 | /* |
| 227 | * Re-initializing the inode failed, and we are in deep |
| 228 | * trouble. Try to re-add it to the reclaim list. |
| 229 | */ |
| 230 | read_lock(&pag->pag_ici_lock); |
| 231 | spin_lock(&ip->i_flags_lock); |
| 232 | |
| 233 | ip->i_flags &= ~XFS_INEW; |
| 234 | ip->i_flags |= XFS_IRECLAIMABLE; |
| 235 | __xfs_inode_set_reclaim_tag(pag, ip); |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 236 | trace_xfs_iget_reclaim_fail(ip); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 237 | goto out_error; |
| 238 | } |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 239 | |
| 240 | write_lock(&pag->pag_ici_lock); |
| 241 | spin_lock(&ip->i_flags_lock); |
| 242 | ip->i_flags &= ~(XFS_IRECLAIMABLE | XFS_IRECLAIM); |
| 243 | ip->i_flags |= XFS_INEW; |
| 244 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); |
Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 245 | inode->i_state = I_NEW; |
Dave Chinner | dcfcf20 | 2010-12-23 11:57:13 +1100 | [diff] [blame] | 246 | |
| 247 | ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock)); |
| 248 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
| 249 | lockdep_set_class_and_name(&ip->i_iolock.mr_lock, |
| 250 | &xfs_iolock_active, "xfs_iolock_active"); |
| 251 | |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 252 | spin_unlock(&ip->i_flags_lock); |
| 253 | write_unlock(&pag->pag_ici_lock); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 254 | } else { |
| 255 | /* If the VFS inode is being torn down, pause and try again. */ |
| 256 | if (!igrab(inode)) { |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 257 | trace_xfs_iget_skip(ip); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 258 | error = EAGAIN; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 259 | goto out_error; |
| 260 | } |
David Chinner | 6bfb3d0 | 2008-10-30 18:32:43 +1100 | [diff] [blame] | 261 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 262 | /* We've got a live one. */ |
| 263 | spin_unlock(&ip->i_flags_lock); |
| 264 | read_unlock(&pag->pag_ici_lock); |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 265 | trace_xfs_iget_hit(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 266 | } |
| 267 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 268 | if (lock_flags != 0) |
| 269 | xfs_ilock(ip, lock_flags); |
| 270 | |
| 271 | xfs_iflags_clear(ip, XFS_ISTALE); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 272 | XFS_STATS_INC(xs_ig_found); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 273 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 274 | return 0; |
| 275 | |
| 276 | out_error: |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 277 | spin_unlock(&ip->i_flags_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 278 | read_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 279 | return error; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | static int |
| 284 | xfs_iget_cache_miss( |
| 285 | struct xfs_mount *mp, |
| 286 | struct xfs_perag *pag, |
| 287 | xfs_trans_t *tp, |
| 288 | xfs_ino_t ino, |
| 289 | struct xfs_inode **ipp, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 290 | int flags, |
Christoph Hellwig | 0c3dc2b | 2009-11-14 16:17:23 +0000 | [diff] [blame] | 291 | int lock_flags) |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 292 | { |
| 293 | struct xfs_inode *ip; |
| 294 | int error; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 295 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
| 296 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 297 | ip = xfs_inode_alloc(mp, ino); |
| 298 | if (!ip) |
| 299 | return ENOMEM; |
| 300 | |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 301 | error = xfs_iread(mp, tp, ip, flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 302 | if (error) |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 303 | goto out_destroy; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 304 | |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 305 | trace_xfs_iget_miss(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 306 | |
| 307 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
| 308 | error = ENOENT; |
| 309 | goto out_destroy; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Preload the radix tree so we can insert safely under the |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 314 | * write spinlock. Note that we cannot sleep inside the preload |
| 315 | * region. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 316 | */ |
| 317 | if (radix_tree_preload(GFP_KERNEL)) { |
| 318 | error = EAGAIN; |
Christoph Hellwig | ed93ec3 | 2009-03-03 14:48:35 -0500 | [diff] [blame] | 319 | goto out_destroy; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Because the inode hasn't been added to the radix-tree yet it can't |
| 324 | * be found by another thread, so we can do the non-sleeping lock here. |
| 325 | */ |
| 326 | if (lock_flags) { |
| 327 | if (!xfs_ilock_nowait(ip, lock_flags)) |
| 328 | BUG(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 329 | } |
| 330 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 331 | write_lock(&pag->pag_ici_lock); |
| 332 | |
| 333 | /* insert the new inode */ |
| 334 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
| 335 | if (unlikely(error)) { |
| 336 | WARN_ON(error != -EEXIST); |
| 337 | XFS_STATS_INC(xs_ig_dup); |
| 338 | error = EAGAIN; |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 339 | goto out_preload_end; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* These values _must_ be set before releasing the radix tree lock! */ |
| 343 | ip->i_udquot = ip->i_gdquot = NULL; |
| 344 | xfs_iflags_set(ip, XFS_INEW); |
| 345 | |
| 346 | write_unlock(&pag->pag_ici_lock); |
| 347 | radix_tree_preload_end(); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 348 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 349 | *ipp = ip; |
| 350 | return 0; |
| 351 | |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 352 | out_preload_end: |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 353 | write_unlock(&pag->pag_ici_lock); |
| 354 | radix_tree_preload_end(); |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 355 | if (lock_flags) |
| 356 | xfs_iunlock(ip, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 357 | out_destroy: |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 358 | __destroy_inode(VFS_I(ip)); |
| 359 | xfs_inode_free(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 360 | return error; |
| 361 | } |
| 362 | |
| 363 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | * Look up an inode by number in the given file system. |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 365 | * The inode is looked up in the cache held in each AG. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 366 | * If the inode is found in the cache, initialise the vfs inode |
| 367 | * if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | * |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 369 | * 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] | 370 | * add it to the cache and initialise the vfs inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | * |
| 372 | * The inode is locked according to the value of the lock_flags parameter. |
| 373 | * This flag parameter indicates how and if the inode's IO lock and inode lock |
| 374 | * should be taken. |
| 375 | * |
| 376 | * mp -- the mount point structure for the current file system. It points |
| 377 | * to the inode hash table. |
| 378 | * tp -- a pointer to the current transaction if there is one. This is |
| 379 | * simply passed through to the xfs_iread() call. |
| 380 | * ino -- the number of the inode desired. This is the unique identifier |
| 381 | * within the file system for the inode being requested. |
| 382 | * lock_flags -- flags indicating how to lock the inode. See the comment |
| 383 | * for xfs_ilock() for a list of valid values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 385 | int |
| 386 | xfs_iget( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | xfs_mount_t *mp, |
| 388 | xfs_trans_t *tp, |
| 389 | xfs_ino_t ino, |
| 390 | uint flags, |
| 391 | uint lock_flags, |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 392 | xfs_inode_t **ipp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | int error; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 396 | xfs_perag_t *pag; |
| 397 | xfs_agino_t agino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Christoph Hellwig | d276734 | 2010-10-06 18:31:23 +0000 | [diff] [blame] | 399 | /* reject inode numbers outside existing AGs */ |
| 400 | if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount) |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 401 | return EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 403 | /* get the perag structure and ensure that it's inode capable */ |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 404 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 405 | agino = XFS_INO_TO_AGINO(mp, ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | again: |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 408 | error = 0; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 409 | read_lock(&pag->pag_ici_lock); |
| 410 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 412 | if (ip) { |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 413 | error = xfs_iget_cache_hit(pag, ip, flags, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 414 | if (error) |
| 415 | goto out_error_or_again; |
| 416 | } else { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 417 | read_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 418 | XFS_STATS_INC(xs_ig_missed); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 419 | |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 420 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 421 | flags, lock_flags); |
| 422 | if (error) |
| 423 | goto out_error_or_again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 425 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | *ipp = ip; |
| 428 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 429 | ASSERT(ip->i_df.if_ext_max == |
| 430 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t)); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 431 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | * If we have a real type for an on-disk inode, we can set ops(&unlock) |
| 433 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
| 434 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 435 | 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] | 436 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return 0; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 438 | |
| 439 | out_error_or_again: |
| 440 | if (error == EAGAIN) { |
| 441 | delay(1); |
| 442 | goto again; |
| 443 | } |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 444 | xfs_perag_put(pag); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 445 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | * This is a wrapper routine around the xfs_ilock() routine |
| 450 | * used to centralize some grungy code. It is used in places |
| 451 | * that wish to lock the inode solely for reading the extents. |
| 452 | * The reason these places can't just call xfs_ilock(SHARED) |
| 453 | * is that the inode lock also guards to bringing in of the |
| 454 | * extents from disk for a file in b-tree format. If the inode |
| 455 | * is in b-tree format, then we need to lock the inode exclusively |
| 456 | * until the extents are read in. Locking it exclusively all |
| 457 | * the time would limit our parallelism unnecessarily, though. |
| 458 | * What we do instead is check to see if the extents have been |
| 459 | * read in yet, and only lock the inode exclusively if they |
| 460 | * have not. |
| 461 | * |
| 462 | * The function returns a value which should be given to the |
| 463 | * corresponding xfs_iunlock_map_shared(). This value is |
| 464 | * the mode in which the lock was actually taken. |
| 465 | */ |
| 466 | uint |
| 467 | xfs_ilock_map_shared( |
| 468 | xfs_inode_t *ip) |
| 469 | { |
| 470 | uint lock_mode; |
| 471 | |
| 472 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && |
| 473 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { |
| 474 | lock_mode = XFS_ILOCK_EXCL; |
| 475 | } else { |
| 476 | lock_mode = XFS_ILOCK_SHARED; |
| 477 | } |
| 478 | |
| 479 | xfs_ilock(ip, lock_mode); |
| 480 | |
| 481 | return lock_mode; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). |
| 486 | * All it does is call xfs_iunlock() with the given lock_mode. |
| 487 | */ |
| 488 | void |
| 489 | xfs_iunlock_map_shared( |
| 490 | xfs_inode_t *ip, |
| 491 | unsigned int lock_mode) |
| 492 | { |
| 493 | xfs_iunlock(ip, lock_mode); |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * The xfs inode contains 2 locks: a multi-reader lock called the |
| 498 | * i_iolock and a multi-reader lock called the i_lock. This routine |
| 499 | * allows either or both of the locks to be obtained. |
| 500 | * |
| 501 | * The 2 locks should always be ordered so that the IO lock is |
| 502 | * obtained first in order to prevent deadlock. |
| 503 | * |
| 504 | * ip -- the inode being locked |
| 505 | * lock_flags -- this parameter indicates the inode's locks |
| 506 | * to be locked. It can be: |
| 507 | * XFS_IOLOCK_SHARED, |
| 508 | * XFS_IOLOCK_EXCL, |
| 509 | * XFS_ILOCK_SHARED, |
| 510 | * XFS_ILOCK_EXCL, |
| 511 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, |
| 512 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, |
| 513 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, |
| 514 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
| 515 | */ |
| 516 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 517 | xfs_ilock( |
| 518 | xfs_inode_t *ip, |
| 519 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
| 521 | /* |
| 522 | * You can't set both SHARED and EXCL for the same lock, |
| 523 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 524 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 525 | */ |
| 526 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 527 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 528 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 529 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 530 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 532 | if (lock_flags & XFS_IOLOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 533 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 534 | else if (lock_flags & XFS_IOLOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 535 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 536 | |
| 537 | if (lock_flags & XFS_ILOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 538 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 539 | else if (lock_flags & XFS_ILOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 540 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 541 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 542 | trace_xfs_ilock(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
| 546 | * This is just like xfs_ilock(), except that the caller |
| 547 | * is guaranteed not to sleep. It returns 1 if it gets |
| 548 | * the requested locks and 0 otherwise. If the IO lock is |
| 549 | * obtained but the inode lock cannot be, then the IO lock |
| 550 | * is dropped before returning. |
| 551 | * |
| 552 | * ip -- the inode being locked |
| 553 | * lock_flags -- this parameter indicates the inode's locks to be |
| 554 | * to be locked. See the comment for xfs_ilock() for a list |
| 555 | * of valid values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | */ |
| 557 | int |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 558 | xfs_ilock_nowait( |
| 559 | xfs_inode_t *ip, |
| 560 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* |
| 563 | * You can't set both SHARED and EXCL for the same lock, |
| 564 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 565 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 566 | */ |
| 567 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 568 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 569 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 570 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 571 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if (lock_flags & XFS_IOLOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 574 | if (!mrtryupdate(&ip->i_iolock)) |
| 575 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 577 | if (!mrtryaccess(&ip->i_iolock)) |
| 578 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | if (lock_flags & XFS_ILOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 581 | if (!mrtryupdate(&ip->i_lock)) |
| 582 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 584 | if (!mrtryaccess(&ip->i_lock)) |
| 585 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 587 | trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | return 1; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 589 | |
| 590 | out_undo_iolock: |
| 591 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 592 | mrunlock_excl(&ip->i_iolock); |
| 593 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 594 | mrunlock_shared(&ip->i_iolock); |
| 595 | out: |
| 596 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* |
| 600 | * xfs_iunlock() is used to drop the inode locks acquired with |
| 601 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass |
| 602 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so |
| 603 | * that we know which locks to drop. |
| 604 | * |
| 605 | * ip -- the inode being unlocked |
| 606 | * lock_flags -- this parameter indicates the inode's locks to be |
| 607 | * to be unlocked. See the comment for xfs_ilock() for a list |
| 608 | * of valid values for this parameter. |
| 609 | * |
| 610 | */ |
| 611 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 612 | xfs_iunlock( |
| 613 | xfs_inode_t *ip, |
| 614 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
| 616 | /* |
| 617 | * You can't set both SHARED and EXCL for the same lock, |
| 618 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 619 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 620 | */ |
| 621 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 622 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 623 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 624 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 625 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY | |
| 626 | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | ASSERT(lock_flags != 0); |
| 628 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 629 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 630 | mrunlock_excl(&ip->i_iolock); |
| 631 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 632 | mrunlock_shared(&ip->i_iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 634 | if (lock_flags & XFS_ILOCK_EXCL) |
| 635 | mrunlock_excl(&ip->i_lock); |
| 636 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 637 | mrunlock_shared(&ip->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 639 | if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) && |
| 640 | !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | /* |
| 642 | * Let the AIL know that this item has been unlocked in case |
| 643 | * it is in the AIL and anyone is waiting on it. Don't do |
| 644 | * this if the caller has asked us not to. |
| 645 | */ |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 646 | xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp, |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 647 | (xfs_log_item_t*)(ip->i_itemp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 649 | trace_xfs_iunlock(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* |
| 653 | * give up write locks. the i/o lock cannot be held nested |
| 654 | * if it is being demoted. |
| 655 | */ |
| 656 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 657 | xfs_ilock_demote( |
| 658 | xfs_inode_t *ip, |
| 659 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | { |
| 661 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); |
| 662 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); |
| 663 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 664 | if (lock_flags & XFS_ILOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | mrdemote(&ip->i_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 666 | if (lock_flags & XFS_IOLOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | mrdemote(&ip->i_iolock); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 668 | |
| 669 | trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 672 | #ifdef DEBUG |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 673 | int |
| 674 | xfs_isilocked( |
| 675 | xfs_inode_t *ip, |
| 676 | uint lock_flags) |
| 677 | { |
Christoph Hellwig | f936972 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 678 | if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { |
| 679 | if (!(lock_flags & XFS_ILOCK_SHARED)) |
| 680 | return !!ip->i_lock.mr_writer; |
| 681 | return rwsem_is_locked(&ip->i_lock.mr_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 682 | } |
| 683 | |
Christoph Hellwig | f936972 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 684 | if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) { |
| 685 | if (!(lock_flags & XFS_IOLOCK_SHARED)) |
| 686 | return !!ip->i_iolock.mr_writer; |
| 687 | return rwsem_is_locked(&ip->i_iolock.mr_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 688 | } |
| 689 | |
Christoph Hellwig | f936972 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 690 | ASSERT(0); |
| 691 | return 0; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 692 | } |
| 693 | #endif |