blob: 06aa5a1fb61bc883c52420d0f8a546d74429e9f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/inode.c
3 *
4 * (C) 1997 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#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>
Robert Love0eeca282005-07-12 17:06:03 -040023#include <linux/inotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080024#include <linux/mount.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080025#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
28 * This is needed for the following functions:
29 * - inode_has_buffers
30 * - invalidate_inode_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * - invalidate_bdev
32 *
33 * FIXME: remove all knowledge of the buffer layer from this file
34 */
35#include <linux/buffer_head.h>
36
37/*
38 * New inode.c implementation.
39 *
40 * This implementation has the basic premise of trying
41 * to be extremely low-overhead and SMP-safe, yet be
42 * simple enough to be "obviously correct".
43 *
44 * Famous last words.
45 */
46
47/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
48
49/* #define INODE_PARANOIA 1 */
50/* #define INODE_DEBUG 1 */
51
52/*
53 * Inode lookup is no longer as critical as it used to be:
54 * most of the lookups are going to be through the dcache.
55 */
56#define I_HASHBITS i_hash_shift
57#define I_HASHMASK i_hash_mask
58
Eric Dumazetfa3536c2006-03-26 01:37:24 -080059static unsigned int i_hash_mask __read_mostly;
60static unsigned int i_hash_shift __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * Each inode can be on two separate lists. One is
64 * the hash list of the inode, used for lookups. The
65 * other linked list is the "type" list:
66 * "in_use" - valid inode, i_count > 0, i_nlink > 0
67 * "dirty" - as "in_use" but also dirty
68 * "unused" - valid inode, i_count = 0
69 *
70 * A "dirty" list is maintained for each super block,
71 * allowing for low-overhead inode sync() operations.
72 */
73
74LIST_HEAD(inode_in_use);
75LIST_HEAD(inode_unused);
Eric Dumazetfa3536c2006-03-26 01:37:24 -080076static struct hlist_head *inode_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * A simple spinlock to protect the list manipulations.
80 *
81 * NOTE! You also have to own the lock if you change
82 * the i_state of an inode while it is in use..
83 */
84DEFINE_SPINLOCK(inode_lock);
85
86/*
Ingo Molnarf24075b2006-03-23 03:00:34 -080087 * iprune_mutex provides exclusion between the kswapd or try_to_free_pages
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * icache shrinking path, and the umount path. Without this exclusion,
89 * by the time prune_icache calls iput for the inode whose pages it has
90 * been invalidating, or by the time it calls clear_inode & destroy_inode
91 * from its final dispose_list, the struct super_block they refer to
92 * (for inode->i_sb->s_op) may already have been freed and reused.
93 */
Adrian Bunkbdfc3262006-03-25 03:06:56 -080094static DEFINE_MUTEX(iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
97 * Statistics gathering..
98 */
99struct inodes_stat_t inodes_stat;
100
Christoph Lametere18b8902006-12-06 20:33:20 -0800101static struct kmem_cache * inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700103static void wake_up_inode(struct inode *inode)
104{
105 /*
106 * Prevent speculative execution through spin_unlock(&inode_lock);
107 */
108 smp_mb();
109 wake_up_bit(&inode->i_state, __I_LOCK);
110}
111
David Chinner2cb15992008-10-30 17:32:23 +1100112/**
113 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800114 * @sb: superblock inode belongs to
115 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100116 *
117 * These are initializations that need to be done on every inode
118 * allocation as the fields are not initialised by slab allocation.
119 */
120struct inode *inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700122 static const struct address_space_operations empty_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 static struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800124 static const struct file_operations empty_fops;
David Chinner2cb15992008-10-30 17:32:23 +1100125
126 struct address_space * const mapping = &inode->i_data;
127
128 inode->i_sb = sb;
129 inode->i_blkbits = sb->s_blocksize_bits;
130 inode->i_flags = 0;
131 atomic_set(&inode->i_count, 1);
132 inode->i_op = &empty_iops;
133 inode->i_fop = &empty_fops;
134 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500135 inode->i_uid = 0;
136 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100137 atomic_set(&inode->i_writecount, 0);
138 inode->i_size = 0;
139 inode->i_blocks = 0;
140 inode->i_bytes = 0;
141 inode->i_generation = 0;
142#ifdef CONFIG_QUOTA
143 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
144#endif
145 inode->i_pipe = NULL;
146 inode->i_bdev = NULL;
147 inode->i_cdev = NULL;
148 inode->i_rdev = 0;
149 inode->dirtied_when = 0;
150 if (security_inode_alloc(inode)) {
151 if (inode->i_sb->s_op->destroy_inode)
152 inode->i_sb->s_op->destroy_inode(inode);
153 else
154 kmem_cache_free(inode_cachep, (inode));
155 return NULL;
156 }
157
158 spin_lock_init(&inode->i_lock);
159 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
160
161 mutex_init(&inode->i_mutex);
162 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
163
164 init_rwsem(&inode->i_alloc_sem);
165 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
166
167 mapping->a_ops = &empty_aops;
168 mapping->host = inode;
169 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800170 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100171 mapping->assoc_mapping = NULL;
172 mapping->backing_dev_info = &default_backing_dev_info;
173 mapping->writeback_index = 0;
174
175 /*
176 * If the block_device provides a backing_dev_info for client
177 * inodes then use that. Otherwise the inode share the bdev's
178 * backing_dev_info.
179 */
180 if (sb->s_bdev) {
181 struct backing_dev_info *bdi;
182
183 bdi = sb->s_bdev->bd_inode_backing_dev_info;
184 if (!bdi)
185 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
186 mapping->backing_dev_info = bdi;
187 }
188 inode->i_private = NULL;
189 inode->i_mapping = mapping;
190
191 return inode;
192}
193EXPORT_SYMBOL(inode_init_always);
194
195static struct inode *alloc_inode(struct super_block *sb)
196{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct inode *inode;
198
199 if (sb->s_op->alloc_inode)
200 inode = sb->s_op->alloc_inode(sb);
201 else
David Chinner2cb15992008-10-30 17:32:23 +1100202 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David Chinner2cb15992008-10-30 17:32:23 +1100204 if (inode)
205 return inode_init_always(sb, inode);
206 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209void destroy_inode(struct inode *inode)
210{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200211 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 security_inode_free(inode);
213 if (inode->i_sb->s_op->destroy_inode)
214 inode->i_sb->s_op->destroy_inode(inode);
215 else
216 kmem_cache_free(inode_cachep, (inode));
217}
Christoph Hellwig087e3b02008-10-30 18:24:37 +1100218EXPORT_SYMBOL(destroy_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220
221/*
222 * These are initializations that only need to be done
223 * once, because the fields are idempotent across use
224 * of the inode, so let the slab aware of that.
225 */
226void inode_init_once(struct inode *inode)
227{
228 memset(inode, 0, sizeof(*inode));
229 INIT_HLIST_NODE(&inode->i_hash);
230 INIT_LIST_HEAD(&inode->i_dentry);
231 INIT_LIST_HEAD(&inode->i_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
Nick Piggin19fd6232008-07-25 19:45:32 -0700233 spin_lock_init(&inode->i_data.tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_lock_init(&inode->i_data.i_mmap_lock);
235 INIT_LIST_HEAD(&inode->i_data.private_list);
236 spin_lock_init(&inode->i_data.private_lock);
237 INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap);
238 INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 i_size_ordered_init(inode);
Robert Love0eeca282005-07-12 17:06:03 -0400240#ifdef CONFIG_INOTIFY
241 INIT_LIST_HEAD(&inode->inotify_watches);
Ingo Molnard4f9af92006-03-23 03:00:30 -0800242 mutex_init(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246EXPORT_SYMBOL(inode_init_once);
247
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700248static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct inode * inode = (struct inode *) foo;
251
Christoph Lametera35afb82007-05-16 22:10:57 -0700252 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/*
256 * inode_lock must be held
257 */
258void __iget(struct inode * inode)
259{
260 if (atomic_read(&inode->i_count)) {
261 atomic_inc(&inode->i_count);
262 return;
263 }
264 atomic_inc(&inode->i_count);
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700265 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 list_move(&inode->i_list, &inode_in_use);
267 inodes_stat.nr_unused--;
268}
269
270/**
271 * clear_inode - clear an inode
272 * @inode: inode to clear
273 *
274 * This is called by the filesystem to tell us
275 * that the inode is no longer useful. We just
276 * terminate it with extreme prejudice.
277 */
278void clear_inode(struct inode *inode)
279{
280 might_sleep();
281 invalidate_inode_buffers(inode);
282
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200283 BUG_ON(inode->i_data.nrpages);
284 BUG_ON(!(inode->i_state & I_FREEING));
285 BUG_ON(inode->i_state & I_CLEAR);
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700286 inode_sync_wait(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 DQUOT_DROP(inode);
Christoph Hellwigacb0c852007-05-08 00:25:52 -0700288 if (inode->i_sb->s_op->clear_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 inode->i_sb->s_op->clear_inode(inode);
Theodore Ts'oeaf796e2006-09-27 01:50:48 -0700290 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 bd_forget(inode);
Theodore Ts'o577c4eb2006-09-27 01:50:49 -0700292 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 cd_forget(inode);
294 inode->i_state = I_CLEAR;
295}
296
297EXPORT_SYMBOL(clear_inode);
298
299/*
300 * dispose_list - dispose of the contents of a local list
301 * @head: the head of the list to free
302 *
303 * Dispose-list gets a local list with local inodes in it, so it doesn't
304 * need to worry about list corruption and SMP locks.
305 */
306static void dispose_list(struct list_head *head)
307{
308 int nr_disposed = 0;
309
310 while (!list_empty(head)) {
311 struct inode *inode;
312
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700313 inode = list_first_entry(head, struct inode, i_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 list_del(&inode->i_list);
315
316 if (inode->i_data.nrpages)
317 truncate_inode_pages(&inode->i_data, 0);
318 clear_inode(inode);
Artem B. Bityuckiy4120db42005-07-12 13:58:12 -0700319
320 spin_lock(&inode_lock);
321 hlist_del_init(&inode->i_hash);
322 list_del_init(&inode->i_sb_list);
323 spin_unlock(&inode_lock);
324
325 wake_up_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 destroy_inode(inode);
327 nr_disposed++;
328 }
329 spin_lock(&inode_lock);
330 inodes_stat.nr_inodes -= nr_disposed;
331 spin_unlock(&inode_lock);
332}
333
334/*
335 * Invalidate all inodes for a device.
336 */
337static int invalidate_list(struct list_head *head, struct list_head *dispose)
338{
339 struct list_head *next;
340 int busy = 0, count = 0;
341
342 next = head->next;
343 for (;;) {
344 struct list_head * tmp = next;
345 struct inode * inode;
346
347 /*
348 * We can reschedule here without worrying about the list's
349 * consistency because the per-sb list of inodes must not
Ingo Molnarf24075b2006-03-23 03:00:34 -0800350 * change during umount anymore, and because iprune_mutex keeps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * shrink_icache_memory() away.
352 */
353 cond_resched_lock(&inode_lock);
354
355 next = next->next;
356 if (tmp == head)
357 break;
358 inode = list_entry(tmp, struct inode, i_sb_list);
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700359 if (inode->i_state & I_NEW)
360 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 invalidate_inode_buffers(inode);
362 if (!atomic_read(&inode->i_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 list_move(&inode->i_list, dispose);
Nick Piggin7ef0d732009-03-12 14:31:38 -0700364 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 inode->i_state |= I_FREEING;
366 count++;
367 continue;
368 }
369 busy = 1;
370 }
371 /* only unused inodes may be cached with i_count zero */
372 inodes_stat.nr_unused -= count;
373 return busy;
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/**
377 * invalidate_inodes - discard the inodes on a device
378 * @sb: superblock
379 *
380 * Discard all of the inodes for a given superblock. If the discard
381 * fails because there are busy inodes then a non zero value is returned.
382 * If the discard is successful all the inodes have been discarded.
383 */
384int invalidate_inodes(struct super_block * sb)
385{
386 int busy;
387 LIST_HEAD(throw_away);
388
Ingo Molnarf24075b2006-03-23 03:00:34 -0800389 mutex_lock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_lock(&inode_lock);
Robert Love0eeca282005-07-12 17:06:03 -0400391 inotify_unmount_inodes(&sb->s_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 busy = invalidate_list(&sb->s_inodes, &throw_away);
393 spin_unlock(&inode_lock);
394
395 dispose_list(&throw_away);
Ingo Molnarf24075b2006-03-23 03:00:34 -0800396 mutex_unlock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 return busy;
399}
400
401EXPORT_SYMBOL(invalidate_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403static int can_unuse(struct inode *inode)
404{
405 if (inode->i_state)
406 return 0;
407 if (inode_has_buffers(inode))
408 return 0;
409 if (atomic_read(&inode->i_count))
410 return 0;
411 if (inode->i_data.nrpages)
412 return 0;
413 return 1;
414}
415
416/*
417 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
418 * a temporary list and then are freed outside inode_lock by dispose_list().
419 *
420 * Any inodes which are pinned purely because of attached pagecache have their
421 * pagecache removed. We expect the final iput() on that inode to add it to
422 * the front of the inode_unused list. So look for it there and if the
423 * inode is still freeable, proceed. The right inode is found 99.9% of the
424 * time in testing on a 4-way.
425 *
426 * If the inode has metadata buffers attached to mapping->private_list then
427 * try to remove them.
428 */
429static void prune_icache(int nr_to_scan)
430{
431 LIST_HEAD(freeable);
432 int nr_pruned = 0;
433 int nr_scanned;
434 unsigned long reap = 0;
435
Ingo Molnarf24075b2006-03-23 03:00:34 -0800436 mutex_lock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 spin_lock(&inode_lock);
438 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
439 struct inode *inode;
440
441 if (list_empty(&inode_unused))
442 break;
443
444 inode = list_entry(inode_unused.prev, struct inode, i_list);
445
446 if (inode->i_state || atomic_read(&inode->i_count)) {
447 list_move(&inode->i_list, &inode_unused);
448 continue;
449 }
450 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
451 __iget(inode);
452 spin_unlock(&inode_lock);
453 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800454 reap += invalidate_mapping_pages(&inode->i_data,
455 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 iput(inode);
457 spin_lock(&inode_lock);
458
459 if (inode != list_entry(inode_unused.next,
460 struct inode, i_list))
461 continue; /* wrong inode or list_empty */
462 if (!can_unuse(inode))
463 continue;
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 list_move(&inode->i_list, &freeable);
Nick Piggin7ef0d732009-03-12 14:31:38 -0700466 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 inode->i_state |= I_FREEING;
468 nr_pruned++;
469 }
470 inodes_stat.nr_unused -= nr_pruned;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700471 if (current_is_kswapd())
472 __count_vm_events(KSWAPD_INODESTEAL, reap);
473 else
474 __count_vm_events(PGINODESTEAL, reap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 spin_unlock(&inode_lock);
476
477 dispose_list(&freeable);
Ingo Molnarf24075b2006-03-23 03:00:34 -0800478 mutex_unlock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481/*
482 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
483 * "unused" means that no dentries are referring to the inodes: the files are
484 * not open and the dcache references to those inodes have already been
485 * reclaimed.
486 *
487 * This function is passed the number of inodes to scan, and it returns the
488 * total number of remaining possibly-reclaimable inodes.
489 */
Al Viro27496a82005-10-21 03:20:48 -0400490static int shrink_icache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 if (nr) {
493 /*
494 * Nasty deadlock avoidance. We may hold various FS locks,
495 * and we don't want to recurse into the FS that called us
496 * in clear_inode() and friends..
497 */
498 if (!(gfp_mask & __GFP_FS))
499 return -1;
500 prune_icache(nr);
501 }
502 return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
503}
504
Rusty Russell8e1f9362007-07-17 04:03:17 -0700505static struct shrinker icache_shrinker = {
506 .shrink = shrink_icache_memory,
507 .seeks = DEFAULT_SEEKS,
508};
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static void __wait_on_freeing_inode(struct inode *inode);
511/*
512 * Called with the inode lock held.
513 * NOTE: we are not increasing the inode-refcount, you must call __iget()
514 * by hand after calling find_inode now! This simplifies iunique and won't
515 * add any additional branch in the common code.
516 */
517static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
518{
519 struct hlist_node *node;
520 struct inode * inode = NULL;
521
522repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700523 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (inode->i_sb != sb)
525 continue;
526 if (!test(inode, data))
527 continue;
Alexander Viro991114c2005-06-23 00:09:01 -0700528 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 __wait_on_freeing_inode(inode);
530 goto repeat;
531 }
532 break;
533 }
534 return node ? inode : NULL;
535}
536
537/*
538 * find_inode_fast is the fast path version of find_inode, see the comment at
539 * iget_locked for details.
540 */
541static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
542{
543 struct hlist_node *node;
544 struct inode * inode = NULL;
545
546repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700547 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (inode->i_ino != ino)
549 continue;
550 if (inode->i_sb != sb)
551 continue;
Alexander Viro991114c2005-06-23 00:09:01 -0700552 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 __wait_on_freeing_inode(inode);
554 goto repeat;
555 }
556 break;
557 }
558 return node ? inode : NULL;
559}
560
David Chinner8290c352008-10-30 17:35:24 +1100561static unsigned long hash(struct super_block *sb, unsigned long hashval)
562{
563 unsigned long tmp;
564
565 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
566 L1_CACHE_BYTES;
567 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
568 return tmp & I_HASHMASK;
569}
570
571static inline void
572__inode_add_to_lists(struct super_block *sb, struct hlist_head *head,
573 struct inode *inode)
574{
575 inodes_stat.nr_inodes++;
576 list_add(&inode->i_list, &inode_in_use);
577 list_add(&inode->i_sb_list, &sb->s_inodes);
578 if (head)
579 hlist_add_head(&inode->i_hash, head);
580}
581
582/**
583 * inode_add_to_lists - add a new inode to relevant lists
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800584 * @sb: superblock inode belongs to
585 * @inode: inode to mark in use
David Chinner8290c352008-10-30 17:35:24 +1100586 *
587 * When an inode is allocated it needs to be accounted for, added to the in use
588 * list, the owning superblock and the inode hash. This needs to be done under
589 * the inode_lock, so export a function to do this rather than the inode lock
590 * itself. We calculate the hash list to add to here so it is all internal
591 * which requires the caller to have already set up the inode number in the
592 * inode to add.
593 */
594void inode_add_to_lists(struct super_block *sb, struct inode *inode)
595{
596 struct hlist_head *head = inode_hashtable + hash(sb, inode->i_ino);
597
598 spin_lock(&inode_lock);
599 __inode_add_to_lists(sb, head, inode);
600 spin_unlock(&inode_lock);
601}
602EXPORT_SYMBOL_GPL(inode_add_to_lists);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/**
605 * new_inode - obtain an inode
606 * @sb: superblock
607 *
Mel Gorman769848c2007-07-17 04:03:05 -0700608 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800609 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700610 * If HIGHMEM pages are unsuitable or it is known that pages allocated
611 * for the page cache are not reclaimable or migratable,
612 * mapping_set_gfp_mask() must be called with suitable flags on the
613 * newly created inode's mapping
614 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
616struct inode *new_inode(struct super_block *sb)
617{
Jeff Layton866b04f2007-05-08 00:32:29 -0700618 /*
619 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
620 * error if st_ino won't fit in target struct field. Use 32bit counter
621 * here to attempt to avoid that.
622 */
623 static unsigned int last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct inode * inode;
625
626 spin_lock_prefetch(&inode_lock);
627
628 inode = alloc_inode(sb);
629 if (inode) {
630 spin_lock(&inode_lock);
David Chinner8290c352008-10-30 17:35:24 +1100631 __inode_add_to_lists(sb, NULL, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 inode->i_ino = ++last_ino;
633 inode->i_state = 0;
634 spin_unlock(&inode_lock);
635 }
636 return inode;
637}
638
639EXPORT_SYMBOL(new_inode);
640
641void unlock_new_inode(struct inode *inode)
642{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200643#ifdef CONFIG_DEBUG_LOCK_ALLOC
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200644 if (inode->i_mode & S_IFDIR) {
645 struct file_system_type *type = inode->i_sb->s_type;
646
647 /*
648 * ensure nobody is actually holding i_mutex
649 */
650 mutex_destroy(&inode->i_mutex);
651 mutex_init(&inode->i_mutex);
Peter Zijlstra14358e62007-10-14 01:38:33 +0200652 lockdep_set_class(&inode->i_mutex, &type->i_mutex_dir_key);
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200653 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /*
656 * This is special! We do not need the spinlock
657 * when clearing I_LOCK, because we're guaranteed
658 * that nobody else tries to do anything about the
659 * state of the inode when it is locked, as we
660 * just created it (so there can be no old holders
661 * that haven't tested I_LOCK).
662 */
Nick Piggin7ef0d732009-03-12 14:31:38 -0700663 WARN_ON((inode->i_state & (I_LOCK|I_NEW)) != (I_LOCK|I_NEW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 inode->i_state &= ~(I_LOCK|I_NEW);
665 wake_up_inode(inode);
666}
667
668EXPORT_SYMBOL(unlock_new_inode);
669
670/*
671 * This is called without the inode lock held.. Be careful.
672 *
673 * We no longer cache the sb_flags in i_flags - see fs.h
674 * -- rmk@arm.uk.linux.org
675 */
676static 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)
677{
678 struct inode * inode;
679
680 inode = alloc_inode(sb);
681 if (inode) {
682 struct inode * old;
683
684 spin_lock(&inode_lock);
685 /* We released the lock, so.. */
686 old = find_inode(sb, head, test, data);
687 if (!old) {
688 if (set(inode, data))
689 goto set_failed;
690
David Chinner8290c352008-10-30 17:35:24 +1100691 __inode_add_to_lists(sb, head, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 inode->i_state = I_LOCK|I_NEW;
693 spin_unlock(&inode_lock);
694
695 /* Return the locked inode with I_NEW set, the
696 * caller is responsible for filling in the contents
697 */
698 return inode;
699 }
700
701 /*
702 * Uhhuh, somebody else created the same inode under
703 * us. Use the old inode instead of the one we just
704 * allocated.
705 */
706 __iget(old);
707 spin_unlock(&inode_lock);
708 destroy_inode(inode);
709 inode = old;
710 wait_on_inode(inode);
711 }
712 return inode;
713
714set_failed:
715 spin_unlock(&inode_lock);
716 destroy_inode(inode);
717 return NULL;
718}
719
720/*
721 * get_new_inode_fast is the fast path version of get_new_inode, see the
722 * comment at iget_locked for details.
723 */
724static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
725{
726 struct inode * inode;
727
728 inode = alloc_inode(sb);
729 if (inode) {
730 struct inode * old;
731
732 spin_lock(&inode_lock);
733 /* We released the lock, so.. */
734 old = find_inode_fast(sb, head, ino);
735 if (!old) {
736 inode->i_ino = ino;
David Chinner8290c352008-10-30 17:35:24 +1100737 __inode_add_to_lists(sb, head, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 inode->i_state = I_LOCK|I_NEW;
739 spin_unlock(&inode_lock);
740
741 /* Return the locked inode with I_NEW set, the
742 * caller is responsible for filling in the contents
743 */
744 return inode;
745 }
746
747 /*
748 * Uhhuh, somebody else created the same inode under
749 * us. Use the old inode instead of the one we just
750 * allocated.
751 */
752 __iget(old);
753 spin_unlock(&inode_lock);
754 destroy_inode(inode);
755 inode = old;
756 wait_on_inode(inode);
757 }
758 return inode;
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/**
762 * iunique - get a unique inode number
763 * @sb: superblock
764 * @max_reserved: highest reserved inode number
765 *
766 * Obtain an inode number that is unique on the system for a given
767 * superblock. This is used by file systems that have no natural
768 * permanent inode numbering system. An inode number is returned that
769 * is higher than the reserved limit but unique.
770 *
771 * BUGS:
772 * With a large number of inodes live on the file system this function
773 * currently becomes quite slow.
774 */
775ino_t iunique(struct super_block *sb, ino_t max_reserved)
776{
Jeff Layton866b04f2007-05-08 00:32:29 -0700777 /*
778 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
779 * error if st_ino won't fit in target struct field. Use 32bit counter
780 * here to attempt to avoid that.
781 */
782 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct inode *inode;
Jeffrey Layton3361c7b2007-05-08 00:29:48 -0700784 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jeffrey Layton3361c7b2007-05-08 00:29:48 -0700787 spin_lock(&inode_lock);
788 do {
789 if (counter <= max_reserved)
790 counter = max_reserved + 1;
791 res = counter++;
792 head = inode_hashtable + hash(sb, res);
793 inode = find_inode_fast(sb, head, res);
794 } while (inode != NULL);
795 spin_unlock(&inode_lock);
796
797 return res;
798}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799EXPORT_SYMBOL(iunique);
800
801struct inode *igrab(struct inode *inode)
802{
803 spin_lock(&inode_lock);
Jan Blunck4a3b0a42007-02-10 01:44:59 -0800804 if (!(inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 __iget(inode);
806 else
807 /*
808 * Handle the case where s_op->clear_inode is not been
809 * called yet, and somebody is calling igrab
810 * while the inode is getting freed.
811 */
812 inode = NULL;
813 spin_unlock(&inode_lock);
814 return inode;
815}
816
817EXPORT_SYMBOL(igrab);
818
819/**
820 * ifind - internal function, you want ilookup5() or iget5().
821 * @sb: super block of file system to search
822 * @head: the head of the list to search
823 * @test: callback used for comparisons between inodes
824 * @data: opaque data pointer to pass to @test
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700825 * @wait: if true wait for the inode to be unlocked, if false do not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *
827 * ifind() searches for the inode specified by @data in the inode
828 * cache. This is a generalized version of ifind_fast() for file systems where
829 * the inode number is not sufficient for unique identification of an inode.
830 *
831 * If the inode is in the cache, the inode is returned with an incremented
832 * reference count.
833 *
834 * Otherwise NULL is returned.
835 *
836 * Note, @test is called with the inode_lock held, so can't sleep.
837 */
Matt Mackall5d2bea42006-01-08 01:05:21 -0800838static struct inode *ifind(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct hlist_head *head, int (*test)(struct inode *, void *),
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700840 void *data, const int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct inode *inode;
843
844 spin_lock(&inode_lock);
845 inode = find_inode(sb, head, test, data);
846 if (inode) {
847 __iget(inode);
848 spin_unlock(&inode_lock);
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700849 if (likely(wait))
850 wait_on_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return inode;
852 }
853 spin_unlock(&inode_lock);
854 return NULL;
855}
856
857/**
858 * ifind_fast - internal function, you want ilookup() or iget().
859 * @sb: super block of file system to search
860 * @head: head of the list to search
861 * @ino: inode number to search for
862 *
863 * ifind_fast() searches for the inode @ino in the inode cache. This is for
864 * file systems where the inode number is sufficient for unique identification
865 * of an inode.
866 *
867 * If the inode is in the cache, the inode is returned with an incremented
868 * reference count.
869 *
870 * Otherwise NULL is returned.
871 */
Matt Mackall5d2bea42006-01-08 01:05:21 -0800872static struct inode *ifind_fast(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct hlist_head *head, unsigned long ino)
874{
875 struct inode *inode;
876
877 spin_lock(&inode_lock);
878 inode = find_inode_fast(sb, head, ino);
879 if (inode) {
880 __iget(inode);
881 spin_unlock(&inode_lock);
882 wait_on_inode(inode);
883 return inode;
884 }
885 spin_unlock(&inode_lock);
886 return NULL;
887}
888
889/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700890 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * @sb: super block of file system to search
892 * @hashval: hash value (usually inode number) to search for
893 * @test: callback used for comparisons between inodes
894 * @data: opaque data pointer to pass to @test
895 *
896 * ilookup5() uses ifind() to search for the inode specified by @hashval and
897 * @data in the inode cache. This is a generalized version of ilookup() for
898 * file systems where the inode number is not sufficient for unique
899 * identification of an inode.
900 *
901 * If the inode is in the cache, the inode is returned with an incremented
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700902 * reference count. Note, the inode lock is not waited upon so you have to be
903 * very careful what you do with the returned inode. You probably should be
904 * using ilookup5() instead.
905 *
906 * Otherwise NULL is returned.
907 *
908 * Note, @test is called with the inode_lock held, so can't sleep.
909 */
910struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
911 int (*test)(struct inode *, void *), void *data)
912{
913 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
914
915 return ifind(sb, head, test, data, 0);
916}
917
918EXPORT_SYMBOL(ilookup5_nowait);
919
920/**
921 * ilookup5 - search for an inode in the inode cache
922 * @sb: super block of file system to search
923 * @hashval: hash value (usually inode number) to search for
924 * @test: callback used for comparisons between inodes
925 * @data: opaque data pointer to pass to @test
926 *
927 * ilookup5() uses ifind() to search for the inode specified by @hashval and
928 * @data in the inode cache. This is a generalized version of ilookup() for
929 * file systems where the inode number is not sufficient for unique
930 * identification of an inode.
931 *
932 * If the inode is in the cache, the inode lock is waited upon and the inode is
933 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 *
935 * Otherwise NULL is returned.
936 *
937 * Note, @test is called with the inode_lock held, so can't sleep.
938 */
939struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
940 int (*test)(struct inode *, void *), void *data)
941{
942 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
943
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700944 return ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947EXPORT_SYMBOL(ilookup5);
948
949/**
950 * ilookup - search for an inode in the inode cache
951 * @sb: super block of file system to search
952 * @ino: inode number to search for
953 *
954 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
955 * This is for file systems where the inode number is sufficient for unique
956 * identification of an inode.
957 *
958 * If the inode is in the cache, the inode is returned with an incremented
959 * reference count.
960 *
961 * Otherwise NULL is returned.
962 */
963struct inode *ilookup(struct super_block *sb, unsigned long ino)
964{
965 struct hlist_head *head = inode_hashtable + hash(sb, ino);
966
967 return ifind_fast(sb, head, ino);
968}
969
970EXPORT_SYMBOL(ilookup);
971
972/**
973 * iget5_locked - obtain an inode from a mounted file system
974 * @sb: super block of file system
975 * @hashval: hash value (usually inode number) to get
976 * @test: callback used for comparisons between inodes
977 * @set: callback used to initialize a new struct inode
978 * @data: opaque data pointer to pass to @test and @set
979 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * iget5_locked() uses ifind() to search for the inode specified by @hashval
981 * and @data in the inode cache and if present it is returned with an increased
982 * reference count. This is a generalized version of iget_locked() for file
983 * systems where the inode number is not sufficient for unique identification
984 * of an inode.
985 *
986 * If the inode is not in cache, get_new_inode() is called to allocate a new
987 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
988 * file system gets to fill it in before unlocking it via unlock_new_inode().
989 *
990 * Note both @test and @set are called with the inode_lock held, so can't sleep.
991 */
992struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
993 int (*test)(struct inode *, void *),
994 int (*set)(struct inode *, void *), void *data)
995{
996 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
997 struct inode *inode;
998
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700999 inode = ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (inode)
1001 return inode;
1002 /*
1003 * get_new_inode() will do the right thing, re-trying the search
1004 * in case it had to block at any point.
1005 */
1006 return get_new_inode(sb, head, test, set, data);
1007}
1008
1009EXPORT_SYMBOL(iget5_locked);
1010
1011/**
1012 * iget_locked - obtain an inode from a mounted file system
1013 * @sb: super block of file system
1014 * @ino: inode number to get
1015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1017 * the inode cache and if present it is returned with an increased reference
1018 * count. This is for file systems where the inode number is sufficient for
1019 * unique identification of an inode.
1020 *
1021 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1022 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1023 * The file system gets to fill it in before unlocking it via
1024 * unlock_new_inode().
1025 */
1026struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1027{
1028 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1029 struct inode *inode;
1030
1031 inode = ifind_fast(sb, head, ino);
1032 if (inode)
1033 return inode;
1034 /*
1035 * get_new_inode_fast() will do the right thing, re-trying the search
1036 * in case it had to block at any point.
1037 */
1038 return get_new_inode_fast(sb, head, ino);
1039}
1040
1041EXPORT_SYMBOL(iget_locked);
1042
Al Viro261bca82008-12-30 01:48:21 -05001043int insert_inode_locked(struct inode *inode)
1044{
1045 struct super_block *sb = inode->i_sb;
1046 ino_t ino = inode->i_ino;
1047 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1048 struct inode *old;
1049
1050 inode->i_state |= I_LOCK|I_NEW;
1051 while (1) {
1052 spin_lock(&inode_lock);
1053 old = find_inode_fast(sb, head, ino);
1054 if (likely(!old)) {
1055 hlist_add_head(&inode->i_hash, head);
1056 spin_unlock(&inode_lock);
1057 return 0;
1058 }
1059 __iget(old);
1060 spin_unlock(&inode_lock);
1061 wait_on_inode(old);
1062 if (unlikely(!hlist_unhashed(&old->i_hash))) {
1063 iput(old);
1064 return -EBUSY;
1065 }
1066 iput(old);
1067 }
1068}
1069
1070EXPORT_SYMBOL(insert_inode_locked);
1071
1072int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1073 int (*test)(struct inode *, void *), void *data)
1074{
1075 struct super_block *sb = inode->i_sb;
1076 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1077 struct inode *old;
1078
1079 inode->i_state |= I_LOCK|I_NEW;
1080
1081 while (1) {
1082 spin_lock(&inode_lock);
1083 old = find_inode(sb, head, test, data);
1084 if (likely(!old)) {
1085 hlist_add_head(&inode->i_hash, head);
1086 spin_unlock(&inode_lock);
1087 return 0;
1088 }
1089 __iget(old);
1090 spin_unlock(&inode_lock);
1091 wait_on_inode(old);
1092 if (unlikely(!hlist_unhashed(&old->i_hash))) {
1093 iput(old);
1094 return -EBUSY;
1095 }
1096 iput(old);
1097 }
1098}
1099
1100EXPORT_SYMBOL(insert_inode_locked4);
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/**
1103 * __insert_inode_hash - hash an inode
1104 * @inode: unhashed inode
1105 * @hashval: unsigned long value used to locate this object in the
1106 * inode_hashtable.
1107 *
1108 * Add an inode to the inode hash for this superblock.
1109 */
1110void __insert_inode_hash(struct inode *inode, unsigned long hashval)
1111{
1112 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1113 spin_lock(&inode_lock);
1114 hlist_add_head(&inode->i_hash, head);
1115 spin_unlock(&inode_lock);
1116}
1117
1118EXPORT_SYMBOL(__insert_inode_hash);
1119
1120/**
1121 * remove_inode_hash - remove an inode from the hash
1122 * @inode: inode to unhash
1123 *
1124 * Remove an inode from the superblock.
1125 */
1126void remove_inode_hash(struct inode *inode)
1127{
1128 spin_lock(&inode_lock);
1129 hlist_del_init(&inode->i_hash);
1130 spin_unlock(&inode_lock);
1131}
1132
1133EXPORT_SYMBOL(remove_inode_hash);
1134
1135/*
1136 * Tell the filesystem that this inode is no longer of any interest and should
1137 * be completely destroyed.
1138 *
1139 * We leave the inode in the inode hash table until *after* the filesystem's
1140 * ->delete_inode completes. This ensures that an iget (such as nfsd might
1141 * instigate) will always find up-to-date information either in the hash or on
1142 * disk.
1143 *
1144 * I_FREEING is set so that no-one will take a new reference to the inode while
1145 * it is being deleted.
1146 */
Arjan van de Venb32714b2009-01-09 07:04:15 -08001147void generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001149 const struct super_operations *op = inode->i_sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Arjan van de Venb32714b2009-01-09 07:04:15 -08001151 list_del_init(&inode->i_list);
1152 list_del_init(&inode->i_sb_list);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001153 WARN_ON(inode->i_state & I_NEW);
Arjan van de Venb32714b2009-01-09 07:04:15 -08001154 inode->i_state |= I_FREEING;
1155 inodes_stat.nr_inodes--;
1156 spin_unlock(&inode_lock);
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 security_inode_delete(inode);
1159
1160 if (op->delete_inode) {
1161 void (*delete)(struct inode *) = op->delete_inode;
1162 if (!is_bad_inode(inode))
1163 DQUOT_INIT(inode);
Mark Fashehe85b5652005-09-09 13:01:29 -07001164 /* Filesystems implementing their own
1165 * s_op->delete_inode are required to call
1166 * truncate_inode_pages and clear_inode()
1167 * internally */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 delete(inode);
Mark Fashehe85b5652005-09-09 13:01:29 -07001169 } else {
1170 truncate_inode_pages(&inode->i_data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 clear_inode(inode);
Mark Fashehe85b5652005-09-09 13:01:29 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 spin_lock(&inode_lock);
1174 hlist_del_init(&inode->i_hash);
1175 spin_unlock(&inode_lock);
1176 wake_up_inode(inode);
Eric Sesterhennb7542f82006-04-02 13:38:18 +02001177 BUG_ON(inode->i_state != I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 destroy_inode(inode);
1179}
1180
1181EXPORT_SYMBOL(generic_delete_inode);
1182
1183static void generic_forget_inode(struct inode *inode)
1184{
1185 struct super_block *sb = inode->i_sb;
1186
1187 if (!hlist_unhashed(&inode->i_hash)) {
Joern Engel1c0eeaf2007-10-16 23:30:44 -07001188 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 list_move(&inode->i_list, &inode_unused);
1190 inodes_stat.nr_unused++;
Christoph Hellwigacb0c852007-05-08 00:25:52 -07001191 if (sb->s_flags & MS_ACTIVE) {
Alexander Viro991114c2005-06-23 00:09:01 -07001192 spin_unlock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return;
Alexander Viro991114c2005-06-23 00:09:01 -07001194 }
Nick Piggin7ef0d732009-03-12 14:31:38 -07001195 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001196 inode->i_state |= I_WILL_FREE;
1197 spin_unlock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 write_inode_now(inode, 1);
1199 spin_lock(&inode_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001200 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001201 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 inodes_stat.nr_unused--;
1203 hlist_del_init(&inode->i_hash);
1204 }
1205 list_del_init(&inode->i_list);
1206 list_del_init(&inode->i_sb_list);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001207 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001208 inode->i_state |= I_FREEING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 inodes_stat.nr_inodes--;
1210 spin_unlock(&inode_lock);
1211 if (inode->i_data.nrpages)
1212 truncate_inode_pages(&inode->i_data, 0);
1213 clear_inode(inode);
Andrea Arcangeli7f04c262005-10-30 15:03:05 -08001214 wake_up_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 destroy_inode(inode);
1216}
1217
1218/*
1219 * Normal UNIX filesystem behaviour: delete the
1220 * inode when the usage count drops to zero, and
1221 * i_nlink is zero.
1222 */
Mark Fashehcb2c0232005-07-07 17:56:03 -07001223void generic_drop_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 if (!inode->i_nlink)
1226 generic_delete_inode(inode);
1227 else
1228 generic_forget_inode(inode);
1229}
1230
Mark Fashehcb2c0232005-07-07 17:56:03 -07001231EXPORT_SYMBOL_GPL(generic_drop_inode);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233/*
1234 * Called when we're dropping the last reference
1235 * to an inode.
1236 *
1237 * Call the FS "drop()" function, defaulting to
1238 * the legacy UNIX filesystem behaviour..
1239 *
1240 * NOTE! NOTE! NOTE! We're called with the inode lock
1241 * held, and the drop function is supposed to release
1242 * the lock!
1243 */
1244static inline void iput_final(struct inode *inode)
1245{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001246 const struct super_operations *op = inode->i_sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 void (*drop)(struct inode *) = generic_drop_inode;
1248
1249 if (op && op->drop_inode)
1250 drop = op->drop_inode;
1251 drop(inode);
1252}
1253
1254/**
1255 * iput - put an inode
1256 * @inode: inode to put
1257 *
1258 * Puts an inode, dropping its usage count. If the inode use count hits
1259 * zero, the inode is then freed and may also be destroyed.
1260 *
1261 * Consequently, iput() can sleep.
1262 */
1263void iput(struct inode *inode)
1264{
1265 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 BUG_ON(inode->i_state == I_CLEAR);
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1269 iput_final(inode);
1270 }
1271}
1272
1273EXPORT_SYMBOL(iput);
1274
1275/**
1276 * bmap - find a block number in a file
1277 * @inode: inode of file
1278 * @block: block to find
1279 *
1280 * Returns the block number on the device holding the inode that
1281 * is the disk block number for the block of the file requested.
1282 * That is, asked for block 4 of inode 1 the function will return the
1283 * disk block relative to the disk start that holds that block of the
1284 * file.
1285 */
1286sector_t bmap(struct inode * inode, sector_t block)
1287{
1288 sector_t res = 0;
1289 if (inode->i_mapping->a_ops->bmap)
1290 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1291 return res;
1292}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293EXPORT_SYMBOL(bmap);
1294
1295/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001296 * touch_atime - update the access time
1297 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001298 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 *
1300 * Update the accessed time on an inode and mark it for writeback.
1301 * This function automatically handles read only file systems and media,
1302 * as well as the "noatime" flag and inode specific "noatime" markers.
1303 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001304void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001306 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct timespec now;
1308
Dave Hansencdb70f32008-02-15 14:37:41 -08001309 if (mnt_want_write(mnt))
1310 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001311 if (inode->i_flags & S_NOATIME)
Dave Hansencdb70f32008-02-15 14:37:41 -08001312 goto out;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001313 if (IS_NOATIME(inode))
Dave Hansencdb70f32008-02-15 14:37:41 -08001314 goto out;
Andrew Mortonb2276132006-12-13 00:34:33 -08001315 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Dave Hansencdb70f32008-02-15 14:37:41 -08001316 goto out;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001317
Dave Hansencdb70f32008-02-15 14:37:41 -08001318 if (mnt->mnt_flags & MNT_NOATIME)
1319 goto out;
1320 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1321 goto out;
1322 if (mnt->mnt_flags & MNT_RELATIME) {
1323 /*
1324 * With relative atime, only update atime if the previous
1325 * atime is earlier than either the ctime or mtime.
1326 */
1327 if (timespec_compare(&inode->i_mtime, &inode->i_atime) < 0 &&
1328 timespec_compare(&inode->i_ctime, &inode->i_atime) < 0)
1329 goto out;
Andrew Mortonb2276132006-12-13 00:34:33 -08001330 }
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 now = current_fs_time(inode->i_sb);
Valerie Henson47ae32d2006-12-13 00:34:34 -08001333 if (timespec_equal(&inode->i_atime, &now))
Dave Hansencdb70f32008-02-15 14:37:41 -08001334 goto out;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001335
1336 inode->i_atime = now;
1337 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001338out:
1339 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001341EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001344 * file_update_time - update mtime and ctime time
1345 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001347 * Update the mtime and ctime members of an inode and mark the inode
1348 * for writeback. Note that this function is meant exclusively for
1349 * usage in the file write path of filesystems, and filesystems may
1350 * choose to explicitly ignore update via this function with the
1351 * S_NOCTIME inode flag, e.g. for network filesystem where these
1352 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354
Christoph Hellwig870f4812006-01-09 20:52:01 -08001355void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001357 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 struct timespec now;
1359 int sync_it = 0;
Dave Hansen20ddee22008-02-15 14:37:43 -08001360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 if (IS_NOCMTIME(inode))
1363 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001364
1365 err = mnt_want_write(file->f_path.mnt);
1366 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return;
1368
1369 now = current_fs_time(inode->i_sb);
Andreas Mohred97bd32006-10-02 02:17:17 -07001370 if (!timespec_equal(&inode->i_mtime, &now)) {
1371 inode->i_mtime = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 sync_it = 1;
Andreas Mohred97bd32006-10-02 02:17:17 -07001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Andreas Mohred97bd32006-10-02 02:17:17 -07001375 if (!timespec_equal(&inode->i_ctime, &now)) {
1376 inode->i_ctime = now;
Christoph Hellwig870f4812006-01-09 20:52:01 -08001377 sync_it = 1;
Andreas Mohred97bd32006-10-02 02:17:17 -07001378 }
Christoph Hellwig870f4812006-01-09 20:52:01 -08001379
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001380 if (IS_I_VERSION(inode)) {
1381 inode_inc_iversion(inode);
1382 sync_it = 1;
1383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (sync_it)
1386 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001387 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Christoph Hellwig870f4812006-01-09 20:52:01 -08001390EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392int inode_needs_sync(struct inode *inode)
1393{
1394 if (IS_SYNC(inode))
1395 return 1;
1396 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1397 return 1;
1398 return 0;
1399}
1400
1401EXPORT_SYMBOL(inode_needs_sync);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403int inode_wait(void *word)
1404{
1405 schedule();
1406 return 0;
1407}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001408EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001411 * If we try to find an inode in the inode hash while it is being
1412 * deleted, we have to wait until the filesystem completes its
1413 * deletion before reporting that it isn't found. This function waits
1414 * until the deletion _might_ have completed. Callers are responsible
1415 * to recheck inode state.
1416 *
1417 * It doesn't matter if I_LOCK is not set initially, a call to
1418 * wake_up_inode() after removing from the hash list will DTRT.
1419 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * This is called with inode_lock held.
1421 */
1422static void __wait_on_freeing_inode(struct inode *inode)
1423{
1424 wait_queue_head_t *wq;
1425 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 wq = bit_waitqueue(&inode->i_state, __I_LOCK);
1427 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1428 spin_unlock(&inode_lock);
1429 schedule();
1430 finish_wait(wq, &wait.wait);
1431 spin_lock(&inode_lock);
1432}
1433
Mark Fasheh62752ee2006-10-17 10:31:38 +02001434/*
1435 * We rarely want to lock two inodes that do not have a parent/child
1436 * relationship (such as directory, child inode) simultaneously. The
1437 * vast majority of file systems should be able to get along fine
1438 * without this. Do not use these functions except as a last resort.
1439 */
1440void inode_double_lock(struct inode *inode1, struct inode *inode2)
1441{
1442 if (inode1 == NULL || inode2 == NULL || inode1 == inode2) {
1443 if (inode1)
1444 mutex_lock(&inode1->i_mutex);
1445 else if (inode2)
1446 mutex_lock(&inode2->i_mutex);
1447 return;
1448 }
1449
1450 if (inode1 < inode2) {
1451 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1452 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1453 } else {
1454 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1455 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1456 }
1457}
1458EXPORT_SYMBOL(inode_double_lock);
1459
1460void inode_double_unlock(struct inode *inode1, struct inode *inode2)
1461{
1462 if (inode1)
1463 mutex_unlock(&inode1->i_mutex);
1464
1465 if (inode2 && inode2 != inode1)
1466 mutex_unlock(&inode2->i_mutex);
1467}
1468EXPORT_SYMBOL(inode_double_unlock);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470static __initdata unsigned long ihash_entries;
1471static int __init set_ihash_entries(char *str)
1472{
1473 if (!str)
1474 return 0;
1475 ihash_entries = simple_strtoul(str, &str, 0);
1476 return 1;
1477}
1478__setup("ihash_entries=", set_ihash_entries);
1479
1480/*
1481 * Initialize the waitqueues and inode hash table.
1482 */
1483void __init inode_init_early(void)
1484{
1485 int loop;
1486
1487 /* If hashes are distributed across NUMA nodes, defer
1488 * hash allocation until vmalloc space is available.
1489 */
1490 if (hashdist)
1491 return;
1492
1493 inode_hashtable =
1494 alloc_large_system_hash("Inode-cache",
1495 sizeof(struct hlist_head),
1496 ihash_entries,
1497 14,
1498 HASH_EARLY,
1499 &i_hash_shift,
1500 &i_hash_mask,
1501 0);
1502
1503 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1504 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1505}
1506
Denis Cheng74bf17c2007-10-16 23:26:30 -07001507void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 int loop;
1510
1511 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001512 inode_cachep = kmem_cache_create("inode_cache",
1513 sizeof(struct inode),
1514 0,
1515 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1516 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001517 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001518 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /* Hash may have been set up in inode_init_early */
1521 if (!hashdist)
1522 return;
1523
1524 inode_hashtable =
1525 alloc_large_system_hash("Inode-cache",
1526 sizeof(struct hlist_head),
1527 ihash_entries,
1528 14,
1529 0,
1530 &i_hash_shift,
1531 &i_hash_mask,
1532 0);
1533
1534 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1535 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1536}
1537
1538void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1539{
1540 inode->i_mode = mode;
1541 if (S_ISCHR(mode)) {
1542 inode->i_fop = &def_chr_fops;
1543 inode->i_rdev = rdev;
1544 } else if (S_ISBLK(mode)) {
1545 inode->i_fop = &def_blk_fops;
1546 inode->i_rdev = rdev;
1547 } else if (S_ISFIFO(mode))
1548 inode->i_fop = &def_fifo_fops;
1549 else if (S_ISSOCK(mode))
1550 inode->i_fop = &bad_sock_fops;
1551 else
1552 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1553 mode);
1554}
1555EXPORT_SYMBOL(init_special_inode);