blob: 784a803383ec01d670366fc6a334dcc9442176ca [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_btree.h"
34#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_quota.h"
36#include "xfs_utils.h"
David Chinner783a2f62008-10-30 17:39:58 +110037#include "xfs_trans_priv.h"
38#include "xfs_inode_item.h"
Christoph Hellwig24f211b2008-11-28 14:23:42 +110039#include "xfs_bmap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000040#include "xfs_trace.h"
Christoph Hellwig24f211b2008-11-28 14:23:42 +110041
42
43/*
44 * Allocate and initialise an xfs_inode.
45 */
46STATIC struct xfs_inode *
47xfs_inode_alloc(
48 struct xfs_mount *mp,
49 xfs_ino_t ino)
50{
51 struct xfs_inode *ip;
52
53 /*
54 * if this didn't occur in transactions, we could use
55 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
56 * code up to do this anyway.
57 */
58 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
59 if (!ip)
60 return NULL;
Christoph Hellwig54e34622009-08-07 14:38:25 -030061 if (inode_init_always(mp->m_super, VFS_I(ip))) {
62 kmem_zone_free(xfs_inode_zone, ip);
63 return NULL;
64 }
Christoph Hellwig24f211b2008-11-28 14:23:42 +110065
Christoph Hellwig24f211b2008-11-28 14:23:42 +110066 ASSERT(atomic_read(&ip->i_pincount) == 0);
67 ASSERT(!spin_is_locked(&ip->i_flags_lock));
Christoph Hellwig474fce02011-12-18 20:00:09 +000068 ASSERT(!xfs_isiflocked(ip));
Dave Chinner1a3e8f32010-12-17 17:29:43 +110069 ASSERT(ip->i_ino == 0);
Christoph Hellwig033da482009-10-19 04:05:26 +000070
71 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
Christoph Hellwig24f211b2008-11-28 14:23:42 +110072
Christoph Hellwig24f211b2008-11-28 14:23:42 +110073 /* initialise the xfs inode */
74 ip->i_ino = ino;
75 ip->i_mount = mp;
76 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
77 ip->i_afp = NULL;
78 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
79 ip->i_flags = 0;
Christoph Hellwig24f211b2008-11-28 14:23:42 +110080 ip->i_delayed_blks = 0;
81 memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
Christoph Hellwig24f211b2008-11-28 14:23:42 +110082
Christoph Hellwig24f211b2008-11-28 14:23:42 +110083 return ip;
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110086STATIC void
87xfs_inode_free_callback(
88 struct rcu_head *head)
89{
90 struct inode *inode = container_of(head, struct inode, i_rcu);
91 struct xfs_inode *ip = XFS_I(inode);
92
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110093 kmem_zone_free(xfs_inode_zone, ip);
94}
95
Dave Chinner2f11fea2010-07-20 17:53:25 +100096void
Christoph Hellwigb36ec042009-08-07 14:38:34 -030097xfs_inode_free(
98 struct xfs_inode *ip)
99{
100 switch (ip->i_d.di_mode & S_IFMT) {
101 case S_IFREG:
102 case S_IFDIR:
103 case S_IFLNK:
104 xfs_idestroy_fork(ip, XFS_DATA_FORK);
105 break;
106 }
107
108 if (ip->i_afp)
109 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
110
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300111 if (ip->i_itemp) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +1000112 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300113 xfs_inode_item_destroy(ip);
114 ip->i_itemp = NULL;
115 }
116
117 /* asserts to verify all state is correct here */
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300118 ASSERT(atomic_read(&ip->i_pincount) == 0);
119 ASSERT(!spin_is_locked(&ip->i_flags_lock));
Christoph Hellwig474fce02011-12-18 20:00:09 +0000120 ASSERT(!xfs_isiflocked(ip));
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300121
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100122 /*
123 * Because we use RCU freeing we need to ensure the inode always
124 * appears to be reclaimed with an invalid inode number when in the
125 * free state. The ip->i_flags_lock provides the barrier against lookup
126 * races.
127 */
128 spin_lock(&ip->i_flags_lock);
129 ip->i_flags = XFS_IRECLAIM;
130 ip->i_ino = 0;
131 spin_unlock(&ip->i_flags_lock);
Alex Elder92f1c002011-01-10 21:35:55 -0600132
133 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
David Chinner6441e542008-10-30 17:21:19 +1100137 * Check the validity of the inode we just found it the cache
138 */
139static int
140xfs_iget_cache_hit(
David Chinner6441e542008-10-30 17:21:19 +1100141 struct xfs_perag *pag,
142 struct xfs_inode *ip,
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100143 xfs_ino_t ino,
David Chinner6441e542008-10-30 17:21:19 +1100144 int flags,
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100145 int lock_flags) __releases(RCU)
David Chinner6441e542008-10-30 17:21:19 +1100146{
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400147 struct inode *inode = VFS_I(ip);
David Chinner6441e542008-10-30 17:21:19 +1100148 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400149 int error;
150
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100151 /*
152 * check for re-use of an inode within an RCU grace period due to the
153 * radix tree nodes not being updated yet. We monitor for this by
154 * setting the inode number to zero before freeing the inode structure.
155 * If the inode has been reallocated and set up, then the inode number
156 * will not match, so check for that, too.
157 */
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400158 spin_lock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100159 if (ip->i_ino != ino) {
160 trace_xfs_iget_skip(ip);
161 XFS_STATS_INC(xs_ig_frecycle);
162 error = EAGAIN;
163 goto out_error;
164 }
165
David Chinner6441e542008-10-30 17:21:19 +1100166
167 /*
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400168 * If we are racing with another cache hit that is currently
169 * instantiating this inode or currently recycling it out of
170 * reclaimabe state, wait for the initialisation to complete
171 * before continuing.
172 *
173 * XXX(hch): eventually we should do something equivalent to
174 * wait_on_inode to wait for these flags to be cleared
175 * instead of polling for it.
David Chinner6441e542008-10-30 17:21:19 +1100176 */
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400177 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000178 trace_xfs_iget_skip(ip);
David Chinner6441e542008-10-30 17:21:19 +1100179 XFS_STATS_INC(xs_ig_frecycle);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400180 error = EAGAIN;
David Chinner6441e542008-10-30 17:21:19 +1100181 goto out_error;
182 }
183
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400184 /*
185 * If lookup is racing with unlink return an error immediately.
186 */
187 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
188 error = ENOENT;
189 goto out_error;
190 }
David Chinner6441e542008-10-30 17:21:19 +1100191
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400192 /*
193 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
194 * Need to carefully get it back into useable state.
195 */
196 if (ip->i_flags & XFS_IRECLAIMABLE) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000197 trace_xfs_iget_reclaim(ip);
David Chinner6441e542008-10-30 17:21:19 +1100198
David Chinnerbf904242008-10-30 17:36:14 +1100199 /*
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000200 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
201 * from stomping over us while we recycle the inode. We can't
202 * clear the radix tree reclaimable tag yet as it requires
203 * pag_ici_lock to be held exclusive.
David Chinnerbf904242008-10-30 17:36:14 +1100204 */
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000205 ip->i_flags |= XFS_IRECLAIM;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400206
207 spin_unlock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100208 rcu_read_unlock();
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400209
210 error = -inode_init_always(mp->m_super, inode);
211 if (error) {
212 /*
213 * Re-initializing the inode failed, and we are in deep
214 * trouble. Try to re-add it to the reclaim list.
215 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100216 rcu_read_lock();
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400217 spin_lock(&ip->i_flags_lock);
218
Dave Chinner778e24b2011-06-23 01:34:59 +0000219 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
220 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000221 trace_xfs_iget_reclaim_fail(ip);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400222 goto out_error;
223 }
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000224
Dave Chinner1a427ab2010-12-16 17:08:41 +1100225 spin_lock(&pag->pag_ici_lock);
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000226 spin_lock(&ip->i_flags_lock);
Dave Chinner778e24b2011-06-23 01:34:59 +0000227
228 /*
229 * Clear the per-lifetime state in the inode as we are now
230 * effectively a new inode and need to return to the initial
231 * state before reuse occurs.
232 */
233 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000234 ip->i_flags |= XFS_INEW;
235 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100236 inode->i_state = I_NEW;
Dave Chinnerdcfcf202010-12-23 11:57:13 +1100237
238 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
239 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
Dave Chinnerdcfcf202010-12-23 11:57:13 +1100240
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000241 spin_unlock(&ip->i_flags_lock);
Dave Chinner1a427ab2010-12-16 17:08:41 +1100242 spin_unlock(&pag->pag_ici_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400243 } else {
244 /* If the VFS inode is being torn down, pause and try again. */
245 if (!igrab(inode)) {
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000246 trace_xfs_iget_skip(ip);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400247 error = EAGAIN;
David Chinnerbf904242008-10-30 17:36:14 +1100248 goto out_error;
249 }
David Chinner6bfb3d02008-10-30 18:32:43 +1100250
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400251 /* We've got a live one. */
252 spin_unlock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100253 rcu_read_unlock();
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000254 trace_xfs_iget_hit(ip);
David Chinner6441e542008-10-30 17:21:19 +1100255 }
256
David Chinner6441e542008-10-30 17:21:19 +1100257 if (lock_flags != 0)
258 xfs_ilock(ip, lock_flags);
259
Dave Chinner5132ba82012-03-22 05:15:10 +0000260 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
David Chinner6441e542008-10-30 17:21:19 +1100261 XFS_STATS_INC(xs_ig_found);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000262
David Chinner6441e542008-10-30 17:21:19 +1100263 return 0;
264
265out_error:
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400266 spin_unlock(&ip->i_flags_lock);
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100267 rcu_read_unlock();
David Chinner6441e542008-10-30 17:21:19 +1100268 return error;
269}
270
271
272static int
273xfs_iget_cache_miss(
274 struct xfs_mount *mp,
275 struct xfs_perag *pag,
276 xfs_trans_t *tp,
277 xfs_ino_t ino,
278 struct xfs_inode **ipp,
David Chinner6441e542008-10-30 17:21:19 +1100279 int flags,
Christoph Hellwig0c3dc2b2009-11-14 16:17:23 +0000280 int lock_flags)
David Chinner6441e542008-10-30 17:21:19 +1100281{
282 struct xfs_inode *ip;
283 int error;
David Chinner6441e542008-10-30 17:21:19 +1100284 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
Dave Chinner5132ba82012-03-22 05:15:10 +0000285 int iflags;
David Chinner6441e542008-10-30 17:21:19 +1100286
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100287 ip = xfs_inode_alloc(mp, ino);
288 if (!ip)
289 return ENOMEM;
290
Dave Chinner7b6259e2010-06-24 11:35:17 +1000291 error = xfs_iread(mp, tp, ip, flags);
David Chinner6441e542008-10-30 17:21:19 +1100292 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100293 goto out_destroy;
David Chinner6441e542008-10-30 17:21:19 +1100294
Christoph Hellwigd2e078c2010-06-24 11:50:22 +1000295 trace_xfs_iget_miss(ip);
David Chinner6441e542008-10-30 17:21:19 +1100296
297 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
298 error = ENOENT;
299 goto out_destroy;
300 }
301
302 /*
303 * Preload the radix tree so we can insert safely under the
David Chinner56e73ec2008-10-30 17:55:27 +1100304 * write spinlock. Note that we cannot sleep inside the preload
Peter Watkins3ba31602012-05-07 16:11:37 -0400305 * region. Since we can be called from transaction context, don't
306 * recurse into the file system.
David Chinner6441e542008-10-30 17:21:19 +1100307 */
Peter Watkins3ba31602012-05-07 16:11:37 -0400308 if (radix_tree_preload(GFP_NOFS)) {
David Chinner6441e542008-10-30 17:21:19 +1100309 error = EAGAIN;
Christoph Hellwiged93ec32009-03-03 14:48:35 -0500310 goto out_destroy;
311 }
312
313 /*
314 * Because the inode hasn't been added to the radix-tree yet it can't
315 * be found by another thread, so we can do the non-sleeping lock here.
316 */
317 if (lock_flags) {
318 if (!xfs_ilock_nowait(ip, lock_flags))
319 BUG();
David Chinner6441e542008-10-30 17:21:19 +1100320 }
321
Dave Chinnerf30d5002012-03-07 04:50:25 +0000322 /*
323 * These values must be set before inserting the inode into the radix
324 * tree as the moment it is inserted a concurrent lookup (allowed by the
325 * RCU locking mechanism) can find it and that lookup must see that this
326 * is an inode currently under construction (i.e. that XFS_INEW is set).
327 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
328 * memory barrier that ensures this detection works correctly at lookup
329 * time.
330 */
Dave Chinner5132ba82012-03-22 05:15:10 +0000331 iflags = XFS_INEW;
332 if (flags & XFS_IGET_DONTCACHE)
333 iflags |= XFS_IDONTCACHE;
Dave Chinnerf30d5002012-03-07 04:50:25 +0000334 ip->i_udquot = ip->i_gdquot = NULL;
Dave Chinner5132ba82012-03-22 05:15:10 +0000335 xfs_iflags_set(ip, iflags);
David Chinner6441e542008-10-30 17:21:19 +1100336
337 /* insert the new inode */
Dave Chinnerf30d5002012-03-07 04:50:25 +0000338 spin_lock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100339 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
340 if (unlikely(error)) {
341 WARN_ON(error != -EEXIST);
342 XFS_STATS_INC(xs_ig_dup);
343 error = EAGAIN;
David Chinner56e73ec2008-10-30 17:55:27 +1100344 goto out_preload_end;
David Chinner6441e542008-10-30 17:21:19 +1100345 }
Dave Chinner1a427ab2010-12-16 17:08:41 +1100346 spin_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100347 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:
Dave Chinner1a427ab2010-12-16 17:08:41 +1100353 spin_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100354 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
Alex Elderad637a12012-02-16 22:01:00 +0000399 /*
400 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
401 * doesn't get freed while it's being referenced during a
402 * radix tree traversal here. It assumes this function
403 * aqcuires only the ILOCK (and therefore it has no need to
404 * involve the IOLOCK in this synchronization).
405 */
406 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
407
Christoph Hellwigd2767342010-10-06 18:31:23 +0000408 /* reject inode numbers outside existing AGs */
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100409 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
David Chinnerda353b02007-08-28 14:00:13 +1000410 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
David Chinnerda353b02007-08-28 14:00:13 +1000412 /* get the perag structure and ensure that it's inode capable */
Dave Chinner5017e972010-01-11 11:47:40 +0000413 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
David Chinnerda353b02007-08-28 14:00:13 +1000414 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416again:
David Chinner6441e542008-10-30 17:21:19 +1100417 error = 0;
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100418 rcu_read_lock();
David Chinnerda353b02007-08-28 14:00:13 +1000419 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
David Chinner6441e542008-10-30 17:21:19 +1100421 if (ip) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100422 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100423 if (error)
424 goto out_error_or_again;
425 } else {
Dave Chinner1a3e8f32010-12-17 17:29:43 +1100426 rcu_read_unlock();
David Chinner6441e542008-10-30 17:21:19 +1100427 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000428
Dave Chinner7b6259e2010-06-24 11:35:17 +1000429 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
David Chinner6441e542008-10-30 17:21:19 +1100430 flags, lock_flags);
431 if (error)
432 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Dave Chinner5017e972010-01-11 11:47:40 +0000434 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *ipp = ip;
437
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000438 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * If we have a real type for an on-disk inode, we can set ops(&unlock)
440 * now. If it's a new inode being created, xfs_ialloc will handle it.
441 */
David Chinnerbf904242008-10-30 17:36:14 +1100442 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000443 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100445
446out_error_or_again:
447 if (error == EAGAIN) {
448 delay(1);
449 goto again;
450 }
Dave Chinner5017e972010-01-11 11:47:40 +0000451 xfs_perag_put(pag);
David Chinner6441e542008-10-30 17:21:19 +1100452 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * This is a wrapper routine around the xfs_ilock() routine
457 * used to centralize some grungy code. It is used in places
458 * that wish to lock the inode solely for reading the extents.
459 * The reason these places can't just call xfs_ilock(SHARED)
460 * is that the inode lock also guards to bringing in of the
461 * extents from disk for a file in b-tree format. If the inode
462 * is in b-tree format, then we need to lock the inode exclusively
463 * until the extents are read in. Locking it exclusively all
464 * the time would limit our parallelism unnecessarily, though.
465 * What we do instead is check to see if the extents have been
466 * read in yet, and only lock the inode exclusively if they
467 * have not.
468 *
469 * The function returns a value which should be given to the
470 * corresponding xfs_iunlock_map_shared(). This value is
471 * the mode in which the lock was actually taken.
472 */
473uint
474xfs_ilock_map_shared(
475 xfs_inode_t *ip)
476{
477 uint lock_mode;
478
479 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
480 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
481 lock_mode = XFS_ILOCK_EXCL;
482 } else {
483 lock_mode = XFS_ILOCK_SHARED;
484 }
485
486 xfs_ilock(ip, lock_mode);
487
488 return lock_mode;
489}
490
491/*
492 * This is simply the unlock routine to go with xfs_ilock_map_shared().
493 * All it does is call xfs_iunlock() with the given lock_mode.
494 */
495void
496xfs_iunlock_map_shared(
497 xfs_inode_t *ip,
498 unsigned int lock_mode)
499{
500 xfs_iunlock(ip, lock_mode);
501}
502
503/*
504 * The xfs inode contains 2 locks: a multi-reader lock called the
505 * i_iolock and a multi-reader lock called the i_lock. This routine
506 * allows either or both of the locks to be obtained.
507 *
508 * The 2 locks should always be ordered so that the IO lock is
509 * obtained first in order to prevent deadlock.
510 *
511 * ip -- the inode being locked
512 * lock_flags -- this parameter indicates the inode's locks
513 * to be locked. It can be:
514 * XFS_IOLOCK_SHARED,
515 * XFS_IOLOCK_EXCL,
516 * XFS_ILOCK_SHARED,
517 * XFS_ILOCK_EXCL,
518 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
519 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
520 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
521 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
522 */
523void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000524xfs_ilock(
525 xfs_inode_t *ip,
526 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 /*
529 * You can't set both SHARED and EXCL for the same lock,
530 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
531 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
532 */
533 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
534 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
535 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
536 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000537 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000539 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000540 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000541 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000542 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000543
544 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000545 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000546 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000547 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000548
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000549 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/*
553 * This is just like xfs_ilock(), except that the caller
554 * is guaranteed not to sleep. It returns 1 if it gets
555 * the requested locks and 0 otherwise. If the IO lock is
556 * obtained but the inode lock cannot be, then the IO lock
557 * is dropped before returning.
558 *
559 * ip -- the inode being locked
560 * lock_flags -- this parameter indicates the inode's locks to be
561 * to be locked. See the comment for xfs_ilock() for a list
562 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
564int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000565xfs_ilock_nowait(
566 xfs_inode_t *ip,
567 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * You can't set both SHARED and EXCL for the same lock,
571 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
572 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
573 */
574 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
575 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
576 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
577 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000578 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000581 if (!mrtryupdate(&ip->i_iolock))
582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000584 if (!mrtryaccess(&ip->i_iolock))
585 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000588 if (!mrtryupdate(&ip->i_lock))
589 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000591 if (!mrtryaccess(&ip->i_lock))
592 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000594 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000596
597 out_undo_iolock:
598 if (lock_flags & XFS_IOLOCK_EXCL)
599 mrunlock_excl(&ip->i_iolock);
600 else if (lock_flags & XFS_IOLOCK_SHARED)
601 mrunlock_shared(&ip->i_iolock);
602 out:
603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * xfs_iunlock() is used to drop the inode locks acquired with
608 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
609 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
610 * that we know which locks to drop.
611 *
612 * ip -- the inode being unlocked
613 * lock_flags -- this parameter indicates the inode's locks to be
614 * to be unlocked. See the comment for xfs_ilock() for a list
615 * of valid values for this parameter.
616 *
617 */
618void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000619xfs_iunlock(
620 xfs_inode_t *ip,
621 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 /*
624 * You can't set both SHARED and EXCL for the same lock,
625 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
626 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
627 */
628 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
629 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
630 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
631 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Christoph Hellwig5b03ff12012-02-20 02:31:22 +0000632 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 ASSERT(lock_flags != 0);
634
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000635 if (lock_flags & XFS_IOLOCK_EXCL)
636 mrunlock_excl(&ip->i_iolock);
637 else if (lock_flags & XFS_IOLOCK_SHARED)
638 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000640 if (lock_flags & XFS_ILOCK_EXCL)
641 mrunlock_excl(&ip->i_lock);
642 else if (lock_flags & XFS_ILOCK_SHARED)
643 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000645 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
649 * give up write locks. the i/o lock cannot be held nested
650 * if it is being demoted.
651 */
652void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000653xfs_ilock_demote(
654 xfs_inode_t *ip,
655 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
658 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
659
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000660 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000662 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 mrdemote(&ip->i_iolock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000664
665 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000668#ifdef DEBUG
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000669int
670xfs_isilocked(
671 xfs_inode_t *ip,
672 uint lock_flags)
673{
Christoph Hellwigf9369722010-06-03 16:22:29 +1000674 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
675 if (!(lock_flags & XFS_ILOCK_SHARED))
676 return !!ip->i_lock.mr_writer;
677 return rwsem_is_locked(&ip->i_lock.mr_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000678 }
679
Christoph Hellwigf9369722010-06-03 16:22:29 +1000680 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
681 if (!(lock_flags & XFS_IOLOCK_SHARED))
682 return !!ip->i_iolock.mr_writer;
683 return rwsem_is_locked(&ip->i_iolock.mr_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000684 }
685
Christoph Hellwigf9369722010-06-03 16:22:29 +1000686 ASSERT(0);
687 return 0;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000688}
689#endif
Christoph Hellwig474fce02011-12-18 20:00:09 +0000690
691void
692__xfs_iflock(
693 struct xfs_inode *ip)
694{
695 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
696 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
697
698 do {
699 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
700 if (xfs_isiflocked(ip))
701 io_schedule();
702 } while (!xfs_iflock_nowait(ip));
703
704 finish_wait(wq, &wait.wait);
705}