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 * 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
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 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(inode_wb_list_lock
);
71 * iprune_sem provides exclusion between the icache shrinking and the
74 * We don't actually need it to protect anything in the umount path,
75 * but only need to cycle through it to make sure any inode that
76 * prune_icache_sb took off the LRU list has been fully torn down by the
77 * time we are past evict_inodes.
79 static DECLARE_RWSEM(iprune_sem
);
82 * Empty aops. Can be used for the cases where the user does not
83 * define any of the address_space operations.
85 const struct address_space_operations empty_aops
= {
87 EXPORT_SYMBOL(empty_aops
);
90 * Statistics gathering..
92 struct inodes_stat_t inodes_stat
;
94 static DEFINE_PER_CPU(unsigned int, nr_inodes
);
95 static DEFINE_PER_CPU(unsigned int, nr_unused
);
97 static struct kmem_cache
*inode_cachep __read_mostly
;
99 static int get_nr_inodes(void)
103 for_each_possible_cpu(i
)
104 sum
+= per_cpu(nr_inodes
, i
);
105 return sum
< 0 ? 0 : sum
;
108 static inline int get_nr_inodes_unused(void)
112 for_each_possible_cpu(i
)
113 sum
+= per_cpu(nr_unused
, i
);
114 return sum
< 0 ? 0 : sum
;
117 int get_nr_dirty_inodes(void)
119 /* not actually dirty inodes, but a wild approximation */
120 int nr_dirty
= get_nr_inodes() - get_nr_inodes_unused();
121 return nr_dirty
> 0 ? nr_dirty
: 0;
125 * Handle nr_inode sysctl
128 int proc_nr_inodes(ctl_table
*table
, int write
,
129 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
131 inodes_stat
.nr_inodes
= get_nr_inodes();
132 inodes_stat
.nr_unused
= get_nr_inodes_unused();
133 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
138 * inode_init_always - perform inode structure intialisation
139 * @sb: superblock inode belongs to
140 * @inode: inode to initialise
142 * These are initializations that need to be done on every inode
143 * allocation as the fields are not initialised by slab allocation.
145 int inode_init_always(struct super_block
*sb
, struct inode
*inode
)
147 static const struct inode_operations empty_iops
;
148 static const struct file_operations empty_fops
;
149 struct address_space
*const mapping
= &inode
->i_data
;
152 inode
->i_blkbits
= sb
->s_blocksize_bits
;
154 atomic_set(&inode
->i_count
, 1);
155 inode
->i_op
= &empty_iops
;
156 inode
->i_fop
= &empty_fops
;
160 atomic_set(&inode
->i_writecount
, 0);
164 inode
->i_generation
= 0;
166 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
168 inode
->i_pipe
= NULL
;
169 inode
->i_bdev
= NULL
;
170 inode
->i_cdev
= NULL
;
172 inode
->dirtied_when
= 0;
174 if (security_inode_alloc(inode
))
176 spin_lock_init(&inode
->i_lock
);
177 lockdep_set_class(&inode
->i_lock
, &sb
->s_type
->i_lock_key
);
179 mutex_init(&inode
->i_mutex
);
180 lockdep_set_class(&inode
->i_mutex
, &sb
->s_type
->i_mutex_key
);
182 init_rwsem(&inode
->i_alloc_sem
);
183 lockdep_set_class(&inode
->i_alloc_sem
, &sb
->s_type
->i_alloc_sem_key
);
185 mapping
->a_ops
= &empty_aops
;
186 mapping
->host
= inode
;
188 mapping_set_gfp_mask(mapping
, GFP_HIGHUSER_MOVABLE
);
189 mapping
->assoc_mapping
= NULL
;
190 mapping
->backing_dev_info
= &default_backing_dev_info
;
191 mapping
->writeback_index
= 0;
194 * If the block_device provides a backing_dev_info for client
195 * inodes then use that. Otherwise the inode share the bdev's
199 struct backing_dev_info
*bdi
;
201 bdi
= sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
202 mapping
->backing_dev_info
= bdi
;
204 inode
->i_private
= NULL
;
205 inode
->i_mapping
= mapping
;
206 #ifdef CONFIG_FS_POSIX_ACL
207 inode
->i_acl
= inode
->i_default_acl
= ACL_NOT_CACHED
;
210 #ifdef CONFIG_FSNOTIFY
211 inode
->i_fsnotify_mask
= 0;
214 this_cpu_inc(nr_inodes
);
220 EXPORT_SYMBOL(inode_init_always
);
222 static struct inode
*alloc_inode(struct super_block
*sb
)
226 if (sb
->s_op
->alloc_inode
)
227 inode
= sb
->s_op
->alloc_inode(sb
);
229 inode
= kmem_cache_alloc(inode_cachep
, GFP_KERNEL
);
234 if (unlikely(inode_init_always(sb
, inode
))) {
235 if (inode
->i_sb
->s_op
->destroy_inode
)
236 inode
->i_sb
->s_op
->destroy_inode(inode
);
238 kmem_cache_free(inode_cachep
, inode
);
245 void free_inode_nonrcu(struct inode
*inode
)
247 kmem_cache_free(inode_cachep
, inode
);
249 EXPORT_SYMBOL(free_inode_nonrcu
);
251 void __destroy_inode(struct inode
*inode
)
253 BUG_ON(inode_has_buffers(inode
));
254 security_inode_free(inode
);
255 fsnotify_inode_delete(inode
);
256 #ifdef CONFIG_FS_POSIX_ACL
257 if (inode
->i_acl
&& inode
->i_acl
!= ACL_NOT_CACHED
)
258 posix_acl_release(inode
->i_acl
);
259 if (inode
->i_default_acl
&& inode
->i_default_acl
!= ACL_NOT_CACHED
)
260 posix_acl_release(inode
->i_default_acl
);
262 this_cpu_dec(nr_inodes
);
264 EXPORT_SYMBOL(__destroy_inode
);
266 static void i_callback(struct rcu_head
*head
)
268 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
269 INIT_LIST_HEAD(&inode
->i_dentry
);
270 kmem_cache_free(inode_cachep
, inode
);
273 static void destroy_inode(struct inode
*inode
)
275 BUG_ON(!list_empty(&inode
->i_lru
));
276 __destroy_inode(inode
);
277 if (inode
->i_sb
->s_op
->destroy_inode
)
278 inode
->i_sb
->s_op
->destroy_inode(inode
);
280 call_rcu(&inode
->i_rcu
, i_callback
);
283 void address_space_init_once(struct address_space
*mapping
)
285 memset(mapping
, 0, sizeof(*mapping
));
286 INIT_RADIX_TREE(&mapping
->page_tree
, GFP_ATOMIC
);
287 spin_lock_init(&mapping
->tree_lock
);
288 mutex_init(&mapping
->i_mmap_mutex
);
289 INIT_LIST_HEAD(&mapping
->private_list
);
290 spin_lock_init(&mapping
->private_lock
);
291 INIT_RAW_PRIO_TREE_ROOT(&mapping
->i_mmap
);
292 INIT_LIST_HEAD(&mapping
->i_mmap_nonlinear
);
294 EXPORT_SYMBOL(address_space_init_once
);
297 * These are initializations that only need to be done
298 * once, because the fields are idempotent across use
299 * of the inode, so let the slab aware of that.
301 void inode_init_once(struct inode
*inode
)
303 memset(inode
, 0, sizeof(*inode
));
304 INIT_HLIST_NODE(&inode
->i_hash
);
305 INIT_LIST_HEAD(&inode
->i_dentry
);
306 INIT_LIST_HEAD(&inode
->i_devices
);
307 INIT_LIST_HEAD(&inode
->i_wb_list
);
308 INIT_LIST_HEAD(&inode
->i_lru
);
309 address_space_init_once(&inode
->i_data
);
310 i_size_ordered_init(inode
);
311 #ifdef CONFIG_FSNOTIFY
312 INIT_HLIST_HEAD(&inode
->i_fsnotify_marks
);
315 EXPORT_SYMBOL(inode_init_once
);
317 static void init_once(void *foo
)
319 struct inode
*inode
= (struct inode
*) foo
;
321 inode_init_once(inode
);
325 * inode->i_lock must be held
327 void __iget(struct inode
*inode
)
329 atomic_inc(&inode
->i_count
);
333 * get additional reference to inode; caller must already hold one.
335 void ihold(struct inode
*inode
)
337 WARN_ON(atomic_inc_return(&inode
->i_count
) < 2);
339 EXPORT_SYMBOL(ihold
);
341 static void inode_lru_list_add(struct inode
*inode
)
343 spin_lock(&inode
->i_sb
->s_inode_lru_lock
);
344 if (list_empty(&inode
->i_lru
)) {
345 list_add(&inode
->i_lru
, &inode
->i_sb
->s_inode_lru
);
346 inode
->i_sb
->s_nr_inodes_unused
++;
347 this_cpu_inc(nr_unused
);
349 spin_unlock(&inode
->i_sb
->s_inode_lru_lock
);
352 static void inode_lru_list_del(struct inode
*inode
)
354 spin_lock(&inode
->i_sb
->s_inode_lru_lock
);
355 if (!list_empty(&inode
->i_lru
)) {
356 list_del_init(&inode
->i_lru
);
357 inode
->i_sb
->s_nr_inodes_unused
--;
358 this_cpu_dec(nr_unused
);
360 spin_unlock(&inode
->i_sb
->s_inode_lru_lock
);
364 * inode_sb_list_add - add inode to the superblock list of inodes
365 * @inode: inode to add
367 void inode_sb_list_add(struct inode
*inode
)
369 spin_lock(&inode_sb_list_lock
);
370 list_add(&inode
->i_sb_list
, &inode
->i_sb
->s_inodes
);
371 spin_unlock(&inode_sb_list_lock
);
373 EXPORT_SYMBOL_GPL(inode_sb_list_add
);
375 static inline void inode_sb_list_del(struct inode
*inode
)
377 spin_lock(&inode_sb_list_lock
);
378 list_del_init(&inode
->i_sb_list
);
379 spin_unlock(&inode_sb_list_lock
);
382 static unsigned long hash(struct super_block
*sb
, unsigned long hashval
)
386 tmp
= (hashval
* (unsigned long)sb
) ^ (GOLDEN_RATIO_PRIME
+ hashval
) /
388 tmp
= tmp
^ ((tmp
^ GOLDEN_RATIO_PRIME
) >> i_hash_shift
);
389 return tmp
& i_hash_mask
;
393 * __insert_inode_hash - hash an inode
394 * @inode: unhashed inode
395 * @hashval: unsigned long value used to locate this object in the
398 * Add an inode to the inode hash for this superblock.
400 void __insert_inode_hash(struct inode
*inode
, unsigned long hashval
)
402 struct hlist_head
*b
= inode_hashtable
+ hash(inode
->i_sb
, hashval
);
404 spin_lock(&inode_hash_lock
);
405 spin_lock(&inode
->i_lock
);
406 hlist_add_head(&inode
->i_hash
, b
);
407 spin_unlock(&inode
->i_lock
);
408 spin_unlock(&inode_hash_lock
);
410 EXPORT_SYMBOL(__insert_inode_hash
);
413 * remove_inode_hash - remove an inode from the hash
414 * @inode: inode to unhash
416 * Remove an inode from the superblock.
418 void remove_inode_hash(struct inode
*inode
)
420 spin_lock(&inode_hash_lock
);
421 spin_lock(&inode
->i_lock
);
422 hlist_del_init(&inode
->i_hash
);
423 spin_unlock(&inode
->i_lock
);
424 spin_unlock(&inode_hash_lock
);
426 EXPORT_SYMBOL(remove_inode_hash
);
428 void end_writeback(struct inode
*inode
)
432 * We have to cycle tree_lock here because reclaim can be still in the
433 * process of removing the last page (in __delete_from_page_cache())
434 * and we must not free mapping under it.
436 spin_lock_irq(&inode
->i_data
.tree_lock
);
437 BUG_ON(inode
->i_data
.nrpages
);
438 spin_unlock_irq(&inode
->i_data
.tree_lock
);
439 BUG_ON(!list_empty(&inode
->i_data
.private_list
));
440 BUG_ON(!(inode
->i_state
& I_FREEING
));
441 BUG_ON(inode
->i_state
& I_CLEAR
);
442 inode_sync_wait(inode
);
443 /* don't need i_lock here, no concurrent mods to i_state */
444 inode
->i_state
= I_FREEING
| I_CLEAR
;
446 EXPORT_SYMBOL(end_writeback
);
449 * Free the inode passed in, removing it from the lists it is still connected
450 * to. We remove any pages still attached to the inode and wait for any IO that
451 * is still in progress before finally destroying the inode.
453 * An inode must already be marked I_FREEING so that we avoid the inode being
454 * moved back onto lists if we race with other code that manipulates the lists
455 * (e.g. writeback_single_inode). The caller is responsible for setting this.
457 * An inode must already be removed from the LRU list before being evicted from
458 * the cache. This should occur atomically with setting the I_FREEING state
459 * flag, so no inodes here should ever be on the LRU when being evicted.
461 static void evict(struct inode
*inode
)
463 const struct super_operations
*op
= inode
->i_sb
->s_op
;
465 BUG_ON(!(inode
->i_state
& I_FREEING
));
466 BUG_ON(!list_empty(&inode
->i_lru
));
468 inode_wb_list_del(inode
);
469 inode_sb_list_del(inode
);
471 if (op
->evict_inode
) {
472 op
->evict_inode(inode
);
474 if (inode
->i_data
.nrpages
)
475 truncate_inode_pages(&inode
->i_data
, 0);
476 end_writeback(inode
);
478 if (S_ISBLK(inode
->i_mode
) && inode
->i_bdev
)
480 if (S_ISCHR(inode
->i_mode
) && inode
->i_cdev
)
483 remove_inode_hash(inode
);
485 spin_lock(&inode
->i_lock
);
486 wake_up_bit(&inode
->i_state
, __I_NEW
);
487 BUG_ON(inode
->i_state
!= (I_FREEING
| I_CLEAR
));
488 spin_unlock(&inode
->i_lock
);
490 destroy_inode(inode
);
494 * dispose_list - dispose of the contents of a local list
495 * @head: the head of the list to free
497 * Dispose-list gets a local list with local inodes in it, so it doesn't
498 * need to worry about list corruption and SMP locks.
500 static void dispose_list(struct list_head
*head
)
502 while (!list_empty(head
)) {
505 inode
= list_first_entry(head
, struct inode
, i_lru
);
506 list_del_init(&inode
->i_lru
);
513 * evict_inodes - evict all evictable inodes for a superblock
514 * @sb: superblock to operate on
516 * Make sure that no inodes with zero refcount are retained. This is
517 * called by superblock shutdown after having MS_ACTIVE flag removed,
518 * so any inode reaching zero refcount during or after that call will
519 * be immediately evicted.
521 void evict_inodes(struct super_block
*sb
)
523 struct inode
*inode
, *next
;
526 spin_lock(&inode_sb_list_lock
);
527 list_for_each_entry_safe(inode
, next
, &sb
->s_inodes
, i_sb_list
) {
528 if (atomic_read(&inode
->i_count
))
531 spin_lock(&inode
->i_lock
);
532 if (inode
->i_state
& (I_NEW
| I_FREEING
| I_WILL_FREE
)) {
533 spin_unlock(&inode
->i_lock
);
537 inode
->i_state
|= I_FREEING
;
538 inode_lru_list_del(inode
);
539 spin_unlock(&inode
->i_lock
);
540 list_add(&inode
->i_lru
, &dispose
);
542 spin_unlock(&inode_sb_list_lock
);
544 dispose_list(&dispose
);
547 * Cycle through iprune_sem to make sure any inode that prune_icache_sb
548 * moved off the list before we took the lock has been fully torn
551 down_write(&iprune_sem
);
552 up_write(&iprune_sem
);
556 * invalidate_inodes - attempt to free all inodes on a superblock
557 * @sb: superblock to operate on
558 * @kill_dirty: flag to guide handling of dirty inodes
560 * Attempts to free all inodes for a given superblock. If there were any
561 * busy inodes return a non-zero value, else zero.
562 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
565 int invalidate_inodes(struct super_block
*sb
, bool kill_dirty
)
568 struct inode
*inode
, *next
;
571 spin_lock(&inode_sb_list_lock
);
572 list_for_each_entry_safe(inode
, next
, &sb
->s_inodes
, i_sb_list
) {
573 spin_lock(&inode
->i_lock
);
574 if (inode
->i_state
& (I_NEW
| I_FREEING
| I_WILL_FREE
)) {
575 spin_unlock(&inode
->i_lock
);
578 if (inode
->i_state
& I_DIRTY
&& !kill_dirty
) {
579 spin_unlock(&inode
->i_lock
);
583 if (atomic_read(&inode
->i_count
)) {
584 spin_unlock(&inode
->i_lock
);
589 inode
->i_state
|= I_FREEING
;
590 inode_lru_list_del(inode
);
591 spin_unlock(&inode
->i_lock
);
592 list_add(&inode
->i_lru
, &dispose
);
594 spin_unlock(&inode_sb_list_lock
);
596 dispose_list(&dispose
);
601 static int can_unuse(struct inode
*inode
)
603 if (inode
->i_state
& ~I_REFERENCED
)
605 if (inode_has_buffers(inode
))
607 if (atomic_read(&inode
->i_count
))
609 if (inode
->i_data
.nrpages
)
615 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
616 * This is called from the superblock shrinker function with a number of inodes
617 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
618 * then are freed outside inode_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 void prune_icache_sb(struct super_block
*sb
, int nr_to_scan
)
636 unsigned long reap
= 0;
638 down_read(&iprune_sem
);
639 spin_lock(&sb
->s_inode_lru_lock
);
640 for (nr_scanned
= nr_to_scan
; nr_scanned
>= 0; nr_scanned
--) {
643 if (list_empty(&sb
->s_inode_lru
))
646 inode
= list_entry(sb
->s_inode_lru
.prev
, struct inode
, i_lru
);
649 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
650 * so use a trylock. If we fail to get the lock, just move the
651 * inode to the back of the list so we don't spin on it.
653 if (!spin_trylock(&inode
->i_lock
)) {
654 list_move(&inode
->i_lru
, &sb
->s_inode_lru
);
659 * Referenced or dirty inodes are still in use. Give them
660 * another pass through the LRU as we canot reclaim them now.
662 if (atomic_read(&inode
->i_count
) ||
663 (inode
->i_state
& ~I_REFERENCED
)) {
664 list_del_init(&inode
->i_lru
);
665 spin_unlock(&inode
->i_lock
);
666 sb
->s_nr_inodes_unused
--;
667 this_cpu_dec(nr_unused
);
671 /* recently referenced inodes get one more pass */
672 if (inode
->i_state
& I_REFERENCED
) {
673 inode
->i_state
&= ~I_REFERENCED
;
674 list_move(&inode
->i_lru
, &sb
->s_inode_lru
);
675 spin_unlock(&inode
->i_lock
);
678 if (inode_has_buffers(inode
) || inode
->i_data
.nrpages
) {
680 spin_unlock(&inode
->i_lock
);
681 spin_unlock(&sb
->s_inode_lru_lock
);
682 if (remove_inode_buffers(inode
))
683 reap
+= invalidate_mapping_pages(&inode
->i_data
,
686 spin_lock(&sb
->s_inode_lru_lock
);
688 if (inode
!= list_entry(sb
->s_inode_lru
.next
,
689 struct inode
, i_lru
))
690 continue; /* wrong inode or list_empty */
691 /* avoid lock inversions with trylock */
692 if (!spin_trylock(&inode
->i_lock
))
694 if (!can_unuse(inode
)) {
695 spin_unlock(&inode
->i_lock
);
699 WARN_ON(inode
->i_state
& I_NEW
);
700 inode
->i_state
|= I_FREEING
;
701 spin_unlock(&inode
->i_lock
);
703 list_move(&inode
->i_lru
, &freeable
);
704 sb
->s_nr_inodes_unused
--;
705 this_cpu_dec(nr_unused
);
707 if (current_is_kswapd())
708 __count_vm_events(KSWAPD_INODESTEAL
, reap
);
710 __count_vm_events(PGINODESTEAL
, reap
);
711 spin_unlock(&sb
->s_inode_lru_lock
);
713 dispose_list(&freeable
);
714 up_read(&iprune_sem
);
717 static void __wait_on_freeing_inode(struct inode
*inode
);
719 * Called with the inode lock held.
721 static struct inode
*find_inode(struct super_block
*sb
,
722 struct hlist_head
*head
,
723 int (*test
)(struct inode
*, void *),
726 struct hlist_node
*node
;
727 struct inode
*inode
= NULL
;
730 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
731 spin_lock(&inode
->i_lock
);
732 if (inode
->i_sb
!= sb
) {
733 spin_unlock(&inode
->i_lock
);
736 if (!test(inode
, data
)) {
737 spin_unlock(&inode
->i_lock
);
740 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
741 __wait_on_freeing_inode(inode
);
745 spin_unlock(&inode
->i_lock
);
752 * find_inode_fast is the fast path version of find_inode, see the comment at
753 * iget_locked for details.
755 static struct inode
*find_inode_fast(struct super_block
*sb
,
756 struct hlist_head
*head
, unsigned long ino
)
758 struct hlist_node
*node
;
759 struct inode
*inode
= NULL
;
762 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
763 spin_lock(&inode
->i_lock
);
764 if (inode
->i_ino
!= ino
) {
765 spin_unlock(&inode
->i_lock
);
768 if (inode
->i_sb
!= sb
) {
769 spin_unlock(&inode
->i_lock
);
772 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
773 __wait_on_freeing_inode(inode
);
777 spin_unlock(&inode
->i_lock
);
784 * Each cpu owns a range of LAST_INO_BATCH numbers.
785 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
786 * to renew the exhausted range.
788 * This does not significantly increase overflow rate because every CPU can
789 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
790 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
791 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
792 * overflow rate by 2x, which does not seem too significant.
794 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
795 * error if st_ino won't fit in target struct field. Use 32bit counter
796 * here to attempt to avoid that.
798 #define LAST_INO_BATCH 1024
799 static DEFINE_PER_CPU(unsigned int, last_ino
);
801 unsigned int get_next_ino(void)
803 unsigned int *p
= &get_cpu_var(last_ino
);
804 unsigned int res
= *p
;
807 if (unlikely((res
& (LAST_INO_BATCH
-1)) == 0)) {
808 static atomic_t shared_last_ino
;
809 int next
= atomic_add_return(LAST_INO_BATCH
, &shared_last_ino
);
811 res
= next
- LAST_INO_BATCH
;
816 put_cpu_var(last_ino
);
819 EXPORT_SYMBOL(get_next_ino
);
822 * new_inode - obtain an inode
825 * Allocates a new inode for given superblock. The default gfp_mask
826 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
827 * If HIGHMEM pages are unsuitable or it is known that pages allocated
828 * for the page cache are not reclaimable or migratable,
829 * mapping_set_gfp_mask() must be called with suitable flags on the
830 * newly created inode's mapping
833 struct inode
*new_inode(struct super_block
*sb
)
837 spin_lock_prefetch(&inode_sb_list_lock
);
839 inode
= alloc_inode(sb
);
841 spin_lock(&inode
->i_lock
);
843 spin_unlock(&inode
->i_lock
);
844 inode_sb_list_add(inode
);
848 EXPORT_SYMBOL(new_inode
);
851 * unlock_new_inode - clear the I_NEW state and wake up any waiters
852 * @inode: new inode to unlock
854 * Called when the inode is fully initialised to clear the new state of the
855 * inode and wake up anyone waiting for the inode to finish initialisation.
857 void unlock_new_inode(struct inode
*inode
)
859 #ifdef CONFIG_DEBUG_LOCK_ALLOC
860 if (S_ISDIR(inode
->i_mode
)) {
861 struct file_system_type
*type
= inode
->i_sb
->s_type
;
863 /* Set new key only if filesystem hasn't already changed it */
864 if (!lockdep_match_class(&inode
->i_mutex
,
865 &type
->i_mutex_key
)) {
867 * ensure nobody is actually holding i_mutex
869 mutex_destroy(&inode
->i_mutex
);
870 mutex_init(&inode
->i_mutex
);
871 lockdep_set_class(&inode
->i_mutex
,
872 &type
->i_mutex_dir_key
);
876 spin_lock(&inode
->i_lock
);
877 WARN_ON(!(inode
->i_state
& I_NEW
));
878 inode
->i_state
&= ~I_NEW
;
879 wake_up_bit(&inode
->i_state
, __I_NEW
);
880 spin_unlock(&inode
->i_lock
);
882 EXPORT_SYMBOL(unlock_new_inode
);
885 * iget5_locked - obtain an inode from a mounted file system
886 * @sb: super block of file system
887 * @hashval: hash value (usually inode number) to get
888 * @test: callback used for comparisons between inodes
889 * @set: callback used to initialize a new struct inode
890 * @data: opaque data pointer to pass to @test and @set
892 * Search for the inode specified by @hashval and @data in the inode cache,
893 * and if present it is return it with an increased reference count. This is
894 * a generalized version of iget_locked() for file systems where the inode
895 * number is not sufficient for unique identification of an inode.
897 * If the inode is not in cache, allocate a new inode and return it locked,
898 * hashed, and with the I_NEW flag set. The file system gets to fill it in
899 * before unlocking it via unlock_new_inode().
901 * Note both @test and @set are called with the inode_hash_lock held, so can't
904 struct inode
*iget5_locked(struct super_block
*sb
, unsigned long hashval
,
905 int (*test
)(struct inode
*, void *),
906 int (*set
)(struct inode
*, void *), void *data
)
908 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
911 spin_lock(&inode_hash_lock
);
912 inode
= find_inode(sb
, head
, test
, data
);
913 spin_unlock(&inode_hash_lock
);
916 wait_on_inode(inode
);
920 inode
= alloc_inode(sb
);
924 spin_lock(&inode_hash_lock
);
925 /* We released the lock, so.. */
926 old
= find_inode(sb
, head
, test
, data
);
928 if (set(inode
, data
))
931 spin_lock(&inode
->i_lock
);
932 inode
->i_state
= I_NEW
;
933 hlist_add_head(&inode
->i_hash
, head
);
934 spin_unlock(&inode
->i_lock
);
935 inode_sb_list_add(inode
);
936 spin_unlock(&inode_hash_lock
);
938 /* Return the locked inode with I_NEW set, the
939 * caller is responsible for filling in the contents
945 * Uhhuh, somebody else created the same inode under
946 * us. Use the old inode instead of the one we just
949 spin_unlock(&inode_hash_lock
);
950 destroy_inode(inode
);
952 wait_on_inode(inode
);
957 spin_unlock(&inode_hash_lock
);
958 destroy_inode(inode
);
961 EXPORT_SYMBOL(iget5_locked
);
964 * iget_locked - obtain an inode from a mounted file system
965 * @sb: super block of file system
966 * @ino: inode number to get
968 * Search for the inode specified by @ino in the inode cache and if present
969 * return it with an increased reference count. This is for file systems
970 * where the inode number is sufficient for unique identification of an inode.
972 * If the inode is not in cache, allocate a new inode and return it locked,
973 * hashed, and with the I_NEW flag set. The file system gets to fill it in
974 * before unlocking it via unlock_new_inode().
976 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
978 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
981 spin_lock(&inode_hash_lock
);
982 inode
= find_inode_fast(sb
, head
, ino
);
983 spin_unlock(&inode_hash_lock
);
985 wait_on_inode(inode
);
989 inode
= alloc_inode(sb
);
993 spin_lock(&inode_hash_lock
);
994 /* We released the lock, so.. */
995 old
= find_inode_fast(sb
, head
, ino
);
998 spin_lock(&inode
->i_lock
);
999 inode
->i_state
= I_NEW
;
1000 hlist_add_head(&inode
->i_hash
, head
);
1001 spin_unlock(&inode
->i_lock
);
1002 inode_sb_list_add(inode
);
1003 spin_unlock(&inode_hash_lock
);
1005 /* Return the locked inode with I_NEW set, the
1006 * caller is responsible for filling in the contents
1012 * Uhhuh, somebody else created the same inode under
1013 * us. Use the old inode instead of the one we just
1016 spin_unlock(&inode_hash_lock
);
1017 destroy_inode(inode
);
1019 wait_on_inode(inode
);
1023 EXPORT_SYMBOL(iget_locked
);
1026 * search the inode cache for a matching inode number.
1027 * If we find one, then the inode number we are trying to
1028 * allocate is not unique and so we should not use it.
1030 * Returns 1 if the inode number is unique, 0 if it is not.
1032 static int test_inode_iunique(struct super_block
*sb
, unsigned long ino
)
1034 struct hlist_head
*b
= inode_hashtable
+ hash(sb
, ino
);
1035 struct hlist_node
*node
;
1036 struct inode
*inode
;
1038 spin_lock(&inode_hash_lock
);
1039 hlist_for_each_entry(inode
, node
, b
, i_hash
) {
1040 if (inode
->i_ino
== ino
&& inode
->i_sb
== sb
) {
1041 spin_unlock(&inode_hash_lock
);
1045 spin_unlock(&inode_hash_lock
);
1051 * iunique - get a unique inode number
1053 * @max_reserved: highest reserved inode number
1055 * Obtain an inode number that is unique on the system for a given
1056 * superblock. This is used by file systems that have no natural
1057 * permanent inode numbering system. An inode number is returned that
1058 * is higher than the reserved limit but unique.
1061 * With a large number of inodes live on the file system this function
1062 * currently becomes quite slow.
1064 ino_t
iunique(struct super_block
*sb
, ino_t max_reserved
)
1067 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1068 * error if st_ino won't fit in target struct field. Use 32bit counter
1069 * here to attempt to avoid that.
1071 static DEFINE_SPINLOCK(iunique_lock
);
1072 static unsigned int counter
;
1075 spin_lock(&iunique_lock
);
1077 if (counter
<= max_reserved
)
1078 counter
= max_reserved
+ 1;
1080 } while (!test_inode_iunique(sb
, res
));
1081 spin_unlock(&iunique_lock
);
1085 EXPORT_SYMBOL(iunique
);
1087 struct inode
*igrab(struct inode
*inode
)
1089 spin_lock(&inode
->i_lock
);
1090 if (!(inode
->i_state
& (I_FREEING
|I_WILL_FREE
))) {
1092 spin_unlock(&inode
->i_lock
);
1094 spin_unlock(&inode
->i_lock
);
1096 * Handle the case where s_op->clear_inode is not been
1097 * called yet, and somebody is calling igrab
1098 * while the inode is getting freed.
1104 EXPORT_SYMBOL(igrab
);
1107 * ilookup5_nowait - search for an inode in the inode cache
1108 * @sb: super block of file system to search
1109 * @hashval: hash value (usually inode number) to search for
1110 * @test: callback used for comparisons between inodes
1111 * @data: opaque data pointer to pass to @test
1113 * Search for the inode specified by @hashval and @data in the inode cache.
1114 * If the inode is in the cache, the inode is returned with an incremented
1117 * Note: I_NEW is not waited upon so you have to be very careful what you do
1118 * with the returned inode. You probably should be using ilookup5() instead.
1120 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1122 struct inode
*ilookup5_nowait(struct super_block
*sb
, unsigned long hashval
,
1123 int (*test
)(struct inode
*, void *), void *data
)
1125 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1126 struct inode
*inode
;
1128 spin_lock(&inode_hash_lock
);
1129 inode
= find_inode(sb
, head
, test
, data
);
1130 spin_unlock(&inode_hash_lock
);
1134 EXPORT_SYMBOL(ilookup5_nowait
);
1137 * ilookup5 - search for an inode in the inode cache
1138 * @sb: super block of file system to search
1139 * @hashval: hash value (usually inode number) to search for
1140 * @test: callback used for comparisons between inodes
1141 * @data: opaque data pointer to pass to @test
1143 * Search for the inode specified by @hashval and @data in the inode cache,
1144 * and if the inode is in the cache, return the inode with an incremented
1145 * reference count. Waits on I_NEW before returning the inode.
1146 * returned with an incremented reference count.
1148 * This is a generalized version of ilookup() for file systems where the
1149 * inode number is not sufficient for unique identification of an inode.
1151 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1153 struct inode
*ilookup5(struct super_block
*sb
, unsigned long hashval
,
1154 int (*test
)(struct inode
*, void *), void *data
)
1156 struct inode
*inode
= ilookup5_nowait(sb
, hashval
, test
, data
);
1159 wait_on_inode(inode
);
1162 EXPORT_SYMBOL(ilookup5
);
1165 * ilookup - search for an inode in the inode cache
1166 * @sb: super block of file system to search
1167 * @ino: inode number to search for
1169 * Search for the inode @ino in the inode cache, and if the inode is in the
1170 * cache, the inode is returned with an incremented reference count.
1172 struct inode
*ilookup(struct super_block
*sb
, unsigned long ino
)
1174 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1175 struct inode
*inode
;
1177 spin_lock(&inode_hash_lock
);
1178 inode
= find_inode_fast(sb
, head
, ino
);
1179 spin_unlock(&inode_hash_lock
);
1182 wait_on_inode(inode
);
1185 EXPORT_SYMBOL(ilookup
);
1187 int insert_inode_locked(struct inode
*inode
)
1189 struct super_block
*sb
= inode
->i_sb
;
1190 ino_t ino
= inode
->i_ino
;
1191 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1194 struct hlist_node
*node
;
1195 struct inode
*old
= NULL
;
1196 spin_lock(&inode_hash_lock
);
1197 hlist_for_each_entry(old
, node
, head
, i_hash
) {
1198 if (old
->i_ino
!= ino
)
1200 if (old
->i_sb
!= sb
)
1202 spin_lock(&old
->i_lock
);
1203 if (old
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
1204 spin_unlock(&old
->i_lock
);
1209 if (likely(!node
)) {
1210 spin_lock(&inode
->i_lock
);
1211 inode
->i_state
|= I_NEW
;
1212 hlist_add_head(&inode
->i_hash
, head
);
1213 spin_unlock(&inode
->i_lock
);
1214 spin_unlock(&inode_hash_lock
);
1218 spin_unlock(&old
->i_lock
);
1219 spin_unlock(&inode_hash_lock
);
1221 if (unlikely(!inode_unhashed(old
))) {
1228 EXPORT_SYMBOL(insert_inode_locked
);
1230 int insert_inode_locked4(struct inode
*inode
, unsigned long hashval
,
1231 int (*test
)(struct inode
*, void *), void *data
)
1233 struct super_block
*sb
= inode
->i_sb
;
1234 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1237 struct hlist_node
*node
;
1238 struct inode
*old
= NULL
;
1240 spin_lock(&inode_hash_lock
);
1241 hlist_for_each_entry(old
, node
, head
, i_hash
) {
1242 if (old
->i_sb
!= sb
)
1244 if (!test(old
, data
))
1246 spin_lock(&old
->i_lock
);
1247 if (old
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
1248 spin_unlock(&old
->i_lock
);
1253 if (likely(!node
)) {
1254 spin_lock(&inode
->i_lock
);
1255 inode
->i_state
|= I_NEW
;
1256 hlist_add_head(&inode
->i_hash
, head
);
1257 spin_unlock(&inode
->i_lock
);
1258 spin_unlock(&inode_hash_lock
);
1262 spin_unlock(&old
->i_lock
);
1263 spin_unlock(&inode_hash_lock
);
1265 if (unlikely(!inode_unhashed(old
))) {
1272 EXPORT_SYMBOL(insert_inode_locked4
);
1275 int generic_delete_inode(struct inode
*inode
)
1279 EXPORT_SYMBOL(generic_delete_inode
);
1282 * Normal UNIX filesystem behaviour: delete the
1283 * inode when the usage count drops to zero, and
1286 int generic_drop_inode(struct inode
*inode
)
1288 return !inode
->i_nlink
|| inode_unhashed(inode
);
1290 EXPORT_SYMBOL_GPL(generic_drop_inode
);
1293 * Called when we're dropping the last reference
1296 * Call the FS "drop_inode()" function, defaulting to
1297 * the legacy UNIX filesystem behaviour. If it tells
1298 * us to evict inode, do so. Otherwise, retain inode
1299 * in cache if fs is alive, sync and evict if fs is
1302 static void iput_final(struct inode
*inode
)
1304 struct super_block
*sb
= inode
->i_sb
;
1305 const struct super_operations
*op
= inode
->i_sb
->s_op
;
1308 WARN_ON(inode
->i_state
& I_NEW
);
1311 drop
= op
->drop_inode(inode
);
1313 drop
= generic_drop_inode(inode
);
1315 if (!drop
&& (sb
->s_flags
& MS_ACTIVE
)) {
1316 inode
->i_state
|= I_REFERENCED
;
1317 if (!(inode
->i_state
& (I_DIRTY
|I_SYNC
)))
1318 inode_lru_list_add(inode
);
1319 spin_unlock(&inode
->i_lock
);
1324 inode
->i_state
|= I_WILL_FREE
;
1325 spin_unlock(&inode
->i_lock
);
1326 write_inode_now(inode
, 1);
1327 spin_lock(&inode
->i_lock
);
1328 WARN_ON(inode
->i_state
& I_NEW
);
1329 inode
->i_state
&= ~I_WILL_FREE
;
1332 inode
->i_state
|= I_FREEING
;
1333 inode_lru_list_del(inode
);
1334 spin_unlock(&inode
->i_lock
);
1340 * iput - put an inode
1341 * @inode: inode to put
1343 * Puts an inode, dropping its usage count. If the inode use count hits
1344 * zero, the inode is then freed and may also be destroyed.
1346 * Consequently, iput() can sleep.
1348 void iput(struct inode
*inode
)
1351 BUG_ON(inode
->i_state
& I_CLEAR
);
1353 if (atomic_dec_and_lock(&inode
->i_count
, &inode
->i_lock
))
1357 EXPORT_SYMBOL(iput
);
1360 * bmap - find a block number in a file
1361 * @inode: inode of file
1362 * @block: block to find
1364 * Returns the block number on the device holding the inode that
1365 * is the disk block number for the block of the file requested.
1366 * That is, asked for block 4 of inode 1 the function will return the
1367 * disk block relative to the disk start that holds that block of the
1370 sector_t
bmap(struct inode
*inode
, sector_t block
)
1373 if (inode
->i_mapping
->a_ops
->bmap
)
1374 res
= inode
->i_mapping
->a_ops
->bmap(inode
->i_mapping
, block
);
1377 EXPORT_SYMBOL(bmap
);
1380 * With relative atime, only update atime if the previous atime is
1381 * earlier than either the ctime or mtime or if at least a day has
1382 * passed since the last atime update.
1384 static int relatime_need_update(struct vfsmount
*mnt
, struct inode
*inode
,
1385 struct timespec now
)
1388 if (!(mnt
->mnt_flags
& MNT_RELATIME
))
1391 * Is mtime younger than atime? If yes, update atime:
1393 if (timespec_compare(&inode
->i_mtime
, &inode
->i_atime
) >= 0)
1396 * Is ctime younger than atime? If yes, update atime:
1398 if (timespec_compare(&inode
->i_ctime
, &inode
->i_atime
) >= 0)
1402 * Is the previous atime value older than a day? If yes,
1405 if ((long)(now
.tv_sec
- inode
->i_atime
.tv_sec
) >= 24*60*60)
1408 * Good, we can skip the atime update:
1414 * touch_atime - update the access time
1415 * @mnt: mount the inode is accessed on
1416 * @dentry: dentry accessed
1418 * Update the accessed time on an inode and mark it for writeback.
1419 * This function automatically handles read only file systems and media,
1420 * as well as the "noatime" flag and inode specific "noatime" markers.
1422 void touch_atime(struct vfsmount
*mnt
, struct dentry
*dentry
)
1424 struct inode
*inode
= dentry
->d_inode
;
1425 struct timespec now
;
1427 if (inode
->i_flags
& S_NOATIME
)
1429 if (IS_NOATIME(inode
))
1431 if ((inode
->i_sb
->s_flags
& MS_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1434 if (mnt
->mnt_flags
& MNT_NOATIME
)
1436 if ((mnt
->mnt_flags
& MNT_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1439 now
= current_fs_time(inode
->i_sb
);
1441 if (!relatime_need_update(mnt
, inode
, now
))
1444 if (timespec_equal(&inode
->i_atime
, &now
))
1447 if (mnt_want_write(mnt
))
1450 inode
->i_atime
= now
;
1451 mark_inode_dirty_sync(inode
);
1452 mnt_drop_write(mnt
);
1454 EXPORT_SYMBOL(touch_atime
);
1457 * file_update_time - update mtime and ctime time
1458 * @file: file accessed
1460 * Update the mtime and ctime members of an inode and mark the inode
1461 * for writeback. Note that this function is meant exclusively for
1462 * usage in the file write path of filesystems, and filesystems may
1463 * choose to explicitly ignore update via this function with the
1464 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1465 * timestamps are handled by the server.
1468 void file_update_time(struct file
*file
)
1470 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1471 struct timespec now
;
1472 enum { S_MTIME
= 1, S_CTIME
= 2, S_VERSION
= 4 } sync_it
= 0;
1474 /* First try to exhaust all avenues to not sync */
1475 if (IS_NOCMTIME(inode
))
1478 now
= current_fs_time(inode
->i_sb
);
1479 if (!timespec_equal(&inode
->i_mtime
, &now
))
1482 if (!timespec_equal(&inode
->i_ctime
, &now
))
1485 if (IS_I_VERSION(inode
))
1486 sync_it
|= S_VERSION
;
1491 /* Finally allowed to write? Takes lock. */
1492 if (mnt_want_write_file(file
))
1495 /* Only change inode inside the lock region */
1496 if (sync_it
& S_VERSION
)
1497 inode_inc_iversion(inode
);
1498 if (sync_it
& S_CTIME
)
1499 inode
->i_ctime
= now
;
1500 if (sync_it
& S_MTIME
)
1501 inode
->i_mtime
= now
;
1502 mark_inode_dirty_sync(inode
);
1503 mnt_drop_write(file
->f_path
.mnt
);
1505 EXPORT_SYMBOL(file_update_time
);
1507 int inode_needs_sync(struct inode
*inode
)
1511 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
1515 EXPORT_SYMBOL(inode_needs_sync
);
1517 int inode_wait(void *word
)
1522 EXPORT_SYMBOL(inode_wait
);
1525 * If we try to find an inode in the inode hash while it is being
1526 * deleted, we have to wait until the filesystem completes its
1527 * deletion before reporting that it isn't found. This function waits
1528 * until the deletion _might_ have completed. Callers are responsible
1529 * to recheck inode state.
1531 * It doesn't matter if I_NEW is not set initially, a call to
1532 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1535 static void __wait_on_freeing_inode(struct inode
*inode
)
1537 wait_queue_head_t
*wq
;
1538 DEFINE_WAIT_BIT(wait
, &inode
->i_state
, __I_NEW
);
1539 wq
= bit_waitqueue(&inode
->i_state
, __I_NEW
);
1540 prepare_to_wait(wq
, &wait
.wait
, TASK_UNINTERRUPTIBLE
);
1541 spin_unlock(&inode
->i_lock
);
1542 spin_unlock(&inode_hash_lock
);
1544 finish_wait(wq
, &wait
.wait
);
1545 spin_lock(&inode_hash_lock
);
1548 static __initdata
unsigned long ihash_entries
;
1549 static int __init
set_ihash_entries(char *str
)
1553 ihash_entries
= simple_strtoul(str
, &str
, 0);
1556 __setup("ihash_entries=", set_ihash_entries
);
1559 * Initialize the waitqueues and inode hash table.
1561 void __init
inode_init_early(void)
1565 /* If hashes are distributed across NUMA nodes, defer
1566 * hash allocation until vmalloc space is available.
1572 alloc_large_system_hash("Inode-cache",
1573 sizeof(struct hlist_head
),
1581 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1582 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1585 void __init
inode_init(void)
1589 /* inode slab cache */
1590 inode_cachep
= kmem_cache_create("inode_cache",
1591 sizeof(struct inode
),
1593 (SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|
1597 /* Hash may have been set up in inode_init_early */
1602 alloc_large_system_hash("Inode-cache",
1603 sizeof(struct hlist_head
),
1611 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1612 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1615 void init_special_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
1617 inode
->i_mode
= mode
;
1618 if (S_ISCHR(mode
)) {
1619 inode
->i_fop
= &def_chr_fops
;
1620 inode
->i_rdev
= rdev
;
1621 } else if (S_ISBLK(mode
)) {
1622 inode
->i_fop
= &def_blk_fops
;
1623 inode
->i_rdev
= rdev
;
1624 } else if (S_ISFIFO(mode
))
1625 inode
->i_fop
= &def_fifo_fops
;
1626 else if (S_ISSOCK(mode
))
1627 inode
->i_fop
= &bad_sock_fops
;
1629 printk(KERN_DEBUG
"init_special_inode: bogus i_mode (%o) for"
1630 " inode %s:%lu\n", mode
, inode
->i_sb
->s_id
,
1633 EXPORT_SYMBOL(init_special_inode
);
1636 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
1638 * @dir: Directory inode
1639 * @mode: mode of the new inode
1641 void inode_init_owner(struct inode
*inode
, const struct inode
*dir
,
1644 inode
->i_uid
= current_fsuid();
1645 if (dir
&& dir
->i_mode
& S_ISGID
) {
1646 inode
->i_gid
= dir
->i_gid
;
1650 inode
->i_gid
= current_fsgid();
1651 inode
->i_mode
= mode
;
1653 EXPORT_SYMBOL(inode_init_owner
);
1656 * inode_owner_or_capable - check current task permissions to inode
1657 * @inode: inode being checked
1659 * Return true if current either has CAP_FOWNER to the inode, or
1662 bool inode_owner_or_capable(const struct inode
*inode
)
1664 struct user_namespace
*ns
= inode_userns(inode
);
1666 if (current_user_ns() == ns
&& current_fsuid() == inode
->i_uid
)
1668 if (ns_capable(ns
, CAP_FOWNER
))
1672 EXPORT_SYMBOL(inode_owner_or_capable
);