blob: c4414e8bce8d76a5d15305b85359b175c68ed832 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_quota.h"
40#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
David Chinner6441e542008-10-30 17:21:19 +110043 * Check the validity of the inode we just found it the cache
44 */
45static int
46xfs_iget_cache_hit(
David Chinner6441e542008-10-30 17:21:19 +110047 struct xfs_perag *pag,
48 struct xfs_inode *ip,
49 int flags,
50 int lock_flags) __releases(pag->pag_ici_lock)
51{
52 struct xfs_mount *mp = ip->i_mount;
David Chinner6441e542008-10-30 17:21:19 +110053 int error = 0;
54
55 /*
56 * If INEW is set this inode is being set up
David Chinnerbf904242008-10-30 17:36:14 +110057 * If IRECLAIM is set this inode is being torn down
David Chinner6441e542008-10-30 17:21:19 +110058 * Pause and try again.
59 */
David Chinnerbf904242008-10-30 17:36:14 +110060 if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) {
David Chinner6441e542008-10-30 17:21:19 +110061 error = EAGAIN;
62 XFS_STATS_INC(xs_ig_frecycle);
63 goto out_error;
64 }
65
David Chinnerbf904242008-10-30 17:36:14 +110066 /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
67 if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
David Chinner6441e542008-10-30 17:21:19 +110068
69 /*
David Chinnerbf904242008-10-30 17:36:14 +110070 * If lookup is racing with unlink, then we should return an
71 * error immediately so we don't remove it from the reclaim
72 * list and potentially leak the inode.
David Chinner6441e542008-10-30 17:21:19 +110073 */
David Chinnerbf904242008-10-30 17:36:14 +110074
75 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
David Chinner6441e542008-10-30 17:21:19 +110076 error = ENOENT;
77 goto out_error;
78 }
David Chinnerbf904242008-10-30 17:36:14 +110079
David Chinner6441e542008-10-30 17:21:19 +110080 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
81
David Chinnerbf904242008-10-30 17:36:14 +110082 /*
83 * We need to re-initialise the VFS inode as it has been
84 * 'freed' by the VFS. Do this here so we can deal with
85 * errors cleanly, then tag it so it can be set up correctly
86 * later.
87 */
88 if (!inode_init_always(mp->m_super, VFS_I(ip))) {
89 error = ENOMEM;
90 goto out_error;
91 }
92 xfs_iflags_set(ip, XFS_INEW);
David Chinner6441e542008-10-30 17:21:19 +110093 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
94 read_unlock(&pag->pag_ici_lock);
95
96 XFS_MOUNT_ILOCK(mp);
97 list_del_init(&ip->i_reclaim);
98 XFS_MOUNT_IUNLOCK(mp);
David Chinnerbf904242008-10-30 17:36:14 +110099 } else if (!igrab(VFS_I(ip))) {
100 /* If the VFS inode is being torn down, pause and try again. */
101 error = EAGAIN;
102 XFS_STATS_INC(xs_ig_frecycle);
103 goto out_error;
David Chinner6441e542008-10-30 17:21:19 +1100104 } else {
David Chinnerbf904242008-10-30 17:36:14 +1100105 /* we've got a live one */
David Chinner6441e542008-10-30 17:21:19 +1100106 read_unlock(&pag->pag_ici_lock);
107 }
108
109 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
110 error = ENOENT;
111 goto out;
112 }
113
114 if (lock_flags != 0)
115 xfs_ilock(ip, lock_flags);
116
117 xfs_iflags_clear(ip, XFS_ISTALE);
118 xfs_itrace_exit_tag(ip, "xfs_iget.found");
119 XFS_STATS_INC(xs_ig_found);
120 return 0;
121
122out_error:
123 read_unlock(&pag->pag_ici_lock);
124out:
125 return error;
126}
127
128
129static int
130xfs_iget_cache_miss(
131 struct xfs_mount *mp,
132 struct xfs_perag *pag,
133 xfs_trans_t *tp,
134 xfs_ino_t ino,
135 struct xfs_inode **ipp,
136 xfs_daddr_t bno,
137 int flags,
138 int lock_flags) __releases(pag->pag_ici_lock)
139{
140 struct xfs_inode *ip;
141 int error;
142 unsigned long first_index, mask;
143 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
144
145 /*
146 * Read the disk inode attributes into a new inode structure and get
147 * a new vnode for it. This should also initialize i_ino and i_mount.
148 */
149 error = xfs_iread(mp, tp, ino, &ip, bno,
150 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
151 if (error)
152 return error;
153
154 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
155
156 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
157 error = ENOENT;
158 goto out_destroy;
159 }
160
161 /*
162 * Preload the radix tree so we can insert safely under the
163 * write spinlock.
164 */
165 if (radix_tree_preload(GFP_KERNEL)) {
166 error = EAGAIN;
167 goto out_destroy;
168 }
169
170 if (lock_flags)
171 xfs_ilock(ip, lock_flags);
172
173 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
174 first_index = agino & mask;
175 write_lock(&pag->pag_ici_lock);
176
177 /* insert the new inode */
178 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
179 if (unlikely(error)) {
180 WARN_ON(error != -EEXIST);
181 XFS_STATS_INC(xs_ig_dup);
182 error = EAGAIN;
183 goto out_unlock;
184 }
185
186 /* These values _must_ be set before releasing the radix tree lock! */
187 ip->i_udquot = ip->i_gdquot = NULL;
188 xfs_iflags_set(ip, XFS_INEW);
189
190 write_unlock(&pag->pag_ici_lock);
191 radix_tree_preload_end();
192 *ipp = ip;
193 return 0;
194
195out_unlock:
196 write_unlock(&pag->pag_ici_lock);
197 radix_tree_preload_end();
198out_destroy:
199 xfs_idestroy(ip);
200 return error;
201}
202
203/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000205 * The inode is looked up in the cache held in each AG.
David Chinnerbf904242008-10-30 17:36:14 +1100206 * If the inode is found in the cache, initialise the vfs inode
207 * if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
David Chinnerda353b02007-08-28 14:00:13 +1000209 * If it is not in core, read it in from the file system's device,
David Chinnerbf904242008-10-30 17:36:14 +1100210 * add it to the cache and initialise the vfs inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 *
212 * The inode is locked according to the value of the lock_flags parameter.
213 * This flag parameter indicates how and if the inode's IO lock and inode lock
214 * should be taken.
215 *
216 * mp -- the mount point structure for the current file system. It points
217 * to the inode hash table.
218 * tp -- a pointer to the current transaction if there is one. This is
219 * simply passed through to the xfs_iread() call.
220 * ino -- the number of the inode desired. This is the unique identifier
221 * within the file system for the inode being requested.
222 * lock_flags -- flags indicating how to lock the inode. See the comment
223 * for xfs_ilock() for a list of valid values.
224 * bno -- the block number starting the buffer containing the inode,
225 * if known (as by bulkstat), else 0.
226 */
David Chinnerbf904242008-10-30 17:36:14 +1100227int
228xfs_iget(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 xfs_mount_t *mp,
230 xfs_trans_t *tp,
231 xfs_ino_t ino,
232 uint flags,
233 uint lock_flags,
234 xfs_inode_t **ipp,
235 xfs_daddr_t bno)
236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000239 xfs_perag_t *pag;
240 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
David Chinnerda353b02007-08-28 14:00:13 +1000242 /* the radix tree exists only in inode capable AGs */
243 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
244 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David Chinnerda353b02007-08-28 14:00:13 +1000246 /* get the perag structure and ensure that it's inode capable */
247 pag = xfs_get_perag(mp, ino);
248 if (!pag->pagi_inodeok)
249 return EINVAL;
250 ASSERT(pag->pag_ici_init);
251 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253again:
David Chinner6441e542008-10-30 17:21:19 +1100254 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000255 read_lock(&pag->pag_ici_lock);
256 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David Chinner6441e542008-10-30 17:21:19 +1100258 if (ip) {
David Chinnerbf904242008-10-30 17:36:14 +1100259 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100260 if (error)
261 goto out_error_or_again;
262 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000263 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100264 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000265
David Chinner6441e542008-10-30 17:21:19 +1100266 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
267 flags, lock_flags);
268 if (error)
269 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
David Chinnerda353b02007-08-28 14:00:13 +1000271 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000273 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *ipp = ip;
275
David Chinnerbf904242008-10-30 17:36:14 +1100276 ASSERT(ip->i_df.if_ext_max ==
277 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000278 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * If we have a real type for an on-disk inode, we can set ops(&unlock)
280 * now. If it's a new inode being created, xfs_ialloc will handle it.
281 */
David Chinnerbf904242008-10-30 17:36:14 +1100282 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000283 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100285
286out_error_or_again:
287 if (error == EAGAIN) {
288 delay(1);
289 goto again;
290 }
291 xfs_put_perag(mp, pag);
292 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295
296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Look for the inode corresponding to the given ino in the hash table.
298 * If it is there and its i_transp pointer matches tp, return it.
299 * Otherwise, return NULL.
300 */
301xfs_inode_t *
302xfs_inode_incore(xfs_mount_t *mp,
303 xfs_ino_t ino,
304 xfs_trans_t *tp)
305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 xfs_inode_t *ip;
David Chinnerda353b02007-08-28 14:00:13 +1000307 xfs_perag_t *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
David Chinnerda353b02007-08-28 14:00:13 +1000309 pag = xfs_get_perag(mp, ino);
310 read_lock(&pag->pag_ici_lock);
311 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
312 read_unlock(&pag->pag_ici_lock);
313 xfs_put_perag(mp, pag);
314
315 /* the returned inode must match the transaction */
316 if (ip && (ip->i_transp != tp))
317 return NULL;
318 return ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
322 * Decrement reference count of an inode structure and unlock it.
323 *
324 * ip -- the inode being released
325 * lock_flags -- this parameter indicates the inode's locks to be
326 * to be released. See the comment on xfs_iunlock() for a list
327 * of valid values.
328 */
329void
330xfs_iput(xfs_inode_t *ip,
331 uint lock_flags)
332{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100333 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000335 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/*
339 * Special iput for brand-new inodes that are still locked
340 */
341void
David Chinner01651642008-08-13 15:45:15 +1000342xfs_iput_new(
343 xfs_inode_t *ip,
344 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
David Chinner01651642008-08-13 15:45:15 +1000346 struct inode *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100348 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100351 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Christoph Hellwig10090be2007-10-11 18:11:03 +1000352 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 if (inode->i_state & I_NEW)
355 unlock_new_inode(inode);
356 if (lock_flags)
357 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000358 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361
362/*
363 * This routine embodies the part of the reclaim code that pulls
364 * the inode from the inode hash table and the mount structure's
365 * inode list.
366 * This should only be called from xfs_reclaim().
367 */
368void
369xfs_ireclaim(xfs_inode_t *ip)
370{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /*
372 * Remove from old hash list and mount list.
373 */
374 XFS_STATS_INC(xs_ig_reclaims);
375
376 xfs_iextract(ip);
377
378 /*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100379 * Here we do a spurious inode lock in order to coordinate with inode
380 * cache radix tree lookups. This is because the lookup can reference
381 * the inodes in the cache without taking references. We make that OK
382 * here by ensuring that we wait until the inode is unlocked after the
383 * lookup before we go ahead and free it. We get both the ilock and
384 * the iolock because the code may need to drop the ilock one but will
385 * still hold the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
387 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
388
389 /*
390 * Release dquots (and their references) if any. An inode may escape
391 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
392 */
393 XFS_QM_DQDETACH(ip->i_mount, ip);
394
395 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * Free all memory associated with the inode.
397 */
Tim Shimmin439b8432006-11-11 18:04:34 +1100398 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 xfs_idestroy(ip);
400}
401
402/*
403 * This routine removes an about-to-be-destroyed inode from
404 * all of the lists in which it is located with the exception
405 * of the behavior chain.
406 */
407void
408xfs_iextract(
409 xfs_inode_t *ip)
410{
David Chinnerda353b02007-08-28 14:00:13 +1000411 xfs_mount_t *mp = ip->i_mount;
412 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
David Chinnerda353b02007-08-28 14:00:13 +1000414 write_lock(&pag->pag_ici_lock);
415 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
416 write_unlock(&pag->pag_ici_lock);
417 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Deal with the deleted inodes list */
David Chinner6c7699c2008-10-30 17:11:29 +1100420 XFS_MOUNT_ILOCK(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 list_del_init(&ip->i_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 mp->m_ireclaims++;
423 XFS_MOUNT_IUNLOCK(mp);
424}
425
426/*
427 * This is a wrapper routine around the xfs_ilock() routine
428 * used to centralize some grungy code. It is used in places
429 * that wish to lock the inode solely for reading the extents.
430 * The reason these places can't just call xfs_ilock(SHARED)
431 * is that the inode lock also guards to bringing in of the
432 * extents from disk for a file in b-tree format. If the inode
433 * is in b-tree format, then we need to lock the inode exclusively
434 * until the extents are read in. Locking it exclusively all
435 * the time would limit our parallelism unnecessarily, though.
436 * What we do instead is check to see if the extents have been
437 * read in yet, and only lock the inode exclusively if they
438 * have not.
439 *
440 * The function returns a value which should be given to the
441 * corresponding xfs_iunlock_map_shared(). This value is
442 * the mode in which the lock was actually taken.
443 */
444uint
445xfs_ilock_map_shared(
446 xfs_inode_t *ip)
447{
448 uint lock_mode;
449
450 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
451 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
452 lock_mode = XFS_ILOCK_EXCL;
453 } else {
454 lock_mode = XFS_ILOCK_SHARED;
455 }
456
457 xfs_ilock(ip, lock_mode);
458
459 return lock_mode;
460}
461
462/*
463 * This is simply the unlock routine to go with xfs_ilock_map_shared().
464 * All it does is call xfs_iunlock() with the given lock_mode.
465 */
466void
467xfs_iunlock_map_shared(
468 xfs_inode_t *ip,
469 unsigned int lock_mode)
470{
471 xfs_iunlock(ip, lock_mode);
472}
473
474/*
475 * The xfs inode contains 2 locks: a multi-reader lock called the
476 * i_iolock and a multi-reader lock called the i_lock. This routine
477 * allows either or both of the locks to be obtained.
478 *
479 * The 2 locks should always be ordered so that the IO lock is
480 * obtained first in order to prevent deadlock.
481 *
482 * ip -- the inode being locked
483 * lock_flags -- this parameter indicates the inode's locks
484 * to be locked. It can be:
485 * XFS_IOLOCK_SHARED,
486 * XFS_IOLOCK_EXCL,
487 * XFS_ILOCK_SHARED,
488 * XFS_ILOCK_EXCL,
489 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
490 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
491 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
492 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
493 */
494void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000495xfs_ilock(
496 xfs_inode_t *ip,
497 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 /*
500 * You can't set both SHARED and EXCL for the same lock,
501 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
502 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
503 */
504 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
505 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
506 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
507 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000508 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000510 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000511 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000512 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000513 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000514
515 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000516 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000517 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000518 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
521}
522
523/*
524 * This is just like xfs_ilock(), except that the caller
525 * is guaranteed not to sleep. It returns 1 if it gets
526 * the requested locks and 0 otherwise. If the IO lock is
527 * obtained but the inode lock cannot be, then the IO lock
528 * is dropped before returning.
529 *
530 * ip -- the inode being locked
531 * lock_flags -- this parameter indicates the inode's locks to be
532 * to be locked. See the comment for xfs_ilock() for a list
533 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000536xfs_ilock_nowait(
537 xfs_inode_t *ip,
538 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * You can't set both SHARED and EXCL for the same lock,
542 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
543 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
544 */
545 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
546 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
547 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
548 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000549 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000552 if (!mrtryupdate(&ip->i_iolock))
553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000555 if (!mrtryaccess(&ip->i_iolock))
556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000559 if (!mrtryupdate(&ip->i_lock))
560 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000562 if (!mrtryaccess(&ip->i_lock))
563 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
566 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000567
568 out_undo_iolock:
569 if (lock_flags & XFS_IOLOCK_EXCL)
570 mrunlock_excl(&ip->i_iolock);
571 else if (lock_flags & XFS_IOLOCK_SHARED)
572 mrunlock_shared(&ip->i_iolock);
573 out:
574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * xfs_iunlock() is used to drop the inode locks acquired with
579 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
580 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
581 * that we know which locks to drop.
582 *
583 * ip -- the inode being unlocked
584 * lock_flags -- this parameter indicates the inode's locks to be
585 * to be unlocked. See the comment for xfs_ilock() for a list
586 * of valid values for this parameter.
587 *
588 */
589void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000590xfs_iunlock(
591 xfs_inode_t *ip,
592 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 /*
595 * You can't set both SHARED and EXCL for the same lock,
596 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
597 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
598 */
599 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
600 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
601 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
602 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000603 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
604 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ASSERT(lock_flags != 0);
606
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000607 if (lock_flags & XFS_IOLOCK_EXCL)
608 mrunlock_excl(&ip->i_iolock);
609 else if (lock_flags & XFS_IOLOCK_SHARED)
610 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000612 if (lock_flags & XFS_ILOCK_EXCL)
613 mrunlock_excl(&ip->i_lock);
614 else if (lock_flags & XFS_ILOCK_SHARED)
615 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000617 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
618 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /*
620 * Let the AIL know that this item has been unlocked in case
621 * it is in the AIL and anyone is waiting on it. Don't do
622 * this if the caller has asked us not to.
623 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000624 xfs_trans_unlocked_item(ip->i_mount,
625 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
628}
629
630/*
631 * give up write locks. the i/o lock cannot be held nested
632 * if it is being demoted.
633 */
634void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000635xfs_ilock_demote(
636 xfs_inode_t *ip,
637 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
640 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
641
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000642 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000644 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 mrdemote(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000648#ifdef DEBUG
649/*
650 * Debug-only routine, without additional rw_semaphore APIs, we can
651 * now only answer requests regarding whether we hold the lock for write
652 * (reader state is outside our visibility, we only track writer state).
653 *
654 * Note: this means !xfs_isilocked would give false positives, so don't do that.
655 */
656int
657xfs_isilocked(
658 xfs_inode_t *ip,
659 uint lock_flags)
660{
661 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
662 XFS_ILOCK_EXCL) {
663 if (!ip->i_lock.mr_writer)
664 return 0;
665 }
666
667 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
668 XFS_IOLOCK_EXCL) {
669 if (!ip->i_iolock.mr_writer)
670 return 0;
671 }
672
673 return 1;
674}
675#endif
676