blob: 80e526489be5d7ee3dc415ec592fe2807a879a01 [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_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_btree.h"
39#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_quota.h"
41#include "xfs_utils.h"
David Chinner783a2f62008-10-30 17:39:58 +110042#include "xfs_trans_priv.h"
43#include "xfs_inode_item.h"
Christoph Hellwig24f211b2008-11-28 14:23:42 +110044#include "xfs_bmap.h"
45#include "xfs_btree_trace.h"
46#include "xfs_dir2_trace.h"
47
48
49/*
50 * Allocate and initialise an xfs_inode.
51 */
52STATIC struct xfs_inode *
53xfs_inode_alloc(
54 struct xfs_mount *mp,
55 xfs_ino_t ino)
56{
57 struct xfs_inode *ip;
58
59 /*
60 * if this didn't occur in transactions, we could use
61 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
62 * code up to do this anyway.
63 */
64 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
65 if (!ip)
66 return NULL;
Christoph Hellwig54e34622009-08-07 14:38:25 -030067 if (inode_init_always(mp->m_super, VFS_I(ip))) {
68 kmem_zone_free(xfs_inode_zone, ip);
69 return NULL;
70 }
Christoph Hellwig24f211b2008-11-28 14:23:42 +110071
72 ASSERT(atomic_read(&ip->i_iocount) == 0);
73 ASSERT(atomic_read(&ip->i_pincount) == 0);
74 ASSERT(!spin_is_locked(&ip->i_flags_lock));
75 ASSERT(completion_done(&ip->i_flush));
76
Christoph Hellwig24f211b2008-11-28 14:23:42 +110077 /* initialise the xfs inode */
78 ip->i_ino = ino;
79 ip->i_mount = mp;
80 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
81 ip->i_afp = NULL;
82 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
83 ip->i_flags = 0;
84 ip->i_update_core = 0;
Christoph Hellwig24f211b2008-11-28 14:23:42 +110085 ip->i_delayed_blks = 0;
86 memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
87 ip->i_size = 0;
88 ip->i_new_size = 0;
89
90 /*
91 * Initialize inode's trace buffers.
92 */
93#ifdef XFS_INODE_TRACE
94 ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS);
95#endif
96#ifdef XFS_BMAP_TRACE
97 ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS);
98#endif
99#ifdef XFS_BTREE_TRACE
100 ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS);
101#endif
102#ifdef XFS_RW_TRACE
103 ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS);
104#endif
105#ifdef XFS_ILOCK_TRACE
106 ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS);
107#endif
108#ifdef XFS_DIR2_TRACE
109 ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS);
110#endif
Dave Chinner705db3f2009-04-06 18:40:17 +0200111
112 /* prevent anyone from using this yet */
113 VFS_I(ip)->i_state = I_NEW|I_LOCK;
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100114
115 return ip;
116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300118STATIC void
119xfs_inode_free(
120 struct xfs_inode *ip)
121{
122 switch (ip->i_d.di_mode & S_IFMT) {
123 case S_IFREG:
124 case S_IFDIR:
125 case S_IFLNK:
126 xfs_idestroy_fork(ip, XFS_DATA_FORK);
127 break;
128 }
129
130 if (ip->i_afp)
131 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
132
133#ifdef XFS_INODE_TRACE
134 ktrace_free(ip->i_trace);
135#endif
136#ifdef XFS_BMAP_TRACE
137 ktrace_free(ip->i_xtrace);
138#endif
139#ifdef XFS_BTREE_TRACE
140 ktrace_free(ip->i_btrace);
141#endif
142#ifdef XFS_RW_TRACE
143 ktrace_free(ip->i_rwtrace);
144#endif
145#ifdef XFS_ILOCK_TRACE
146 ktrace_free(ip->i_lock_trace);
147#endif
148#ifdef XFS_DIR2_TRACE
149 ktrace_free(ip->i_dir_trace);
150#endif
151
152 if (ip->i_itemp) {
153 /*
154 * Only if we are shutting down the fs will we see an
155 * inode still in the AIL. If it is there, we should remove
156 * it to prevent a use-after-free from occurring.
157 */
158 xfs_log_item_t *lip = &ip->i_itemp->ili_item;
159 struct xfs_ail *ailp = lip->li_ailp;
160
161 ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
162 XFS_FORCED_SHUTDOWN(ip->i_mount));
163 if (lip->li_flags & XFS_LI_IN_AIL) {
164 spin_lock(&ailp->xa_lock);
165 if (lip->li_flags & XFS_LI_IN_AIL)
166 xfs_trans_ail_delete(ailp, lip);
167 else
168 spin_unlock(&ailp->xa_lock);
169 }
170 xfs_inode_item_destroy(ip);
171 ip->i_itemp = NULL;
172 }
173
174 /* asserts to verify all state is correct here */
175 ASSERT(atomic_read(&ip->i_iocount) == 0);
176 ASSERT(atomic_read(&ip->i_pincount) == 0);
177 ASSERT(!spin_is_locked(&ip->i_flags_lock));
178 ASSERT(completion_done(&ip->i_flush));
179
180 kmem_zone_free(xfs_inode_zone, ip);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
David Chinner6441e542008-10-30 17:21:19 +1100184 * Check the validity of the inode we just found it the cache
185 */
186static int
187xfs_iget_cache_hit(
David Chinner6441e542008-10-30 17:21:19 +1100188 struct xfs_perag *pag,
189 struct xfs_inode *ip,
190 int flags,
191 int lock_flags) __releases(pag->pag_ici_lock)
192{
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400193 struct inode *inode = VFS_I(ip);
David Chinner6441e542008-10-30 17:21:19 +1100194 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400195 int error;
196
197 spin_lock(&ip->i_flags_lock);
David Chinner6441e542008-10-30 17:21:19 +1100198
199 /*
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400200 * If we are racing with another cache hit that is currently
201 * instantiating this inode or currently recycling it out of
202 * reclaimabe state, wait for the initialisation to complete
203 * before continuing.
204 *
205 * XXX(hch): eventually we should do something equivalent to
206 * wait_on_inode to wait for these flags to be cleared
207 * instead of polling for it.
David Chinner6441e542008-10-30 17:21:19 +1100208 */
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400209 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
David Chinner6441e542008-10-30 17:21:19 +1100210 XFS_STATS_INC(xs_ig_frecycle);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400211 error = EAGAIN;
David Chinner6441e542008-10-30 17:21:19 +1100212 goto out_error;
213 }
214
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400215 /*
216 * If lookup is racing with unlink return an error immediately.
217 */
218 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
219 error = ENOENT;
220 goto out_error;
221 }
David Chinner6441e542008-10-30 17:21:19 +1100222
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400223 /*
224 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
225 * Need to carefully get it back into useable state.
226 */
227 if (ip->i_flags & XFS_IRECLAIMABLE) {
David Chinner6441e542008-10-30 17:21:19 +1100228 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
229
David Chinnerbf904242008-10-30 17:36:14 +1100230 /*
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400231 * We need to set XFS_INEW atomically with clearing the
232 * reclaimable tag so that we do have an indicator of the
233 * inode still being initialized.
David Chinnerbf904242008-10-30 17:36:14 +1100234 */
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400235 ip->i_flags |= XFS_INEW;
236 ip->i_flags &= ~XFS_IRECLAIMABLE;
237 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
238
239 spin_unlock(&ip->i_flags_lock);
240 read_unlock(&pag->pag_ici_lock);
241
242 error = -inode_init_always(mp->m_super, inode);
243 if (error) {
244 /*
245 * Re-initializing the inode failed, and we are in deep
246 * trouble. Try to re-add it to the reclaim list.
247 */
248 read_lock(&pag->pag_ici_lock);
249 spin_lock(&ip->i_flags_lock);
250
251 ip->i_flags &= ~XFS_INEW;
252 ip->i_flags |= XFS_IRECLAIMABLE;
253 __xfs_inode_set_reclaim_tag(pag, ip);
254 goto out_error;
255 }
256 inode->i_state = I_LOCK|I_NEW;
257 } else {
258 /* If the VFS inode is being torn down, pause and try again. */
259 if (!igrab(inode)) {
260 error = EAGAIN;
David Chinnerbf904242008-10-30 17:36:14 +1100261 goto out_error;
262 }
David Chinner6bfb3d02008-10-30 18:32:43 +1100263
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400264 /* We've got a live one. */
265 spin_unlock(&ip->i_flags_lock);
266 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100267 }
268
David Chinner6441e542008-10-30 17:21:19 +1100269 if (lock_flags != 0)
270 xfs_ilock(ip, lock_flags);
271
272 xfs_iflags_clear(ip, XFS_ISTALE);
273 xfs_itrace_exit_tag(ip, "xfs_iget.found");
274 XFS_STATS_INC(xs_ig_found);
275 return 0;
276
277out_error:
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400278 spin_unlock(&ip->i_flags_lock);
David Chinner6441e542008-10-30 17:21:19 +1100279 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100280 return error;
281}
282
283
284static int
285xfs_iget_cache_miss(
286 struct xfs_mount *mp,
287 struct xfs_perag *pag,
288 xfs_trans_t *tp,
289 xfs_ino_t ino,
290 struct xfs_inode **ipp,
291 xfs_daddr_t bno,
292 int flags,
293 int lock_flags) __releases(pag->pag_ici_lock)
294{
295 struct xfs_inode *ip;
296 int error;
297 unsigned long first_index, mask;
298 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
299
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100300 ip = xfs_inode_alloc(mp, ino);
301 if (!ip)
302 return ENOMEM;
303
304 error = xfs_iread(mp, tp, ip, bno, flags);
David Chinner6441e542008-10-30 17:21:19 +1100305 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100306 goto out_destroy;
David Chinner6441e542008-10-30 17:21:19 +1100307
308 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
309
310 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
311 error = ENOENT;
312 goto out_destroy;
313 }
314
315 /*
316 * Preload the radix tree so we can insert safely under the
David Chinner56e73ec2008-10-30 17:55:27 +1100317 * write spinlock. Note that we cannot sleep inside the preload
318 * region.
David Chinner6441e542008-10-30 17:21:19 +1100319 */
320 if (radix_tree_preload(GFP_KERNEL)) {
321 error = EAGAIN;
Christoph Hellwiged93ec32009-03-03 14:48:35 -0500322 goto out_destroy;
323 }
324
325 /*
326 * Because the inode hasn't been added to the radix-tree yet it can't
327 * be found by another thread, so we can do the non-sleeping lock here.
328 */
329 if (lock_flags) {
330 if (!xfs_ilock_nowait(ip, lock_flags))
331 BUG();
David Chinner6441e542008-10-30 17:21:19 +1100332 }
333
David Chinner6441e542008-10-30 17:21:19 +1100334 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
335 first_index = agino & mask;
336 write_lock(&pag->pag_ici_lock);
337
338 /* insert the new inode */
339 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 }
346
347 /* These values _must_ be set before releasing the radix tree lock! */
348 ip->i_udquot = ip->i_gdquot = NULL;
349 xfs_iflags_set(ip, XFS_INEW);
350
351 write_unlock(&pag->pag_ici_lock);
352 radix_tree_preload_end();
353 *ipp = ip;
354 return 0;
355
David Chinner56e73ec2008-10-30 17:55:27 +1100356out_preload_end:
David Chinner6441e542008-10-30 17:21:19 +1100357 write_unlock(&pag->pag_ici_lock);
358 radix_tree_preload_end();
David Chinner56e73ec2008-10-30 17:55:27 +1100359 if (lock_flags)
360 xfs_iunlock(ip, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100361out_destroy:
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300362 __destroy_inode(VFS_I(ip));
363 xfs_inode_free(ip);
David Chinner6441e542008-10-30 17:21:19 +1100364 return error;
365}
366
367/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000369 * The inode is looked up in the cache held in each AG.
David Chinnerbf904242008-10-30 17:36:14 +1100370 * If the inode is found in the cache, initialise the vfs inode
371 * if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *
David Chinnerda353b02007-08-28 14:00:13 +1000373 * If it is not in core, read it in from the file system's device,
David Chinnerbf904242008-10-30 17:36:14 +1100374 * add it to the cache and initialise the vfs inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
376 * The inode is locked according to the value of the lock_flags parameter.
377 * This flag parameter indicates how and if the inode's IO lock and inode lock
378 * should be taken.
379 *
380 * mp -- the mount point structure for the current file system. It points
381 * to the inode hash table.
382 * tp -- a pointer to the current transaction if there is one. This is
383 * simply passed through to the xfs_iread() call.
384 * ino -- the number of the inode desired. This is the unique identifier
385 * within the file system for the inode being requested.
386 * lock_flags -- flags indicating how to lock the inode. See the comment
387 * for xfs_ilock() for a list of valid values.
388 * bno -- the block number starting the buffer containing the inode,
389 * if known (as by bulkstat), else 0.
390 */
David Chinnerbf904242008-10-30 17:36:14 +1100391int
392xfs_iget(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 xfs_mount_t *mp,
394 xfs_trans_t *tp,
395 xfs_ino_t ino,
396 uint flags,
397 uint lock_flags,
398 xfs_inode_t **ipp,
399 xfs_daddr_t bno)
400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000403 xfs_perag_t *pag;
404 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
David Chinnerda353b02007-08-28 14:00:13 +1000406 /* the radix tree exists only in inode capable AGs */
407 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
408 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
David Chinnerda353b02007-08-28 14:00:13 +1000410 /* get the perag structure and ensure that it's inode capable */
411 pag = xfs_get_perag(mp, ino);
412 if (!pag->pagi_inodeok)
413 return EINVAL;
414 ASSERT(pag->pag_ici_init);
415 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417again:
David Chinner6441e542008-10-30 17:21:19 +1100418 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000419 read_lock(&pag->pag_ici_lock);
420 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
David Chinner6441e542008-10-30 17:21:19 +1100422 if (ip) {
David Chinnerbf904242008-10-30 17:36:14 +1100423 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100424 if (error)
425 goto out_error_or_again;
426 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000427 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100428 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000429
David Chinner6441e542008-10-30 17:21:19 +1100430 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
431 flags, lock_flags);
432 if (error)
433 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
David Chinnerda353b02007-08-28 14:00:13 +1000435 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *ipp = ip;
438
David Chinnerbf904242008-10-30 17:36:14 +1100439 ASSERT(ip->i_df.if_ext_max ==
440 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000441 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * If we have a real type for an on-disk inode, we can set ops(&unlock)
443 * now. If it's a new inode being created, xfs_ialloc will handle it.
444 */
David Chinnerbf904242008-10-30 17:36:14 +1100445 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000446 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100448
449out_error_or_again:
450 if (error == EAGAIN) {
451 delay(1);
452 goto again;
453 }
454 xfs_put_perag(mp, pag);
455 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * Decrement reference count of an inode structure and unlock it.
460 *
461 * ip -- the inode being released
462 * lock_flags -- this parameter indicates the inode's locks to be
463 * to be released. See the comment on xfs_iunlock() for a list
464 * of valid values.
465 */
466void
467xfs_iput(xfs_inode_t *ip,
468 uint lock_flags)
469{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100470 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000472 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475/*
476 * Special iput for brand-new inodes that are still locked
477 */
478void
David Chinner01651642008-08-13 15:45:15 +1000479xfs_iput_new(
480 xfs_inode_t *ip,
481 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David Chinner01651642008-08-13 15:45:15 +1000483 struct inode *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100485 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100488 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Christoph Hellwig10090be2007-10-11 18:11:03 +1000489 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 if (inode->i_state & I_NEW)
492 unlock_new_inode(inode);
493 if (lock_flags)
494 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000495 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100499 * This is called free all the memory associated with an inode.
500 * It must free the inode itself and any buffers allocated for
501 * if_extents/if_data and if_broot. It must also free the lock
502 * associated with the inode.
503 *
504 * Note: because we don't initialise everything on reallocation out
505 * of the zone, we must ensure we nullify everything correctly before
506 * freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
508void
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100509xfs_ireclaim(
510 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100512 struct xfs_mount *mp = ip->i_mount;
513 struct xfs_perag *pag;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 XFS_STATS_INC(xs_ig_reclaims);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100518 * Remove the inode from the per-AG radix tree. It doesn't matter
519 * if it was never added to it because radix_tree_delete can deal
520 * with that case just fine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100522 pag = xfs_get_perag(mp, ip->i_ino);
David Chinnerda353b02007-08-28 14:00:13 +1000523 write_lock(&pag->pag_ici_lock);
524 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
525 write_unlock(&pag->pag_ici_lock);
526 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100528 /*
529 * Here we do an (almost) spurious inode lock in order to coordinate
530 * with inode cache radix tree lookups. This is because the lookup
531 * can reference the inodes in the cache without taking references.
532 *
533 * We make that OK here by ensuring that we wait until the inode is
534 * unlocked after the lookup before we go ahead and free it. We get
535 * both the ilock and the iolock because the code may need to drop the
536 * ilock one but will still hold the iolock.
537 */
538 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200539 xfs_qm_dqdetach(ip);
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100540 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
541
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300542 xfs_inode_free(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/*
546 * This is a wrapper routine around the xfs_ilock() routine
547 * used to centralize some grungy code. It is used in places
548 * that wish to lock the inode solely for reading the extents.
549 * The reason these places can't just call xfs_ilock(SHARED)
550 * is that the inode lock also guards to bringing in of the
551 * extents from disk for a file in b-tree format. If the inode
552 * is in b-tree format, then we need to lock the inode exclusively
553 * until the extents are read in. Locking it exclusively all
554 * the time would limit our parallelism unnecessarily, though.
555 * What we do instead is check to see if the extents have been
556 * read in yet, and only lock the inode exclusively if they
557 * have not.
558 *
559 * The function returns a value which should be given to the
560 * corresponding xfs_iunlock_map_shared(). This value is
561 * the mode in which the lock was actually taken.
562 */
563uint
564xfs_ilock_map_shared(
565 xfs_inode_t *ip)
566{
567 uint lock_mode;
568
569 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
570 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
571 lock_mode = XFS_ILOCK_EXCL;
572 } else {
573 lock_mode = XFS_ILOCK_SHARED;
574 }
575
576 xfs_ilock(ip, lock_mode);
577
578 return lock_mode;
579}
580
581/*
582 * This is simply the unlock routine to go with xfs_ilock_map_shared().
583 * All it does is call xfs_iunlock() with the given lock_mode.
584 */
585void
586xfs_iunlock_map_shared(
587 xfs_inode_t *ip,
588 unsigned int lock_mode)
589{
590 xfs_iunlock(ip, lock_mode);
591}
592
593/*
594 * The xfs inode contains 2 locks: a multi-reader lock called the
595 * i_iolock and a multi-reader lock called the i_lock. This routine
596 * allows either or both of the locks to be obtained.
597 *
598 * The 2 locks should always be ordered so that the IO lock is
599 * obtained first in order to prevent deadlock.
600 *
601 * ip -- the inode being locked
602 * lock_flags -- this parameter indicates the inode's locks
603 * to be locked. It can be:
604 * XFS_IOLOCK_SHARED,
605 * XFS_IOLOCK_EXCL,
606 * XFS_ILOCK_SHARED,
607 * XFS_ILOCK_EXCL,
608 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
609 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
610 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
611 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
612 */
613void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000614xfs_ilock(
615 xfs_inode_t *ip,
616 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 /*
619 * You can't set both SHARED and EXCL for the same lock,
620 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
621 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
622 */
623 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
624 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
625 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
626 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000627 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000629 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000630 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000631 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000632 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000633
634 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000635 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000636 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000637 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
640}
641
642/*
643 * This is just like xfs_ilock(), except that the caller
644 * is guaranteed not to sleep. It returns 1 if it gets
645 * the requested locks and 0 otherwise. If the IO lock is
646 * obtained but the inode lock cannot be, then the IO lock
647 * is dropped before returning.
648 *
649 * ip -- the inode being locked
650 * lock_flags -- this parameter indicates the inode's locks to be
651 * to be locked. See the comment for xfs_ilock() for a list
652 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
654int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000655xfs_ilock_nowait(
656 xfs_inode_t *ip,
657 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * You can't set both SHARED and EXCL for the same lock,
661 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
662 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
663 */
664 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
665 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
666 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
667 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000668 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000671 if (!mrtryupdate(&ip->i_iolock))
672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000674 if (!mrtryaccess(&ip->i_iolock))
675 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000678 if (!mrtryupdate(&ip->i_lock))
679 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000681 if (!mrtryaccess(&ip->i_lock))
682 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
685 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000686
687 out_undo_iolock:
688 if (lock_flags & XFS_IOLOCK_EXCL)
689 mrunlock_excl(&ip->i_iolock);
690 else if (lock_flags & XFS_IOLOCK_SHARED)
691 mrunlock_shared(&ip->i_iolock);
692 out:
693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*
697 * xfs_iunlock() is used to drop the inode locks acquired with
698 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
699 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
700 * that we know which locks to drop.
701 *
702 * ip -- the inode being unlocked
703 * lock_flags -- this parameter indicates the inode's locks to be
704 * to be unlocked. See the comment for xfs_ilock() for a list
705 * of valid values for this parameter.
706 *
707 */
708void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000709xfs_iunlock(
710 xfs_inode_t *ip,
711 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 /*
714 * You can't set both SHARED and EXCL for the same lock,
715 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
716 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
717 */
718 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
719 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
720 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
721 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000722 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
723 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 ASSERT(lock_flags != 0);
725
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000726 if (lock_flags & XFS_IOLOCK_EXCL)
727 mrunlock_excl(&ip->i_iolock);
728 else if (lock_flags & XFS_IOLOCK_SHARED)
729 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000731 if (lock_flags & XFS_ILOCK_EXCL)
732 mrunlock_excl(&ip->i_lock);
733 else if (lock_flags & XFS_ILOCK_SHARED)
734 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000736 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
737 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
739 * Let the AIL know that this item has been unlocked in case
740 * it is in the AIL and anyone is waiting on it. Don't do
741 * this if the caller has asked us not to.
742 */
David Chinner783a2f62008-10-30 17:39:58 +1100743 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000744 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
747}
748
749/*
750 * give up write locks. the i/o lock cannot be held nested
751 * if it is being demoted.
752 */
753void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000754xfs_ilock_demote(
755 xfs_inode_t *ip,
756 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
759 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
760
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000761 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000763 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mrdemote(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000767#ifdef DEBUG
768/*
769 * Debug-only routine, without additional rw_semaphore APIs, we can
770 * now only answer requests regarding whether we hold the lock for write
771 * (reader state is outside our visibility, we only track writer state).
772 *
773 * Note: this means !xfs_isilocked would give false positives, so don't do that.
774 */
775int
776xfs_isilocked(
777 xfs_inode_t *ip,
778 uint lock_flags)
779{
780 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
781 XFS_ILOCK_EXCL) {
782 if (!ip->i_lock.mr_writer)
783 return 0;
784 }
785
786 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
787 XFS_IOLOCK_EXCL) {
788 if (!ip->i_iolock.mr_writer)
789 return 0;
790 }
791
792 return 1;
793}
794#endif
795
Christoph Hellwig5a8d0f32008-12-03 12:20:40 +0100796#ifdef XFS_INODE_TRACE
797
798#define KTRACE_ENTER(ip, vk, s, line, ra) \
799 ktrace_enter((ip)->i_trace, \
800/* 0 */ (void *)(__psint_t)(vk), \
801/* 1 */ (void *)(s), \
802/* 2 */ (void *)(__psint_t) line, \
803/* 3 */ (void *)(__psint_t)atomic_read(&VFS_I(ip)->i_count), \
804/* 4 */ (void *)(ra), \
805/* 5 */ NULL, \
806/* 6 */ (void *)(__psint_t)current_cpu(), \
807/* 7 */ (void *)(__psint_t)current_pid(), \
808/* 8 */ (void *)__return_address, \
809/* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
810
811/*
812 * Vnode tracing code.
813 */
814void
815_xfs_itrace_entry(xfs_inode_t *ip, const char *func, inst_t *ra)
816{
817 KTRACE_ENTER(ip, INODE_KTRACE_ENTRY, func, 0, ra);
818}
819
820void
821_xfs_itrace_exit(xfs_inode_t *ip, const char *func, inst_t *ra)
822{
823 KTRACE_ENTER(ip, INODE_KTRACE_EXIT, func, 0, ra);
824}
825
826void
827xfs_itrace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra)
828{
829 KTRACE_ENTER(ip, INODE_KTRACE_HOLD, file, line, ra);
830}
831
832void
833_xfs_itrace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra)
834{
835 KTRACE_ENTER(ip, INODE_KTRACE_REF, file, line, ra);
836}
837
838void
839xfs_itrace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra)
840{
841 KTRACE_ENTER(ip, INODE_KTRACE_RELE, file, line, ra);
842}
843#endif /* XFS_INODE_TRACE */