4 * (C) 1997 Linus Torvalds
7 #include <linux/config.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:
27 * - invalidate_inode_buffers
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".
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;
108 if (sb->s_op->alloc_inode)
109 inode = sb->s_op->alloc_inode(sb);
111 inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
114 struct address_space * const mapping = &inode->i_data;
117 inode->i_blkbits = sb->s_blocksize_bits;
119 atomic_set(&inode->i_count, 1);
121 inode->i_op = &empty_iops;
122 inode->i_fop = &empty_fops;
124 atomic_set(&inode->i_writecount, 0);
128 inode->i_generation = 0;
130 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
132 inode->i_pipe = NULL;
133 inode->i_bdev = NULL;
134 inode->i_cdev = NULL;
136 inode->i_security = NULL;
137 inode->dirtied_when = 0;
138 if (security_inode_alloc(inode)) {
139 if (inode->i_sb->s_op->destroy_inode)
140 inode->i_sb->s_op->destroy_inode(inode);
142 kmem_cache_free(inode_cachep, (inode));
146 mapping->a_ops = &empty_aops;
147 mapping->host = inode;
149 mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
150 mapping->assoc_mapping = NULL;
151 mapping->backing_dev_info = &default_backing_dev_info;
153 mapping->backing_dev_info = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
154 memset(&inode->u, 0, sizeof(inode->u));
155 inode->i_mapping = mapping;
160 void destroy_inode(struct inode *inode)
162 if (inode_has_buffers(inode))
164 security_inode_free(inode);
165 if (inode->i_sb->s_op->destroy_inode)
166 inode->i_sb->s_op->destroy_inode(inode);
168 kmem_cache_free(inode_cachep, (inode));
173 * These are initializations that only need to be done
174 * once, because the fields are idempotent across use
175 * of the inode, so let the slab aware of that.
177 void inode_init_once(struct inode *inode)
179 memset(inode, 0, sizeof(*inode));
180 INIT_HLIST_NODE(&inode->i_hash);
181 INIT_LIST_HEAD(&inode->i_dentry);
182 INIT_LIST_HEAD(&inode->i_devices);
183 sema_init(&inode->i_sem, 1);
184 init_rwsem(&inode->i_alloc_sem);
185 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
186 spin_lock_init(&inode->i_data.tree_lock);
187 init_MUTEX(&inode->i_data.i_shared_sem);
188 atomic_set(&inode->i_data.truncate_count, 0);
189 INIT_LIST_HEAD(&inode->i_data.private_list);
190 spin_lock_init(&inode->i_data.private_lock);
191 INIT_LIST_HEAD(&inode->i_data.i_mmap);
192 INIT_LIST_HEAD(&inode->i_data.i_mmap_shared);
193 spin_lock_init(&inode->i_lock);
194 i_size_ordered_init(inode);
197 EXPORT_SYMBOL(inode_init_once);
199 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
201 struct inode * inode = (struct inode *) foo;
203 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
204 SLAB_CTOR_CONSTRUCTOR)
205 inode_init_once(inode);
209 * inode_lock must be held
211 void __iget(struct inode * inode)
213 if (atomic_read(&inode->i_count)) {
214 atomic_inc(&inode->i_count);
217 atomic_inc(&inode->i_count);
218 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
219 list_move(&inode->i_list, &inode_in_use);
220 inodes_stat.nr_unused--;
224 * clear_inode - clear an inode
225 * @inode: inode to clear
227 * This is called by the filesystem to tell us
228 * that the inode is no longer useful. We just
229 * terminate it with extreme prejudice.
231 void clear_inode(struct inode *inode)
233 invalidate_inode_buffers(inode);
235 if (inode->i_data.nrpages)
237 if (!(inode->i_state & I_FREEING))
239 if (inode->i_state & I_CLEAR)
241 wait_on_inode(inode);
243 if (inode->i_sb && inode->i_sb->s_op->clear_inode)
244 inode->i_sb->s_op->clear_inode(inode);
249 inode->i_state = I_CLEAR;
252 EXPORT_SYMBOL(clear_inode);
255 * dispose_list - dispose of the contents of a local list
256 * @head: the head of the list to free
258 * Dispose-list gets a local list with local inodes in it, so it doesn't
259 * need to worry about list corruption and SMP locks.
261 static void dispose_list(struct list_head *head)
265 while (!list_empty(head)) {
268 inode = list_entry(head->next, struct inode, i_list);
269 list_del(&inode->i_list);
271 if (inode->i_data.nrpages)
272 truncate_inode_pages(&inode->i_data, 0);
274 destroy_inode(inode);
277 spin_lock(&inode_lock);
278 inodes_stat.nr_inodes -= nr_disposed;
279 spin_unlock(&inode_lock);
283 * Invalidate all inodes for a device.
285 static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
287 struct list_head *next;
288 int busy = 0, count = 0;
292 struct list_head * tmp = next;
293 struct inode * inode;
298 inode = list_entry(tmp, struct inode, i_list);
299 if (inode->i_sb != sb)
301 invalidate_inode_buffers(inode);
302 if (!atomic_read(&inode->i_count)) {
303 hlist_del_init(&inode->i_hash);
304 list_move(&inode->i_list, dispose);
305 inode->i_state |= I_FREEING;
311 /* only unused inodes may be cached with i_count zero */
312 inodes_stat.nr_unused -= count;
317 * This is a two-stage process. First we collect all
318 * offending inodes onto the throw-away list, and in
319 * the second stage we actually dispose of them. This
320 * is because we don't want to sleep while messing
321 * with the global lists..
325 * invalidate_inodes - discard the inodes on a device
328 * Discard all of the inodes for a given superblock. If the discard
329 * fails because there are busy inodes then a non zero value is returned.
330 * If the discard is successful all the inodes have been discarded.
332 int invalidate_inodes(struct super_block * sb)
335 LIST_HEAD(throw_away);
338 spin_lock(&inode_lock);
339 busy = invalidate_list(&inode_in_use, sb, &throw_away);
340 busy |= invalidate_list(&inode_unused, sb, &throw_away);
341 busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
342 busy |= invalidate_list(&sb->s_io, sb, &throw_away);
343 spin_unlock(&inode_lock);
345 dispose_list(&throw_away);
351 EXPORT_SYMBOL(invalidate_inodes);
353 int __invalidate_device(struct block_device *bdev, int do_sync)
355 struct super_block *sb;
362 sb = get_super(bdev);
365 * no need to lock the super, get_super holds the
366 * read semaphore so the filesystem cannot go away
367 * under us (->put_super runs with the write lock
370 shrink_dcache_sb(sb);
371 res = invalidate_inodes(sb);
374 invalidate_bdev(bdev, 0);
378 EXPORT_SYMBOL(__invalidate_device);
380 static int can_unuse(struct inode *inode)
384 if (inode_has_buffers(inode))
386 if (atomic_read(&inode->i_count))
388 if (inode->i_data.nrpages)
394 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
395 * a temporary list and then are freed outside inode_lock by dispose_list().
397 * Any inodes which are pinned purely because of attached pagecache have their
398 * pagecache removed. We expect the final iput() on that inode to add it to
399 * the front of the inode_unused list. So look for it there and if the
400 * inode is still freeable, proceed. The right inode is found 99.9% of the
401 * time in testing on a 4-way.
403 * If the inode has metadata buffers attached to mapping->private_list then
404 * try to remove them.
406 static void prune_icache(int nr_to_scan)
411 unsigned long reap = 0;
414 spin_lock(&inode_lock);
415 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
418 if (list_empty(&inode_unused))
421 inode = list_entry(inode_unused.prev, struct inode, i_list);
423 if (inode->i_state || atomic_read(&inode->i_count)) {
424 list_move(&inode->i_list, &inode_unused);
427 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
429 spin_unlock(&inode_lock);
430 if (remove_inode_buffers(inode))
431 reap += invalidate_inode_pages(&inode->i_data);
433 spin_lock(&inode_lock);
435 if (inode != list_entry(inode_unused.next,
436 struct inode, i_list))
437 continue; /* wrong inode or list_empty */
438 if (!can_unuse(inode))
441 hlist_del_init(&inode->i_hash);
442 list_move(&inode->i_list, &freeable);
443 inode->i_state |= I_FREEING;
446 inodes_stat.nr_unused -= nr_pruned;
447 spin_unlock(&inode_lock);
449 dispose_list(&freeable);
452 if (current_is_kswapd())
453 mod_page_state(kswapd_inodesteal, reap);
455 mod_page_state(pginodesteal, reap);
459 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
460 * "unused" means that no dentries are referring to the inodes: the files are
461 * not open and the dcache references to those inodes have already been
464 * This function is passed the number of inodes to scan, and it returns the
465 * total number of remaining possibly-reclaimable inodes.
467 static int shrink_icache_memory(int nr, unsigned int gfp_mask)
471 * Nasty deadlock avoidance. We may hold various FS locks,
472 * and we don't want to recurse into the FS that called us
473 * in clear_inode() and friends..
475 if (gfp_mask & __GFP_FS)
478 return inodes_stat.nr_unused;
481 static void __wait_on_freeing_inode(struct inode *inode);
483 * Called with the inode lock held.
484 * NOTE: we are not increasing the inode-refcount, you must call __iget()
485 * by hand after calling find_inode now! This simplifies iunique and won't
486 * add any additional branch in the common code.
488 static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
490 struct hlist_node *node;
491 struct inode * inode = NULL;
494 hlist_for_each (node, head) {
495 inode = hlist_entry(node, struct inode, i_hash);
496 if (inode->i_sb != sb)
498 if (!test(inode, data))
500 if (inode->i_state & (I_FREEING|I_CLEAR)) {
501 __wait_on_freeing_inode(inode);
506 return node ? inode : NULL;
510 * find_inode_fast is the fast path version of find_inode, see the comment at
511 * iget_locked for details.
513 static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
515 struct hlist_node *node;
516 struct inode * inode = NULL;
519 hlist_for_each (node, head) {
520 inode = hlist_entry(node, struct inode, i_hash);
521 if (inode->i_ino != ino)
523 if (inode->i_sb != sb)
525 if (inode->i_state & (I_FREEING|I_CLEAR)) {
526 __wait_on_freeing_inode(inode);
531 return node ? inode : NULL;
535 * new_inode - obtain an inode
538 * Allocates a new inode for given superblock.
540 struct inode *new_inode(struct super_block *sb)
542 static unsigned long last_ino;
543 struct inode * inode;
545 spin_lock_prefetch(&inode_lock);
547 inode = alloc_inode(sb);
549 spin_lock(&inode_lock);
550 inodes_stat.nr_inodes++;
551 list_add(&inode->i_list, &inode_in_use);
552 inode->i_ino = ++last_ino;
554 spin_unlock(&inode_lock);
559 EXPORT_SYMBOL(new_inode);
561 void unlock_new_inode(struct inode *inode)
564 * This is special! We do not need the spinlock
565 * when clearing I_LOCK, because we're guaranteed
566 * that nobody else tries to do anything about the
567 * state of the inode when it is locked, as we
568 * just created it (so there can be no old holders
569 * that haven't tested I_LOCK).
571 inode->i_state &= ~(I_LOCK|I_NEW);
572 wake_up_inode(inode);
575 EXPORT_SYMBOL(unlock_new_inode);
578 * This is called without the inode lock held.. Be careful.
580 * We no longer cache the sb_flags in i_flags - see fs.h
581 * -- rmk@arm.uk.linux.org
583 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)
585 struct inode * inode;
587 inode = alloc_inode(sb);
591 spin_lock(&inode_lock);
592 /* We released the lock, so.. */
593 old = find_inode(sb, head, test, data);
595 if (set(inode, data))
598 inodes_stat.nr_inodes++;
599 list_add(&inode->i_list, &inode_in_use);
600 hlist_add_head(&inode->i_hash, head);
601 inode->i_state = I_LOCK|I_NEW;
602 spin_unlock(&inode_lock);
604 /* Return the locked inode with I_NEW set, the
605 * caller is responsible for filling in the contents
611 * Uhhuh, somebody else created the same inode under
612 * us. Use the old inode instead of the one we just
616 spin_unlock(&inode_lock);
617 destroy_inode(inode);
619 wait_on_inode(inode);
624 spin_unlock(&inode_lock);
625 destroy_inode(inode);
630 * get_new_inode_fast is the fast path version of get_new_inode, see the
631 * comment at iget_locked for details.
633 static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
635 struct inode * inode;
637 inode = alloc_inode(sb);
641 spin_lock(&inode_lock);
642 /* We released the lock, so.. */
643 old = find_inode_fast(sb, head, ino);
646 inodes_stat.nr_inodes++;
647 list_add(&inode->i_list, &inode_in_use);
648 hlist_add_head(&inode->i_hash, head);
649 inode->i_state = I_LOCK|I_NEW;
650 spin_unlock(&inode_lock);
652 /* Return the locked inode with I_NEW set, the
653 * caller is responsible for filling in the contents
659 * Uhhuh, somebody else created the same inode under
660 * us. Use the old inode instead of the one we just
664 spin_unlock(&inode_lock);
665 destroy_inode(inode);
667 wait_on_inode(inode);
672 static inline unsigned long hash(struct super_block *sb, unsigned long hashval)
674 unsigned long tmp = hashval + ((unsigned long) sb / L1_CACHE_BYTES);
675 tmp = tmp + (tmp >> I_HASHBITS);
676 return tmp & I_HASHMASK;
679 /* Yeah, I know about quadratic hash. Maybe, later. */
682 * iunique - get a unique inode number
684 * @max_reserved: highest reserved inode number
686 * Obtain an inode number that is unique on the system for a given
687 * superblock. This is used by file systems that have no natural
688 * permanent inode numbering system. An inode number is returned that
689 * is higher than the reserved limit but unique.
692 * With a large number of inodes live on the file system this function
693 * currently becomes quite slow.
695 ino_t iunique(struct super_block *sb, ino_t max_reserved)
697 static ino_t counter;
699 struct hlist_head * head;
701 spin_lock(&inode_lock);
703 if (counter > max_reserved) {
704 head = inode_hashtable + hash(sb,counter);
706 inode = find_inode_fast(sb, head, res);
708 spin_unlock(&inode_lock);
712 counter = max_reserved + 1;
718 EXPORT_SYMBOL(iunique);
720 struct inode *igrab(struct inode *inode)
722 spin_lock(&inode_lock);
723 if (!(inode->i_state & I_FREEING))
727 * Handle the case where s_op->clear_inode is not been
728 * called yet, and somebody is calling igrab
729 * while the inode is getting freed.
732 spin_unlock(&inode_lock);
736 EXPORT_SYMBOL(igrab);
739 * ifind - internal function, you want ilookup5() or iget5().
740 * @sb: super block of file system to search
741 * @head: the head of the list to search
742 * @test: callback used for comparisons between inodes
743 * @data: opaque data pointer to pass to @test
745 * ifind() searches for the inode specified by @data in the inode
746 * cache. This is a generalized version of ifind_fast() for file systems where
747 * the inode number is not sufficient for unique identification of an inode.
749 * If the inode is in the cache, the inode is returned with an incremented
752 * Otherwise NULL is returned.
754 * Note, @test is called with the inode_lock held, so can't sleep.
756 static inline struct inode *ifind(struct super_block *sb,
757 struct hlist_head *head, int (*test)(struct inode *, void *),
762 spin_lock(&inode_lock);
763 inode = find_inode(sb, head, test, data);
766 spin_unlock(&inode_lock);
767 wait_on_inode(inode);
770 spin_unlock(&inode_lock);
775 * ifind_fast - internal function, you want ilookup() or iget().
776 * @sb: super block of file system to search
777 * @head: head of the list to search
778 * @ino: inode number to search for
780 * ifind_fast() searches for the inode @ino in the inode cache. This is for
781 * file systems where the inode number is sufficient for unique identification
784 * If the inode is in the cache, the inode is returned with an incremented
787 * Otherwise NULL is returned.
789 static inline struct inode *ifind_fast(struct super_block *sb,
790 struct hlist_head *head, unsigned long ino)
794 spin_lock(&inode_lock);
795 inode = find_inode_fast(sb, head, ino);
798 spin_unlock(&inode_lock);
799 wait_on_inode(inode);
802 spin_unlock(&inode_lock);
807 * ilookup5 - search for an inode in the inode cache
808 * @sb: super block of file system to search
809 * @hashval: hash value (usually inode number) to search for
810 * @test: callback used for comparisons between inodes
811 * @data: opaque data pointer to pass to @test
813 * ilookup5() uses ifind() to search for the inode specified by @hashval and
814 * @data in the inode cache. This is a generalized version of ilookup() for
815 * file systems where the inode number is not sufficient for unique
816 * identification of an inode.
818 * If the inode is in the cache, the inode is returned with an incremented
821 * Otherwise NULL is returned.
823 * Note, @test is called with the inode_lock held, so can't sleep.
825 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
826 int (*test)(struct inode *, void *), void *data)
828 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
830 return ifind(sb, head, test, data);
833 EXPORT_SYMBOL(ilookup5);
836 * ilookup - search for an inode in the inode cache
837 * @sb: super block of file system to search
838 * @ino: inode number to search for
840 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
841 * This is for file systems where the inode number is sufficient for unique
842 * identification of an inode.
844 * If the inode is in the cache, the inode is returned with an incremented
847 * Otherwise NULL is returned.
849 struct inode *ilookup(struct super_block *sb, unsigned long ino)
851 struct hlist_head *head = inode_hashtable + hash(sb, ino);
853 return ifind_fast(sb, head, ino);
856 EXPORT_SYMBOL(ilookup);
859 * iget5_locked - obtain an inode from a mounted file system
860 * @sb: super block of file system
861 * @hashval: hash value (usually inode number) to get
862 * @test: callback used for comparisons between inodes
863 * @set: callback used to initialize a new struct inode
864 * @data: opaque data pointer to pass to @test and @set
866 * This is iget() without the read_inode() portion of get_new_inode().
868 * iget5_locked() uses ifind() to search for the inode specified by @hashval
869 * and @data in the inode cache and if present it is returned with an increased
870 * reference count. This is a generalized version of iget_locked() for file
871 * systems where the inode number is not sufficient for unique identification
874 * If the inode is not in cache, get_new_inode() is called to allocate a new
875 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
876 * file system gets to fill it in before unlocking it via unlock_new_inode().
878 * Note both @test and @set are called with the inode_lock held, so can't sleep.
880 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
881 int (*test)(struct inode *, void *),
882 int (*set)(struct inode *, void *), void *data)
884 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
887 inode = ifind(sb, head, test, data);
891 * get_new_inode() will do the right thing, re-trying the search
892 * in case it had to block at any point.
894 return get_new_inode(sb, head, test, set, data);
897 EXPORT_SYMBOL(iget5_locked);
900 * iget_locked - obtain an inode from a mounted file system
901 * @sb: super block of file system
902 * @ino: inode number to get
904 * This is iget() without the read_inode() portion of get_new_inode_fast().
906 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
907 * the inode cache and if present it is returned with an increased reference
908 * count. This is for file systems where the inode number is sufficient for
909 * unique identification of an inode.
911 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
912 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
913 * The file system gets to fill it in before unlocking it via
914 * unlock_new_inode().
916 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
918 struct hlist_head *head = inode_hashtable + hash(sb, ino);
921 inode = ifind_fast(sb, head, ino);
925 * get_new_inode_fast() will do the right thing, re-trying the search
926 * in case it had to block at any point.
928 return get_new_inode_fast(sb, head, ino);
931 EXPORT_SYMBOL(iget_locked);
934 * __insert_inode_hash - hash an inode
935 * @inode: unhashed inode
936 * @hashval: unsigned long value used to locate this object in the
939 * Add an inode to the inode hash for this superblock.
941 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
943 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
944 spin_lock(&inode_lock);
945 hlist_add_head(&inode->i_hash, head);
946 spin_unlock(&inode_lock);
949 EXPORT_SYMBOL(__insert_inode_hash);
952 * remove_inode_hash - remove an inode from the hash
953 * @inode: inode to unhash
955 * Remove an inode from the superblock.
957 void remove_inode_hash(struct inode *inode)
959 spin_lock(&inode_lock);
960 hlist_del_init(&inode->i_hash);
961 spin_unlock(&inode_lock);
964 EXPORT_SYMBOL(remove_inode_hash);
967 * Tell the filesystem that this inode is no longer of any interest and should
968 * be completely destroyed.
970 * We leave the inode in the inode hash table until *after* the filesystem's
971 * ->delete_inode completes. This ensures that an iget (such as nfsd might
972 * instigate) will always find up-to-date information either in the hash or on
975 * I_FREEING is set so that no-one will take a new reference to the inode while
976 * it is being deleted.
978 void generic_delete_inode(struct inode *inode)
980 struct super_operations *op = inode->i_sb->s_op;
982 list_del_init(&inode->i_list);
983 inode->i_state|=I_FREEING;
984 inodes_stat.nr_inodes--;
985 spin_unlock(&inode_lock);
987 if (inode->i_data.nrpages)
988 truncate_inode_pages(&inode->i_data, 0);
990 security_inode_delete(inode);
992 if (op->delete_inode) {
993 void (*delete)(struct inode *) = op->delete_inode;
994 if (!is_bad_inode(inode))
996 /* s_op->delete_inode internally recalls clear_inode() */
1000 spin_lock(&inode_lock);
1001 hlist_del_init(&inode->i_hash);
1002 spin_unlock(&inode_lock);
1003 wake_up_inode(inode);
1004 if (inode->i_state != I_CLEAR)
1006 destroy_inode(inode);
1009 EXPORT_SYMBOL(generic_delete_inode);
1011 static void generic_forget_inode(struct inode *inode)
1013 struct super_block *sb = inode->i_sb;
1015 if (!hlist_unhashed(&inode->i_hash)) {
1016 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
1017 list_move(&inode->i_list, &inode_unused);
1018 inodes_stat.nr_unused++;
1019 spin_unlock(&inode_lock);
1020 if (!sb || (sb->s_flags & MS_ACTIVE))
1022 write_inode_now(inode, 1);
1023 spin_lock(&inode_lock);
1024 inodes_stat.nr_unused--;
1025 hlist_del_init(&inode->i_hash);
1027 list_del_init(&inode->i_list);
1028 inode->i_state|=I_FREEING;
1029 inodes_stat.nr_inodes--;
1030 spin_unlock(&inode_lock);
1031 if (inode->i_data.nrpages)
1032 truncate_inode_pages(&inode->i_data, 0);
1034 destroy_inode(inode);
1038 * Normal UNIX filesystem behaviour: delete the
1039 * inode when the usage count drops to zero, and
1042 static void generic_drop_inode(struct inode *inode)
1044 if (!inode->i_nlink)
1045 generic_delete_inode(inode);
1047 generic_forget_inode(inode);
1051 * Called when we're dropping the last reference
1054 * Call the FS "drop()" function, defaulting to
1055 * the legacy UNIX filesystem behaviour..
1057 * NOTE! NOTE! NOTE! We're called with the inode lock
1058 * held, and the drop function is supposed to release
1061 static inline void iput_final(struct inode *inode)
1063 struct super_operations *op = inode->i_sb->s_op;
1064 void (*drop)(struct inode *) = generic_drop_inode;
1066 if (op && op->drop_inode)
1067 drop = op->drop_inode;
1072 * iput - put an inode
1073 * @inode: inode to put
1075 * Puts an inode, dropping its usage count. If the inode use count hits
1076 * zero the inode is also then freed and may be destroyed.
1078 void iput(struct inode *inode)
1081 struct super_operations *op = inode->i_sb->s_op;
1083 if (inode->i_state == I_CLEAR)
1086 if (op && op->put_inode)
1087 op->put_inode(inode);
1089 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1094 EXPORT_SYMBOL(iput);
1097 * bmap - find a block number in a file
1098 * @inode: inode of file
1099 * @block: block to find
1101 * Returns the block number on the device holding the inode that
1102 * is the disk block number for the block of the file requested.
1103 * That is, asked for block 4 of inode 1 the function will return the
1104 * disk block relative to the disk start that holds that block of the
1107 sector_t bmap(struct inode * inode, sector_t block)
1110 if (inode->i_mapping->a_ops->bmap)
1111 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1115 EXPORT_SYMBOL(bmap);
1118 * Return true if the filesystem which backs this inode considers the two
1119 * passed timespecs to be sufficiently different to warrant flushing the
1120 * altered time out to disk.
1122 static int inode_times_differ(struct inode *inode,
1123 struct timespec *old, struct timespec *new)
1125 if (IS_ONE_SECOND(inode))
1126 return old->tv_sec != new->tv_sec;
1127 return !timespec_equal(old, new);
1131 * update_atime - update the access time
1132 * @inode: inode accessed
1134 * Update the accessed time on an inode and mark it for writeback.
1135 * This function automatically handles read only file systems and media,
1136 * as well as the "noatime" flag and inode specific "noatime" markers.
1138 void update_atime(struct inode *inode)
1140 struct timespec now;
1142 if (IS_NOATIME(inode))
1144 if (IS_NODIRATIME(inode) && S_ISDIR(inode->i_mode))
1146 if (IS_RDONLY(inode))
1149 now = current_kernel_time();
1150 if (inode_times_differ(inode, &inode->i_atime, &now)) {
1151 inode->i_atime = now;
1152 mark_inode_dirty_sync(inode);
1154 if (!timespec_equal(&inode->i_atime, &now))
1155 inode->i_atime = now;
1159 EXPORT_SYMBOL(update_atime);
1162 * inode_update_time - update mtime and ctime time
1163 * @inode: inode accessed
1164 * @ctime_too: update ctime too
1166 * Update the mtime time on an inode and mark it for writeback.
1167 * When ctime_too is specified update the ctime too.
1170 void inode_update_time(struct inode *inode, int ctime_too)
1172 struct timespec now;
1175 if (IS_NOCMTIME(inode))
1177 if (IS_RDONLY(inode))
1180 now = current_kernel_time();
1182 if (inode_times_differ(inode, &inode->i_mtime, &now))
1184 inode->i_mtime = now;
1187 if (inode_times_differ(inode, &inode->i_ctime, &now))
1189 inode->i_ctime = now;
1192 mark_inode_dirty_sync(inode);
1195 EXPORT_SYMBOL(inode_update_time);
1197 int inode_needs_sync(struct inode *inode)
1201 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1206 EXPORT_SYMBOL(inode_needs_sync);
1209 * Quota functions that want to walk the inode lists..
1213 /* Function back in dquot.c */
1214 int remove_inode_dquot_ref(struct inode *, int, struct list_head *);
1216 void remove_dquot_ref(struct super_block *sb, int type, struct list_head *tofree_head)
1218 struct inode *inode;
1219 struct list_head *act_head;
1222 return; /* nothing to do */
1223 spin_lock(&inode_lock); /* This lock is for inodes code */
1224 /* We don't have to lock against quota code - test IS_QUOTAINIT is just for speedup... */
1226 list_for_each(act_head, &inode_in_use) {
1227 inode = list_entry(act_head, struct inode, i_list);
1228 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1229 remove_inode_dquot_ref(inode, type, tofree_head);
1231 list_for_each(act_head, &inode_unused) {
1232 inode = list_entry(act_head, struct inode, i_list);
1233 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1234 remove_inode_dquot_ref(inode, type, tofree_head);
1236 list_for_each(act_head, &sb->s_dirty) {
1237 inode = list_entry(act_head, struct inode, i_list);
1238 if (IS_QUOTAINIT(inode))
1239 remove_inode_dquot_ref(inode, type, tofree_head);
1241 list_for_each(act_head, &sb->s_io) {
1242 inode = list_entry(act_head, struct inode, i_list);
1243 if (IS_QUOTAINIT(inode))
1244 remove_inode_dquot_ref(inode, type, tofree_head);
1246 spin_unlock(&inode_lock);
1252 * Hashed waitqueues for wait_on_inode(). The table is pretty small - the
1253 * kernel doesn't lock many inodes at the same time.
1255 #define I_WAIT_TABLE_ORDER 3
1256 static struct i_wait_queue_head {
1257 wait_queue_head_t wqh;
1258 } ____cacheline_aligned_in_smp i_wait_queue_heads[1<<I_WAIT_TABLE_ORDER];
1261 * Return the address of the waitqueue_head to be used for this inode
1263 static wait_queue_head_t *i_waitq_head(struct inode *inode)
1265 return &i_wait_queue_heads[hash_ptr(inode, I_WAIT_TABLE_ORDER)].wqh;
1268 void __wait_on_inode(struct inode *inode)
1270 DECLARE_WAITQUEUE(wait, current);
1271 wait_queue_head_t *wq = i_waitq_head(inode);
1273 add_wait_queue(wq, &wait);
1275 set_current_state(TASK_UNINTERRUPTIBLE);
1276 if (inode->i_state & I_LOCK) {
1280 remove_wait_queue(wq, &wait);
1281 __set_current_state(TASK_RUNNING);
1285 * If we try to find an inode in the inode hash while it is being deleted, we
1286 * have to wait until the filesystem completes its deletion before reporting
1287 * that it isn't found. This is because iget will immediately call
1288 * ->read_inode, and we want to be sure that evidence of the deletion is found
1291 * This call might return early if an inode which shares the waitq is woken up.
1292 * This is most easily handled by the caller which will loop around again
1293 * looking for the inode.
1295 * This is called with inode_lock held.
1297 static void __wait_on_freeing_inode(struct inode *inode)
1299 DECLARE_WAITQUEUE(wait, current);
1300 wait_queue_head_t *wq = i_waitq_head(inode);
1302 add_wait_queue(wq, &wait);
1303 set_current_state(TASK_UNINTERRUPTIBLE);
1304 spin_unlock(&inode_lock);
1306 remove_wait_queue(wq, &wait);
1307 spin_lock(&inode_lock);
1310 void wake_up_inode(struct inode *inode)
1312 wait_queue_head_t *wq = i_waitq_head(inode);
1315 * Prevent speculative execution through spin_unlock(&inode_lock);
1318 if (waitqueue_active(wq))
1322 static __initdata unsigned long ihash_entries;
1323 static int __init set_ihash_entries(char *str)
1327 ihash_entries = simple_strtoul(str, &str, 0);
1330 __setup("ihash_entries=", set_ihash_entries);
1333 * Initialize the waitqueues and inode hash table.
1335 void __init inode_init(unsigned long mempages)
1337 struct hlist_head *head;
1338 unsigned long order;
1339 unsigned int nr_hash;
1342 for (i = 0; i < ARRAY_SIZE(i_wait_queue_heads); i++)
1343 init_waitqueue_head(&i_wait_queue_heads[i].wqh);
1346 ihash_entries = PAGE_SHIFT < 14 ?
1347 mempages >> (14 - PAGE_SHIFT) :
1348 mempages << (PAGE_SHIFT - 14);
1350 ihash_entries *= sizeof(struct hlist_head);
1351 for (order = 0; ((1UL << order) << PAGE_SHIFT) < ihash_entries; order++)
1357 nr_hash = (1UL << order) * PAGE_SIZE /
1358 sizeof(struct hlist_head);
1359 i_hash_mask = (nr_hash - 1);
1363 while ((tmp >>= 1UL) != 0UL)
1366 inode_hashtable = (struct hlist_head *)
1367 __get_free_pages(GFP_ATOMIC, order);
1368 } while (inode_hashtable == NULL && --order >= 0);
1370 printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1371 nr_hash, order, (PAGE_SIZE << order));
1373 if (!inode_hashtable)
1374 panic("Failed to allocate inode hash table\n");
1376 head = inode_hashtable;
1379 INIT_HLIST_HEAD(head);
1384 /* inode slab cache */
1385 inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
1386 0, SLAB_HWCACHE_ALIGN, init_once,
1389 panic("cannot create inode slab cache");
1391 set_shrinker(DEFAULT_SEEKS, shrink_icache_memory);
1394 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1396 inode->i_mode = mode;
1397 if (S_ISCHR(mode)) {
1398 inode->i_fop = &def_chr_fops;
1399 inode->i_rdev = rdev;
1400 } else if (S_ISBLK(mode)) {
1401 inode->i_fop = &def_blk_fops;
1402 inode->i_rdev = rdev;
1403 } else if (S_ISFIFO(mode))
1404 inode->i_fop = &def_fifo_fops;
1405 else if (S_ISSOCK(mode))
1406 inode->i_fop = &bad_sock_fops;
1408 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1412 EXPORT_SYMBOL(init_special_inode);