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