blob: 9fae4755660457d09c0b68c4ef7ed5ad7a4269d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020021#include "xfs_acl.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
33#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
35#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_quota.h"
37#include "xfs_utils.h"
David Chinner783a2f62008-10-30 17:39:58 +110038#include "xfs_trans_priv.h"
39#include "xfs_inode_item.h"
Christoph Hellwig24f211b2008-11-28 14:23:42 +110040#include "xfs_bmap.h"
41#include "xfs_btree_trace.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042#include "xfs_trace.h"
Christoph Hellwig24f211b2008-11-28 14:23:42 +110043
44
45/*
Dave Chinnerdcfcf202010-12-23 11:57:13 +110046 * Define xfs inode iolock lockdep classes. We need to ensure that all active
47 * inodes are considered the same for lockdep purposes, including inodes that
48 * are recycled through the XFS_IRECLAIMABLE state. This is the the only way to
49 * guarantee the locks are considered the same when there are multiple lock
50 * initialisation siteѕ. Also, define a reclaimable inode class so it is
51 * obvious in lockdep reports which class the report is against.
52 */
53static struct lock_class_key xfs_iolock_active;
54struct lock_class_key xfs_iolock_reclaimable;
55
56/*
Christoph Hellwig24f211b2008-11-28 14:23:42 +110057 * Allocate and initialise an xfs_inode.
58 */
59STATIC struct xfs_inode *
60xfs_inode_alloc(
61 struct xfs_mount *mp,
62 xfs_ino_t ino)
63{
64 struct xfs_inode *ip;
65
66 /*
67 * if this didn't occur in transactions, we could use
68 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
69 * code up to do this anyway.
70 */
71 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
72 if (!ip)
73 return NULL;
Christoph Hellwig54e34622009-08-07 14:38:25 -030074 if (inode_init_always(mp->m_super, VFS_I(ip))) {
75 kmem_zone_free(xfs_inode_zone, ip);
76 return NULL;
77 }
Christoph Hellwig24f211b2008-11-28 14:23:42 +110078
79 ASSERT(atomic_read(&ip->i_iocount) == 0);
80 ASSERT(atomic_read(&ip->i_pincount) == 0);
81 ASSERT(!spin_is_locked(&ip->i_flags_lock));
82 ASSERT(completion_done(&ip->i_flush));
Christoph Hellwig033da482009-10-19 04:05:26 +000083
84 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
Dave Chinnerdcfcf202010-12-23 11:57:13 +110085 lockdep_set_class_and_name(&ip->i_iolock.mr_lock,
86 &xfs_iolock_active, "xfs_iolock_active");
Christoph Hellwig24f211b2008-11-28 14:23:42 +110087
Christoph Hellwig24f211b2008-11-28 14:23:42 +110088 /* initialise the xfs inode */
89 ip->i_ino = ino;
90 ip->i_mount = mp;
91 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
92 ip->i_afp = NULL;
93 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
94 ip->i_flags = 0;
95 ip->i_update_core = 0;
Christoph Hellwig24f211b2008-11-28 14:23:42 +110096 ip->i_delayed_blks = 0;
97 memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
98 ip->i_size = 0;
99 ip->i_new_size = 0;
100
Dave Chinner705db3f2009-04-06 18:40:17 +0200101 /* prevent anyone from using this yet */
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100102 VFS_I(ip)->i_state = I_NEW;
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100103
104 return ip;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Dave Chinner2f11fea2010-07-20 17:53:25 +1000107void
Dave Chinnerd95b7aa2010-12-16 16:41:39 +1100108__xfs_inode_free(
109 struct rcu_head *head)
110{
111 struct inode *inode = container_of((void *)head,
112 struct inode, i_dentry);
113 struct xfs_inode *ip = XFS_I(inode);
114
115 INIT_LIST_HEAD(&inode->i_dentry);
116 kmem_zone_free(xfs_inode_zone, ip);
117}
118
119void
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300120xfs_inode_free(
121 struct xfs_inode *ip)
122{
123 switch (ip->i_d.di_mode & S_IFMT) {
124 case S_IFREG:
125 case S_IFDIR:
126 case S_IFLNK:
127 xfs_idestroy_fork(ip, XFS_DATA_FORK);
128 break;
129 }
130
131 if (ip->i_afp)
132 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
133
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300134 if (ip->i_itemp) {
135 /*
136 * Only if we are shutting down the fs will we see an
137 * inode still in the AIL. If it is there, we should remove
138 * it to prevent a use-after-free from occurring.
139 */
140 xfs_log_item_t *lip = &ip->i_itemp->ili_item;
141 struct xfs_ail *ailp = lip->li_ailp;
142
143 ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
144 XFS_FORCED_SHUTDOWN(ip->i_mount));
145 if (lip->li_flags & XFS_LI_IN_AIL) {
146 spin_lock(&ailp->xa_lock);
147 if (lip->li_flags & XFS_LI_IN_AIL)
148 xfs_trans_ail_delete(ailp, lip);
149 else
150 spin_unlock(&ailp->xa_lock);
151 }
152 xfs_inode_item_destroy(ip);
153 ip->i_itemp = NULL;
154 }
155
156 /* asserts to verify all state is correct here */
157 ASSERT(atomic_read(&ip->i_iocount) == 0);
158 ASSERT(atomic_read(&ip->i_pincount) == 0);
159 ASSERT(!spin_is_locked(&ip->i_flags_lock));
160 ASSERT(completion_done(&ip->i_flush));
161
Dave Chinnerd95b7aa2010-12-16 16:41:39 +1100162 call_rcu((struct rcu_head *)&VFS_I(ip)->i_dentry, __xfs_inode_free);
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
David Chinner6441e542008-10-30 17:21:19 +1100166 * Check the validity of the inode we just found it the cache
167 */
168static int
169xfs_iget_cache_hit(
David Chinner6441e542008-10-30 17:21:19 +1100170 struct xfs_perag *pag,
171 struct xfs_inode *ip,
172 int flags,
173 int lock_flags) __releases(pag->pag_ici_lock)
174{
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400175 struct inode *inode = VFS_I(ip);
David Chinner6441e542008-10-30 17:21:19 +1100176 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400177 int error;
178
179 spin_lock(&ip->i_flags_lock);
David Chinner6441e542008-10-30 17:21:19 +1100180
181 /*
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400182 * If we are racing with another cache hit that is currently
183 * instantiating this inode or currently recycling it out of
184 * reclaimabe state, wait for the initialisation to complete
185 * before continuing.
186 *
187 * XXX(hch): eventually we should do something equivalent to
188 * wait_on_inode to wait for these flags to be cleared
189 * instead of polling for it.
David Chinner6441e542008-10-30 17:21:19 +1100190 */
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400191 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000192 trace_xfs_iget_skip(ip);
David Chinner6441e542008-10-30 17:21:19 +1100193 XFS_STATS_INC(xs_ig_frecycle);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400194 error = EAGAIN;
David Chinner6441e542008-10-30 17:21:19 +1100195 goto out_error;
196 }
197
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400198 /*
199 * If lookup is racing with unlink return an error immediately.
200 */
201 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
202 error = ENOENT;
203 goto out_error;
204 }
David Chinner6441e542008-10-30 17:21:19 +1100205
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400206 /*
207 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
208 * Need to carefully get it back into useable state.
209 */
210 if (ip->i_flags & XFS_IRECLAIMABLE) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000211 trace_xfs_iget_reclaim(ip);
David Chinner6441e542008-10-30 17:21:19 +1100212
David Chinnerbf904242008-10-30 17:36:14 +1100213 /*
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000214 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
215 * from stomping over us while we recycle the inode. We can't
216 * clear the radix tree reclaimable tag yet as it requires
217 * pag_ici_lock to be held exclusive.
David Chinnerbf904242008-10-30 17:36:14 +1100218 */
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000219 ip->i_flags |= XFS_IRECLAIM;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400220
221 spin_unlock(&ip->i_flags_lock);
222 read_unlock(&pag->pag_ici_lock);
223
224 error = -inode_init_always(mp->m_super, inode);
225 if (error) {
226 /*
227 * Re-initializing the inode failed, and we are in deep
228 * trouble. Try to re-add it to the reclaim list.
229 */
230 read_lock(&pag->pag_ici_lock);
231 spin_lock(&ip->i_flags_lock);
232
233 ip->i_flags &= ~XFS_INEW;
234 ip->i_flags |= XFS_IRECLAIMABLE;
235 __xfs_inode_set_reclaim_tag(pag, ip);
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000236 trace_xfs_iget_reclaim_fail(ip);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400237 goto out_error;
238 }
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000239
240 write_lock(&pag->pag_ici_lock);
241 spin_lock(&ip->i_flags_lock);
242 ip->i_flags &= ~(XFS_IRECLAIMABLE | XFS_IRECLAIM);
243 ip->i_flags |= XFS_INEW;
244 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100245 inode->i_state = I_NEW;
Dave Chinnerdcfcf202010-12-23 11:57:13 +1100246
247 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
248 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
249 lockdep_set_class_and_name(&ip->i_iolock.mr_lock,
250 &xfs_iolock_active, "xfs_iolock_active");
251
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000252 spin_unlock(&ip->i_flags_lock);
253 write_unlock(&pag->pag_ici_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400254 } else {
255 /* If the VFS inode is being torn down, pause and try again. */
256 if (!igrab(inode)) {
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000257 trace_xfs_iget_skip(ip);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400258 error = EAGAIN;
David Chinnerbf904242008-10-30 17:36:14 +1100259 goto out_error;
260 }
David Chinner6bfb3d02008-10-30 18:32:43 +1100261
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400262 /* We've got a live one. */
263 spin_unlock(&ip->i_flags_lock);
264 read_unlock(&pag->pag_ici_lock);
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000265 trace_xfs_iget_hit(ip);
David Chinner6441e542008-10-30 17:21:19 +1100266 }
267
David Chinner6441e542008-10-30 17:21:19 +1100268 if (lock_flags != 0)
269 xfs_ilock(ip, lock_flags);
270
271 xfs_iflags_clear(ip, XFS_ISTALE);
David Chinner6441e542008-10-30 17:21:19 +1100272 XFS_STATS_INC(xs_ig_found);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000273
David Chinner6441e542008-10-30 17:21:19 +1100274 return 0;
275
276out_error:
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400277 spin_unlock(&ip->i_flags_lock);
David Chinner6441e542008-10-30 17:21:19 +1100278 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100279 return error;
280}
281
282
283static int
284xfs_iget_cache_miss(
285 struct xfs_mount *mp,
286 struct xfs_perag *pag,
287 xfs_trans_t *tp,
288 xfs_ino_t ino,
289 struct xfs_inode **ipp,
David Chinner6441e542008-10-30 17:21:19 +1100290 int flags,
Christoph Hellwig0c3dc2b2009-11-14 16:17:23 +0000291 int lock_flags)
David Chinner6441e542008-10-30 17:21:19 +1100292{
293 struct xfs_inode *ip;
294 int error;
David Chinner6441e542008-10-30 17:21:19 +1100295 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
296
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100297 ip = xfs_inode_alloc(mp, ino);
298 if (!ip)
299 return ENOMEM;
300
Dave Chinner7b6259e2010-06-24 11:35:17 +1000301 error = xfs_iread(mp, tp, ip, flags);
David Chinner6441e542008-10-30 17:21:19 +1100302 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100303 goto out_destroy;
David Chinner6441e542008-10-30 17:21:19 +1100304
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000305 trace_xfs_iget_miss(ip);
David Chinner6441e542008-10-30 17:21:19 +1100306
307 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
308 error = ENOENT;
309 goto out_destroy;
310 }
311
312 /*
313 * Preload the radix tree so we can insert safely under the
David Chinner56e73ec2008-10-30 17:55:27 +1100314 * write spinlock. Note that we cannot sleep inside the preload
315 * region.
David Chinner6441e542008-10-30 17:21:19 +1100316 */
317 if (radix_tree_preload(GFP_KERNEL)) {
318 error = EAGAIN;
Christoph Hellwiged93ec32009-03-03 14:48:35 -0500319 goto out_destroy;
320 }
321
322 /*
323 * Because the inode hasn't been added to the radix-tree yet it can't
324 * be found by another thread, so we can do the non-sleeping lock here.
325 */
326 if (lock_flags) {
327 if (!xfs_ilock_nowait(ip, lock_flags))
328 BUG();
David Chinner6441e542008-10-30 17:21:19 +1100329 }
330
David Chinner6441e542008-10-30 17:21:19 +1100331 write_lock(&pag->pag_ici_lock);
332
333 /* insert the new inode */
334 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
335 if (unlikely(error)) {
336 WARN_ON(error != -EEXIST);
337 XFS_STATS_INC(xs_ig_dup);
338 error = EAGAIN;
David Chinner56e73ec2008-10-30 17:55:27 +1100339 goto out_preload_end;
David Chinner6441e542008-10-30 17:21:19 +1100340 }
341
342 /* These values _must_ be set before releasing the radix tree lock! */
343 ip->i_udquot = ip->i_gdquot = NULL;
344 xfs_iflags_set(ip, XFS_INEW);
345
346 write_unlock(&pag->pag_ici_lock);
347 radix_tree_preload_end();
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000348
David Chinner6441e542008-10-30 17:21:19 +1100349 *ipp = ip;
350 return 0;
351
David Chinner56e73ec2008-10-30 17:55:27 +1100352out_preload_end:
David Chinner6441e542008-10-30 17:21:19 +1100353 write_unlock(&pag->pag_ici_lock);
354 radix_tree_preload_end();
David Chinner56e73ec2008-10-30 17:55:27 +1100355 if (lock_flags)
356 xfs_iunlock(ip, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100357out_destroy:
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300358 __destroy_inode(VFS_I(ip));
359 xfs_inode_free(ip);
David Chinner6441e542008-10-30 17:21:19 +1100360 return error;
361}
362
363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000365 * The inode is looked up in the cache held in each AG.
David Chinnerbf904242008-10-30 17:36:14 +1100366 * If the inode is found in the cache, initialise the vfs inode
367 * if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
David Chinnerda353b02007-08-28 14:00:13 +1000369 * If it is not in core, read it in from the file system's device,
David Chinnerbf904242008-10-30 17:36:14 +1100370 * add it to the cache and initialise the vfs inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
372 * The inode is locked according to the value of the lock_flags parameter.
373 * This flag parameter indicates how and if the inode's IO lock and inode lock
374 * should be taken.
375 *
376 * mp -- the mount point structure for the current file system. It points
377 * to the inode hash table.
378 * tp -- a pointer to the current transaction if there is one. This is
379 * simply passed through to the xfs_iread() call.
380 * ino -- the number of the inode desired. This is the unique identifier
381 * within the file system for the inode being requested.
382 * lock_flags -- flags indicating how to lock the inode. See the comment
383 * for xfs_ilock() for a list of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
David Chinnerbf904242008-10-30 17:36:14 +1100385int
386xfs_iget(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 xfs_mount_t *mp,
388 xfs_trans_t *tp,
389 xfs_ino_t ino,
390 uint flags,
391 uint lock_flags,
Dave Chinner7b6259e2010-06-24 11:35:17 +1000392 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000396 xfs_perag_t *pag;
397 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Christoph Hellwigd2767342010-10-06 18:31:23 +0000399 /* reject inode numbers outside existing AGs */
400 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
David Chinnerda353b02007-08-28 14:00:13 +1000401 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Chinnerda353b02007-08-28 14:00:13 +1000403 /* get the perag structure and ensure that it's inode capable */
Dave Chinner5017e972010-01-11 11:47:40 +0000404 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
David Chinnerda353b02007-08-28 14:00:13 +1000405 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407again:
David Chinner6441e542008-10-30 17:21:19 +1100408 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000409 read_lock(&pag->pag_ici_lock);
410 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
David Chinner6441e542008-10-30 17:21:19 +1100412 if (ip) {
David Chinnerbf904242008-10-30 17:36:14 +1100413 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100414 if (error)
415 goto out_error_or_again;
416 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000417 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100418 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000419
Dave Chinner7b6259e2010-06-24 11:35:17 +1000420 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
David Chinner6441e542008-10-30 17:21:19 +1100421 flags, lock_flags);
422 if (error)
423 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Dave Chinner5017e972010-01-11 11:47:40 +0000425 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *ipp = ip;
428
David Chinnerbf904242008-10-30 17:36:14 +1100429 ASSERT(ip->i_df.if_ext_max ==
430 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000431 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * If we have a real type for an on-disk inode, we can set ops(&unlock)
433 * now. If it's a new inode being created, xfs_ialloc will handle it.
434 */
David Chinnerbf904242008-10-30 17:36:14 +1100435 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000436 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100438
439out_error_or_again:
440 if (error == EAGAIN) {
441 delay(1);
442 goto again;
443 }
Dave Chinner5017e972010-01-11 11:47:40 +0000444 xfs_perag_put(pag);
David Chinner6441e542008-10-30 17:21:19 +1100445 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * This is a wrapper routine around the xfs_ilock() routine
450 * used to centralize some grungy code. It is used in places
451 * that wish to lock the inode solely for reading the extents.
452 * The reason these places can't just call xfs_ilock(SHARED)
453 * is that the inode lock also guards to bringing in of the
454 * extents from disk for a file in b-tree format. If the inode
455 * is in b-tree format, then we need to lock the inode exclusively
456 * until the extents are read in. Locking it exclusively all
457 * the time would limit our parallelism unnecessarily, though.
458 * What we do instead is check to see if the extents have been
459 * read in yet, and only lock the inode exclusively if they
460 * have not.
461 *
462 * The function returns a value which should be given to the
463 * corresponding xfs_iunlock_map_shared(). This value is
464 * the mode in which the lock was actually taken.
465 */
466uint
467xfs_ilock_map_shared(
468 xfs_inode_t *ip)
469{
470 uint lock_mode;
471
472 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
473 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
474 lock_mode = XFS_ILOCK_EXCL;
475 } else {
476 lock_mode = XFS_ILOCK_SHARED;
477 }
478
479 xfs_ilock(ip, lock_mode);
480
481 return lock_mode;
482}
483
484/*
485 * This is simply the unlock routine to go with xfs_ilock_map_shared().
486 * All it does is call xfs_iunlock() with the given lock_mode.
487 */
488void
489xfs_iunlock_map_shared(
490 xfs_inode_t *ip,
491 unsigned int lock_mode)
492{
493 xfs_iunlock(ip, lock_mode);
494}
495
496/*
497 * The xfs inode contains 2 locks: a multi-reader lock called the
498 * i_iolock and a multi-reader lock called the i_lock. This routine
499 * allows either or both of the locks to be obtained.
500 *
501 * The 2 locks should always be ordered so that the IO lock is
502 * obtained first in order to prevent deadlock.
503 *
504 * ip -- the inode being locked
505 * lock_flags -- this parameter indicates the inode's locks
506 * to be locked. It can be:
507 * XFS_IOLOCK_SHARED,
508 * XFS_IOLOCK_EXCL,
509 * XFS_ILOCK_SHARED,
510 * XFS_ILOCK_EXCL,
511 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
512 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
513 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
514 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
515 */
516void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000517xfs_ilock(
518 xfs_inode_t *ip,
519 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 /*
522 * You can't set both SHARED and EXCL for the same lock,
523 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
524 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
525 */
526 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
527 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
528 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
529 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000530 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000532 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000533 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000534 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000535 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000536
537 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000538 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000539 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000540 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000541
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000542 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/*
546 * This is just like xfs_ilock(), except that the caller
547 * is guaranteed not to sleep. It returns 1 if it gets
548 * the requested locks and 0 otherwise. If the IO lock is
549 * obtained but the inode lock cannot be, then the IO lock
550 * is dropped before returning.
551 *
552 * ip -- the inode being locked
553 * lock_flags -- this parameter indicates the inode's locks to be
554 * to be locked. See the comment for xfs_ilock() for a list
555 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
557int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000558xfs_ilock_nowait(
559 xfs_inode_t *ip,
560 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /*
563 * You can't set both SHARED and EXCL for the same lock,
564 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
565 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
566 */
567 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
568 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
569 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
570 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000571 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000574 if (!mrtryupdate(&ip->i_iolock))
575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000577 if (!mrtryaccess(&ip->i_iolock))
578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000581 if (!mrtryupdate(&ip->i_lock))
582 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000584 if (!mrtryaccess(&ip->i_lock))
585 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000587 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000589
590 out_undo_iolock:
591 if (lock_flags & XFS_IOLOCK_EXCL)
592 mrunlock_excl(&ip->i_iolock);
593 else if (lock_flags & XFS_IOLOCK_SHARED)
594 mrunlock_shared(&ip->i_iolock);
595 out:
596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599/*
600 * xfs_iunlock() is used to drop the inode locks acquired with
601 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
602 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
603 * that we know which locks to drop.
604 *
605 * ip -- the inode being unlocked
606 * lock_flags -- this parameter indicates the inode's locks to be
607 * to be unlocked. See the comment for xfs_ilock() for a list
608 * of valid values for this parameter.
609 *
610 */
611void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000612xfs_iunlock(
613 xfs_inode_t *ip,
614 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 /*
617 * You can't set both SHARED and EXCL for the same lock,
618 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
619 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
620 */
621 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
622 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
623 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
624 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000625 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
626 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 ASSERT(lock_flags != 0);
628
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000629 if (lock_flags & XFS_IOLOCK_EXCL)
630 mrunlock_excl(&ip->i_iolock);
631 else if (lock_flags & XFS_IOLOCK_SHARED)
632 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000634 if (lock_flags & XFS_ILOCK_EXCL)
635 mrunlock_excl(&ip->i_lock);
636 else if (lock_flags & XFS_ILOCK_SHARED)
637 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000639 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
640 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Let the AIL know that this item has been unlocked in case
643 * it is in the AIL and anyone is waiting on it. Don't do
644 * this if the caller has asked us not to.
645 */
David Chinner783a2f62008-10-30 17:39:58 +1100646 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000647 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000649 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/*
653 * give up write locks. the i/o lock cannot be held nested
654 * if it is being demoted.
655 */
656void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000657xfs_ilock_demote(
658 xfs_inode_t *ip,
659 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
662 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
663
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000664 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000666 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 mrdemote(&ip->i_iolock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000668
669 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000672#ifdef DEBUG
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000673int
674xfs_isilocked(
675 xfs_inode_t *ip,
676 uint lock_flags)
677{
Christoph Hellwigf9369722010-06-03 16:22:29 +1000678 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
679 if (!(lock_flags & XFS_ILOCK_SHARED))
680 return !!ip->i_lock.mr_writer;
681 return rwsem_is_locked(&ip->i_lock.mr_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000682 }
683
Christoph Hellwigf9369722010-06-03 16:22:29 +1000684 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
685 if (!(lock_flags & XFS_IOLOCK_SHARED))
686 return !!ip->i_iolock.mr_writer;
687 return rwsem_is_locked(&ip->i_iolock.mr_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000688 }
689
Christoph Hellwigf9369722010-06-03 16:22:29 +1000690 ASSERT(0);
691 return 0;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000692}
693#endif