[XFS] Remove xfs_macros.c, xfs_macros.h, rework headers a whole lot.
[linux-2.6-block.git] / fs / xfs / xfs_iget.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 #include "xfs.h"
33 #include "xfs_fs.h"
34 #include "xfs_types.h"
35 #include "xfs_bit.h"
36 #include "xfs_log.h"
37 #include "xfs_inum.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_ag.h"
41 #include "xfs_dir.h"
42 #include "xfs_dir2.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_bmap_btree.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dinode.h"
52 #include "xfs_inode.h"
53 #include "xfs_btree.h"
54 #include "xfs_ialloc.h"
55 #include "xfs_quota.h"
56 #include "xfs_utils.h"
57
58 /*
59  * Initialize the inode hash table for the newly mounted file system.
60  * Choose an initial table size based on user specified value, else
61  * use a simple algorithm using the maximum number of inodes as an
62  * indicator for table size, and clamp it between one and some large
63  * number of pages.
64  */
65 void
66 xfs_ihash_init(xfs_mount_t *mp)
67 {
68         __uint64_t      icount;
69         uint            i, flags = KM_SLEEP | KM_MAYFAIL;
70
71         if (!mp->m_ihsize) {
72                 icount = mp->m_maxicount ? mp->m_maxicount :
73                          (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog);
74                 mp->m_ihsize = 1 << max_t(uint, 8,
75                                         (xfs_highbit64(icount) + 1) / 2);
76                 mp->m_ihsize = min_t(uint, mp->m_ihsize,
77                                         (64 * NBPP) / sizeof(xfs_ihash_t));
78         }
79
80         while (!(mp->m_ihash = (xfs_ihash_t *)kmem_zalloc(mp->m_ihsize *
81                                                 sizeof(xfs_ihash_t), flags))) {
82                 if ((mp->m_ihsize >>= 1) <= NBPP)
83                         flags = KM_SLEEP;
84         }
85         for (i = 0; i < mp->m_ihsize; i++) {
86                 rwlock_init(&(mp->m_ihash[i].ih_lock));
87         }
88 }
89
90 /*
91  * Free up structures allocated by xfs_ihash_init, at unmount time.
92  */
93 void
94 xfs_ihash_free(xfs_mount_t *mp)
95 {
96         kmem_free(mp->m_ihash, mp->m_ihsize*sizeof(xfs_ihash_t));
97         mp->m_ihash = NULL;
98 }
99
100 /*
101  * Initialize the inode cluster hash table for the newly mounted file system.
102  * Its size is derived from the ihash table size.
103  */
104 void
105 xfs_chash_init(xfs_mount_t *mp)
106 {
107         uint    i;
108
109         mp->m_chsize = max_t(uint, 1, mp->m_ihsize /
110                          (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog));
111         mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize);
112         mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize
113                                                  * sizeof(xfs_chash_t),
114                                                  KM_SLEEP);
115         for (i = 0; i < mp->m_chsize; i++) {
116                 spinlock_init(&mp->m_chash[i].ch_lock,"xfshash");
117         }
118 }
119
120 /*
121  * Free up structures allocated by xfs_chash_init, at unmount time.
122  */
123 void
124 xfs_chash_free(xfs_mount_t *mp)
125 {
126         int     i;
127
128         for (i = 0; i < mp->m_chsize; i++) {
129                 spinlock_destroy(&mp->m_chash[i].ch_lock);
130         }
131
132         kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
133         mp->m_chash = NULL;
134 }
135
136 /*
137  * Try to move an inode to the front of its hash list if possible
138  * (and if its not there already).  Called right after obtaining
139  * the list version number and then dropping the read_lock on the
140  * hash list in question (which is done right after looking up the
141  * inode in question...).
142  */
143 STATIC void
144 xfs_ihash_promote(
145         xfs_ihash_t     *ih,
146         xfs_inode_t     *ip,
147         ulong           version)
148 {
149         xfs_inode_t     *iq;
150
151         if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) {
152                 if (likely(version == ih->ih_version)) {
153                         /* remove from list */
154                         if ((iq = ip->i_next)) {
155                                 iq->i_prevp = ip->i_prevp;
156                         }
157                         *ip->i_prevp = iq;
158
159                         /* insert at list head */
160                         iq = ih->ih_next;
161                         iq->i_prevp = &ip->i_next;
162                         ip->i_next = iq;
163                         ip->i_prevp = &ih->ih_next;
164                         ih->ih_next = ip;
165                 }
166                 write_unlock(&ih->ih_lock);
167         }
168 }
169
170 /*
171  * Look up an inode by number in the given file system.
172  * The inode is looked up in the hash table for the file system
173  * represented by the mount point parameter mp.  Each bucket of
174  * the hash table is guarded by an individual semaphore.
175  *
176  * If the inode is found in the hash table, its corresponding vnode
177  * is obtained with a call to vn_get().  This call takes care of
178  * coordination with the reclamation of the inode and vnode.  Note
179  * that the vmap structure is filled in while holding the hash lock.
180  * This gives us the state of the inode/vnode when we found it and
181  * is used for coordination in vn_get().
182  *
183  * If it is not in core, read it in from the file system's device and
184  * add the inode into the hash table.
185  *
186  * The inode is locked according to the value of the lock_flags parameter.
187  * This flag parameter indicates how and if the inode's IO lock and inode lock
188  * should be taken.
189  *
190  * mp -- the mount point structure for the current file system.  It points
191  *       to the inode hash table.
192  * tp -- a pointer to the current transaction if there is one.  This is
193  *       simply passed through to the xfs_iread() call.
194  * ino -- the number of the inode desired.  This is the unique identifier
195  *        within the file system for the inode being requested.
196  * lock_flags -- flags indicating how to lock the inode.  See the comment
197  *               for xfs_ilock() for a list of valid values.
198  * bno -- the block number starting the buffer containing the inode,
199  *        if known (as by bulkstat), else 0.
200  */
201 STATIC int
202 xfs_iget_core(
203         vnode_t         *vp,
204         xfs_mount_t     *mp,
205         xfs_trans_t     *tp,
206         xfs_ino_t       ino,
207         uint            flags,
208         uint            lock_flags,
209         xfs_inode_t     **ipp,
210         xfs_daddr_t     bno)
211 {
212         xfs_ihash_t     *ih;
213         xfs_inode_t     *ip;
214         xfs_inode_t     *iq;
215         vnode_t         *inode_vp;
216         ulong           version;
217         int             error;
218         /* REFERENCED */
219         xfs_chash_t     *ch;
220         xfs_chashlist_t *chl, *chlnew;
221         SPLDECL(s);
222
223
224         ih = XFS_IHASH(mp, ino);
225
226 again:
227         read_lock(&ih->ih_lock);
228
229         for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
230                 if (ip->i_ino == ino) {
231                         /*
232                          * If INEW is set this inode is being set up
233                          * we need to pause and try again.
234                          */
235                         if (ip->i_flags & XFS_INEW) {
236                                 read_unlock(&ih->ih_lock);
237                                 delay(1);
238                                 XFS_STATS_INC(xs_ig_frecycle);
239
240                                 goto again;
241                         }
242
243                         inode_vp = XFS_ITOV_NULL(ip);
244                         if (inode_vp == NULL) {
245                                 /*
246                                  * If IRECLAIM is set this inode is
247                                  * on its way out of the system,
248                                  * we need to pause and try again.
249                                  */
250                                 if (ip->i_flags & XFS_IRECLAIM) {
251                                         read_unlock(&ih->ih_lock);
252                                         delay(1);
253                                         XFS_STATS_INC(xs_ig_frecycle);
254
255                                         goto again;
256                                 }
257
258                                 vn_trace_exit(vp, "xfs_iget.alloc",
259                                         (inst_t *)__return_address);
260
261                                 XFS_STATS_INC(xs_ig_found);
262
263                                 ip->i_flags &= ~XFS_IRECLAIMABLE;
264                                 version = ih->ih_version;
265                                 read_unlock(&ih->ih_lock);
266                                 xfs_ihash_promote(ih, ip, version);
267
268                                 XFS_MOUNT_ILOCK(mp);
269                                 list_del_init(&ip->i_reclaim);
270                                 XFS_MOUNT_IUNLOCK(mp);
271
272                                 goto finish_inode;
273
274                         } else if (vp != inode_vp) {
275                                 struct inode *inode = LINVFS_GET_IP(inode_vp);
276
277                                 /* The inode is being torn down, pause and
278                                  * try again.
279                                  */
280                                 if (inode->i_state & (I_FREEING | I_CLEAR)) {
281                                         read_unlock(&ih->ih_lock);
282                                         delay(1);
283                                         XFS_STATS_INC(xs_ig_frecycle);
284
285                                         goto again;
286                                 }
287 /* Chances are the other vnode (the one in the inode) is being torn
288  * down right now, and we landed on top of it. Question is, what do
289  * we do? Unhook the old inode and hook up the new one?
290  */
291                                 cmn_err(CE_PANIC,
292                         "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
293                                                 inode_vp, vp);
294                         }
295
296                         /*
297                          * Inode cache hit: if ip is not at the front of
298                          * its hash chain, move it there now.
299                          * Do this with the lock held for update, but
300                          * do statistics after releasing the lock.
301                          */
302                         version = ih->ih_version;
303                         read_unlock(&ih->ih_lock);
304                         xfs_ihash_promote(ih, ip, version);
305                         XFS_STATS_INC(xs_ig_found);
306
307 finish_inode:
308                         if (ip->i_d.di_mode == 0) {
309                                 if (!(flags & IGET_CREATE))
310                                         return ENOENT;
311                                 xfs_iocore_inode_reinit(ip);
312                         }
313         
314                         if (lock_flags != 0)
315                                 xfs_ilock(ip, lock_flags);
316
317                         ip->i_flags &= ~XFS_ISTALE;
318
319                         vn_trace_exit(vp, "xfs_iget.found",
320                                                 (inst_t *)__return_address);
321                         goto return_ip;
322                 }
323         }
324
325         /*
326          * Inode cache miss: save the hash chain version stamp and unlock
327          * the chain, so we don't deadlock in vn_alloc.
328          */
329         XFS_STATS_INC(xs_ig_missed);
330
331         version = ih->ih_version;
332
333         read_unlock(&ih->ih_lock);
334
335         /*
336          * Read the disk inode attributes into a new inode structure and get
337          * a new vnode for it. This should also initialize i_ino and i_mount.
338          */
339         error = xfs_iread(mp, tp, ino, &ip, bno);
340         if (error) {
341                 return error;
342         }
343
344         vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
345
346         xfs_inode_lock_init(ip, vp);
347         xfs_iocore_inode_init(ip);
348
349         if (lock_flags != 0) {
350                 xfs_ilock(ip, lock_flags);
351         }
352                 
353         if ((ip->i_d.di_mode == 0) && !(flags & IGET_CREATE)) {
354                 xfs_idestroy(ip);
355                 return ENOENT;
356         }
357
358         /*
359          * Put ip on its hash chain, unless someone else hashed a duplicate
360          * after we released the hash lock.
361          */
362         write_lock(&ih->ih_lock);
363
364         if (ih->ih_version != version) {
365                 for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
366                         if (iq->i_ino == ino) {
367                                 write_unlock(&ih->ih_lock);
368                                 xfs_idestroy(ip);
369
370                                 XFS_STATS_INC(xs_ig_dup);
371                                 goto again;
372                         }
373                 }
374         }
375
376         /*
377          * These values _must_ be set before releasing ihlock!
378          */
379         ip->i_hash = ih;
380         if ((iq = ih->ih_next)) {
381                 iq->i_prevp = &ip->i_next;
382         }
383         ip->i_next = iq;
384         ip->i_prevp = &ih->ih_next;
385         ih->ih_next = ip;
386         ip->i_udquot = ip->i_gdquot = NULL;
387         ih->ih_version++;
388         ip->i_flags |= XFS_INEW;
389
390         write_unlock(&ih->ih_lock);
391
392         /*
393          * put ip on its cluster's hash chain
394          */
395         ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
396                ip->i_cnext == NULL);
397
398         chlnew = NULL;
399         ch = XFS_CHASH(mp, ip->i_blkno);
400  chlredo:
401         s = mutex_spinlock(&ch->ch_lock);
402         for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
403                 if (chl->chl_blkno == ip->i_blkno) {
404
405                         /* insert this inode into the doubly-linked list
406                          * where chl points */
407                         if ((iq = chl->chl_ip)) {
408                                 ip->i_cprev = iq->i_cprev;
409                                 iq->i_cprev->i_cnext = ip;
410                                 iq->i_cprev = ip;
411                                 ip->i_cnext = iq;
412                         } else {
413                                 ip->i_cnext = ip;
414                                 ip->i_cprev = ip;
415                         }
416                         chl->chl_ip = ip;
417                         ip->i_chash = chl;
418                         break;
419                 }
420         }
421
422         /* no hash list found for this block; add a new hash list */
423         if (chl == NULL)  {
424                 if (chlnew == NULL) {
425                         mutex_spinunlock(&ch->ch_lock, s);
426                         ASSERT(xfs_chashlist_zone != NULL);
427                         chlnew = (xfs_chashlist_t *)
428                                         kmem_zone_alloc(xfs_chashlist_zone,
429                                                 KM_SLEEP);
430                         ASSERT(chlnew != NULL);
431                         goto chlredo;
432                 } else {
433                         ip->i_cnext = ip;
434                         ip->i_cprev = ip;
435                         ip->i_chash = chlnew;
436                         chlnew->chl_ip = ip;
437                         chlnew->chl_blkno = ip->i_blkno;
438                         chlnew->chl_next = ch->ch_list;
439                         ch->ch_list = chlnew;
440                         chlnew = NULL;
441                 }
442         } else {
443                 if (chlnew != NULL) {
444                         kmem_zone_free(xfs_chashlist_zone, chlnew);
445                 }
446         }
447
448         mutex_spinunlock(&ch->ch_lock, s);
449
450
451         /*
452          * Link ip to its mount and thread it on the mount's inode list.
453          */
454         XFS_MOUNT_ILOCK(mp);
455         if ((iq = mp->m_inodes)) {
456                 ASSERT(iq->i_mprev->i_mnext == iq);
457                 ip->i_mprev = iq->i_mprev;
458                 iq->i_mprev->i_mnext = ip;
459                 iq->i_mprev = ip;
460                 ip->i_mnext = iq;
461         } else {
462                 ip->i_mnext = ip;
463                 ip->i_mprev = ip;
464         }
465         mp->m_inodes = ip;
466
467         XFS_MOUNT_IUNLOCK(mp);
468
469  return_ip:
470         ASSERT(ip->i_df.if_ext_max ==
471                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
472
473         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
474                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
475
476         *ipp = ip;
477
478         /*
479          * If we have a real type for an on-disk inode, we can set ops(&unlock)
480          * now.  If it's a new inode being created, xfs_ialloc will handle it.
481          */
482         VFS_INIT_VNODE(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
483
484         return 0;
485 }
486
487
488 /*
489  * The 'normal' internal xfs_iget, if needed it will
490  * 'allocate', or 'get', the vnode.
491  */
492 int
493 xfs_iget(
494         xfs_mount_t     *mp,
495         xfs_trans_t     *tp,
496         xfs_ino_t       ino,
497         uint            flags,
498         uint            lock_flags,
499         xfs_inode_t     **ipp,
500         xfs_daddr_t     bno)
501 {
502         struct inode    *inode;
503         vnode_t         *vp = NULL;
504         int             error;
505
506         XFS_STATS_INC(xs_ig_attempts);
507
508 retry:
509         if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
510                 bhv_desc_t      *bdp;
511                 xfs_inode_t     *ip;
512
513                 vp = LINVFS_GET_VP(inode);
514                 if (inode->i_state & I_NEW) {
515                         vn_initialize(inode);
516                         error = xfs_iget_core(vp, mp, tp, ino, flags,
517                                         lock_flags, ipp, bno);
518                         if (error) {
519                                 vn_mark_bad(vp);
520                                 if (inode->i_state & I_NEW)
521                                         unlock_new_inode(inode);
522                                 iput(inode);
523                         }
524                 } else {
525                         /*
526                          * If the inode is not fully constructed due to
527                          * filehandle mistmatches wait for the inode to go
528                          * away and try again.
529                          *
530                          * iget_locked will call __wait_on_freeing_inode
531                          * to wait for the inode to go away.
532                          */
533                         if (is_bad_inode(inode) ||
534                             ((bdp = vn_bhv_lookup(VN_BHV_HEAD(vp),
535                                                   &xfs_vnodeops)) == NULL)) {
536                                 iput(inode);
537                                 delay(1);
538                                 goto retry;
539                         }
540
541                         ip = XFS_BHVTOI(bdp);
542                         if (lock_flags != 0)
543                                 xfs_ilock(ip, lock_flags);
544                         XFS_STATS_INC(xs_ig_found);
545                         *ipp = ip;
546                         error = 0;
547                 }
548         } else
549                 error = ENOMEM; /* If we got no inode we are out of memory */
550
551         return error;
552 }
553
554 /*
555  * Do the setup for the various locks within the incore inode.
556  */
557 void
558 xfs_inode_lock_init(
559         xfs_inode_t     *ip,
560         vnode_t         *vp)
561 {
562         mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
563                      "xfsino", (long)vp->v_number);
564         mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
565         init_waitqueue_head(&ip->i_ipin_wait);
566         atomic_set(&ip->i_pincount, 0);
567         init_sema(&ip->i_flock, 1, "xfsfino", vp->v_number);
568 }
569
570 /*
571  * Look for the inode corresponding to the given ino in the hash table.
572  * If it is there and its i_transp pointer matches tp, return it.
573  * Otherwise, return NULL.
574  */
575 xfs_inode_t *
576 xfs_inode_incore(xfs_mount_t    *mp,
577                  xfs_ino_t      ino,
578                  xfs_trans_t    *tp)
579 {
580         xfs_ihash_t     *ih;
581         xfs_inode_t     *ip;
582         ulong           version;
583
584         ih = XFS_IHASH(mp, ino);
585         read_lock(&ih->ih_lock);
586         for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
587                 if (ip->i_ino == ino) {
588                         /*
589                          * If we find it and tp matches, return it.
590                          * Also move it to the front of the hash list
591                          * if we find it and it is not already there.
592                          * Otherwise break from the loop and return
593                          * NULL.
594                          */
595                         if (ip->i_transp == tp) {
596                                 version = ih->ih_version;
597                                 read_unlock(&ih->ih_lock);
598                                 xfs_ihash_promote(ih, ip, version);
599                                 return (ip);
600                         }
601                         break;
602                 }
603         }
604         read_unlock(&ih->ih_lock);
605         return (NULL);
606 }
607
608 /*
609  * Decrement reference count of an inode structure and unlock it.
610  *
611  * ip -- the inode being released
612  * lock_flags -- this parameter indicates the inode's locks to be
613  *       to be released.  See the comment on xfs_iunlock() for a list
614  *       of valid values.
615  */
616 void
617 xfs_iput(xfs_inode_t    *ip,
618          uint           lock_flags)
619 {
620         vnode_t *vp = XFS_ITOV(ip);
621
622         vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
623
624         xfs_iunlock(ip, lock_flags);
625
626         VN_RELE(vp);
627 }
628
629 /*
630  * Special iput for brand-new inodes that are still locked
631  */
632 void
633 xfs_iput_new(xfs_inode_t        *ip,
634              uint               lock_flags)
635 {
636         vnode_t         *vp = XFS_ITOV(ip);
637         struct inode    *inode = LINVFS_GET_IP(vp);
638
639         vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
640
641         if ((ip->i_d.di_mode == 0)) {
642                 ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
643                 vn_mark_bad(vp);
644         }
645         if (inode->i_state & I_NEW)
646                 unlock_new_inode(inode);
647         if (lock_flags)
648                 xfs_iunlock(ip, lock_flags);
649         VN_RELE(vp);
650 }
651
652
653 /*
654  * This routine embodies the part of the reclaim code that pulls
655  * the inode from the inode hash table and the mount structure's
656  * inode list.
657  * This should only be called from xfs_reclaim().
658  */
659 void
660 xfs_ireclaim(xfs_inode_t *ip)
661 {
662         vnode_t         *vp;
663
664         /*
665          * Remove from old hash list and mount list.
666          */
667         XFS_STATS_INC(xs_ig_reclaims);
668
669         xfs_iextract(ip);
670
671         /*
672          * Here we do a spurious inode lock in order to coordinate with
673          * xfs_sync().  This is because xfs_sync() references the inodes
674          * in the mount list without taking references on the corresponding
675          * vnodes.  We make that OK here by ensuring that we wait until
676          * the inode is unlocked in xfs_sync() before we go ahead and
677          * free it.  We get both the regular lock and the io lock because
678          * the xfs_sync() code may need to drop the regular one but will
679          * still hold the io lock.
680          */
681         xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
682
683         /*
684          * Release dquots (and their references) if any. An inode may escape
685          * xfs_inactive and get here via vn_alloc->vn_reclaim path.
686          */
687         XFS_QM_DQDETACH(ip->i_mount, ip);
688
689         /*
690          * Pull our behavior descriptor from the vnode chain.
691          */
692         vp = XFS_ITOV_NULL(ip);
693         if (vp) {
694                 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
695         }
696
697         /*
698          * Free all memory associated with the inode.
699          */
700         xfs_idestroy(ip);
701 }
702
703 /*
704  * This routine removes an about-to-be-destroyed inode from
705  * all of the lists in which it is located with the exception
706  * of the behavior chain.
707  */
708 void
709 xfs_iextract(
710         xfs_inode_t     *ip)
711 {
712         xfs_ihash_t     *ih;
713         xfs_inode_t     *iq;
714         xfs_mount_t     *mp;
715         xfs_chash_t     *ch;
716         xfs_chashlist_t *chl, *chm;
717         SPLDECL(s);
718
719         ih = ip->i_hash;
720         write_lock(&ih->ih_lock);
721         if ((iq = ip->i_next)) {
722                 iq->i_prevp = ip->i_prevp;
723         }
724         *ip->i_prevp = iq;
725         ih->ih_version++;
726         write_unlock(&ih->ih_lock);
727
728         /*
729          * Remove from cluster hash list
730          *   1) delete the chashlist if this is the last inode on the chashlist
731          *   2) unchain from list of inodes
732          *   3) point chashlist->chl_ip to 'chl_next' if to this inode.
733          */
734         mp = ip->i_mount;
735         ch = XFS_CHASH(mp, ip->i_blkno);
736         s = mutex_spinlock(&ch->ch_lock);
737
738         if (ip->i_cnext == ip) {
739                 /* Last inode on chashlist */
740                 ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
741                 ASSERT(ip->i_chash != NULL);
742                 chm=NULL;
743                 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
744                         if (chl->chl_blkno == ip->i_blkno) {
745                                 if (chm == NULL) {
746                                         /* first item on the list */
747                                         ch->ch_list = chl->chl_next;
748                                 } else {
749                                         chm->chl_next = chl->chl_next;
750                                 }
751                                 kmem_zone_free(xfs_chashlist_zone, chl);
752                                 break;
753                         } else {
754                                 ASSERT(chl->chl_ip != ip);
755                                 chm = chl;
756                         }
757                 }
758                 ASSERT_ALWAYS(chl != NULL);
759        } else {
760                 /* delete one inode from a non-empty list */
761                 iq = ip->i_cnext;
762                 iq->i_cprev = ip->i_cprev;
763                 ip->i_cprev->i_cnext = iq;
764                 if (ip->i_chash->chl_ip == ip) {
765                         ip->i_chash->chl_ip = iq;
766                 }
767                 ip->i_chash = __return_address;
768                 ip->i_cprev = __return_address;
769                 ip->i_cnext = __return_address;
770         }
771         mutex_spinunlock(&ch->ch_lock, s);
772
773         /*
774          * Remove from mount's inode list.
775          */
776         XFS_MOUNT_ILOCK(mp);
777         ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
778         iq = ip->i_mnext;
779         iq->i_mprev = ip->i_mprev;
780         ip->i_mprev->i_mnext = iq;
781
782         /*
783          * Fix up the head pointer if it points to the inode being deleted.
784          */
785         if (mp->m_inodes == ip) {
786                 if (ip == iq) {
787                         mp->m_inodes = NULL;
788                 } else {
789                         mp->m_inodes = iq;
790                 }
791         }
792
793         /* Deal with the deleted inodes list */
794         list_del_init(&ip->i_reclaim);
795
796         mp->m_ireclaims++;
797         XFS_MOUNT_IUNLOCK(mp);
798 }
799
800 /*
801  * This is a wrapper routine around the xfs_ilock() routine
802  * used to centralize some grungy code.  It is used in places
803  * that wish to lock the inode solely for reading the extents.
804  * The reason these places can't just call xfs_ilock(SHARED)
805  * is that the inode lock also guards to bringing in of the
806  * extents from disk for a file in b-tree format.  If the inode
807  * is in b-tree format, then we need to lock the inode exclusively
808  * until the extents are read in.  Locking it exclusively all
809  * the time would limit our parallelism unnecessarily, though.
810  * What we do instead is check to see if the extents have been
811  * read in yet, and only lock the inode exclusively if they
812  * have not.
813  *
814  * The function returns a value which should be given to the
815  * corresponding xfs_iunlock_map_shared().  This value is
816  * the mode in which the lock was actually taken.
817  */
818 uint
819 xfs_ilock_map_shared(
820         xfs_inode_t     *ip)
821 {
822         uint    lock_mode;
823
824         if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
825             ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
826                 lock_mode = XFS_ILOCK_EXCL;
827         } else {
828                 lock_mode = XFS_ILOCK_SHARED;
829         }
830
831         xfs_ilock(ip, lock_mode);
832
833         return lock_mode;
834 }
835
836 /*
837  * This is simply the unlock routine to go with xfs_ilock_map_shared().
838  * All it does is call xfs_iunlock() with the given lock_mode.
839  */
840 void
841 xfs_iunlock_map_shared(
842         xfs_inode_t     *ip,
843         unsigned int    lock_mode)
844 {
845         xfs_iunlock(ip, lock_mode);
846 }
847
848 /*
849  * The xfs inode contains 2 locks: a multi-reader lock called the
850  * i_iolock and a multi-reader lock called the i_lock.  This routine
851  * allows either or both of the locks to be obtained.
852  *
853  * The 2 locks should always be ordered so that the IO lock is
854  * obtained first in order to prevent deadlock.
855  *
856  * ip -- the inode being locked
857  * lock_flags -- this parameter indicates the inode's locks
858  *       to be locked.  It can be:
859  *              XFS_IOLOCK_SHARED,
860  *              XFS_IOLOCK_EXCL,
861  *              XFS_ILOCK_SHARED,
862  *              XFS_ILOCK_EXCL,
863  *              XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
864  *              XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
865  *              XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
866  *              XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
867  */
868 void
869 xfs_ilock(xfs_inode_t   *ip,
870           uint          lock_flags)
871 {
872         /*
873          * You can't set both SHARED and EXCL for the same lock,
874          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
875          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
876          */
877         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
878                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
879         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
880                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
881         ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
882
883         if (lock_flags & XFS_IOLOCK_EXCL) {
884                 mrupdate(&ip->i_iolock);
885         } else if (lock_flags & XFS_IOLOCK_SHARED) {
886                 mraccess(&ip->i_iolock);
887         }
888         if (lock_flags & XFS_ILOCK_EXCL) {
889                 mrupdate(&ip->i_lock);
890         } else if (lock_flags & XFS_ILOCK_SHARED) {
891                 mraccess(&ip->i_lock);
892         }
893         xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
894 }
895
896 /*
897  * This is just like xfs_ilock(), except that the caller
898  * is guaranteed not to sleep.  It returns 1 if it gets
899  * the requested locks and 0 otherwise.  If the IO lock is
900  * obtained but the inode lock cannot be, then the IO lock
901  * is dropped before returning.
902  *
903  * ip -- the inode being locked
904  * lock_flags -- this parameter indicates the inode's locks to be
905  *       to be locked.  See the comment for xfs_ilock() for a list
906  *       of valid values.
907  *
908  */
909 int
910 xfs_ilock_nowait(xfs_inode_t    *ip,
911                  uint           lock_flags)
912 {
913         int     iolocked;
914         int     ilocked;
915
916         /*
917          * You can't set both SHARED and EXCL for the same lock,
918          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
919          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
920          */
921         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
922                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
923         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
924                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
925         ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
926
927         iolocked = 0;
928         if (lock_flags & XFS_IOLOCK_EXCL) {
929                 iolocked = mrtryupdate(&ip->i_iolock);
930                 if (!iolocked) {
931                         return 0;
932                 }
933         } else if (lock_flags & XFS_IOLOCK_SHARED) {
934                 iolocked = mrtryaccess(&ip->i_iolock);
935                 if (!iolocked) {
936                         return 0;
937                 }
938         }
939         if (lock_flags & XFS_ILOCK_EXCL) {
940                 ilocked = mrtryupdate(&ip->i_lock);
941                 if (!ilocked) {
942                         if (iolocked) {
943                                 mrunlock(&ip->i_iolock);
944                         }
945                         return 0;
946                 }
947         } else if (lock_flags & XFS_ILOCK_SHARED) {
948                 ilocked = mrtryaccess(&ip->i_lock);
949                 if (!ilocked) {
950                         if (iolocked) {
951                                 mrunlock(&ip->i_iolock);
952                         }
953                         return 0;
954                 }
955         }
956         xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
957         return 1;
958 }
959
960 /*
961  * xfs_iunlock() is used to drop the inode locks acquired with
962  * xfs_ilock() and xfs_ilock_nowait().  The caller must pass
963  * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
964  * that we know which locks to drop.
965  *
966  * ip -- the inode being unlocked
967  * lock_flags -- this parameter indicates the inode's locks to be
968  *       to be unlocked.  See the comment for xfs_ilock() for a list
969  *       of valid values for this parameter.
970  *
971  */
972 void
973 xfs_iunlock(xfs_inode_t *ip,
974             uint        lock_flags)
975 {
976         /*
977          * You can't set both SHARED and EXCL for the same lock,
978          * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
979          * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
980          */
981         ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
982                (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
983         ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
984                (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
985         ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
986         ASSERT(lock_flags != 0);
987
988         if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
989                 ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
990                        (ismrlocked(&ip->i_iolock, MR_ACCESS)));
991                 ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
992                        (ismrlocked(&ip->i_iolock, MR_UPDATE)));
993                 mrunlock(&ip->i_iolock);
994         }
995
996         if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
997                 ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
998                        (ismrlocked(&ip->i_lock, MR_ACCESS)));
999                 ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
1000                        (ismrlocked(&ip->i_lock, MR_UPDATE)));
1001                 mrunlock(&ip->i_lock);
1002
1003                 /*
1004                  * Let the AIL know that this item has been unlocked in case
1005                  * it is in the AIL and anyone is waiting on it.  Don't do
1006                  * this if the caller has asked us not to.
1007                  */
1008                 if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
1009                      ip->i_itemp != NULL) {
1010                         xfs_trans_unlocked_item(ip->i_mount,
1011                                                 (xfs_log_item_t*)(ip->i_itemp));
1012                 }
1013         }
1014         xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
1015 }
1016
1017 /*
1018  * give up write locks.  the i/o lock cannot be held nested
1019  * if it is being demoted.
1020  */
1021 void
1022 xfs_ilock_demote(xfs_inode_t    *ip,
1023                  uint           lock_flags)
1024 {
1025         ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
1026         ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
1027
1028         if (lock_flags & XFS_ILOCK_EXCL) {
1029                 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
1030                 mrdemote(&ip->i_lock);
1031         }
1032         if (lock_flags & XFS_IOLOCK_EXCL) {
1033                 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1034                 mrdemote(&ip->i_iolock);
1035         }
1036 }
1037
1038 /*
1039  * The following three routines simply manage the i_flock
1040  * semaphore embedded in the inode.  This semaphore synchronizes
1041  * processes attempting to flush the in-core inode back to disk.
1042  */
1043 void
1044 xfs_iflock(xfs_inode_t *ip)
1045 {
1046         psema(&(ip->i_flock), PINOD|PLTWAIT);
1047 }
1048
1049 int
1050 xfs_iflock_nowait(xfs_inode_t *ip)
1051 {
1052         return (cpsema(&(ip->i_flock)));
1053 }
1054
1055 void
1056 xfs_ifunlock(xfs_inode_t *ip)
1057 {
1058         ASSERT(valusema(&(ip->i_flock)) <= 0);
1059         vsema(&(ip->i_flock));
1060 }