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->i_sb->s_inode_lru_lock protects:
37 * inode->i_sb->s_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
49 * inode->i_sb->s_inode_lru_lock
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 __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
68 EXPORT_SYMBOL(inode_sb_list_lock);
71 * Empty aops. Can be used for the cases where the user does not
72 * define any of the address_space operations.
74 const struct address_space_operations empty_aops = {
76 EXPORT_SYMBOL(empty_aops);
79 * Statistics gathering..
81 struct inodes_stat_t inodes_stat;
83 static DEFINE_PER_CPU(unsigned int, nr_inodes);
84 static DEFINE_PER_CPU(unsigned int, nr_unused);
86 static struct kmem_cache *inode_cachep __read_mostly;
88 static int get_nr_inodes(void)
92 for_each_possible_cpu(i)
93 sum += per_cpu(nr_inodes, i);
94 return sum < 0 ? 0 : sum;
97 static inline int get_nr_inodes_unused(void)
101 for_each_possible_cpu(i)
102 sum += per_cpu(nr_unused, i);
103 return sum < 0 ? 0 : sum;
106 int get_nr_dirty_inodes(void)
108 /* not actually dirty inodes, but a wild approximation */
109 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
110 return nr_dirty > 0 ? nr_dirty : 0;
114 * Handle nr_inode sysctl
117 int proc_nr_inodes(ctl_table *table, int write,
118 void __user *buffer, size_t *lenp, loff_t *ppos)
120 inodes_stat.nr_inodes = get_nr_inodes();
121 inodes_stat.nr_unused = get_nr_inodes_unused();
122 return proc_dointvec(table, write, buffer, lenp, ppos);
127 * inode_init_always - perform inode structure intialisation
128 * @sb: superblock inode belongs to
129 * @inode: inode to initialise
131 * These are initializations that need to be done on every inode
132 * allocation as the fields are not initialised by slab allocation.
134 int inode_init_always(struct super_block *sb, struct inode *inode)
136 static const struct inode_operations empty_iops;
137 static const struct file_operations empty_fops;
138 struct address_space *const mapping = &inode->i_data;
141 inode->i_blkbits = sb->s_blocksize_bits;
143 atomic_set(&inode->i_count, 1);
144 inode->i_op = &empty_iops;
145 inode->i_fop = &empty_fops;
146 inode->__i_nlink = 1;
147 inode->i_opflags = 0;
150 atomic_set(&inode->i_writecount, 0);
154 inode->i_generation = 0;
156 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
158 inode->i_pipe = NULL;
159 inode->i_bdev = NULL;
160 inode->i_cdev = NULL;
162 inode->dirtied_when = 0;
164 if (security_inode_alloc(inode))
166 spin_lock_init(&inode->i_lock);
167 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
169 mutex_init(&inode->i_mutex);
170 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
172 atomic_set(&inode->i_dio_count, 0);
174 mapping->a_ops = &empty_aops;
175 mapping->host = inode;
177 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
178 mapping->assoc_mapping = NULL;
179 mapping->backing_dev_info = &default_backing_dev_info;
180 mapping->writeback_index = 0;
183 * If the block_device provides a backing_dev_info for client
184 * inodes then use that. Otherwise the inode share the bdev's
188 struct backing_dev_info *bdi;
190 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
191 mapping->backing_dev_info = bdi;
193 inode->i_private = NULL;
194 inode->i_mapping = mapping;
195 #ifdef CONFIG_FS_POSIX_ACL
196 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
199 #ifdef CONFIG_FSNOTIFY
200 inode->i_fsnotify_mask = 0;
203 this_cpu_inc(nr_inodes);
209 EXPORT_SYMBOL(inode_init_always);
211 static struct inode *alloc_inode(struct super_block *sb)
215 if (sb->s_op->alloc_inode)
216 inode = sb->s_op->alloc_inode(sb);
218 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
223 if (unlikely(inode_init_always(sb, inode))) {
224 if (inode->i_sb->s_op->destroy_inode)
225 inode->i_sb->s_op->destroy_inode(inode);
227 kmem_cache_free(inode_cachep, inode);
234 void free_inode_nonrcu(struct inode *inode)
236 kmem_cache_free(inode_cachep, inode);
238 EXPORT_SYMBOL(free_inode_nonrcu);
240 void __destroy_inode(struct inode *inode)
242 BUG_ON(inode_has_buffers(inode));
243 security_inode_free(inode);
244 fsnotify_inode_delete(inode);
245 #ifdef CONFIG_FS_POSIX_ACL
246 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
247 posix_acl_release(inode->i_acl);
248 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
249 posix_acl_release(inode->i_default_acl);
251 this_cpu_dec(nr_inodes);
253 EXPORT_SYMBOL(__destroy_inode);
255 static void i_callback(struct rcu_head *head)
257 struct inode *inode = container_of(head, struct inode, i_rcu);
258 INIT_LIST_HEAD(&inode->i_dentry);
259 kmem_cache_free(inode_cachep, inode);
262 static void destroy_inode(struct inode *inode)
264 BUG_ON(!list_empty(&inode->i_lru));
265 __destroy_inode(inode);
266 if (inode->i_sb->s_op->destroy_inode)
267 inode->i_sb->s_op->destroy_inode(inode);
269 call_rcu(&inode->i_rcu, i_callback);
272 void address_space_init_once(struct address_space *mapping)
274 memset(mapping, 0, sizeof(*mapping));
275 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
276 spin_lock_init(&mapping->tree_lock);
277 mutex_init(&mapping->i_mmap_mutex);
278 INIT_LIST_HEAD(&mapping->private_list);
279 spin_lock_init(&mapping->private_lock);
280 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
281 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
283 EXPORT_SYMBOL(address_space_init_once);
286 * These are initializations that only need to be done
287 * once, because the fields are idempotent across use
288 * of the inode, so let the slab aware of that.
290 void inode_init_once(struct inode *inode)
292 memset(inode, 0, sizeof(*inode));
293 INIT_HLIST_NODE(&inode->i_hash);
294 INIT_LIST_HEAD(&inode->i_dentry);
295 INIT_LIST_HEAD(&inode->i_devices);
296 INIT_LIST_HEAD(&inode->i_wb_list);
297 INIT_LIST_HEAD(&inode->i_lru);
298 address_space_init_once(&inode->i_data);
299 i_size_ordered_init(inode);
300 #ifdef CONFIG_FSNOTIFY
301 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
304 EXPORT_SYMBOL(inode_init_once);
306 static void init_once(void *foo)
308 struct inode *inode = (struct inode *) foo;
310 inode_init_once(inode);
314 * inode->i_lock must be held
316 void __iget(struct inode *inode)
318 atomic_inc(&inode->i_count);
322 * get additional reference to inode; caller must already hold one.
324 void ihold(struct inode *inode)
326 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
328 EXPORT_SYMBOL(ihold);
330 static void inode_lru_list_add(struct inode *inode)
332 spin_lock(&inode->i_sb->s_inode_lru_lock);
333 if (list_empty(&inode->i_lru)) {
334 list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
335 inode->i_sb->s_nr_inodes_unused++;
336 this_cpu_inc(nr_unused);
338 spin_unlock(&inode->i_sb->s_inode_lru_lock);
341 static void inode_lru_list_del(struct inode *inode)
343 spin_lock(&inode->i_sb->s_inode_lru_lock);
344 if (!list_empty(&inode->i_lru)) {
345 list_del_init(&inode->i_lru);
346 inode->i_sb->s_nr_inodes_unused--;
347 this_cpu_dec(nr_unused);
349 spin_unlock(&inode->i_sb->s_inode_lru_lock);
353 * inode_sb_list_add - add inode to the superblock list of inodes
354 * @inode: inode to add
356 void inode_sb_list_add(struct inode *inode)
358 spin_lock(&inode_sb_list_lock);
359 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
360 spin_unlock(&inode_sb_list_lock);
362 EXPORT_SYMBOL_GPL(inode_sb_list_add);
364 static inline void inode_sb_list_del(struct inode *inode)
366 if (!list_empty(&inode->i_sb_list)) {
367 spin_lock(&inode_sb_list_lock);
368 list_del_init(&inode->i_sb_list);
369 spin_unlock(&inode_sb_list_lock);
373 static unsigned long hash(struct super_block *sb, unsigned long hashval)
377 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
379 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
380 return tmp & i_hash_mask;
384 * __insert_inode_hash - hash an inode
385 * @inode: unhashed inode
386 * @hashval: unsigned long value used to locate this object in the
389 * Add an inode to the inode hash for this superblock.
391 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
393 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
395 spin_lock(&inode_hash_lock);
396 spin_lock(&inode->i_lock);
397 hlist_add_head(&inode->i_hash, b);
398 spin_unlock(&inode->i_lock);
399 spin_unlock(&inode_hash_lock);
401 EXPORT_SYMBOL(__insert_inode_hash);
404 * __remove_inode_hash - remove an inode from the hash
405 * @inode: inode to unhash
407 * Remove an inode from the superblock.
409 void __remove_inode_hash(struct inode *inode)
411 spin_lock(&inode_hash_lock);
412 spin_lock(&inode->i_lock);
413 hlist_del_init(&inode->i_hash);
414 spin_unlock(&inode->i_lock);
415 spin_unlock(&inode_hash_lock);
417 EXPORT_SYMBOL(__remove_inode_hash);
419 void end_writeback(struct inode *inode)
423 * We have to cycle tree_lock here because reclaim can be still in the
424 * process of removing the last page (in __delete_from_page_cache())
425 * and we must not free mapping under it.
427 spin_lock_irq(&inode->i_data.tree_lock);
428 BUG_ON(inode->i_data.nrpages);
429 spin_unlock_irq(&inode->i_data.tree_lock);
430 BUG_ON(!list_empty(&inode->i_data.private_list));
431 BUG_ON(!(inode->i_state & I_FREEING));
432 BUG_ON(inode->i_state & I_CLEAR);
433 inode_sync_wait(inode);
434 /* don't need i_lock here, no concurrent mods to i_state */
435 inode->i_state = I_FREEING | I_CLEAR;
437 EXPORT_SYMBOL(end_writeback);
440 * Free the inode passed in, removing it from the lists it is still connected
441 * to. We remove any pages still attached to the inode and wait for any IO that
442 * is still in progress before finally destroying the inode.
444 * An inode must already be marked I_FREEING so that we avoid the inode being
445 * moved back onto lists if we race with other code that manipulates the lists
446 * (e.g. writeback_single_inode). The caller is responsible for setting this.
448 * An inode must already be removed from the LRU list before being evicted from
449 * the cache. This should occur atomically with setting the I_FREEING state
450 * flag, so no inodes here should ever be on the LRU when being evicted.
452 static void evict(struct inode *inode)
454 const struct super_operations *op = inode->i_sb->s_op;
456 BUG_ON(!(inode->i_state & I_FREEING));
457 BUG_ON(!list_empty(&inode->i_lru));
459 if (!list_empty(&inode->i_wb_list))
460 inode_wb_list_del(inode);
462 inode_sb_list_del(inode);
464 if (op->evict_inode) {
465 op->evict_inode(inode);
467 if (inode->i_data.nrpages)
468 truncate_inode_pages(&inode->i_data, 0);
469 end_writeback(inode);
471 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
473 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
476 remove_inode_hash(inode);
478 spin_lock(&inode->i_lock);
479 wake_up_bit(&inode->i_state, __I_NEW);
480 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
481 spin_unlock(&inode->i_lock);
483 destroy_inode(inode);
487 * dispose_list - dispose of the contents of a local list
488 * @head: the head of the list to free
490 * Dispose-list gets a local list with local inodes in it, so it doesn't
491 * need to worry about list corruption and SMP locks.
493 static void dispose_list(struct list_head *head)
495 while (!list_empty(head)) {
498 inode = list_first_entry(head, struct inode, i_lru);
499 list_del_init(&inode->i_lru);
506 * evict_inodes - evict all evictable inodes for a superblock
507 * @sb: superblock to operate on
509 * Make sure that no inodes with zero refcount are retained. This is
510 * called by superblock shutdown after having MS_ACTIVE flag removed,
511 * so any inode reaching zero refcount during or after that call will
512 * be immediately evicted.
514 void evict_inodes(struct super_block *sb)
516 struct inode *inode, *next;
519 spin_lock(&inode_sb_list_lock);
520 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
521 if (atomic_read(&inode->i_count))
524 spin_lock(&inode->i_lock);
525 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
526 spin_unlock(&inode->i_lock);
530 inode->i_state |= I_FREEING;
531 inode_lru_list_del(inode);
532 spin_unlock(&inode->i_lock);
533 list_add(&inode->i_lru, &dispose);
535 spin_unlock(&inode_sb_list_lock);
537 dispose_list(&dispose);
541 * invalidate_inodes - attempt to free all inodes on a superblock
542 * @sb: superblock to operate on
543 * @kill_dirty: flag to guide handling of dirty inodes
545 * Attempts to free all inodes for a given superblock. If there were any
546 * busy inodes return a non-zero value, else zero.
547 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
550 int invalidate_inodes(struct super_block *sb, bool kill_dirty)
553 struct inode *inode, *next;
556 spin_lock(&inode_sb_list_lock);
557 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
558 spin_lock(&inode->i_lock);
559 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
560 spin_unlock(&inode->i_lock);
563 if (inode->i_state & I_DIRTY && !kill_dirty) {
564 spin_unlock(&inode->i_lock);
568 if (atomic_read(&inode->i_count)) {
569 spin_unlock(&inode->i_lock);
574 inode->i_state |= I_FREEING;
575 inode_lru_list_del(inode);
576 spin_unlock(&inode->i_lock);
577 list_add(&inode->i_lru, &dispose);
579 spin_unlock(&inode_sb_list_lock);
581 dispose_list(&dispose);
586 static int can_unuse(struct inode *inode)
588 if (inode->i_state & ~I_REFERENCED)
590 if (inode_has_buffers(inode))
592 if (atomic_read(&inode->i_count))
594 if (inode->i_data.nrpages)
600 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
601 * This is called from the superblock shrinker function with a number of inodes
602 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
603 * then are freed outside inode_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 void prune_icache_sb(struct super_block *sb, int nr_to_scan)
621 unsigned long reap = 0;
623 spin_lock(&sb->s_inode_lru_lock);
624 for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
627 if (list_empty(&sb->s_inode_lru))
630 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
633 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
634 * so use a trylock. If we fail to get the lock, just move the
635 * inode to the back of the list so we don't spin on it.
637 if (!spin_trylock(&inode->i_lock)) {
638 list_move_tail(&inode->i_lru, &sb->s_inode_lru);
643 * Referenced or dirty inodes are still in use. Give them
644 * another pass through the LRU as we canot reclaim them now.
646 if (atomic_read(&inode->i_count) ||
647 (inode->i_state & ~I_REFERENCED)) {
648 list_del_init(&inode->i_lru);
649 spin_unlock(&inode->i_lock);
650 sb->s_nr_inodes_unused--;
651 this_cpu_dec(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, &sb->s_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(&sb->s_inode_lru_lock);
666 if (remove_inode_buffers(inode))
667 reap += invalidate_mapping_pages(&inode->i_data,
670 spin_lock(&sb->s_inode_lru_lock);
672 if (inode != list_entry(sb->s_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 sb->s_nr_inodes_unused--;
689 this_cpu_dec(nr_unused);
691 if (current_is_kswapd())
692 __count_vm_events(KSWAPD_INODESTEAL, reap);
694 __count_vm_events(PGINODESTEAL, reap);
695 spin_unlock(&sb->s_inode_lru_lock);
697 dispose_list(&freeable);
700 static void __wait_on_freeing_inode(struct inode *inode);
702 * Called with the inode lock held.
704 static struct inode *find_inode(struct super_block *sb,
705 struct hlist_head *head,
706 int (*test)(struct inode *, void *),
709 struct hlist_node *node;
710 struct inode *inode = NULL;
713 hlist_for_each_entry(inode, node, head, i_hash) {
714 spin_lock(&inode->i_lock);
715 if (inode->i_sb != sb) {
716 spin_unlock(&inode->i_lock);
719 if (!test(inode, data)) {
720 spin_unlock(&inode->i_lock);
723 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
724 __wait_on_freeing_inode(inode);
728 spin_unlock(&inode->i_lock);
735 * find_inode_fast is the fast path version of find_inode, see the comment at
736 * iget_locked for details.
738 static struct inode *find_inode_fast(struct super_block *sb,
739 struct hlist_head *head, unsigned long ino)
741 struct hlist_node *node;
742 struct inode *inode = NULL;
745 hlist_for_each_entry(inode, node, head, i_hash) {
746 spin_lock(&inode->i_lock);
747 if (inode->i_ino != ino) {
748 spin_unlock(&inode->i_lock);
751 if (inode->i_sb != sb) {
752 spin_unlock(&inode->i_lock);
755 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
756 __wait_on_freeing_inode(inode);
760 spin_unlock(&inode->i_lock);
767 * Each cpu owns a range of LAST_INO_BATCH numbers.
768 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
769 * to renew the exhausted range.
771 * This does not significantly increase overflow rate because every CPU can
772 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
773 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
774 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
775 * overflow rate by 2x, which does not seem too significant.
777 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
778 * error if st_ino won't fit in target struct field. Use 32bit counter
779 * here to attempt to avoid that.
781 #define LAST_INO_BATCH 1024
782 static DEFINE_PER_CPU(unsigned int, last_ino);
784 unsigned int get_next_ino(void)
786 unsigned int *p = &get_cpu_var(last_ino);
787 unsigned int res = *p;
790 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
791 static atomic_t shared_last_ino;
792 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
794 res = next - LAST_INO_BATCH;
799 put_cpu_var(last_ino);
802 EXPORT_SYMBOL(get_next_ino);
805 * new_inode_pseudo - obtain an inode
808 * Allocates a new inode for given superblock.
809 * Inode wont be chained in superblock s_inodes list
811 * - fs can't be unmount
812 * - quotas, fsnotify, writeback can't work
814 struct inode *new_inode_pseudo(struct super_block *sb)
816 struct inode *inode = alloc_inode(sb);
819 spin_lock(&inode->i_lock);
821 spin_unlock(&inode->i_lock);
822 INIT_LIST_HEAD(&inode->i_sb_list);
828 * new_inode - obtain an inode
831 * Allocates a new inode for given superblock. The default gfp_mask
832 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
833 * If HIGHMEM pages are unsuitable or it is known that pages allocated
834 * for the page cache are not reclaimable or migratable,
835 * mapping_set_gfp_mask() must be called with suitable flags on the
836 * newly created inode's mapping
839 struct inode *new_inode(struct super_block *sb)
843 spin_lock_prefetch(&inode_sb_list_lock);
845 inode = new_inode_pseudo(sb);
847 inode_sb_list_add(inode);
850 EXPORT_SYMBOL(new_inode);
852 #ifdef CONFIG_DEBUG_LOCK_ALLOC
853 void lockdep_annotate_inode_mutex_key(struct inode *inode)
855 if (S_ISDIR(inode->i_mode)) {
856 struct file_system_type *type = inode->i_sb->s_type;
858 /* Set new key only if filesystem hasn't already changed it */
859 if (!lockdep_match_class(&inode->i_mutex,
860 &type->i_mutex_key)) {
862 * ensure nobody is actually holding i_mutex
864 mutex_destroy(&inode->i_mutex);
865 mutex_init(&inode->i_mutex);
866 lockdep_set_class(&inode->i_mutex,
867 &type->i_mutex_dir_key);
871 EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
875 * unlock_new_inode - clear the I_NEW state and wake up any waiters
876 * @inode: new inode to unlock
878 * Called when the inode is fully initialised to clear the new state of the
879 * inode and wake up anyone waiting for the inode to finish initialisation.
881 void unlock_new_inode(struct inode *inode)
883 lockdep_annotate_inode_mutex_key(inode);
884 spin_lock(&inode->i_lock);
885 WARN_ON(!(inode->i_state & I_NEW));
886 inode->i_state &= ~I_NEW;
887 wake_up_bit(&inode->i_state, __I_NEW);
888 spin_unlock(&inode->i_lock);
890 EXPORT_SYMBOL(unlock_new_inode);
893 * iget5_locked - obtain an inode from a mounted file system
894 * @sb: super block of file system
895 * @hashval: hash value (usually inode number) to get
896 * @test: callback used for comparisons between inodes
897 * @set: callback used to initialize a new struct inode
898 * @data: opaque data pointer to pass to @test and @set
900 * Search for the inode specified by @hashval and @data in the inode cache,
901 * and if present it is return it with an increased reference count. This is
902 * a generalized version of iget_locked() for file systems where the inode
903 * number is not sufficient for unique identification of an inode.
905 * If the inode is not in cache, allocate a new inode and return it locked,
906 * hashed, and with the I_NEW flag set. The file system gets to fill it in
907 * before unlocking it via unlock_new_inode().
909 * Note both @test and @set are called with the inode_hash_lock held, so can't
912 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
913 int (*test)(struct inode *, void *),
914 int (*set)(struct inode *, void *), void *data)
916 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
919 spin_lock(&inode_hash_lock);
920 inode = find_inode(sb, head, test, data);
921 spin_unlock(&inode_hash_lock);
924 wait_on_inode(inode);
928 inode = alloc_inode(sb);
932 spin_lock(&inode_hash_lock);
933 /* We released the lock, so.. */
934 old = find_inode(sb, head, test, data);
936 if (set(inode, data))
939 spin_lock(&inode->i_lock);
940 inode->i_state = I_NEW;
941 hlist_add_head(&inode->i_hash, head);
942 spin_unlock(&inode->i_lock);
943 inode_sb_list_add(inode);
944 spin_unlock(&inode_hash_lock);
946 /* Return the locked inode with I_NEW set, the
947 * caller is responsible for filling in the contents
953 * Uhhuh, somebody else created the same inode under
954 * us. Use the old inode instead of the one we just
957 spin_unlock(&inode_hash_lock);
958 destroy_inode(inode);
960 wait_on_inode(inode);
965 spin_unlock(&inode_hash_lock);
966 destroy_inode(inode);
969 EXPORT_SYMBOL(iget5_locked);
972 * iget_locked - obtain an inode from a mounted file system
973 * @sb: super block of file system
974 * @ino: inode number to get
976 * Search for the inode specified by @ino in the inode cache and if present
977 * return it with an increased reference count. This is for file systems
978 * where the inode number is sufficient for unique identification of an inode.
980 * If the inode is not in cache, allocate a new inode and return it locked,
981 * hashed, and with the I_NEW flag set. The file system gets to fill it in
982 * before unlocking it via unlock_new_inode().
984 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
986 struct hlist_head *head = inode_hashtable + hash(sb, ino);
989 spin_lock(&inode_hash_lock);
990 inode = find_inode_fast(sb, head, ino);
991 spin_unlock(&inode_hash_lock);
993 wait_on_inode(inode);
997 inode = alloc_inode(sb);
1001 spin_lock(&inode_hash_lock);
1002 /* We released the lock, so.. */
1003 old = find_inode_fast(sb, head, ino);
1006 spin_lock(&inode->i_lock);
1007 inode->i_state = I_NEW;
1008 hlist_add_head(&inode->i_hash, head);
1009 spin_unlock(&inode->i_lock);
1010 inode_sb_list_add(inode);
1011 spin_unlock(&inode_hash_lock);
1013 /* Return the locked inode with I_NEW set, the
1014 * caller is responsible for filling in the contents
1020 * Uhhuh, somebody else created the same inode under
1021 * us. Use the old inode instead of the one we just
1024 spin_unlock(&inode_hash_lock);
1025 destroy_inode(inode);
1027 wait_on_inode(inode);
1031 EXPORT_SYMBOL(iget_locked);
1034 * search the inode cache for a matching inode number.
1035 * If we find one, then the inode number we are trying to
1036 * allocate is not unique and so we should not use it.
1038 * Returns 1 if the inode number is unique, 0 if it is not.
1040 static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1042 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1043 struct hlist_node *node;
1044 struct inode *inode;
1046 spin_lock(&inode_hash_lock);
1047 hlist_for_each_entry(inode, node, b, i_hash) {
1048 if (inode->i_ino == ino && inode->i_sb == sb) {
1049 spin_unlock(&inode_hash_lock);
1053 spin_unlock(&inode_hash_lock);
1059 * iunique - get a unique inode number
1061 * @max_reserved: highest reserved inode number
1063 * Obtain an inode number that is unique on the system for a given
1064 * superblock. This is used by file systems that have no natural
1065 * permanent inode numbering system. An inode number is returned that
1066 * is higher than the reserved limit but unique.
1069 * With a large number of inodes live on the file system this function
1070 * currently becomes quite slow.
1072 ino_t iunique(struct super_block *sb, ino_t max_reserved)
1075 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1076 * error if st_ino won't fit in target struct field. Use 32bit counter
1077 * here to attempt to avoid that.
1079 static DEFINE_SPINLOCK(iunique_lock);
1080 static unsigned int counter;
1083 spin_lock(&iunique_lock);
1085 if (counter <= max_reserved)
1086 counter = max_reserved + 1;
1088 } while (!test_inode_iunique(sb, res));
1089 spin_unlock(&iunique_lock);
1093 EXPORT_SYMBOL(iunique);
1095 struct inode *igrab(struct inode *inode)
1097 spin_lock(&inode->i_lock);
1098 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1100 spin_unlock(&inode->i_lock);
1102 spin_unlock(&inode->i_lock);
1104 * Handle the case where s_op->clear_inode is not been
1105 * called yet, and somebody is calling igrab
1106 * while the inode is getting freed.
1112 EXPORT_SYMBOL(igrab);
1115 * ilookup5_nowait - search for an inode in the inode cache
1116 * @sb: super block of file system to search
1117 * @hashval: hash value (usually inode number) to search for
1118 * @test: callback used for comparisons between inodes
1119 * @data: opaque data pointer to pass to @test
1121 * Search for the inode specified by @hashval and @data in the inode cache.
1122 * If the inode is in the cache, the inode is returned with an incremented
1125 * Note: I_NEW is not waited upon so you have to be very careful what you do
1126 * with the returned inode. You probably should be using ilookup5() instead.
1128 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1130 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1131 int (*test)(struct inode *, void *), void *data)
1133 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1134 struct inode *inode;
1136 spin_lock(&inode_hash_lock);
1137 inode = find_inode(sb, head, test, data);
1138 spin_unlock(&inode_hash_lock);
1142 EXPORT_SYMBOL(ilookup5_nowait);
1145 * ilookup5 - search for an inode in the inode cache
1146 * @sb: super block of file system to search
1147 * @hashval: hash value (usually inode number) to search for
1148 * @test: callback used for comparisons between inodes
1149 * @data: opaque data pointer to pass to @test
1151 * Search for the inode specified by @hashval and @data in the inode cache,
1152 * and if the inode is in the cache, return the inode with an incremented
1153 * reference count. Waits on I_NEW before returning the inode.
1154 * returned with an incremented reference count.
1156 * This is a generalized version of ilookup() for file systems where the
1157 * inode number is not sufficient for unique identification of an inode.
1159 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1161 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1162 int (*test)(struct inode *, void *), void *data)
1164 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1167 wait_on_inode(inode);
1170 EXPORT_SYMBOL(ilookup5);
1173 * ilookup - search for an inode in the inode cache
1174 * @sb: super block of file system to search
1175 * @ino: inode number to search for
1177 * Search for the inode @ino in the inode cache, and if the inode is in the
1178 * cache, the inode is returned with an incremented reference count.
1180 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1182 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1183 struct inode *inode;
1185 spin_lock(&inode_hash_lock);
1186 inode = find_inode_fast(sb, head, ino);
1187 spin_unlock(&inode_hash_lock);
1190 wait_on_inode(inode);
1193 EXPORT_SYMBOL(ilookup);
1195 int insert_inode_locked(struct inode *inode)
1197 struct super_block *sb = inode->i_sb;
1198 ino_t ino = inode->i_ino;
1199 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1202 struct hlist_node *node;
1203 struct inode *old = NULL;
1204 spin_lock(&inode_hash_lock);
1205 hlist_for_each_entry(old, node, head, i_hash) {
1206 if (old->i_ino != ino)
1208 if (old->i_sb != sb)
1210 spin_lock(&old->i_lock);
1211 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1212 spin_unlock(&old->i_lock);
1217 if (likely(!node)) {
1218 spin_lock(&inode->i_lock);
1219 inode->i_state |= I_NEW;
1220 hlist_add_head(&inode->i_hash, head);
1221 spin_unlock(&inode->i_lock);
1222 spin_unlock(&inode_hash_lock);
1226 spin_unlock(&old->i_lock);
1227 spin_unlock(&inode_hash_lock);
1229 if (unlikely(!inode_unhashed(old))) {
1236 EXPORT_SYMBOL(insert_inode_locked);
1238 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1239 int (*test)(struct inode *, void *), void *data)
1241 struct super_block *sb = inode->i_sb;
1242 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1245 struct hlist_node *node;
1246 struct inode *old = NULL;
1248 spin_lock(&inode_hash_lock);
1249 hlist_for_each_entry(old, node, head, i_hash) {
1250 if (old->i_sb != sb)
1252 if (!test(old, data))
1254 spin_lock(&old->i_lock);
1255 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1256 spin_unlock(&old->i_lock);
1261 if (likely(!node)) {
1262 spin_lock(&inode->i_lock);
1263 inode->i_state |= I_NEW;
1264 hlist_add_head(&inode->i_hash, head);
1265 spin_unlock(&inode->i_lock);
1266 spin_unlock(&inode_hash_lock);
1270 spin_unlock(&old->i_lock);
1271 spin_unlock(&inode_hash_lock);
1273 if (unlikely(!inode_unhashed(old))) {
1280 EXPORT_SYMBOL(insert_inode_locked4);
1283 int generic_delete_inode(struct inode *inode)
1287 EXPORT_SYMBOL(generic_delete_inode);
1290 * Normal UNIX filesystem behaviour: delete the
1291 * inode when the usage count drops to zero, and
1294 int generic_drop_inode(struct inode *inode)
1296 return !inode->i_nlink || inode_unhashed(inode);
1298 EXPORT_SYMBOL_GPL(generic_drop_inode);
1301 * Called when we're dropping the last reference
1304 * Call the FS "drop_inode()" function, defaulting to
1305 * the legacy UNIX filesystem behaviour. If it tells
1306 * us to evict inode, do so. Otherwise, retain inode
1307 * in cache if fs is alive, sync and evict if fs is
1310 static void iput_final(struct inode *inode)
1312 struct super_block *sb = inode->i_sb;
1313 const struct super_operations *op = inode->i_sb->s_op;
1316 WARN_ON(inode->i_state & I_NEW);
1319 drop = op->drop_inode(inode);
1321 drop = generic_drop_inode(inode);
1323 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1324 inode->i_state |= I_REFERENCED;
1325 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1326 inode_lru_list_add(inode);
1327 spin_unlock(&inode->i_lock);
1332 inode->i_state |= I_WILL_FREE;
1333 spin_unlock(&inode->i_lock);
1334 write_inode_now(inode, 1);
1335 spin_lock(&inode->i_lock);
1336 WARN_ON(inode->i_state & I_NEW);
1337 inode->i_state &= ~I_WILL_FREE;
1340 inode->i_state |= I_FREEING;
1341 if (!list_empty(&inode->i_lru))
1342 inode_lru_list_del(inode);
1343 spin_unlock(&inode->i_lock);
1349 * iput - put an inode
1350 * @inode: inode to put
1352 * Puts an inode, dropping its usage count. If the inode use count hits
1353 * zero, the inode is then freed and may also be destroyed.
1355 * Consequently, iput() can sleep.
1357 void iput(struct inode *inode)
1360 BUG_ON(inode->i_state & I_CLEAR);
1362 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
1366 EXPORT_SYMBOL(iput);
1369 * bmap - find a block number in a file
1370 * @inode: inode of file
1371 * @block: block to find
1373 * Returns the block number on the device holding the inode that
1374 * is the disk block number for the block of the file requested.
1375 * That is, asked for block 4 of inode 1 the function will return the
1376 * disk block relative to the disk start that holds that block of the
1379 sector_t bmap(struct inode *inode, sector_t block)
1382 if (inode->i_mapping->a_ops->bmap)
1383 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1386 EXPORT_SYMBOL(bmap);
1389 * With relative atime, only update atime if the previous atime is
1390 * earlier than either the ctime or mtime or if at least a day has
1391 * passed since the last atime update.
1393 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1394 struct timespec now)
1397 if (!(mnt->mnt_flags & MNT_RELATIME))
1400 * Is mtime younger than atime? If yes, update atime:
1402 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1405 * Is ctime younger than atime? If yes, update atime:
1407 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1411 * Is the previous atime value older than a day? If yes,
1414 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1417 * Good, we can skip the atime update:
1423 * touch_atime - update the access time
1424 * @mnt: mount the inode is accessed on
1425 * @dentry: dentry accessed
1427 * Update the accessed time on an inode and mark it for writeback.
1428 * This function automatically handles read only file systems and media,
1429 * as well as the "noatime" flag and inode specific "noatime" markers.
1431 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1433 struct inode *inode = dentry->d_inode;
1434 struct timespec now;
1436 if (inode->i_flags & S_NOATIME)
1438 if (IS_NOATIME(inode))
1440 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1443 if (mnt->mnt_flags & MNT_NOATIME)
1445 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1448 now = current_fs_time(inode->i_sb);
1450 if (!relatime_need_update(mnt, inode, now))
1453 if (timespec_equal(&inode->i_atime, &now))
1456 if (mnt_want_write(mnt))
1459 inode->i_atime = now;
1460 mark_inode_dirty_sync(inode);
1461 mnt_drop_write(mnt);
1463 EXPORT_SYMBOL(touch_atime);
1466 * file_update_time - update mtime and ctime time
1467 * @file: file accessed
1469 * Update the mtime and ctime members of an inode and mark the inode
1470 * for writeback. Note that this function is meant exclusively for
1471 * usage in the file write path of filesystems, and filesystems may
1472 * choose to explicitly ignore update via this function with the
1473 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1474 * timestamps are handled by the server.
1477 void file_update_time(struct file *file)
1479 struct inode *inode = file->f_path.dentry->d_inode;
1480 struct timespec now;
1481 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1483 /* First try to exhaust all avenues to not sync */
1484 if (IS_NOCMTIME(inode))
1487 now = current_fs_time(inode->i_sb);
1488 if (!timespec_equal(&inode->i_mtime, &now))
1491 if (!timespec_equal(&inode->i_ctime, &now))
1494 if (IS_I_VERSION(inode))
1495 sync_it |= S_VERSION;
1500 /* Finally allowed to write? Takes lock. */
1501 if (mnt_want_write_file(file))
1504 /* Only change inode inside the lock region */
1505 if (sync_it & S_VERSION)
1506 inode_inc_iversion(inode);
1507 if (sync_it & S_CTIME)
1508 inode->i_ctime = now;
1509 if (sync_it & S_MTIME)
1510 inode->i_mtime = now;
1511 mark_inode_dirty_sync(inode);
1512 mnt_drop_write(file->f_path.mnt);
1514 EXPORT_SYMBOL(file_update_time);
1516 int inode_needs_sync(struct inode *inode)
1520 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1524 EXPORT_SYMBOL(inode_needs_sync);
1526 int inode_wait(void *word)
1531 EXPORT_SYMBOL(inode_wait);
1534 * If we try to find an inode in the inode hash while it is being
1535 * deleted, we have to wait until the filesystem completes its
1536 * deletion before reporting that it isn't found. This function waits
1537 * until the deletion _might_ have completed. Callers are responsible
1538 * to recheck inode state.
1540 * It doesn't matter if I_NEW is not set initially, a call to
1541 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1544 static void __wait_on_freeing_inode(struct inode *inode)
1546 wait_queue_head_t *wq;
1547 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1548 wq = bit_waitqueue(&inode->i_state, __I_NEW);
1549 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1550 spin_unlock(&inode->i_lock);
1551 spin_unlock(&inode_hash_lock);
1553 finish_wait(wq, &wait.wait);
1554 spin_lock(&inode_hash_lock);
1557 static __initdata unsigned long ihash_entries;
1558 static int __init set_ihash_entries(char *str)
1562 ihash_entries = simple_strtoul(str, &str, 0);
1565 __setup("ihash_entries=", set_ihash_entries);
1568 * Initialize the waitqueues and inode hash table.
1570 void __init inode_init_early(void)
1574 /* If hashes are distributed across NUMA nodes, defer
1575 * hash allocation until vmalloc space is available.
1581 alloc_large_system_hash("Inode-cache",
1582 sizeof(struct hlist_head),
1590 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1591 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1594 void __init inode_init(void)
1598 /* inode slab cache */
1599 inode_cachep = kmem_cache_create("inode_cache",
1600 sizeof(struct inode),
1602 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1606 /* Hash may have been set up in inode_init_early */
1611 alloc_large_system_hash("Inode-cache",
1612 sizeof(struct hlist_head),
1620 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1621 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1624 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1626 inode->i_mode = mode;
1627 if (S_ISCHR(mode)) {
1628 inode->i_fop = &def_chr_fops;
1629 inode->i_rdev = rdev;
1630 } else if (S_ISBLK(mode)) {
1631 inode->i_fop = &def_blk_fops;
1632 inode->i_rdev = rdev;
1633 } else if (S_ISFIFO(mode))
1634 inode->i_fop = &def_fifo_fops;
1635 else if (S_ISSOCK(mode))
1636 inode->i_fop = &bad_sock_fops;
1638 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1639 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1642 EXPORT_SYMBOL(init_special_inode);
1645 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
1647 * @dir: Directory inode
1648 * @mode: mode of the new inode
1650 void inode_init_owner(struct inode *inode, const struct inode *dir,
1653 inode->i_uid = current_fsuid();
1654 if (dir && dir->i_mode & S_ISGID) {
1655 inode->i_gid = dir->i_gid;
1659 inode->i_gid = current_fsgid();
1660 inode->i_mode = mode;
1662 EXPORT_SYMBOL(inode_init_owner);
1665 * inode_owner_or_capable - check current task permissions to inode
1666 * @inode: inode being checked
1668 * Return true if current either has CAP_FOWNER to the inode, or
1671 bool inode_owner_or_capable(const struct inode *inode)
1673 struct user_namespace *ns = inode_userns(inode);
1675 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1677 if (ns_capable(ns, CAP_FOWNER))
1681 EXPORT_SYMBOL(inode_owner_or_capable);