[XFS] kill the XFS_IMAP_BULKSTAT flag
[safe/jmp/linux-2.6] / fs / xfs / xfs_iget.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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
7  * published by the Free Software Foundation.
8  *
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.
13  *
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
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_quota.h"
40 #include "xfs_utils.h"
41 #include "xfs_trans_priv.h"
42 #include "xfs_inode_item.h"
43
44 /*
45  * Check the validity of the inode we just found it the cache
46  */
47 static int
48 xfs_iget_cache_hit(
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;
55         int                     error = EAGAIN;
56
57         /*
58          * If INEW is set this inode is being set up
59          * If IRECLAIM is set this inode is being torn down
60          * Pause and try again.
61          */
62         if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) {
63                 XFS_STATS_INC(xs_ig_frecycle);
64                 goto out_error;
65         }
66
67         /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
68         if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
69
70                 /*
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.
74                  */
75                 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
76                         error = ENOENT;
77                         goto out_error;
78                 }
79
80                 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
81
82                 /*
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
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                  */
100                 xfs_iflags_set(ip, XFS_INEW);
101                 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
102
103                 /* clear the radix tree reclaim flag as well. */
104                 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
105         } else if (!igrab(VFS_I(ip))) {
106                 /* If the VFS inode is being torn down, pause and try again. */
107                 XFS_STATS_INC(xs_ig_frecycle);
108                 goto out_error;
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));
117         }
118
119         if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
120                 error = ENOENT;
121                 iput(VFS_I(ip));
122                 goto out_error;
123         }
124
125         /* We've got a live one. */
126         read_unlock(&pag->pag_ici_lock);
127
128         if (lock_flags != 0)
129                 xfs_ilock(ip, lock_flags);
130
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;
135
136 out_error:
137         read_unlock(&pag->pag_ici_lock);
138         return error;
139 }
140
141
142 static int
143 xfs_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);
157
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          */
162         error = xfs_iread(mp, tp, ino, &ip, bno, flags);
163         if (error)
164                 return error;
165
166         xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
167
168         if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
169                 error = ENOENT;
170                 goto out_destroy;
171         }
172
173         if (lock_flags)
174                 xfs_ilock(ip, lock_flags);
175
176         /*
177          * Preload the radix tree so we can insert safely under the
178          * write spinlock. Note that we cannot sleep inside the preload
179          * region.
180          */
181         if (radix_tree_preload(GFP_KERNEL)) {
182                 error = EAGAIN;
183                 goto out_unlock;
184         }
185
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);
189
190         /* insert the new inode */
191         error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
192         if (unlikely(error)) {
193                 WARN_ON(error != -EEXIST);
194                 XFS_STATS_INC(xs_ig_dup);
195                 error = EAGAIN;
196                 goto out_preload_end;
197         }
198
199         /* These values _must_ be set before releasing the radix tree lock! */
200         ip->i_udquot = ip->i_gdquot = NULL;
201         xfs_iflags_set(ip, XFS_INEW);
202
203         write_unlock(&pag->pag_ici_lock);
204         radix_tree_preload_end();
205         *ipp = ip;
206         return 0;
207
208 out_preload_end:
209         write_unlock(&pag->pag_ici_lock);
210         radix_tree_preload_end();
211 out_unlock:
212         if (lock_flags)
213                 xfs_iunlock(ip, lock_flags);
214 out_destroy:
215         xfs_destroy_inode(ip);
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.
222  * If the inode is found in the cache, initialise the vfs inode
223  * if necessary.
224  *
225  * If it is not in core, read it in from the file system's device,
226  * add it to the cache and initialise the vfs inode.
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  */
243 int
244 xfs_iget(
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
269 again:
270         error = 0;
271         read_lock(&pag->pag_ici_lock);
272         ip = radix_tree_lookup(&pag->pag_ici_root, agino);
273
274         if (ip) {
275                 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
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         }
287         xfs_put_perag(mp, pag);
288
289         xfs_iflags_set(ip, XFS_IMODIFIED);
290         *ipp = ip;
291
292         ASSERT(ip->i_df.if_ext_max ==
293                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
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          */
298         if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
299                 xfs_setup_inode(ip);
300         return 0;
301
302 out_error_or_again:
303         if (error == EAGAIN) {
304                 delay(1);
305                 goto again;
306         }
307         xfs_put_perag(mp, pag);
308         return error;
309 }
310
311
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  */
317 xfs_inode_t *
318 xfs_inode_incore(xfs_mount_t    *mp,
319                  xfs_ino_t      ino,
320                  xfs_trans_t    *tp)
321 {
322         xfs_inode_t     *ip;
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;
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  */
345 void
346 xfs_iput(xfs_inode_t    *ip,
347          uint           lock_flags)
348 {
349         xfs_itrace_entry(ip);
350         xfs_iunlock(ip, lock_flags);
351         IRELE(ip);
352 }
353
354 /*
355  * Special iput for brand-new inodes that are still locked
356  */
357 void
358 xfs_iput_new(
359         xfs_inode_t     *ip,
360         uint            lock_flags)
361 {
362         struct inode    *inode = VFS_I(ip);
363
364         xfs_itrace_entry(ip);
365
366         if ((ip->i_d.di_mode == 0)) {
367                 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
368                 make_bad_inode(inode);
369         }
370         if (inode->i_state & I_NEW)
371                 unlock_new_inode(inode);
372         if (lock_flags)
373                 xfs_iunlock(ip, lock_flags);
374         IRELE(ip);
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  */
384 void
385 xfs_ireclaim(xfs_inode_t *ip)
386 {
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         /*
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.
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
411         /*
412          * Free all memory associated with the inode.
413          */
414         xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
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  */
423 void
424 xfs_iextract(
425         xfs_inode_t     *ip)
426 {
427         xfs_mount_t     *mp = ip->i_mount;
428         xfs_perag_t     *pag = xfs_get_perag(mp, ip->i_ino);
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);
434
435         mp->m_ireclaims++;
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  */
456 uint
457 xfs_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  */
478 void
479 xfs_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  */
506 void
507 xfs_ilock(
508         xfs_inode_t             *ip,
509         uint                    lock_flags)
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));
520         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
521
522         if (lock_flags & XFS_IOLOCK_EXCL)
523                 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
524         else if (lock_flags & XFS_IOLOCK_SHARED)
525                 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
526
527         if (lock_flags & XFS_ILOCK_EXCL)
528                 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
529         else if (lock_flags & XFS_ILOCK_SHARED)
530                 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
531
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.
546  */
547 int
548 xfs_ilock_nowait(
549         xfs_inode_t             *ip,
550         uint                    lock_flags)
551 {
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));
561         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
562
563         if (lock_flags & XFS_IOLOCK_EXCL) {
564                 if (!mrtryupdate(&ip->i_iolock))
565                         goto out;
566         } else if (lock_flags & XFS_IOLOCK_SHARED) {
567                 if (!mrtryaccess(&ip->i_iolock))
568                         goto out;
569         }
570         if (lock_flags & XFS_ILOCK_EXCL) {
571                 if (!mrtryupdate(&ip->i_lock))
572                         goto out_undo_iolock;
573         } else if (lock_flags & XFS_ILOCK_SHARED) {
574                 if (!mrtryaccess(&ip->i_lock))
575                         goto out_undo_iolock;
576         }
577         xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
578         return 1;
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;
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  */
601 void
602 xfs_iunlock(
603         xfs_inode_t             *ip,
604         uint                    lock_flags)
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));
615         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
616                         XFS_LOCK_DEP_MASK)) == 0);
617         ASSERT(lock_flags != 0);
618
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);
623
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);
628
629         if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
630             !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
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                  */
636                 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
637                                         (xfs_log_item_t*)(ip->i_itemp));
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  */
646 void
647 xfs_ilock_demote(
648         xfs_inode_t             *ip,
649         uint                    lock_flags)
650 {
651         ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
652         ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
653
654         if (lock_flags & XFS_ILOCK_EXCL)
655                 mrdemote(&ip->i_lock);
656         if (lock_flags & XFS_IOLOCK_EXCL)
657                 mrdemote(&ip->i_iolock);
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  */
668 int
669 xfs_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;
677         }
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;
686 }
687 #endif
688