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))) {
64 XFS_STATS_INC(xs_ig_frecycle);
68 /* If IRECLAIMABLE is set, we've torn down the vfs inode part */
69 if (xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
72 * If lookup is racing with unlink, then we should return an
73 * error immediately so we don't remove it from the reclaim
74 * list and potentially leak the inode.
77 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
82 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
85 * We need to re-initialise the VFS inode as it has been
86 * 'freed' by the VFS. Do this here so we can deal with
87 * errors cleanly, then tag it so it can be set up correctly
90 if (!inode_init_always(mp->m_super, VFS_I(ip))) {
94 xfs_iflags_set(ip, XFS_INEW);
95 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
97 /* clear the radix tree reclaim flag as well. */
98 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
99 read_unlock(&pag->pag_ici_lock);
100 } else if (!igrab(VFS_I(ip))) {
101 /* If the VFS inode is being torn down, pause and try again. */
103 XFS_STATS_INC(xs_ig_frecycle);
106 /* we've got a live one */
107 read_unlock(&pag->pag_ici_lock);
110 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
116 xfs_ilock(ip, lock_flags);
118 xfs_iflags_clear(ip, XFS_ISTALE);
119 xfs_itrace_exit_tag(ip, "xfs_iget.found");
120 XFS_STATS_INC(xs_ig_found);
124 read_unlock(&pag->pag_ici_lock);
132 struct xfs_mount *mp,
133 struct xfs_perag *pag,
136 struct xfs_inode **ipp,
139 int lock_flags) __releases(pag->pag_ici_lock)
141 struct xfs_inode *ip;
143 unsigned long first_index, mask;
144 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
147 * Read the disk inode attributes into a new inode structure and get
148 * a new vnode for it. This should also initialize i_ino and i_mount.
150 error = xfs_iread(mp, tp, ino, &ip, bno,
151 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
155 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
157 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
163 xfs_ilock(ip, lock_flags);
166 * Preload the radix tree so we can insert safely under the
167 * write spinlock. Note that we cannot sleep inside the preload
170 if (radix_tree_preload(GFP_KERNEL)) {
175 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
176 first_index = agino & mask;
177 write_lock(&pag->pag_ici_lock);
179 /* insert the new inode */
180 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
181 if (unlikely(error)) {
182 WARN_ON(error != -EEXIST);
183 XFS_STATS_INC(xs_ig_dup);
185 goto out_preload_end;
188 /* These values _must_ be set before releasing the radix tree lock! */
189 ip->i_udquot = ip->i_gdquot = NULL;
190 xfs_iflags_set(ip, XFS_INEW);
192 write_unlock(&pag->pag_ici_lock);
193 radix_tree_preload_end();
198 write_unlock(&pag->pag_ici_lock);
199 radix_tree_preload_end();
202 xfs_iunlock(ip, lock_flags);
204 xfs_destroy_inode(ip);
209 * Look up an inode by number in the given file system.
210 * The inode is looked up in the cache held in each AG.
211 * If the inode is found in the cache, initialise the vfs inode
214 * If it is not in core, read it in from the file system's device,
215 * add it to the cache and initialise the vfs inode.
217 * The inode is locked according to the value of the lock_flags parameter.
218 * This flag parameter indicates how and if the inode's IO lock and inode lock
221 * mp -- the mount point structure for the current file system. It points
222 * to the inode hash table.
223 * tp -- a pointer to the current transaction if there is one. This is
224 * simply passed through to the xfs_iread() call.
225 * ino -- the number of the inode desired. This is the unique identifier
226 * within the file system for the inode being requested.
227 * lock_flags -- flags indicating how to lock the inode. See the comment
228 * for xfs_ilock() for a list of valid values.
229 * bno -- the block number starting the buffer containing the inode,
230 * if known (as by bulkstat), else 0.
247 /* the radix tree exists only in inode capable AGs */
248 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
251 /* get the perag structure and ensure that it's inode capable */
252 pag = xfs_get_perag(mp, ino);
253 if (!pag->pagi_inodeok)
255 ASSERT(pag->pag_ici_init);
256 agino = XFS_INO_TO_AGINO(mp, ino);
260 read_lock(&pag->pag_ici_lock);
261 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
264 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
266 goto out_error_or_again;
268 read_unlock(&pag->pag_ici_lock);
269 XFS_STATS_INC(xs_ig_missed);
271 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
274 goto out_error_or_again;
276 xfs_put_perag(mp, pag);
278 xfs_iflags_set(ip, XFS_IMODIFIED);
281 ASSERT(ip->i_df.if_ext_max ==
282 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
284 * If we have a real type for an on-disk inode, we can set ops(&unlock)
285 * now. If it's a new inode being created, xfs_ialloc will handle it.
287 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
292 if (error == EAGAIN) {
296 xfs_put_perag(mp, pag);
302 * Look for the inode corresponding to the given ino in the hash table.
303 * If it is there and its i_transp pointer matches tp, return it.
304 * Otherwise, return NULL.
307 xfs_inode_incore(xfs_mount_t *mp,
314 pag = xfs_get_perag(mp, ino);
315 read_lock(&pag->pag_ici_lock);
316 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
317 read_unlock(&pag->pag_ici_lock);
318 xfs_put_perag(mp, pag);
320 /* the returned inode must match the transaction */
321 if (ip && (ip->i_transp != tp))
327 * Decrement reference count of an inode structure and unlock it.
329 * ip -- the inode being released
330 * lock_flags -- this parameter indicates the inode's locks to be
331 * to be released. See the comment on xfs_iunlock() for a list
335 xfs_iput(xfs_inode_t *ip,
338 xfs_itrace_entry(ip);
339 xfs_iunlock(ip, lock_flags);
344 * Special iput for brand-new inodes that are still locked
351 struct inode *inode = VFS_I(ip);
353 xfs_itrace_entry(ip);
355 if ((ip->i_d.di_mode == 0)) {
356 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
357 make_bad_inode(inode);
359 if (inode->i_state & I_NEW)
360 unlock_new_inode(inode);
362 xfs_iunlock(ip, lock_flags);
368 * This routine embodies the part of the reclaim code that pulls
369 * the inode from the inode hash table and the mount structure's
371 * This should only be called from xfs_reclaim().
374 xfs_ireclaim(xfs_inode_t *ip)
377 * Remove from old hash list and mount list.
379 XFS_STATS_INC(xs_ig_reclaims);
384 * Here we do a spurious inode lock in order to coordinate with inode
385 * cache radix tree lookups. This is because the lookup can reference
386 * the inodes in the cache without taking references. We make that OK
387 * here by ensuring that we wait until the inode is unlocked after the
388 * lookup before we go ahead and free it. We get both the ilock and
389 * the iolock because the code may need to drop the ilock one but will
390 * still hold the iolock.
392 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
395 * Release dquots (and their references) if any. An inode may escape
396 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
398 XFS_QM_DQDETACH(ip->i_mount, ip);
401 * Free all memory associated with the inode.
403 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
408 * This routine removes an about-to-be-destroyed inode from
409 * all of the lists in which it is located with the exception
410 * of the behavior chain.
416 xfs_mount_t *mp = ip->i_mount;
417 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
419 write_lock(&pag->pag_ici_lock);
420 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
421 write_unlock(&pag->pag_ici_lock);
422 xfs_put_perag(mp, pag);
428 * This is a wrapper routine around the xfs_ilock() routine
429 * used to centralize some grungy code. It is used in places
430 * that wish to lock the inode solely for reading the extents.
431 * The reason these places can't just call xfs_ilock(SHARED)
432 * is that the inode lock also guards to bringing in of the
433 * extents from disk for a file in b-tree format. If the inode
434 * is in b-tree format, then we need to lock the inode exclusively
435 * until the extents are read in. Locking it exclusively all
436 * the time would limit our parallelism unnecessarily, though.
437 * What we do instead is check to see if the extents have been
438 * read in yet, and only lock the inode exclusively if they
441 * The function returns a value which should be given to the
442 * corresponding xfs_iunlock_map_shared(). This value is
443 * the mode in which the lock was actually taken.
446 xfs_ilock_map_shared(
451 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
452 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
453 lock_mode = XFS_ILOCK_EXCL;
455 lock_mode = XFS_ILOCK_SHARED;
458 xfs_ilock(ip, lock_mode);
464 * This is simply the unlock routine to go with xfs_ilock_map_shared().
465 * All it does is call xfs_iunlock() with the given lock_mode.
468 xfs_iunlock_map_shared(
470 unsigned int lock_mode)
472 xfs_iunlock(ip, lock_mode);
476 * The xfs inode contains 2 locks: a multi-reader lock called the
477 * i_iolock and a multi-reader lock called the i_lock. This routine
478 * allows either or both of the locks to be obtained.
480 * The 2 locks should always be ordered so that the IO lock is
481 * obtained first in order to prevent deadlock.
483 * ip -- the inode being locked
484 * lock_flags -- this parameter indicates the inode's locks
485 * to be locked. It can be:
490 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
491 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
492 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
493 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
501 * You can't set both SHARED and EXCL for the same lock,
502 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
503 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
505 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
506 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
507 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
508 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
509 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
511 if (lock_flags & XFS_IOLOCK_EXCL)
512 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
513 else if (lock_flags & XFS_IOLOCK_SHARED)
514 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
516 if (lock_flags & XFS_ILOCK_EXCL)
517 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
518 else if (lock_flags & XFS_ILOCK_SHARED)
519 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
521 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
525 * This is just like xfs_ilock(), except that the caller
526 * is guaranteed not to sleep. It returns 1 if it gets
527 * the requested locks and 0 otherwise. If the IO lock is
528 * obtained but the inode lock cannot be, then the IO lock
529 * is dropped before returning.
531 * ip -- the inode being locked
532 * lock_flags -- this parameter indicates the inode's locks to be
533 * to be locked. See the comment for xfs_ilock() for a list
542 * You can't set both SHARED and EXCL for the same lock,
543 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
544 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
546 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
547 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
548 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
549 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
550 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
552 if (lock_flags & XFS_IOLOCK_EXCL) {
553 if (!mrtryupdate(&ip->i_iolock))
555 } else if (lock_flags & XFS_IOLOCK_SHARED) {
556 if (!mrtryaccess(&ip->i_iolock))
559 if (lock_flags & XFS_ILOCK_EXCL) {
560 if (!mrtryupdate(&ip->i_lock))
561 goto out_undo_iolock;
562 } else if (lock_flags & XFS_ILOCK_SHARED) {
563 if (!mrtryaccess(&ip->i_lock))
564 goto out_undo_iolock;
566 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
570 if (lock_flags & XFS_IOLOCK_EXCL)
571 mrunlock_excl(&ip->i_iolock);
572 else if (lock_flags & XFS_IOLOCK_SHARED)
573 mrunlock_shared(&ip->i_iolock);
579 * xfs_iunlock() is used to drop the inode locks acquired with
580 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
581 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
582 * that we know which locks to drop.
584 * ip -- the inode being unlocked
585 * lock_flags -- this parameter indicates the inode's locks to be
586 * to be unlocked. See the comment for xfs_ilock() for a list
587 * of valid values for this parameter.
596 * You can't set both SHARED and EXCL for the same lock,
597 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
598 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
600 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
601 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
602 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
603 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
604 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
605 XFS_LOCK_DEP_MASK)) == 0);
606 ASSERT(lock_flags != 0);
608 if (lock_flags & XFS_IOLOCK_EXCL)
609 mrunlock_excl(&ip->i_iolock);
610 else if (lock_flags & XFS_IOLOCK_SHARED)
611 mrunlock_shared(&ip->i_iolock);
613 if (lock_flags & XFS_ILOCK_EXCL)
614 mrunlock_excl(&ip->i_lock);
615 else if (lock_flags & XFS_ILOCK_SHARED)
616 mrunlock_shared(&ip->i_lock);
618 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
619 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
621 * Let the AIL know that this item has been unlocked in case
622 * it is in the AIL and anyone is waiting on it. Don't do
623 * this if the caller has asked us not to.
625 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
626 (xfs_log_item_t*)(ip->i_itemp));
628 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
632 * give up write locks. the i/o lock cannot be held nested
633 * if it is being demoted.
640 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
641 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
643 if (lock_flags & XFS_ILOCK_EXCL)
644 mrdemote(&ip->i_lock);
645 if (lock_flags & XFS_IOLOCK_EXCL)
646 mrdemote(&ip->i_iolock);
651 * Debug-only routine, without additional rw_semaphore APIs, we can
652 * now only answer requests regarding whether we hold the lock for write
653 * (reader state is outside our visibility, we only track writer state).
655 * Note: this means !xfs_isilocked would give false positives, so don't do that.
662 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
664 if (!ip->i_lock.mr_writer)
668 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
670 if (!ip->i_iolock.mr_writer)