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