282d86aed6224fabb25e0bfd5f1f479dfcb2188b
[linux-2.6.git] / fs / inode.c
1 /*
2  * linux/fs/inode.c
3  *
4  * (C) 1997 Linus Torvalds
5  */
6
7 #include <linux/config.h>
8 #include <linux/fs.h>
9 #include <linux/mm.h>
10 #include <linux/dcache.h>
11 #include <linux/init.h>
12 #include <linux/quotaops.h>
13 #include <linux/slab.h>
14 #include <linux/writeback.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/wait.h>
18 #include <linux/hash.h>
19 #include <linux/swap.h>
20 #include <linux/security.h>
21 #include <linux/pagemap.h>
22 #include <linux/cdev.h>
23
24 /*
25  * This is needed for the following functions:
26  *  - inode_has_buffers
27  *  - invalidate_inode_buffers
28  *  - fsync_bdev
29  *  - invalidate_bdev
30  *
31  * FIXME: remove all knowledge of the buffer layer from this file
32  */
33 #include <linux/buffer_head.h>
34
35 /*
36  * New inode.c implementation.
37  *
38  * This implementation has the basic premise of trying
39  * to be extremely low-overhead and SMP-safe, yet be
40  * simple enough to be "obviously correct".
41  *
42  * Famous last words.
43  */
44
45 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
46
47 /* #define INODE_PARANOIA 1 */
48 /* #define INODE_DEBUG 1 */
49
50 /*
51  * Inode lookup is no longer as critical as it used to be:
52  * most of the lookups are going to be through the dcache.
53  */
54 #define I_HASHBITS      i_hash_shift
55 #define I_HASHMASK      i_hash_mask
56
57 static unsigned int i_hash_mask;
58 static unsigned int i_hash_shift;
59
60 /*
61  * Each inode can be on two separate lists. One is
62  * the hash list of the inode, used for lookups. The
63  * other linked list is the "type" list:
64  *  "in_use" - valid inode, i_count > 0, i_nlink > 0
65  *  "dirty"  - as "in_use" but also dirty
66  *  "unused" - valid inode, i_count = 0
67  *
68  * A "dirty" list is maintained for each super block,
69  * allowing for low-overhead inode sync() operations.
70  */
71
72 LIST_HEAD(inode_in_use);
73 LIST_HEAD(inode_unused);
74 static struct hlist_head *inode_hashtable;
75
76 /*
77  * A simple spinlock to protect the list manipulations.
78  *
79  * NOTE! You also have to own the lock if you change
80  * the i_state of an inode while it is in use..
81  */
82 spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
83
84 /*
85  * iprune_sem provides exclusion between the kswapd or try_to_free_pages
86  * icache shrinking path, and the umount path.  Without this exclusion,
87  * by the time prune_icache calls iput for the inode whose pages it has
88  * been invalidating, or by the time it calls clear_inode & destroy_inode
89  * from its final dispose_list, the struct super_block they refer to
90  * (for inode->i_sb->s_op) may already have been freed and reused.
91  */
92 static DECLARE_MUTEX(iprune_sem);
93
94 /*
95  * Statistics gathering..
96  */
97 struct inodes_stat_t inodes_stat;
98
99 static kmem_cache_t * inode_cachep;
100
101 static struct inode *alloc_inode(struct super_block *sb)
102 {
103         static struct address_space_operations empty_aops;
104         static struct inode_operations empty_iops;
105         static struct file_operations empty_fops;
106         struct inode *inode;
107
108         if (sb->s_op->alloc_inode)
109                 inode = sb->s_op->alloc_inode(sb);
110         else
111                 inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
112
113         if (inode) {
114                 struct address_space * const mapping = &inode->i_data;
115
116                 inode->i_sb = sb;
117                 inode->i_blkbits = sb->s_blocksize_bits;
118                 inode->i_flags = 0;
119                 atomic_set(&inode->i_count, 1);
120                 inode->i_sock = 0;
121                 inode->i_op = &empty_iops;
122                 inode->i_fop = &empty_fops;
123                 inode->i_nlink = 1;
124                 atomic_set(&inode->i_writecount, 0);
125                 inode->i_size = 0;
126                 inode->i_blocks = 0;
127                 inode->i_bytes = 0;
128                 inode->i_generation = 0;
129 #ifdef CONFIG_QUOTA
130                 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
131 #endif
132                 inode->i_pipe = NULL;
133                 inode->i_bdev = NULL;
134                 inode->i_cdev = NULL;
135                 inode->i_rdev = 0;
136                 inode->i_security = NULL;
137                 inode->dirtied_when = 0;
138                 if (security_inode_alloc(inode)) {
139                         if (inode->i_sb->s_op->destroy_inode)
140                                 inode->i_sb->s_op->destroy_inode(inode);
141                         else
142                                 kmem_cache_free(inode_cachep, (inode));
143                         return NULL;
144                 }
145
146                 mapping->a_ops = &empty_aops;
147                 mapping->host = inode;
148                 mapping->flags = 0;
149                 mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
150                 mapping->assoc_mapping = NULL;
151                 mapping->backing_dev_info = &default_backing_dev_info;
152                 if (sb->s_bdev)
153                         mapping->backing_dev_info = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
154                 memset(&inode->u, 0, sizeof(inode->u));
155                 inode->i_mapping = mapping;
156         }
157         return inode;
158 }
159
160 void destroy_inode(struct inode *inode) 
161 {
162         if (inode_has_buffers(inode))
163                 BUG();
164         security_inode_free(inode);
165         if (inode->i_sb->s_op->destroy_inode)
166                 inode->i_sb->s_op->destroy_inode(inode);
167         else
168                 kmem_cache_free(inode_cachep, (inode));
169 }
170
171
172 /*
173  * These are initializations that only need to be done
174  * once, because the fields are idempotent across use
175  * of the inode, so let the slab aware of that.
176  */
177 void inode_init_once(struct inode *inode)
178 {
179         memset(inode, 0, sizeof(*inode));
180         INIT_HLIST_NODE(&inode->i_hash);
181         INIT_LIST_HEAD(&inode->i_dentry);
182         INIT_LIST_HEAD(&inode->i_devices);
183         sema_init(&inode->i_sem, 1);
184         init_rwsem(&inode->i_alloc_sem);
185         INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
186         spin_lock_init(&inode->i_data.tree_lock);
187         init_MUTEX(&inode->i_data.i_shared_sem);
188         atomic_set(&inode->i_data.truncate_count, 0);
189         INIT_LIST_HEAD(&inode->i_data.private_list);
190         spin_lock_init(&inode->i_data.private_lock);
191         INIT_LIST_HEAD(&inode->i_data.i_mmap);
192         INIT_LIST_HEAD(&inode->i_data.i_mmap_shared);
193         spin_lock_init(&inode->i_lock);
194         i_size_ordered_init(inode);
195 }
196
197 EXPORT_SYMBOL(inode_init_once);
198
199 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
200 {
201         struct inode * inode = (struct inode *) foo;
202
203         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
204             SLAB_CTOR_CONSTRUCTOR)
205                 inode_init_once(inode);
206 }
207
208 /*
209  * inode_lock must be held
210  */
211 void __iget(struct inode * inode)
212 {
213         if (atomic_read(&inode->i_count)) {
214                 atomic_inc(&inode->i_count);
215                 return;
216         }
217         atomic_inc(&inode->i_count);
218         if (!(inode->i_state & (I_DIRTY|I_LOCK)))
219                 list_move(&inode->i_list, &inode_in_use);
220         inodes_stat.nr_unused--;
221 }
222
223 /**
224  * clear_inode - clear an inode
225  * @inode: inode to clear
226  *
227  * This is called by the filesystem to tell us
228  * that the inode is no longer useful. We just
229  * terminate it with extreme prejudice.
230  */
231 void clear_inode(struct inode *inode)
232 {
233         invalidate_inode_buffers(inode);
234        
235         if (inode->i_data.nrpages)
236                 BUG();
237         if (!(inode->i_state & I_FREEING))
238                 BUG();
239         if (inode->i_state & I_CLEAR)
240                 BUG();
241         wait_on_inode(inode);
242         DQUOT_DROP(inode);
243         if (inode->i_sb && inode->i_sb->s_op->clear_inode)
244                 inode->i_sb->s_op->clear_inode(inode);
245         if (inode->i_bdev)
246                 bd_forget(inode);
247         if (inode->i_cdev)
248                 cd_forget(inode);
249         inode->i_state = I_CLEAR;
250 }
251
252 EXPORT_SYMBOL(clear_inode);
253
254 /*
255  * dispose_list - dispose of the contents of a local list
256  * @head: the head of the list to free
257  *
258  * Dispose-list gets a local list with local inodes in it, so it doesn't
259  * need to worry about list corruption and SMP locks.
260  */
261 static void dispose_list(struct list_head *head)
262 {
263         int nr_disposed = 0;
264
265         while (!list_empty(head)) {
266                 struct inode *inode;
267
268                 inode = list_entry(head->next, struct inode, i_list);
269                 list_del(&inode->i_list);
270
271                 if (inode->i_data.nrpages)
272                         truncate_inode_pages(&inode->i_data, 0);
273                 clear_inode(inode);
274                 destroy_inode(inode);
275                 nr_disposed++;
276         }
277         spin_lock(&inode_lock);
278         inodes_stat.nr_inodes -= nr_disposed;
279         spin_unlock(&inode_lock);
280 }
281
282 /*
283  * Invalidate all inodes for a device.
284  */
285 static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
286 {
287         struct list_head *next;
288         int busy = 0, count = 0;
289
290         next = head->next;
291         for (;;) {
292                 struct list_head * tmp = next;
293                 struct inode * inode;
294
295                 next = next->next;
296                 if (tmp == head)
297                         break;
298                 inode = list_entry(tmp, struct inode, i_list);
299                 if (inode->i_sb != sb)
300                         continue;
301                 invalidate_inode_buffers(inode);
302                 if (!atomic_read(&inode->i_count)) {
303                         hlist_del_init(&inode->i_hash);
304                         list_move(&inode->i_list, dispose);
305                         inode->i_state |= I_FREEING;
306                         count++;
307                         continue;
308                 }
309                 busy = 1;
310         }
311         /* only unused inodes may be cached with i_count zero */
312         inodes_stat.nr_unused -= count;
313         return busy;
314 }
315
316 /*
317  * This is a two-stage process. First we collect all
318  * offending inodes onto the throw-away list, and in
319  * the second stage we actually dispose of them. This
320  * is because we don't want to sleep while messing
321  * with the global lists..
322  */
323  
324 /**
325  *      invalidate_inodes       - discard the inodes on a device
326  *      @sb: superblock
327  *
328  *      Discard all of the inodes for a given superblock. If the discard
329  *      fails because there are busy inodes then a non zero value is returned.
330  *      If the discard is successful all the inodes have been discarded.
331  */
332 int invalidate_inodes(struct super_block * sb)
333 {
334         int busy;
335         LIST_HEAD(throw_away);
336
337         down(&iprune_sem);
338         spin_lock(&inode_lock);
339         busy = invalidate_list(&inode_in_use, sb, &throw_away);
340         busy |= invalidate_list(&inode_unused, sb, &throw_away);
341         busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
342         busy |= invalidate_list(&sb->s_io, sb, &throw_away);
343         spin_unlock(&inode_lock);
344
345         dispose_list(&throw_away);
346         up(&iprune_sem);
347
348         return busy;
349 }
350
351 EXPORT_SYMBOL(invalidate_inodes);
352  
353 int __invalidate_device(struct block_device *bdev, int do_sync)
354 {
355         struct super_block *sb;
356         int res;
357
358         if (do_sync)
359                 fsync_bdev(bdev);
360
361         res = 0;
362         sb = get_super(bdev);
363         if (sb) {
364                 /*
365                  * no need to lock the super, get_super holds the
366                  * read semaphore so the filesystem cannot go away
367                  * under us (->put_super runs with the write lock
368                  * hold).
369                  */
370                 shrink_dcache_sb(sb);
371                 res = invalidate_inodes(sb);
372                 drop_super(sb);
373         }
374         invalidate_bdev(bdev, 0);
375         return res;
376 }
377
378 EXPORT_SYMBOL(__invalidate_device);
379
380 static int can_unuse(struct inode *inode)
381 {
382         if (inode->i_state)
383                 return 0;
384         if (inode_has_buffers(inode))
385                 return 0;
386         if (atomic_read(&inode->i_count))
387                 return 0;
388         if (inode->i_data.nrpages)
389                 return 0;
390         return 1;
391 }
392
393 /*
394  * Scan `goal' inodes on the unused list for freeable ones. They are moved to
395  * a temporary list and then are freed outside inode_lock by dispose_list().
396  *
397  * Any inodes which are pinned purely because of attached pagecache have their
398  * pagecache removed.  We expect the final iput() on that inode to add it to
399  * the front of the inode_unused list.  So look for it there and if the
400  * inode is still freeable, proceed.  The right inode is found 99.9% of the
401  * time in testing on a 4-way.
402  *
403  * If the inode has metadata buffers attached to mapping->private_list then
404  * try to remove them.
405  */
406 static void prune_icache(int nr_to_scan)
407 {
408         LIST_HEAD(freeable);
409         int nr_pruned = 0;
410         int nr_scanned;
411         unsigned long reap = 0;
412
413         down(&iprune_sem);
414         spin_lock(&inode_lock);
415         for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
416                 struct inode *inode;
417
418                 if (list_empty(&inode_unused))
419                         break;
420
421                 inode = list_entry(inode_unused.prev, struct inode, i_list);
422
423                 if (inode->i_state || atomic_read(&inode->i_count)) {
424                         list_move(&inode->i_list, &inode_unused);
425                         continue;
426                 }
427                 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
428                         __iget(inode);
429                         spin_unlock(&inode_lock);
430                         if (remove_inode_buffers(inode))
431                                 reap += invalidate_inode_pages(&inode->i_data);
432                         iput(inode);
433                         spin_lock(&inode_lock);
434
435                         if (inode != list_entry(inode_unused.next,
436                                                 struct inode, i_list))
437                                 continue;       /* wrong inode or list_empty */
438                         if (!can_unuse(inode))
439                                 continue;
440                 }
441                 hlist_del_init(&inode->i_hash);
442                 list_move(&inode->i_list, &freeable);
443                 inode->i_state |= I_FREEING;
444                 nr_pruned++;
445         }
446         inodes_stat.nr_unused -= nr_pruned;
447         spin_unlock(&inode_lock);
448
449         dispose_list(&freeable);
450         up(&iprune_sem);
451
452         if (current_is_kswapd())
453                 mod_page_state(kswapd_inodesteal, reap);
454         else
455                 mod_page_state(pginodesteal, reap);
456 }
457
458 /*
459  * shrink_icache_memory() will attempt to reclaim some unused inodes.  Here,
460  * "unused" means that no dentries are referring to the inodes: the files are
461  * not open and the dcache references to those inodes have already been
462  * reclaimed.
463  *
464  * This function is passed the number of inodes to scan, and it returns the
465  * total number of remaining possibly-reclaimable inodes.
466  */
467 static int shrink_icache_memory(int nr, unsigned int gfp_mask)
468 {
469         if (nr) {
470                 /*
471                  * Nasty deadlock avoidance.  We may hold various FS locks,
472                  * and we don't want to recurse into the FS that called us
473                  * in clear_inode() and friends..
474                  */
475                 if (gfp_mask & __GFP_FS)
476                         prune_icache(nr);
477         }
478         return inodes_stat.nr_unused;
479 }
480
481 static void __wait_on_freeing_inode(struct inode *inode);
482 /*
483  * Called with the inode lock held.
484  * NOTE: we are not increasing the inode-refcount, you must call __iget()
485  * by hand after calling find_inode now! This simplifies iunique and won't
486  * add any additional branch in the common code.
487  */
488 static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
489 {
490         struct hlist_node *node;
491         struct inode * inode = NULL;
492
493 repeat:
494         hlist_for_each (node, head) { 
495                 inode = hlist_entry(node, struct inode, i_hash);
496                 if (inode->i_sb != sb)
497                         continue;
498                 if (!test(inode, data))
499                         continue;
500                 if (inode->i_state & (I_FREEING|I_CLEAR)) {
501                         __wait_on_freeing_inode(inode);
502                         goto repeat;
503                 }
504                 break;
505         }
506         return node ? inode : NULL;
507 }
508
509 /*
510  * find_inode_fast is the fast path version of find_inode, see the comment at
511  * iget_locked for details.
512  */
513 static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
514 {
515         struct hlist_node *node;
516         struct inode * inode = NULL;
517
518 repeat:
519         hlist_for_each (node, head) {
520                 inode = hlist_entry(node, struct inode, i_hash);
521                 if (inode->i_ino != ino)
522                         continue;
523                 if (inode->i_sb != sb)
524                         continue;
525                 if (inode->i_state & (I_FREEING|I_CLEAR)) {
526                         __wait_on_freeing_inode(inode);
527                         goto repeat;
528                 }
529                 break;
530         }
531         return node ? inode : NULL;
532 }
533
534 /**
535  *      new_inode       - obtain an inode
536  *      @sb: superblock
537  *
538  *      Allocates a new inode for given superblock.
539  */
540 struct inode *new_inode(struct super_block *sb)
541 {
542         static unsigned long last_ino;
543         struct inode * inode;
544
545         spin_lock_prefetch(&inode_lock);
546         
547         inode = alloc_inode(sb);
548         if (inode) {
549                 spin_lock(&inode_lock);
550                 inodes_stat.nr_inodes++;
551                 list_add(&inode->i_list, &inode_in_use);
552                 inode->i_ino = ++last_ino;
553                 inode->i_state = 0;
554                 spin_unlock(&inode_lock);
555         }
556         return inode;
557 }
558
559 EXPORT_SYMBOL(new_inode);
560
561 void unlock_new_inode(struct inode *inode)
562 {
563         /*
564          * This is special!  We do not need the spinlock
565          * when clearing I_LOCK, because we're guaranteed
566          * that nobody else tries to do anything about the
567          * state of the inode when it is locked, as we
568          * just created it (so there can be no old holders
569          * that haven't tested I_LOCK).
570          */
571         inode->i_state &= ~(I_LOCK|I_NEW);
572         wake_up_inode(inode);
573 }
574
575 EXPORT_SYMBOL(unlock_new_inode);
576
577 /*
578  * This is called without the inode lock held.. Be careful.
579  *
580  * We no longer cache the sb_flags in i_flags - see fs.h
581  *      -- rmk@arm.uk.linux.org
582  */
583 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)
584 {
585         struct inode * inode;
586
587         inode = alloc_inode(sb);
588         if (inode) {
589                 struct inode * old;
590
591                 spin_lock(&inode_lock);
592                 /* We released the lock, so.. */
593                 old = find_inode(sb, head, test, data);
594                 if (!old) {
595                         if (set(inode, data))
596                                 goto set_failed;
597
598                         inodes_stat.nr_inodes++;
599                         list_add(&inode->i_list, &inode_in_use);
600                         hlist_add_head(&inode->i_hash, head);
601                         inode->i_state = I_LOCK|I_NEW;
602                         spin_unlock(&inode_lock);
603
604                         /* Return the locked inode with I_NEW set, the
605                          * caller is responsible for filling in the contents
606                          */
607                         return inode;
608                 }
609
610                 /*
611                  * Uhhuh, somebody else created the same inode under
612                  * us. Use the old inode instead of the one we just
613                  * allocated.
614                  */
615                 __iget(old);
616                 spin_unlock(&inode_lock);
617                 destroy_inode(inode);
618                 inode = old;
619                 wait_on_inode(inode);
620         }
621         return inode;
622
623 set_failed:
624         spin_unlock(&inode_lock);
625         destroy_inode(inode);
626         return NULL;
627 }
628
629 /*
630  * get_new_inode_fast is the fast path version of get_new_inode, see the
631  * comment at iget_locked for details.
632  */
633 static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
634 {
635         struct inode * inode;
636
637         inode = alloc_inode(sb);
638         if (inode) {
639                 struct inode * old;
640
641                 spin_lock(&inode_lock);
642                 /* We released the lock, so.. */
643                 old = find_inode_fast(sb, head, ino);
644                 if (!old) {
645                         inode->i_ino = ino;
646                         inodes_stat.nr_inodes++;
647                         list_add(&inode->i_list, &inode_in_use);
648                         hlist_add_head(&inode->i_hash, head);
649                         inode->i_state = I_LOCK|I_NEW;
650                         spin_unlock(&inode_lock);
651
652                         /* Return the locked inode with I_NEW set, the
653                          * caller is responsible for filling in the contents
654                          */
655                         return inode;
656                 }
657
658                 /*
659                  * Uhhuh, somebody else created the same inode under
660                  * us. Use the old inode instead of the one we just
661                  * allocated.
662                  */
663                 __iget(old);
664                 spin_unlock(&inode_lock);
665                 destroy_inode(inode);
666                 inode = old;
667                 wait_on_inode(inode);
668         }
669         return inode;
670 }
671
672 static inline unsigned long hash(struct super_block *sb, unsigned long hashval)
673 {
674         unsigned long tmp = hashval + ((unsigned long) sb / L1_CACHE_BYTES);
675         tmp = tmp + (tmp >> I_HASHBITS);
676         return tmp & I_HASHMASK;
677 }
678
679 /* Yeah, I know about quadratic hash. Maybe, later. */
680
681 /**
682  *      iunique - get a unique inode number
683  *      @sb: superblock
684  *      @max_reserved: highest reserved inode number
685  *
686  *      Obtain an inode number that is unique on the system for a given
687  *      superblock. This is used by file systems that have no natural
688  *      permanent inode numbering system. An inode number is returned that
689  *      is higher than the reserved limit but unique.
690  *
691  *      BUGS:
692  *      With a large number of inodes live on the file system this function
693  *      currently becomes quite slow.
694  */
695 ino_t iunique(struct super_block *sb, ino_t max_reserved)
696 {
697         static ino_t counter;
698         struct inode *inode;
699         struct hlist_head * head;
700         ino_t res;
701         spin_lock(&inode_lock);
702 retry:
703         if (counter > max_reserved) {
704                 head = inode_hashtable + hash(sb,counter);
705                 res = counter++;
706                 inode = find_inode_fast(sb, head, res);
707                 if (!inode) {
708                         spin_unlock(&inode_lock);
709                         return res;
710                 }
711         } else {
712                 counter = max_reserved + 1;
713         }
714         goto retry;
715         
716 }
717
718 EXPORT_SYMBOL(iunique);
719
720 struct inode *igrab(struct inode *inode)
721 {
722         spin_lock(&inode_lock);
723         if (!(inode->i_state & I_FREEING))
724                 __iget(inode);
725         else
726                 /*
727                  * Handle the case where s_op->clear_inode is not been
728                  * called yet, and somebody is calling igrab
729                  * while the inode is getting freed.
730                  */
731                 inode = NULL;
732         spin_unlock(&inode_lock);
733         return inode;
734 }
735
736 EXPORT_SYMBOL(igrab);
737
738 /**
739  * ifind - internal function, you want ilookup5() or iget5().
740  * @sb:         super block of file system to search
741  * @head:       the head of the list to search
742  * @test:       callback used for comparisons between inodes
743  * @data:       opaque data pointer to pass to @test
744  *
745  * ifind() searches for the inode specified by @data in the inode
746  * cache. This is a generalized version of ifind_fast() for file systems where
747  * the inode number is not sufficient for unique identification of an inode.
748  *
749  * If the inode is in the cache, the inode is returned with an incremented
750  * reference count.
751  *
752  * Otherwise NULL is returned.
753  *
754  * Note, @test is called with the inode_lock held, so can't sleep.
755  */
756 static inline struct inode *ifind(struct super_block *sb,
757                 struct hlist_head *head, int (*test)(struct inode *, void *),
758                 void *data)
759 {
760         struct inode *inode;
761
762         spin_lock(&inode_lock);
763         inode = find_inode(sb, head, test, data);
764         if (inode) {
765                 __iget(inode);
766                 spin_unlock(&inode_lock);
767                 wait_on_inode(inode);
768                 return inode;
769         }
770         spin_unlock(&inode_lock);
771         return NULL;
772 }
773
774 /**
775  * ifind_fast - internal function, you want ilookup() or iget().
776  * @sb:         super block of file system to search
777  * @head:       head of the list to search
778  * @ino:        inode number to search for
779  *
780  * ifind_fast() searches for the inode @ino in the inode cache. This is for
781  * file systems where the inode number is sufficient for unique identification
782  * of an inode.
783  *
784  * If the inode is in the cache, the inode is returned with an incremented
785  * reference count.
786  *
787  * Otherwise NULL is returned.
788  */
789 static inline struct inode *ifind_fast(struct super_block *sb,
790                 struct hlist_head *head, unsigned long ino)
791 {
792         struct inode *inode;
793
794         spin_lock(&inode_lock);
795         inode = find_inode_fast(sb, head, ino);
796         if (inode) {
797                 __iget(inode);
798                 spin_unlock(&inode_lock);
799                 wait_on_inode(inode);
800                 return inode;
801         }
802         spin_unlock(&inode_lock);
803         return NULL;
804 }
805
806 /**
807  * ilookup5 - search for an inode in the inode cache
808  * @sb:         super block of file system to search
809  * @hashval:    hash value (usually inode number) to search for
810  * @test:       callback used for comparisons between inodes
811  * @data:       opaque data pointer to pass to @test
812  *
813  * ilookup5() uses ifind() to search for the inode specified by @hashval and
814  * @data in the inode cache. This is a generalized version of ilookup() for
815  * file systems where the inode number is not sufficient for unique
816  * identification of an inode.
817  *
818  * If the inode is in the cache, the inode is returned with an incremented
819  * reference count.
820  *
821  * Otherwise NULL is returned.
822  *
823  * Note, @test is called with the inode_lock held, so can't sleep.
824  */
825 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
826                 int (*test)(struct inode *, void *), void *data)
827 {
828         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
829
830         return ifind(sb, head, test, data);
831 }
832
833 EXPORT_SYMBOL(ilookup5);
834
835 /**
836  * ilookup - search for an inode in the inode cache
837  * @sb:         super block of file system to search
838  * @ino:        inode number to search for
839  *
840  * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
841  * This is for file systems where the inode number is sufficient for unique
842  * identification of an inode.
843  *
844  * If the inode is in the cache, the inode is returned with an incremented
845  * reference count.
846  *
847  * Otherwise NULL is returned.
848  */
849 struct inode *ilookup(struct super_block *sb, unsigned long ino)
850 {
851         struct hlist_head *head = inode_hashtable + hash(sb, ino);
852
853         return ifind_fast(sb, head, ino);
854 }
855
856 EXPORT_SYMBOL(ilookup);
857
858 /**
859  * iget5_locked - obtain an inode from a mounted file system
860  * @sb:         super block of file system
861  * @hashval:    hash value (usually inode number) to get
862  * @test:       callback used for comparisons between inodes
863  * @set:        callback used to initialize a new struct inode
864  * @data:       opaque data pointer to pass to @test and @set
865  *
866  * This is iget() without the read_inode() portion of get_new_inode().
867  *
868  * iget5_locked() uses ifind() to search for the inode specified by @hashval
869  * and @data in the inode cache and if present it is returned with an increased
870  * reference count. This is a generalized version of iget_locked() for file
871  * systems where the inode number is not sufficient for unique identification
872  * of an inode.
873  *
874  * If the inode is not in cache, get_new_inode() is called to allocate a new
875  * inode and this is returned locked, hashed, and with the I_NEW flag set. The
876  * file system gets to fill it in before unlocking it via unlock_new_inode().
877  *
878  * Note both @test and @set are called with the inode_lock held, so can't sleep.
879  */
880 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
881                 int (*test)(struct inode *, void *),
882                 int (*set)(struct inode *, void *), void *data)
883 {
884         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
885         struct inode *inode;
886
887         inode = ifind(sb, head, test, data);
888         if (inode)
889                 return inode;
890         /*
891          * get_new_inode() will do the right thing, re-trying the search
892          * in case it had to block at any point.
893          */
894         return get_new_inode(sb, head, test, set, data);
895 }
896
897 EXPORT_SYMBOL(iget5_locked);
898
899 /**
900  * iget_locked - obtain an inode from a mounted file system
901  * @sb:         super block of file system
902  * @ino:        inode number to get
903  *
904  * This is iget() without the read_inode() portion of get_new_inode_fast().
905  *
906  * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
907  * the inode cache and if present it is returned with an increased reference
908  * count. This is for file systems where the inode number is sufficient for
909  * unique identification of an inode.
910  *
911  * If the inode is not in cache, get_new_inode_fast() is called to allocate a
912  * new inode and this is returned locked, hashed, and with the I_NEW flag set.
913  * The file system gets to fill it in before unlocking it via
914  * unlock_new_inode().
915  */
916 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
917 {
918         struct hlist_head *head = inode_hashtable + hash(sb, ino);
919         struct inode *inode;
920
921         inode = ifind_fast(sb, head, ino);
922         if (inode)
923                 return inode;
924         /*
925          * get_new_inode_fast() will do the right thing, re-trying the search
926          * in case it had to block at any point.
927          */
928         return get_new_inode_fast(sb, head, ino);
929 }
930
931 EXPORT_SYMBOL(iget_locked);
932
933 /**
934  *      __insert_inode_hash - hash an inode
935  *      @inode: unhashed inode
936  *      @hashval: unsigned long value used to locate this object in the
937  *              inode_hashtable.
938  *
939  *      Add an inode to the inode hash for this superblock.
940  */
941 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
942 {
943         struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
944         spin_lock(&inode_lock);
945         hlist_add_head(&inode->i_hash, head);
946         spin_unlock(&inode_lock);
947 }
948
949 EXPORT_SYMBOL(__insert_inode_hash);
950
951 /**
952  *      remove_inode_hash - remove an inode from the hash
953  *      @inode: inode to unhash
954  *
955  *      Remove an inode from the superblock.
956  */
957 void remove_inode_hash(struct inode *inode)
958 {
959         spin_lock(&inode_lock);
960         hlist_del_init(&inode->i_hash);
961         spin_unlock(&inode_lock);
962 }
963
964 EXPORT_SYMBOL(remove_inode_hash);
965
966 /*
967  * Tell the filesystem that this inode is no longer of any interest and should
968  * be completely destroyed.
969  *
970  * We leave the inode in the inode hash table until *after* the filesystem's
971  * ->delete_inode completes.  This ensures that an iget (such as nfsd might
972  * instigate) will always find up-to-date information either in the hash or on
973  * disk.
974  *
975  * I_FREEING is set so that no-one will take a new reference to the inode while
976  * it is being deleted.
977  */
978 void generic_delete_inode(struct inode *inode)
979 {
980         struct super_operations *op = inode->i_sb->s_op;
981
982         list_del_init(&inode->i_list);
983         inode->i_state|=I_FREEING;
984         inodes_stat.nr_inodes--;
985         spin_unlock(&inode_lock);
986
987         if (inode->i_data.nrpages)
988                 truncate_inode_pages(&inode->i_data, 0);
989
990         security_inode_delete(inode);
991
992         if (op->delete_inode) {
993                 void (*delete)(struct inode *) = op->delete_inode;
994                 if (!is_bad_inode(inode))
995                         DQUOT_INIT(inode);
996                 /* s_op->delete_inode internally recalls clear_inode() */
997                 delete(inode);
998         } else
999                 clear_inode(inode);
1000         spin_lock(&inode_lock);
1001         hlist_del_init(&inode->i_hash);
1002         spin_unlock(&inode_lock);
1003         wake_up_inode(inode);
1004         if (inode->i_state != I_CLEAR)
1005                 BUG();
1006         destroy_inode(inode);
1007 }
1008
1009 EXPORT_SYMBOL(generic_delete_inode);
1010
1011 static void generic_forget_inode(struct inode *inode)
1012 {
1013         struct super_block *sb = inode->i_sb;
1014
1015         if (!hlist_unhashed(&inode->i_hash)) {
1016                 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
1017                         list_move(&inode->i_list, &inode_unused);
1018                 inodes_stat.nr_unused++;
1019                 spin_unlock(&inode_lock);
1020                 if (!sb || (sb->s_flags & MS_ACTIVE))
1021                         return;
1022                 write_inode_now(inode, 1);
1023                 spin_lock(&inode_lock);
1024                 inodes_stat.nr_unused--;
1025                 hlist_del_init(&inode->i_hash);
1026         }
1027         list_del_init(&inode->i_list);
1028         inode->i_state|=I_FREEING;
1029         inodes_stat.nr_inodes--;
1030         spin_unlock(&inode_lock);
1031         if (inode->i_data.nrpages)
1032                 truncate_inode_pages(&inode->i_data, 0);
1033         clear_inode(inode);
1034         destroy_inode(inode);
1035 }
1036
1037 /*
1038  * Normal UNIX filesystem behaviour: delete the
1039  * inode when the usage count drops to zero, and
1040  * i_nlink is zero.
1041  */
1042 static void generic_drop_inode(struct inode *inode)
1043 {
1044         if (!inode->i_nlink)
1045                 generic_delete_inode(inode);
1046         else
1047                 generic_forget_inode(inode);
1048 }
1049
1050 /*
1051  * Called when we're dropping the last reference
1052  * to an inode. 
1053  *
1054  * Call the FS "drop()" function, defaulting to
1055  * the legacy UNIX filesystem behaviour..
1056  *
1057  * NOTE! NOTE! NOTE! We're called with the inode lock
1058  * held, and the drop function is supposed to release
1059  * the lock!
1060  */
1061 static inline void iput_final(struct inode *inode)
1062 {
1063         struct super_operations *op = inode->i_sb->s_op;
1064         void (*drop)(struct inode *) = generic_drop_inode;
1065
1066         if (op && op->drop_inode)
1067                 drop = op->drop_inode;
1068         drop(inode);
1069 }
1070
1071 /**
1072  *      iput    - put an inode 
1073  *      @inode: inode to put
1074  *
1075  *      Puts an inode, dropping its usage count. If the inode use count hits
1076  *      zero the inode is also then freed and may be destroyed.
1077  */
1078 void iput(struct inode *inode)
1079 {
1080         if (inode) {
1081                 struct super_operations *op = inode->i_sb->s_op;
1082
1083                 if (inode->i_state == I_CLEAR)
1084                         BUG();
1085
1086                 if (op && op->put_inode)
1087                         op->put_inode(inode);
1088
1089                 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1090                         iput_final(inode);
1091         }
1092 }
1093
1094 EXPORT_SYMBOL(iput);
1095
1096 /**
1097  *      bmap    - find a block number in a file
1098  *      @inode: inode of file
1099  *      @block: block to find
1100  *
1101  *      Returns the block number on the device holding the inode that
1102  *      is the disk block number for the block of the file requested.
1103  *      That is, asked for block 4 of inode 1 the function will return the
1104  *      disk block relative to the disk start that holds that block of the 
1105  *      file.
1106  */
1107 sector_t bmap(struct inode * inode, sector_t block)
1108 {
1109         sector_t res = 0;
1110         if (inode->i_mapping->a_ops->bmap)
1111                 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1112         return res;
1113 }
1114
1115 EXPORT_SYMBOL(bmap);
1116
1117 /*
1118  * Return true if the filesystem which backs this inode considers the two
1119  * passed timespecs to be sufficiently different to warrant flushing the
1120  * altered time out to disk.
1121  */
1122 static int inode_times_differ(struct inode *inode,
1123                         struct timespec *old, struct timespec *new)
1124 {
1125         if (IS_ONE_SECOND(inode))
1126                 return old->tv_sec != new->tv_sec;
1127         return !timespec_equal(old, new);
1128 }
1129
1130 /**
1131  *      update_atime    -       update the access time
1132  *      @inode: inode accessed
1133  *
1134  *      Update the accessed time on an inode and mark it for writeback.
1135  *      This function automatically handles read only file systems and media,
1136  *      as well as the "noatime" flag and inode specific "noatime" markers.
1137  */
1138 void update_atime(struct inode *inode)
1139 {
1140         struct timespec now;
1141
1142         if (IS_NOATIME(inode))
1143                 return;
1144         if (IS_NODIRATIME(inode) && S_ISDIR(inode->i_mode))
1145                 return;
1146         if (IS_RDONLY(inode))
1147                 return;
1148
1149         now = current_kernel_time();
1150         if (inode_times_differ(inode, &inode->i_atime, &now)) {
1151                 inode->i_atime = now;
1152                 mark_inode_dirty_sync(inode);
1153         } else {
1154                 if (!timespec_equal(&inode->i_atime, &now))
1155                         inode->i_atime = now;
1156         }
1157 }
1158
1159 EXPORT_SYMBOL(update_atime);
1160
1161 /**
1162  *      inode_update_time       -       update mtime and ctime time
1163  *      @inode: inode accessed
1164  *      @ctime_too: update ctime too
1165  *
1166  *      Update the mtime time on an inode and mark it for writeback.
1167  *      When ctime_too is specified update the ctime too.
1168  */
1169
1170 void inode_update_time(struct inode *inode, int ctime_too)
1171 {
1172         struct timespec now;
1173         int sync_it = 0;
1174
1175         if (IS_NOCMTIME(inode))
1176                 return;
1177         if (IS_RDONLY(inode))
1178                 return;
1179
1180         now = current_kernel_time();
1181
1182         if (inode_times_differ(inode, &inode->i_mtime, &now))
1183                 sync_it = 1;
1184         inode->i_mtime = now;
1185
1186         if (ctime_too) {
1187                 if (inode_times_differ(inode, &inode->i_ctime, &now))
1188                         sync_it = 1;
1189                 inode->i_ctime = now;
1190         }
1191         if (sync_it)
1192                 mark_inode_dirty_sync(inode);
1193 }
1194
1195 EXPORT_SYMBOL(inode_update_time);
1196
1197 int inode_needs_sync(struct inode *inode)
1198 {
1199         if (IS_SYNC(inode))
1200                 return 1;
1201         if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1202                 return 1;
1203         return 0;
1204 }
1205
1206 EXPORT_SYMBOL(inode_needs_sync);
1207
1208 /*
1209  *      Quota functions that want to walk the inode lists..
1210  */
1211 #ifdef CONFIG_QUOTA
1212
1213 /* Function back in dquot.c */
1214 int remove_inode_dquot_ref(struct inode *, int, struct list_head *);
1215
1216 void remove_dquot_ref(struct super_block *sb, int type, struct list_head *tofree_head)
1217 {
1218         struct inode *inode;
1219         struct list_head *act_head;
1220
1221         if (!sb->dq_op)
1222                 return; /* nothing to do */
1223         spin_lock(&inode_lock); /* This lock is for inodes code */
1224         /* We don't have to lock against quota code - test IS_QUOTAINIT is just for speedup... */
1225  
1226         list_for_each(act_head, &inode_in_use) {
1227                 inode = list_entry(act_head, struct inode, i_list);
1228                 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1229                         remove_inode_dquot_ref(inode, type, tofree_head);
1230         }
1231         list_for_each(act_head, &inode_unused) {
1232                 inode = list_entry(act_head, struct inode, i_list);
1233                 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1234                         remove_inode_dquot_ref(inode, type, tofree_head);
1235         }
1236         list_for_each(act_head, &sb->s_dirty) {
1237                 inode = list_entry(act_head, struct inode, i_list);
1238                 if (IS_QUOTAINIT(inode))
1239                         remove_inode_dquot_ref(inode, type, tofree_head);
1240         }
1241         list_for_each(act_head, &sb->s_io) {
1242                 inode = list_entry(act_head, struct inode, i_list);
1243                 if (IS_QUOTAINIT(inode))
1244                         remove_inode_dquot_ref(inode, type, tofree_head);
1245         }
1246         spin_unlock(&inode_lock);
1247 }
1248
1249 #endif
1250
1251 /*
1252  * Hashed waitqueues for wait_on_inode().  The table is pretty small - the
1253  * kernel doesn't lock many inodes at the same time.
1254  */
1255 #define I_WAIT_TABLE_ORDER      3
1256 static struct i_wait_queue_head {
1257         wait_queue_head_t wqh;
1258 } ____cacheline_aligned_in_smp i_wait_queue_heads[1<<I_WAIT_TABLE_ORDER];
1259
1260 /*
1261  * Return the address of the waitqueue_head to be used for this inode
1262  */
1263 static wait_queue_head_t *i_waitq_head(struct inode *inode)
1264 {
1265         return &i_wait_queue_heads[hash_ptr(inode, I_WAIT_TABLE_ORDER)].wqh;
1266 }
1267
1268 void __wait_on_inode(struct inode *inode)
1269 {
1270         DECLARE_WAITQUEUE(wait, current);
1271         wait_queue_head_t *wq = i_waitq_head(inode);
1272
1273         add_wait_queue(wq, &wait);
1274 repeat:
1275         set_current_state(TASK_UNINTERRUPTIBLE);
1276         if (inode->i_state & I_LOCK) {
1277                 schedule();
1278                 goto repeat;
1279         }
1280         remove_wait_queue(wq, &wait);
1281         __set_current_state(TASK_RUNNING);
1282 }
1283
1284 /*
1285  * If we try to find an inode in the inode hash while it is being deleted, we
1286  * have to wait until the filesystem completes its deletion before reporting
1287  * that it isn't found.  This is because iget will immediately call
1288  * ->read_inode, and we want to be sure that evidence of the deletion is found
1289  * by ->read_inode.
1290  *
1291  * This call might return early if an inode which shares the waitq is woken up.
1292  * This is most easily handled by the caller which will loop around again
1293  * looking for the inode.
1294  *
1295  * This is called with inode_lock held.
1296  */
1297 static void __wait_on_freeing_inode(struct inode *inode)
1298 {
1299         DECLARE_WAITQUEUE(wait, current);
1300         wait_queue_head_t *wq = i_waitq_head(inode);
1301
1302         add_wait_queue(wq, &wait);
1303         set_current_state(TASK_UNINTERRUPTIBLE);
1304         spin_unlock(&inode_lock);
1305         schedule();
1306         remove_wait_queue(wq, &wait);
1307         spin_lock(&inode_lock);
1308 }
1309
1310 void wake_up_inode(struct inode *inode)
1311 {
1312         wait_queue_head_t *wq = i_waitq_head(inode);
1313
1314         /*
1315          * Prevent speculative execution through spin_unlock(&inode_lock);
1316          */
1317         smp_mb();
1318         if (waitqueue_active(wq))
1319                 wake_up_all(wq);
1320 }
1321
1322 static __initdata unsigned long ihash_entries;
1323 static int __init set_ihash_entries(char *str)
1324 {
1325         if (!str)
1326                 return 0;
1327         ihash_entries = simple_strtoul(str, &str, 0);
1328         return 1;
1329 }
1330 __setup("ihash_entries=", set_ihash_entries);
1331
1332 /*
1333  * Initialize the waitqueues and inode hash table.
1334  */
1335 void __init inode_init(unsigned long mempages)
1336 {
1337         struct hlist_head *head;
1338         unsigned long order;
1339         unsigned int nr_hash;
1340         int i;
1341
1342         for (i = 0; i < ARRAY_SIZE(i_wait_queue_heads); i++)
1343                 init_waitqueue_head(&i_wait_queue_heads[i].wqh);
1344
1345         if (!ihash_entries)
1346                 ihash_entries = PAGE_SHIFT < 14 ?
1347                                 mempages >> (14 - PAGE_SHIFT) :
1348                                 mempages << (PAGE_SHIFT - 14);
1349
1350         ihash_entries *= sizeof(struct hlist_head);
1351         for (order = 0; ((1UL << order) << PAGE_SHIFT) < ihash_entries; order++)
1352                 ;
1353
1354         do {
1355                 unsigned long tmp;
1356
1357                 nr_hash = (1UL << order) * PAGE_SIZE /
1358                         sizeof(struct hlist_head);
1359                 i_hash_mask = (nr_hash - 1);
1360
1361                 tmp = nr_hash;
1362                 i_hash_shift = 0;
1363                 while ((tmp >>= 1UL) != 0UL)
1364                         i_hash_shift++;
1365
1366                 inode_hashtable = (struct hlist_head *)
1367                         __get_free_pages(GFP_ATOMIC, order);
1368         } while (inode_hashtable == NULL && --order >= 0);
1369
1370         printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1371                         nr_hash, order, (PAGE_SIZE << order));
1372
1373         if (!inode_hashtable)
1374                 panic("Failed to allocate inode hash table\n");
1375
1376         head = inode_hashtable;
1377         i = nr_hash;
1378         do {
1379                 INIT_HLIST_HEAD(head);
1380                 head++;
1381                 i--;
1382         } while (i);
1383
1384         /* inode slab cache */
1385         inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
1386                                          0, SLAB_HWCACHE_ALIGN, init_once,
1387                                          NULL);
1388         if (!inode_cachep)
1389                 panic("cannot create inode slab cache");
1390
1391         set_shrinker(DEFAULT_SEEKS, shrink_icache_memory);
1392 }
1393
1394 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1395 {
1396         inode->i_mode = mode;
1397         if (S_ISCHR(mode)) {
1398                 inode->i_fop = &def_chr_fops;
1399                 inode->i_rdev = rdev;
1400         } else if (S_ISBLK(mode)) {
1401                 inode->i_fop = &def_blk_fops;
1402                 inode->i_rdev = rdev;
1403         } else if (S_ISFIFO(mode))
1404                 inode->i_fop = &def_fifo_fops;
1405         else if (S_ISSOCK(mode))
1406                 inode->i_fop = &bad_sock_fops;
1407         else
1408                 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1409                        mode);
1410 }
1411
1412 EXPORT_SYMBOL(init_special_inode);