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