blob: 913ab2d9a5d10ecd183630c1a25770435613aca5 [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);
359 invalidate_inode_buffers(inode);
360 if (!atomic_read(&inode->i_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 list_move(&inode->i_list, dispose);
362 inode->i_state |= I_FREEING;
363 count++;
364 continue;
365 }
366 busy = 1;
367 }
368 /* only unused inodes may be cached with i_count zero */
369 inodes_stat.nr_unused -= count;
370 return busy;
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/**
374 * invalidate_inodes - discard the inodes on a device
375 * @sb: superblock
376 *
377 * Discard all of the inodes for a given superblock. If the discard
378 * fails because there are busy inodes then a non zero value is returned.
379 * If the discard is successful all the inodes have been discarded.
380 */
381int invalidate_inodes(struct super_block * sb)
382{
383 int busy;
384 LIST_HEAD(throw_away);
385
Ingo Molnarf24075b2006-03-23 03:00:34 -0800386 mutex_lock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 spin_lock(&inode_lock);
Robert Love0eeca282005-07-12 17:06:03 -0400388 inotify_unmount_inodes(&sb->s_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 busy = invalidate_list(&sb->s_inodes, &throw_away);
390 spin_unlock(&inode_lock);
391
392 dispose_list(&throw_away);
Ingo Molnarf24075b2006-03-23 03:00:34 -0800393 mutex_unlock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 return busy;
396}
397
398EXPORT_SYMBOL(invalidate_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400static int can_unuse(struct inode *inode)
401{
402 if (inode->i_state)
403 return 0;
404 if (inode_has_buffers(inode))
405 return 0;
406 if (atomic_read(&inode->i_count))
407 return 0;
408 if (inode->i_data.nrpages)
409 return 0;
410 return 1;
411}
412
413/*
414 * Scan `goal' inodes on the unused list for freeable ones. They are moved to
415 * a temporary list and then are freed outside inode_lock by dispose_list().
416 *
417 * Any inodes which are pinned purely because of attached pagecache have their
418 * pagecache removed. We expect the final iput() on that inode to add it to
419 * the front of the inode_unused list. So look for it there and if the
420 * inode is still freeable, proceed. The right inode is found 99.9% of the
421 * time in testing on a 4-way.
422 *
423 * If the inode has metadata buffers attached to mapping->private_list then
424 * try to remove them.
425 */
426static void prune_icache(int nr_to_scan)
427{
428 LIST_HEAD(freeable);
429 int nr_pruned = 0;
430 int nr_scanned;
431 unsigned long reap = 0;
432
Ingo Molnarf24075b2006-03-23 03:00:34 -0800433 mutex_lock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_lock(&inode_lock);
435 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
436 struct inode *inode;
437
438 if (list_empty(&inode_unused))
439 break;
440
441 inode = list_entry(inode_unused.prev, struct inode, i_list);
442
443 if (inode->i_state || atomic_read(&inode->i_count)) {
444 list_move(&inode->i_list, &inode_unused);
445 continue;
446 }
447 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
448 __iget(inode);
449 spin_unlock(&inode_lock);
450 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800451 reap += invalidate_mapping_pages(&inode->i_data,
452 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 iput(inode);
454 spin_lock(&inode_lock);
455
456 if (inode != list_entry(inode_unused.next,
457 struct inode, i_list))
458 continue; /* wrong inode or list_empty */
459 if (!can_unuse(inode))
460 continue;
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 list_move(&inode->i_list, &freeable);
463 inode->i_state |= I_FREEING;
464 nr_pruned++;
465 }
466 inodes_stat.nr_unused -= nr_pruned;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700467 if (current_is_kswapd())
468 __count_vm_events(KSWAPD_INODESTEAL, reap);
469 else
470 __count_vm_events(PGINODESTEAL, reap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 spin_unlock(&inode_lock);
472
473 dispose_list(&freeable);
Ingo Molnarf24075b2006-03-23 03:00:34 -0800474 mutex_unlock(&iprune_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/*
478 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
479 * "unused" means that no dentries are referring to the inodes: the files are
480 * not open and the dcache references to those inodes have already been
481 * reclaimed.
482 *
483 * This function is passed the number of inodes to scan, and it returns the
484 * total number of remaining possibly-reclaimable inodes.
485 */
Al Viro27496a82005-10-21 03:20:48 -0400486static int shrink_icache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 if (nr) {
489 /*
490 * Nasty deadlock avoidance. We may hold various FS locks,
491 * and we don't want to recurse into the FS that called us
492 * in clear_inode() and friends..
493 */
494 if (!(gfp_mask & __GFP_FS))
495 return -1;
496 prune_icache(nr);
497 }
498 return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
499}
500
Rusty Russell8e1f9362007-07-17 04:03:17 -0700501static struct shrinker icache_shrinker = {
502 .shrink = shrink_icache_memory,
503 .seeks = DEFAULT_SEEKS,
504};
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static void __wait_on_freeing_inode(struct inode *inode);
507/*
508 * Called with the inode lock held.
509 * NOTE: we are not increasing the inode-refcount, you must call __iget()
510 * by hand after calling find_inode now! This simplifies iunique and won't
511 * add any additional branch in the common code.
512 */
513static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
514{
515 struct hlist_node *node;
516 struct inode * inode = NULL;
517
518repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700519 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (inode->i_sb != sb)
521 continue;
522 if (!test(inode, data))
523 continue;
Alexander Viro991114c2005-06-23 00:09:01 -0700524 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 __wait_on_freeing_inode(inode);
526 goto repeat;
527 }
528 break;
529 }
530 return node ? inode : NULL;
531}
532
533/*
534 * find_inode_fast is the fast path version of find_inode, see the comment at
535 * iget_locked for details.
536 */
537static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
538{
539 struct hlist_node *node;
540 struct inode * inode = NULL;
541
542repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700543 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (inode->i_ino != ino)
545 continue;
546 if (inode->i_sb != sb)
547 continue;
Alexander Viro991114c2005-06-23 00:09:01 -0700548 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 __wait_on_freeing_inode(inode);
550 goto repeat;
551 }
552 break;
553 }
554 return node ? inode : NULL;
555}
556
David Chinner8290c352008-10-30 17:35:24 +1100557static unsigned long hash(struct super_block *sb, unsigned long hashval)
558{
559 unsigned long tmp;
560
561 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
562 L1_CACHE_BYTES;
563 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
564 return tmp & I_HASHMASK;
565}
566
567static inline void
568__inode_add_to_lists(struct super_block *sb, struct hlist_head *head,
569 struct inode *inode)
570{
571 inodes_stat.nr_inodes++;
572 list_add(&inode->i_list, &inode_in_use);
573 list_add(&inode->i_sb_list, &sb->s_inodes);
574 if (head)
575 hlist_add_head(&inode->i_hash, head);
576}
577
578/**
579 * inode_add_to_lists - add a new inode to relevant lists
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800580 * @sb: superblock inode belongs to
581 * @inode: inode to mark in use
David Chinner8290c352008-10-30 17:35:24 +1100582 *
583 * When an inode is allocated it needs to be accounted for, added to the in use
584 * list, the owning superblock and the inode hash. This needs to be done under
585 * the inode_lock, so export a function to do this rather than the inode lock
586 * itself. We calculate the hash list to add to here so it is all internal
587 * which requires the caller to have already set up the inode number in the
588 * inode to add.
589 */
590void inode_add_to_lists(struct super_block *sb, struct inode *inode)
591{
592 struct hlist_head *head = inode_hashtable + hash(sb, inode->i_ino);
593
594 spin_lock(&inode_lock);
595 __inode_add_to_lists(sb, head, inode);
596 spin_unlock(&inode_lock);
597}
598EXPORT_SYMBOL_GPL(inode_add_to_lists);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
601 * new_inode - obtain an inode
602 * @sb: superblock
603 *
Mel Gorman769848c2007-07-17 04:03:05 -0700604 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800605 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700606 * If HIGHMEM pages are unsuitable or it is known that pages allocated
607 * for the page cache are not reclaimable or migratable,
608 * mapping_set_gfp_mask() must be called with suitable flags on the
609 * newly created inode's mapping
610 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
612struct inode *new_inode(struct super_block *sb)
613{
Jeff Layton866b04f2007-05-08 00:32:29 -0700614 /*
615 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
616 * error if st_ino won't fit in target struct field. Use 32bit counter
617 * here to attempt to avoid that.
618 */
619 static unsigned int last_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct inode * inode;
621
622 spin_lock_prefetch(&inode_lock);
623
624 inode = alloc_inode(sb);
625 if (inode) {
626 spin_lock(&inode_lock);
David Chinner8290c352008-10-30 17:35:24 +1100627 __inode_add_to_lists(sb, NULL, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 inode->i_ino = ++last_ino;
629 inode->i_state = 0;
630 spin_unlock(&inode_lock);
631 }
632 return inode;
633}
634
635EXPORT_SYMBOL(new_inode);
636
637void unlock_new_inode(struct inode *inode)
638{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200639#ifdef CONFIG_DEBUG_LOCK_ALLOC
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200640 if (inode->i_mode & S_IFDIR) {
641 struct file_system_type *type = inode->i_sb->s_type;
642
643 /*
644 * ensure nobody is actually holding i_mutex
645 */
646 mutex_destroy(&inode->i_mutex);
647 mutex_init(&inode->i_mutex);
Peter Zijlstra14358e62007-10-14 01:38:33 +0200648 lockdep_set_class(&inode->i_mutex, &type->i_mutex_dir_key);
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200649 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200650#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
652 * This is special! We do not need the spinlock
653 * when clearing I_LOCK, because we're guaranteed
654 * that nobody else tries to do anything about the
655 * state of the inode when it is locked, as we
656 * just created it (so there can be no old holders
657 * that haven't tested I_LOCK).
658 */
659 inode->i_state &= ~(I_LOCK|I_NEW);
660 wake_up_inode(inode);
661}
662
663EXPORT_SYMBOL(unlock_new_inode);
664
665/*
666 * This is called without the inode lock held.. Be careful.
667 *
668 * We no longer cache the sb_flags in i_flags - see fs.h
669 * -- rmk@arm.uk.linux.org
670 */
671static 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)
672{
673 struct inode * inode;
674
675 inode = alloc_inode(sb);
676 if (inode) {
677 struct inode * old;
678
679 spin_lock(&inode_lock);
680 /* We released the lock, so.. */
681 old = find_inode(sb, head, test, data);
682 if (!old) {
683 if (set(inode, data))
684 goto set_failed;
685
David Chinner8290c352008-10-30 17:35:24 +1100686 __inode_add_to_lists(sb, head, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 inode->i_state = I_LOCK|I_NEW;
688 spin_unlock(&inode_lock);
689
690 /* Return the locked inode with I_NEW set, the
691 * caller is responsible for filling in the contents
692 */
693 return inode;
694 }
695
696 /*
697 * Uhhuh, somebody else created the same inode under
698 * us. Use the old inode instead of the one we just
699 * allocated.
700 */
701 __iget(old);
702 spin_unlock(&inode_lock);
703 destroy_inode(inode);
704 inode = old;
705 wait_on_inode(inode);
706 }
707 return inode;
708
709set_failed:
710 spin_unlock(&inode_lock);
711 destroy_inode(inode);
712 return NULL;
713}
714
715/*
716 * get_new_inode_fast is the fast path version of get_new_inode, see the
717 * comment at iget_locked for details.
718 */
719static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
720{
721 struct inode * inode;
722
723 inode = alloc_inode(sb);
724 if (inode) {
725 struct inode * old;
726
727 spin_lock(&inode_lock);
728 /* We released the lock, so.. */
729 old = find_inode_fast(sb, head, ino);
730 if (!old) {
731 inode->i_ino = ino;
David Chinner8290c352008-10-30 17:35:24 +1100732 __inode_add_to_lists(sb, head, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 inode->i_state = I_LOCK|I_NEW;
734 spin_unlock(&inode_lock);
735
736 /* Return the locked inode with I_NEW set, the
737 * caller is responsible for filling in the contents
738 */
739 return inode;
740 }
741
742 /*
743 * Uhhuh, somebody else created the same inode under
744 * us. Use the old inode instead of the one we just
745 * allocated.
746 */
747 __iget(old);
748 spin_unlock(&inode_lock);
749 destroy_inode(inode);
750 inode = old;
751 wait_on_inode(inode);
752 }
753 return inode;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/**
757 * iunique - get a unique inode number
758 * @sb: superblock
759 * @max_reserved: highest reserved inode number
760 *
761 * Obtain an inode number that is unique on the system for a given
762 * superblock. This is used by file systems that have no natural
763 * permanent inode numbering system. An inode number is returned that
764 * is higher than the reserved limit but unique.
765 *
766 * BUGS:
767 * With a large number of inodes live on the file system this function
768 * currently becomes quite slow.
769 */
770ino_t iunique(struct super_block *sb, ino_t max_reserved)
771{
Jeff Layton866b04f2007-05-08 00:32:29 -0700772 /*
773 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
774 * error if st_ino won't fit in target struct field. Use 32bit counter
775 * here to attempt to avoid that.
776 */
777 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct inode *inode;
Jeffrey Layton3361c7b2007-05-08 00:29:48 -0700779 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Jeffrey Layton3361c7b2007-05-08 00:29:48 -0700782 spin_lock(&inode_lock);
783 do {
784 if (counter <= max_reserved)
785 counter = max_reserved + 1;
786 res = counter++;
787 head = inode_hashtable + hash(sb, res);
788 inode = find_inode_fast(sb, head, res);
789 } while (inode != NULL);
790 spin_unlock(&inode_lock);
791
792 return res;
793}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794EXPORT_SYMBOL(iunique);
795
796struct inode *igrab(struct inode *inode)
797{
798 spin_lock(&inode_lock);
Jan Blunck4a3b0a42007-02-10 01:44:59 -0800799 if (!(inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 __iget(inode);
801 else
802 /*
803 * Handle the case where s_op->clear_inode is not been
804 * called yet, and somebody is calling igrab
805 * while the inode is getting freed.
806 */
807 inode = NULL;
808 spin_unlock(&inode_lock);
809 return inode;
810}
811
812EXPORT_SYMBOL(igrab);
813
814/**
815 * ifind - internal function, you want ilookup5() or iget5().
816 * @sb: super block of file system to search
817 * @head: the head of the list to search
818 * @test: callback used for comparisons between inodes
819 * @data: opaque data pointer to pass to @test
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700820 * @wait: if true wait for the inode to be unlocked, if false do not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
822 * ifind() searches for the inode specified by @data in the inode
823 * cache. This is a generalized version of ifind_fast() for file systems where
824 * the inode number is not sufficient for unique identification of an inode.
825 *
826 * If the inode is in the cache, the inode is returned with an incremented
827 * reference count.
828 *
829 * Otherwise NULL is returned.
830 *
831 * Note, @test is called with the inode_lock held, so can't sleep.
832 */
Matt Mackall5d2bea42006-01-08 01:05:21 -0800833static struct inode *ifind(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct hlist_head *head, int (*test)(struct inode *, void *),
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700835 void *data, const int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct inode *inode;
838
839 spin_lock(&inode_lock);
840 inode = find_inode(sb, head, test, data);
841 if (inode) {
842 __iget(inode);
843 spin_unlock(&inode_lock);
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700844 if (likely(wait))
845 wait_on_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return inode;
847 }
848 spin_unlock(&inode_lock);
849 return NULL;
850}
851
852/**
853 * ifind_fast - internal function, you want ilookup() or iget().
854 * @sb: super block of file system to search
855 * @head: head of the list to search
856 * @ino: inode number to search for
857 *
858 * ifind_fast() searches for the inode @ino in the inode cache. This is for
859 * file systems where the inode number is sufficient for unique identification
860 * of an inode.
861 *
862 * If the inode is in the cache, the inode is returned with an incremented
863 * reference count.
864 *
865 * Otherwise NULL is returned.
866 */
Matt Mackall5d2bea42006-01-08 01:05:21 -0800867static struct inode *ifind_fast(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct hlist_head *head, unsigned long ino)
869{
870 struct inode *inode;
871
872 spin_lock(&inode_lock);
873 inode = find_inode_fast(sb, head, ino);
874 if (inode) {
875 __iget(inode);
876 spin_unlock(&inode_lock);
877 wait_on_inode(inode);
878 return inode;
879 }
880 spin_unlock(&inode_lock);
881 return NULL;
882}
883
884/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700885 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * @sb: super block of file system to search
887 * @hashval: hash value (usually inode number) to search for
888 * @test: callback used for comparisons between inodes
889 * @data: opaque data pointer to pass to @test
890 *
891 * ilookup5() uses ifind() to search for the inode specified by @hashval and
892 * @data in the inode cache. This is a generalized version of ilookup() for
893 * file systems where the inode number is not sufficient for unique
894 * identification of an inode.
895 *
896 * If the inode is in the cache, the inode is returned with an incremented
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700897 * reference count. Note, the inode lock is not waited upon so you have to be
898 * very careful what you do with the returned inode. You probably should be
899 * using ilookup5() instead.
900 *
901 * Otherwise NULL is returned.
902 *
903 * Note, @test is called with the inode_lock held, so can't sleep.
904 */
905struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
906 int (*test)(struct inode *, void *), void *data)
907{
908 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
909
910 return ifind(sb, head, test, data, 0);
911}
912
913EXPORT_SYMBOL(ilookup5_nowait);
914
915/**
916 * ilookup5 - search for an inode in the inode cache
917 * @sb: super block of file system to search
918 * @hashval: hash value (usually inode number) to search for
919 * @test: callback used for comparisons between inodes
920 * @data: opaque data pointer to pass to @test
921 *
922 * ilookup5() uses ifind() to search for the inode specified by @hashval and
923 * @data in the inode cache. This is a generalized version of ilookup() for
924 * file systems where the inode number is not sufficient for unique
925 * identification of an inode.
926 *
927 * If the inode is in the cache, the inode lock is waited upon and the inode is
928 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
930 * Otherwise NULL is returned.
931 *
932 * Note, @test is called with the inode_lock held, so can't sleep.
933 */
934struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
935 int (*test)(struct inode *, void *), void *data)
936{
937 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
938
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700939 return ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942EXPORT_SYMBOL(ilookup5);
943
944/**
945 * ilookup - search for an inode in the inode cache
946 * @sb: super block of file system to search
947 * @ino: inode number to search for
948 *
949 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
950 * This is for file systems where the inode number is sufficient for unique
951 * identification of an inode.
952 *
953 * If the inode is in the cache, the inode is returned with an incremented
954 * reference count.
955 *
956 * Otherwise NULL is returned.
957 */
958struct inode *ilookup(struct super_block *sb, unsigned long ino)
959{
960 struct hlist_head *head = inode_hashtable + hash(sb, ino);
961
962 return ifind_fast(sb, head, ino);
963}
964
965EXPORT_SYMBOL(ilookup);
966
967/**
968 * iget5_locked - obtain an inode from a mounted file system
969 * @sb: super block of file system
970 * @hashval: hash value (usually inode number) to get
971 * @test: callback used for comparisons between inodes
972 * @set: callback used to initialize a new struct inode
973 * @data: opaque data pointer to pass to @test and @set
974 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 * iget5_locked() uses ifind() to search for the inode specified by @hashval
976 * and @data in the inode cache and if present it is returned with an increased
977 * reference count. This is a generalized version of iget_locked() for file
978 * systems where the inode number is not sufficient for unique identification
979 * of an inode.
980 *
981 * If the inode is not in cache, get_new_inode() is called to allocate a new
982 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
983 * file system gets to fill it in before unlocking it via unlock_new_inode().
984 *
985 * Note both @test and @set are called with the inode_lock held, so can't sleep.
986 */
987struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
988 int (*test)(struct inode *, void *),
989 int (*set)(struct inode *, void *), void *data)
990{
991 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
992 struct inode *inode;
993
Anton Altaparmakov88bd5122005-07-13 01:10:44 -0700994 inode = ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (inode)
996 return inode;
997 /*
998 * get_new_inode() will do the right thing, re-trying the search
999 * in case it had to block at any point.
1000 */
1001 return get_new_inode(sb, head, test, set, data);
1002}
1003
1004EXPORT_SYMBOL(iget5_locked);
1005
1006/**
1007 * iget_locked - obtain an inode from a mounted file system
1008 * @sb: super block of file system
1009 * @ino: inode number to get
1010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1012 * the inode cache and if present it is returned with an increased reference
1013 * count. This is for file systems where the inode number is sufficient for
1014 * unique identification of an inode.
1015 *
1016 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1017 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1018 * The file system gets to fill it in before unlocking it via
1019 * unlock_new_inode().
1020 */
1021struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1022{
1023 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1024 struct inode *inode;
1025
1026 inode = ifind_fast(sb, head, ino);
1027 if (inode)
1028 return inode;
1029 /*
1030 * get_new_inode_fast() will do the right thing, re-trying the search
1031 * in case it had to block at any point.
1032 */
1033 return get_new_inode_fast(sb, head, ino);
1034}
1035
1036EXPORT_SYMBOL(iget_locked);
1037
Al Viro261bca82008-12-30 01:48:21 -05001038int insert_inode_locked(struct inode *inode)
1039{
1040 struct super_block *sb = inode->i_sb;
1041 ino_t ino = inode->i_ino;
1042 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1043 struct inode *old;
1044
1045 inode->i_state |= I_LOCK|I_NEW;
1046 while (1) {
1047 spin_lock(&inode_lock);
1048 old = find_inode_fast(sb, head, ino);
1049 if (likely(!old)) {
1050 hlist_add_head(&inode->i_hash, head);
1051 spin_unlock(&inode_lock);
1052 return 0;
1053 }
1054 __iget(old);
1055 spin_unlock(&inode_lock);
1056 wait_on_inode(old);
1057 if (unlikely(!hlist_unhashed(&old->i_hash))) {
1058 iput(old);
1059 return -EBUSY;
1060 }
1061 iput(old);
1062 }
1063}
1064
1065EXPORT_SYMBOL(insert_inode_locked);
1066
1067int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1068 int (*test)(struct inode *, void *), void *data)
1069{
1070 struct super_block *sb = inode->i_sb;
1071 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1072 struct inode *old;
1073
1074 inode->i_state |= I_LOCK|I_NEW;
1075
1076 while (1) {
1077 spin_lock(&inode_lock);
1078 old = find_inode(sb, head, test, data);
1079 if (likely(!old)) {
1080 hlist_add_head(&inode->i_hash, head);
1081 spin_unlock(&inode_lock);
1082 return 0;
1083 }
1084 __iget(old);
1085 spin_unlock(&inode_lock);
1086 wait_on_inode(old);
1087 if (unlikely(!hlist_unhashed(&old->i_hash))) {
1088 iput(old);
1089 return -EBUSY;
1090 }
1091 iput(old);
1092 }
1093}
1094
1095EXPORT_SYMBOL(insert_inode_locked4);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/**
1098 * __insert_inode_hash - hash an inode
1099 * @inode: unhashed inode
1100 * @hashval: unsigned long value used to locate this object in the
1101 * inode_hashtable.
1102 *
1103 * Add an inode to the inode hash for this superblock.
1104 */
1105void __insert_inode_hash(struct inode *inode, unsigned long hashval)
1106{
1107 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1108 spin_lock(&inode_lock);
1109 hlist_add_head(&inode->i_hash, head);
1110 spin_unlock(&inode_lock);
1111}
1112
1113EXPORT_SYMBOL(__insert_inode_hash);
1114
1115/**
1116 * remove_inode_hash - remove an inode from the hash
1117 * @inode: inode to unhash
1118 *
1119 * Remove an inode from the superblock.
1120 */
1121void remove_inode_hash(struct inode *inode)
1122{
1123 spin_lock(&inode_lock);
1124 hlist_del_init(&inode->i_hash);
1125 spin_unlock(&inode_lock);
1126}
1127
1128EXPORT_SYMBOL(remove_inode_hash);
1129
1130/*
1131 * Tell the filesystem that this inode is no longer of any interest and should
1132 * be completely destroyed.
1133 *
1134 * We leave the inode in the inode hash table until *after* the filesystem's
1135 * ->delete_inode completes. This ensures that an iget (such as nfsd might
1136 * instigate) will always find up-to-date information either in the hash or on
1137 * disk.
1138 *
1139 * I_FREEING is set so that no-one will take a new reference to the inode while
1140 * it is being deleted.
1141 */
Arjan van de Venb32714b2009-01-09 07:04:15 -08001142void generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001144 const struct super_operations *op = inode->i_sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Arjan van de Venb32714b2009-01-09 07:04:15 -08001146 list_del_init(&inode->i_list);
1147 list_del_init(&inode->i_sb_list);
1148 inode->i_state |= I_FREEING;
1149 inodes_stat.nr_inodes--;
1150 spin_unlock(&inode_lock);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 security_inode_delete(inode);
1153
1154 if (op->delete_inode) {
1155 void (*delete)(struct inode *) = op->delete_inode;
1156 if (!is_bad_inode(inode))
1157 DQUOT_INIT(inode);
Mark Fashehe85b5652005-09-09 13:01:29 -07001158 /* Filesystems implementing their own
1159 * s_op->delete_inode are required to call
1160 * truncate_inode_pages and clear_inode()
1161 * internally */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 delete(inode);
Mark Fashehe85b5652005-09-09 13:01:29 -07001163 } else {
1164 truncate_inode_pages(&inode->i_data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 clear_inode(inode);
Mark Fashehe85b5652005-09-09 13:01:29 -07001166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 spin_lock(&inode_lock);
1168 hlist_del_init(&inode->i_hash);
1169 spin_unlock(&inode_lock);
1170 wake_up_inode(inode);
Eric Sesterhennb7542f82006-04-02 13:38:18 +02001171 BUG_ON(inode->i_state != I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 destroy_inode(inode);
1173}
1174
1175EXPORT_SYMBOL(generic_delete_inode);
1176
1177static void generic_forget_inode(struct inode *inode)
1178{
1179 struct super_block *sb = inode->i_sb;
1180
1181 if (!hlist_unhashed(&inode->i_hash)) {
Joern Engel1c0eeaf2007-10-16 23:30:44 -07001182 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 list_move(&inode->i_list, &inode_unused);
1184 inodes_stat.nr_unused++;
Christoph Hellwigacb0c852007-05-08 00:25:52 -07001185 if (sb->s_flags & MS_ACTIVE) {
Alexander Viro991114c2005-06-23 00:09:01 -07001186 spin_unlock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return;
Alexander Viro991114c2005-06-23 00:09:01 -07001188 }
1189 inode->i_state |= I_WILL_FREE;
1190 spin_unlock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 write_inode_now(inode, 1);
1192 spin_lock(&inode_lock);
Alexander Viro991114c2005-06-23 00:09:01 -07001193 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 inodes_stat.nr_unused--;
1195 hlist_del_init(&inode->i_hash);
1196 }
1197 list_del_init(&inode->i_list);
1198 list_del_init(&inode->i_sb_list);
Alexander Viro991114c2005-06-23 00:09:01 -07001199 inode->i_state |= I_FREEING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 inodes_stat.nr_inodes--;
1201 spin_unlock(&inode_lock);
1202 if (inode->i_data.nrpages)
1203 truncate_inode_pages(&inode->i_data, 0);
1204 clear_inode(inode);
Andrea Arcangeli7f04c262005-10-30 15:03:05 -08001205 wake_up_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 destroy_inode(inode);
1207}
1208
1209/*
1210 * Normal UNIX filesystem behaviour: delete the
1211 * inode when the usage count drops to zero, and
1212 * i_nlink is zero.
1213 */
Mark Fashehcb2c0232005-07-07 17:56:03 -07001214void generic_drop_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 if (!inode->i_nlink)
1217 generic_delete_inode(inode);
1218 else
1219 generic_forget_inode(inode);
1220}
1221
Mark Fashehcb2c0232005-07-07 17:56:03 -07001222EXPORT_SYMBOL_GPL(generic_drop_inode);
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/*
1225 * Called when we're dropping the last reference
1226 * to an inode.
1227 *
1228 * Call the FS "drop()" function, defaulting to
1229 * the legacy UNIX filesystem behaviour..
1230 *
1231 * NOTE! NOTE! NOTE! We're called with the inode lock
1232 * held, and the drop function is supposed to release
1233 * the lock!
1234 */
1235static inline void iput_final(struct inode *inode)
1236{
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001237 const struct super_operations *op = inode->i_sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 void (*drop)(struct inode *) = generic_drop_inode;
1239
1240 if (op && op->drop_inode)
1241 drop = op->drop_inode;
1242 drop(inode);
1243}
1244
1245/**
1246 * iput - put an inode
1247 * @inode: inode to put
1248 *
1249 * Puts an inode, dropping its usage count. If the inode use count hits
1250 * zero, the inode is then freed and may also be destroyed.
1251 *
1252 * Consequently, iput() can sleep.
1253 */
1254void iput(struct inode *inode)
1255{
1256 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 BUG_ON(inode->i_state == I_CLEAR);
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1260 iput_final(inode);
1261 }
1262}
1263
1264EXPORT_SYMBOL(iput);
1265
1266/**
1267 * bmap - find a block number in a file
1268 * @inode: inode of file
1269 * @block: block to find
1270 *
1271 * Returns the block number on the device holding the inode that
1272 * is the disk block number for the block of the file requested.
1273 * That is, asked for block 4 of inode 1 the function will return the
1274 * disk block relative to the disk start that holds that block of the
1275 * file.
1276 */
1277sector_t bmap(struct inode * inode, sector_t block)
1278{
1279 sector_t res = 0;
1280 if (inode->i_mapping->a_ops->bmap)
1281 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1282 return res;
1283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284EXPORT_SYMBOL(bmap);
1285
1286/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001287 * touch_atime - update the access time
1288 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001289 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 *
1291 * Update the accessed time on an inode and mark it for writeback.
1292 * This function automatically handles read only file systems and media,
1293 * as well as the "noatime" flag and inode specific "noatime" markers.
1294 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001295void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001297 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 struct timespec now;
1299
Dave Hansencdb70f32008-02-15 14:37:41 -08001300 if (mnt_want_write(mnt))
1301 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001302 if (inode->i_flags & S_NOATIME)
Dave Hansencdb70f32008-02-15 14:37:41 -08001303 goto out;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001304 if (IS_NOATIME(inode))
Dave Hansencdb70f32008-02-15 14:37:41 -08001305 goto out;
Andrew Mortonb2276132006-12-13 00:34:33 -08001306 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Dave Hansencdb70f32008-02-15 14:37:41 -08001307 goto out;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001308
Dave Hansencdb70f32008-02-15 14:37:41 -08001309 if (mnt->mnt_flags & MNT_NOATIME)
1310 goto out;
1311 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1312 goto out;
1313 if (mnt->mnt_flags & MNT_RELATIME) {
1314 /*
1315 * With relative atime, only update atime if the previous
1316 * atime is earlier than either the ctime or mtime.
1317 */
1318 if (timespec_compare(&inode->i_mtime, &inode->i_atime) < 0 &&
1319 timespec_compare(&inode->i_ctime, &inode->i_atime) < 0)
1320 goto out;
Andrew Mortonb2276132006-12-13 00:34:33 -08001321 }
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 now = current_fs_time(inode->i_sb);
Valerie Henson47ae32d2006-12-13 00:34:34 -08001324 if (timespec_equal(&inode->i_atime, &now))
Dave Hansencdb70f32008-02-15 14:37:41 -08001325 goto out;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001326
1327 inode->i_atime = now;
1328 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001329out:
1330 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001332EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001335 * file_update_time - update mtime and ctime time
1336 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001338 * Update the mtime and ctime members of an inode and mark the inode
1339 * for writeback. Note that this function is meant exclusively for
1340 * usage in the file write path of filesystems, and filesystems may
1341 * choose to explicitly ignore update via this function with the
1342 * S_NOCTIME inode flag, e.g. for network filesystem where these
1343 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
1345
Christoph Hellwig870f4812006-01-09 20:52:01 -08001346void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001348 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 struct timespec now;
1350 int sync_it = 0;
Dave Hansen20ddee22008-02-15 14:37:43 -08001351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (IS_NOCMTIME(inode))
1354 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001355
1356 err = mnt_want_write(file->f_path.mnt);
1357 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return;
1359
1360 now = current_fs_time(inode->i_sb);
Andreas Mohred97bd32006-10-02 02:17:17 -07001361 if (!timespec_equal(&inode->i_mtime, &now)) {
1362 inode->i_mtime = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 sync_it = 1;
Andreas Mohred97bd32006-10-02 02:17:17 -07001364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Andreas Mohred97bd32006-10-02 02:17:17 -07001366 if (!timespec_equal(&inode->i_ctime, &now)) {
1367 inode->i_ctime = now;
Christoph Hellwig870f4812006-01-09 20:52:01 -08001368 sync_it = 1;
Andreas Mohred97bd32006-10-02 02:17:17 -07001369 }
Christoph Hellwig870f4812006-01-09 20:52:01 -08001370
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001371 if (IS_I_VERSION(inode)) {
1372 inode_inc_iversion(inode);
1373 sync_it = 1;
1374 }
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (sync_it)
1377 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001378 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Christoph Hellwig870f4812006-01-09 20:52:01 -08001381EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383int inode_needs_sync(struct inode *inode)
1384{
1385 if (IS_SYNC(inode))
1386 return 1;
1387 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1388 return 1;
1389 return 0;
1390}
1391
1392EXPORT_SYMBOL(inode_needs_sync);
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394int inode_wait(void *word)
1395{
1396 schedule();
1397 return 0;
1398}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001399EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001402 * If we try to find an inode in the inode hash while it is being
1403 * deleted, we have to wait until the filesystem completes its
1404 * deletion before reporting that it isn't found. This function waits
1405 * until the deletion _might_ have completed. Callers are responsible
1406 * to recheck inode state.
1407 *
1408 * It doesn't matter if I_LOCK is not set initially, a call to
1409 * wake_up_inode() after removing from the hash list will DTRT.
1410 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 * This is called with inode_lock held.
1412 */
1413static void __wait_on_freeing_inode(struct inode *inode)
1414{
1415 wait_queue_head_t *wq;
1416 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 wq = bit_waitqueue(&inode->i_state, __I_LOCK);
1418 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1419 spin_unlock(&inode_lock);
1420 schedule();
1421 finish_wait(wq, &wait.wait);
1422 spin_lock(&inode_lock);
1423}
1424
Mark Fasheh62752ee2006-10-17 10:31:38 +02001425/*
1426 * We rarely want to lock two inodes that do not have a parent/child
1427 * relationship (such as directory, child inode) simultaneously. The
1428 * vast majority of file systems should be able to get along fine
1429 * without this. Do not use these functions except as a last resort.
1430 */
1431void inode_double_lock(struct inode *inode1, struct inode *inode2)
1432{
1433 if (inode1 == NULL || inode2 == NULL || inode1 == inode2) {
1434 if (inode1)
1435 mutex_lock(&inode1->i_mutex);
1436 else if (inode2)
1437 mutex_lock(&inode2->i_mutex);
1438 return;
1439 }
1440
1441 if (inode1 < inode2) {
1442 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
1443 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
1444 } else {
1445 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_PARENT);
1446 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_CHILD);
1447 }
1448}
1449EXPORT_SYMBOL(inode_double_lock);
1450
1451void inode_double_unlock(struct inode *inode1, struct inode *inode2)
1452{
1453 if (inode1)
1454 mutex_unlock(&inode1->i_mutex);
1455
1456 if (inode2 && inode2 != inode1)
1457 mutex_unlock(&inode2->i_mutex);
1458}
1459EXPORT_SYMBOL(inode_double_unlock);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static __initdata unsigned long ihash_entries;
1462static int __init set_ihash_entries(char *str)
1463{
1464 if (!str)
1465 return 0;
1466 ihash_entries = simple_strtoul(str, &str, 0);
1467 return 1;
1468}
1469__setup("ihash_entries=", set_ihash_entries);
1470
1471/*
1472 * Initialize the waitqueues and inode hash table.
1473 */
1474void __init inode_init_early(void)
1475{
1476 int loop;
1477
1478 /* If hashes are distributed across NUMA nodes, defer
1479 * hash allocation until vmalloc space is available.
1480 */
1481 if (hashdist)
1482 return;
1483
1484 inode_hashtable =
1485 alloc_large_system_hash("Inode-cache",
1486 sizeof(struct hlist_head),
1487 ihash_entries,
1488 14,
1489 HASH_EARLY,
1490 &i_hash_shift,
1491 &i_hash_mask,
1492 0);
1493
1494 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1495 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1496}
1497
Denis Cheng74bf17c2007-10-16 23:26:30 -07001498void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 int loop;
1501
1502 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001503 inode_cachep = kmem_cache_create("inode_cache",
1504 sizeof(struct inode),
1505 0,
1506 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1507 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001508 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001509 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 /* Hash may have been set up in inode_init_early */
1512 if (!hashdist)
1513 return;
1514
1515 inode_hashtable =
1516 alloc_large_system_hash("Inode-cache",
1517 sizeof(struct hlist_head),
1518 ihash_entries,
1519 14,
1520 0,
1521 &i_hash_shift,
1522 &i_hash_mask,
1523 0);
1524
1525 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1526 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1527}
1528
1529void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1530{
1531 inode->i_mode = mode;
1532 if (S_ISCHR(mode)) {
1533 inode->i_fop = &def_chr_fops;
1534 inode->i_rdev = rdev;
1535 } else if (S_ISBLK(mode)) {
1536 inode->i_fop = &def_blk_fops;
1537 inode->i_rdev = rdev;
1538 } else if (S_ISFIFO(mode))
1539 inode->i_fop = &def_fifo_fops;
1540 else if (S_ISSOCK(mode))
1541 inode->i_fop = &bad_sock_fops;
1542 else
1543 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1544 mode);
1545}
1546EXPORT_SYMBOL(init_special_inode);