blob: b2539b17c95444a29a5bc0c03e15d77866a03663 [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"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.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_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_quota.h"
40#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
David Chinner6441e542008-10-30 17:21:19 +110043 * Check the validity of the inode we just found it the cache
44 */
45static int
46xfs_iget_cache_hit(
47 struct inode *inode,
48 struct xfs_perag *pag,
49 struct xfs_inode *ip,
50 int flags,
51 int lock_flags) __releases(pag->pag_ici_lock)
52{
53 struct xfs_mount *mp = ip->i_mount;
54 struct inode *old_inode;
55 int error = 0;
56
57 /*
58 * If INEW is set this inode is being set up
59 * Pause and try again.
60 */
61 if (xfs_iflags_test(ip, XFS_INEW)) {
62 error = EAGAIN;
63 XFS_STATS_INC(xs_ig_frecycle);
64 goto out_error;
65 }
66
67 old_inode = ip->i_vnode;
68 if (old_inode == NULL) {
69 /*
70 * If IRECLAIM is set this inode is
71 * on its way out of the system,
72 * we need to pause and try again.
73 */
74 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
75 error = EAGAIN;
76 XFS_STATS_INC(xs_ig_frecycle);
77 goto out_error;
78 }
79 ASSERT(xfs_iflags_test(ip, XFS_IRECLAIMABLE));
80
81 /*
82 * If lookup is racing with unlink, then we
83 * should return an error immediately so we
84 * don't remove it from the reclaim list and
85 * potentially leak the inode.
86 */
87 if ((ip->i_d.di_mode == 0) &&
88 !(flags & XFS_IGET_CREATE)) {
89 error = ENOENT;
90 goto out_error;
91 }
92 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
93
94 xfs_iflags_clear(ip, XFS_IRECLAIMABLE);
95 read_unlock(&pag->pag_ici_lock);
96
97 XFS_MOUNT_ILOCK(mp);
98 list_del_init(&ip->i_reclaim);
99 XFS_MOUNT_IUNLOCK(mp);
100
101 } else if (inode != old_inode) {
102 /* The inode is being torn down, pause and
103 * try again.
104 */
105 if (old_inode->i_state & (I_FREEING | I_CLEAR)) {
106 error = EAGAIN;
107 XFS_STATS_INC(xs_ig_frecycle);
108 goto out_error;
109 }
110/* Chances are the other vnode (the one in the inode) is being torn
111* down right now, and we landed on top of it. Question is, what do
112* we do? Unhook the old inode and hook up the new one?
113*/
114 cmn_err(CE_PANIC,
115 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
116 old_inode, inode);
117 } else {
118 read_unlock(&pag->pag_ici_lock);
119 }
120
121 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
122 error = ENOENT;
123 goto out;
124 }
125
126 if (lock_flags != 0)
127 xfs_ilock(ip, lock_flags);
128
129 xfs_iflags_clear(ip, XFS_ISTALE);
130 xfs_itrace_exit_tag(ip, "xfs_iget.found");
131 XFS_STATS_INC(xs_ig_found);
132 return 0;
133
134out_error:
135 read_unlock(&pag->pag_ici_lock);
136out:
137 return error;
138}
139
140
141static int
142xfs_iget_cache_miss(
143 struct xfs_mount *mp,
144 struct xfs_perag *pag,
145 xfs_trans_t *tp,
146 xfs_ino_t ino,
147 struct xfs_inode **ipp,
148 xfs_daddr_t bno,
149 int flags,
150 int lock_flags) __releases(pag->pag_ici_lock)
151{
152 struct xfs_inode *ip;
153 int error;
154 unsigned long first_index, mask;
155 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
156
157 /*
158 * Read the disk inode attributes into a new inode structure and get
159 * a new vnode for it. This should also initialize i_ino and i_mount.
160 */
161 error = xfs_iread(mp, tp, ino, &ip, bno,
162 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
163 if (error)
164 return error;
165
166 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
167
168 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
169 error = ENOENT;
170 goto out_destroy;
171 }
172
173 /*
174 * Preload the radix tree so we can insert safely under the
175 * write spinlock.
176 */
177 if (radix_tree_preload(GFP_KERNEL)) {
178 error = EAGAIN;
179 goto out_destroy;
180 }
181
182 if (lock_flags)
183 xfs_ilock(ip, lock_flags);
184
185 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
186 first_index = agino & mask;
187 write_lock(&pag->pag_ici_lock);
188
189 /* insert the new inode */
190 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
191 if (unlikely(error)) {
192 WARN_ON(error != -EEXIST);
193 XFS_STATS_INC(xs_ig_dup);
194 error = EAGAIN;
195 goto out_unlock;
196 }
197
198 /* These values _must_ be set before releasing the radix tree lock! */
199 ip->i_udquot = ip->i_gdquot = NULL;
200 xfs_iflags_set(ip, XFS_INEW);
201
202 write_unlock(&pag->pag_ici_lock);
203 radix_tree_preload_end();
204 *ipp = ip;
205 return 0;
206
207out_unlock:
208 write_unlock(&pag->pag_ici_lock);
209 radix_tree_preload_end();
210out_destroy:
211 xfs_idestroy(ip);
212 return error;
213}
214
215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Look up an inode by number in the given file system.
David Chinnerda353b02007-08-28 14:00:13 +1000217 * The inode is looked up in the cache held in each AG.
218 * If the inode is found in the cache, attach it to the provided
219 * vnode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
David Chinnerda353b02007-08-28 14:00:13 +1000221 * If it is not in core, read it in from the file system's device,
222 * add it to the cache and attach the provided vnode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *
224 * The inode is locked according to the value of the lock_flags parameter.
225 * This flag parameter indicates how and if the inode's IO lock and inode lock
226 * should be taken.
227 *
228 * mp -- the mount point structure for the current file system. It points
229 * to the inode hash table.
230 * tp -- a pointer to the current transaction if there is one. This is
231 * simply passed through to the xfs_iread() call.
232 * ino -- the number of the inode desired. This is the unique identifier
233 * within the file system for the inode being requested.
234 * lock_flags -- flags indicating how to lock the inode. See the comment
235 * for xfs_ilock() for a list of valid values.
236 * bno -- the block number starting the buffer containing the inode,
237 * if known (as by bulkstat), else 0.
238 */
239STATIC int
240xfs_iget_core(
Christoph Hellwig10090be2007-10-11 18:11:03 +1000241 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfs_mount_t *mp,
243 xfs_trans_t *tp,
244 xfs_ino_t ino,
245 uint flags,
246 uint lock_flags,
247 xfs_inode_t **ipp,
248 xfs_daddr_t bno)
249{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int error;
David Chinnerda353b02007-08-28 14:00:13 +1000252 xfs_perag_t *pag;
253 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David Chinnerda353b02007-08-28 14:00:13 +1000255 /* the radix tree exists only in inode capable AGs */
256 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
257 return EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David Chinnerda353b02007-08-28 14:00:13 +1000259 /* get the perag structure and ensure that it's inode capable */
260 pag = xfs_get_perag(mp, ino);
261 if (!pag->pagi_inodeok)
262 return EINVAL;
263 ASSERT(pag->pag_ici_init);
264 agino = XFS_INO_TO_AGINO(mp, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266again:
David Chinner6441e542008-10-30 17:21:19 +1100267 error = 0;
David Chinnerda353b02007-08-28 14:00:13 +1000268 read_lock(&pag->pag_ici_lock);
269 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David Chinner6441e542008-10-30 17:21:19 +1100271 if (ip) {
272 error = xfs_iget_cache_hit(inode, pag, ip, flags, lock_flags);
273 if (error)
274 goto out_error_or_again;
275 } else {
David Chinnerda353b02007-08-28 14:00:13 +1000276 read_unlock(&pag->pag_ici_lock);
David Chinner6441e542008-10-30 17:21:19 +1100277 XFS_STATS_INC(xs_ig_missed);
David Chinnerda353b02007-08-28 14:00:13 +1000278
David Chinner6441e542008-10-30 17:21:19 +1100279 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
280 flags, lock_flags);
281 if (error)
282 goto out_error_or_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
David Chinnerda353b02007-08-28 14:00:13 +1000284 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 ASSERT(ip->i_df.if_ext_max ==
287 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
288
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000289 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *ipp = ip;
291
292 /*
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000293 * Set up the Linux with the Linux inode.
294 */
295 ip->i_vnode = inode;
296 inode->i_private = ip;
297
298 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * If we have a real type for an on-disk inode, we can set ops(&unlock)
300 * now. If it's a new inode being created, xfs_ialloc will handle it.
301 */
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000302 if (ip->i_d.di_mode != 0)
303 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
David Chinner6441e542008-10-30 17:21:19 +1100305
306out_error_or_again:
307 if (error == EAGAIN) {
308 delay(1);
309 goto again;
310 }
311 xfs_put_perag(mp, pag);
312 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315
316/*
317 * The 'normal' internal xfs_iget, if needed it will
318 * 'allocate', or 'get', the vnode.
319 */
320int
321xfs_iget(
322 xfs_mount_t *mp,
323 xfs_trans_t *tp,
324 xfs_ino_t ino,
325 uint flags,
326 uint lock_flags,
327 xfs_inode_t **ipp,
328 xfs_daddr_t bno)
329{
330 struct inode *inode;
Christoph Hellwig10090be2007-10-11 18:11:03 +1000331 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int error;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 XFS_STATS_INC(xs_ig_attempts);
335
Christoph Hellwigba403ab2005-09-05 08:33:00 +1000336retry:
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000337 inode = iget_locked(mp->m_super, ino);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000338 if (!inode)
339 /* If we got no inode we are out of memory */
340 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Christoph Hellwig10090be2007-10-11 18:11:03 +1000342 if (inode->i_state & I_NEW) {
343 XFS_STATS_INC(vn_active);
344 XFS_STATS_INC(vn_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Christoph Hellwig10090be2007-10-11 18:11:03 +1000346 error = xfs_iget_core(inode, mp, tp, ino, flags,
347 lock_flags, ipp, bno);
348 if (error) {
349 make_bad_inode(inode);
350 if (inode->i_state & I_NEW)
351 unlock_new_inode(inode);
352 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Christoph Hellwig10090be2007-10-11 18:11:03 +1000354 return error;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Christoph Hellwig10090be2007-10-11 18:11:03 +1000357 /*
358 * If the inode is not fully constructed due to
359 * filehandle mismatches wait for the inode to go
360 * away and try again.
361 *
362 * iget_locked will call __wait_on_freeing_inode
363 * to wait for the inode to go away.
364 */
365 if (is_bad_inode(inode)) {
366 iput(inode);
367 delay(1);
368 goto retry;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Christoph Hellwig10090be2007-10-11 18:11:03 +1000371 ip = XFS_I(inode);
372 if (!ip) {
373 iput(inode);
374 delay(1);
375 goto retry;
376 }
377
378 if (lock_flags != 0)
379 xfs_ilock(ip, lock_flags);
380 XFS_STATS_INC(xs_ig_found);
381 *ipp = ip;
382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/*
386 * Look for the inode corresponding to the given ino in the hash table.
387 * If it is there and its i_transp pointer matches tp, return it.
388 * Otherwise, return NULL.
389 */
390xfs_inode_t *
391xfs_inode_incore(xfs_mount_t *mp,
392 xfs_ino_t ino,
393 xfs_trans_t *tp)
394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xfs_inode_t *ip;
David Chinnerda353b02007-08-28 14:00:13 +1000396 xfs_perag_t *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Chinnerda353b02007-08-28 14:00:13 +1000398 pag = xfs_get_perag(mp, ino);
399 read_lock(&pag->pag_ici_lock);
400 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ino));
401 read_unlock(&pag->pag_ici_lock);
402 xfs_put_perag(mp, pag);
403
404 /* the returned inode must match the transaction */
405 if (ip && (ip->i_transp != tp))
406 return NULL;
407 return ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/*
411 * Decrement reference count of an inode structure and unlock it.
412 *
413 * ip -- the inode being released
414 * lock_flags -- this parameter indicates the inode's locks to be
415 * to be released. See the comment on xfs_iunlock() for a list
416 * of valid values.
417 */
418void
419xfs_iput(xfs_inode_t *ip,
420 uint lock_flags)
421{
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100422 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000424 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/*
428 * Special iput for brand-new inodes that are still locked
429 */
430void
David Chinner01651642008-08-13 15:45:15 +1000431xfs_iput_new(
432 xfs_inode_t *ip,
433 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
David Chinner01651642008-08-13 15:45:15 +1000435 struct inode *inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100437 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if ((ip->i_d.di_mode == 0)) {
David Chinner7a18c382006-11-11 18:04:54 +1100440 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
Christoph Hellwig10090be2007-10-11 18:11:03 +1000441 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 if (inode->i_state & I_NEW)
444 unlock_new_inode(inode);
445 if (lock_flags)
446 xfs_iunlock(ip, lock_flags);
Christoph Hellwig10090be2007-10-11 18:11:03 +1000447 IRELE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450
451/*
452 * This routine embodies the part of the reclaim code that pulls
453 * the inode from the inode hash table and the mount structure's
454 * inode list.
455 * This should only be called from xfs_reclaim().
456 */
457void
458xfs_ireclaim(xfs_inode_t *ip)
459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /*
461 * Remove from old hash list and mount list.
462 */
463 XFS_STATS_INC(xs_ig_reclaims);
464
465 xfs_iextract(ip);
466
467 /*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100468 * Here we do a spurious inode lock in order to coordinate with inode
469 * cache radix tree lookups. This is because the lookup can reference
470 * the inodes in the cache without taking references. We make that OK
471 * here by ensuring that we wait until the inode is unlocked after the
472 * lookup before we go ahead and free it. We get both the ilock and
473 * the iolock because the code may need to drop the ilock one but will
474 * still hold the iolock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
476 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
477
478 /*
479 * Release dquots (and their references) if any. An inode may escape
480 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
481 */
482 XFS_QM_DQDETACH(ip->i_mount, ip);
483
484 /*
485 * Pull our behavior descriptor from the vnode chain.
486 */
Christoph Hellwig10090be2007-10-11 18:11:03 +1000487 if (ip->i_vnode) {
488 ip->i_vnode->i_private = NULL;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000489 ip->i_vnode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 /*
493 * Free all memory associated with the inode.
494 */
Tim Shimmin439b8432006-11-11 18:04:34 +1100495 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 xfs_idestroy(ip);
497}
498
499/*
500 * This routine removes an about-to-be-destroyed inode from
501 * all of the lists in which it is located with the exception
502 * of the behavior chain.
503 */
504void
505xfs_iextract(
506 xfs_inode_t *ip)
507{
David Chinnerda353b02007-08-28 14:00:13 +1000508 xfs_mount_t *mp = ip->i_mount;
509 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
David Chinnerda353b02007-08-28 14:00:13 +1000511 write_lock(&pag->pag_ici_lock);
512 radix_tree_delete(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino));
513 write_unlock(&pag->pag_ici_lock);
514 xfs_put_perag(mp, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* Deal with the deleted inodes list */
David Chinner6c7699c2008-10-30 17:11:29 +1100517 XFS_MOUNT_ILOCK(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 list_del_init(&ip->i_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 mp->m_ireclaims++;
520 XFS_MOUNT_IUNLOCK(mp);
521}
522
523/*
524 * This is a wrapper routine around the xfs_ilock() routine
525 * used to centralize some grungy code. It is used in places
526 * that wish to lock the inode solely for reading the extents.
527 * The reason these places can't just call xfs_ilock(SHARED)
528 * is that the inode lock also guards to bringing in of the
529 * extents from disk for a file in b-tree format. If the inode
530 * is in b-tree format, then we need to lock the inode exclusively
531 * until the extents are read in. Locking it exclusively all
532 * the time would limit our parallelism unnecessarily, though.
533 * What we do instead is check to see if the extents have been
534 * read in yet, and only lock the inode exclusively if they
535 * have not.
536 *
537 * The function returns a value which should be given to the
538 * corresponding xfs_iunlock_map_shared(). This value is
539 * the mode in which the lock was actually taken.
540 */
541uint
542xfs_ilock_map_shared(
543 xfs_inode_t *ip)
544{
545 uint lock_mode;
546
547 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
548 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
549 lock_mode = XFS_ILOCK_EXCL;
550 } else {
551 lock_mode = XFS_ILOCK_SHARED;
552 }
553
554 xfs_ilock(ip, lock_mode);
555
556 return lock_mode;
557}
558
559/*
560 * This is simply the unlock routine to go with xfs_ilock_map_shared().
561 * All it does is call xfs_iunlock() with the given lock_mode.
562 */
563void
564xfs_iunlock_map_shared(
565 xfs_inode_t *ip,
566 unsigned int lock_mode)
567{
568 xfs_iunlock(ip, lock_mode);
569}
570
571/*
572 * The xfs inode contains 2 locks: a multi-reader lock called the
573 * i_iolock and a multi-reader lock called the i_lock. This routine
574 * allows either or both of the locks to be obtained.
575 *
576 * The 2 locks should always be ordered so that the IO lock is
577 * obtained first in order to prevent deadlock.
578 *
579 * ip -- the inode being locked
580 * lock_flags -- this parameter indicates the inode's locks
581 * to be locked. It can be:
582 * XFS_IOLOCK_SHARED,
583 * XFS_IOLOCK_EXCL,
584 * XFS_ILOCK_SHARED,
585 * XFS_ILOCK_EXCL,
586 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
587 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
588 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
589 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
590 */
591void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000592xfs_ilock(
593 xfs_inode_t *ip,
594 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 /*
597 * You can't set both SHARED and EXCL for the same lock,
598 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
599 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
600 */
601 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
602 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
603 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
604 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000605 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000607 if (lock_flags & XFS_IOLOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000608 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000609 else if (lock_flags & XFS_IOLOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000610 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000611
612 if (lock_flags & XFS_ILOCK_EXCL)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000613 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000614 else if (lock_flags & XFS_ILOCK_SHARED)
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000615 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
618}
619
620/*
621 * This is just like xfs_ilock(), except that the caller
622 * is guaranteed not to sleep. It returns 1 if it gets
623 * the requested locks and 0 otherwise. If the IO lock is
624 * obtained but the inode lock cannot be, then the IO lock
625 * is dropped before returning.
626 *
627 * ip -- the inode being locked
628 * lock_flags -- this parameter indicates the inode's locks to be
629 * to be locked. See the comment for xfs_ilock() for a list
630 * of valid values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
632int
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000633xfs_ilock_nowait(
634 xfs_inode_t *ip,
635 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * You can't set both SHARED and EXCL for the same lock,
639 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
640 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
641 */
642 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
643 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
644 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
645 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000646 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (lock_flags & XFS_IOLOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000649 if (!mrtryupdate(&ip->i_iolock))
650 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 } else if (lock_flags & XFS_IOLOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000652 if (!mrtryaccess(&ip->i_iolock))
653 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 if (lock_flags & XFS_ILOCK_EXCL) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000656 if (!mrtryupdate(&ip->i_lock))
657 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 } else if (lock_flags & XFS_ILOCK_SHARED) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000659 if (!mrtryaccess(&ip->i_lock))
660 goto out_undo_iolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
663 return 1;
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000664
665 out_undo_iolock:
666 if (lock_flags & XFS_IOLOCK_EXCL)
667 mrunlock_excl(&ip->i_iolock);
668 else if (lock_flags & XFS_IOLOCK_SHARED)
669 mrunlock_shared(&ip->i_iolock);
670 out:
671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*
675 * xfs_iunlock() is used to drop the inode locks acquired with
676 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
677 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
678 * that we know which locks to drop.
679 *
680 * ip -- the inode being unlocked
681 * lock_flags -- this parameter indicates the inode's locks to be
682 * to be unlocked. See the comment for xfs_ilock() for a list
683 * of valid values for this parameter.
684 *
685 */
686void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000687xfs_iunlock(
688 xfs_inode_t *ip,
689 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 /*
692 * You can't set both SHARED and EXCL for the same lock,
693 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
694 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
695 */
696 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
697 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
698 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
699 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +1000700 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
701 XFS_LOCK_DEP_MASK)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ASSERT(lock_flags != 0);
703
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000704 if (lock_flags & XFS_IOLOCK_EXCL)
705 mrunlock_excl(&ip->i_iolock);
706 else if (lock_flags & XFS_IOLOCK_SHARED)
707 mrunlock_shared(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000709 if (lock_flags & XFS_ILOCK_EXCL)
710 mrunlock_excl(&ip->i_lock);
711 else if (lock_flags & XFS_ILOCK_SHARED)
712 mrunlock_shared(&ip->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000714 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
715 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * Let the AIL know that this item has been unlocked in case
718 * it is in the AIL and anyone is waiting on it. Don't do
719 * this if the caller has asked us not to.
720 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000721 xfs_trans_unlocked_item(ip->i_mount,
722 (xfs_log_item_t*)(ip->i_itemp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
725}
726
727/*
728 * give up write locks. the i/o lock cannot be held nested
729 * if it is being demoted.
730 */
731void
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000732xfs_ilock_demote(
733 xfs_inode_t *ip,
734 uint lock_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
737 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
738
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000739 if (lock_flags & XFS_ILOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 mrdemote(&ip->i_lock);
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000741 if (lock_flags & XFS_IOLOCK_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mrdemote(&ip->i_iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000745#ifdef DEBUG
746/*
747 * Debug-only routine, without additional rw_semaphore APIs, we can
748 * now only answer requests regarding whether we hold the lock for write
749 * (reader state is outside our visibility, we only track writer state).
750 *
751 * Note: this means !xfs_isilocked would give false positives, so don't do that.
752 */
753int
754xfs_isilocked(
755 xfs_inode_t *ip,
756 uint lock_flags)
757{
758 if ((lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) ==
759 XFS_ILOCK_EXCL) {
760 if (!ip->i_lock.mr_writer)
761 return 0;
762 }
763
764 if ((lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) ==
765 XFS_IOLOCK_EXCL) {
766 if (!ip->i_iolock.mr_writer)
767 return 0;
768 }
769
770 return 1;
771}
772#endif
773