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