2 * (C) 1997 Linus Torvalds
3 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
7 #include <linux/dcache.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/writeback.h>
11 #include <linux/module.h>
12 #include <linux/backing-dev.h>
13 #include <linux/wait.h>
14 #include <linux/rwsem.h>
15 #include <linux/hash.h>
16 #include <linux/swap.h>
17 #include <linux/security.h>
18 #include <linux/pagemap.h>
19 #include <linux/cdev.h>
20 #include <linux/bootmem.h>
21 #include <linux/fsnotify.h>
22 #include <linux/mount.h>
23 #include <linux/async.h>
24 #include <linux/posix_acl.h>
25 #include <linux/prefetch.h>
26 #include <linux/ima.h>
27 #include <linux/cred.h>
28 #include <linux/buffer_head.h> /* for inode_has_buffers */
32 * Inode locking rules:
34 * inode->i_lock protects:
35 * inode->i_state, inode->i_hash, __iget()
36 * inode_lru_lock protects:
37 * inode_lru, inode->i_lru
38 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
40 * bdi->wb.list_lock protects:
41 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
42 * inode_hash_lock protects:
43 * inode_hashtable, inode->i_hash
62 static unsigned int i_hash_mask __read_mostly;
63 static unsigned int i_hash_shift __read_mostly;
64 static struct hlist_head *inode_hashtable __read_mostly;
65 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
67 static LIST_HEAD(inode_lru);
68 static DEFINE_SPINLOCK(inode_lru_lock);
70 __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
73 * iprune_sem provides exclusion between the icache shrinking and the
76 * We don't actually need it to protect anything in the umount path,
77 * but only need to cycle through it to make sure any inode that
78 * prune_icache took off the LRU list has been fully torn down by the
79 * time we are past evict_inodes.
81 static DECLARE_RWSEM(iprune_sem);
84 * Empty aops. Can be used for the cases where the user does not
85 * define any of the address_space operations.
87 const struct address_space_operations empty_aops = {
89 EXPORT_SYMBOL(empty_aops);
92 * Statistics gathering..
94 struct inodes_stat_t inodes_stat;
96 static DEFINE_PER_CPU(unsigned int, nr_inodes);
98 static struct kmem_cache *inode_cachep __read_mostly;
100 static int get_nr_inodes(void)
104 for_each_possible_cpu(i)
105 sum += per_cpu(nr_inodes, i);
106 return sum < 0 ? 0 : sum;
109 static inline int get_nr_inodes_unused(void)
111 return inodes_stat.nr_unused;
114 int get_nr_dirty_inodes(void)
116 /* not actually dirty inodes, but a wild approximation */
117 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
118 return nr_dirty > 0 ? nr_dirty : 0;
122 * Handle nr_inode sysctl
125 int proc_nr_inodes(ctl_table *table, int write,
126 void __user *buffer, size_t *lenp, loff_t *ppos)
128 inodes_stat.nr_inodes = get_nr_inodes();
129 return proc_dointvec(table, write, buffer, lenp, ppos);
134 * inode_init_always - perform inode structure intialisation
135 * @sb: superblock inode belongs to
136 * @inode: inode to initialise
138 * These are initializations that need to be done on every inode
139 * allocation as the fields are not initialised by slab allocation.
141 int inode_init_always(struct super_block *sb, struct inode *inode)
143 static const struct inode_operations empty_iops;
144 static const struct file_operations empty_fops;
145 struct address_space *const mapping = &inode->i_data;
148 inode->i_blkbits = sb->s_blocksize_bits;
150 atomic_set(&inode->i_count, 1);
151 inode->i_op = &empty_iops;
152 inode->i_fop = &empty_fops;
156 atomic_set(&inode->i_writecount, 0);
160 inode->i_generation = 0;
162 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
164 inode->i_pipe = NULL;
165 inode->i_bdev = NULL;
166 inode->i_cdev = NULL;
168 inode->dirtied_when = 0;
170 if (security_inode_alloc(inode))
172 spin_lock_init(&inode->i_lock);
173 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
175 mutex_init(&inode->i_mutex);
176 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
178 init_rwsem(&inode->i_alloc_sem);
179 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
181 mapping->a_ops = &empty_aops;
182 mapping->host = inode;
184 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
185 mapping->assoc_mapping = NULL;
186 mapping->backing_dev_info = &default_backing_dev_info;
187 mapping->writeback_index = 0;
190 * If the block_device provides a backing_dev_info for client
191 * inodes then use that. Otherwise the inode share the bdev's
195 struct backing_dev_info *bdi;
197 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
198 mapping->backing_dev_info = bdi;
200 inode->i_private = NULL;
201 inode->i_mapping = mapping;
202 #ifdef CONFIG_FS_POSIX_ACL
203 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
206 #ifdef CONFIG_FSNOTIFY
207 inode->i_fsnotify_mask = 0;
210 this_cpu_inc(nr_inodes);
216 EXPORT_SYMBOL(inode_init_always);
218 static struct inode *alloc_inode(struct super_block *sb)
222 if (sb->s_op->alloc_inode)
223 inode = sb->s_op->alloc_inode(sb);
225 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
230 if (unlikely(inode_init_always(sb, inode))) {
231 if (inode->i_sb->s_op->destroy_inode)
232 inode->i_sb->s_op->destroy_inode(inode);
234 kmem_cache_free(inode_cachep, inode);
241 void free_inode_nonrcu(struct inode *inode)
243 kmem_cache_free(inode_cachep, inode);
245 EXPORT_SYMBOL(free_inode_nonrcu);
247 void __destroy_inode(struct inode *inode)
249 BUG_ON(inode_has_buffers(inode));
250 security_inode_free(inode);
251 fsnotify_inode_delete(inode);
252 #ifdef CONFIG_FS_POSIX_ACL
253 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
254 posix_acl_release(inode->i_acl);
255 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
256 posix_acl_release(inode->i_default_acl);
258 this_cpu_dec(nr_inodes);
260 EXPORT_SYMBOL(__destroy_inode);
262 static void i_callback(struct rcu_head *head)
264 struct inode *inode = container_of(head, struct inode, i_rcu);
265 INIT_LIST_HEAD(&inode->i_dentry);
266 kmem_cache_free(inode_cachep, inode);
269 static void destroy_inode(struct inode *inode)
271 BUG_ON(!list_empty(&inode->i_lru));
272 __destroy_inode(inode);
273 if (inode->i_sb->s_op->destroy_inode)
274 inode->i_sb->s_op->destroy_inode(inode);
276 call_rcu(&inode->i_rcu, i_callback);
279 void address_space_init_once(struct address_space *mapping)
281 memset(mapping, 0, sizeof(*mapping));
282 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
283 spin_lock_init(&mapping->tree_lock);
284 mutex_init(&mapping->i_mmap_mutex);
285 INIT_LIST_HEAD(&mapping->private_list);
286 spin_lock_init(&mapping->private_lock);
287 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
288 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
290 EXPORT_SYMBOL(address_space_init_once);
293 * These are initializations that only need to be done
294 * once, because the fields are idempotent across use
295 * of the inode, so let the slab aware of that.
297 void inode_init_once(struct inode *inode)
299 memset(inode, 0, sizeof(*inode));
300 INIT_HLIST_NODE(&inode->i_hash);
301 INIT_LIST_HEAD(&inode->i_dentry);
302 INIT_LIST_HEAD(&inode->i_devices);
303 INIT_LIST_HEAD(&inode->i_wb_list);
304 INIT_LIST_HEAD(&inode->i_lru);
305 address_space_init_once(&inode->i_data);
306 i_size_ordered_init(inode);
307 #ifdef CONFIG_FSNOTIFY
308 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
311 EXPORT_SYMBOL(inode_init_once);
313 static void init_once(void *foo)
315 struct inode *inode = (struct inode *) foo;
317 inode_init_once(inode);
321 * inode->i_lock must be held
323 void __iget(struct inode *inode)
325 atomic_inc(&inode->i_count);
329 * get additional reference to inode; caller must already hold one.
331 void ihold(struct inode *inode)
333 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
335 EXPORT_SYMBOL(ihold);
337 static void inode_lru_list_add(struct inode *inode)
339 spin_lock(&inode_lru_lock);
340 if (list_empty(&inode->i_lru)) {
341 list_add(&inode->i_lru, &inode_lru);
342 inodes_stat.nr_unused++;
344 spin_unlock(&inode_lru_lock);
347 static void inode_lru_list_del(struct inode *inode)
349 spin_lock(&inode_lru_lock);
350 if (!list_empty(&inode->i_lru)) {
351 list_del_init(&inode->i_lru);
352 inodes_stat.nr_unused--;
354 spin_unlock(&inode_lru_lock);
358 * inode_sb_list_add - add inode to the superblock list of inodes
359 * @inode: inode to add
361 void inode_sb_list_add(struct inode *inode)
363 spin_lock(&inode_sb_list_lock);
364 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
365 spin_unlock(&inode_sb_list_lock);
367 EXPORT_SYMBOL_GPL(inode_sb_list_add);
369 static inline void inode_sb_list_del(struct inode *inode)
371 spin_lock(&inode_sb_list_lock);
372 list_del_init(&inode->i_sb_list);
373 spin_unlock(&inode_sb_list_lock);
376 static unsigned long hash(struct super_block *sb, unsigned long hashval)
380 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
382 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
383 return tmp & i_hash_mask;
387 * __insert_inode_hash - hash an inode
388 * @inode: unhashed inode
389 * @hashval: unsigned long value used to locate this object in the
392 * Add an inode to the inode hash for this superblock.
394 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
396 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
398 spin_lock(&inode_hash_lock);
399 spin_lock(&inode->i_lock);
400 hlist_add_head(&inode->i_hash, b);
401 spin_unlock(&inode->i_lock);
402 spin_unlock(&inode_hash_lock);
404 EXPORT_SYMBOL(__insert_inode_hash);
407 * remove_inode_hash - remove an inode from the hash
408 * @inode: inode to unhash
410 * Remove an inode from the superblock.
412 void remove_inode_hash(struct inode *inode)
414 spin_lock(&inode_hash_lock);
415 spin_lock(&inode->i_lock);
416 hlist_del_init(&inode->i_hash);
417 spin_unlock(&inode->i_lock);
418 spin_unlock(&inode_hash_lock);
420 EXPORT_SYMBOL(remove_inode_hash);
422 void end_writeback(struct inode *inode)
425 BUG_ON(inode->i_data.nrpages);
426 BUG_ON(!list_empty(&inode->i_data.private_list));
427 BUG_ON(!(inode->i_state & I_FREEING));
428 BUG_ON(inode->i_state & I_CLEAR);
429 inode_sync_wait(inode);
430 /* don't need i_lock here, no concurrent mods to i_state */
431 inode->i_state = I_FREEING | I_CLEAR;
433 EXPORT_SYMBOL(end_writeback);
436 * Free the inode passed in, removing it from the lists it is still connected
437 * to. We remove any pages still attached to the inode and wait for any IO that
438 * is still in progress before finally destroying the inode.
440 * An inode must already be marked I_FREEING so that we avoid the inode being
441 * moved back onto lists if we race with other code that manipulates the lists
442 * (e.g. writeback_single_inode). The caller is responsible for setting this.
444 * An inode must already be removed from the LRU list before being evicted from
445 * the cache. This should occur atomically with setting the I_FREEING state
446 * flag, so no inodes here should ever be on the LRU when being evicted.
448 static void evict(struct inode *inode)
450 const struct super_operations *op = inode->i_sb->s_op;
452 BUG_ON(!(inode->i_state & I_FREEING));
453 BUG_ON(!list_empty(&inode->i_lru));
455 inode_wb_list_del(inode);
456 inode_sb_list_del(inode);
458 if (op->evict_inode) {
459 op->evict_inode(inode);
461 if (inode->i_data.nrpages)
462 truncate_inode_pages(&inode->i_data, 0);
463 end_writeback(inode);
465 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
467 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
470 remove_inode_hash(inode);
472 spin_lock(&inode->i_lock);
473 wake_up_bit(&inode->i_state, __I_NEW);
474 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
475 spin_unlock(&inode->i_lock);
477 destroy_inode(inode);
481 * dispose_list - dispose of the contents of a local list
482 * @head: the head of the list to free
484 * Dispose-list gets a local list with local inodes in it, so it doesn't
485 * need to worry about list corruption and SMP locks.
487 static void dispose_list(struct list_head *head)
489 while (!list_empty(head)) {
492 inode = list_first_entry(head, struct inode, i_lru);
493 list_del_init(&inode->i_lru);
500 * evict_inodes - evict all evictable inodes for a superblock
501 * @sb: superblock to operate on
503 * Make sure that no inodes with zero refcount are retained. This is
504 * called by superblock shutdown after having MS_ACTIVE flag removed,
505 * so any inode reaching zero refcount during or after that call will
506 * be immediately evicted.
508 void evict_inodes(struct super_block *sb)
510 struct inode *inode, *next;
513 spin_lock(&inode_sb_list_lock);
514 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
515 if (atomic_read(&inode->i_count))
518 spin_lock(&inode->i_lock);
519 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
520 spin_unlock(&inode->i_lock);
524 inode->i_state |= I_FREEING;
525 inode_lru_list_del(inode);
526 spin_unlock(&inode->i_lock);
527 list_add(&inode->i_lru, &dispose);
529 spin_unlock(&inode_sb_list_lock);
531 dispose_list(&dispose);
534 * Cycle through iprune_sem to make sure any inode that prune_icache
535 * moved off the list before we took the lock has been fully torn
538 down_write(&iprune_sem);
539 up_write(&iprune_sem);
543 * invalidate_inodes - attempt to free all inodes on a superblock
544 * @sb: superblock to operate on
545 * @kill_dirty: flag to guide handling of dirty inodes
547 * Attempts to free all inodes for a given superblock. If there were any
548 * busy inodes return a non-zero value, else zero.
549 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
552 int invalidate_inodes(struct super_block *sb, bool kill_dirty)
555 struct inode *inode, *next;
558 spin_lock(&inode_sb_list_lock);
559 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
560 spin_lock(&inode->i_lock);
561 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
562 spin_unlock(&inode->i_lock);
565 if (inode->i_state & I_DIRTY && !kill_dirty) {
566 spin_unlock(&inode->i_lock);
570 if (atomic_read(&inode->i_count)) {
571 spin_unlock(&inode->i_lock);
576 inode->i_state |= I_FREEING;
577 inode_lru_list_del(inode);
578 spin_unlock(&inode->i_lock);
579 list_add(&inode->i_lru, &dispose);
581 spin_unlock(&inode_sb_list_lock);
583 dispose_list(&dispose);
588 static int can_unuse(struct inode *inode)
590 if (inode->i_state & ~I_REFERENCED)
592 if (inode_has_buffers(inode))
594 if (atomic_read(&inode->i_count))
596 if (inode->i_data.nrpages)
602 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
603 * temporary list and then are freed outside inode_lru_lock by dispose_list().
605 * Any inodes which are pinned purely because of attached pagecache have their
606 * pagecache removed. If the inode has metadata buffers attached to
607 * mapping->private_list then try to remove them.
609 * If the inode has the I_REFERENCED flag set, then it means that it has been
610 * used recently - the flag is set in iput_final(). When we encounter such an
611 * inode, clear the flag and move it to the back of the LRU so it gets another
612 * pass through the LRU before it gets reclaimed. This is necessary because of
613 * the fact we are doing lazy LRU updates to minimise lock contention so the
614 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
615 * with this flag set because they are the inodes that are out of order.
617 static void prune_icache(int nr_to_scan)
621 unsigned long reap = 0;
623 down_read(&iprune_sem);
624 spin_lock(&inode_lru_lock);
625 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
628 if (list_empty(&inode_lru))
631 inode = list_entry(inode_lru.prev, struct inode, i_lru);
634 * we are inverting the inode_lru_lock/inode->i_lock here,
635 * so use a trylock. If we fail to get the lock, just move the
636 * inode to the back of the list so we don't spin on it.
638 if (!spin_trylock(&inode->i_lock)) {
639 list_move(&inode->i_lru, &inode_lru);
644 * Referenced or dirty inodes are still in use. Give them
645 * another pass through the LRU as we canot reclaim them now.
647 if (atomic_read(&inode->i_count) ||
648 (inode->i_state & ~I_REFERENCED)) {
649 list_del_init(&inode->i_lru);
650 spin_unlock(&inode->i_lock);
651 inodes_stat.nr_unused--;
655 /* recently referenced inodes get one more pass */
656 if (inode->i_state & I_REFERENCED) {
657 inode->i_state &= ~I_REFERENCED;
658 list_move(&inode->i_lru, &inode_lru);
659 spin_unlock(&inode->i_lock);
662 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
664 spin_unlock(&inode->i_lock);
665 spin_unlock(&inode_lru_lock);
666 if (remove_inode_buffers(inode))
667 reap += invalidate_mapping_pages(&inode->i_data,
670 spin_lock(&inode_lru_lock);
672 if (inode != list_entry(inode_lru.next,
673 struct inode, i_lru))
674 continue; /* wrong inode or list_empty */
675 /* avoid lock inversions with trylock */
676 if (!spin_trylock(&inode->i_lock))
678 if (!can_unuse(inode)) {
679 spin_unlock(&inode->i_lock);
683 WARN_ON(inode->i_state & I_NEW);
684 inode->i_state |= I_FREEING;
685 spin_unlock(&inode->i_lock);
687 list_move(&inode->i_lru, &freeable);
688 inodes_stat.nr_unused--;
690 if (current_is_kswapd())
691 __count_vm_events(KSWAPD_INODESTEAL, reap);
693 __count_vm_events(PGINODESTEAL, reap);
694 spin_unlock(&inode_lru_lock);
696 dispose_list(&freeable);
697 up_read(&iprune_sem);
701 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
702 * "unused" means that no dentries are referring to the inodes: the files are
703 * not open and the dcache references to those inodes have already been
706 * This function is passed the number of inodes to scan, and it returns the
707 * total number of remaining possibly-reclaimable inodes.
709 static int shrink_icache_memory(struct shrinker *shrink,
710 struct shrink_control *sc)
712 int nr = sc->nr_to_scan;
713 gfp_t gfp_mask = sc->gfp_mask;
717 * Nasty deadlock avoidance. We may hold various FS locks,
718 * and we don't want to recurse into the FS that called us
719 * in clear_inode() and friends..
721 if (!(gfp_mask & __GFP_FS))
725 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
728 static struct shrinker icache_shrinker = {
729 .shrink = shrink_icache_memory,
730 .seeks = DEFAULT_SEEKS,
733 static void __wait_on_freeing_inode(struct inode *inode);
735 * Called with the inode lock held.
737 static struct inode *find_inode(struct super_block *sb,
738 struct hlist_head *head,
739 int (*test)(struct inode *, void *),
742 struct hlist_node *node;
743 struct inode *inode = NULL;
746 hlist_for_each_entry(inode, node, head, i_hash) {
747 spin_lock(&inode->i_lock);
748 if (inode->i_sb != sb) {
749 spin_unlock(&inode->i_lock);
752 if (!test(inode, data)) {
753 spin_unlock(&inode->i_lock);
756 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
757 __wait_on_freeing_inode(inode);
761 spin_unlock(&inode->i_lock);
768 * find_inode_fast is the fast path version of find_inode, see the comment at
769 * iget_locked for details.
771 static struct inode *find_inode_fast(struct super_block *sb,
772 struct hlist_head *head, unsigned long ino)
774 struct hlist_node *node;
775 struct inode *inode = NULL;
778 hlist_for_each_entry(inode, node, head, i_hash) {
779 spin_lock(&inode->i_lock);
780 if (inode->i_ino != ino) {
781 spin_unlock(&inode->i_lock);
784 if (inode->i_sb != sb) {
785 spin_unlock(&inode->i_lock);
788 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
789 __wait_on_freeing_inode(inode);
793 spin_unlock(&inode->i_lock);
800 * Each cpu owns a range of LAST_INO_BATCH numbers.
801 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
802 * to renew the exhausted range.
804 * This does not significantly increase overflow rate because every CPU can
805 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
806 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
807 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
808 * overflow rate by 2x, which does not seem too significant.
810 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
811 * error if st_ino won't fit in target struct field. Use 32bit counter
812 * here to attempt to avoid that.
814 #define LAST_INO_BATCH 1024
815 static DEFINE_PER_CPU(unsigned int, last_ino);
817 unsigned int get_next_ino(void)
819 unsigned int *p = &get_cpu_var(last_ino);
820 unsigned int res = *p;
823 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
824 static atomic_t shared_last_ino;
825 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
827 res = next - LAST_INO_BATCH;
832 put_cpu_var(last_ino);
835 EXPORT_SYMBOL(get_next_ino);
838 * new_inode - obtain an inode
841 * Allocates a new inode for given superblock. The default gfp_mask
842 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
843 * If HIGHMEM pages are unsuitable or it is known that pages allocated
844 * for the page cache are not reclaimable or migratable,
845 * mapping_set_gfp_mask() must be called with suitable flags on the
846 * newly created inode's mapping
849 struct inode *new_inode(struct super_block *sb)
853 spin_lock_prefetch(&inode_sb_list_lock);
855 inode = alloc_inode(sb);
857 spin_lock(&inode->i_lock);
859 spin_unlock(&inode->i_lock);
860 inode_sb_list_add(inode);
864 EXPORT_SYMBOL(new_inode);
867 * unlock_new_inode - clear the I_NEW state and wake up any waiters
868 * @inode: new inode to unlock
870 * Called when the inode is fully initialised to clear the new state of the
871 * inode and wake up anyone waiting for the inode to finish initialisation.
873 void unlock_new_inode(struct inode *inode)
875 #ifdef CONFIG_DEBUG_LOCK_ALLOC
876 if (S_ISDIR(inode->i_mode)) {
877 struct file_system_type *type = inode->i_sb->s_type;
879 /* Set new key only if filesystem hasn't already changed it */
880 if (!lockdep_match_class(&inode->i_mutex,
881 &type->i_mutex_key)) {
883 * ensure nobody is actually holding i_mutex
885 mutex_destroy(&inode->i_mutex);
886 mutex_init(&inode->i_mutex);
887 lockdep_set_class(&inode->i_mutex,
888 &type->i_mutex_dir_key);
892 spin_lock(&inode->i_lock);
893 WARN_ON(!(inode->i_state & I_NEW));
894 inode->i_state &= ~I_NEW;
895 wake_up_bit(&inode->i_state, __I_NEW);
896 spin_unlock(&inode->i_lock);
898 EXPORT_SYMBOL(unlock_new_inode);
901 * iget5_locked - obtain an inode from a mounted file system
902 * @sb: super block of file system
903 * @hashval: hash value (usually inode number) to get
904 * @test: callback used for comparisons between inodes
905 * @set: callback used to initialize a new struct inode
906 * @data: opaque data pointer to pass to @test and @set
908 * Search for the inode specified by @hashval and @data in the inode cache,
909 * and if present it is return it with an increased reference count. This is
910 * a generalized version of iget_locked() for file systems where the inode
911 * number is not sufficient for unique identification of an inode.
913 * If the inode is not in cache, allocate a new inode and return it locked,
914 * hashed, and with the I_NEW flag set. The file system gets to fill it in
915 * before unlocking it via unlock_new_inode().
917 * Note both @test and @set are called with the inode_hash_lock held, so can't
920 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
921 int (*test)(struct inode *, void *),
922 int (*set)(struct inode *, void *), void *data)
924 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
927 spin_lock(&inode_hash_lock);
928 inode = find_inode(sb, head, test, data);
929 spin_unlock(&inode_hash_lock);
932 wait_on_inode(inode);
936 inode = alloc_inode(sb);
940 spin_lock(&inode_hash_lock);
941 /* We released the lock, so.. */
942 old = find_inode(sb, head, test, data);
944 if (set(inode, data))
947 spin_lock(&inode->i_lock);
948 inode->i_state = I_NEW;
949 hlist_add_head(&inode->i_hash, head);
950 spin_unlock(&inode->i_lock);
951 inode_sb_list_add(inode);
952 spin_unlock(&inode_hash_lock);
954 /* Return the locked inode with I_NEW set, the
955 * caller is responsible for filling in the contents
961 * Uhhuh, somebody else created the same inode under
962 * us. Use the old inode instead of the one we just
965 spin_unlock(&inode_hash_lock);
966 destroy_inode(inode);
968 wait_on_inode(inode);
973 spin_unlock(&inode_hash_lock);
974 destroy_inode(inode);
977 EXPORT_SYMBOL(iget5_locked);
980 * iget_locked - obtain an inode from a mounted file system
981 * @sb: super block of file system
982 * @ino: inode number to get
984 * Search for the inode specified by @ino in the inode cache and if present
985 * return it with an increased reference count. This is for file systems
986 * where the inode number is sufficient for unique identification of an inode.
988 * If the inode is not in cache, allocate a new inode and return it locked,
989 * hashed, and with the I_NEW flag set. The file system gets to fill it in
990 * before unlocking it via unlock_new_inode().
992 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
994 struct hlist_head *head = inode_hashtable + hash(sb, ino);
997 spin_lock(&inode_hash_lock);
998 inode = find_inode_fast(sb, head, ino);
999 spin_unlock(&inode_hash_lock);
1001 wait_on_inode(inode);
1005 inode = alloc_inode(sb);
1009 spin_lock(&inode_hash_lock);
1010 /* We released the lock, so.. */
1011 old = find_inode_fast(sb, head, ino);
1014 spin_lock(&inode->i_lock);
1015 inode->i_state = I_NEW;
1016 hlist_add_head(&inode->i_hash, head);
1017 spin_unlock(&inode->i_lock);
1018 inode_sb_list_add(inode);
1019 spin_unlock(&inode_hash_lock);
1021 /* Return the locked inode with I_NEW set, the
1022 * caller is responsible for filling in the contents
1028 * Uhhuh, somebody else created the same inode under
1029 * us. Use the old inode instead of the one we just
1032 spin_unlock(&inode_hash_lock);
1033 destroy_inode(inode);
1035 wait_on_inode(inode);
1039 EXPORT_SYMBOL(iget_locked);
1042 * search the inode cache for a matching inode number.
1043 * If we find one, then the inode number we are trying to
1044 * allocate is not unique and so we should not use it.
1046 * Returns 1 if the inode number is unique, 0 if it is not.
1048 static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1050 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1051 struct hlist_node *node;
1052 struct inode *inode;
1054 spin_lock(&inode_hash_lock);
1055 hlist_for_each_entry(inode, node, b, i_hash) {
1056 if (inode->i_ino == ino && inode->i_sb == sb) {
1057 spin_unlock(&inode_hash_lock);
1061 spin_unlock(&inode_hash_lock);
1067 * iunique - get a unique inode number
1069 * @max_reserved: highest reserved inode number
1071 * Obtain an inode number that is unique on the system for a given
1072 * superblock. This is used by file systems that have no natural
1073 * permanent inode numbering system. An inode number is returned that
1074 * is higher than the reserved limit but unique.
1077 * With a large number of inodes live on the file system this function
1078 * currently becomes quite slow.
1080 ino_t iunique(struct super_block *sb, ino_t max_reserved)
1083 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1084 * error if st_ino won't fit in target struct field. Use 32bit counter
1085 * here to attempt to avoid that.
1087 static DEFINE_SPINLOCK(iunique_lock);
1088 static unsigned int counter;
1091 spin_lock(&iunique_lock);
1093 if (counter <= max_reserved)
1094 counter = max_reserved + 1;
1096 } while (!test_inode_iunique(sb, res));
1097 spin_unlock(&iunique_lock);
1101 EXPORT_SYMBOL(iunique);
1103 struct inode *igrab(struct inode *inode)
1105 spin_lock(&inode->i_lock);
1106 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1108 spin_unlock(&inode->i_lock);
1110 spin_unlock(&inode->i_lock);
1112 * Handle the case where s_op->clear_inode is not been
1113 * called yet, and somebody is calling igrab
1114 * while the inode is getting freed.
1120 EXPORT_SYMBOL(igrab);
1123 * ilookup5_nowait - search for an inode in the inode cache
1124 * @sb: super block of file system to search
1125 * @hashval: hash value (usually inode number) to search for
1126 * @test: callback used for comparisons between inodes
1127 * @data: opaque data pointer to pass to @test
1129 * Search for the inode specified by @hashval and @data in the inode cache.
1130 * If the inode is in the cache, the inode is returned with an incremented
1133 * Note: I_NEW is not waited upon so you have to be very careful what you do
1134 * with the returned inode. You probably should be using ilookup5() instead.
1136 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1138 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1139 int (*test)(struct inode *, void *), void *data)
1141 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1142 struct inode *inode;
1144 spin_lock(&inode_hash_lock);
1145 inode = find_inode(sb, head, test, data);
1146 spin_unlock(&inode_hash_lock);
1150 EXPORT_SYMBOL(ilookup5_nowait);
1153 * ilookup5 - search for an inode in the inode cache
1154 * @sb: super block of file system to search
1155 * @hashval: hash value (usually inode number) to search for
1156 * @test: callback used for comparisons between inodes
1157 * @data: opaque data pointer to pass to @test
1159 * Search for the inode specified by @hashval and @data in the inode cache,
1160 * and if the inode is in the cache, return the inode with an incremented
1161 * reference count. Waits on I_NEW before returning the inode.
1162 * returned with an incremented reference count.
1164 * This is a generalized version of ilookup() for file systems where the
1165 * inode number is not sufficient for unique identification of an inode.
1167 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1169 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1170 int (*test)(struct inode *, void *), void *data)
1172 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1175 wait_on_inode(inode);
1178 EXPORT_SYMBOL(ilookup5);
1181 * ilookup - search for an inode in the inode cache
1182 * @sb: super block of file system to search
1183 * @ino: inode number to search for
1185 * Search for the inode @ino in the inode cache, and if the inode is in the
1186 * cache, the inode is returned with an incremented reference count.
1188 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1190 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1191 struct inode *inode;
1193 spin_lock(&inode_hash_lock);
1194 inode = find_inode_fast(sb, head, ino);
1195 spin_unlock(&inode_hash_lock);
1198 wait_on_inode(inode);
1201 EXPORT_SYMBOL(ilookup);
1203 int insert_inode_locked(struct inode *inode)
1205 struct super_block *sb = inode->i_sb;
1206 ino_t ino = inode->i_ino;
1207 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1210 struct hlist_node *node;
1211 struct inode *old = NULL;
1212 spin_lock(&inode_hash_lock);
1213 hlist_for_each_entry(old, node, head, i_hash) {
1214 if (old->i_ino != ino)
1216 if (old->i_sb != sb)
1218 spin_lock(&old->i_lock);
1219 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1220 spin_unlock(&old->i_lock);
1225 if (likely(!node)) {
1226 spin_lock(&inode->i_lock);
1227 inode->i_state |= I_NEW;
1228 hlist_add_head(&inode->i_hash, head);
1229 spin_unlock(&inode->i_lock);
1230 spin_unlock(&inode_hash_lock);
1234 spin_unlock(&old->i_lock);
1235 spin_unlock(&inode_hash_lock);
1237 if (unlikely(!inode_unhashed(old))) {
1244 EXPORT_SYMBOL(insert_inode_locked);
1246 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1247 int (*test)(struct inode *, void *), void *data)
1249 struct super_block *sb = inode->i_sb;
1250 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1253 struct hlist_node *node;
1254 struct inode *old = NULL;
1256 spin_lock(&inode_hash_lock);
1257 hlist_for_each_entry(old, node, head, i_hash) {
1258 if (old->i_sb != sb)
1260 if (!test(old, data))
1262 spin_lock(&old->i_lock);
1263 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1264 spin_unlock(&old->i_lock);
1269 if (likely(!node)) {
1270 spin_lock(&inode->i_lock);
1271 inode->i_state |= I_NEW;
1272 hlist_add_head(&inode->i_hash, head);
1273 spin_unlock(&inode->i_lock);
1274 spin_unlock(&inode_hash_lock);
1278 spin_unlock(&old->i_lock);
1279 spin_unlock(&inode_hash_lock);
1281 if (unlikely(!inode_unhashed(old))) {
1288 EXPORT_SYMBOL(insert_inode_locked4);
1291 int generic_delete_inode(struct inode *inode)
1295 EXPORT_SYMBOL(generic_delete_inode);
1298 * Normal UNIX filesystem behaviour: delete the
1299 * inode when the usage count drops to zero, and
1302 int generic_drop_inode(struct inode *inode)
1304 return !inode->i_nlink || inode_unhashed(inode);
1306 EXPORT_SYMBOL_GPL(generic_drop_inode);
1309 * Called when we're dropping the last reference
1312 * Call the FS "drop_inode()" function, defaulting to
1313 * the legacy UNIX filesystem behaviour. If it tells
1314 * us to evict inode, do so. Otherwise, retain inode
1315 * in cache if fs is alive, sync and evict if fs is
1318 static void iput_final(struct inode *inode)
1320 struct super_block *sb = inode->i_sb;
1321 const struct super_operations *op = inode->i_sb->s_op;
1324 WARN_ON(inode->i_state & I_NEW);
1326 if (op && op->drop_inode)
1327 drop = op->drop_inode(inode);
1329 drop = generic_drop_inode(inode);
1331 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1332 inode->i_state |= I_REFERENCED;
1333 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1334 inode_lru_list_add(inode);
1335 spin_unlock(&inode->i_lock);
1340 inode->i_state |= I_WILL_FREE;
1341 spin_unlock(&inode->i_lock);
1342 write_inode_now(inode, 1);
1343 spin_lock(&inode->i_lock);
1344 WARN_ON(inode->i_state & I_NEW);
1345 inode->i_state &= ~I_WILL_FREE;
1348 inode->i_state |= I_FREEING;
1349 inode_lru_list_del(inode);
1350 spin_unlock(&inode->i_lock);
1356 * iput - put an inode
1357 * @inode: inode to put
1359 * Puts an inode, dropping its usage count. If the inode use count hits
1360 * zero, the inode is then freed and may also be destroyed.
1362 * Consequently, iput() can sleep.
1364 void iput(struct inode *inode)
1367 BUG_ON(inode->i_state & I_CLEAR);
1369 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
1373 EXPORT_SYMBOL(iput);
1376 * bmap - find a block number in a file
1377 * @inode: inode of file
1378 * @block: block to find
1380 * Returns the block number on the device holding the inode that
1381 * is the disk block number for the block of the file requested.
1382 * That is, asked for block 4 of inode 1 the function will return the
1383 * disk block relative to the disk start that holds that block of the
1386 sector_t bmap(struct inode *inode, sector_t block)
1389 if (inode->i_mapping->a_ops->bmap)
1390 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1393 EXPORT_SYMBOL(bmap);
1396 * With relative atime, only update atime if the previous atime is
1397 * earlier than either the ctime or mtime or if at least a day has
1398 * passed since the last atime update.
1400 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1401 struct timespec now)
1404 if (!(mnt->mnt_flags & MNT_RELATIME))
1407 * Is mtime younger than atime? If yes, update atime:
1409 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1412 * Is ctime younger than atime? If yes, update atime:
1414 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1418 * Is the previous atime value older than a day? If yes,
1421 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1424 * Good, we can skip the atime update:
1430 * touch_atime - update the access time
1431 * @mnt: mount the inode is accessed on
1432 * @dentry: dentry accessed
1434 * Update the accessed time on an inode and mark it for writeback.
1435 * This function automatically handles read only file systems and media,
1436 * as well as the "noatime" flag and inode specific "noatime" markers.
1438 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1440 struct inode *inode = dentry->d_inode;
1441 struct timespec now;
1443 if (inode->i_flags & S_NOATIME)
1445 if (IS_NOATIME(inode))
1447 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1450 if (mnt->mnt_flags & MNT_NOATIME)
1452 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1455 now = current_fs_time(inode->i_sb);
1457 if (!relatime_need_update(mnt, inode, now))
1460 if (timespec_equal(&inode->i_atime, &now))
1463 if (mnt_want_write(mnt))
1466 inode->i_atime = now;
1467 mark_inode_dirty_sync(inode);
1468 mnt_drop_write(mnt);
1470 EXPORT_SYMBOL(touch_atime);
1473 * file_update_time - update mtime and ctime time
1474 * @file: file accessed
1476 * Update the mtime and ctime members of an inode and mark the inode
1477 * for writeback. Note that this function is meant exclusively for
1478 * usage in the file write path of filesystems, and filesystems may
1479 * choose to explicitly ignore update via this function with the
1480 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1481 * timestamps are handled by the server.
1484 void file_update_time(struct file *file)
1486 struct inode *inode = file->f_path.dentry->d_inode;
1487 struct timespec now;
1488 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1490 /* First try to exhaust all avenues to not sync */
1491 if (IS_NOCMTIME(inode))
1494 now = current_fs_time(inode->i_sb);
1495 if (!timespec_equal(&inode->i_mtime, &now))
1498 if (!timespec_equal(&inode->i_ctime, &now))
1501 if (IS_I_VERSION(inode))
1502 sync_it |= S_VERSION;
1507 /* Finally allowed to write? Takes lock. */
1508 if (mnt_want_write_file(file))
1511 /* Only change inode inside the lock region */
1512 if (sync_it & S_VERSION)
1513 inode_inc_iversion(inode);
1514 if (sync_it & S_CTIME)
1515 inode->i_ctime = now;
1516 if (sync_it & S_MTIME)
1517 inode->i_mtime = now;
1518 mark_inode_dirty_sync(inode);
1519 mnt_drop_write(file->f_path.mnt);
1521 EXPORT_SYMBOL(file_update_time);
1523 int inode_needs_sync(struct inode *inode)
1527 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1531 EXPORT_SYMBOL(inode_needs_sync);
1533 int inode_wait(void *word)
1538 EXPORT_SYMBOL(inode_wait);
1541 * If we try to find an inode in the inode hash while it is being
1542 * deleted, we have to wait until the filesystem completes its
1543 * deletion before reporting that it isn't found. This function waits
1544 * until the deletion _might_ have completed. Callers are responsible
1545 * to recheck inode state.
1547 * It doesn't matter if I_NEW is not set initially, a call to
1548 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1551 static void __wait_on_freeing_inode(struct inode *inode)
1553 wait_queue_head_t *wq;
1554 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1555 wq = bit_waitqueue(&inode->i_state, __I_NEW);
1556 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1557 spin_unlock(&inode->i_lock);
1558 spin_unlock(&inode_hash_lock);
1560 finish_wait(wq, &wait.wait);
1561 spin_lock(&inode_hash_lock);
1564 static __initdata unsigned long ihash_entries;
1565 static int __init set_ihash_entries(char *str)
1569 ihash_entries = simple_strtoul(str, &str, 0);
1572 __setup("ihash_entries=", set_ihash_entries);
1575 * Initialize the waitqueues and inode hash table.
1577 void __init inode_init_early(void)
1581 /* If hashes are distributed across NUMA nodes, defer
1582 * hash allocation until vmalloc space is available.
1588 alloc_large_system_hash("Inode-cache",
1589 sizeof(struct hlist_head),
1597 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1598 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1601 void __init inode_init(void)
1605 /* inode slab cache */
1606 inode_cachep = kmem_cache_create("inode_cache",
1607 sizeof(struct inode),
1609 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1612 register_shrinker(&icache_shrinker);
1614 /* Hash may have been set up in inode_init_early */
1619 alloc_large_system_hash("Inode-cache",
1620 sizeof(struct hlist_head),
1628 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1629 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1632 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1634 inode->i_mode = mode;
1635 if (S_ISCHR(mode)) {
1636 inode->i_fop = &def_chr_fops;
1637 inode->i_rdev = rdev;
1638 } else if (S_ISBLK(mode)) {
1639 inode->i_fop = &def_blk_fops;
1640 inode->i_rdev = rdev;
1641 } else if (S_ISFIFO(mode))
1642 inode->i_fop = &def_fifo_fops;
1643 else if (S_ISSOCK(mode))
1644 inode->i_fop = &bad_sock_fops;
1646 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1647 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1650 EXPORT_SYMBOL(init_special_inode);
1653 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
1655 * @dir: Directory inode
1656 * @mode: mode of the new inode
1658 void inode_init_owner(struct inode *inode, const struct inode *dir,
1661 inode->i_uid = current_fsuid();
1662 if (dir && dir->i_mode & S_ISGID) {
1663 inode->i_gid = dir->i_gid;
1667 inode->i_gid = current_fsgid();
1668 inode->i_mode = mode;
1670 EXPORT_SYMBOL(inode_init_owner);
1673 * inode_owner_or_capable - check current task permissions to inode
1674 * @inode: inode being checked
1676 * Return true if current either has CAP_FOWNER to the inode, or
1679 bool inode_owner_or_capable(const struct inode *inode)
1681 struct user_namespace *ns = inode_userns(inode);
1683 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1685 if (ns_capable(ns, CAP_FOWNER))
1689 EXPORT_SYMBOL(inode_owner_or_capable);