2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_quota.h"
40 #include "xfs_utils.h"
43 * Initialize the inode hash table for the newly mounted file system.
44 * Choose an initial table size based on user specified value, else
45 * use a simple algorithm using the maximum number of inodes as an
46 * indicator for table size, and clamp it between one and some large
50 xfs_ihash_init(xfs_mount_t *mp)
56 icount = mp->m_maxicount ? mp->m_maxicount :
57 (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog);
58 mp->m_ihsize = 1 << max_t(uint, 8,
59 (xfs_highbit64(icount) + 1) / 2);
60 mp->m_ihsize = min_t(uint, mp->m_ihsize,
61 (64 * NBPP) / sizeof(xfs_ihash_t));
64 mp->m_ihash = kmem_zalloc_greedy(&mp->m_ihsize,
65 NBPC * sizeof(xfs_ihash_t),
66 mp->m_ihsize * sizeof(xfs_ihash_t),
67 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
68 mp->m_ihsize /= sizeof(xfs_ihash_t);
69 for (i = 0; i < mp->m_ihsize; i++)
70 rwlock_init(&(mp->m_ihash[i].ih_lock));
74 * Free up structures allocated by xfs_ihash_init, at unmount time.
77 xfs_ihash_free(xfs_mount_t *mp)
79 kmem_free(mp->m_ihash, mp->m_ihsize * sizeof(xfs_ihash_t));
84 * Initialize the inode cluster hash table for the newly mounted file system.
85 * Its size is derived from the ihash table size.
88 xfs_chash_init(xfs_mount_t *mp)
92 mp->m_chsize = max_t(uint, 1, mp->m_ihsize /
93 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog));
94 mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize);
95 mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize
96 * sizeof(xfs_chash_t),
98 for (i = 0; i < mp->m_chsize; i++) {
99 spinlock_init(&mp->m_chash[i].ch_lock,"xfshash");
104 * Free up structures allocated by xfs_chash_init, at unmount time.
107 xfs_chash_free(xfs_mount_t *mp)
111 for (i = 0; i < mp->m_chsize; i++) {
112 spinlock_destroy(&mp->m_chash[i].ch_lock);
115 kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
120 * Try to move an inode to the front of its hash list if possible
121 * (and if its not there already). Called right after obtaining
122 * the list version number and then dropping the read_lock on the
123 * hash list in question (which is done right after looking up the
124 * inode in question...).
134 if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) {
135 if (likely(version == ih->ih_version)) {
136 /* remove from list */
137 if ((iq = ip->i_next)) {
138 iq->i_prevp = ip->i_prevp;
142 /* insert at list head */
144 iq->i_prevp = &ip->i_next;
146 ip->i_prevp = &ih->ih_next;
149 write_unlock(&ih->ih_lock);
154 * Look up an inode by number in the given file system.
155 * The inode is looked up in the hash table for the file system
156 * represented by the mount point parameter mp. Each bucket of
157 * the hash table is guarded by an individual semaphore.
159 * If the inode is found in the hash table, its corresponding vnode
160 * is obtained with a call to vn_get(). This call takes care of
161 * coordination with the reclamation of the inode and vnode. Note
162 * that the vmap structure is filled in while holding the hash lock.
163 * This gives us the state of the inode/vnode when we found it and
164 * is used for coordination in vn_get().
166 * If it is not in core, read it in from the file system's device and
167 * add the inode into the hash table.
169 * The inode is locked according to the value of the lock_flags parameter.
170 * This flag parameter indicates how and if the inode's IO lock and inode lock
173 * mp -- the mount point structure for the current file system. It points
174 * to the inode hash table.
175 * tp -- a pointer to the current transaction if there is one. This is
176 * simply passed through to the xfs_iread() call.
177 * ino -- the number of the inode desired. This is the unique identifier
178 * within the file system for the inode being requested.
179 * lock_flags -- flags indicating how to lock the inode. See the comment
180 * for xfs_ilock() for a list of valid values.
181 * bno -- the block number starting the buffer containing the inode,
182 * if known (as by bulkstat), else 0.
198 bhv_vnode_t *inode_vp;
203 xfs_chashlist_t *chl, *chlnew;
207 ih = XFS_IHASH(mp, ino);
210 read_lock(&ih->ih_lock);
212 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
213 if (ip->i_ino == ino) {
215 * If INEW is set this inode is being set up
216 * we need to pause and try again.
218 if (ip->i_flags & XFS_INEW) {
219 read_unlock(&ih->ih_lock);
221 XFS_STATS_INC(xs_ig_frecycle);
226 inode_vp = XFS_ITOV_NULL(ip);
227 if (inode_vp == NULL) {
229 * If IRECLAIM is set this inode is
230 * on its way out of the system,
231 * we need to pause and try again.
233 if (ip->i_flags & XFS_IRECLAIM) {
234 read_unlock(&ih->ih_lock);
236 XFS_STATS_INC(xs_ig_frecycle);
241 vn_trace_exit(vp, "xfs_iget.alloc",
242 (inst_t *)__return_address);
244 XFS_STATS_INC(xs_ig_found);
246 spin_lock(&ip->i_flags_lock);
247 ip->i_flags &= ~XFS_IRECLAIMABLE;
248 spin_unlock(&ip->i_flags_lock);
249 version = ih->ih_version;
250 read_unlock(&ih->ih_lock);
251 xfs_ihash_promote(ih, ip, version);
254 list_del_init(&ip->i_reclaim);
255 XFS_MOUNT_IUNLOCK(mp);
259 } else if (vp != inode_vp) {
260 struct inode *inode = vn_to_inode(inode_vp);
262 /* The inode is being torn down, pause and
265 if (inode->i_state & (I_FREEING | I_CLEAR)) {
266 read_unlock(&ih->ih_lock);
268 XFS_STATS_INC(xs_ig_frecycle);
272 /* Chances are the other vnode (the one in the inode) is being torn
273 * down right now, and we landed on top of it. Question is, what do
274 * we do? Unhook the old inode and hook up the new one?
277 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
282 * Inode cache hit: if ip is not at the front of
283 * its hash chain, move it there now.
284 * Do this with the lock held for update, but
285 * do statistics after releasing the lock.
287 version = ih->ih_version;
288 read_unlock(&ih->ih_lock);
289 xfs_ihash_promote(ih, ip, version);
290 XFS_STATS_INC(xs_ig_found);
293 if (ip->i_d.di_mode == 0) {
294 if (!(flags & XFS_IGET_CREATE))
296 xfs_iocore_inode_reinit(ip);
300 xfs_ilock(ip, lock_flags);
302 spin_lock(&ip->i_flags_lock);
303 ip->i_flags &= ~XFS_ISTALE;
304 spin_unlock(&ip->i_flags_lock);
306 vn_trace_exit(vp, "xfs_iget.found",
307 (inst_t *)__return_address);
313 * Inode cache miss: save the hash chain version stamp and unlock
314 * the chain, so we don't deadlock in vn_alloc.
316 XFS_STATS_INC(xs_ig_missed);
318 version = ih->ih_version;
320 read_unlock(&ih->ih_lock);
323 * Read the disk inode attributes into a new inode structure and get
324 * a new vnode for it. This should also initialize i_ino and i_mount.
326 error = xfs_iread(mp, tp, ino, &ip, bno,
327 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
331 vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
333 xfs_inode_lock_init(ip, vp);
334 xfs_iocore_inode_init(ip);
337 xfs_ilock(ip, lock_flags);
339 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
345 * Put ip on its hash chain, unless someone else hashed a duplicate
346 * after we released the hash lock.
348 write_lock(&ih->ih_lock);
350 if (ih->ih_version != version) {
351 for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
352 if (iq->i_ino == ino) {
353 write_unlock(&ih->ih_lock);
356 XFS_STATS_INC(xs_ig_dup);
363 * These values _must_ be set before releasing ihlock!
366 if ((iq = ih->ih_next)) {
367 iq->i_prevp = &ip->i_next;
370 ip->i_prevp = &ih->ih_next;
372 ip->i_udquot = ip->i_gdquot = NULL;
374 spin_lock(&ip->i_flags_lock);
375 ip->i_flags |= XFS_INEW;
376 spin_unlock(&ip->i_flags_lock);
378 write_unlock(&ih->ih_lock);
381 * put ip on its cluster's hash chain
383 ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
384 ip->i_cnext == NULL);
387 ch = XFS_CHASH(mp, ip->i_blkno);
389 s = mutex_spinlock(&ch->ch_lock);
390 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
391 if (chl->chl_blkno == ip->i_blkno) {
393 /* insert this inode into the doubly-linked list
394 * where chl points */
395 if ((iq = chl->chl_ip)) {
396 ip->i_cprev = iq->i_cprev;
397 iq->i_cprev->i_cnext = ip;
410 /* no hash list found for this block; add a new hash list */
412 if (chlnew == NULL) {
413 mutex_spinunlock(&ch->ch_lock, s);
414 ASSERT(xfs_chashlist_zone != NULL);
415 chlnew = (xfs_chashlist_t *)
416 kmem_zone_alloc(xfs_chashlist_zone,
418 ASSERT(chlnew != NULL);
423 ip->i_chash = chlnew;
425 chlnew->chl_blkno = ip->i_blkno;
427 ch->ch_list->chl_prev = chlnew;
428 chlnew->chl_next = ch->ch_list;
429 chlnew->chl_prev = NULL;
430 ch->ch_list = chlnew;
434 if (chlnew != NULL) {
435 kmem_zone_free(xfs_chashlist_zone, chlnew);
439 mutex_spinunlock(&ch->ch_lock, s);
443 * Link ip to its mount and thread it on the mount's inode list.
446 if ((iq = mp->m_inodes)) {
447 ASSERT(iq->i_mprev->i_mnext == iq);
448 ip->i_mprev = iq->i_mprev;
449 iq->i_mprev->i_mnext = ip;
458 XFS_MOUNT_IUNLOCK(mp);
461 ASSERT(ip->i_df.if_ext_max ==
462 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
464 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
465 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
470 * If we have a real type for an on-disk inode, we can set ops(&unlock)
471 * now. If it's a new inode being created, xfs_ialloc will handle it.
473 bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
480 * The 'normal' internal xfs_iget, if needed it will
481 * 'allocate', or 'get', the vnode.
494 bhv_vnode_t *vp = NULL;
497 XFS_STATS_INC(xs_ig_attempts);
500 if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
503 vp = vn_from_inode(inode);
504 if (inode->i_state & I_NEW) {
505 vn_initialize(inode);
506 error = xfs_iget_core(vp, mp, tp, ino, flags,
507 lock_flags, ipp, bno);
510 if (inode->i_state & I_NEW)
511 unlock_new_inode(inode);
516 * If the inode is not fully constructed due to
517 * filehandle mismatches wait for the inode to go
518 * away and try again.
520 * iget_locked will call __wait_on_freeing_inode
521 * to wait for the inode to go away.
523 if (is_bad_inode(inode) ||
524 ((ip = xfs_vtoi(vp)) == NULL)) {
531 xfs_ilock(ip, lock_flags);
532 XFS_STATS_INC(xs_ig_found);
537 error = ENOMEM; /* If we got no inode we are out of memory */
543 * Do the setup for the various locks within the incore inode.
550 mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
551 "xfsino", (long)vp->v_number);
552 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
553 init_waitqueue_head(&ip->i_ipin_wait);
554 atomic_set(&ip->i_pincount, 0);
555 initnsema(&ip->i_flock, 1, "xfsfino");
559 * Look for the inode corresponding to the given ino in the hash table.
560 * If it is there and its i_transp pointer matches tp, return it.
561 * Otherwise, return NULL.
564 xfs_inode_incore(xfs_mount_t *mp,
572 ih = XFS_IHASH(mp, ino);
573 read_lock(&ih->ih_lock);
574 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
575 if (ip->i_ino == ino) {
577 * If we find it and tp matches, return it.
578 * Also move it to the front of the hash list
579 * if we find it and it is not already there.
580 * Otherwise break from the loop and return
583 if (ip->i_transp == tp) {
584 version = ih->ih_version;
585 read_unlock(&ih->ih_lock);
586 xfs_ihash_promote(ih, ip, version);
592 read_unlock(&ih->ih_lock);
597 * Decrement reference count of an inode structure and unlock it.
599 * ip -- the inode being released
600 * lock_flags -- this parameter indicates the inode's locks to be
601 * to be released. See the comment on xfs_iunlock() for a list
605 xfs_iput(xfs_inode_t *ip,
608 bhv_vnode_t *vp = XFS_ITOV(ip);
610 vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
611 xfs_iunlock(ip, lock_flags);
616 * Special iput for brand-new inodes that are still locked
619 xfs_iput_new(xfs_inode_t *ip,
622 bhv_vnode_t *vp = XFS_ITOV(ip);
623 struct inode *inode = vn_to_inode(vp);
625 vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
627 if ((ip->i_d.di_mode == 0)) {
628 ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
631 if (inode->i_state & I_NEW)
632 unlock_new_inode(inode);
634 xfs_iunlock(ip, lock_flags);
640 * This routine embodies the part of the reclaim code that pulls
641 * the inode from the inode hash table and the mount structure's
643 * This should only be called from xfs_reclaim().
646 xfs_ireclaim(xfs_inode_t *ip)
651 * Remove from old hash list and mount list.
653 XFS_STATS_INC(xs_ig_reclaims);
658 * Here we do a spurious inode lock in order to coordinate with
659 * xfs_sync(). This is because xfs_sync() references the inodes
660 * in the mount list without taking references on the corresponding
661 * vnodes. We make that OK here by ensuring that we wait until
662 * the inode is unlocked in xfs_sync() before we go ahead and
663 * free it. We get both the regular lock and the io lock because
664 * the xfs_sync() code may need to drop the regular one but will
665 * still hold the io lock.
667 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
670 * Release dquots (and their references) if any. An inode may escape
671 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
673 XFS_QM_DQDETACH(ip->i_mount, ip);
676 * Pull our behavior descriptor from the vnode chain.
678 vp = XFS_ITOV_NULL(ip);
680 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
684 * Free all memory associated with the inode.
690 * This routine removes an about-to-be-destroyed inode from
691 * all of the lists in which it is located with the exception
692 * of the behavior chain.
702 xfs_chashlist_t *chl, *chm;
706 write_lock(&ih->ih_lock);
707 if ((iq = ip->i_next)) {
708 iq->i_prevp = ip->i_prevp;
712 write_unlock(&ih->ih_lock);
715 * Remove from cluster hash list
716 * 1) delete the chashlist if this is the last inode on the chashlist
717 * 2) unchain from list of inodes
718 * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
721 ch = XFS_CHASH(mp, ip->i_blkno);
722 s = mutex_spinlock(&ch->ch_lock);
724 if (ip->i_cnext == ip) {
725 /* Last inode on chashlist */
726 ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
727 ASSERT(ip->i_chash != NULL);
731 chl->chl_prev->chl_next = chl->chl_next;
733 ch->ch_list = chl->chl_next;
735 chl->chl_next->chl_prev = chl->chl_prev;
736 kmem_zone_free(xfs_chashlist_zone, chl);
738 /* delete one inode from a non-empty list */
740 iq->i_cprev = ip->i_cprev;
741 ip->i_cprev->i_cnext = iq;
742 if (ip->i_chash->chl_ip == ip) {
743 ip->i_chash->chl_ip = iq;
745 ip->i_chash = __return_address;
746 ip->i_cprev = __return_address;
747 ip->i_cnext = __return_address;
749 mutex_spinunlock(&ch->ch_lock, s);
752 * Remove from mount's inode list.
755 ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
757 iq->i_mprev = ip->i_mprev;
758 ip->i_mprev->i_mnext = iq;
761 * Fix up the head pointer if it points to the inode being deleted.
763 if (mp->m_inodes == ip) {
771 /* Deal with the deleted inodes list */
772 list_del_init(&ip->i_reclaim);
775 XFS_MOUNT_IUNLOCK(mp);
779 * This is a wrapper routine around the xfs_ilock() routine
780 * used to centralize some grungy code. It is used in places
781 * that wish to lock the inode solely for reading the extents.
782 * The reason these places can't just call xfs_ilock(SHARED)
783 * is that the inode lock also guards to bringing in of the
784 * extents from disk for a file in b-tree format. If the inode
785 * is in b-tree format, then we need to lock the inode exclusively
786 * until the extents are read in. Locking it exclusively all
787 * the time would limit our parallelism unnecessarily, though.
788 * What we do instead is check to see if the extents have been
789 * read in yet, and only lock the inode exclusively if they
792 * The function returns a value which should be given to the
793 * corresponding xfs_iunlock_map_shared(). This value is
794 * the mode in which the lock was actually taken.
797 xfs_ilock_map_shared(
802 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
803 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
804 lock_mode = XFS_ILOCK_EXCL;
806 lock_mode = XFS_ILOCK_SHARED;
809 xfs_ilock(ip, lock_mode);
815 * This is simply the unlock routine to go with xfs_ilock_map_shared().
816 * All it does is call xfs_iunlock() with the given lock_mode.
819 xfs_iunlock_map_shared(
821 unsigned int lock_mode)
823 xfs_iunlock(ip, lock_mode);
827 * The xfs inode contains 2 locks: a multi-reader lock called the
828 * i_iolock and a multi-reader lock called the i_lock. This routine
829 * allows either or both of the locks to be obtained.
831 * The 2 locks should always be ordered so that the IO lock is
832 * obtained first in order to prevent deadlock.
834 * ip -- the inode being locked
835 * lock_flags -- this parameter indicates the inode's locks
836 * to be locked. It can be:
841 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
842 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
843 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
844 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
847 xfs_ilock(xfs_inode_t *ip,
851 * You can't set both SHARED and EXCL for the same lock,
852 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
853 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
855 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
856 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
857 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
858 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
859 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
861 if (lock_flags & XFS_IOLOCK_EXCL) {
862 mrupdate(&ip->i_iolock);
863 } else if (lock_flags & XFS_IOLOCK_SHARED) {
864 mraccess(&ip->i_iolock);
866 if (lock_flags & XFS_ILOCK_EXCL) {
867 mrupdate(&ip->i_lock);
868 } else if (lock_flags & XFS_ILOCK_SHARED) {
869 mraccess(&ip->i_lock);
871 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
875 * This is just like xfs_ilock(), except that the caller
876 * is guaranteed not to sleep. It returns 1 if it gets
877 * the requested locks and 0 otherwise. If the IO lock is
878 * obtained but the inode lock cannot be, then the IO lock
879 * is dropped before returning.
881 * ip -- the inode being locked
882 * lock_flags -- this parameter indicates the inode's locks to be
883 * to be locked. See the comment for xfs_ilock() for a list
888 xfs_ilock_nowait(xfs_inode_t *ip,
895 * You can't set both SHARED and EXCL for the same lock,
896 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
897 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
899 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
900 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
901 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
902 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
903 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
906 if (lock_flags & XFS_IOLOCK_EXCL) {
907 iolocked = mrtryupdate(&ip->i_iolock);
911 } else if (lock_flags & XFS_IOLOCK_SHARED) {
912 iolocked = mrtryaccess(&ip->i_iolock);
917 if (lock_flags & XFS_ILOCK_EXCL) {
918 ilocked = mrtryupdate(&ip->i_lock);
921 mrunlock(&ip->i_iolock);
925 } else if (lock_flags & XFS_ILOCK_SHARED) {
926 ilocked = mrtryaccess(&ip->i_lock);
929 mrunlock(&ip->i_iolock);
934 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
939 * xfs_iunlock() is used to drop the inode locks acquired with
940 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
941 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
942 * that we know which locks to drop.
944 * ip -- the inode being unlocked
945 * lock_flags -- this parameter indicates the inode's locks to be
946 * to be unlocked. See the comment for xfs_ilock() for a list
947 * of valid values for this parameter.
951 xfs_iunlock(xfs_inode_t *ip,
955 * You can't set both SHARED and EXCL for the same lock,
956 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
957 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
959 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
960 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
961 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
962 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
963 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
964 ASSERT(lock_flags != 0);
966 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
967 ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
968 (ismrlocked(&ip->i_iolock, MR_ACCESS)));
969 ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
970 (ismrlocked(&ip->i_iolock, MR_UPDATE)));
971 mrunlock(&ip->i_iolock);
974 if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
975 ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
976 (ismrlocked(&ip->i_lock, MR_ACCESS)));
977 ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
978 (ismrlocked(&ip->i_lock, MR_UPDATE)));
979 mrunlock(&ip->i_lock);
982 * Let the AIL know that this item has been unlocked in case
983 * it is in the AIL and anyone is waiting on it. Don't do
984 * this if the caller has asked us not to.
986 if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
987 ip->i_itemp != NULL) {
988 xfs_trans_unlocked_item(ip->i_mount,
989 (xfs_log_item_t*)(ip->i_itemp));
992 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
996 * give up write locks. the i/o lock cannot be held nested
997 * if it is being demoted.
1000 xfs_ilock_demote(xfs_inode_t *ip,
1003 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
1004 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
1006 if (lock_flags & XFS_ILOCK_EXCL) {
1007 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
1008 mrdemote(&ip->i_lock);
1010 if (lock_flags & XFS_IOLOCK_EXCL) {
1011 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1012 mrdemote(&ip->i_iolock);
1017 * The following three routines simply manage the i_flock
1018 * semaphore embedded in the inode. This semaphore synchronizes
1019 * processes attempting to flush the in-core inode back to disk.
1022 xfs_iflock(xfs_inode_t *ip)
1024 psema(&(ip->i_flock), PINOD|PLTWAIT);
1028 xfs_iflock_nowait(xfs_inode_t *ip)
1030 return (cpsema(&(ip->i_flock)));
1034 xfs_ifunlock(xfs_inode_t *ip)
1036 ASSERT(issemalocked(&(ip->i_flock)));
1037 vsema(&(ip->i_flock));