RPC: killing RPC tasks races fixed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / inode.c
blobe45734b9637be81a1da07ed1746a1d32e9f76849
1 /*
2 * linux/fs/inode.c
4 * (C) 1997 Linus Torvalds
5 */
7 #include <linux/fs.h>
8 #include <linux/mm.h>
9 #include <linux/dcache.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/writeback.h>
13 #include <linux/module.h>
14 #include <linux/backing-dev.h>
15 #include <linux/wait.h>
16 #include <linux/rwsem.h>
17 #include <linux/hash.h>
18 #include <linux/swap.h>
19 #include <linux/security.h>
20 #include <linux/pagemap.h>
21 #include <linux/cdev.h>
22 #include <linux/bootmem.h>
23 #include <linux/fsnotify.h>
24 #include <linux/mount.h>
25 #include <linux/async.h>
26 #include <linux/posix_acl.h>
27 #include <linux/ima.h>
30 * This is needed for the following functions:
31 * - inode_has_buffers
32 * - invalidate_bdev
34 * FIXME: remove all knowledge of the buffer layer from this file
36 #include <linux/buffer_head.h>
39 * New inode.c implementation.
41 * This implementation has the basic premise of trying
42 * to be extremely low-overhead and SMP-safe, yet be
43 * simple enough to be "obviously correct".
45 * Famous last words.
48 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
50 /* #define INODE_PARANOIA 1 */
51 /* #define INODE_DEBUG 1 */
54 * Inode lookup is no longer as critical as it used to be:
55 * most of the lookups are going to be through the dcache.
57 #define I_HASHBITS i_hash_shift
58 #define I_HASHMASK i_hash_mask
60 static unsigned int i_hash_mask __read_mostly;
61 static unsigned int i_hash_shift __read_mostly;
64 * Each inode can be on two separate lists. One is
65 * the hash list of the inode, used for lookups. The
66 * other linked list is the "type" list:
67 * "in_use" - valid inode, i_count > 0, i_nlink > 0
68 * "dirty" - as "in_use" but also dirty
69 * "unused" - valid inode, i_count = 0
71 * A "dirty" list is maintained for each super block,
72 * allowing for low-overhead inode sync() operations.
75 static LIST_HEAD(inode_lru);
76 static struct hlist_head *inode_hashtable __read_mostly;
79 * A simple spinlock to protect the list manipulations.
81 * NOTE! You also have to own the lock if you change
82 * the i_state of an inode while it is in use..
84 DEFINE_SPINLOCK(inode_lock);
87 * iprune_sem provides exclusion between the kswapd or try_to_free_pages
88 * icache shrinking path, and the umount path. Without this exclusion,
89 * by the time prune_icache calls iput for the inode whose pages it has
90 * been invalidating, or by the time it calls clear_inode & destroy_inode
91 * from its final dispose_list, the struct super_block they refer to
92 * (for inode->i_sb->s_op) may already have been freed and reused.
94 * We make this an rwsem because the fastpath is icache shrinking. In
95 * some cases a filesystem may be doing a significant amount of work in
96 * its inode reclaim code, so this should improve parallelism.
98 static DECLARE_RWSEM(iprune_sem);
101 * Statistics gathering..
103 struct inodes_stat_t inodes_stat;
105 static struct percpu_counter nr_inodes __cacheline_aligned_in_smp;
106 static struct percpu_counter nr_inodes_unused __cacheline_aligned_in_smp;
108 static struct kmem_cache *inode_cachep __read_mostly;
110 static inline int get_nr_inodes(void)
112 return percpu_counter_sum_positive(&nr_inodes);
115 static inline int get_nr_inodes_unused(void)
117 return percpu_counter_sum_positive(&nr_inodes_unused);
120 int get_nr_dirty_inodes(void)
122 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
123 return nr_dirty > 0 ? nr_dirty : 0;
128 * Handle nr_inode sysctl
130 #ifdef CONFIG_SYSCTL
131 int proc_nr_inodes(ctl_table *table, int write,
132 void __user *buffer, size_t *lenp, loff_t *ppos)
134 inodes_stat.nr_inodes = get_nr_inodes();
135 inodes_stat.nr_unused = get_nr_inodes_unused();
136 return proc_dointvec(table, write, buffer, lenp, ppos);
138 #endif
140 static void wake_up_inode(struct inode *inode)
143 * Prevent speculative execution through spin_unlock(&inode_lock);
145 smp_mb();
146 wake_up_bit(&inode->i_state, __I_NEW);
150 * inode_init_always - perform inode structure intialisation
151 * @sb: superblock inode belongs to
152 * @inode: inode to initialise
154 * These are initializations that need to be done on every inode
155 * allocation as the fields are not initialised by slab allocation.
157 int inode_init_always(struct super_block *sb, struct inode *inode)
159 static const struct address_space_operations empty_aops;
160 static const struct inode_operations empty_iops;
161 static const struct file_operations empty_fops;
162 struct address_space *const mapping = &inode->i_data;
164 inode->i_sb = sb;
165 inode->i_blkbits = sb->s_blocksize_bits;
166 inode->i_flags = 0;
167 atomic_set(&inode->i_count, 1);
168 inode->i_op = &empty_iops;
169 inode->i_fop = &empty_fops;
170 inode->i_nlink = 1;
171 inode->i_uid = 0;
172 inode->i_gid = 0;
173 atomic_set(&inode->i_writecount, 0);
174 inode->i_size = 0;
175 inode->i_blocks = 0;
176 inode->i_bytes = 0;
177 inode->i_generation = 0;
178 #ifdef CONFIG_QUOTA
179 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
180 #endif
181 inode->i_pipe = NULL;
182 inode->i_bdev = NULL;
183 inode->i_cdev = NULL;
184 inode->i_rdev = 0;
185 inode->dirtied_when = 0;
187 if (security_inode_alloc(inode))
188 goto out;
189 spin_lock_init(&inode->i_lock);
190 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
192 mutex_init(&inode->i_mutex);
193 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
195 init_rwsem(&inode->i_alloc_sem);
196 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
198 mapping->a_ops = &empty_aops;
199 mapping->host = inode;
200 mapping->flags = 0;
201 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
202 mapping->assoc_mapping = NULL;
203 mapping->backing_dev_info = &default_backing_dev_info;
204 mapping->writeback_index = 0;
207 * If the block_device provides a backing_dev_info for client
208 * inodes then use that. Otherwise the inode share the bdev's
209 * backing_dev_info.
211 if (sb->s_bdev) {
212 struct backing_dev_info *bdi;
214 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
215 mapping->backing_dev_info = bdi;
217 inode->i_private = NULL;
218 inode->i_mapping = mapping;
219 #ifdef CONFIG_FS_POSIX_ACL
220 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
221 #endif
223 #ifdef CONFIG_FSNOTIFY
224 inode->i_fsnotify_mask = 0;
225 #endif
227 percpu_counter_inc(&nr_inodes);
229 return 0;
230 out:
231 return -ENOMEM;
233 EXPORT_SYMBOL(inode_init_always);
235 static struct inode *alloc_inode(struct super_block *sb)
237 struct inode *inode;
239 if (sb->s_op->alloc_inode)
240 inode = sb->s_op->alloc_inode(sb);
241 else
242 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
244 if (!inode)
245 return NULL;
247 if (unlikely(inode_init_always(sb, inode))) {
248 if (inode->i_sb->s_op->destroy_inode)
249 inode->i_sb->s_op->destroy_inode(inode);
250 else
251 kmem_cache_free(inode_cachep, inode);
252 return NULL;
255 return inode;
258 void __destroy_inode(struct inode *inode)
260 BUG_ON(inode_has_buffers(inode));
261 security_inode_free(inode);
262 fsnotify_inode_delete(inode);
263 #ifdef CONFIG_FS_POSIX_ACL
264 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
265 posix_acl_release(inode->i_acl);
266 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
267 posix_acl_release(inode->i_default_acl);
268 #endif
269 percpu_counter_dec(&nr_inodes);
271 EXPORT_SYMBOL(__destroy_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);
279 else
280 kmem_cache_free(inode_cachep, (inode));
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 spin_lock_init(&mapping->i_mmap_lock);
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);
293 mutex_init(&mapping->unmap_mutex);
295 EXPORT_SYMBOL(address_space_init_once);
298 * These are initializations that only need to be done
299 * once, because the fields are idempotent across use
300 * of the inode, so let the slab aware of that.
302 void inode_init_once(struct inode *inode)
304 memset(inode, 0, sizeof(*inode));
305 INIT_HLIST_NODE(&inode->i_hash);
306 INIT_LIST_HEAD(&inode->i_dentry);
307 INIT_LIST_HEAD(&inode->i_devices);
308 INIT_LIST_HEAD(&inode->i_wb_list);
309 INIT_LIST_HEAD(&inode->i_lru);
310 address_space_init_once(&inode->i_data);
311 i_size_ordered_init(inode);
312 #ifdef CONFIG_FSNOTIFY
313 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
314 #endif
316 EXPORT_SYMBOL(inode_init_once);
318 static void init_once(void *foo)
320 struct inode *inode = (struct inode *) foo;
322 inode_init_once(inode);
326 * inode_lock must be held
328 void __iget(struct inode *inode)
330 atomic_inc(&inode->i_count);
334 * get additional reference to inode; caller must already hold one.
336 void ihold(struct inode *inode)
338 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
340 EXPORT_SYMBOL(ihold);
342 static void inode_lru_list_add(struct inode *inode)
344 if (list_empty(&inode->i_lru)) {
345 list_add(&inode->i_lru, &inode_lru);
346 percpu_counter_inc(&nr_inodes_unused);
350 static void inode_lru_list_del(struct inode *inode)
352 if (!list_empty(&inode->i_lru)) {
353 list_del_init(&inode->i_lru);
354 percpu_counter_dec(&nr_inodes_unused);
358 static inline void __inode_sb_list_add(struct inode *inode)
360 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
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_lock);
370 __inode_sb_list_add(inode);
371 spin_unlock(&inode_lock);
373 EXPORT_SYMBOL_GPL(inode_sb_list_add);
375 static inline void __inode_sb_list_del(struct inode *inode)
377 list_del_init(&inode->i_sb_list);
380 static unsigned long hash(struct super_block *sb, unsigned long hashval)
382 unsigned long tmp;
384 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
385 L1_CACHE_BYTES;
386 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
387 return tmp & I_HASHMASK;
391 * __insert_inode_hash - hash an inode
392 * @inode: unhashed inode
393 * @hashval: unsigned long value used to locate this object in the
394 * inode_hashtable.
396 * Add an inode to the inode hash for this superblock.
398 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
400 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
402 spin_lock(&inode_lock);
403 hlist_add_head(&inode->i_hash, b);
404 spin_unlock(&inode_lock);
406 EXPORT_SYMBOL(__insert_inode_hash);
409 * __remove_inode_hash - remove an inode from the hash
410 * @inode: inode to unhash
412 * Remove an inode from the superblock.
414 static void __remove_inode_hash(struct inode *inode)
416 hlist_del_init(&inode->i_hash);
420 * remove_inode_hash - remove an inode from the hash
421 * @inode: inode to unhash
423 * Remove an inode from the superblock.
425 void remove_inode_hash(struct inode *inode)
427 spin_lock(&inode_lock);
428 hlist_del_init(&inode->i_hash);
429 spin_unlock(&inode_lock);
431 EXPORT_SYMBOL(remove_inode_hash);
433 void end_writeback(struct inode *inode)
435 might_sleep();
436 BUG_ON(inode->i_data.nrpages);
437 BUG_ON(!list_empty(&inode->i_data.private_list));
438 BUG_ON(!(inode->i_state & I_FREEING));
439 BUG_ON(inode->i_state & I_CLEAR);
440 inode_sync_wait(inode);
441 inode->i_state = I_FREEING | I_CLEAR;
443 EXPORT_SYMBOL(end_writeback);
445 static void evict(struct inode *inode)
447 const struct super_operations *op = inode->i_sb->s_op;
449 if (op->evict_inode) {
450 op->evict_inode(inode);
451 } else {
452 if (inode->i_data.nrpages)
453 truncate_inode_pages(&inode->i_data, 0);
454 end_writeback(inode);
456 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
457 bd_forget(inode);
458 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
459 cd_forget(inode);
463 * dispose_list - dispose of the contents of a local list
464 * @head: the head of the list to free
466 * Dispose-list gets a local list with local inodes in it, so it doesn't
467 * need to worry about list corruption and SMP locks.
469 static void dispose_list(struct list_head *head)
471 while (!list_empty(head)) {
472 struct inode *inode;
474 inode = list_first_entry(head, struct inode, i_lru);
475 list_del_init(&inode->i_lru);
477 evict(inode);
479 spin_lock(&inode_lock);
480 __remove_inode_hash(inode);
481 __inode_sb_list_del(inode);
482 spin_unlock(&inode_lock);
484 wake_up_inode(inode);
485 destroy_inode(inode);
490 * evict_inodes - evict all evictable inodes for a superblock
491 * @sb: superblock to operate on
493 * Make sure that no inodes with zero refcount are retained. This is
494 * called by superblock shutdown after having MS_ACTIVE flag removed,
495 * so any inode reaching zero refcount during or after that call will
496 * be immediately evicted.
498 void evict_inodes(struct super_block *sb)
500 struct inode *inode, *next;
501 LIST_HEAD(dispose);
503 down_write(&iprune_sem);
505 spin_lock(&inode_lock);
506 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
507 if (atomic_read(&inode->i_count))
508 continue;
510 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
511 WARN_ON(1);
512 continue;
515 inode->i_state |= I_FREEING;
518 * Move the inode off the IO lists and LRU once I_FREEING is
519 * set so that it won't get moved back on there if it is dirty.
521 list_move(&inode->i_lru, &dispose);
522 list_del_init(&inode->i_wb_list);
523 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
524 percpu_counter_dec(&nr_inodes_unused);
526 spin_unlock(&inode_lock);
528 dispose_list(&dispose);
529 up_write(&iprune_sem);
533 * invalidate_inodes - attempt to free all inodes on a superblock
534 * @sb: superblock to operate on
535 * @kill_dirty: flag to guide handling of dirty inodes
537 * Attempts to free all inodes for a given superblock. If there were any
538 * busy inodes return a non-zero value, else zero.
539 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
540 * them as busy.
542 int invalidate_inodes(struct super_block *sb, bool kill_dirty)
544 int busy = 0;
545 struct inode *inode, *next;
546 LIST_HEAD(dispose);
548 down_write(&iprune_sem);
550 spin_lock(&inode_lock);
551 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
552 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE))
553 continue;
554 if (inode->i_state & I_DIRTY && !kill_dirty) {
555 busy = 1;
556 continue;
558 if (atomic_read(&inode->i_count)) {
559 busy = 1;
560 continue;
563 inode->i_state |= I_FREEING;
566 * Move the inode off the IO lists and LRU once I_FREEING is
567 * set so that it won't get moved back on there if it is dirty.
569 list_move(&inode->i_lru, &dispose);
570 list_del_init(&inode->i_wb_list);
571 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
572 percpu_counter_dec(&nr_inodes_unused);
574 spin_unlock(&inode_lock);
576 dispose_list(&dispose);
577 up_write(&iprune_sem);
579 return busy;
582 static int can_unuse(struct inode *inode)
584 if (inode->i_state & ~I_REFERENCED)
585 return 0;
586 if (inode_has_buffers(inode))
587 return 0;
588 if (atomic_read(&inode->i_count))
589 return 0;
590 if (inode->i_data.nrpages)
591 return 0;
592 return 1;
596 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
597 * temporary list and 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 static void prune_icache(int nr_to_scan)
613 LIST_HEAD(freeable);
614 int nr_scanned;
615 unsigned long reap = 0;
617 down_read(&iprune_sem);
618 spin_lock(&inode_lock);
619 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
620 struct inode *inode;
622 if (list_empty(&inode_lru))
623 break;
625 inode = list_entry(inode_lru.prev, struct inode, i_lru);
628 * Referenced or dirty inodes are still in use. Give them
629 * another pass through the LRU as we canot reclaim them now.
631 if (atomic_read(&inode->i_count) ||
632 (inode->i_state & ~I_REFERENCED)) {
633 list_del_init(&inode->i_lru);
634 percpu_counter_dec(&nr_inodes_unused);
635 continue;
638 /* recently referenced inodes get one more pass */
639 if (inode->i_state & I_REFERENCED) {
640 list_move(&inode->i_lru, &inode_lru);
641 inode->i_state &= ~I_REFERENCED;
642 continue;
644 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
645 __iget(inode);
646 spin_unlock(&inode_lock);
647 if (remove_inode_buffers(inode))
648 reap += invalidate_mapping_pages(&inode->i_data,
649 0, -1);
650 iput(inode);
651 spin_lock(&inode_lock);
653 if (inode != list_entry(inode_lru.next,
654 struct inode, i_lru))
655 continue; /* wrong inode or list_empty */
656 if (!can_unuse(inode))
657 continue;
659 WARN_ON(inode->i_state & I_NEW);
660 inode->i_state |= I_FREEING;
663 * Move the inode off the IO lists and LRU once I_FREEING is
664 * set so that it won't get moved back on there if it is dirty.
666 list_move(&inode->i_lru, &freeable);
667 list_del_init(&inode->i_wb_list);
668 percpu_counter_dec(&nr_inodes_unused);
670 if (current_is_kswapd())
671 __count_vm_events(KSWAPD_INODESTEAL, reap);
672 else
673 __count_vm_events(PGINODESTEAL, reap);
674 spin_unlock(&inode_lock);
676 dispose_list(&freeable);
677 up_read(&iprune_sem);
681 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
682 * "unused" means that no dentries are referring to the inodes: the files are
683 * not open and the dcache references to those inodes have already been
684 * reclaimed.
686 * This function is passed the number of inodes to scan, and it returns the
687 * total number of remaining possibly-reclaimable inodes.
689 static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
691 if (nr) {
693 * Nasty deadlock avoidance. We may hold various FS locks,
694 * and we don't want to recurse into the FS that called us
695 * in clear_inode() and friends..
697 if (!(gfp_mask & __GFP_FS))
698 return -1;
699 prune_icache(nr);
701 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
704 static struct shrinker icache_shrinker = {
705 .shrink = shrink_icache_memory,
706 .seeks = DEFAULT_SEEKS,
709 static void __wait_on_freeing_inode(struct inode *inode);
711 * Called with the inode lock held.
713 static struct inode *find_inode(struct super_block *sb,
714 struct hlist_head *head,
715 int (*test)(struct inode *, void *),
716 void *data)
718 struct hlist_node *node;
719 struct inode *inode = NULL;
721 repeat:
722 hlist_for_each_entry(inode, node, head, i_hash) {
723 if (inode->i_sb != sb)
724 continue;
725 if (!test(inode, data))
726 continue;
727 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
728 __wait_on_freeing_inode(inode);
729 goto repeat;
731 __iget(inode);
732 return inode;
734 return NULL;
738 * find_inode_fast is the fast path version of find_inode, see the comment at
739 * iget_locked for details.
741 static struct inode *find_inode_fast(struct super_block *sb,
742 struct hlist_head *head, unsigned long ino)
744 struct hlist_node *node;
745 struct inode *inode = NULL;
747 repeat:
748 hlist_for_each_entry(inode, node, head, i_hash) {
749 if (inode->i_ino != ino)
750 continue;
751 if (inode->i_sb != sb)
752 continue;
753 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
754 __wait_on_freeing_inode(inode);
755 goto repeat;
757 __iget(inode);
758 return inode;
760 return NULL;
764 * Each cpu owns a range of LAST_INO_BATCH numbers.
765 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
766 * to renew the exhausted range.
768 * This does not significantly increase overflow rate because every CPU can
769 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
770 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
771 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
772 * overflow rate by 2x, which does not seem too significant.
774 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
775 * error if st_ino won't fit in target struct field. Use 32bit counter
776 * here to attempt to avoid that.
778 #define LAST_INO_BATCH 1024
779 static DEFINE_PER_CPU(unsigned int, last_ino);
781 unsigned int get_next_ino(void)
783 unsigned int *p = &get_cpu_var(last_ino);
784 unsigned int res = *p;
786 #ifdef CONFIG_SMP
787 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
788 static atomic_t shared_last_ino;
789 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
791 res = next - LAST_INO_BATCH;
793 #endif
795 *p = ++res;
796 put_cpu_var(last_ino);
797 return res;
799 EXPORT_SYMBOL(get_next_ino);
802 * new_inode - obtain an inode
803 * @sb: superblock
805 * Allocates a new inode for given superblock. The default gfp_mask
806 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
807 * If HIGHMEM pages are unsuitable or it is known that pages allocated
808 * for the page cache are not reclaimable or migratable,
809 * mapping_set_gfp_mask() must be called with suitable flags on the
810 * newly created inode's mapping
813 struct inode *new_inode(struct super_block *sb)
815 struct inode *inode;
817 spin_lock_prefetch(&inode_lock);
819 inode = alloc_inode(sb);
820 if (inode) {
821 spin_lock(&inode_lock);
822 __inode_sb_list_add(inode);
823 inode->i_state = 0;
824 spin_unlock(&inode_lock);
826 return inode;
828 EXPORT_SYMBOL(new_inode);
830 void unlock_new_inode(struct inode *inode)
832 #ifdef CONFIG_DEBUG_LOCK_ALLOC
833 if (S_ISDIR(inode->i_mode)) {
834 struct file_system_type *type = inode->i_sb->s_type;
836 /* Set new key only if filesystem hasn't already changed it */
837 if (!lockdep_match_class(&inode->i_mutex,
838 &type->i_mutex_key)) {
840 * ensure nobody is actually holding i_mutex
842 mutex_destroy(&inode->i_mutex);
843 mutex_init(&inode->i_mutex);
844 lockdep_set_class(&inode->i_mutex,
845 &type->i_mutex_dir_key);
848 #endif
850 * This is special! We do not need the spinlock when clearing I_NEW,
851 * because we're guaranteed that nobody else tries to do anything about
852 * the state of the inode when it is locked, as we just created it (so
853 * there can be no old holders that haven't tested I_NEW).
854 * However we must emit the memory barrier so that other CPUs reliably
855 * see the clearing of I_NEW after the other inode initialisation has
856 * completed.
858 smp_mb();
859 WARN_ON(!(inode->i_state & I_NEW));
860 inode->i_state &= ~I_NEW;
861 wake_up_inode(inode);
863 EXPORT_SYMBOL(unlock_new_inode);
866 * This is called without the inode lock held.. Be careful.
868 * We no longer cache the sb_flags in i_flags - see fs.h
869 * -- rmk@arm.uk.linux.org
871 static struct inode *get_new_inode(struct super_block *sb,
872 struct hlist_head *head,
873 int (*test)(struct inode *, void *),
874 int (*set)(struct inode *, void *),
875 void *data)
877 struct inode *inode;
879 inode = alloc_inode(sb);
880 if (inode) {
881 struct inode *old;
883 spin_lock(&inode_lock);
884 /* We released the lock, so.. */
885 old = find_inode(sb, head, test, data);
886 if (!old) {
887 if (set(inode, data))
888 goto set_failed;
890 hlist_add_head(&inode->i_hash, head);
891 __inode_sb_list_add(inode);
892 inode->i_state = I_NEW;
893 spin_unlock(&inode_lock);
895 /* Return the locked inode with I_NEW set, the
896 * caller is responsible for filling in the contents
898 return inode;
902 * Uhhuh, somebody else created the same inode under
903 * us. Use the old inode instead of the one we just
904 * allocated.
906 spin_unlock(&inode_lock);
907 destroy_inode(inode);
908 inode = old;
909 wait_on_inode(inode);
911 return inode;
913 set_failed:
914 spin_unlock(&inode_lock);
915 destroy_inode(inode);
916 return NULL;
920 * get_new_inode_fast is the fast path version of get_new_inode, see the
921 * comment at iget_locked for details.
923 static struct inode *get_new_inode_fast(struct super_block *sb,
924 struct hlist_head *head, unsigned long ino)
926 struct inode *inode;
928 inode = alloc_inode(sb);
929 if (inode) {
930 struct inode *old;
932 spin_lock(&inode_lock);
933 /* We released the lock, so.. */
934 old = find_inode_fast(sb, head, ino);
935 if (!old) {
936 inode->i_ino = ino;
937 hlist_add_head(&inode->i_hash, head);
938 __inode_sb_list_add(inode);
939 inode->i_state = I_NEW;
940 spin_unlock(&inode_lock);
942 /* Return the locked inode with I_NEW set, the
943 * caller is responsible for filling in the contents
945 return inode;
949 * Uhhuh, somebody else created the same inode under
950 * us. Use the old inode instead of the one we just
951 * allocated.
953 spin_unlock(&inode_lock);
954 destroy_inode(inode);
955 inode = old;
956 wait_on_inode(inode);
958 return inode;
962 * search the inode cache for a matching inode number.
963 * If we find one, then the inode number we are trying to
964 * allocate is not unique and so we should not use it.
966 * Returns 1 if the inode number is unique, 0 if it is not.
968 static int test_inode_iunique(struct super_block *sb, unsigned long ino)
970 struct hlist_head *b = inode_hashtable + hash(sb, ino);
971 struct hlist_node *node;
972 struct inode *inode;
974 hlist_for_each_entry(inode, node, b, i_hash) {
975 if (inode->i_ino == ino && inode->i_sb == sb)
976 return 0;
979 return 1;
983 * iunique - get a unique inode number
984 * @sb: superblock
985 * @max_reserved: highest reserved inode number
987 * Obtain an inode number that is unique on the system for a given
988 * superblock. This is used by file systems that have no natural
989 * permanent inode numbering system. An inode number is returned that
990 * is higher than the reserved limit but unique.
992 * BUGS:
993 * With a large number of inodes live on the file system this function
994 * currently becomes quite slow.
996 ino_t iunique(struct super_block *sb, ino_t max_reserved)
999 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1000 * error if st_ino won't fit in target struct field. Use 32bit counter
1001 * here to attempt to avoid that.
1003 static DEFINE_SPINLOCK(iunique_lock);
1004 static unsigned int counter;
1005 ino_t res;
1007 spin_lock(&inode_lock);
1008 spin_lock(&iunique_lock);
1009 do {
1010 if (counter <= max_reserved)
1011 counter = max_reserved + 1;
1012 res = counter++;
1013 } while (!test_inode_iunique(sb, res));
1014 spin_unlock(&iunique_lock);
1015 spin_unlock(&inode_lock);
1017 return res;
1019 EXPORT_SYMBOL(iunique);
1021 struct inode *igrab(struct inode *inode)
1023 spin_lock(&inode_lock);
1024 if (!(inode->i_state & (I_FREEING|I_WILL_FREE)))
1025 __iget(inode);
1026 else
1028 * Handle the case where s_op->clear_inode is not been
1029 * called yet, and somebody is calling igrab
1030 * while the inode is getting freed.
1032 inode = NULL;
1033 spin_unlock(&inode_lock);
1034 return inode;
1036 EXPORT_SYMBOL(igrab);
1039 * ifind - internal function, you want ilookup5() or iget5().
1040 * @sb: super block of file system to search
1041 * @head: the head of the list to search
1042 * @test: callback used for comparisons between inodes
1043 * @data: opaque data pointer to pass to @test
1044 * @wait: if true wait for the inode to be unlocked, if false do not
1046 * ifind() searches for the inode specified by @data in the inode
1047 * cache. This is a generalized version of ifind_fast() for file systems where
1048 * the inode number is not sufficient for unique identification of an inode.
1050 * If the inode is in the cache, the inode is returned with an incremented
1051 * reference count.
1053 * Otherwise NULL is returned.
1055 * Note, @test is called with the inode_lock held, so can't sleep.
1057 static struct inode *ifind(struct super_block *sb,
1058 struct hlist_head *head, int (*test)(struct inode *, void *),
1059 void *data, const int wait)
1061 struct inode *inode;
1063 spin_lock(&inode_lock);
1064 inode = find_inode(sb, head, test, data);
1065 if (inode) {
1066 spin_unlock(&inode_lock);
1067 if (likely(wait))
1068 wait_on_inode(inode);
1069 return inode;
1071 spin_unlock(&inode_lock);
1072 return NULL;
1076 * ifind_fast - internal function, you want ilookup() or iget().
1077 * @sb: super block of file system to search
1078 * @head: head of the list to search
1079 * @ino: inode number to search for
1081 * ifind_fast() searches for the inode @ino in the inode cache. This is for
1082 * file systems where the inode number is sufficient for unique identification
1083 * of an inode.
1085 * If the inode is in the cache, the inode is returned with an incremented
1086 * reference count.
1088 * Otherwise NULL is returned.
1090 static struct inode *ifind_fast(struct super_block *sb,
1091 struct hlist_head *head, unsigned long ino)
1093 struct inode *inode;
1095 spin_lock(&inode_lock);
1096 inode = find_inode_fast(sb, head, ino);
1097 if (inode) {
1098 spin_unlock(&inode_lock);
1099 wait_on_inode(inode);
1100 return inode;
1102 spin_unlock(&inode_lock);
1103 return NULL;
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 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1114 * @data in the inode cache. This is a generalized version of ilookup() for
1115 * file systems where the inode number is not sufficient for unique
1116 * identification of an inode.
1118 * If the inode is in the cache, the inode is returned with an incremented
1119 * reference count. Note, the inode lock is not waited upon so you have to be
1120 * very careful what you do with the returned inode. You probably should be
1121 * using ilookup5() instead.
1123 * Otherwise NULL is returned.
1125 * Note, @test is called with the inode_lock held, so can't sleep.
1127 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1128 int (*test)(struct inode *, void *), void *data)
1130 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1132 return ifind(sb, head, test, data, 0);
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 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1144 * @data in the inode cache. This is a generalized version of ilookup() for
1145 * file systems where the inode number is not sufficient for unique
1146 * identification of an inode.
1148 * If the inode is in the cache, the inode lock is waited upon and the inode is
1149 * returned with an incremented reference count.
1151 * Otherwise NULL is returned.
1153 * Note, @test is called with the inode_lock held, so can't sleep.
1155 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1156 int (*test)(struct inode *, void *), void *data)
1158 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1160 return ifind(sb, head, test, data, 1);
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 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
1170 * This is for file systems where the inode number is sufficient for unique
1171 * identification of an inode.
1173 * If the inode is in the cache, the inode is returned with an incremented
1174 * reference count.
1176 * Otherwise NULL is returned.
1178 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1180 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1182 return ifind_fast(sb, head, ino);
1184 EXPORT_SYMBOL(ilookup);
1187 * iget5_locked - obtain an inode from a mounted file system
1188 * @sb: super block of file system
1189 * @hashval: hash value (usually inode number) to get
1190 * @test: callback used for comparisons between inodes
1191 * @set: callback used to initialize a new struct inode
1192 * @data: opaque data pointer to pass to @test and @set
1194 * iget5_locked() uses ifind() to search for the inode specified by @hashval
1195 * and @data in the inode cache and if present it is returned with an increased
1196 * reference count. This is a generalized version of iget_locked() for file
1197 * systems where the inode number is not sufficient for unique identification
1198 * of an inode.
1200 * If the inode is not in cache, get_new_inode() is called to allocate a new
1201 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1202 * file system gets to fill it in before unlocking it via unlock_new_inode().
1204 * Note both @test and @set are called with the inode_lock held, so can't sleep.
1206 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1207 int (*test)(struct inode *, void *),
1208 int (*set)(struct inode *, void *), void *data)
1210 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1211 struct inode *inode;
1213 inode = ifind(sb, head, test, data, 1);
1214 if (inode)
1215 return inode;
1217 * get_new_inode() will do the right thing, re-trying the search
1218 * in case it had to block at any point.
1220 return get_new_inode(sb, head, test, set, data);
1222 EXPORT_SYMBOL(iget5_locked);
1225 * iget_locked - obtain an inode from a mounted file system
1226 * @sb: super block of file system
1227 * @ino: inode number to get
1229 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1230 * the inode cache and if present it is returned with an increased reference
1231 * count. This is for file systems where the inode number is sufficient for
1232 * unique identification of an inode.
1234 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1235 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1236 * The file system gets to fill it in before unlocking it via
1237 * unlock_new_inode().
1239 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1241 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1242 struct inode *inode;
1244 inode = ifind_fast(sb, head, ino);
1245 if (inode)
1246 return inode;
1248 * get_new_inode_fast() will do the right thing, re-trying the search
1249 * in case it had to block at any point.
1251 return get_new_inode_fast(sb, head, ino);
1253 EXPORT_SYMBOL(iget_locked);
1255 int insert_inode_locked(struct inode *inode)
1257 struct super_block *sb = inode->i_sb;
1258 ino_t ino = inode->i_ino;
1259 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1261 inode->i_state |= I_NEW;
1262 while (1) {
1263 struct hlist_node *node;
1264 struct inode *old = NULL;
1265 spin_lock(&inode_lock);
1266 hlist_for_each_entry(old, node, head, i_hash) {
1267 if (old->i_ino != ino)
1268 continue;
1269 if (old->i_sb != sb)
1270 continue;
1271 if (old->i_state & (I_FREEING|I_WILL_FREE))
1272 continue;
1273 break;
1275 if (likely(!node)) {
1276 hlist_add_head(&inode->i_hash, head);
1277 spin_unlock(&inode_lock);
1278 return 0;
1280 __iget(old);
1281 spin_unlock(&inode_lock);
1282 wait_on_inode(old);
1283 if (unlikely(!inode_unhashed(old))) {
1284 iput(old);
1285 return -EBUSY;
1287 iput(old);
1290 EXPORT_SYMBOL(insert_inode_locked);
1292 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1293 int (*test)(struct inode *, void *), void *data)
1295 struct super_block *sb = inode->i_sb;
1296 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1298 inode->i_state |= I_NEW;
1300 while (1) {
1301 struct hlist_node *node;
1302 struct inode *old = NULL;
1304 spin_lock(&inode_lock);
1305 hlist_for_each_entry(old, node, head, i_hash) {
1306 if (old->i_sb != sb)
1307 continue;
1308 if (!test(old, data))
1309 continue;
1310 if (old->i_state & (I_FREEING|I_WILL_FREE))
1311 continue;
1312 break;
1314 if (likely(!node)) {
1315 hlist_add_head(&inode->i_hash, head);
1316 spin_unlock(&inode_lock);
1317 return 0;
1319 __iget(old);
1320 spin_unlock(&inode_lock);
1321 wait_on_inode(old);
1322 if (unlikely(!inode_unhashed(old))) {
1323 iput(old);
1324 return -EBUSY;
1326 iput(old);
1329 EXPORT_SYMBOL(insert_inode_locked4);
1332 int generic_delete_inode(struct inode *inode)
1334 return 1;
1336 EXPORT_SYMBOL(generic_delete_inode);
1339 * Normal UNIX filesystem behaviour: delete the
1340 * inode when the usage count drops to zero, and
1341 * i_nlink is zero.
1343 int generic_drop_inode(struct inode *inode)
1345 return !inode->i_nlink || inode_unhashed(inode);
1347 EXPORT_SYMBOL_GPL(generic_drop_inode);
1350 * Called when we're dropping the last reference
1351 * to an inode.
1353 * Call the FS "drop_inode()" function, defaulting to
1354 * the legacy UNIX filesystem behaviour. If it tells
1355 * us to evict inode, do so. Otherwise, retain inode
1356 * in cache if fs is alive, sync and evict if fs is
1357 * shutting down.
1359 static void iput_final(struct inode *inode)
1361 struct super_block *sb = inode->i_sb;
1362 const struct super_operations *op = inode->i_sb->s_op;
1363 int drop;
1365 if (op && op->drop_inode)
1366 drop = op->drop_inode(inode);
1367 else
1368 drop = generic_drop_inode(inode);
1370 if (!drop) {
1371 if (sb->s_flags & MS_ACTIVE) {
1372 inode->i_state |= I_REFERENCED;
1373 if (!(inode->i_state & (I_DIRTY|I_SYNC))) {
1374 inode_lru_list_add(inode);
1376 spin_unlock(&inode_lock);
1377 return;
1379 WARN_ON(inode->i_state & I_NEW);
1380 inode->i_state |= I_WILL_FREE;
1381 spin_unlock(&inode_lock);
1382 write_inode_now(inode, 1);
1383 spin_lock(&inode_lock);
1384 WARN_ON(inode->i_state & I_NEW);
1385 inode->i_state &= ~I_WILL_FREE;
1386 __remove_inode_hash(inode);
1389 WARN_ON(inode->i_state & I_NEW);
1390 inode->i_state |= I_FREEING;
1393 * Move the inode off the IO lists and LRU once I_FREEING is
1394 * set so that it won't get moved back on there if it is dirty.
1396 inode_lru_list_del(inode);
1397 list_del_init(&inode->i_wb_list);
1399 __inode_sb_list_del(inode);
1400 spin_unlock(&inode_lock);
1401 evict(inode);
1402 remove_inode_hash(inode);
1403 wake_up_inode(inode);
1404 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1405 destroy_inode(inode);
1409 * iput - put an inode
1410 * @inode: inode to put
1412 * Puts an inode, dropping its usage count. If the inode use count hits
1413 * zero, the inode is then freed and may also be destroyed.
1415 * Consequently, iput() can sleep.
1417 void iput(struct inode *inode)
1419 if (inode) {
1420 BUG_ON(inode->i_state & I_CLEAR);
1422 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1423 iput_final(inode);
1426 EXPORT_SYMBOL(iput);
1429 * bmap - find a block number in a file
1430 * @inode: inode of file
1431 * @block: block to find
1433 * Returns the block number on the device holding the inode that
1434 * is the disk block number for the block of the file requested.
1435 * That is, asked for block 4 of inode 1 the function will return the
1436 * disk block relative to the disk start that holds that block of the
1437 * file.
1439 sector_t bmap(struct inode *inode, sector_t block)
1441 sector_t res = 0;
1442 if (inode->i_mapping->a_ops->bmap)
1443 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1444 return res;
1446 EXPORT_SYMBOL(bmap);
1449 * With relative atime, only update atime if the previous atime is
1450 * earlier than either the ctime or mtime or if at least a day has
1451 * passed since the last atime update.
1453 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1454 struct timespec now)
1457 if (!(mnt->mnt_flags & MNT_RELATIME))
1458 return 1;
1460 * Is mtime younger than atime? If yes, update atime:
1462 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1463 return 1;
1465 * Is ctime younger than atime? If yes, update atime:
1467 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1468 return 1;
1471 * Is the previous atime value older than a day? If yes,
1472 * update atime:
1474 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1475 return 1;
1477 * Good, we can skip the atime update:
1479 return 0;
1483 * touch_atime - update the access time
1484 * @mnt: mount the inode is accessed on
1485 * @dentry: dentry accessed
1487 * Update the accessed time on an inode and mark it for writeback.
1488 * This function automatically handles read only file systems and media,
1489 * as well as the "noatime" flag and inode specific "noatime" markers.
1491 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1493 struct inode *inode = dentry->d_inode;
1494 struct timespec now;
1496 if (inode->i_flags & S_NOATIME)
1497 return;
1498 if (IS_NOATIME(inode))
1499 return;
1500 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1501 return;
1503 if (mnt->mnt_flags & MNT_NOATIME)
1504 return;
1505 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1506 return;
1508 now = current_fs_time(inode->i_sb);
1510 if (!relatime_need_update(mnt, inode, now))
1511 return;
1513 if (timespec_equal(&inode->i_atime, &now))
1514 return;
1516 if (mnt_want_write(mnt))
1517 return;
1519 inode->i_atime = now;
1520 mark_inode_dirty_sync(inode);
1521 mnt_drop_write(mnt);
1523 EXPORT_SYMBOL(touch_atime);
1526 * file_update_time - update mtime and ctime time
1527 * @file: file accessed
1529 * Update the mtime and ctime members of an inode and mark the inode
1530 * for writeback. Note that this function is meant exclusively for
1531 * usage in the file write path of filesystems, and filesystems may
1532 * choose to explicitly ignore update via this function with the
1533 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1534 * timestamps are handled by the server.
1537 void file_update_time(struct file *file)
1539 struct inode *inode = file->f_path.dentry->d_inode;
1540 struct timespec now;
1541 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1543 /* First try to exhaust all avenues to not sync */
1544 if (IS_NOCMTIME(inode))
1545 return;
1547 now = current_fs_time(inode->i_sb);
1548 if (!timespec_equal(&inode->i_mtime, &now))
1549 sync_it = S_MTIME;
1551 if (!timespec_equal(&inode->i_ctime, &now))
1552 sync_it |= S_CTIME;
1554 if (IS_I_VERSION(inode))
1555 sync_it |= S_VERSION;
1557 if (!sync_it)
1558 return;
1560 /* Finally allowed to write? Takes lock. */
1561 if (mnt_want_write_file(file))
1562 return;
1564 /* Only change inode inside the lock region */
1565 if (sync_it & S_VERSION)
1566 inode_inc_iversion(inode);
1567 if (sync_it & S_CTIME)
1568 inode->i_ctime = now;
1569 if (sync_it & S_MTIME)
1570 inode->i_mtime = now;
1571 mark_inode_dirty_sync(inode);
1572 mnt_drop_write(file->f_path.mnt);
1574 EXPORT_SYMBOL(file_update_time);
1576 int inode_needs_sync(struct inode *inode)
1578 if (IS_SYNC(inode))
1579 return 1;
1580 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1581 return 1;
1582 return 0;
1584 EXPORT_SYMBOL(inode_needs_sync);
1586 int inode_wait(void *word)
1588 schedule();
1589 return 0;
1591 EXPORT_SYMBOL(inode_wait);
1594 * If we try to find an inode in the inode hash while it is being
1595 * deleted, we have to wait until the filesystem completes its
1596 * deletion before reporting that it isn't found. This function waits
1597 * until the deletion _might_ have completed. Callers are responsible
1598 * to recheck inode state.
1600 * It doesn't matter if I_NEW is not set initially, a call to
1601 * wake_up_inode() after removing from the hash list will DTRT.
1603 * This is called with inode_lock held.
1605 static void __wait_on_freeing_inode(struct inode *inode)
1607 wait_queue_head_t *wq;
1608 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1609 wq = bit_waitqueue(&inode->i_state, __I_NEW);
1610 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1611 spin_unlock(&inode_lock);
1612 schedule();
1613 finish_wait(wq, &wait.wait);
1614 spin_lock(&inode_lock);
1617 static __initdata unsigned long ihash_entries;
1618 static int __init set_ihash_entries(char *str)
1620 if (!str)
1621 return 0;
1622 ihash_entries = simple_strtoul(str, &str, 0);
1623 return 1;
1625 __setup("ihash_entries=", set_ihash_entries);
1628 * Initialize the waitqueues and inode hash table.
1630 void __init inode_init_early(void)
1632 int loop;
1634 /* If hashes are distributed across NUMA nodes, defer
1635 * hash allocation until vmalloc space is available.
1637 if (hashdist)
1638 return;
1640 inode_hashtable =
1641 alloc_large_system_hash("Inode-cache",
1642 sizeof(struct hlist_head),
1643 ihash_entries,
1645 HASH_EARLY,
1646 &i_hash_shift,
1647 &i_hash_mask,
1650 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1651 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1654 void __init inode_init(void)
1656 int loop;
1658 /* inode slab cache */
1659 inode_cachep = kmem_cache_create("inode_cache",
1660 sizeof(struct inode),
1662 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1663 SLAB_MEM_SPREAD),
1664 init_once);
1665 register_shrinker(&icache_shrinker);
1666 percpu_counter_init(&nr_inodes, 0);
1667 percpu_counter_init(&nr_inodes_unused, 0);
1669 /* Hash may have been set up in inode_init_early */
1670 if (!hashdist)
1671 return;
1673 inode_hashtable =
1674 alloc_large_system_hash("Inode-cache",
1675 sizeof(struct hlist_head),
1676 ihash_entries,
1679 &i_hash_shift,
1680 &i_hash_mask,
1683 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1684 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1687 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1689 inode->i_mode = mode;
1690 if (S_ISCHR(mode)) {
1691 inode->i_fop = &def_chr_fops;
1692 inode->i_rdev = rdev;
1693 } else if (S_ISBLK(mode)) {
1694 inode->i_fop = &def_blk_fops;
1695 inode->i_rdev = rdev;
1696 } else if (S_ISFIFO(mode))
1697 inode->i_fop = &def_fifo_fops;
1698 else if (S_ISSOCK(mode))
1699 inode->i_fop = &bad_sock_fops;
1700 else
1701 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1702 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1703 inode->i_ino);
1705 EXPORT_SYMBOL(init_special_inode);
1708 * Init uid,gid,mode for new inode according to posix standards
1709 * @inode: New inode
1710 * @dir: Directory inode
1711 * @mode: mode of the new inode
1713 void inode_init_owner(struct inode *inode, const struct inode *dir,
1714 mode_t mode)
1716 inode->i_uid = current_fsuid();
1717 if (dir && dir->i_mode & S_ISGID) {
1718 inode->i_gid = dir->i_gid;
1719 if (S_ISDIR(mode))
1720 mode |= S_ISGID;
1721 } else
1722 inode->i_gid = current_fsgid();
1723 inode->i_mode = mode;
1725 EXPORT_SYMBOL(inode_init_owner);