Import changeset
[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/string.h>
10 #include <linux/mm.h>
11 #include <linux/dcache.h>
12 #include <linux/init.h>
13 #include <linux/quotaops.h>
14 #include <linux/slab.h>
15 #include <linux/cache.h>
16
17 /*
18  * New inode.c implementation.
19  *
20  * This implementation has the basic premise of trying
21  * to be extremely low-overhead and SMP-safe, yet be
22  * simple enough to be "obviously correct".
23  *
24  * Famous last words.
25  */
26
27 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
28
29 /* #define INODE_PARANOIA 1 */
30 /* #define INODE_DEBUG 1 */
31
32 /*
33  * Inode lookup is no longer as critical as it used to be:
34  * most of the lookups are going to be through the dcache.
35  */
36 #define I_HASHBITS      i_hash_shift
37 #define I_HASHMASK      i_hash_mask
38
39 static unsigned int i_hash_mask;
40 static unsigned int i_hash_shift;
41
42 /*
43  * Each inode can be on two separate lists. One is
44  * the hash list of the inode, used for lookups. The
45  * other linked list is the "type" list:
46  *  "in_use" - valid inode, i_count > 0, i_nlink > 0
47  *  "dirty"  - as "in_use" but also dirty
48  *  "unused" - valid inode, i_count = 0
49  *
50  * A "dirty" list is maintained for each super block,
51  * allowing for low-overhead inode sync() operations.
52  */
53
54 static LIST_HEAD(inode_in_use);
55 static LIST_HEAD(inode_unused);
56 static struct list_head *inode_hashtable;
57 static LIST_HEAD(anon_hash_chain); /* for inodes with NULL i_sb */
58
59 /*
60  * A simple spinlock to protect the list manipulations.
61  *
62  * NOTE! You also have to own the lock if you change
63  * the i_state of an inode while it is in use..
64  */
65 spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
66
67 /*
68  * Statistics gathering..
69  */
70 struct {
71         int nr_inodes;
72         int nr_unused;
73         int dummy[5];
74 } inodes_stat;
75
76 static kmem_cache_t * inode_cachep;
77
78 #define alloc_inode() \
79          ((struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL))
80 static void destroy_inode(struct inode *inode) 
81 {
82         if (!list_empty(&inode->i_dirty_buffers))
83                 BUG();
84         kmem_cache_free(inode_cachep, (inode));
85 }
86
87
88 /*
89  * These are initializations that only need to be done
90  * once, because the fields are idempotent across use
91  * of the inode, so let the slab aware of that.
92  */
93 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
94 {
95         struct inode * inode = (struct inode *) foo;
96
97         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
98             SLAB_CTOR_CONSTRUCTOR)
99         {
100                 memset(inode, 0, sizeof(*inode));
101                 init_waitqueue_head(&inode->i_wait);
102                 INIT_LIST_HEAD(&inode->i_hash);
103                 INIT_LIST_HEAD(&inode->i_data.clean_pages);
104                 INIT_LIST_HEAD(&inode->i_data.dirty_pages);
105                 INIT_LIST_HEAD(&inode->i_data.locked_pages);
106                 INIT_LIST_HEAD(&inode->i_dentry);
107                 INIT_LIST_HEAD(&inode->i_dirty_buffers);
108                 sema_init(&inode->i_sem, 1);
109                 sema_init(&inode->i_zombie, 1);
110                 spin_lock_init(&inode->i_data.i_shared_lock);
111         }
112 }
113
114 /*
115  * Put the inode on the super block's dirty list.
116  *
117  * CAREFUL! We mark it dirty unconditionally, but
118  * move it onto the dirty list only if it is hashed.
119  * If it was not hashed, it will never be added to
120  * the dirty list even if it is later hashed, as it
121  * will have been marked dirty already.
122  *
123  * In short, make sure you hash any inodes _before_
124  * you start marking them dirty..
125  */
126  
127 /**
128  *      __mark_inode_dirty -    internal function
129  *      @inode: inode to mark
130  *
131  *      Mark an inode as dirty. Callers should use mark_inode_dirty.
132  */
133  
134 void __mark_inode_dirty(struct inode *inode, int flags)
135 {
136         struct super_block * sb = inode->i_sb;
137
138         if (sb) {
139                 spin_lock(&inode_lock);
140                 if ((inode->i_state & flags) != flags) {
141                         inode->i_state |= flags;
142                         /* Only add valid (ie hashed) inodes to the dirty list */
143                         if (!list_empty(&inode->i_hash)) {
144                                 list_del(&inode->i_list);
145                                 list_add(&inode->i_list, &sb->s_dirty);
146                         }
147                 }
148                 spin_unlock(&inode_lock);
149         }
150 }
151
152 static void __wait_on_inode(struct inode * inode)
153 {
154         DECLARE_WAITQUEUE(wait, current);
155
156         add_wait_queue(&inode->i_wait, &wait);
157 repeat:
158         set_current_state(TASK_UNINTERRUPTIBLE);
159         if (inode->i_state & I_LOCK) {
160                 schedule();
161                 goto repeat;
162         }
163         remove_wait_queue(&inode->i_wait, &wait);
164         current->state = TASK_RUNNING;
165 }
166
167 static inline void wait_on_inode(struct inode *inode)
168 {
169         if (inode->i_state & I_LOCK)
170                 __wait_on_inode(inode);
171 }
172
173
174 static inline void write_inode(struct inode *inode, int sync)
175 {
176         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->write_inode)
177                 inode->i_sb->s_op->write_inode(inode, sync);
178 }
179
180 static inline void __iget(struct inode * inode)
181 {
182         if (atomic_read(&inode->i_count)) {
183                 atomic_inc(&inode->i_count);
184                 return;
185         }
186         atomic_inc(&inode->i_count);
187         if (!(inode->i_state & I_DIRTY)) {
188                 list_del(&inode->i_list);
189                 list_add(&inode->i_list, &inode_in_use);
190         }
191         inodes_stat.nr_unused--;
192 }
193
194 static inline void sync_one(struct inode *inode, int sync)
195 {
196         if (inode->i_state & I_LOCK) {
197                 __iget(inode);
198                 spin_unlock(&inode_lock);
199                 __wait_on_inode(inode);
200                 iput(inode);
201                 spin_lock(&inode_lock);
202         } else {
203                 unsigned dirty;
204
205                 list_del(&inode->i_list);
206                 list_add(&inode->i_list, atomic_read(&inode->i_count)
207                                                         ? &inode_in_use
208                                                         : &inode_unused);
209                 /* Set I_LOCK, reset I_DIRTY */
210                 dirty = inode->i_state & I_DIRTY;
211                 inode->i_state |= I_LOCK;
212                 inode->i_state &= ~I_DIRTY;
213                 spin_unlock(&inode_lock);
214
215                 filemap_fdatasync(inode->i_mapping);
216
217                 /* Don't write the inode if only I_DIRTY_PAGES was set */
218                 if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))
219                         write_inode(inode, sync);
220
221                 filemap_fdatawait(inode->i_mapping);
222
223                 spin_lock(&inode_lock);
224                 inode->i_state &= ~I_LOCK;
225                 wake_up(&inode->i_wait);
226         }
227 }
228
229 static inline void sync_list(struct list_head *head)
230 {
231         struct list_head * tmp;
232
233         while ((tmp = head->prev) != head)
234                 sync_one(list_entry(tmp, struct inode, i_list), 0);
235 }
236
237 /**
238  *      sync_inodes
239  *      @dev: device to sync the inodes from.
240  *
241  *      sync_inodes goes through the super block's dirty list, 
242  *      writes them out, and puts them back on the normal list.
243  */
244  
245 void sync_inodes(kdev_t dev)
246 {
247         struct super_block * sb = sb_entry(super_blocks.next);
248
249         /*
250          * Search the super_blocks array for the device(s) to sync.
251          */
252         spin_lock(&inode_lock);
253         for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.next)) {
254                 if (!sb->s_dev)
255                         continue;
256                 if (dev && sb->s_dev != dev)
257                         continue;
258
259                 sync_list(&sb->s_dirty);
260
261                 if (dev)
262                         break;
263         }
264         spin_unlock(&inode_lock);
265 }
266
267 /*
268  * Called with the spinlock already held..
269  */
270 static void sync_all_inodes(void)
271 {
272         struct super_block * sb = sb_entry(super_blocks.next);
273         for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.next)) {
274                 if (!sb->s_dev)
275                         continue;
276                 sync_list(&sb->s_dirty);
277         }
278 }
279
280 /**
281  *      write_inode_now -       write an inode to disk
282  *      @inode: inode to write to disk
283  *      @sync: whether the write should be synchronous or not
284  *
285  *      This function commits an inode to disk immediately if it is
286  *      dirty. This is primarily needed by knfsd.
287  */
288  
289 void write_inode_now(struct inode *inode, int sync)
290 {
291         struct super_block * sb = inode->i_sb;
292
293         if (sb) {
294                 spin_lock(&inode_lock);
295                 while (inode->i_state & I_DIRTY)
296                         sync_one(inode, sync);
297                 spin_unlock(&inode_lock);
298         }
299         else
300                 printk("write_inode_now: no super block\n");
301 }
302
303 /**
304  * generic_osync_inode - flush all dirty data for a given inode to disk
305  * @inode: inode to write
306  * @datasync: if set, don't bother flushing timestamps
307  *
308  * This can be called by file_write functions for files which have the
309  * O_SYNC flag set, to flush dirty writes to disk.  
310  */
311
312 int generic_osync_inode(struct inode *inode, int datasync)
313 {
314         int err;
315         
316         /* 
317          * WARNING
318          *
319          * Currently, the filesystem write path does not pass the
320          * filp down to the low-level write functions.  Therefore it
321          * is impossible for (say) __block_commit_write to know if
322          * the operation is O_SYNC or not.
323          *
324          * Ideally, O_SYNC writes would have the filesystem call
325          * ll_rw_block as it went to kick-start the writes, and we
326          * could call osync_inode_buffers() here to wait only for
327          * those IOs which have already been submitted to the device
328          * driver layer.  As it stands, if we did this we'd not write
329          * anything to disk since our writes have not been queued by
330          * this point: they are still on the dirty LRU.
331          * 
332          * So, currently we will call fsync_inode_buffers() instead,
333          * to flush _all_ dirty buffers for this inode to disk on 
334          * every O_SYNC write, not just the synchronous I/Os.  --sct
335          */
336
337 #ifdef WRITERS_QUEUE_IO
338         err = osync_inode_buffers(inode);
339 #else
340         err = fsync_inode_buffers(inode);
341 #endif
342
343         spin_lock(&inode_lock);
344         if (!(inode->i_state & I_DIRTY))
345                 goto out;
346         if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
347                 goto out;
348         spin_unlock(&inode_lock);
349         write_inode_now(inode, 1);
350         return err;
351
352  out:
353         spin_unlock(&inode_lock);
354         return err;
355 }
356
357 /**
358  * clear_inode - clear an inode
359  * @inode: inode to clear
360  *
361  * This is called by the filesystem to tell us
362  * that the inode is no longer useful. We just
363  * terminate it with extreme prejudice.
364  */
365  
366 void clear_inode(struct inode *inode)
367 {
368         if (!list_empty(&inode->i_dirty_buffers))
369                 invalidate_inode_buffers(inode);
370        
371         if (inode->i_data.nrpages)
372                 BUG();
373         if (!(inode->i_state & I_FREEING))
374                 BUG();
375         if (inode->i_state & I_CLEAR)
376                 BUG();
377         wait_on_inode(inode);
378         if (IS_QUOTAINIT(inode))
379                 DQUOT_DROP(inode);
380         if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->clear_inode)
381                 inode->i_sb->s_op->clear_inode(inode);
382         if (inode->i_bdev) {
383                 bdput(inode->i_bdev);
384                 inode->i_bdev = NULL;
385         }
386         inode->i_state = I_CLEAR;
387 }
388
389 /*
390  * Dispose-list gets a local list with local inodes in it, so it doesn't
391  * need to worry about list corruption and SMP locks.
392  */
393 static void dispose_list(struct list_head * head)
394 {
395         struct list_head * inode_entry;
396         struct inode * inode;
397
398         while ((inode_entry = head->next) != head)
399         {
400                 list_del(inode_entry);
401
402                 inode = list_entry(inode_entry, struct inode, i_list);
403                 if (inode->i_data.nrpages)
404                         truncate_inode_pages(&inode->i_data, 0);
405                 clear_inode(inode);
406                 destroy_inode(inode);
407                 inodes_stat.nr_inodes--;
408         }
409 }
410
411 /*
412  * Invalidate all inodes for a device.
413  */
414 static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
415 {
416         struct list_head *next;
417         int busy = 0, count = 0;
418
419         next = head->next;
420         for (;;) {
421                 struct list_head * tmp = next;
422                 struct inode * inode;
423
424                 next = next->next;
425                 if (tmp == head)
426                         break;
427                 inode = list_entry(tmp, struct inode, i_list);
428                 if (inode->i_sb != sb)
429                         continue;
430                 invalidate_inode_buffers(inode);
431                 if (!atomic_read(&inode->i_count)) {
432                         list_del(&inode->i_hash);
433                         INIT_LIST_HEAD(&inode->i_hash);
434                         list_del(&inode->i_list);
435                         list_add(&inode->i_list, dispose);
436                         inode->i_state |= I_FREEING;
437                         count++;
438                         continue;
439                 }
440                 busy = 1;
441         }
442         /* only unused inodes may be cached with i_count zero */
443         inodes_stat.nr_unused -= count;
444         return busy;
445 }
446
447 /*
448  * This is a two-stage process. First we collect all
449  * offending inodes onto the throw-away list, and in
450  * the second stage we actually dispose of them. This
451  * is because we don't want to sleep while messing
452  * with the global lists..
453  */
454  
455 /**
456  *      invalidate_inodes       - discard the inodes on a device
457  *      @sb: superblock
458  *
459  *      Discard all of the inodes for a given superblock. If the discard
460  *      fails because there are busy inodes then a non zero value is returned.
461  *      If the discard is successful all the inodes have been discarded.
462  */
463  
464 int invalidate_inodes(struct super_block * sb)
465 {
466         int busy;
467         LIST_HEAD(throw_away);
468
469         spin_lock(&inode_lock);
470         busy = invalidate_list(&inode_in_use, sb, &throw_away);
471         busy |= invalidate_list(&inode_unused, sb, &throw_away);
472         busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
473         spin_unlock(&inode_lock);
474
475         dispose_list(&throw_away);
476
477         return busy;
478 }
479
480 /*
481  * This is called with the inode lock held. It searches
482  * the in-use for freeable inodes, which are moved to a
483  * temporary list and then placed on the unused list by
484  * dispose_list. 
485  *
486  * We don't expect to have to call this very often.
487  *
488  * N.B. The spinlock is released during the call to
489  *      dispose_list.
490  */
491 #define CAN_UNUSE(inode) \
492         ((((inode)->i_state | (inode)->i_data.nrpages) == 0)  && \
493          !inode_has_buffers(inode))
494 #define INODE(entry)    (list_entry(entry, struct inode, i_list))
495
496 void prune_icache(int goal)
497 {
498         LIST_HEAD(list);
499         struct list_head *entry, *freeable = &list;
500         int count = 0;
501         struct inode * inode;
502
503         spin_lock(&inode_lock);
504         /* go simple and safe syncing everything before starting */
505         sync_all_inodes();
506
507         entry = inode_unused.prev;
508         while (entry != &inode_unused)
509         {
510                 struct list_head *tmp = entry;
511
512                 entry = entry->prev;
513                 inode = INODE(tmp);
514                 if (inode->i_state & (I_FREEING|I_CLEAR))
515                         BUG();
516                 if (!CAN_UNUSE(inode))
517                         continue;
518                 if (atomic_read(&inode->i_count))
519                         BUG();
520                 list_del(tmp);
521                 list_del(&inode->i_hash);
522                 INIT_LIST_HEAD(&inode->i_hash);
523                 list_add(tmp, freeable);
524                 inode->i_state |= I_FREEING;
525                 count++;
526                 if (!--goal)
527                         break;
528         }
529         inodes_stat.nr_unused -= count;
530         spin_unlock(&inode_lock);
531
532         dispose_list(freeable);
533 }
534
535 void shrink_icache_memory(int priority, int gfp_mask)
536 {
537         int count = 0;
538
539         /*
540          * Nasty deadlock avoidance..
541          *
542          * We may hold various FS locks, and we don't
543          * want to recurse into the FS that called us
544          * in clear_inode() and friends..
545          */
546         if (!(gfp_mask & __GFP_IO))
547                 return;
548
549         if (priority)
550                 count = inodes_stat.nr_unused / priority;
551
552         prune_icache(count);
553         kmem_cache_shrink(inode_cachep);
554 }
555
556 /*
557  * Called with the inode lock held.
558  * NOTE: we are not increasing the inode-refcount, you must call __iget()
559  * by hand after calling find_inode now! This simplifies iunique and won't
560  * add any additional branch in the common code.
561  */
562 static struct inode * find_inode(struct super_block * sb, unsigned long ino, struct list_head *head, find_inode_t find_actor, void *opaque)
563 {
564         struct list_head *tmp;
565         struct inode * inode;
566
567         tmp = head;
568         for (;;) {
569                 tmp = tmp->next;
570                 inode = NULL;
571                 if (tmp == head)
572                         break;
573                 inode = list_entry(tmp, struct inode, i_hash);
574                 if (inode->i_ino != ino)
575                         continue;
576                 if (inode->i_sb != sb)
577                         continue;
578                 if (find_actor && !find_actor(inode, ino, opaque))
579                         continue;
580                 break;
581         }
582         return inode;
583 }
584
585 /*
586  * This just initializes the inode fields
587  * to known values before returning the inode..
588  *
589  * i_sb, i_ino, i_count, i_state and the lists have
590  * been initialized elsewhere..
591  */
592 static void clean_inode(struct inode *inode)
593 {
594         static struct address_space_operations empty_aops;
595         static struct inode_operations empty_iops;
596         static struct file_operations empty_fops;
597         memset(&inode->u, 0, sizeof(inode->u));
598         inode->i_sock = 0;
599         inode->i_op = &empty_iops;
600         inode->i_fop = &empty_fops;
601         inode->i_nlink = 1;
602         atomic_set(&inode->i_writecount, 0);
603         inode->i_size = 0;
604         inode->i_generation = 0;
605         memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
606         inode->i_pipe = NULL;
607         inode->i_bdev = NULL;
608         inode->i_data.a_ops = &empty_aops;
609         inode->i_data.host = inode;
610         inode->i_mapping = &inode->i_data;
611 }
612
613 /**
614  * get_empty_inode      - obtain an inode
615  *
616  * This is called by things like the networking layer
617  * etc that want to get an inode without any inode
618  * number, or filesystems that allocate new inodes with
619  * no pre-existing information.
620  *
621  * On a successful return the inode pointer is returned. On a failure
622  * a %NULL pointer is returned. The returned inode is not on any superblock
623  * lists.
624  */
625  
626 struct inode * get_empty_inode(void)
627 {
628         static unsigned long last_ino;
629         struct inode * inode;
630
631         inode = alloc_inode();
632         if (inode)
633         {
634                 spin_lock(&inode_lock);
635                 inodes_stat.nr_inodes++;
636                 list_add(&inode->i_list, &inode_in_use);
637                 inode->i_sb = NULL;
638                 inode->i_dev = 0;
639                 inode->i_ino = ++last_ino;
640                 inode->i_flags = 0;
641                 atomic_set(&inode->i_count, 1);
642                 inode->i_state = 0;
643                 spin_unlock(&inode_lock);
644                 clean_inode(inode);
645         }
646         return inode;
647 }
648
649 /*
650  * This is called without the inode lock held.. Be careful.
651  *
652  * We no longer cache the sb_flags in i_flags - see fs.h
653  *      -- rmk@arm.uk.linux.org
654  */
655 static struct inode * get_new_inode(struct super_block *sb, unsigned long ino, struct list_head *head, find_inode_t find_actor, void *opaque)
656 {
657         struct inode * inode;
658
659         inode = alloc_inode();
660         if (inode) {
661                 struct inode * old;
662
663                 spin_lock(&inode_lock);
664                 /* We released the lock, so.. */
665                 old = find_inode(sb, ino, head, find_actor, opaque);
666                 if (!old) {
667                         inodes_stat.nr_inodes++;
668                         list_add(&inode->i_list, &inode_in_use);
669                         list_add(&inode->i_hash, head);
670                         inode->i_sb = sb;
671                         inode->i_dev = sb->s_dev;
672                         inode->i_ino = ino;
673                         inode->i_flags = 0;
674                         atomic_set(&inode->i_count, 1);
675                         inode->i_state = I_LOCK;
676                         spin_unlock(&inode_lock);
677
678                         clean_inode(inode);
679                         sb->s_op->read_inode(inode);
680
681                         /*
682                          * This is special!  We do not need the spinlock
683                          * when clearing I_LOCK, because we're guaranteed
684                          * that nobody else tries to do anything about the
685                          * state of the inode when it is locked, as we
686                          * just created it (so there can be no old holders
687                          * that haven't tested I_LOCK).
688                          */
689                         inode->i_state &= ~I_LOCK;
690                         wake_up(&inode->i_wait);
691
692                         return inode;
693                 }
694
695                 /*
696                  * Uhhuh, somebody else created the same inode under
697                  * us. Use the old inode instead of the one we just
698                  * allocated.
699                  */
700                 __iget(old);
701                 spin_unlock(&inode_lock);
702                 destroy_inode(inode);
703                 inode = old;
704                 wait_on_inode(inode);
705         }
706         return inode;
707 }
708
709 static inline unsigned long hash(struct super_block *sb, unsigned long i_ino)
710 {
711         unsigned long tmp = i_ino | ((unsigned long) sb / L1_CACHE_BYTES);
712         tmp = tmp + (tmp >> I_HASHBITS) + (tmp >> I_HASHBITS*2);
713         return tmp & I_HASHMASK;
714 }
715
716 /* Yeah, I know about quadratic hash. Maybe, later. */
717
718 /**
719  *      iunique - get a unique inode number
720  *      @sb: superblock
721  *      @max_reserved: highest reserved inode number
722  *
723  *      Obtain an inode number that is unique on the system for a given
724  *      superblock. This is used by file systems that have no natural
725  *      permanent inode numbering system. An inode number is returned that
726  *      is higher than the reserved limit but unique.
727  *
728  *      BUGS:
729  *      With a large number of inodes live on the file system this function
730  *      currently becomes quite slow.
731  */
732  
733 ino_t iunique(struct super_block *sb, ino_t max_reserved)
734 {
735         static ino_t counter = 0;
736         struct inode *inode;
737         struct list_head * head;
738         ino_t res;
739         spin_lock(&inode_lock);
740 retry:
741         if (counter > max_reserved) {
742                 head = inode_hashtable + hash(sb,counter);
743                 inode = find_inode(sb, res = counter++, head, NULL, NULL);
744                 if (!inode) {
745                         spin_unlock(&inode_lock);
746                         return res;
747                 }
748         } else {
749                 counter = max_reserved + 1;
750         }
751         goto retry;
752         
753 }
754
755 struct inode *igrab(struct inode *inode)
756 {
757         spin_lock(&inode_lock);
758         if (!(inode->i_state & I_FREEING))
759                 __iget(inode);
760         else
761                 /*
762                  * Handle the case where s_op->clear_inode is not been
763                  * called yet, and somebody is calling igrab
764                  * while the inode is getting freed.
765                  */
766                 inode = NULL;
767         spin_unlock(&inode_lock);
768         if (inode)
769                 wait_on_inode(inode);
770         return inode;
771 }
772
773
774 struct inode *iget4(struct super_block *sb, unsigned long ino, find_inode_t find_actor, void *opaque)
775 {
776         struct list_head * head = inode_hashtable + hash(sb,ino);
777         struct inode * inode;
778
779         spin_lock(&inode_lock);
780         inode = find_inode(sb, ino, head, find_actor, opaque);
781         if (inode) {
782                 __iget(inode);
783                 spin_unlock(&inode_lock);
784                 wait_on_inode(inode);
785                 return inode;
786         }
787         spin_unlock(&inode_lock);
788
789         /*
790          * get_new_inode() will do the right thing, re-trying the search
791          * in case it had to block at any point.
792          */
793         return get_new_inode(sb, ino, head, find_actor, opaque);
794 }
795
796 /**
797  *      insert_inode_hash - hash an inode
798  *      @inode: unhashed inode
799  *
800  *      Add an inode to the inode hash for this superblock. If the inode
801  *      has no superblock it is added to a separate anonymous chain.
802  */
803  
804 void insert_inode_hash(struct inode *inode)
805 {
806         struct list_head *head = &anon_hash_chain;
807         if (inode->i_sb)
808                 head = inode_hashtable + hash(inode->i_sb, inode->i_ino);
809         spin_lock(&inode_lock);
810         list_add(&inode->i_hash, head);
811         spin_unlock(&inode_lock);
812 }
813
814 /**
815  *      remove_inode_hash - remove an inode from the hash
816  *      @inode: inode to unhash
817  *
818  *      Remove an inode from the superblock or anonymous hash.
819  */
820  
821 void remove_inode_hash(struct inode *inode)
822 {
823         spin_lock(&inode_lock);
824         list_del(&inode->i_hash);
825         INIT_LIST_HEAD(&inode->i_hash);
826         spin_unlock(&inode_lock);
827 }
828
829 /**
830  *      iput    - put an inode 
831  *      @inode: inode to put
832  *
833  *      Puts an inode, dropping its usage count. If the inode use count hits
834  *      zero the inode is also then freed and may be destroyed.
835  */
836  
837 void iput(struct inode *inode)
838 {
839         if (inode) {
840                 struct super_operations *op = NULL;
841
842                 if (inode->i_sb && inode->i_sb->s_op)
843                         op = inode->i_sb->s_op;
844                 if (op && op->put_inode)
845                         op->put_inode(inode);
846
847                 if (!atomic_dec_and_lock(&inode->i_count, &inode_lock))
848                         return;
849
850                 if (!inode->i_nlink) {
851                         list_del(&inode->i_hash);
852                         INIT_LIST_HEAD(&inode->i_hash);
853                         list_del(&inode->i_list);
854                         INIT_LIST_HEAD(&inode->i_list);
855                         inode->i_state|=I_FREEING;
856                         inodes_stat.nr_inodes--;
857                         spin_unlock(&inode_lock);
858
859                         if (inode->i_data.nrpages)
860                                 truncate_inode_pages(&inode->i_data, 0);
861
862                         if (op && op->delete_inode) {
863                                 void (*delete)(struct inode *) = op->delete_inode;
864                                 /* s_op->delete_inode internally recalls clear_inode() */
865                                 delete(inode);
866                         } else
867                                 clear_inode(inode);
868                         if (inode->i_state != I_CLEAR)
869                                 BUG();
870                 } else {
871                         if (!list_empty(&inode->i_hash)) {
872                                 if (!(inode->i_state & I_DIRTY)) {
873                                         list_del(&inode->i_list);
874                                         list_add(&inode->i_list,
875                                                  &inode_unused);
876                                 }
877                                 inodes_stat.nr_unused++;
878                                 spin_unlock(&inode_lock);
879                                 return;
880                         } else {
881                                 /* magic nfs path */
882                                 list_del(&inode->i_list);
883                                 INIT_LIST_HEAD(&inode->i_list);
884                                 inode->i_state|=I_FREEING;
885                                 inodes_stat.nr_inodes--;
886                                 spin_unlock(&inode_lock);
887                                 clear_inode(inode);
888                         }
889                 }
890                 destroy_inode(inode);
891         }
892 }
893
894 void force_delete(struct inode *inode)
895 {
896         /*
897          * Kill off unused inodes ... iput() will unhash and
898          * delete the inode if we set i_nlink to zero.
899          */
900         if (atomic_read(&inode->i_count) == 1)
901                 inode->i_nlink = 0;
902 }
903
904 /**
905  *      bmap    - find a block number in a file
906  *      @inode: inode of file
907  *      @block: block to find
908  *
909  *      Returns the block number on the device holding the inode that
910  *      is the disk block number for the block of the file requested.
911  *      That is, asked for block 4 of inode 1 the function will return the
912  *      disk block relative to the disk start that holds that block of the 
913  *      file.
914  */
915  
916 int bmap(struct inode * inode, int block)
917 {
918         int res = 0;
919         if (inode->i_mapping->a_ops->bmap)
920                 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
921         return res;
922 }
923
924 /*
925  * Initialize the hash tables.
926  */
927 void __init inode_init(unsigned long mempages)
928 {
929         struct list_head *head;
930         unsigned long order;
931         unsigned int nr_hash;
932         int i;
933
934         mempages >>= (14 - PAGE_SHIFT);
935         mempages *= sizeof(struct list_head);
936         for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
937                 ;
938
939         do {
940                 unsigned long tmp;
941
942                 nr_hash = (1UL << order) * PAGE_SIZE /
943                         sizeof(struct list_head);
944                 i_hash_mask = (nr_hash - 1);
945
946                 tmp = nr_hash;
947                 i_hash_shift = 0;
948                 while ((tmp >>= 1UL) != 0UL)
949                         i_hash_shift++;
950
951                 inode_hashtable = (struct list_head *)
952                         __get_free_pages(GFP_ATOMIC, order);
953         } while (inode_hashtable == NULL && --order >= 0);
954
955         printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
956                         nr_hash, order, (PAGE_SIZE << order));
957
958         if (!inode_hashtable)
959                 panic("Failed to allocate inode hash table\n");
960
961         head = inode_hashtable;
962         i = nr_hash;
963         do {
964                 INIT_LIST_HEAD(head);
965                 head++;
966                 i--;
967         } while (i);
968
969         /* inode slab cache */
970         inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
971                                          0, SLAB_HWCACHE_ALIGN, init_once,
972                                          NULL);
973         if (!inode_cachep)
974                 panic("cannot create inode slab cache");
975 }
976
977 /**
978  *      update_atime    -       update the access time
979  *      @inode: inode accessed
980  *
981  *      Update the accessed time on an inode and mark it for writeback.
982  *      This function automatically handles read only file systems and media,
983  *      as well as the "noatime" flag and inode specific "noatime" markers.
984  */
985  
986 void update_atime (struct inode *inode)
987 {
988         if ( IS_NOATIME (inode) ) return;
989         if ( IS_NODIRATIME (inode) && S_ISDIR (inode->i_mode) ) return;
990         if ( IS_RDONLY (inode) ) return;
991         inode->i_atime = CURRENT_TIME;
992         mark_inode_dirty_sync (inode);
993 }   /*  End Function update_atime  */
994
995
996 /*
997  *      Quota functions that want to walk the inode lists..
998  */
999 #ifdef CONFIG_QUOTA
1000
1001 /* Functions back in dquot.c */
1002 void put_dquot_list(struct list_head *);
1003 int remove_inode_dquot_ref(struct inode *, short, struct list_head *);
1004
1005 void remove_dquot_ref(kdev_t dev, short type)
1006 {
1007         struct super_block *sb = get_super(dev);
1008         struct inode *inode;
1009         struct list_head *act_head;
1010         LIST_HEAD(tofree_head);
1011
1012         if (!sb || !sb->dq_op)
1013                 return; /* nothing to do */
1014
1015         /* We have to be protected against other CPUs */
1016         spin_lock(&inode_lock);
1017  
1018         for (act_head = inode_in_use.next; act_head != &inode_in_use; act_head = act_head->next) {
1019                 inode = list_entry(act_head, struct inode, i_list);
1020                 if (inode->i_sb != sb || !IS_QUOTAINIT(inode))
1021                         continue;
1022                 remove_inode_dquot_ref(inode, type, &tofree_head);
1023         }
1024         for (act_head = inode_unused.next; act_head != &inode_unused; act_head = act_head->next) {
1025                 inode = list_entry(act_head, struct inode, i_list);
1026                 if (inode->i_sb != sb || !IS_QUOTAINIT(inode))
1027                         continue;
1028                 remove_inode_dquot_ref(inode, type, &tofree_head);
1029         }
1030         for (act_head = sb->s_dirty.next; act_head != &sb->s_dirty; act_head = act_head->next) {
1031                 inode = list_entry(act_head, struct inode, i_list);
1032                 if (!IS_QUOTAINIT(inode))
1033                         continue;
1034                 remove_inode_dquot_ref(inode, type, &tofree_head);
1035         }
1036         spin_unlock(&inode_lock);
1037
1038         put_dquot_list(&tofree_head);
1039 }
1040
1041 #endif