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, flags);
166 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
168 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
174 xfs_ilock(ip, lock_flags);
177 * Preload the radix tree so we can insert safely under the
178 * write spinlock. Note that we cannot sleep inside the preload
181 if (radix_tree_preload(GFP_KERNEL)) {
186 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
187 first_index = agino & mask;
188 write_lock(&pag->pag_ici_lock);
190 /* insert the new inode */
191 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
192 if (unlikely(error)) {
193 WARN_ON(error != -EEXIST);
194 XFS_STATS_INC(xs_ig_dup);
196 goto out_preload_end;
199 /* These values _must_ be set before releasing the radix tree lock! */
200 ip->i_udquot = ip->i_gdquot = NULL;
201 xfs_iflags_set(ip, XFS_INEW);
203 write_unlock(&pag->pag_ici_lock);
204 radix_tree_preload_end();
209 write_unlock(&pag->pag_ici_lock);
210 radix_tree_preload_end();
213 xfs_iunlock(ip, lock_flags);
215 xfs_destroy_inode(ip);
220 * Look up an inode by number in the given file system.
221 * The inode is looked up in the cache held in each AG.
222 * If the inode is found in the cache, initialise the vfs inode
225 * If it is not in core, read it in from the file system's device,
226 * add it to the cache and initialise the vfs inode.
228 * The inode is locked according to the value of the lock_flags parameter.
229 * This flag parameter indicates how and if the inode's IO lock and inode lock
232 * mp -- the mount point structure for the current file system. It points
233 * to the inode hash table.
234 * tp -- a pointer to the current transaction if there is one. This is
235 * simply passed through to the xfs_iread() call.
236 * ino -- the number of the inode desired. This is the unique identifier
237 * within the file system for the inode being requested.
238 * lock_flags -- flags indicating how to lock the inode. See the comment
239 * for xfs_ilock() for a list of valid values.
240 * bno -- the block number starting the buffer containing the inode,
241 * if known (as by bulkstat), else 0.
258 /* the radix tree exists only in inode capable AGs */
259 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
262 /* get the perag structure and ensure that it's inode capable */
263 pag = xfs_get_perag(mp, ino);
264 if (!pag->pagi_inodeok)
266 ASSERT(pag->pag_ici_init);
267 agino = XFS_INO_TO_AGINO(mp, ino);
271 read_lock(&pag->pag_ici_lock);
272 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
275 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
277 goto out_error_or_again;
279 read_unlock(&pag->pag_ici_lock);
280 XFS_STATS_INC(xs_ig_missed);
282 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
285 goto out_error_or_again;
287 xfs_put_perag(mp, pag);
289 xfs_iflags_set(ip, XFS_IMODIFIED);
292 ASSERT(ip->i_df.if_ext_max ==
293 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
295 * If we have a real type for an on-disk inode, we can set ops(&unlock)
296 * now. If it's a new inode being created, xfs_ialloc will handle it.
298 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
303 if (error == EAGAIN) {
307 xfs_put_perag(mp, pag);
313 * Look for the inode corresponding to the given ino in the hash table.
314 * If it is there and its i_transp pointer matches tp, return it.
315 * Otherwise, return NULL.
318 xfs_inode_incore(xfs_mount_t *mp,
325 pag = xfs_get_perag(mp, ino);
326 read_lock(&pag->pag_ici_lock);
327 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
328 read_unlock(&pag->pag_ici_lock);
329 xfs_put_perag(mp, pag);
331 /* the returned inode must match the transaction */
332 if (ip && (ip->i_transp != tp))
338 * Decrement reference count of an inode structure and unlock it.
340 * ip -- the inode being released
341 * lock_flags -- this parameter indicates the inode's locks to be
342 * to be released. See the comment on xfs_iunlock() for a list
346 xfs_iput(xfs_inode_t *ip,
349 xfs_itrace_entry(ip);
350 xfs_iunlock(ip, lock_flags);
355 * Special iput for brand-new inodes that are still locked
362 struct inode *inode = VFS_I(ip);
364 xfs_itrace_entry(ip);
366 if ((ip->i_d.di_mode == 0)) {
367 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
368 make_bad_inode(inode);
370 if (inode->i_state & I_NEW)
371 unlock_new_inode(inode);
373 xfs_iunlock(ip, lock_flags);
379 * This routine embodies the part of the reclaim code that pulls
380 * the inode from the inode hash table and the mount structure's
382 * This should only be called from xfs_reclaim().
385 xfs_ireclaim(xfs_inode_t *ip)
388 * Remove from old hash list and mount list.
390 XFS_STATS_INC(xs_ig_reclaims);
395 * Here we do a spurious inode lock in order to coordinate with inode
396 * cache radix tree lookups. This is because the lookup can reference
397 * the inodes in the cache without taking references. We make that OK
398 * here by ensuring that we wait until the inode is unlocked after the
399 * lookup before we go ahead and free it. We get both the ilock and
400 * the iolock because the code may need to drop the ilock one but will
401 * still hold the iolock.
403 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
406 * Release dquots (and their references) if any. An inode may escape
407 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
409 XFS_QM_DQDETACH(ip->i_mount, ip);
412 * Free all memory associated with the inode.
414 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
419 * This routine removes an about-to-be-destroyed inode from
420 * all of the lists in which it is located with the exception
421 * of the behavior chain.
427 xfs_mount_t *mp = ip->i_mount;
428 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
430 write_lock(&pag->pag_ici_lock);
431 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
432 write_unlock(&pag->pag_ici_lock);
433 xfs_put_perag(mp, pag);
439 * This is a wrapper routine around the xfs_ilock() routine
440 * used to centralize some grungy code. It is used in places
441 * that wish to lock the inode solely for reading the extents.
442 * The reason these places can't just call xfs_ilock(SHARED)
443 * is that the inode lock also guards to bringing in of the
444 * extents from disk for a file in b-tree format. If the inode
445 * is in b-tree format, then we need to lock the inode exclusively
446 * until the extents are read in. Locking it exclusively all
447 * the time would limit our parallelism unnecessarily, though.
448 * What we do instead is check to see if the extents have been
449 * read in yet, and only lock the inode exclusively if they
452 * The function returns a value which should be given to the
453 * corresponding xfs_iunlock_map_shared(). This value is
454 * the mode in which the lock was actually taken.
457 xfs_ilock_map_shared(
462 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
463 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
464 lock_mode = XFS_ILOCK_EXCL;
466 lock_mode = XFS_ILOCK_SHARED;
469 xfs_ilock(ip, lock_mode);
475 * This is simply the unlock routine to go with xfs_ilock_map_shared().
476 * All it does is call xfs_iunlock() with the given lock_mode.
479 xfs_iunlock_map_shared(
481 unsigned int lock_mode)
483 xfs_iunlock(ip, lock_mode);
487 * The xfs inode contains 2 locks: a multi-reader lock called the
488 * i_iolock and a multi-reader lock called the i_lock. This routine
489 * allows either or both of the locks to be obtained.
491 * The 2 locks should always be ordered so that the IO lock is
492 * obtained first in order to prevent deadlock.
494 * ip -- the inode being locked
495 * lock_flags -- this parameter indicates the inode's locks
496 * to be locked. It can be:
501 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
502 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
503 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
504 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
512 * You can't set both SHARED and EXCL for the same lock,
513 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
514 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
516 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
517 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
518 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
519 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
520 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
522 if (lock_flags & XFS_IOLOCK_EXCL)
523 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
524 else if (lock_flags & XFS_IOLOCK_SHARED)
525 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
527 if (lock_flags & XFS_ILOCK_EXCL)
528 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
529 else if (lock_flags & XFS_ILOCK_SHARED)
530 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
532 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
536 * This is just like xfs_ilock(), except that the caller
537 * is guaranteed not to sleep. It returns 1 if it gets
538 * the requested locks and 0 otherwise. If the IO lock is
539 * obtained but the inode lock cannot be, then the IO lock
540 * is dropped before returning.
542 * ip -- the inode being locked
543 * lock_flags -- this parameter indicates the inode's locks to be
544 * to be locked. See the comment for xfs_ilock() for a list
553 * You can't set both SHARED and EXCL for the same lock,
554 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
555 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
557 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
558 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
559 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
560 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
561 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
563 if (lock_flags & XFS_IOLOCK_EXCL) {
564 if (!mrtryupdate(&ip->i_iolock))
566 } else if (lock_flags & XFS_IOLOCK_SHARED) {
567 if (!mrtryaccess(&ip->i_iolock))
570 if (lock_flags & XFS_ILOCK_EXCL) {
571 if (!mrtryupdate(&ip->i_lock))
572 goto out_undo_iolock;
573 } else if (lock_flags & XFS_ILOCK_SHARED) {
574 if (!mrtryaccess(&ip->i_lock))
575 goto out_undo_iolock;
577 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
581 if (lock_flags & XFS_IOLOCK_EXCL)
582 mrunlock_excl(&ip->i_iolock);
583 else if (lock_flags & XFS_IOLOCK_SHARED)
584 mrunlock_shared(&ip->i_iolock);
590 * xfs_iunlock() is used to drop the inode locks acquired with
591 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
592 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
593 * that we know which locks to drop.
595 * ip -- the inode being unlocked
596 * lock_flags -- this parameter indicates the inode's locks to be
597 * to be unlocked. See the comment for xfs_ilock() for a list
598 * of valid values for this parameter.
607 * You can't set both SHARED and EXCL for the same lock,
608 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
609 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
611 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
612 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
613 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
614 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
615 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
616 XFS_LOCK_DEP_MASK)) == 0);
617 ASSERT(lock_flags != 0);
619 if (lock_flags & XFS_IOLOCK_EXCL)
620 mrunlock_excl(&ip->i_iolock);
621 else if (lock_flags & XFS_IOLOCK_SHARED)
622 mrunlock_shared(&ip->i_iolock);
624 if (lock_flags & XFS_ILOCK_EXCL)
625 mrunlock_excl(&ip->i_lock);
626 else if (lock_flags & XFS_ILOCK_SHARED)
627 mrunlock_shared(&ip->i_lock);
629 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
630 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
632 * Let the AIL know that this item has been unlocked in case
633 * it is in the AIL and anyone is waiting on it. Don't do
634 * this if the caller has asked us not to.
636 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
637 (xfs_log_item_t*)(ip->i_itemp));
639 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
643 * give up write locks. the i/o lock cannot be held nested
644 * if it is being demoted.
651 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
652 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
654 if (lock_flags & XFS_ILOCK_EXCL)
655 mrdemote(&ip->i_lock);
656 if (lock_flags & XFS_IOLOCK_EXCL)
657 mrdemote(&ip->i_iolock);
662 * Debug-only routine, without additional rw_semaphore APIs, we can
663 * now only answer requests regarding whether we hold the lock for write
664 * (reader state is outside our visibility, we only track writer state).
666 * Note: this means !xfs_isilocked would give false positives, so don't do that.
673 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
675 if (!ip->i_lock.mr_writer)
679 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
681 if (!ip->i_iolock.mr_writer)