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"
41 #include "xfs_trans_priv.h"
42 #include "xfs_inode_item.h"
45 * Check the validity of the inode we just found it the cache
49 struct xfs_perag *pag,
52 int lock_flags) __releases(pag->pag_ici_lock)
54 struct xfs_mount *mp = ip->i_mount;
58 * If INEW is set this inode is being set up
59 * If IRECLAIM is set this inode is being torn down
60 * Pause and try again.
62 if (xfs_iflags_test(ip, (XFS_INEW|XFS_IRECLAIM))) {
63 XFS_STATS_INC(xs_ig_frecycle);
67 /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
68 if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
71 * If lookup is racing with unlink, then we should return an
72 * error immediately so we don't remove it from the reclaim
73 * list and potentially leak the inode.
75 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
80 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
83 * We need to re-initialise the VFS inode as it has been
84 * 'freed' by the VFS. Do this here so we can deal with
85 * errors cleanly, then tag it so it can be set up correctly
88 if (!inode_init_always(mp->m_super, VFS_I(ip))) {
94 * We must set the XFS_INEW flag before clearing the
95 * XFS_IRECLAIMABLE flag so that if a racing lookup does
96 * not find the XFS_IRECLAIMABLE above but has the igrab()
97 * below succeed we can safely check XFS_INEW to detect
98 * that this inode is still being initialised.
100 xfs_iflags_set(ip, XFS_INEW);
101 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
103 /* clear the radix tree reclaim flag as well. */
104 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
105 } else if (!igrab(VFS_I(ip))) {
106 /* If the VFS inode is being torn down, pause and try again. */
107 XFS_STATS_INC(xs_ig_frecycle);
109 } else if (xfs_iflags_test(ip, XFS_INEW)) {
111 * We are racing with another cache hit that is
112 * currently recycling this inode out of the XFS_IRECLAIMABLE
113 * state. Wait for the initialisation to complete before
116 wait_on_inode(VFS_I(ip));
119 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
125 /* We've got a live one. */
126 read_unlock(&pag->pag_ici_lock);
129 xfs_ilock(ip, lock_flags);
131 xfs_iflags_clear(ip, XFS_ISTALE);
132 xfs_itrace_exit_tag(ip, "xfs_iget.found");
133 XFS_STATS_INC(xs_ig_found);
137 read_unlock(&pag->pag_ici_lock);
144 struct xfs_mount *mp,
145 struct xfs_perag *pag,
148 struct xfs_inode **ipp,
151 int lock_flags) __releases(pag->pag_ici_lock)
153 struct xfs_inode *ip;
155 unsigned long first_index, mask;
156 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
159 * Read the disk inode attributes into a new inode structure and get
160 * a new vnode for it. This should also initialize i_ino and i_mount.
162 error = xfs_iread(mp, tp, ino, &ip, bno,
163 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
167 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
169 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
175 xfs_ilock(ip, lock_flags);
178 * Preload the radix tree so we can insert safely under the
179 * write spinlock. Note that we cannot sleep inside the preload
182 if (radix_tree_preload(GFP_KERNEL)) {
187 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
188 first_index = agino & mask;
189 write_lock(&pag->pag_ici_lock);
191 /* insert the new inode */
192 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
193 if (unlikely(error)) {
194 WARN_ON(error != -EEXIST);
195 XFS_STATS_INC(xs_ig_dup);
197 goto out_preload_end;
200 /* These values _must_ be set before releasing the radix tree lock! */
201 ip->i_udquot = ip->i_gdquot = NULL;
202 xfs_iflags_set(ip, XFS_INEW);
204 write_unlock(&pag->pag_ici_lock);
205 radix_tree_preload_end();
210 write_unlock(&pag->pag_ici_lock);
211 radix_tree_preload_end();
214 xfs_iunlock(ip, lock_flags);
216 xfs_destroy_inode(ip);
221 * Look up an inode by number in the given file system.
222 * The inode is looked up in the cache held in each AG.
223 * If the inode is found in the cache, initialise the vfs inode
226 * If it is not in core, read it in from the file system's device,
227 * add it to the cache and initialise the vfs inode.
229 * The inode is locked according to the value of the lock_flags parameter.
230 * This flag parameter indicates how and if the inode's IO lock and inode lock
233 * mp -- the mount point structure for the current file system. It points
234 * to the inode hash table.
235 * tp -- a pointer to the current transaction if there is one. This is
236 * simply passed through to the xfs_iread() call.
237 * ino -- the number of the inode desired. This is the unique identifier
238 * within the file system for the inode being requested.
239 * lock_flags -- flags indicating how to lock the inode. See the comment
240 * for xfs_ilock() for a list of valid values.
241 * bno -- the block number starting the buffer containing the inode,
242 * if known (as by bulkstat), else 0.
259 /* the radix tree exists only in inode capable AGs */
260 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
263 /* get the perag structure and ensure that it's inode capable */
264 pag = xfs_get_perag(mp, ino);
265 if (!pag->pagi_inodeok)
267 ASSERT(pag->pag_ici_init);
268 agino = XFS_INO_TO_AGINO(mp, ino);
272 read_lock(&pag->pag_ici_lock);
273 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
276 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
278 goto out_error_or_again;
280 read_unlock(&pag->pag_ici_lock);
281 XFS_STATS_INC(xs_ig_missed);
283 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
286 goto out_error_or_again;
288 xfs_put_perag(mp, pag);
290 xfs_iflags_set(ip, XFS_IMODIFIED);
293 ASSERT(ip->i_df.if_ext_max ==
294 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
296 * If we have a real type for an on-disk inode, we can set ops(&unlock)
297 * now. If it's a new inode being created, xfs_ialloc will handle it.
299 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
304 if (error == EAGAIN) {
308 xfs_put_perag(mp, pag);
314 * Look for the inode corresponding to the given ino in the hash table.
315 * If it is there and its i_transp pointer matches tp, return it.
316 * Otherwise, return NULL.
319 xfs_inode_incore(xfs_mount_t *mp,
326 pag = xfs_get_perag(mp, ino);
327 read_lock(&pag->pag_ici_lock);
328 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
329 read_unlock(&pag->pag_ici_lock);
330 xfs_put_perag(mp, pag);
332 /* the returned inode must match the transaction */
333 if (ip && (ip->i_transp != tp))
339 * Decrement reference count of an inode structure and unlock it.
341 * ip -- the inode being released
342 * lock_flags -- this parameter indicates the inode's locks to be
343 * to be released. See the comment on xfs_iunlock() for a list
347 xfs_iput(xfs_inode_t *ip,
350 xfs_itrace_entry(ip);
351 xfs_iunlock(ip, lock_flags);
356 * Special iput for brand-new inodes that are still locked
363 struct inode *inode = VFS_I(ip);
365 xfs_itrace_entry(ip);
367 if ((ip->i_d.di_mode == 0)) {
368 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
369 make_bad_inode(inode);
371 if (inode->i_state & I_NEW)
372 unlock_new_inode(inode);
374 xfs_iunlock(ip, lock_flags);
380 * This routine embodies the part of the reclaim code that pulls
381 * the inode from the inode hash table and the mount structure's
383 * This should only be called from xfs_reclaim().
386 xfs_ireclaim(xfs_inode_t *ip)
389 * Remove from old hash list and mount list.
391 XFS_STATS_INC(xs_ig_reclaims);
396 * Here we do a spurious inode lock in order to coordinate with inode
397 * cache radix tree lookups. This is because the lookup can reference
398 * the inodes in the cache without taking references. We make that OK
399 * here by ensuring that we wait until the inode is unlocked after the
400 * lookup before we go ahead and free it. We get both the ilock and
401 * the iolock because the code may need to drop the ilock one but will
402 * still hold the iolock.
404 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
407 * Release dquots (and their references) if any. An inode may escape
408 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
410 XFS_QM_DQDETACH(ip->i_mount, ip);
413 * Free all memory associated with the inode.
415 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
420 * This routine removes an about-to-be-destroyed inode from
421 * all of the lists in which it is located with the exception
422 * of the behavior chain.
428 xfs_mount_t *mp = ip->i_mount;
429 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
431 write_lock(&pag->pag_ici_lock);
432 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
433 write_unlock(&pag->pag_ici_lock);
434 xfs_put_perag(mp, pag);
440 * This is a wrapper routine around the xfs_ilock() routine
441 * used to centralize some grungy code. It is used in places
442 * that wish to lock the inode solely for reading the extents.
443 * The reason these places can't just call xfs_ilock(SHARED)
444 * is that the inode lock also guards to bringing in of the
445 * extents from disk for a file in b-tree format. If the inode
446 * is in b-tree format, then we need to lock the inode exclusively
447 * until the extents are read in. Locking it exclusively all
448 * the time would limit our parallelism unnecessarily, though.
449 * What we do instead is check to see if the extents have been
450 * read in yet, and only lock the inode exclusively if they
453 * The function returns a value which should be given to the
454 * corresponding xfs_iunlock_map_shared(). This value is
455 * the mode in which the lock was actually taken.
458 xfs_ilock_map_shared(
463 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
464 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
465 lock_mode = XFS_ILOCK_EXCL;
467 lock_mode = XFS_ILOCK_SHARED;
470 xfs_ilock(ip, lock_mode);
476 * This is simply the unlock routine to go with xfs_ilock_map_shared().
477 * All it does is call xfs_iunlock() with the given lock_mode.
480 xfs_iunlock_map_shared(
482 unsigned int lock_mode)
484 xfs_iunlock(ip, lock_mode);
488 * The xfs inode contains 2 locks: a multi-reader lock called the
489 * i_iolock and a multi-reader lock called the i_lock. This routine
490 * allows either or both of the locks to be obtained.
492 * The 2 locks should always be ordered so that the IO lock is
493 * obtained first in order to prevent deadlock.
495 * ip -- the inode being locked
496 * lock_flags -- this parameter indicates the inode's locks
497 * to be locked. It can be:
502 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
503 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
504 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
505 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
513 * You can't set both SHARED and EXCL for the same lock,
514 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
515 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
517 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
518 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
519 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
520 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
521 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
523 if (lock_flags & XFS_IOLOCK_EXCL)
524 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
525 else if (lock_flags & XFS_IOLOCK_SHARED)
526 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
528 if (lock_flags & XFS_ILOCK_EXCL)
529 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
530 else if (lock_flags & XFS_ILOCK_SHARED)
531 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
533 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
537 * This is just like xfs_ilock(), except that the caller
538 * is guaranteed not to sleep. It returns 1 if it gets
539 * the requested locks and 0 otherwise. If the IO lock is
540 * obtained but the inode lock cannot be, then the IO lock
541 * is dropped before returning.
543 * ip -- the inode being locked
544 * lock_flags -- this parameter indicates the inode's locks to be
545 * to be locked. See the comment for xfs_ilock() for a list
554 * You can't set both SHARED and EXCL for the same lock,
555 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
556 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
558 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
559 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
560 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
561 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
562 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
564 if (lock_flags & XFS_IOLOCK_EXCL) {
565 if (!mrtryupdate(&ip->i_iolock))
567 } else if (lock_flags & XFS_IOLOCK_SHARED) {
568 if (!mrtryaccess(&ip->i_iolock))
571 if (lock_flags & XFS_ILOCK_EXCL) {
572 if (!mrtryupdate(&ip->i_lock))
573 goto out_undo_iolock;
574 } else if (lock_flags & XFS_ILOCK_SHARED) {
575 if (!mrtryaccess(&ip->i_lock))
576 goto out_undo_iolock;
578 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
582 if (lock_flags & XFS_IOLOCK_EXCL)
583 mrunlock_excl(&ip->i_iolock);
584 else if (lock_flags & XFS_IOLOCK_SHARED)
585 mrunlock_shared(&ip->i_iolock);
591 * xfs_iunlock() is used to drop the inode locks acquired with
592 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
593 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
594 * that we know which locks to drop.
596 * ip -- the inode being unlocked
597 * lock_flags -- this parameter indicates the inode's locks to be
598 * to be unlocked. See the comment for xfs_ilock() for a list
599 * of valid values for this parameter.
608 * You can't set both SHARED and EXCL for the same lock,
609 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
610 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
612 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
613 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
614 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
615 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
616 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
617 XFS_LOCK_DEP_MASK)) == 0);
618 ASSERT(lock_flags != 0);
620 if (lock_flags & XFS_IOLOCK_EXCL)
621 mrunlock_excl(&ip->i_iolock);
622 else if (lock_flags & XFS_IOLOCK_SHARED)
623 mrunlock_shared(&ip->i_iolock);
625 if (lock_flags & XFS_ILOCK_EXCL)
626 mrunlock_excl(&ip->i_lock);
627 else if (lock_flags & XFS_ILOCK_SHARED)
628 mrunlock_shared(&ip->i_lock);
630 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
631 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
633 * Let the AIL know that this item has been unlocked in case
634 * it is in the AIL and anyone is waiting on it. Don't do
635 * this if the caller has asked us not to.
637 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
638 (xfs_log_item_t*)(ip->i_itemp));
640 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
644 * give up write locks. the i/o lock cannot be held nested
645 * if it is being demoted.
652 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
653 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
655 if (lock_flags & XFS_ILOCK_EXCL)
656 mrdemote(&ip->i_lock);
657 if (lock_flags & XFS_IOLOCK_EXCL)
658 mrdemote(&ip->i_iolock);
663 * Debug-only routine, without additional rw_semaphore APIs, we can
664 * now only answer requests regarding whether we hold the lock for write
665 * (reader state is outside our visibility, we only track writer state).
667 * Note: this means !xfs_isilocked would give false positives, so don't do that.
674 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
676 if (!ip->i_lock.mr_writer)
680 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
682 if (!ip->i_iolock.mr_writer)