[XFS] factor xfs_iget_core() into hit and miss cases
[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
42 /*
43  * Check the validity of the inode we just found it the cache
44  */
45 static int
46 xfs_iget_cache_hit(
47         struct inode            *inode,
48         struct xfs_perag        *pag,
49         struct xfs_inode        *ip,
50         int                     flags,
51         int                     lock_flags) __releases(pag->pag_ici_lock)
52 {
53         struct xfs_mount        *mp = ip->i_mount;
54         struct inode            *old_inode;
55         int                     error = 0;
56
57         /*
58          * If INEW is set this inode is being set up
59          * Pause and try again.
60          */
61         if (xfs_iflags_test(ip, XFS_INEW)) {
62                 error = EAGAIN;
63                 XFS_STATS_INC(xs_ig_frecycle);
64                 goto out_error;
65         }
66
67         old_inode = ip->i_vnode;
68         if (old_inode == NULL) {
69                 /*
70                  * If IRECLAIM is set this inode is
71                  * on its way out of the system,
72                  * we need to pause and try again.
73                  */
74                 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
75                         error = EAGAIN;
76                         XFS_STATS_INC(xs_ig_frecycle);
77                         goto out_error;
78                 }
79                 ASSERT(xfs_iflags_test(ip, XFS_IRECLAIMABLE));
80
81                 /*
82                  * If lookup is racing with unlink, then we
83                  * should return an error immediately so we
84                  * don't remove it from the reclaim list and
85                  * potentially leak the inode.
86                  */
87                 if ((ip->i_d.di_mode == 0) &&
88                     !(flags & XFS_IGET_CREATE)) {
89                         error = ENOENT;
90                         goto out_error;
91                 }
92                 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
93
94                 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
95                 read_unlock(&pag->pag_ici_lock);
96
97                 XFS_MOUNT_ILOCK(mp);
98                 list_del_init(&ip->i_reclaim);
99                 XFS_MOUNT_IUNLOCK(mp);
100
101         } else if (inode != old_inode) {
102                 /* The inode is being torn down, pause and
103                  * try again.
104                  */
105                 if (old_inode->i_state & (I_FREEING | I_CLEAR)) {
106                         error = EAGAIN;
107                         XFS_STATS_INC(xs_ig_frecycle);
108                         goto out_error;
109                 }
110 /* Chances are the other vnode (the one in the inode) is being torn
111 * down right now, and we landed on top of it. Question is, what do
112 * we do? Unhook the old inode and hook up the new one?
113 */
114                 cmn_err(CE_PANIC,
115         "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
116                                 old_inode, inode);
117         } else {
118                 read_unlock(&pag->pag_ici_lock);
119         }
120
121         if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
122                 error = ENOENT;
123                 goto out;
124         }
125
126         if (lock_flags != 0)
127                 xfs_ilock(ip, lock_flags);
128
129         xfs_iflags_clear(ip, XFS_ISTALE);
130         xfs_itrace_exit_tag(ip, "xfs_iget.found");
131         XFS_STATS_INC(xs_ig_found);
132         return 0;
133
134 out_error:
135         read_unlock(&pag->pag_ici_lock);
136 out:
137         return error;
138 }
139
140
141 static int
142 xfs_iget_cache_miss(
143         struct xfs_mount        *mp,
144         struct xfs_perag        *pag,
145         xfs_trans_t             *tp,
146         xfs_ino_t               ino,
147         struct xfs_inode        **ipp,
148         xfs_daddr_t             bno,
149         int                     flags,
150         int                     lock_flags) __releases(pag->pag_ici_lock)
151 {
152         struct xfs_inode        *ip;
153         int                     error;
154         unsigned long           first_index, mask;
155         xfs_agino_t             agino = XFS_INO_TO_AGINO(mp, ino);
156
157         /*
158          * Read the disk inode attributes into a new inode structure and get
159          * a new vnode for it. This should also initialize i_ino and i_mount.
160          */
161         error = xfs_iread(mp, tp, ino, &ip, bno,
162                           (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
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         /*
174          * Preload the radix tree so we can insert safely under the
175          * write spinlock.
176          */
177         if (radix_tree_preload(GFP_KERNEL)) {
178                 error = EAGAIN;
179                 goto out_destroy;
180         }
181
182         if (lock_flags)
183                 xfs_ilock(ip, lock_flags);
184
185         mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
186         first_index = agino & mask;
187         write_lock(&pag->pag_ici_lock);
188
189         /* insert the new inode */
190         error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
191         if (unlikely(error)) {
192                 WARN_ON(error != -EEXIST);
193                 XFS_STATS_INC(xs_ig_dup);
194                 error = EAGAIN;
195                 goto out_unlock;
196         }
197
198         /* These values _must_ be set before releasing the radix tree lock! */
199         ip->i_udquot = ip->i_gdquot = NULL;
200         xfs_iflags_set(ip, XFS_INEW);
201
202         write_unlock(&pag->pag_ici_lock);
203         radix_tree_preload_end();
204         *ipp = ip;
205         return 0;
206
207 out_unlock:
208         write_unlock(&pag->pag_ici_lock);
209         radix_tree_preload_end();
210 out_destroy:
211         xfs_idestroy(ip);
212         return error;
213 }
214
215 /*
216  * Look up an inode by number in the given file system.
217  * The inode is looked up in the cache held in each AG.
218  * If the inode is found in the cache, attach it to the provided
219  * vnode.
220  *
221  * If it is not in core, read it in from the file system's device,
222  * add it to the cache and attach the provided vnode.
223  *
224  * The inode is locked according to the value of the lock_flags parameter.
225  * This flag parameter indicates how and if the inode's IO lock and inode lock
226  * should be taken.
227  *
228  * mp -- the mount point structure for the current file system.  It points
229  *       to the inode hash table.
230  * tp -- a pointer to the current transaction if there is one.  This is
231  *       simply passed through to the xfs_iread() call.
232  * ino -- the number of the inode desired.  This is the unique identifier
233  *        within the file system for the inode being requested.
234  * lock_flags -- flags indicating how to lock the inode.  See the comment
235  *               for xfs_ilock() for a list of valid values.
236  * bno -- the block number starting the buffer containing the inode,
237  *        if known (as by bulkstat), else 0.
238  */
239 STATIC int
240 xfs_iget_core(
241         struct inode    *inode,
242         xfs_mount_t     *mp,
243         xfs_trans_t     *tp,
244         xfs_ino_t       ino,
245         uint            flags,
246         uint            lock_flags,
247         xfs_inode_t     **ipp,
248         xfs_daddr_t     bno)
249 {
250         xfs_inode_t     *ip;
251         int             error;
252         xfs_perag_t     *pag;
253         xfs_agino_t     agino;
254
255         /* the radix tree exists only in inode capable AGs */
256         if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
257                 return EINVAL;
258
259         /* get the perag structure and ensure that it's inode capable */
260         pag = xfs_get_perag(mp, ino);
261         if (!pag->pagi_inodeok)
262                 return EINVAL;
263         ASSERT(pag->pag_ici_init);
264         agino = XFS_INO_TO_AGINO(mp, ino);
265
266 again:
267         error = 0;
268         read_lock(&pag->pag_ici_lock);
269         ip = radix_tree_lookup(&pag->pag_ici_root, agino);
270
271         if (ip) {
272                 error = xfs_iget_cache_hit(inode, pag, ip, flags, lock_flags);
273                 if (error)
274                         goto out_error_or_again;
275         } else {
276                 read_unlock(&pag->pag_ici_lock);
277                 XFS_STATS_INC(xs_ig_missed);
278
279                 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
280                                                         flags, lock_flags);
281                 if (error)
282                         goto out_error_or_again;
283         }
284         xfs_put_perag(mp, pag);
285
286         ASSERT(ip->i_df.if_ext_max ==
287                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
288
289         xfs_iflags_set(ip, XFS_IMODIFIED);
290         *ipp = ip;
291
292         /*
293          * Set up the Linux with the Linux inode.
294          */
295         ip->i_vnode = inode;
296         inode->i_private = ip;
297
298         /*
299          * If we have a real type for an on-disk inode, we can set ops(&unlock)
300          * now.  If it's a new inode being created, xfs_ialloc will handle it.
301          */
302         if (ip->i_d.di_mode != 0)
303                 xfs_setup_inode(ip);
304         return 0;
305
306 out_error_or_again:
307         if (error == EAGAIN) {
308                 delay(1);
309                 goto again;
310         }
311         xfs_put_perag(mp, pag);
312         return error;
313 }
314
315
316 /*
317  * The 'normal' internal xfs_iget, if needed it will
318  * 'allocate', or 'get', the vnode.
319  */
320 int
321 xfs_iget(
322         xfs_mount_t     *mp,
323         xfs_trans_t     *tp,
324         xfs_ino_t       ino,
325         uint            flags,
326         uint            lock_flags,
327         xfs_inode_t     **ipp,
328         xfs_daddr_t     bno)
329 {
330         struct inode    *inode;
331         xfs_inode_t     *ip;
332         int             error;
333
334         XFS_STATS_INC(xs_ig_attempts);
335
336 retry:
337         inode = iget_locked(mp->m_super, ino);
338         if (!inode)
339                 /* If we got no inode we are out of memory */
340                 return ENOMEM;
341
342         if (inode->i_state & I_NEW) {
343                 XFS_STATS_INC(vn_active);
344                 XFS_STATS_INC(vn_alloc);
345
346                 error = xfs_iget_core(inode, mp, tp, ino, flags,
347                                 lock_flags, ipp, bno);
348                 if (error) {
349                         make_bad_inode(inode);
350                         if (inode->i_state & I_NEW)
351                                 unlock_new_inode(inode);
352                         iput(inode);
353                 }
354                 return error;
355         }
356
357         /*
358          * If the inode is not fully constructed due to
359          * filehandle mismatches wait for the inode to go
360          * away and try again.
361          *
362          * iget_locked will call __wait_on_freeing_inode
363          * to wait for the inode to go away.
364          */
365         if (is_bad_inode(inode)) {
366                 iput(inode);
367                 delay(1);
368                 goto retry;
369         }
370
371         ip = XFS_I(inode);
372         if (!ip) {
373                 iput(inode);
374                 delay(1);
375                 goto retry;
376         }
377
378         if (lock_flags != 0)
379                 xfs_ilock(ip, lock_flags);
380         XFS_STATS_INC(xs_ig_found);
381         *ipp = ip;
382         return 0;
383 }
384
385 /*
386  * Look for the inode corresponding to the given ino in the hash table.
387  * If it is there and its i_transp pointer matches tp, return it.
388  * Otherwise, return NULL.
389  */
390 xfs_inode_t *
391 xfs_inode_incore(xfs_mount_t    *mp,
392                  xfs_ino_t      ino,
393                  xfs_trans_t    *tp)
394 {
395         xfs_inode_t     *ip;
396         xfs_perag_t     *pag;
397
398         pag = xfs_get_perag(mp, ino);
399         read_lock(&pag->pag_ici_lock);
400         ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
401         read_unlock(&pag->pag_ici_lock);
402         xfs_put_perag(mp, pag);
403
404         /* the returned inode must match the transaction */
405         if (ip && (ip->i_transp != tp))
406                 return NULL;
407         return ip;
408 }
409
410 /*
411  * Decrement reference count of an inode structure and unlock it.
412  *
413  * ip -- the inode being released
414  * lock_flags -- this parameter indicates the inode's locks to be
415  *       to be released.  See the comment on xfs_iunlock() for a list
416  *       of valid values.
417  */
418 void
419 xfs_iput(xfs_inode_t    *ip,
420          uint           lock_flags)
421 {
422         xfs_itrace_entry(ip);
423         xfs_iunlock(ip, lock_flags);
424         IRELE(ip);
425 }
426
427 /*
428  * Special iput for brand-new inodes that are still locked
429  */
430 void
431 xfs_iput_new(
432         xfs_inode_t     *ip,
433         uint            lock_flags)
434 {
435         struct inode    *inode = VFS_I(ip);
436
437         xfs_itrace_entry(ip);
438
439         if ((ip->i_d.di_mode == 0)) {
440                 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
441                 make_bad_inode(inode);
442         }
443         if (inode->i_state & I_NEW)
444                 unlock_new_inode(inode);
445         if (lock_flags)
446                 xfs_iunlock(ip, lock_flags);
447         IRELE(ip);
448 }
449
450
451 /*
452  * This routine embodies the part of the reclaim code that pulls
453  * the inode from the inode hash table and the mount structure's
454  * inode list.
455  * This should only be called from xfs_reclaim().
456  */
457 void
458 xfs_ireclaim(xfs_inode_t *ip)
459 {
460         /*
461          * Remove from old hash list and mount list.
462          */
463         XFS_STATS_INC(xs_ig_reclaims);
464
465         xfs_iextract(ip);
466
467         /*
468          * Here we do a spurious inode lock in order to coordinate with inode
469          * cache radix tree lookups.  This is because the lookup can reference
470          * the inodes in the cache without taking references.  We make that OK
471          * here by ensuring that we wait until the inode is unlocked after the
472          * lookup before we go ahead and free it.  We get both the ilock and
473          * the iolock because the code may need to drop the ilock one but will
474          * still hold the iolock.
475          */
476         xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
477
478         /*
479          * Release dquots (and their references) if any. An inode may escape
480          * xfs_inactive and get here via vn_alloc->vn_reclaim path.
481          */
482         XFS_QM_DQDETACH(ip->i_mount, ip);
483
484         /*
485          * Pull our behavior descriptor from the vnode chain.
486          */
487         if (ip->i_vnode) {
488                 ip->i_vnode->i_private = NULL;
489                 ip->i_vnode = NULL;
490         }
491
492         /*
493          * Free all memory associated with the inode.
494          */
495         xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
496         xfs_idestroy(ip);
497 }
498
499 /*
500  * This routine removes an about-to-be-destroyed inode from
501  * all of the lists in which it is located with the exception
502  * of the behavior chain.
503  */
504 void
505 xfs_iextract(
506         xfs_inode_t     *ip)
507 {
508         xfs_mount_t     *mp = ip->i_mount;
509         xfs_perag_t     *pag = xfs_get_perag(mp, ip->i_ino);
510
511         write_lock(&pag->pag_ici_lock);
512         radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
513         write_unlock(&pag->pag_ici_lock);
514         xfs_put_perag(mp, pag);
515
516         /* Deal with the deleted inodes list */
517         XFS_MOUNT_ILOCK(mp);
518         list_del_init(&ip->i_reclaim);
519         mp->m_ireclaims++;
520         XFS_MOUNT_IUNLOCK(mp);
521 }
522
523 /*
524  * This is a wrapper routine around the xfs_ilock() routine
525  * used to centralize some grungy code.  It is used in places
526  * that wish to lock the inode solely for reading the extents.
527  * The reason these places can't just call xfs_ilock(SHARED)
528  * is that the inode lock also guards to bringing in of the
529  * extents from disk for a file in b-tree format.  If the inode
530  * is in b-tree format, then we need to lock the inode exclusively
531  * until the extents are read in.  Locking it exclusively all
532  * the time would limit our parallelism unnecessarily, though.
533  * What we do instead is check to see if the extents have been
534  * read in yet, and only lock the inode exclusively if they
535  * have not.
536  *
537  * The function returns a value which should be given to the
538  * corresponding xfs_iunlock_map_shared().  This value is
539  * the mode in which the lock was actually taken.
540  */
541 uint
542 xfs_ilock_map_shared(
543         xfs_inode_t     *ip)
544 {
545         uint    lock_mode;
546
547         if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
548             ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
549                 lock_mode = XFS_ILOCK_EXCL;
550         } else {
551                 lock_mode = XFS_ILOCK_SHARED;
552         }
553
554         xfs_ilock(ip, lock_mode);
555
556         return lock_mode;
557 }
558
559 /*
560  * This is simply the unlock routine to go with xfs_ilock_map_shared().
561  * All it does is call xfs_iunlock() with the given lock_mode.
562  */
563 void
564 xfs_iunlock_map_shared(
565         xfs_inode_t     *ip,
566         unsigned int    lock_mode)
567 {
568         xfs_iunlock(ip, lock_mode);
569 }
570
571 /*
572  * The xfs inode contains 2 locks: a multi-reader lock called the
573  * i_iolock and a multi-reader lock called the i_lock.  This routine
574  * allows either or both of the locks to be obtained.
575  *
576  * The 2 locks should always be ordered so that the IO lock is
577  * obtained first in order to prevent deadlock.
578  *
579  * ip -- the inode being locked
580  * lock_flags -- this parameter indicates the inode's locks
581  *       to be locked.  It can be:
582  *              XFS_IOLOCK_SHARED,
583  *              XFS_IOLOCK_EXCL,
584  *              XFS_ILOCK_SHARED,
585  *              XFS_ILOCK_EXCL,
586  *              XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
587  *              XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
588  *              XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
589  *              XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
590  */
591 void
592 xfs_ilock(
593         xfs_inode_t             *ip,
594         uint                    lock_flags)
595 {
596         /*
597          * You can't set both SHARED and EXCL for the same lock,
598          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
599          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
600          */
601         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
602                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
603         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
604                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
605         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
606
607         if (lock_flags & XFS_IOLOCK_EXCL)
608                 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
609         else if (lock_flags & XFS_IOLOCK_SHARED)
610                 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
611
612         if (lock_flags & XFS_ILOCK_EXCL)
613                 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
614         else if (lock_flags & XFS_ILOCK_SHARED)
615                 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
616
617         xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
618 }
619
620 /*
621  * This is just like xfs_ilock(), except that the caller
622  * is guaranteed not to sleep.  It returns 1 if it gets
623  * the requested locks and 0 otherwise.  If the IO lock is
624  * obtained but the inode lock cannot be, then the IO lock
625  * is dropped before returning.
626  *
627  * ip -- the inode being locked
628  * lock_flags -- this parameter indicates the inode's locks to be
629  *       to be locked.  See the comment for xfs_ilock() for a list
630  *       of valid values.
631  */
632 int
633 xfs_ilock_nowait(
634         xfs_inode_t             *ip,
635         uint                    lock_flags)
636 {
637         /*
638          * You can't set both SHARED and EXCL for the same lock,
639          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
640          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
641          */
642         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
643                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
644         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
645                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
646         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
647
648         if (lock_flags & XFS_IOLOCK_EXCL) {
649                 if (!mrtryupdate(&ip->i_iolock))
650                         goto out;
651         } else if (lock_flags & XFS_IOLOCK_SHARED) {
652                 if (!mrtryaccess(&ip->i_iolock))
653                         goto out;
654         }
655         if (lock_flags & XFS_ILOCK_EXCL) {
656                 if (!mrtryupdate(&ip->i_lock))
657                         goto out_undo_iolock;
658         } else if (lock_flags & XFS_ILOCK_SHARED) {
659                 if (!mrtryaccess(&ip->i_lock))
660                         goto out_undo_iolock;
661         }
662         xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
663         return 1;
664
665  out_undo_iolock:
666         if (lock_flags & XFS_IOLOCK_EXCL)
667                 mrunlock_excl(&ip->i_iolock);
668         else if (lock_flags & XFS_IOLOCK_SHARED)
669                 mrunlock_shared(&ip->i_iolock);
670  out:
671         return 0;
672 }
673
674 /*
675  * xfs_iunlock() is used to drop the inode locks acquired with
676  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
677  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
678  * that we know which locks to drop.
679  *
680  * ip -- the inode being unlocked
681  * lock_flags -- this parameter indicates the inode's locks to be
682  *       to be unlocked.  See the comment for xfs_ilock() for a list
683  *       of valid values for this parameter.
684  *
685  */
686 void
687 xfs_iunlock(
688         xfs_inode_t             *ip,
689         uint                    lock_flags)
690 {
691         /*
692          * You can't set both SHARED and EXCL for the same lock,
693          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
694          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
695          */
696         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
697                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
698         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
699                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
700         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
701                         XFS_LOCK_DEP_MASK)) == 0);
702         ASSERT(lock_flags != 0);
703
704         if (lock_flags & XFS_IOLOCK_EXCL)
705                 mrunlock_excl(&ip->i_iolock);
706         else if (lock_flags & XFS_IOLOCK_SHARED)
707                 mrunlock_shared(&ip->i_iolock);
708
709         if (lock_flags & XFS_ILOCK_EXCL)
710                 mrunlock_excl(&ip->i_lock);
711         else if (lock_flags & XFS_ILOCK_SHARED)
712                 mrunlock_shared(&ip->i_lock);
713
714         if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
715             !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
716                 /*
717                  * Let the AIL know that this item has been unlocked in case
718                  * it is in the AIL and anyone is waiting on it.  Don't do
719                  * this if the caller has asked us not to.
720                  */
721                 xfs_trans_unlocked_item(ip->i_mount,
722                                         (xfs_log_item_t*)(ip->i_itemp));
723         }
724         xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
725 }
726
727 /*
728  * give up write locks.  the i/o lock cannot be held nested
729  * if it is being demoted.
730  */
731 void
732 xfs_ilock_demote(
733         xfs_inode_t             *ip,
734         uint                    lock_flags)
735 {
736         ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
737         ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
738
739         if (lock_flags & XFS_ILOCK_EXCL)
740                 mrdemote(&ip->i_lock);
741         if (lock_flags & XFS_IOLOCK_EXCL)
742                 mrdemote(&ip->i_iolock);
743 }
744
745 #ifdef DEBUG
746 /*
747  * Debug-only routine, without additional rw_semaphore APIs, we can
748  * now only answer requests regarding whether we hold the lock for write
749  * (reader state is outside our visibility, we only track writer state).
750  *
751  * Note: this means !xfs_isilocked would give false positives, so don't do that.
752  */
753 int
754 xfs_isilocked(
755         xfs_inode_t             *ip,
756         uint                    lock_flags)
757 {
758         if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
759                         XFS_ILOCK_EXCL) {
760                 if (!ip->i_lock.mr_writer)
761                         return 0;
762         }
763
764         if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
765                         XFS_IOLOCK_EXCL) {
766                 if (!ip->i_iolock.mr_writer)
767                         return 0;
768         }
769
770         return 1;
771 }
772 #endif
773