[PATCH] Fix up 'linux-dvb' maintainers entry
[linux-2.6/history.git] / fs / inode.c
blob4ba2baca5b0cabf750102850bea77733559fed3f
1 /*
2 * linux/fs/inode.c
4 * (C) 1997 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/fs.h>
9 #include <linux/mm.h>
10 #include <linux/dcache.h>
11 #include <linux/init.h>
12 #include <linux/quotaops.h>
13 #include <linux/slab.h>
14 #include <linux/writeback.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/wait.h>
18 #include <linux/hash.h>
19 #include <linux/swap.h>
20 #include <linux/security.h>
21 #include <linux/pagemap.h>
22 #include <linux/cdev.h>
25 * This is needed for the following functions:
26 * - inode_has_buffers
27 * - invalidate_inode_buffers
28 * - fsync_bdev
29 * - invalidate_bdev
31 * FIXME: remove all knowledge of the buffer layer from this file
33 #include <linux/buffer_head.h>
36 * New inode.c implementation.
38 * This implementation has the basic premise of trying
39 * to be extremely low-overhead and SMP-safe, yet be
40 * simple enough to be "obviously correct".
42 * Famous last words.
45 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
47 /* #define INODE_PARANOIA 1 */
48 /* #define INODE_DEBUG 1 */
51 * Inode lookup is no longer as critical as it used to be:
52 * most of the lookups are going to be through the dcache.
54 #define I_HASHBITS i_hash_shift
55 #define I_HASHMASK i_hash_mask
57 static unsigned int i_hash_mask;
58 static unsigned int i_hash_shift;
61 * Each inode can be on two separate lists. One is
62 * the hash list of the inode, used for lookups. The
63 * other linked list is the "type" list:
64 * "in_use" - valid inode, i_count > 0, i_nlink > 0
65 * "dirty" - as "in_use" but also dirty
66 * "unused" - valid inode, i_count = 0
68 * A "dirty" list is maintained for each super block,
69 * allowing for low-overhead inode sync() operations.
72 LIST_HEAD(inode_in_use);
73 LIST_HEAD(inode_unused);
74 static struct hlist_head *inode_hashtable;
77 * A simple spinlock to protect the list manipulations.
79 * NOTE! You also have to own the lock if you change
80 * the i_state of an inode while it is in use..
82 spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
85 * iprune_sem provides exclusion between the kswapd or try_to_free_pages
86 * icache shrinking path, and the umount path. Without this exclusion,
87 * by the time prune_icache calls iput for the inode whose pages it has
88 * been invalidating, or by the time it calls clear_inode & destroy_inode
89 * from its final dispose_list, the struct super_block they refer to
90 * (for inode->i_sb->s_op) may already have been freed and reused.
92 static DECLARE_MUTEX(iprune_sem);
95 * Statistics gathering..
97 struct inodes_stat_t inodes_stat;
99 static kmem_cache_t * inode_cachep;
101 static struct inode *alloc_inode(struct super_block *sb)
103 static struct address_space_operations empty_aops;
104 static struct inode_operations empty_iops;
105 static struct file_operations empty_fops;
106 struct inode *inode;
108 if (sb->s_op->alloc_inode)
109 inode = sb->s_op->alloc_inode(sb);
110 else
111 inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
113 if (inode) {
114 struct address_space * const mapping = &inode->i_data;
116 inode->i_sb = sb;
117 inode->i_blkbits = sb->s_blocksize_bits;
118 inode->i_flags = 0;
119 atomic_set(&inode->i_count, 1);
120 inode->i_sock = 0;
121 inode->i_op = &empty_iops;
122 inode->i_fop = &empty_fops;
123 inode->i_nlink = 1;
124 atomic_set(&inode->i_writecount, 0);
125 inode->i_size = 0;
126 inode->i_blocks = 0;
127 inode->i_bytes = 0;
128 inode->i_generation = 0;
129 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
130 inode->i_pipe = NULL;
131 inode->i_bdev = NULL;
132 inode->i_cdev = NULL;
133 inode->i_rdev = 0;
134 inode->i_security = NULL;
135 if (security_inode_alloc(inode)) {
136 if (inode->i_sb->s_op->destroy_inode)
137 inode->i_sb->s_op->destroy_inode(inode);
138 else
139 kmem_cache_free(inode_cachep, (inode));
140 return NULL;
143 mapping->a_ops = &empty_aops;
144 mapping->host = inode;
145 mapping->flags = 0;
146 mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
147 mapping->dirtied_when = 0;
148 mapping->assoc_mapping = NULL;
149 mapping->backing_dev_info = &default_backing_dev_info;
150 if (sb->s_bdev)
151 mapping->backing_dev_info = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
152 memset(&inode->u, 0, sizeof(inode->u));
153 inode->i_mapping = mapping;
155 return inode;
158 void destroy_inode(struct inode *inode)
160 if (inode_has_buffers(inode))
161 BUG();
162 security_inode_free(inode);
163 if (inode->i_sb->s_op->destroy_inode)
164 inode->i_sb->s_op->destroy_inode(inode);
165 else
166 kmem_cache_free(inode_cachep, (inode));
171 * These are initializations that only need to be done
172 * once, because the fields are idempotent across use
173 * of the inode, so let the slab aware of that.
175 void inode_init_once(struct inode *inode)
177 memset(inode, 0, sizeof(*inode));
178 INIT_HLIST_NODE(&inode->i_hash);
179 INIT_LIST_HEAD(&inode->i_data.clean_pages);
180 INIT_LIST_HEAD(&inode->i_data.dirty_pages);
181 INIT_LIST_HEAD(&inode->i_data.locked_pages);
182 INIT_LIST_HEAD(&inode->i_data.io_pages);
183 INIT_LIST_HEAD(&inode->i_dentry);
184 INIT_LIST_HEAD(&inode->i_devices);
185 sema_init(&inode->i_sem, 1);
186 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
187 spin_lock_init(&inode->i_data.page_lock);
188 init_MUTEX(&inode->i_data.i_shared_sem);
189 atomic_set(&inode->i_data.truncate_count, 0);
190 INIT_LIST_HEAD(&inode->i_data.private_list);
191 spin_lock_init(&inode->i_data.private_lock);
192 INIT_LIST_HEAD(&inode->i_data.i_mmap);
193 INIT_LIST_HEAD(&inode->i_data.i_mmap_shared);
194 spin_lock_init(&inode->i_lock);
195 i_size_ordered_init(inode);
198 EXPORT_SYMBOL(inode_init_once);
200 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
202 struct inode * inode = (struct inode *) foo;
204 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
205 SLAB_CTOR_CONSTRUCTOR)
206 inode_init_once(inode);
210 * inode_lock must be held
212 void __iget(struct inode * inode)
214 if (atomic_read(&inode->i_count)) {
215 atomic_inc(&inode->i_count);
216 return;
218 atomic_inc(&inode->i_count);
219 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
220 list_del(&inode->i_list);
221 list_add(&inode->i_list, &inode_in_use);
223 inodes_stat.nr_unused--;
227 * clear_inode - clear an inode
228 * @inode: inode to clear
230 * This is called by the filesystem to tell us
231 * that the inode is no longer useful. We just
232 * terminate it with extreme prejudice.
234 void clear_inode(struct inode *inode)
236 invalidate_inode_buffers(inode);
238 if (inode->i_data.nrpages)
239 BUG();
240 if (!(inode->i_state & I_FREEING))
241 BUG();
242 if (inode->i_state & I_CLEAR)
243 BUG();
244 wait_on_inode(inode);
245 DQUOT_DROP(inode);
246 if (inode->i_sb && inode->i_sb->s_op->clear_inode)
247 inode->i_sb->s_op->clear_inode(inode);
248 if (inode->i_bdev)
249 bd_forget(inode);
250 if (inode->i_cdev)
251 cd_forget(inode);
252 inode->i_state = I_CLEAR;
255 EXPORT_SYMBOL(clear_inode);
258 * dispose_list - dispose of the contents of a local list
259 * @head: the head of the list to free
261 * Dispose-list gets a local list with local inodes in it, so it doesn't
262 * need to worry about list corruption and SMP locks.
264 static void dispose_list(struct list_head *head)
266 int nr_disposed = 0;
268 while (!list_empty(head)) {
269 struct inode *inode;
271 inode = list_entry(head->next, struct inode, i_list);
272 list_del(&inode->i_list);
274 if (inode->i_data.nrpages)
275 truncate_inode_pages(&inode->i_data, 0);
276 clear_inode(inode);
277 destroy_inode(inode);
278 nr_disposed++;
280 spin_lock(&inode_lock);
281 inodes_stat.nr_inodes -= nr_disposed;
282 spin_unlock(&inode_lock);
286 * Invalidate all inodes for a device.
288 static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
290 struct list_head *next;
291 int busy = 0, count = 0;
293 next = head->next;
294 for (;;) {
295 struct list_head * tmp = next;
296 struct inode * inode;
298 next = next->next;
299 if (tmp == head)
300 break;
301 inode = list_entry(tmp, struct inode, i_list);
302 if (inode->i_sb != sb)
303 continue;
304 invalidate_inode_buffers(inode);
305 if (!atomic_read(&inode->i_count)) {
306 hlist_del_init(&inode->i_hash);
307 list_del(&inode->i_list);
308 list_add(&inode->i_list, dispose);
309 inode->i_state |= I_FREEING;
310 count++;
311 continue;
313 busy = 1;
315 /* only unused inodes may be cached with i_count zero */
316 inodes_stat.nr_unused -= count;
317 return busy;
321 * This is a two-stage process. First we collect all
322 * offending inodes onto the throw-away list, and in
323 * the second stage we actually dispose of them. This
324 * is because we don't want to sleep while messing
325 * with the global lists..
329 * invalidate_inodes - discard the inodes on a device
330 * @sb: superblock
332 * Discard all of the inodes for a given superblock. If the discard
333 * fails because there are busy inodes then a non zero value is returned.
334 * If the discard is successful all the inodes have been discarded.
336 int invalidate_inodes(struct super_block * sb)
338 int busy;
339 LIST_HEAD(throw_away);
341 down(&iprune_sem);
342 spin_lock(&inode_lock);
343 busy = invalidate_list(&inode_in_use, sb, &throw_away);
344 busy |= invalidate_list(&inode_unused, sb, &throw_away);
345 busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
346 busy |= invalidate_list(&sb->s_io, sb, &throw_away);
347 spin_unlock(&inode_lock);
349 dispose_list(&throw_away);
350 up(&iprune_sem);
352 return busy;
355 EXPORT_SYMBOL(invalidate_inodes);
357 int __invalidate_device(struct block_device *bdev, int do_sync)
359 struct super_block *sb;
360 int res;
362 if (do_sync)
363 fsync_bdev(bdev);
365 res = 0;
366 sb = get_super(bdev);
367 if (sb) {
369 * no need to lock the super, get_super holds the
370 * read semaphore so the filesystem cannot go away
371 * under us (->put_super runs with the write lock
372 * hold).
374 shrink_dcache_sb(sb);
375 res = invalidate_inodes(sb);
376 drop_super(sb);
378 invalidate_bdev(bdev, 0);
379 return res;
382 EXPORT_SYMBOL(__invalidate_device);
384 static int can_unuse(struct inode *inode)
386 if (inode->i_state)
387 return 0;
388 if (inode_has_buffers(inode))
389 return 0;
390 if (atomic_read(&inode->i_count))
391 return 0;
392 if (inode->i_data.nrpages)
393 return 0;
394 return 1;
398 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
399 * a temporary list and then are freed outside inode_lock by dispose_list().
401 * Any inodes which are pinned purely because of attached pagecache have their
402 * pagecache removed. We expect the final iput() on that inode to add it to
403 * the front of the inode_unused list. So look for it there and if the
404 * inode is still freeable, proceed. The right inode is found 99.9% of the
405 * time in testing on a 4-way.
407 * If the inode has metadata buffers attached to mapping->private_list then
408 * try to remove them.
410 static void prune_icache(int nr_to_scan)
412 LIST_HEAD(freeable);
413 int nr_pruned = 0;
414 int nr_scanned;
415 unsigned long reap = 0;
417 down(&iprune_sem);
418 spin_lock(&inode_lock);
419 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
420 struct inode *inode;
422 if (list_empty(&inode_unused))
423 break;
425 inode = list_entry(inode_unused.prev, struct inode, i_list);
427 if (inode->i_state || atomic_read(&inode->i_count)) {
428 list_move(&inode->i_list, &inode_unused);
429 continue;
431 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
432 __iget(inode);
433 spin_unlock(&inode_lock);
434 if (remove_inode_buffers(inode))
435 reap += invalidate_inode_pages(&inode->i_data);
436 iput(inode);
437 spin_lock(&inode_lock);
439 if (inode != list_entry(inode_unused.next,
440 struct inode, i_list))
441 continue; /* wrong inode or list_empty */
442 if (!can_unuse(inode))
443 continue;
445 hlist_del_init(&inode->i_hash);
446 list_move(&inode->i_list, &freeable);
447 inode->i_state |= I_FREEING;
448 nr_pruned++;
450 inodes_stat.nr_unused -= nr_pruned;
451 spin_unlock(&inode_lock);
453 dispose_list(&freeable);
454 up(&iprune_sem);
456 if (current_is_kswapd())
457 mod_page_state(kswapd_inodesteal, reap);
458 else
459 mod_page_state(pginodesteal, reap);
463 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
464 * "unused" means that no dentries are referring to the inodes: the files are
465 * not open and the dcache references to those inodes have already been
466 * reclaimed.
468 * This function is passed the number of inodes to scan, and it returns the
469 * total number of remaining possibly-reclaimable inodes.
471 static int shrink_icache_memory(int nr, unsigned int gfp_mask)
473 if (nr) {
475 * Nasty deadlock avoidance. We may hold various FS locks,
476 * and we don't want to recurse into the FS that called us
477 * in clear_inode() and friends..
479 if (gfp_mask & __GFP_FS)
480 prune_icache(nr);
482 return inodes_stat.nr_unused;
485 static void __wait_on_freeing_inode(struct inode *inode);
487 * Called with the inode lock held.
488 * NOTE: we are not increasing the inode-refcount, you must call __iget()
489 * by hand after calling find_inode now! This simplifies iunique and won't
490 * add any additional branch in the common code.
492 static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
494 struct hlist_node *node;
495 struct inode * inode = NULL;
497 repeat:
498 hlist_for_each (node, head) {
499 inode = hlist_entry(node, struct inode, i_hash);
500 if (inode->i_sb != sb)
501 continue;
502 if (!test(inode, data))
503 continue;
504 if (inode->i_state & (I_FREEING|I_CLEAR)) {
505 __wait_on_freeing_inode(inode);
506 goto repeat;
508 break;
510 return node ? inode : NULL;
514 * find_inode_fast is the fast path version of find_inode, see the comment at
515 * iget_locked for details.
517 static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
519 struct hlist_node *node;
520 struct inode * inode = NULL;
522 repeat:
523 hlist_for_each (node, head) {
524 inode = hlist_entry(node, struct inode, i_hash);
525 if (inode->i_ino != ino)
526 continue;
527 if (inode->i_sb != sb)
528 continue;
529 if (inode->i_state & (I_FREEING|I_CLEAR)) {
530 __wait_on_freeing_inode(inode);
531 goto repeat;
533 break;
535 return node ? inode : NULL;
539 * new_inode - obtain an inode
540 * @sb: superblock
542 * Allocates a new inode for given superblock.
544 struct inode *new_inode(struct super_block *sb)
546 static unsigned long last_ino;
547 struct inode * inode;
549 spin_lock_prefetch(&inode_lock);
551 inode = alloc_inode(sb);
552 if (inode) {
553 spin_lock(&inode_lock);
554 inodes_stat.nr_inodes++;
555 list_add(&inode->i_list, &inode_in_use);
556 inode->i_ino = ++last_ino;
557 inode->i_state = 0;
558 spin_unlock(&inode_lock);
560 return inode;
563 EXPORT_SYMBOL(new_inode);
565 void unlock_new_inode(struct inode *inode)
568 * This is special! We do not need the spinlock
569 * when clearing I_LOCK, because we're guaranteed
570 * that nobody else tries to do anything about the
571 * state of the inode when it is locked, as we
572 * just created it (so there can be no old holders
573 * that haven't tested I_LOCK).
575 inode->i_state &= ~(I_LOCK|I_NEW);
576 wake_up_inode(inode);
579 EXPORT_SYMBOL(unlock_new_inode);
582 * This is called without the inode lock held.. Be careful.
584 * We no longer cache the sb_flags in i_flags - see fs.h
585 * -- rmk@arm.uk.linux.org
587 static struct inode * get_new_inode(struct super_block *sb, struct hlist_head *head, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *data)
589 struct inode * inode;
591 inode = alloc_inode(sb);
592 if (inode) {
593 struct inode * old;
595 spin_lock(&inode_lock);
596 /* We released the lock, so.. */
597 old = find_inode(sb, head, test, data);
598 if (!old) {
599 if (set(inode, data))
600 goto set_failed;
602 inodes_stat.nr_inodes++;
603 list_add(&inode->i_list, &inode_in_use);
604 hlist_add_head(&inode->i_hash, head);
605 inode->i_state = I_LOCK|I_NEW;
606 spin_unlock(&inode_lock);
608 /* Return the locked inode with I_NEW set, the
609 * caller is responsible for filling in the contents
611 return inode;
615 * Uhhuh, somebody else created the same inode under
616 * us. Use the old inode instead of the one we just
617 * allocated.
619 __iget(old);
620 spin_unlock(&inode_lock);
621 destroy_inode(inode);
622 inode = old;
623 wait_on_inode(inode);
625 return inode;
627 set_failed:
628 spin_unlock(&inode_lock);
629 destroy_inode(inode);
630 return NULL;
634 * get_new_inode_fast is the fast path version of get_new_inode, see the
635 * comment at iget_locked for details.
637 static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
639 struct inode * inode;
641 inode = alloc_inode(sb);
642 if (inode) {
643 struct inode * old;
645 spin_lock(&inode_lock);
646 /* We released the lock, so.. */
647 old = find_inode_fast(sb, head, ino);
648 if (!old) {
649 inode->i_ino = ino;
650 inodes_stat.nr_inodes++;
651 list_add(&inode->i_list, &inode_in_use);
652 hlist_add_head(&inode->i_hash, head);
653 inode->i_state = I_LOCK|I_NEW;
654 spin_unlock(&inode_lock);
656 /* Return the locked inode with I_NEW set, the
657 * caller is responsible for filling in the contents
659 return inode;
663 * Uhhuh, somebody else created the same inode under
664 * us. Use the old inode instead of the one we just
665 * allocated.
667 __iget(old);
668 spin_unlock(&inode_lock);
669 destroy_inode(inode);
670 inode = old;
671 wait_on_inode(inode);
673 return inode;
676 static inline unsigned long hash(struct super_block *sb, unsigned long hashval)
678 unsigned long tmp = hashval + ((unsigned long) sb / L1_CACHE_BYTES);
679 tmp = tmp + (tmp >> I_HASHBITS);
680 return tmp & I_HASHMASK;
683 /* Yeah, I know about quadratic hash. Maybe, later. */
686 * iunique - get a unique inode number
687 * @sb: superblock
688 * @max_reserved: highest reserved inode number
690 * Obtain an inode number that is unique on the system for a given
691 * superblock. This is used by file systems that have no natural
692 * permanent inode numbering system. An inode number is returned that
693 * is higher than the reserved limit but unique.
695 * BUGS:
696 * With a large number of inodes live on the file system this function
697 * currently becomes quite slow.
699 ino_t iunique(struct super_block *sb, ino_t max_reserved)
701 static ino_t counter;
702 struct inode *inode;
703 struct hlist_head * head;
704 ino_t res;
705 spin_lock(&inode_lock);
706 retry:
707 if (counter > max_reserved) {
708 head = inode_hashtable + hash(sb,counter);
709 res = counter++;
710 inode = find_inode_fast(sb, head, res);
711 if (!inode) {
712 spin_unlock(&inode_lock);
713 return res;
715 } else {
716 counter = max_reserved + 1;
718 goto retry;
722 EXPORT_SYMBOL(iunique);
724 struct inode *igrab(struct inode *inode)
726 spin_lock(&inode_lock);
727 if (!(inode->i_state & I_FREEING))
728 __iget(inode);
729 else
731 * Handle the case where s_op->clear_inode is not been
732 * called yet, and somebody is calling igrab
733 * while the inode is getting freed.
735 inode = NULL;
736 spin_unlock(&inode_lock);
737 return inode;
740 EXPORT_SYMBOL(igrab);
743 * ifind - internal function, you want ilookup5() or iget5().
744 * @sb: super block of file system to search
745 * @head: the head of the list to search
746 * @test: callback used for comparisons between inodes
747 * @data: opaque data pointer to pass to @test
749 * ifind() searches for the inode specified by @data in the inode
750 * cache. This is a generalized version of ifind_fast() for file systems where
751 * the inode number is not sufficient for unique identification of an inode.
753 * If the inode is in the cache, the inode is returned with an incremented
754 * reference count.
756 * Otherwise NULL is returned.
758 * Note, @test is called with the inode_lock held, so can't sleep.
760 static inline struct inode *ifind(struct super_block *sb,
761 struct hlist_head *head, int (*test)(struct inode *, void *),
762 void *data)
764 struct inode *inode;
766 spin_lock(&inode_lock);
767 inode = find_inode(sb, head, test, data);
768 if (inode) {
769 __iget(inode);
770 spin_unlock(&inode_lock);
771 wait_on_inode(inode);
772 return inode;
774 spin_unlock(&inode_lock);
775 return NULL;
779 * ifind_fast - internal function, you want ilookup() or iget().
780 * @sb: super block of file system to search
781 * @head: head of the list to search
782 * @ino: inode number to search for
784 * ifind_fast() searches for the inode @ino in the inode cache. This is for
785 * file systems where the inode number is sufficient for unique identification
786 * of an inode.
788 * If the inode is in the cache, the inode is returned with an incremented
789 * reference count.
791 * Otherwise NULL is returned.
793 static inline struct inode *ifind_fast(struct super_block *sb,
794 struct hlist_head *head, unsigned long ino)
796 struct inode *inode;
798 spin_lock(&inode_lock);
799 inode = find_inode_fast(sb, head, ino);
800 if (inode) {
801 __iget(inode);
802 spin_unlock(&inode_lock);
803 wait_on_inode(inode);
804 return inode;
806 spin_unlock(&inode_lock);
807 return NULL;
811 * ilookup5 - search for an inode in the inode cache
812 * @sb: super block of file system to search
813 * @hashval: hash value (usually inode number) to search for
814 * @test: callback used for comparisons between inodes
815 * @data: opaque data pointer to pass to @test
817 * ilookup5() uses ifind() to search for the inode specified by @hashval and
818 * @data in the inode cache. This is a generalized version of ilookup() for
819 * file systems where the inode number is not sufficient for unique
820 * identification of an inode.
822 * If the inode is in the cache, the inode is returned with an incremented
823 * reference count.
825 * Otherwise NULL is returned.
827 * Note, @test is called with the inode_lock held, so can't sleep.
829 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
830 int (*test)(struct inode *, void *), void *data)
832 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
834 return ifind(sb, head, test, data);
837 EXPORT_SYMBOL(ilookup5);
840 * ilookup - search for an inode in the inode cache
841 * @sb: super block of file system to search
842 * @ino: inode number to search for
844 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
845 * This is for file systems where the inode number is sufficient for unique
846 * identification of an inode.
848 * If the inode is in the cache, the inode is returned with an incremented
849 * reference count.
851 * Otherwise NULL is returned.
853 struct inode *ilookup(struct super_block *sb, unsigned long ino)
855 struct hlist_head *head = inode_hashtable + hash(sb, ino);
857 return ifind_fast(sb, head, ino);
860 EXPORT_SYMBOL(ilookup);
863 * iget5_locked - obtain an inode from a mounted file system
864 * @sb: super block of file system
865 * @hashval: hash value (usually inode number) to get
866 * @test: callback used for comparisons between inodes
867 * @set: callback used to initialize a new struct inode
868 * @data: opaque data pointer to pass to @test and @set
870 * This is iget() without the read_inode() portion of get_new_inode().
872 * iget5_locked() uses ifind() to search for the inode specified by @hashval
873 * and @data in the inode cache and if present it is returned with an increased
874 * reference count. This is a generalized version of iget_locked() for file
875 * systems where the inode number is not sufficient for unique identification
876 * of an inode.
878 * If the inode is not in cache, get_new_inode() is called to allocate a new
879 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
880 * file system gets to fill it in before unlocking it via unlock_new_inode().
882 * Note both @test and @set are called with the inode_lock held, so can't sleep.
884 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
885 int (*test)(struct inode *, void *),
886 int (*set)(struct inode *, void *), void *data)
888 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
889 struct inode *inode;
891 inode = ifind(sb, head, test, data);
892 if (inode)
893 return inode;
895 * get_new_inode() will do the right thing, re-trying the search
896 * in case it had to block at any point.
898 return get_new_inode(sb, head, test, set, data);
901 EXPORT_SYMBOL(iget5_locked);
904 * iget_locked - obtain an inode from a mounted file system
905 * @sb: super block of file system
906 * @ino: inode number to get
908 * This is iget() without the read_inode() portion of get_new_inode_fast().
910 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
911 * the inode cache and if present it is returned with an increased reference
912 * count. This is for file systems where the inode number is sufficient for
913 * unique identification of an inode.
915 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
916 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
917 * The file system gets to fill it in before unlocking it via
918 * unlock_new_inode().
920 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
922 struct hlist_head *head = inode_hashtable + hash(sb, ino);
923 struct inode *inode;
925 inode = ifind_fast(sb, head, ino);
926 if (inode)
927 return inode;
929 * get_new_inode_fast() will do the right thing, re-trying the search
930 * in case it had to block at any point.
932 return get_new_inode_fast(sb, head, ino);
935 EXPORT_SYMBOL(iget_locked);
938 * __insert_inode_hash - hash an inode
939 * @inode: unhashed inode
940 * @hashval: unsigned long value used to locate this object in the
941 * inode_hashtable.
943 * Add an inode to the inode hash for this superblock.
945 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
947 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
948 spin_lock(&inode_lock);
949 hlist_add_head(&inode->i_hash, head);
950 spin_unlock(&inode_lock);
953 EXPORT_SYMBOL(__insert_inode_hash);
956 * remove_inode_hash - remove an inode from the hash
957 * @inode: inode to unhash
959 * Remove an inode from the superblock.
961 void remove_inode_hash(struct inode *inode)
963 spin_lock(&inode_lock);
964 hlist_del_init(&inode->i_hash);
965 spin_unlock(&inode_lock);
968 EXPORT_SYMBOL(remove_inode_hash);
971 * Tell the filesystem that this inode is no longer of any interest and should
972 * be completely destroyed.
974 * We leave the inode in the inode hash table until *after* the filesystem's
975 * ->delete_inode completes. This ensures that an iget (such as nfsd might
976 * instigate) will always find up-to-date information either in the hash or on
977 * disk.
979 * I_FREEING is set so that no-one will take a new reference to the inode while
980 * it is being deleted.
982 void generic_delete_inode(struct inode *inode)
984 struct super_operations *op = inode->i_sb->s_op;
986 list_del_init(&inode->i_list);
987 inode->i_state|=I_FREEING;
988 inodes_stat.nr_inodes--;
989 spin_unlock(&inode_lock);
991 if (inode->i_data.nrpages)
992 truncate_inode_pages(&inode->i_data, 0);
994 security_inode_delete(inode);
996 if (op->delete_inode) {
997 void (*delete)(struct inode *) = op->delete_inode;
998 if (!is_bad_inode(inode))
999 DQUOT_INIT(inode);
1000 /* s_op->delete_inode internally recalls clear_inode() */
1001 delete(inode);
1002 } else
1003 clear_inode(inode);
1004 spin_lock(&inode_lock);
1005 hlist_del_init(&inode->i_hash);
1006 spin_unlock(&inode_lock);
1007 wake_up_inode(inode);
1008 if (inode->i_state != I_CLEAR)
1009 BUG();
1010 destroy_inode(inode);
1013 EXPORT_SYMBOL(generic_delete_inode);
1015 static void generic_forget_inode(struct inode *inode)
1017 struct super_block *sb = inode->i_sb;
1019 if (!hlist_unhashed(&inode->i_hash)) {
1020 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
1021 list_del(&inode->i_list);
1022 list_add(&inode->i_list, &inode_unused);
1024 inodes_stat.nr_unused++;
1025 spin_unlock(&inode_lock);
1026 if (!sb || (sb->s_flags & MS_ACTIVE))
1027 return;
1028 write_inode_now(inode, 1);
1029 spin_lock(&inode_lock);
1030 inodes_stat.nr_unused--;
1031 hlist_del_init(&inode->i_hash);
1033 list_del_init(&inode->i_list);
1034 inode->i_state|=I_FREEING;
1035 inodes_stat.nr_inodes--;
1036 spin_unlock(&inode_lock);
1037 if (inode->i_data.nrpages)
1038 truncate_inode_pages(&inode->i_data, 0);
1039 clear_inode(inode);
1040 destroy_inode(inode);
1044 * Normal UNIX filesystem behaviour: delete the
1045 * inode when the usage count drops to zero, and
1046 * i_nlink is zero.
1048 static void generic_drop_inode(struct inode *inode)
1050 if (!inode->i_nlink)
1051 generic_delete_inode(inode);
1052 else
1053 generic_forget_inode(inode);
1057 * Called when we're dropping the last reference
1058 * to an inode.
1060 * Call the FS "drop()" function, defaulting to
1061 * the legacy UNIX filesystem behaviour..
1063 * NOTE! NOTE! NOTE! We're called with the inode lock
1064 * held, and the drop function is supposed to release
1065 * the lock!
1067 static inline void iput_final(struct inode *inode)
1069 struct super_operations *op = inode->i_sb->s_op;
1070 void (*drop)(struct inode *) = generic_drop_inode;
1072 if (op && op->drop_inode)
1073 drop = op->drop_inode;
1074 drop(inode);
1078 * iput - put an inode
1079 * @inode: inode to put
1081 * Puts an inode, dropping its usage count. If the inode use count hits
1082 * zero the inode is also then freed and may be destroyed.
1084 void iput(struct inode *inode)
1086 if (inode) {
1087 struct super_operations *op = inode->i_sb->s_op;
1089 if (inode->i_state == I_CLEAR)
1090 BUG();
1092 if (op && op->put_inode)
1093 op->put_inode(inode);
1095 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1096 iput_final(inode);
1100 EXPORT_SYMBOL(iput);
1103 * bmap - find a block number in a file
1104 * @inode: inode of file
1105 * @block: block to find
1107 * Returns the block number on the device holding the inode that
1108 * is the disk block number for the block of the file requested.
1109 * That is, asked for block 4 of inode 1 the function will return the
1110 * disk block relative to the disk start that holds that block of the
1111 * file.
1113 sector_t bmap(struct inode * inode, sector_t block)
1115 sector_t res = 0;
1116 if (inode->i_mapping->a_ops->bmap)
1117 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1118 return res;
1121 EXPORT_SYMBOL(bmap);
1124 * Return true if the filesystem which backs this inode considers the two
1125 * passed timespecs to be sufficiently different to warrant flushing the
1126 * altered time out to disk.
1128 static int inode_times_differ(struct inode *inode,
1129 struct timespec *old, struct timespec *new)
1131 if (IS_ONE_SECOND(inode))
1132 return old->tv_sec != new->tv_sec;
1133 return !timespec_equal(old, new);
1137 * update_atime - update the access time
1138 * @inode: inode accessed
1140 * Update the accessed time on an inode and mark it for writeback.
1141 * This function automatically handles read only file systems and media,
1142 * as well as the "noatime" flag and inode specific "noatime" markers.
1144 void update_atime(struct inode *inode)
1146 struct timespec now;
1148 if (IS_NOATIME(inode))
1149 return;
1150 if (IS_NODIRATIME(inode) && S_ISDIR(inode->i_mode))
1151 return;
1152 if (IS_RDONLY(inode))
1153 return;
1155 now = current_kernel_time();
1156 if (inode_times_differ(inode, &inode->i_atime, &now)) {
1157 inode->i_atime = now;
1158 mark_inode_dirty_sync(inode);
1159 } else {
1160 if (!timespec_equal(&inode->i_atime, &now))
1161 inode->i_atime = now;
1165 EXPORT_SYMBOL(update_atime);
1168 * inode_update_time - update mtime and ctime time
1169 * @inode: inode accessed
1170 * @ctime_too: update ctime too
1172 * Update the mtime time on an inode and mark it for writeback.
1173 * When ctime_too is specified update the ctime too.
1176 void inode_update_time(struct inode *inode, int ctime_too)
1178 struct timespec now;
1179 int sync_it = 0;
1181 if (IS_RDONLY(inode))
1182 return;
1184 now = current_kernel_time();
1186 if (inode_times_differ(inode, &inode->i_mtime, &now))
1187 sync_it = 1;
1188 inode->i_mtime = now;
1190 if (ctime_too) {
1191 if (inode_times_differ(inode, &inode->i_ctime, &now))
1192 sync_it = 1;
1193 inode->i_ctime = now;
1195 if (sync_it)
1196 mark_inode_dirty_sync(inode);
1199 EXPORT_SYMBOL(inode_update_time);
1201 int inode_needs_sync(struct inode *inode)
1203 if (IS_SYNC(inode))
1204 return 1;
1205 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1206 return 1;
1207 return 0;
1210 EXPORT_SYMBOL(inode_needs_sync);
1213 * Quota functions that want to walk the inode lists..
1215 #ifdef CONFIG_QUOTA
1217 /* Functions back in dquot.c */
1218 void put_dquot_list(struct list_head *);
1219 int remove_inode_dquot_ref(struct inode *, int, struct list_head *);
1221 void remove_dquot_ref(struct super_block *sb, int type)
1223 struct inode *inode;
1224 struct list_head *act_head;
1225 LIST_HEAD(tofree_head);
1227 if (!sb->dq_op)
1228 return; /* nothing to do */
1229 spin_lock(&inode_lock); /* This lock is for inodes code */
1230 /* We don't have to lock against quota code - test IS_QUOTAINIT is just for speedup... */
1232 list_for_each(act_head, &inode_in_use) {
1233 inode = list_entry(act_head, struct inode, i_list);
1234 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1235 remove_inode_dquot_ref(inode, type, &tofree_head);
1237 list_for_each(act_head, &inode_unused) {
1238 inode = list_entry(act_head, struct inode, i_list);
1239 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1240 remove_inode_dquot_ref(inode, type, &tofree_head);
1242 list_for_each(act_head, &sb->s_dirty) {
1243 inode = list_entry(act_head, struct inode, i_list);
1244 if (IS_QUOTAINIT(inode))
1245 remove_inode_dquot_ref(inode, type, &tofree_head);
1247 list_for_each(act_head, &sb->s_io) {
1248 inode = list_entry(act_head, struct inode, i_list);
1249 if (IS_QUOTAINIT(inode))
1250 remove_inode_dquot_ref(inode, type, &tofree_head);
1252 spin_unlock(&inode_lock);
1254 put_dquot_list(&tofree_head);
1257 #endif
1260 * Hashed waitqueues for wait_on_inode(). The table is pretty small - the
1261 * kernel doesn't lock many inodes at the same time.
1263 #define I_WAIT_TABLE_ORDER 3
1264 static struct i_wait_queue_head {
1265 wait_queue_head_t wqh;
1266 } ____cacheline_aligned_in_smp i_wait_queue_heads[1<<I_WAIT_TABLE_ORDER];
1269 * Return the address of the waitqueue_head to be used for this inode
1271 static wait_queue_head_t *i_waitq_head(struct inode *inode)
1273 return &i_wait_queue_heads[hash_ptr(inode, I_WAIT_TABLE_ORDER)].wqh;
1276 void __wait_on_inode(struct inode *inode)
1278 DECLARE_WAITQUEUE(wait, current);
1279 wait_queue_head_t *wq = i_waitq_head(inode);
1281 add_wait_queue(wq, &wait);
1282 repeat:
1283 set_current_state(TASK_UNINTERRUPTIBLE);
1284 if (inode->i_state & I_LOCK) {
1285 schedule();
1286 goto repeat;
1288 remove_wait_queue(wq, &wait);
1289 __set_current_state(TASK_RUNNING);
1293 * If we try to find an inode in the inode hash while it is being deleted, we
1294 * have to wait until the filesystem completes its deletion before reporting
1295 * that it isn't found. This is because iget will immediately call
1296 * ->read_inode, and we want to be sure that evidence of the deletion is found
1297 * by ->read_inode.
1299 * This call might return early if an inode which shares the waitq is woken up.
1300 * This is most easily handled by the caller which will loop around again
1301 * looking for the inode.
1303 * This is called with inode_lock held.
1305 static void __wait_on_freeing_inode(struct inode *inode)
1307 DECLARE_WAITQUEUE(wait, current);
1308 wait_queue_head_t *wq = i_waitq_head(inode);
1310 add_wait_queue(wq, &wait);
1311 set_current_state(TASK_UNINTERRUPTIBLE);
1312 spin_unlock(&inode_lock);
1313 schedule();
1314 remove_wait_queue(wq, &wait);
1315 spin_lock(&inode_lock);
1318 void wake_up_inode(struct inode *inode)
1320 wait_queue_head_t *wq = i_waitq_head(inode);
1323 * Prevent speculative execution through spin_unlock(&inode_lock);
1325 smp_mb();
1326 if (waitqueue_active(wq))
1327 wake_up_all(wq);
1331 * Initialize the waitqueues and inode hash table.
1333 void __init inode_init(unsigned long mempages)
1335 struct hlist_head *head;
1336 unsigned long order;
1337 unsigned int nr_hash;
1338 int i;
1340 for (i = 0; i < ARRAY_SIZE(i_wait_queue_heads); i++)
1341 init_waitqueue_head(&i_wait_queue_heads[i].wqh);
1343 mempages >>= (14 - PAGE_SHIFT);
1344 mempages *= sizeof(struct hlist_head);
1345 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1348 do {
1349 unsigned long tmp;
1351 nr_hash = (1UL << order) * PAGE_SIZE /
1352 sizeof(struct hlist_head);
1353 i_hash_mask = (nr_hash - 1);
1355 tmp = nr_hash;
1356 i_hash_shift = 0;
1357 while ((tmp >>= 1UL) != 0UL)
1358 i_hash_shift++;
1360 inode_hashtable = (struct hlist_head *)
1361 __get_free_pages(GFP_ATOMIC, order);
1362 } while (inode_hashtable == NULL && --order >= 0);
1364 printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1365 nr_hash, order, (PAGE_SIZE << order));
1367 if (!inode_hashtable)
1368 panic("Failed to allocate inode hash table\n");
1370 head = inode_hashtable;
1371 i = nr_hash;
1372 do {
1373 INIT_HLIST_HEAD(head);
1374 head++;
1375 i--;
1376 } while (i);
1378 /* inode slab cache */
1379 inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
1380 0, SLAB_HWCACHE_ALIGN, init_once,
1381 NULL);
1382 if (!inode_cachep)
1383 panic("cannot create inode slab cache");
1385 set_shrinker(DEFAULT_SEEKS, shrink_icache_memory);
1388 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1390 inode->i_mode = mode;
1391 if (S_ISCHR(mode)) {
1392 inode->i_fop = &def_chr_fops;
1393 inode->i_rdev = rdev;
1394 } else if (S_ISBLK(mode)) {
1395 inode->i_fop = &def_blk_fops;
1396 inode->i_rdev = rdev;
1397 } else if (S_ISFIFO(mode))
1398 inode->i_fop = &def_fifo_fops;
1399 else if (S_ISSOCK(mode))
1400 inode->i_fop = &bad_sock_fops;
1401 else
1402 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1403 mode);
1406 EXPORT_SYMBOL(init_special_inode);