USB: EHCI: fix handling of unusual interrupt intervals
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / inode.c
blob79777d911a3ffa4410322e8475a6b601d3cba3f6
1 /*
2 * linux/fs/inode.c
4 * (C) 1997 Linus Torvalds
5 */
7 #include <linux/fs.h>
8 #include <linux/mm.h>
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/hash.h>
18 #include <linux/swap.h>
19 #include <linux/security.h>
20 #include <linux/pagemap.h>
21 #include <linux/cdev.h>
22 #include <linux/bootmem.h>
23 #include <linux/inotify.h>
24 #include <linux/mount.h>
27 * This is needed for the following functions:
28 * - inode_has_buffers
29 * - invalidate_inode_buffers
30 * - invalidate_bdev
32 * FIXME: remove all knowledge of the buffer layer from this file
34 #include <linux/buffer_head.h>
37 * New inode.c implementation.
39 * This implementation has the basic premise of trying
40 * to be extremely low-overhead and SMP-safe, yet be
41 * simple enough to be "obviously correct".
43 * Famous last words.
46 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
48 /* #define INODE_PARANOIA 1 */
49 /* #define INODE_DEBUG 1 */
52 * Inode lookup is no longer as critical as it used to be:
53 * most of the lookups are going to be through the dcache.
55 #define I_HASHBITS i_hash_shift
56 #define I_HASHMASK i_hash_mask
58 static unsigned int i_hash_mask __read_mostly;
59 static unsigned int i_hash_shift __read_mostly;
62 * Each inode can be on two separate lists. One is
63 * the hash list of the inode, used for lookups. The
64 * other linked list is the "type" list:
65 * "in_use" - valid inode, i_count > 0, i_nlink > 0
66 * "dirty" - as "in_use" but also dirty
67 * "unused" - valid inode, i_count = 0
69 * A "dirty" list is maintained for each super block,
70 * allowing for low-overhead inode sync() operations.
73 LIST_HEAD(inode_in_use);
74 LIST_HEAD(inode_unused);
75 static struct hlist_head *inode_hashtable __read_mostly;
78 * A simple spinlock to protect the list manipulations.
80 * NOTE! You also have to own the lock if you change
81 * the i_state of an inode while it is in use..
83 DEFINE_SPINLOCK(inode_lock);
86 * iprune_mutex provides exclusion between the kswapd or try_to_free_pages
87 * icache shrinking path, and the umount path. Without this exclusion,
88 * by the time prune_icache calls iput for the inode whose pages it has
89 * been invalidating, or by the time it calls clear_inode & destroy_inode
90 * from its final dispose_list, the struct super_block they refer to
91 * (for inode->i_sb->s_op) may already have been freed and reused.
93 static DEFINE_MUTEX(iprune_mutex);
96 * Statistics gathering..
98 struct inodes_stat_t inodes_stat;
100 static struct kmem_cache * inode_cachep __read_mostly;
102 static void wake_up_inode(struct inode *inode)
105 * Prevent speculative execution through spin_unlock(&inode_lock);
107 smp_mb();
108 wake_up_bit(&inode->i_state, __I_LOCK);
111 static struct inode *alloc_inode(struct super_block *sb)
113 static const struct address_space_operations empty_aops;
114 static struct inode_operations empty_iops;
115 static const struct file_operations empty_fops;
116 struct inode *inode;
118 if (sb->s_op->alloc_inode)
119 inode = sb->s_op->alloc_inode(sb);
120 else
121 inode = (struct inode *) kmem_cache_alloc(inode_cachep, GFP_KERNEL);
123 if (inode) {
124 struct address_space * const mapping = &inode->i_data;
126 inode->i_sb = sb;
127 inode->i_blkbits = sb->s_blocksize_bits;
128 inode->i_flags = 0;
129 atomic_set(&inode->i_count, 1);
130 inode->i_op = &empty_iops;
131 inode->i_fop = &empty_fops;
132 inode->i_nlink = 1;
133 atomic_set(&inode->i_writecount, 0);
134 inode->i_size = 0;
135 inode->i_blocks = 0;
136 inode->i_bytes = 0;
137 inode->i_generation = 0;
138 #ifdef CONFIG_QUOTA
139 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
140 #endif
141 inode->i_pipe = NULL;
142 inode->i_bdev = NULL;
143 inode->i_cdev = NULL;
144 inode->i_rdev = 0;
145 inode->dirtied_when = 0;
146 if (security_inode_alloc(inode)) {
147 if (inode->i_sb->s_op->destroy_inode)
148 inode->i_sb->s_op->destroy_inode(inode);
149 else
150 kmem_cache_free(inode_cachep, (inode));
151 return NULL;
154 spin_lock_init(&inode->i_lock);
155 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
157 mutex_init(&inode->i_mutex);
158 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
160 init_rwsem(&inode->i_alloc_sem);
161 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
163 mapping->a_ops = &empty_aops;
164 mapping->host = inode;
165 mapping->flags = 0;
166 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_PAGECACHE);
167 mapping->assoc_mapping = NULL;
168 mapping->backing_dev_info = &default_backing_dev_info;
169 mapping->writeback_index = 0;
172 * If the block_device provides a backing_dev_info for client
173 * inodes then use that. Otherwise the inode share the bdev's
174 * backing_dev_info.
176 if (sb->s_bdev) {
177 struct backing_dev_info *bdi;
179 bdi = sb->s_bdev->bd_inode_backing_dev_info;
180 if (!bdi)
181 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
182 mapping->backing_dev_info = bdi;
184 inode->i_private = NULL;
185 inode->i_mapping = mapping;
187 return inode;
190 void destroy_inode(struct inode *inode)
192 BUG_ON(inode_has_buffers(inode));
193 security_inode_free(inode);
194 if (inode->i_sb->s_op->destroy_inode)
195 inode->i_sb->s_op->destroy_inode(inode);
196 else
197 kmem_cache_free(inode_cachep, (inode));
202 * These are initializations that only need to be done
203 * once, because the fields are idempotent across use
204 * of the inode, so let the slab aware of that.
206 void inode_init_once(struct inode *inode)
208 memset(inode, 0, sizeof(*inode));
209 INIT_HLIST_NODE(&inode->i_hash);
210 INIT_LIST_HEAD(&inode->i_dentry);
211 INIT_LIST_HEAD(&inode->i_devices);
212 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
213 spin_lock_init(&inode->i_data.tree_lock);
214 spin_lock_init(&inode->i_data.i_mmap_lock);
215 INIT_LIST_HEAD(&inode->i_data.private_list);
216 spin_lock_init(&inode->i_data.private_lock);
217 INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap);
218 INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear);
219 i_size_ordered_init(inode);
220 #ifdef CONFIG_INOTIFY
221 INIT_LIST_HEAD(&inode->inotify_watches);
222 mutex_init(&inode->inotify_mutex);
223 #endif
226 EXPORT_SYMBOL(inode_init_once);
228 static void init_once(void *foo)
230 struct inode * inode = (struct inode *) foo;
232 inode_init_once(inode);
236 * inode_lock must be held
238 void __iget(struct inode * inode)
240 if (atomic_read(&inode->i_count)) {
241 atomic_inc(&inode->i_count);
242 return;
244 atomic_inc(&inode->i_count);
245 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
246 list_move(&inode->i_list, &inode_in_use);
247 inodes_stat.nr_unused--;
251 * clear_inode - clear an inode
252 * @inode: inode to clear
254 * This is called by the filesystem to tell us
255 * that the inode is no longer useful. We just
256 * terminate it with extreme prejudice.
258 void clear_inode(struct inode *inode)
260 might_sleep();
261 invalidate_inode_buffers(inode);
263 BUG_ON(inode->i_data.nrpages);
264 BUG_ON(!(inode->i_state & I_FREEING));
265 BUG_ON(inode->i_state & I_CLEAR);
266 inode_sync_wait(inode);
267 DQUOT_DROP(inode);
268 if (inode->i_sb->s_op->clear_inode)
269 inode->i_sb->s_op->clear_inode(inode);
270 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
271 bd_forget(inode);
272 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
273 cd_forget(inode);
274 inode->i_state = I_CLEAR;
277 EXPORT_SYMBOL(clear_inode);
280 * dispose_list - dispose of the contents of a local list
281 * @head: the head of the list to free
283 * Dispose-list gets a local list with local inodes in it, so it doesn't
284 * need to worry about list corruption and SMP locks.
286 static void dispose_list(struct list_head *head)
288 int nr_disposed = 0;
290 while (!list_empty(head)) {
291 struct inode *inode;
293 inode = list_first_entry(head, struct inode, i_list);
294 list_del(&inode->i_list);
296 if (inode->i_data.nrpages)
297 truncate_inode_pages(&inode->i_data, 0);
298 clear_inode(inode);
300 spin_lock(&inode_lock);
301 hlist_del_init(&inode->i_hash);
302 list_del_init(&inode->i_sb_list);
303 spin_unlock(&inode_lock);
305 wake_up_inode(inode);
306 destroy_inode(inode);
307 nr_disposed++;
309 spin_lock(&inode_lock);
310 inodes_stat.nr_inodes -= nr_disposed;
311 spin_unlock(&inode_lock);
315 * Invalidate all inodes for a device.
317 static int invalidate_list(struct list_head *head, struct list_head *dispose)
319 struct list_head *next;
320 int busy = 0, count = 0;
322 next = head->next;
323 for (;;) {
324 struct list_head * tmp = next;
325 struct inode * inode;
328 * We can reschedule here without worrying about the list's
329 * consistency because the per-sb list of inodes must not
330 * change during umount anymore, and because iprune_mutex keeps
331 * shrink_icache_memory() away.
333 cond_resched_lock(&inode_lock);
335 next = next->next;
336 if (tmp == head)
337 break;
338 inode = list_entry(tmp, struct inode, i_sb_list);
339 invalidate_inode_buffers(inode);
340 if (!atomic_read(&inode->i_count)) {
341 list_move(&inode->i_list, dispose);
342 WARN_ON(inode->i_state & I_NEW);
343 inode->i_state |= I_FREEING;
344 count++;
345 continue;
347 busy = 1;
349 /* only unused inodes may be cached with i_count zero */
350 inodes_stat.nr_unused -= count;
351 return busy;
355 * invalidate_inodes - discard the inodes on a device
356 * @sb: superblock
358 * Discard all of the inodes for a given superblock. If the discard
359 * fails because there are busy inodes then a non zero value is returned.
360 * If the discard is successful all the inodes have been discarded.
362 int invalidate_inodes(struct super_block * sb)
364 int busy;
365 LIST_HEAD(throw_away);
367 mutex_lock(&iprune_mutex);
368 spin_lock(&inode_lock);
369 inotify_unmount_inodes(&sb->s_inodes);
370 busy = invalidate_list(&sb->s_inodes, &throw_away);
371 spin_unlock(&inode_lock);
373 dispose_list(&throw_away);
374 mutex_unlock(&iprune_mutex);
376 return busy;
379 EXPORT_SYMBOL(invalidate_inodes);
381 static int can_unuse(struct inode *inode)
383 if (inode->i_state)
384 return 0;
385 if (inode_has_buffers(inode))
386 return 0;
387 if (atomic_read(&inode->i_count))
388 return 0;
389 if (inode->i_data.nrpages)
390 return 0;
391 return 1;
395 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
396 * a temporary list and then are freed outside inode_lock by dispose_list().
398 * Any inodes which are pinned purely because of attached pagecache have their
399 * pagecache removed. We expect the final iput() on that inode to add it to
400 * the front of the inode_unused list. So look for it there and if the
401 * inode is still freeable, proceed. The right inode is found 99.9% of the
402 * time in testing on a 4-way.
404 * If the inode has metadata buffers attached to mapping->private_list then
405 * try to remove them.
407 static void prune_icache(int nr_to_scan)
409 LIST_HEAD(freeable);
410 int nr_pruned = 0;
411 int nr_scanned;
412 unsigned long reap = 0;
414 mutex_lock(&iprune_mutex);
415 spin_lock(&inode_lock);
416 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
417 struct inode *inode;
419 if (list_empty(&inode_unused))
420 break;
422 inode = list_entry(inode_unused.prev, struct inode, i_list);
424 if (inode->i_state || atomic_read(&inode->i_count)) {
425 list_move(&inode->i_list, &inode_unused);
426 continue;
428 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
429 __iget(inode);
430 spin_unlock(&inode_lock);
431 if (remove_inode_buffers(inode))
432 reap += invalidate_mapping_pages(&inode->i_data,
433 0, -1);
434 iput(inode);
435 spin_lock(&inode_lock);
437 if (inode != list_entry(inode_unused.next,
438 struct inode, i_list))
439 continue; /* wrong inode or list_empty */
440 if (!can_unuse(inode))
441 continue;
443 list_move(&inode->i_list, &freeable);
444 WARN_ON(inode->i_state & I_NEW);
445 inode->i_state |= I_FREEING;
446 nr_pruned++;
448 inodes_stat.nr_unused -= nr_pruned;
449 if (current_is_kswapd())
450 __count_vm_events(KSWAPD_INODESTEAL, reap);
451 else
452 __count_vm_events(PGINODESTEAL, reap);
453 spin_unlock(&inode_lock);
455 dispose_list(&freeable);
456 mutex_unlock(&iprune_mutex);
460 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
461 * "unused" means that no dentries are referring to the inodes: the files are
462 * not open and the dcache references to those inodes have already been
463 * reclaimed.
465 * This function is passed the number of inodes to scan, and it returns the
466 * total number of remaining possibly-reclaimable inodes.
468 static int shrink_icache_memory(int nr, gfp_t gfp_mask)
470 if (nr) {
472 * Nasty deadlock avoidance. We may hold various FS locks,
473 * and we don't want to recurse into the FS that called us
474 * in clear_inode() and friends..
476 if (!(gfp_mask & __GFP_FS))
477 return -1;
478 prune_icache(nr);
480 return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
483 static struct shrinker icache_shrinker = {
484 .shrink = shrink_icache_memory,
485 .seeks = DEFAULT_SEEKS,
488 static void __wait_on_freeing_inode(struct inode *inode);
490 * Called with the inode lock held.
491 * NOTE: we are not increasing the inode-refcount, you must call __iget()
492 * by hand after calling find_inode now! This simplifies iunique and won't
493 * add any additional branch in the common code.
495 static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
497 struct hlist_node *node;
498 struct inode * inode = NULL;
500 repeat:
501 hlist_for_each_entry(inode, node, head, i_hash) {
502 if (inode->i_sb != sb)
503 continue;
504 if (!test(inode, data))
505 continue;
506 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
507 __wait_on_freeing_inode(inode);
508 goto repeat;
510 break;
512 return node ? inode : NULL;
516 * find_inode_fast is the fast path version of find_inode, see the comment at
517 * iget_locked for details.
519 static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
521 struct hlist_node *node;
522 struct inode * inode = NULL;
524 repeat:
525 hlist_for_each_entry(inode, node, head, i_hash) {
526 if (inode->i_ino != ino)
527 continue;
528 if (inode->i_sb != sb)
529 continue;
530 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
531 __wait_on_freeing_inode(inode);
532 goto repeat;
534 break;
536 return node ? inode : NULL;
540 * new_inode - obtain an inode
541 * @sb: superblock
543 * Allocates a new inode for given superblock. The default gfp_mask
544 * for allocations related to inode->i_mapping is GFP_HIGHUSER_PAGECACHE.
545 * If HIGHMEM pages are unsuitable or it is known that pages allocated
546 * for the page cache are not reclaimable or migratable,
547 * mapping_set_gfp_mask() must be called with suitable flags on the
548 * newly created inode's mapping
551 struct inode *new_inode(struct super_block *sb)
554 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
555 * error if st_ino won't fit in target struct field. Use 32bit counter
556 * here to attempt to avoid that.
558 static unsigned int last_ino;
559 struct inode * inode;
561 spin_lock_prefetch(&inode_lock);
563 inode = alloc_inode(sb);
564 if (inode) {
565 spin_lock(&inode_lock);
566 inodes_stat.nr_inodes++;
567 list_add(&inode->i_list, &inode_in_use);
568 list_add(&inode->i_sb_list, &sb->s_inodes);
569 inode->i_ino = ++last_ino;
570 inode->i_state = 0;
571 spin_unlock(&inode_lock);
573 return inode;
576 EXPORT_SYMBOL(new_inode);
578 void unlock_new_inode(struct inode *inode)
580 #ifdef CONFIG_DEBUG_LOCK_ALLOC
581 if (inode->i_mode & S_IFDIR) {
582 struct file_system_type *type = inode->i_sb->s_type;
585 * ensure nobody is actually holding i_mutex
587 mutex_destroy(&inode->i_mutex);
588 mutex_init(&inode->i_mutex);
589 lockdep_set_class(&inode->i_mutex, &type->i_mutex_dir_key);
591 #endif
593 * This is special! We do not need the spinlock when clearing I_LOCK,
594 * because we're guaranteed that nobody else tries to do anything about
595 * the state of the inode when it is locked, as we just created it (so
596 * there can be no old holders that haven't tested I_LOCK).
597 * However we must emit the memory barrier so that other CPUs reliably
598 * see the clearing of I_LOCK after the other inode initialisation has
599 * completed.
601 smp_mb();
602 WARN_ON((inode->i_state & (I_LOCK|I_NEW)) != (I_LOCK|I_NEW));
603 inode->i_state &= ~(I_LOCK|I_NEW);
604 wake_up_inode(inode);
607 EXPORT_SYMBOL(unlock_new_inode);
610 * This is called without the inode lock held.. Be careful.
612 * We no longer cache the sb_flags in i_flags - see fs.h
613 * -- rmk@arm.uk.linux.org
615 static struct inode * get_new_inode(struct super_block *sb, struct hlist_head *head, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *data)
617 struct inode * inode;
619 inode = alloc_inode(sb);
620 if (inode) {
621 struct inode * old;
623 spin_lock(&inode_lock);
624 /* We released the lock, so.. */
625 old = find_inode(sb, head, test, data);
626 if (!old) {
627 if (set(inode, data))
628 goto set_failed;
630 inodes_stat.nr_inodes++;
631 list_add(&inode->i_list, &inode_in_use);
632 list_add(&inode->i_sb_list, &sb->s_inodes);
633 hlist_add_head(&inode->i_hash, head);
634 inode->i_state = I_LOCK|I_NEW;
635 spin_unlock(&inode_lock);
637 /* Return the locked inode with I_NEW set, the
638 * caller is responsible for filling in the contents
640 return inode;
644 * Uhhuh, somebody else created the same inode under
645 * us. Use the old inode instead of the one we just
646 * allocated.
648 __iget(old);
649 spin_unlock(&inode_lock);
650 destroy_inode(inode);
651 inode = old;
652 wait_on_inode(inode);
654 return inode;
656 set_failed:
657 spin_unlock(&inode_lock);
658 destroy_inode(inode);
659 return NULL;
663 * get_new_inode_fast is the fast path version of get_new_inode, see the
664 * comment at iget_locked for details.
666 static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
668 struct inode * inode;
670 inode = alloc_inode(sb);
671 if (inode) {
672 struct inode * old;
674 spin_lock(&inode_lock);
675 /* We released the lock, so.. */
676 old = find_inode_fast(sb, head, ino);
677 if (!old) {
678 inode->i_ino = ino;
679 inodes_stat.nr_inodes++;
680 list_add(&inode->i_list, &inode_in_use);
681 list_add(&inode->i_sb_list, &sb->s_inodes);
682 hlist_add_head(&inode->i_hash, head);
683 inode->i_state = I_LOCK|I_NEW;
684 spin_unlock(&inode_lock);
686 /* Return the locked inode with I_NEW set, the
687 * caller is responsible for filling in the contents
689 return inode;
693 * Uhhuh, somebody else created the same inode under
694 * us. Use the old inode instead of the one we just
695 * allocated.
697 __iget(old);
698 spin_unlock(&inode_lock);
699 destroy_inode(inode);
700 inode = old;
701 wait_on_inode(inode);
703 return inode;
706 static unsigned long hash(struct super_block *sb, unsigned long hashval)
708 unsigned long tmp;
710 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
711 L1_CACHE_BYTES;
712 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
713 return tmp & I_HASHMASK;
717 * iunique - get a unique inode number
718 * @sb: superblock
719 * @max_reserved: highest reserved inode number
721 * Obtain an inode number that is unique on the system for a given
722 * superblock. This is used by file systems that have no natural
723 * permanent inode numbering system. An inode number is returned that
724 * is higher than the reserved limit but unique.
726 * BUGS:
727 * With a large number of inodes live on the file system this function
728 * currently becomes quite slow.
730 ino_t iunique(struct super_block *sb, ino_t max_reserved)
733 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
734 * error if st_ino won't fit in target struct field. Use 32bit counter
735 * here to attempt to avoid that.
737 static unsigned int counter;
738 struct inode *inode;
739 struct hlist_head *head;
740 ino_t res;
742 spin_lock(&inode_lock);
743 do {
744 if (counter <= max_reserved)
745 counter = max_reserved + 1;
746 res = counter++;
747 head = inode_hashtable + hash(sb, res);
748 inode = find_inode_fast(sb, head, res);
749 } while (inode != NULL);
750 spin_unlock(&inode_lock);
752 return res;
754 EXPORT_SYMBOL(iunique);
756 struct inode *igrab(struct inode *inode)
758 spin_lock(&inode_lock);
759 if (!(inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)))
760 __iget(inode);
761 else
763 * Handle the case where s_op->clear_inode is not been
764 * called yet, and somebody is calling igrab
765 * while the inode is getting freed.
767 inode = NULL;
768 spin_unlock(&inode_lock);
769 return inode;
772 EXPORT_SYMBOL(igrab);
775 * ifind - internal function, you want ilookup5() or iget5().
776 * @sb: super block of file system to search
777 * @head: the head of the list to search
778 * @test: callback used for comparisons between inodes
779 * @data: opaque data pointer to pass to @test
780 * @wait: if true wait for the inode to be unlocked, if false do not
782 * ifind() searches for the inode specified by @data in the inode
783 * cache. This is a generalized version of ifind_fast() for file systems where
784 * the inode number is not sufficient for unique identification of an inode.
786 * If the inode is in the cache, the inode is returned with an incremented
787 * reference count.
789 * Otherwise NULL is returned.
791 * Note, @test is called with the inode_lock held, so can't sleep.
793 static struct inode *ifind(struct super_block *sb,
794 struct hlist_head *head, int (*test)(struct inode *, void *),
795 void *data, const int wait)
797 struct inode *inode;
799 spin_lock(&inode_lock);
800 inode = find_inode(sb, head, test, data);
801 if (inode) {
802 __iget(inode);
803 spin_unlock(&inode_lock);
804 if (likely(wait))
805 wait_on_inode(inode);
806 return inode;
808 spin_unlock(&inode_lock);
809 return NULL;
813 * ifind_fast - internal function, you want ilookup() or iget().
814 * @sb: super block of file system to search
815 * @head: head of the list to search
816 * @ino: inode number to search for
818 * ifind_fast() searches for the inode @ino in the inode cache. This is for
819 * file systems where the inode number is sufficient for unique identification
820 * of an inode.
822 * If the inode is in the cache, the inode is returned with an incremented
823 * reference count.
825 * Otherwise NULL is returned.
827 static struct inode *ifind_fast(struct super_block *sb,
828 struct hlist_head *head, unsigned long ino)
830 struct inode *inode;
832 spin_lock(&inode_lock);
833 inode = find_inode_fast(sb, head, ino);
834 if (inode) {
835 __iget(inode);
836 spin_unlock(&inode_lock);
837 wait_on_inode(inode);
838 return inode;
840 spin_unlock(&inode_lock);
841 return NULL;
845 * ilookup5_nowait - search for an inode in the inode cache
846 * @sb: super block of file system to search
847 * @hashval: hash value (usually inode number) to search for
848 * @test: callback used for comparisons between inodes
849 * @data: opaque data pointer to pass to @test
851 * ilookup5() uses ifind() to search for the inode specified by @hashval and
852 * @data in the inode cache. This is a generalized version of ilookup() for
853 * file systems where the inode number is not sufficient for unique
854 * identification of an inode.
856 * If the inode is in the cache, the inode is returned with an incremented
857 * reference count. Note, the inode lock is not waited upon so you have to be
858 * very careful what you do with the returned inode. You probably should be
859 * using ilookup5() instead.
861 * Otherwise NULL is returned.
863 * Note, @test is called with the inode_lock held, so can't sleep.
865 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
866 int (*test)(struct inode *, void *), void *data)
868 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
870 return ifind(sb, head, test, data, 0);
873 EXPORT_SYMBOL(ilookup5_nowait);
876 * ilookup5 - search for an inode in the inode cache
877 * @sb: super block of file system to search
878 * @hashval: hash value (usually inode number) to search for
879 * @test: callback used for comparisons between inodes
880 * @data: opaque data pointer to pass to @test
882 * ilookup5() uses ifind() to search for the inode specified by @hashval and
883 * @data in the inode cache. This is a generalized version of ilookup() for
884 * file systems where the inode number is not sufficient for unique
885 * identification of an inode.
887 * If the inode is in the cache, the inode lock is waited upon and the inode is
888 * returned with an incremented reference count.
890 * Otherwise NULL is returned.
892 * Note, @test is called with the inode_lock held, so can't sleep.
894 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
895 int (*test)(struct inode *, void *), void *data)
897 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
899 return ifind(sb, head, test, data, 1);
902 EXPORT_SYMBOL(ilookup5);
905 * ilookup - search for an inode in the inode cache
906 * @sb: super block of file system to search
907 * @ino: inode number to search for
909 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
910 * This is for file systems where the inode number is sufficient for unique
911 * identification of an inode.
913 * If the inode is in the cache, the inode is returned with an incremented
914 * reference count.
916 * Otherwise NULL is returned.
918 struct inode *ilookup(struct super_block *sb, unsigned long ino)
920 struct hlist_head *head = inode_hashtable + hash(sb, ino);
922 return ifind_fast(sb, head, ino);
925 EXPORT_SYMBOL(ilookup);
928 * iget5_locked - obtain an inode from a mounted file system
929 * @sb: super block of file system
930 * @hashval: hash value (usually inode number) to get
931 * @test: callback used for comparisons between inodes
932 * @set: callback used to initialize a new struct inode
933 * @data: opaque data pointer to pass to @test and @set
935 * iget5_locked() uses ifind() to search for the inode specified by @hashval
936 * and @data in the inode cache and if present it is returned with an increased
937 * reference count. This is a generalized version of iget_locked() for file
938 * systems where the inode number is not sufficient for unique identification
939 * of an inode.
941 * If the inode is not in cache, get_new_inode() is called to allocate a new
942 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
943 * file system gets to fill it in before unlocking it via unlock_new_inode().
945 * Note both @test and @set are called with the inode_lock held, so can't sleep.
947 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
948 int (*test)(struct inode *, void *),
949 int (*set)(struct inode *, void *), void *data)
951 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
952 struct inode *inode;
954 inode = ifind(sb, head, test, data, 1);
955 if (inode)
956 return inode;
958 * get_new_inode() will do the right thing, re-trying the search
959 * in case it had to block at any point.
961 return get_new_inode(sb, head, test, set, data);
964 EXPORT_SYMBOL(iget5_locked);
967 * iget_locked - obtain an inode from a mounted file system
968 * @sb: super block of file system
969 * @ino: inode number to get
971 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
972 * the inode cache and if present it is returned with an increased reference
973 * count. This is for file systems where the inode number is sufficient for
974 * unique identification of an inode.
976 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
977 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
978 * The file system gets to fill it in before unlocking it via
979 * unlock_new_inode().
981 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
983 struct hlist_head *head = inode_hashtable + hash(sb, ino);
984 struct inode *inode;
986 inode = ifind_fast(sb, head, ino);
987 if (inode)
988 return inode;
990 * get_new_inode_fast() will do the right thing, re-trying the search
991 * in case it had to block at any point.
993 return get_new_inode_fast(sb, head, ino);
996 EXPORT_SYMBOL(iget_locked);
999 * __insert_inode_hash - hash an inode
1000 * @inode: unhashed inode
1001 * @hashval: unsigned long value used to locate this object in the
1002 * inode_hashtable.
1004 * Add an inode to the inode hash for this superblock.
1006 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
1008 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1009 spin_lock(&inode_lock);
1010 hlist_add_head(&inode->i_hash, head);
1011 spin_unlock(&inode_lock);
1014 EXPORT_SYMBOL(__insert_inode_hash);
1017 * remove_inode_hash - remove an inode from the hash
1018 * @inode: inode to unhash
1020 * Remove an inode from the superblock.
1022 void remove_inode_hash(struct inode *inode)
1024 spin_lock(&inode_lock);
1025 hlist_del_init(&inode->i_hash);
1026 spin_unlock(&inode_lock);
1029 EXPORT_SYMBOL(remove_inode_hash);
1032 * Tell the filesystem that this inode is no longer of any interest and should
1033 * be completely destroyed.
1035 * We leave the inode in the inode hash table until *after* the filesystem's
1036 * ->delete_inode completes. This ensures that an iget (such as nfsd might
1037 * instigate) will always find up-to-date information either in the hash or on
1038 * disk.
1040 * I_FREEING is set so that no-one will take a new reference to the inode while
1041 * it is being deleted.
1043 void generic_delete_inode(struct inode *inode)
1045 const struct super_operations *op = inode->i_sb->s_op;
1047 list_del_init(&inode->i_list);
1048 list_del_init(&inode->i_sb_list);
1049 WARN_ON(inode->i_state & I_NEW);
1050 inode->i_state |= I_FREEING;
1051 inodes_stat.nr_inodes--;
1052 spin_unlock(&inode_lock);
1054 security_inode_delete(inode);
1056 if (op->delete_inode) {
1057 void (*delete)(struct inode *) = op->delete_inode;
1058 if (!is_bad_inode(inode))
1059 DQUOT_INIT(inode);
1060 /* Filesystems implementing their own
1061 * s_op->delete_inode are required to call
1062 * truncate_inode_pages and clear_inode()
1063 * internally */
1064 delete(inode);
1065 } else {
1066 truncate_inode_pages(&inode->i_data, 0);
1067 clear_inode(inode);
1069 spin_lock(&inode_lock);
1070 hlist_del_init(&inode->i_hash);
1071 spin_unlock(&inode_lock);
1072 wake_up_inode(inode);
1073 BUG_ON(inode->i_state != I_CLEAR);
1074 destroy_inode(inode);
1077 EXPORT_SYMBOL(generic_delete_inode);
1079 static void generic_forget_inode(struct inode *inode)
1081 struct super_block *sb = inode->i_sb;
1083 if (!hlist_unhashed(&inode->i_hash)) {
1084 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1085 list_move(&inode->i_list, &inode_unused);
1086 inodes_stat.nr_unused++;
1087 if (sb->s_flags & MS_ACTIVE) {
1088 spin_unlock(&inode_lock);
1089 return;
1091 WARN_ON(inode->i_state & I_NEW);
1092 inode->i_state |= I_WILL_FREE;
1093 spin_unlock(&inode_lock);
1094 write_inode_now(inode, 1);
1095 spin_lock(&inode_lock);
1096 WARN_ON(inode->i_state & I_NEW);
1097 inode->i_state &= ~I_WILL_FREE;
1098 inodes_stat.nr_unused--;
1099 hlist_del_init(&inode->i_hash);
1101 list_del_init(&inode->i_list);
1102 list_del_init(&inode->i_sb_list);
1103 WARN_ON(inode->i_state & I_NEW);
1104 inode->i_state |= I_FREEING;
1105 inodes_stat.nr_inodes--;
1106 spin_unlock(&inode_lock);
1107 if (inode->i_data.nrpages)
1108 truncate_inode_pages(&inode->i_data, 0);
1109 clear_inode(inode);
1110 wake_up_inode(inode);
1111 destroy_inode(inode);
1115 * Normal UNIX filesystem behaviour: delete the
1116 * inode when the usage count drops to zero, and
1117 * i_nlink is zero.
1119 void generic_drop_inode(struct inode *inode)
1121 if (!inode->i_nlink)
1122 generic_delete_inode(inode);
1123 else
1124 generic_forget_inode(inode);
1127 EXPORT_SYMBOL_GPL(generic_drop_inode);
1130 * Called when we're dropping the last reference
1131 * to an inode.
1133 * Call the FS "drop()" function, defaulting to
1134 * the legacy UNIX filesystem behaviour..
1136 * NOTE! NOTE! NOTE! We're called with the inode lock
1137 * held, and the drop function is supposed to release
1138 * the lock!
1140 static inline void iput_final(struct inode *inode)
1142 const struct super_operations *op = inode->i_sb->s_op;
1143 void (*drop)(struct inode *) = generic_drop_inode;
1145 if (op && op->drop_inode)
1146 drop = op->drop_inode;
1147 drop(inode);
1151 * iput - put an inode
1152 * @inode: inode to put
1154 * Puts an inode, dropping its usage count. If the inode use count hits
1155 * zero, the inode is then freed and may also be destroyed.
1157 * Consequently, iput() can sleep.
1159 void iput(struct inode *inode)
1161 if (inode) {
1162 BUG_ON(inode->i_state == I_CLEAR);
1164 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1165 iput_final(inode);
1169 EXPORT_SYMBOL(iput);
1172 * bmap - find a block number in a file
1173 * @inode: inode of file
1174 * @block: block to find
1176 * Returns the block number on the device holding the inode that
1177 * is the disk block number for the block of the file requested.
1178 * That is, asked for block 4 of inode 1 the function will return the
1179 * disk block relative to the disk start that holds that block of the
1180 * file.
1182 sector_t bmap(struct inode * inode, sector_t block)
1184 sector_t res = 0;
1185 if (inode->i_mapping->a_ops->bmap)
1186 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1187 return res;
1189 EXPORT_SYMBOL(bmap);
1192 * touch_atime - update the access time
1193 * @mnt: mount the inode is accessed on
1194 * @dentry: dentry accessed
1196 * Update the accessed time on an inode and mark it for writeback.
1197 * This function automatically handles read only file systems and media,
1198 * as well as the "noatime" flag and inode specific "noatime" markers.
1200 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1202 struct inode *inode = dentry->d_inode;
1203 struct timespec now;
1205 if (mnt_want_write(mnt))
1206 return;
1207 if (inode->i_flags & S_NOATIME)
1208 goto out;
1209 if (IS_NOATIME(inode))
1210 goto out;
1211 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1212 goto out;
1214 if (mnt->mnt_flags & MNT_NOATIME)
1215 goto out;
1216 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1217 goto out;
1218 if (mnt->mnt_flags & MNT_RELATIME) {
1220 * With relative atime, only update atime if the previous
1221 * atime is earlier than either the ctime or mtime.
1223 if (timespec_compare(&inode->i_mtime, &inode->i_atime) < 0 &&
1224 timespec_compare(&inode->i_ctime, &inode->i_atime) < 0)
1225 goto out;
1228 now = current_fs_time(inode->i_sb);
1229 if (timespec_equal(&inode->i_atime, &now))
1230 goto out;
1232 inode->i_atime = now;
1233 mark_inode_dirty_sync(inode);
1234 out:
1235 mnt_drop_write(mnt);
1237 EXPORT_SYMBOL(touch_atime);
1240 * file_update_time - update mtime and ctime time
1241 * @file: file accessed
1243 * Update the mtime and ctime members of an inode and mark the inode
1244 * for writeback. Note that this function is meant exclusively for
1245 * usage in the file write path of filesystems, and filesystems may
1246 * choose to explicitly ignore update via this function with the
1247 * S_NOCTIME inode flag, e.g. for network filesystem where these
1248 * timestamps are handled by the server.
1251 void file_update_time(struct file *file)
1253 struct inode *inode = file->f_path.dentry->d_inode;
1254 struct timespec now;
1255 int sync_it = 0;
1256 int err;
1258 if (IS_NOCMTIME(inode))
1259 return;
1261 err = mnt_want_write(file->f_path.mnt);
1262 if (err)
1263 return;
1265 now = current_fs_time(inode->i_sb);
1266 if (!timespec_equal(&inode->i_mtime, &now)) {
1267 inode->i_mtime = now;
1268 sync_it = 1;
1271 if (!timespec_equal(&inode->i_ctime, &now)) {
1272 inode->i_ctime = now;
1273 sync_it = 1;
1276 if (IS_I_VERSION(inode)) {
1277 inode_inc_iversion(inode);
1278 sync_it = 1;
1281 if (sync_it)
1282 mark_inode_dirty_sync(inode);
1283 mnt_drop_write(file->f_path.mnt);
1286 EXPORT_SYMBOL(file_update_time);
1288 int inode_needs_sync(struct inode *inode)
1290 if (IS_SYNC(inode))
1291 return 1;
1292 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1293 return 1;
1294 return 0;
1297 EXPORT_SYMBOL(inode_needs_sync);
1299 int inode_wait(void *word)
1301 schedule();
1302 return 0;
1306 * If we try to find an inode in the inode hash while it is being
1307 * deleted, we have to wait until the filesystem completes its
1308 * deletion before reporting that it isn't found. This function waits
1309 * until the deletion _might_ have completed. Callers are responsible
1310 * to recheck inode state.
1312 * It doesn't matter if I_LOCK is not set initially, a call to
1313 * wake_up_inode() after removing from the hash list will DTRT.
1315 * This is called with inode_lock held.
1317 static void __wait_on_freeing_inode(struct inode *inode)
1319 wait_queue_head_t *wq;
1320 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK);
1321 wq = bit_waitqueue(&inode->i_state, __I_LOCK);
1322 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1323 spin_unlock(&inode_lock);
1324 schedule();
1325 finish_wait(wq, &wait.wait);
1326 spin_lock(&inode_lock);
1330 * We rarely want to lock two inodes that do not have a parent/child
1331 * relationship (such as directory, child inode) simultaneously. The
1332 * vast majority of file systems should be able to get along fine
1333 * without this. Do not use these functions except as a last resort.
1335 void inode_double_lock(struct inode *inode1, struct inode *inode2)
1337 if (inode1 == NULL || inode2 == NULL || inode1 == inode2) {
1338 if (inode1)
1339 mutex_lock(&inode1->i_mutex);
1340 else if (inode2)
1341 mutex_lock(&inode2->i_mutex);
1342 return;
1345 if (inode1 < inode2) {
1346 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1347 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1348 } else {
1349 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1350 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1353 EXPORT_SYMBOL(inode_double_lock);
1355 void inode_double_unlock(struct inode *inode1, struct inode *inode2)
1357 if (inode1)
1358 mutex_unlock(&inode1->i_mutex);
1360 if (inode2 && inode2 != inode1)
1361 mutex_unlock(&inode2->i_mutex);
1363 EXPORT_SYMBOL(inode_double_unlock);
1365 static __initdata unsigned long ihash_entries;
1366 static int __init set_ihash_entries(char *str)
1368 if (!str)
1369 return 0;
1370 ihash_entries = simple_strtoul(str, &str, 0);
1371 return 1;
1373 __setup("ihash_entries=", set_ihash_entries);
1376 * Initialize the waitqueues and inode hash table.
1378 void __init inode_init_early(void)
1380 int loop;
1382 /* If hashes are distributed across NUMA nodes, defer
1383 * hash allocation until vmalloc space is available.
1385 if (hashdist)
1386 return;
1388 inode_hashtable =
1389 alloc_large_system_hash("Inode-cache",
1390 sizeof(struct hlist_head),
1391 ihash_entries,
1393 HASH_EARLY,
1394 &i_hash_shift,
1395 &i_hash_mask,
1398 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1399 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1402 void __init inode_init(void)
1404 int loop;
1406 /* inode slab cache */
1407 inode_cachep = kmem_cache_create("inode_cache",
1408 sizeof(struct inode),
1410 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1411 SLAB_MEM_SPREAD),
1412 init_once);
1413 register_shrinker(&icache_shrinker);
1415 /* Hash may have been set up in inode_init_early */
1416 if (!hashdist)
1417 return;
1419 inode_hashtable =
1420 alloc_large_system_hash("Inode-cache",
1421 sizeof(struct hlist_head),
1422 ihash_entries,
1425 &i_hash_shift,
1426 &i_hash_mask,
1429 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1430 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1433 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1435 inode->i_mode = mode;
1436 if (S_ISCHR(mode)) {
1437 inode->i_fop = &def_chr_fops;
1438 inode->i_rdev = rdev;
1439 } else if (S_ISBLK(mode)) {
1440 inode->i_fop = &def_blk_fops;
1441 inode->i_rdev = rdev;
1442 } else if (S_ISFIFO(mode))
1443 inode->i_fop = &def_fifo_fops;
1444 else if (S_ISSOCK(mode))
1445 inode->i_fop = &bad_sock_fops;
1446 else
1447 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1448 mode);
1450 EXPORT_SYMBOL(init_special_inode);