[XFS] Add lock annotations to xfs_trans_update_ail and
[GitHub/mt8127/android_kernel_alcatel_ttab.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"
a844f451 21#include "xfs_bit.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
LT
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
1da177e4 33#include "xfs_dir2_sf.h"
a844f451 34#include "xfs_attr_sf.h"
1da177e4
LT
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_btree.h"
38#include "xfs_ialloc.h"
1da177e4
LT
39#include "xfs_quota.h"
40#include "xfs_utils.h"
1da177e4
LT
41
42/*
43 * Initialize the inode hash table for the newly mounted file system.
44 * Choose an initial table size based on user specified value, else
45 * use a simple algorithm using the maximum number of inodes as an
46 * indicator for table size, and clamp it between one and some large
47 * number of pages.
48 */
49void
50xfs_ihash_init(xfs_mount_t *mp)
51{
52 __uint64_t icount;
77e4635a 53 uint i;
1da177e4
LT
54
55 if (!mp->m_ihsize) {
56 icount = mp->m_maxicount ? mp->m_maxicount :
57 (mp->m_sb.sb_dblocks << mp->m_sb.sb_inopblog);
58 mp->m_ihsize = 1 << max_t(uint, 8,
59 (xfs_highbit64(icount) + 1) / 2);
60 mp->m_ihsize = min_t(uint, mp->m_ihsize,
61 (64 * NBPP) / sizeof(xfs_ihash_t));
62 }
63
77e4635a
NS
64 mp->m_ihash = kmem_zalloc_greedy(&mp->m_ihsize,
65 NBPC * sizeof(xfs_ihash_t),
66 mp->m_ihsize * sizeof(xfs_ihash_t),
67 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
68 mp->m_ihsize /= sizeof(xfs_ihash_t);
69 for (i = 0; i < mp->m_ihsize; i++)
1da177e4 70 rwlock_init(&(mp->m_ihash[i].ih_lock));
1da177e4
LT
71}
72
73/*
74 * Free up structures allocated by xfs_ihash_init, at unmount time.
75 */
76void
77xfs_ihash_free(xfs_mount_t *mp)
78{
77e4635a 79 kmem_free(mp->m_ihash, mp->m_ihsize * sizeof(xfs_ihash_t));
1da177e4
LT
80 mp->m_ihash = NULL;
81}
82
83/*
84 * Initialize the inode cluster hash table for the newly mounted file system.
85 * Its size is derived from the ihash table size.
86 */
87void
88xfs_chash_init(xfs_mount_t *mp)
89{
90 uint i;
91
92 mp->m_chsize = max_t(uint, 1, mp->m_ihsize /
93 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog));
94 mp->m_chsize = min_t(uint, mp->m_chsize, mp->m_ihsize);
95 mp->m_chash = (xfs_chash_t *)kmem_zalloc(mp->m_chsize
96 * sizeof(xfs_chash_t),
efb8ad7e 97 KM_SLEEP | KM_LARGE);
1da177e4
LT
98 for (i = 0; i < mp->m_chsize; i++) {
99 spinlock_init(&mp->m_chash[i].ch_lock,"xfshash");
100 }
101}
102
103/*
104 * Free up structures allocated by xfs_chash_init, at unmount time.
105 */
106void
107xfs_chash_free(xfs_mount_t *mp)
108{
109 int i;
110
111 for (i = 0; i < mp->m_chsize; i++) {
112 spinlock_destroy(&mp->m_chash[i].ch_lock);
113 }
114
115 kmem_free(mp->m_chash, mp->m_chsize*sizeof(xfs_chash_t));
116 mp->m_chash = NULL;
117}
118
71bce256
NS
119/*
120 * Try to move an inode to the front of its hash list if possible
121 * (and if its not there already). Called right after obtaining
122 * the list version number and then dropping the read_lock on the
123 * hash list in question (which is done right after looking up the
124 * inode in question...).
125 */
126STATIC void
127xfs_ihash_promote(
128 xfs_ihash_t *ih,
129 xfs_inode_t *ip,
130 ulong version)
131{
132 xfs_inode_t *iq;
133
134 if ((ip->i_prevp != &ih->ih_next) && write_trylock(&ih->ih_lock)) {
135 if (likely(version == ih->ih_version)) {
136 /* remove from list */
137 if ((iq = ip->i_next)) {
138 iq->i_prevp = ip->i_prevp;
139 }
140 *ip->i_prevp = iq;
141
142 /* insert at list head */
143 iq = ih->ih_next;
144 iq->i_prevp = &ip->i_next;
145 ip->i_next = iq;
146 ip->i_prevp = &ih->ih_next;
147 ih->ih_next = ip;
148 }
149 write_unlock(&ih->ih_lock);
150 }
151}
152
1da177e4
LT
153/*
154 * Look up an inode by number in the given file system.
155 * The inode is looked up in the hash table for the file system
156 * represented by the mount point parameter mp. Each bucket of
157 * the hash table is guarded by an individual semaphore.
158 *
159 * If the inode is found in the hash table, its corresponding vnode
160 * is obtained with a call to vn_get(). This call takes care of
161 * coordination with the reclamation of the inode and vnode. Note
162 * that the vmap structure is filled in while holding the hash lock.
163 * This gives us the state of the inode/vnode when we found it and
164 * is used for coordination in vn_get().
165 *
166 * If it is not in core, read it in from the file system's device and
167 * add the inode into the hash table.
168 *
169 * The inode is locked according to the value of the lock_flags parameter.
170 * This flag parameter indicates how and if the inode's IO lock and inode lock
171 * should be taken.
172 *
173 * mp -- the mount point structure for the current file system. It points
174 * to the inode hash table.
175 * tp -- a pointer to the current transaction if there is one. This is
176 * simply passed through to the xfs_iread() call.
177 * ino -- the number of the inode desired. This is the unique identifier
178 * within the file system for the inode being requested.
179 * lock_flags -- flags indicating how to lock the inode. See the comment
180 * for xfs_ilock() for a list of valid values.
181 * bno -- the block number starting the buffer containing the inode,
182 * if known (as by bulkstat), else 0.
183 */
184STATIC int
185xfs_iget_core(
67fcaa73 186 bhv_vnode_t *vp,
1da177e4
LT
187 xfs_mount_t *mp,
188 xfs_trans_t *tp,
189 xfs_ino_t ino,
190 uint flags,
191 uint lock_flags,
192 xfs_inode_t **ipp,
193 xfs_daddr_t bno)
194{
195 xfs_ihash_t *ih;
196 xfs_inode_t *ip;
197 xfs_inode_t *iq;
67fcaa73 198 bhv_vnode_t *inode_vp;
1da177e4
LT
199 ulong version;
200 int error;
201 /* REFERENCED */
202 xfs_chash_t *ch;
203 xfs_chashlist_t *chl, *chlnew;
204 SPLDECL(s);
205
206
207 ih = XFS_IHASH(mp, ino);
208
209again:
210 read_lock(&ih->ih_lock);
211
212 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
213 if (ip->i_ino == ino) {
214 /*
215 * If INEW is set this inode is being set up
216 * we need to pause and try again.
217 */
218 if (ip->i_flags & XFS_INEW) {
219 read_unlock(&ih->ih_lock);
220 delay(1);
221 XFS_STATS_INC(xs_ig_frecycle);
222
223 goto again;
224 }
225
226 inode_vp = XFS_ITOV_NULL(ip);
227 if (inode_vp == NULL) {
228 /*
229 * If IRECLAIM is set this inode is
230 * on its way out of the system,
231 * we need to pause and try again.
232 */
233 if (ip->i_flags & XFS_IRECLAIM) {
234 read_unlock(&ih->ih_lock);
235 delay(1);
236 XFS_STATS_INC(xs_ig_frecycle);
237
238 goto again;
239 }
240
241 vn_trace_exit(vp, "xfs_iget.alloc",
242 (inst_t *)__return_address);
243
244 XFS_STATS_INC(xs_ig_found);
245
246 ip->i_flags &= ~XFS_IRECLAIMABLE;
71bce256 247 version = ih->ih_version;
1da177e4 248 read_unlock(&ih->ih_lock);
71bce256 249 xfs_ihash_promote(ih, ip, version);
1da177e4
LT
250
251 XFS_MOUNT_ILOCK(mp);
252 list_del_init(&ip->i_reclaim);
253 XFS_MOUNT_IUNLOCK(mp);
254
255 goto finish_inode;
256
257 } else if (vp != inode_vp) {
ec86dc02 258 struct inode *inode = vn_to_inode(inode_vp);
1da177e4
LT
259
260 /* The inode is being torn down, pause and
261 * try again.
262 */
263 if (inode->i_state & (I_FREEING | I_CLEAR)) {
264 read_unlock(&ih->ih_lock);
265 delay(1);
266 XFS_STATS_INC(xs_ig_frecycle);
267
268 goto again;
269 }
270/* Chances are the other vnode (the one in the inode) is being torn
271 * down right now, and we landed on top of it. Question is, what do
272 * we do? Unhook the old inode and hook up the new one?
273 */
274 cmn_err(CE_PANIC,
275 "xfs_iget_core: ambiguous vns: vp/0x%p, invp/0x%p",
276 inode_vp, vp);
277 }
278
71bce256
NS
279 /*
280 * Inode cache hit: if ip is not at the front of
281 * its hash chain, move it there now.
282 * Do this with the lock held for update, but
283 * do statistics after releasing the lock.
284 */
285 version = ih->ih_version;
1da177e4 286 read_unlock(&ih->ih_lock);
71bce256 287 xfs_ihash_promote(ih, ip, version);
1da177e4
LT
288 XFS_STATS_INC(xs_ig_found);
289
290finish_inode:
291 if (ip->i_d.di_mode == 0) {
745b1f47 292 if (!(flags & XFS_IGET_CREATE))
1da177e4
LT
293 return ENOENT;
294 xfs_iocore_inode_reinit(ip);
295 }
745b1f47 296
1da177e4
LT
297 if (lock_flags != 0)
298 xfs_ilock(ip, lock_flags);
299
300 ip->i_flags &= ~XFS_ISTALE;
301
302 vn_trace_exit(vp, "xfs_iget.found",
303 (inst_t *)__return_address);
304 goto return_ip;
305 }
306 }
307
308 /*
309 * Inode cache miss: save the hash chain version stamp and unlock
310 * the chain, so we don't deadlock in vn_alloc.
311 */
312 XFS_STATS_INC(xs_ig_missed);
313
314 version = ih->ih_version;
315
316 read_unlock(&ih->ih_lock);
317
318 /*
319 * Read the disk inode attributes into a new inode structure and get
320 * a new vnode for it. This should also initialize i_ino and i_mount.
321 */
745b1f47
NS
322 error = xfs_iread(mp, tp, ino, &ip, bno,
323 (flags & XFS_IGET_BULKSTAT) ? XFS_IMAP_BULKSTAT : 0);
324 if (error)
1da177e4 325 return error;
1da177e4
LT
326
327 vn_trace_exit(vp, "xfs_iget.alloc", (inst_t *)__return_address);
328
329 xfs_inode_lock_init(ip, vp);
330 xfs_iocore_inode_init(ip);
331
745b1f47 332 if (lock_flags)
1da177e4 333 xfs_ilock(ip, lock_flags);
745b1f47
NS
334
335 if ((ip->i_d.di_mode == 0) && !(flags & XFS_IGET_CREATE)) {
1da177e4
LT
336 xfs_idestroy(ip);
337 return ENOENT;
338 }
339
340 /*
341 * Put ip on its hash chain, unless someone else hashed a duplicate
342 * after we released the hash lock.
343 */
344 write_lock(&ih->ih_lock);
345
346 if (ih->ih_version != version) {
347 for (iq = ih->ih_next; iq != NULL; iq = iq->i_next) {
348 if (iq->i_ino == ino) {
349 write_unlock(&ih->ih_lock);
350 xfs_idestroy(ip);
351
352 XFS_STATS_INC(xs_ig_dup);
353 goto again;
354 }
355 }
356 }
357
358 /*
359 * These values _must_ be set before releasing ihlock!
360 */
361 ip->i_hash = ih;
362 if ((iq = ih->ih_next)) {
363 iq->i_prevp = &ip->i_next;
364 }
365 ip->i_next = iq;
366 ip->i_prevp = &ih->ih_next;
367 ih->ih_next = ip;
368 ip->i_udquot = ip->i_gdquot = NULL;
369 ih->ih_version++;
370 ip->i_flags |= XFS_INEW;
371
372 write_unlock(&ih->ih_lock);
373
374 /*
375 * put ip on its cluster's hash chain
376 */
377 ASSERT(ip->i_chash == NULL && ip->i_cprev == NULL &&
378 ip->i_cnext == NULL);
379
380 chlnew = NULL;
381 ch = XFS_CHASH(mp, ip->i_blkno);
382 chlredo:
383 s = mutex_spinlock(&ch->ch_lock);
384 for (chl = ch->ch_list; chl != NULL; chl = chl->chl_next) {
385 if (chl->chl_blkno == ip->i_blkno) {
386
387 /* insert this inode into the doubly-linked list
388 * where chl points */
389 if ((iq = chl->chl_ip)) {
390 ip->i_cprev = iq->i_cprev;
391 iq->i_cprev->i_cnext = ip;
392 iq->i_cprev = ip;
393 ip->i_cnext = iq;
394 } else {
395 ip->i_cnext = ip;
396 ip->i_cprev = ip;
397 }
398 chl->chl_ip = ip;
399 ip->i_chash = chl;
400 break;
401 }
402 }
403
404 /* no hash list found for this block; add a new hash list */
405 if (chl == NULL) {
406 if (chlnew == NULL) {
407 mutex_spinunlock(&ch->ch_lock, s);
408 ASSERT(xfs_chashlist_zone != NULL);
409 chlnew = (xfs_chashlist_t *)
410 kmem_zone_alloc(xfs_chashlist_zone,
411 KM_SLEEP);
412 ASSERT(chlnew != NULL);
413 goto chlredo;
414 } else {
415 ip->i_cnext = ip;
416 ip->i_cprev = ip;
417 ip->i_chash = chlnew;
418 chlnew->chl_ip = ip;
419 chlnew->chl_blkno = ip->i_blkno;
1fc5d959
DC
420 if (ch->ch_list)
421 ch->ch_list->chl_prev = chlnew;
1da177e4 422 chlnew->chl_next = ch->ch_list;
1fc5d959 423 chlnew->chl_prev = NULL;
1da177e4
LT
424 ch->ch_list = chlnew;
425 chlnew = NULL;
426 }
427 } else {
428 if (chlnew != NULL) {
429 kmem_zone_free(xfs_chashlist_zone, chlnew);
430 }
431 }
432
433 mutex_spinunlock(&ch->ch_lock, s);
434
435
436 /*
437 * Link ip to its mount and thread it on the mount's inode list.
438 */
439 XFS_MOUNT_ILOCK(mp);
440 if ((iq = mp->m_inodes)) {
441 ASSERT(iq->i_mprev->i_mnext == iq);
442 ip->i_mprev = iq->i_mprev;
443 iq->i_mprev->i_mnext = ip;
444 iq->i_mprev = ip;
445 ip->i_mnext = iq;
446 } else {
447 ip->i_mnext = ip;
448 ip->i_mprev = ip;
449 }
450 mp->m_inodes = ip;
451
452 XFS_MOUNT_IUNLOCK(mp);
453
454 return_ip:
455 ASSERT(ip->i_df.if_ext_max ==
456 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t));
457
458 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
459 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
460
461 *ipp = ip;
462
463 /*
464 * If we have a real type for an on-disk inode, we can set ops(&unlock)
465 * now. If it's a new inode being created, xfs_ialloc will handle it.
466 */
b83bd138 467 bhv_vfs_init_vnode(XFS_MTOVFS(mp), vp, XFS_ITOBHV(ip), 1);
1da177e4
LT
468
469 return 0;
470}
471
472
473/*
474 * The 'normal' internal xfs_iget, if needed it will
475 * 'allocate', or 'get', the vnode.
476 */
477int
478xfs_iget(
479 xfs_mount_t *mp,
480 xfs_trans_t *tp,
481 xfs_ino_t ino,
482 uint flags,
483 uint lock_flags,
484 xfs_inode_t **ipp,
485 xfs_daddr_t bno)
486{
487 struct inode *inode;
67fcaa73 488 bhv_vnode_t *vp = NULL;
1da177e4
LT
489 int error;
490
1da177e4
LT
491 XFS_STATS_INC(xs_ig_attempts);
492
ba403ab4 493retry:
1da177e4 494 if ((inode = iget_locked(XFS_MTOVFS(mp)->vfs_super, ino))) {
1da177e4 495 xfs_inode_t *ip;
1da177e4 496
ec86dc02 497 vp = vn_from_inode(inode);
1da177e4 498 if (inode->i_state & I_NEW) {
1da177e4
LT
499 vn_initialize(inode);
500 error = xfs_iget_core(vp, mp, tp, ino, flags,
501 lock_flags, ipp, bno);
502 if (error) {
503 vn_mark_bad(vp);
504 if (inode->i_state & I_NEW)
505 unlock_new_inode(inode);
506 iput(inode);
507 }
508 } else {
ba403ab4
CH
509 /*
510 * If the inode is not fully constructed due to
c41564b5 511 * filehandle mismatches wait for the inode to go
ba403ab4
CH
512 * away and try again.
513 *
514 * iget_locked will call __wait_on_freeing_inode
515 * to wait for the inode to go away.
516 */
517 if (is_bad_inode(inode) ||
75e17b3c 518 ((ip = xfs_vtoi(vp)) == NULL)) {
1da177e4 519 iput(inode);
ba403ab4
CH
520 delay(1);
521 goto retry;
1da177e4
LT
522 }
523
1da177e4
LT
524 if (lock_flags != 0)
525 xfs_ilock(ip, lock_flags);
1da177e4
LT
526 XFS_STATS_INC(xs_ig_found);
527 *ipp = ip;
528 error = 0;
529 }
530 } else
531 error = ENOMEM; /* If we got no inode we are out of memory */
532
533 return error;
534}
535
536/*
537 * Do the setup for the various locks within the incore inode.
538 */
539void
540xfs_inode_lock_init(
541 xfs_inode_t *ip,
67fcaa73 542 bhv_vnode_t *vp)
1da177e4
LT
543{
544 mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
545 "xfsino", (long)vp->v_number);
546 mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", vp->v_number);
547 init_waitqueue_head(&ip->i_ipin_wait);
548 atomic_set(&ip->i_pincount, 0);
549 init_sema(&ip->i_flock, 1, "xfsfino", vp->v_number);
550}
551
552/*
553 * Look for the inode corresponding to the given ino in the hash table.
554 * If it is there and its i_transp pointer matches tp, return it.
555 * Otherwise, return NULL.
556 */
557xfs_inode_t *
558xfs_inode_incore(xfs_mount_t *mp,
559 xfs_ino_t ino,
560 xfs_trans_t *tp)
561{
562 xfs_ihash_t *ih;
563 xfs_inode_t *ip;
71bce256 564 ulong version;
1da177e4
LT
565
566 ih = XFS_IHASH(mp, ino);
567 read_lock(&ih->ih_lock);
568 for (ip = ih->ih_next; ip != NULL; ip = ip->i_next) {
569 if (ip->i_ino == ino) {
570 /*
571 * If we find it and tp matches, return it.
71bce256
NS
572 * Also move it to the front of the hash list
573 * if we find it and it is not already there.
1da177e4
LT
574 * Otherwise break from the loop and return
575 * NULL.
576 */
577 if (ip->i_transp == tp) {
71bce256 578 version = ih->ih_version;
1da177e4 579 read_unlock(&ih->ih_lock);
71bce256 580 xfs_ihash_promote(ih, ip, version);
1da177e4
LT
581 return (ip);
582 }
583 break;
584 }
585 }
586 read_unlock(&ih->ih_lock);
587 return (NULL);
588}
589
590/*
591 * Decrement reference count of an inode structure and unlock it.
592 *
593 * ip -- the inode being released
594 * lock_flags -- this parameter indicates the inode's locks to be
595 * to be released. See the comment on xfs_iunlock() for a list
596 * of valid values.
597 */
598void
599xfs_iput(xfs_inode_t *ip,
600 uint lock_flags)
601{
67fcaa73 602 bhv_vnode_t *vp = XFS_ITOV(ip);
1da177e4
LT
603
604 vn_trace_entry(vp, "xfs_iput", (inst_t *)__return_address);
1da177e4 605 xfs_iunlock(ip, lock_flags);
1da177e4
LT
606 VN_RELE(vp);
607}
608
609/*
610 * Special iput for brand-new inodes that are still locked
611 */
612void
613xfs_iput_new(xfs_inode_t *ip,
614 uint lock_flags)
615{
67fcaa73 616 bhv_vnode_t *vp = XFS_ITOV(ip);
ec86dc02 617 struct inode *inode = vn_to_inode(vp);
1da177e4
LT
618
619 vn_trace_entry(vp, "xfs_iput_new", (inst_t *)__return_address);
620
621 if ((ip->i_d.di_mode == 0)) {
622 ASSERT(!(ip->i_flags & XFS_IRECLAIMABLE));
623 vn_mark_bad(vp);
624 }
625 if (inode->i_state & I_NEW)
626 unlock_new_inode(inode);
627 if (lock_flags)
628 xfs_iunlock(ip, lock_flags);
629 VN_RELE(vp);
630}
631
632
633/*
634 * This routine embodies the part of the reclaim code that pulls
635 * the inode from the inode hash table and the mount structure's
636 * inode list.
637 * This should only be called from xfs_reclaim().
638 */
639void
640xfs_ireclaim(xfs_inode_t *ip)
641{
67fcaa73 642 bhv_vnode_t *vp;
1da177e4
LT
643
644 /*
645 * Remove from old hash list and mount list.
646 */
647 XFS_STATS_INC(xs_ig_reclaims);
648
649 xfs_iextract(ip);
650
651 /*
652 * Here we do a spurious inode lock in order to coordinate with
653 * xfs_sync(). This is because xfs_sync() references the inodes
654 * in the mount list without taking references on the corresponding
655 * vnodes. We make that OK here by ensuring that we wait until
656 * the inode is unlocked in xfs_sync() before we go ahead and
657 * free it. We get both the regular lock and the io lock because
658 * the xfs_sync() code may need to drop the regular one but will
659 * still hold the io lock.
660 */
661 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
662
663 /*
664 * Release dquots (and their references) if any. An inode may escape
665 * xfs_inactive and get here via vn_alloc->vn_reclaim path.
666 */
667 XFS_QM_DQDETACH(ip->i_mount, ip);
668
669 /*
670 * Pull our behavior descriptor from the vnode chain.
671 */
672 vp = XFS_ITOV_NULL(ip);
673 if (vp) {
674 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
675 }
676
677 /*
678 * Free all memory associated with the inode.
679 */
680 xfs_idestroy(ip);
681}
682
683/*
684 * This routine removes an about-to-be-destroyed inode from
685 * all of the lists in which it is located with the exception
686 * of the behavior chain.
687 */
688void
689xfs_iextract(
690 xfs_inode_t *ip)
691{
692 xfs_ihash_t *ih;
693 xfs_inode_t *iq;
694 xfs_mount_t *mp;
695 xfs_chash_t *ch;
696 xfs_chashlist_t *chl, *chm;
697 SPLDECL(s);
698
699 ih = ip->i_hash;
700 write_lock(&ih->ih_lock);
701 if ((iq = ip->i_next)) {
702 iq->i_prevp = ip->i_prevp;
703 }
704 *ip->i_prevp = iq;
71bce256 705 ih->ih_version++;
1da177e4
LT
706 write_unlock(&ih->ih_lock);
707
708 /*
709 * Remove from cluster hash list
710 * 1) delete the chashlist if this is the last inode on the chashlist
711 * 2) unchain from list of inodes
712 * 3) point chashlist->chl_ip to 'chl_next' if to this inode.
713 */
714 mp = ip->i_mount;
715 ch = XFS_CHASH(mp, ip->i_blkno);
716 s = mutex_spinlock(&ch->ch_lock);
717
718 if (ip->i_cnext == ip) {
719 /* Last inode on chashlist */
720 ASSERT(ip->i_cnext == ip && ip->i_cprev == ip);
721 ASSERT(ip->i_chash != NULL);
722 chm=NULL;
1fc5d959
DC
723 chl = ip->i_chash;
724 if (chl->chl_prev)
725 chl->chl_prev->chl_next = chl->chl_next;
726 else
727 ch->ch_list = chl->chl_next;
728 if (chl->chl_next)
729 chl->chl_next->chl_prev = chl->chl_prev;
730 kmem_zone_free(xfs_chashlist_zone, chl);
731 } else {
1da177e4
LT
732 /* delete one inode from a non-empty list */
733 iq = ip->i_cnext;
734 iq->i_cprev = ip->i_cprev;
735 ip->i_cprev->i_cnext = iq;
736 if (ip->i_chash->chl_ip == ip) {
737 ip->i_chash->chl_ip = iq;
738 }
739 ip->i_chash = __return_address;
740 ip->i_cprev = __return_address;
741 ip->i_cnext = __return_address;
742 }
743 mutex_spinunlock(&ch->ch_lock, s);
744
745 /*
746 * Remove from mount's inode list.
747 */
748 XFS_MOUNT_ILOCK(mp);
749 ASSERT((ip->i_mnext != NULL) && (ip->i_mprev != NULL));
750 iq = ip->i_mnext;
751 iq->i_mprev = ip->i_mprev;
752 ip->i_mprev->i_mnext = iq;
753
754 /*
755 * Fix up the head pointer if it points to the inode being deleted.
756 */
757 if (mp->m_inodes == ip) {
758 if (ip == iq) {
759 mp->m_inodes = NULL;
760 } else {
761 mp->m_inodes = iq;
762 }
763 }
764
765 /* Deal with the deleted inodes list */
766 list_del_init(&ip->i_reclaim);
767
768 mp->m_ireclaims++;
769 XFS_MOUNT_IUNLOCK(mp);
770}
771
772/*
773 * This is a wrapper routine around the xfs_ilock() routine
774 * used to centralize some grungy code. It is used in places
775 * that wish to lock the inode solely for reading the extents.
776 * The reason these places can't just call xfs_ilock(SHARED)
777 * is that the inode lock also guards to bringing in of the
778 * extents from disk for a file in b-tree format. If the inode
779 * is in b-tree format, then we need to lock the inode exclusively
780 * until the extents are read in. Locking it exclusively all
781 * the time would limit our parallelism unnecessarily, though.
782 * What we do instead is check to see if the extents have been
783 * read in yet, and only lock the inode exclusively if they
784 * have not.
785 *
786 * The function returns a value which should be given to the
787 * corresponding xfs_iunlock_map_shared(). This value is
788 * the mode in which the lock was actually taken.
789 */
790uint
791xfs_ilock_map_shared(
792 xfs_inode_t *ip)
793{
794 uint lock_mode;
795
796 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
797 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
798 lock_mode = XFS_ILOCK_EXCL;
799 } else {
800 lock_mode = XFS_ILOCK_SHARED;
801 }
802
803 xfs_ilock(ip, lock_mode);
804
805 return lock_mode;
806}
807
808/*
809 * This is simply the unlock routine to go with xfs_ilock_map_shared().
810 * All it does is call xfs_iunlock() with the given lock_mode.
811 */
812void
813xfs_iunlock_map_shared(
814 xfs_inode_t *ip,
815 unsigned int lock_mode)
816{
817 xfs_iunlock(ip, lock_mode);
818}
819
820/*
821 * The xfs inode contains 2 locks: a multi-reader lock called the
822 * i_iolock and a multi-reader lock called the i_lock. This routine
823 * allows either or both of the locks to be obtained.
824 *
825 * The 2 locks should always be ordered so that the IO lock is
826 * obtained first in order to prevent deadlock.
827 *
828 * ip -- the inode being locked
829 * lock_flags -- this parameter indicates the inode's locks
830 * to be locked. It can be:
831 * XFS_IOLOCK_SHARED,
832 * XFS_IOLOCK_EXCL,
833 * XFS_ILOCK_SHARED,
834 * XFS_ILOCK_EXCL,
835 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
836 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
837 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
838 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
839 */
840void
841xfs_ilock(xfs_inode_t *ip,
842 uint lock_flags)
843{
844 /*
845 * You can't set both SHARED and EXCL for the same lock,
846 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
847 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
848 */
849 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
850 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
851 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
852 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
853 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
854
855 if (lock_flags & XFS_IOLOCK_EXCL) {
856 mrupdate(&ip->i_iolock);
857 } else if (lock_flags & XFS_IOLOCK_SHARED) {
858 mraccess(&ip->i_iolock);
859 }
860 if (lock_flags & XFS_ILOCK_EXCL) {
861 mrupdate(&ip->i_lock);
862 } else if (lock_flags & XFS_ILOCK_SHARED) {
863 mraccess(&ip->i_lock);
864 }
865 xfs_ilock_trace(ip, 1, lock_flags, (inst_t *)__return_address);
866}
867
868/*
869 * This is just like xfs_ilock(), except that the caller
870 * is guaranteed not to sleep. It returns 1 if it gets
871 * the requested locks and 0 otherwise. If the IO lock is
872 * obtained but the inode lock cannot be, then the IO lock
873 * is dropped before returning.
874 *
875 * ip -- the inode being locked
876 * lock_flags -- this parameter indicates the inode's locks to be
877 * to be locked. See the comment for xfs_ilock() for a list
878 * of valid values.
879 *
880 */
881int
882xfs_ilock_nowait(xfs_inode_t *ip,
883 uint lock_flags)
884{
885 int iolocked;
886 int ilocked;
887
888 /*
889 * You can't set both SHARED and EXCL for the same lock,
890 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
891 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
892 */
893 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
894 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
895 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
896 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
897 ASSERT((lock_flags & ~XFS_LOCK_MASK) == 0);
898
899 iolocked = 0;
900 if (lock_flags & XFS_IOLOCK_EXCL) {
901 iolocked = mrtryupdate(&ip->i_iolock);
902 if (!iolocked) {
903 return 0;
904 }
905 } else if (lock_flags & XFS_IOLOCK_SHARED) {
906 iolocked = mrtryaccess(&ip->i_iolock);
907 if (!iolocked) {
908 return 0;
909 }
910 }
911 if (lock_flags & XFS_ILOCK_EXCL) {
912 ilocked = mrtryupdate(&ip->i_lock);
913 if (!ilocked) {
914 if (iolocked) {
915 mrunlock(&ip->i_iolock);
916 }
917 return 0;
918 }
919 } else if (lock_flags & XFS_ILOCK_SHARED) {
920 ilocked = mrtryaccess(&ip->i_lock);
921 if (!ilocked) {
922 if (iolocked) {
923 mrunlock(&ip->i_iolock);
924 }
925 return 0;
926 }
927 }
928 xfs_ilock_trace(ip, 2, lock_flags, (inst_t *)__return_address);
929 return 1;
930}
931
932/*
933 * xfs_iunlock() is used to drop the inode locks acquired with
934 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
935 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
936 * that we know which locks to drop.
937 *
938 * ip -- the inode being unlocked
939 * lock_flags -- this parameter indicates the inode's locks to be
940 * to be unlocked. See the comment for xfs_ilock() for a list
941 * of valid values for this parameter.
942 *
943 */
944void
945xfs_iunlock(xfs_inode_t *ip,
946 uint lock_flags)
947{
948 /*
949 * You can't set both SHARED and EXCL for the same lock,
950 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
951 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
952 */
953 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
954 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
955 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
956 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
957 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_IUNLOCK_NONOTIFY)) == 0);
958 ASSERT(lock_flags != 0);
959
960 if (lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) {
961 ASSERT(!(lock_flags & XFS_IOLOCK_SHARED) ||
962 (ismrlocked(&ip->i_iolock, MR_ACCESS)));
963 ASSERT(!(lock_flags & XFS_IOLOCK_EXCL) ||
964 (ismrlocked(&ip->i_iolock, MR_UPDATE)));
965 mrunlock(&ip->i_iolock);
966 }
967
968 if (lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) {
969 ASSERT(!(lock_flags & XFS_ILOCK_SHARED) ||
970 (ismrlocked(&ip->i_lock, MR_ACCESS)));
971 ASSERT(!(lock_flags & XFS_ILOCK_EXCL) ||
972 (ismrlocked(&ip->i_lock, MR_UPDATE)));
973 mrunlock(&ip->i_lock);
974
975 /*
976 * Let the AIL know that this item has been unlocked in case
977 * it is in the AIL and anyone is waiting on it. Don't do
978 * this if the caller has asked us not to.
979 */
980 if (!(lock_flags & XFS_IUNLOCK_NONOTIFY) &&
981 ip->i_itemp != NULL) {
982 xfs_trans_unlocked_item(ip->i_mount,
983 (xfs_log_item_t*)(ip->i_itemp));
984 }
985 }
986 xfs_ilock_trace(ip, 3, lock_flags, (inst_t *)__return_address);
987}
988
989/*
990 * give up write locks. the i/o lock cannot be held nested
991 * if it is being demoted.
992 */
993void
994xfs_ilock_demote(xfs_inode_t *ip,
995 uint lock_flags)
996{
997 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
998 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
999
1000 if (lock_flags & XFS_ILOCK_EXCL) {
1001 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
1002 mrdemote(&ip->i_lock);
1003 }
1004 if (lock_flags & XFS_IOLOCK_EXCL) {
1005 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1006 mrdemote(&ip->i_iolock);
1007 }
1008}
1009
1010/*
1011 * The following three routines simply manage the i_flock
1012 * semaphore embedded in the inode. This semaphore synchronizes
1013 * processes attempting to flush the in-core inode back to disk.
1014 */
1015void
1016xfs_iflock(xfs_inode_t *ip)
1017{
1018 psema(&(ip->i_flock), PINOD|PLTWAIT);
1019}
1020
1021int
1022xfs_iflock_nowait(xfs_inode_t *ip)
1023{
1024 return (cpsema(&(ip->i_flock)));
1025}
1026
1027void
1028xfs_ifunlock(xfs_inode_t *ip)
1029{
0d8fee32 1030 ASSERT(issemalocked(&(ip->i_flock)));
1da177e4
LT
1031 vsema(&(ip->i_flock));
1032}