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
);
70 * Empty aops. Can be used for the cases where the user does not
71 * define any of the address_space operations.
73 const struct address_space_operations empty_aops
= {
75 EXPORT_SYMBOL(empty_aops
);
78 * Statistics gathering..
80 struct inodes_stat_t inodes_stat
;
82 static DEFINE_PER_CPU(unsigned int, nr_inodes
);
83 static DEFINE_PER_CPU(unsigned int, nr_unused
);
85 static struct kmem_cache
*inode_cachep __read_mostly
;
87 static int get_nr_inodes(void)
91 for_each_possible_cpu(i
)
92 sum
+= per_cpu(nr_inodes
, i
);
93 return sum
< 0 ? 0 : sum
;
96 static inline int get_nr_inodes_unused(void)
100 for_each_possible_cpu(i
)
101 sum
+= per_cpu(nr_unused
, i
);
102 return sum
< 0 ? 0 : sum
;
105 int get_nr_dirty_inodes(void)
107 /* not actually dirty inodes, but a wild approximation */
108 int nr_dirty
= get_nr_inodes() - get_nr_inodes_unused();
109 return nr_dirty
> 0 ? nr_dirty
: 0;
113 * Handle nr_inode sysctl
116 int proc_nr_inodes(ctl_table
*table
, int write
,
117 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
119 inodes_stat
.nr_inodes
= get_nr_inodes();
120 inodes_stat
.nr_unused
= get_nr_inodes_unused();
121 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
126 * inode_init_always - perform inode structure intialisation
127 * @sb: superblock inode belongs to
128 * @inode: inode to initialise
130 * These are initializations that need to be done on every inode
131 * allocation as the fields are not initialised by slab allocation.
133 int inode_init_always(struct super_block
*sb
, struct inode
*inode
)
135 static const struct inode_operations empty_iops
;
136 static const struct file_operations empty_fops
;
137 struct address_space
*const mapping
= &inode
->i_data
;
140 inode
->i_blkbits
= sb
->s_blocksize_bits
;
142 atomic_set(&inode
->i_count
, 1);
143 inode
->i_op
= &empty_iops
;
144 inode
->i_fop
= &empty_fops
;
148 atomic_set(&inode
->i_writecount
, 0);
152 inode
->i_generation
= 0;
154 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
156 inode
->i_pipe
= NULL
;
157 inode
->i_bdev
= NULL
;
158 inode
->i_cdev
= NULL
;
160 inode
->dirtied_when
= 0;
162 if (security_inode_alloc(inode
))
164 spin_lock_init(&inode
->i_lock
);
165 lockdep_set_class(&inode
->i_lock
, &sb
->s_type
->i_lock_key
);
167 mutex_init(&inode
->i_mutex
);
168 lockdep_set_class(&inode
->i_mutex
, &sb
->s_type
->i_mutex_key
);
170 atomic_set(&inode
->i_dio_count
, 0);
172 mapping
->a_ops
= &empty_aops
;
173 mapping
->host
= inode
;
175 mapping_set_gfp_mask(mapping
, GFP_HIGHUSER_MOVABLE
);
176 mapping
->assoc_mapping
= NULL
;
177 mapping
->backing_dev_info
= &default_backing_dev_info
;
178 mapping
->writeback_index
= 0;
181 * If the block_device provides a backing_dev_info for client
182 * inodes then use that. Otherwise the inode share the bdev's
186 struct backing_dev_info
*bdi
;
188 bdi
= sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
189 mapping
->backing_dev_info
= bdi
;
191 inode
->i_private
= NULL
;
192 inode
->i_mapping
= mapping
;
193 #ifdef CONFIG_FS_POSIX_ACL
194 inode
->i_acl
= inode
->i_default_acl
= ACL_NOT_CACHED
;
197 #ifdef CONFIG_FSNOTIFY
198 inode
->i_fsnotify_mask
= 0;
201 this_cpu_inc(nr_inodes
);
207 EXPORT_SYMBOL(inode_init_always
);
209 static struct inode
*alloc_inode(struct super_block
*sb
)
213 if (sb
->s_op
->alloc_inode
)
214 inode
= sb
->s_op
->alloc_inode(sb
);
216 inode
= kmem_cache_alloc(inode_cachep
, GFP_KERNEL
);
221 if (unlikely(inode_init_always(sb
, inode
))) {
222 if (inode
->i_sb
->s_op
->destroy_inode
)
223 inode
->i_sb
->s_op
->destroy_inode(inode
);
225 kmem_cache_free(inode_cachep
, inode
);
232 void free_inode_nonrcu(struct inode
*inode
)
234 kmem_cache_free(inode_cachep
, inode
);
236 EXPORT_SYMBOL(free_inode_nonrcu
);
238 void __destroy_inode(struct inode
*inode
)
240 BUG_ON(inode_has_buffers(inode
));
241 security_inode_free(inode
);
242 fsnotify_inode_delete(inode
);
243 #ifdef CONFIG_FS_POSIX_ACL
244 if (inode
->i_acl
&& inode
->i_acl
!= ACL_NOT_CACHED
)
245 posix_acl_release(inode
->i_acl
);
246 if (inode
->i_default_acl
&& inode
->i_default_acl
!= ACL_NOT_CACHED
)
247 posix_acl_release(inode
->i_default_acl
);
249 this_cpu_dec(nr_inodes
);
251 EXPORT_SYMBOL(__destroy_inode
);
253 static void i_callback(struct rcu_head
*head
)
255 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
256 INIT_LIST_HEAD(&inode
->i_dentry
);
257 kmem_cache_free(inode_cachep
, inode
);
260 static void destroy_inode(struct inode
*inode
)
262 BUG_ON(!list_empty(&inode
->i_lru
));
263 __destroy_inode(inode
);
264 if (inode
->i_sb
->s_op
->destroy_inode
)
265 inode
->i_sb
->s_op
->destroy_inode(inode
);
267 call_rcu(&inode
->i_rcu
, i_callback
);
270 void address_space_init_once(struct address_space
*mapping
)
272 memset(mapping
, 0, sizeof(*mapping
));
273 INIT_RADIX_TREE(&mapping
->page_tree
, GFP_ATOMIC
);
274 spin_lock_init(&mapping
->tree_lock
);
275 mutex_init(&mapping
->i_mmap_mutex
);
276 INIT_LIST_HEAD(&mapping
->private_list
);
277 spin_lock_init(&mapping
->private_lock
);
278 INIT_RAW_PRIO_TREE_ROOT(&mapping
->i_mmap
);
279 INIT_LIST_HEAD(&mapping
->i_mmap_nonlinear
);
281 EXPORT_SYMBOL(address_space_init_once
);
284 * These are initializations that only need to be done
285 * once, because the fields are idempotent across use
286 * of the inode, so let the slab aware of that.
288 void inode_init_once(struct inode
*inode
)
290 memset(inode
, 0, sizeof(*inode
));
291 INIT_HLIST_NODE(&inode
->i_hash
);
292 INIT_LIST_HEAD(&inode
->i_dentry
);
293 INIT_LIST_HEAD(&inode
->i_devices
);
294 INIT_LIST_HEAD(&inode
->i_wb_list
);
295 INIT_LIST_HEAD(&inode
->i_lru
);
296 address_space_init_once(&inode
->i_data
);
297 i_size_ordered_init(inode
);
298 #ifdef CONFIG_FSNOTIFY
299 INIT_HLIST_HEAD(&inode
->i_fsnotify_marks
);
302 EXPORT_SYMBOL(inode_init_once
);
304 static void init_once(void *foo
)
306 struct inode
*inode
= (struct inode
*) foo
;
308 inode_init_once(inode
);
312 * inode->i_lock must be held
314 void __iget(struct inode
*inode
)
316 atomic_inc(&inode
->i_count
);
320 * get additional reference to inode; caller must already hold one.
322 void ihold(struct inode
*inode
)
324 WARN_ON(atomic_inc_return(&inode
->i_count
) < 2);
326 EXPORT_SYMBOL(ihold
);
328 static void inode_lru_list_add(struct inode
*inode
)
330 spin_lock(&inode
->i_sb
->s_inode_lru_lock
);
331 if (list_empty(&inode
->i_lru
)) {
332 list_add(&inode
->i_lru
, &inode
->i_sb
->s_inode_lru
);
333 inode
->i_sb
->s_nr_inodes_unused
++;
334 this_cpu_inc(nr_unused
);
336 spin_unlock(&inode
->i_sb
->s_inode_lru_lock
);
339 static void inode_lru_list_del(struct inode
*inode
)
341 spin_lock(&inode
->i_sb
->s_inode_lru_lock
);
342 if (!list_empty(&inode
->i_lru
)) {
343 list_del_init(&inode
->i_lru
);
344 inode
->i_sb
->s_nr_inodes_unused
--;
345 this_cpu_dec(nr_unused
);
347 spin_unlock(&inode
->i_sb
->s_inode_lru_lock
);
351 * inode_sb_list_add - add inode to the superblock list of inodes
352 * @inode: inode to add
354 void inode_sb_list_add(struct inode
*inode
)
356 spin_lock(&inode_sb_list_lock
);
357 list_add(&inode
->i_sb_list
, &inode
->i_sb
->s_inodes
);
358 spin_unlock(&inode_sb_list_lock
);
360 EXPORT_SYMBOL_GPL(inode_sb_list_add
);
362 static inline void inode_sb_list_del(struct inode
*inode
)
364 if (!list_empty(&inode
->i_sb_list
)) {
365 spin_lock(&inode_sb_list_lock
);
366 list_del_init(&inode
->i_sb_list
);
367 spin_unlock(&inode_sb_list_lock
);
371 static unsigned long hash(struct super_block
*sb
, unsigned long hashval
)
375 tmp
= (hashval
* (unsigned long)sb
) ^ (GOLDEN_RATIO_PRIME
+ hashval
) /
377 tmp
= tmp
^ ((tmp
^ GOLDEN_RATIO_PRIME
) >> i_hash_shift
);
378 return tmp
& i_hash_mask
;
382 * __insert_inode_hash - hash an inode
383 * @inode: unhashed inode
384 * @hashval: unsigned long value used to locate this object in the
387 * Add an inode to the inode hash for this superblock.
389 void __insert_inode_hash(struct inode
*inode
, unsigned long hashval
)
391 struct hlist_head
*b
= inode_hashtable
+ hash(inode
->i_sb
, hashval
);
393 spin_lock(&inode_hash_lock
);
394 spin_lock(&inode
->i_lock
);
395 hlist_add_head(&inode
->i_hash
, b
);
396 spin_unlock(&inode
->i_lock
);
397 spin_unlock(&inode_hash_lock
);
399 EXPORT_SYMBOL(__insert_inode_hash
);
402 * remove_inode_hash - remove an inode from the hash
403 * @inode: inode to unhash
405 * Remove an inode from the superblock.
407 void remove_inode_hash(struct inode
*inode
)
409 spin_lock(&inode_hash_lock
);
410 spin_lock(&inode
->i_lock
);
411 hlist_del_init(&inode
->i_hash
);
412 spin_unlock(&inode
->i_lock
);
413 spin_unlock(&inode_hash_lock
);
415 EXPORT_SYMBOL(remove_inode_hash
);
417 void end_writeback(struct inode
*inode
)
421 * We have to cycle tree_lock here because reclaim can be still in the
422 * process of removing the last page (in __delete_from_page_cache())
423 * and we must not free mapping under it.
425 spin_lock_irq(&inode
->i_data
.tree_lock
);
426 BUG_ON(inode
->i_data
.nrpages
);
427 spin_unlock_irq(&inode
->i_data
.tree_lock
);
428 BUG_ON(!list_empty(&inode
->i_data
.private_list
));
429 BUG_ON(!(inode
->i_state
& I_FREEING
));
430 BUG_ON(inode
->i_state
& I_CLEAR
);
431 inode_sync_wait(inode
);
432 /* don't need i_lock here, no concurrent mods to i_state */
433 inode
->i_state
= I_FREEING
| I_CLEAR
;
435 EXPORT_SYMBOL(end_writeback
);
438 * Free the inode passed in, removing it from the lists it is still connected
439 * to. We remove any pages still attached to the inode and wait for any IO that
440 * is still in progress before finally destroying the inode.
442 * An inode must already be marked I_FREEING so that we avoid the inode being
443 * moved back onto lists if we race with other code that manipulates the lists
444 * (e.g. writeback_single_inode). The caller is responsible for setting this.
446 * An inode must already be removed from the LRU list before being evicted from
447 * the cache. This should occur atomically with setting the I_FREEING state
448 * flag, so no inodes here should ever be on the LRU when being evicted.
450 static void evict(struct inode
*inode
)
452 const struct super_operations
*op
= inode
->i_sb
->s_op
;
454 BUG_ON(!(inode
->i_state
& I_FREEING
));
455 BUG_ON(!list_empty(&inode
->i_lru
));
457 inode_wb_list_del(inode
);
458 inode_sb_list_del(inode
);
460 if (op
->evict_inode
) {
461 op
->evict_inode(inode
);
463 if (inode
->i_data
.nrpages
)
464 truncate_inode_pages(&inode
->i_data
, 0);
465 end_writeback(inode
);
467 if (S_ISBLK(inode
->i_mode
) && inode
->i_bdev
)
469 if (S_ISCHR(inode
->i_mode
) && inode
->i_cdev
)
472 remove_inode_hash(inode
);
474 spin_lock(&inode
->i_lock
);
475 wake_up_bit(&inode
->i_state
, __I_NEW
);
476 BUG_ON(inode
->i_state
!= (I_FREEING
| I_CLEAR
));
477 spin_unlock(&inode
->i_lock
);
479 destroy_inode(inode
);
483 * dispose_list - dispose of the contents of a local list
484 * @head: the head of the list to free
486 * Dispose-list gets a local list with local inodes in it, so it doesn't
487 * need to worry about list corruption and SMP locks.
489 static void dispose_list(struct list_head
*head
)
491 while (!list_empty(head
)) {
494 inode
= list_first_entry(head
, struct inode
, i_lru
);
495 list_del_init(&inode
->i_lru
);
502 * evict_inodes - evict all evictable inodes for a superblock
503 * @sb: superblock to operate on
505 * Make sure that no inodes with zero refcount are retained. This is
506 * called by superblock shutdown after having MS_ACTIVE flag removed,
507 * so any inode reaching zero refcount during or after that call will
508 * be immediately evicted.
510 void evict_inodes(struct super_block
*sb
)
512 struct inode
*inode
, *next
;
515 spin_lock(&inode_sb_list_lock
);
516 list_for_each_entry_safe(inode
, next
, &sb
->s_inodes
, i_sb_list
) {
517 if (atomic_read(&inode
->i_count
))
520 spin_lock(&inode
->i_lock
);
521 if (inode
->i_state
& (I_NEW
| I_FREEING
| I_WILL_FREE
)) {
522 spin_unlock(&inode
->i_lock
);
526 inode
->i_state
|= I_FREEING
;
527 inode_lru_list_del(inode
);
528 spin_unlock(&inode
->i_lock
);
529 list_add(&inode
->i_lru
, &dispose
);
531 spin_unlock(&inode_sb_list_lock
);
533 dispose_list(&dispose
);
537 * invalidate_inodes - attempt to free all inodes on a superblock
538 * @sb: superblock to operate on
539 * @kill_dirty: flag to guide handling of dirty inodes
541 * Attempts to free all inodes for a given superblock. If there were any
542 * busy inodes return a non-zero value, else zero.
543 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
546 int invalidate_inodes(struct super_block
*sb
, bool kill_dirty
)
549 struct inode
*inode
, *next
;
552 spin_lock(&inode_sb_list_lock
);
553 list_for_each_entry_safe(inode
, next
, &sb
->s_inodes
, i_sb_list
) {
554 spin_lock(&inode
->i_lock
);
555 if (inode
->i_state
& (I_NEW
| I_FREEING
| I_WILL_FREE
)) {
556 spin_unlock(&inode
->i_lock
);
559 if (inode
->i_state
& I_DIRTY
&& !kill_dirty
) {
560 spin_unlock(&inode
->i_lock
);
564 if (atomic_read(&inode
->i_count
)) {
565 spin_unlock(&inode
->i_lock
);
570 inode
->i_state
|= I_FREEING
;
571 inode_lru_list_del(inode
);
572 spin_unlock(&inode
->i_lock
);
573 list_add(&inode
->i_lru
, &dispose
);
575 spin_unlock(&inode_sb_list_lock
);
577 dispose_list(&dispose
);
582 static int can_unuse(struct inode
*inode
)
584 if (inode
->i_state
& ~I_REFERENCED
)
586 if (inode_has_buffers(inode
))
588 if (atomic_read(&inode
->i_count
))
590 if (inode
->i_data
.nrpages
)
596 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
597 * This is called from the superblock shrinker function with a number of inodes
598 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
599 * then are freed outside inode_lock by dispose_list().
601 * Any inodes which are pinned purely because of attached pagecache have their
602 * pagecache removed. If the inode has metadata buffers attached to
603 * mapping->private_list then try to remove them.
605 * If the inode has the I_REFERENCED flag set, then it means that it has been
606 * used recently - the flag is set in iput_final(). When we encounter such an
607 * inode, clear the flag and move it to the back of the LRU so it gets another
608 * pass through the LRU before it gets reclaimed. This is necessary because of
609 * the fact we are doing lazy LRU updates to minimise lock contention so the
610 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
611 * with this flag set because they are the inodes that are out of order.
613 void prune_icache_sb(struct super_block
*sb
, int nr_to_scan
)
617 unsigned long reap
= 0;
619 spin_lock(&sb
->s_inode_lru_lock
);
620 for (nr_scanned
= nr_to_scan
; nr_scanned
>= 0; nr_scanned
--) {
623 if (list_empty(&sb
->s_inode_lru
))
626 inode
= list_entry(sb
->s_inode_lru
.prev
, struct inode
, i_lru
);
629 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
630 * so use a trylock. If we fail to get the lock, just move the
631 * inode to the back of the list so we don't spin on it.
633 if (!spin_trylock(&inode
->i_lock
)) {
634 list_move(&inode
->i_lru
, &sb
->s_inode_lru
);
639 * Referenced or dirty inodes are still in use. Give them
640 * another pass through the LRU as we canot reclaim them now.
642 if (atomic_read(&inode
->i_count
) ||
643 (inode
->i_state
& ~I_REFERENCED
)) {
644 list_del_init(&inode
->i_lru
);
645 spin_unlock(&inode
->i_lock
);
646 sb
->s_nr_inodes_unused
--;
647 this_cpu_dec(nr_unused
);
651 /* recently referenced inodes get one more pass */
652 if (inode
->i_state
& I_REFERENCED
) {
653 inode
->i_state
&= ~I_REFERENCED
;
654 list_move(&inode
->i_lru
, &sb
->s_inode_lru
);
655 spin_unlock(&inode
->i_lock
);
658 if (inode_has_buffers(inode
) || inode
->i_data
.nrpages
) {
660 spin_unlock(&inode
->i_lock
);
661 spin_unlock(&sb
->s_inode_lru_lock
);
662 if (remove_inode_buffers(inode
))
663 reap
+= invalidate_mapping_pages(&inode
->i_data
,
666 spin_lock(&sb
->s_inode_lru_lock
);
668 if (inode
!= list_entry(sb
->s_inode_lru
.next
,
669 struct inode
, i_lru
))
670 continue; /* wrong inode or list_empty */
671 /* avoid lock inversions with trylock */
672 if (!spin_trylock(&inode
->i_lock
))
674 if (!can_unuse(inode
)) {
675 spin_unlock(&inode
->i_lock
);
679 WARN_ON(inode
->i_state
& I_NEW
);
680 inode
->i_state
|= I_FREEING
;
681 spin_unlock(&inode
->i_lock
);
683 list_move(&inode
->i_lru
, &freeable
);
684 sb
->s_nr_inodes_unused
--;
685 this_cpu_dec(nr_unused
);
687 if (current_is_kswapd())
688 __count_vm_events(KSWAPD_INODESTEAL
, reap
);
690 __count_vm_events(PGINODESTEAL
, reap
);
691 spin_unlock(&sb
->s_inode_lru_lock
);
693 dispose_list(&freeable
);
696 static void __wait_on_freeing_inode(struct inode
*inode
);
698 * Called with the inode lock held.
700 static struct inode
*find_inode(struct super_block
*sb
,
701 struct hlist_head
*head
,
702 int (*test
)(struct inode
*, void *),
705 struct hlist_node
*node
;
706 struct inode
*inode
= NULL
;
709 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
710 spin_lock(&inode
->i_lock
);
711 if (inode
->i_sb
!= sb
) {
712 spin_unlock(&inode
->i_lock
);
715 if (!test(inode
, data
)) {
716 spin_unlock(&inode
->i_lock
);
719 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
720 __wait_on_freeing_inode(inode
);
724 spin_unlock(&inode
->i_lock
);
731 * find_inode_fast is the fast path version of find_inode, see the comment at
732 * iget_locked for details.
734 static struct inode
*find_inode_fast(struct super_block
*sb
,
735 struct hlist_head
*head
, unsigned long ino
)
737 struct hlist_node
*node
;
738 struct inode
*inode
= NULL
;
741 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
742 spin_lock(&inode
->i_lock
);
743 if (inode
->i_ino
!= ino
) {
744 spin_unlock(&inode
->i_lock
);
747 if (inode
->i_sb
!= sb
) {
748 spin_unlock(&inode
->i_lock
);
751 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
752 __wait_on_freeing_inode(inode
);
756 spin_unlock(&inode
->i_lock
);
763 * Each cpu owns a range of LAST_INO_BATCH numbers.
764 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
765 * to renew the exhausted range.
767 * This does not significantly increase overflow rate because every CPU can
768 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
769 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
770 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
771 * overflow rate by 2x, which does not seem too significant.
773 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
774 * error if st_ino won't fit in target struct field. Use 32bit counter
775 * here to attempt to avoid that.
777 #define LAST_INO_BATCH 1024
778 static DEFINE_PER_CPU(unsigned int, last_ino
);
780 unsigned int get_next_ino(void)
782 unsigned int *p
= &get_cpu_var(last_ino
);
783 unsigned int res
= *p
;
786 if (unlikely((res
& (LAST_INO_BATCH
-1)) == 0)) {
787 static atomic_t shared_last_ino
;
788 int next
= atomic_add_return(LAST_INO_BATCH
, &shared_last_ino
);
790 res
= next
- LAST_INO_BATCH
;
795 put_cpu_var(last_ino
);
798 EXPORT_SYMBOL(get_next_ino
);
801 * new_inode_pseudo - obtain an inode
804 * Allocates a new inode for given superblock.
805 * Inode wont be chained in superblock s_inodes list
807 * - fs can't be unmount
808 * - quotas, fsnotify, writeback can't work
810 struct inode
*new_inode_pseudo(struct super_block
*sb
)
812 struct inode
*inode
= alloc_inode(sb
);
815 spin_lock(&inode
->i_lock
);
817 spin_unlock(&inode
->i_lock
);
818 INIT_LIST_HEAD(&inode
->i_sb_list
);
824 * new_inode - obtain an inode
827 * Allocates a new inode for given superblock. The default gfp_mask
828 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
829 * If HIGHMEM pages are unsuitable or it is known that pages allocated
830 * for the page cache are not reclaimable or migratable,
831 * mapping_set_gfp_mask() must be called with suitable flags on the
832 * newly created inode's mapping
835 struct inode
*new_inode(struct super_block
*sb
)
839 spin_lock_prefetch(&inode_sb_list_lock
);
841 inode
= new_inode_pseudo(sb
);
843 inode_sb_list_add(inode
);
846 EXPORT_SYMBOL(new_inode
);
849 * unlock_new_inode - clear the I_NEW state and wake up any waiters
850 * @inode: new inode to unlock
852 * Called when the inode is fully initialised to clear the new state of the
853 * inode and wake up anyone waiting for the inode to finish initialisation.
855 void unlock_new_inode(struct inode
*inode
)
857 #ifdef CONFIG_DEBUG_LOCK_ALLOC
858 if (S_ISDIR(inode
->i_mode
)) {
859 struct file_system_type
*type
= inode
->i_sb
->s_type
;
861 /* Set new key only if filesystem hasn't already changed it */
862 if (!lockdep_match_class(&inode
->i_mutex
,
863 &type
->i_mutex_key
)) {
865 * ensure nobody is actually holding i_mutex
867 mutex_destroy(&inode
->i_mutex
);
868 mutex_init(&inode
->i_mutex
);
869 lockdep_set_class(&inode
->i_mutex
,
870 &type
->i_mutex_dir_key
);
874 spin_lock(&inode
->i_lock
);
875 WARN_ON(!(inode
->i_state
& I_NEW
));
876 inode
->i_state
&= ~I_NEW
;
877 wake_up_bit(&inode
->i_state
, __I_NEW
);
878 spin_unlock(&inode
->i_lock
);
880 EXPORT_SYMBOL(unlock_new_inode
);
883 * iget5_locked - obtain an inode from a mounted file system
884 * @sb: super block of file system
885 * @hashval: hash value (usually inode number) to get
886 * @test: callback used for comparisons between inodes
887 * @set: callback used to initialize a new struct inode
888 * @data: opaque data pointer to pass to @test and @set
890 * Search for the inode specified by @hashval and @data in the inode cache,
891 * and if present it is return it with an increased reference count. This is
892 * a generalized version of iget_locked() for file systems where the inode
893 * number is not sufficient for unique identification of an inode.
895 * If the inode is not in cache, allocate a new inode and return it locked,
896 * hashed, and with the I_NEW flag set. The file system gets to fill it in
897 * before unlocking it via unlock_new_inode().
899 * Note both @test and @set are called with the inode_hash_lock held, so can't
902 struct inode
*iget5_locked(struct super_block
*sb
, unsigned long hashval
,
903 int (*test
)(struct inode
*, void *),
904 int (*set
)(struct inode
*, void *), void *data
)
906 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
909 spin_lock(&inode_hash_lock
);
910 inode
= find_inode(sb
, head
, test
, data
);
911 spin_unlock(&inode_hash_lock
);
914 wait_on_inode(inode
);
918 inode
= alloc_inode(sb
);
922 spin_lock(&inode_hash_lock
);
923 /* We released the lock, so.. */
924 old
= find_inode(sb
, head
, test
, data
);
926 if (set(inode
, data
))
929 spin_lock(&inode
->i_lock
);
930 inode
->i_state
= I_NEW
;
931 hlist_add_head(&inode
->i_hash
, head
);
932 spin_unlock(&inode
->i_lock
);
933 inode_sb_list_add(inode
);
934 spin_unlock(&inode_hash_lock
);
936 /* Return the locked inode with I_NEW set, the
937 * caller is responsible for filling in the contents
943 * Uhhuh, somebody else created the same inode under
944 * us. Use the old inode instead of the one we just
947 spin_unlock(&inode_hash_lock
);
948 destroy_inode(inode
);
950 wait_on_inode(inode
);
955 spin_unlock(&inode_hash_lock
);
956 destroy_inode(inode
);
959 EXPORT_SYMBOL(iget5_locked
);
962 * iget_locked - obtain an inode from a mounted file system
963 * @sb: super block of file system
964 * @ino: inode number to get
966 * Search for the inode specified by @ino in the inode cache and if present
967 * return it with an increased reference count. This is for file systems
968 * where the inode number is sufficient for unique identification of an inode.
970 * If the inode is not in cache, allocate a new inode and return it locked,
971 * hashed, and with the I_NEW flag set. The file system gets to fill it in
972 * before unlocking it via unlock_new_inode().
974 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
976 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
979 spin_lock(&inode_hash_lock
);
980 inode
= find_inode_fast(sb
, head
, ino
);
981 spin_unlock(&inode_hash_lock
);
983 wait_on_inode(inode
);
987 inode
= alloc_inode(sb
);
991 spin_lock(&inode_hash_lock
);
992 /* We released the lock, so.. */
993 old
= find_inode_fast(sb
, head
, ino
);
996 spin_lock(&inode
->i_lock
);
997 inode
->i_state
= I_NEW
;
998 hlist_add_head(&inode
->i_hash
, head
);
999 spin_unlock(&inode
->i_lock
);
1000 inode_sb_list_add(inode
);
1001 spin_unlock(&inode_hash_lock
);
1003 /* Return the locked inode with I_NEW set, the
1004 * caller is responsible for filling in the contents
1010 * Uhhuh, somebody else created the same inode under
1011 * us. Use the old inode instead of the one we just
1014 spin_unlock(&inode_hash_lock
);
1015 destroy_inode(inode
);
1017 wait_on_inode(inode
);
1021 EXPORT_SYMBOL(iget_locked
);
1024 * search the inode cache for a matching inode number.
1025 * If we find one, then the inode number we are trying to
1026 * allocate is not unique and so we should not use it.
1028 * Returns 1 if the inode number is unique, 0 if it is not.
1030 static int test_inode_iunique(struct super_block
*sb
, unsigned long ino
)
1032 struct hlist_head
*b
= inode_hashtable
+ hash(sb
, ino
);
1033 struct hlist_node
*node
;
1034 struct inode
*inode
;
1036 spin_lock(&inode_hash_lock
);
1037 hlist_for_each_entry(inode
, node
, b
, i_hash
) {
1038 if (inode
->i_ino
== ino
&& inode
->i_sb
== sb
) {
1039 spin_unlock(&inode_hash_lock
);
1043 spin_unlock(&inode_hash_lock
);
1049 * iunique - get a unique inode number
1051 * @max_reserved: highest reserved inode number
1053 * Obtain an inode number that is unique on the system for a given
1054 * superblock. This is used by file systems that have no natural
1055 * permanent inode numbering system. An inode number is returned that
1056 * is higher than the reserved limit but unique.
1059 * With a large number of inodes live on the file system this function
1060 * currently becomes quite slow.
1062 ino_t
iunique(struct super_block
*sb
, ino_t max_reserved
)
1065 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1066 * error if st_ino won't fit in target struct field. Use 32bit counter
1067 * here to attempt to avoid that.
1069 static DEFINE_SPINLOCK(iunique_lock
);
1070 static unsigned int counter
;
1073 spin_lock(&iunique_lock
);
1075 if (counter
<= max_reserved
)
1076 counter
= max_reserved
+ 1;
1078 } while (!test_inode_iunique(sb
, res
));
1079 spin_unlock(&iunique_lock
);
1083 EXPORT_SYMBOL(iunique
);
1085 struct inode
*igrab(struct inode
*inode
)
1087 spin_lock(&inode
->i_lock
);
1088 if (!(inode
->i_state
& (I_FREEING
|I_WILL_FREE
))) {
1090 spin_unlock(&inode
->i_lock
);
1092 spin_unlock(&inode
->i_lock
);
1094 * Handle the case where s_op->clear_inode is not been
1095 * called yet, and somebody is calling igrab
1096 * while the inode is getting freed.
1102 EXPORT_SYMBOL(igrab
);
1105 * ilookup5_nowait - search for an inode in the inode cache
1106 * @sb: super block of file system to search
1107 * @hashval: hash value (usually inode number) to search for
1108 * @test: callback used for comparisons between inodes
1109 * @data: opaque data pointer to pass to @test
1111 * Search for the inode specified by @hashval and @data in the inode cache.
1112 * If the inode is in the cache, the inode is returned with an incremented
1115 * Note: I_NEW is not waited upon so you have to be very careful what you do
1116 * with the returned inode. You probably should be using ilookup5() instead.
1118 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1120 struct inode
*ilookup5_nowait(struct super_block
*sb
, unsigned long hashval
,
1121 int (*test
)(struct inode
*, void *), void *data
)
1123 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1124 struct inode
*inode
;
1126 spin_lock(&inode_hash_lock
);
1127 inode
= find_inode(sb
, head
, test
, data
);
1128 spin_unlock(&inode_hash_lock
);
1132 EXPORT_SYMBOL(ilookup5_nowait
);
1135 * ilookup5 - search for an inode in the inode cache
1136 * @sb: super block of file system to search
1137 * @hashval: hash value (usually inode number) to search for
1138 * @test: callback used for comparisons between inodes
1139 * @data: opaque data pointer to pass to @test
1141 * Search for the inode specified by @hashval and @data in the inode cache,
1142 * and if the inode is in the cache, return the inode with an incremented
1143 * reference count. Waits on I_NEW before returning the inode.
1144 * returned with an incremented reference count.
1146 * This is a generalized version of ilookup() for file systems where the
1147 * inode number is not sufficient for unique identification of an inode.
1149 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1151 struct inode
*ilookup5(struct super_block
*sb
, unsigned long hashval
,
1152 int (*test
)(struct inode
*, void *), void *data
)
1154 struct inode
*inode
= ilookup5_nowait(sb
, hashval
, test
, data
);
1157 wait_on_inode(inode
);
1160 EXPORT_SYMBOL(ilookup5
);
1163 * ilookup - search for an inode in the inode cache
1164 * @sb: super block of file system to search
1165 * @ino: inode number to search for
1167 * Search for the inode @ino in the inode cache, and if the inode is in the
1168 * cache, the inode is returned with an incremented reference count.
1170 struct inode
*ilookup(struct super_block
*sb
, unsigned long ino
)
1172 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1173 struct inode
*inode
;
1175 spin_lock(&inode_hash_lock
);
1176 inode
= find_inode_fast(sb
, head
, ino
);
1177 spin_unlock(&inode_hash_lock
);
1180 wait_on_inode(inode
);
1183 EXPORT_SYMBOL(ilookup
);
1185 int insert_inode_locked(struct inode
*inode
)
1187 struct super_block
*sb
= inode
->i_sb
;
1188 ino_t ino
= inode
->i_ino
;
1189 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1192 struct hlist_node
*node
;
1193 struct inode
*old
= NULL
;
1194 spin_lock(&inode_hash_lock
);
1195 hlist_for_each_entry(old
, node
, head
, i_hash
) {
1196 if (old
->i_ino
!= ino
)
1198 if (old
->i_sb
!= sb
)
1200 spin_lock(&old
->i_lock
);
1201 if (old
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
1202 spin_unlock(&old
->i_lock
);
1207 if (likely(!node
)) {
1208 spin_lock(&inode
->i_lock
);
1209 inode
->i_state
|= I_NEW
;
1210 hlist_add_head(&inode
->i_hash
, head
);
1211 spin_unlock(&inode
->i_lock
);
1212 spin_unlock(&inode_hash_lock
);
1216 spin_unlock(&old
->i_lock
);
1217 spin_unlock(&inode_hash_lock
);
1219 if (unlikely(!inode_unhashed(old
))) {
1226 EXPORT_SYMBOL(insert_inode_locked
);
1228 int insert_inode_locked4(struct inode
*inode
, unsigned long hashval
,
1229 int (*test
)(struct inode
*, void *), void *data
)
1231 struct super_block
*sb
= inode
->i_sb
;
1232 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1235 struct hlist_node
*node
;
1236 struct inode
*old
= NULL
;
1238 spin_lock(&inode_hash_lock
);
1239 hlist_for_each_entry(old
, node
, head
, i_hash
) {
1240 if (old
->i_sb
!= sb
)
1242 if (!test(old
, data
))
1244 spin_lock(&old
->i_lock
);
1245 if (old
->i_state
& (I_FREEING
|I_WILL_FREE
)) {
1246 spin_unlock(&old
->i_lock
);
1251 if (likely(!node
)) {
1252 spin_lock(&inode
->i_lock
);
1253 inode
->i_state
|= I_NEW
;
1254 hlist_add_head(&inode
->i_hash
, head
);
1255 spin_unlock(&inode
->i_lock
);
1256 spin_unlock(&inode_hash_lock
);
1260 spin_unlock(&old
->i_lock
);
1261 spin_unlock(&inode_hash_lock
);
1263 if (unlikely(!inode_unhashed(old
))) {
1270 EXPORT_SYMBOL(insert_inode_locked4
);
1273 int generic_delete_inode(struct inode
*inode
)
1277 EXPORT_SYMBOL(generic_delete_inode
);
1280 * Normal UNIX filesystem behaviour: delete the
1281 * inode when the usage count drops to zero, and
1284 int generic_drop_inode(struct inode
*inode
)
1286 return !inode
->i_nlink
|| inode_unhashed(inode
);
1288 EXPORT_SYMBOL_GPL(generic_drop_inode
);
1291 * Called when we're dropping the last reference
1294 * Call the FS "drop_inode()" function, defaulting to
1295 * the legacy UNIX filesystem behaviour. If it tells
1296 * us to evict inode, do so. Otherwise, retain inode
1297 * in cache if fs is alive, sync and evict if fs is
1300 static void iput_final(struct inode
*inode
)
1302 struct super_block
*sb
= inode
->i_sb
;
1303 const struct super_operations
*op
= inode
->i_sb
->s_op
;
1306 WARN_ON(inode
->i_state
& I_NEW
);
1309 drop
= op
->drop_inode(inode
);
1311 drop
= generic_drop_inode(inode
);
1313 if (!drop
&& (sb
->s_flags
& MS_ACTIVE
)) {
1314 inode
->i_state
|= I_REFERENCED
;
1315 if (!(inode
->i_state
& (I_DIRTY
|I_SYNC
)))
1316 inode_lru_list_add(inode
);
1317 spin_unlock(&inode
->i_lock
);
1322 inode
->i_state
|= I_WILL_FREE
;
1323 spin_unlock(&inode
->i_lock
);
1324 write_inode_now(inode
, 1);
1325 spin_lock(&inode
->i_lock
);
1326 WARN_ON(inode
->i_state
& I_NEW
);
1327 inode
->i_state
&= ~I_WILL_FREE
;
1330 inode
->i_state
|= I_FREEING
;
1331 inode_lru_list_del(inode
);
1332 spin_unlock(&inode
->i_lock
);
1338 * iput - put an inode
1339 * @inode: inode to put
1341 * Puts an inode, dropping its usage count. If the inode use count hits
1342 * zero, the inode is then freed and may also be destroyed.
1344 * Consequently, iput() can sleep.
1346 void iput(struct inode
*inode
)
1349 BUG_ON(inode
->i_state
& I_CLEAR
);
1351 if (atomic_dec_and_lock(&inode
->i_count
, &inode
->i_lock
))
1355 EXPORT_SYMBOL(iput
);
1358 * bmap - find a block number in a file
1359 * @inode: inode of file
1360 * @block: block to find
1362 * Returns the block number on the device holding the inode that
1363 * is the disk block number for the block of the file requested.
1364 * That is, asked for block 4 of inode 1 the function will return the
1365 * disk block relative to the disk start that holds that block of the
1368 sector_t
bmap(struct inode
*inode
, sector_t block
)
1371 if (inode
->i_mapping
->a_ops
->bmap
)
1372 res
= inode
->i_mapping
->a_ops
->bmap(inode
->i_mapping
, block
);
1375 EXPORT_SYMBOL(bmap
);
1378 * With relative atime, only update atime if the previous atime is
1379 * earlier than either the ctime or mtime or if at least a day has
1380 * passed since the last atime update.
1382 static int relatime_need_update(struct vfsmount
*mnt
, struct inode
*inode
,
1383 struct timespec now
)
1386 if (!(mnt
->mnt_flags
& MNT_RELATIME
))
1389 * Is mtime younger than atime? If yes, update atime:
1391 if (timespec_compare(&inode
->i_mtime
, &inode
->i_atime
) >= 0)
1394 * Is ctime younger than atime? If yes, update atime:
1396 if (timespec_compare(&inode
->i_ctime
, &inode
->i_atime
) >= 0)
1400 * Is the previous atime value older than a day? If yes,
1403 if ((long)(now
.tv_sec
- inode
->i_atime
.tv_sec
) >= 24*60*60)
1406 * Good, we can skip the atime update:
1412 * touch_atime - update the access time
1413 * @mnt: mount the inode is accessed on
1414 * @dentry: dentry accessed
1416 * Update the accessed time on an inode and mark it for writeback.
1417 * This function automatically handles read only file systems and media,
1418 * as well as the "noatime" flag and inode specific "noatime" markers.
1420 void touch_atime(struct vfsmount
*mnt
, struct dentry
*dentry
)
1422 struct inode
*inode
= dentry
->d_inode
;
1423 struct timespec now
;
1425 if (inode
->i_flags
& S_NOATIME
)
1427 if (IS_NOATIME(inode
))
1429 if ((inode
->i_sb
->s_flags
& MS_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1432 if (mnt
->mnt_flags
& MNT_NOATIME
)
1434 if ((mnt
->mnt_flags
& MNT_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1437 now
= current_fs_time(inode
->i_sb
);
1439 if (!relatime_need_update(mnt
, inode
, now
))
1442 if (timespec_equal(&inode
->i_atime
, &now
))
1445 if (mnt_want_write(mnt
))
1448 inode
->i_atime
= now
;
1449 mark_inode_dirty_sync(inode
);
1450 mnt_drop_write(mnt
);
1452 EXPORT_SYMBOL(touch_atime
);
1455 * file_update_time - update mtime and ctime time
1456 * @file: file accessed
1458 * Update the mtime and ctime members of an inode and mark the inode
1459 * for writeback. Note that this function is meant exclusively for
1460 * usage in the file write path of filesystems, and filesystems may
1461 * choose to explicitly ignore update via this function with the
1462 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1463 * timestamps are handled by the server.
1466 void file_update_time(struct file
*file
)
1468 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1469 struct timespec now
;
1470 enum { S_MTIME
= 1, S_CTIME
= 2, S_VERSION
= 4 } sync_it
= 0;
1472 /* First try to exhaust all avenues to not sync */
1473 if (IS_NOCMTIME(inode
))
1476 now
= current_fs_time(inode
->i_sb
);
1477 if (!timespec_equal(&inode
->i_mtime
, &now
))
1480 if (!timespec_equal(&inode
->i_ctime
, &now
))
1483 if (IS_I_VERSION(inode
))
1484 sync_it
|= S_VERSION
;
1489 /* Finally allowed to write? Takes lock. */
1490 if (mnt_want_write_file(file
))
1493 /* Only change inode inside the lock region */
1494 if (sync_it
& S_VERSION
)
1495 inode_inc_iversion(inode
);
1496 if (sync_it
& S_CTIME
)
1497 inode
->i_ctime
= now
;
1498 if (sync_it
& S_MTIME
)
1499 inode
->i_mtime
= now
;
1500 mark_inode_dirty_sync(inode
);
1501 mnt_drop_write(file
->f_path
.mnt
);
1503 EXPORT_SYMBOL(file_update_time
);
1505 int inode_needs_sync(struct inode
*inode
)
1509 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
1513 EXPORT_SYMBOL(inode_needs_sync
);
1515 int inode_wait(void *word
)
1520 EXPORT_SYMBOL(inode_wait
);
1523 * If we try to find an inode in the inode hash while it is being
1524 * deleted, we have to wait until the filesystem completes its
1525 * deletion before reporting that it isn't found. This function waits
1526 * until the deletion _might_ have completed. Callers are responsible
1527 * to recheck inode state.
1529 * It doesn't matter if I_NEW is not set initially, a call to
1530 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1533 static void __wait_on_freeing_inode(struct inode
*inode
)
1535 wait_queue_head_t
*wq
;
1536 DEFINE_WAIT_BIT(wait
, &inode
->i_state
, __I_NEW
);
1537 wq
= bit_waitqueue(&inode
->i_state
, __I_NEW
);
1538 prepare_to_wait(wq
, &wait
.wait
, TASK_UNINTERRUPTIBLE
);
1539 spin_unlock(&inode
->i_lock
);
1540 spin_unlock(&inode_hash_lock
);
1542 finish_wait(wq
, &wait
.wait
);
1543 spin_lock(&inode_hash_lock
);
1546 static __initdata
unsigned long ihash_entries
;
1547 static int __init
set_ihash_entries(char *str
)
1551 ihash_entries
= simple_strtoul(str
, &str
, 0);
1554 __setup("ihash_entries=", set_ihash_entries
);
1557 * Initialize the waitqueues and inode hash table.
1559 void __init
inode_init_early(void)
1563 /* If hashes are distributed across NUMA nodes, defer
1564 * hash allocation until vmalloc space is available.
1570 alloc_large_system_hash("Inode-cache",
1571 sizeof(struct hlist_head
),
1579 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1580 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1583 void __init
inode_init(void)
1587 /* inode slab cache */
1588 inode_cachep
= kmem_cache_create("inode_cache",
1589 sizeof(struct inode
),
1591 (SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|
1595 /* Hash may have been set up in inode_init_early */
1600 alloc_large_system_hash("Inode-cache",
1601 sizeof(struct hlist_head
),
1609 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1610 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1613 void init_special_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
1615 inode
->i_mode
= mode
;
1616 if (S_ISCHR(mode
)) {
1617 inode
->i_fop
= &def_chr_fops
;
1618 inode
->i_rdev
= rdev
;
1619 } else if (S_ISBLK(mode
)) {
1620 inode
->i_fop
= &def_blk_fops
;
1621 inode
->i_rdev
= rdev
;
1622 } else if (S_ISFIFO(mode
))
1623 inode
->i_fop
= &def_fifo_fops
;
1624 else if (S_ISSOCK(mode
))
1625 inode
->i_fop
= &bad_sock_fops
;
1627 printk(KERN_DEBUG
"init_special_inode: bogus i_mode (%o) for"
1628 " inode %s:%lu\n", mode
, inode
->i_sb
->s_id
,
1631 EXPORT_SYMBOL(init_special_inode
);
1634 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
1636 * @dir: Directory inode
1637 * @mode: mode of the new inode
1639 void inode_init_owner(struct inode
*inode
, const struct inode
*dir
,
1642 inode
->i_uid
= current_fsuid();
1643 if (dir
&& dir
->i_mode
& S_ISGID
) {
1644 inode
->i_gid
= dir
->i_gid
;
1648 inode
->i_gid
= current_fsgid();
1649 inode
->i_mode
= mode
;
1651 EXPORT_SYMBOL(inode_init_owner
);
1654 * inode_owner_or_capable - check current task permissions to inode
1655 * @inode: inode being checked
1657 * Return true if current either has CAP_FOWNER to the inode, or
1660 bool inode_owner_or_capable(const struct inode
*inode
)
1662 struct user_namespace
*ns
= inode_userns(inode
);
1664 if (current_user_ns() == ns
&& current_fsuid() == inode
->i_uid
)
1666 if (ns_capable(ns
, CAP_FOWNER
))
1670 EXPORT_SYMBOL(inode_owner_or_capable
);