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
;
74 static HLIST_HEAD(anon_hash_chain
); /* for inodes with NULL i_sb */
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;
129 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
130 inode
->i_pipe
= NULL
;
131 inode
->i_bdev
= NULL
;
132 inode
->i_cdev
= NULL
;
133 inode
->i_rdev
= to_kdev_t(0);
134 inode
->i_security
= NULL
;
135 if (security_inode_alloc(inode
)) {
136 if (inode
->i_sb
->s_op
->destroy_inode
)
137 inode
->i_sb
->s_op
->destroy_inode(inode
);
139 kmem_cache_free(inode_cachep
, (inode
));
143 mapping
->a_ops
= &empty_aops
;
144 mapping
->host
= inode
;
145 mapping
->gfp_mask
= GFP_HIGHUSER
;
146 mapping
->dirtied_when
= 0;
147 mapping
->assoc_mapping
= NULL
;
148 mapping
->backing_dev_info
= &default_backing_dev_info
;
150 mapping
->backing_dev_info
= sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
151 memset(&inode
->u
, 0, sizeof(inode
->u
));
152 inode
->i_mapping
= mapping
;
157 void destroy_inode(struct inode
*inode
)
159 if (inode_has_buffers(inode
))
161 security_inode_free(inode
);
162 if (inode
->i_sb
->s_op
->destroy_inode
)
163 inode
->i_sb
->s_op
->destroy_inode(inode
);
165 kmem_cache_free(inode_cachep
, (inode
));
170 * These are initializations that only need to be done
171 * once, because the fields are idempotent across use
172 * of the inode, so let the slab aware of that.
174 void inode_init_once(struct inode
*inode
)
176 memset(inode
, 0, sizeof(*inode
));
177 INIT_HLIST_NODE(&inode
->i_hash
);
178 INIT_LIST_HEAD(&inode
->i_data
.clean_pages
);
179 INIT_LIST_HEAD(&inode
->i_data
.dirty_pages
);
180 INIT_LIST_HEAD(&inode
->i_data
.locked_pages
);
181 INIT_LIST_HEAD(&inode
->i_data
.io_pages
);
182 INIT_LIST_HEAD(&inode
->i_dentry
);
183 INIT_LIST_HEAD(&inode
->i_devices
);
184 sema_init(&inode
->i_sem
, 1);
185 INIT_RADIX_TREE(&inode
->i_data
.page_tree
, GFP_ATOMIC
);
186 spin_lock_init(&inode
->i_data
.page_lock
);
187 init_MUTEX(&inode
->i_data
.i_shared_sem
);
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
);
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 prefetch(node
->next
);
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 prefetch(node
->next
);
514 inode
= list_entry(node
, struct inode
, i_hash
);
515 if (inode
->i_ino
!= ino
)
517 if (inode
->i_sb
!= sb
)
519 if (inode
->i_state
& (I_FREEING
|I_CLEAR
)) {
520 __wait_on_freeing_inode(inode
);
525 return node
? inode
: NULL
;
529 * new_inode - obtain an inode
532 * Allocates a new inode for given superblock.
535 struct inode
*new_inode(struct super_block
*sb
)
537 static unsigned long last_ino
;
538 struct inode
* inode
;
540 spin_lock_prefetch(&inode_lock
);
542 inode
= alloc_inode(sb
);
544 spin_lock(&inode_lock
);
545 inodes_stat
.nr_inodes
++;
546 list_add(&inode
->i_list
, &inode_in_use
);
547 inode
->i_ino
= ++last_ino
;
549 spin_unlock(&inode_lock
);
554 void unlock_new_inode(struct inode
*inode
)
557 * This is special! We do not need the spinlock
558 * when clearing I_LOCK, because we're guaranteed
559 * that nobody else tries to do anything about the
560 * state of the inode when it is locked, as we
561 * just created it (so there can be no old holders
562 * that haven't tested I_LOCK).
564 inode
->i_state
&= ~(I_LOCK
|I_NEW
);
565 wake_up_inode(inode
);
567 EXPORT_SYMBOL(unlock_new_inode
);
570 * This is called without the inode lock held.. Be careful.
572 * We no longer cache the sb_flags in i_flags - see fs.h
573 * -- rmk@arm.uk.linux.org
575 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
)
577 struct inode
* inode
;
579 inode
= alloc_inode(sb
);
583 spin_lock(&inode_lock
);
584 /* We released the lock, so.. */
585 old
= find_inode(sb
, head
, test
, data
);
587 if (set(inode
, data
))
590 inodes_stat
.nr_inodes
++;
591 list_add(&inode
->i_list
, &inode_in_use
);
592 hlist_add_head(&inode
->i_hash
, head
);
593 inode
->i_state
= I_LOCK
|I_NEW
;
594 spin_unlock(&inode_lock
);
596 /* Return the locked inode with I_NEW set, the
597 * caller is responsible for filling in the contents
603 * Uhhuh, somebody else created the same inode under
604 * us. Use the old inode instead of the one we just
608 spin_unlock(&inode_lock
);
609 destroy_inode(inode
);
611 wait_on_inode(inode
);
616 spin_unlock(&inode_lock
);
617 destroy_inode(inode
);
622 * get_new_inode_fast is the fast path version of get_new_inode, see the
623 * comment at iget_locked for details.
625 static struct inode
* get_new_inode_fast(struct super_block
*sb
, struct hlist_head
*head
, unsigned long ino
)
627 struct inode
* inode
;
629 inode
= alloc_inode(sb
);
633 spin_lock(&inode_lock
);
634 /* We released the lock, so.. */
635 old
= find_inode_fast(sb
, head
, ino
);
638 inodes_stat
.nr_inodes
++;
639 list_add(&inode
->i_list
, &inode_in_use
);
640 hlist_add_head(&inode
->i_hash
, head
);
641 inode
->i_state
= I_LOCK
|I_NEW
;
642 spin_unlock(&inode_lock
);
644 /* Return the locked inode with I_NEW set, the
645 * caller is responsible for filling in the contents
651 * Uhhuh, somebody else created the same inode under
652 * us. Use the old inode instead of the one we just
656 spin_unlock(&inode_lock
);
657 destroy_inode(inode
);
659 wait_on_inode(inode
);
664 static inline unsigned long hash(struct super_block
*sb
, unsigned long hashval
)
666 unsigned long tmp
= hashval
+ ((unsigned long) sb
/ L1_CACHE_BYTES
);
667 tmp
= tmp
+ (tmp
>> I_HASHBITS
);
668 return tmp
& I_HASHMASK
;
671 /* Yeah, I know about quadratic hash. Maybe, later. */
674 * iunique - get a unique inode number
676 * @max_reserved: highest reserved inode number
678 * Obtain an inode number that is unique on the system for a given
679 * superblock. This is used by file systems that have no natural
680 * permanent inode numbering system. An inode number is returned that
681 * is higher than the reserved limit but unique.
684 * With a large number of inodes live on the file system this function
685 * currently becomes quite slow.
688 ino_t
iunique(struct super_block
*sb
, ino_t max_reserved
)
690 static ino_t counter
= 0;
692 struct hlist_head
* head
;
694 spin_lock(&inode_lock
);
696 if (counter
> max_reserved
) {
697 head
= inode_hashtable
+ hash(sb
,counter
);
699 inode
= find_inode_fast(sb
, head
, res
);
701 spin_unlock(&inode_lock
);
705 counter
= max_reserved
+ 1;
711 struct inode
*igrab(struct inode
*inode
)
713 spin_lock(&inode_lock
);
714 if (!(inode
->i_state
& I_FREEING
))
718 * Handle the case where s_op->clear_inode is not been
719 * called yet, and somebody is calling igrab
720 * while the inode is getting freed.
723 spin_unlock(&inode_lock
);
728 * ifind - internal function, you want ilookup5() or iget5().
729 * @sb: super block of file system to search
730 * @hashval: hash value (usually inode number) to search for
731 * @test: callback used for comparisons between inodes
732 * @data: opaque data pointer to pass to @test
734 * ifind() searches for the inode specified by @hashval and @data in the inode
735 * cache. This is a generalized version of ifind_fast() for file systems where
736 * the inode number is not sufficient for unique identification of an inode.
738 * If the inode is in the cache, the inode is returned with an incremented
741 * Otherwise NULL is returned.
743 * Note, @test is called with the inode_lock held, so can't sleep.
745 static inline struct inode
*ifind(struct super_block
*sb
,
746 struct hlist_head
*head
, int (*test
)(struct inode
*, void *),
751 spin_lock(&inode_lock
);
752 inode
= find_inode(sb
, head
, test
, data
);
755 spin_unlock(&inode_lock
);
756 wait_on_inode(inode
);
759 spin_unlock(&inode_lock
);
764 * ifind_fast - internal function, you want ilookup() or iget().
765 * @sb: super block of file system to search
766 * @ino: inode number to search for
768 * ifind_fast() searches for the inode @ino in the inode cache. This is for
769 * file systems where the inode number is sufficient for unique identification
772 * If the inode is in the cache, the inode is returned with an incremented
775 * Otherwise NULL is returned.
777 static inline struct inode
*ifind_fast(struct super_block
*sb
,
778 struct hlist_head
*head
, unsigned long ino
)
782 spin_lock(&inode_lock
);
783 inode
= find_inode_fast(sb
, head
, ino
);
786 spin_unlock(&inode_lock
);
787 wait_on_inode(inode
);
790 spin_unlock(&inode_lock
);
795 * ilookup5 - search for an inode in the inode cache
796 * @sb: super block of file system to search
797 * @hashval: hash value (usually inode number) to search for
798 * @test: callback used for comparisons between inodes
799 * @data: opaque data pointer to pass to @test
801 * ilookup5() uses ifind() to search for the inode specified by @hashval and
802 * @data in the inode cache. This is a generalized version of ilookup() for
803 * file systems where the inode number is not sufficient for unique
804 * identification of an inode.
806 * If the inode is in the cache, the inode is returned with an incremented
809 * Otherwise NULL is returned.
811 * Note, @test is called with the inode_lock held, so can't sleep.
813 struct inode
*ilookup5(struct super_block
*sb
, unsigned long hashval
,
814 int (*test
)(struct inode
*, void *), void *data
)
816 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
818 return ifind(sb
, head
, test
, data
);
820 EXPORT_SYMBOL(ilookup5
);
823 * ilookup - search for an inode in the inode cache
824 * @sb: super block of file system to search
825 * @ino: inode number to search for
827 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
828 * This is for file systems where the inode number is sufficient for unique
829 * identification of an inode.
831 * If the inode is in the cache, the inode is returned with an incremented
834 * Otherwise NULL is returned.
836 struct inode
*ilookup(struct super_block
*sb
, unsigned long ino
)
838 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
840 return ifind_fast(sb
, head
, ino
);
842 EXPORT_SYMBOL(ilookup
);
845 * iget5_locked - obtain an inode from a mounted file system
846 * @sb: super block of file system
847 * @hashval: hash value (usually inode number) to get
848 * @test: callback used for comparisons between inodes
849 * @set: callback used to initialize a new struct inode
850 * @data: opaque data pointer to pass to @test and @set
852 * This is iget() without the read_inode() portion of get_new_inode().
854 * iget5_locked() uses ifind() to search for the inode specified by @hashval
855 * and @data in the inode cache and if present it is returned with an increased
856 * reference count. This is a generalized version of iget_locked() for file
857 * systems where the inode number is not sufficient for unique identification
860 * If the inode is not in cache, get_new_inode() is called to allocate a new
861 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
862 * file system gets to fill it in before unlocking it via unlock_new_inode().
864 * Note both @test and @set are called with the inode_lock held, so can't sleep.
866 struct inode
*iget5_locked(struct super_block
*sb
, unsigned long hashval
,
867 int (*test
)(struct inode
*, void *),
868 int (*set
)(struct inode
*, void *), void *data
)
870 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
873 inode
= ifind(sb
, head
, test
, data
);
877 * get_new_inode() will do the right thing, re-trying the search
878 * in case it had to block at any point.
880 return get_new_inode(sb
, head
, test
, set
, data
);
882 EXPORT_SYMBOL(iget5_locked
);
885 * iget_locked - obtain an inode from a mounted file system
886 * @sb: super block of file system
887 * @ino: inode number to get
889 * This is iget() without the read_inode() portion of get_new_inode_fast().
891 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
892 * the inode cache and if present it is returned with an increased reference
893 * count. This is for file systems where the inode number is sufficient for
894 * unique identification of an inode.
896 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
897 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
898 * The file system gets to fill it in before unlocking it via
899 * unlock_new_inode().
901 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
903 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
906 inode
= ifind_fast(sb
, head
, ino
);
910 * get_new_inode_fast() will do the right thing, re-trying the search
911 * in case it had to block at any point.
913 return get_new_inode_fast(sb
, head
, ino
);
915 EXPORT_SYMBOL(iget_locked
);
918 * __insert_inode_hash - hash an inode
919 * @inode: unhashed inode
920 * @hashval: unsigned long value used to locate this object in the
923 * Add an inode to the inode hash for this superblock. If the inode
924 * has no superblock it is added to a separate anonymous chain.
927 void __insert_inode_hash(struct inode
*inode
, unsigned long hashval
)
929 struct hlist_head
*head
= &anon_hash_chain
;
931 head
= inode_hashtable
+ hash(inode
->i_sb
, hashval
);
932 spin_lock(&inode_lock
);
933 hlist_add_head(&inode
->i_hash
, head
);
934 spin_unlock(&inode_lock
);
938 * remove_inode_hash - remove an inode from the hash
939 * @inode: inode to unhash
941 * Remove an inode from the superblock or anonymous hash.
944 void remove_inode_hash(struct inode
*inode
)
946 spin_lock(&inode_lock
);
947 hlist_del_init(&inode
->i_hash
);
948 spin_unlock(&inode_lock
);
952 * Tell the filesystem that this inode is no longer of any interest and should
953 * be completely destroyed.
955 * We leave the inode in the inode hash table until *after* the filesystem's
956 * ->delete_inode completes. This ensures that an iget (such as nfsd might
957 * instigate) will always find up-to-date information either in the hash or on
960 * I_FREEING is set so that no-one will take a new reference to the inode while
961 * it is being deleted.
963 void generic_delete_inode(struct inode
*inode
)
965 struct super_operations
*op
= inode
->i_sb
->s_op
;
967 list_del_init(&inode
->i_list
);
968 inode
->i_state
|=I_FREEING
;
969 inodes_stat
.nr_inodes
--;
970 spin_unlock(&inode_lock
);
972 if (inode
->i_data
.nrpages
)
973 truncate_inode_pages(&inode
->i_data
, 0);
975 security_inode_delete(inode
);
977 if (op
->delete_inode
) {
978 void (*delete)(struct inode
*) = op
->delete_inode
;
979 if (!is_bad_inode(inode
))
981 /* s_op->delete_inode internally recalls clear_inode() */
985 spin_lock(&inode_lock
);
986 hlist_del_init(&inode
->i_hash
);
987 spin_unlock(&inode_lock
);
988 wake_up_inode(inode
);
989 if (inode
->i_state
!= I_CLEAR
)
991 destroy_inode(inode
);
993 EXPORT_SYMBOL(generic_delete_inode
);
995 static void generic_forget_inode(struct inode
*inode
)
997 struct super_block
*sb
= inode
->i_sb
;
999 if (!hlist_unhashed(&inode
->i_hash
)) {
1000 if (!(inode
->i_state
& (I_DIRTY
|I_LOCK
))) {
1001 list_del(&inode
->i_list
);
1002 list_add(&inode
->i_list
, &inode_unused
);
1004 inodes_stat
.nr_unused
++;
1005 spin_unlock(&inode_lock
);
1006 if (!sb
|| (sb
->s_flags
& MS_ACTIVE
))
1008 write_inode_now(inode
, 1);
1009 spin_lock(&inode_lock
);
1010 inodes_stat
.nr_unused
--;
1011 hlist_del_init(&inode
->i_hash
);
1013 list_del_init(&inode
->i_list
);
1014 inode
->i_state
|=I_FREEING
;
1015 inodes_stat
.nr_inodes
--;
1016 spin_unlock(&inode_lock
);
1017 if (inode
->i_data
.nrpages
)
1018 truncate_inode_pages(&inode
->i_data
, 0);
1020 destroy_inode(inode
);
1024 * Normal UNIX filesystem behaviour: delete the
1025 * inode when the usage count drops to zero, and
1028 static void generic_drop_inode(struct inode
*inode
)
1030 if (!inode
->i_nlink
)
1031 generic_delete_inode(inode
);
1033 generic_forget_inode(inode
);
1037 * Called when we're dropping the last reference
1040 * Call the FS "drop()" function, defaulting to
1041 * the legacy UNIX filesystem behaviour..
1043 * NOTE! NOTE! NOTE! We're called with the inode lock
1044 * held, and the drop function is supposed to release
1047 static inline void iput_final(struct inode
*inode
)
1049 struct super_operations
*op
= inode
->i_sb
->s_op
;
1050 void (*drop
)(struct inode
*) = generic_drop_inode
;
1052 if (op
&& op
->drop_inode
)
1053 drop
= op
->drop_inode
;
1058 * iput - put an inode
1059 * @inode: inode to put
1061 * Puts an inode, dropping its usage count. If the inode use count hits
1062 * zero the inode is also then freed and may be destroyed.
1065 void iput(struct inode
*inode
)
1068 struct super_operations
*op
= inode
->i_sb
->s_op
;
1070 if (inode
->i_state
== I_CLEAR
)
1073 if (op
&& op
->put_inode
)
1074 op
->put_inode(inode
);
1076 if (atomic_dec_and_lock(&inode
->i_count
, &inode_lock
))
1082 * bmap - find a block number in a file
1083 * @inode: inode of file
1084 * @block: block to find
1086 * Returns the block number on the device holding the inode that
1087 * is the disk block number for the block of the file requested.
1088 * That is, asked for block 4 of inode 1 the function will return the
1089 * disk block relative to the disk start that holds that block of the
1093 sector_t
bmap(struct inode
* inode
, sector_t block
)
1096 if (inode
->i_mapping
->a_ops
->bmap
)
1097 res
= inode
->i_mapping
->a_ops
->bmap(inode
->i_mapping
, block
);
1102 * Return true if the filesystem which backs this inode considers the two
1103 * passed timespecs to be sufficiently different to warrant flushing the
1104 * altered time out to disk.
1106 static int inode_times_differ(struct inode
*inode
,
1107 struct timespec
*old
, struct timespec
*new)
1109 if (IS_ONE_SECOND(inode
))
1110 return old
->tv_sec
!= new->tv_sec
;
1111 return !timespec_equal(old
, new);
1115 * update_atime - update the access time
1116 * @inode: inode accessed
1118 * Update the accessed time on an inode and mark it for writeback.
1119 * This function automatically handles read only file systems and media,
1120 * as well as the "noatime" flag and inode specific "noatime" markers.
1123 void update_atime(struct inode
*inode
)
1125 struct timespec now
;
1127 if (IS_NOATIME(inode
))
1129 if (IS_NODIRATIME(inode
) && S_ISDIR(inode
->i_mode
))
1131 if (IS_RDONLY(inode
))
1134 now
= current_kernel_time();
1135 if (inode_times_differ(inode
, &inode
->i_atime
, &now
)) {
1136 inode
->i_atime
= now
;
1137 mark_inode_dirty_sync(inode
);
1139 if (!timespec_equal(&inode
->i_atime
, &now
))
1140 inode
->i_atime
= now
;
1145 * inode_update_time - update mtime and ctime time
1146 * @inode: inode accessed
1147 * @ctime_too: update ctime too
1149 * Update the mtime time on an inode and mark it for writeback.
1150 * When ctime_too is specified update the ctime too.
1153 void inode_update_time(struct inode
*inode
, int ctime_too
)
1155 struct timespec now
= current_kernel_time();
1158 if (inode_times_differ(inode
, &inode
->i_mtime
, &now
))
1160 inode
->i_mtime
= now
;
1163 if (inode_times_differ(inode
, &inode
->i_ctime
, &now
))
1165 inode
->i_ctime
= now
;
1168 mark_inode_dirty_sync(inode
);
1170 EXPORT_SYMBOL(inode_update_time
);
1172 int inode_needs_sync(struct inode
*inode
)
1176 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
1180 EXPORT_SYMBOL(inode_needs_sync
);
1183 * Quota functions that want to walk the inode lists..
1187 /* Functions back in dquot.c */
1188 void put_dquot_list(struct list_head
*);
1189 int remove_inode_dquot_ref(struct inode
*, int, struct list_head
*);
1191 void remove_dquot_ref(struct super_block
*sb
, int type
)
1193 struct inode
*inode
;
1194 struct list_head
*act_head
;
1195 LIST_HEAD(tofree_head
);
1198 return; /* nothing to do */
1199 spin_lock(&inode_lock
); /* This lock is for inodes code */
1200 /* We don't have to lock against quota code - test IS_QUOTAINIT is just for speedup... */
1202 list_for_each(act_head
, &inode_in_use
) {
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
, &inode_unused
) {
1208 inode
= list_entry(act_head
, struct inode
, i_list
);
1209 if (inode
->i_sb
== sb
&& IS_QUOTAINIT(inode
))
1210 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1212 list_for_each(act_head
, &sb
->s_dirty
) {
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 list_for_each(act_head
, &sb
->s_io
) {
1218 inode
= list_entry(act_head
, struct inode
, i_list
);
1219 if (IS_QUOTAINIT(inode
))
1220 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1222 spin_unlock(&inode_lock
);
1224 put_dquot_list(&tofree_head
);
1230 * Hashed waitqueues for wait_on_inode(). The table is pretty small - the
1231 * kernel doesn't lock many inodes at the same time.
1233 #define I_WAIT_TABLE_ORDER 3
1234 static struct i_wait_queue_head
{
1235 wait_queue_head_t wqh
;
1236 } ____cacheline_aligned_in_smp i_wait_queue_heads
[1<<I_WAIT_TABLE_ORDER
];
1239 * Return the address of the waitqueue_head to be used for this inode
1241 static wait_queue_head_t
*i_waitq_head(struct inode
*inode
)
1243 return &i_wait_queue_heads
[hash_ptr(inode
, I_WAIT_TABLE_ORDER
)].wqh
;
1246 void __wait_on_inode(struct inode
*inode
)
1248 DECLARE_WAITQUEUE(wait
, current
);
1249 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1251 add_wait_queue(wq
, &wait
);
1253 set_current_state(TASK_UNINTERRUPTIBLE
);
1254 if (inode
->i_state
& I_LOCK
) {
1258 remove_wait_queue(wq
, &wait
);
1259 __set_current_state(TASK_RUNNING
);
1263 * If we try to find an inode in the inode hash while it is being deleted, we
1264 * have to wait until the filesystem completes its deletion before reporting
1265 * that it isn't found. This is because iget will immediately call
1266 * ->read_inode, and we want to be sure that evidence of the deletion is found
1269 * This call might return early if an inode which shares the waitq is woken up.
1270 * This is most easily handled by the caller which will loop around again
1271 * looking for the inode.
1273 * This is called with inode_lock held.
1275 static void __wait_on_freeing_inode(struct inode
*inode
)
1277 DECLARE_WAITQUEUE(wait
, current
);
1278 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1280 add_wait_queue(wq
, &wait
);
1281 set_current_state(TASK_UNINTERRUPTIBLE
);
1282 spin_unlock(&inode_lock
);
1284 remove_wait_queue(wq
, &wait
);
1285 spin_lock(&inode_lock
);
1288 void wake_up_inode(struct inode
*inode
)
1290 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1293 * Prevent speculative execution through spin_unlock(&inode_lock);
1296 if (waitqueue_active(wq
))
1301 * Initialize the waitqueues and inode hash table.
1303 void __init
inode_init(unsigned long mempages
)
1305 struct hlist_head
*head
;
1306 unsigned long order
;
1307 unsigned int nr_hash
;
1310 for (i
= 0; i
< ARRAY_SIZE(i_wait_queue_heads
); i
++)
1311 init_waitqueue_head(&i_wait_queue_heads
[i
].wqh
);
1313 mempages
>>= (14 - PAGE_SHIFT
);
1314 mempages
*= sizeof(struct hlist_head
);
1315 for (order
= 0; ((1UL << order
) << PAGE_SHIFT
) < mempages
; order
++)
1321 nr_hash
= (1UL << order
) * PAGE_SIZE
/
1322 sizeof(struct hlist_head
);
1323 i_hash_mask
= (nr_hash
- 1);
1327 while ((tmp
>>= 1UL) != 0UL)
1330 inode_hashtable
= (struct hlist_head
*)
1331 __get_free_pages(GFP_ATOMIC
, order
);
1332 } while (inode_hashtable
== NULL
&& --order
>= 0);
1334 printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1335 nr_hash
, order
, (PAGE_SIZE
<< order
));
1337 if (!inode_hashtable
)
1338 panic("Failed to allocate inode hash table\n");
1340 head
= inode_hashtable
;
1343 INIT_HLIST_HEAD(head
);
1348 /* inode slab cache */
1349 inode_cachep
= kmem_cache_create("inode_cache", sizeof(struct inode
),
1350 0, SLAB_HWCACHE_ALIGN
, init_once
,
1353 panic("cannot create inode slab cache");
1355 set_shrinker(DEFAULT_SEEKS
, shrink_icache_memory
);
1358 void init_special_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
1360 inode
->i_mode
= mode
;
1361 if (S_ISCHR(mode
)) {
1362 inode
->i_fop
= &def_chr_fops
;
1363 inode
->i_rdev
= to_kdev_t(rdev
);
1364 } else if (S_ISBLK(mode
)) {
1365 inode
->i_fop
= &def_blk_fops
;
1366 inode
->i_rdev
= to_kdev_t(rdev
);
1367 } else if (S_ISFIFO(mode
))
1368 inode
->i_fop
= &def_fifo_fops
;
1369 else if (S_ISSOCK(mode
))
1370 inode
->i_fop
= &bad_sock_fops
;
1372 printk(KERN_DEBUG
"init_special_inode: bogus i_mode (%o)\n",