blob: 073bb4a26b194a8e1508c9b50f4363b444e84b54 [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));
Christoph Hellwig033da482009-10-19 04:05:26 +000076 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
77
78 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
Christoph Hellwig24f211b2008-11-28 14:23:42 +110079
Christoph Hellwig24f211b2008-11-28 14:23:42 +110080 /* initialise the xfs inode */
81 ip->i_ino = ino;
82 ip->i_mount = mp;
83 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
84 ip->i_afp = NULL;
85 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
86 ip->i_flags = 0;
87 ip->i_update_core = 0;
Christoph Hellwig24f211b2008-11-28 14:23:42 +110088 ip->i_delayed_blks = 0;
89 memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
90 ip->i_size = 0;
91 ip->i_new_size = 0;
92
93 /*
94 * Initialize inode's trace buffers.
95 */
96#ifdef XFS_INODE_TRACE
97 ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS);
98#endif
99#ifdef XFS_BMAP_TRACE
100 ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS);
101#endif
102#ifdef XFS_BTREE_TRACE
103 ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS);
104#endif
105#ifdef XFS_RW_TRACE
106 ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS);
107#endif
108#ifdef XFS_ILOCK_TRACE
109 ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS);
110#endif
111#ifdef XFS_DIR2_TRACE
112 ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS);
113#endif
Dave Chinner705db3f2009-04-06 18:40:17 +0200114
115 /* prevent anyone from using this yet */
116 VFS_I(ip)->i_state = I_NEW|I_LOCK;
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100117
118 return ip;
119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300121STATIC void
122xfs_inode_free(
123 struct xfs_inode *ip)
124{
125 switch (ip->i_d.di_mode & S_IFMT) {
126 case S_IFREG:
127 case S_IFDIR:
128 case S_IFLNK:
129 xfs_idestroy_fork(ip, XFS_DATA_FORK);
130 break;
131 }
132
133 if (ip->i_afp)
134 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
135
136#ifdef XFS_INODE_TRACE
137 ktrace_free(ip->i_trace);
138#endif
139#ifdef XFS_BMAP_TRACE
140 ktrace_free(ip->i_xtrace);
141#endif
142#ifdef XFS_BTREE_TRACE
143 ktrace_free(ip->i_btrace);
144#endif
145#ifdef XFS_RW_TRACE
146 ktrace_free(ip->i_rwtrace);
147#endif
148#ifdef XFS_ILOCK_TRACE
149 ktrace_free(ip->i_lock_trace);
150#endif
151#ifdef XFS_DIR2_TRACE
152 ktrace_free(ip->i_dir_trace);
153#endif
154
155 if (ip->i_itemp) {
156 /*
157 * Only if we are shutting down the fs will we see an
158 * inode still in the AIL. If it is there, we should remove
159 * it to prevent a use-after-free from occurring.
160 */
161 xfs_log_item_t *lip = &ip->i_itemp->ili_item;
162 struct xfs_ail *ailp = lip->li_ailp;
163
164 ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
165 XFS_FORCED_SHUTDOWN(ip->i_mount));
166 if (lip->li_flags & XFS_LI_IN_AIL) {
167 spin_lock(&ailp->xa_lock);
168 if (lip->li_flags & XFS_LI_IN_AIL)
169 xfs_trans_ail_delete(ailp, lip);
170 else
171 spin_unlock(&ailp->xa_lock);
172 }
173 xfs_inode_item_destroy(ip);
174 ip->i_itemp = NULL;
175 }
176
177 /* asserts to verify all state is correct here */
178 ASSERT(atomic_read(&ip->i_iocount) == 0);
179 ASSERT(atomic_read(&ip->i_pincount) == 0);
180 ASSERT(!spin_is_locked(&ip->i_flags_lock));
181 ASSERT(completion_done(&ip->i_flush));
182
183 kmem_zone_free(xfs_inode_zone, ip);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
David Chinner6441e542008-10-30 17:21:19 +1100187 * Check the validity of the inode we just found it the cache
188 */
189static int
190xfs_iget_cache_hit(
David Chinner6441e542008-10-30 17:21:19 +1100191 struct xfs_perag *pag,
192 struct xfs_inode *ip,
193 int flags,
194 int lock_flags) __releases(pag->pag_ici_lock)
195{
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400196 struct inode *inode = VFS_I(ip);
David Chinner6441e542008-10-30 17:21:19 +1100197 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400198 int error;
199
200 spin_lock(&ip->i_flags_lock);
David Chinner6441e542008-10-30 17:21:19 +1100201
202 /*
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400203 * If we are racing with another cache hit that is currently
204 * instantiating this inode or currently recycling it out of
205 * reclaimabe state, wait for the initialisation to complete
206 * before continuing.
207 *
208 * XXX(hch): eventually we should do something equivalent to
209 * wait_on_inode to wait for these flags to be cleared
210 * instead of polling for it.
David Chinner6441e542008-10-30 17:21:19 +1100211 */
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400212 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
David Chinner6441e542008-10-30 17:21:19 +1100213 XFS_STATS_INC(xs_ig_frecycle);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400214 error = EAGAIN;
David Chinner6441e542008-10-30 17:21:19 +1100215 goto out_error;
216 }
217
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400218 /*
219 * If lookup is racing with unlink return an error immediately.
220 */
221 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
222 error = ENOENT;
223 goto out_error;
224 }
David Chinner6441e542008-10-30 17:21:19 +1100225
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400226 /*
227 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
228 * Need to carefully get it back into useable state.
229 */
230 if (ip->i_flags & XFS_IRECLAIMABLE) {
David Chinner6441e542008-10-30 17:21:19 +1100231 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
232
David Chinnerbf904242008-10-30 17:36:14 +1100233 /*
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400234 * We need to set XFS_INEW atomically with clearing the
235 * reclaimable tag so that we do have an indicator of the
236 * inode still being initialized.
David Chinnerbf904242008-10-30 17:36:14 +1100237 */
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400238 ip->i_flags |= XFS_INEW;
239 ip->i_flags &= ~XFS_IRECLAIMABLE;
240 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
241
242 spin_unlock(&ip->i_flags_lock);
243 read_unlock(&pag->pag_ici_lock);
244
245 error = -inode_init_always(mp->m_super, inode);
246 if (error) {
247 /*
248 * Re-initializing the inode failed, and we are in deep
249 * trouble. Try to re-add it to the reclaim list.
250 */
251 read_lock(&pag->pag_ici_lock);
252 spin_lock(&ip->i_flags_lock);
253
254 ip->i_flags &= ~XFS_INEW;
255 ip->i_flags |= XFS_IRECLAIMABLE;
256 __xfs_inode_set_reclaim_tag(pag, ip);
257 goto out_error;
258 }
259 inode->i_state = I_LOCK|I_NEW;
260 } else {
261 /* If the VFS inode is being torn down, pause and try again. */
262 if (!igrab(inode)) {
263 error = EAGAIN;
David Chinnerbf904242008-10-30 17:36:14 +1100264 goto out_error;
265 }
David Chinner6bfb3d02008-10-30 18:32:43 +1100266
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400267 /* We've got a live one. */
268 spin_unlock(&ip->i_flags_lock);
269 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100270 }
271
David Chinner6441e542008-10-30 17:21:19 +1100272 if (lock_flags != 0)
273 xfs_ilock(ip, lock_flags);
274
275 xfs_iflags_clear(ip, XFS_ISTALE);
276 xfs_itrace_exit_tag(ip, "xfs_iget.found");
277 XFS_STATS_INC(xs_ig_found);
278 return 0;
279
280out_error:
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400281 spin_unlock(&ip->i_flags_lock);
David Chinner6441e542008-10-30 17:21:19 +1100282 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100283 return error;
284}
285
286
287static int
288xfs_iget_cache_miss(
289 struct xfs_mount *mp,
290 struct xfs_perag *pag,
291 xfs_trans_t *tp,
292 xfs_ino_t ino,
293 struct xfs_inode **ipp,
294 xfs_daddr_t bno,
295 int flags,
Christoph Hellwig0c3dc2b2009-11-14 16:17:23 +0000296 int lock_flags)
David Chinner6441e542008-10-30 17:21:19 +1100297{
298 struct xfs_inode *ip;
299 int error;
300 unsigned long first_index, mask;
301 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
302
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100303 ip = xfs_inode_alloc(mp, ino);
304 if (!ip)
305 return ENOMEM;
306
307 error = xfs_iread(mp, tp, ip, bno, flags);
David Chinner6441e542008-10-30 17:21:19 +1100308 if (error)
Christoph Hellwig24f211b2008-11-28 14:23:42 +1100309 goto out_destroy;
David Chinner6441e542008-10-30 17:21:19 +1100310
311 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
312
313 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
314 error = ENOENT;
315 goto out_destroy;
316 }
317
318 /*
319 * Preload the radix tree so we can insert safely under the
David Chinner56e73ec2008-10-30 17:55:27 +1100320 * write spinlock. Note that we cannot sleep inside the preload
321 * region.
David Chinner6441e542008-10-30 17:21:19 +1100322 */
323 if (radix_tree_preload(GFP_KERNEL)) {
324 error = EAGAIN;
Christoph Hellwiged93ec32009-03-03 14:48:35 -0500325 goto out_destroy;
326 }
327
328 /*
329 * Because the inode hasn't been added to the radix-tree yet it can't
330 * be found by another thread, so we can do the non-sleeping lock here.
331 */
332 if (lock_flags) {
333 if (!xfs_ilock_nowait(ip, lock_flags))
334 BUG();
David Chinner6441e542008-10-30 17:21:19 +1100335 }
336
David Chinner6441e542008-10-30 17:21:19 +1100337 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
338 first_index = agino & mask;
339 write_lock(&pag->pag_ici_lock);
340
341 /* insert the new inode */
342 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
343 if (unlikely(error)) {
344 WARN_ON(error != -EEXIST);
345 XFS_STATS_INC(xs_ig_dup);
346 error = EAGAIN;
David Chinner56e73ec2008-10-30 17:55:27 +1100347 goto out_preload_end;
David Chinner6441e542008-10-30 17:21:19 +1100348 }
349
350 /* These values _must_ be set before releasing the radix tree lock! */
351 ip->i_udquot = ip->i_gdquot = NULL;
352 xfs_iflags_set(ip, XFS_INEW);
353
354 write_unlock(&pag->pag_ici_lock);
355 radix_tree_preload_end();
356 *ipp = ip;
357 return 0;
358
David Chinner56e73ec2008-10-30 17:55:27 +1100359out_preload_end:
David Chinner6441e542008-10-30 17:21:19 +1100360 write_unlock(&pag->pag_ici_lock);
361 radix_tree_preload_end();
David Chinner56e73ec2008-10-30 17:55:27 +1100362 if (lock_flags)
363 xfs_iunlock(ip, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100364out_destroy:
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300365 __destroy_inode(VFS_I(ip));
366 xfs_inode_free(ip);
David Chinner6441e542008-10-30 17:21:19 +1100367 return error;
368}
369
370/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000372 * The inode is looked up in the cache held in each AG.
David Chinnerbf904242008-10-30 17:36:14 +1100373 * If the inode is found in the cache, initialise the vfs inode
374 * if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
David Chinnerda353b02007-08-28 14:00:13 +1000376 * If it is not in core, read it in from the file system's device,
David Chinnerbf904242008-10-30 17:36:14 +1100377 * add it to the cache and initialise the vfs inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 * The inode is locked according to the value of the lock_flags parameter.
380 * This flag parameter indicates how and if the inode's IO lock and inode lock
381 * should be taken.
382 *
383 * mp -- the mount point structure for the current file system. It points
384 * to the inode hash table.
385 * tp -- a pointer to the current transaction if there is one. This is
386 * simply passed through to the xfs_iread() call.
387 * ino -- the number of the inode desired. This is the unique identifier
388 * within the file system for the inode being requested.
389 * lock_flags -- flags indicating how to lock the inode. See the comment
390 * for xfs_ilock() for a list of valid values.
391 * bno -- the block number starting the buffer containing the inode,
392 * if known (as by bulkstat), else 0.
393 */
David Chinnerbf904242008-10-30 17:36:14 +1100394int
395xfs_iget(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 xfs_mount_t *mp,
397 xfs_trans_t *tp,
398 xfs_ino_t ino,
399 uint flags,
400 uint lock_flags,
401 xfs_inode_t **ipp,
402 xfs_daddr_t bno)
403{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000406 xfs_perag_t *pag;
407 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
David Chinnerda353b02007-08-28 14:00:13 +1000409 /* the radix tree exists only in inode capable AGs */
410 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
411 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Chinnerda353b02007-08-28 14:00:13 +1000413 /* get the perag structure and ensure that it's inode capable */
414 pag = xfs_get_perag(mp, ino);
415 if (!pag->pagi_inodeok)
416 return EINVAL;
417 ASSERT(pag->pag_ici_init);
418 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420again:
David Chinner6441e542008-10-30 17:21:19 +1100421 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000422 read_lock(&pag->pag_ici_lock);
423 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David Chinner6441e542008-10-30 17:21:19 +1100425 if (ip) {
David Chinnerbf904242008-10-30 17:36:14 +1100426 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
David Chinner6441e542008-10-30 17:21:19 +1100427 if (error)
428 goto out_error_or_again;
429 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000430 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100431 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000432
David Chinner6441e542008-10-30 17:21:19 +1100433 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
434 flags, lock_flags);
435 if (error)
436 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
David Chinnerda353b02007-08-28 14:00:13 +1000438 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *ipp = ip;
441
David Chinnerbf904242008-10-30 17:36:14 +1100442 ASSERT(ip->i_df.if_ext_max ==
443 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000444 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * If we have a real type for an on-disk inode, we can set ops(&unlock)
446 * now. If it's a new inode being created, xfs_ialloc will handle it.
447 */
David Chinnerbf904242008-10-30 17:36:14 +1100448 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000449 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100451
452out_error_or_again:
453 if (error == EAGAIN) {
454 delay(1);
455 goto again;
456 }
457 xfs_put_perag(mp, pag);
458 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
462 * Decrement reference count of an inode structure and unlock it.
463 *
464 * ip -- the inode being released
465 * lock_flags -- this parameter indicates the inode's locks to be
466 * to be released. See the comment on xfs_iunlock() for a list
467 * of valid values.
468 */
469void
470xfs_iput(xfs_inode_t *ip,
471 uint lock_flags)
472{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100473 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000475 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478/*
479 * Special iput for brand-new inodes that are still locked
480 */
481void
David Chinner01651642008-08-13 15:45:15 +1000482xfs_iput_new(
483 xfs_inode_t *ip,
484 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
David Chinner01651642008-08-13 15:45:15 +1000486 struct inode *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100488 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100491 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Christoph Hellwig10090be2007-10-11 18:11:03 +1000492 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 if (inode->i_state & I_NEW)
495 unlock_new_inode(inode);
496 if (lock_flags)
497 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000498 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100502 * This is called free all the memory associated with an inode.
503 * It must free the inode itself and any buffers allocated for
504 * if_extents/if_data and if_broot. It must also free the lock
505 * associated with the inode.
506 *
507 * Note: because we don't initialise everything on reallocation out
508 * of the zone, we must ensure we nullify everything correctly before
509 * freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
511void
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100512xfs_ireclaim(
513 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100515 struct xfs_mount *mp = ip->i_mount;
516 struct xfs_perag *pag;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 XFS_STATS_INC(xs_ig_reclaims);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100521 * Remove the inode from the per-AG radix tree. It doesn't matter
522 * if it was never added to it because radix_tree_delete can deal
523 * with that case just fine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100525 pag = xfs_get_perag(mp, ip->i_ino);
David Chinnerda353b02007-08-28 14:00:13 +1000526 write_lock(&pag->pag_ici_lock);
527 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
528 write_unlock(&pag->pag_ici_lock);
529 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100531 /*
532 * Here we do an (almost) spurious inode lock in order to coordinate
533 * with inode cache radix tree lookups. This is because the lookup
534 * can reference the inodes in the cache without taking references.
535 *
536 * We make that OK here by ensuring that we wait until the inode is
537 * unlocked after the lookup before we go ahead and free it. We get
538 * both the ilock and the iolock because the code may need to drop the
539 * ilock one but will still hold the iolock.
540 */
541 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200542 xfs_qm_dqdetach(ip);
Christoph Hellwig5cafdeb2008-12-03 12:20:25 +0100543 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
544
Christoph Hellwigb36ec042009-08-07 14:38:34 -0300545 xfs_inode_free(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548/*
549 * This is a wrapper routine around the xfs_ilock() routine
550 * used to centralize some grungy code. It is used in places
551 * that wish to lock the inode solely for reading the extents.
552 * The reason these places can't just call xfs_ilock(SHARED)
553 * is that the inode lock also guards to bringing in of the
554 * extents from disk for a file in b-tree format. If the inode
555 * is in b-tree format, then we need to lock the inode exclusively
556 * until the extents are read in. Locking it exclusively all
557 * the time would limit our parallelism unnecessarily, though.
558 * What we do instead is check to see if the extents have been
559 * read in yet, and only lock the inode exclusively if they
560 * have not.
561 *
562 * The function returns a value which should be given to the
563 * corresponding xfs_iunlock_map_shared(). This value is
564 * the mode in which the lock was actually taken.
565 */
566uint
567xfs_ilock_map_shared(
568 xfs_inode_t *ip)
569{
570 uint lock_mode;
571
572 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
573 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
574 lock_mode = XFS_ILOCK_EXCL;
575 } else {
576 lock_mode = XFS_ILOCK_SHARED;
577 }
578
579 xfs_ilock(ip, lock_mode);
580
581 return lock_mode;
582}
583
584/*
585 * This is simply the unlock routine to go with xfs_ilock_map_shared().
586 * All it does is call xfs_iunlock() with the given lock_mode.
587 */
588void
589xfs_iunlock_map_shared(
590 xfs_inode_t *ip,
591 unsigned int lock_mode)
592{
593 xfs_iunlock(ip, lock_mode);
594}
595
596/*
597 * The xfs inode contains 2 locks: a multi-reader lock called the
598 * i_iolock and a multi-reader lock called the i_lock. This routine
599 * allows either or both of the locks to be obtained.
600 *
601 * The 2 locks should always be ordered so that the IO lock is
602 * obtained first in order to prevent deadlock.
603 *
604 * ip -- the inode being locked
605 * lock_flags -- this parameter indicates the inode's locks
606 * to be locked. It can be:
607 * XFS_IOLOCK_SHARED,
608 * XFS_IOLOCK_EXCL,
609 * XFS_ILOCK_SHARED,
610 * XFS_ILOCK_EXCL,
611 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
612 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
613 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
614 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
615 */
616void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000617xfs_ilock(
618 xfs_inode_t *ip,
619 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 /*
622 * You can't set both SHARED and EXCL for the same lock,
623 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
624 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
625 */
626 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
627 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
628 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
629 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000630 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000632 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000633 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000634 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000635 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000636
637 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000638 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000639 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000640 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
643}
644
645/*
646 * This is just like xfs_ilock(), except that the caller
647 * is guaranteed not to sleep. It returns 1 if it gets
648 * the requested locks and 0 otherwise. If the IO lock is
649 * obtained but the inode lock cannot be, then the IO lock
650 * is dropped before returning.
651 *
652 * ip -- the inode being locked
653 * lock_flags -- this parameter indicates the inode's locks to be
654 * to be locked. See the comment for xfs_ilock() for a list
655 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000658xfs_ilock_nowait(
659 xfs_inode_t *ip,
660 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * You can't set both SHARED and EXCL for the same lock,
664 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
665 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
666 */
667 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
668 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
669 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
670 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000671 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000674 if (!mrtryupdate(&ip->i_iolock))
675 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000677 if (!mrtryaccess(&ip->i_iolock))
678 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000681 if (!mrtryupdate(&ip->i_lock))
682 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000684 if (!mrtryaccess(&ip->i_lock))
685 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
688 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000689
690 out_undo_iolock:
691 if (lock_flags & XFS_IOLOCK_EXCL)
692 mrunlock_excl(&ip->i_iolock);
693 else if (lock_flags & XFS_IOLOCK_SHARED)
694 mrunlock_shared(&ip->i_iolock);
695 out:
696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699/*
700 * xfs_iunlock() is used to drop the inode locks acquired with
701 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
702 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
703 * that we know which locks to drop.
704 *
705 * ip -- the inode being unlocked
706 * lock_flags -- this parameter indicates the inode's locks to be
707 * to be unlocked. See the comment for xfs_ilock() for a list
708 * of valid values for this parameter.
709 *
710 */
711void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000712xfs_iunlock(
713 xfs_inode_t *ip,
714 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 /*
717 * You can't set both SHARED and EXCL for the same lock,
718 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
719 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
720 */
721 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
722 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
723 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
724 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000725 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
726 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 ASSERT(lock_flags != 0);
728
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000729 if (lock_flags & XFS_IOLOCK_EXCL)
730 mrunlock_excl(&ip->i_iolock);
731 else if (lock_flags & XFS_IOLOCK_SHARED)
732 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000734 if (lock_flags & XFS_ILOCK_EXCL)
735 mrunlock_excl(&ip->i_lock);
736 else if (lock_flags & XFS_ILOCK_SHARED)
737 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000739 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
740 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /*
742 * Let the AIL know that this item has been unlocked in case
743 * it is in the AIL and anyone is waiting on it. Don't do
744 * this if the caller has asked us not to.
745 */
David Chinner783a2f62008-10-30 17:39:58 +1100746 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000747 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
750}
751
752/*
753 * give up write locks. the i/o lock cannot be held nested
754 * if it is being demoted.
755 */
756void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000757xfs_ilock_demote(
758 xfs_inode_t *ip,
759 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
762 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
763
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000764 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000766 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 mrdemote(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000770#ifdef DEBUG
771/*
772 * Debug-only routine, without additional rw_semaphore APIs, we can
773 * now only answer requests regarding whether we hold the lock for write
774 * (reader state is outside our visibility, we only track writer state).
775 *
776 * Note: this means !xfs_isilocked would give false positives, so don't do that.
777 */
778int
779xfs_isilocked(
780 xfs_inode_t *ip,
781 uint lock_flags)
782{
783 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
784 XFS_ILOCK_EXCL) {
785 if (!ip->i_lock.mr_writer)
786 return 0;
787 }
788
789 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
790 XFS_IOLOCK_EXCL) {
791 if (!ip->i_iolock.mr_writer)
792 return 0;
793 }
794
795 return 1;
796}
797#endif
798
Christoph Hellwig5a8d0f32008-12-03 12:20:40 +0100799#ifdef XFS_INODE_TRACE
800
801#define KTRACE_ENTER(ip, vk, s, line, ra) \
802 ktrace_enter((ip)->i_trace, \
803/* 0 */ (void *)(__psint_t)(vk), \
804/* 1 */ (void *)(s), \
805/* 2 */ (void *)(__psint_t) line, \
806/* 3 */ (void *)(__psint_t)atomic_read(&VFS_I(ip)->i_count), \
807/* 4 */ (void *)(ra), \
808/* 5 */ NULL, \
809/* 6 */ (void *)(__psint_t)current_cpu(), \
810/* 7 */ (void *)(__psint_t)current_pid(), \
811/* 8 */ (void *)__return_address, \
812/* 9 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL)
813
814/*
815 * Vnode tracing code.
816 */
817void
818_xfs_itrace_entry(xfs_inode_t *ip, const char *func, inst_t *ra)
819{
820 KTRACE_ENTER(ip, INODE_KTRACE_ENTRY, func, 0, ra);
821}
822
823void
824_xfs_itrace_exit(xfs_inode_t *ip, const char *func, inst_t *ra)
825{
826 KTRACE_ENTER(ip, INODE_KTRACE_EXIT, func, 0, ra);
827}
828
829void
830xfs_itrace_hold(xfs_inode_t *ip, char *file, int line, inst_t *ra)
831{
832 KTRACE_ENTER(ip, INODE_KTRACE_HOLD, file, line, ra);
833}
834
835void
836_xfs_itrace_ref(xfs_inode_t *ip, char *file, int line, inst_t *ra)
837{
838 KTRACE_ENTER(ip, INODE_KTRACE_REF, file, line, ra);
839}
840
841void
842xfs_itrace_rele(xfs_inode_t *ip, char *file, int line, inst_t *ra)
843{
844 KTRACE_ENTER(ip, INODE_KTRACE_RELE, file, line, ra);
845}
846#endif /* XFS_INODE_TRACE */