Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[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,
163                           (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
164         if (error)
165                 return error;
166
167         xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
168
169         if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
170                 error = ENOENT;
171                 goto out_destroy;
172         }
173
174         if (lock_flags)
175                 xfs_ilock(ip, lock_flags);
176
177         /*
178          * Preload the radix tree so we can insert safely under the
179          * write spinlock. Note that we cannot sleep inside the preload
180          * region.
181          */
182         if (radix_tree_preload(GFP_KERNEL)) {
183                 error = EAGAIN;
184                 goto out_unlock;
185         }
186
187         mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
188         first_index = agino & mask;
189         write_lock(&pag->pag_ici_lock);
190
191         /* insert the new inode */
192         error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
193         if (unlikely(error)) {
194                 WARN_ON(error != -EEXIST);
195                 XFS_STATS_INC(xs_ig_dup);
196                 error = EAGAIN;
197                 goto out_preload_end;
198         }
199
200         /* These values _must_ be set before releasing the radix tree lock! */
201         ip->i_udquot = ip->i_gdquot = NULL;
202         xfs_iflags_set(ip, XFS_INEW);
203
204         write_unlock(&pag->pag_ici_lock);
205         radix_tree_preload_end();
206         *ipp = ip;
207         return 0;
208
209 out_preload_end:
210         write_unlock(&pag->pag_ici_lock);
211         radix_tree_preload_end();
212 out_unlock:
213         if (lock_flags)
214                 xfs_iunlock(ip, lock_flags);
215 out_destroy:
216         xfs_destroy_inode(ip);
217         return error;
218 }
219
220 /*
221  * Look up an inode by number in the given file system.
222  * The inode is looked up in the cache held in each AG.
223  * If the inode is found in the cache, initialise the vfs inode
224  * if necessary.
225  *
226  * If it is not in core, read it in from the file system's device,
227  * add it to the cache and initialise the vfs inode.
228  *
229  * The inode is locked according to the value of the lock_flags parameter.
230  * This flag parameter indicates how and if the inode's IO lock and inode lock
231  * should be taken.
232  *
233  * mp -- the mount point structure for the current file system.  It points
234  *       to the inode hash table.
235  * tp -- a pointer to the current transaction if there is one.  This is
236  *       simply passed through to the xfs_iread() call.
237  * ino -- the number of the inode desired.  This is the unique identifier
238  *        within the file system for the inode being requested.
239  * lock_flags -- flags indicating how to lock the inode.  See the comment
240  *               for xfs_ilock() for a list of valid values.
241  * bno -- the block number starting the buffer containing the inode,
242  *        if known (as by bulkstat), else 0.
243  */
244 int
245 xfs_iget(
246         xfs_mount_t     *mp,
247         xfs_trans_t     *tp,
248         xfs_ino_t       ino,
249         uint            flags,
250         uint            lock_flags,
251         xfs_inode_t     **ipp,
252         xfs_daddr_t     bno)
253 {
254         xfs_inode_t     *ip;
255         int             error;
256         xfs_perag_t     *pag;
257         xfs_agino_t     agino;
258
259         /* the radix tree exists only in inode capable AGs */
260         if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
261                 return EINVAL;
262
263         /* get the perag structure and ensure that it's inode capable */
264         pag = xfs_get_perag(mp, ino);
265         if (!pag->pagi_inodeok)
266                 return EINVAL;
267         ASSERT(pag->pag_ici_init);
268         agino = XFS_INO_TO_AGINO(mp, ino);
269
270 again:
271         error = 0;
272         read_lock(&pag->pag_ici_lock);
273         ip = radix_tree_lookup(&pag->pag_ici_root, agino);
274
275         if (ip) {
276                 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
277                 if (error)
278                         goto out_error_or_again;
279         } else {
280                 read_unlock(&pag->pag_ici_lock);
281                 XFS_STATS_INC(xs_ig_missed);
282
283                 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
284                                                         flags, lock_flags);
285                 if (error)
286                         goto out_error_or_again;
287         }
288         xfs_put_perag(mp, pag);
289
290         xfs_iflags_set(ip, XFS_IMODIFIED);
291         *ipp = ip;
292
293         ASSERT(ip->i_df.if_ext_max ==
294                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
295         /*
296          * If we have a real type for an on-disk inode, we can set ops(&unlock)
297          * now.  If it's a new inode being created, xfs_ialloc will handle it.
298          */
299         if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
300                 xfs_setup_inode(ip);
301         return 0;
302
303 out_error_or_again:
304         if (error == EAGAIN) {
305                 delay(1);
306                 goto again;
307         }
308         xfs_put_perag(mp, pag);
309         return error;
310 }
311
312
313 /*
314  * Look for the inode corresponding to the given ino in the hash table.
315  * If it is there and its i_transp pointer matches tp, return it.
316  * Otherwise, return NULL.
317  */
318 xfs_inode_t *
319 xfs_inode_incore(xfs_mount_t    *mp,
320                  xfs_ino_t      ino,
321                  xfs_trans_t    *tp)
322 {
323         xfs_inode_t     *ip;
324         xfs_perag_t     *pag;
325
326         pag = xfs_get_perag(mp, ino);
327         read_lock(&pag->pag_ici_lock);
328         ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
329         read_unlock(&pag->pag_ici_lock);
330         xfs_put_perag(mp, pag);
331
332         /* the returned inode must match the transaction */
333         if (ip && (ip->i_transp != tp))
334                 return NULL;
335         return ip;
336 }
337
338 /*
339  * Decrement reference count of an inode structure and unlock it.
340  *
341  * ip -- the inode being released
342  * lock_flags -- this parameter indicates the inode's locks to be
343  *       to be released.  See the comment on xfs_iunlock() for a list
344  *       of valid values.
345  */
346 void
347 xfs_iput(xfs_inode_t    *ip,
348          uint           lock_flags)
349 {
350         xfs_itrace_entry(ip);
351         xfs_iunlock(ip, lock_flags);
352         IRELE(ip);
353 }
354
355 /*
356  * Special iput for brand-new inodes that are still locked
357  */
358 void
359 xfs_iput_new(
360         xfs_inode_t     *ip,
361         uint            lock_flags)
362 {
363         struct inode    *inode = VFS_I(ip);
364
365         xfs_itrace_entry(ip);
366
367         if ((ip->i_d.di_mode == 0)) {
368                 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
369                 make_bad_inode(inode);
370         }
371         if (inode->i_state & I_NEW)
372                 unlock_new_inode(inode);
373         if (lock_flags)
374                 xfs_iunlock(ip, lock_flags);
375         IRELE(ip);
376 }
377
378
379 /*
380  * This routine embodies the part of the reclaim code that pulls
381  * the inode from the inode hash table and the mount structure's
382  * inode list.
383  * This should only be called from xfs_reclaim().
384  */
385 void
386 xfs_ireclaim(xfs_inode_t *ip)
387 {
388         /*
389          * Remove from old hash list and mount list.
390          */
391         XFS_STATS_INC(xs_ig_reclaims);
392
393         xfs_iextract(ip);
394
395         /*
396          * Here we do a spurious inode lock in order to coordinate with inode
397          * cache radix tree lookups.  This is because the lookup can reference
398          * the inodes in the cache without taking references.  We make that OK
399          * here by ensuring that we wait until the inode is unlocked after the
400          * lookup before we go ahead and free it.  We get both the ilock and
401          * the iolock because the code may need to drop the ilock one but will
402          * still hold the iolock.
403          */
404         xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
405
406         /*
407          * Release dquots (and their references) if any. An inode may escape
408          * xfs_inactive and get here via vn_alloc->vn_reclaim path.
409          */
410         XFS_QM_DQDETACH(ip->i_mount, ip);
411
412         /*
413          * Free all memory associated with the inode.
414          */
415         xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
416         xfs_idestroy(ip);
417 }
418
419 /*
420  * This routine removes an about-to-be-destroyed inode from
421  * all of the lists in which it is located with the exception
422  * of the behavior chain.
423  */
424 void
425 xfs_iextract(
426         xfs_inode_t     *ip)
427 {
428         xfs_mount_t     *mp = ip->i_mount;
429         xfs_perag_t     *pag = xfs_get_perag(mp, ip->i_ino);
430
431         write_lock(&pag->pag_ici_lock);
432         radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
433         write_unlock(&pag->pag_ici_lock);
434         xfs_put_perag(mp, pag);
435
436         mp->m_ireclaims++;
437 }
438
439 /*
440  * This is a wrapper routine around the xfs_ilock() routine
441  * used to centralize some grungy code.  It is used in places
442  * that wish to lock the inode solely for reading the extents.
443  * The reason these places can't just call xfs_ilock(SHARED)
444  * is that the inode lock also guards to bringing in of the
445  * extents from disk for a file in b-tree format.  If the inode
446  * is in b-tree format, then we need to lock the inode exclusively
447  * until the extents are read in.  Locking it exclusively all
448  * the time would limit our parallelism unnecessarily, though.
449  * What we do instead is check to see if the extents have been
450  * read in yet, and only lock the inode exclusively if they
451  * have not.
452  *
453  * The function returns a value which should be given to the
454  * corresponding xfs_iunlock_map_shared().  This value is
455  * the mode in which the lock was actually taken.
456  */
457 uint
458 xfs_ilock_map_shared(
459         xfs_inode_t     *ip)
460 {
461         uint    lock_mode;
462
463         if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
464             ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
465                 lock_mode = XFS_ILOCK_EXCL;
466         } else {
467                 lock_mode = XFS_ILOCK_SHARED;
468         }
469
470         xfs_ilock(ip, lock_mode);
471
472         return lock_mode;
473 }
474
475 /*
476  * This is simply the unlock routine to go with xfs_ilock_map_shared().
477  * All it does is call xfs_iunlock() with the given lock_mode.
478  */
479 void
480 xfs_iunlock_map_shared(
481         xfs_inode_t     *ip,
482         unsigned int    lock_mode)
483 {
484         xfs_iunlock(ip, lock_mode);
485 }
486
487 /*
488  * The xfs inode contains 2 locks: a multi-reader lock called the
489  * i_iolock and a multi-reader lock called the i_lock.  This routine
490  * allows either or both of the locks to be obtained.
491  *
492  * The 2 locks should always be ordered so that the IO lock is
493  * obtained first in order to prevent deadlock.
494  *
495  * ip -- the inode being locked
496  * lock_flags -- this parameter indicates the inode's locks
497  *       to be locked.  It can be:
498  *              XFS_IOLOCK_SHARED,
499  *              XFS_IOLOCK_EXCL,
500  *              XFS_ILOCK_SHARED,
501  *              XFS_ILOCK_EXCL,
502  *              XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
503  *              XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
504  *              XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
505  *              XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
506  */
507 void
508 xfs_ilock(
509         xfs_inode_t             *ip,
510         uint                    lock_flags)
511 {
512         /*
513          * You can't set both SHARED and EXCL for the same lock,
514          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
515          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
516          */
517         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
518                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
519         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
520                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
521         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
522
523         if (lock_flags & XFS_IOLOCK_EXCL)
524                 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
525         else if (lock_flags & XFS_IOLOCK_SHARED)
526                 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
527
528         if (lock_flags & XFS_ILOCK_EXCL)
529                 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
530         else if (lock_flags & XFS_ILOCK_SHARED)
531                 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
532
533         xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
534 }
535
536 /*
537  * This is just like xfs_ilock(), except that the caller
538  * is guaranteed not to sleep.  It returns 1 if it gets
539  * the requested locks and 0 otherwise.  If the IO lock is
540  * obtained but the inode lock cannot be, then the IO lock
541  * is dropped before returning.
542  *
543  * ip -- the inode being locked
544  * lock_flags -- this parameter indicates the inode's locks to be
545  *       to be locked.  See the comment for xfs_ilock() for a list
546  *       of valid values.
547  */
548 int
549 xfs_ilock_nowait(
550         xfs_inode_t             *ip,
551         uint                    lock_flags)
552 {
553         /*
554          * You can't set both SHARED and EXCL for the same lock,
555          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
556          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
557          */
558         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
559                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
560         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
561                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
562         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
563
564         if (lock_flags & XFS_IOLOCK_EXCL) {
565                 if (!mrtryupdate(&ip->i_iolock))
566                         goto out;
567         } else if (lock_flags & XFS_IOLOCK_SHARED) {
568                 if (!mrtryaccess(&ip->i_iolock))
569                         goto out;
570         }
571         if (lock_flags & XFS_ILOCK_EXCL) {
572                 if (!mrtryupdate(&ip->i_lock))
573                         goto out_undo_iolock;
574         } else if (lock_flags & XFS_ILOCK_SHARED) {
575                 if (!mrtryaccess(&ip->i_lock))
576                         goto out_undo_iolock;
577         }
578         xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
579         return 1;
580
581  out_undo_iolock:
582         if (lock_flags & XFS_IOLOCK_EXCL)
583                 mrunlock_excl(&ip->i_iolock);
584         else if (lock_flags & XFS_IOLOCK_SHARED)
585                 mrunlock_shared(&ip->i_iolock);
586  out:
587         return 0;
588 }
589
590 /*
591  * xfs_iunlock() is used to drop the inode locks acquired with
592  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
593  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
594  * that we know which locks to drop.
595  *
596  * ip -- the inode being unlocked
597  * lock_flags -- this parameter indicates the inode's locks to be
598  *       to be unlocked.  See the comment for xfs_ilock() for a list
599  *       of valid values for this parameter.
600  *
601  */
602 void
603 xfs_iunlock(
604         xfs_inode_t             *ip,
605         uint                    lock_flags)
606 {
607         /*
608          * You can't set both SHARED and EXCL for the same lock,
609          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
610          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
611          */
612         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
613                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
614         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
615                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
616         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
617                         XFS_LOCK_DEP_MASK)) == 0);
618         ASSERT(lock_flags != 0);
619
620         if (lock_flags & XFS_IOLOCK_EXCL)
621                 mrunlock_excl(&ip->i_iolock);
622         else if (lock_flags & XFS_IOLOCK_SHARED)
623                 mrunlock_shared(&ip->i_iolock);
624
625         if (lock_flags & XFS_ILOCK_EXCL)
626                 mrunlock_excl(&ip->i_lock);
627         else if (lock_flags & XFS_ILOCK_SHARED)
628                 mrunlock_shared(&ip->i_lock);
629
630         if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
631             !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
632                 /*
633                  * Let the AIL know that this item has been unlocked in case
634                  * it is in the AIL and anyone is waiting on it.  Don't do
635                  * this if the caller has asked us not to.
636                  */
637                 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
638                                         (xfs_log_item_t*)(ip->i_itemp));
639         }
640         xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
641 }
642
643 /*
644  * give up write locks.  the i/o lock cannot be held nested
645  * if it is being demoted.
646  */
647 void
648 xfs_ilock_demote(
649         xfs_inode_t             *ip,
650         uint                    lock_flags)
651 {
652         ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
653         ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
654
655         if (lock_flags & XFS_ILOCK_EXCL)
656                 mrdemote(&ip->i_lock);
657         if (lock_flags & XFS_IOLOCK_EXCL)
658                 mrdemote(&ip->i_iolock);
659 }
660
661 #ifdef DEBUG
662 /*
663  * Debug-only routine, without additional rw_semaphore APIs, we can
664  * now only answer requests regarding whether we hold the lock for write
665  * (reader state is outside our visibility, we only track writer state).
666  *
667  * Note: this means !xfs_isilocked would give false positives, so don't do that.
668  */
669 int
670 xfs_isilocked(
671         xfs_inode_t             *ip,
672         uint                    lock_flags)
673 {
674         if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
675                         XFS_ILOCK_EXCL) {
676                 if (!ip->i_lock.mr_writer)
677                         return 0;
678         }
679
680         if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
681                         XFS_IOLOCK_EXCL) {
682                 if (!ip->i_iolock.mr_writer)
683                         return 0;
684         }
685
686         return 1;
687 }
688 #endif
689