ASoC: omap-abe-twl6040: Add device tree support
[deliverable/linux.git] / fs / xfs / xfs_iget.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
ef14f0c1 21#include "xfs_acl.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4 27#include "xfs_mount.h"
1da177e4 28#include "xfs_bmap_btree.h"
a844f451 29#include "xfs_alloc_btree.h"
1da177e4 30#include "xfs_ialloc_btree.h"
1da177e4
LT
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
a844f451
NS
33#include "xfs_btree.h"
34#include "xfs_ialloc.h"
1da177e4
LT
35#include "xfs_quota.h"
36#include "xfs_utils.h"
783a2f65
DC
37#include "xfs_trans_priv.h"
38#include "xfs_inode_item.h"
24f211ba 39#include "xfs_bmap.h"
0b1b213f 40#include "xfs_trace.h"
24f211ba
CH
41
42
43/*
44 * Allocate and initialise an xfs_inode.
45 */
46STATIC struct xfs_inode *
47xfs_inode_alloc(
48 struct xfs_mount *mp,
49 xfs_ino_t ino)
50{
51 struct xfs_inode *ip;
52
53 /*
54 * if this didn't occur in transactions, we could use
55 * KM_MAYFAIL and return NULL here on ENOMEM. Set the
56 * code up to do this anyway.
57 */
58 ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
59 if (!ip)
60 return NULL;
54e34621
CH
61 if (inode_init_always(mp->m_super, VFS_I(ip))) {
62 kmem_zone_free(xfs_inode_zone, ip);
63 return NULL;
64 }
24f211ba 65
24f211ba
CH
66 ASSERT(atomic_read(&ip->i_pincount) == 0);
67 ASSERT(!spin_is_locked(&ip->i_flags_lock));
474fce06 68 ASSERT(!xfs_isiflocked(ip));
1a3e8f3d 69 ASSERT(ip->i_ino == 0);
033da48f
CH
70
71 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
24f211ba 72
24f211ba
CH
73 /* initialise the xfs inode */
74 ip->i_ino = ino;
75 ip->i_mount = mp;
76 memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
77 ip->i_afp = NULL;
78 memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
79 ip->i_flags = 0;
24f211ba
CH
80 ip->i_delayed_blks = 0;
81 memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
24f211ba 82
24f211ba
CH
83 return ip;
84}
1da177e4 85
fa0d7e3d
NP
86STATIC void
87xfs_inode_free_callback(
88 struct rcu_head *head)
89{
90 struct inode *inode = container_of(head, struct inode, i_rcu);
91 struct xfs_inode *ip = XFS_I(inode);
92
fa0d7e3d
NP
93 kmem_zone_free(xfs_inode_zone, ip);
94}
95
2f11feab 96void
b36ec042
CH
97xfs_inode_free(
98 struct xfs_inode *ip)
99{
100 switch (ip->i_d.di_mode & S_IFMT) {
101 case S_IFREG:
102 case S_IFDIR:
103 case S_IFLNK:
104 xfs_idestroy_fork(ip, XFS_DATA_FORK);
105 break;
106 }
107
108 if (ip->i_afp)
109 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
110
b36ec042 111 if (ip->i_itemp) {
32ce90a4 112 ASSERT(!(ip->i_itemp->ili_item.li_flags & XFS_LI_IN_AIL));
b36ec042
CH
113 xfs_inode_item_destroy(ip);
114 ip->i_itemp = NULL;
115 }
116
117 /* asserts to verify all state is correct here */
b36ec042
CH
118 ASSERT(atomic_read(&ip->i_pincount) == 0);
119 ASSERT(!spin_is_locked(&ip->i_flags_lock));
474fce06 120 ASSERT(!xfs_isiflocked(ip));
b36ec042 121
1a3e8f3d
DC
122 /*
123 * Because we use RCU freeing we need to ensure the inode always
124 * appears to be reclaimed with an invalid inode number when in the
125 * free state. The ip->i_flags_lock provides the barrier against lookup
126 * races.
127 */
128 spin_lock(&ip->i_flags_lock);
129 ip->i_flags = XFS_IRECLAIM;
130 ip->i_ino = 0;
131 spin_unlock(&ip->i_flags_lock);
92f1c008
AE
132
133 call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback);
b36ec042
CH
134}
135
1da177e4 136/*
6441e549 137 * Check the validity of the inode we just found it the cache
1da177e4 138 */
6441e549
DC
139static int
140xfs_iget_cache_hit(
6441e549
DC
141 struct xfs_perag *pag,
142 struct xfs_inode *ip,
1a3e8f3d 143 xfs_ino_t ino,
6441e549 144 int flags,
1a3e8f3d 145 int lock_flags) __releases(RCU)
1da177e4 146{
bc990f5c 147 struct inode *inode = VFS_I(ip);
6441e549 148 struct xfs_mount *mp = ip->i_mount;
bc990f5c
CH
149 int error;
150
1a3e8f3d
DC
151 /*
152 * check for re-use of an inode within an RCU grace period due to the
153 * radix tree nodes not being updated yet. We monitor for this by
154 * setting the inode number to zero before freeing the inode structure.
155 * If the inode has been reallocated and set up, then the inode number
156 * will not match, so check for that, too.
157 */
bc990f5c 158 spin_lock(&ip->i_flags_lock);
1a3e8f3d
DC
159 if (ip->i_ino != ino) {
160 trace_xfs_iget_skip(ip);
161 XFS_STATS_INC(xs_ig_frecycle);
162 error = EAGAIN;
163 goto out_error;
164 }
165
da353b0d 166
6441e549 167 /*
bc990f5c
CH
168 * If we are racing with another cache hit that is currently
169 * instantiating this inode or currently recycling it out of
170 * reclaimabe state, wait for the initialisation to complete
171 * before continuing.
172 *
173 * XXX(hch): eventually we should do something equivalent to
174 * wait_on_inode to wait for these flags to be cleared
175 * instead of polling for it.
6441e549 176 */
bc990f5c 177 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
0b1b213f 178 trace_xfs_iget_skip(ip);
6441e549 179 XFS_STATS_INC(xs_ig_frecycle);
bc990f5c 180 error = EAGAIN;
6441e549
DC
181 goto out_error;
182 }
da353b0d 183
bc990f5c
CH
184 /*
185 * If lookup is racing with unlink return an error immediately.
186 */
187 if (ip->i_d.di_mode == 0 && !(flags & XFS_IGET_CREATE)) {
188 error = ENOENT;
189 goto out_error;
190 }
bf904248 191
bc990f5c
CH
192 /*
193 * If IRECLAIMABLE is set, we've torn down the VFS inode already.
194 * Need to carefully get it back into useable state.
195 */
196 if (ip->i_flags & XFS_IRECLAIMABLE) {
0b1b213f 197 trace_xfs_iget_reclaim(ip);
da353b0d 198
6441e549 199 /*
f1f724e4
CH
200 * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode
201 * from stomping over us while we recycle the inode. We can't
202 * clear the radix tree reclaimable tag yet as it requires
203 * pag_ici_lock to be held exclusive.
6441e549 204 */
f1f724e4 205 ip->i_flags |= XFS_IRECLAIM;
bc990f5c
CH
206
207 spin_unlock(&ip->i_flags_lock);
1a3e8f3d 208 rcu_read_unlock();
bc990f5c
CH
209
210 error = -inode_init_always(mp->m_super, inode);
211 if (error) {
212 /*
213 * Re-initializing the inode failed, and we are in deep
214 * trouble. Try to re-add it to the reclaim list.
215 */
1a3e8f3d 216 rcu_read_lock();
bc990f5c
CH
217 spin_lock(&ip->i_flags_lock);
218
778e24bb
DC
219 ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM);
220 ASSERT(ip->i_flags & XFS_IRECLAIMABLE);
d2e078c3 221 trace_xfs_iget_reclaim_fail(ip);
6441e549 222 goto out_error;
da353b0d 223 }
f1f724e4 224
1a427ab0 225 spin_lock(&pag->pag_ici_lock);
f1f724e4 226 spin_lock(&ip->i_flags_lock);
778e24bb
DC
227
228 /*
229 * Clear the per-lifetime state in the inode as we are now
230 * effectively a new inode and need to return to the initial
231 * state before reuse occurs.
232 */
233 ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS;
f1f724e4
CH
234 ip->i_flags |= XFS_INEW;
235 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
eaff8079 236 inode->i_state = I_NEW;
dcfcf205
DC
237
238 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
239 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
dcfcf205 240
f1f724e4 241 spin_unlock(&ip->i_flags_lock);
1a427ab0 242 spin_unlock(&pag->pag_ici_lock);
bc990f5c 243 } else {
bf904248 244 /* If the VFS inode is being torn down, pause and try again. */
bc990f5c 245 if (!igrab(inode)) {
d2e078c3 246 trace_xfs_iget_skip(ip);
bc990f5c
CH
247 error = EAGAIN;
248 goto out_error;
249 }
1da177e4 250
bc990f5c
CH
251 /* We've got a live one. */
252 spin_unlock(&ip->i_flags_lock);
1a3e8f3d 253 rcu_read_unlock();
d2e078c3 254 trace_xfs_iget_hit(ip);
6441e549 255 }
da353b0d 256
6441e549
DC
257 if (lock_flags != 0)
258 xfs_ilock(ip, lock_flags);
da353b0d 259
5132ba8f 260 xfs_iflags_clear(ip, XFS_ISTALE | XFS_IDONTCACHE);
6441e549 261 XFS_STATS_INC(xs_ig_found);
0b1b213f 262
6441e549 263 return 0;
1da177e4 264
6441e549 265out_error:
bc990f5c 266 spin_unlock(&ip->i_flags_lock);
1a3e8f3d 267 rcu_read_unlock();
6441e549
DC
268 return error;
269}
270
271
272static int
273xfs_iget_cache_miss(
274 struct xfs_mount *mp,
275 struct xfs_perag *pag,
276 xfs_trans_t *tp,
277 xfs_ino_t ino,
278 struct xfs_inode **ipp,
6441e549 279 int flags,
0c3dc2b0 280 int lock_flags)
6441e549
DC
281{
282 struct xfs_inode *ip;
283 int error;
6441e549 284 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino);
5132ba8f 285 int iflags;
1da177e4 286
24f211ba
CH
287 ip = xfs_inode_alloc(mp, ino);
288 if (!ip)
289 return ENOMEM;
290
7b6259e7 291 error = xfs_iread(mp, tp, ip, flags);
6441e549 292 if (error)
24f211ba 293 goto out_destroy;
1da177e4 294
d2e078c3 295 trace_xfs_iget_miss(ip);
1da177e4 296
745b1f47 297 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
6441e549
DC
298 error = ENOENT;
299 goto out_destroy;
1da177e4
LT
300 }
301
302 /*
bad55843 303 * Preload the radix tree so we can insert safely under the
56e73ec4 304 * write spinlock. Note that we cannot sleep inside the preload
3ba31603
PW
305 * region. Since we can be called from transaction context, don't
306 * recurse into the file system.
1da177e4 307 */
3ba31603 308 if (radix_tree_preload(GFP_NOFS)) {
6441e549 309 error = EAGAIN;
ed93ec39
CH
310 goto out_destroy;
311 }
312
313 /*
314 * Because the inode hasn't been added to the radix-tree yet it can't
315 * be found by another thread, so we can do the non-sleeping lock here.
316 */
317 if (lock_flags) {
318 if (!xfs_ilock_nowait(ip, lock_flags))
319 BUG();
da353b0d 320 }
f338f903 321
f30d500f
DC
322 /*
323 * These values must be set before inserting the inode into the radix
324 * tree as the moment it is inserted a concurrent lookup (allowed by the
325 * RCU locking mechanism) can find it and that lookup must see that this
326 * is an inode currently under construction (i.e. that XFS_INEW is set).
327 * The ip->i_flags_lock that protects the XFS_INEW flag forms the
328 * memory barrier that ensures this detection works correctly at lookup
329 * time.
330 */
5132ba8f
DC
331 iflags = XFS_INEW;
332 if (flags & XFS_IGET_DONTCACHE)
333 iflags |= XFS_IDONTCACHE;
f30d500f 334 ip->i_udquot = ip->i_gdquot = NULL;
5132ba8f 335 xfs_iflags_set(ip, iflags);
6441e549
DC
336
337 /* insert the new inode */
f30d500f 338 spin_lock(&pag->pag_ici_lock);
da353b0d
DC
339 error = radix_tree_insert(&pag->pag_ici_root, agino, ip);
340 if (unlikely(error)) {
6441e549 341 WARN_ON(error != -EEXIST);
da353b0d 342 XFS_STATS_INC(xs_ig_dup);
6441e549 343 error = EAGAIN;
56e73ec4 344 goto out_preload_end;
1da177e4 345 }
1a427ab0 346 spin_unlock(&pag->pag_ici_lock);
da353b0d 347 radix_tree_preload_end();
0b1b213f 348
6441e549
DC
349 *ipp = ip;
350 return 0;
351
56e73ec4 352out_preload_end:
1a427ab0 353 spin_unlock(&pag->pag_ici_lock);
6441e549 354 radix_tree_preload_end();
56e73ec4
DC
355 if (lock_flags)
356 xfs_iunlock(ip, lock_flags);
6441e549 357out_destroy:
b36ec042
CH
358 __destroy_inode(VFS_I(ip));
359 xfs_inode_free(ip);
6441e549
DC
360 return error;
361}
362
363/*
364 * Look up an inode by number in the given file system.
365 * The inode is looked up in the cache held in each AG.
bf904248
DC
366 * If the inode is found in the cache, initialise the vfs inode
367 * if necessary.
6441e549
DC
368 *
369 * If it is not in core, read it in from the file system's device,
bf904248 370 * add it to the cache and initialise the vfs inode.
6441e549
DC
371 *
372 * The inode is locked according to the value of the lock_flags parameter.
373 * This flag parameter indicates how and if the inode's IO lock and inode lock
374 * should be taken.
375 *
376 * mp -- the mount point structure for the current file system. It points
377 * to the inode hash table.
378 * tp -- a pointer to the current transaction if there is one. This is
379 * simply passed through to the xfs_iread() call.
380 * ino -- the number of the inode desired. This is the unique identifier
381 * within the file system for the inode being requested.
382 * lock_flags -- flags indicating how to lock the inode. See the comment
383 * for xfs_ilock() for a list of valid values.
6441e549 384 */
bf904248
DC
385int
386xfs_iget(
6441e549
DC
387 xfs_mount_t *mp,
388 xfs_trans_t *tp,
389 xfs_ino_t ino,
390 uint flags,
391 uint lock_flags,
7b6259e7 392 xfs_inode_t **ipp)
6441e549
DC
393{
394 xfs_inode_t *ip;
395 int error;
396 xfs_perag_t *pag;
397 xfs_agino_t agino;
398
ad637a10
AE
399 /*
400 * xfs_reclaim_inode() uses the ILOCK to ensure an inode
401 * doesn't get freed while it's being referenced during a
402 * radix tree traversal here. It assumes this function
403 * aqcuires only the ILOCK (and therefore it has no need to
404 * involve the IOLOCK in this synchronization).
405 */
406 ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0);
407
d276734d 408 /* reject inode numbers outside existing AGs */
1a3e8f3d 409 if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount)
6441e549
DC
410 return EINVAL;
411
412 /* get the perag structure and ensure that it's inode capable */
5017e97d 413 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino));
6441e549
DC
414 agino = XFS_INO_TO_AGINO(mp, ino);
415
416again:
417 error = 0;
1a3e8f3d 418 rcu_read_lock();
6441e549
DC
419 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
420
421 if (ip) {
1a3e8f3d 422 error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags);
6441e549
DC
423 if (error)
424 goto out_error_or_again;
425 } else {
1a3e8f3d 426 rcu_read_unlock();
6441e549
DC
427 XFS_STATS_INC(xs_ig_missed);
428
7b6259e7 429 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip,
6441e549
DC
430 flags, lock_flags);
431 if (error)
432 goto out_error_or_again;
433 }
5017e97d 434 xfs_perag_put(pag);
1da177e4 435
1da177e4
LT
436 *ipp = ip;
437
438 /*
439 * If we have a real type for an on-disk inode, we can set ops(&unlock)
440 * now. If it's a new inode being created, xfs_ialloc will handle it.
441 */
bf904248 442 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
41be8bed 443 xfs_setup_inode(ip);
1da177e4 444 return 0;
6441e549
DC
445
446out_error_or_again:
447 if (error == EAGAIN) {
448 delay(1);
449 goto again;
450 }
5017e97d 451 xfs_perag_put(pag);
6441e549 452 return error;
1da177e4
LT
453}
454
1da177e4
LT
455/*
456 * This is a wrapper routine around the xfs_ilock() routine
457 * used to centralize some grungy code. It is used in places
458 * that wish to lock the inode solely for reading the extents.
459 * The reason these places can't just call xfs_ilock(SHARED)
460 * is that the inode lock also guards to bringing in of the
461 * extents from disk for a file in b-tree format. If the inode
462 * is in b-tree format, then we need to lock the inode exclusively
463 * until the extents are read in. Locking it exclusively all
464 * the time would limit our parallelism unnecessarily, though.
465 * What we do instead is check to see if the extents have been
466 * read in yet, and only lock the inode exclusively if they
467 * have not.
468 *
469 * The function returns a value which should be given to the
470 * corresponding xfs_iunlock_map_shared(). This value is
471 * the mode in which the lock was actually taken.
472 */
473uint
474xfs_ilock_map_shared(
475 xfs_inode_t *ip)
476{
477 uint lock_mode;
478
479 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
480 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
481 lock_mode = XFS_ILOCK_EXCL;
482 } else {
483 lock_mode = XFS_ILOCK_SHARED;
484 }
485
486 xfs_ilock(ip, lock_mode);
487
488 return lock_mode;
489}
490
491/*
492 * This is simply the unlock routine to go with xfs_ilock_map_shared().
493 * All it does is call xfs_iunlock() with the given lock_mode.
494 */
495void
496xfs_iunlock_map_shared(
497 xfs_inode_t *ip,
498 unsigned int lock_mode)
499{
500 xfs_iunlock(ip, lock_mode);
501}
502
503/*
504 * The xfs inode contains 2 locks: a multi-reader lock called the
505 * i_iolock and a multi-reader lock called the i_lock. This routine
506 * allows either or both of the locks to be obtained.
507 *
508 * The 2 locks should always be ordered so that the IO lock is
509 * obtained first in order to prevent deadlock.
510 *
511 * ip -- the inode being locked
512 * lock_flags -- this parameter indicates the inode's locks
513 * to be locked. It can be:
514 * XFS_IOLOCK_SHARED,
515 * XFS_IOLOCK_EXCL,
516 * XFS_ILOCK_SHARED,
517 * XFS_ILOCK_EXCL,
518 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
519 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
520 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
521 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
522 */
523void
579aa9ca
CH
524xfs_ilock(
525 xfs_inode_t *ip,
526 uint lock_flags)
1da177e4
LT
527{
528 /*
529 * You can't set both SHARED and EXCL for the same lock,
530 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
531 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
532 */
533 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
534 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
535 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
536 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
f7c66ce3 537 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
1da177e4 538
579aa9ca 539 if (lock_flags & XFS_IOLOCK_EXCL)
f7c66ce3 540 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
579aa9ca 541 else if (lock_flags & XFS_IOLOCK_SHARED)
f7c66ce3 542 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
579aa9ca
CH
543
544 if (lock_flags & XFS_ILOCK_EXCL)
f7c66ce3 545 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
579aa9ca 546 else if (lock_flags & XFS_ILOCK_SHARED)
f7c66ce3 547 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
579aa9ca 548
0b1b213f 549 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
1da177e4
LT
550}
551
552/*
553 * This is just like xfs_ilock(), except that the caller
554 * is guaranteed not to sleep. It returns 1 if it gets
555 * the requested locks and 0 otherwise. If the IO lock is
556 * obtained but the inode lock cannot be, then the IO lock
557 * is dropped before returning.
558 *
559 * ip -- the inode being locked
560 * lock_flags -- this parameter indicates the inode's locks to be
561 * to be locked. See the comment for xfs_ilock() for a list
562 * of valid values.
1da177e4
LT
563 */
564int
579aa9ca
CH
565xfs_ilock_nowait(
566 xfs_inode_t *ip,
567 uint lock_flags)
1da177e4 568{
1da177e4
LT
569 /*
570 * You can't set both SHARED and EXCL for the same lock,
571 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
572 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
573 */
574 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
575 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
576 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
577 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
f7c66ce3 578 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
1da177e4 579
1da177e4 580 if (lock_flags & XFS_IOLOCK_EXCL) {
579aa9ca
CH
581 if (!mrtryupdate(&ip->i_iolock))
582 goto out;
1da177e4 583 } else if (lock_flags & XFS_IOLOCK_SHARED) {
579aa9ca
CH
584 if (!mrtryaccess(&ip->i_iolock))
585 goto out;
1da177e4
LT
586 }
587 if (lock_flags & XFS_ILOCK_EXCL) {
579aa9ca
CH
588 if (!mrtryupdate(&ip->i_lock))
589 goto out_undo_iolock;
1da177e4 590 } else if (lock_flags & XFS_ILOCK_SHARED) {
579aa9ca
CH
591 if (!mrtryaccess(&ip->i_lock))
592 goto out_undo_iolock;
1da177e4 593 }
0b1b213f 594 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
1da177e4 595 return 1;
579aa9ca
CH
596
597 out_undo_iolock:
598 if (lock_flags & XFS_IOLOCK_EXCL)
599 mrunlock_excl(&ip->i_iolock);
600 else if (lock_flags & XFS_IOLOCK_SHARED)
601 mrunlock_shared(&ip->i_iolock);
602 out:
603 return 0;
1da177e4
LT
604}
605
606/*
607 * xfs_iunlock() is used to drop the inode locks acquired with
608 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
609 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
610 * that we know which locks to drop.
611 *
612 * ip -- the inode being unlocked
613 * lock_flags -- this parameter indicates the inode's locks to be
614 * to be unlocked. See the comment for xfs_ilock() for a list
615 * of valid values for this parameter.
616 *
617 */
618void
579aa9ca
CH
619xfs_iunlock(
620 xfs_inode_t *ip,
621 uint lock_flags)
1da177e4
LT
622{
623 /*
624 * You can't set both SHARED and EXCL for the same lock,
625 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
626 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
627 */
628 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
629 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
630 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
631 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
5b03ff1b 632 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
1da177e4
LT
633 ASSERT(lock_flags != 0);
634
579aa9ca
CH
635 if (lock_flags & XFS_IOLOCK_EXCL)
636 mrunlock_excl(&ip->i_iolock);
637 else if (lock_flags & XFS_IOLOCK_SHARED)
638 mrunlock_shared(&ip->i_iolock);
1da177e4 639
579aa9ca
CH
640 if (lock_flags & XFS_ILOCK_EXCL)
641 mrunlock_excl(&ip->i_lock);
642 else if (lock_flags & XFS_ILOCK_SHARED)
643 mrunlock_shared(&ip->i_lock);
1da177e4 644
0b1b213f 645 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
1da177e4
LT
646}
647
648/*
649 * give up write locks. the i/o lock cannot be held nested
650 * if it is being demoted.
651 */
652void
579aa9ca
CH
653xfs_ilock_demote(
654 xfs_inode_t *ip,
655 uint lock_flags)
1da177e4
LT
656{
657 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
658 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
659
579aa9ca 660 if (lock_flags & XFS_ILOCK_EXCL)
1da177e4 661 mrdemote(&ip->i_lock);
579aa9ca 662 if (lock_flags & XFS_IOLOCK_EXCL)
1da177e4 663 mrdemote(&ip->i_iolock);
0b1b213f
CH
664
665 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
579aa9ca
CH
666}
667
668#ifdef DEBUG
579aa9ca
CH
669int
670xfs_isilocked(
671 xfs_inode_t *ip,
672 uint lock_flags)
673{
f9369729
CH
674 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
675 if (!(lock_flags & XFS_ILOCK_SHARED))
676 return !!ip->i_lock.mr_writer;
677 return rwsem_is_locked(&ip->i_lock.mr_lock);
1da177e4 678 }
579aa9ca 679
f9369729
CH
680 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
681 if (!(lock_flags & XFS_IOLOCK_SHARED))
682 return !!ip->i_iolock.mr_writer;
683 return rwsem_is_locked(&ip->i_iolock.mr_lock);
579aa9ca
CH
684 }
685
f9369729
CH
686 ASSERT(0);
687 return 0;
1da177e4 688}
579aa9ca 689#endif
474fce06
CH
690
691void
692__xfs_iflock(
693 struct xfs_inode *ip)
694{
695 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
696 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
697
698 do {
699 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
700 if (xfs_isiflocked(ip))
701 io_schedule();
702 } while (!xfs_iflock_nowait(ip));
703
704 finish_wait(wq, &wait.wait);
705}
This page took 0.635482 seconds and 5 git commands to generate.