Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/wfg/writeback
[linux-2.6/kvm.git] / fs / inode.c
bloba48fa5355fb44ef5a8e5a3a1acbcdaea095d829e
1 /*
2 * (C) 1997 Linus Torvalds
3 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
4 */
5 #include <linux/fs.h>
6 #include <linux/mm.h>
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 */
29 #include "internal.h"
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
45 * Lock ordering:
47 * inode_sb_list_lock
48 * inode->i_lock
49 * inode->i_sb->s_inode_lru_lock
51 * bdi->wb.list_lock
52 * inode->i_lock
54 * inode_hash_lock
55 * inode_sb_list_lock
56 * inode->i_lock
58 * iunique_lock
59 * inode_hash_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)
89 int i;
90 int sum = 0;
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)
98 int i;
99 int sum = 0;
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
115 #ifdef CONFIG_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);
123 #endif
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;
139 inode->i_sb = sb;
140 inode->i_blkbits = sb->s_blocksize_bits;
141 inode->i_flags = 0;
142 atomic_set(&inode->i_count, 1);
143 inode->i_op = &empty_iops;
144 inode->i_fop = &empty_fops;
145 inode->i_nlink = 1;
146 inode->i_uid = 0;
147 inode->i_gid = 0;
148 atomic_set(&inode->i_writecount, 0);
149 inode->i_size = 0;
150 inode->i_blocks = 0;
151 inode->i_bytes = 0;
152 inode->i_generation = 0;
153 #ifdef CONFIG_QUOTA
154 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
155 #endif
156 inode->i_pipe = NULL;
157 inode->i_bdev = NULL;
158 inode->i_cdev = NULL;
159 inode->i_rdev = 0;
160 inode->dirtied_when = 0;
162 if (security_inode_alloc(inode))
163 goto out;
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;
174 mapping->flags = 0;
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
183 * backing_dev_info.
185 if (sb->s_bdev) {
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;
195 #endif
197 #ifdef CONFIG_FSNOTIFY
198 inode->i_fsnotify_mask = 0;
199 #endif
201 this_cpu_inc(nr_inodes);
203 return 0;
204 out:
205 return -ENOMEM;
207 EXPORT_SYMBOL(inode_init_always);
209 static struct inode *alloc_inode(struct super_block *sb)
211 struct inode *inode;
213 if (sb->s_op->alloc_inode)
214 inode = sb->s_op->alloc_inode(sb);
215 else
216 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
218 if (!inode)
219 return NULL;
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);
224 else
225 kmem_cache_free(inode_cachep, inode);
226 return NULL;
229 return 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);
248 #endif
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);
266 else
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);
300 #endif
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 spin_lock(&inode_sb_list_lock);
365 list_del_init(&inode->i_sb_list);
366 spin_unlock(&inode_sb_list_lock);
369 static unsigned long hash(struct super_block *sb, unsigned long hashval)
371 unsigned long tmp;
373 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
374 L1_CACHE_BYTES;
375 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
376 return tmp & i_hash_mask;
380 * __insert_inode_hash - hash an inode
381 * @inode: unhashed inode
382 * @hashval: unsigned long value used to locate this object in the
383 * inode_hashtable.
385 * Add an inode to the inode hash for this superblock.
387 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
389 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
391 spin_lock(&inode_hash_lock);
392 spin_lock(&inode->i_lock);
393 hlist_add_head(&inode->i_hash, b);
394 spin_unlock(&inode->i_lock);
395 spin_unlock(&inode_hash_lock);
397 EXPORT_SYMBOL(__insert_inode_hash);
400 * remove_inode_hash - remove an inode from the hash
401 * @inode: inode to unhash
403 * Remove an inode from the superblock.
405 void remove_inode_hash(struct inode *inode)
407 spin_lock(&inode_hash_lock);
408 spin_lock(&inode->i_lock);
409 hlist_del_init(&inode->i_hash);
410 spin_unlock(&inode->i_lock);
411 spin_unlock(&inode_hash_lock);
413 EXPORT_SYMBOL(remove_inode_hash);
415 void end_writeback(struct inode *inode)
417 might_sleep();
419 * We have to cycle tree_lock here because reclaim can be still in the
420 * process of removing the last page (in __delete_from_page_cache())
421 * and we must not free mapping under it.
423 spin_lock_irq(&inode->i_data.tree_lock);
424 BUG_ON(inode->i_data.nrpages);
425 spin_unlock_irq(&inode->i_data.tree_lock);
426 BUG_ON(!list_empty(&inode->i_data.private_list));
427 BUG_ON(!(inode->i_state & I_FREEING));
428 BUG_ON(inode->i_state & I_CLEAR);
429 inode_sync_wait(inode);
430 /* don't need i_lock here, no concurrent mods to i_state */
431 inode->i_state = I_FREEING | I_CLEAR;
433 EXPORT_SYMBOL(end_writeback);
436 * Free the inode passed in, removing it from the lists it is still connected
437 * to. We remove any pages still attached to the inode and wait for any IO that
438 * is still in progress before finally destroying the inode.
440 * An inode must already be marked I_FREEING so that we avoid the inode being
441 * moved back onto lists if we race with other code that manipulates the lists
442 * (e.g. writeback_single_inode). The caller is responsible for setting this.
444 * An inode must already be removed from the LRU list before being evicted from
445 * the cache. This should occur atomically with setting the I_FREEING state
446 * flag, so no inodes here should ever be on the LRU when being evicted.
448 static void evict(struct inode *inode)
450 const struct super_operations *op = inode->i_sb->s_op;
452 BUG_ON(!(inode->i_state & I_FREEING));
453 BUG_ON(!list_empty(&inode->i_lru));
455 inode_wb_list_del(inode);
456 inode_sb_list_del(inode);
458 if (op->evict_inode) {
459 op->evict_inode(inode);
460 } else {
461 if (inode->i_data.nrpages)
462 truncate_inode_pages(&inode->i_data, 0);
463 end_writeback(inode);
465 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
466 bd_forget(inode);
467 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
468 cd_forget(inode);
470 remove_inode_hash(inode);
472 spin_lock(&inode->i_lock);
473 wake_up_bit(&inode->i_state, __I_NEW);
474 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
475 spin_unlock(&inode->i_lock);
477 destroy_inode(inode);
481 * dispose_list - dispose of the contents of a local list
482 * @head: the head of the list to free
484 * Dispose-list gets a local list with local inodes in it, so it doesn't
485 * need to worry about list corruption and SMP locks.
487 static void dispose_list(struct list_head *head)
489 while (!list_empty(head)) {
490 struct inode *inode;
492 inode = list_first_entry(head, struct inode, i_lru);
493 list_del_init(&inode->i_lru);
495 evict(inode);
500 * evict_inodes - evict all evictable inodes for a superblock
501 * @sb: superblock to operate on
503 * Make sure that no inodes with zero refcount are retained. This is
504 * called by superblock shutdown after having MS_ACTIVE flag removed,
505 * so any inode reaching zero refcount during or after that call will
506 * be immediately evicted.
508 void evict_inodes(struct super_block *sb)
510 struct inode *inode, *next;
511 LIST_HEAD(dispose);
513 spin_lock(&inode_sb_list_lock);
514 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
515 if (atomic_read(&inode->i_count))
516 continue;
518 spin_lock(&inode->i_lock);
519 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
520 spin_unlock(&inode->i_lock);
521 continue;
524 inode->i_state |= I_FREEING;
525 inode_lru_list_del(inode);
526 spin_unlock(&inode->i_lock);
527 list_add(&inode->i_lru, &dispose);
529 spin_unlock(&inode_sb_list_lock);
531 dispose_list(&dispose);
535 * invalidate_inodes - attempt to free all inodes on a superblock
536 * @sb: superblock to operate on
537 * @kill_dirty: flag to guide handling of dirty inodes
539 * Attempts to free all inodes for a given superblock. If there were any
540 * busy inodes return a non-zero value, else zero.
541 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
542 * them as busy.
544 int invalidate_inodes(struct super_block *sb, bool kill_dirty)
546 int busy = 0;
547 struct inode *inode, *next;
548 LIST_HEAD(dispose);
550 spin_lock(&inode_sb_list_lock);
551 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
552 spin_lock(&inode->i_lock);
553 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
554 spin_unlock(&inode->i_lock);
555 continue;
557 if (inode->i_state & I_DIRTY && !kill_dirty) {
558 spin_unlock(&inode->i_lock);
559 busy = 1;
560 continue;
562 if (atomic_read(&inode->i_count)) {
563 spin_unlock(&inode->i_lock);
564 busy = 1;
565 continue;
568 inode->i_state |= I_FREEING;
569 inode_lru_list_del(inode);
570 spin_unlock(&inode->i_lock);
571 list_add(&inode->i_lru, &dispose);
573 spin_unlock(&inode_sb_list_lock);
575 dispose_list(&dispose);
577 return busy;
580 static int can_unuse(struct inode *inode)
582 if (inode->i_state & ~I_REFERENCED)
583 return 0;
584 if (inode_has_buffers(inode))
585 return 0;
586 if (atomic_read(&inode->i_count))
587 return 0;
588 if (inode->i_data.nrpages)
589 return 0;
590 return 1;
594 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
595 * This is called from the superblock shrinker function with a number of inodes
596 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
597 * then are freed outside inode_lock by dispose_list().
599 * Any inodes which are pinned purely because of attached pagecache have their
600 * pagecache removed. If the inode has metadata buffers attached to
601 * mapping->private_list then try to remove them.
603 * If the inode has the I_REFERENCED flag set, then it means that it has been
604 * used recently - the flag is set in iput_final(). When we encounter such an
605 * inode, clear the flag and move it to the back of the LRU so it gets another
606 * pass through the LRU before it gets reclaimed. This is necessary because of
607 * the fact we are doing lazy LRU updates to minimise lock contention so the
608 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
609 * with this flag set because they are the inodes that are out of order.
611 void prune_icache_sb(struct super_block *sb, int nr_to_scan)
613 LIST_HEAD(freeable);
614 int nr_scanned;
615 unsigned long reap = 0;
617 spin_lock(&sb->s_inode_lru_lock);
618 for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
619 struct inode *inode;
621 if (list_empty(&sb->s_inode_lru))
622 break;
624 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
627 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
628 * so use a trylock. If we fail to get the lock, just move the
629 * inode to the back of the list so we don't spin on it.
631 if (!spin_trylock(&inode->i_lock)) {
632 list_move(&inode->i_lru, &sb->s_inode_lru);
633 continue;
637 * Referenced or dirty inodes are still in use. Give them
638 * another pass through the LRU as we canot reclaim them now.
640 if (atomic_read(&inode->i_count) ||
641 (inode->i_state & ~I_REFERENCED)) {
642 list_del_init(&inode->i_lru);
643 spin_unlock(&inode->i_lock);
644 sb->s_nr_inodes_unused--;
645 this_cpu_dec(nr_unused);
646 continue;
649 /* recently referenced inodes get one more pass */
650 if (inode->i_state & I_REFERENCED) {
651 inode->i_state &= ~I_REFERENCED;
652 list_move(&inode->i_lru, &sb->s_inode_lru);
653 spin_unlock(&inode->i_lock);
654 continue;
656 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
657 __iget(inode);
658 spin_unlock(&inode->i_lock);
659 spin_unlock(&sb->s_inode_lru_lock);
660 if (remove_inode_buffers(inode))
661 reap += invalidate_mapping_pages(&inode->i_data,
662 0, -1);
663 iput(inode);
664 spin_lock(&sb->s_inode_lru_lock);
666 if (inode != list_entry(sb->s_inode_lru.next,
667 struct inode, i_lru))
668 continue; /* wrong inode or list_empty */
669 /* avoid lock inversions with trylock */
670 if (!spin_trylock(&inode->i_lock))
671 continue;
672 if (!can_unuse(inode)) {
673 spin_unlock(&inode->i_lock);
674 continue;
677 WARN_ON(inode->i_state & I_NEW);
678 inode->i_state |= I_FREEING;
679 spin_unlock(&inode->i_lock);
681 list_move(&inode->i_lru, &freeable);
682 sb->s_nr_inodes_unused--;
683 this_cpu_dec(nr_unused);
685 if (current_is_kswapd())
686 __count_vm_events(KSWAPD_INODESTEAL, reap);
687 else
688 __count_vm_events(PGINODESTEAL, reap);
689 spin_unlock(&sb->s_inode_lru_lock);
691 dispose_list(&freeable);
694 static void __wait_on_freeing_inode(struct inode *inode);
696 * Called with the inode lock held.
698 static struct inode *find_inode(struct super_block *sb,
699 struct hlist_head *head,
700 int (*test)(struct inode *, void *),
701 void *data)
703 struct hlist_node *node;
704 struct inode *inode = NULL;
706 repeat:
707 hlist_for_each_entry(inode, node, head, i_hash) {
708 spin_lock(&inode->i_lock);
709 if (inode->i_sb != sb) {
710 spin_unlock(&inode->i_lock);
711 continue;
713 if (!test(inode, data)) {
714 spin_unlock(&inode->i_lock);
715 continue;
717 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
718 __wait_on_freeing_inode(inode);
719 goto repeat;
721 __iget(inode);
722 spin_unlock(&inode->i_lock);
723 return inode;
725 return NULL;
729 * find_inode_fast is the fast path version of find_inode, see the comment at
730 * iget_locked for details.
732 static struct inode *find_inode_fast(struct super_block *sb,
733 struct hlist_head *head, unsigned long ino)
735 struct hlist_node *node;
736 struct inode *inode = NULL;
738 repeat:
739 hlist_for_each_entry(inode, node, head, i_hash) {
740 spin_lock(&inode->i_lock);
741 if (inode->i_ino != ino) {
742 spin_unlock(&inode->i_lock);
743 continue;
745 if (inode->i_sb != sb) {
746 spin_unlock(&inode->i_lock);
747 continue;
749 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
750 __wait_on_freeing_inode(inode);
751 goto repeat;
753 __iget(inode);
754 spin_unlock(&inode->i_lock);
755 return inode;
757 return NULL;
761 * Each cpu owns a range of LAST_INO_BATCH numbers.
762 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
763 * to renew the exhausted range.
765 * This does not significantly increase overflow rate because every CPU can
766 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
767 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
768 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
769 * overflow rate by 2x, which does not seem too significant.
771 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
772 * error if st_ino won't fit in target struct field. Use 32bit counter
773 * here to attempt to avoid that.
775 #define LAST_INO_BATCH 1024
776 static DEFINE_PER_CPU(unsigned int, last_ino);
778 unsigned int get_next_ino(void)
780 unsigned int *p = &get_cpu_var(last_ino);
781 unsigned int res = *p;
783 #ifdef CONFIG_SMP
784 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
785 static atomic_t shared_last_ino;
786 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
788 res = next - LAST_INO_BATCH;
790 #endif
792 *p = ++res;
793 put_cpu_var(last_ino);
794 return res;
796 EXPORT_SYMBOL(get_next_ino);
799 * new_inode - obtain an inode
800 * @sb: superblock
802 * Allocates a new inode for given superblock. The default gfp_mask
803 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
804 * If HIGHMEM pages are unsuitable or it is known that pages allocated
805 * for the page cache are not reclaimable or migratable,
806 * mapping_set_gfp_mask() must be called with suitable flags on the
807 * newly created inode's mapping
810 struct inode *new_inode(struct super_block *sb)
812 struct inode *inode;
814 spin_lock_prefetch(&inode_sb_list_lock);
816 inode = alloc_inode(sb);
817 if (inode) {
818 spin_lock(&inode->i_lock);
819 inode->i_state = 0;
820 spin_unlock(&inode->i_lock);
821 inode_sb_list_add(inode);
823 return inode;
825 EXPORT_SYMBOL(new_inode);
828 * unlock_new_inode - clear the I_NEW state and wake up any waiters
829 * @inode: new inode to unlock
831 * Called when the inode is fully initialised to clear the new state of the
832 * inode and wake up anyone waiting for the inode to finish initialisation.
834 void unlock_new_inode(struct inode *inode)
836 #ifdef CONFIG_DEBUG_LOCK_ALLOC
837 if (S_ISDIR(inode->i_mode)) {
838 struct file_system_type *type = inode->i_sb->s_type;
840 /* Set new key only if filesystem hasn't already changed it */
841 if (!lockdep_match_class(&inode->i_mutex,
842 &type->i_mutex_key)) {
844 * ensure nobody is actually holding i_mutex
846 mutex_destroy(&inode->i_mutex);
847 mutex_init(&inode->i_mutex);
848 lockdep_set_class(&inode->i_mutex,
849 &type->i_mutex_dir_key);
852 #endif
853 spin_lock(&inode->i_lock);
854 WARN_ON(!(inode->i_state & I_NEW));
855 inode->i_state &= ~I_NEW;
856 wake_up_bit(&inode->i_state, __I_NEW);
857 spin_unlock(&inode->i_lock);
859 EXPORT_SYMBOL(unlock_new_inode);
862 * iget5_locked - obtain an inode from a mounted file system
863 * @sb: super block of file system
864 * @hashval: hash value (usually inode number) to get
865 * @test: callback used for comparisons between inodes
866 * @set: callback used to initialize a new struct inode
867 * @data: opaque data pointer to pass to @test and @set
869 * Search for the inode specified by @hashval and @data in the inode cache,
870 * and if present it is return it with an increased reference count. This is
871 * a generalized version of iget_locked() for file systems where the inode
872 * number is not sufficient for unique identification of an inode.
874 * If the inode is not in cache, allocate a new inode and return it locked,
875 * hashed, and with the I_NEW flag set. The file system gets to fill it in
876 * before unlocking it via unlock_new_inode().
878 * Note both @test and @set are called with the inode_hash_lock held, so can't
879 * sleep.
881 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
882 int (*test)(struct inode *, void *),
883 int (*set)(struct inode *, void *), void *data)
885 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
886 struct inode *inode;
888 spin_lock(&inode_hash_lock);
889 inode = find_inode(sb, head, test, data);
890 spin_unlock(&inode_hash_lock);
892 if (inode) {
893 wait_on_inode(inode);
894 return inode;
897 inode = alloc_inode(sb);
898 if (inode) {
899 struct inode *old;
901 spin_lock(&inode_hash_lock);
902 /* We released the lock, so.. */
903 old = find_inode(sb, head, test, data);
904 if (!old) {
905 if (set(inode, data))
906 goto set_failed;
908 spin_lock(&inode->i_lock);
909 inode->i_state = I_NEW;
910 hlist_add_head(&inode->i_hash, head);
911 spin_unlock(&inode->i_lock);
912 inode_sb_list_add(inode);
913 spin_unlock(&inode_hash_lock);
915 /* Return the locked inode with I_NEW set, the
916 * caller is responsible for filling in the contents
918 return inode;
922 * Uhhuh, somebody else created the same inode under
923 * us. Use the old inode instead of the one we just
924 * allocated.
926 spin_unlock(&inode_hash_lock);
927 destroy_inode(inode);
928 inode = old;
929 wait_on_inode(inode);
931 return inode;
933 set_failed:
934 spin_unlock(&inode_hash_lock);
935 destroy_inode(inode);
936 return NULL;
938 EXPORT_SYMBOL(iget5_locked);
941 * iget_locked - obtain an inode from a mounted file system
942 * @sb: super block of file system
943 * @ino: inode number to get
945 * Search for the inode specified by @ino in the inode cache and if present
946 * return it with an increased reference count. This is for file systems
947 * where the inode number is sufficient for unique identification of an inode.
949 * If the inode is not in cache, allocate a new inode and return it locked,
950 * hashed, and with the I_NEW flag set. The file system gets to fill it in
951 * before unlocking it via unlock_new_inode().
953 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
955 struct hlist_head *head = inode_hashtable + hash(sb, ino);
956 struct inode *inode;
958 spin_lock(&inode_hash_lock);
959 inode = find_inode_fast(sb, head, ino);
960 spin_unlock(&inode_hash_lock);
961 if (inode) {
962 wait_on_inode(inode);
963 return inode;
966 inode = alloc_inode(sb);
967 if (inode) {
968 struct inode *old;
970 spin_lock(&inode_hash_lock);
971 /* We released the lock, so.. */
972 old = find_inode_fast(sb, head, ino);
973 if (!old) {
974 inode->i_ino = ino;
975 spin_lock(&inode->i_lock);
976 inode->i_state = I_NEW;
977 hlist_add_head(&inode->i_hash, head);
978 spin_unlock(&inode->i_lock);
979 inode_sb_list_add(inode);
980 spin_unlock(&inode_hash_lock);
982 /* Return the locked inode with I_NEW set, the
983 * caller is responsible for filling in the contents
985 return inode;
989 * Uhhuh, somebody else created the same inode under
990 * us. Use the old inode instead of the one we just
991 * allocated.
993 spin_unlock(&inode_hash_lock);
994 destroy_inode(inode);
995 inode = old;
996 wait_on_inode(inode);
998 return inode;
1000 EXPORT_SYMBOL(iget_locked);
1003 * search the inode cache for a matching inode number.
1004 * If we find one, then the inode number we are trying to
1005 * allocate is not unique and so we should not use it.
1007 * Returns 1 if the inode number is unique, 0 if it is not.
1009 static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1011 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1012 struct hlist_node *node;
1013 struct inode *inode;
1015 spin_lock(&inode_hash_lock);
1016 hlist_for_each_entry(inode, node, b, i_hash) {
1017 if (inode->i_ino == ino && inode->i_sb == sb) {
1018 spin_unlock(&inode_hash_lock);
1019 return 0;
1022 spin_unlock(&inode_hash_lock);
1024 return 1;
1028 * iunique - get a unique inode number
1029 * @sb: superblock
1030 * @max_reserved: highest reserved inode number
1032 * Obtain an inode number that is unique on the system for a given
1033 * superblock. This is used by file systems that have no natural
1034 * permanent inode numbering system. An inode number is returned that
1035 * is higher than the reserved limit but unique.
1037 * BUGS:
1038 * With a large number of inodes live on the file system this function
1039 * currently becomes quite slow.
1041 ino_t iunique(struct super_block *sb, ino_t max_reserved)
1044 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1045 * error if st_ino won't fit in target struct field. Use 32bit counter
1046 * here to attempt to avoid that.
1048 static DEFINE_SPINLOCK(iunique_lock);
1049 static unsigned int counter;
1050 ino_t res;
1052 spin_lock(&iunique_lock);
1053 do {
1054 if (counter <= max_reserved)
1055 counter = max_reserved + 1;
1056 res = counter++;
1057 } while (!test_inode_iunique(sb, res));
1058 spin_unlock(&iunique_lock);
1060 return res;
1062 EXPORT_SYMBOL(iunique);
1064 struct inode *igrab(struct inode *inode)
1066 spin_lock(&inode->i_lock);
1067 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1068 __iget(inode);
1069 spin_unlock(&inode->i_lock);
1070 } else {
1071 spin_unlock(&inode->i_lock);
1073 * Handle the case where s_op->clear_inode is not been
1074 * called yet, and somebody is calling igrab
1075 * while the inode is getting freed.
1077 inode = NULL;
1079 return inode;
1081 EXPORT_SYMBOL(igrab);
1084 * ilookup5_nowait - search for an inode in the inode cache
1085 * @sb: super block of file system to search
1086 * @hashval: hash value (usually inode number) to search for
1087 * @test: callback used for comparisons between inodes
1088 * @data: opaque data pointer to pass to @test
1090 * Search for the inode specified by @hashval and @data in the inode cache.
1091 * If the inode is in the cache, the inode is returned with an incremented
1092 * reference count.
1094 * Note: I_NEW is not waited upon so you have to be very careful what you do
1095 * with the returned inode. You probably should be using ilookup5() instead.
1097 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1099 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1100 int (*test)(struct inode *, void *), void *data)
1102 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1103 struct inode *inode;
1105 spin_lock(&inode_hash_lock);
1106 inode = find_inode(sb, head, test, data);
1107 spin_unlock(&inode_hash_lock);
1109 return inode;
1111 EXPORT_SYMBOL(ilookup5_nowait);
1114 * ilookup5 - search for an inode in the inode cache
1115 * @sb: super block of file system to search
1116 * @hashval: hash value (usually inode number) to search for
1117 * @test: callback used for comparisons between inodes
1118 * @data: opaque data pointer to pass to @test
1120 * Search for the inode specified by @hashval and @data in the inode cache,
1121 * and if the inode is in the cache, return the inode with an incremented
1122 * reference count. Waits on I_NEW before returning the inode.
1123 * returned with an incremented reference count.
1125 * This is a generalized version of ilookup() for file systems where the
1126 * inode number is not sufficient for unique identification of an inode.
1128 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1130 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1131 int (*test)(struct inode *, void *), void *data)
1133 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1135 if (inode)
1136 wait_on_inode(inode);
1137 return inode;
1139 EXPORT_SYMBOL(ilookup5);
1142 * ilookup - search for an inode in the inode cache
1143 * @sb: super block of file system to search
1144 * @ino: inode number to search for
1146 * Search for the inode @ino in the inode cache, and if the inode is in the
1147 * cache, the inode is returned with an incremented reference count.
1149 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1151 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1152 struct inode *inode;
1154 spin_lock(&inode_hash_lock);
1155 inode = find_inode_fast(sb, head, ino);
1156 spin_unlock(&inode_hash_lock);
1158 if (inode)
1159 wait_on_inode(inode);
1160 return inode;
1162 EXPORT_SYMBOL(ilookup);
1164 int insert_inode_locked(struct inode *inode)
1166 struct super_block *sb = inode->i_sb;
1167 ino_t ino = inode->i_ino;
1168 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1170 while (1) {
1171 struct hlist_node *node;
1172 struct inode *old = NULL;
1173 spin_lock(&inode_hash_lock);
1174 hlist_for_each_entry(old, node, head, i_hash) {
1175 if (old->i_ino != ino)
1176 continue;
1177 if (old->i_sb != sb)
1178 continue;
1179 spin_lock(&old->i_lock);
1180 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1181 spin_unlock(&old->i_lock);
1182 continue;
1184 break;
1186 if (likely(!node)) {
1187 spin_lock(&inode->i_lock);
1188 inode->i_state |= I_NEW;
1189 hlist_add_head(&inode->i_hash, head);
1190 spin_unlock(&inode->i_lock);
1191 spin_unlock(&inode_hash_lock);
1192 return 0;
1194 __iget(old);
1195 spin_unlock(&old->i_lock);
1196 spin_unlock(&inode_hash_lock);
1197 wait_on_inode(old);
1198 if (unlikely(!inode_unhashed(old))) {
1199 iput(old);
1200 return -EBUSY;
1202 iput(old);
1205 EXPORT_SYMBOL(insert_inode_locked);
1207 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1208 int (*test)(struct inode *, void *), void *data)
1210 struct super_block *sb = inode->i_sb;
1211 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1213 while (1) {
1214 struct hlist_node *node;
1215 struct inode *old = NULL;
1217 spin_lock(&inode_hash_lock);
1218 hlist_for_each_entry(old, node, head, i_hash) {
1219 if (old->i_sb != sb)
1220 continue;
1221 if (!test(old, data))
1222 continue;
1223 spin_lock(&old->i_lock);
1224 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1225 spin_unlock(&old->i_lock);
1226 continue;
1228 break;
1230 if (likely(!node)) {
1231 spin_lock(&inode->i_lock);
1232 inode->i_state |= I_NEW;
1233 hlist_add_head(&inode->i_hash, head);
1234 spin_unlock(&inode->i_lock);
1235 spin_unlock(&inode_hash_lock);
1236 return 0;
1238 __iget(old);
1239 spin_unlock(&old->i_lock);
1240 spin_unlock(&inode_hash_lock);
1241 wait_on_inode(old);
1242 if (unlikely(!inode_unhashed(old))) {
1243 iput(old);
1244 return -EBUSY;
1246 iput(old);
1249 EXPORT_SYMBOL(insert_inode_locked4);
1252 int generic_delete_inode(struct inode *inode)
1254 return 1;
1256 EXPORT_SYMBOL(generic_delete_inode);
1259 * Normal UNIX filesystem behaviour: delete the
1260 * inode when the usage count drops to zero, and
1261 * i_nlink is zero.
1263 int generic_drop_inode(struct inode *inode)
1265 return !inode->i_nlink || inode_unhashed(inode);
1267 EXPORT_SYMBOL_GPL(generic_drop_inode);
1270 * Called when we're dropping the last reference
1271 * to an inode.
1273 * Call the FS "drop_inode()" function, defaulting to
1274 * the legacy UNIX filesystem behaviour. If it tells
1275 * us to evict inode, do so. Otherwise, retain inode
1276 * in cache if fs is alive, sync and evict if fs is
1277 * shutting down.
1279 static void iput_final(struct inode *inode)
1281 struct super_block *sb = inode->i_sb;
1282 const struct super_operations *op = inode->i_sb->s_op;
1283 int drop;
1285 WARN_ON(inode->i_state & I_NEW);
1287 if (op->drop_inode)
1288 drop = op->drop_inode(inode);
1289 else
1290 drop = generic_drop_inode(inode);
1292 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1293 inode->i_state |= I_REFERENCED;
1294 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1295 inode_lru_list_add(inode);
1296 spin_unlock(&inode->i_lock);
1297 return;
1300 if (!drop) {
1301 inode->i_state |= I_WILL_FREE;
1302 spin_unlock(&inode->i_lock);
1303 write_inode_now(inode, 1);
1304 spin_lock(&inode->i_lock);
1305 WARN_ON(inode->i_state & I_NEW);
1306 inode->i_state &= ~I_WILL_FREE;
1309 inode->i_state |= I_FREEING;
1310 inode_lru_list_del(inode);
1311 spin_unlock(&inode->i_lock);
1313 evict(inode);
1317 * iput - put an inode
1318 * @inode: inode to put
1320 * Puts an inode, dropping its usage count. If the inode use count hits
1321 * zero, the inode is then freed and may also be destroyed.
1323 * Consequently, iput() can sleep.
1325 void iput(struct inode *inode)
1327 if (inode) {
1328 BUG_ON(inode->i_state & I_CLEAR);
1330 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
1331 iput_final(inode);
1334 EXPORT_SYMBOL(iput);
1337 * bmap - find a block number in a file
1338 * @inode: inode of file
1339 * @block: block to find
1341 * Returns the block number on the device holding the inode that
1342 * is the disk block number for the block of the file requested.
1343 * That is, asked for block 4 of inode 1 the function will return the
1344 * disk block relative to the disk start that holds that block of the
1345 * file.
1347 sector_t bmap(struct inode *inode, sector_t block)
1349 sector_t res = 0;
1350 if (inode->i_mapping->a_ops->bmap)
1351 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1352 return res;
1354 EXPORT_SYMBOL(bmap);
1357 * With relative atime, only update atime if the previous atime is
1358 * earlier than either the ctime or mtime or if at least a day has
1359 * passed since the last atime update.
1361 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1362 struct timespec now)
1365 if (!(mnt->mnt_flags & MNT_RELATIME))
1366 return 1;
1368 * Is mtime younger than atime? If yes, update atime:
1370 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1371 return 1;
1373 * Is ctime younger than atime? If yes, update atime:
1375 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1376 return 1;
1379 * Is the previous atime value older than a day? If yes,
1380 * update atime:
1382 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1383 return 1;
1385 * Good, we can skip the atime update:
1387 return 0;
1391 * touch_atime - update the access time
1392 * @mnt: mount the inode is accessed on
1393 * @dentry: dentry accessed
1395 * Update the accessed time on an inode and mark it for writeback.
1396 * This function automatically handles read only file systems and media,
1397 * as well as the "noatime" flag and inode specific "noatime" markers.
1399 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1401 struct inode *inode = dentry->d_inode;
1402 struct timespec now;
1404 if (inode->i_flags & S_NOATIME)
1405 return;
1406 if (IS_NOATIME(inode))
1407 return;
1408 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1409 return;
1411 if (mnt->mnt_flags & MNT_NOATIME)
1412 return;
1413 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1414 return;
1416 now = current_fs_time(inode->i_sb);
1418 if (!relatime_need_update(mnt, inode, now))
1419 return;
1421 if (timespec_equal(&inode->i_atime, &now))
1422 return;
1424 if (mnt_want_write(mnt))
1425 return;
1427 inode->i_atime = now;
1428 mark_inode_dirty_sync(inode);
1429 mnt_drop_write(mnt);
1431 EXPORT_SYMBOL(touch_atime);
1434 * file_update_time - update mtime and ctime time
1435 * @file: file accessed
1437 * Update the mtime and ctime members of an inode and mark the inode
1438 * for writeback. Note that this function is meant exclusively for
1439 * usage in the file write path of filesystems, and filesystems may
1440 * choose to explicitly ignore update via this function with the
1441 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1442 * timestamps are handled by the server.
1445 void file_update_time(struct file *file)
1447 struct inode *inode = file->f_path.dentry->d_inode;
1448 struct timespec now;
1449 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1451 /* First try to exhaust all avenues to not sync */
1452 if (IS_NOCMTIME(inode))
1453 return;
1455 now = current_fs_time(inode->i_sb);
1456 if (!timespec_equal(&inode->i_mtime, &now))
1457 sync_it = S_MTIME;
1459 if (!timespec_equal(&inode->i_ctime, &now))
1460 sync_it |= S_CTIME;
1462 if (IS_I_VERSION(inode))
1463 sync_it |= S_VERSION;
1465 if (!sync_it)
1466 return;
1468 /* Finally allowed to write? Takes lock. */
1469 if (mnt_want_write_file(file))
1470 return;
1472 /* Only change inode inside the lock region */
1473 if (sync_it & S_VERSION)
1474 inode_inc_iversion(inode);
1475 if (sync_it & S_CTIME)
1476 inode->i_ctime = now;
1477 if (sync_it & S_MTIME)
1478 inode->i_mtime = now;
1479 mark_inode_dirty_sync(inode);
1480 mnt_drop_write(file->f_path.mnt);
1482 EXPORT_SYMBOL(file_update_time);
1484 int inode_needs_sync(struct inode *inode)
1486 if (IS_SYNC(inode))
1487 return 1;
1488 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1489 return 1;
1490 return 0;
1492 EXPORT_SYMBOL(inode_needs_sync);
1494 int inode_wait(void *word)
1496 schedule();
1497 return 0;
1499 EXPORT_SYMBOL(inode_wait);
1502 * If we try to find an inode in the inode hash while it is being
1503 * deleted, we have to wait until the filesystem completes its
1504 * deletion before reporting that it isn't found. This function waits
1505 * until the deletion _might_ have completed. Callers are responsible
1506 * to recheck inode state.
1508 * It doesn't matter if I_NEW is not set initially, a call to
1509 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1510 * will DTRT.
1512 static void __wait_on_freeing_inode(struct inode *inode)
1514 wait_queue_head_t *wq;
1515 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1516 wq = bit_waitqueue(&inode->i_state, __I_NEW);
1517 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1518 spin_unlock(&inode->i_lock);
1519 spin_unlock(&inode_hash_lock);
1520 schedule();
1521 finish_wait(wq, &wait.wait);
1522 spin_lock(&inode_hash_lock);
1525 static __initdata unsigned long ihash_entries;
1526 static int __init set_ihash_entries(char *str)
1528 if (!str)
1529 return 0;
1530 ihash_entries = simple_strtoul(str, &str, 0);
1531 return 1;
1533 __setup("ihash_entries=", set_ihash_entries);
1536 * Initialize the waitqueues and inode hash table.
1538 void __init inode_init_early(void)
1540 int loop;
1542 /* If hashes are distributed across NUMA nodes, defer
1543 * hash allocation until vmalloc space is available.
1545 if (hashdist)
1546 return;
1548 inode_hashtable =
1549 alloc_large_system_hash("Inode-cache",
1550 sizeof(struct hlist_head),
1551 ihash_entries,
1553 HASH_EARLY,
1554 &i_hash_shift,
1555 &i_hash_mask,
1558 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1559 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1562 void __init inode_init(void)
1564 int loop;
1566 /* inode slab cache */
1567 inode_cachep = kmem_cache_create("inode_cache",
1568 sizeof(struct inode),
1570 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1571 SLAB_MEM_SPREAD),
1572 init_once);
1574 /* Hash may have been set up in inode_init_early */
1575 if (!hashdist)
1576 return;
1578 inode_hashtable =
1579 alloc_large_system_hash("Inode-cache",
1580 sizeof(struct hlist_head),
1581 ihash_entries,
1584 &i_hash_shift,
1585 &i_hash_mask,
1588 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1589 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1592 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1594 inode->i_mode = mode;
1595 if (S_ISCHR(mode)) {
1596 inode->i_fop = &def_chr_fops;
1597 inode->i_rdev = rdev;
1598 } else if (S_ISBLK(mode)) {
1599 inode->i_fop = &def_blk_fops;
1600 inode->i_rdev = rdev;
1601 } else if (S_ISFIFO(mode))
1602 inode->i_fop = &def_fifo_fops;
1603 else if (S_ISSOCK(mode))
1604 inode->i_fop = &bad_sock_fops;
1605 else
1606 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1607 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1608 inode->i_ino);
1610 EXPORT_SYMBOL(init_special_inode);
1613 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
1614 * @inode: New inode
1615 * @dir: Directory inode
1616 * @mode: mode of the new inode
1618 void inode_init_owner(struct inode *inode, const struct inode *dir,
1619 mode_t mode)
1621 inode->i_uid = current_fsuid();
1622 if (dir && dir->i_mode & S_ISGID) {
1623 inode->i_gid = dir->i_gid;
1624 if (S_ISDIR(mode))
1625 mode |= S_ISGID;
1626 } else
1627 inode->i_gid = current_fsgid();
1628 inode->i_mode = mode;
1630 EXPORT_SYMBOL(inode_init_owner);
1633 * inode_owner_or_capable - check current task permissions to inode
1634 * @inode: inode being checked
1636 * Return true if current either has CAP_FOWNER to the inode, or
1637 * owns the file.
1639 bool inode_owner_or_capable(const struct inode *inode)
1641 struct user_namespace *ns = inode_userns(inode);
1643 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1644 return true;
1645 if (ns_capable(ns, CAP_FOWNER))
1646 return true;
1647 return false;
1649 EXPORT_SYMBOL(inode_owner_or_capable);