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