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