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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.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_dir2.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_dinode.h" |
| 36 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_btree.h" |
| 38 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_quota.h" |
| 40 | #include "xfs_utils.h" |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 41 | #include "xfs_trans_priv.h" |
| 42 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 45 | * Check the validity of the inode we just found it the cache |
| 46 | */ |
| 47 | static int |
| 48 | xfs_iget_cache_hit( |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 49 | struct xfs_perag *pag, |
| 50 | struct xfs_inode *ip, |
| 51 | int flags, |
| 52 | int lock_flags) __releases(pag->pag_ici_lock) |
| 53 | { |
| 54 | struct xfs_mount *mp = ip->i_mount; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 55 | int error = 0; |
| 56 | |
| 57 | /* |
| 58 | * If INEW is set this inode is being set up |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 59 | * If IRECLAIM is set this inode is being torn down |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 60 | * Pause and try again. |
| 61 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 62 | if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) { |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 63 | error = EAGAIN; |
| 64 | XFS_STATS_INC(xs_ig_frecycle); |
| 65 | goto out_error; |
| 66 | } |
| 67 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 68 | /* If IRECLAIMABLE is set, we've torn down the vfs inode part */ |
| 69 | if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) { |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 70 | |
| 71 | /* |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 72 | * If lookup is racing with unlink, then we should return an |
| 73 | * error immediately so we don't remove it from the reclaim |
| 74 | * list and potentially leak the inode. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 75 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 76 | |
| 77 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 78 | error = ENOENT; |
| 79 | goto out_error; |
| 80 | } |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 81 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 82 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); |
| 83 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 84 | /* |
| 85 | * We need to re-initialise the VFS inode as it has been |
| 86 | * 'freed' by the VFS. Do this here so we can deal with |
| 87 | * errors cleanly, then tag it so it can be set up correctly |
| 88 | * later. |
| 89 | */ |
| 90 | if (!inode_init_always(mp->m_super, VFS_I(ip))) { |
| 91 | error = ENOMEM; |
| 92 | goto out_error; |
| 93 | } |
| 94 | xfs_iflags_set(ip, XFS_INEW); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 95 | xfs_iflags_clear(ip, XFS_IRECLAIMABLE); |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 96 | |
| 97 | /* clear the radix tree reclaim flag as well. */ |
| 98 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 99 | read_unlock(&pag->pag_ici_lock); |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 100 | } else if (!igrab(VFS_I(ip))) { |
| 101 | /* If the VFS inode is being torn down, pause and try again. */ |
| 102 | error = EAGAIN; |
| 103 | XFS_STATS_INC(xs_ig_frecycle); |
| 104 | goto out_error; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 105 | } else { |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 106 | /* we've got a live one */ |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 107 | read_unlock(&pag->pag_ici_lock); |
| 108 | } |
| 109 | |
| 110 | if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) { |
| 111 | error = ENOENT; |
| 112 | goto out; |
| 113 | } |
| 114 | |
| 115 | if (lock_flags != 0) |
| 116 | xfs_ilock(ip, lock_flags); |
| 117 | |
| 118 | xfs_iflags_clear(ip, XFS_ISTALE); |
| 119 | xfs_itrace_exit_tag(ip, "xfs_iget.found"); |
| 120 | XFS_STATS_INC(xs_ig_found); |
| 121 | return 0; |
| 122 | |
| 123 | out_error: |
| 124 | read_unlock(&pag->pag_ici_lock); |
| 125 | out: |
| 126 | return error; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static int |
| 131 | xfs_iget_cache_miss( |
| 132 | struct xfs_mount *mp, |
| 133 | struct xfs_perag *pag, |
| 134 | xfs_trans_t *tp, |
| 135 | xfs_ino_t ino, |
| 136 | struct xfs_inode **ipp, |
| 137 | xfs_daddr_t bno, |
| 138 | int flags, |
| 139 | int lock_flags) __releases(pag->pag_ici_lock) |
| 140 | { |
| 141 | struct xfs_inode *ip; |
| 142 | int error; |
| 143 | unsigned long first_index, mask; |
| 144 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); |
| 145 | |
| 146 | /* |
| 147 | * Read the disk inode attributes into a new inode structure and get |
| 148 | * a new vnode for it. This should also initialize i_ino and i_mount. |
| 149 | */ |
| 150 | error = xfs_iread(mp, tp, ino, &ip, bno, |
| 151 | (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0); |
| 152 | if (error) |
| 153 | return error; |
| 154 | |
| 155 | xfs_itrace_exit_tag(ip, "xfs_iget.alloc"); |
| 156 | |
| 157 | if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) { |
| 158 | error = ENOENT; |
| 159 | goto out_destroy; |
| 160 | } |
| 161 | |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 162 | if (lock_flags) |
| 163 | xfs_ilock(ip, lock_flags); |
| 164 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 165 | /* |
| 166 | * Preload the radix tree so we can insert safely under the |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 167 | * write spinlock. Note that we cannot sleep inside the preload |
| 168 | * region. |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 169 | */ |
| 170 | if (radix_tree_preload(GFP_KERNEL)) { |
| 171 | error = EAGAIN; |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 172 | goto out_unlock; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 173 | } |
| 174 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 175 | mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1); |
| 176 | first_index = agino & mask; |
| 177 | write_lock(&pag->pag_ici_lock); |
| 178 | |
| 179 | /* insert the new inode */ |
| 180 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); |
| 181 | if (unlikely(error)) { |
| 182 | WARN_ON(error != -EEXIST); |
| 183 | XFS_STATS_INC(xs_ig_dup); |
| 184 | error = EAGAIN; |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 185 | goto out_preload_end; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* These values _must_ be set before releasing the radix tree lock! */ |
| 189 | ip->i_udquot = ip->i_gdquot = NULL; |
| 190 | xfs_iflags_set(ip, XFS_INEW); |
| 191 | |
| 192 | write_unlock(&pag->pag_ici_lock); |
| 193 | radix_tree_preload_end(); |
| 194 | *ipp = ip; |
| 195 | return 0; |
| 196 | |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 197 | out_preload_end: |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 198 | write_unlock(&pag->pag_ici_lock); |
| 199 | radix_tree_preload_end(); |
David Chinner | 56e73ec | 2008-10-30 17:55:27 +1100 | [diff] [blame] | 200 | out_unlock: |
| 201 | if (lock_flags) |
| 202 | xfs_iunlock(ip, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 203 | out_destroy: |
Christoph Hellwig | 9ed0451 | 2008-10-30 18:26:04 +1100 | [diff] [blame] | 204 | xfs_destroy_inode(ip); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 205 | return error; |
| 206 | } |
| 207 | |
| 208 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | * Look up an inode by number in the given file system. |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 210 | * The inode is looked up in the cache held in each AG. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 211 | * If the inode is found in the cache, initialise the vfs inode |
| 212 | * if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | * |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 214 | * 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] | 215 | * add it to the cache and initialise the vfs inode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | * |
| 217 | * The inode is locked according to the value of the lock_flags parameter. |
| 218 | * This flag parameter indicates how and if the inode's IO lock and inode lock |
| 219 | * should be taken. |
| 220 | * |
| 221 | * mp -- the mount point structure for the current file system. It points |
| 222 | * to the inode hash table. |
| 223 | * tp -- a pointer to the current transaction if there is one. This is |
| 224 | * simply passed through to the xfs_iread() call. |
| 225 | * ino -- the number of the inode desired. This is the unique identifier |
| 226 | * within the file system for the inode being requested. |
| 227 | * lock_flags -- flags indicating how to lock the inode. See the comment |
| 228 | * for xfs_ilock() for a list of valid values. |
| 229 | * bno -- the block number starting the buffer containing the inode, |
| 230 | * if known (as by bulkstat), else 0. |
| 231 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 232 | int |
| 233 | xfs_iget( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | xfs_mount_t *mp, |
| 235 | xfs_trans_t *tp, |
| 236 | xfs_ino_t ino, |
| 237 | uint flags, |
| 238 | uint lock_flags, |
| 239 | xfs_inode_t **ipp, |
| 240 | xfs_daddr_t bno) |
| 241 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | xfs_inode_t *ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | int error; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 244 | xfs_perag_t *pag; |
| 245 | xfs_agino_t agino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 247 | /* the radix tree exists only in inode capable AGs */ |
| 248 | if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi) |
| 249 | return EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 251 | /* get the perag structure and ensure that it's inode capable */ |
| 252 | pag = xfs_get_perag(mp, ino); |
| 253 | if (!pag->pagi_inodeok) |
| 254 | return EINVAL; |
| 255 | ASSERT(pag->pag_ici_init); |
| 256 | agino = XFS_INO_TO_AGINO(mp, ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | again: |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 259 | error = 0; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 260 | read_lock(&pag->pag_ici_lock); |
| 261 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 263 | if (ip) { |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 264 | error = xfs_iget_cache_hit(pag, ip, flags, lock_flags); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 265 | if (error) |
| 266 | goto out_error_or_again; |
| 267 | } else { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 268 | read_unlock(&pag->pag_ici_lock); |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 269 | XFS_STATS_INC(xs_ig_missed); |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 270 | |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 271 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno, |
| 272 | flags, lock_flags); |
| 273 | if (error) |
| 274 | goto out_error_or_again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 276 | xfs_put_perag(mp, pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 278 | xfs_iflags_set(ip, XFS_IMODIFIED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | *ipp = ip; |
| 280 | |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 281 | ASSERT(ip->i_df.if_ext_max == |
| 282 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t)); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 283 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | * If we have a real type for an on-disk inode, we can set ops(&unlock) |
| 285 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
| 286 | */ |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 287 | 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] | 288 | xfs_setup_inode(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return 0; |
David Chinner | 6441e54 | 2008-10-30 17:21:19 +1100 | [diff] [blame] | 290 | |
| 291 | out_error_or_again: |
| 292 | if (error == EAGAIN) { |
| 293 | delay(1); |
| 294 | goto again; |
| 295 | } |
| 296 | xfs_put_perag(mp, pag); |
| 297 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
| 301 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * Look for the inode corresponding to the given ino in the hash table. |
| 303 | * If it is there and its i_transp pointer matches tp, return it. |
| 304 | * Otherwise, return NULL. |
| 305 | */ |
| 306 | xfs_inode_t * |
| 307 | xfs_inode_incore(xfs_mount_t *mp, |
| 308 | xfs_ino_t ino, |
| 309 | xfs_trans_t *tp) |
| 310 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | xfs_inode_t *ip; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 312 | xfs_perag_t *pag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 314 | pag = xfs_get_perag(mp, ino); |
| 315 | read_lock(&pag->pag_ici_lock); |
| 316 | ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino)); |
| 317 | read_unlock(&pag->pag_ici_lock); |
| 318 | xfs_put_perag(mp, pag); |
| 319 | |
| 320 | /* the returned inode must match the transaction */ |
| 321 | if (ip && (ip->i_transp != tp)) |
| 322 | return NULL; |
| 323 | return ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Decrement reference count of an inode structure and unlock it. |
| 328 | * |
| 329 | * ip -- the inode being released |
| 330 | * lock_flags -- this parameter indicates the inode's locks to be |
| 331 | * to be released. See the comment on xfs_iunlock() for a list |
| 332 | * of valid values. |
| 333 | */ |
| 334 | void |
| 335 | xfs_iput(xfs_inode_t *ip, |
| 336 | uint lock_flags) |
| 337 | { |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 338 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | xfs_iunlock(ip, lock_flags); |
Christoph Hellwig | 10090be | 2007-10-11 18:11:03 +1000 | [diff] [blame] | 340 | IRELE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Special iput for brand-new inodes that are still locked |
| 345 | */ |
| 346 | void |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 347 | xfs_iput_new( |
| 348 | xfs_inode_t *ip, |
| 349 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 351 | struct inode *inode = VFS_I(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Lachlan McIlroy | cf441ee | 2008-02-07 16:42:19 +1100 | [diff] [blame] | 353 | xfs_itrace_entry(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | if ((ip->i_d.di_mode == 0)) { |
David Chinner | 7a18c38 | 2006-11-11 18:04:54 +1100 | [diff] [blame] | 356 | ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE)); |
Christoph Hellwig | 10090be | 2007-10-11 18:11:03 +1000 | [diff] [blame] | 357 | make_bad_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | if (inode->i_state & I_NEW) |
| 360 | unlock_new_inode(inode); |
| 361 | if (lock_flags) |
| 362 | xfs_iunlock(ip, lock_flags); |
Christoph Hellwig | 10090be | 2007-10-11 18:11:03 +1000 | [diff] [blame] | 363 | IRELE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
| 367 | /* |
| 368 | * This routine embodies the part of the reclaim code that pulls |
| 369 | * the inode from the inode hash table and the mount structure's |
| 370 | * inode list. |
| 371 | * This should only be called from xfs_reclaim(). |
| 372 | */ |
| 373 | void |
| 374 | xfs_ireclaim(xfs_inode_t *ip) |
| 375 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | /* |
| 377 | * Remove from old hash list and mount list. |
| 378 | */ |
| 379 | XFS_STATS_INC(xs_ig_reclaims); |
| 380 | |
| 381 | xfs_iextract(ip); |
| 382 | |
| 383 | /* |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 384 | * Here we do a spurious inode lock in order to coordinate with inode |
| 385 | * cache radix tree lookups. This is because the lookup can reference |
| 386 | * the inodes in the cache without taking references. We make that OK |
| 387 | * here by ensuring that we wait until the inode is unlocked after the |
| 388 | * lookup before we go ahead and free it. We get both the ilock and |
| 389 | * the iolock because the code may need to drop the ilock one but will |
| 390 | * still hold the iolock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
| 392 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
| 393 | |
| 394 | /* |
| 395 | * Release dquots (and their references) if any. An inode may escape |
| 396 | * xfs_inactive and get here via vn_alloc->vn_reclaim path. |
| 397 | */ |
| 398 | XFS_QM_DQDETACH(ip->i_mount, ip); |
| 399 | |
| 400 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | * Free all memory associated with the inode. |
| 402 | */ |
Tim Shimmin | 439b843 | 2006-11-11 18:04:34 +1100 | [diff] [blame] | 403 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | xfs_idestroy(ip); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * This routine removes an about-to-be-destroyed inode from |
| 409 | * all of the lists in which it is located with the exception |
| 410 | * of the behavior chain. |
| 411 | */ |
| 412 | void |
| 413 | xfs_iextract( |
| 414 | xfs_inode_t *ip) |
| 415 | { |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 416 | xfs_mount_t *mp = ip->i_mount; |
| 417 | xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 419 | write_lock(&pag->pag_ici_lock); |
| 420 | radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino)); |
| 421 | write_unlock(&pag->pag_ici_lock); |
| 422 | xfs_put_perag(mp, pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | mp->m_ireclaims++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* |
| 428 | * This is a wrapper routine around the xfs_ilock() routine |
| 429 | * used to centralize some grungy code. It is used in places |
| 430 | * that wish to lock the inode solely for reading the extents. |
| 431 | * The reason these places can't just call xfs_ilock(SHARED) |
| 432 | * is that the inode lock also guards to bringing in of the |
| 433 | * extents from disk for a file in b-tree format. If the inode |
| 434 | * is in b-tree format, then we need to lock the inode exclusively |
| 435 | * until the extents are read in. Locking it exclusively all |
| 436 | * the time would limit our parallelism unnecessarily, though. |
| 437 | * What we do instead is check to see if the extents have been |
| 438 | * read in yet, and only lock the inode exclusively if they |
| 439 | * have not. |
| 440 | * |
| 441 | * The function returns a value which should be given to the |
| 442 | * corresponding xfs_iunlock_map_shared(). This value is |
| 443 | * the mode in which the lock was actually taken. |
| 444 | */ |
| 445 | uint |
| 446 | xfs_ilock_map_shared( |
| 447 | xfs_inode_t *ip) |
| 448 | { |
| 449 | uint lock_mode; |
| 450 | |
| 451 | if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) && |
| 452 | ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) { |
| 453 | lock_mode = XFS_ILOCK_EXCL; |
| 454 | } else { |
| 455 | lock_mode = XFS_ILOCK_SHARED; |
| 456 | } |
| 457 | |
| 458 | xfs_ilock(ip, lock_mode); |
| 459 | |
| 460 | return lock_mode; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * This is simply the unlock routine to go with xfs_ilock_map_shared(). |
| 465 | * All it does is call xfs_iunlock() with the given lock_mode. |
| 466 | */ |
| 467 | void |
| 468 | xfs_iunlock_map_shared( |
| 469 | xfs_inode_t *ip, |
| 470 | unsigned int lock_mode) |
| 471 | { |
| 472 | xfs_iunlock(ip, lock_mode); |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * The xfs inode contains 2 locks: a multi-reader lock called the |
| 477 | * i_iolock and a multi-reader lock called the i_lock. This routine |
| 478 | * allows either or both of the locks to be obtained. |
| 479 | * |
| 480 | * The 2 locks should always be ordered so that the IO lock is |
| 481 | * obtained first in order to prevent deadlock. |
| 482 | * |
| 483 | * ip -- the inode being locked |
| 484 | * lock_flags -- this parameter indicates the inode's locks |
| 485 | * to be locked. It can be: |
| 486 | * XFS_IOLOCK_SHARED, |
| 487 | * XFS_IOLOCK_EXCL, |
| 488 | * XFS_ILOCK_SHARED, |
| 489 | * XFS_ILOCK_EXCL, |
| 490 | * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED, |
| 491 | * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL, |
| 492 | * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED, |
| 493 | * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
| 494 | */ |
| 495 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 496 | xfs_ilock( |
| 497 | xfs_inode_t *ip, |
| 498 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
| 500 | /* |
| 501 | * You can't set both SHARED and EXCL for the same lock, |
| 502 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 503 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 504 | */ |
| 505 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 506 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 507 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 508 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 509 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 511 | if (lock_flags & XFS_IOLOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 512 | mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 513 | else if (lock_flags & XFS_IOLOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 514 | mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 515 | |
| 516 | if (lock_flags & XFS_ILOCK_EXCL) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 517 | mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 518 | else if (lock_flags & XFS_ILOCK_SHARED) |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 519 | mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address); |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * This is just like xfs_ilock(), except that the caller |
| 526 | * is guaranteed not to sleep. It returns 1 if it gets |
| 527 | * the requested locks and 0 otherwise. If the IO lock is |
| 528 | * obtained but the inode lock cannot be, then the IO lock |
| 529 | * is dropped before returning. |
| 530 | * |
| 531 | * ip -- the inode being locked |
| 532 | * lock_flags -- this parameter indicates the inode's locks to be |
| 533 | * to be locked. See the comment for xfs_ilock() for a list |
| 534 | * of valid values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | */ |
| 536 | int |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 537 | xfs_ilock_nowait( |
| 538 | xfs_inode_t *ip, |
| 539 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /* |
| 542 | * You can't set both SHARED and EXCL for the same lock, |
| 543 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 544 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 545 | */ |
| 546 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 547 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 548 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 549 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 550 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (lock_flags & XFS_IOLOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 553 | if (!mrtryupdate(&ip->i_iolock)) |
| 554 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } else if (lock_flags & XFS_IOLOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 556 | if (!mrtryaccess(&ip->i_iolock)) |
| 557 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | if (lock_flags & XFS_ILOCK_EXCL) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 560 | if (!mrtryupdate(&ip->i_lock)) |
| 561 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } else if (lock_flags & XFS_ILOCK_SHARED) { |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 563 | if (!mrtryaccess(&ip->i_lock)) |
| 564 | goto out_undo_iolock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address); |
| 567 | return 1; |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 568 | |
| 569 | out_undo_iolock: |
| 570 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 571 | mrunlock_excl(&ip->i_iolock); |
| 572 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 573 | mrunlock_shared(&ip->i_iolock); |
| 574 | out: |
| 575 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* |
| 579 | * xfs_iunlock() is used to drop the inode locks acquired with |
| 580 | * xfs_ilock() and xfs_ilock_nowait(). The caller must pass |
| 581 | * in the flags given to xfs_ilock() or xfs_ilock_nowait() so |
| 582 | * that we know which locks to drop. |
| 583 | * |
| 584 | * ip -- the inode being unlocked |
| 585 | * lock_flags -- this parameter indicates the inode's locks to be |
| 586 | * to be unlocked. See the comment for xfs_ilock() for a list |
| 587 | * of valid values for this parameter. |
| 588 | * |
| 589 | */ |
| 590 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 591 | xfs_iunlock( |
| 592 | xfs_inode_t *ip, |
| 593 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
| 595 | /* |
| 596 | * You can't set both SHARED and EXCL for the same lock, |
| 597 | * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED, |
| 598 | * and XFS_ILOCK_EXCL are valid values to set in lock_flags. |
| 599 | */ |
| 600 | ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != |
| 601 | (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); |
| 602 | ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != |
| 603 | (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 604 | ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY | |
| 605 | XFS_LOCK_DEP_MASK)) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | ASSERT(lock_flags != 0); |
| 607 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 608 | if (lock_flags & XFS_IOLOCK_EXCL) |
| 609 | mrunlock_excl(&ip->i_iolock); |
| 610 | else if (lock_flags & XFS_IOLOCK_SHARED) |
| 611 | mrunlock_shared(&ip->i_iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 613 | if (lock_flags & XFS_ILOCK_EXCL) |
| 614 | mrunlock_excl(&ip->i_lock); |
| 615 | else if (lock_flags & XFS_ILOCK_SHARED) |
| 616 | mrunlock_shared(&ip->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 618 | if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) && |
| 619 | !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | /* |
| 621 | * Let the AIL know that this item has been unlocked in case |
| 622 | * it is in the AIL and anyone is waiting on it. Don't do |
| 623 | * this if the caller has asked us not to. |
| 624 | */ |
David Chinner | 783a2f6 | 2008-10-30 17:39:58 +1100 | [diff] [blame] | 625 | xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp, |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 626 | (xfs_log_item_t*)(ip->i_itemp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address); |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * give up write locks. the i/o lock cannot be held nested |
| 633 | * if it is being demoted. |
| 634 | */ |
| 635 | void |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 636 | xfs_ilock_demote( |
| 637 | xfs_inode_t *ip, |
| 638 | uint lock_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
| 640 | ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)); |
| 641 | ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); |
| 642 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 643 | if (lock_flags & XFS_ILOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | mrdemote(&ip->i_lock); |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 645 | if (lock_flags & XFS_IOLOCK_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | mrdemote(&ip->i_iolock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Christoph Hellwig | 579aa9c | 2008-04-22 17:34:00 +1000 | [diff] [blame] | 649 | #ifdef DEBUG |
| 650 | /* |
| 651 | * Debug-only routine, without additional rw_semaphore APIs, we can |
| 652 | * now only answer requests regarding whether we hold the lock for write |
| 653 | * (reader state is outside our visibility, we only track writer state). |
| 654 | * |
| 655 | * Note: this means !xfs_isilocked would give false positives, so don't do that. |
| 656 | */ |
| 657 | int |
| 658 | xfs_isilocked( |
| 659 | xfs_inode_t *ip, |
| 660 | uint lock_flags) |
| 661 | { |
| 662 | if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) == |
| 663 | XFS_ILOCK_EXCL) { |
| 664 | if (!ip->i_lock.mr_writer) |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) == |
| 669 | XFS_IOLOCK_EXCL) { |
| 670 | if (!ip->i_iolock.mr_writer) |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | return 1; |
| 675 | } |
| 676 | #endif |
| 677 | |