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 * Preload the radix tree so we can insert safely under the
166 if (radix_tree_preload(GFP_KERNEL)) {
172 xfs_ilock(ip, lock_flags);
174 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
175 first_index = agino & mask;
176 write_lock(&pag->pag_ici_lock);
178 /* insert the new inode */
179 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
180 if (unlikely(error)) {
181 WARN_ON(error != -EEXIST);
182 XFS_STATS_INC(xs_ig_dup);
187 /* These values _must_ be set before releasing the radix tree lock! */
188 ip->i_udquot = ip->i_gdquot = NULL;
189 xfs_iflags_set(ip, XFS_INEW);
191 write_unlock(&pag->pag_ici_lock);
192 radix_tree_preload_end();
197 write_unlock(&pag->pag_ici_lock);
198 radix_tree_preload_end();
205 * Look up an inode by number in the given file system.
206 * The inode is looked up in the cache held in each AG.
207 * If the inode is found in the cache, initialise the vfs inode
210 * If it is not in core, read it in from the file system's device,
211 * add it to the cache and initialise the vfs inode.
213 * The inode is locked according to the value of the lock_flags parameter.
214 * This flag parameter indicates how and if the inode's IO lock and inode lock
217 * mp -- the mount point structure for the current file system. It points
218 * to the inode hash table.
219 * tp -- a pointer to the current transaction if there is one. This is
220 * simply passed through to the xfs_iread() call.
221 * ino -- the number of the inode desired. This is the unique identifier
222 * within the file system for the inode being requested.
223 * lock_flags -- flags indicating how to lock the inode. See the comment
224 * for xfs_ilock() for a list of valid values.
225 * bno -- the block number starting the buffer containing the inode,
226 * if known (as by bulkstat), else 0.
243 /* the radix tree exists only in inode capable AGs */
244 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
247 /* get the perag structure and ensure that it's inode capable */
248 pag = xfs_get_perag(mp, ino);
249 if (!pag->pagi_inodeok)
251 ASSERT(pag->pag_ici_init);
252 agino = XFS_INO_TO_AGINO(mp, ino);
256 read_lock(&pag->pag_ici_lock);
257 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
260 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
262 goto out_error_or_again;
264 read_unlock(&pag->pag_ici_lock);
265 XFS_STATS_INC(xs_ig_missed);
267 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
270 goto out_error_or_again;
272 xfs_put_perag(mp, pag);
274 xfs_iflags_set(ip, XFS_IMODIFIED);
277 ASSERT(ip->i_df.if_ext_max ==
278 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
280 * If we have a real type for an on-disk inode, we can set ops(&unlock)
281 * now. If it's a new inode being created, xfs_ialloc will handle it.
283 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
288 if (error == EAGAIN) {
292 xfs_put_perag(mp, pag);
298 * Look for the inode corresponding to the given ino in the hash table.
299 * If it is there and its i_transp pointer matches tp, return it.
300 * Otherwise, return NULL.
303 xfs_inode_incore(xfs_mount_t *mp,
310 pag = xfs_get_perag(mp, ino);
311 read_lock(&pag->pag_ici_lock);
312 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
313 read_unlock(&pag->pag_ici_lock);
314 xfs_put_perag(mp, pag);
316 /* the returned inode must match the transaction */
317 if (ip && (ip->i_transp != tp))
323 * Decrement reference count of an inode structure and unlock it.
325 * ip -- the inode being released
326 * lock_flags -- this parameter indicates the inode's locks to be
327 * to be released. See the comment on xfs_iunlock() for a list
331 xfs_iput(xfs_inode_t *ip,
334 xfs_itrace_entry(ip);
335 xfs_iunlock(ip, lock_flags);
340 * Special iput for brand-new inodes that are still locked
347 struct inode *inode = VFS_I(ip);
349 xfs_itrace_entry(ip);
351 if ((ip->i_d.di_mode == 0)) {
352 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
353 make_bad_inode(inode);
355 if (inode->i_state & I_NEW)
356 unlock_new_inode(inode);
358 xfs_iunlock(ip, lock_flags);
364 * This routine embodies the part of the reclaim code that pulls
365 * the inode from the inode hash table and the mount structure's
367 * This should only be called from xfs_reclaim().
370 xfs_ireclaim(xfs_inode_t *ip)
373 * Remove from old hash list and mount list.
375 XFS_STATS_INC(xs_ig_reclaims);
380 * Here we do a spurious inode lock in order to coordinate with inode
381 * cache radix tree lookups. This is because the lookup can reference
382 * the inodes in the cache without taking references. We make that OK
383 * here by ensuring that we wait until the inode is unlocked after the
384 * lookup before we go ahead and free it. We get both the ilock and
385 * the iolock because the code may need to drop the ilock one but will
386 * still hold the iolock.
388 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
391 * Release dquots (and their references) if any. An inode may escape
392 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
394 XFS_QM_DQDETACH(ip->i_mount, ip);
397 * Free all memory associated with the inode.
399 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
404 * This routine removes an about-to-be-destroyed inode from
405 * all of the lists in which it is located with the exception
406 * of the behavior chain.
412 xfs_mount_t *mp = ip->i_mount;
413 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
415 write_lock(&pag->pag_ici_lock);
416 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
417 write_unlock(&pag->pag_ici_lock);
418 xfs_put_perag(mp, pag);
424 * This is a wrapper routine around the xfs_ilock() routine
425 * used to centralize some grungy code. It is used in places
426 * that wish to lock the inode solely for reading the extents.
427 * The reason these places can't just call xfs_ilock(SHARED)
428 * is that the inode lock also guards to bringing in of the
429 * extents from disk for a file in b-tree format. If the inode
430 * is in b-tree format, then we need to lock the inode exclusively
431 * until the extents are read in. Locking it exclusively all
432 * the time would limit our parallelism unnecessarily, though.
433 * What we do instead is check to see if the extents have been
434 * read in yet, and only lock the inode exclusively if they
437 * The function returns a value which should be given to the
438 * corresponding xfs_iunlock_map_shared(). This value is
439 * the mode in which the lock was actually taken.
442 xfs_ilock_map_shared(
447 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
448 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
449 lock_mode = XFS_ILOCK_EXCL;
451 lock_mode = XFS_ILOCK_SHARED;
454 xfs_ilock(ip, lock_mode);
460 * This is simply the unlock routine to go with xfs_ilock_map_shared().
461 * All it does is call xfs_iunlock() with the given lock_mode.
464 xfs_iunlock_map_shared(
466 unsigned int lock_mode)
468 xfs_iunlock(ip, lock_mode);
472 * The xfs inode contains 2 locks: a multi-reader lock called the
473 * i_iolock and a multi-reader lock called the i_lock. This routine
474 * allows either or both of the locks to be obtained.
476 * The 2 locks should always be ordered so that the IO lock is
477 * obtained first in order to prevent deadlock.
479 * ip -- the inode being locked
480 * lock_flags -- this parameter indicates the inode's locks
481 * to be locked. It can be:
486 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
487 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
488 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
489 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
497 * You can't set both SHARED and EXCL for the same lock,
498 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
499 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
501 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
502 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
503 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
504 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
505 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
507 if (lock_flags & XFS_IOLOCK_EXCL)
508 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
509 else if (lock_flags & XFS_IOLOCK_SHARED)
510 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
512 if (lock_flags & XFS_ILOCK_EXCL)
513 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
514 else if (lock_flags & XFS_ILOCK_SHARED)
515 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
517 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
521 * This is just like xfs_ilock(), except that the caller
522 * is guaranteed not to sleep. It returns 1 if it gets
523 * the requested locks and 0 otherwise. If the IO lock is
524 * obtained but the inode lock cannot be, then the IO lock
525 * is dropped before returning.
527 * ip -- the inode being locked
528 * lock_flags -- this parameter indicates the inode's locks to be
529 * to be locked. See the comment for xfs_ilock() for a list
538 * You can't set both SHARED and EXCL for the same lock,
539 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
540 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
542 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
543 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
544 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
545 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
546 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
548 if (lock_flags & XFS_IOLOCK_EXCL) {
549 if (!mrtryupdate(&ip->i_iolock))
551 } else if (lock_flags & XFS_IOLOCK_SHARED) {
552 if (!mrtryaccess(&ip->i_iolock))
555 if (lock_flags & XFS_ILOCK_EXCL) {
556 if (!mrtryupdate(&ip->i_lock))
557 goto out_undo_iolock;
558 } else if (lock_flags & XFS_ILOCK_SHARED) {
559 if (!mrtryaccess(&ip->i_lock))
560 goto out_undo_iolock;
562 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
566 if (lock_flags & XFS_IOLOCK_EXCL)
567 mrunlock_excl(&ip->i_iolock);
568 else if (lock_flags & XFS_IOLOCK_SHARED)
569 mrunlock_shared(&ip->i_iolock);
575 * xfs_iunlock() is used to drop the inode locks acquired with
576 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
577 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
578 * that we know which locks to drop.
580 * ip -- the inode being unlocked
581 * lock_flags -- this parameter indicates the inode's locks to be
582 * to be unlocked. See the comment for xfs_ilock() for a list
583 * of valid values for this parameter.
592 * You can't set both SHARED and EXCL for the same lock,
593 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
594 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
596 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
597 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
598 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
599 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
600 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
601 XFS_LOCK_DEP_MASK)) == 0);
602 ASSERT(lock_flags != 0);
604 if (lock_flags & XFS_IOLOCK_EXCL)
605 mrunlock_excl(&ip->i_iolock);
606 else if (lock_flags & XFS_IOLOCK_SHARED)
607 mrunlock_shared(&ip->i_iolock);
609 if (lock_flags & XFS_ILOCK_EXCL)
610 mrunlock_excl(&ip->i_lock);
611 else if (lock_flags & XFS_ILOCK_SHARED)
612 mrunlock_shared(&ip->i_lock);
614 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
615 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
617 * Let the AIL know that this item has been unlocked in case
618 * it is in the AIL and anyone is waiting on it. Don't do
619 * this if the caller has asked us not to.
621 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
622 (xfs_log_item_t*)(ip->i_itemp));
624 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
628 * give up write locks. the i/o lock cannot be held nested
629 * if it is being demoted.
636 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
637 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
639 if (lock_flags & XFS_ILOCK_EXCL)
640 mrdemote(&ip->i_lock);
641 if (lock_flags & XFS_IOLOCK_EXCL)
642 mrdemote(&ip->i_iolock);
647 * Debug-only routine, without additional rw_semaphore APIs, we can
648 * now only answer requests regarding whether we hold the lock for write
649 * (reader state is outside our visibility, we only track writer state).
651 * Note: this means !xfs_isilocked would give false positives, so don't do that.
658 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
660 if (!ip->i_lock.mr_writer)
664 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
666 if (!ip->i_iolock.mr_writer)