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:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
39 #include "xfs_trans.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"
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
68 xfs_ihash_init(xfs_mount_t *mp)
71 uint i, flags = KM_SLEEP | KM_MAYFAIL;
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)
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.
96 xfs_ihash_free(xfs_mount_t *mp)
98 kmem_free(mp->m_ihash, mp->m_ihsize*sizeof(xfs_ihash_t));
103 * Initialize the inode cluster hash table for the newly mounted file system.
104 * Its size is derived from the ihash table size.
107 xfs_chash_init(xfs_mount_t *mp)
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),
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.
126 xfs_chash_free(xfs_mount_t *mp)
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));
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...).
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;
161 /* insert at list head */
163 iq->i_prevp = &ip->i_next;
165 ip->i_prevp = &ih->ih_next;
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
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.
222 xfs_chashlist_t *chl, *chlnew;
226 ih = XFS_IHASH(mp, ino);
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);
240 XFS_STATS_INC(xs_ig_frecycle);
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);
255 XFS_STATS_INC(xs_ig_frecycle);
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);
271 list_del_init(&ip->i_reclaim);
272 XFS_MOUNT_IUNLOCK(mp);
276 } else if (vp != inode_vp) {
277 struct inode *inode = LINVFS_GET_IP(inode_vp);
279 /* The inode is being torn down, pause and
282 if (inode->i_state & (I_FREEING | I_CLEAR)) {
283 read_unlock(&ih->ih_lock);
285 XFS_STATS_INC(xs_ig_frecycle);
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?
294 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
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);
310 if (ip->i_d.di_mode == 0) {
311 if (!(flags & IGET_CREATE))
313 xfs_iocore_inode_reinit(ip);
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);
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);
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)) {
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);
372 XFS_STATS_INC(xs_ig_dup);
379 * These values _must_ be set before releasing ihlock!
382 if ((iq = ih->ih_next)) {
383 iq->i_prevp = &ip->i_next;
386 ip->i_prevp = &ih->ih_next;
388 ip->i_udquot = ip->i_gdquot = NULL;
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);
401 ch = XFS_CHASH(mp, ip->i_blkno);
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;
424 /* no hash list found for this block; add a new hash list */
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,
432 ASSERT(chlnew != NULL);
437 ip->i_chash = chlnew;
439 chlnew->chl_blkno = ip->i_blkno;
440 chlnew->chl_next = ch->ch_list;
441 ch->ch_list = chlnew;
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.
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;
469 XFS_MOUNT_IUNLOCK(mp);
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));
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);
491 * The 'normal' internal xfs_iget, if needed it will
492 * 'allocate', or 'get', the vnode.
509 XFS_STATS_INC(xs_ig_attempts);
511 if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
516 vp = LINVFS_GET_VP(inode);
517 if (inode->i_state & I_NEW) {
519 vn_initialize(inode);
520 error = xfs_iget_core(vp, mp, tp, ino, flags,
521 lock_flags, ipp, bno);
524 if (inode->i_state & I_NEW)
525 unlock_new_inode(inode);
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)) {
539 if (is_bad_inode(inode)) {
544 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
546 XFS_STATS_INC(xs_ig_dup);
549 ip = XFS_BHVTOI(bdp);
551 xfs_ilock(ip, lock_flags);
552 newnode = (ip->i_d.di_mode == 0);
554 xfs_iocore_inode_reinit(ip);
555 XFS_STATS_INC(xs_ig_found);
560 error = ENOMEM; /* If we got no inode we are out of memory */
566 * Do the setup for the various locks within the incore inode.
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.
587 xfs_inode_incore(xfs_mount_t *mp,
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
606 if (ip->i_transp == tp) {
607 version = ih->ih_version;
608 read_unlock(&ih->ih_lock);
609 xfs_ihash_promote(ih, ip, version);
615 read_unlock(&ih->ih_lock);
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
628 xfs_iput(xfs_inode_t *ip,
631 vnode_t *vp = XFS_ITOV(ip);
633 vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
635 xfs_iunlock(ip, lock_flags);
641 * Special iput for brand-new inodes that are still locked
644 xfs_iput_new(xfs_inode_t *ip,
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));
656 if (inode->i_state & I_NEW)
657 unlock_new_inode(inode);
659 xfs_iunlock(ip, lock_flags);
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
668 * This should only be called from xfs_reclaim().
671 xfs_ireclaim(xfs_inode_t *ip)
676 * Remove from old hash list and mount list.
678 XFS_STATS_INC(xs_ig_reclaims);
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);
705 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
709 * Free all memory associated with the inode.
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.
727 xfs_chashlist_t *chl, *chm;
731 write_lock(&ih->ih_lock);
732 if ((iq = ip->i_next)) {
733 iq->i_prevp = ip->i_prevp;
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.
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);
754 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
755 if (chl->chl_blkno == ip->i_blkno) {
757 /* first item on the list */
758 ch->ch_list = chl->chl_next;
760 chm->chl_next = chl->chl_next;
762 kmem_zone_free(xfs_chashlist_zone, chl);
765 ASSERT(chl->chl_ip != ip);
769 ASSERT_ALWAYS(chl != NULL);
771 /* delete one inode from a non-empty list */
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.
788 ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
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) {
804 /* Deal with the deleted inodes list */
805 list_del_init(&ip->i_reclaim);
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
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.
830 xfs_ilock_map_shared(
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;
839 lock_mode = XFS_ILOCK_SHARED;
842 xfs_ilock(ip, 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.
852 xfs_iunlock_map_shared(
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:
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
880 xfs_ilock(xfs_inode_t *ip,
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
921 xfs_ilock_nowait(xfs_inode_t *ip,
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);
939 if (lock_flags & XFS_IOLOCK_EXCL) {
940 iolocked = mrtryupdate(&ip->i_iolock);
944 } else if (lock_flags & XFS_IOLOCK_SHARED) {
945 iolocked = mrtryaccess(&ip->i_iolock);
950 if (lock_flags & XFS_ILOCK_EXCL) {
951 ilocked = mrtryupdate(&ip->i_lock);
954 mrunlock(&ip->i_iolock);
958 } else if (lock_flags & XFS_ILOCK_SHARED) {
959 ilocked = mrtryaccess(&ip->i_lock);
962 mrunlock(&ip->i_iolock);
967 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
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.
984 xfs_iunlock(xfs_inode_t *ip,
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.
1033 xfs_ilock_demote(xfs_inode_t *ip,
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.
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)));
1067 xfs_ifunlock(xfs_inode_t *ip)
1069 ASSERT(valusema(&(ip->i_flock)) <= 0);
1070 vsema(&(ip->i_flock));