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 INIT_LIST_HEAD(&inode
->i_data
.private_list
);
188 spin_lock_init(&inode
->i_data
.private_lock
);
189 INIT_LIST_HEAD(&inode
->i_data
.i_mmap
);
190 INIT_LIST_HEAD(&inode
->i_data
.i_mmap_shared
);
191 spin_lock_init(&inode
->i_lock
);
192 i_size_ordered_init(inode
);
195 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
197 struct inode
* inode
= (struct inode
*) foo
;
199 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
200 SLAB_CTOR_CONSTRUCTOR
)
201 inode_init_once(inode
);
205 * inode_lock must be held
207 void __iget(struct inode
* inode
)
209 if (atomic_read(&inode
->i_count
)) {
210 atomic_inc(&inode
->i_count
);
213 atomic_inc(&inode
->i_count
);
214 if (!(inode
->i_state
& (I_DIRTY
|I_LOCK
))) {
215 list_del(&inode
->i_list
);
216 list_add(&inode
->i_list
, &inode_in_use
);
218 inodes_stat
.nr_unused
--;
222 * clear_inode - clear an inode
223 * @inode: inode to clear
225 * This is called by the filesystem to tell us
226 * that the inode is no longer useful. We just
227 * terminate it with extreme prejudice.
230 void clear_inode(struct inode
*inode
)
232 invalidate_inode_buffers(inode
);
234 if (inode
->i_data
.nrpages
)
236 if (!(inode
->i_state
& I_FREEING
))
238 if (inode
->i_state
& I_CLEAR
)
240 wait_on_inode(inode
);
242 if (inode
->i_sb
&& inode
->i_sb
->s_op
->clear_inode
)
243 inode
->i_sb
->s_op
->clear_inode(inode
);
248 inode
->i_state
= I_CLEAR
;
252 * Dispose-list gets a local list with local inodes in it, so it doesn't
253 * need to worry about list corruption and SMP locks.
255 static void dispose_list(struct list_head
*head
)
259 while (!list_empty(head
)) {
262 inode
= list_entry(head
->next
, struct inode
, i_list
);
263 list_del(&inode
->i_list
);
265 if (inode
->i_data
.nrpages
)
266 truncate_inode_pages(&inode
->i_data
, 0);
268 destroy_inode(inode
);
271 spin_lock(&inode_lock
);
272 inodes_stat
.nr_inodes
-= nr_disposed
;
273 spin_unlock(&inode_lock
);
277 * Invalidate all inodes for a device.
279 static int invalidate_list(struct list_head
*head
, struct super_block
* sb
, struct list_head
* dispose
)
281 struct list_head
*next
;
282 int busy
= 0, count
= 0;
286 struct list_head
* tmp
= next
;
287 struct inode
* inode
;
292 inode
= list_entry(tmp
, struct inode
, i_list
);
293 if (inode
->i_sb
!= sb
)
295 invalidate_inode_buffers(inode
);
296 if (!atomic_read(&inode
->i_count
)) {
297 hlist_del_init(&inode
->i_hash
);
298 list_del(&inode
->i_list
);
299 list_add(&inode
->i_list
, dispose
);
300 inode
->i_state
|= I_FREEING
;
306 /* only unused inodes may be cached with i_count zero */
307 inodes_stat
.nr_unused
-= count
;
312 * This is a two-stage process. First we collect all
313 * offending inodes onto the throw-away list, and in
314 * the second stage we actually dispose of them. This
315 * is because we don't want to sleep while messing
316 * with the global lists..
320 * invalidate_inodes - discard the inodes on a device
323 * Discard all of the inodes for a given superblock. If the discard
324 * fails because there are busy inodes then a non zero value is returned.
325 * If the discard is successful all the inodes have been discarded.
328 int invalidate_inodes(struct super_block
* sb
)
331 LIST_HEAD(throw_away
);
334 spin_lock(&inode_lock
);
335 busy
= invalidate_list(&inode_in_use
, sb
, &throw_away
);
336 busy
|= invalidate_list(&inode_unused
, sb
, &throw_away
);
337 busy
|= invalidate_list(&sb
->s_dirty
, sb
, &throw_away
);
338 busy
|= invalidate_list(&sb
->s_io
, sb
, &throw_away
);
339 spin_unlock(&inode_lock
);
341 dispose_list(&throw_away
);
347 int __invalidate_device(struct block_device
*bdev
, int do_sync
)
349 struct super_block
*sb
;
356 sb
= get_super(bdev
);
359 * no need to lock the super, get_super holds the
360 * read semaphore so the filesystem cannot go away
361 * under us (->put_super runs with the write lock
364 shrink_dcache_sb(sb
);
365 res
= invalidate_inodes(sb
);
368 invalidate_bdev(bdev
, 0);
372 static int can_unuse(struct inode
*inode
)
376 if (inode_has_buffers(inode
))
378 if (atomic_read(&inode
->i_count
))
380 if (inode
->i_data
.nrpages
)
386 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
387 * a temporary list and then are freed outside inode_lock by dispose_list().
389 * Any inodes which are pinned purely because of attached pagecache have their
390 * pagecache removed. We expect the final iput() on that inode to add it to
391 * the front of the inode_unused list. So look for it there and if the
392 * inode is still freeable, proceed. The right inode is found 99.9% of the
393 * time in testing on a 4-way.
395 * If the inode has metadata buffers attached to mapping->private_list then
396 * try to remove them.
398 static void prune_icache(int nr_to_scan
)
403 unsigned long reap
= 0;
406 spin_lock(&inode_lock
);
407 for (nr_scanned
= 0; nr_scanned
< nr_to_scan
; nr_scanned
++) {
410 if (list_empty(&inode_unused
))
413 inode
= list_entry(inode_unused
.prev
, struct inode
, i_list
);
415 if (inode
->i_state
|| atomic_read(&inode
->i_count
)) {
416 list_move(&inode
->i_list
, &inode_unused
);
419 if (inode_has_buffers(inode
) || inode
->i_data
.nrpages
) {
421 spin_unlock(&inode_lock
);
422 if (remove_inode_buffers(inode
))
423 reap
+= invalidate_inode_pages(&inode
->i_data
);
425 spin_lock(&inode_lock
);
427 if (inode
!= list_entry(inode_unused
.next
,
428 struct inode
, i_list
))
429 continue; /* wrong inode or list_empty */
430 if (!can_unuse(inode
))
433 hlist_del_init(&inode
->i_hash
);
434 list_move(&inode
->i_list
, &freeable
);
435 inode
->i_state
|= I_FREEING
;
438 inodes_stat
.nr_unused
-= nr_pruned
;
439 spin_unlock(&inode_lock
);
441 dispose_list(&freeable
);
444 if (current_is_kswapd
)
445 mod_page_state(kswapd_inodesteal
, reap
);
447 mod_page_state(pginodesteal
, reap
);
451 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
452 * "unused" means that no dentries are referring to the inodes: the files are
453 * not open and the dcache references to those inodes have already been
456 * This function is passed the number of inodes to scan, and it returns the
457 * total number of remaining possibly-reclaimable inodes.
459 static int shrink_icache_memory(int nr
, unsigned int gfp_mask
)
463 * Nasty deadlock avoidance. We may hold various FS locks,
464 * and we don't want to recurse into the FS that called us
465 * in clear_inode() and friends..
467 if (gfp_mask
& __GFP_FS
)
470 return inodes_stat
.nr_unused
;
473 static void __wait_on_freeing_inode(struct inode
*inode
);
475 * Called with the inode lock held.
476 * NOTE: we are not increasing the inode-refcount, you must call __iget()
477 * by hand after calling find_inode now! This simplifies iunique and won't
478 * add any additional branch in the common code.
480 static struct inode
* find_inode(struct super_block
* sb
, struct hlist_head
*head
, int (*test
)(struct inode
*, void *), void *data
)
482 struct hlist_node
*node
;
483 struct inode
* inode
= NULL
;
486 hlist_for_each (node
, head
) {
487 inode
= hlist_entry(node
, struct inode
, i_hash
);
488 if (inode
->i_sb
!= sb
)
490 if (!test(inode
, data
))
492 if (inode
->i_state
& (I_FREEING
|I_CLEAR
)) {
493 __wait_on_freeing_inode(inode
);
498 return node
? inode
: NULL
;
502 * find_inode_fast is the fast path version of find_inode, see the comment at
503 * iget_locked for details.
505 static struct inode
* find_inode_fast(struct super_block
* sb
, struct hlist_head
*head
, unsigned long ino
)
507 struct hlist_node
*node
;
508 struct inode
* inode
= NULL
;
511 hlist_for_each (node
, head
) {
512 inode
= hlist_entry(node
, struct inode
, i_hash
);
513 if (inode
->i_ino
!= ino
)
515 if (inode
->i_sb
!= sb
)
517 if (inode
->i_state
& (I_FREEING
|I_CLEAR
)) {
518 __wait_on_freeing_inode(inode
);
523 return node
? inode
: NULL
;
527 * new_inode - obtain an inode
530 * Allocates a new inode for given superblock.
533 struct inode
*new_inode(struct super_block
*sb
)
535 static unsigned long last_ino
;
536 struct inode
* inode
;
538 spin_lock_prefetch(&inode_lock
);
540 inode
= alloc_inode(sb
);
542 spin_lock(&inode_lock
);
543 inodes_stat
.nr_inodes
++;
544 list_add(&inode
->i_list
, &inode_in_use
);
545 inode
->i_ino
= ++last_ino
;
547 spin_unlock(&inode_lock
);
552 void unlock_new_inode(struct inode
*inode
)
555 * This is special! We do not need the spinlock
556 * when clearing I_LOCK, because we're guaranteed
557 * that nobody else tries to do anything about the
558 * state of the inode when it is locked, as we
559 * just created it (so there can be no old holders
560 * that haven't tested I_LOCK).
562 inode
->i_state
&= ~(I_LOCK
|I_NEW
);
563 wake_up_inode(inode
);
565 EXPORT_SYMBOL(unlock_new_inode
);
568 * This is called without the inode lock held.. Be careful.
570 * We no longer cache the sb_flags in i_flags - see fs.h
571 * -- rmk@arm.uk.linux.org
573 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
)
575 struct inode
* inode
;
577 inode
= alloc_inode(sb
);
581 spin_lock(&inode_lock
);
582 /* We released the lock, so.. */
583 old
= find_inode(sb
, head
, test
, data
);
585 if (set(inode
, data
))
588 inodes_stat
.nr_inodes
++;
589 list_add(&inode
->i_list
, &inode_in_use
);
590 hlist_add_head(&inode
->i_hash
, head
);
591 inode
->i_state
= I_LOCK
|I_NEW
;
592 spin_unlock(&inode_lock
);
594 /* Return the locked inode with I_NEW set, the
595 * caller is responsible for filling in the contents
601 * Uhhuh, somebody else created the same inode under
602 * us. Use the old inode instead of the one we just
606 spin_unlock(&inode_lock
);
607 destroy_inode(inode
);
609 wait_on_inode(inode
);
614 spin_unlock(&inode_lock
);
615 destroy_inode(inode
);
620 * get_new_inode_fast is the fast path version of get_new_inode, see the
621 * comment at iget_locked for details.
623 static struct inode
* get_new_inode_fast(struct super_block
*sb
, struct hlist_head
*head
, unsigned long ino
)
625 struct inode
* inode
;
627 inode
= alloc_inode(sb
);
631 spin_lock(&inode_lock
);
632 /* We released the lock, so.. */
633 old
= find_inode_fast(sb
, head
, ino
);
636 inodes_stat
.nr_inodes
++;
637 list_add(&inode
->i_list
, &inode_in_use
);
638 hlist_add_head(&inode
->i_hash
, head
);
639 inode
->i_state
= I_LOCK
|I_NEW
;
640 spin_unlock(&inode_lock
);
642 /* Return the locked inode with I_NEW set, the
643 * caller is responsible for filling in the contents
649 * Uhhuh, somebody else created the same inode under
650 * us. Use the old inode instead of the one we just
654 spin_unlock(&inode_lock
);
655 destroy_inode(inode
);
657 wait_on_inode(inode
);
662 static inline unsigned long hash(struct super_block
*sb
, unsigned long hashval
)
664 unsigned long tmp
= hashval
+ ((unsigned long) sb
/ L1_CACHE_BYTES
);
665 tmp
= tmp
+ (tmp
>> I_HASHBITS
);
666 return tmp
& I_HASHMASK
;
669 /* Yeah, I know about quadratic hash. Maybe, later. */
672 * iunique - get a unique inode number
674 * @max_reserved: highest reserved inode number
676 * Obtain an inode number that is unique on the system for a given
677 * superblock. This is used by file systems that have no natural
678 * permanent inode numbering system. An inode number is returned that
679 * is higher than the reserved limit but unique.
682 * With a large number of inodes live on the file system this function
683 * currently becomes quite slow.
686 ino_t
iunique(struct super_block
*sb
, ino_t max_reserved
)
688 static ino_t counter
= 0;
690 struct hlist_head
* head
;
692 spin_lock(&inode_lock
);
694 if (counter
> max_reserved
) {
695 head
= inode_hashtable
+ hash(sb
,counter
);
697 inode
= find_inode_fast(sb
, head
, res
);
699 spin_unlock(&inode_lock
);
703 counter
= max_reserved
+ 1;
709 struct inode
*igrab(struct inode
*inode
)
711 spin_lock(&inode_lock
);
712 if (!(inode
->i_state
& I_FREEING
))
716 * Handle the case where s_op->clear_inode is not been
717 * called yet, and somebody is calling igrab
718 * while the inode is getting freed.
721 spin_unlock(&inode_lock
);
726 * ifind - internal function, you want ilookup5() or iget5().
727 * @sb: super block of file system to search
728 * @hashval: hash value (usually inode number) to search for
729 * @test: callback used for comparisons between inodes
730 * @data: opaque data pointer to pass to @test
732 * ifind() searches for the inode specified by @hashval and @data in the inode
733 * cache. This is a generalized version of ifind_fast() for file systems where
734 * the inode number is not sufficient for unique identification of an inode.
736 * If the inode is in the cache, the inode is returned with an incremented
739 * Otherwise NULL is returned.
741 * Note, @test is called with the inode_lock held, so can't sleep.
743 static inline struct inode
*ifind(struct super_block
*sb
,
744 struct hlist_head
*head
, int (*test
)(struct inode
*, void *),
749 spin_lock(&inode_lock
);
750 inode
= find_inode(sb
, head
, test
, data
);
753 spin_unlock(&inode_lock
);
754 wait_on_inode(inode
);
757 spin_unlock(&inode_lock
);
762 * ifind_fast - internal function, you want ilookup() or iget().
763 * @sb: super block of file system to search
764 * @ino: inode number to search for
766 * ifind_fast() searches for the inode @ino in the inode cache. This is for
767 * file systems where the inode number is sufficient for unique identification
770 * If the inode is in the cache, the inode is returned with an incremented
773 * Otherwise NULL is returned.
775 static inline struct inode
*ifind_fast(struct super_block
*sb
,
776 struct hlist_head
*head
, unsigned long ino
)
780 spin_lock(&inode_lock
);
781 inode
= find_inode_fast(sb
, head
, ino
);
784 spin_unlock(&inode_lock
);
785 wait_on_inode(inode
);
788 spin_unlock(&inode_lock
);
793 * ilookup5 - search for an inode in the inode cache
794 * @sb: super block of file system to search
795 * @hashval: hash value (usually inode number) to search for
796 * @test: callback used for comparisons between inodes
797 * @data: opaque data pointer to pass to @test
799 * ilookup5() uses ifind() to search for the inode specified by @hashval and
800 * @data in the inode cache. This is a generalized version of ilookup() for
801 * file systems where the inode number is not sufficient for unique
802 * identification of an inode.
804 * If the inode is in the cache, the inode is returned with an incremented
807 * Otherwise NULL is returned.
809 * Note, @test is called with the inode_lock held, so can't sleep.
811 struct inode
*ilookup5(struct super_block
*sb
, unsigned long hashval
,
812 int (*test
)(struct inode
*, void *), void *data
)
814 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
816 return ifind(sb
, head
, test
, data
);
818 EXPORT_SYMBOL(ilookup5
);
821 * ilookup - search for an inode in the inode cache
822 * @sb: super block of file system to search
823 * @ino: inode number to search for
825 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
826 * This is for file systems where the inode number is sufficient for unique
827 * identification of an inode.
829 * If the inode is in the cache, the inode is returned with an incremented
832 * Otherwise NULL is returned.
834 struct inode
*ilookup(struct super_block
*sb
, unsigned long ino
)
836 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
838 return ifind_fast(sb
, head
, ino
);
840 EXPORT_SYMBOL(ilookup
);
843 * iget5_locked - obtain an inode from a mounted file system
844 * @sb: super block of file system
845 * @hashval: hash value (usually inode number) to get
846 * @test: callback used for comparisons between inodes
847 * @set: callback used to initialize a new struct inode
848 * @data: opaque data pointer to pass to @test and @set
850 * This is iget() without the read_inode() portion of get_new_inode().
852 * iget5_locked() uses ifind() to search for the inode specified by @hashval
853 * and @data in the inode cache and if present it is returned with an increased
854 * reference count. This is a generalized version of iget_locked() for file
855 * systems where the inode number is not sufficient for unique identification
858 * If the inode is not in cache, get_new_inode() is called to allocate a new
859 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
860 * file system gets to fill it in before unlocking it via unlock_new_inode().
862 * Note both @test and @set are called with the inode_lock held, so can't sleep.
864 struct inode
*iget5_locked(struct super_block
*sb
, unsigned long hashval
,
865 int (*test
)(struct inode
*, void *),
866 int (*set
)(struct inode
*, void *), void *data
)
868 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
871 inode
= ifind(sb
, head
, test
, data
);
875 * get_new_inode() will do the right thing, re-trying the search
876 * in case it had to block at any point.
878 return get_new_inode(sb
, head
, test
, set
, data
);
880 EXPORT_SYMBOL(iget5_locked
);
883 * iget_locked - obtain an inode from a mounted file system
884 * @sb: super block of file system
885 * @ino: inode number to get
887 * This is iget() without the read_inode() portion of get_new_inode_fast().
889 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
890 * the inode cache and if present it is returned with an increased reference
891 * count. This is for file systems where the inode number is sufficient for
892 * unique identification of an inode.
894 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
895 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
896 * The file system gets to fill it in before unlocking it via
897 * unlock_new_inode().
899 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
901 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
904 inode
= ifind_fast(sb
, head
, ino
);
908 * get_new_inode_fast() will do the right thing, re-trying the search
909 * in case it had to block at any point.
911 return get_new_inode_fast(sb
, head
, ino
);
913 EXPORT_SYMBOL(iget_locked
);
916 * __insert_inode_hash - hash an inode
917 * @inode: unhashed inode
918 * @hashval: unsigned long value used to locate this object in the
921 * Add an inode to the inode hash for this superblock.
924 void __insert_inode_hash(struct inode
*inode
, unsigned long hashval
)
926 struct hlist_head
*head
= inode_hashtable
+ hash(inode
->i_sb
, hashval
);
927 spin_lock(&inode_lock
);
928 hlist_add_head(&inode
->i_hash
, head
);
929 spin_unlock(&inode_lock
);
933 * remove_inode_hash - remove an inode from the hash
934 * @inode: inode to unhash
936 * Remove an inode from the superblock.
939 void remove_inode_hash(struct inode
*inode
)
941 spin_lock(&inode_lock
);
942 hlist_del_init(&inode
->i_hash
);
943 spin_unlock(&inode_lock
);
947 * Tell the filesystem that this inode is no longer of any interest and should
948 * be completely destroyed.
950 * We leave the inode in the inode hash table until *after* the filesystem's
951 * ->delete_inode completes. This ensures that an iget (such as nfsd might
952 * instigate) will always find up-to-date information either in the hash or on
955 * I_FREEING is set so that no-one will take a new reference to the inode while
956 * it is being deleted.
958 void generic_delete_inode(struct inode
*inode
)
960 struct super_operations
*op
= inode
->i_sb
->s_op
;
962 list_del_init(&inode
->i_list
);
963 inode
->i_state
|=I_FREEING
;
964 inodes_stat
.nr_inodes
--;
965 spin_unlock(&inode_lock
);
967 if (inode
->i_data
.nrpages
)
968 truncate_inode_pages(&inode
->i_data
, 0);
970 security_inode_delete(inode
);
972 if (op
->delete_inode
) {
973 void (*delete)(struct inode
*) = op
->delete_inode
;
974 if (!is_bad_inode(inode
))
976 /* s_op->delete_inode internally recalls clear_inode() */
980 spin_lock(&inode_lock
);
981 hlist_del_init(&inode
->i_hash
);
982 spin_unlock(&inode_lock
);
983 wake_up_inode(inode
);
984 if (inode
->i_state
!= I_CLEAR
)
986 destroy_inode(inode
);
988 EXPORT_SYMBOL(generic_delete_inode
);
990 static void generic_forget_inode(struct inode
*inode
)
992 struct super_block
*sb
= inode
->i_sb
;
994 if (!hlist_unhashed(&inode
->i_hash
)) {
995 if (!(inode
->i_state
& (I_DIRTY
|I_LOCK
))) {
996 list_del(&inode
->i_list
);
997 list_add(&inode
->i_list
, &inode_unused
);
999 inodes_stat
.nr_unused
++;
1000 spin_unlock(&inode_lock
);
1001 if (!sb
|| (sb
->s_flags
& MS_ACTIVE
))
1003 write_inode_now(inode
, 1);
1004 spin_lock(&inode_lock
);
1005 inodes_stat
.nr_unused
--;
1006 hlist_del_init(&inode
->i_hash
);
1008 list_del_init(&inode
->i_list
);
1009 inode
->i_state
|=I_FREEING
;
1010 inodes_stat
.nr_inodes
--;
1011 spin_unlock(&inode_lock
);
1012 if (inode
->i_data
.nrpages
)
1013 truncate_inode_pages(&inode
->i_data
, 0);
1015 destroy_inode(inode
);
1019 * Normal UNIX filesystem behaviour: delete the
1020 * inode when the usage count drops to zero, and
1023 static void generic_drop_inode(struct inode
*inode
)
1025 if (!inode
->i_nlink
)
1026 generic_delete_inode(inode
);
1028 generic_forget_inode(inode
);
1032 * Called when we're dropping the last reference
1035 * Call the FS "drop()" function, defaulting to
1036 * the legacy UNIX filesystem behaviour..
1038 * NOTE! NOTE! NOTE! We're called with the inode lock
1039 * held, and the drop function is supposed to release
1042 static inline void iput_final(struct inode
*inode
)
1044 struct super_operations
*op
= inode
->i_sb
->s_op
;
1045 void (*drop
)(struct inode
*) = generic_drop_inode
;
1047 if (op
&& op
->drop_inode
)
1048 drop
= op
->drop_inode
;
1053 * iput - put an inode
1054 * @inode: inode to put
1056 * Puts an inode, dropping its usage count. If the inode use count hits
1057 * zero the inode is also then freed and may be destroyed.
1060 void iput(struct inode
*inode
)
1063 struct super_operations
*op
= inode
->i_sb
->s_op
;
1065 if (inode
->i_state
== I_CLEAR
)
1068 if (op
&& op
->put_inode
)
1069 op
->put_inode(inode
);
1071 if (atomic_dec_and_lock(&inode
->i_count
, &inode_lock
))
1077 * bmap - find a block number in a file
1078 * @inode: inode of file
1079 * @block: block to find
1081 * Returns the block number on the device holding the inode that
1082 * is the disk block number for the block of the file requested.
1083 * That is, asked for block 4 of inode 1 the function will return the
1084 * disk block relative to the disk start that holds that block of the
1088 sector_t
bmap(struct inode
* inode
, sector_t block
)
1091 if (inode
->i_mapping
->a_ops
->bmap
)
1092 res
= inode
->i_mapping
->a_ops
->bmap(inode
->i_mapping
, block
);
1097 * Return true if the filesystem which backs this inode considers the two
1098 * passed timespecs to be sufficiently different to warrant flushing the
1099 * altered time out to disk.
1101 static int inode_times_differ(struct inode
*inode
,
1102 struct timespec
*old
, struct timespec
*new)
1104 if (IS_ONE_SECOND(inode
))
1105 return old
->tv_sec
!= new->tv_sec
;
1106 return !timespec_equal(old
, new);
1110 * update_atime - update the access time
1111 * @inode: inode accessed
1113 * Update the accessed time on an inode and mark it for writeback.
1114 * This function automatically handles read only file systems and media,
1115 * as well as the "noatime" flag and inode specific "noatime" markers.
1118 void update_atime(struct inode
*inode
)
1120 struct timespec now
;
1122 if (IS_NOATIME(inode
))
1124 if (IS_NODIRATIME(inode
) && S_ISDIR(inode
->i_mode
))
1126 if (IS_RDONLY(inode
))
1129 now
= current_kernel_time();
1130 if (inode_times_differ(inode
, &inode
->i_atime
, &now
)) {
1131 inode
->i_atime
= now
;
1132 mark_inode_dirty_sync(inode
);
1134 if (!timespec_equal(&inode
->i_atime
, &now
))
1135 inode
->i_atime
= now
;
1140 * inode_update_time - update mtime and ctime time
1141 * @inode: inode accessed
1142 * @ctime_too: update ctime too
1144 * Update the mtime time on an inode and mark it for writeback.
1145 * When ctime_too is specified update the ctime too.
1148 void inode_update_time(struct inode
*inode
, int ctime_too
)
1150 struct timespec now
= current_kernel_time();
1153 if (inode_times_differ(inode
, &inode
->i_mtime
, &now
))
1155 inode
->i_mtime
= now
;
1158 if (inode_times_differ(inode
, &inode
->i_ctime
, &now
))
1160 inode
->i_ctime
= now
;
1163 mark_inode_dirty_sync(inode
);
1165 EXPORT_SYMBOL(inode_update_time
);
1167 int inode_needs_sync(struct inode
*inode
)
1171 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
1175 EXPORT_SYMBOL(inode_needs_sync
);
1178 * Quota functions that want to walk the inode lists..
1182 /* Functions back in dquot.c */
1183 void put_dquot_list(struct list_head
*);
1184 int remove_inode_dquot_ref(struct inode
*, int, struct list_head
*);
1186 void remove_dquot_ref(struct super_block
*sb
, int type
)
1188 struct inode
*inode
;
1189 struct list_head
*act_head
;
1190 LIST_HEAD(tofree_head
);
1193 return; /* nothing to do */
1194 spin_lock(&inode_lock
); /* This lock is for inodes code */
1195 /* We don't have to lock against quota code - test IS_QUOTAINIT is just for speedup... */
1197 list_for_each(act_head
, &inode_in_use
) {
1198 inode
= list_entry(act_head
, struct inode
, i_list
);
1199 if (inode
->i_sb
== sb
&& IS_QUOTAINIT(inode
))
1200 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1202 list_for_each(act_head
, &inode_unused
) {
1203 inode
= list_entry(act_head
, struct inode
, i_list
);
1204 if (inode
->i_sb
== sb
&& IS_QUOTAINIT(inode
))
1205 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1207 list_for_each(act_head
, &sb
->s_dirty
) {
1208 inode
= list_entry(act_head
, struct inode
, i_list
);
1209 if (IS_QUOTAINIT(inode
))
1210 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1212 list_for_each(act_head
, &sb
->s_io
) {
1213 inode
= list_entry(act_head
, struct inode
, i_list
);
1214 if (IS_QUOTAINIT(inode
))
1215 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1217 spin_unlock(&inode_lock
);
1219 put_dquot_list(&tofree_head
);
1225 * Hashed waitqueues for wait_on_inode(). The table is pretty small - the
1226 * kernel doesn't lock many inodes at the same time.
1228 #define I_WAIT_TABLE_ORDER 3
1229 static struct i_wait_queue_head
{
1230 wait_queue_head_t wqh
;
1231 } ____cacheline_aligned_in_smp i_wait_queue_heads
[1<<I_WAIT_TABLE_ORDER
];
1234 * Return the address of the waitqueue_head to be used for this inode
1236 static wait_queue_head_t
*i_waitq_head(struct inode
*inode
)
1238 return &i_wait_queue_heads
[hash_ptr(inode
, I_WAIT_TABLE_ORDER
)].wqh
;
1241 void __wait_on_inode(struct inode
*inode
)
1243 DECLARE_WAITQUEUE(wait
, current
);
1244 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1246 add_wait_queue(wq
, &wait
);
1248 set_current_state(TASK_UNINTERRUPTIBLE
);
1249 if (inode
->i_state
& I_LOCK
) {
1253 remove_wait_queue(wq
, &wait
);
1254 __set_current_state(TASK_RUNNING
);
1258 * If we try to find an inode in the inode hash while it is being deleted, we
1259 * have to wait until the filesystem completes its deletion before reporting
1260 * that it isn't found. This is because iget will immediately call
1261 * ->read_inode, and we want to be sure that evidence of the deletion is found
1264 * This call might return early if an inode which shares the waitq is woken up.
1265 * This is most easily handled by the caller which will loop around again
1266 * looking for the inode.
1268 * This is called with inode_lock held.
1270 static void __wait_on_freeing_inode(struct inode
*inode
)
1272 DECLARE_WAITQUEUE(wait
, current
);
1273 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1275 add_wait_queue(wq
, &wait
);
1276 set_current_state(TASK_UNINTERRUPTIBLE
);
1277 spin_unlock(&inode_lock
);
1279 remove_wait_queue(wq
, &wait
);
1280 spin_lock(&inode_lock
);
1283 void wake_up_inode(struct inode
*inode
)
1285 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1288 * Prevent speculative execution through spin_unlock(&inode_lock);
1291 if (waitqueue_active(wq
))
1296 * Initialize the waitqueues and inode hash table.
1298 void __init
inode_init(unsigned long mempages
)
1300 struct hlist_head
*head
;
1301 unsigned long order
;
1302 unsigned int nr_hash
;
1305 for (i
= 0; i
< ARRAY_SIZE(i_wait_queue_heads
); i
++)
1306 init_waitqueue_head(&i_wait_queue_heads
[i
].wqh
);
1308 mempages
>>= (14 - PAGE_SHIFT
);
1309 mempages
*= sizeof(struct hlist_head
);
1310 for (order
= 0; ((1UL << order
) << PAGE_SHIFT
) < mempages
; order
++)
1316 nr_hash
= (1UL << order
) * PAGE_SIZE
/
1317 sizeof(struct hlist_head
);
1318 i_hash_mask
= (nr_hash
- 1);
1322 while ((tmp
>>= 1UL) != 0UL)
1325 inode_hashtable
= (struct hlist_head
*)
1326 __get_free_pages(GFP_ATOMIC
, order
);
1327 } while (inode_hashtable
== NULL
&& --order
>= 0);
1329 printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1330 nr_hash
, order
, (PAGE_SIZE
<< order
));
1332 if (!inode_hashtable
)
1333 panic("Failed to allocate inode hash table\n");
1335 head
= inode_hashtable
;
1338 INIT_HLIST_HEAD(head
);
1343 /* inode slab cache */
1344 inode_cachep
= kmem_cache_create("inode_cache", sizeof(struct inode
),
1345 0, SLAB_HWCACHE_ALIGN
, init_once
,
1348 panic("cannot create inode slab cache");
1350 set_shrinker(DEFAULT_SEEKS
, shrink_icache_memory
);
1353 void init_special_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
1355 inode
->i_mode
= mode
;
1356 if (S_ISCHR(mode
)) {
1357 inode
->i_fop
= &def_chr_fops
;
1358 inode
->i_rdev
= to_kdev_t(rdev
);
1359 } else if (S_ISBLK(mode
)) {
1360 inode
->i_fop
= &def_blk_fops
;
1361 inode
->i_rdev
= to_kdev_t(rdev
);
1362 } else if (S_ISFIFO(mode
))
1363 inode
->i_fop
= &def_fifo_fops
;
1364 else if (S_ISSOCK(mode
))
1365 inode
->i_fop
= &bad_sock_fops
;
1367 printk(KERN_DEBUG
"init_special_inode: bogus i_mode (%o)\n",