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->i_sb->s_inode_lru, inode->i_lru
38 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
40 * inode_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 DEFINE_SPINLOCK(inode_lru_lock);
69 __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
70 __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_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);
97 static DEFINE_PER_CPU(unsigned int, nr_unused);
99 static struct kmem_cache *inode_cachep __read_mostly;
101 static int get_nr_inodes(void)
105 for_each_possible_cpu(i)
106 sum += per_cpu(nr_inodes, i);
107 return sum < 0 ? 0 : sum;
110 static inline int get_nr_inodes_unused(void)
114 for_each_possible_cpu(i)
115 sum += per_cpu(nr_unused, i);
116 return sum < 0 ? 0 : sum;
119 int get_nr_dirty_inodes(void)
121 /* not actually dirty inodes, but a wild approximation */
122 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
123 return nr_dirty > 0 ? nr_dirty : 0;
127 * Handle nr_inode sysctl
130 int proc_nr_inodes(ctl_table *table, int write,
131 void __user *buffer, size_t *lenp, loff_t *ppos)
133 inodes_stat.nr_inodes = get_nr_inodes();
134 inodes_stat.nr_unused = get_nr_inodes_unused();
135 return proc_dointvec(table, write, buffer, lenp, ppos);
140 * inode_init_always - perform inode structure intialisation
141 * @sb: superblock inode belongs to
142 * @inode: inode to initialise
144 * These are initializations that need to be done on every inode
145 * allocation as the fields are not initialised by slab allocation.
147 int inode_init_always(struct super_block *sb, struct inode *inode)
149 static const struct inode_operations empty_iops;
150 static const struct file_operations empty_fops;
151 struct address_space *const mapping = &inode->i_data;
154 inode->i_blkbits = sb->s_blocksize_bits;
156 atomic_set(&inode->i_count, 1);
157 inode->i_op = &empty_iops;
158 inode->i_fop = &empty_fops;
162 atomic_set(&inode->i_writecount, 0);
166 inode->i_generation = 0;
168 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
170 inode->i_pipe = NULL;
171 inode->i_bdev = NULL;
172 inode->i_cdev = NULL;
174 inode->dirtied_when = 0;
176 if (security_inode_alloc(inode))
178 spin_lock_init(&inode->i_lock);
179 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
181 mutex_init(&inode->i_mutex);
182 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
184 init_rwsem(&inode->i_alloc_sem);
185 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
187 mapping->a_ops = &empty_aops;
188 mapping->host = inode;
190 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
191 mapping->assoc_mapping = NULL;
192 mapping->backing_dev_info = &default_backing_dev_info;
193 mapping->writeback_index = 0;
196 * If the block_device provides a backing_dev_info for client
197 * inodes then use that. Otherwise the inode share the bdev's
201 struct backing_dev_info *bdi;
203 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
204 mapping->backing_dev_info = bdi;
206 inode->i_private = NULL;
207 inode->i_mapping = mapping;
208 #ifdef CONFIG_FS_POSIX_ACL
209 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
212 #ifdef CONFIG_FSNOTIFY
213 inode->i_fsnotify_mask = 0;
216 this_cpu_inc(nr_inodes);
222 EXPORT_SYMBOL(inode_init_always);
224 static struct inode *alloc_inode(struct super_block *sb)
228 if (sb->s_op->alloc_inode)
229 inode = sb->s_op->alloc_inode(sb);
231 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
236 if (unlikely(inode_init_always(sb, inode))) {
237 if (inode->i_sb->s_op->destroy_inode)
238 inode->i_sb->s_op->destroy_inode(inode);
240 kmem_cache_free(inode_cachep, inode);
247 void free_inode_nonrcu(struct inode *inode)
249 kmem_cache_free(inode_cachep, inode);
251 EXPORT_SYMBOL(free_inode_nonrcu);
253 void __destroy_inode(struct inode *inode)
255 BUG_ON(inode_has_buffers(inode));
256 security_inode_free(inode);
257 fsnotify_inode_delete(inode);
258 #ifdef CONFIG_FS_POSIX_ACL
259 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
260 posix_acl_release(inode->i_acl);
261 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
262 posix_acl_release(inode->i_default_acl);
264 this_cpu_dec(nr_inodes);
266 EXPORT_SYMBOL(__destroy_inode);
268 static void i_callback(struct rcu_head *head)
270 struct inode *inode = container_of(head, struct inode, i_rcu);
271 INIT_LIST_HEAD(&inode->i_dentry);
272 kmem_cache_free(inode_cachep, inode);
275 static void destroy_inode(struct inode *inode)
277 BUG_ON(!list_empty(&inode->i_lru));
278 __destroy_inode(inode);
279 if (inode->i_sb->s_op->destroy_inode)
280 inode->i_sb->s_op->destroy_inode(inode);
282 call_rcu(&inode->i_rcu, i_callback);
285 void address_space_init_once(struct address_space *mapping)
287 memset(mapping, 0, sizeof(*mapping));
288 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
289 spin_lock_init(&mapping->tree_lock);
290 mutex_init(&mapping->i_mmap_mutex);
291 INIT_LIST_HEAD(&mapping->private_list);
292 spin_lock_init(&mapping->private_lock);
293 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
294 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
296 EXPORT_SYMBOL(address_space_init_once);
299 * These are initializations that only need to be done
300 * once, because the fields are idempotent across use
301 * of the inode, so let the slab aware of that.
303 void inode_init_once(struct inode *inode)
305 memset(inode, 0, sizeof(*inode));
306 INIT_HLIST_NODE(&inode->i_hash);
307 INIT_LIST_HEAD(&inode->i_dentry);
308 INIT_LIST_HEAD(&inode->i_devices);
309 INIT_LIST_HEAD(&inode->i_wb_list);
310 INIT_LIST_HEAD(&inode->i_lru);
311 address_space_init_once(&inode->i_data);
312 i_size_ordered_init(inode);
313 #ifdef CONFIG_FSNOTIFY
314 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
317 EXPORT_SYMBOL(inode_init_once);
319 static void init_once(void *foo)
321 struct inode *inode = (struct inode *) foo;
323 inode_init_once(inode);
327 * inode->i_lock must be held
329 void __iget(struct inode *inode)
331 atomic_inc(&inode->i_count);
335 * get additional reference to inode; caller must already hold one.
337 void ihold(struct inode *inode)
339 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
341 EXPORT_SYMBOL(ihold);
343 static void inode_lru_list_add(struct inode *inode)
345 spin_lock(&inode_lru_lock);
346 if (list_empty(&inode->i_lru)) {
347 list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
348 inode->i_sb->s_nr_inodes_unused++;
349 this_cpu_inc(nr_unused);
351 spin_unlock(&inode_lru_lock);
354 static void inode_lru_list_del(struct inode *inode)
356 spin_lock(&inode_lru_lock);
357 if (!list_empty(&inode->i_lru)) {
358 list_del_init(&inode->i_lru);
359 inode->i_sb->s_nr_inodes_unused--;
360 this_cpu_dec(nr_unused);
362 spin_unlock(&inode_lru_lock);
366 * inode_sb_list_add - add inode to the superblock list of inodes
367 * @inode: inode to add
369 void inode_sb_list_add(struct inode *inode)
371 spin_lock(&inode_sb_list_lock);
372 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
373 spin_unlock(&inode_sb_list_lock);
375 EXPORT_SYMBOL_GPL(inode_sb_list_add);
377 static inline void inode_sb_list_del(struct inode *inode)
379 spin_lock(&inode_sb_list_lock);
380 list_del_init(&inode->i_sb_list);
381 spin_unlock(&inode_sb_list_lock);
384 static unsigned long hash(struct super_block *sb, unsigned long hashval)
388 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
390 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
391 return tmp & i_hash_mask;
395 * __insert_inode_hash - hash an inode
396 * @inode: unhashed inode
397 * @hashval: unsigned long value used to locate this object in the
400 * Add an inode to the inode hash for this superblock.
402 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
404 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
406 spin_lock(&inode_hash_lock);
407 spin_lock(&inode->i_lock);
408 hlist_add_head(&inode->i_hash, b);
409 spin_unlock(&inode->i_lock);
410 spin_unlock(&inode_hash_lock);
412 EXPORT_SYMBOL(__insert_inode_hash);
415 * remove_inode_hash - remove an inode from the hash
416 * @inode: inode to unhash
418 * Remove an inode from the superblock.
420 void remove_inode_hash(struct inode *inode)
422 spin_lock(&inode_hash_lock);
423 spin_lock(&inode->i_lock);
424 hlist_del_init(&inode->i_hash);
425 spin_unlock(&inode->i_lock);
426 spin_unlock(&inode_hash_lock);
428 EXPORT_SYMBOL(remove_inode_hash);
430 void end_writeback(struct inode *inode)
434 * We have to cycle tree_lock here because reclaim can be still in the
435 * process of removing the last page (in __delete_from_page_cache())
436 * and we must not free mapping under it.
438 spin_lock_irq(&inode->i_data.tree_lock);
439 BUG_ON(inode->i_data.nrpages);
440 spin_unlock_irq(&inode->i_data.tree_lock);
441 BUG_ON(!list_empty(&inode->i_data.private_list));
442 BUG_ON(!(inode->i_state & I_FREEING));
443 BUG_ON(inode->i_state & I_CLEAR);
444 inode_sync_wait(inode);
445 /* don't need i_lock here, no concurrent mods to i_state */
446 inode->i_state = I_FREEING | I_CLEAR;
448 EXPORT_SYMBOL(end_writeback);
451 * Free the inode passed in, removing it from the lists it is still connected
452 * to. We remove any pages still attached to the inode and wait for any IO that
453 * is still in progress before finally destroying the inode.
455 * An inode must already be marked I_FREEING so that we avoid the inode being
456 * moved back onto lists if we race with other code that manipulates the lists
457 * (e.g. writeback_single_inode). The caller is responsible for setting this.
459 * An inode must already be removed from the LRU list before being evicted from
460 * the cache. This should occur atomically with setting the I_FREEING state
461 * flag, so no inodes here should ever be on the LRU when being evicted.
463 static void evict(struct inode *inode)
465 const struct super_operations *op = inode->i_sb->s_op;
467 BUG_ON(!(inode->i_state & I_FREEING));
468 BUG_ON(!list_empty(&inode->i_lru));
470 inode_wb_list_del(inode);
471 inode_sb_list_del(inode);
473 if (op->evict_inode) {
474 op->evict_inode(inode);
476 if (inode->i_data.nrpages)
477 truncate_inode_pages(&inode->i_data, 0);
478 end_writeback(inode);
480 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
482 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
485 remove_inode_hash(inode);
487 spin_lock(&inode->i_lock);
488 wake_up_bit(&inode->i_state, __I_NEW);
489 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
490 spin_unlock(&inode->i_lock);
492 destroy_inode(inode);
496 * dispose_list - dispose of the contents of a local list
497 * @head: the head of the list to free
499 * Dispose-list gets a local list with local inodes in it, so it doesn't
500 * need to worry about list corruption and SMP locks.
502 static void dispose_list(struct list_head *head)
504 while (!list_empty(head)) {
507 inode = list_first_entry(head, struct inode, i_lru);
508 list_del_init(&inode->i_lru);
515 * evict_inodes - evict all evictable inodes for a superblock
516 * @sb: superblock to operate on
518 * Make sure that no inodes with zero refcount are retained. This is
519 * called by superblock shutdown after having MS_ACTIVE flag removed,
520 * so any inode reaching zero refcount during or after that call will
521 * be immediately evicted.
523 void evict_inodes(struct super_block *sb)
525 struct inode *inode, *next;
528 spin_lock(&inode_sb_list_lock);
529 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
530 if (atomic_read(&inode->i_count))
533 spin_lock(&inode->i_lock);
534 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
535 spin_unlock(&inode->i_lock);
539 inode->i_state |= I_FREEING;
540 inode_lru_list_del(inode);
541 spin_unlock(&inode->i_lock);
542 list_add(&inode->i_lru, &dispose);
544 spin_unlock(&inode_sb_list_lock);
546 dispose_list(&dispose);
549 * Cycle through iprune_sem to make sure any inode that prune_icache
550 * moved off the list before we took the lock has been fully torn
553 down_write(&iprune_sem);
554 up_write(&iprune_sem);
558 * invalidate_inodes - attempt to free all inodes on a superblock
559 * @sb: superblock to operate on
560 * @kill_dirty: flag to guide handling of dirty inodes
562 * Attempts to free all inodes for a given superblock. If there were any
563 * busy inodes return a non-zero value, else zero.
564 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
567 int invalidate_inodes(struct super_block *sb, bool kill_dirty)
570 struct inode *inode, *next;
573 spin_lock(&inode_sb_list_lock);
574 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
575 spin_lock(&inode->i_lock);
576 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
577 spin_unlock(&inode->i_lock);
580 if (inode->i_state & I_DIRTY && !kill_dirty) {
581 spin_unlock(&inode->i_lock);
585 if (atomic_read(&inode->i_count)) {
586 spin_unlock(&inode->i_lock);
591 inode->i_state |= I_FREEING;
592 inode_lru_list_del(inode);
593 spin_unlock(&inode->i_lock);
594 list_add(&inode->i_lru, &dispose);
596 spin_unlock(&inode_sb_list_lock);
598 dispose_list(&dispose);
603 static int can_unuse(struct inode *inode)
605 if (inode->i_state & ~I_REFERENCED)
607 if (inode_has_buffers(inode))
609 if (atomic_read(&inode->i_count))
611 if (inode->i_data.nrpages)
617 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
618 * temporary list and then are freed outside inode_lru_lock by dispose_list().
620 * Any inodes which are pinned purely because of attached pagecache have their
621 * pagecache removed. If the inode has metadata buffers attached to
622 * mapping->private_list then try to remove them.
624 * If the inode has the I_REFERENCED flag set, then it means that it has been
625 * used recently - the flag is set in iput_final(). When we encounter such an
626 * inode, clear the flag and move it to the back of the LRU so it gets another
627 * pass through the LRU before it gets reclaimed. This is necessary because of
628 * the fact we are doing lazy LRU updates to minimise lock contention so the
629 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
630 * with this flag set because they are the inodes that are out of order.
632 static void shrink_icache_sb(struct super_block *sb, int *nr_to_scan)
636 unsigned long reap = 0;
638 spin_lock(&inode_lru_lock);
639 for (nr_scanned = *nr_to_scan; nr_scanned >= 0; nr_scanned--) {
642 if (list_empty(&sb->s_inode_lru))
645 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
648 * we are inverting the inode_lru_lock/inode->i_lock here,
649 * so use a trylock. If we fail to get the lock, just move the
650 * inode to the back of the list so we don't spin on it.
652 if (!spin_trylock(&inode->i_lock)) {
653 list_move(&inode->i_lru, &sb->s_inode_lru);
658 * Referenced or dirty inodes are still in use. Give them
659 * another pass through the LRU as we canot reclaim them now.
661 if (atomic_read(&inode->i_count) ||
662 (inode->i_state & ~I_REFERENCED)) {
663 list_del_init(&inode->i_lru);
664 spin_unlock(&inode->i_lock);
665 sb->s_nr_inodes_unused--;
666 this_cpu_dec(nr_unused);
670 /* recently referenced inodes get one more pass */
671 if (inode->i_state & I_REFERENCED) {
672 inode->i_state &= ~I_REFERENCED;
673 list_move(&inode->i_lru, &sb->s_inode_lru);
674 spin_unlock(&inode->i_lock);
677 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
679 spin_unlock(&inode->i_lock);
680 spin_unlock(&inode_lru_lock);
681 if (remove_inode_buffers(inode))
682 reap += invalidate_mapping_pages(&inode->i_data,
685 spin_lock(&inode_lru_lock);
687 if (inode != list_entry(sb->s_inode_lru.next,
688 struct inode, i_lru))
689 continue; /* wrong inode or list_empty */
690 /* avoid lock inversions with trylock */
691 if (!spin_trylock(&inode->i_lock))
693 if (!can_unuse(inode)) {
694 spin_unlock(&inode->i_lock);
698 WARN_ON(inode->i_state & I_NEW);
699 inode->i_state |= I_FREEING;
700 spin_unlock(&inode->i_lock);
702 list_move(&inode->i_lru, &freeable);
703 sb->s_nr_inodes_unused--;
704 this_cpu_dec(nr_unused);
706 if (current_is_kswapd())
707 __count_vm_events(KSWAPD_INODESTEAL, reap);
709 __count_vm_events(PGINODESTEAL, reap);
710 spin_unlock(&inode_lru_lock);
711 *nr_to_scan = nr_scanned;
713 dispose_list(&freeable);
716 static void prune_icache(int count)
718 struct super_block *sb, *p = NULL;
720 int unused = inodes_stat.nr_unused;
724 if (unused == 0 || count == 0)
726 down_read(&iprune_sem);
730 prune_ratio = unused / count;
732 list_for_each_entry(sb, &super_blocks, s_list) {
733 if (list_empty(&sb->s_instances))
735 if (sb->s_nr_inodes_unused == 0)
738 /* Now, we reclaim unused dentrins with fairness.
739 * We reclaim them same percentage from each superblock.
740 * We calculate number of dentries to scan on this sb
741 * as follows, but the implementation is arranged to avoid
743 * number of dentries to scan on this sb =
744 * count * (number of dentries on this sb /
745 * number of dentries in the machine)
747 spin_unlock(&sb_lock);
748 if (prune_ratio != 1)
749 w_count = (sb->s_nr_inodes_unused / prune_ratio) + 1;
751 w_count = sb->s_nr_inodes_unused;
754 * We need to be sure this filesystem isn't being unmounted,
755 * otherwise we could race with generic_shutdown_super(), and
756 * end up holding a reference to an inode while the filesystem
757 * is unmounted. So we try to get s_umount, and make sure
760 if (down_read_trylock(&sb->s_umount)) {
761 if ((sb->s_root != NULL) &&
762 (!list_empty(&sb->s_dentry_lru))) {
763 shrink_icache_sb(sb, &w_count);
766 up_read(&sb->s_umount);
773 /* more work left to do? */
779 spin_unlock(&sb_lock);
780 up_read(&iprune_sem);
784 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
785 * "unused" means that no dentries are referring to the inodes: the files are
786 * not open and the dcache references to those inodes have already been
789 * This function is passed the number of inodes to scan, and it returns the
790 * total number of remaining possibly-reclaimable inodes.
792 static int shrink_icache_memory(struct shrinker *shrink,
793 struct shrink_control *sc)
795 int nr = sc->nr_to_scan;
796 gfp_t gfp_mask = sc->gfp_mask;
800 * Nasty deadlock avoidance. We may hold various FS locks,
801 * and we don't want to recurse into the FS that called us
802 * in clear_inode() and friends..
804 if (!(gfp_mask & __GFP_FS))
808 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
811 static struct shrinker icache_shrinker = {
812 .shrink = shrink_icache_memory,
813 .seeks = DEFAULT_SEEKS,
816 static void __wait_on_freeing_inode(struct inode *inode);
818 * Called with the inode lock held.
820 static struct inode *find_inode(struct super_block *sb,
821 struct hlist_head *head,
822 int (*test)(struct inode *, void *),
825 struct hlist_node *node;
826 struct inode *inode = NULL;
829 hlist_for_each_entry(inode, node, head, i_hash) {
830 spin_lock(&inode->i_lock);
831 if (inode->i_sb != sb) {
832 spin_unlock(&inode->i_lock);
835 if (!test(inode, data)) {
836 spin_unlock(&inode->i_lock);
839 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
840 __wait_on_freeing_inode(inode);
844 spin_unlock(&inode->i_lock);
851 * find_inode_fast is the fast path version of find_inode, see the comment at
852 * iget_locked for details.
854 static struct inode *find_inode_fast(struct super_block *sb,
855 struct hlist_head *head, unsigned long ino)
857 struct hlist_node *node;
858 struct inode *inode = NULL;
861 hlist_for_each_entry(inode, node, head, i_hash) {
862 spin_lock(&inode->i_lock);
863 if (inode->i_ino != ino) {
864 spin_unlock(&inode->i_lock);
867 if (inode->i_sb != sb) {
868 spin_unlock(&inode->i_lock);
871 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
872 __wait_on_freeing_inode(inode);
876 spin_unlock(&inode->i_lock);
883 * Each cpu owns a range of LAST_INO_BATCH numbers.
884 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
885 * to renew the exhausted range.
887 * This does not significantly increase overflow rate because every CPU can
888 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
889 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
890 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
891 * overflow rate by 2x, which does not seem too significant.
893 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
894 * error if st_ino won't fit in target struct field. Use 32bit counter
895 * here to attempt to avoid that.
897 #define LAST_INO_BATCH 1024
898 static DEFINE_PER_CPU(unsigned int, last_ino);
900 unsigned int get_next_ino(void)
902 unsigned int *p = &get_cpu_var(last_ino);
903 unsigned int res = *p;
906 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
907 static atomic_t shared_last_ino;
908 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
910 res = next - LAST_INO_BATCH;
915 put_cpu_var(last_ino);
918 EXPORT_SYMBOL(get_next_ino);
921 * new_inode - obtain an inode
924 * Allocates a new inode for given superblock. The default gfp_mask
925 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
926 * If HIGHMEM pages are unsuitable or it is known that pages allocated
927 * for the page cache are not reclaimable or migratable,
928 * mapping_set_gfp_mask() must be called with suitable flags on the
929 * newly created inode's mapping
932 struct inode *new_inode(struct super_block *sb)
936 spin_lock_prefetch(&inode_sb_list_lock);
938 inode = alloc_inode(sb);
940 spin_lock(&inode->i_lock);
942 spin_unlock(&inode->i_lock);
943 inode_sb_list_add(inode);
947 EXPORT_SYMBOL(new_inode);
950 * unlock_new_inode - clear the I_NEW state and wake up any waiters
951 * @inode: new inode to unlock
953 * Called when the inode is fully initialised to clear the new state of the
954 * inode and wake up anyone waiting for the inode to finish initialisation.
956 void unlock_new_inode(struct inode *inode)
958 #ifdef CONFIG_DEBUG_LOCK_ALLOC
959 if (S_ISDIR(inode->i_mode)) {
960 struct file_system_type *type = inode->i_sb->s_type;
962 /* Set new key only if filesystem hasn't already changed it */
963 if (!lockdep_match_class(&inode->i_mutex,
964 &type->i_mutex_key)) {
966 * ensure nobody is actually holding i_mutex
968 mutex_destroy(&inode->i_mutex);
969 mutex_init(&inode->i_mutex);
970 lockdep_set_class(&inode->i_mutex,
971 &type->i_mutex_dir_key);
975 spin_lock(&inode->i_lock);
976 WARN_ON(!(inode->i_state & I_NEW));
977 inode->i_state &= ~I_NEW;
978 wake_up_bit(&inode->i_state, __I_NEW);
979 spin_unlock(&inode->i_lock);
981 EXPORT_SYMBOL(unlock_new_inode);
984 * iget5_locked - obtain an inode from a mounted file system
985 * @sb: super block of file system
986 * @hashval: hash value (usually inode number) to get
987 * @test: callback used for comparisons between inodes
988 * @set: callback used to initialize a new struct inode
989 * @data: opaque data pointer to pass to @test and @set
991 * Search for the inode specified by @hashval and @data in the inode cache,
992 * and if present it is return it with an increased reference count. This is
993 * a generalized version of iget_locked() for file systems where the inode
994 * number is not sufficient for unique identification of an inode.
996 * If the inode is not in cache, allocate a new inode and return it locked,
997 * hashed, and with the I_NEW flag set. The file system gets to fill it in
998 * before unlocking it via unlock_new_inode().
1000 * Note both @test and @set are called with the inode_hash_lock held, so can't
1003 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1004 int (*test)(struct inode *, void *),
1005 int (*set)(struct inode *, void *), void *data)
1007 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1008 struct inode *inode;
1010 spin_lock(&inode_hash_lock);
1011 inode = find_inode(sb, head, test, data);
1012 spin_unlock(&inode_hash_lock);
1015 wait_on_inode(inode);
1019 inode = alloc_inode(sb);
1023 spin_lock(&inode_hash_lock);
1024 /* We released the lock, so.. */
1025 old = find_inode(sb, head, test, data);
1027 if (set(inode, data))
1030 spin_lock(&inode->i_lock);
1031 inode->i_state = I_NEW;
1032 hlist_add_head(&inode->i_hash, head);
1033 spin_unlock(&inode->i_lock);
1034 inode_sb_list_add(inode);
1035 spin_unlock(&inode_hash_lock);
1037 /* Return the locked inode with I_NEW set, the
1038 * caller is responsible for filling in the contents
1044 * Uhhuh, somebody else created the same inode under
1045 * us. Use the old inode instead of the one we just
1048 spin_unlock(&inode_hash_lock);
1049 destroy_inode(inode);
1051 wait_on_inode(inode);
1056 spin_unlock(&inode_hash_lock);
1057 destroy_inode(inode);
1060 EXPORT_SYMBOL(iget5_locked);
1063 * iget_locked - obtain an inode from a mounted file system
1064 * @sb: super block of file system
1065 * @ino: inode number to get
1067 * Search for the inode specified by @ino in the inode cache and if present
1068 * return it with an increased reference count. This is for file systems
1069 * where the inode number is sufficient for unique identification of an inode.
1071 * If the inode is not in cache, allocate a new inode and return it locked,
1072 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1073 * before unlocking it via unlock_new_inode().
1075 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1077 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1078 struct inode *inode;
1080 spin_lock(&inode_hash_lock);
1081 inode = find_inode_fast(sb, head, ino);
1082 spin_unlock(&inode_hash_lock);
1084 wait_on_inode(inode);
1088 inode = alloc_inode(sb);
1092 spin_lock(&inode_hash_lock);
1093 /* We released the lock, so.. */
1094 old = find_inode_fast(sb, head, ino);
1097 spin_lock(&inode->i_lock);
1098 inode->i_state = I_NEW;
1099 hlist_add_head(&inode->i_hash, head);
1100 spin_unlock(&inode->i_lock);
1101 inode_sb_list_add(inode);
1102 spin_unlock(&inode_hash_lock);
1104 /* Return the locked inode with I_NEW set, the
1105 * caller is responsible for filling in the contents
1111 * Uhhuh, somebody else created the same inode under
1112 * us. Use the old inode instead of the one we just
1115 spin_unlock(&inode_hash_lock);
1116 destroy_inode(inode);
1118 wait_on_inode(inode);
1122 EXPORT_SYMBOL(iget_locked);
1125 * search the inode cache for a matching inode number.
1126 * If we find one, then the inode number we are trying to
1127 * allocate is not unique and so we should not use it.
1129 * Returns 1 if the inode number is unique, 0 if it is not.
1131 static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1133 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1134 struct hlist_node *node;
1135 struct inode *inode;
1137 spin_lock(&inode_hash_lock);
1138 hlist_for_each_entry(inode, node, b, i_hash) {
1139 if (inode->i_ino == ino && inode->i_sb == sb) {
1140 spin_unlock(&inode_hash_lock);
1144 spin_unlock(&inode_hash_lock);
1150 * iunique - get a unique inode number
1152 * @max_reserved: highest reserved inode number
1154 * Obtain an inode number that is unique on the system for a given
1155 * superblock. This is used by file systems that have no natural
1156 * permanent inode numbering system. An inode number is returned that
1157 * is higher than the reserved limit but unique.
1160 * With a large number of inodes live on the file system this function
1161 * currently becomes quite slow.
1163 ino_t iunique(struct super_block *sb, ino_t max_reserved)
1166 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1167 * error if st_ino won't fit in target struct field. Use 32bit counter
1168 * here to attempt to avoid that.
1170 static DEFINE_SPINLOCK(iunique_lock);
1171 static unsigned int counter;
1174 spin_lock(&iunique_lock);
1176 if (counter <= max_reserved)
1177 counter = max_reserved + 1;
1179 } while (!test_inode_iunique(sb, res));
1180 spin_unlock(&iunique_lock);
1184 EXPORT_SYMBOL(iunique);
1186 struct inode *igrab(struct inode *inode)
1188 spin_lock(&inode->i_lock);
1189 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1191 spin_unlock(&inode->i_lock);
1193 spin_unlock(&inode->i_lock);
1195 * Handle the case where s_op->clear_inode is not been
1196 * called yet, and somebody is calling igrab
1197 * while the inode is getting freed.
1203 EXPORT_SYMBOL(igrab);
1206 * ilookup5_nowait - search for an inode in the inode cache
1207 * @sb: super block of file system to search
1208 * @hashval: hash value (usually inode number) to search for
1209 * @test: callback used for comparisons between inodes
1210 * @data: opaque data pointer to pass to @test
1212 * Search for the inode specified by @hashval and @data in the inode cache.
1213 * If the inode is in the cache, the inode is returned with an incremented
1216 * Note: I_NEW is not waited upon so you have to be very careful what you do
1217 * with the returned inode. You probably should be using ilookup5() instead.
1219 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1221 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1222 int (*test)(struct inode *, void *), void *data)
1224 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1225 struct inode *inode;
1227 spin_lock(&inode_hash_lock);
1228 inode = find_inode(sb, head, test, data);
1229 spin_unlock(&inode_hash_lock);
1233 EXPORT_SYMBOL(ilookup5_nowait);
1236 * ilookup5 - search for an inode in the inode cache
1237 * @sb: super block of file system to search
1238 * @hashval: hash value (usually inode number) to search for
1239 * @test: callback used for comparisons between inodes
1240 * @data: opaque data pointer to pass to @test
1242 * Search for the inode specified by @hashval and @data in the inode cache,
1243 * and if the inode is in the cache, return the inode with an incremented
1244 * reference count. Waits on I_NEW before returning the inode.
1245 * returned with an incremented reference count.
1247 * This is a generalized version of ilookup() for file systems where the
1248 * inode number is not sufficient for unique identification of an inode.
1250 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1252 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1253 int (*test)(struct inode *, void *), void *data)
1255 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1258 wait_on_inode(inode);
1261 EXPORT_SYMBOL(ilookup5);
1264 * ilookup - search for an inode in the inode cache
1265 * @sb: super block of file system to search
1266 * @ino: inode number to search for
1268 * Search for the inode @ino in the inode cache, and if the inode is in the
1269 * cache, the inode is returned with an incremented reference count.
1271 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1273 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1274 struct inode *inode;
1276 spin_lock(&inode_hash_lock);
1277 inode = find_inode_fast(sb, head, ino);
1278 spin_unlock(&inode_hash_lock);
1281 wait_on_inode(inode);
1284 EXPORT_SYMBOL(ilookup);
1286 int insert_inode_locked(struct inode *inode)
1288 struct super_block *sb = inode->i_sb;
1289 ino_t ino = inode->i_ino;
1290 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1293 struct hlist_node *node;
1294 struct inode *old = NULL;
1295 spin_lock(&inode_hash_lock);
1296 hlist_for_each_entry(old, node, head, i_hash) {
1297 if (old->i_ino != ino)
1299 if (old->i_sb != sb)
1301 spin_lock(&old->i_lock);
1302 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1303 spin_unlock(&old->i_lock);
1308 if (likely(!node)) {
1309 spin_lock(&inode->i_lock);
1310 inode->i_state |= I_NEW;
1311 hlist_add_head(&inode->i_hash, head);
1312 spin_unlock(&inode->i_lock);
1313 spin_unlock(&inode_hash_lock);
1317 spin_unlock(&old->i_lock);
1318 spin_unlock(&inode_hash_lock);
1320 if (unlikely(!inode_unhashed(old))) {
1327 EXPORT_SYMBOL(insert_inode_locked);
1329 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1330 int (*test)(struct inode *, void *), void *data)
1332 struct super_block *sb = inode->i_sb;
1333 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1336 struct hlist_node *node;
1337 struct inode *old = NULL;
1339 spin_lock(&inode_hash_lock);
1340 hlist_for_each_entry(old, node, head, i_hash) {
1341 if (old->i_sb != sb)
1343 if (!test(old, data))
1345 spin_lock(&old->i_lock);
1346 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1347 spin_unlock(&old->i_lock);
1352 if (likely(!node)) {
1353 spin_lock(&inode->i_lock);
1354 inode->i_state |= I_NEW;
1355 hlist_add_head(&inode->i_hash, head);
1356 spin_unlock(&inode->i_lock);
1357 spin_unlock(&inode_hash_lock);
1361 spin_unlock(&old->i_lock);
1362 spin_unlock(&inode_hash_lock);
1364 if (unlikely(!inode_unhashed(old))) {
1371 EXPORT_SYMBOL(insert_inode_locked4);
1374 int generic_delete_inode(struct inode *inode)
1378 EXPORT_SYMBOL(generic_delete_inode);
1381 * Normal UNIX filesystem behaviour: delete the
1382 * inode when the usage count drops to zero, and
1385 int generic_drop_inode(struct inode *inode)
1387 return !inode->i_nlink || inode_unhashed(inode);
1389 EXPORT_SYMBOL_GPL(generic_drop_inode);
1392 * Called when we're dropping the last reference
1395 * Call the FS "drop_inode()" function, defaulting to
1396 * the legacy UNIX filesystem behaviour. If it tells
1397 * us to evict inode, do so. Otherwise, retain inode
1398 * in cache if fs is alive, sync and evict if fs is
1401 static void iput_final(struct inode *inode)
1403 struct super_block *sb = inode->i_sb;
1404 const struct super_operations *op = inode->i_sb->s_op;
1407 WARN_ON(inode->i_state & I_NEW);
1410 drop = op->drop_inode(inode);
1412 drop = generic_drop_inode(inode);
1414 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1415 inode->i_state |= I_REFERENCED;
1416 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1417 inode_lru_list_add(inode);
1418 spin_unlock(&inode->i_lock);
1423 inode->i_state |= I_WILL_FREE;
1424 spin_unlock(&inode->i_lock);
1425 write_inode_now(inode, 1);
1426 spin_lock(&inode->i_lock);
1427 WARN_ON(inode->i_state & I_NEW);
1428 inode->i_state &= ~I_WILL_FREE;
1431 inode->i_state |= I_FREEING;
1432 inode_lru_list_del(inode);
1433 spin_unlock(&inode->i_lock);
1439 * iput - put an inode
1440 * @inode: inode to put
1442 * Puts an inode, dropping its usage count. If the inode use count hits
1443 * zero, the inode is then freed and may also be destroyed.
1445 * Consequently, iput() can sleep.
1447 void iput(struct inode *inode)
1450 BUG_ON(inode->i_state & I_CLEAR);
1452 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
1456 EXPORT_SYMBOL(iput);
1459 * bmap - find a block number in a file
1460 * @inode: inode of file
1461 * @block: block to find
1463 * Returns the block number on the device holding the inode that
1464 * is the disk block number for the block of the file requested.
1465 * That is, asked for block 4 of inode 1 the function will return the
1466 * disk block relative to the disk start that holds that block of the
1469 sector_t bmap(struct inode *inode, sector_t block)
1472 if (inode->i_mapping->a_ops->bmap)
1473 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1476 EXPORT_SYMBOL(bmap);
1479 * With relative atime, only update atime if the previous atime is
1480 * earlier than either the ctime or mtime or if at least a day has
1481 * passed since the last atime update.
1483 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1484 struct timespec now)
1487 if (!(mnt->mnt_flags & MNT_RELATIME))
1490 * Is mtime younger than atime? If yes, update atime:
1492 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1495 * Is ctime younger than atime? If yes, update atime:
1497 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1501 * Is the previous atime value older than a day? If yes,
1504 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1507 * Good, we can skip the atime update:
1513 * touch_atime - update the access time
1514 * @mnt: mount the inode is accessed on
1515 * @dentry: dentry accessed
1517 * Update the accessed time on an inode and mark it for writeback.
1518 * This function automatically handles read only file systems and media,
1519 * as well as the "noatime" flag and inode specific "noatime" markers.
1521 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1523 struct inode *inode = dentry->d_inode;
1524 struct timespec now;
1526 if (inode->i_flags & S_NOATIME)
1528 if (IS_NOATIME(inode))
1530 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1533 if (mnt->mnt_flags & MNT_NOATIME)
1535 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1538 now = current_fs_time(inode->i_sb);
1540 if (!relatime_need_update(mnt, inode, now))
1543 if (timespec_equal(&inode->i_atime, &now))
1546 if (mnt_want_write(mnt))
1549 inode->i_atime = now;
1550 mark_inode_dirty_sync(inode);
1551 mnt_drop_write(mnt);
1553 EXPORT_SYMBOL(touch_atime);
1556 * file_update_time - update mtime and ctime time
1557 * @file: file accessed
1559 * Update the mtime and ctime members of an inode and mark the inode
1560 * for writeback. Note that this function is meant exclusively for
1561 * usage in the file write path of filesystems, and filesystems may
1562 * choose to explicitly ignore update via this function with the
1563 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1564 * timestamps are handled by the server.
1567 void file_update_time(struct file *file)
1569 struct inode *inode = file->f_path.dentry->d_inode;
1570 struct timespec now;
1571 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1573 /* First try to exhaust all avenues to not sync */
1574 if (IS_NOCMTIME(inode))
1577 now = current_fs_time(inode->i_sb);
1578 if (!timespec_equal(&inode->i_mtime, &now))
1581 if (!timespec_equal(&inode->i_ctime, &now))
1584 if (IS_I_VERSION(inode))
1585 sync_it |= S_VERSION;
1590 /* Finally allowed to write? Takes lock. */
1591 if (mnt_want_write_file(file))
1594 /* Only change inode inside the lock region */
1595 if (sync_it & S_VERSION)
1596 inode_inc_iversion(inode);
1597 if (sync_it & S_CTIME)
1598 inode->i_ctime = now;
1599 if (sync_it & S_MTIME)
1600 inode->i_mtime = now;
1601 mark_inode_dirty_sync(inode);
1602 mnt_drop_write(file->f_path.mnt);
1604 EXPORT_SYMBOL(file_update_time);
1606 int inode_needs_sync(struct inode *inode)
1610 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1614 EXPORT_SYMBOL(inode_needs_sync);
1616 int inode_wait(void *word)
1621 EXPORT_SYMBOL(inode_wait);
1624 * If we try to find an inode in the inode hash while it is being
1625 * deleted, we have to wait until the filesystem completes its
1626 * deletion before reporting that it isn't found. This function waits
1627 * until the deletion _might_ have completed. Callers are responsible
1628 * to recheck inode state.
1630 * It doesn't matter if I_NEW is not set initially, a call to
1631 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1634 static void __wait_on_freeing_inode(struct inode *inode)
1636 wait_queue_head_t *wq;
1637 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1638 wq = bit_waitqueue(&inode->i_state, __I_NEW);
1639 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1640 spin_unlock(&inode->i_lock);
1641 spin_unlock(&inode_hash_lock);
1643 finish_wait(wq, &wait.wait);
1644 spin_lock(&inode_hash_lock);
1647 static __initdata unsigned long ihash_entries;
1648 static int __init set_ihash_entries(char *str)
1652 ihash_entries = simple_strtoul(str, &str, 0);
1655 __setup("ihash_entries=", set_ihash_entries);
1658 * Initialize the waitqueues and inode hash table.
1660 void __init inode_init_early(void)
1664 /* If hashes are distributed across NUMA nodes, defer
1665 * hash allocation until vmalloc space is available.
1671 alloc_large_system_hash("Inode-cache",
1672 sizeof(struct hlist_head),
1680 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1681 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1684 void __init inode_init(void)
1688 /* inode slab cache */
1689 inode_cachep = kmem_cache_create("inode_cache",
1690 sizeof(struct inode),
1692 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1695 register_shrinker(&icache_shrinker);
1697 /* Hash may have been set up in inode_init_early */
1702 alloc_large_system_hash("Inode-cache",
1703 sizeof(struct hlist_head),
1711 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1712 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1715 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1717 inode->i_mode = mode;
1718 if (S_ISCHR(mode)) {
1719 inode->i_fop = &def_chr_fops;
1720 inode->i_rdev = rdev;
1721 } else if (S_ISBLK(mode)) {
1722 inode->i_fop = &def_blk_fops;
1723 inode->i_rdev = rdev;
1724 } else if (S_ISFIFO(mode))
1725 inode->i_fop = &def_fifo_fops;
1726 else if (S_ISSOCK(mode))
1727 inode->i_fop = &bad_sock_fops;
1729 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1730 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1733 EXPORT_SYMBOL(init_special_inode);
1736 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
1738 * @dir: Directory inode
1739 * @mode: mode of the new inode
1741 void inode_init_owner(struct inode *inode, const struct inode *dir,
1744 inode->i_uid = current_fsuid();
1745 if (dir && dir->i_mode & S_ISGID) {
1746 inode->i_gid = dir->i_gid;
1750 inode->i_gid = current_fsgid();
1751 inode->i_mode = mode;
1753 EXPORT_SYMBOL(inode_init_owner);
1756 * inode_owner_or_capable - check current task permissions to inode
1757 * @inode: inode being checked
1759 * Return true if current either has CAP_FOWNER to the inode, or
1762 bool inode_owner_or_capable(const struct inode *inode)
1764 struct user_namespace *ns = inode_userns(inode);
1766 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1768 if (ns_capable(ns, CAP_FOWNER))
1772 EXPORT_SYMBOL(inode_owner_or_capable);