]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/inode.c
unexport invalidate_inodes
[net-next-2.6.git] / fs / inode.c
1 /*
2  * linux/fs/inode.c
3  *
4  * (C) 1997 Linus Torvalds
5  */
6
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
28 /*
29  * This is needed for the following functions:
30  *  - inode_has_buffers
31  *  - invalidate_inode_buffers
32  *  - invalidate_bdev
33  *
34  * FIXME: remove all knowledge of the buffer layer from this file
35  */
36 #include <linux/buffer_head.h>
37
38 /*
39  * New inode.c implementation.
40  *
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".
44  *
45  * Famous last words.
46  */
47
48 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
49
50 /* #define INODE_PARANOIA 1 */
51 /* #define INODE_DEBUG 1 */
52
53 /*
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.
56  */
57 #define I_HASHBITS      i_hash_shift
58 #define I_HASHMASK      i_hash_mask
59
60 static unsigned int i_hash_mask __read_mostly;
61 static unsigned int i_hash_shift __read_mostly;
62
63 /*
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
70  *
71  * A "dirty" list is maintained for each super block,
72  * allowing for low-overhead inode sync() operations.
73  */
74
75 LIST_HEAD(inode_in_use);
76 LIST_HEAD(inode_unused);
77 static struct hlist_head *inode_hashtable __read_mostly;
78
79 /*
80  * A simple spinlock to protect the list manipulations.
81  *
82  * NOTE! You also have to own the lock if you change
83  * the i_state of an inode while it is in use..
84  */
85 DEFINE_SPINLOCK(inode_lock);
86
87 /*
88  * iprune_sem provides exclusion between the kswapd or try_to_free_pages
89  * icache shrinking path, and the umount path.  Without this exclusion,
90  * by the time prune_icache calls iput for the inode whose pages it has
91  * been invalidating, or by the time it calls clear_inode & destroy_inode
92  * from its final dispose_list, the struct super_block they refer to
93  * (for inode->i_sb->s_op) may already have been freed and reused.
94  *
95  * We make this an rwsem because the fastpath is icache shrinking. In
96  * some cases a filesystem may be doing a significant amount of work in
97  * its inode reclaim code, so this should improve parallelism.
98  */
99 static DECLARE_RWSEM(iprune_sem);
100
101 /*
102  * Statistics gathering..
103  */
104 struct inodes_stat_t inodes_stat;
105
106 static struct kmem_cache *inode_cachep __read_mostly;
107
108 static void wake_up_inode(struct inode *inode)
109 {
110         /*
111          * Prevent speculative execution through spin_unlock(&inode_lock);
112          */
113         smp_mb();
114         wake_up_bit(&inode->i_state, __I_NEW);
115 }
116
117 /**
118  * inode_init_always - perform inode structure intialisation
119  * @sb: superblock inode belongs to
120  * @inode: inode to initialise
121  *
122  * These are initializations that need to be done on every inode
123  * allocation as the fields are not initialised by slab allocation.
124  */
125 int inode_init_always(struct super_block *sb, struct inode *inode)
126 {
127         static const struct address_space_operations empty_aops;
128         static const struct inode_operations empty_iops;
129         static const struct file_operations empty_fops;
130         struct address_space *const mapping = &inode->i_data;
131
132         inode->i_sb = sb;
133         inode->i_blkbits = sb->s_blocksize_bits;
134         inode->i_flags = 0;
135         atomic_set(&inode->i_count, 1);
136         inode->i_op = &empty_iops;
137         inode->i_fop = &empty_fops;
138         inode->i_nlink = 1;
139         inode->i_uid = 0;
140         inode->i_gid = 0;
141         atomic_set(&inode->i_writecount, 0);
142         inode->i_size = 0;
143         inode->i_blocks = 0;
144         inode->i_bytes = 0;
145         inode->i_generation = 0;
146 #ifdef CONFIG_QUOTA
147         memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
148 #endif
149         inode->i_pipe = NULL;
150         inode->i_bdev = NULL;
151         inode->i_cdev = NULL;
152         inode->i_rdev = 0;
153         inode->dirtied_when = 0;
154
155         if (security_inode_alloc(inode))
156                 goto out;
157         spin_lock_init(&inode->i_lock);
158         lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
159
160         mutex_init(&inode->i_mutex);
161         lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
162
163         init_rwsem(&inode->i_alloc_sem);
164         lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
165
166         mapping->a_ops = &empty_aops;
167         mapping->host = inode;
168         mapping->flags = 0;
169         mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
170         mapping->assoc_mapping = NULL;
171         mapping->backing_dev_info = &default_backing_dev_info;
172         mapping->writeback_index = 0;
173
174         /*
175          * If the block_device provides a backing_dev_info for client
176          * inodes then use that.  Otherwise the inode share the bdev's
177          * backing_dev_info.
178          */
179         if (sb->s_bdev) {
180                 struct backing_dev_info *bdi;
181
182                 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
183                 mapping->backing_dev_info = bdi;
184         }
185         inode->i_private = NULL;
186         inode->i_mapping = mapping;
187 #ifdef CONFIG_FS_POSIX_ACL
188         inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
189 #endif
190
191 #ifdef CONFIG_FSNOTIFY
192         inode->i_fsnotify_mask = 0;
193 #endif
194
195         return 0;
196 out:
197         return -ENOMEM;
198 }
199 EXPORT_SYMBOL(inode_init_always);
200
201 static struct inode *alloc_inode(struct super_block *sb)
202 {
203         struct inode *inode;
204
205         if (sb->s_op->alloc_inode)
206                 inode = sb->s_op->alloc_inode(sb);
207         else
208                 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
209
210         if (!inode)
211                 return NULL;
212
213         if (unlikely(inode_init_always(sb, inode))) {
214                 if (inode->i_sb->s_op->destroy_inode)
215                         inode->i_sb->s_op->destroy_inode(inode);
216                 else
217                         kmem_cache_free(inode_cachep, inode);
218                 return NULL;
219         }
220
221         return inode;
222 }
223
224 void __destroy_inode(struct inode *inode)
225 {
226         BUG_ON(inode_has_buffers(inode));
227         security_inode_free(inode);
228         fsnotify_inode_delete(inode);
229 #ifdef CONFIG_FS_POSIX_ACL
230         if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
231                 posix_acl_release(inode->i_acl);
232         if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
233                 posix_acl_release(inode->i_default_acl);
234 #endif
235 }
236 EXPORT_SYMBOL(__destroy_inode);
237
238 static void destroy_inode(struct inode *inode)
239 {
240         __destroy_inode(inode);
241         if (inode->i_sb->s_op->destroy_inode)
242                 inode->i_sb->s_op->destroy_inode(inode);
243         else
244                 kmem_cache_free(inode_cachep, (inode));
245 }
246
247 /*
248  * These are initializations that only need to be done
249  * once, because the fields are idempotent across use
250  * of the inode, so let the slab aware of that.
251  */
252 void inode_init_once(struct inode *inode)
253 {
254         memset(inode, 0, sizeof(*inode));
255         INIT_HLIST_NODE(&inode->i_hash);
256         INIT_LIST_HEAD(&inode->i_dentry);
257         INIT_LIST_HEAD(&inode->i_devices);
258         INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
259         spin_lock_init(&inode->i_data.tree_lock);
260         spin_lock_init(&inode->i_data.i_mmap_lock);
261         INIT_LIST_HEAD(&inode->i_data.private_list);
262         spin_lock_init(&inode->i_data.private_lock);
263         INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap);
264         INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear);
265         i_size_ordered_init(inode);
266 #ifdef CONFIG_FSNOTIFY
267         INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
268 #endif
269 }
270 EXPORT_SYMBOL(inode_init_once);
271
272 static void init_once(void *foo)
273 {
274         struct inode *inode = (struct inode *) foo;
275
276         inode_init_once(inode);
277 }
278
279 /*
280  * inode_lock must be held
281  */
282 void __iget(struct inode *inode)
283 {
284         if (atomic_inc_return(&inode->i_count) != 1)
285                 return;
286
287         if (!(inode->i_state & (I_DIRTY|I_SYNC)))
288                 list_move(&inode->i_list, &inode_in_use);
289         inodes_stat.nr_unused--;
290 }
291
292 void end_writeback(struct inode *inode)
293 {
294         might_sleep();
295         BUG_ON(inode->i_data.nrpages);
296         BUG_ON(!list_empty(&inode->i_data.private_list));
297         BUG_ON(!(inode->i_state & I_FREEING));
298         BUG_ON(inode->i_state & I_CLEAR);
299         inode_sync_wait(inode);
300         inode->i_state = I_FREEING | I_CLEAR;
301 }
302 EXPORT_SYMBOL(end_writeback);
303
304 static void evict(struct inode *inode)
305 {
306         const struct super_operations *op = inode->i_sb->s_op;
307
308         if (op->evict_inode) {
309                 op->evict_inode(inode);
310         } else {
311                 if (inode->i_data.nrpages)
312                         truncate_inode_pages(&inode->i_data, 0);
313                 end_writeback(inode);
314         }
315         if (S_ISBLK(inode->i_mode) && inode->i_bdev)
316                 bd_forget(inode);
317         if (S_ISCHR(inode->i_mode) && inode->i_cdev)
318                 cd_forget(inode);
319 }
320
321 /*
322  * dispose_list - dispose of the contents of a local list
323  * @head: the head of the list to free
324  *
325  * Dispose-list gets a local list with local inodes in it, so it doesn't
326  * need to worry about list corruption and SMP locks.
327  */
328 static void dispose_list(struct list_head *head)
329 {
330         int nr_disposed = 0;
331
332         while (!list_empty(head)) {
333                 struct inode *inode;
334
335                 inode = list_first_entry(head, struct inode, i_list);
336                 list_del(&inode->i_list);
337
338                 evict(inode);
339
340                 spin_lock(&inode_lock);
341                 hlist_del_init(&inode->i_hash);
342                 list_del_init(&inode->i_sb_list);
343                 spin_unlock(&inode_lock);
344
345                 wake_up_inode(inode);
346                 destroy_inode(inode);
347                 nr_disposed++;
348         }
349         spin_lock(&inode_lock);
350         inodes_stat.nr_inodes -= nr_disposed;
351         spin_unlock(&inode_lock);
352 }
353
354 /*
355  * Invalidate all inodes for a device.
356  */
357 static int invalidate_list(struct list_head *head, struct list_head *dispose)
358 {
359         struct list_head *next;
360         int busy = 0, count = 0;
361
362         next = head->next;
363         for (;;) {
364                 struct list_head *tmp = next;
365                 struct inode *inode;
366
367                 /*
368                  * We can reschedule here without worrying about the list's
369                  * consistency because the per-sb list of inodes must not
370                  * change during umount anymore, and because iprune_sem keeps
371                  * shrink_icache_memory() away.
372                  */
373                 cond_resched_lock(&inode_lock);
374
375                 next = next->next;
376                 if (tmp == head)
377                         break;
378                 inode = list_entry(tmp, struct inode, i_sb_list);
379                 if (inode->i_state & I_NEW)
380                         continue;
381                 invalidate_inode_buffers(inode);
382                 if (!atomic_read(&inode->i_count)) {
383                         list_move(&inode->i_list, dispose);
384                         WARN_ON(inode->i_state & I_NEW);
385                         inode->i_state |= I_FREEING;
386                         count++;
387                         continue;
388                 }
389                 busy = 1;
390         }
391         /* only unused inodes may be cached with i_count zero */
392         inodes_stat.nr_unused -= count;
393         return busy;
394 }
395
396 /**
397  *      invalidate_inodes       - discard the inodes on a device
398  *      @sb: superblock
399  *
400  *      Discard all of the inodes for a given superblock. If the discard
401  *      fails because there are busy inodes then a non zero value is returned.
402  *      If the discard is successful all the inodes have been discarded.
403  */
404 int invalidate_inodes(struct super_block *sb)
405 {
406         int busy;
407         LIST_HEAD(throw_away);
408
409         down_write(&iprune_sem);
410         spin_lock(&inode_lock);
411         fsnotify_unmount_inodes(&sb->s_inodes);
412         busy = invalidate_list(&sb->s_inodes, &throw_away);
413         spin_unlock(&inode_lock);
414
415         dispose_list(&throw_away);
416         up_write(&iprune_sem);
417
418         return busy;
419 }
420
421 static int can_unuse(struct inode *inode)
422 {
423         if (inode->i_state)
424                 return 0;
425         if (inode_has_buffers(inode))
426                 return 0;
427         if (atomic_read(&inode->i_count))
428                 return 0;
429         if (inode->i_data.nrpages)
430                 return 0;
431         return 1;
432 }
433
434 /*
435  * Scan `goal' inodes on the unused list for freeable ones. They are moved to
436  * a temporary list and then are freed outside inode_lock by dispose_list().
437  *
438  * Any inodes which are pinned purely because of attached pagecache have their
439  * pagecache removed.  We expect the final iput() on that inode to add it to
440  * the front of the inode_unused list.  So look for it there and if the
441  * inode is still freeable, proceed.  The right inode is found 99.9% of the
442  * time in testing on a 4-way.
443  *
444  * If the inode has metadata buffers attached to mapping->private_list then
445  * try to remove them.
446  */
447 static void prune_icache(int nr_to_scan)
448 {
449         LIST_HEAD(freeable);
450         int nr_pruned = 0;
451         int nr_scanned;
452         unsigned long reap = 0;
453
454         down_read(&iprune_sem);
455         spin_lock(&inode_lock);
456         for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
457                 struct inode *inode;
458
459                 if (list_empty(&inode_unused))
460                         break;
461
462                 inode = list_entry(inode_unused.prev, struct inode, i_list);
463
464                 if (inode->i_state || atomic_read(&inode->i_count)) {
465                         list_move(&inode->i_list, &inode_unused);
466                         continue;
467                 }
468                 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
469                         __iget(inode);
470                         spin_unlock(&inode_lock);
471                         if (remove_inode_buffers(inode))
472                                 reap += invalidate_mapping_pages(&inode->i_data,
473                                                                 0, -1);
474                         iput(inode);
475                         spin_lock(&inode_lock);
476
477                         if (inode != list_entry(inode_unused.next,
478                                                 struct inode, i_list))
479                                 continue;       /* wrong inode or list_empty */
480                         if (!can_unuse(inode))
481                                 continue;
482                 }
483                 list_move(&inode->i_list, &freeable);
484                 WARN_ON(inode->i_state & I_NEW);
485                 inode->i_state |= I_FREEING;
486                 nr_pruned++;
487         }
488         inodes_stat.nr_unused -= nr_pruned;
489         if (current_is_kswapd())
490                 __count_vm_events(KSWAPD_INODESTEAL, reap);
491         else
492                 __count_vm_events(PGINODESTEAL, reap);
493         spin_unlock(&inode_lock);
494
495         dispose_list(&freeable);
496         up_read(&iprune_sem);
497 }
498
499 /*
500  * shrink_icache_memory() will attempt to reclaim some unused inodes.  Here,
501  * "unused" means that no dentries are referring to the inodes: the files are
502  * not open and the dcache references to those inodes have already been
503  * reclaimed.
504  *
505  * This function is passed the number of inodes to scan, and it returns the
506  * total number of remaining possibly-reclaimable inodes.
507  */
508 static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
509 {
510         if (nr) {
511                 /*
512                  * Nasty deadlock avoidance.  We may hold various FS locks,
513                  * and we don't want to recurse into the FS that called us
514                  * in clear_inode() and friends..
515                  */
516                 if (!(gfp_mask & __GFP_FS))
517                         return -1;
518                 prune_icache(nr);
519         }
520         return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
521 }
522
523 static struct shrinker icache_shrinker = {
524         .shrink = shrink_icache_memory,
525         .seeks = DEFAULT_SEEKS,
526 };
527
528 static void __wait_on_freeing_inode(struct inode *inode);
529 /*
530  * Called with the inode lock held.
531  * NOTE: we are not increasing the inode-refcount, you must call __iget()
532  * by hand after calling find_inode now! This simplifies iunique and won't
533  * add any additional branch in the common code.
534  */
535 static struct inode *find_inode(struct super_block *sb,
536                                 struct hlist_head *head,
537                                 int (*test)(struct inode *, void *),
538                                 void *data)
539 {
540         struct hlist_node *node;
541         struct inode *inode = NULL;
542
543 repeat:
544         hlist_for_each_entry(inode, node, head, i_hash) {
545                 if (inode->i_sb != sb)
546                         continue;
547                 if (!test(inode, data))
548                         continue;
549                 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
550                         __wait_on_freeing_inode(inode);
551                         goto repeat;
552                 }
553                 break;
554         }
555         return node ? inode : NULL;
556 }
557
558 /*
559  * find_inode_fast is the fast path version of find_inode, see the comment at
560  * iget_locked for details.
561  */
562 static struct inode *find_inode_fast(struct super_block *sb,
563                                 struct hlist_head *head, unsigned long ino)
564 {
565         struct hlist_node *node;
566         struct inode *inode = NULL;
567
568 repeat:
569         hlist_for_each_entry(inode, node, head, i_hash) {
570                 if (inode->i_ino != ino)
571                         continue;
572                 if (inode->i_sb != sb)
573                         continue;
574                 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
575                         __wait_on_freeing_inode(inode);
576                         goto repeat;
577                 }
578                 break;
579         }
580         return node ? inode : NULL;
581 }
582
583 static unsigned long hash(struct super_block *sb, unsigned long hashval)
584 {
585         unsigned long tmp;
586
587         tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
588                         L1_CACHE_BYTES;
589         tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
590         return tmp & I_HASHMASK;
591 }
592
593 static inline void
594 __inode_add_to_lists(struct super_block *sb, struct hlist_head *head,
595                         struct inode *inode)
596 {
597         inodes_stat.nr_inodes++;
598         list_add(&inode->i_list, &inode_in_use);
599         list_add(&inode->i_sb_list, &sb->s_inodes);
600         if (head)
601                 hlist_add_head(&inode->i_hash, head);
602 }
603
604 /**
605  * inode_add_to_lists - add a new inode to relevant lists
606  * @sb: superblock inode belongs to
607  * @inode: inode to mark in use
608  *
609  * When an inode is allocated it needs to be accounted for, added to the in use
610  * list, the owning superblock and the inode hash. This needs to be done under
611  * the inode_lock, so export a function to do this rather than the inode lock
612  * itself. We calculate the hash list to add to here so it is all internal
613  * which requires the caller to have already set up the inode number in the
614  * inode to add.
615  */
616 void inode_add_to_lists(struct super_block *sb, struct inode *inode)
617 {
618         struct hlist_head *head = inode_hashtable + hash(sb, inode->i_ino);
619
620         spin_lock(&inode_lock);
621         __inode_add_to_lists(sb, head, inode);
622         spin_unlock(&inode_lock);
623 }
624 EXPORT_SYMBOL_GPL(inode_add_to_lists);
625
626 /**
627  *      new_inode       - obtain an inode
628  *      @sb: superblock
629  *
630  *      Allocates a new inode for given superblock. The default gfp_mask
631  *      for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
632  *      If HIGHMEM pages are unsuitable or it is known that pages allocated
633  *      for the page cache are not reclaimable or migratable,
634  *      mapping_set_gfp_mask() must be called with suitable flags on the
635  *      newly created inode's mapping
636  *
637  */
638 struct inode *new_inode(struct super_block *sb)
639 {
640         /*
641          * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
642          * error if st_ino won't fit in target struct field. Use 32bit counter
643          * here to attempt to avoid that.
644          */
645         static unsigned int last_ino;
646         struct inode *inode;
647
648         spin_lock_prefetch(&inode_lock);
649
650         inode = alloc_inode(sb);
651         if (inode) {
652                 spin_lock(&inode_lock);
653                 __inode_add_to_lists(sb, NULL, inode);
654                 inode->i_ino = ++last_ino;
655                 inode->i_state = 0;
656                 spin_unlock(&inode_lock);
657         }
658         return inode;
659 }
660 EXPORT_SYMBOL(new_inode);
661
662 void unlock_new_inode(struct inode *inode)
663 {
664 #ifdef CONFIG_DEBUG_LOCK_ALLOC
665         if (S_ISDIR(inode->i_mode)) {
666                 struct file_system_type *type = inode->i_sb->s_type;
667
668                 /* Set new key only if filesystem hasn't already changed it */
669                 if (!lockdep_match_class(&inode->i_mutex,
670                     &type->i_mutex_key)) {
671                         /*
672                          * ensure nobody is actually holding i_mutex
673                          */
674                         mutex_destroy(&inode->i_mutex);
675                         mutex_init(&inode->i_mutex);
676                         lockdep_set_class(&inode->i_mutex,
677                                           &type->i_mutex_dir_key);
678                 }
679         }
680 #endif
681         /*
682          * This is special!  We do not need the spinlock when clearing I_NEW,
683          * because we're guaranteed that nobody else tries to do anything about
684          * the state of the inode when it is locked, as we just created it (so
685          * there can be no old holders that haven't tested I_NEW).
686          * However we must emit the memory barrier so that other CPUs reliably
687          * see the clearing of I_NEW after the other inode initialisation has
688          * completed.
689          */
690         smp_mb();
691         WARN_ON(!(inode->i_state & I_NEW));
692         inode->i_state &= ~I_NEW;
693         wake_up_inode(inode);
694 }
695 EXPORT_SYMBOL(unlock_new_inode);
696
697 /*
698  * This is called without the inode lock held.. Be careful.
699  *
700  * We no longer cache the sb_flags in i_flags - see fs.h
701  *      -- rmk@arm.uk.linux.org
702  */
703 static struct inode *get_new_inode(struct super_block *sb,
704                                 struct hlist_head *head,
705                                 int (*test)(struct inode *, void *),
706                                 int (*set)(struct inode *, void *),
707                                 void *data)
708 {
709         struct inode *inode;
710
711         inode = alloc_inode(sb);
712         if (inode) {
713                 struct inode *old;
714
715                 spin_lock(&inode_lock);
716                 /* We released the lock, so.. */
717                 old = find_inode(sb, head, test, data);
718                 if (!old) {
719                         if (set(inode, data))
720                                 goto set_failed;
721
722                         __inode_add_to_lists(sb, head, inode);
723                         inode->i_state = I_NEW;
724                         spin_unlock(&inode_lock);
725
726                         /* Return the locked inode with I_NEW set, the
727                          * caller is responsible for filling in the contents
728                          */
729                         return inode;
730                 }
731
732                 /*
733                  * Uhhuh, somebody else created the same inode under
734                  * us. Use the old inode instead of the one we just
735                  * allocated.
736                  */
737                 __iget(old);
738                 spin_unlock(&inode_lock);
739                 destroy_inode(inode);
740                 inode = old;
741                 wait_on_inode(inode);
742         }
743         return inode;
744
745 set_failed:
746         spin_unlock(&inode_lock);
747         destroy_inode(inode);
748         return NULL;
749 }
750
751 /*
752  * get_new_inode_fast is the fast path version of get_new_inode, see the
753  * comment at iget_locked for details.
754  */
755 static struct inode *get_new_inode_fast(struct super_block *sb,
756                                 struct hlist_head *head, unsigned long ino)
757 {
758         struct inode *inode;
759
760         inode = alloc_inode(sb);
761         if (inode) {
762                 struct inode *old;
763
764                 spin_lock(&inode_lock);
765                 /* We released the lock, so.. */
766                 old = find_inode_fast(sb, head, ino);
767                 if (!old) {
768                         inode->i_ino = ino;
769                         __inode_add_to_lists(sb, head, inode);
770                         inode->i_state = I_NEW;
771                         spin_unlock(&inode_lock);
772
773                         /* Return the locked inode with I_NEW set, the
774                          * caller is responsible for filling in the contents
775                          */
776                         return inode;
777                 }
778
779                 /*
780                  * Uhhuh, somebody else created the same inode under
781                  * us. Use the old inode instead of the one we just
782                  * allocated.
783                  */
784                 __iget(old);
785                 spin_unlock(&inode_lock);
786                 destroy_inode(inode);
787                 inode = old;
788                 wait_on_inode(inode);
789         }
790         return inode;
791 }
792
793 /**
794  *      iunique - get a unique inode number
795  *      @sb: superblock
796  *      @max_reserved: highest reserved inode number
797  *
798  *      Obtain an inode number that is unique on the system for a given
799  *      superblock. This is used by file systems that have no natural
800  *      permanent inode numbering system. An inode number is returned that
801  *      is higher than the reserved limit but unique.
802  *
803  *      BUGS:
804  *      With a large number of inodes live on the file system this function
805  *      currently becomes quite slow.
806  */
807 ino_t iunique(struct super_block *sb, ino_t max_reserved)
808 {
809         /*
810          * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
811          * error if st_ino won't fit in target struct field. Use 32bit counter
812          * here to attempt to avoid that.
813          */
814         static unsigned int counter;
815         struct inode *inode;
816         struct hlist_head *head;
817         ino_t res;
818
819         spin_lock(&inode_lock);
820         do {
821                 if (counter <= max_reserved)
822                         counter = max_reserved + 1;
823                 res = counter++;
824                 head = inode_hashtable + hash(sb, res);
825                 inode = find_inode_fast(sb, head, res);
826         } while (inode != NULL);
827         spin_unlock(&inode_lock);
828
829         return res;
830 }
831 EXPORT_SYMBOL(iunique);
832
833 struct inode *igrab(struct inode *inode)
834 {
835         spin_lock(&inode_lock);
836         if (!(inode->i_state & (I_FREEING|I_WILL_FREE)))
837                 __iget(inode);
838         else
839                 /*
840                  * Handle the case where s_op->clear_inode is not been
841                  * called yet, and somebody is calling igrab
842                  * while the inode is getting freed.
843                  */
844                 inode = NULL;
845         spin_unlock(&inode_lock);
846         return inode;
847 }
848 EXPORT_SYMBOL(igrab);
849
850 /**
851  * ifind - internal function, you want ilookup5() or iget5().
852  * @sb:         super block of file system to search
853  * @head:       the head of the list to search
854  * @test:       callback used for comparisons between inodes
855  * @data:       opaque data pointer to pass to @test
856  * @wait:       if true wait for the inode to be unlocked, if false do not
857  *
858  * ifind() searches for the inode specified by @data in the inode
859  * cache. This is a generalized version of ifind_fast() for file systems where
860  * the inode number is not sufficient for unique identification of an inode.
861  *
862  * If the inode is in the cache, the inode is returned with an incremented
863  * reference count.
864  *
865  * Otherwise NULL is returned.
866  *
867  * Note, @test is called with the inode_lock held, so can't sleep.
868  */
869 static struct inode *ifind(struct super_block *sb,
870                 struct hlist_head *head, int (*test)(struct inode *, void *),
871                 void *data, const int wait)
872 {
873         struct inode *inode;
874
875         spin_lock(&inode_lock);
876         inode = find_inode(sb, head, test, data);
877         if (inode) {
878                 __iget(inode);
879                 spin_unlock(&inode_lock);
880                 if (likely(wait))
881                         wait_on_inode(inode);
882                 return inode;
883         }
884         spin_unlock(&inode_lock);
885         return NULL;
886 }
887
888 /**
889  * ifind_fast - internal function, you want ilookup() or iget().
890  * @sb:         super block of file system to search
891  * @head:       head of the list to search
892  * @ino:        inode number to search for
893  *
894  * ifind_fast() searches for the inode @ino in the inode cache. This is for
895  * file systems where the inode number is sufficient for unique identification
896  * of an inode.
897  *
898  * If the inode is in the cache, the inode is returned with an incremented
899  * reference count.
900  *
901  * Otherwise NULL is returned.
902  */
903 static struct inode *ifind_fast(struct super_block *sb,
904                 struct hlist_head *head, unsigned long ino)
905 {
906         struct inode *inode;
907
908         spin_lock(&inode_lock);
909         inode = find_inode_fast(sb, head, ino);
910         if (inode) {
911                 __iget(inode);
912                 spin_unlock(&inode_lock);
913                 wait_on_inode(inode);
914                 return inode;
915         }
916         spin_unlock(&inode_lock);
917         return NULL;
918 }
919
920 /**
921  * ilookup5_nowait - search for an inode in the inode cache
922  * @sb:         super block of file system to search
923  * @hashval:    hash value (usually inode number) to search for
924  * @test:       callback used for comparisons between inodes
925  * @data:       opaque data pointer to pass to @test
926  *
927  * ilookup5() uses ifind() to search for the inode specified by @hashval and
928  * @data in the inode cache. This is a generalized version of ilookup() for
929  * file systems where the inode number is not sufficient for unique
930  * identification of an inode.
931  *
932  * If the inode is in the cache, the inode is returned with an incremented
933  * reference count.  Note, the inode lock is not waited upon so you have to be
934  * very careful what you do with the returned inode.  You probably should be
935  * using ilookup5() instead.
936  *
937  * Otherwise NULL is returned.
938  *
939  * Note, @test is called with the inode_lock held, so can't sleep.
940  */
941 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
942                 int (*test)(struct inode *, void *), void *data)
943 {
944         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
945
946         return ifind(sb, head, test, data, 0);
947 }
948 EXPORT_SYMBOL(ilookup5_nowait);
949
950 /**
951  * ilookup5 - search for an inode in the inode cache
952  * @sb:         super block of file system to search
953  * @hashval:    hash value (usually inode number) to search for
954  * @test:       callback used for comparisons between inodes
955  * @data:       opaque data pointer to pass to @test
956  *
957  * ilookup5() uses ifind() to search for the inode specified by @hashval and
958  * @data in the inode cache. This is a generalized version of ilookup() for
959  * file systems where the inode number is not sufficient for unique
960  * identification of an inode.
961  *
962  * If the inode is in the cache, the inode lock is waited upon and the inode is
963  * returned with an incremented reference count.
964  *
965  * Otherwise NULL is returned.
966  *
967  * Note, @test is called with the inode_lock held, so can't sleep.
968  */
969 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
970                 int (*test)(struct inode *, void *), void *data)
971 {
972         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
973
974         return ifind(sb, head, test, data, 1);
975 }
976 EXPORT_SYMBOL(ilookup5);
977
978 /**
979  * ilookup - search for an inode in the inode cache
980  * @sb:         super block of file system to search
981  * @ino:        inode number to search for
982  *
983  * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
984  * This is for file systems where the inode number is sufficient for unique
985  * identification of an inode.
986  *
987  * If the inode is in the cache, the inode is returned with an incremented
988  * reference count.
989  *
990  * Otherwise NULL is returned.
991  */
992 struct inode *ilookup(struct super_block *sb, unsigned long ino)
993 {
994         struct hlist_head *head = inode_hashtable + hash(sb, ino);
995
996         return ifind_fast(sb, head, ino);
997 }
998 EXPORT_SYMBOL(ilookup);
999
1000 /**
1001  * iget5_locked - obtain an inode from a mounted file system
1002  * @sb:         super block of file system
1003  * @hashval:    hash value (usually inode number) to get
1004  * @test:       callback used for comparisons between inodes
1005  * @set:        callback used to initialize a new struct inode
1006  * @data:       opaque data pointer to pass to @test and @set
1007  *
1008  * iget5_locked() uses ifind() to search for the inode specified by @hashval
1009  * and @data in the inode cache and if present it is returned with an increased
1010  * reference count. This is a generalized version of iget_locked() for file
1011  * systems where the inode number is not sufficient for unique identification
1012  * of an inode.
1013  *
1014  * If the inode is not in cache, get_new_inode() is called to allocate a new
1015  * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1016  * file system gets to fill it in before unlocking it via unlock_new_inode().
1017  *
1018  * Note both @test and @set are called with the inode_lock held, so can't sleep.
1019  */
1020 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1021                 int (*test)(struct inode *, void *),
1022                 int (*set)(struct inode *, void *), void *data)
1023 {
1024         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1025         struct inode *inode;
1026
1027         inode = ifind(sb, head, test, data, 1);
1028         if (inode)
1029                 return inode;
1030         /*
1031          * get_new_inode() will do the right thing, re-trying the search
1032          * in case it had to block at any point.
1033          */
1034         return get_new_inode(sb, head, test, set, data);
1035 }
1036 EXPORT_SYMBOL(iget5_locked);
1037
1038 /**
1039  * iget_locked - obtain an inode from a mounted file system
1040  * @sb:         super block of file system
1041  * @ino:        inode number to get
1042  *
1043  * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1044  * the inode cache and if present it is returned with an increased reference
1045  * count. This is for file systems where the inode number is sufficient for
1046  * unique identification of an inode.
1047  *
1048  * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1049  * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1050  * The file system gets to fill it in before unlocking it via
1051  * unlock_new_inode().
1052  */
1053 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1054 {
1055         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1056         struct inode *inode;
1057
1058         inode = ifind_fast(sb, head, ino);
1059         if (inode)
1060                 return inode;
1061         /*
1062          * get_new_inode_fast() will do the right thing, re-trying the search
1063          * in case it had to block at any point.
1064          */
1065         return get_new_inode_fast(sb, head, ino);
1066 }
1067 EXPORT_SYMBOL(iget_locked);
1068
1069 int insert_inode_locked(struct inode *inode)
1070 {
1071         struct super_block *sb = inode->i_sb;
1072         ino_t ino = inode->i_ino;
1073         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1074
1075         inode->i_state |= I_NEW;
1076         while (1) {
1077                 struct hlist_node *node;
1078                 struct inode *old = NULL;
1079                 spin_lock(&inode_lock);
1080                 hlist_for_each_entry(old, node, head, i_hash) {
1081                         if (old->i_ino != ino)
1082                                 continue;
1083                         if (old->i_sb != sb)
1084                                 continue;
1085                         if (old->i_state & (I_FREEING|I_WILL_FREE))
1086                                 continue;
1087                         break;
1088                 }
1089                 if (likely(!node)) {
1090                         hlist_add_head(&inode->i_hash, head);
1091                         spin_unlock(&inode_lock);
1092                         return 0;
1093                 }
1094                 __iget(old);
1095                 spin_unlock(&inode_lock);
1096                 wait_on_inode(old);
1097                 if (unlikely(!hlist_unhashed(&old->i_hash))) {
1098                         iput(old);
1099                         return -EBUSY;
1100                 }
1101                 iput(old);
1102         }
1103 }
1104 EXPORT_SYMBOL(insert_inode_locked);
1105
1106 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1107                 int (*test)(struct inode *, void *), void *data)
1108 {
1109         struct super_block *sb = inode->i_sb;
1110         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1111
1112         inode->i_state |= I_NEW;
1113
1114         while (1) {
1115                 struct hlist_node *node;
1116                 struct inode *old = NULL;
1117
1118                 spin_lock(&inode_lock);
1119                 hlist_for_each_entry(old, node, head, i_hash) {
1120                         if (old->i_sb != sb)
1121                                 continue;
1122                         if (!test(old, data))
1123                                 continue;
1124                         if (old->i_state & (I_FREEING|I_WILL_FREE))
1125                                 continue;
1126                         break;
1127                 }
1128                 if (likely(!node)) {
1129                         hlist_add_head(&inode->i_hash, head);
1130                         spin_unlock(&inode_lock);
1131                         return 0;
1132                 }
1133                 __iget(old);
1134                 spin_unlock(&inode_lock);
1135                 wait_on_inode(old);
1136                 if (unlikely(!hlist_unhashed(&old->i_hash))) {
1137                         iput(old);
1138                         return -EBUSY;
1139                 }
1140                 iput(old);
1141         }
1142 }
1143 EXPORT_SYMBOL(insert_inode_locked4);
1144
1145 /**
1146  *      __insert_inode_hash - hash an inode
1147  *      @inode: unhashed inode
1148  *      @hashval: unsigned long value used to locate this object in the
1149  *              inode_hashtable.
1150  *
1151  *      Add an inode to the inode hash for this superblock.
1152  */
1153 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
1154 {
1155         struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1156         spin_lock(&inode_lock);
1157         hlist_add_head(&inode->i_hash, head);
1158         spin_unlock(&inode_lock);
1159 }
1160 EXPORT_SYMBOL(__insert_inode_hash);
1161
1162 /**
1163  *      remove_inode_hash - remove an inode from the hash
1164  *      @inode: inode to unhash
1165  *
1166  *      Remove an inode from the superblock.
1167  */
1168 void remove_inode_hash(struct inode *inode)
1169 {
1170         spin_lock(&inode_lock);
1171         hlist_del_init(&inode->i_hash);
1172         spin_unlock(&inode_lock);
1173 }
1174 EXPORT_SYMBOL(remove_inode_hash);
1175
1176 int generic_delete_inode(struct inode *inode)
1177 {
1178         return 1;
1179 }
1180 EXPORT_SYMBOL(generic_delete_inode);
1181
1182 /*
1183  * Normal UNIX filesystem behaviour: delete the
1184  * inode when the usage count drops to zero, and
1185  * i_nlink is zero.
1186  */
1187 int generic_drop_inode(struct inode *inode)
1188 {
1189         return !inode->i_nlink || hlist_unhashed(&inode->i_hash);
1190 }
1191 EXPORT_SYMBOL_GPL(generic_drop_inode);
1192
1193 /*
1194  * Called when we're dropping the last reference
1195  * to an inode.
1196  *
1197  * Call the FS "drop_inode()" function, defaulting to
1198  * the legacy UNIX filesystem behaviour.  If it tells
1199  * us to evict inode, do so.  Otherwise, retain inode
1200  * in cache if fs is alive, sync and evict if fs is
1201  * shutting down.
1202  */
1203 static void iput_final(struct inode *inode)
1204 {
1205         struct super_block *sb = inode->i_sb;
1206         const struct super_operations *op = inode->i_sb->s_op;
1207         int drop;
1208
1209         if (op && op->drop_inode)
1210                 drop = op->drop_inode(inode);
1211         else
1212                 drop = generic_drop_inode(inode);
1213
1214         if (!drop) {
1215                 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1216                         list_move(&inode->i_list, &inode_unused);
1217                 inodes_stat.nr_unused++;
1218                 if (sb->s_flags & MS_ACTIVE) {
1219                         spin_unlock(&inode_lock);
1220                         return;
1221                 }
1222                 WARN_ON(inode->i_state & I_NEW);
1223                 inode->i_state |= I_WILL_FREE;
1224                 spin_unlock(&inode_lock);
1225                 write_inode_now(inode, 1);
1226                 spin_lock(&inode_lock);
1227                 WARN_ON(inode->i_state & I_NEW);
1228                 inode->i_state &= ~I_WILL_FREE;
1229                 inodes_stat.nr_unused--;
1230                 hlist_del_init(&inode->i_hash);
1231         }
1232         list_del_init(&inode->i_list);
1233         list_del_init(&inode->i_sb_list);
1234         WARN_ON(inode->i_state & I_NEW);
1235         inode->i_state |= I_FREEING;
1236         inodes_stat.nr_inodes--;
1237         spin_unlock(&inode_lock);
1238         evict(inode);
1239         spin_lock(&inode_lock);
1240         hlist_del_init(&inode->i_hash);
1241         spin_unlock(&inode_lock);
1242         wake_up_inode(inode);
1243         BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1244         destroy_inode(inode);
1245 }
1246
1247 /**
1248  *      iput    - put an inode
1249  *      @inode: inode to put
1250  *
1251  *      Puts an inode, dropping its usage count. If the inode use count hits
1252  *      zero, the inode is then freed and may also be destroyed.
1253  *
1254  *      Consequently, iput() can sleep.
1255  */
1256 void iput(struct inode *inode)
1257 {
1258         if (inode) {
1259                 BUG_ON(inode->i_state & I_CLEAR);
1260
1261                 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1262                         iput_final(inode);
1263         }
1264 }
1265 EXPORT_SYMBOL(iput);
1266
1267 /**
1268  *      bmap    - find a block number in a file
1269  *      @inode: inode of file
1270  *      @block: block to find
1271  *
1272  *      Returns the block number on the device holding the inode that
1273  *      is the disk block number for the block of the file requested.
1274  *      That is, asked for block 4 of inode 1 the function will return the
1275  *      disk block relative to the disk start that holds that block of the
1276  *      file.
1277  */
1278 sector_t bmap(struct inode *inode, sector_t block)
1279 {
1280         sector_t res = 0;
1281         if (inode->i_mapping->a_ops->bmap)
1282                 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1283         return res;
1284 }
1285 EXPORT_SYMBOL(bmap);
1286
1287 /*
1288  * With relative atime, only update atime if the previous atime is
1289  * earlier than either the ctime or mtime or if at least a day has
1290  * passed since the last atime update.
1291  */
1292 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1293                              struct timespec now)
1294 {
1295
1296         if (!(mnt->mnt_flags & MNT_RELATIME))
1297                 return 1;
1298         /*
1299          * Is mtime younger than atime? If yes, update atime:
1300          */
1301         if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1302                 return 1;
1303         /*
1304          * Is ctime younger than atime? If yes, update atime:
1305          */
1306         if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1307                 return 1;
1308
1309         /*
1310          * Is the previous atime value older than a day? If yes,
1311          * update atime:
1312          */
1313         if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1314                 return 1;
1315         /*
1316          * Good, we can skip the atime update:
1317          */
1318         return 0;
1319 }
1320
1321 /**
1322  *      touch_atime     -       update the access time
1323  *      @mnt: mount the inode is accessed on
1324  *      @dentry: dentry accessed
1325  *
1326  *      Update the accessed time on an inode and mark it for writeback.
1327  *      This function automatically handles read only file systems and media,
1328  *      as well as the "noatime" flag and inode specific "noatime" markers.
1329  */
1330 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1331 {
1332         struct inode *inode = dentry->d_inode;
1333         struct timespec now;
1334
1335         if (inode->i_flags & S_NOATIME)
1336                 return;
1337         if (IS_NOATIME(inode))
1338                 return;
1339         if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1340                 return;
1341
1342         if (mnt->mnt_flags & MNT_NOATIME)
1343                 return;
1344         if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1345                 return;
1346
1347         now = current_fs_time(inode->i_sb);
1348
1349         if (!relatime_need_update(mnt, inode, now))
1350                 return;
1351
1352         if (timespec_equal(&inode->i_atime, &now))
1353                 return;
1354
1355         if (mnt_want_write(mnt))
1356                 return;
1357
1358         inode->i_atime = now;
1359         mark_inode_dirty_sync(inode);
1360         mnt_drop_write(mnt);
1361 }
1362 EXPORT_SYMBOL(touch_atime);
1363
1364 /**
1365  *      file_update_time        -       update mtime and ctime time
1366  *      @file: file accessed
1367  *
1368  *      Update the mtime and ctime members of an inode and mark the inode
1369  *      for writeback.  Note that this function is meant exclusively for
1370  *      usage in the file write path of filesystems, and filesystems may
1371  *      choose to explicitly ignore update via this function with the
1372  *      S_NOCMTIME inode flag, e.g. for network filesystem where these
1373  *      timestamps are handled by the server.
1374  */
1375
1376 void file_update_time(struct file *file)
1377 {
1378         struct inode *inode = file->f_path.dentry->d_inode;
1379         struct timespec now;
1380         enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1381
1382         /* First try to exhaust all avenues to not sync */
1383         if (IS_NOCMTIME(inode))
1384                 return;
1385
1386         now = current_fs_time(inode->i_sb);
1387         if (!timespec_equal(&inode->i_mtime, &now))
1388                 sync_it = S_MTIME;
1389
1390         if (!timespec_equal(&inode->i_ctime, &now))
1391                 sync_it |= S_CTIME;
1392
1393         if (IS_I_VERSION(inode))
1394                 sync_it |= S_VERSION;
1395
1396         if (!sync_it)
1397                 return;
1398
1399         /* Finally allowed to write? Takes lock. */
1400         if (mnt_want_write_file(file))
1401                 return;
1402
1403         /* Only change inode inside the lock region */
1404         if (sync_it & S_VERSION)
1405                 inode_inc_iversion(inode);
1406         if (sync_it & S_CTIME)
1407                 inode->i_ctime = now;
1408         if (sync_it & S_MTIME)
1409                 inode->i_mtime = now;
1410         mark_inode_dirty_sync(inode);
1411         mnt_drop_write(file->f_path.mnt);
1412 }
1413 EXPORT_SYMBOL(file_update_time);
1414
1415 int inode_needs_sync(struct inode *inode)
1416 {
1417         if (IS_SYNC(inode))
1418                 return 1;
1419         if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1420                 return 1;
1421         return 0;
1422 }
1423 EXPORT_SYMBOL(inode_needs_sync);
1424
1425 int inode_wait(void *word)
1426 {
1427         schedule();
1428         return 0;
1429 }
1430 EXPORT_SYMBOL(inode_wait);
1431
1432 /*
1433  * If we try to find an inode in the inode hash while it is being
1434  * deleted, we have to wait until the filesystem completes its
1435  * deletion before reporting that it isn't found.  This function waits
1436  * until the deletion _might_ have completed.  Callers are responsible
1437  * to recheck inode state.
1438  *
1439  * It doesn't matter if I_NEW is not set initially, a call to
1440  * wake_up_inode() after removing from the hash list will DTRT.
1441  *
1442  * This is called with inode_lock held.
1443  */
1444 static void __wait_on_freeing_inode(struct inode *inode)
1445 {
1446         wait_queue_head_t *wq;
1447         DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1448         wq = bit_waitqueue(&inode->i_state, __I_NEW);
1449         prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1450         spin_unlock(&inode_lock);
1451         schedule();
1452         finish_wait(wq, &wait.wait);
1453         spin_lock(&inode_lock);
1454 }
1455
1456 static __initdata unsigned long ihash_entries;
1457 static int __init set_ihash_entries(char *str)
1458 {
1459         if (!str)
1460                 return 0;
1461         ihash_entries = simple_strtoul(str, &str, 0);
1462         return 1;
1463 }
1464 __setup("ihash_entries=", set_ihash_entries);
1465
1466 /*
1467  * Initialize the waitqueues and inode hash table.
1468  */
1469 void __init inode_init_early(void)
1470 {
1471         int loop;
1472
1473         /* If hashes are distributed across NUMA nodes, defer
1474          * hash allocation until vmalloc space is available.
1475          */
1476         if (hashdist)
1477                 return;
1478
1479         inode_hashtable =
1480                 alloc_large_system_hash("Inode-cache",
1481                                         sizeof(struct hlist_head),
1482                                         ihash_entries,
1483                                         14,
1484                                         HASH_EARLY,
1485                                         &i_hash_shift,
1486                                         &i_hash_mask,
1487                                         0);
1488
1489         for (loop = 0; loop < (1 << i_hash_shift); loop++)
1490                 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1491 }
1492
1493 void __init inode_init(void)
1494 {
1495         int loop;
1496
1497         /* inode slab cache */
1498         inode_cachep = kmem_cache_create("inode_cache",
1499                                          sizeof(struct inode),
1500                                          0,
1501                                          (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1502                                          SLAB_MEM_SPREAD),
1503                                          init_once);
1504         register_shrinker(&icache_shrinker);
1505
1506         /* Hash may have been set up in inode_init_early */
1507         if (!hashdist)
1508                 return;
1509
1510         inode_hashtable =
1511                 alloc_large_system_hash("Inode-cache",
1512                                         sizeof(struct hlist_head),
1513                                         ihash_entries,
1514                                         14,
1515                                         0,
1516                                         &i_hash_shift,
1517                                         &i_hash_mask,
1518                                         0);
1519
1520         for (loop = 0; loop < (1 << i_hash_shift); loop++)
1521                 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1522 }
1523
1524 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1525 {
1526         inode->i_mode = mode;
1527         if (S_ISCHR(mode)) {
1528                 inode->i_fop = &def_chr_fops;
1529                 inode->i_rdev = rdev;
1530         } else if (S_ISBLK(mode)) {
1531                 inode->i_fop = &def_blk_fops;
1532                 inode->i_rdev = rdev;
1533         } else if (S_ISFIFO(mode))
1534                 inode->i_fop = &def_fifo_fops;
1535         else if (S_ISSOCK(mode))
1536                 inode->i_fop = &bad_sock_fops;
1537         else
1538                 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1539                                   " inode %s:%lu\n", mode, inode->i_sb->s_id,
1540                                   inode->i_ino);
1541 }
1542 EXPORT_SYMBOL(init_special_inode);
1543
1544 /**
1545  * Init uid,gid,mode for new inode according to posix standards
1546  * @inode: New inode
1547  * @dir: Directory inode
1548  * @mode: mode of the new inode
1549  */
1550 void inode_init_owner(struct inode *inode, const struct inode *dir,
1551                         mode_t mode)
1552 {
1553         inode->i_uid = current_fsuid();
1554         if (dir && dir->i_mode & S_ISGID) {
1555                 inode->i_gid = dir->i_gid;
1556                 if (S_ISDIR(mode))
1557                         mode |= S_ISGID;
1558         } else
1559                 inode->i_gid = current_fsgid();
1560         inode->i_mode = mode;
1561 }
1562 EXPORT_SYMBOL(inode_init_owner);