Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / fs / xfs / xfs_iget.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_acl.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_quota.h"
41 #include "xfs_utils.h"
42 #include "xfs_trans_priv.h"
43 #include "xfs_inode_item.h"
44 #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 */
52 STATIC struct xfs_inode *
53 xfs_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;
67 if (inode_init_always(mp->m_super, VFS_I(ip))) {
68 kmem_zone_free(xfs_inode_zone, ip);
69 return NULL;
70 }
71
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 ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
77
78 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
79
80 /* 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;
88 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
114
115 /* prevent anyone from using this yet */
116 VFS_I(ip)->i_state = I_NEW|I_LOCK;
117
118 return ip;
119 }
120
121 STATIC void
122 xfs_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
186 /*
187 * Check the validity of the inode we just found it the cache
188 */
189 static int
190 xfs_iget_cache_hit(
191 struct xfs_perag *pag,
192 struct xfs_inode *ip,
193 int flags,
194 int lock_flags) __releases(pag->pag_ici_lock)
195 {
196 struct inode *inode = VFS_I(ip);
197 struct xfs_mount *mp = ip->i_mount;
198 int error;
199
200 spin_lock(&ip->i_flags_lock);
201
202 /*
203 * 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.
211 */
212 if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) {
213 XFS_STATS_INC(xs_ig_frecycle);
214 error = EAGAIN;
215 goto out_error;
216 }
217
218 /*
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 }
225
226 /*
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) {
231 xfs_itrace_exit_tag(ip, "xfs_iget.alloc");
232
233 /*
234 * 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.
237 */
238 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;
264 goto out_error;
265 }
266
267 /* We've got a live one. */
268 spin_unlock(&ip->i_flags_lock);
269 read_unlock(&pag->pag_ici_lock);
270 }
271
272 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
280 out_error:
281 spin_unlock(&ip->i_flags_lock);
282 read_unlock(&pag->pag_ici_lock);
283 return error;
284 }
285
286
287 static int
288 xfs_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,
296 int lock_flags)
297 {
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
303 ip = xfs_inode_alloc(mp, ino);
304 if (!ip)
305 return ENOMEM;
306
307 error = xfs_iread(mp, tp, ip, bno, flags);
308 if (error)
309 goto out_destroy;
310
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
320 * write spinlock. Note that we cannot sleep inside the preload
321 * region.
322 */
323 if (radix_tree_preload(GFP_KERNEL)) {
324 error = EAGAIN;
325 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();
335 }
336
337 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;
347 goto out_preload_end;
348 }
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
359 out_preload_end:
360 write_unlock(&pag->pag_ici_lock);
361 radix_tree_preload_end();
362 if (lock_flags)
363 xfs_iunlock(ip, lock_flags);
364 out_destroy:
365 __destroy_inode(VFS_I(ip));
366 xfs_inode_free(ip);
367 return error;
368 }
369
370 /*
371 * Look up an inode by number in the given file system.
372 * The inode is looked up in the cache held in each AG.
373 * If the inode is found in the cache, initialise the vfs inode
374 * if necessary.
375 *
376 * If it is not in core, read it in from the file system's device,
377 * add it to the cache and initialise the vfs inode.
378 *
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 */
394 int
395 xfs_iget(
396 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 {
404 xfs_inode_t *ip;
405 int error;
406 xfs_perag_t *pag;
407 xfs_agino_t agino;
408
409 /* the radix tree exists only in inode capable AGs */
410 if (XFS_INO_TO_AGNO(mp, ino) >= mp->m_maxagi)
411 return EINVAL;
412
413 /* 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);
419
420 again:
421 error = 0;
422 read_lock(&pag->pag_ici_lock);
423 ip = radix_tree_lookup(&pag->pag_ici_root, agino);
424
425 if (ip) {
426 error = xfs_iget_cache_hit(pag, ip, flags, lock_flags);
427 if (error)
428 goto out_error_or_again;
429 } else {
430 read_unlock(&pag->pag_ici_lock);
431 XFS_STATS_INC(xs_ig_missed);
432
433 error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, bno,
434 flags, lock_flags);
435 if (error)
436 goto out_error_or_again;
437 }
438 xfs_put_perag(mp, pag);
439
440 *ipp = ip;
441
442 ASSERT(ip->i_df.if_ext_max ==
443 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
444 /*
445 * 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 */
448 if (xfs_iflags_test(ip, XFS_INEW) && ip->i_d.di_mode != 0)
449 xfs_setup_inode(ip);
450 return 0;
451
452 out_error_or_again:
453 if (error == EAGAIN) {
454 delay(1);
455 goto again;
456 }
457 xfs_put_perag(mp, pag);
458 return error;
459 }
460
461 /*
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 */
469 void
470 xfs_iput(xfs_inode_t *ip,
471 uint lock_flags)
472 {
473 xfs_itrace_entry(ip);
474 xfs_iunlock(ip, lock_flags);
475 IRELE(ip);
476 }
477
478 /*
479 * Special iput for brand-new inodes that are still locked
480 */
481 void
482 xfs_iput_new(
483 xfs_inode_t *ip,
484 uint lock_flags)
485 {
486 struct inode *inode = VFS_I(ip);
487
488 xfs_itrace_entry(ip);
489
490 if ((ip->i_d.di_mode == 0)) {
491 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
492 make_bad_inode(inode);
493 }
494 if (inode->i_state & I_NEW)
495 unlock_new_inode(inode);
496 if (lock_flags)
497 xfs_iunlock(ip, lock_flags);
498 IRELE(ip);
499 }
500
501 /*
502 * 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.
510 */
511 void
512 xfs_ireclaim(
513 struct xfs_inode *ip)
514 {
515 struct xfs_mount *mp = ip->i_mount;
516 struct xfs_perag *pag;
517
518 XFS_STATS_INC(xs_ig_reclaims);
519
520 /*
521 * 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.
524 */
525 pag = xfs_get_perag(mp, ip->i_ino);
526 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);
530
531 /*
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);
542 xfs_qm_dqdetach(ip);
543 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
544
545 xfs_inode_free(ip);
546 }
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 */
566 uint
567 xfs_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 */
588 void
589 xfs_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 */
616 void
617 xfs_ilock(
618 xfs_inode_t *ip,
619 uint lock_flags)
620 {
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));
630 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
631
632 if (lock_flags & XFS_IOLOCK_EXCL)
633 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
634 else if (lock_flags & XFS_IOLOCK_SHARED)
635 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
636
637 if (lock_flags & XFS_ILOCK_EXCL)
638 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
639 else if (lock_flags & XFS_ILOCK_SHARED)
640 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
641
642 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.
656 */
657 int
658 xfs_ilock_nowait(
659 xfs_inode_t *ip,
660 uint lock_flags)
661 {
662 /*
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));
671 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
672
673 if (lock_flags & XFS_IOLOCK_EXCL) {
674 if (!mrtryupdate(&ip->i_iolock))
675 goto out;
676 } else if (lock_flags & XFS_IOLOCK_SHARED) {
677 if (!mrtryaccess(&ip->i_iolock))
678 goto out;
679 }
680 if (lock_flags & XFS_ILOCK_EXCL) {
681 if (!mrtryupdate(&ip->i_lock))
682 goto out_undo_iolock;
683 } else if (lock_flags & XFS_ILOCK_SHARED) {
684 if (!mrtryaccess(&ip->i_lock))
685 goto out_undo_iolock;
686 }
687 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
688 return 1;
689
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;
697 }
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 */
711 void
712 xfs_iunlock(
713 xfs_inode_t *ip,
714 uint lock_flags)
715 {
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));
725 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY |
726 XFS_LOCK_DEP_MASK)) == 0);
727 ASSERT(lock_flags != 0);
728
729 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);
733
734 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);
738
739 if ((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) &&
740 !(lock_flags & XFS_IUNLOCK_NONOTIFY) && ip->i_itemp) {
741 /*
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 */
746 xfs_trans_unlocked_item(ip->i_itemp->ili_item.li_ailp,
747 (xfs_log_item_t*)(ip->i_itemp));
748 }
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 */
756 void
757 xfs_ilock_demote(
758 xfs_inode_t *ip,
759 uint lock_flags)
760 {
761 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
762 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
763
764 if (lock_flags & XFS_ILOCK_EXCL)
765 mrdemote(&ip->i_lock);
766 if (lock_flags & XFS_IOLOCK_EXCL)
767 mrdemote(&ip->i_iolock);
768 }
769
770 #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 */
778 int
779 xfs_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
799 #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 */
817 void
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
823 void
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
829 void
830 xfs_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
835 void
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
841 void
842 xfs_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 */
This page took 0.070451 seconds and 6 git commands to generate.