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