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