4 * (C) 1997 Linus Torvalds
7 #include <linux/config.h>
10 #include <linux/dcache.h>
11 #include <linux/init.h>
12 #include <linux/quotaops.h>
13 #include <linux/slab.h>
14 #include <linux/writeback.h>
15 #include <linux/module.h>
16 #include <linux/backing-dev.h>
17 #include <linux/wait.h>
18 #include <linux/hash.h>
19 #include <linux/security.h>
22 * This is needed for the following functions:
24 * - invalidate_inode_buffers
28 * FIXME: remove all knowledge of the buffer layer from this file
30 #include <linux/buffer_head.h>
33 * New inode.c implementation.
35 * This implementation has the basic premise of trying
36 * to be extremely low-overhead and SMP-safe, yet be
37 * simple enough to be "obviously correct".
42 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
44 /* #define INODE_PARANOIA 1 */
45 /* #define INODE_DEBUG 1 */
48 * Inode lookup is no longer as critical as it used to be:
49 * most of the lookups are going to be through the dcache.
51 #define I_HASHBITS i_hash_shift
52 #define I_HASHMASK i_hash_mask
54 static unsigned int i_hash_mask
;
55 static unsigned int i_hash_shift
;
58 * Each inode can be on two separate lists. One is
59 * the hash list of the inode, used for lookups. The
60 * other linked list is the "type" list:
61 * "in_use" - valid inode, i_count > 0, i_nlink > 0
62 * "dirty" - as "in_use" but also dirty
63 * "unused" - valid inode, i_count = 0
65 * A "dirty" list is maintained for each super block,
66 * allowing for low-overhead inode sync() operations.
69 LIST_HEAD(inode_in_use
);
70 LIST_HEAD(inode_unused
);
71 static struct list_head
*inode_hashtable
;
72 static LIST_HEAD(anon_hash_chain
); /* for inodes with NULL i_sb */
75 * A simple spinlock to protect the list manipulations.
77 * NOTE! You also have to own the lock if you change
78 * the i_state of an inode while it is in use..
80 spinlock_t inode_lock
= SPIN_LOCK_UNLOCKED
;
83 * Statistics gathering..
85 struct inodes_stat_t inodes_stat
;
87 static kmem_cache_t
* inode_cachep
;
89 static struct inode
*alloc_inode(struct super_block
*sb
)
91 static struct address_space_operations empty_aops
;
92 static struct inode_operations empty_iops
;
93 static struct file_operations empty_fops
;
96 if (sb
->s_op
->alloc_inode
)
97 inode
= sb
->s_op
->alloc_inode(sb
);
99 inode
= (struct inode
*) kmem_cache_alloc(inode_cachep
, SLAB_KERNEL
);
102 struct address_space
* const mapping
= &inode
->i_data
;
105 inode
->i_dev
= sb
->s_dev
;
106 inode
->i_blkbits
= sb
->s_blocksize_bits
;
108 atomic_set(&inode
->i_count
, 1);
110 inode
->i_op
= &empty_iops
;
111 inode
->i_fop
= &empty_fops
;
113 atomic_set(&inode
->i_writecount
, 0);
117 inode
->i_generation
= 0;
118 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
119 inode
->i_pipe
= NULL
;
120 inode
->i_bdev
= NULL
;
121 inode
->i_cdev
= NULL
;
122 inode
->i_security
= NULL
;
123 if (security_ops
->inode_alloc_security(inode
)) {
124 if (inode
->i_sb
->s_op
->destroy_inode
)
125 inode
->i_sb
->s_op
->destroy_inode(inode
);
127 kmem_cache_free(inode_cachep
, (inode
));
131 mapping
->a_ops
= &empty_aops
;
132 mapping
->host
= inode
;
133 mapping
->gfp_mask
= GFP_HIGHUSER
;
134 mapping
->dirtied_when
= 0;
135 mapping
->assoc_mapping
= NULL
;
136 mapping
->backing_dev_info
= &default_backing_dev_info
;
138 inode
->i_data
.backing_dev_info
= sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
139 memset(&inode
->u
, 0, sizeof(inode
->u
));
140 inode
->i_mapping
= mapping
;
145 void destroy_inode(struct inode
*inode
)
147 if (inode_has_buffers(inode
))
149 security_ops
->inode_free_security(inode
);
150 if (inode
->i_sb
->s_op
->destroy_inode
) {
151 inode
->i_sb
->s_op
->destroy_inode(inode
);
153 BUG_ON(inode
->i_data
.page_tree
.rnode
!= NULL
);
154 kmem_cache_free(inode_cachep
, (inode
));
160 * These are initializations that only need to be done
161 * once, because the fields are idempotent across use
162 * of the inode, so let the slab aware of that.
164 void inode_init_once(struct inode
*inode
)
166 memset(inode
, 0, sizeof(*inode
));
167 INIT_LIST_HEAD(&inode
->i_hash
);
168 INIT_LIST_HEAD(&inode
->i_data
.clean_pages
);
169 INIT_LIST_HEAD(&inode
->i_data
.dirty_pages
);
170 INIT_LIST_HEAD(&inode
->i_data
.locked_pages
);
171 INIT_LIST_HEAD(&inode
->i_data
.io_pages
);
172 INIT_LIST_HEAD(&inode
->i_dentry
);
173 INIT_LIST_HEAD(&inode
->i_devices
);
174 sema_init(&inode
->i_sem
, 1);
175 INIT_RADIX_TREE(&inode
->i_data
.page_tree
, GFP_ATOMIC
);
176 rwlock_init(&inode
->i_data
.page_lock
);
177 spin_lock_init(&inode
->i_data
.i_shared_lock
);
178 INIT_LIST_HEAD(&inode
->i_data
.private_list
);
179 spin_lock_init(&inode
->i_data
.private_lock
);
180 INIT_LIST_HEAD(&inode
->i_data
.i_mmap
);
181 INIT_LIST_HEAD(&inode
->i_data
.i_mmap_shared
);
184 static void init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
186 struct inode
* inode
= (struct inode
*) foo
;
188 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
189 SLAB_CTOR_CONSTRUCTOR
)
190 inode_init_once(inode
);
194 * inode_lock must be held
196 void __iget(struct inode
* inode
)
198 if (atomic_read(&inode
->i_count
)) {
199 atomic_inc(&inode
->i_count
);
202 atomic_inc(&inode
->i_count
);
203 if (!(inode
->i_state
& (I_DIRTY
|I_LOCK
))) {
204 list_del(&inode
->i_list
);
205 list_add(&inode
->i_list
, &inode_in_use
);
207 inodes_stat
.nr_unused
--;
211 * clear_inode - clear an inode
212 * @inode: inode to clear
214 * This is called by the filesystem to tell us
215 * that the inode is no longer useful. We just
216 * terminate it with extreme prejudice.
219 void clear_inode(struct inode
*inode
)
221 invalidate_inode_buffers(inode
);
223 if (inode
->i_data
.nrpages
)
225 if (!(inode
->i_state
& I_FREEING
))
227 if (inode
->i_state
& I_CLEAR
)
229 wait_on_inode(inode
);
231 if (inode
->i_sb
&& inode
->i_sb
->s_op
&& inode
->i_sb
->s_op
->clear_inode
)
232 inode
->i_sb
->s_op
->clear_inode(inode
);
235 else if (inode
->i_cdev
) {
236 cdput(inode
->i_cdev
);
237 inode
->i_cdev
= NULL
;
239 inode
->i_state
= I_CLEAR
;
243 * Dispose-list gets a local list with local inodes in it, so it doesn't
244 * need to worry about list corruption and SMP locks.
246 static void dispose_list(struct list_head
*head
)
250 while (!list_empty(head
)) {
253 inode
= list_entry(head
->next
, struct inode
, i_list
);
254 list_del(&inode
->i_list
);
256 if (inode
->i_data
.nrpages
)
257 truncate_inode_pages(&inode
->i_data
, 0);
259 destroy_inode(inode
);
262 spin_lock(&inode_lock
);
263 inodes_stat
.nr_inodes
-= nr_disposed
;
264 spin_unlock(&inode_lock
);
268 * Invalidate all inodes for a device.
270 static int invalidate_list(struct list_head
*head
, struct super_block
* sb
, struct list_head
* dispose
)
272 struct list_head
*next
;
273 int busy
= 0, count
= 0;
277 struct list_head
* tmp
= next
;
278 struct inode
* inode
;
283 inode
= list_entry(tmp
, struct inode
, i_list
);
284 if (inode
->i_sb
!= sb
)
286 invalidate_inode_buffers(inode
);
287 if (!atomic_read(&inode
->i_count
)) {
288 list_del_init(&inode
->i_hash
);
289 list_del(&inode
->i_list
);
290 list_add(&inode
->i_list
, dispose
);
291 inode
->i_state
|= I_FREEING
;
297 /* only unused inodes may be cached with i_count zero */
298 inodes_stat
.nr_unused
-= count
;
303 * This is a two-stage process. First we collect all
304 * offending inodes onto the throw-away list, and in
305 * the second stage we actually dispose of them. This
306 * is because we don't want to sleep while messing
307 * with the global lists..
311 * invalidate_inodes - discard the inodes on a device
314 * Discard all of the inodes for a given superblock. If the discard
315 * fails because there are busy inodes then a non zero value is returned.
316 * If the discard is successful all the inodes have been discarded.
319 int invalidate_inodes(struct super_block
* sb
)
322 LIST_HEAD(throw_away
);
324 spin_lock(&inode_lock
);
325 busy
= invalidate_list(&inode_in_use
, sb
, &throw_away
);
326 busy
|= invalidate_list(&inode_unused
, sb
, &throw_away
);
327 busy
|= invalidate_list(&sb
->s_dirty
, sb
, &throw_away
);
328 busy
|= invalidate_list(&sb
->s_io
, sb
, &throw_away
);
329 spin_unlock(&inode_lock
);
331 dispose_list(&throw_away
);
336 int invalidate_device(kdev_t dev
, int do_sync
)
338 struct super_block
*sb
;
339 struct block_device
*bdev
= bdget(kdev_t_to_nr(dev
));
349 sb
= get_super(bdev
);
352 * no need to lock the super, get_super holds the
353 * read semaphore so the filesystem cannot go away
354 * under us (->put_super runs with the write lock
357 shrink_dcache_sb(sb
);
358 res
= invalidate_inodes(sb
);
361 invalidate_bdev(bdev
, 0);
366 static int can_unuse(struct inode
*inode
)
370 if (inode_has_buffers(inode
))
372 if (atomic_read(&inode
->i_count
))
378 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
379 * a temporary list and then are freed outside inode_lock by dispose_list().
381 * Any inodes which are pinned purely because of attached pagecache have their
382 * pagecache removed. We expect the final iput() on that inode to add it to
383 * the front of the inode_unused list. So look for it there and if the
384 * inode is still freeable, proceed. The right inode is found 99.9% of the
385 * time in testing on a 4-way.
387 static void prune_icache(int nr_to_scan
)
393 spin_lock(&inode_lock
);
394 for (nr_scanned
= 0; nr_scanned
< nr_to_scan
; nr_scanned
++) {
397 if (list_empty(&inode_unused
))
400 inode
= list_entry(inode_unused
.prev
, struct inode
, i_list
);
402 if (!can_unuse(inode
)) {
403 list_move(&inode
->i_list
, &inode_unused
);
406 if (inode
->i_data
.nrpages
) {
408 spin_unlock(&inode_lock
);
409 invalidate_inode_pages(&inode
->i_data
);
411 spin_lock(&inode_lock
);
413 if (inode
!= list_entry(inode_unused
.next
,
414 struct inode
, i_list
))
415 continue; /* wrong inode or list_empty */
416 if (!can_unuse(inode
))
418 if (inode
->i_data
.nrpages
)
421 list_del_init(&inode
->i_hash
);
422 list_move(&inode
->i_list
, &freeable
);
423 inode
->i_state
|= I_FREEING
;
426 inodes_stat
.nr_unused
-= nr_pruned
;
427 spin_unlock(&inode_lock
);
428 dispose_list(&freeable
);
432 * This is called from kswapd when we think we need some
435 static int shrink_icache_memory(int nr
, unsigned int gfp_mask
)
439 * Nasty deadlock avoidance. We may hold various FS locks,
440 * and we don't want to recurse into the FS that called us
441 * in clear_inode() and friends..
443 if (gfp_mask
& __GFP_FS
)
446 return inodes_stat
.nr_inodes
;
450 * Called with the inode lock held.
451 * NOTE: we are not increasing the inode-refcount, you must call __iget()
452 * by hand after calling find_inode now! This simplifies iunique and won't
453 * add any additional branch in the common code.
455 static struct inode
* find_inode(struct super_block
* sb
, struct list_head
*head
, int (*test
)(struct inode
*, void *), void *data
)
457 struct list_head
*tmp
;
458 struct inode
* inode
;
466 inode
= list_entry(tmp
, struct inode
, i_hash
);
467 if (inode
->i_sb
!= sb
)
469 if (!test(inode
, data
))
477 * find_inode_fast is the fast path version of find_inode, see the comment at
478 * iget_locked for details.
480 static struct inode
* find_inode_fast(struct super_block
* sb
, struct list_head
*head
, unsigned long ino
)
482 struct list_head
*tmp
;
483 struct inode
* inode
;
491 inode
= list_entry(tmp
, struct inode
, i_hash
);
492 if (inode
->i_ino
!= ino
)
494 if (inode
->i_sb
!= sb
)
502 * new_inode - obtain an inode
505 * Allocates a new inode for given superblock.
508 struct inode
*new_inode(struct super_block
*sb
)
510 static unsigned long last_ino
;
511 struct inode
* inode
;
513 spin_lock_prefetch(&inode_lock
);
515 inode
= alloc_inode(sb
);
517 spin_lock(&inode_lock
);
518 inodes_stat
.nr_inodes
++;
519 list_add(&inode
->i_list
, &inode_in_use
);
520 inode
->i_ino
= ++last_ino
;
522 spin_unlock(&inode_lock
);
527 void unlock_new_inode(struct inode
*inode
)
530 * This is special! We do not need the spinlock
531 * when clearing I_LOCK, because we're guaranteed
532 * that nobody else tries to do anything about the
533 * state of the inode when it is locked, as we
534 * just created it (so there can be no old holders
535 * that haven't tested I_LOCK).
537 inode
->i_state
&= ~(I_LOCK
|I_NEW
);
538 wake_up_inode(inode
);
540 EXPORT_SYMBOL(unlock_new_inode
);
543 * This is called without the inode lock held.. Be careful.
545 * We no longer cache the sb_flags in i_flags - see fs.h
546 * -- rmk@arm.uk.linux.org
548 static struct inode
* get_new_inode(struct super_block
*sb
, struct list_head
*head
, int (*test
)(struct inode
*, void *), int (*set
)(struct inode
*, void *), void *data
)
550 struct inode
* inode
;
552 inode
= alloc_inode(sb
);
556 spin_lock(&inode_lock
);
557 /* We released the lock, so.. */
558 old
= find_inode(sb
, head
, test
, data
);
560 if (set(inode
, data
))
563 inodes_stat
.nr_inodes
++;
564 list_add(&inode
->i_list
, &inode_in_use
);
565 list_add(&inode
->i_hash
, head
);
566 inode
->i_state
= I_LOCK
|I_NEW
;
567 spin_unlock(&inode_lock
);
569 /* Return the locked inode with I_NEW set, the
570 * caller is responsible for filling in the contents
576 * Uhhuh, somebody else created the same inode under
577 * us. Use the old inode instead of the one we just
581 spin_unlock(&inode_lock
);
582 destroy_inode(inode
);
584 wait_on_inode(inode
);
589 spin_unlock(&inode_lock
);
590 destroy_inode(inode
);
595 * get_new_inode_fast is the fast path version of get_new_inode, see the
596 * comment at iget_locked for details.
598 static struct inode
* get_new_inode_fast(struct super_block
*sb
, struct list_head
*head
, unsigned long ino
)
600 struct inode
* inode
;
602 inode
= alloc_inode(sb
);
606 spin_lock(&inode_lock
);
607 /* We released the lock, so.. */
608 old
= find_inode_fast(sb
, head
, ino
);
611 inodes_stat
.nr_inodes
++;
612 list_add(&inode
->i_list
, &inode_in_use
);
613 list_add(&inode
->i_hash
, head
);
614 inode
->i_state
= I_LOCK
|I_NEW
;
615 spin_unlock(&inode_lock
);
617 /* Return the locked inode with I_NEW set, the
618 * caller is responsible for filling in the contents
624 * Uhhuh, somebody else created the same inode under
625 * us. Use the old inode instead of the one we just
629 spin_unlock(&inode_lock
);
630 destroy_inode(inode
);
632 wait_on_inode(inode
);
637 static inline unsigned long hash(struct super_block
*sb
, unsigned long hashval
)
639 unsigned long tmp
= hashval
+ ((unsigned long) sb
/ L1_CACHE_BYTES
);
640 tmp
= tmp
+ (tmp
>> I_HASHBITS
);
641 return tmp
& I_HASHMASK
;
644 /* Yeah, I know about quadratic hash. Maybe, later. */
647 * iunique - get a unique inode number
649 * @max_reserved: highest reserved inode number
651 * Obtain an inode number that is unique on the system for a given
652 * superblock. This is used by file systems that have no natural
653 * permanent inode numbering system. An inode number is returned that
654 * is higher than the reserved limit but unique.
657 * With a large number of inodes live on the file system this function
658 * currently becomes quite slow.
661 ino_t
iunique(struct super_block
*sb
, ino_t max_reserved
)
663 static ino_t counter
= 0;
665 struct list_head
* head
;
667 spin_lock(&inode_lock
);
669 if (counter
> max_reserved
) {
670 head
= inode_hashtable
+ hash(sb
,counter
);
672 inode
= find_inode_fast(sb
, head
, res
);
674 spin_unlock(&inode_lock
);
678 counter
= max_reserved
+ 1;
684 struct inode
*igrab(struct inode
*inode
)
686 spin_lock(&inode_lock
);
687 if (!(inode
->i_state
& I_FREEING
))
691 * Handle the case where s_op->clear_inode is not been
692 * called yet, and somebody is calling igrab
693 * while the inode is getting freed.
696 spin_unlock(&inode_lock
);
701 * ifind - internal function, you want ilookup5() or iget5().
702 * @sb: super block of file system to search
703 * @hashval: hash value (usually inode number) to search for
704 * @test: callback used for comparisons between inodes
705 * @data: opaque data pointer to pass to @test
707 * ifind() searches for the inode specified by @hashval and @data in the inode
708 * cache. This is a generalized version of ifind_fast() for file systems where
709 * the inode number is not sufficient for unique identification of an inode.
711 * If the inode is in the cache, the inode is returned with an incremented
714 * Otherwise NULL is returned.
716 * Note, @test is called with the inode_lock held, so can't sleep.
718 static inline struct inode
*ifind(struct super_block
*sb
,
719 struct list_head
*head
, int (*test
)(struct inode
*, void *),
724 spin_lock(&inode_lock
);
725 inode
= find_inode(sb
, head
, test
, data
);
728 spin_unlock(&inode_lock
);
729 wait_on_inode(inode
);
732 spin_unlock(&inode_lock
);
737 * ifind_fast - internal function, you want ilookup() or iget().
738 * @sb: super block of file system to search
739 * @ino: inode number to search for
741 * ifind_fast() searches for the inode @ino in the inode cache. This is for
742 * file systems where the inode number is sufficient for unique identification
745 * If the inode is in the cache, the inode is returned with an incremented
748 * Otherwise NULL is returned.
750 static inline struct inode
*ifind_fast(struct super_block
*sb
,
751 struct list_head
*head
, unsigned long ino
)
755 spin_lock(&inode_lock
);
756 inode
= find_inode_fast(sb
, head
, ino
);
759 spin_unlock(&inode_lock
);
760 wait_on_inode(inode
);
763 spin_unlock(&inode_lock
);
768 * ilookup5 - search for an inode in the inode cache
769 * @sb: super block of file system to search
770 * @hashval: hash value (usually inode number) to search for
771 * @test: callback used for comparisons between inodes
772 * @data: opaque data pointer to pass to @test
774 * ilookup5() uses ifind() to search for the inode specified by @hashval and
775 * @data in the inode cache. This is a generalized version of ilookup() for
776 * file systems where the inode number is not sufficient for unique
777 * identification of an inode.
779 * If the inode is in the cache, the inode is returned with an incremented
782 * Otherwise NULL is returned.
784 * Note, @test is called with the inode_lock held, so can't sleep.
786 struct inode
*ilookup5(struct super_block
*sb
, unsigned long hashval
,
787 int (*test
)(struct inode
*, void *), void *data
)
789 struct list_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
791 return ifind(sb
, head
, test
, data
);
793 EXPORT_SYMBOL(ilookup5
);
796 * ilookup - search for an inode in the inode cache
797 * @sb: super block of file system to search
798 * @ino: inode number to search for
800 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
801 * This is for file systems where the inode number is sufficient for unique
802 * identification of an inode.
804 * If the inode is in the cache, the inode is returned with an incremented
807 * Otherwise NULL is returned.
809 struct inode
*ilookup(struct super_block
*sb
, unsigned long ino
)
811 struct list_head
*head
= inode_hashtable
+ hash(sb
, ino
);
813 return ifind_fast(sb
, head
, ino
);
815 EXPORT_SYMBOL(ilookup
);
818 * iget5_locked - obtain an inode from a mounted file system
819 * @sb: super block of file system
820 * @hashval: hash value (usually inode number) to get
821 * @test: callback used for comparisons between inodes
822 * @set: callback used to initialize a new struct inode
823 * @data: opaque data pointer to pass to @test and @set
825 * This is iget() without the read_inode() portion of get_new_inode().
827 * iget5_locked() uses ifind() to search for the inode specified by @hashval
828 * and @data in the inode cache and if present it is returned with an increased
829 * reference count. This is a generalized version of iget_locked() for file
830 * systems where the inode number is not sufficient for unique identification
833 * If the inode is not in cache, get_new_inode() is called to allocate a new
834 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
835 * file system gets to fill it in before unlocking it via unlock_new_inode().
837 * Note both @test and @set are called with the inode_lock held, so can't sleep.
839 struct inode
*iget5_locked(struct super_block
*sb
, unsigned long hashval
,
840 int (*test
)(struct inode
*, void *),
841 int (*set
)(struct inode
*, void *), void *data
)
843 struct list_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
846 inode
= ifind(sb
, head
, test
, data
);
850 * get_new_inode() will do the right thing, re-trying the search
851 * in case it had to block at any point.
853 return get_new_inode(sb
, head
, test
, set
, data
);
855 EXPORT_SYMBOL(iget5_locked
);
858 * iget_locked - obtain an inode from a mounted file system
859 * @sb: super block of file system
860 * @ino: inode number to get
862 * This is iget() without the read_inode() portion of get_new_inode_fast().
864 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
865 * the inode cache and if present it is returned with an increased reference
866 * count. This is for file systems where the inode number is sufficient for
867 * unique identification of an inode.
869 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
870 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
871 * The file system gets to fill it in before unlocking it via
872 * unlock_new_inode().
874 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
876 struct list_head
*head
= inode_hashtable
+ hash(sb
, ino
);
879 inode
= ifind_fast(sb
, head
, ino
);
883 * get_new_inode_fast() will do the right thing, re-trying the search
884 * in case it had to block at any point.
886 return get_new_inode_fast(sb
, head
, ino
);
888 EXPORT_SYMBOL(iget_locked
);
891 * __insert_inode_hash - hash an inode
892 * @inode: unhashed inode
893 * @hashval: unsigned long value used to locate this object in the
896 * Add an inode to the inode hash for this superblock. If the inode
897 * has no superblock it is added to a separate anonymous chain.
900 void __insert_inode_hash(struct inode
*inode
, unsigned long hashval
)
902 struct list_head
*head
= &anon_hash_chain
;
904 head
= inode_hashtable
+ hash(inode
->i_sb
, hashval
);
905 spin_lock(&inode_lock
);
906 list_add(&inode
->i_hash
, head
);
907 spin_unlock(&inode_lock
);
911 * remove_inode_hash - remove an inode from the hash
912 * @inode: inode to unhash
914 * Remove an inode from the superblock or anonymous hash.
917 void remove_inode_hash(struct inode
*inode
)
919 spin_lock(&inode_lock
);
920 list_del_init(&inode
->i_hash
);
921 spin_unlock(&inode_lock
);
924 void generic_delete_inode(struct inode
*inode
)
926 struct super_operations
*op
= inode
->i_sb
->s_op
;
928 list_del_init(&inode
->i_hash
);
929 list_del_init(&inode
->i_list
);
930 inode
->i_state
|=I_FREEING
;
931 inodes_stat
.nr_inodes
--;
932 spin_unlock(&inode_lock
);
934 if (inode
->i_data
.nrpages
)
935 truncate_inode_pages(&inode
->i_data
, 0);
937 security_ops
->inode_delete(inode
);
939 if (op
&& op
->delete_inode
) {
940 void (*delete)(struct inode
*) = op
->delete_inode
;
941 if (!is_bad_inode(inode
))
943 /* s_op->delete_inode internally recalls clear_inode() */
947 if (inode
->i_state
!= I_CLEAR
)
949 destroy_inode(inode
);
951 EXPORT_SYMBOL(generic_delete_inode
);
953 static void generic_forget_inode(struct inode
*inode
)
955 struct super_block
*sb
= inode
->i_sb
;
957 if (!list_empty(&inode
->i_hash
)) {
958 if (!(inode
->i_state
& (I_DIRTY
|I_LOCK
))) {
959 list_del(&inode
->i_list
);
960 list_add(&inode
->i_list
, &inode_unused
);
962 inodes_stat
.nr_unused
++;
963 spin_unlock(&inode_lock
);
964 if (!sb
|| (sb
->s_flags
& MS_ACTIVE
))
966 write_inode_now(inode
, 1);
967 spin_lock(&inode_lock
);
968 inodes_stat
.nr_unused
--;
969 list_del_init(&inode
->i_hash
);
971 list_del_init(&inode
->i_list
);
972 inode
->i_state
|=I_FREEING
;
973 inodes_stat
.nr_inodes
--;
974 spin_unlock(&inode_lock
);
975 if (inode
->i_data
.nrpages
)
976 truncate_inode_pages(&inode
->i_data
, 0);
978 destroy_inode(inode
);
982 * Normal UNIX filesystem behaviour: delete the
983 * inode when the usage count drops to zero, and
986 static void generic_drop_inode(struct inode
*inode
)
989 generic_delete_inode(inode
);
991 generic_forget_inode(inode
);
995 * Called when we're dropping the last reference
998 * Call the FS "drop()" function, defaulting to
999 * the legacy UNIX filesystem behaviour..
1001 * NOTE! NOTE! NOTE! We're called with the inode lock
1002 * held, and the drop function is supposed to release
1005 static inline void iput_final(struct inode
*inode
)
1007 struct super_operations
*op
= inode
->i_sb
->s_op
;
1008 void (*drop
)(struct inode
*) = generic_drop_inode
;
1010 if (op
&& op
->drop_inode
)
1011 drop
= op
->drop_inode
;
1016 * iput - put an inode
1017 * @inode: inode to put
1019 * Puts an inode, dropping its usage count. If the inode use count hits
1020 * zero the inode is also then freed and may be destroyed.
1023 void iput(struct inode
*inode
)
1026 struct super_operations
*op
= inode
->i_sb
->s_op
;
1028 if (inode
->i_state
== I_CLEAR
)
1031 if (op
&& op
->put_inode
)
1032 op
->put_inode(inode
);
1034 if (atomic_dec_and_lock(&inode
->i_count
, &inode_lock
))
1040 * bmap - find a block number in a file
1041 * @inode: inode of file
1042 * @block: block to find
1044 * Returns the block number on the device holding the inode that
1045 * is the disk block number for the block of the file requested.
1046 * That is, asked for block 4 of inode 1 the function will return the
1047 * disk block relative to the disk start that holds that block of the
1051 sector_t
bmap(struct inode
* inode
, sector_t block
)
1054 if (inode
->i_mapping
->a_ops
->bmap
)
1055 res
= inode
->i_mapping
->a_ops
->bmap(inode
->i_mapping
, block
);
1060 * update_atime - update the access time
1061 * @inode: inode accessed
1063 * Update the accessed time on an inode and mark it for writeback.
1064 * This function automatically handles read only file systems and media,
1065 * as well as the "noatime" flag and inode specific "noatime" markers.
1068 void update_atime(struct inode
*inode
)
1070 if (inode
->i_atime
== CURRENT_TIME
)
1072 if (IS_NOATIME(inode
))
1074 if (IS_NODIRATIME(inode
) && S_ISDIR(inode
->i_mode
))
1076 if (IS_RDONLY(inode
))
1078 inode
->i_atime
= CURRENT_TIME
;
1079 mark_inode_dirty_sync(inode
);
1082 int inode_needs_sync(struct inode
*inode
)
1086 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
1090 EXPORT_SYMBOL(inode_needs_sync
);
1093 * Quota functions that want to walk the inode lists..
1097 /* Functions back in dquot.c */
1098 void put_dquot_list(struct list_head
*);
1099 int remove_inode_dquot_ref(struct inode
*, int, struct list_head
*);
1101 void remove_dquot_ref(struct super_block
*sb
, int type
)
1103 struct inode
*inode
;
1104 struct list_head
*act_head
;
1105 LIST_HEAD(tofree_head
);
1108 return; /* nothing to do */
1109 /* We have to be protected against other CPUs */
1110 lock_kernel(); /* This lock is for quota code */
1111 spin_lock(&inode_lock
); /* This lock is for inodes code */
1113 list_for_each(act_head
, &inode_in_use
) {
1114 inode
= list_entry(act_head
, struct inode
, i_list
);
1115 if (inode
->i_sb
== sb
&& IS_QUOTAINIT(inode
))
1116 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1118 list_for_each(act_head
, &inode_unused
) {
1119 inode
= list_entry(act_head
, struct inode
, i_list
);
1120 if (inode
->i_sb
== sb
&& IS_QUOTAINIT(inode
))
1121 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1123 list_for_each(act_head
, &sb
->s_dirty
) {
1124 inode
= list_entry(act_head
, struct inode
, i_list
);
1125 if (IS_QUOTAINIT(inode
))
1126 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1128 list_for_each(act_head
, &sb
->s_io
) {
1129 inode
= list_entry(act_head
, struct inode
, i_list
);
1130 if (IS_QUOTAINIT(inode
))
1131 remove_inode_dquot_ref(inode
, type
, &tofree_head
);
1133 spin_unlock(&inode_lock
);
1136 put_dquot_list(&tofree_head
);
1142 * Hashed waitqueues for wait_on_inode(). The table is pretty small - the
1143 * kernel doesn't lock many inodes at the same time.
1145 #define I_WAIT_TABLE_ORDER 3
1146 static struct i_wait_queue_head
{
1147 wait_queue_head_t wqh
;
1148 } ____cacheline_aligned_in_smp i_wait_queue_heads
[1<<I_WAIT_TABLE_ORDER
];
1151 * Return the address of the waitqueue_head to be used for this inode
1153 static wait_queue_head_t
*i_waitq_head(struct inode
*inode
)
1155 return &i_wait_queue_heads
[hash_ptr(inode
, I_WAIT_TABLE_ORDER
)].wqh
;
1158 void __wait_on_inode(struct inode
*inode
)
1160 DECLARE_WAITQUEUE(wait
, current
);
1161 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1163 add_wait_queue(wq
, &wait
);
1165 set_current_state(TASK_UNINTERRUPTIBLE
);
1166 if (inode
->i_state
& I_LOCK
) {
1170 remove_wait_queue(wq
, &wait
);
1171 current
->state
= TASK_RUNNING
;
1174 void wake_up_inode(struct inode
*inode
)
1176 wait_queue_head_t
*wq
= i_waitq_head(inode
);
1179 * Prevent speculative execution through spin_unlock(&inode_lock);
1182 if (waitqueue_active(wq
))
1187 * Initialize the waitqueues and inode hash table.
1189 void __init
inode_init(unsigned long mempages
)
1191 struct list_head
*head
;
1192 unsigned long order
;
1193 unsigned int nr_hash
;
1196 for (i
= 0; i
< ARRAY_SIZE(i_wait_queue_heads
); i
++)
1197 init_waitqueue_head(&i_wait_queue_heads
[i
].wqh
);
1199 mempages
>>= (14 - PAGE_SHIFT
);
1200 mempages
*= sizeof(struct list_head
);
1201 for (order
= 0; ((1UL << order
) << PAGE_SHIFT
) < mempages
; order
++)
1207 nr_hash
= (1UL << order
) * PAGE_SIZE
/
1208 sizeof(struct list_head
);
1209 i_hash_mask
= (nr_hash
- 1);
1213 while ((tmp
>>= 1UL) != 0UL)
1216 inode_hashtable
= (struct list_head
*)
1217 __get_free_pages(GFP_ATOMIC
, order
);
1218 } while (inode_hashtable
== NULL
&& --order
>= 0);
1220 printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1221 nr_hash
, order
, (PAGE_SIZE
<< order
));
1223 if (!inode_hashtable
)
1224 panic("Failed to allocate inode hash table\n");
1226 head
= inode_hashtable
;
1229 INIT_LIST_HEAD(head
);
1234 /* inode slab cache */
1235 inode_cachep
= kmem_cache_create("inode_cache", sizeof(struct inode
),
1236 0, SLAB_HWCACHE_ALIGN
, init_once
,
1239 panic("cannot create inode slab cache");
1241 set_shrinker(DEFAULT_SEEKS
, shrink_icache_memory
);