4 * (C) 1997 Linus Torvalds
9 #include <linux/dcache.h>
10 #include <linux/init.h>
11 #include <linux/quotaops.h>
12 #include <linux/slab.h>
13 #include <linux/writeback.h>
14 #include <linux/module.h>
15 #include <linux/backing-dev.h>
16 #include <linux/wait.h>
17 #include <linux/hash.h>
18 #include <linux/swap.h>
19 #include <linux/security.h>
20 #include <linux/pagemap.h>
21 #include <linux/cdev.h>
22 #include <linux/bootmem.h>
23 #include <linux/inotify.h>
24 #include <linux/mount.h>
25 #include <linux/async.h>
28 * This is needed for the following functions:
30 * - invalidate_inode_buffers
33 * FIXME: remove all knowledge of the buffer layer from this file
35 #include <linux/buffer_head.h>
38 * New inode.c implementation.
40 * This implementation has the basic premise of trying
41 * to be extremely low-overhead and SMP-safe, yet be
42 * simple enough to be "obviously correct".
47 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
49 /* #define INODE_PARANOIA 1 */
50 /* #define INODE_DEBUG 1 */
53 * Inode lookup is no longer as critical as it used to be:
54 * most of the lookups are going to be through the dcache.
56 #define I_HASHBITS i_hash_shift
57 #define I_HASHMASK i_hash_mask
59 static unsigned int i_hash_mask __read_mostly
;
60 static unsigned int i_hash_shift __read_mostly
;
63 * Each inode can be on two separate lists. One is
64 * the hash list of the inode, used for lookups. The
65 * other linked list is the "type" list:
66 * "in_use" - valid inode, i_count > 0, i_nlink > 0
67 * "dirty" - as "in_use" but also dirty
68 * "unused" - valid inode, i_count = 0
70 * A "dirty" list is maintained for each super block,
71 * allowing for low-overhead inode sync() operations.
74 LIST_HEAD(inode_in_use
);
75 LIST_HEAD(inode_unused
);
76 static struct hlist_head
*inode_hashtable __read_mostly
;
79 * A simple spinlock to protect the list manipulations.
81 * NOTE! You also have to own the lock if you change
82 * the i_state of an inode while it is in use..
84 DEFINE_SPINLOCK(inode_lock
);
87 * iprune_mutex provides exclusion between the kswapd or try_to_free_pages
88 * icache shrinking path, and the umount path. Without this exclusion,
89 * by the time prune_icache calls iput for the inode whose pages it has
90 * been invalidating, or by the time it calls clear_inode & destroy_inode
91 * from its final dispose_list, the struct super_block they refer to
92 * (for inode->i_sb->s_op) may already have been freed and reused.
94 static DEFINE_MUTEX(iprune_mutex
);
97 * Statistics gathering..
99 struct inodes_stat_t inodes_stat
;
101 static struct kmem_cache
* inode_cachep __read_mostly
;
103 static void wake_up_inode(struct inode
*inode
)
106 * Prevent speculative execution through spin_unlock(&inode_lock);
109 wake_up_bit(&inode
->i_state
, __I_LOCK
);
113 * inode_init_always - perform inode structure intialisation
114 * @sb: superblock inode belongs to
115 * @inode: inode to initialise
117 * These are initializations that need to be done on every inode
118 * allocation as the fields are not initialised by slab allocation.
120 struct inode
*inode_init_always(struct super_block
*sb
, struct inode
*inode
)
122 static const struct address_space_operations empty_aops
;
123 static struct inode_operations empty_iops
;
124 static const struct file_operations empty_fops
;
126 struct address_space
* const mapping
= &inode
->i_data
;
129 inode
->i_blkbits
= sb
->s_blocksize_bits
;
131 atomic_set(&inode
->i_count
, 1);
132 inode
->i_op
= &empty_iops
;
133 inode
->i_fop
= &empty_fops
;
137 atomic_set(&inode
->i_writecount
, 0);
141 inode
->i_generation
= 0;
143 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
145 inode
->i_pipe
= NULL
;
146 inode
->i_bdev
= NULL
;
147 inode
->i_cdev
= NULL
;
149 inode
->dirtied_when
= 0;
150 if (security_inode_alloc(inode
)) {
151 if (inode
->i_sb
->s_op
->destroy_inode
)
152 inode
->i_sb
->s_op
->destroy_inode(inode
);
154 kmem_cache_free(inode_cachep
, (inode
));
158 spin_lock_init(&inode
->i_lock
);
159 lockdep_set_class(&inode
->i_lock
, &sb
->s_type
->i_lock_key
);
161 mutex_init(&inode
->i_mutex
);
162 lockdep_set_class(&inode
->i_mutex
, &sb
->s_type
->i_mutex_key
);
164 init_rwsem(&inode
->i_alloc_sem
);
165 lockdep_set_class(&inode
->i_alloc_sem
, &sb
->s_type
->i_alloc_sem_key
);
167 mapping
->a_ops
= &empty_aops
;
168 mapping
->host
= inode
;
170 mapping_set_gfp_mask(mapping
, GFP_HIGHUSER_MOVABLE
);
171 mapping
->assoc_mapping
= NULL
;
172 mapping
->backing_dev_info
= &default_backing_dev_info
;
173 mapping
->writeback_index
= 0;
176 * If the block_device provides a backing_dev_info for client
177 * inodes then use that. Otherwise the inode share the bdev's
181 struct backing_dev_info
*bdi
;
183 bdi
= sb
->s_bdev
->bd_inode_backing_dev_info
;
185 bdi
= sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
186 mapping
->backing_dev_info
= bdi
;
188 inode
->i_private
= NULL
;
189 inode
->i_mapping
= mapping
;
193 EXPORT_SYMBOL(inode_init_always
);
195 static struct inode
*alloc_inode(struct super_block
*sb
)
199 if (sb
->s_op
->alloc_inode
)
200 inode
= sb
->s_op
->alloc_inode(sb
);
202 inode
= kmem_cache_alloc(inode_cachep
, GFP_KERNEL
);
205 return inode_init_always(sb
, inode
);
209 void destroy_inode(struct inode
*inode
)
211 BUG_ON(inode_has_buffers(inode
));
212 security_inode_free(inode
);
213 if (inode
->i_sb
->s_op
->destroy_inode
)
214 inode
->i_sb
->s_op
->destroy_inode(inode
);
216 kmem_cache_free(inode_cachep
, (inode
));
218 EXPORT_SYMBOL(destroy_inode
);
222 * These are initializations that only need to be done
223 * once, because the fields are idempotent across use
224 * of the inode, so let the slab aware of that.
226 void inode_init_once(struct inode
*inode
)
228 memset(inode
, 0, sizeof(*inode
));
229 INIT_HLIST_NODE(&inode
->i_hash
);
230 INIT_LIST_HEAD(&inode
->i_dentry
);
231 INIT_LIST_HEAD(&inode
->i_devices
);
232 INIT_RADIX_TREE(&inode
->i_data
.page_tree
, GFP_ATOMIC
);
233 spin_lock_init(&inode
->i_data
.tree_lock
);
234 spin_lock_init(&inode
->i_data
.i_mmap_lock
);
235 INIT_LIST_HEAD(&inode
->i_data
.private_list
);
236 spin_lock_init(&inode
->i_data
.private_lock
);
237 INIT_RAW_PRIO_TREE_ROOT(&inode
->i_data
.i_mmap
);
238 INIT_LIST_HEAD(&inode
->i_data
.i_mmap_nonlinear
);
239 i_size_ordered_init(inode
);
240 #ifdef CONFIG_INOTIFY
241 INIT_LIST_HEAD(&inode
->inotify_watches
);
242 mutex_init(&inode
->inotify_mutex
);
246 EXPORT_SYMBOL(inode_init_once
);
248 static void init_once(void *foo
)
250 struct inode
* inode
= (struct inode
*) foo
;
252 inode_init_once(inode
);
256 * inode_lock must be held
258 void __iget(struct inode
* inode
)
260 if (atomic_read(&inode
->i_count
)) {
261 atomic_inc(&inode
->i_count
);
264 atomic_inc(&inode
->i_count
);
265 if (!(inode
->i_state
& (I_DIRTY
|I_SYNC
)))
266 list_move(&inode
->i_list
, &inode_in_use
);
267 inodes_stat
.nr_unused
--;
271 * clear_inode - clear an inode
272 * @inode: inode to clear
274 * This is called by the filesystem to tell us
275 * that the inode is no longer useful. We just
276 * terminate it with extreme prejudice.
278 void clear_inode(struct inode
*inode
)
281 invalidate_inode_buffers(inode
);
283 BUG_ON(inode
->i_data
.nrpages
);
284 BUG_ON(!(inode
->i_state
& I_FREEING
));
285 BUG_ON(inode
->i_state
& I_CLEAR
);
286 inode_sync_wait(inode
);
288 if (inode
->i_sb
->s_op
->clear_inode
)
289 inode
->i_sb
->s_op
->clear_inode(inode
);
290 if (S_ISBLK(inode
->i_mode
) && inode
->i_bdev
)
292 if (S_ISCHR(inode
->i_mode
) && inode
->i_cdev
)
294 inode
->i_state
= I_CLEAR
;
297 EXPORT_SYMBOL(clear_inode
);
300 * dispose_list - dispose of the contents of a local list
301 * @head: the head of the list to free
303 * Dispose-list gets a local list with local inodes in it, so it doesn't
304 * need to worry about list corruption and SMP locks.
306 static void dispose_list(struct list_head
*head
)
310 while (!list_empty(head
)) {
313 inode
= list_first_entry(head
, struct inode
, i_list
);
314 list_del(&inode
->i_list
);
316 if (inode
->i_data
.nrpages
)
317 truncate_inode_pages(&inode
->i_data
, 0);
320 spin_lock(&inode_lock
);
321 hlist_del_init(&inode
->i_hash
);
322 list_del_init(&inode
->i_sb_list
);
323 spin_unlock(&inode_lock
);
325 wake_up_inode(inode
);
326 destroy_inode(inode
);
329 spin_lock(&inode_lock
);
330 inodes_stat
.nr_inodes
-= nr_disposed
;
331 spin_unlock(&inode_lock
);
335 * Invalidate all inodes for a device.
337 static int invalidate_list(struct list_head
*head
, struct list_head
*dispose
)
339 struct list_head
*next
;
340 int busy
= 0, count
= 0;
344 struct list_head
* tmp
= next
;
345 struct inode
* inode
;
348 * We can reschedule here without worrying about the list's
349 * consistency because the per-sb list of inodes must not
350 * change during umount anymore, and because iprune_mutex keeps
351 * shrink_icache_memory() away.
353 cond_resched_lock(&inode_lock
);
358 inode
= list_entry(tmp
, struct inode
, i_sb_list
);
359 invalidate_inode_buffers(inode
);
360 if (!atomic_read(&inode
->i_count
)) {
361 list_move(&inode
->i_list
, dispose
);
362 inode
->i_state
|= I_FREEING
;
368 /* only unused inodes may be cached with i_count zero */
369 inodes_stat
.nr_unused
-= count
;
374 * invalidate_inodes - discard the inodes on a device
377 * Discard all of the inodes for a given superblock. If the discard
378 * fails because there are busy inodes then a non zero value is returned.
379 * If the discard is successful all the inodes have been discarded.
381 int invalidate_inodes(struct super_block
* sb
)
384 LIST_HEAD(throw_away
);
386 mutex_lock(&iprune_mutex
);
387 spin_lock(&inode_lock
);
388 inotify_unmount_inodes(&sb
->s_inodes
);
389 busy
= invalidate_list(&sb
->s_inodes
, &throw_away
);
390 spin_unlock(&inode_lock
);
392 dispose_list(&throw_away
);
393 mutex_unlock(&iprune_mutex
);
398 EXPORT_SYMBOL(invalidate_inodes
);
400 static int can_unuse(struct inode
*inode
)
404 if (inode_has_buffers(inode
))
406 if (atomic_read(&inode
->i_count
))
408 if (inode
->i_data
.nrpages
)
414 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
415 * a temporary list and then are freed outside inode_lock by dispose_list().
417 * Any inodes which are pinned purely because of attached pagecache have their
418 * pagecache removed. We expect the final iput() on that inode to add it to
419 * the front of the inode_unused list. So look for it there and if the
420 * inode is still freeable, proceed. The right inode is found 99.9% of the
421 * time in testing on a 4-way.
423 * If the inode has metadata buffers attached to mapping->private_list then
424 * try to remove them.
426 static void prune_icache(int nr_to_scan
)
431 unsigned long reap
= 0;
433 mutex_lock(&iprune_mutex
);
434 spin_lock(&inode_lock
);
435 for (nr_scanned
= 0; nr_scanned
< nr_to_scan
; nr_scanned
++) {
438 if (list_empty(&inode_unused
))
441 inode
= list_entry(inode_unused
.prev
, struct inode
, i_list
);
443 if (inode
->i_state
|| atomic_read(&inode
->i_count
)) {
444 list_move(&inode
->i_list
, &inode_unused
);
447 if (inode_has_buffers(inode
) || inode
->i_data
.nrpages
) {
449 spin_unlock(&inode_lock
);
450 if (remove_inode_buffers(inode
))
451 reap
+= invalidate_mapping_pages(&inode
->i_data
,
454 spin_lock(&inode_lock
);
456 if (inode
!= list_entry(inode_unused
.next
,
457 struct inode
, i_list
))
458 continue; /* wrong inode or list_empty */
459 if (!can_unuse(inode
))
462 list_move(&inode
->i_list
, &freeable
);
463 inode
->i_state
|= I_FREEING
;
466 inodes_stat
.nr_unused
-= nr_pruned
;
467 if (current_is_kswapd())
468 __count_vm_events(KSWAPD_INODESTEAL
, reap
);
470 __count_vm_events(PGINODESTEAL
, reap
);
471 spin_unlock(&inode_lock
);
473 dispose_list(&freeable
);
474 mutex_unlock(&iprune_mutex
);
478 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
479 * "unused" means that no dentries are referring to the inodes: the files are
480 * not open and the dcache references to those inodes have already been
483 * This function is passed the number of inodes to scan, and it returns the
484 * total number of remaining possibly-reclaimable inodes.
486 static int shrink_icache_memory(int nr
, gfp_t gfp_mask
)
490 * Nasty deadlock avoidance. We may hold various FS locks,
491 * and we don't want to recurse into the FS that called us
492 * in clear_inode() and friends..
494 if (!(gfp_mask
& __GFP_FS
))
498 return (inodes_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
501 static struct shrinker icache_shrinker
= {
502 .shrink
= shrink_icache_memory
,
503 .seeks
= DEFAULT_SEEKS
,
506 static void __wait_on_freeing_inode(struct inode
*inode
);
508 * Called with the inode lock held.
509 * NOTE: we are not increasing the inode-refcount, you must call __iget()
510 * by hand after calling find_inode now! This simplifies iunique and won't
511 * add any additional branch in the common code.
513 static struct inode
* find_inode(struct super_block
* sb
, struct hlist_head
*head
, int (*test
)(struct inode
*, void *), void *data
)
515 struct hlist_node
*node
;
516 struct inode
* inode
= NULL
;
519 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
520 if (inode
->i_sb
!= sb
)
522 if (!test(inode
, data
))
524 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
)) {
525 __wait_on_freeing_inode(inode
);
530 return node
? inode
: NULL
;
534 * find_inode_fast is the fast path version of find_inode, see the comment at
535 * iget_locked for details.
537 static struct inode
* find_inode_fast(struct super_block
* sb
, struct hlist_head
*head
, unsigned long ino
)
539 struct hlist_node
*node
;
540 struct inode
* inode
= NULL
;
543 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
544 if (inode
->i_ino
!= ino
)
546 if (inode
->i_sb
!= sb
)
548 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
)) {
549 __wait_on_freeing_inode(inode
);
554 return node
? inode
: NULL
;
557 static unsigned long hash(struct super_block
*sb
, unsigned long hashval
)
561 tmp
= (hashval
* (unsigned long)sb
) ^ (GOLDEN_RATIO_PRIME
+ hashval
) /
563 tmp
= tmp
^ ((tmp
^ GOLDEN_RATIO_PRIME
) >> I_HASHBITS
);
564 return tmp
& I_HASHMASK
;
568 __inode_add_to_lists(struct super_block
*sb
, struct hlist_head
*head
,
571 inodes_stat
.nr_inodes
++;
572 list_add(&inode
->i_list
, &inode_in_use
);
573 list_add(&inode
->i_sb_list
, &sb
->s_inodes
);
575 hlist_add_head(&inode
->i_hash
, head
);
579 * inode_add_to_lists - add a new inode to relevant lists
580 * @sb: superblock inode belongs to
581 * @inode: inode to mark in use
583 * When an inode is allocated it needs to be accounted for, added to the in use
584 * list, the owning superblock and the inode hash. This needs to be done under
585 * the inode_lock, so export a function to do this rather than the inode lock
586 * itself. We calculate the hash list to add to here so it is all internal
587 * which requires the caller to have already set up the inode number in the
590 void inode_add_to_lists(struct super_block
*sb
, struct inode
*inode
)
592 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, inode
->i_ino
);
594 spin_lock(&inode_lock
);
595 __inode_add_to_lists(sb
, head
, inode
);
596 spin_unlock(&inode_lock
);
598 EXPORT_SYMBOL_GPL(inode_add_to_lists
);
601 * new_inode - obtain an inode
604 * Allocates a new inode for given superblock. The default gfp_mask
605 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
606 * If HIGHMEM pages are unsuitable or it is known that pages allocated
607 * for the page cache are not reclaimable or migratable,
608 * mapping_set_gfp_mask() must be called with suitable flags on the
609 * newly created inode's mapping
612 struct inode
*new_inode(struct super_block
*sb
)
615 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
616 * error if st_ino won't fit in target struct field. Use 32bit counter
617 * here to attempt to avoid that.
619 static unsigned int last_ino
;
620 struct inode
* inode
;
622 spin_lock_prefetch(&inode_lock
);
624 inode
= alloc_inode(sb
);
626 spin_lock(&inode_lock
);
627 __inode_add_to_lists(sb
, NULL
, inode
);
628 inode
->i_ino
= ++last_ino
;
630 spin_unlock(&inode_lock
);
635 EXPORT_SYMBOL(new_inode
);
637 void unlock_new_inode(struct inode
*inode
)
639 #ifdef CONFIG_DEBUG_LOCK_ALLOC
640 if (inode
->i_mode
& S_IFDIR
) {
641 struct file_system_type
*type
= inode
->i_sb
->s_type
;
644 * ensure nobody is actually holding i_mutex
646 mutex_destroy(&inode
->i_mutex
);
647 mutex_init(&inode
->i_mutex
);
648 lockdep_set_class(&inode
->i_mutex
, &type
->i_mutex_dir_key
);
652 * This is special! We do not need the spinlock
653 * when clearing I_LOCK, because we're guaranteed
654 * that nobody else tries to do anything about the
655 * state of the inode when it is locked, as we
656 * just created it (so there can be no old holders
657 * that haven't tested I_LOCK).
659 inode
->i_state
&= ~(I_LOCK
|I_NEW
);
660 wake_up_inode(inode
);
663 EXPORT_SYMBOL(unlock_new_inode
);
666 * This is called without the inode lock held.. Be careful.
668 * We no longer cache the sb_flags in i_flags - see fs.h
669 * -- rmk@arm.uk.linux.org
671 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
)
673 struct inode
* inode
;
675 inode
= alloc_inode(sb
);
679 spin_lock(&inode_lock
);
680 /* We released the lock, so.. */
681 old
= find_inode(sb
, head
, test
, data
);
683 if (set(inode
, data
))
686 __inode_add_to_lists(sb
, head
, inode
);
687 inode
->i_state
= I_LOCK
|I_NEW
;
688 spin_unlock(&inode_lock
);
690 /* Return the locked inode with I_NEW set, the
691 * caller is responsible for filling in the contents
697 * Uhhuh, somebody else created the same inode under
698 * us. Use the old inode instead of the one we just
702 spin_unlock(&inode_lock
);
703 destroy_inode(inode
);
705 wait_on_inode(inode
);
710 spin_unlock(&inode_lock
);
711 destroy_inode(inode
);
716 * get_new_inode_fast is the fast path version of get_new_inode, see the
717 * comment at iget_locked for details.
719 static struct inode
* get_new_inode_fast(struct super_block
*sb
, struct hlist_head
*head
, unsigned long ino
)
721 struct inode
* inode
;
723 inode
= alloc_inode(sb
);
727 spin_lock(&inode_lock
);
728 /* We released the lock, so.. */
729 old
= find_inode_fast(sb
, head
, ino
);
732 __inode_add_to_lists(sb
, head
, inode
);
733 inode
->i_state
= I_LOCK
|I_NEW
;
734 spin_unlock(&inode_lock
);
736 /* Return the locked inode with I_NEW set, the
737 * caller is responsible for filling in the contents
743 * Uhhuh, somebody else created the same inode under
744 * us. Use the old inode instead of the one we just
748 spin_unlock(&inode_lock
);
749 destroy_inode(inode
);
751 wait_on_inode(inode
);
757 * iunique - get a unique inode number
759 * @max_reserved: highest reserved inode number
761 * Obtain an inode number that is unique on the system for a given
762 * superblock. This is used by file systems that have no natural
763 * permanent inode numbering system. An inode number is returned that
764 * is higher than the reserved limit but unique.
767 * With a large number of inodes live on the file system this function
768 * currently becomes quite slow.
770 ino_t
iunique(struct super_block
*sb
, ino_t max_reserved
)
773 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
774 * error if st_ino won't fit in target struct field. Use 32bit counter
775 * here to attempt to avoid that.
777 static unsigned int counter
;
779 struct hlist_head
*head
;
782 spin_lock(&inode_lock
);
784 if (counter
<= max_reserved
)
785 counter
= max_reserved
+ 1;
787 head
= inode_hashtable
+ hash(sb
, res
);
788 inode
= find_inode_fast(sb
, head
, res
);
789 } while (inode
!= NULL
);
790 spin_unlock(&inode_lock
);
794 EXPORT_SYMBOL(iunique
);
796 struct inode
*igrab(struct inode
*inode
)
798 spin_lock(&inode_lock
);
799 if (!(inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
)))
803 * Handle the case where s_op->clear_inode is not been
804 * called yet, and somebody is calling igrab
805 * while the inode is getting freed.
808 spin_unlock(&inode_lock
);
812 EXPORT_SYMBOL(igrab
);
815 * ifind - internal function, you want ilookup5() or iget5().
816 * @sb: super block of file system to search
817 * @head: the head of the list to search
818 * @test: callback used for comparisons between inodes
819 * @data: opaque data pointer to pass to @test
820 * @wait: if true wait for the inode to be unlocked, if false do not
822 * ifind() searches for the inode specified by @data in the inode
823 * cache. This is a generalized version of ifind_fast() for file systems where
824 * the inode number is not sufficient for unique identification of an inode.
826 * If the inode is in the cache, the inode is returned with an incremented
829 * Otherwise NULL is returned.
831 * Note, @test is called with the inode_lock held, so can't sleep.
833 static struct inode
*ifind(struct super_block
*sb
,
834 struct hlist_head
*head
, int (*test
)(struct inode
*, void *),
835 void *data
, const int wait
)
839 spin_lock(&inode_lock
);
840 inode
= find_inode(sb
, head
, test
, data
);
843 spin_unlock(&inode_lock
);
845 wait_on_inode(inode
);
848 spin_unlock(&inode_lock
);
853 * ifind_fast - internal function, you want ilookup() or iget().
854 * @sb: super block of file system to search
855 * @head: head of the list to search
856 * @ino: inode number to search for
858 * ifind_fast() searches for the inode @ino in the inode cache. This is for
859 * file systems where the inode number is sufficient for unique identification
862 * If the inode is in the cache, the inode is returned with an incremented
865 * Otherwise NULL is returned.
867 static struct inode
*ifind_fast(struct super_block
*sb
,
868 struct hlist_head
*head
, unsigned long ino
)
872 spin_lock(&inode_lock
);
873 inode
= find_inode_fast(sb
, head
, ino
);
876 spin_unlock(&inode_lock
);
877 wait_on_inode(inode
);
880 spin_unlock(&inode_lock
);
885 * ilookup5_nowait - search for an inode in the inode cache
886 * @sb: super block of file system to search
887 * @hashval: hash value (usually inode number) to search for
888 * @test: callback used for comparisons between inodes
889 * @data: opaque data pointer to pass to @test
891 * ilookup5() uses ifind() to search for the inode specified by @hashval and
892 * @data in the inode cache. This is a generalized version of ilookup() for
893 * file systems where the inode number is not sufficient for unique
894 * identification of an inode.
896 * If the inode is in the cache, the inode is returned with an incremented
897 * reference count. Note, the inode lock is not waited upon so you have to be
898 * very careful what you do with the returned inode. You probably should be
899 * using ilookup5() instead.
901 * Otherwise NULL is returned.
903 * Note, @test is called with the inode_lock held, so can't sleep.
905 struct inode
*ilookup5_nowait(struct super_block
*sb
, unsigned long hashval
,
906 int (*test
)(struct inode
*, void *), void *data
)
908 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
910 return ifind(sb
, head
, test
, data
, 0);
913 EXPORT_SYMBOL(ilookup5_nowait
);
916 * ilookup5 - search for an inode in the inode cache
917 * @sb: super block of file system to search
918 * @hashval: hash value (usually inode number) to search for
919 * @test: callback used for comparisons between inodes
920 * @data: opaque data pointer to pass to @test
922 * ilookup5() uses ifind() to search for the inode specified by @hashval and
923 * @data in the inode cache. This is a generalized version of ilookup() for
924 * file systems where the inode number is not sufficient for unique
925 * identification of an inode.
927 * If the inode is in the cache, the inode lock is waited upon and the inode is
928 * returned with an incremented reference count.
930 * Otherwise NULL is returned.
932 * Note, @test is called with the inode_lock held, so can't sleep.
934 struct inode
*ilookup5(struct super_block
*sb
, unsigned long hashval
,
935 int (*test
)(struct inode
*, void *), void *data
)
937 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
939 return ifind(sb
, head
, test
, data
, 1);
942 EXPORT_SYMBOL(ilookup5
);
945 * ilookup - search for an inode in the inode cache
946 * @sb: super block of file system to search
947 * @ino: inode number to search for
949 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
950 * This is for file systems where the inode number is sufficient for unique
951 * identification of an inode.
953 * If the inode is in the cache, the inode is returned with an incremented
956 * Otherwise NULL is returned.
958 struct inode
*ilookup(struct super_block
*sb
, unsigned long ino
)
960 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
962 return ifind_fast(sb
, head
, ino
);
965 EXPORT_SYMBOL(ilookup
);
968 * iget5_locked - obtain an inode from a mounted file system
969 * @sb: super block of file system
970 * @hashval: hash value (usually inode number) to get
971 * @test: callback used for comparisons between inodes
972 * @set: callback used to initialize a new struct inode
973 * @data: opaque data pointer to pass to @test and @set
975 * iget5_locked() uses ifind() to search for the inode specified by @hashval
976 * and @data in the inode cache and if present it is returned with an increased
977 * reference count. This is a generalized version of iget_locked() for file
978 * systems where the inode number is not sufficient for unique identification
981 * If the inode is not in cache, get_new_inode() is called to allocate a new
982 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
983 * file system gets to fill it in before unlocking it via unlock_new_inode().
985 * Note both @test and @set are called with the inode_lock held, so can't sleep.
987 struct inode
*iget5_locked(struct super_block
*sb
, unsigned long hashval
,
988 int (*test
)(struct inode
*, void *),
989 int (*set
)(struct inode
*, void *), void *data
)
991 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
994 inode
= ifind(sb
, head
, test
, data
, 1);
998 * get_new_inode() will do the right thing, re-trying the search
999 * in case it had to block at any point.
1001 return get_new_inode(sb
, head
, test
, set
, data
);
1004 EXPORT_SYMBOL(iget5_locked
);
1007 * iget_locked - obtain an inode from a mounted file system
1008 * @sb: super block of file system
1009 * @ino: inode number to get
1011 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1012 * the inode cache and if present it is returned with an increased reference
1013 * count. This is for file systems where the inode number is sufficient for
1014 * unique identification of an inode.
1016 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1017 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1018 * The file system gets to fill it in before unlocking it via
1019 * unlock_new_inode().
1021 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
1023 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1024 struct inode
*inode
;
1026 inode
= ifind_fast(sb
, head
, ino
);
1030 * get_new_inode_fast() will do the right thing, re-trying the search
1031 * in case it had to block at any point.
1033 return get_new_inode_fast(sb
, head
, ino
);
1036 EXPORT_SYMBOL(iget_locked
);
1038 int insert_inode_locked(struct inode
*inode
)
1040 struct super_block
*sb
= inode
->i_sb
;
1041 ino_t ino
= inode
->i_ino
;
1042 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1045 inode
->i_state
|= I_LOCK
|I_NEW
;
1047 spin_lock(&inode_lock
);
1048 old
= find_inode_fast(sb
, head
, ino
);
1050 hlist_add_head(&inode
->i_hash
, head
);
1051 spin_unlock(&inode_lock
);
1055 spin_unlock(&inode_lock
);
1057 if (unlikely(!hlist_unhashed(&old
->i_hash
))) {
1065 EXPORT_SYMBOL(insert_inode_locked
);
1067 int insert_inode_locked4(struct inode
*inode
, unsigned long hashval
,
1068 int (*test
)(struct inode
*, void *), void *data
)
1070 struct super_block
*sb
= inode
->i_sb
;
1071 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1074 inode
->i_state
|= I_LOCK
|I_NEW
;
1077 spin_lock(&inode_lock
);
1078 old
= find_inode(sb
, head
, test
, data
);
1080 hlist_add_head(&inode
->i_hash
, head
);
1081 spin_unlock(&inode_lock
);
1085 spin_unlock(&inode_lock
);
1087 if (unlikely(!hlist_unhashed(&old
->i_hash
))) {
1095 EXPORT_SYMBOL(insert_inode_locked4
);
1098 * __insert_inode_hash - hash an inode
1099 * @inode: unhashed inode
1100 * @hashval: unsigned long value used to locate this object in the
1103 * Add an inode to the inode hash for this superblock.
1105 void __insert_inode_hash(struct inode
*inode
, unsigned long hashval
)
1107 struct hlist_head
*head
= inode_hashtable
+ hash(inode
->i_sb
, hashval
);
1108 spin_lock(&inode_lock
);
1109 hlist_add_head(&inode
->i_hash
, head
);
1110 spin_unlock(&inode_lock
);
1113 EXPORT_SYMBOL(__insert_inode_hash
);
1116 * remove_inode_hash - remove an inode from the hash
1117 * @inode: inode to unhash
1119 * Remove an inode from the superblock.
1121 void remove_inode_hash(struct inode
*inode
)
1123 spin_lock(&inode_lock
);
1124 hlist_del_init(&inode
->i_hash
);
1125 spin_unlock(&inode_lock
);
1128 EXPORT_SYMBOL(remove_inode_hash
);
1131 * Tell the filesystem that this inode is no longer of any interest and should
1132 * be completely destroyed.
1134 * We leave the inode in the inode hash table until *after* the filesystem's
1135 * ->delete_inode completes. This ensures that an iget (such as nfsd might
1136 * instigate) will always find up-to-date information either in the hash or on
1139 * I_FREEING is set so that no-one will take a new reference to the inode while
1140 * it is being deleted.
1142 static void generic_delete_inode_async(void *data
, async_cookie_t cookie
)
1144 struct inode
*inode
= data
;
1145 const struct super_operations
*op
= inode
->i_sb
->s_op
;
1147 security_inode_delete(inode
);
1149 if (op
->delete_inode
) {
1150 void (*delete)(struct inode
*) = op
->delete_inode
;
1151 if (!is_bad_inode(inode
))
1153 /* Filesystems implementing their own
1154 * s_op->delete_inode are required to call
1155 * truncate_inode_pages and clear_inode()
1159 truncate_inode_pages(&inode
->i_data
, 0);
1162 spin_lock(&inode_lock
);
1163 hlist_del_init(&inode
->i_hash
);
1164 spin_unlock(&inode_lock
);
1165 wake_up_inode(inode
);
1166 BUG_ON(inode
->i_state
!= I_CLEAR
);
1167 destroy_inode(inode
);
1170 void generic_delete_inode(struct inode
*inode
)
1172 list_del_init(&inode
->i_list
);
1173 list_del_init(&inode
->i_sb_list
);
1174 inode
->i_state
|= I_FREEING
;
1175 inodes_stat
.nr_inodes
--;
1176 spin_unlock(&inode_lock
);
1177 async_schedule_special(generic_delete_inode_async
, inode
, &inode
->i_sb
->s_async_list
);
1180 EXPORT_SYMBOL(generic_delete_inode
);
1182 static void generic_forget_inode(struct inode
*inode
)
1184 struct super_block
*sb
= inode
->i_sb
;
1186 if (!hlist_unhashed(&inode
->i_hash
)) {
1187 if (!(inode
->i_state
& (I_DIRTY
|I_SYNC
)))
1188 list_move(&inode
->i_list
, &inode_unused
);
1189 inodes_stat
.nr_unused
++;
1190 if (sb
->s_flags
& MS_ACTIVE
) {
1191 spin_unlock(&inode_lock
);
1194 inode
->i_state
|= I_WILL_FREE
;
1195 spin_unlock(&inode_lock
);
1196 write_inode_now(inode
, 1);
1197 spin_lock(&inode_lock
);
1198 inode
->i_state
&= ~I_WILL_FREE
;
1199 inodes_stat
.nr_unused
--;
1200 hlist_del_init(&inode
->i_hash
);
1202 list_del_init(&inode
->i_list
);
1203 list_del_init(&inode
->i_sb_list
);
1204 inode
->i_state
|= I_FREEING
;
1205 inodes_stat
.nr_inodes
--;
1206 spin_unlock(&inode_lock
);
1207 if (inode
->i_data
.nrpages
)
1208 truncate_inode_pages(&inode
->i_data
, 0);
1210 wake_up_inode(inode
);
1211 destroy_inode(inode
);
1215 * Normal UNIX filesystem behaviour: delete the
1216 * inode when the usage count drops to zero, and
1219 void generic_drop_inode(struct inode
*inode
)
1221 if (!inode
->i_nlink
)
1222 generic_delete_inode(inode
);
1224 generic_forget_inode(inode
);
1227 EXPORT_SYMBOL_GPL(generic_drop_inode
);
1230 * Called when we're dropping the last reference
1233 * Call the FS "drop()" function, defaulting to
1234 * the legacy UNIX filesystem behaviour..
1236 * NOTE! NOTE! NOTE! We're called with the inode lock
1237 * held, and the drop function is supposed to release
1240 static inline void iput_final(struct inode
*inode
)
1242 const struct super_operations
*op
= inode
->i_sb
->s_op
;
1243 void (*drop
)(struct inode
*) = generic_drop_inode
;
1245 if (op
&& op
->drop_inode
)
1246 drop
= op
->drop_inode
;
1251 * iput - put an inode
1252 * @inode: inode to put
1254 * Puts an inode, dropping its usage count. If the inode use count hits
1255 * zero, the inode is then freed and may also be destroyed.
1257 * Consequently, iput() can sleep.
1259 void iput(struct inode
*inode
)
1262 BUG_ON(inode
->i_state
== I_CLEAR
);
1264 if (atomic_dec_and_lock(&inode
->i_count
, &inode_lock
))
1269 EXPORT_SYMBOL(iput
);
1272 * bmap - find a block number in a file
1273 * @inode: inode of file
1274 * @block: block to find
1276 * Returns the block number on the device holding the inode that
1277 * is the disk block number for the block of the file requested.
1278 * That is, asked for block 4 of inode 1 the function will return the
1279 * disk block relative to the disk start that holds that block of the
1282 sector_t
bmap(struct inode
* inode
, sector_t block
)
1285 if (inode
->i_mapping
->a_ops
->bmap
)
1286 res
= inode
->i_mapping
->a_ops
->bmap(inode
->i_mapping
, block
);
1289 EXPORT_SYMBOL(bmap
);
1292 * touch_atime - update the access time
1293 * @mnt: mount the inode is accessed on
1294 * @dentry: dentry accessed
1296 * Update the accessed time on an inode and mark it for writeback.
1297 * This function automatically handles read only file systems and media,
1298 * as well as the "noatime" flag and inode specific "noatime" markers.
1300 void touch_atime(struct vfsmount
*mnt
, struct dentry
*dentry
)
1302 struct inode
*inode
= dentry
->d_inode
;
1303 struct timespec now
;
1305 if (mnt_want_write(mnt
))
1307 if (inode
->i_flags
& S_NOATIME
)
1309 if (IS_NOATIME(inode
))
1311 if ((inode
->i_sb
->s_flags
& MS_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1314 if (mnt
->mnt_flags
& MNT_NOATIME
)
1316 if ((mnt
->mnt_flags
& MNT_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1318 if (mnt
->mnt_flags
& MNT_RELATIME
) {
1320 * With relative atime, only update atime if the previous
1321 * atime is earlier than either the ctime or mtime.
1323 if (timespec_compare(&inode
->i_mtime
, &inode
->i_atime
) < 0 &&
1324 timespec_compare(&inode
->i_ctime
, &inode
->i_atime
) < 0)
1328 now
= current_fs_time(inode
->i_sb
);
1329 if (timespec_equal(&inode
->i_atime
, &now
))
1332 inode
->i_atime
= now
;
1333 mark_inode_dirty_sync(inode
);
1335 mnt_drop_write(mnt
);
1337 EXPORT_SYMBOL(touch_atime
);
1340 * file_update_time - update mtime and ctime time
1341 * @file: file accessed
1343 * Update the mtime and ctime members of an inode and mark the inode
1344 * for writeback. Note that this function is meant exclusively for
1345 * usage in the file write path of filesystems, and filesystems may
1346 * choose to explicitly ignore update via this function with the
1347 * S_NOCTIME inode flag, e.g. for network filesystem where these
1348 * timestamps are handled by the server.
1351 void file_update_time(struct file
*file
)
1353 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1354 struct timespec now
;
1358 if (IS_NOCMTIME(inode
))
1361 err
= mnt_want_write(file
->f_path
.mnt
);
1365 now
= current_fs_time(inode
->i_sb
);
1366 if (!timespec_equal(&inode
->i_mtime
, &now
)) {
1367 inode
->i_mtime
= now
;
1371 if (!timespec_equal(&inode
->i_ctime
, &now
)) {
1372 inode
->i_ctime
= now
;
1376 if (IS_I_VERSION(inode
)) {
1377 inode_inc_iversion(inode
);
1382 mark_inode_dirty_sync(inode
);
1383 mnt_drop_write(file
->f_path
.mnt
);
1386 EXPORT_SYMBOL(file_update_time
);
1388 int inode_needs_sync(struct inode
*inode
)
1392 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
1397 EXPORT_SYMBOL(inode_needs_sync
);
1399 int inode_wait(void *word
)
1404 EXPORT_SYMBOL(inode_wait
);
1407 * If we try to find an inode in the inode hash while it is being
1408 * deleted, we have to wait until the filesystem completes its
1409 * deletion before reporting that it isn't found. This function waits
1410 * until the deletion _might_ have completed. Callers are responsible
1411 * to recheck inode state.
1413 * It doesn't matter if I_LOCK is not set initially, a call to
1414 * wake_up_inode() after removing from the hash list will DTRT.
1416 * This is called with inode_lock held.
1418 static void __wait_on_freeing_inode(struct inode
*inode
)
1420 wait_queue_head_t
*wq
;
1421 DEFINE_WAIT_BIT(wait
, &inode
->i_state
, __I_LOCK
);
1422 wq
= bit_waitqueue(&inode
->i_state
, __I_LOCK
);
1423 prepare_to_wait(wq
, &wait
.wait
, TASK_UNINTERRUPTIBLE
);
1424 spin_unlock(&inode_lock
);
1426 finish_wait(wq
, &wait
.wait
);
1427 spin_lock(&inode_lock
);
1431 * We rarely want to lock two inodes that do not have a parent/child
1432 * relationship (such as directory, child inode) simultaneously. The
1433 * vast majority of file systems should be able to get along fine
1434 * without this. Do not use these functions except as a last resort.
1436 void inode_double_lock(struct inode
*inode1
, struct inode
*inode2
)
1438 if (inode1
== NULL
|| inode2
== NULL
|| inode1
== inode2
) {
1440 mutex_lock(&inode1
->i_mutex
);
1442 mutex_lock(&inode2
->i_mutex
);
1446 if (inode1
< inode2
) {
1447 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_PARENT
);
1448 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_CHILD
);
1450 mutex_lock_nested(&inode2
->i_mutex
, I_MUTEX_PARENT
);
1451 mutex_lock_nested(&inode1
->i_mutex
, I_MUTEX_CHILD
);
1454 EXPORT_SYMBOL(inode_double_lock
);
1456 void inode_double_unlock(struct inode
*inode1
, struct inode
*inode2
)
1459 mutex_unlock(&inode1
->i_mutex
);
1461 if (inode2
&& inode2
!= inode1
)
1462 mutex_unlock(&inode2
->i_mutex
);
1464 EXPORT_SYMBOL(inode_double_unlock
);
1466 static __initdata
unsigned long ihash_entries
;
1467 static int __init
set_ihash_entries(char *str
)
1471 ihash_entries
= simple_strtoul(str
, &str
, 0);
1474 __setup("ihash_entries=", set_ihash_entries
);
1477 * Initialize the waitqueues and inode hash table.
1479 void __init
inode_init_early(void)
1483 /* If hashes are distributed across NUMA nodes, defer
1484 * hash allocation until vmalloc space is available.
1490 alloc_large_system_hash("Inode-cache",
1491 sizeof(struct hlist_head
),
1499 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1500 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1503 void __init
inode_init(void)
1507 /* inode slab cache */
1508 inode_cachep
= kmem_cache_create("inode_cache",
1509 sizeof(struct inode
),
1511 (SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|
1514 register_shrinker(&icache_shrinker
);
1516 /* Hash may have been set up in inode_init_early */
1521 alloc_large_system_hash("Inode-cache",
1522 sizeof(struct hlist_head
),
1530 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1531 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1534 void init_special_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
1536 inode
->i_mode
= mode
;
1537 if (S_ISCHR(mode
)) {
1538 inode
->i_fop
= &def_chr_fops
;
1539 inode
->i_rdev
= rdev
;
1540 } else if (S_ISBLK(mode
)) {
1541 inode
->i_fop
= &def_blk_fops
;
1542 inode
->i_rdev
= rdev
;
1543 } else if (S_ISFIFO(mode
))
1544 inode
->i_fop
= &def_fifo_fops
;
1545 else if (S_ISSOCK(mode
))
1546 inode
->i_fop
= &bad_sock_fops
;
1548 printk(KERN_DEBUG
"init_special_inode: bogus i_mode (%o)\n",
1551 EXPORT_SYMBOL(init_special_inode
);