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 * Check the validity of the inode we just found it the cache
48 struct xfs_perag *pag,
51 int lock_flags) __releases(pag->pag_ici_lock)
53 struct xfs_mount *mp = ip->i_mount;
54 struct inode *old_inode;
58 * If INEW is set this inode is being set up
59 * Pause and try again.
61 if (xfs_iflags_test(ip, XFS_INEW)) {
63 XFS_STATS_INC(xs_ig_frecycle);
67 old_inode = ip->i_vnode;
68 if (old_inode == NULL) {
70 * If IRECLAIM is set this inode is
71 * on its way out of the system,
72 * we need to pause and try again.
74 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
76 XFS_STATS_INC(xs_ig_frecycle);
79 ASSERT(xfs_iflags_test(ip, XFS_IRECLAIMABLE));
82 * If lookup is racing with unlink, then we
83 * should return an error immediately so we
84 * don't remove it from the reclaim list and
85 * potentially leak the inode.
87 if ((ip->i_d.di_mode == 0) &&
88 !(flags & XFS_IGET_CREATE)) {
92 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
94 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
95 read_unlock(&pag->pag_ici_lock);
98 list_del_init(&ip->i_reclaim);
99 XFS_MOUNT_IUNLOCK(mp);
101 } else if (inode != old_inode) {
102 /* The inode is being torn down, pause and
105 if (old_inode->i_state & (I_FREEING | I_CLEAR)) {
107 XFS_STATS_INC(xs_ig_frecycle);
110 /* Chances are the other vnode (the one in the inode) is being torn
111 * down right now, and we landed on top of it. Question is, what do
112 * we do? Unhook the old inode and hook up the new one?
115 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
118 read_unlock(&pag->pag_ici_lock);
121 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
127 xfs_ilock(ip, lock_flags);
129 xfs_iflags_clear(ip, XFS_ISTALE);
130 xfs_itrace_exit_tag(ip, "xfs_iget.found");
131 XFS_STATS_INC(xs_ig_found);
135 read_unlock(&pag->pag_ici_lock);
143 struct xfs_mount *mp,
144 struct xfs_perag *pag,
147 struct xfs_inode **ipp,
150 int lock_flags) __releases(pag->pag_ici_lock)
152 struct xfs_inode *ip;
154 unsigned long first_index, mask;
155 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
158 * Read the disk inode attributes into a new inode structure and get
159 * a new vnode for it. This should also initialize i_ino and i_mount.
161 error = xfs_iread(mp, tp, ino, &ip, bno,
162 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
166 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
168 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
174 * Preload the radix tree so we can insert safely under the
177 if (radix_tree_preload(GFP_KERNEL)) {
183 xfs_ilock(ip, lock_flags);
185 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
186 first_index = agino & mask;
187 write_lock(&pag->pag_ici_lock);
189 /* insert the new inode */
190 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
191 if (unlikely(error)) {
192 WARN_ON(error != -EEXIST);
193 XFS_STATS_INC(xs_ig_dup);
198 /* These values _must_ be set before releasing the radix tree lock! */
199 ip->i_udquot = ip->i_gdquot = NULL;
200 xfs_iflags_set(ip, XFS_INEW);
202 write_unlock(&pag->pag_ici_lock);
203 radix_tree_preload_end();
208 write_unlock(&pag->pag_ici_lock);
209 radix_tree_preload_end();
216 * Look up an inode by number in the given file system.
217 * The inode is looked up in the cache held in each AG.
218 * If the inode is found in the cache, attach it to the provided
221 * If it is not in core, read it in from the file system's device,
222 * add it to the cache and attach the provided vnode.
224 * The inode is locked according to the value of the lock_flags parameter.
225 * This flag parameter indicates how and if the inode's IO lock and inode lock
228 * mp -- the mount point structure for the current file system. It points
229 * to the inode hash table.
230 * tp -- a pointer to the current transaction if there is one. This is
231 * simply passed through to the xfs_iread() call.
232 * ino -- the number of the inode desired. This is the unique identifier
233 * within the file system for the inode being requested.
234 * lock_flags -- flags indicating how to lock the inode. See the comment
235 * for xfs_ilock() for a list of valid values.
236 * bno -- the block number starting the buffer containing the inode,
237 * if known (as by bulkstat), else 0.
255 /* the radix tree exists only in inode capable AGs */
256 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
259 /* get the perag structure and ensure that it's inode capable */
260 pag = xfs_get_perag(mp, ino);
261 if (!pag->pagi_inodeok)
263 ASSERT(pag->pag_ici_init);
264 agino = XFS_INO_TO_AGINO(mp, ino);
268 read_lock(&pag->pag_ici_lock);
269 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
272 error = xfs_iget_cache_hit(inode, pag, ip, flags, lock_flags);
274 goto out_error_or_again;
276 read_unlock(&pag->pag_ici_lock);
277 XFS_STATS_INC(xs_ig_missed);
279 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
282 goto out_error_or_again;
284 xfs_put_perag(mp, pag);
286 ASSERT(ip->i_df.if_ext_max ==
287 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
289 xfs_iflags_set(ip, XFS_IMODIFIED);
293 * Set up the Linux with the Linux inode.
296 inode->i_private = ip;
299 * If we have a real type for an on-disk inode, we can set ops(&unlock)
300 * now. If it's a new inode being created, xfs_ialloc will handle it.
302 if (ip->i_d.di_mode != 0)
307 if (error == EAGAIN) {
311 xfs_put_perag(mp, pag);
317 * The 'normal' internal xfs_iget, if needed it will
318 * 'allocate', or 'get', the vnode.
334 XFS_STATS_INC(xs_ig_attempts);
337 inode = iget_locked(mp->m_super, ino);
339 /* If we got no inode we are out of memory */
342 if (inode->i_state & I_NEW) {
343 XFS_STATS_INC(vn_active);
344 XFS_STATS_INC(vn_alloc);
346 error = xfs_iget_core(inode, mp, tp, ino, flags,
347 lock_flags, ipp, bno);
349 make_bad_inode(inode);
350 if (inode->i_state & I_NEW)
351 unlock_new_inode(inode);
358 * If the inode is not fully constructed due to
359 * filehandle mismatches wait for the inode to go
360 * away and try again.
362 * iget_locked will call __wait_on_freeing_inode
363 * to wait for the inode to go away.
365 if (is_bad_inode(inode)) {
379 xfs_ilock(ip, lock_flags);
380 XFS_STATS_INC(xs_ig_found);
386 * Look for the inode corresponding to the given ino in the hash table.
387 * If it is there and its i_transp pointer matches tp, return it.
388 * Otherwise, return NULL.
391 xfs_inode_incore(xfs_mount_t *mp,
398 pag = xfs_get_perag(mp, ino);
399 read_lock(&pag->pag_ici_lock);
400 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
401 read_unlock(&pag->pag_ici_lock);
402 xfs_put_perag(mp, pag);
404 /* the returned inode must match the transaction */
405 if (ip && (ip->i_transp != tp))
411 * Decrement reference count of an inode structure and unlock it.
413 * ip -- the inode being released
414 * lock_flags -- this parameter indicates the inode's locks to be
415 * to be released. See the comment on xfs_iunlock() for a list
419 xfs_iput(xfs_inode_t *ip,
422 xfs_itrace_entry(ip);
423 xfs_iunlock(ip, lock_flags);
428 * Special iput for brand-new inodes that are still locked
435 struct inode *inode = VFS_I(ip);
437 xfs_itrace_entry(ip);
439 if ((ip->i_d.di_mode == 0)) {
440 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
441 make_bad_inode(inode);
443 if (inode->i_state & I_NEW)
444 unlock_new_inode(inode);
446 xfs_iunlock(ip, lock_flags);
452 * This routine embodies the part of the reclaim code that pulls
453 * the inode from the inode hash table and the mount structure's
455 * This should only be called from xfs_reclaim().
458 xfs_ireclaim(xfs_inode_t *ip)
461 * Remove from old hash list and mount list.
463 XFS_STATS_INC(xs_ig_reclaims);
468 * Here we do a spurious inode lock in order to coordinate with inode
469 * cache radix tree lookups. This is because the lookup can reference
470 * the inodes in the cache without taking references. We make that OK
471 * here by ensuring that we wait until the inode is unlocked after the
472 * lookup before we go ahead and free it. We get both the ilock and
473 * the iolock because the code may need to drop the ilock one but will
474 * still hold the iolock.
476 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
479 * Release dquots (and their references) if any. An inode may escape
480 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
482 XFS_QM_DQDETACH(ip->i_mount, ip);
485 * Pull our behavior descriptor from the vnode chain.
488 ip->i_vnode->i_private = NULL;
493 * Free all memory associated with the inode.
495 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
500 * This routine removes an about-to-be-destroyed inode from
501 * all of the lists in which it is located with the exception
502 * of the behavior chain.
508 xfs_mount_t *mp = ip->i_mount;
509 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
511 write_lock(&pag->pag_ici_lock);
512 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
513 write_unlock(&pag->pag_ici_lock);
514 xfs_put_perag(mp, pag);
516 /* Deal with the deleted inodes list */
518 list_del_init(&ip->i_reclaim);
520 XFS_MOUNT_IUNLOCK(mp);
524 * This is a wrapper routine around the xfs_ilock() routine
525 * used to centralize some grungy code. It is used in places
526 * that wish to lock the inode solely for reading the extents.
527 * The reason these places can't just call xfs_ilock(SHARED)
528 * is that the inode lock also guards to bringing in of the
529 * extents from disk for a file in b-tree format. If the inode
530 * is in b-tree format, then we need to lock the inode exclusively
531 * until the extents are read in. Locking it exclusively all
532 * the time would limit our parallelism unnecessarily, though.
533 * What we do instead is check to see if the extents have been
534 * read in yet, and only lock the inode exclusively if they
537 * The function returns a value which should be given to the
538 * corresponding xfs_iunlock_map_shared(). This value is
539 * the mode in which the lock was actually taken.
542 xfs_ilock_map_shared(
547 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
548 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
549 lock_mode = XFS_ILOCK_EXCL;
551 lock_mode = XFS_ILOCK_SHARED;
554 xfs_ilock(ip, lock_mode);
560 * This is simply the unlock routine to go with xfs_ilock_map_shared().
561 * All it does is call xfs_iunlock() with the given lock_mode.
564 xfs_iunlock_map_shared(
566 unsigned int lock_mode)
568 xfs_iunlock(ip, lock_mode);
572 * The xfs inode contains 2 locks: a multi-reader lock called the
573 * i_iolock and a multi-reader lock called the i_lock. This routine
574 * allows either or both of the locks to be obtained.
576 * The 2 locks should always be ordered so that the IO lock is
577 * obtained first in order to prevent deadlock.
579 * ip -- the inode being locked
580 * lock_flags -- this parameter indicates the inode's locks
581 * to be locked. It can be:
586 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
587 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
588 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
589 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
597 * You can't set both SHARED and EXCL for the same lock,
598 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
599 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
601 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
602 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
603 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
604 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
605 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
607 if (lock_flags & XFS_IOLOCK_EXCL)
608 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
609 else if (lock_flags & XFS_IOLOCK_SHARED)
610 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
612 if (lock_flags & XFS_ILOCK_EXCL)
613 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
614 else if (lock_flags & XFS_ILOCK_SHARED)
615 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
617 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
621 * This is just like xfs_ilock(), except that the caller
622 * is guaranteed not to sleep. It returns 1 if it gets
623 * the requested locks and 0 otherwise. If the IO lock is
624 * obtained but the inode lock cannot be, then the IO lock
625 * is dropped before returning.
627 * ip -- the inode being locked
628 * lock_flags -- this parameter indicates the inode's locks to be
629 * to be locked. See the comment for xfs_ilock() for a list
638 * You can't set both SHARED and EXCL for the same lock,
639 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
640 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
642 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
643 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
644 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
645 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
646 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
648 if (lock_flags & XFS_IOLOCK_EXCL) {
649 if (!mrtryupdate(&ip->i_iolock))
651 } else if (lock_flags & XFS_IOLOCK_SHARED) {
652 if (!mrtryaccess(&ip->i_iolock))
655 if (lock_flags & XFS_ILOCK_EXCL) {
656 if (!mrtryupdate(&ip->i_lock))
657 goto out_undo_iolock;
658 } else if (lock_flags & XFS_ILOCK_SHARED) {
659 if (!mrtryaccess(&ip->i_lock))
660 goto out_undo_iolock;
662 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
666 if (lock_flags & XFS_IOLOCK_EXCL)
667 mrunlock_excl(&ip->i_iolock);
668 else if (lock_flags & XFS_IOLOCK_SHARED)
669 mrunlock_shared(&ip->i_iolock);
675 * xfs_iunlock() is used to drop the inode locks acquired with
676 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
677 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
678 * that we know which locks to drop.
680 * ip -- the inode being unlocked
681 * lock_flags -- this parameter indicates the inode's locks to be
682 * to be unlocked. See the comment for xfs_ilock() for a list
683 * of valid values for this parameter.
692 * You can't set both SHARED and EXCL for the same lock,
693 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
694 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
696 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
697 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
698 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
699 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
700 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
701 XFS_LOCK_DEP_MASK)) == 0);
702 ASSERT(lock_flags != 0);
704 if (lock_flags & XFS_IOLOCK_EXCL)
705 mrunlock_excl(&ip->i_iolock);
706 else if (lock_flags & XFS_IOLOCK_SHARED)
707 mrunlock_shared(&ip->i_iolock);
709 if (lock_flags & XFS_ILOCK_EXCL)
710 mrunlock_excl(&ip->i_lock);
711 else if (lock_flags & XFS_ILOCK_SHARED)
712 mrunlock_shared(&ip->i_lock);
714 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
715 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
717 * Let the AIL know that this item has been unlocked in case
718 * it is in the AIL and anyone is waiting on it. Don't do
719 * this if the caller has asked us not to.
721 xfs_trans_unlocked_item(ip->i_mount,
722 (xfs_log_item_t*)(ip->i_itemp));
724 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
728 * give up write locks. the i/o lock cannot be held nested
729 * if it is being demoted.
736 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
737 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
739 if (lock_flags & XFS_ILOCK_EXCL)
740 mrdemote(&ip->i_lock);
741 if (lock_flags & XFS_IOLOCK_EXCL)
742 mrdemote(&ip->i_iolock);
747 * Debug-only routine, without additional rw_semaphore APIs, we can
748 * now only answer requests regarding whether we hold the lock for write
749 * (reader state is outside our visibility, we only track writer state).
751 * Note: this means !xfs_isilocked would give false positives, so don't do that.
758 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
760 if (!ip->i_lock.mr_writer)
764 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
766 if (!ip->i_iolock.mr_writer)