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" |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /* |
| 44 | * Allocate and initialise an xfs_inode. |
| 45 | */ |
| 46 | STATIC struct xfs_inode * |
| 47 | xfs_inode_alloc( |
| 48 | struct xfs_mount *mp, |
| 49 | xfs_ino_t ino) |
| 50 | { |
| 51 | struct xfs_inode *ip; |
| 52 | |
| 53 | /* |
| 54 | * if this didn't occur in transactions, we could use |
| 55 | * KM_MAYFAIL and return NULL here on ENOMEM. Set the |
| 56 | * code up to do this anyway. |
| 57 | */ |
| 58 | ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP); |
| 59 | if (!ip) |
| 60 | return NULL; |
Christoph Hellwig | 54e3462 | 2009-08-07 14:38:25 -0300 | [diff] [blame] | 61 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
| 62 | kmem_zone_free(xfs_inode_zone, ip); |
| 63 | return NULL; |
| 64 | } |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 65 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 66 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 67 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 68 | ASSERT(!xfs_isiflocked(ip)); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 69 | ASSERT(ip->i_ino == 0); |
Christoph Hellwig | 033da48 | 2009-10-19 04:05:26 +0000 | [diff] [blame] | 70 | |
| 71 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 72 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 73 | /* initialise the xfs inode */ |
| 74 | ip->i_ino = ino; |
| 75 | ip->i_mount = mp; |
| 76 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); |
| 77 | ip->i_afp = NULL; |
| 78 | memset(&ip->i_df, 0, sizeof(xfs_ifork_t)); |
| 79 | ip->i_flags = 0; |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 80 | ip->i_delayed_blks = 0; |
| 81 | memset(&ip->i_d, 0, sizeof(xfs_icdinode_t)); |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 82 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 83 | return ip; |
| 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 86 | STATIC void |
| 87 | xfs_inode_free_callback( |
| 88 | struct rcu_head *head) |
| 89 | { |
| 90 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 91 | struct xfs_inode *ip = XFS_I(inode); |
| 92 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 93 | kmem_zone_free(xfs_inode_zone, ip); |
| 94 | } |
| 95 | |
Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 96 | void |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 97 | xfs_inode_free( |
| 98 | struct xfs_inode *ip) |
| 99 | { |
| 100 | switch (ip->i_d.di_mode & S_IFMT) { |
| 101 | case S_IFREG: |
| 102 | case S_IFDIR: |
| 103 | case S_IFLNK: |
| 104 | xfs_idestroy_fork(ip, XFS_DATA_FORK); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | if (ip->i_afp) |
| 109 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); |
| 110 | |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 111 | if (ip->i_itemp) { |
Christoph Hellwig | 32ce90a | 2012-04-23 15:58:32 +1000 | [diff] [blame] | 112 | ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL)); |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 113 | xfs_inode_item_destroy(ip); |
| 114 | ip->i_itemp = NULL; |
| 115 | } |
| 116 | |
| 117 | /* asserts to verify all state is correct here */ |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 118 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
| 119 | ASSERT(!spin_is_locked(&ip->i_flags_lock)); |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 120 | ASSERT(!xfs_isiflocked(ip)); |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 121 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 122 | /* |
| 123 | * Because we use RCU freeing we need to ensure the inode always |
| 124 | * appears to be reclaimed with an invalid inode number when in the |
| 125 | * free state. The ip->i_flags_lock provides the barrier against lookup |
| 126 | * races. |
| 127 | */ |
| 128 | spin_lock(&ip->i_flags_lock); |
| 129 | ip->i_flags = XFS_IRECLAIM; |
| 130 | ip->i_ino = 0; |
| 131 | spin_unlock(&ip->i_flags_lock); |
Alex Elder | 92f1c00 | 2011-01-10 21:35:55 -0600 | [diff] [blame] | 132 | |
| 133 | call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback); |
Christoph Hellwig | b36ec04 | 2009-08-07 14:38:34 -0300 | [diff] [blame] | 134 | } |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 137 | * Check the validity of the inode we just found it the cache |
| 138 | */ |
| 139 | static int |
| 140 | xfs_iget_cache_hit( |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 141 | struct xfs_perag *pag, |
| 142 | struct xfs_inode *ip, |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 143 | xfs_ino_t ino, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 144 | int flags, |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 145 | int lock_flags) __releases(RCU) |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 146 | { |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 147 | struct inode *inode = VFS_I(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 148 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 149 | int error; |
| 150 | |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 151 | /* |
| 152 | * check for re-use of an inode within an RCU grace period due to the |
| 153 | * radix tree nodes not being updated yet. We monitor for this by |
| 154 | * setting the inode number to zero before freeing the inode structure. |
| 155 | * If the inode has been reallocated and set up, then the inode number |
| 156 | * will not match, so check for that, too. |
| 157 | */ |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 158 | spin_lock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 159 | if (ip->i_ino != ino) { |
| 160 | trace_xfs_iget_skip(ip); |
| 161 | XFS_STATS_INC(xs_ig_frecycle); |
| 162 | error = EAGAIN; |
| 163 | goto out_error; |
| 164 | } |
| 165 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 166 | |
| 167 | /* |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 168 | * If we are racing with another cache hit that is currently |
| 169 | * instantiating this inode or currently recycling it out of |
| 170 | * reclaimabe state, wait for the initialisation to complete |
| 171 | * before continuing. |
| 172 | * |
| 173 | * XXX(hch): eventually we should do something equivalent to |
| 174 | * wait_on_inode to wait for these flags to be cleared |
| 175 | * instead of polling for it. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 176 | */ |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 177 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 178 | trace_xfs_iget_skip(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 179 | XFS_STATS_INC(xs_ig_frecycle); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 180 | error = EAGAIN; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 181 | goto out_error; |
| 182 | } |
| 183 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 184 | /* |
| 185 | * If lookup is racing with unlink return an error immediately. |
| 186 | */ |
| 187 | if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) { |
| 188 | error = ENOENT; |
| 189 | goto out_error; |
| 190 | } |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 191 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 192 | /* |
| 193 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. |
| 194 | * Need to carefully get it back into useable state. |
| 195 | */ |
| 196 | if (ip->i_flags & XFS_IRECLAIMABLE) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 197 | trace_xfs_iget_reclaim(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 198 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 199 | /* |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 200 | * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode |
| 201 | * from stomping over us while we recycle the inode. We can't |
| 202 | * clear the radix tree reclaimable tag yet as it requires |
| 203 | * pag_ici_lock to be held exclusive. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 204 | */ |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 205 | ip->i_flags |= XFS_IRECLAIM; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 206 | |
| 207 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 208 | rcu_read_unlock(); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 209 | |
| 210 | error = -inode_init_always(mp->m_super, inode); |
| 211 | if (error) { |
| 212 | /* |
| 213 | * Re-initializing the inode failed, and we are in deep |
| 214 | * trouble. Try to re-add it to the reclaim list. |
| 215 | */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 216 | rcu_read_lock(); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 217 | spin_lock(&ip->i_flags_lock); |
| 218 | |
Dave Chinner | 778e24b | 2011-06-23 01:34:59 +0000 | [diff] [blame] | 219 | ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM); |
| 220 | ASSERT(ip->i_flags & XFS_IRECLAIMABLE); |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 221 | trace_xfs_iget_reclaim_fail(ip); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 222 | goto out_error; |
| 223 | } |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 224 | |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 225 | spin_lock(&pag->pag_ici_lock); |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 226 | spin_lock(&ip->i_flags_lock); |
Dave Chinner | 778e24b | 2011-06-23 01:34:59 +0000 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * Clear the per-lifetime state in the inode as we are now |
| 230 | * effectively a new inode and need to return to the initial |
| 231 | * state before reuse occurs. |
| 232 | */ |
| 233 | ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS; |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 234 | ip->i_flags |= XFS_INEW; |
| 235 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); |
Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 236 | inode->i_state = I_NEW; |
Dave Chinner | dcfcf20 | 2010-12-23 11:57:13 +1100 | [diff] [blame] | 237 | |
| 238 | ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock)); |
| 239 | mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino); |
Dave Chinner | dcfcf20 | 2010-12-23 11:57:13 +1100 | [diff] [blame] | 240 | |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 241 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 242 | spin_unlock(&pag->pag_ici_lock); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 243 | } else { |
| 244 | /* If the VFS inode is being torn down, pause and try again. */ |
| 245 | if (!igrab(inode)) { |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 246 | trace_xfs_iget_skip(ip); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 247 | error = EAGAIN; |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 248 | goto out_error; |
| 249 | } |
David Chinner | 6bfb3d0 | 2008-10-30 18:32:43 +1100 | [diff] [blame] | 250 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 251 | /* We've got a live one. */ |
| 252 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 253 | rcu_read_unlock(); |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 254 | trace_xfs_iget_hit(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 255 | } |
| 256 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 257 | if (lock_flags != 0) |
| 258 | xfs_ilock(ip, lock_flags); |
| 259 | |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 260 | xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 261 | XFS_STATS_INC(xs_ig_found); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 262 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 263 | return 0; |
| 264 | |
| 265 | out_error: |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 266 | spin_unlock(&ip->i_flags_lock); |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 267 | rcu_read_unlock(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 268 | return error; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | static int |
| 273 | xfs_iget_cache_miss( |
| 274 | struct xfs_mount *mp, |
| 275 | struct xfs_perag *pag, |
| 276 | xfs_trans_t *tp, |
| 277 | xfs_ino_t ino, |
| 278 | struct xfs_inode **ipp, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 279 | int flags, |
Christoph Hellwig | 0c3dc2b | 2009-11-14 16:17:23 +0000 | [diff] [blame] | 280 | int lock_flags) |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 281 | { |
| 282 | struct xfs_inode *ip; |
| 283 | int error; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 284 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 285 | int iflags; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 286 | |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 287 | ip = xfs_inode_alloc(mp, ino); |
| 288 | if (!ip) |
| 289 | return ENOMEM; |
| 290 | |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 291 | error = xfs_iread(mp, tp, ip, flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 292 | if (error) |
Christoph Hellwig | 24f211b | 2008-11-28 14:23:42 +1100 | [diff] [blame] | 293 | goto out_destroy; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 294 | |
Christoph Hellwig | d2e078c | 2010-06-24 11:50:22 +1000 | [diff] [blame] | 295 | trace_xfs_iget_miss(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 296 | |
| 297 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
| 298 | error = ENOENT; |
| 299 | goto out_destroy; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Preload the radix tree so we can insert safely under the |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 304 | * write spinlock. Note that we cannot sleep inside the preload |
Peter Watkins | 3ba3160 | 2012-05-07 16:11:37 -0400 | [diff] [blame] | 305 | * region. Since we can be called from transaction context, don't |
| 306 | * recurse into the file system. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 307 | */ |
Peter Watkins | 3ba3160 | 2012-05-07 16:11:37 -0400 | [diff] [blame] | 308 | if (radix_tree_preload(GFP_NOFS)) { |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 309 | error = EAGAIN; |
Christoph Hellwig | ed93ec3 | 2009-03-03 14:48:35 -0500 | [diff] [blame] | 310 | goto out_destroy; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Because the inode hasn't been added to the radix-tree yet it can't |
| 315 | * be found by another thread, so we can do the non-sleeping lock here. |
| 316 | */ |
| 317 | if (lock_flags) { |
| 318 | if (!xfs_ilock_nowait(ip, lock_flags)) |
| 319 | BUG(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 320 | } |
| 321 | |
Dave Chinner | f30d500 | 2012-03-07 04:50:25 +0000 | [diff] [blame] | 322 | /* |
| 323 | * These values must be set before inserting the inode into the radix |
| 324 | * tree as the moment it is inserted a concurrent lookup (allowed by the |
| 325 | * RCU locking mechanism) can find it and that lookup must see that this |
| 326 | * is an inode currently under construction (i.e. that XFS_INEW is set). |
| 327 | * The ip->i_flags_lock that protects the XFS_INEW flag forms the |
| 328 | * memory barrier that ensures this detection works correctly at lookup |
| 329 | * time. |
| 330 | */ |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 331 | iflags = XFS_INEW; |
| 332 | if (flags & XFS_IGET_DONTCACHE) |
| 333 | iflags |= XFS_IDONTCACHE; |
Dave Chinner | f30d500 | 2012-03-07 04:50:25 +0000 | [diff] [blame] | 334 | ip->i_udquot = ip->i_gdquot = NULL; |
Dave Chinner | 5132ba8 | 2012-03-22 05:15:10 +0000 | [diff] [blame] | 335 | xfs_iflags_set(ip, iflags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 336 | |
| 337 | /* insert the new inode */ |
Dave Chinner | f30d500 | 2012-03-07 04:50:25 +0000 | [diff] [blame] | 338 | spin_lock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 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 | } |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 346 | spin_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 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: |
Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 353 | spin_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 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 | |
Alex Elder | ad637a1 | 2012-02-16 22:01:00 +0000 | [diff] [blame] | 399 | /* |
| 400 | * xfs_reclaim_inode() uses the ILOCK to ensure an inode |
| 401 | * doesn't get freed while it's being referenced during a |
| 402 | * radix tree traversal here. It assumes this function |
| 403 | * aqcuires only the ILOCK (and therefore it has no need to |
| 404 | * involve the IOLOCK in this synchronization). |
| 405 | */ |
| 406 | ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0); |
| 407 | |
Christoph Hellwig | d276734 | 2010-10-06 18:31:23 +0000 | [diff] [blame] | 408 | /* reject inode numbers outside existing AGs */ |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 409 | 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] | 410 | return EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 412 | /* get the perag structure and ensure that it's inode capable */ |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 413 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 414 | agino = XFS_INO_TO_AGINO(mp, ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | again: |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 417 | error = 0; |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 418 | rcu_read_lock(); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 419 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 421 | if (ip) { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 422 | error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 423 | if (error) |
| 424 | goto out_error_or_again; |
| 425 | } else { |
Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 426 | rcu_read_unlock(); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 427 | XFS_STATS_INC(xs_ig_missed); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 428 | |
Dave Chinner | 7b6259e | 2010-06-24 11:35:17 +1000 | [diff] [blame] | 429 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 430 | flags, lock_flags); |
| 431 | if (error) |
| 432 | goto out_error_or_again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 434 | xfs_perag_put(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | *ipp = ip; |
| 437 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 438 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | * If we have a real type for an on-disk inode, we can set ops(&unlock) |
| 440 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
| 441 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 442 | 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] | 443 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | return 0; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 445 | |
| 446 | out_error_or_again: |
| 447 | if (error == EAGAIN) { |
| 448 | delay(1); |
| 449 | goto again; |
| 450 | } |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 451 | xfs_perag_put(pag); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 452 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | * This is a wrapper routine around the xfs_ilock() routine |
| 457 | * used to centralize some grungy code. It is used in places |
| 458 | * that wish to lock the inode solely for reading the extents. |
| 459 | * The reason these places can't just call xfs_ilock(SHARED) |
| 460 | * is that the inode lock also guards to bringing in of the |
| 461 | * extents from disk for a file in b-tree format. If the inode |
| 462 | * is in b-tree format, then we need to lock the inode exclusively |
| 463 | * until the extents are read in. Locking it exclusively all |
| 464 | * the time would limit our parallelism unnecessarily, though. |
| 465 | * What we do instead is check to see if the extents have been |
| 466 | * read in yet, and only lock the inode exclusively if they |
| 467 | * have not. |
| 468 | * |
| 469 | * The function returns a value which should be given to the |
| 470 | * corresponding xfs_iunlock_map_shared(). This value is |
| 471 | * the mode in which the lock was actually taken. |
| 472 | */ |
| 473 | uint |
| 474 | xfs_ilock_map_shared( |
| 475 | xfs_inode_t *ip) |
| 476 | { |
| 477 | uint lock_mode; |
| 478 | |
| 479 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && |
| 480 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { |
| 481 | lock_mode = XFS_ILOCK_EXCL; |
| 482 | } else { |
| 483 | lock_mode = XFS_ILOCK_SHARED; |
| 484 | } |
| 485 | |
| 486 | xfs_ilock(ip, lock_mode); |
| 487 | |
| 488 | return lock_mode; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). |
| 493 | * All it does is call xfs_iunlock() with the given lock_mode. |
| 494 | */ |
| 495 | void |
| 496 | xfs_iunlock_map_shared( |
| 497 | xfs_inode_t *ip, |
| 498 | unsigned int lock_mode) |
| 499 | { |
| 500 | xfs_iunlock(ip, lock_mode); |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * The xfs inode contains 2 locks: a multi-reader lock called the |
| 505 | * i_iolock and a multi-reader lock called the i_lock. This routine |
| 506 | * allows either or both of the locks to be obtained. |
| 507 | * |
| 508 | * The 2 locks should always be ordered so that the IO lock is |
| 509 | * obtained first in order to prevent deadlock. |
| 510 | * |
| 511 | * ip -- the inode being locked |
| 512 | * lock_flags -- this parameter indicates the inode's locks |
| 513 | * to be locked. It can be: |
| 514 | * XFS_IOLOCK_SHARED, |
| 515 | * XFS_IOLOCK_EXCL, |
| 516 | * XFS_ILOCK_SHARED, |
| 517 | * XFS_ILOCK_EXCL, |
| 518 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, |
| 519 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, |
| 520 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, |
| 521 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
| 522 | */ |
| 523 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 524 | xfs_ilock( |
| 525 | xfs_inode_t *ip, |
| 526 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
| 528 | /* |
| 529 | * You can't set both SHARED and EXCL for the same lock, |
| 530 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 531 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 532 | */ |
| 533 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 534 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 535 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 536 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 537 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 539 | if (lock_flags & XFS_IOLOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 540 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 541 | else if (lock_flags & XFS_IOLOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 542 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 543 | |
| 544 | if (lock_flags & XFS_ILOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 545 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 546 | else if (lock_flags & XFS_ILOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 547 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 548 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 549 | trace_xfs_ilock(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* |
| 553 | * This is just like xfs_ilock(), except that the caller |
| 554 | * is guaranteed not to sleep. It returns 1 if it gets |
| 555 | * the requested locks and 0 otherwise. If the IO lock is |
| 556 | * obtained but the inode lock cannot be, then the IO lock |
| 557 | * is dropped before returning. |
| 558 | * |
| 559 | * ip -- the inode being locked |
| 560 | * lock_flags -- this parameter indicates the inode's locks to be |
| 561 | * to be locked. See the comment for xfs_ilock() for a list |
| 562 | * of valid values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | */ |
| 564 | int |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 565 | xfs_ilock_nowait( |
| 566 | xfs_inode_t *ip, |
| 567 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | /* |
| 570 | * You can't set both SHARED and EXCL for the same lock, |
| 571 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 572 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 573 | */ |
| 574 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 575 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 576 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 577 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 578 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (lock_flags & XFS_IOLOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 581 | if (!mrtryupdate(&ip->i_iolock)) |
| 582 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 584 | if (!mrtryaccess(&ip->i_iolock)) |
| 585 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | if (lock_flags & XFS_ILOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 588 | if (!mrtryupdate(&ip->i_lock)) |
| 589 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 591 | if (!mrtryaccess(&ip->i_lock)) |
| 592 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 594 | trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | return 1; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 596 | |
| 597 | out_undo_iolock: |
| 598 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 599 | mrunlock_excl(&ip->i_iolock); |
| 600 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 601 | mrunlock_shared(&ip->i_iolock); |
| 602 | out: |
| 603 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* |
| 607 | * xfs_iunlock() is used to drop the inode locks acquired with |
| 608 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass |
| 609 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so |
| 610 | * that we know which locks to drop. |
| 611 | * |
| 612 | * ip -- the inode being unlocked |
| 613 | * lock_flags -- this parameter indicates the inode's locks to be |
| 614 | * to be unlocked. See the comment for xfs_ilock() for a list |
| 615 | * of valid values for this parameter. |
| 616 | * |
| 617 | */ |
| 618 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 619 | xfs_iunlock( |
| 620 | xfs_inode_t *ip, |
| 621 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
| 623 | /* |
| 624 | * You can't set both SHARED and EXCL for the same lock, |
| 625 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 626 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 627 | */ |
| 628 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 629 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 630 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 631 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Christoph Hellwig | 5b03ff1 | 2012-02-20 02:31:22 +0000 | [diff] [blame] | 632 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | ASSERT(lock_flags != 0); |
| 634 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 635 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 636 | mrunlock_excl(&ip->i_iolock); |
| 637 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 638 | mrunlock_shared(&ip->i_iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 640 | if (lock_flags & XFS_ILOCK_EXCL) |
| 641 | mrunlock_excl(&ip->i_lock); |
| 642 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 643 | mrunlock_shared(&ip->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 645 | trace_xfs_iunlock(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* |
| 649 | * give up write locks. the i/o lock cannot be held nested |
| 650 | * if it is being demoted. |
| 651 | */ |
| 652 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 653 | xfs_ilock_demote( |
| 654 | xfs_inode_t *ip, |
| 655 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); |
| 658 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); |
| 659 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 660 | if (lock_flags & XFS_ILOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | mrdemote(&ip->i_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 662 | if (lock_flags & XFS_IOLOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | mrdemote(&ip->i_iolock); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 664 | |
| 665 | trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 668 | #ifdef DEBUG |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 669 | int |
| 670 | xfs_isilocked( |
| 671 | xfs_inode_t *ip, |
| 672 | uint lock_flags) |
| 673 | { |
Christoph Hellwig | f936972 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 674 | if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { |
| 675 | if (!(lock_flags & XFS_ILOCK_SHARED)) |
| 676 | return !!ip->i_lock.mr_writer; |
| 677 | return rwsem_is_locked(&ip->i_lock.mr_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 678 | } |
| 679 | |
Christoph Hellwig | f936972 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 680 | if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) { |
| 681 | if (!(lock_flags & XFS_IOLOCK_SHARED)) |
| 682 | return !!ip->i_iolock.mr_writer; |
| 683 | return rwsem_is_locked(&ip->i_iolock.mr_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 684 | } |
| 685 | |
Christoph Hellwig | f936972 | 2010-06-03 16:22:29 +1000 | [diff] [blame] | 686 | ASSERT(0); |
| 687 | return 0; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 688 | } |
| 689 | #endif |
Christoph Hellwig | 474fce0 | 2011-12-18 20:00:09 +0000 | [diff] [blame] | 690 | |
| 691 | void |
| 692 | __xfs_iflock( |
| 693 | struct xfs_inode *ip) |
| 694 | { |
| 695 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT); |
| 696 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT); |
| 697 | |
| 698 | do { |
| 699 | prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 700 | if (xfs_isiflocked(ip)) |
| 701 | io_schedule(); |
| 702 | } while (!xfs_iflock_nowait(ip)); |
| 703 | |
| 704 | finish_wait(wq, &wait.wait); |
| 705 | } |