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