[PATCH] ext3: fix log_do_checkpoint() assertion failure
[linux-2.6/pdupreez.git] / fs / xfs / xfs_iget.c
blobd3da00045f260074fca466319a23bfdc34ed14c3
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
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"
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.
67 void
68 xfs_ihash_init(xfs_mount_t *mp)
70 __uint64_t icount;
71 uint i, flags = KM_SLEEP | KM_MAYFAIL;
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));
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;
87 for (i = 0; i < mp->m_ihsize; i++) {
88 rwlock_init(&(mp->m_ihash[i].ih_lock));
93 * Free up structures allocated by xfs_ihash_init, at unmount time.
95 void
96 xfs_ihash_free(xfs_mount_t *mp)
98 kmem_free(mp->m_ihash, mp->m_ihsize*sizeof(xfs_ihash_t));
99 mp->m_ihash = NULL;
103 * Initialize the inode cluster hash table for the newly mounted file system.
104 * Its size is derived from the ihash table size.
106 void
107 xfs_chash_init(xfs_mount_t *mp)
109 uint i;
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");
123 * Free up structures allocated by xfs_chash_init, at unmount time.
125 void
126 xfs_chash_free(xfs_mount_t *mp)
128 int i;
130 for (i = 0; i < mp->m_chsize; i++) {
131 spinlock_destroy(&mp->m_chash[i].ch_lock);
134 kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
135 mp->m_chash = NULL;
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...).
145 STATIC void
146 xfs_ihash_promote(
147 xfs_ihash_t *ih,
148 xfs_inode_t *ip,
149 ulong version)
151 xfs_inode_t *iq;
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;
159 *ip->i_prevp = iq;
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;
168 write_unlock(&ih->ih_lock);
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.
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().
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.
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.
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.
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)
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);
226 ih = XFS_IHASH(mp, ino);
228 again:
229 read_lock(&ih->ih_lock);
231 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
232 if (ip->i_ino == ino) {
234 * If INEW is set this inode is being set up
235 * we need to pause and try again.
237 if (ip->i_flags & XFS_INEW) {
238 read_unlock(&ih->ih_lock);
239 delay(1);
240 XFS_STATS_INC(xs_ig_frecycle);
242 goto again;
245 inode_vp = XFS_ITOV_NULL(ip);
246 if (inode_vp == NULL) {
248 * If IRECLAIM is set this inode is
249 * on its way out of the system,
250 * we need to pause and try again.
252 if (ip->i_flags & XFS_IRECLAIM) {
253 read_unlock(&ih->ih_lock);
254 delay(1);
255 XFS_STATS_INC(xs_ig_frecycle);
257 goto again;
260 vn_trace_exit(vp, "xfs_iget.alloc",
261 (inst_t *)__return_address);
263 XFS_STATS_INC(xs_ig_found);
265 ip->i_flags &= ~XFS_IRECLAIMABLE;
266 version = ih->ih_version;
267 read_unlock(&ih->ih_lock);
268 xfs_ihash_promote(ih, ip, version);
270 XFS_MOUNT_ILOCK(mp);
271 list_del_init(&ip->i_reclaim);
272 XFS_MOUNT_IUNLOCK(mp);
274 goto finish_inode;
276 } else if (vp != inode_vp) {
277 struct inode *inode = LINVFS_GET_IP(inode_vp);
279 /* The inode is being torn down, pause and
280 * try again.
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);
287 goto again;
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?
293 cmn_err(CE_PANIC,
294 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
295 inode_vp, vp);
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.
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);
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);
316 if (lock_flags != 0)
317 xfs_ilock(ip, lock_flags);
319 ip->i_flags &= ~XFS_ISTALE;
321 vn_trace_exit(vp, "xfs_iget.found",
322 (inst_t *)__return_address);
323 goto return_ip;
328 * Inode cache miss: save the hash chain version stamp and unlock
329 * the chain, so we don't deadlock in vn_alloc.
331 XFS_STATS_INC(xs_ig_missed);
333 version = ih->ih_version;
335 read_unlock(&ih->ih_lock);
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.
341 error = xfs_iread(mp, tp, ino, &ip, bno);
342 if (error) {
343 return error;
346 vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
348 xfs_inode_lock_init(ip, vp);
349 xfs_iocore_inode_init(ip);
351 if (lock_flags != 0) {
352 xfs_ilock(ip, lock_flags);
355 if ((ip->i_d.di_mode == 0) && !(flags & IGET_CREATE)) {
356 xfs_idestroy(ip);
357 return ENOENT;
361 * Put ip on its hash chain, unless someone else hashed a duplicate
362 * after we released the hash lock.
364 write_lock(&ih->ih_lock);
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);
372 XFS_STATS_INC(xs_ig_dup);
373 goto again;
379 * These values _must_ be set before releasing ihlock!
381 ip->i_hash = ih;
382 if ((iq = ih->ih_next)) {
383 iq->i_prevp = &ip->i_next;
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;
392 write_unlock(&ih->ih_lock);
395 * put ip on its cluster's hash chain
397 ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
398 ip->i_cnext == NULL);
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) {
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;
418 chl->chl_ip = ip;
419 ip->i_chash = chl;
420 break;
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;
444 } else {
445 if (chlnew != NULL) {
446 kmem_zone_free(xfs_chashlist_zone, chlnew);
450 mutex_spinunlock(&ch->ch_lock, s);
454 * Link ip to its mount and thread it on the mount's inode list.
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;
467 mp->m_inodes = ip;
469 XFS_MOUNT_IUNLOCK(mp);
471 return_ip:
472 ASSERT(ip->i_df.if_ext_max ==
473 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
475 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
476 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
478 *ipp = ip;
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.
484 VFS_INIT_VNODE(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
486 return 0;
491 * The 'normal' internal xfs_iget, if needed it will
492 * 'allocate', or 'get', the vnode.
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)
504 struct inode *inode;
505 vnode_t *vp = NULL;
506 int error;
508 retry:
509 XFS_STATS_INC(xs_ig_attempts);
511 if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
512 bhv_desc_t *bdp;
513 xfs_inode_t *ip;
514 int newnode;
516 vp = LINVFS_GET_VP(inode);
517 if (inode->i_state & I_NEW) {
518 inode_allocate:
519 vn_initialize(inode);
520 error = xfs_iget_core(vp, mp, tp, ino, flags,
521 lock_flags, ipp, bno);
522 if (error) {
523 vn_mark_bad(vp);
524 if (inode->i_state & I_NEW)
525 unlock_new_inode(inode);
526 iput(inode);
528 } else {
529 /* These are true if the inode is in inactive or
530 * reclaim. The linux inode is about to go away,
531 * wait for that path to finish, and try again.
533 if (vp->v_flag & (VINACT | VRECLM)) {
534 vn_wait(vp);
535 iput(inode);
536 goto retry;
539 if (is_bad_inode(inode)) {
540 iput(inode);
541 return EIO;
544 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
545 if (bdp == NULL) {
546 XFS_STATS_INC(xs_ig_dup);
547 goto inode_allocate;
549 ip = XFS_BHVTOI(bdp);
550 if (lock_flags != 0)
551 xfs_ilock(ip, lock_flags);
552 newnode = (ip->i_d.di_mode == 0);
553 if (newnode)
554 xfs_iocore_inode_reinit(ip);
555 XFS_STATS_INC(xs_ig_found);
556 *ipp = ip;
557 error = 0;
559 } else
560 error = ENOMEM; /* If we got no inode we are out of memory */
562 return error;
566 * Do the setup for the various locks within the incore inode.
568 void
569 xfs_inode_lock_init(
570 xfs_inode_t *ip,
571 vnode_t *vp)
573 mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
574 "xfsino", (long)vp->v_number);
575 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
576 init_waitqueue_head(&ip->i_ipin_wait);
577 atomic_set(&ip->i_pincount, 0);
578 init_sema(&ip->i_flock, 1, "xfsfino", vp->v_number);
582 * Look for the inode corresponding to the given ino in the hash table.
583 * If it is there and its i_transp pointer matches tp, return it.
584 * Otherwise, return NULL.
586 xfs_inode_t *
587 xfs_inode_incore(xfs_mount_t *mp,
588 xfs_ino_t ino,
589 xfs_trans_t *tp)
591 xfs_ihash_t *ih;
592 xfs_inode_t *ip;
593 ulong version;
595 ih = XFS_IHASH(mp, ino);
596 read_lock(&ih->ih_lock);
597 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
598 if (ip->i_ino == ino) {
600 * If we find it and tp matches, return it.
601 * Also move it to the front of the hash list
602 * if we find it and it is not already there.
603 * Otherwise break from the loop and return
604 * NULL.
606 if (ip->i_transp == tp) {
607 version = ih->ih_version;
608 read_unlock(&ih->ih_lock);
609 xfs_ihash_promote(ih, ip, version);
610 return (ip);
612 break;
615 read_unlock(&ih->ih_lock);
616 return (NULL);
620 * Decrement reference count of an inode structure and unlock it.
622 * ip -- the inode being released
623 * lock_flags -- this parameter indicates the inode's locks to be
624 * to be released. See the comment on xfs_iunlock() for a list
625 * of valid values.
627 void
628 xfs_iput(xfs_inode_t *ip,
629 uint lock_flags)
631 vnode_t *vp = XFS_ITOV(ip);
633 vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
635 xfs_iunlock(ip, lock_flags);
637 VN_RELE(vp);
641 * Special iput for brand-new inodes that are still locked
643 void
644 xfs_iput_new(xfs_inode_t *ip,
645 uint lock_flags)
647 vnode_t *vp = XFS_ITOV(ip);
648 struct inode *inode = LINVFS_GET_IP(vp);
650 vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
652 if ((ip->i_d.di_mode == 0)) {
653 ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
654 vn_mark_bad(vp);
656 if (inode->i_state & I_NEW)
657 unlock_new_inode(inode);
658 if (lock_flags)
659 xfs_iunlock(ip, lock_flags);
660 VN_RELE(vp);
665 * This routine embodies the part of the reclaim code that pulls
666 * the inode from the inode hash table and the mount structure's
667 * inode list.
668 * This should only be called from xfs_reclaim().
670 void
671 xfs_ireclaim(xfs_inode_t *ip)
673 vnode_t *vp;
676 * Remove from old hash list and mount list.
678 XFS_STATS_INC(xs_ig_reclaims);
680 xfs_iextract(ip);
683 * Here we do a spurious inode lock in order to coordinate with
684 * xfs_sync(). This is because xfs_sync() references the inodes
685 * in the mount list without taking references on the corresponding
686 * vnodes. We make that OK here by ensuring that we wait until
687 * the inode is unlocked in xfs_sync() before we go ahead and
688 * free it. We get both the regular lock and the io lock because
689 * the xfs_sync() code may need to drop the regular one but will
690 * still hold the io lock.
692 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
695 * Release dquots (and their references) if any. An inode may escape
696 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
698 XFS_QM_DQDETACH(ip->i_mount, ip);
701 * Pull our behavior descriptor from the vnode chain.
703 vp = XFS_ITOV_NULL(ip);
704 if (vp) {
705 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
709 * Free all memory associated with the inode.
711 xfs_idestroy(ip);
715 * This routine removes an about-to-be-destroyed inode from
716 * all of the lists in which it is located with the exception
717 * of the behavior chain.
719 void
720 xfs_iextract(
721 xfs_inode_t *ip)
723 xfs_ihash_t *ih;
724 xfs_inode_t *iq;
725 xfs_mount_t *mp;
726 xfs_chash_t *ch;
727 xfs_chashlist_t *chl, *chm;
728 SPLDECL(s);
730 ih = ip->i_hash;
731 write_lock(&ih->ih_lock);
732 if ((iq = ip->i_next)) {
733 iq->i_prevp = ip->i_prevp;
735 *ip->i_prevp = iq;
736 ih->ih_version++;
737 write_unlock(&ih->ih_lock);
740 * Remove from cluster hash list
741 * 1) delete the chashlist if this is the last inode on the chashlist
742 * 2) unchain from list of inodes
743 * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
745 mp = ip->i_mount;
746 ch = XFS_CHASH(mp, ip->i_blkno);
747 s = mutex_spinlock(&ch->ch_lock);
749 if (ip->i_cnext == ip) {
750 /* Last inode on chashlist */
751 ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
752 ASSERT(ip->i_chash != NULL);
753 chm=NULL;
754 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
755 if (chl->chl_blkno == ip->i_blkno) {
756 if (chm == NULL) {
757 /* first item on the list */
758 ch->ch_list = chl->chl_next;
759 } else {
760 chm->chl_next = chl->chl_next;
762 kmem_zone_free(xfs_chashlist_zone, chl);
763 break;
764 } else {
765 ASSERT(chl->chl_ip != ip);
766 chm = chl;
769 ASSERT_ALWAYS(chl != NULL);
770 } else {
771 /* delete one inode from a non-empty list */
772 iq = ip->i_cnext;
773 iq->i_cprev = ip->i_cprev;
774 ip->i_cprev->i_cnext = iq;
775 if (ip->i_chash->chl_ip == ip) {
776 ip->i_chash->chl_ip = iq;
778 ip->i_chash = __return_address;
779 ip->i_cprev = __return_address;
780 ip->i_cnext = __return_address;
782 mutex_spinunlock(&ch->ch_lock, s);
785 * Remove from mount's inode list.
787 XFS_MOUNT_ILOCK(mp);
788 ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
789 iq = ip->i_mnext;
790 iq->i_mprev = ip->i_mprev;
791 ip->i_mprev->i_mnext = iq;
794 * Fix up the head pointer if it points to the inode being deleted.
796 if (mp->m_inodes == ip) {
797 if (ip == iq) {
798 mp->m_inodes = NULL;
799 } else {
800 mp->m_inodes = iq;
804 /* Deal with the deleted inodes list */
805 list_del_init(&ip->i_reclaim);
807 mp->m_ireclaims++;
808 XFS_MOUNT_IUNLOCK(mp);
812 * This is a wrapper routine around the xfs_ilock() routine
813 * used to centralize some grungy code. It is used in places
814 * that wish to lock the inode solely for reading the extents.
815 * The reason these places can't just call xfs_ilock(SHARED)
816 * is that the inode lock also guards to bringing in of the
817 * extents from disk for a file in b-tree format. If the inode
818 * is in b-tree format, then we need to lock the inode exclusively
819 * until the extents are read in. Locking it exclusively all
820 * the time would limit our parallelism unnecessarily, though.
821 * What we do instead is check to see if the extents have been
822 * read in yet, and only lock the inode exclusively if they
823 * have not.
825 * The function returns a value which should be given to the
826 * corresponding xfs_iunlock_map_shared(). This value is
827 * the mode in which the lock was actually taken.
829 uint
830 xfs_ilock_map_shared(
831 xfs_inode_t *ip)
833 uint lock_mode;
835 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
836 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
837 lock_mode = XFS_ILOCK_EXCL;
838 } else {
839 lock_mode = XFS_ILOCK_SHARED;
842 xfs_ilock(ip, lock_mode);
844 return lock_mode;
848 * This is simply the unlock routine to go with xfs_ilock_map_shared().
849 * All it does is call xfs_iunlock() with the given lock_mode.
851 void
852 xfs_iunlock_map_shared(
853 xfs_inode_t *ip,
854 unsigned int lock_mode)
856 xfs_iunlock(ip, lock_mode);
860 * The xfs inode contains 2 locks: a multi-reader lock called the
861 * i_iolock and a multi-reader lock called the i_lock. This routine
862 * allows either or both of the locks to be obtained.
864 * The 2 locks should always be ordered so that the IO lock is
865 * obtained first in order to prevent deadlock.
867 * ip -- the inode being locked
868 * lock_flags -- this parameter indicates the inode's locks
869 * to be locked. It can be:
870 * XFS_IOLOCK_SHARED,
871 * XFS_IOLOCK_EXCL,
872 * XFS_ILOCK_SHARED,
873 * XFS_ILOCK_EXCL,
874 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
875 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
876 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
877 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
879 void
880 xfs_ilock(xfs_inode_t *ip,
881 uint lock_flags)
884 * You can't set both SHARED and EXCL for the same lock,
885 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
886 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
888 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
889 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
890 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
891 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
892 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
894 if (lock_flags & XFS_IOLOCK_EXCL) {
895 mrupdate(&ip->i_iolock);
896 } else if (lock_flags & XFS_IOLOCK_SHARED) {
897 mraccess(&ip->i_iolock);
899 if (lock_flags & XFS_ILOCK_EXCL) {
900 mrupdate(&ip->i_lock);
901 } else if (lock_flags & XFS_ILOCK_SHARED) {
902 mraccess(&ip->i_lock);
904 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
908 * This is just like xfs_ilock(), except that the caller
909 * is guaranteed not to sleep. It returns 1 if it gets
910 * the requested locks and 0 otherwise. If the IO lock is
911 * obtained but the inode lock cannot be, then the IO lock
912 * is dropped before returning.
914 * ip -- the inode being locked
915 * lock_flags -- this parameter indicates the inode's locks to be
916 * to be locked. See the comment for xfs_ilock() for a list
917 * of valid values.
921 xfs_ilock_nowait(xfs_inode_t *ip,
922 uint lock_flags)
924 int iolocked;
925 int ilocked;
928 * You can't set both SHARED and EXCL for the same lock,
929 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
930 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
932 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
933 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
934 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
935 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
936 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
938 iolocked = 0;
939 if (lock_flags & XFS_IOLOCK_EXCL) {
940 iolocked = mrtryupdate(&ip->i_iolock);
941 if (!iolocked) {
942 return 0;
944 } else if (lock_flags & XFS_IOLOCK_SHARED) {
945 iolocked = mrtryaccess(&ip->i_iolock);
946 if (!iolocked) {
947 return 0;
950 if (lock_flags & XFS_ILOCK_EXCL) {
951 ilocked = mrtryupdate(&ip->i_lock);
952 if (!ilocked) {
953 if (iolocked) {
954 mrunlock(&ip->i_iolock);
956 return 0;
958 } else if (lock_flags & XFS_ILOCK_SHARED) {
959 ilocked = mrtryaccess(&ip->i_lock);
960 if (!ilocked) {
961 if (iolocked) {
962 mrunlock(&ip->i_iolock);
964 return 0;
967 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
968 return 1;
972 * xfs_iunlock() is used to drop the inode locks acquired with
973 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
974 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
975 * that we know which locks to drop.
977 * ip -- the inode being unlocked
978 * lock_flags -- this parameter indicates the inode's locks to be
979 * to be unlocked. See the comment for xfs_ilock() for a list
980 * of valid values for this parameter.
983 void
984 xfs_iunlock(xfs_inode_t *ip,
985 uint lock_flags)
988 * You can't set both SHARED and EXCL for the same lock,
989 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
990 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
992 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
993 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
994 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
995 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
996 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
997 ASSERT(lock_flags != 0);
999 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
1000 ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
1001 (ismrlocked(&ip->i_iolock, MR_ACCESS)));
1002 ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
1003 (ismrlocked(&ip->i_iolock, MR_UPDATE)));
1004 mrunlock(&ip->i_iolock);
1007 if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
1008 ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
1009 (ismrlocked(&ip->i_lock, MR_ACCESS)));
1010 ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
1011 (ismrlocked(&ip->i_lock, MR_UPDATE)));
1012 mrunlock(&ip->i_lock);
1015 * Let the AIL know that this item has been unlocked in case
1016 * it is in the AIL and anyone is waiting on it. Don't do
1017 * this if the caller has asked us not to.
1019 if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
1020 ip->i_itemp != NULL) {
1021 xfs_trans_unlocked_item(ip->i_mount,
1022 (xfs_log_item_t*)(ip->i_itemp));
1025 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
1029 * give up write locks. the i/o lock cannot be held nested
1030 * if it is being demoted.
1032 void
1033 xfs_ilock_demote(xfs_inode_t *ip,
1034 uint lock_flags)
1036 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
1037 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
1039 if (lock_flags & XFS_ILOCK_EXCL) {
1040 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
1041 mrdemote(&ip->i_lock);
1043 if (lock_flags & XFS_IOLOCK_EXCL) {
1044 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1045 mrdemote(&ip->i_iolock);
1050 * The following three routines simply manage the i_flock
1051 * semaphore embedded in the inode. This semaphore synchronizes
1052 * processes attempting to flush the in-core inode back to disk.
1054 void
1055 xfs_iflock(xfs_inode_t *ip)
1057 psema(&(ip->i_flock), PINOD|PLTWAIT);
1061 xfs_iflock_nowait(xfs_inode_t *ip)
1063 return (cpsema(&(ip->i_flock)));
1066 void
1067 xfs_ifunlock(xfs_inode_t *ip)
1069 ASSERT(valusema(&(ip->i_flock)) <= 0);
1070 vsema(&(ip->i_flock));