[XFS] streamline the clear_inode path
[safe/jmp/linux-2.6] / 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
33 #include "xfs.h"
34
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
37 #include "xfs_inum.h"
38 #include "xfs_log.h"
39 #include "xfs_trans.h"
40 #include "xfs_sb.h"
41 #include "xfs_ag.h"
42 #include "xfs_dir.h"
43 #include "xfs_dir2.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_quota.h"
57 #include "xfs_utils.h"
58 #include "xfs_bit.h"
59
60 /*
61  * Initialize the inode hash table for the newly mounted file system.
62  * Choose an initial table size based on user specified value, else
63  * use a simple algorithm using the maximum number of inodes as an
64  * indicator for table size, and clamp it between one and some large
65  * number of pages.
66  */
67 void
68 xfs_ihash_init(xfs_mount_t *mp)
69 {
70         __uint64_t      icount;
71         uint            i, flags = KM_SLEEP | KM_MAYFAIL;
72
73         if (!mp->m_ihsize) {
74                 icount = mp->m_maxicount ? mp->m_maxicount :
75                          (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog);
76                 mp->m_ihsize = 1 << max_t(uint, 8,
77                                         (xfs_highbit64(icount) + 1) / 2);
78                 mp->m_ihsize = min_t(uint, mp->m_ihsize,
79                                         (64 * NBPP) / sizeof(xfs_ihash_t));
80         }
81
82         while (!(mp->m_ihash = (xfs_ihash_t *)kmem_zalloc(mp->m_ihsize *
83                                                 sizeof(xfs_ihash_t), flags))) {
84                 if ((mp->m_ihsize >>= 1) <= NBPP)
85                         flags = KM_SLEEP;
86         }
87         for (i = 0; i < mp->m_ihsize; i++) {
88                 rwlock_init(&(mp->m_ihash[i].ih_lock));
89         }
90 }
91
92 /*
93  * Free up structures allocated by xfs_ihash_init, at unmount time.
94  */
95 void
96 xfs_ihash_free(xfs_mount_t *mp)
97 {
98         kmem_free(mp->m_ihash, mp->m_ihsize*sizeof(xfs_ihash_t));
99         mp->m_ihash = NULL;
100 }
101
102 /*
103  * Initialize the inode cluster hash table for the newly mounted file system.
104  * Its size is derived from the ihash table size.
105  */
106 void
107 xfs_chash_init(xfs_mount_t *mp)
108 {
109         uint    i;
110
111         mp->m_chsize = max_t(uint, 1, mp->m_ihsize /
112                          (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog));
113         mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize);
114         mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize
115                                                  * sizeof(xfs_chash_t),
116                                                  KM_SLEEP);
117         for (i = 0; i < mp->m_chsize; i++) {
118                 spinlock_init(&mp->m_chash[i].ch_lock,"xfshash");
119         }
120 }
121
122 /*
123  * Free up structures allocated by xfs_chash_init, at unmount time.
124  */
125 void
126 xfs_chash_free(xfs_mount_t *mp)
127 {
128         int     i;
129
130         for (i = 0; i < mp->m_chsize; i++) {
131                 spinlock_destroy(&mp->m_chash[i].ch_lock);
132         }
133
134         kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
135         mp->m_chash = NULL;
136 }
137
138 /*
139  * Try to move an inode to the front of its hash list if possible
140  * (and if its not there already).  Called right after obtaining
141  * the list version number and then dropping the read_lock on the
142  * hash list in question (which is done right after looking up the
143  * inode in question...).
144  */
145 STATIC void
146 xfs_ihash_promote(
147         xfs_ihash_t     *ih,
148         xfs_inode_t     *ip,
149         ulong           version)
150 {
151         xfs_inode_t     *iq;
152
153         if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) {
154                 if (likely(version == ih->ih_version)) {
155                         /* remove from list */
156                         if ((iq = ip->i_next)) {
157                                 iq->i_prevp = ip->i_prevp;
158                         }
159                         *ip->i_prevp = iq;
160
161                         /* insert at list head */
162                         iq = ih->ih_next;
163                         iq->i_prevp = &ip->i_next;
164                         ip->i_next = iq;
165                         ip->i_prevp = &ih->ih_next;
166                         ih->ih_next = ip;
167                 }
168                 write_unlock(&ih->ih_lock);
169         }
170 }
171
172 /*
173  * Look up an inode by number in the given file system.
174  * The inode is looked up in the hash table for the file system
175  * represented by the mount point parameter mp.  Each bucket of
176  * the hash table is guarded by an individual semaphore.
177  *
178  * If the inode is found in the hash table, its corresponding vnode
179  * is obtained with a call to vn_get().  This call takes care of
180  * coordination with the reclamation of the inode and vnode.  Note
181  * that the vmap structure is filled in while holding the hash lock.
182  * This gives us the state of the inode/vnode when we found it and
183  * is used for coordination in vn_get().
184  *
185  * If it is not in core, read it in from the file system's device and
186  * add the inode into the hash table.
187  *
188  * The inode is locked according to the value of the lock_flags parameter.
189  * This flag parameter indicates how and if the inode's IO lock and inode lock
190  * should be taken.
191  *
192  * mp -- the mount point structure for the current file system.  It points
193  *       to the inode hash table.
194  * tp -- a pointer to the current transaction if there is one.  This is
195  *       simply passed through to the xfs_iread() call.
196  * ino -- the number of the inode desired.  This is the unique identifier
197  *        within the file system for the inode being requested.
198  * lock_flags -- flags indicating how to lock the inode.  See the comment
199  *               for xfs_ilock() for a list of valid values.
200  * bno -- the block number starting the buffer containing the inode,
201  *        if known (as by bulkstat), else 0.
202  */
203 STATIC int
204 xfs_iget_core(
205         vnode_t         *vp,
206         xfs_mount_t     *mp,
207         xfs_trans_t     *tp,
208         xfs_ino_t       ino,
209         uint            flags,
210         uint            lock_flags,
211         xfs_inode_t     **ipp,
212         xfs_daddr_t     bno)
213 {
214         xfs_ihash_t     *ih;
215         xfs_inode_t     *ip;
216         xfs_inode_t     *iq;
217         vnode_t         *inode_vp;
218         ulong           version;
219         int             error;
220         /* REFERENCED */
221         xfs_chash_t     *ch;
222         xfs_chashlist_t *chl, *chlnew;
223         SPLDECL(s);
224
225
226         ih = XFS_IHASH(mp, ino);
227
228 again:
229         read_lock(&ih->ih_lock);
230
231         for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
232                 if (ip->i_ino == ino) {
233                         /*
234                          * If INEW is set this inode is being set up
235                          * we need to pause and try again.
236                          */
237                         if (ip->i_flags & XFS_INEW) {
238                                 read_unlock(&ih->ih_lock);
239                                 delay(1);
240                                 XFS_STATS_INC(xs_ig_frecycle);
241
242                                 goto again;
243                         }
244
245                         inode_vp = XFS_ITOV_NULL(ip);
246                         if (inode_vp == NULL) {
247                                 /*
248                                  * If IRECLAIM is set this inode is
249                                  * on its way out of the system,
250                                  * we need to pause and try again.
251                                  */
252                                 if (ip->i_flags & XFS_IRECLAIM) {
253                                         read_unlock(&ih->ih_lock);
254                                         delay(1);
255                                         XFS_STATS_INC(xs_ig_frecycle);
256
257                                         goto again;
258                                 }
259
260                                 vn_trace_exit(vp, "xfs_iget.alloc",
261                                         (inst_t *)__return_address);
262
263                                 XFS_STATS_INC(xs_ig_found);
264
265                                 ip->i_flags &= ~XFS_IRECLAIMABLE;
266                                 version = ih->ih_version;
267                                 read_unlock(&ih->ih_lock);
268                                 xfs_ihash_promote(ih, ip, version);
269
270                                 XFS_MOUNT_ILOCK(mp);
271                                 list_del_init(&ip->i_reclaim);
272                                 XFS_MOUNT_IUNLOCK(mp);
273
274                                 goto finish_inode;
275
276                         } else if (vp != inode_vp) {
277                                 struct inode *inode = LINVFS_GET_IP(inode_vp);
278
279                                 /* The inode is being torn down, pause and
280                                  * try again.
281                                  */
282                                 if (inode->i_state & (I_FREEING | I_CLEAR)) {
283                                         read_unlock(&ih->ih_lock);
284                                         delay(1);
285                                         XFS_STATS_INC(xs_ig_frecycle);
286
287                                         goto again;
288                                 }
289 /* Chances are the other vnode (the one in the inode) is being torn
290  * down right now, and we landed on top of it. Question is, what do
291  * we do? Unhook the old inode and hook up the new one?
292  */
293                                 cmn_err(CE_PANIC,
294                         "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
295                                                 inode_vp, vp);
296                         }
297
298                         /*
299                          * Inode cache hit: if ip is not at the front of
300                          * its hash chain, move it there now.
301                          * Do this with the lock held for update, but
302                          * do statistics after releasing the lock.
303                          */
304                         version = ih->ih_version;
305                         read_unlock(&ih->ih_lock);
306                         xfs_ihash_promote(ih, ip, version);
307                         XFS_STATS_INC(xs_ig_found);
308
309 finish_inode:
310                         if (ip->i_d.di_mode == 0) {
311                                 if (!(flags & IGET_CREATE))
312                                         return ENOENT;
313                                 xfs_iocore_inode_reinit(ip);
314                         }
315         
316                         if (lock_flags != 0)
317                                 xfs_ilock(ip, lock_flags);
318
319                         ip->i_flags &= ~XFS_ISTALE;
320
321                         vn_trace_exit(vp, "xfs_iget.found",
322                                                 (inst_t *)__return_address);
323                         goto return_ip;
324                 }
325         }
326
327         /*
328          * Inode cache miss: save the hash chain version stamp and unlock
329          * the chain, so we don't deadlock in vn_alloc.
330          */
331         XFS_STATS_INC(xs_ig_missed);
332
333         version = ih->ih_version;
334
335         read_unlock(&ih->ih_lock);
336
337         /*
338          * Read the disk inode attributes into a new inode structure and get
339          * a new vnode for it. This should also initialize i_ino and i_mount.
340          */
341         error = xfs_iread(mp, tp, ino, &ip, bno);
342         if (error) {
343                 return error;
344         }
345
346         vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
347
348         xfs_inode_lock_init(ip, vp);
349         xfs_iocore_inode_init(ip);
350
351         if (lock_flags != 0) {
352                 xfs_ilock(ip, lock_flags);
353         }
354                 
355         if ((ip->i_d.di_mode == 0) && !(flags & IGET_CREATE)) {
356                 xfs_idestroy(ip);
357                 return ENOENT;
358         }
359
360         /*
361          * Put ip on its hash chain, unless someone else hashed a duplicate
362          * after we released the hash lock.
363          */
364         write_lock(&ih->ih_lock);
365
366         if (ih->ih_version != version) {
367                 for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
368                         if (iq->i_ino == ino) {
369                                 write_unlock(&ih->ih_lock);
370                                 xfs_idestroy(ip);
371
372                                 XFS_STATS_INC(xs_ig_dup);
373                                 goto again;
374                         }
375                 }
376         }
377
378         /*
379          * These values _must_ be set before releasing ihlock!
380          */
381         ip->i_hash = ih;
382         if ((iq = ih->ih_next)) {
383                 iq->i_prevp = &ip->i_next;
384         }
385         ip->i_next = iq;
386         ip->i_prevp = &ih->ih_next;
387         ih->ih_next = ip;
388         ip->i_udquot = ip->i_gdquot = NULL;
389         ih->ih_version++;
390         ip->i_flags |= XFS_INEW;
391
392         write_unlock(&ih->ih_lock);
393
394         /*
395          * put ip on its cluster's hash chain
396          */
397         ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
398                ip->i_cnext == NULL);
399
400         chlnew = NULL;
401         ch = XFS_CHASH(mp, ip->i_blkno);
402  chlredo:
403         s = mutex_spinlock(&ch->ch_lock);
404         for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
405                 if (chl->chl_blkno == ip->i_blkno) {
406
407                         /* insert this inode into the doubly-linked list
408                          * where chl points */
409                         if ((iq = chl->chl_ip)) {
410                                 ip->i_cprev = iq->i_cprev;
411                                 iq->i_cprev->i_cnext = ip;
412                                 iq->i_cprev = ip;
413                                 ip->i_cnext = iq;
414                         } else {
415                                 ip->i_cnext = ip;
416                                 ip->i_cprev = ip;
417                         }
418                         chl->chl_ip = ip;
419                         ip->i_chash = chl;
420                         break;
421                 }
422         }
423
424         /* no hash list found for this block; add a new hash list */
425         if (chl == NULL)  {
426                 if (chlnew == NULL) {
427                         mutex_spinunlock(&ch->ch_lock, s);
428                         ASSERT(xfs_chashlist_zone != NULL);
429                         chlnew = (xfs_chashlist_t *)
430                                         kmem_zone_alloc(xfs_chashlist_zone,
431                                                 KM_SLEEP);
432                         ASSERT(chlnew != NULL);
433                         goto chlredo;
434                 } else {
435                         ip->i_cnext = ip;
436                         ip->i_cprev = ip;
437                         ip->i_chash = chlnew;
438                         chlnew->chl_ip = ip;
439                         chlnew->chl_blkno = ip->i_blkno;
440                         chlnew->chl_next = ch->ch_list;
441                         ch->ch_list = chlnew;
442                         chlnew = NULL;
443                 }
444         } else {
445                 if (chlnew != NULL) {
446                         kmem_zone_free(xfs_chashlist_zone, chlnew);
447                 }
448         }
449
450         mutex_spinunlock(&ch->ch_lock, s);
451
452
453         /*
454          * Link ip to its mount and thread it on the mount's inode list.
455          */
456         XFS_MOUNT_ILOCK(mp);
457         if ((iq = mp->m_inodes)) {
458                 ASSERT(iq->i_mprev->i_mnext == iq);
459                 ip->i_mprev = iq->i_mprev;
460                 iq->i_mprev->i_mnext = ip;
461                 iq->i_mprev = ip;
462                 ip->i_mnext = iq;
463         } else {
464                 ip->i_mnext = ip;
465                 ip->i_mprev = ip;
466         }
467         mp->m_inodes = ip;
468
469         XFS_MOUNT_IUNLOCK(mp);
470
471  return_ip:
472         ASSERT(ip->i_df.if_ext_max ==
473                XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
474
475         ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
476                ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
477
478         *ipp = ip;
479
480         /*
481          * If we have a real type for an on-disk inode, we can set ops(&unlock)
482          * now.  If it's a new inode being created, xfs_ialloc will handle it.
483          */
484         VFS_INIT_VNODE(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
485
486         return 0;
487 }
488
489
490 /*
491  * The 'normal' internal xfs_iget, if needed it will
492  * 'allocate', or 'get', the vnode.
493  */
494 int
495 xfs_iget(
496         xfs_mount_t     *mp,
497         xfs_trans_t     *tp,
498         xfs_ino_t       ino,
499         uint            flags,
500         uint            lock_flags,
501         xfs_inode_t     **ipp,
502         xfs_daddr_t     bno)
503 {
504         struct inode    *inode;
505         vnode_t         *vp = NULL;
506         int             error;
507
508         XFS_STATS_INC(xs_ig_attempts);
509
510         if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
511                 bhv_desc_t      *bdp;
512                 xfs_inode_t     *ip;
513                 int             newnode;
514
515                 vp = LINVFS_GET_VP(inode);
516                 if (inode->i_state & I_NEW) {
517 inode_allocate:
518                         vn_initialize(inode);
519                         error = xfs_iget_core(vp, mp, tp, ino, flags,
520                                         lock_flags, ipp, bno);
521                         if (error) {
522                                 vn_mark_bad(vp);
523                                 if (inode->i_state & I_NEW)
524                                         unlock_new_inode(inode);
525                                 iput(inode);
526                         }
527                 } else {
528                         if (is_bad_inode(inode)) {
529                                 iput(inode);
530                                 return EIO;
531                         }
532
533                         bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
534                         if (bdp == NULL) {
535                                 XFS_STATS_INC(xs_ig_dup);
536                                 goto inode_allocate;
537                         }
538                         ip = XFS_BHVTOI(bdp);
539                         if (lock_flags != 0)
540                                 xfs_ilock(ip, lock_flags);
541                         newnode = (ip->i_d.di_mode == 0);
542                         if (newnode)
543                                 xfs_iocore_inode_reinit(ip);
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 }