Merge git://git.kernel.org/pub/scm/linux/kernel/git/hirofumi/fatfs-2.6
[sfrench/cifs-2.6.git] / 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_acl.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_quota.h"
41 #include "xfs_utils.h"
42 #include "xfs_trans_priv.h"
43 #include "xfs_inode_item.h"
44 #include "xfs_bmap.h"
45 #include "xfs_btree_trace.h"
46 #include "xfs_dir2_trace.h"
47
48
49 /*
50  * Allocate and initialise an xfs_inode.
51  */
52 STATIC struct xfs_inode *
53 xfs_inode_alloc(
54         struct xfs_mount        *mp,
55         xfs_ino_t               ino)
56 {
57         struct xfs_inode        *ip;
58
59         /*
60          * if this didn't occur in transactions, we could use
61          * KM_MAYFAIL and return NULL here on ENOMEM. Set the
62          * code up to do this anyway.
63          */
64         ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
65         if (!ip)
66                 return NULL;
67         if (inode_init_always(mp->m_super, VFS_I(ip))) {
68                 kmem_zone_free(xfs_inode_zone, ip);
69                 return NULL;
70         }
71
72         ASSERT(atomic_read(&ip->i_iocount) == 0);
73         ASSERT(atomic_read(&ip->i_pincount) == 0);
74         ASSERT(!spin_is_locked(&ip->i_flags_lock));
75         ASSERT(completion_done(&ip->i_flush));
76
77         /* initialise the xfs inode */
78         ip->i_ino = ino;
79         ip->i_mount = mp;
80         memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
81         ip->i_afp = NULL;
82         memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
83         ip->i_flags = 0;
84         ip->i_update_core = 0;
85         ip->i_delayed_blks = 0;
86         memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
87         ip->i_size = 0;
88         ip->i_new_size = 0;
89
90         /*
91          * Initialize inode's trace buffers.
92          */
93 #ifdef  XFS_INODE_TRACE
94         ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS);
95 #endif
96 #ifdef XFS_BMAP_TRACE
97         ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS);
98 #endif
99 #ifdef XFS_BTREE_TRACE
100         ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS);
101 #endif
102 #ifdef XFS_RW_TRACE
103         ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS);
104 #endif
105 #ifdef XFS_ILOCK_TRACE
106         ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS);
107 #endif
108 #ifdef XFS_DIR2_TRACE
109         ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS);
110 #endif
111
112         /* prevent anyone from using this yet */
113         VFS_I(ip)->i_state = I_NEW|I_LOCK;
114
115         return ip;
116 }
117
118 STATIC void
119 xfs_inode_free(
120         struct xfs_inode        *ip)
121 {
122         switch (ip->i_d.di_mode & S_IFMT) {
123         case S_IFREG:
124         case S_IFDIR:
125         case S_IFLNK:
126                 xfs_idestroy_fork(ip, XFS_DATA_FORK);
127                 break;
128         }
129
130         if (ip->i_afp)
131                 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
132
133 #ifdef XFS_INODE_TRACE
134         ktrace_free(ip->i_trace);
135 #endif
136 #ifdef XFS_BMAP_TRACE
137         ktrace_free(ip->i_xtrace);
138 #endif
139 #ifdef XFS_BTREE_TRACE
140         ktrace_free(ip->i_btrace);
141 #endif
142 #ifdef XFS_RW_TRACE
143         ktrace_free(ip->i_rwtrace);
144 #endif
145 #ifdef XFS_ILOCK_TRACE
146         ktrace_free(ip->i_lock_trace);
147 #endif
148 #ifdef XFS_DIR2_TRACE
149         ktrace_free(ip->i_dir_trace);
150 #endif
151
152         if (ip->i_itemp) {
153                 /*
154                  * Only if we are shutting down the fs will we see an
155                  * inode still in the AIL. If it is there, we should remove
156                  * it to prevent a use-after-free from occurring.
157                  */
158                 xfs_log_item_t  *lip = &ip->i_itemp->ili_item;
159                 struct xfs_ail  *ailp = lip->li_ailp;
160
161                 ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
162                                        XFS_FORCED_SHUTDOWN(ip->i_mount));
163                 if (lip->li_flags & XFS_LI_IN_AIL) {
164                         spin_lock(&ailp->xa_lock);
165                         if (lip->li_flags & XFS_LI_IN_AIL)
166                                 xfs_trans_ail_delete(ailp, lip);
167                         else
168                                 spin_unlock(&ailp->xa_lock);
169                 }
170                 xfs_inode_item_destroy(ip);
171                 ip->i_itemp = NULL;
172         }
173
174         /* asserts to verify all state is correct here */
175         ASSERT(atomic_read(&ip->i_iocount) == 0);
176         ASSERT(atomic_read(&ip->i_pincount) == 0);
177         ASSERT(!spin_is_locked(&ip->i_flags_lock));
178         ASSERT(completion_done(&ip->i_flush));
179
180         kmem_zone_free(xfs_inode_zone, ip);
181 }
182
183 /*
184  * Check the validity of the inode we just found it the cache
185  */
186 static int
187 xfs_iget_cache_hit(
188         struct xfs_perag        *pag,
189         struct xfs_inode        *ip,
190         int                     flags,
191         int                     lock_flags) __releases(pag->pag_ici_lock)
192 {
193         struct inode            *inode = VFS_I(ip);
194         struct xfs_mount        *mp = ip->i_mount;
195         int                     error;
196
197         spin_lock(&ip->i_flags_lock);
198
199         /*
200          * If we are racing with another cache hit that is currently
201          * instantiating this inode or currently recycling it out of
202          * reclaimabe state, wait for the initialisation to complete
203          * before continuing.
204          *
205          * XXX(hch): eventually we should do something equivalent to
206          *           wait_on_inode to wait for these flags to be cleared
207          *           instead of polling for it.
208          */
209         if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
210                 XFS_STATS_INC(xs_ig_frecycle);
211                 error = EAGAIN;
212                 goto out_error;
213         }
214
215         /*
216          * If lookup is racing with unlink return an error immediately.
217          */
218         if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
219                 error = ENOENT;
220                 goto out_error;
221         }
222
223         /*
224          * If IRECLAIMABLE is set, we've torn down the VFS inode already.
225          * Need to carefully get it back into useable state.
226          */
227         if (ip->i_flags & XFS_IRECLAIMABLE) {
228                 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
229
230                 /*
231                  * We need to set XFS_INEW atomically with clearing the
232                  * reclaimable tag so that we do have an indicator of the
233                  * inode still being initialized.
234                  */
235                 ip->i_flags |= XFS_INEW;
236                 ip->i_flags &= ~XFS_IRECLAIMABLE;
237                 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
238
239                 spin_unlock(&ip->i_flags_lock);
240                 read_unlock(&pag->pag_ici_lock);
241
242                 error = -inode_init_always(mp->m_super, inode);
243                 if (error) {
244                         /*
245                          * Re-initializing the inode failed, and we are in deep
246                          * trouble.  Try to re-add it to the reclaim list.
247                          */
248                         read_lock(&pag->pag_ici_lock);
249                         spin_lock(&ip->i_flags_lock);
250
251                         ip->i_flags &= ~XFS_INEW;
252                         ip->i_flags |= XFS_IRECLAIMABLE;
253                         __xfs_inode_set_reclaim_tag(pag, ip);
254                         goto out_error;
255                 }
256                 inode->i_state = I_LOCK|I_NEW;
257         } else {
258                 /* If the VFS inode is being torn down, pause and try again. */
259                 if (!igrab(inode)) {
260                         error = EAGAIN;
261                         goto out_error;
262                 }
263
264                 /* We've got a live one. */
265                 spin_unlock(&ip->i_flags_lock);
266                 read_unlock(&pag->pag_ici_lock);
267         }
268
269         if (lock_flags != 0)
270                 xfs_ilock(ip, lock_flags);
271
272         xfs_iflags_clear(ip, XFS_ISTALE);
273         xfs_itrace_exit_tag(ip, "xfs_iget.found");
274         XFS_STATS_INC(xs_ig_found);
275         return 0;
276
277 out_error:
278         spin_unlock(&ip->i_flags_lock);
279         read_unlock(&pag->pag_ici_lock);
280         return error;
281 }
282
283
284 static int
285 xfs_iget_cache_miss(
286         struct xfs_mount        *mp,
287         struct xfs_perag        *pag,
288         xfs_trans_t             *tp,
289         xfs_ino_t               ino,
290         struct xfs_inode        **ipp,
291         xfs_daddr_t             bno,
292         int                     flags,
293         int                     lock_flags) __releases(pag->pag_ici_lock)
294 {
295         struct xfs_inode        *ip;
296         int                     error;
297         unsigned long           first_index, mask;
298         xfs_agino_t             agino = XFS_INO_TO_AGINO(mp, ino);
299
300         ip = xfs_inode_alloc(mp, ino);
301         if (!ip)
302                 return ENOMEM;
303
304         error = xfs_iread(mp, tp, ip, bno, flags);
305         if (error)
306                 goto out_destroy;
307
308         xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
309
310         if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
311                 error = ENOENT;
312                 goto out_destroy;
313         }
314
315         /*
316          * Preload the radix tree so we can insert safely under the
317          * write spinlock. Note that we cannot sleep inside the preload
318          * region.
319          */
320         if (radix_tree_preload(GFP_KERNEL)) {
321                 error = EAGAIN;
322                 goto out_destroy;
323         }
324
325         /*
326          * Because the inode hasn't been added to the radix-tree yet it can't
327          * be found by another thread, so we can do the non-sleeping lock here.
328          */
329         if (lock_flags) {
330                 if (!xfs_ilock_nowait(ip, lock_flags))
331                         BUG();
332         }
333
334         mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
335         first_index = agino & mask;
336         write_lock(&pag->pag_ici_lock);
337
338         /* insert the new inode */
339         error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
340         if (unlikely(error)) {
341                 WARN_ON(error != -EEXIST);
342                 XFS_STATS_INC(xs_ig_dup);
343                 error = EAGAIN;
344                 goto out_preload_end;
345         }
346
347         /* These values _must_ be set before releasing the radix tree lock! */
348         ip->i_udquot = ip->i_gdquot = NULL;
349         xfs_iflags_set(ip, XFS_INEW);
350
351         write_unlock(&pag->pag_ici_lock);
352         radix_tree_preload_end();
353         *ipp = ip;
354         return 0;
355
356 out_preload_end:
357         write_unlock(&pag->pag_ici_lock);
358         radix_tree_preload_end();
359         if (lock_flags)
360                 xfs_iunlock(ip, lock_flags);
361 out_destroy:
362         __destroy_inode(VFS_I(ip));
363         xfs_inode_free(ip);
364         return error;
365 }
366
367 /*
368  * Look up an inode by number in the given file system.
369  * The inode is looked up in the cache held in each AG.
370  * If the inode is found in the cache, initialise the vfs inode
371  * if necessary.
372  *
373  * If it is not in core, read it in from the file system's device,
374  * add it to the cache and initialise the vfs inode.
375  *
376  * The inode is locked according to the value of the lock_flags parameter.
377  * This flag parameter indicates how and if the inode's IO lock and inode lock
378  * should be taken.
379  *
380  * mp -- the mount point structure for the current file system.  It points
381  *       to the inode hash table.
382  * tp -- a pointer to the current transaction if there is one.  This is
383  *       simply passed through to the xfs_iread() call.
384  * ino -- the number of the inode desired.  This is the unique identifier
385  *        within the file system for the inode being requested.
386  * lock_flags -- flags indicating how to lock the inode.  See the comment
387  *               for xfs_ilock() for a list of valid values.
388  * bno -- the block number starting the buffer containing the inode,
389  *        if known (as by bulkstat), else 0.
390  */
391 int
392 xfs_iget(
393         xfs_mount_t     *mp,
394         xfs_trans_t     *tp,
395         xfs_ino_t       ino,
396         uint            flags,
397         uint            lock_flags,
398         xfs_inode_t     **ipp,
399         xfs_daddr_t     bno)
400 {
401         xfs_inode_t     *ip;
402         int             error;
403         xfs_perag_t     *pag;
404         xfs_agino_t     agino;
405
406         /* the radix tree exists only in inode capable AGs */
407         if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
408                 return EINVAL;
409
410         /* get the perag structure and ensure that it's inode capable */
411         pag = xfs_get_perag(mp, ino);
412         if (!pag->pagi_inodeok)
413                 return EINVAL;
414         ASSERT(pag->pag_ici_init);
415         agino = XFS_INO_TO_AGINO(mp, ino);
416
417 again:
418         error = 0;
419         read_lock(&pag->pag_ici_lock);
420         ip = radix_tree_lookup(&pag->pag_ici_root, agino);
421
422         if (ip) {
423                 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
424                 if (error)
425                         goto out_error_or_again;
426         } else {
427                 read_unlock(&pag->pag_ici_lock);
428                 XFS_STATS_INC(xs_ig_missed);
429
430                 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
431                                                         flags, lock_flags);
432                 if (error)
433                         goto out_error_or_again;
434         }
435         xfs_put_perag(mp, pag);
436
437         *ipp = ip;
438
439         ASSERT(ip->i_df.if_ext_max ==
440                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
441         /*
442          * If we have a real type for an on-disk inode, we can set ops(&unlock)
443          * now.  If it's a new inode being created, xfs_ialloc will handle it.
444          */
445         if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
446                 xfs_setup_inode(ip);
447         return 0;
448
449 out_error_or_again:
450         if (error == EAGAIN) {
451                 delay(1);
452                 goto again;
453         }
454         xfs_put_perag(mp, pag);
455         return error;
456 }
457
458 /*
459  * Decrement reference count of an inode structure and unlock it.
460  *
461  * ip -- the inode being released
462  * lock_flags -- this parameter indicates the inode's locks to be
463  *       to be released.  See the comment on xfs_iunlock() for a list
464  *       of valid values.
465  */
466 void
467 xfs_iput(xfs_inode_t    *ip,
468          uint           lock_flags)
469 {
470         xfs_itrace_entry(ip);
471         xfs_iunlock(ip, lock_flags);
472         IRELE(ip);
473 }
474
475 /*
476  * Special iput for brand-new inodes that are still locked
477  */
478 void
479 xfs_iput_new(
480         xfs_inode_t     *ip,
481         uint            lock_flags)
482 {
483         struct inode    *inode = VFS_I(ip);
484
485         xfs_itrace_entry(ip);
486
487         if ((ip->i_d.di_mode == 0)) {
488                 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
489                 make_bad_inode(inode);
490         }
491         if (inode->i_state & I_NEW)
492                 unlock_new_inode(inode);
493         if (lock_flags)
494                 xfs_iunlock(ip, lock_flags);
495         IRELE(ip);
496 }
497
498 /*
499  * This is called free all the memory associated with an inode.
500  * It must free the inode itself and any buffers allocated for
501  * if_extents/if_data and if_broot.  It must also free the lock
502  * associated with the inode.
503  *
504  * Note: because we don't initialise everything on reallocation out
505  * of the zone, we must ensure we nullify everything correctly before
506  * freeing the structure.
507  */
508 void
509 xfs_ireclaim(
510         struct xfs_inode        *ip)
511 {
512         struct xfs_mount        *mp = ip->i_mount;
513         struct xfs_perag        *pag;
514
515         XFS_STATS_INC(xs_ig_reclaims);
516
517         /*
518          * Remove the inode from the per-AG radix tree.  It doesn't matter
519          * if it was never added to it because radix_tree_delete can deal
520          * with that case just fine.
521          */
522         pag = xfs_get_perag(mp, ip->i_ino);
523         write_lock(&pag->pag_ici_lock);
524         radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
525         write_unlock(&pag->pag_ici_lock);
526         xfs_put_perag(mp, pag);
527
528         /*
529          * Here we do an (almost) spurious inode lock in order to coordinate
530          * with inode cache radix tree lookups.  This is because the lookup
531          * can reference the inodes in the cache without taking references.
532          *
533          * We make that OK here by ensuring that we wait until the inode is
534          * unlocked after the lookup before we go ahead and free it.  We get
535          * both the ilock and the iolock because the code may need to drop the
536          * ilock one but will still hold the iolock.
537          */
538         xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
539         xfs_qm_dqdetach(ip);
540         xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
541
542         xfs_inode_free(ip);
543 }
544
545 /*
546  * This is a wrapper routine around the xfs_ilock() routine
547  * used to centralize some grungy code.  It is used in places
548  * that wish to lock the inode solely for reading the extents.
549  * The reason these places can't just call xfs_ilock(SHARED)
550  * is that the inode lock also guards to bringing in of the
551  * extents from disk for a file in b-tree format.  If the inode
552  * is in b-tree format, then we need to lock the inode exclusively
553  * until the extents are read in.  Locking it exclusively all
554  * the time would limit our parallelism unnecessarily, though.
555  * What we do instead is check to see if the extents have been
556  * read in yet, and only lock the inode exclusively if they
557  * have not.
558  *
559  * The function returns a value which should be given to the
560  * corresponding xfs_iunlock_map_shared().  This value is
561  * the mode in which the lock was actually taken.
562  */
563 uint
564 xfs_ilock_map_shared(
565         xfs_inode_t     *ip)
566 {
567         uint    lock_mode;
568
569         if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
570             ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
571                 lock_mode = XFS_ILOCK_EXCL;
572         } else {
573                 lock_mode = XFS_ILOCK_SHARED;
574         }
575
576         xfs_ilock(ip, lock_mode);
577
578         return lock_mode;
579 }
580
581 /*
582  * This is simply the unlock routine to go with xfs_ilock_map_shared().
583  * All it does is call xfs_iunlock() with the given lock_mode.
584  */
585 void
586 xfs_iunlock_map_shared(
587         xfs_inode_t     *ip,
588         unsigned int    lock_mode)
589 {
590         xfs_iunlock(ip, lock_mode);
591 }
592
593 /*
594  * The xfs inode contains 2 locks: a multi-reader lock called the
595  * i_iolock and a multi-reader lock called the i_lock.  This routine
596  * allows either or both of the locks to be obtained.
597  *
598  * The 2 locks should always be ordered so that the IO lock is
599  * obtained first in order to prevent deadlock.
600  *
601  * ip -- the inode being locked
602  * lock_flags -- this parameter indicates the inode's locks
603  *       to be locked.  It can be:
604  *              XFS_IOLOCK_SHARED,
605  *              XFS_IOLOCK_EXCL,
606  *              XFS_ILOCK_SHARED,
607  *              XFS_ILOCK_EXCL,
608  *              XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
609  *              XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
610  *              XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
611  *              XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
612  */
613 void
614 xfs_ilock(
615         xfs_inode_t             *ip,
616         uint                    lock_flags)
617 {
618         /*
619          * You can't set both SHARED and EXCL for the same lock,
620          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
621          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
622          */
623         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
624                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
625         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
626                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
627         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
628
629         if (lock_flags & XFS_IOLOCK_EXCL)
630                 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
631         else if (lock_flags & XFS_IOLOCK_SHARED)
632                 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
633
634         if (lock_flags & XFS_ILOCK_EXCL)
635                 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
636         else if (lock_flags & XFS_ILOCK_SHARED)
637                 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
638
639         xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
640 }
641
642 /*
643  * This is just like xfs_ilock(), except that the caller
644  * is guaranteed not to sleep.  It returns 1 if it gets
645  * the requested locks and 0 otherwise.  If the IO lock is
646  * obtained but the inode lock cannot be, then the IO lock
647  * is dropped before returning.
648  *
649  * ip -- the inode being locked
650  * lock_flags -- this parameter indicates the inode's locks to be
651  *       to be locked.  See the comment for xfs_ilock() for a list
652  *       of valid values.
653  */
654 int
655 xfs_ilock_nowait(
656         xfs_inode_t             *ip,
657         uint                    lock_flags)
658 {
659         /*
660          * You can't set both SHARED and EXCL for the same lock,
661          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
662          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
663          */
664         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
665                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
666         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
667                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
668         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
669
670         if (lock_flags & XFS_IOLOCK_EXCL) {
671                 if (!mrtryupdate(&ip->i_iolock))
672                         goto out;
673         } else if (lock_flags & XFS_IOLOCK_SHARED) {
674                 if (!mrtryaccess(&ip->i_iolock))
675                         goto out;
676         }
677         if (lock_flags & XFS_ILOCK_EXCL) {
678                 if (!mrtryupdate(&ip->i_lock))
679                         goto out_undo_iolock;
680         } else if (lock_flags & XFS_ILOCK_SHARED) {
681                 if (!mrtryaccess(&ip->i_lock))
682                         goto out_undo_iolock;
683         }
684         xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
685         return 1;
686
687  out_undo_iolock:
688         if (lock_flags & XFS_IOLOCK_EXCL)
689                 mrunlock_excl(&ip->i_iolock);
690         else if (lock_flags & XFS_IOLOCK_SHARED)
691                 mrunlock_shared(&ip->i_iolock);
692  out:
693         return 0;
694 }
695
696 /*
697  * xfs_iunlock() is used to drop the inode locks acquired with
698  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
699  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
700  * that we know which locks to drop.
701  *
702  * ip -- the inode being unlocked
703  * lock_flags -- this parameter indicates the inode's locks to be
704  *       to be unlocked.  See the comment for xfs_ilock() for a list
705  *       of valid values for this parameter.
706  *
707  */
708 void
709 xfs_iunlock(
710         xfs_inode_t             *ip,
711         uint                    lock_flags)
712 {
713         /*
714          * You can't set both SHARED and EXCL for the same lock,
715          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
716          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
717          */
718         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
719                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
720         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
721                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
722         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
723                         XFS_LOCK_DEP_MASK)) == 0);
724         ASSERT(lock_flags != 0);
725
726         if (lock_flags & XFS_IOLOCK_EXCL)
727                 mrunlock_excl(&ip->i_iolock);
728         else if (lock_flags & XFS_IOLOCK_SHARED)
729                 mrunlock_shared(&ip->i_iolock);
730
731         if (lock_flags & XFS_ILOCK_EXCL)
732                 mrunlock_excl(&ip->i_lock);
733         else if (lock_flags & XFS_ILOCK_SHARED)
734                 mrunlock_shared(&ip->i_lock);
735
736         if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
737             !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
738                 /*
739                  * Let the AIL know that this item has been unlocked in case
740                  * it is in the AIL and anyone is waiting on it.  Don't do
741                  * this if the caller has asked us not to.
742                  */
743                 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
744                                         (xfs_log_item_t*)(ip->i_itemp));
745         }
746         xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
747 }
748
749 /*
750  * give up write locks.  the i/o lock cannot be held nested
751  * if it is being demoted.
752  */
753 void
754 xfs_ilock_demote(
755         xfs_inode_t             *ip,
756         uint                    lock_flags)
757 {
758         ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
759         ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
760
761         if (lock_flags & XFS_ILOCK_EXCL)
762                 mrdemote(&ip->i_lock);
763         if (lock_flags & XFS_IOLOCK_EXCL)
764                 mrdemote(&ip->i_iolock);
765 }
766
767 #ifdef DEBUG
768 /*
769  * Debug-only routine, without additional rw_semaphore APIs, we can
770  * now only answer requests regarding whether we hold the lock for write
771  * (reader state is outside our visibility, we only track writer state).
772  *
773  * Note: this means !xfs_isilocked would give false positives, so don't do that.
774  */
775 int
776 xfs_isilocked(
777         xfs_inode_t             *ip,
778         uint                    lock_flags)
779 {
780         if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
781                         XFS_ILOCK_EXCL) {
782                 if (!ip->i_lock.mr_writer)
783                         return 0;
784         }
785
786         if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
787                         XFS_IOLOCK_EXCL) {
788                 if (!ip->i_iolock.mr_writer)
789                         return 0;
790         }
791
792         return 1;
793 }
794 #endif
795
796 #ifdef  XFS_INODE_TRACE
797
798 #define KTRACE_ENTER(ip, vk, s, line, ra)                       \
799         ktrace_enter((ip)->i_trace,                             \
800 /*  0 */                (void *)(__psint_t)(vk),                \
801 /*  1 */                (void *)(s),                            \
802 /*  2 */                (void *)(__psint_t) line,               \
803 /*  3 */                (void *)(__psint_t)atomic_read(&VFS_I(ip)->i_count), \
804 /*  4 */                (void *)(ra),                           \
805 /*  5 */                NULL,                                   \
806 /*  6 */                (void *)(__psint_t)current_cpu(),       \
807 /*  7 */                (void *)(__psint_t)current_pid(),       \
808 /*  8 */                (void *)__return_address,               \
809 /*  9 */                NULL, NULL, NULL, NULL, NULL, NULL, NULL)
810
811 /*
812  * Vnode tracing code.
813  */
814 void
815 _xfs_itrace_entry(xfs_inode_t *ip, const char *func, inst_t *ra)
816 {
817         KTRACE_ENTER(ip, INODE_KTRACE_ENTRY, func, 0, ra);
818 }
819
820 void
821 _xfs_itrace_exit(xfs_inode_t *ip, const char *func, inst_t *ra)
822 {
823         KTRACE_ENTER(ip, INODE_KTRACE_EXIT, func, 0, ra);
824 }
825
826 void
827 xfs_itrace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra)
828 {
829         KTRACE_ENTER(ip, INODE_KTRACE_HOLD, file, line, ra);
830 }
831
832 void
833 _xfs_itrace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra)
834 {
835         KTRACE_ENTER(ip, INODE_KTRACE_REF, file, line, ra);
836 }
837
838 void
839 xfs_itrace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra)
840 {
841         KTRACE_ENTER(ip, INODE_KTRACE_RELE, file, line, ra);
842 }
843 #endif  /* XFS_INODE_TRACE */