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