4 * (C) 1997 Linus Torvalds
9 #include <linux/dcache.h>
10 #include <linux/init.h>
11 #include <linux/quotaops.h>
12 #include <linux/slab.h>
13 #include <linux/writeback.h>
14 #include <linux/module.h>
15 #include <linux/backing-dev.h>
16 #include <linux/wait.h>
17 #include <linux/rwsem.h>
18 #include <linux/hash.h>
19 #include <linux/swap.h>
20 #include <linux/security.h>
21 #include <linux/ima.h>
22 #include <linux/pagemap.h>
23 #include <linux/cdev.h>
24 #include <linux/bootmem.h>
25 #include <linux/inotify.h>
26 #include <linux/fsnotify.h>
27 #include <linux/mount.h>
28 #include <linux/async.h>
29 #include <linux/posix_acl.h>
32 * This is needed for the following functions:
34 * - invalidate_inode_buffers
37 * FIXME: remove all knowledge of the buffer layer from this file
39 #include <linux/buffer_head.h>
42 * New inode.c implementation.
44 * This implementation has the basic premise of trying
45 * to be extremely low-overhead and SMP-safe, yet be
46 * simple enough to be "obviously correct".
51 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
53 /* #define INODE_PARANOIA 1 */
54 /* #define INODE_DEBUG 1 */
57 * Inode lookup is no longer as critical as it used to be:
58 * most of the lookups are going to be through the dcache.
60 #define I_HASHBITS i_hash_shift
61 #define I_HASHMASK i_hash_mask
63 static unsigned int i_hash_mask __read_mostly
;
64 static unsigned int i_hash_shift __read_mostly
;
67 * Each inode can be on two separate lists. One is
68 * the hash list of the inode, used for lookups. The
69 * other linked list is the "type" list:
70 * "in_use" - valid inode, i_count > 0, i_nlink > 0
71 * "dirty" - as "in_use" but also dirty
72 * "unused" - valid inode, i_count = 0
74 * A "dirty" list is maintained for each super block,
75 * allowing for low-overhead inode sync() operations.
78 LIST_HEAD(inode_in_use
);
79 LIST_HEAD(inode_unused
);
80 static struct hlist_head
*inode_hashtable __read_mostly
;
83 * A simple spinlock to protect the list manipulations.
85 * NOTE! You also have to own the lock if you change
86 * the i_state of an inode while it is in use..
88 DEFINE_SPINLOCK(inode_lock
);
91 * iprune_sem provides exclusion between the kswapd or try_to_free_pages
92 * icache shrinking path, and the umount path. Without this exclusion,
93 * by the time prune_icache calls iput for the inode whose pages it has
94 * been invalidating, or by the time it calls clear_inode & destroy_inode
95 * from its final dispose_list, the struct super_block they refer to
96 * (for inode->i_sb->s_op) may already have been freed and reused.
98 * We make this an rwsem because the fastpath is icache shrinking. In
99 * some cases a filesystem may be doing a significant amount of work in
100 * its inode reclaim code, so this should improve parallelism.
102 static DECLARE_RWSEM(iprune_sem
);
105 * Statistics gathering..
107 struct inodes_stat_t inodes_stat
;
109 static struct kmem_cache
*inode_cachep __read_mostly
;
111 static void wake_up_inode(struct inode
*inode
)
114 * Prevent speculative execution through spin_unlock(&inode_lock);
117 wake_up_bit(&inode
->i_state
, __I_LOCK
);
121 * inode_init_always - perform inode structure intialisation
122 * @sb: superblock inode belongs to
123 * @inode: inode to initialise
125 * These are initializations that need to be done on every inode
126 * allocation as the fields are not initialised by slab allocation.
128 int inode_init_always(struct super_block
*sb
, struct inode
*inode
)
130 static const struct address_space_operations empty_aops
;
131 static const struct inode_operations empty_iops
;
132 static const struct file_operations empty_fops
;
133 struct address_space
*const mapping
= &inode
->i_data
;
136 inode
->i_blkbits
= sb
->s_blocksize_bits
;
138 atomic_set(&inode
->i_count
, 1);
139 inode
->i_op
= &empty_iops
;
140 inode
->i_fop
= &empty_fops
;
144 atomic_set(&inode
->i_writecount
, 0);
148 inode
->i_generation
= 0;
150 memset(&inode
->i_dquot
, 0, sizeof(inode
->i_dquot
));
152 inode
->i_pipe
= NULL
;
153 inode
->i_bdev
= NULL
;
154 inode
->i_cdev
= NULL
;
156 inode
->dirtied_when
= 0;
158 if (security_inode_alloc(inode
))
161 /* allocate and initialize an i_integrity */
162 if (ima_inode_alloc(inode
))
163 goto out_free_security
;
165 spin_lock_init(&inode
->i_lock
);
166 lockdep_set_class(&inode
->i_lock
, &sb
->s_type
->i_lock_key
);
168 mutex_init(&inode
->i_mutex
);
169 lockdep_set_class(&inode
->i_mutex
, &sb
->s_type
->i_mutex_key
);
171 init_rwsem(&inode
->i_alloc_sem
);
172 lockdep_set_class(&inode
->i_alloc_sem
, &sb
->s_type
->i_alloc_sem_key
);
174 mapping
->a_ops
= &empty_aops
;
175 mapping
->host
= inode
;
177 mapping_set_gfp_mask(mapping
, GFP_HIGHUSER_MOVABLE
);
178 mapping
->assoc_mapping
= NULL
;
179 mapping
->backing_dev_info
= &default_backing_dev_info
;
180 mapping
->writeback_index
= 0;
183 * If the block_device provides a backing_dev_info for client
184 * inodes then use that. Otherwise the inode share the bdev's
188 struct backing_dev_info
*bdi
;
190 bdi
= sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
191 mapping
->backing_dev_info
= bdi
;
193 inode
->i_private
= NULL
;
194 inode
->i_mapping
= mapping
;
195 #ifdef CONFIG_FS_POSIX_ACL
196 inode
->i_acl
= inode
->i_default_acl
= ACL_NOT_CACHED
;
199 #ifdef CONFIG_FSNOTIFY
200 inode
->i_fsnotify_mask
= 0;
206 security_inode_free(inode
);
210 EXPORT_SYMBOL(inode_init_always
);
212 static struct inode
*alloc_inode(struct super_block
*sb
)
216 if (sb
->s_op
->alloc_inode
)
217 inode
= sb
->s_op
->alloc_inode(sb
);
219 inode
= kmem_cache_alloc(inode_cachep
, GFP_KERNEL
);
224 if (unlikely(inode_init_always(sb
, inode
))) {
225 if (inode
->i_sb
->s_op
->destroy_inode
)
226 inode
->i_sb
->s_op
->destroy_inode(inode
);
228 kmem_cache_free(inode_cachep
, inode
);
235 void __destroy_inode(struct inode
*inode
)
237 BUG_ON(inode_has_buffers(inode
));
238 ima_inode_free(inode
);
239 security_inode_free(inode
);
240 fsnotify_inode_delete(inode
);
241 #ifdef CONFIG_FS_POSIX_ACL
242 if (inode
->i_acl
&& inode
->i_acl
!= ACL_NOT_CACHED
)
243 posix_acl_release(inode
->i_acl
);
244 if (inode
->i_default_acl
&& inode
->i_default_acl
!= ACL_NOT_CACHED
)
245 posix_acl_release(inode
->i_default_acl
);
248 EXPORT_SYMBOL(__destroy_inode
);
250 void destroy_inode(struct inode
*inode
)
252 __destroy_inode(inode
);
253 if (inode
->i_sb
->s_op
->destroy_inode
)
254 inode
->i_sb
->s_op
->destroy_inode(inode
);
256 kmem_cache_free(inode_cachep
, (inode
));
259 void address_space_init_once(struct address_space
*mapping
)
261 memset(mapping
, 0, sizeof(*mapping
));
262 INIT_RADIX_TREE(&mapping
->page_tree
, GFP_ATOMIC
);
263 spin_lock_init(&mapping
->tree_lock
);
264 spin_lock_init(&mapping
->i_mmap_lock
);
265 INIT_LIST_HEAD(&mapping
->private_list
);
266 spin_lock_init(&mapping
->private_lock
);
267 INIT_RAW_PRIO_TREE_ROOT(&mapping
->i_mmap
);
268 INIT_LIST_HEAD(&mapping
->i_mmap_nonlinear
);
269 mutex_init(&mapping
->unmap_mutex
);
271 EXPORT_SYMBOL(address_space_init_once
);
274 * These are initializations that only need to be done
275 * once, because the fields are idempotent across use
276 * of the inode, so let the slab aware of that.
278 void inode_init_once(struct inode
*inode
)
280 memset(inode
, 0, sizeof(*inode
));
281 INIT_HLIST_NODE(&inode
->i_hash
);
282 INIT_LIST_HEAD(&inode
->i_dentry
);
283 INIT_LIST_HEAD(&inode
->i_devices
);
284 address_space_init_once(&inode
->i_data
);
285 i_size_ordered_init(inode
);
286 #ifdef CONFIG_INOTIFY
287 INIT_LIST_HEAD(&inode
->inotify_watches
);
288 mutex_init(&inode
->inotify_mutex
);
290 #ifdef CONFIG_FSNOTIFY
291 INIT_HLIST_HEAD(&inode
->i_fsnotify_mark_entries
);
294 EXPORT_SYMBOL(inode_init_once
);
296 static void init_once(void *foo
)
298 struct inode
*inode
= (struct inode
*) foo
;
300 inode_init_once(inode
);
304 * inode_lock must be held
306 void __iget(struct inode
*inode
)
308 if (atomic_read(&inode
->i_count
)) {
309 atomic_inc(&inode
->i_count
);
312 atomic_inc(&inode
->i_count
);
313 if (!(inode
->i_state
& (I_DIRTY
|I_SYNC
)))
314 list_move(&inode
->i_list
, &inode_in_use
);
315 inodes_stat
.nr_unused
--;
319 * clear_inode - clear an inode
320 * @inode: inode to clear
322 * This is called by the filesystem to tell us
323 * that the inode is no longer useful. We just
324 * terminate it with extreme prejudice.
326 void clear_inode(struct inode
*inode
)
329 invalidate_inode_buffers(inode
);
331 BUG_ON(inode
->i_data
.nrpages
);
332 BUG_ON(!(inode
->i_state
& I_FREEING
));
333 BUG_ON(inode
->i_state
& I_CLEAR
);
334 inode_sync_wait(inode
);
336 if (inode
->i_sb
->s_op
->clear_inode
)
337 inode
->i_sb
->s_op
->clear_inode(inode
);
338 if (S_ISBLK(inode
->i_mode
) && inode
->i_bdev
)
340 if (S_ISCHR(inode
->i_mode
) && inode
->i_cdev
)
342 inode
->i_state
= I_CLEAR
;
344 EXPORT_SYMBOL(clear_inode
);
347 * dispose_list - dispose of the contents of a local list
348 * @head: the head of the list to free
350 * Dispose-list gets a local list with local inodes in it, so it doesn't
351 * need to worry about list corruption and SMP locks.
353 static void dispose_list(struct list_head
*head
)
357 while (!list_empty(head
)) {
360 inode
= list_first_entry(head
, struct inode
, i_list
);
361 list_del(&inode
->i_list
);
363 if (inode
->i_data
.nrpages
)
364 truncate_inode_pages(&inode
->i_data
, 0);
367 spin_lock(&inode_lock
);
368 hlist_del_init(&inode
->i_hash
);
369 list_del_init(&inode
->i_sb_list
);
370 spin_unlock(&inode_lock
);
372 wake_up_inode(inode
);
373 destroy_inode(inode
);
376 spin_lock(&inode_lock
);
377 inodes_stat
.nr_inodes
-= nr_disposed
;
378 spin_unlock(&inode_lock
);
382 * Invalidate all inodes for a device.
384 static int invalidate_list(struct list_head
*head
, struct list_head
*dispose
)
386 struct list_head
*next
;
387 int busy
= 0, count
= 0;
391 struct list_head
*tmp
= next
;
395 * We can reschedule here without worrying about the list's
396 * consistency because the per-sb list of inodes must not
397 * change during umount anymore, and because iprune_sem keeps
398 * shrink_icache_memory() away.
400 cond_resched_lock(&inode_lock
);
405 inode
= list_entry(tmp
, struct inode
, i_sb_list
);
406 if (inode
->i_state
& I_NEW
)
408 invalidate_inode_buffers(inode
);
409 if (!atomic_read(&inode
->i_count
)) {
410 list_move(&inode
->i_list
, dispose
);
411 WARN_ON(inode
->i_state
& I_NEW
);
412 inode
->i_state
|= I_FREEING
;
418 /* only unused inodes may be cached with i_count zero */
419 inodes_stat
.nr_unused
-= count
;
424 * invalidate_inodes - discard the inodes on a device
427 * Discard all of the inodes for a given superblock. If the discard
428 * fails because there are busy inodes then a non zero value is returned.
429 * If the discard is successful all the inodes have been discarded.
431 int invalidate_inodes(struct super_block
*sb
)
434 LIST_HEAD(throw_away
);
436 down_write(&iprune_sem
);
437 spin_lock(&inode_lock
);
438 inotify_unmount_inodes(&sb
->s_inodes
);
439 fsnotify_unmount_inodes(&sb
->s_inodes
);
440 busy
= invalidate_list(&sb
->s_inodes
, &throw_away
);
441 spin_unlock(&inode_lock
);
443 dispose_list(&throw_away
);
444 up_write(&iprune_sem
);
448 EXPORT_SYMBOL(invalidate_inodes
);
450 static int can_unuse(struct inode
*inode
)
454 if (inode_has_buffers(inode
))
456 if (atomic_read(&inode
->i_count
))
458 if (inode
->i_data
.nrpages
)
464 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
465 * a temporary list and then are freed outside inode_lock by dispose_list().
467 * Any inodes which are pinned purely because of attached pagecache have their
468 * pagecache removed. We expect the final iput() on that inode to add it to
469 * the front of the inode_unused list. So look for it there and if the
470 * inode is still freeable, proceed. The right inode is found 99.9% of the
471 * time in testing on a 4-way.
473 * If the inode has metadata buffers attached to mapping->private_list then
474 * try to remove them.
476 static void prune_icache(int nr_to_scan
)
481 unsigned long reap
= 0;
483 down_read(&iprune_sem
);
484 spin_lock(&inode_lock
);
485 for (nr_scanned
= 0; nr_scanned
< nr_to_scan
; nr_scanned
++) {
488 if (list_empty(&inode_unused
))
491 inode
= list_entry(inode_unused
.prev
, struct inode
, i_list
);
493 if (inode
->i_state
|| atomic_read(&inode
->i_count
)) {
494 list_move(&inode
->i_list
, &inode_unused
);
497 if (inode_has_buffers(inode
) || inode
->i_data
.nrpages
) {
499 spin_unlock(&inode_lock
);
500 if (remove_inode_buffers(inode
))
501 reap
+= invalidate_mapping_pages(&inode
->i_data
,
504 spin_lock(&inode_lock
);
506 if (inode
!= list_entry(inode_unused
.next
,
507 struct inode
, i_list
))
508 continue; /* wrong inode or list_empty */
509 if (!can_unuse(inode
))
512 list_move(&inode
->i_list
, &freeable
);
513 WARN_ON(inode
->i_state
& I_NEW
);
514 inode
->i_state
|= I_FREEING
;
517 inodes_stat
.nr_unused
-= nr_pruned
;
518 if (current_is_kswapd())
519 __count_vm_events(KSWAPD_INODESTEAL
, reap
);
521 __count_vm_events(PGINODESTEAL
, reap
);
522 spin_unlock(&inode_lock
);
524 dispose_list(&freeable
);
525 up_read(&iprune_sem
);
529 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
530 * "unused" means that no dentries are referring to the inodes: the files are
531 * not open and the dcache references to those inodes have already been
534 * This function is passed the number of inodes to scan, and it returns the
535 * total number of remaining possibly-reclaimable inodes.
537 static int shrink_icache_memory(int nr
, gfp_t gfp_mask
)
541 * Nasty deadlock avoidance. We may hold various FS locks,
542 * and we don't want to recurse into the FS that called us
543 * in clear_inode() and friends..
545 if (!(gfp_mask
& __GFP_FS
))
549 return (inodes_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
552 static struct shrinker icache_shrinker
= {
553 .shrink
= shrink_icache_memory
,
554 .seeks
= DEFAULT_SEEKS
,
557 static void __wait_on_freeing_inode(struct inode
*inode
);
559 * Called with the inode lock held.
560 * NOTE: we are not increasing the inode-refcount, you must call __iget()
561 * by hand after calling find_inode now! This simplifies iunique and won't
562 * add any additional branch in the common code.
564 static struct inode
*find_inode(struct super_block
*sb
,
565 struct hlist_head
*head
,
566 int (*test
)(struct inode
*, void *),
569 struct hlist_node
*node
;
570 struct inode
*inode
= NULL
;
573 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
574 if (inode
->i_sb
!= sb
)
576 if (!test(inode
, data
))
578 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
)) {
579 __wait_on_freeing_inode(inode
);
584 return node
? inode
: NULL
;
588 * find_inode_fast is the fast path version of find_inode, see the comment at
589 * iget_locked for details.
591 static struct inode
*find_inode_fast(struct super_block
*sb
,
592 struct hlist_head
*head
, unsigned long ino
)
594 struct hlist_node
*node
;
595 struct inode
*inode
= NULL
;
598 hlist_for_each_entry(inode
, node
, head
, i_hash
) {
599 if (inode
->i_ino
!= ino
)
601 if (inode
->i_sb
!= sb
)
603 if (inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
)) {
604 __wait_on_freeing_inode(inode
);
609 return node
? inode
: NULL
;
612 static unsigned long hash(struct super_block
*sb
, unsigned long hashval
)
616 tmp
= (hashval
* (unsigned long)sb
) ^ (GOLDEN_RATIO_PRIME
+ hashval
) /
618 tmp
= tmp
^ ((tmp
^ GOLDEN_RATIO_PRIME
) >> I_HASHBITS
);
619 return tmp
& I_HASHMASK
;
623 __inode_add_to_lists(struct super_block
*sb
, struct hlist_head
*head
,
626 inodes_stat
.nr_inodes
++;
627 list_add(&inode
->i_list
, &inode_in_use
);
628 list_add(&inode
->i_sb_list
, &sb
->s_inodes
);
630 hlist_add_head(&inode
->i_hash
, head
);
634 * inode_add_to_lists - add a new inode to relevant lists
635 * @sb: superblock inode belongs to
636 * @inode: inode to mark in use
638 * When an inode is allocated it needs to be accounted for, added to the in use
639 * list, the owning superblock and the inode hash. This needs to be done under
640 * the inode_lock, so export a function to do this rather than the inode lock
641 * itself. We calculate the hash list to add to here so it is all internal
642 * which requires the caller to have already set up the inode number in the
645 void inode_add_to_lists(struct super_block
*sb
, struct inode
*inode
)
647 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, inode
->i_ino
);
649 spin_lock(&inode_lock
);
650 __inode_add_to_lists(sb
, head
, inode
);
651 spin_unlock(&inode_lock
);
653 EXPORT_SYMBOL_GPL(inode_add_to_lists
);
656 * new_inode - obtain an inode
659 * Allocates a new inode for given superblock. The default gfp_mask
660 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
661 * If HIGHMEM pages are unsuitable or it is known that pages allocated
662 * for the page cache are not reclaimable or migratable,
663 * mapping_set_gfp_mask() must be called with suitable flags on the
664 * newly created inode's mapping
667 struct inode
*new_inode(struct super_block
*sb
)
670 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
671 * error if st_ino won't fit in target struct field. Use 32bit counter
672 * here to attempt to avoid that.
674 static unsigned int last_ino
;
677 spin_lock_prefetch(&inode_lock
);
679 inode
= alloc_inode(sb
);
681 spin_lock(&inode_lock
);
682 __inode_add_to_lists(sb
, NULL
, inode
);
683 inode
->i_ino
= ++last_ino
;
685 spin_unlock(&inode_lock
);
689 EXPORT_SYMBOL(new_inode
);
691 void unlock_new_inode(struct inode
*inode
)
693 #ifdef CONFIG_DEBUG_LOCK_ALLOC
694 if (inode
->i_mode
& S_IFDIR
) {
695 struct file_system_type
*type
= inode
->i_sb
->s_type
;
697 /* Set new key only if filesystem hasn't already changed it */
698 if (!lockdep_match_class(&inode
->i_mutex
,
699 &type
->i_mutex_key
)) {
701 * ensure nobody is actually holding i_mutex
703 mutex_destroy(&inode
->i_mutex
);
704 mutex_init(&inode
->i_mutex
);
705 lockdep_set_class(&inode
->i_mutex
,
706 &type
->i_mutex_dir_key
);
711 * This is special! We do not need the spinlock when clearing I_LOCK,
712 * because we're guaranteed that nobody else tries to do anything about
713 * the state of the inode when it is locked, as we just created it (so
714 * there can be no old holders that haven't tested I_LOCK).
715 * However we must emit the memory barrier so that other CPUs reliably
716 * see the clearing of I_LOCK after the other inode initialisation has
720 WARN_ON((inode
->i_state
& (I_LOCK
|I_NEW
)) != (I_LOCK
|I_NEW
));
721 inode
->i_state
&= ~(I_LOCK
|I_NEW
);
722 wake_up_inode(inode
);
724 EXPORT_SYMBOL(unlock_new_inode
);
727 * This is called without the inode lock held.. Be careful.
729 * We no longer cache the sb_flags in i_flags - see fs.h
730 * -- rmk@arm.uk.linux.org
732 static struct inode
*get_new_inode(struct super_block
*sb
,
733 struct hlist_head
*head
,
734 int (*test
)(struct inode
*, void *),
735 int (*set
)(struct inode
*, void *),
740 inode
= alloc_inode(sb
);
744 spin_lock(&inode_lock
);
745 /* We released the lock, so.. */
746 old
= find_inode(sb
, head
, test
, data
);
748 if (set(inode
, data
))
751 __inode_add_to_lists(sb
, head
, inode
);
752 inode
->i_state
= I_LOCK
|I_NEW
;
753 spin_unlock(&inode_lock
);
755 /* Return the locked inode with I_NEW set, the
756 * caller is responsible for filling in the contents
762 * Uhhuh, somebody else created the same inode under
763 * us. Use the old inode instead of the one we just
767 spin_unlock(&inode_lock
);
768 destroy_inode(inode
);
770 wait_on_inode(inode
);
775 spin_unlock(&inode_lock
);
776 destroy_inode(inode
);
781 * get_new_inode_fast is the fast path version of get_new_inode, see the
782 * comment at iget_locked for details.
784 static struct inode
*get_new_inode_fast(struct super_block
*sb
,
785 struct hlist_head
*head
, unsigned long ino
)
789 inode
= alloc_inode(sb
);
793 spin_lock(&inode_lock
);
794 /* We released the lock, so.. */
795 old
= find_inode_fast(sb
, head
, ino
);
798 __inode_add_to_lists(sb
, head
, inode
);
799 inode
->i_state
= I_LOCK
|I_NEW
;
800 spin_unlock(&inode_lock
);
802 /* Return the locked inode with I_NEW set, the
803 * caller is responsible for filling in the contents
809 * Uhhuh, somebody else created the same inode under
810 * us. Use the old inode instead of the one we just
814 spin_unlock(&inode_lock
);
815 destroy_inode(inode
);
817 wait_on_inode(inode
);
823 * iunique - get a unique inode number
825 * @max_reserved: highest reserved inode number
827 * Obtain an inode number that is unique on the system for a given
828 * superblock. This is used by file systems that have no natural
829 * permanent inode numbering system. An inode number is returned that
830 * is higher than the reserved limit but unique.
833 * With a large number of inodes live on the file system this function
834 * currently becomes quite slow.
836 ino_t
iunique(struct super_block
*sb
, ino_t max_reserved
)
839 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
840 * error if st_ino won't fit in target struct field. Use 32bit counter
841 * here to attempt to avoid that.
843 static unsigned int counter
;
845 struct hlist_head
*head
;
848 spin_lock(&inode_lock
);
850 if (counter
<= max_reserved
)
851 counter
= max_reserved
+ 1;
853 head
= inode_hashtable
+ hash(sb
, res
);
854 inode
= find_inode_fast(sb
, head
, res
);
855 } while (inode
!= NULL
);
856 spin_unlock(&inode_lock
);
860 EXPORT_SYMBOL(iunique
);
862 struct inode
*igrab(struct inode
*inode
)
864 spin_lock(&inode_lock
);
865 if (!(inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
)))
869 * Handle the case where s_op->clear_inode is not been
870 * called yet, and somebody is calling igrab
871 * while the inode is getting freed.
874 spin_unlock(&inode_lock
);
877 EXPORT_SYMBOL(igrab
);
880 * ifind - internal function, you want ilookup5() or iget5().
881 * @sb: super block of file system to search
882 * @head: the head of the list to search
883 * @test: callback used for comparisons between inodes
884 * @data: opaque data pointer to pass to @test
885 * @wait: if true wait for the inode to be unlocked, if false do not
887 * ifind() searches for the inode specified by @data in the inode
888 * cache. This is a generalized version of ifind_fast() for file systems where
889 * the inode number is not sufficient for unique identification of an inode.
891 * If the inode is in the cache, the inode is returned with an incremented
894 * Otherwise NULL is returned.
896 * Note, @test is called with the inode_lock held, so can't sleep.
898 static struct inode
*ifind(struct super_block
*sb
,
899 struct hlist_head
*head
, int (*test
)(struct inode
*, void *),
900 void *data
, const int wait
)
904 spin_lock(&inode_lock
);
905 inode
= find_inode(sb
, head
, test
, data
);
908 spin_unlock(&inode_lock
);
910 wait_on_inode(inode
);
913 spin_unlock(&inode_lock
);
918 * ifind_fast - internal function, you want ilookup() or iget().
919 * @sb: super block of file system to search
920 * @head: head of the list to search
921 * @ino: inode number to search for
923 * ifind_fast() searches for the inode @ino in the inode cache. This is for
924 * file systems where the inode number is sufficient for unique identification
927 * If the inode is in the cache, the inode is returned with an incremented
930 * Otherwise NULL is returned.
932 static struct inode
*ifind_fast(struct super_block
*sb
,
933 struct hlist_head
*head
, unsigned long ino
)
937 spin_lock(&inode_lock
);
938 inode
= find_inode_fast(sb
, head
, ino
);
941 spin_unlock(&inode_lock
);
942 wait_on_inode(inode
);
945 spin_unlock(&inode_lock
);
950 * ilookup5_nowait - search for an inode in the inode cache
951 * @sb: super block of file system to search
952 * @hashval: hash value (usually inode number) to search for
953 * @test: callback used for comparisons between inodes
954 * @data: opaque data pointer to pass to @test
956 * ilookup5() uses ifind() to search for the inode specified by @hashval and
957 * @data in the inode cache. This is a generalized version of ilookup() for
958 * file systems where the inode number is not sufficient for unique
959 * identification of an inode.
961 * If the inode is in the cache, the inode is returned with an incremented
962 * reference count. Note, the inode lock is not waited upon so you have to be
963 * very careful what you do with the returned inode. You probably should be
964 * using ilookup5() instead.
966 * Otherwise NULL is returned.
968 * Note, @test is called with the inode_lock held, so can't sleep.
970 struct inode
*ilookup5_nowait(struct super_block
*sb
, unsigned long hashval
,
971 int (*test
)(struct inode
*, void *), void *data
)
973 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
975 return ifind(sb
, head
, test
, data
, 0);
977 EXPORT_SYMBOL(ilookup5_nowait
);
980 * ilookup5 - search for an inode in the inode cache
981 * @sb: super block of file system to search
982 * @hashval: hash value (usually inode number) to search for
983 * @test: callback used for comparisons between inodes
984 * @data: opaque data pointer to pass to @test
986 * ilookup5() uses ifind() to search for the inode specified by @hashval and
987 * @data in the inode cache. This is a generalized version of ilookup() for
988 * file systems where the inode number is not sufficient for unique
989 * identification of an inode.
991 * If the inode is in the cache, the inode lock is waited upon and the inode is
992 * returned with an incremented reference count.
994 * Otherwise NULL is returned.
996 * Note, @test is called with the inode_lock held, so can't sleep.
998 struct inode
*ilookup5(struct super_block
*sb
, unsigned long hashval
,
999 int (*test
)(struct inode
*, void *), void *data
)
1001 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1003 return ifind(sb
, head
, test
, data
, 1);
1005 EXPORT_SYMBOL(ilookup5
);
1008 * ilookup - search for an inode in the inode cache
1009 * @sb: super block of file system to search
1010 * @ino: inode number to search for
1012 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
1013 * This is for file systems where the inode number is sufficient for unique
1014 * identification of an inode.
1016 * If the inode is in the cache, the inode is returned with an incremented
1019 * Otherwise NULL is returned.
1021 struct inode
*ilookup(struct super_block
*sb
, unsigned long ino
)
1023 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1025 return ifind_fast(sb
, head
, ino
);
1027 EXPORT_SYMBOL(ilookup
);
1030 * iget5_locked - obtain an inode from a mounted file system
1031 * @sb: super block of file system
1032 * @hashval: hash value (usually inode number) to get
1033 * @test: callback used for comparisons between inodes
1034 * @set: callback used to initialize a new struct inode
1035 * @data: opaque data pointer to pass to @test and @set
1037 * iget5_locked() uses ifind() to search for the inode specified by @hashval
1038 * and @data in the inode cache and if present it is returned with an increased
1039 * reference count. This is a generalized version of iget_locked() for file
1040 * systems where the inode number is not sufficient for unique identification
1043 * If the inode is not in cache, get_new_inode() is called to allocate a new
1044 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1045 * file system gets to fill it in before unlocking it via unlock_new_inode().
1047 * Note both @test and @set are called with the inode_lock held, so can't sleep.
1049 struct inode
*iget5_locked(struct super_block
*sb
, unsigned long hashval
,
1050 int (*test
)(struct inode
*, void *),
1051 int (*set
)(struct inode
*, void *), void *data
)
1053 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1054 struct inode
*inode
;
1056 inode
= ifind(sb
, head
, test
, data
, 1);
1060 * get_new_inode() will do the right thing, re-trying the search
1061 * in case it had to block at any point.
1063 return get_new_inode(sb
, head
, test
, set
, data
);
1065 EXPORT_SYMBOL(iget5_locked
);
1068 * iget_locked - obtain an inode from a mounted file system
1069 * @sb: super block of file system
1070 * @ino: inode number to get
1072 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1073 * the inode cache and if present it is returned with an increased reference
1074 * count. This is for file systems where the inode number is sufficient for
1075 * unique identification of an inode.
1077 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1078 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1079 * The file system gets to fill it in before unlocking it via
1080 * unlock_new_inode().
1082 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
1084 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1085 struct inode
*inode
;
1087 inode
= ifind_fast(sb
, head
, ino
);
1091 * get_new_inode_fast() will do the right thing, re-trying the search
1092 * in case it had to block at any point.
1094 return get_new_inode_fast(sb
, head
, ino
);
1096 EXPORT_SYMBOL(iget_locked
);
1098 int insert_inode_locked(struct inode
*inode
)
1100 struct super_block
*sb
= inode
->i_sb
;
1101 ino_t ino
= inode
->i_ino
;
1102 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, ino
);
1104 inode
->i_state
|= I_LOCK
|I_NEW
;
1106 struct hlist_node
*node
;
1107 struct inode
*old
= NULL
;
1108 spin_lock(&inode_lock
);
1109 hlist_for_each_entry(old
, node
, head
, i_hash
) {
1110 if (old
->i_ino
!= ino
)
1112 if (old
->i_sb
!= sb
)
1114 if (old
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
))
1118 if (likely(!node
)) {
1119 hlist_add_head(&inode
->i_hash
, head
);
1120 spin_unlock(&inode_lock
);
1124 spin_unlock(&inode_lock
);
1126 if (unlikely(!hlist_unhashed(&old
->i_hash
))) {
1133 EXPORT_SYMBOL(insert_inode_locked
);
1135 int insert_inode_locked4(struct inode
*inode
, unsigned long hashval
,
1136 int (*test
)(struct inode
*, void *), void *data
)
1138 struct super_block
*sb
= inode
->i_sb
;
1139 struct hlist_head
*head
= inode_hashtable
+ hash(sb
, hashval
);
1141 inode
->i_state
|= I_LOCK
|I_NEW
;
1144 struct hlist_node
*node
;
1145 struct inode
*old
= NULL
;
1147 spin_lock(&inode_lock
);
1148 hlist_for_each_entry(old
, node
, head
, i_hash
) {
1149 if (old
->i_sb
!= sb
)
1151 if (!test(old
, data
))
1153 if (old
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
))
1157 if (likely(!node
)) {
1158 hlist_add_head(&inode
->i_hash
, head
);
1159 spin_unlock(&inode_lock
);
1163 spin_unlock(&inode_lock
);
1165 if (unlikely(!hlist_unhashed(&old
->i_hash
))) {
1172 EXPORT_SYMBOL(insert_inode_locked4
);
1175 * __insert_inode_hash - hash an inode
1176 * @inode: unhashed inode
1177 * @hashval: unsigned long value used to locate this object in the
1180 * Add an inode to the inode hash for this superblock.
1182 void __insert_inode_hash(struct inode
*inode
, unsigned long hashval
)
1184 struct hlist_head
*head
= inode_hashtable
+ hash(inode
->i_sb
, hashval
);
1185 spin_lock(&inode_lock
);
1186 hlist_add_head(&inode
->i_hash
, head
);
1187 spin_unlock(&inode_lock
);
1189 EXPORT_SYMBOL(__insert_inode_hash
);
1192 * remove_inode_hash - remove an inode from the hash
1193 * @inode: inode to unhash
1195 * Remove an inode from the superblock.
1197 void remove_inode_hash(struct inode
*inode
)
1199 spin_lock(&inode_lock
);
1200 hlist_del_init(&inode
->i_hash
);
1201 spin_unlock(&inode_lock
);
1203 EXPORT_SYMBOL(remove_inode_hash
);
1206 * Tell the filesystem that this inode is no longer of any interest and should
1207 * be completely destroyed.
1209 * We leave the inode in the inode hash table until *after* the filesystem's
1210 * ->delete_inode completes. This ensures that an iget (such as nfsd might
1211 * instigate) will always find up-to-date information either in the hash or on
1214 * I_FREEING is set so that no-one will take a new reference to the inode while
1215 * it is being deleted.
1217 void generic_delete_inode(struct inode
*inode
)
1219 const struct super_operations
*op
= inode
->i_sb
->s_op
;
1221 list_del_init(&inode
->i_list
);
1222 list_del_init(&inode
->i_sb_list
);
1223 WARN_ON(inode
->i_state
& I_NEW
);
1224 inode
->i_state
|= I_FREEING
;
1225 inodes_stat
.nr_inodes
--;
1226 spin_unlock(&inode_lock
);
1228 security_inode_delete(inode
);
1230 if (op
->delete_inode
) {
1231 void (*delete)(struct inode
*) = op
->delete_inode
;
1232 if (!is_bad_inode(inode
))
1234 /* Filesystems implementing their own
1235 * s_op->delete_inode are required to call
1236 * truncate_inode_pages and clear_inode()
1240 truncate_inode_pages(&inode
->i_data
, 0);
1243 spin_lock(&inode_lock
);
1244 hlist_del_init(&inode
->i_hash
);
1245 spin_unlock(&inode_lock
);
1246 wake_up_inode(inode
);
1247 BUG_ON(inode
->i_state
!= I_CLEAR
);
1248 destroy_inode(inode
);
1250 EXPORT_SYMBOL(generic_delete_inode
);
1253 * generic_detach_inode - remove inode from inode lists
1254 * @inode: inode to remove
1256 * Remove inode from inode lists, write it if it's dirty. This is just an
1257 * internal VFS helper exported for hugetlbfs. Do not use!
1259 * Returns 1 if inode should be completely destroyed.
1261 int generic_detach_inode(struct inode
*inode
)
1263 struct super_block
*sb
= inode
->i_sb
;
1265 if (!hlist_unhashed(&inode
->i_hash
)) {
1266 if (!(inode
->i_state
& (I_DIRTY
|I_SYNC
)))
1267 list_move(&inode
->i_list
, &inode_unused
);
1268 inodes_stat
.nr_unused
++;
1269 if (sb
->s_flags
& MS_ACTIVE
) {
1270 spin_unlock(&inode_lock
);
1273 WARN_ON(inode
->i_state
& I_NEW
);
1274 inode
->i_state
|= I_WILL_FREE
;
1275 spin_unlock(&inode_lock
);
1276 write_inode_now(inode
, 1);
1277 spin_lock(&inode_lock
);
1278 WARN_ON(inode
->i_state
& I_NEW
);
1279 inode
->i_state
&= ~I_WILL_FREE
;
1280 inodes_stat
.nr_unused
--;
1281 hlist_del_init(&inode
->i_hash
);
1283 list_del_init(&inode
->i_list
);
1284 list_del_init(&inode
->i_sb_list
);
1285 WARN_ON(inode
->i_state
& I_NEW
);
1286 inode
->i_state
|= I_FREEING
;
1287 inodes_stat
.nr_inodes
--;
1288 spin_unlock(&inode_lock
);
1291 EXPORT_SYMBOL_GPL(generic_detach_inode
);
1293 static void generic_forget_inode(struct inode
*inode
)
1295 if (!generic_detach_inode(inode
))
1297 if (inode
->i_data
.nrpages
)
1298 truncate_inode_pages(&inode
->i_data
, 0);
1300 wake_up_inode(inode
);
1301 destroy_inode(inode
);
1305 * Normal UNIX filesystem behaviour: delete the
1306 * inode when the usage count drops to zero, and
1309 void generic_drop_inode(struct inode
*inode
)
1311 if (!inode
->i_nlink
)
1312 generic_delete_inode(inode
);
1314 generic_forget_inode(inode
);
1316 EXPORT_SYMBOL_GPL(generic_drop_inode
);
1319 * Called when we're dropping the last reference
1322 * Call the FS "drop()" function, defaulting to
1323 * the legacy UNIX filesystem behaviour..
1325 * NOTE! NOTE! NOTE! We're called with the inode lock
1326 * held, and the drop function is supposed to release
1329 static inline void iput_final(struct inode
*inode
)
1331 const struct super_operations
*op
= inode
->i_sb
->s_op
;
1332 void (*drop
)(struct inode
*) = generic_drop_inode
;
1334 if (op
&& op
->drop_inode
)
1335 drop
= op
->drop_inode
;
1340 * iput - put an inode
1341 * @inode: inode to put
1343 * Puts an inode, dropping its usage count. If the inode use count hits
1344 * zero, the inode is then freed and may also be destroyed.
1346 * Consequently, iput() can sleep.
1348 void iput(struct inode
*inode
)
1351 BUG_ON(inode
->i_state
== I_CLEAR
);
1353 if (atomic_dec_and_lock(&inode
->i_count
, &inode_lock
))
1357 EXPORT_SYMBOL(iput
);
1360 * bmap - find a block number in a file
1361 * @inode: inode of file
1362 * @block: block to find
1364 * Returns the block number on the device holding the inode that
1365 * is the disk block number for the block of the file requested.
1366 * That is, asked for block 4 of inode 1 the function will return the
1367 * disk block relative to the disk start that holds that block of the
1370 sector_t
bmap(struct inode
*inode
, sector_t block
)
1373 if (inode
->i_mapping
->a_ops
->bmap
)
1374 res
= inode
->i_mapping
->a_ops
->bmap(inode
->i_mapping
, block
);
1377 EXPORT_SYMBOL(bmap
);
1380 * With relative atime, only update atime if the previous atime is
1381 * earlier than either the ctime or mtime or if at least a day has
1382 * passed since the last atime update.
1384 static int relatime_need_update(struct vfsmount
*mnt
, struct inode
*inode
,
1385 struct timespec now
)
1388 if (!(mnt
->mnt_flags
& MNT_RELATIME
))
1391 * Is mtime younger than atime? If yes, update atime:
1393 if (timespec_compare(&inode
->i_mtime
, &inode
->i_atime
) >= 0)
1396 * Is ctime younger than atime? If yes, update atime:
1398 if (timespec_compare(&inode
->i_ctime
, &inode
->i_atime
) >= 0)
1402 * Is the previous atime value older than a day? If yes,
1405 if ((long)(now
.tv_sec
- inode
->i_atime
.tv_sec
) >= 24*60*60)
1408 * Good, we can skip the atime update:
1414 * touch_atime - update the access time
1415 * @mnt: mount the inode is accessed on
1416 * @dentry: dentry accessed
1418 * Update the accessed time on an inode and mark it for writeback.
1419 * This function automatically handles read only file systems and media,
1420 * as well as the "noatime" flag and inode specific "noatime" markers.
1422 void touch_atime(struct vfsmount
*mnt
, struct dentry
*dentry
)
1424 struct inode
*inode
= dentry
->d_inode
;
1425 struct timespec now
;
1427 if (inode
->i_flags
& S_NOATIME
)
1429 if (IS_NOATIME(inode
))
1431 if ((inode
->i_sb
->s_flags
& MS_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1434 if (mnt
->mnt_flags
& MNT_NOATIME
)
1436 if ((mnt
->mnt_flags
& MNT_NODIRATIME
) && S_ISDIR(inode
->i_mode
))
1439 now
= current_fs_time(inode
->i_sb
);
1441 if (!relatime_need_update(mnt
, inode
, now
))
1444 if (timespec_equal(&inode
->i_atime
, &now
))
1447 if (mnt_want_write(mnt
))
1450 inode
->i_atime
= now
;
1451 mark_inode_dirty_sync(inode
);
1452 mnt_drop_write(mnt
);
1454 EXPORT_SYMBOL(touch_atime
);
1457 * file_update_time - update mtime and ctime time
1458 * @file: file accessed
1460 * Update the mtime and ctime members of an inode and mark the inode
1461 * for writeback. Note that this function is meant exclusively for
1462 * usage in the file write path of filesystems, and filesystems may
1463 * choose to explicitly ignore update via this function with the
1464 * S_NOCMTIME inode flag, e.g. for network filesystem where these
1465 * timestamps are handled by the server.
1468 void file_update_time(struct file
*file
)
1470 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1471 struct timespec now
;
1472 enum { S_MTIME
= 1, S_CTIME
= 2, S_VERSION
= 4 } sync_it
= 0;
1474 /* First try to exhaust all avenues to not sync */
1475 if (IS_NOCMTIME(inode
))
1478 now
= current_fs_time(inode
->i_sb
);
1479 if (!timespec_equal(&inode
->i_mtime
, &now
))
1482 if (!timespec_equal(&inode
->i_ctime
, &now
))
1485 if (IS_I_VERSION(inode
))
1486 sync_it
|= S_VERSION
;
1491 /* Finally allowed to write? Takes lock. */
1492 if (mnt_want_write_file(file
))
1495 /* Only change inode inside the lock region */
1496 if (sync_it
& S_VERSION
)
1497 inode_inc_iversion(inode
);
1498 if (sync_it
& S_CTIME
)
1499 inode
->i_ctime
= now
;
1500 if (sync_it
& S_MTIME
)
1501 inode
->i_mtime
= now
;
1502 mark_inode_dirty_sync(inode
);
1503 mnt_drop_write(file
->f_path
.mnt
);
1505 EXPORT_SYMBOL(file_update_time
);
1507 int inode_needs_sync(struct inode
*inode
)
1511 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
1515 EXPORT_SYMBOL(inode_needs_sync
);
1517 int inode_wait(void *word
)
1522 EXPORT_SYMBOL(inode_wait
);
1525 * If we try to find an inode in the inode hash while it is being
1526 * deleted, we have to wait until the filesystem completes its
1527 * deletion before reporting that it isn't found. This function waits
1528 * until the deletion _might_ have completed. Callers are responsible
1529 * to recheck inode state.
1531 * It doesn't matter if I_LOCK is not set initially, a call to
1532 * wake_up_inode() after removing from the hash list will DTRT.
1534 * This is called with inode_lock held.
1536 static void __wait_on_freeing_inode(struct inode
*inode
)
1538 wait_queue_head_t
*wq
;
1539 DEFINE_WAIT_BIT(wait
, &inode
->i_state
, __I_LOCK
);
1540 wq
= bit_waitqueue(&inode
->i_state
, __I_LOCK
);
1541 prepare_to_wait(wq
, &wait
.wait
, TASK_UNINTERRUPTIBLE
);
1542 spin_unlock(&inode_lock
);
1544 finish_wait(wq
, &wait
.wait
);
1545 spin_lock(&inode_lock
);
1548 static __initdata
unsigned long ihash_entries
;
1549 static int __init
set_ihash_entries(char *str
)
1553 ihash_entries
= simple_strtoul(str
, &str
, 0);
1556 __setup("ihash_entries=", set_ihash_entries
);
1559 * Initialize the waitqueues and inode hash table.
1561 void __init
inode_init_early(void)
1565 /* If hashes are distributed across NUMA nodes, defer
1566 * hash allocation until vmalloc space is available.
1572 alloc_large_system_hash("Inode-cache",
1573 sizeof(struct hlist_head
),
1581 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1582 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1585 void __init
inode_init(void)
1589 /* inode slab cache */
1590 inode_cachep
= kmem_cache_create("inode_cache",
1591 sizeof(struct inode
),
1593 (SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|
1596 register_shrinker(&icache_shrinker
);
1598 /* Hash may have been set up in inode_init_early */
1603 alloc_large_system_hash("Inode-cache",
1604 sizeof(struct hlist_head
),
1612 for (loop
= 0; loop
< (1 << i_hash_shift
); loop
++)
1613 INIT_HLIST_HEAD(&inode_hashtable
[loop
]);
1616 void init_special_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
1618 inode
->i_mode
= mode
;
1619 if (S_ISCHR(mode
)) {
1620 inode
->i_fop
= &def_chr_fops
;
1621 inode
->i_rdev
= rdev
;
1622 } else if (S_ISBLK(mode
)) {
1623 inode
->i_fop
= &def_blk_fops
;
1624 inode
->i_rdev
= rdev
;
1625 } else if (S_ISFIFO(mode
))
1626 inode
->i_fop
= &def_fifo_fops
;
1627 else if (S_ISSOCK(mode
))
1628 inode
->i_fop
= &bad_sock_fops
;
1630 printk(KERN_DEBUG
"init_special_inode: bogus i_mode (%o) for"
1631 " inode %s:%lu\n", mode
, inode
->i_sb
->s_id
,
1634 EXPORT_SYMBOL(init_special_inode
);