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