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