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