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