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