4 * (C) 1997 Linus Torvalds
7 #include <linux/config.h>
9 #include <linux/string.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>
22 * New inode.c implementation.
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".
31 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
33 /* #define INODE_PARANOIA 1 */
34 /* #define INODE_DEBUG 1 */
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.
40 #define I_HASHBITS i_hash_shift
41 #define I_HASHMASK i_hash_mask
43 static unsigned int i_hash_mask;
44 static unsigned int i_hash_shift;
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, no pages in the pagecache
53 * "unused_pagecache" - valid inode, i_count = 0, data in the pagecache
55 * A "dirty" list is maintained for each super block,
56 * allowing for low-overhead inode sync() operations.
59 static LIST_HEAD(inode_in_use);
60 static LIST_HEAD(inode_unused);
61 static LIST_HEAD(inode_unused_pagecache);
62 static struct list_head *inode_hashtable;
63 static LIST_HEAD(anon_hash_chain); /* for inodes with NULL i_sb */
66 * A simple spinlock to protect the list manipulations.
68 * NOTE! You also have to own the lock if you change
69 * the i_state of an inode while it is in use..
71 static spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
74 * Statistics gathering..
76 struct inodes_stat_t inodes_stat;
78 static kmem_cache_t * inode_cachep;
80 static struct inode *alloc_inode(struct super_block *sb)
82 static struct address_space_operations empty_aops;
83 static struct inode_operations empty_iops;
84 static struct file_operations empty_fops;
87 if (sb->s_op->alloc_inode)
88 inode = sb->s_op->alloc_inode(sb);
90 inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
93 memset(&inode->u, 0, sizeof(inode->u));
97 struct address_space * const mapping = &inode->i_data;
100 inode->i_dev = sb->s_dev;
101 inode->i_blkbits = sb->s_blocksize_bits;
103 atomic_set(&inode->i_count, 1);
105 inode->i_op = &empty_iops;
106 inode->i_fop = &empty_fops;
108 atomic_set(&inode->i_writecount, 0);
112 inode->i_generation = 0;
113 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
114 inode->i_pipe = NULL;
115 inode->i_bdev = NULL;
116 inode->i_cdev = NULL;
118 mapping->a_ops = &empty_aops;
119 mapping->host = inode;
120 mapping->gfp_mask = GFP_HIGHUSER;
121 inode->i_mapping = mapping;
126 static void destroy_inode(struct inode *inode)
128 if (inode_has_buffers(inode))
130 /* Reinitialise the waitqueue head because __wait_on_freeing_inode()
131 may have left stale entries on it which it can't remove (since
132 it knows we're freeing the inode right now */
133 init_waitqueue_head(&inode->i_wait);
134 if (inode->i_sb->s_op->destroy_inode)
135 inode->i_sb->s_op->destroy_inode(inode);
137 kmem_cache_free(inode_cachep, inode);
142 * These are initializations that only need to be done
143 * once, because the fields are idempotent across use
144 * of the inode, so let the slab aware of that.
146 void inode_init_once(struct inode *inode)
148 memset(inode, 0, sizeof(*inode));
149 __inode_init_once(inode);
152 void __inode_init_once(struct inode *inode)
154 init_waitqueue_head(&inode->i_wait);
155 INIT_LIST_HEAD(&inode->i_hash);
156 INIT_LIST_HEAD(&inode->i_data.clean_pages);
157 INIT_LIST_HEAD(&inode->i_data.dirty_pages);
158 INIT_LIST_HEAD(&inode->i_data.locked_pages);
159 INIT_LIST_HEAD(&inode->i_dentry);
160 INIT_LIST_HEAD(&inode->i_dirty_buffers);
161 INIT_LIST_HEAD(&inode->i_dirty_data_buffers);
162 INIT_LIST_HEAD(&inode->i_devices);
163 sema_init(&inode->i_sem, 1);
164 sema_init(&inode->i_zombie, 1);
165 init_rwsem(&inode->i_alloc_sem);
166 spin_lock_init(&inode->i_data.i_shared_lock);
169 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
171 struct inode * inode = (struct inode *) foo;
173 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
174 SLAB_CTOR_CONSTRUCTOR)
175 inode_init_once(inode);
179 * Put the inode on the super block's dirty list.
181 * CAREFUL! We mark it dirty unconditionally, but
182 * move it onto the dirty list only if it is hashed.
183 * If it was not hashed, it will never be added to
184 * the dirty list even if it is later hashed, as it
185 * will have been marked dirty already.
187 * In short, make sure you hash any inodes _before_
188 * you start marking them dirty..
192 * __mark_inode_dirty - internal function
193 * @inode: inode to mark
194 * @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
195 * Mark an inode as dirty. Callers should use mark_inode_dirty or
196 * mark_inode_dirty_sync.
199 void __mark_inode_dirty(struct inode *inode, int flags)
201 struct super_block * sb = inode->i_sb;
206 /* Don't do this for I_DIRTY_PAGES - that doesn't actually dirty the inode itself */
207 if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
208 if (sb->s_op && sb->s_op->dirty_inode)
209 sb->s_op->dirty_inode(inode);
212 /* avoid the locking if we can */
213 if ((inode->i_state & flags) == flags)
216 spin_lock(&inode_lock);
217 if ((inode->i_state & flags) != flags) {
218 inode->i_state |= flags;
219 /* Only add valid (ie hashed) inodes to the dirty list */
220 if (!(inode->i_state & (I_LOCK|I_FREEING|I_CLEAR)) &&
221 !list_empty(&inode->i_hash)) {
222 list_del(&inode->i_list);
223 list_add(&inode->i_list, &sb->s_dirty);
226 spin_unlock(&inode_lock);
229 static void __wait_on_inode(struct inode * inode)
231 DECLARE_WAITQUEUE(wait, current);
233 add_wait_queue(&inode->i_wait, &wait);
235 set_current_state(TASK_UNINTERRUPTIBLE);
236 if (inode->i_state & I_LOCK) {
240 remove_wait_queue(&inode->i_wait, &wait);
241 current->state = TASK_RUNNING;
244 static inline void wait_on_inode(struct inode *inode)
246 if (inode->i_state & I_LOCK)
247 __wait_on_inode(inode);
251 * If we try to find an inode in the inode hash while it is being deleted, we
252 * have to wait until the filesystem completes its deletion before reporting
253 * that it isn't found. This is because iget will immediately call
254 * ->read_inode, and we want to be sure that evidence of the deletion is found
257 * Unlike the 2.6 version, this call call cannot return early, since inodes
258 * do not share wait queue. Therefore, we don't call remove_wait_queue(); it
259 * would be dangerous to do so since the inode may have already been freed,
260 * and it's unnecessary, since the inode is definitely going to get freed.
262 * This is called with inode_lock held.
264 static void __wait_on_freeing_inode(struct inode *inode)
266 DECLARE_WAITQUEUE(wait, current);
268 add_wait_queue(&inode->i_wait, &wait);
269 set_current_state(TASK_UNINTERRUPTIBLE);
270 spin_unlock(&inode_lock);
273 spin_lock(&inode_lock);
276 static inline void write_inode(struct inode *inode, int sync)
278 if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
279 inode->i_sb->s_op->write_inode(inode, sync);
282 static inline void __iget(struct inode * inode)
284 if (atomic_read(&inode->i_count)) {
285 atomic_inc(&inode->i_count);
288 atomic_inc(&inode->i_count);
289 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
290 list_del(&inode->i_list);
291 list_add(&inode->i_list, &inode_in_use);
293 inodes_stat.nr_unused--;
296 static inline void __refile_inode(struct inode *inode)
298 struct list_head *to;
300 if (inode->i_state & (I_FREEING|I_CLEAR))
302 if (list_empty(&inode->i_hash))
305 if (inode->i_state & I_DIRTY)
306 to = &inode->i_sb->s_dirty;
307 else if (atomic_read(&inode->i_count))
309 else if (inode->i_data.nrpages)
310 to = &inode_unused_pagecache;
313 list_del(&inode->i_list);
314 list_add(&inode->i_list, to);
317 void refile_inode(struct inode *inode)
321 spin_lock(&inode_lock);
322 if (!(inode->i_state & I_LOCK))
323 __refile_inode(inode);
324 spin_unlock(&inode_lock);
327 static inline void __sync_one(struct inode *inode, int sync)
331 list_del(&inode->i_list);
332 list_add(&inode->i_list, &inode->i_sb->s_locked_inodes);
334 if (inode->i_state & (I_LOCK|I_FREEING))
337 /* Set I_LOCK, reset I_DIRTY */
338 dirty = inode->i_state & I_DIRTY;
339 inode->i_state |= I_LOCK;
340 inode->i_state &= ~I_DIRTY;
341 spin_unlock(&inode_lock);
343 filemap_fdatasync(inode->i_mapping);
345 /* Don't write the inode if only I_DIRTY_PAGES was set */
346 if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))
347 write_inode(inode, sync);
349 filemap_fdatawait(inode->i_mapping);
351 spin_lock(&inode_lock);
352 inode->i_state &= ~I_LOCK;
353 __refile_inode(inode);
354 wake_up(&inode->i_wait);
357 static inline void sync_one(struct inode *inode, int sync)
359 while (inode->i_state & I_LOCK) {
361 spin_unlock(&inode_lock);
362 __wait_on_inode(inode);
364 spin_lock(&inode_lock);
367 __sync_one(inode, sync);
370 static inline void sync_list(struct list_head *head)
372 struct list_head * tmp;
374 while ((tmp = head->prev) != head)
375 __sync_one(list_entry(tmp, struct inode, i_list), 0);
378 static inline void wait_on_locked(struct list_head *head)
380 struct list_head * tmp;
381 while ((tmp = head->prev) != head) {
382 struct inode *inode = list_entry(tmp, struct inode, i_list);
384 spin_unlock(&inode_lock);
385 __wait_on_inode(inode);
387 spin_lock(&inode_lock);
391 static inline int try_to_sync_unused_list(struct list_head *head, int nr_inodes)
393 struct list_head *tmp = head;
396 while (nr_inodes && (tmp = tmp->prev) != head) {
397 inode = list_entry(tmp, struct inode, i_list);
399 if (!atomic_read(&inode->i_count)) {
400 __sync_one(inode, 0);
404 * __sync_one moved the inode to another list,
405 * so we have to start looking from the list head.
414 void sync_inodes_sb(struct super_block *sb)
416 spin_lock(&inode_lock);
417 while (!list_empty(&sb->s_dirty)||!list_empty(&sb->s_locked_inodes)) {
418 sync_list(&sb->s_dirty);
419 wait_on_locked(&sb->s_locked_inodes);
421 spin_unlock(&inode_lock);
426 * We don't need to grab a reference to superblock here. If it has non-empty
427 * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
428 * past sync_inodes_sb() until both ->s_dirty and ->s_locked_inodes are
429 * empty. Since __sync_one() regains inode_lock before it finally moves
430 * inode from superblock lists we are OK.
433 void sync_unlocked_inodes(void)
435 struct super_block * sb;
436 spin_lock(&inode_lock);
438 sb = sb_entry(super_blocks.next);
439 for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.next)) {
440 if (!list_empty(&sb->s_dirty)) {
441 spin_unlock(&sb_lock);
442 sync_list(&sb->s_dirty);
446 spin_unlock(&sb_lock);
447 spin_unlock(&inode_lock);
451 * Find a superblock with inodes that need to be synced
454 static struct super_block *get_super_to_sync(void)
458 spin_lock(&inode_lock);
460 list_for_each(p, &super_blocks) {
461 struct super_block *s = list_entry(p,struct super_block,s_list);
462 if (list_empty(&s->s_dirty) && list_empty(&s->s_locked_inodes))
465 spin_unlock(&sb_lock);
466 spin_unlock(&inode_lock);
467 down_read(&s->s_umount);
474 spin_unlock(&sb_lock);
475 spin_unlock(&inode_lock);
481 * @dev: device to sync the inodes from.
483 * sync_inodes goes through the super block's dirty list,
484 * writes them out, and puts them back on the normal list.
487 void sync_inodes(kdev_t dev)
489 struct super_block * s;
492 * Search the super_blocks array for the device(s) to sync.
495 if ((s = get_super(dev)) != NULL) {
500 while ((s = get_super_to_sync()) != NULL) {
507 static void try_to_sync_unused_inodes(void * arg)
509 struct super_block * sb;
510 int nr_inodes = inodes_stat.nr_unused;
512 spin_lock(&inode_lock);
514 sb = sb_entry(super_blocks.next);
515 for (; nr_inodes && sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.next)) {
516 if (list_empty(&sb->s_dirty))
518 spin_unlock(&sb_lock);
519 nr_inodes = try_to_sync_unused_list(&sb->s_dirty, nr_inodes);
522 spin_unlock(&sb_lock);
523 spin_unlock(&inode_lock);
526 static struct tq_struct unused_inodes_flush_task;
529 * write_inode_now - write an inode to disk
530 * @inode: inode to write to disk
531 * @sync: whether the write should be synchronous or not
533 * This function commits an inode to disk immediately if it is
534 * dirty. This is primarily needed by knfsd.
537 void write_inode_now(struct inode *inode, int sync)
539 struct super_block * sb = inode->i_sb;
542 spin_lock(&inode_lock);
543 while (inode->i_state & I_DIRTY)
544 sync_one(inode, sync);
545 spin_unlock(&inode_lock);
547 wait_on_inode(inode);
550 printk(KERN_ERR "write_inode_now: no super block\n");
554 * generic_osync_inode - flush all dirty data for a given inode to disk
555 * @inode: inode to write
556 * @datasync: if set, don't bother flushing timestamps
558 * This can be called by file_write functions for files which have the
559 * O_SYNC flag set, to flush dirty writes to disk.
562 int generic_osync_inode(struct inode *inode, int what)
564 int err = 0, err2 = 0, need_write_inode_now = 0;
569 * Currently, the filesystem write path does not pass the
570 * filp down to the low-level write functions. Therefore it
571 * is impossible for (say) __block_commit_write to know if
572 * the operation is O_SYNC or not.
574 * Ideally, O_SYNC writes would have the filesystem call
575 * ll_rw_block as it went to kick-start the writes, and we
576 * could call osync_inode_buffers() here to wait only for
577 * those IOs which have already been submitted to the device
578 * driver layer. As it stands, if we did this we'd not write
579 * anything to disk since our writes have not been queued by
580 * this point: they are still on the dirty LRU.
582 * So, currently we will call fsync_inode_buffers() instead,
583 * to flush _all_ dirty buffers for this inode to disk on
584 * every O_SYNC write, not just the synchronous I/Os. --sct
587 if (what & OSYNC_METADATA)
588 err = fsync_inode_buffers(inode);
589 if (what & OSYNC_DATA)
590 err2 = fsync_inode_data_buffers(inode);
594 spin_lock(&inode_lock);
595 if ((inode->i_state & I_DIRTY) &&
596 ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
597 need_write_inode_now = 1;
598 spin_unlock(&inode_lock);
600 if (need_write_inode_now)
601 write_inode_now(inode, 1);
603 wait_on_inode(inode);
609 * clear_inode - clear an inode
610 * @inode: inode to clear
612 * This is called by the filesystem to tell us
613 * that the inode is no longer useful. We just
614 * terminate it with extreme prejudice.
617 void clear_inode(struct inode *inode)
619 invalidate_inode_buffers(inode);
621 if (inode->i_data.nrpages)
623 if (!(inode->i_state & I_FREEING))
625 if (inode->i_state & I_CLEAR)
627 wait_on_inode(inode);
629 if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->clear_inode)
630 inode->i_sb->s_op->clear_inode(inode);
633 else if (inode->i_cdev) {
634 cdput(inode->i_cdev);
635 inode->i_cdev = NULL;
637 spin_lock(&inode_lock);
638 inode->i_state = I_CLEAR;
639 spin_unlock(&inode_lock);
643 * Dispose-list gets a local list with local inodes in it, so it doesn't
644 * need to worry about list corruption and SMP locks.
646 static void dispose_list(struct list_head *head)
650 while (!list_empty(head)) {
653 inode = list_entry(head->next, struct inode, i_list);
654 list_del(&inode->i_list);
656 if (inode->i_data.nrpages)
657 truncate_inode_pages(&inode->i_data, 0);
659 spin_lock(&inode_lock);
660 list_del(&inode->i_hash);
661 INIT_LIST_HEAD(&inode->i_hash);
662 spin_unlock(&inode_lock);
663 wake_up(&inode->i_wait);
664 destroy_inode(inode);
667 spin_lock(&inode_lock);
668 inodes_stat.nr_inodes -= nr_disposed;
669 spin_unlock(&inode_lock);
673 * Invalidate all inodes for a device.
675 static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
677 struct list_head *next;
678 int busy = 0, count = 0;
682 struct list_head * tmp = next;
683 struct inode * inode;
688 inode = list_entry(tmp, struct inode, i_list);
689 if (inode->i_sb != sb)
691 invalidate_inode_buffers(inode);
692 if (!atomic_read(&inode->i_count)) {
693 list_del_init(&inode->i_hash);
694 list_del(&inode->i_list);
695 list_add(&inode->i_list, dispose);
696 inode->i_state |= I_FREEING;
702 /* only unused inodes may be cached with i_count zero */
703 inodes_stat.nr_unused -= count;
708 * This is a two-stage process. First we collect all
709 * offending inodes onto the throw-away list, and in
710 * the second stage we actually dispose of them. This
711 * is because we don't want to sleep while messing
712 * with the global lists..
716 * invalidate_inodes - discard the inodes on a device
719 * Discard all of the inodes for a given superblock. If the discard
720 * fails because there are busy inodes then a non zero value is returned.
721 * If the discard is successful all the inodes have been discarded.
724 int invalidate_inodes(struct super_block * sb)
727 LIST_HEAD(throw_away);
729 spin_lock(&inode_lock);
730 busy = invalidate_list(&inode_in_use, sb, &throw_away);
731 busy |= invalidate_list(&inode_unused, sb, &throw_away);
732 busy |= invalidate_list(&inode_unused_pagecache, sb, &throw_away);
733 busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
734 busy |= invalidate_list(&sb->s_locked_inodes, sb, &throw_away);
735 spin_unlock(&inode_lock);
737 dispose_list(&throw_away);
742 int invalidate_device(kdev_t dev, int do_sync)
744 struct super_block *sb;
754 * no need to lock the super, get_super holds the
755 * read semaphore so the filesystem cannot go away
756 * under us (->put_super runs with the write lock
759 shrink_dcache_sb(sb);
760 res = invalidate_inodes(sb);
763 invalidate_buffers(dev);
769 * This is called with the inode lock held. It searches
770 * the in-use for freeable inodes, which are moved to a
771 * temporary list and then placed on the unused list by
774 * We don't expect to have to call this very often.
776 * We leave the inode in the inode hash table until *after*
777 * the filesystem's ->delete_inode (in dispose_list) completes.
778 * This ensures that an iget (such as nfsd might instigate) will
779 * always find up-to-date information either in the hash or on disk.
781 * I_FREEING is set so that no-one will take a new reference
782 * to the inode while it is being deleted.
784 * N.B. The spinlock is released during the call to
787 #define CAN_UNUSE(inode) \
788 ((((inode)->i_state | (inode)->i_data.nrpages) == 0) && \
789 !inode_has_buffers(inode))
790 #define INODE(entry) (list_entry(entry, struct inode, i_list))
792 void prune_icache(int goal)
795 struct list_head *entry, *freeable = &list;
797 #ifdef CONFIG_HIGHMEM
800 struct inode * inode;
802 spin_lock(&inode_lock);
805 entry = inode_unused.prev;
806 while (entry != &inode_unused)
808 struct list_head *tmp = entry;
812 if (inode->i_state & (I_FREEING|I_CLEAR|I_LOCK))
814 if (!CAN_UNUSE(inode))
816 if (atomic_read(&inode->i_count))
819 list_add(tmp, freeable);
820 inode->i_state |= I_FREEING;
825 inodes_stat.nr_unused -= count;
826 spin_unlock(&inode_lock);
828 dispose_list(freeable);
831 * If we didn't freed enough clean inodes schedule
832 * a sync of the dirty inodes, we cannot do it
833 * from here or we're either synchronously dogslow
834 * or we deadlock with oom.
837 schedule_task(&unused_inodes_flush_task);
839 #ifdef CONFIG_HIGHMEM
841 * On highmem machines it is possible to have low memory
842 * filled with inodes that cannot be reclaimed because they
843 * have page cache pages in highmem attached to them.
844 * This could deadlock the system if the memory used by
845 * inodes is significant compared to the amount of freeable
846 * low memory. In that case we forcefully remove the page
847 * cache pages from the inodes we want to reclaim.
849 * Note that this loop doesn't actually reclaim the inodes;
850 * once the last pagecache pages belonging to the inode is
851 * gone it will be placed on the inode_unused list and the
852 * loop above will prune it the next time prune_icache() is
857 if (inodes_stat.nr_unused * sizeof(struct inode) * 10 <
858 freeable_lowmem() * PAGE_SIZE)
863 avg_pages = page_cache_size;
864 avg_pages -= atomic_read(&buffermem_pages) + swapper_space.nrpages;
865 avg_pages = avg_pages / (inodes_stat.nr_inodes + 1);
866 spin_lock(&inode_lock);
868 if (list_empty(&inode_unused_pagecache))
870 entry = inode_unused_pagecache.prev;
872 list_add(entry, &inode_unused_pagecache);
874 inode = INODE(entry);
875 /* Don't nuke inodes with lots of page cache attached. */
876 if (inode->i_mapping->nrpages > 5 * avg_pages)
878 /* Because of locking we grab the inode and unlock the list .*/
879 if (inode->i_state & I_LOCK)
881 inode->i_state |= I_LOCK;
882 spin_unlock(&inode_lock);
885 * If the inode has clean pages only, we can free all its
886 * pagecache memory; the inode will automagically be refiled
887 * onto the unused_list. The wakeup_bdflush above makes
888 * sure that all inodes become clean eventually.
890 if (list_empty(&inode->i_mapping->dirty_pages) &&
891 !inode_has_buffers(inode))
892 invalidate_inode_pages(inode);
894 /* Release the inode again. */
895 spin_lock(&inode_lock);
896 inode->i_state &= ~I_LOCK;
897 wake_up(&inode->i_wait);
899 spin_unlock(&inode_lock);
900 #endif /* CONFIG_HIGHMEM */
903 int shrink_icache_memory(int priority, int gfp_mask)
908 * Nasty deadlock avoidance..
910 * We may hold various FS locks, and we don't
911 * want to recurse into the FS that called us
912 * in clear_inode() and friends..
914 if (!(gfp_mask & __GFP_FS))
917 count = inodes_stat.nr_unused / priority;
920 return kmem_cache_shrink(inode_cachep);
924 * Called with the inode lock held.
925 * NOTE: we are not increasing the inode-refcount, you must call __iget()
926 * by hand after calling find_inode now! This simplifies iunique and won't
927 * add any additional branch in the common code.
929 static struct inode * find_inode(struct super_block * sb, unsigned long ino, struct list_head *head, find_inode_t find_actor, void *opaque)
931 struct list_head *tmp;
932 struct inode * inode;
941 inode = list_entry(tmp, struct inode, i_hash);
942 if (inode->i_ino != ino)
944 if (inode->i_sb != sb)
946 if (find_actor && !find_actor(inode, ino, opaque))
948 if (inode->i_state & (I_FREEING|I_CLEAR)) {
949 __wait_on_freeing_inode(inode);
958 * new_inode - obtain an inode
961 * Allocates a new inode for given superblock.
964 struct inode * new_inode(struct super_block *sb)
966 static unsigned long last_ino;
967 struct inode * inode;
969 spin_lock_prefetch(&inode_lock);
971 inode = alloc_inode(sb);
973 spin_lock(&inode_lock);
974 inodes_stat.nr_inodes++;
975 list_add(&inode->i_list, &inode_in_use);
976 inode->i_ino = ++last_ino;
978 spin_unlock(&inode_lock);
983 void unlock_new_inode(struct inode *inode)
986 * This is special! We do not need the spinlock
987 * when clearing I_LOCK, because we're guaranteed
988 * that nobody else tries to do anything about the
989 * state of the inode when it is locked, as we
990 * just created it (so there can be no old holders
991 * that haven't tested I_LOCK).
993 inode->i_state &= ~(I_LOCK|I_NEW);
994 wake_up(&inode->i_wait);
998 * This is called without the inode lock held.. Be careful.
1000 * We no longer cache the sb_flags in i_flags - see fs.h
1001 * -- rmk@arm.uk.linux.org
1003 static struct inode * get_new_inode(struct super_block *sb, unsigned long ino, struct list_head *head, find_inode_t find_actor, void *opaque)
1005 struct inode * inode;
1007 inode = alloc_inode(sb);
1011 spin_lock(&inode_lock);
1012 /* We released the lock, so.. */
1013 old = find_inode(sb, ino, head, find_actor, opaque);
1015 inodes_stat.nr_inodes++;
1016 list_add(&inode->i_list, &inode_in_use);
1017 list_add(&inode->i_hash, head);
1019 inode->i_state = I_LOCK|I_NEW;
1020 spin_unlock(&inode_lock);
1023 * Return the locked inode with I_NEW set, the
1024 * caller is responsible for filling in the contents
1030 * Uhhuh, somebody else created the same inode under
1031 * us. Use the old inode instead of the one we just
1035 spin_unlock(&inode_lock);
1036 destroy_inode(inode);
1038 wait_on_inode(inode);
1043 static inline unsigned long hash(struct super_block *sb, unsigned long i_ino)
1045 unsigned long tmp = i_ino + ((unsigned long) sb / L1_CACHE_BYTES);
1046 tmp = tmp + (tmp >> I_HASHBITS);
1047 return tmp & I_HASHMASK;
1050 /* Yeah, I know about quadratic hash. Maybe, later. */
1053 * iunique - get a unique inode number
1055 * @max_reserved: highest reserved inode number
1057 * Obtain an inode number that is unique on the system for a given
1058 * superblock. This is used by file systems that have no natural
1059 * permanent inode numbering system. An inode number is returned that
1060 * is higher than the reserved limit but unique.
1063 * With a large number of inodes live on the file system this function
1064 * currently becomes quite slow.
1067 ino_t iunique(struct super_block *sb, ino_t max_reserved)
1069 static ino_t counter = 0;
1070 struct inode *inode;
1071 struct list_head * head;
1073 spin_lock(&inode_lock);
1075 if (counter > max_reserved) {
1076 head = inode_hashtable + hash(sb,counter);
1077 inode = find_inode(sb, res = counter++, head, NULL, NULL);
1079 spin_unlock(&inode_lock);
1083 counter = max_reserved + 1;
1090 * ilookup - search for an inode in the inode cache
1091 * @sb: super block of file system to search
1092 * @ino: inode number to search for
1094 * If the inode is in the cache, the inode is returned with an
1095 * incremented reference count.
1097 * Otherwise, %NULL is returned.
1099 * This is almost certainly not the function you are looking for.
1100 * If you think you need to use this, consult an expert first.
1102 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1104 struct list_head * head = inode_hashtable + hash(sb,ino);
1105 struct inode * inode;
1107 spin_lock(&inode_lock);
1108 inode = find_inode(sb, ino, head, NULL, NULL);
1111 spin_unlock(&inode_lock);
1112 wait_on_inode(inode);
1115 spin_unlock(&inode_lock);
1120 struct inode *igrab(struct inode *inode)
1122 spin_lock(&inode_lock);
1123 if (!(inode->i_state & I_FREEING))
1127 * Handle the case where s_op->clear_inode is not been
1128 * called yet, and somebody is calling igrab
1129 * while the inode is getting freed.
1132 spin_unlock(&inode_lock);
1136 struct inode *iget4_locked(struct super_block *sb, unsigned long ino, find_inode_t find_actor, void *opaque)
1138 struct list_head * head = inode_hashtable + hash(sb,ino);
1139 struct inode * inode;
1141 spin_lock(&inode_lock);
1142 inode = find_inode(sb, ino, head, find_actor, opaque);
1145 spin_unlock(&inode_lock);
1146 wait_on_inode(inode);
1149 spin_unlock(&inode_lock);
1152 * get_new_inode() will do the right thing, re-trying the search
1153 * in case it had to block at any point.
1155 return get_new_inode(sb, ino, head, find_actor, opaque);
1159 * insert_inode_hash - hash an inode
1160 * @inode: unhashed inode
1162 * Add an inode to the inode hash for this superblock. If the inode
1163 * has no superblock it is added to a separate anonymous chain.
1166 void insert_inode_hash(struct inode *inode)
1168 struct list_head *head = &anon_hash_chain;
1170 head = inode_hashtable + hash(inode->i_sb, inode->i_ino);
1171 spin_lock(&inode_lock);
1172 list_add(&inode->i_hash, head);
1173 spin_unlock(&inode_lock);
1177 * remove_inode_hash - remove an inode from the hash
1178 * @inode: inode to unhash
1180 * Remove an inode from the superblock or anonymous hash.
1183 void remove_inode_hash(struct inode *inode)
1185 spin_lock(&inode_lock);
1186 list_del(&inode->i_hash);
1187 INIT_LIST_HEAD(&inode->i_hash);
1188 spin_unlock(&inode_lock);
1192 * iput - put an inode
1193 * @inode: inode to put
1195 * Puts an inode, dropping its usage count. If the inode use count hits
1196 * zero the inode is also then freed and may be destroyed.
1199 void iput(struct inode *inode)
1202 struct super_block *sb = inode->i_sb;
1203 struct super_operations *op = NULL;
1205 if (inode->i_state == I_CLEAR)
1210 if (op && op->put_inode)
1211 op->put_inode(inode);
1213 if (!atomic_dec_and_lock(&inode->i_count, &inode_lock))
1216 if (!inode->i_nlink) {
1217 list_del(&inode->i_list);
1218 INIT_LIST_HEAD(&inode->i_list);
1219 inode->i_state|=I_FREEING;
1220 inodes_stat.nr_inodes--;
1221 spin_unlock(&inode_lock);
1223 if (inode->i_data.nrpages)
1224 truncate_inode_pages(&inode->i_data, 0);
1226 if (op && op->delete_inode) {
1227 void (*delete)(struct inode *) = op->delete_inode;
1228 if (!is_bad_inode(inode))
1230 /* s_op->delete_inode internally recalls clear_inode() */
1234 spin_lock(&inode_lock);
1235 list_del(&inode->i_hash);
1236 INIT_LIST_HEAD(&inode->i_hash);
1237 spin_unlock(&inode_lock);
1238 wake_up(&inode->i_wait);
1239 if (inode->i_state != I_CLEAR)
1242 if (!list_empty(&inode->i_hash)) {
1243 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
1244 __refile_inode(inode);
1245 inodes_stat.nr_unused++;
1246 spin_unlock(&inode_lock);
1247 if (!sb || (sb->s_flags & MS_ACTIVE))
1249 write_inode_now(inode, 1);
1250 spin_lock(&inode_lock);
1251 inodes_stat.nr_unused--;
1252 list_del_init(&inode->i_hash);
1254 list_del_init(&inode->i_list);
1255 inode->i_state|=I_FREEING;
1256 inodes_stat.nr_inodes--;
1257 spin_unlock(&inode_lock);
1258 if (inode->i_data.nrpages)
1259 truncate_inode_pages(&inode->i_data, 0);
1262 destroy_inode(inode);
1266 void force_delete(struct inode *inode)
1269 * Kill off unused inodes ... iput() will unhash and
1270 * delete the inode if we set i_nlink to zero.
1272 if (atomic_read(&inode->i_count) == 1)
1277 * bmap - find a block number in a file
1278 * @inode: inode of file
1279 * @block: block to find
1281 * Returns the block number on the device holding the inode that
1282 * is the disk block number for the block of the file requested.
1283 * That is, asked for block 4 of inode 1 the function will return the
1284 * disk block relative to the disk start that holds that block of the
1288 int bmap(struct inode * inode, int block)
1291 if (inode->i_mapping->a_ops->bmap)
1292 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1297 * Initialize the hash tables.
1299 void __init inode_init(unsigned long mempages)
1301 struct list_head *head;
1302 unsigned long order;
1303 unsigned int nr_hash;
1306 mempages >>= (14 - PAGE_SHIFT);
1307 mempages *= sizeof(struct list_head);
1308 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1314 nr_hash = (1UL << order) * PAGE_SIZE /
1315 sizeof(struct list_head);
1316 i_hash_mask = (nr_hash - 1);
1320 while ((tmp >>= 1UL) != 0UL)
1323 inode_hashtable = (struct list_head *)
1324 __get_free_pages(GFP_ATOMIC, order);
1325 } while (inode_hashtable == NULL && --order >= 0);
1327 printk(KERN_INFO "Inode cache hash table entries: %d (order: %ld, %ld bytes)\n",
1328 nr_hash, order, (PAGE_SIZE << order));
1330 if (!inode_hashtable)
1331 panic("Failed to allocate inode hash table\n");
1333 head = inode_hashtable;
1336 INIT_LIST_HEAD(head);
1341 /* inode slab cache */
1342 inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
1343 0, SLAB_HWCACHE_ALIGN, init_once,
1346 panic("cannot create inode slab cache");
1348 unused_inodes_flush_task.routine = try_to_sync_unused_inodes;
1352 * update_atime - update the access time
1353 * @inode: inode accessed
1355 * Update the accessed time on an inode and mark it for writeback.
1356 * This function automatically handles read only file systems and media,
1357 * as well as the "noatime" flag and inode specific "noatime" markers.
1360 void update_atime (struct inode *inode)
1362 if (inode->i_atime == CURRENT_TIME)
1364 if (IS_NOATIME(inode))
1366 if (IS_NODIRATIME(inode) && S_ISDIR(inode->i_mode))
1368 if (IS_RDONLY(inode))
1370 inode->i_atime = CURRENT_TIME;
1371 mark_inode_dirty_sync (inode);
1375 * update_mctime - update the mtime and ctime
1376 * @inode: inode accessed
1378 * Update the modified and changed times on an inode for writes to special
1379 * files such as fifos. No change is forced if the timestamps are already
1380 * up-to-date or if the filesystem is readonly.
1383 void update_mctime (struct inode *inode)
1385 if (inode->i_mtime == CURRENT_TIME && inode->i_ctime == CURRENT_TIME)
1387 if (IS_RDONLY(inode))
1389 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1390 mark_inode_dirty (inode);
1395 * Quota functions that want to walk the inode lists..
1399 /* Functions back in dquot.c */
1400 void put_dquot_list(struct list_head *);
1401 int remove_inode_dquot_ref(struct inode *, short, struct list_head *);
1403 void remove_dquot_ref(struct super_block *sb, short type)
1405 struct inode *inode;
1406 struct list_head *act_head;
1407 LIST_HEAD(tofree_head);
1410 return; /* nothing to do */
1411 /* We have to be protected against other CPUs */
1412 lock_kernel(); /* This lock is for quota code */
1413 spin_lock(&inode_lock); /* This lock is for inodes code */
1415 list_for_each(act_head, &inode_in_use) {
1416 inode = list_entry(act_head, struct inode, i_list);
1417 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1418 remove_inode_dquot_ref(inode, type, &tofree_head);
1420 list_for_each(act_head, &inode_unused) {
1421 inode = list_entry(act_head, struct inode, i_list);
1422 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1423 remove_inode_dquot_ref(inode, type, &tofree_head);
1425 list_for_each(act_head, &inode_unused_pagecache) {
1426 inode = list_entry(act_head, struct inode, i_list);
1427 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1428 remove_inode_dquot_ref(inode, type, &tofree_head);
1430 list_for_each(act_head, &sb->s_dirty) {
1431 inode = list_entry(act_head, struct inode, i_list);
1432 if (IS_QUOTAINIT(inode))
1433 remove_inode_dquot_ref(inode, type, &tofree_head);
1435 list_for_each(act_head, &sb->s_locked_inodes) {
1436 inode = list_entry(act_head, struct inode, i_list);
1437 if (IS_QUOTAINIT(inode))
1438 remove_inode_dquot_ref(inode, type, &tofree_head);
1440 spin_unlock(&inode_lock);
1443 put_dquot_list(&tofree_head);