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/cdev.h>
24 * This is needed for the following functions:
26 * - invalidate_inode_buffers
30 * FIXME: remove all knowledge of the buffer layer from this file
32 #include <linux/buffer_head.h>
35 * New inode.c implementation.
37 * This implementation has the basic premise of trying
38 * to be extremely low-overhead and SMP-safe, yet be
39 * simple enough to be "obviously correct".
44 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
46 /* #define INODE_PARANOIA 1 */
47 /* #define INODE_DEBUG 1 */
50 * Inode lookup is no longer as critical as it used to be:
51 * most of the lookups are going to be through the dcache.
53 #define I_HASHBITS i_hash_shift
54 #define I_HASHMASK i_hash_mask
56 static unsigned int i_hash_mask;
57 static unsigned int i_hash_shift;
60 * Each inode can be on two separate lists. One is
61 * the hash list of the inode, used for lookups. The
62 * other linked list is the "type" list:
63 * "in_use" - valid inode, i_count > 0, i_nlink > 0
64 * "dirty" - as "in_use" but also dirty
65 * "unused" - valid inode, i_count = 0
67 * A "dirty" list is maintained for each super block,
68 * allowing for low-overhead inode sync() operations.
71 LIST_HEAD(inode_in_use);
72 LIST_HEAD(inode_unused);
73 static struct hlist_head *inode_hashtable;
76 * A simple spinlock to protect the list manipulations.
78 * NOTE! You also have to own the lock if you change
79 * the i_state of an inode while it is in use..
81 spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
84 * iprune_sem provides exclusion between the kswapd or try_to_free_pages
85 * icache shrinking path, and the umount path. Without this exclusion,
86 * by the time prune_icache calls iput for the inode whose pages it has
87 * been invalidating, or by the time it calls clear_inode & destroy_inode
88 * from its final dispose_list, the struct super_block they refer to
89 * (for inode->i_sb->s_op) may already have been freed and reused.
91 static DECLARE_MUTEX(iprune_sem);
94 * Statistics gathering..
96 struct inodes_stat_t inodes_stat;
98 static kmem_cache_t * inode_cachep;
100 static struct inode *alloc_inode(struct super_block *sb)
102 static struct address_space_operations empty_aops;
103 static struct inode_operations empty_iops;
104 static struct file_operations empty_fops;
107 if (sb->s_op->alloc_inode)
108 inode = sb->s_op->alloc_inode(sb);
110 inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
113 struct address_space * const mapping = &inode->i_data;
116 inode->i_blkbits = sb->s_blocksize_bits;
118 atomic_set(&inode->i_count, 1);
120 inode->i_op = &empty_iops;
121 inode->i_fop = &empty_fops;
123 atomic_set(&inode->i_writecount, 0);
127 inode->i_generation = 0;
128 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
129 inode->i_pipe = NULL;
130 inode->i_bdev = NULL;
131 inode->i_cdev = NULL;
132 inode->i_rdev = to_kdev_t(0);
133 inode->i_security = NULL;
134 if (security_inode_alloc(inode)) {
135 if (inode->i_sb->s_op->destroy_inode)
136 inode->i_sb->s_op->destroy_inode(inode);
138 kmem_cache_free(inode_cachep, (inode));
142 mapping->a_ops = &empty_aops;
143 mapping->host = inode;
144 mapping->gfp_mask = GFP_HIGHUSER;
145 mapping->dirtied_when = 0;
146 mapping->assoc_mapping = NULL;
147 mapping->backing_dev_info = &default_backing_dev_info;
149 mapping->backing_dev_info = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
150 memset(&inode->u, 0, sizeof(inode->u));
151 inode->i_mapping = mapping;
156 void destroy_inode(struct inode *inode)
158 if (inode_has_buffers(inode))
160 security_inode_free(inode);
161 if (inode->i_sb->s_op->destroy_inode)
162 inode->i_sb->s_op->destroy_inode(inode);
164 kmem_cache_free(inode_cachep, (inode));
169 * These are initializations that only need to be done
170 * once, because the fields are idempotent across use
171 * of the inode, so let the slab aware of that.
173 void inode_init_once(struct inode *inode)
175 memset(inode, 0, sizeof(*inode));
176 INIT_HLIST_NODE(&inode->i_hash);
177 INIT_LIST_HEAD(&inode->i_data.clean_pages);
178 INIT_LIST_HEAD(&inode->i_data.dirty_pages);
179 INIT_LIST_HEAD(&inode->i_data.locked_pages);
180 INIT_LIST_HEAD(&inode->i_data.io_pages);
181 INIT_LIST_HEAD(&inode->i_dentry);
182 INIT_LIST_HEAD(&inode->i_devices);
183 sema_init(&inode->i_sem, 1);
184 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
185 spin_lock_init(&inode->i_data.page_lock);
186 init_MUTEX(&inode->i_data.i_shared_sem);
187 atomic_set(&inode->i_data.truncate_count, 0);
188 INIT_LIST_HEAD(&inode->i_data.private_list);
189 spin_lock_init(&inode->i_data.private_lock);
190 INIT_LIST_HEAD(&inode->i_data.i_mmap);
191 INIT_LIST_HEAD(&inode->i_data.i_mmap_shared);
192 spin_lock_init(&inode->i_lock);
193 i_size_ordered_init(inode);
196 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
198 struct inode * inode = (struct inode *) foo;
200 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
201 SLAB_CTOR_CONSTRUCTOR)
202 inode_init_once(inode);
206 * inode_lock must be held
208 void __iget(struct inode * inode)
210 if (atomic_read(&inode->i_count)) {
211 atomic_inc(&inode->i_count);
214 atomic_inc(&inode->i_count);
215 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
216 list_del(&inode->i_list);
217 list_add(&inode->i_list, &inode_in_use);
219 inodes_stat.nr_unused--;
223 * clear_inode - clear an inode
224 * @inode: inode to clear
226 * This is called by the filesystem to tell us
227 * that the inode is no longer useful. We just
228 * 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;
253 * Dispose-list gets a local list with local inodes in it, so it doesn't
254 * need to worry about list corruption and SMP locks.
256 static void dispose_list(struct list_head *head)
260 while (!list_empty(head)) {
263 inode = list_entry(head->next, struct inode, i_list);
264 list_del(&inode->i_list);
266 if (inode->i_data.nrpages)
267 truncate_inode_pages(&inode->i_data, 0);
269 destroy_inode(inode);
272 spin_lock(&inode_lock);
273 inodes_stat.nr_inodes -= nr_disposed;
274 spin_unlock(&inode_lock);
278 * Invalidate all inodes for a device.
280 static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
282 struct list_head *next;
283 int busy = 0, count = 0;
287 struct list_head * tmp = next;
288 struct inode * inode;
293 inode = list_entry(tmp, struct inode, i_list);
294 if (inode->i_sb != sb)
296 invalidate_inode_buffers(inode);
297 if (!atomic_read(&inode->i_count)) {
298 hlist_del_init(&inode->i_hash);
299 list_del(&inode->i_list);
300 list_add(&inode->i_list, dispose);
301 inode->i_state |= I_FREEING;
307 /* only unused inodes may be cached with i_count zero */
308 inodes_stat.nr_unused -= count;
313 * This is a two-stage process. First we collect all
314 * offending inodes onto the throw-away list, and in
315 * the second stage we actually dispose of them. This
316 * is because we don't want to sleep while messing
317 * with the global lists..
321 * invalidate_inodes - discard the inodes on a device
324 * Discard all of the inodes for a given superblock. If the discard
325 * fails because there are busy inodes then a non zero value is returned.
326 * If the discard is successful all the inodes have been discarded.
329 int invalidate_inodes(struct super_block * sb)
332 LIST_HEAD(throw_away);
335 spin_lock(&inode_lock);
336 busy = invalidate_list(&inode_in_use, sb, &throw_away);
337 busy |= invalidate_list(&inode_unused, sb, &throw_away);
338 busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
339 busy |= invalidate_list(&sb->s_io, sb, &throw_away);
340 spin_unlock(&inode_lock);
342 dispose_list(&throw_away);
348 int __invalidate_device(struct block_device *bdev, int do_sync)
350 struct super_block *sb;
357 sb = get_super(bdev);
360 * no need to lock the super, get_super holds the
361 * read semaphore so the filesystem cannot go away
362 * under us (->put_super runs with the write lock
365 shrink_dcache_sb(sb);
366 res = invalidate_inodes(sb);
369 invalidate_bdev(bdev, 0);
373 static int can_unuse(struct inode *inode)
377 if (inode_has_buffers(inode))
379 if (atomic_read(&inode->i_count))
381 if (inode->i_data.nrpages)
387 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
388 * a temporary list and then are freed outside inode_lock by dispose_list().
390 * Any inodes which are pinned purely because of attached pagecache have their
391 * pagecache removed. We expect the final iput() on that inode to add it to
392 * the front of the inode_unused list. So look for it there and if the
393 * inode is still freeable, proceed. The right inode is found 99.9% of the
394 * time in testing on a 4-way.
396 * If the inode has metadata buffers attached to mapping->private_list then
397 * try to remove them.
399 static void prune_icache(int nr_to_scan)
404 unsigned long reap = 0;
407 spin_lock(&inode_lock);
408 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
411 if (list_empty(&inode_unused))
414 inode = list_entry(inode_unused.prev, struct inode, i_list);
416 if (inode->i_state || atomic_read(&inode->i_count)) {
417 list_move(&inode->i_list, &inode_unused);
420 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
422 spin_unlock(&inode_lock);
423 if (remove_inode_buffers(inode))
424 reap += invalidate_inode_pages(&inode->i_data);
426 spin_lock(&inode_lock);
428 if (inode != list_entry(inode_unused.next,
429 struct inode, i_list))
430 continue; /* wrong inode or list_empty */
431 if (!can_unuse(inode))
434 hlist_del_init(&inode->i_hash);
435 list_move(&inode->i_list, &freeable);
436 inode->i_state |= I_FREEING;
439 inodes_stat.nr_unused -= nr_pruned;
440 spin_unlock(&inode_lock);
442 dispose_list(&freeable);
445 if (current_is_kswapd)
446 mod_page_state(kswapd_inodesteal, reap);
448 mod_page_state(pginodesteal, reap);
452 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
453 * "unused" means that no dentries are referring to the inodes: the files are
454 * not open and the dcache references to those inodes have already been
457 * This function is passed the number of inodes to scan, and it returns the
458 * total number of remaining possibly-reclaimable inodes.
460 static int shrink_icache_memory(int nr, unsigned int gfp_mask)
464 * Nasty deadlock avoidance. We may hold various FS locks,
465 * and we don't want to recurse into the FS that called us
466 * in clear_inode() and friends..
468 if (gfp_mask & __GFP_FS)
471 return inodes_stat.nr_unused;
474 static void __wait_on_freeing_inode(struct inode *inode);
476 * Called with the inode lock held.
477 * NOTE: we are not increasing the inode-refcount, you must call __iget()
478 * by hand after calling find_inode now! This simplifies iunique and won't
479 * add any additional branch in the common code.
481 static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
483 struct hlist_node *node;
484 struct inode * inode = NULL;
487 hlist_for_each (node, head) {
488 inode = hlist_entry(node, struct inode, i_hash);
489 if (inode->i_sb != sb)
491 if (!test(inode, data))
493 if (inode->i_state & (I_FREEING|I_CLEAR)) {
494 __wait_on_freeing_inode(inode);
499 return node ? inode : NULL;
503 * find_inode_fast is the fast path version of find_inode, see the comment at
504 * iget_locked for details.
506 static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
508 struct hlist_node *node;
509 struct inode * inode = NULL;
512 hlist_for_each (node, head) {
513 inode = hlist_entry(node, struct inode, i_hash);
514 if (inode->i_ino != ino)
516 if (inode->i_sb != sb)
518 if (inode->i_state & (I_FREEING|I_CLEAR)) {
519 __wait_on_freeing_inode(inode);
524 return node ? inode : NULL;
528 * new_inode - obtain an inode
531 * Allocates a new inode for given superblock.
534 struct inode *new_inode(struct super_block *sb)
536 static unsigned long last_ino;
537 struct inode * inode;
539 spin_lock_prefetch(&inode_lock);
541 inode = alloc_inode(sb);
543 spin_lock(&inode_lock);
544 inodes_stat.nr_inodes++;
545 list_add(&inode->i_list, &inode_in_use);
546 inode->i_ino = ++last_ino;
548 spin_unlock(&inode_lock);
553 void unlock_new_inode(struct inode *inode)
556 * This is special! We do not need the spinlock
557 * when clearing I_LOCK, because we're guaranteed
558 * that nobody else tries to do anything about the
559 * state of the inode when it is locked, as we
560 * just created it (so there can be no old holders
561 * that haven't tested I_LOCK).
563 inode->i_state &= ~(I_LOCK|I_NEW);
564 wake_up_inode(inode);
566 EXPORT_SYMBOL(unlock_new_inode);
569 * This is called without the inode lock held.. Be careful.
571 * We no longer cache the sb_flags in i_flags - see fs.h
572 * -- rmk@arm.uk.linux.org
574 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)
576 struct inode * inode;
578 inode = alloc_inode(sb);
582 spin_lock(&inode_lock);
583 /* We released the lock, so.. */
584 old = find_inode(sb, head, test, data);
586 if (set(inode, data))
589 inodes_stat.nr_inodes++;
590 list_add(&inode->i_list, &inode_in_use);
591 hlist_add_head(&inode->i_hash, head);
592 inode->i_state = I_LOCK|I_NEW;
593 spin_unlock(&inode_lock);
595 /* Return the locked inode with I_NEW set, the
596 * caller is responsible for filling in the contents
602 * Uhhuh, somebody else created the same inode under
603 * us. Use the old inode instead of the one we just
607 spin_unlock(&inode_lock);
608 destroy_inode(inode);
610 wait_on_inode(inode);
615 spin_unlock(&inode_lock);
616 destroy_inode(inode);
621 * get_new_inode_fast is the fast path version of get_new_inode, see the
622 * comment at iget_locked for details.
624 static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
626 struct inode * inode;
628 inode = alloc_inode(sb);
632 spin_lock(&inode_lock);
633 /* We released the lock, so.. */
634 old = find_inode_fast(sb, head, ino);
637 inodes_stat.nr_inodes++;
638 list_add(&inode->i_list, &inode_in_use);
639 hlist_add_head(&inode->i_hash, head);
640 inode->i_state = I_LOCK|I_NEW;
641 spin_unlock(&inode_lock);
643 /* Return the locked inode with I_NEW set, the
644 * caller is responsible for filling in the contents
650 * Uhhuh, somebody else created the same inode under
651 * us. Use the old inode instead of the one we just
655 spin_unlock(&inode_lock);
656 destroy_inode(inode);
658 wait_on_inode(inode);
663 static inline unsigned long hash(struct super_block *sb, unsigned long hashval)
665 unsigned long tmp = hashval + ((unsigned long) sb / L1_CACHE_BYTES);
666 tmp = tmp + (tmp >> I_HASHBITS);
667 return tmp & I_HASHMASK;
670 /* Yeah, I know about quadratic hash. Maybe, later. */
673 * iunique - get a unique inode number
675 * @max_reserved: highest reserved inode number
677 * Obtain an inode number that is unique on the system for a given
678 * superblock. This is used by file systems that have no natural
679 * permanent inode numbering system. An inode number is returned that
680 * is higher than the reserved limit but unique.
683 * With a large number of inodes live on the file system this function
684 * currently becomes quite slow.
687 ino_t iunique(struct super_block *sb, ino_t max_reserved)
689 static ino_t counter;
691 struct hlist_head * head;
693 spin_lock(&inode_lock);
695 if (counter > max_reserved) {
696 head = inode_hashtable + hash(sb,counter);
698 inode = find_inode_fast(sb, head, res);
700 spin_unlock(&inode_lock);
704 counter = max_reserved + 1;
710 struct inode *igrab(struct inode *inode)
712 spin_lock(&inode_lock);
713 if (!(inode->i_state & I_FREEING))
717 * Handle the case where s_op->clear_inode is not been
718 * called yet, and somebody is calling igrab
719 * while the inode is getting freed.
722 spin_unlock(&inode_lock);
727 * ifind - internal function, you want ilookup5() or iget5().
728 * @sb: super block of file system to search
729 * @hashval: hash value (usually inode number) to search for
730 * @test: callback used for comparisons between inodes
731 * @data: opaque data pointer to pass to @test
733 * ifind() searches for the inode specified by @hashval and @data in the inode
734 * cache. This is a generalized version of ifind_fast() for file systems where
735 * the inode number is not sufficient for unique identification of an inode.
737 * If the inode is in the cache, the inode is returned with an incremented
740 * Otherwise NULL is returned.
742 * Note, @test is called with the inode_lock held, so can't sleep.
744 static inline struct inode *ifind(struct super_block *sb,
745 struct hlist_head *head, int (*test)(struct inode *, void *),
750 spin_lock(&inode_lock);
751 inode = find_inode(sb, head, test, data);
754 spin_unlock(&inode_lock);
755 wait_on_inode(inode);
758 spin_unlock(&inode_lock);
763 * ifind_fast - internal function, you want ilookup() or iget().
764 * @sb: super block of file system to search
765 * @ino: inode number to search for
767 * ifind_fast() searches for the inode @ino in the inode cache. This is for
768 * file systems where the inode number is sufficient for unique identification
771 * If the inode is in the cache, the inode is returned with an incremented
774 * Otherwise NULL is returned.
776 static inline struct inode *ifind_fast(struct super_block *sb,
777 struct hlist_head *head, unsigned long ino)
781 spin_lock(&inode_lock);
782 inode = find_inode_fast(sb, head, ino);
785 spin_unlock(&inode_lock);
786 wait_on_inode(inode);
789 spin_unlock(&inode_lock);
794 * ilookup5 - search for an inode in the inode cache
795 * @sb: super block of file system to search
796 * @hashval: hash value (usually inode number) to search for
797 * @test: callback used for comparisons between inodes
798 * @data: opaque data pointer to pass to @test
800 * ilookup5() uses ifind() to search for the inode specified by @hashval and
801 * @data in the inode cache. This is a generalized version of ilookup() for
802 * file systems where the inode number is not sufficient for unique
803 * identification of an inode.
805 * If the inode is in the cache, the inode is returned with an incremented
808 * Otherwise NULL is returned.
810 * Note, @test is called with the inode_lock held, so can't sleep.
812 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
813 int (*test)(struct inode *, void *), void *data)
815 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
817 return ifind(sb, head, test, data);
819 EXPORT_SYMBOL(ilookup5);
822 * ilookup - search for an inode in the inode cache
823 * @sb: super block of file system to search
824 * @ino: inode number to search for
826 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
827 * This is for file systems where the inode number is sufficient for unique
828 * identification of an inode.
830 * If the inode is in the cache, the inode is returned with an incremented
833 * Otherwise NULL is returned.
835 struct inode *ilookup(struct super_block *sb, unsigned long ino)
837 struct hlist_head *head = inode_hashtable + hash(sb, ino);
839 return ifind_fast(sb, head, ino);
841 EXPORT_SYMBOL(ilookup);
844 * iget5_locked - obtain an inode from a mounted file system
845 * @sb: super block of file system
846 * @hashval: hash value (usually inode number) to get
847 * @test: callback used for comparisons between inodes
848 * @set: callback used to initialize a new struct inode
849 * @data: opaque data pointer to pass to @test and @set
851 * This is iget() without the read_inode() portion of get_new_inode().
853 * iget5_locked() uses ifind() to search for the inode specified by @hashval
854 * and @data in the inode cache and if present it is returned with an increased
855 * reference count. This is a generalized version of iget_locked() for file
856 * systems where the inode number is not sufficient for unique identification
859 * If the inode is not in cache, get_new_inode() is called to allocate a new
860 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
861 * file system gets to fill it in before unlocking it via unlock_new_inode().
863 * Note both @test and @set are called with the inode_lock held, so can't sleep.
865 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
866 int (*test)(struct inode *, void *),
867 int (*set)(struct inode *, void *), void *data)
869 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
872 inode = ifind(sb, head, test, data);
876 * get_new_inode() will do the right thing, re-trying the search
877 * in case it had to block at any point.
879 return get_new_inode(sb, head, test, set, data);
881 EXPORT_SYMBOL(iget5_locked);
884 * iget_locked - obtain an inode from a mounted file system
885 * @sb: super block of file system
886 * @ino: inode number to get
888 * This is iget() without the read_inode() portion of get_new_inode_fast().
890 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
891 * the inode cache and if present it is returned with an increased reference
892 * count. This is for file systems where the inode number is sufficient for
893 * unique identification of an inode.
895 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
896 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
897 * The file system gets to fill it in before unlocking it via
898 * unlock_new_inode().
900 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
902 struct hlist_head *head = inode_hashtable + hash(sb, ino);
905 inode = ifind_fast(sb, head, ino);
909 * get_new_inode_fast() will do the right thing, re-trying the search
910 * in case it had to block at any point.
912 return get_new_inode_fast(sb, head, ino);
914 EXPORT_SYMBOL(iget_locked);
917 * __insert_inode_hash - hash an inode
918 * @inode: unhashed inode
919 * @hashval: unsigned long value used to locate this object in the
922 * Add an inode to the inode hash for this superblock.
925 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
927 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
928 spin_lock(&inode_lock);
929 hlist_add_head(&inode->i_hash, head);
930 spin_unlock(&inode_lock);
934 * remove_inode_hash - remove an inode from the hash
935 * @inode: inode to unhash
937 * Remove an inode from the superblock.
940 void remove_inode_hash(struct inode *inode)
942 spin_lock(&inode_lock);
943 hlist_del_init(&inode->i_hash);
944 spin_unlock(&inode_lock);
948 * Tell the filesystem that this inode is no longer of any interest and should
949 * be completely destroyed.
951 * We leave the inode in the inode hash table until *after* the filesystem's
952 * ->delete_inode completes. This ensures that an iget (such as nfsd might
953 * instigate) will always find up-to-date information either in the hash or on
956 * I_FREEING is set so that no-one will take a new reference to the inode while
957 * it is being deleted.
959 void generic_delete_inode(struct inode *inode)
961 struct super_operations *op = inode->i_sb->s_op;
963 list_del_init(&inode->i_list);
964 inode->i_state|=I_FREEING;
965 inodes_stat.nr_inodes--;
966 spin_unlock(&inode_lock);
968 if (inode->i_data.nrpages)
969 truncate_inode_pages(&inode->i_data, 0);
971 security_inode_delete(inode);
973 if (op->delete_inode) {
974 void (*delete)(struct inode *) = op->delete_inode;
975 if (!is_bad_inode(inode))
977 /* s_op->delete_inode internally recalls clear_inode() */
981 spin_lock(&inode_lock);
982 hlist_del_init(&inode->i_hash);
983 spin_unlock(&inode_lock);
984 wake_up_inode(inode);
985 if (inode->i_state != I_CLEAR)
987 destroy_inode(inode);
989 EXPORT_SYMBOL(generic_delete_inode);
991 static void generic_forget_inode(struct inode *inode)
993 struct super_block *sb = inode->i_sb;
995 if (!hlist_unhashed(&inode->i_hash)) {
996 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
997 list_del(&inode->i_list);
998 list_add(&inode->i_list, &inode_unused);
1000 inodes_stat.nr_unused++;
1001 spin_unlock(&inode_lock);
1002 if (!sb || (sb->s_flags & MS_ACTIVE))
1004 write_inode_now(inode, 1);
1005 spin_lock(&inode_lock);
1006 inodes_stat.nr_unused--;
1007 hlist_del_init(&inode->i_hash);
1009 list_del_init(&inode->i_list);
1010 inode->i_state|=I_FREEING;
1011 inodes_stat.nr_inodes--;
1012 spin_unlock(&inode_lock);
1013 if (inode->i_data.nrpages)
1014 truncate_inode_pages(&inode->i_data, 0);
1016 destroy_inode(inode);
1020 * Normal UNIX filesystem behaviour: delete the
1021 * inode when the usage count drops to zero, and
1024 static void generic_drop_inode(struct inode *inode)
1026 if (!inode->i_nlink)
1027 generic_delete_inode(inode);
1029 generic_forget_inode(inode);
1033 * Called when we're dropping the last reference
1036 * Call the FS "drop()" function, defaulting to
1037 * the legacy UNIX filesystem behaviour..
1039 * NOTE! NOTE! NOTE! We're called with the inode lock
1040 * held, and the drop function is supposed to release
1043 static inline void iput_final(struct inode *inode)
1045 struct super_operations *op = inode->i_sb->s_op;
1046 void (*drop)(struct inode *) = generic_drop_inode;
1048 if (op && op->drop_inode)
1049 drop = op->drop_inode;
1054 * iput - put an inode
1055 * @inode: inode to put
1057 * Puts an inode, dropping its usage count. If the inode use count hits
1058 * zero the inode is also then freed and may be destroyed.
1061 void iput(struct inode *inode)
1064 struct super_operations *op = inode->i_sb->s_op;
1066 if (inode->i_state == I_CLEAR)
1069 if (op && op->put_inode)
1070 op->put_inode(inode);
1072 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1078 * bmap - find a block number in a file
1079 * @inode: inode of file
1080 * @block: block to find
1082 * Returns the block number on the device holding the inode that
1083 * is the disk block number for the block of the file requested.
1084 * That is, asked for block 4 of inode 1 the function will return the
1085 * disk block relative to the disk start that holds that block of the
1089 sector_t bmap(struct inode * inode, sector_t block)
1092 if (inode->i_mapping->a_ops->bmap)
1093 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1098 * Return true if the filesystem which backs this inode considers the two
1099 * passed timespecs to be sufficiently different to warrant flushing the
1100 * altered time out to disk.
1102 static int inode_times_differ(struct inode *inode,
1103 struct timespec *old, struct timespec *new)
1105 if (IS_ONE_SECOND(inode))
1106 return old->tv_sec != new->tv_sec;
1107 return !timespec_equal(old, new);
1111 * update_atime - update the access time
1112 * @inode: inode accessed
1114 * Update the accessed time on an inode and mark it for writeback.
1115 * This function automatically handles read only file systems and media,
1116 * as well as the "noatime" flag and inode specific "noatime" markers.
1119 void update_atime(struct inode *inode)
1121 struct timespec now;
1123 if (IS_NOATIME(inode))
1125 if (IS_NODIRATIME(inode) && S_ISDIR(inode->i_mode))
1127 if (IS_RDONLY(inode))
1130 now = current_kernel_time();
1131 if (inode_times_differ(inode, &inode->i_atime, &now)) {
1132 inode->i_atime = now;
1133 mark_inode_dirty_sync(inode);
1135 if (!timespec_equal(&inode->i_atime, &now))
1136 inode->i_atime = now;
1141 * inode_update_time - update mtime and ctime time
1142 * @inode: inode accessed
1143 * @ctime_too: update ctime too
1145 * Update the mtime time on an inode and mark it for writeback.
1146 * When ctime_too is specified update the ctime too.
1149 void inode_update_time(struct inode *inode, int ctime_too)
1151 struct timespec now = current_kernel_time();
1154 if (inode_times_differ(inode, &inode->i_mtime, &now))
1156 inode->i_mtime = now;
1159 if (inode_times_differ(inode, &inode->i_ctime, &now))
1161 inode->i_ctime = now;
1164 mark_inode_dirty_sync(inode);
1166 EXPORT_SYMBOL(inode_update_time);
1168 int inode_needs_sync(struct inode *inode)
1172 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1176 EXPORT_SYMBOL(inode_needs_sync);
1179 * Quota functions that want to walk the inode lists..
1183 /* Functions back in dquot.c */
1184 void put_dquot_list(struct list_head *);
1185 int remove_inode_dquot_ref(struct inode *, int, struct list_head *);
1187 void remove_dquot_ref(struct super_block *sb, int type)
1189 struct inode *inode;
1190 struct list_head *act_head;
1191 LIST_HEAD(tofree_head);
1194 return; /* nothing to do */
1195 spin_lock(&inode_lock); /* This lock is for inodes code */
1196 /* We don't have to lock against quota code - test IS_QUOTAINIT is just for speedup... */
1198 list_for_each(act_head, &inode_in_use) {
1199 inode = list_entry(act_head, struct inode, i_list);
1200 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1201 remove_inode_dquot_ref(inode, type, &tofree_head);
1203 list_for_each(act_head, &inode_unused) {
1204 inode = list_entry(act_head, struct inode, i_list);
1205 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1206 remove_inode_dquot_ref(inode, type, &tofree_head);
1208 list_for_each(act_head, &sb->s_dirty) {
1209 inode = list_entry(act_head, struct inode, i_list);
1210 if (IS_QUOTAINIT(inode))
1211 remove_inode_dquot_ref(inode, type, &tofree_head);
1213 list_for_each(act_head, &sb->s_io) {
1214 inode = list_entry(act_head, struct inode, i_list);
1215 if (IS_QUOTAINIT(inode))
1216 remove_inode_dquot_ref(inode, type, &tofree_head);
1218 spin_unlock(&inode_lock);
1220 put_dquot_list(&tofree_head);
1226 * Hashed waitqueues for wait_on_inode(). The table is pretty small - the
1227 * kernel doesn't lock many inodes at the same time.
1229 #define I_WAIT_TABLE_ORDER 3
1230 static struct i_wait_queue_head {
1231 wait_queue_head_t wqh;
1232 } ____cacheline_aligned_in_smp i_wait_queue_heads[1<<I_WAIT_TABLE_ORDER];
1235 * Return the address of the waitqueue_head to be used for this inode
1237 static wait_queue_head_t *i_waitq_head(struct inode *inode)
1239 return &i_wait_queue_heads[hash_ptr(inode, I_WAIT_TABLE_ORDER)].wqh;
1242 void __wait_on_inode(struct inode *inode)
1244 DECLARE_WAITQUEUE(wait, current);
1245 wait_queue_head_t *wq = i_waitq_head(inode);
1247 add_wait_queue(wq, &wait);
1249 set_current_state(TASK_UNINTERRUPTIBLE);
1250 if (inode->i_state & I_LOCK) {
1254 remove_wait_queue(wq, &wait);
1255 __set_current_state(TASK_RUNNING);
1259 * If we try to find an inode in the inode hash while it is being deleted, we
1260 * have to wait until the filesystem completes its deletion before reporting
1261 * that it isn't found. This is because iget will immediately call
1262 * ->read_inode, and we want to be sure that evidence of the deletion is found
1265 * This call might return early if an inode which shares the waitq is woken up.
1266 * This is most easily handled by the caller which will loop around again
1267 * looking for the inode.
1269 * This is called with inode_lock held.
1271 static void __wait_on_freeing_inode(struct inode *inode)
1273 DECLARE_WAITQUEUE(wait, current);
1274 wait_queue_head_t *wq = i_waitq_head(inode);
1276 add_wait_queue(wq, &wait);
1277 set_current_state(TASK_UNINTERRUPTIBLE);
1278 spin_unlock(&inode_lock);
1280 remove_wait_queue(wq, &wait);
1281 spin_lock(&inode_lock);
1284 void wake_up_inode(struct inode *inode)
1286 wait_queue_head_t *wq = i_waitq_head(inode);
1289 * Prevent speculative execution through spin_unlock(&inode_lock);
1292 if (waitqueue_active(wq))
1297 * Initialize the waitqueues and inode hash table.
1299 void __init inode_init(unsigned long mempages)
1301 struct hlist_head *head;
1302 unsigned long order;
1303 unsigned int nr_hash;
1306 for (i = 0; i < ARRAY_SIZE(i_wait_queue_heads); i++)
1307 init_waitqueue_head(&i_wait_queue_heads[i].wqh);
1309 mempages >>= (14 - PAGE_SHIFT);
1310 mempages *= sizeof(struct hlist_head);
1311 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1317 nr_hash = (1UL << order) * PAGE_SIZE /
1318 sizeof(struct hlist_head);
1319 i_hash_mask = (nr_hash - 1);
1323 while ((tmp >>= 1UL) != 0UL)
1326 inode_hashtable = (struct hlist_head *)
1327 __get_free_pages(GFP_ATOMIC, order);
1328 } while (inode_hashtable == NULL && --order >= 0);
1330 printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1331 nr_hash, order, (PAGE_SIZE << order));
1333 if (!inode_hashtable)
1334 panic("Failed to allocate inode hash table\n");
1336 head = inode_hashtable;
1339 INIT_HLIST_HEAD(head);
1344 /* inode slab cache */
1345 inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
1346 0, SLAB_HWCACHE_ALIGN, init_once,
1349 panic("cannot create inode slab cache");
1351 set_shrinker(DEFAULT_SEEKS, shrink_icache_memory);
1354 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1356 inode->i_mode = mode;
1357 if (S_ISCHR(mode)) {
1358 inode->i_fop = &def_chr_fops;
1359 inode->i_rdev = to_kdev_t(rdev);
1360 } else if (S_ISBLK(mode)) {
1361 inode->i_fop = &def_blk_fops;
1362 inode->i_rdev = to_kdev_t(rdev);
1363 } else if (S_ISFIFO(mode))
1364 inode->i_fop = &def_fifo_fops;
1365 else if (S_ISSOCK(mode))
1366 inode->i_fop = &bad_sock_fops;
1368 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",