blob: 9910c039f026254a9caea134a5a7ac8d40c9c76f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/slab.h>
12#include <linux/writeback.h>
13#include <linux/module.h>
14#include <linux/backing-dev.h>
15#include <linux/wait.h>
Nick Piggin88e0fbc2009-09-22 16:43:50 -070016#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Eric Paris3be25f42009-05-21 17:01:26 -040023#include <linux/fsnotify.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>
Al Virof19d4a82009-06-08 19:50:45 -040026#include <linux/posix_acl.h>
Eric Parisa178d202010-10-25 14:41:59 -040027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * This is needed for the following functions:
31 * - inode_has_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * - invalidate_bdev
33 *
34 * FIXME: remove all knowledge of the buffer layer from this file
35 */
36#include <linux/buffer_head.h>
37
38/*
39 * New inode.c implementation.
40 *
41 * This implementation has the basic premise of trying
42 * to be extremely low-overhead and SMP-safe, yet be
43 * simple enough to be "obviously correct".
44 *
45 * Famous last words.
46 */
47
48/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
49
50/* #define INODE_PARANOIA 1 */
51/* #define INODE_DEBUG 1 */
52
53/*
54 * Inode lookup is no longer as critical as it used to be:
55 * most of the lookups are going to be through the dcache.
56 */
57#define I_HASHBITS i_hash_shift
58#define I_HASHMASK i_hash_mask
59
Eric Dumazetfa3536c2006-03-26 01:37:24 -080060static unsigned int i_hash_mask __read_mostly;
61static unsigned int i_hash_shift __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * Each inode can be on two separate lists. One is
65 * the hash list of the inode, used for lookups. The
66 * other linked list is the "type" list:
67 * "in_use" - valid inode, i_count > 0, i_nlink > 0
68 * "dirty" - as "in_use" but also dirty
69 * "unused" - valid inode, i_count = 0
70 *
71 * A "dirty" list is maintained for each super block,
72 * allowing for low-overhead inode sync() operations.
73 */
74
Nick Piggin7ccf19a2010-10-21 11:49:30 +110075static LIST_HEAD(inode_lru);
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/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +010087 * iprune_sem provides exclusion between the icache shrinking and the
88 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -070089 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +010090 * We don't actually need it to protect anything in the umount path,
91 * but only need to cycle through it to make sure any inode that
92 * prune_icache took off the LRU list has been fully torn down by the
93 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -070095static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
98 * Statistics gathering..
99 */
100struct inodes_stat_t inodes_stat;
101
Nick Piggin3e880fb2011-01-07 17:49:19 +1100102static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400103
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530104static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Nick Piggin3e880fb2011-01-07 17:49:19 +1100106static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400107{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100108 int i;
109 int sum = 0;
110 for_each_possible_cpu(i)
111 sum += per_cpu(nr_inodes, i);
112 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400113}
114
115static inline int get_nr_inodes_unused(void)
116{
Nick Piggin86c87492011-01-07 17:49:18 +1100117 return inodes_stat.nr_unused;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400118}
119
120int get_nr_dirty_inodes(void)
121{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100122 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400123 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
124 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400125}
126
127/*
128 * Handle nr_inode sysctl
129 */
130#ifdef CONFIG_SYSCTL
131int proc_nr_inodes(ctl_table *table, int write,
132 void __user *buffer, size_t *lenp, loff_t *ppos)
133{
134 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400135 return proc_dointvec(table, write, buffer, lenp, ppos);
136}
137#endif
138
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700139static void wake_up_inode(struct inode *inode)
140{
141 /*
142 * Prevent speculative execution through spin_unlock(&inode_lock);
143 */
144 smp_mb();
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100145 wake_up_bit(&inode->i_state, __I_NEW);
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700146}
147
David Chinner2cb15992008-10-30 17:32:23 +1100148/**
149 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800150 * @sb: superblock inode belongs to
151 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100152 *
153 * These are initializations that need to be done on every inode
154 * allocation as the fields are not initialised by slab allocation.
155 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300156int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700158 static const struct address_space_operations empty_aops;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700159 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800160 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530161 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100162
163 inode->i_sb = sb;
164 inode->i_blkbits = sb->s_blocksize_bits;
165 inode->i_flags = 0;
166 atomic_set(&inode->i_count, 1);
167 inode->i_op = &empty_iops;
168 inode->i_fop = &empty_fops;
169 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500170 inode->i_uid = 0;
171 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100172 atomic_set(&inode->i_writecount, 0);
173 inode->i_size = 0;
174 inode->i_blocks = 0;
175 inode->i_bytes = 0;
176 inode->i_generation = 0;
177#ifdef CONFIG_QUOTA
178 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
179#endif
180 inode->i_pipe = NULL;
181 inode->i_bdev = NULL;
182 inode->i_cdev = NULL;
183 inode->i_rdev = 0;
184 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500185
186 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300187 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100188 spin_lock_init(&inode->i_lock);
189 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
190
191 mutex_init(&inode->i_mutex);
192 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
193
194 init_rwsem(&inode->i_alloc_sem);
195 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
196
197 mapping->a_ops = &empty_aops;
198 mapping->host = inode;
199 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800200 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100201 mapping->assoc_mapping = NULL;
202 mapping->backing_dev_info = &default_backing_dev_info;
203 mapping->writeback_index = 0;
204
205 /*
206 * If the block_device provides a backing_dev_info for client
207 * inodes then use that. Otherwise the inode share the bdev's
208 * backing_dev_info.
209 */
210 if (sb->s_bdev) {
211 struct backing_dev_info *bdi;
212
Jens Axboe2c96ce92009-09-15 09:43:56 +0200213 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100214 mapping->backing_dev_info = bdi;
215 }
216 inode->i_private = NULL;
217 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400218#ifdef CONFIG_FS_POSIX_ACL
219 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
220#endif
David Chinner2cb15992008-10-30 17:32:23 +1100221
Eric Paris3be25f42009-05-21 17:01:26 -0400222#ifdef CONFIG_FSNOTIFY
223 inode->i_fsnotify_mask = 0;
224#endif
225
Nick Piggin3e880fb2011-01-07 17:49:19 +1100226 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400227
Christoph Hellwig54e34622009-08-07 14:38:25 -0300228 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300229out:
230 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100231}
232EXPORT_SYMBOL(inode_init_always);
233
234static struct inode *alloc_inode(struct super_block *sb)
235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct inode *inode;
237
238 if (sb->s_op->alloc_inode)
239 inode = sb->s_op->alloc_inode(sb);
240 else
David Chinner2cb15992008-10-30 17:32:23 +1100241 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Christoph Hellwig54e34622009-08-07 14:38:25 -0300243 if (!inode)
244 return NULL;
245
246 if (unlikely(inode_init_always(sb, inode))) {
247 if (inode->i_sb->s_op->destroy_inode)
248 inode->i_sb->s_op->destroy_inode(inode);
249 else
250 kmem_cache_free(inode_cachep, inode);
251 return NULL;
252 }
253
254 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Nick Pigginff0c7d12011-01-07 17:49:50 +1100257void free_inode_nonrcu(struct inode *inode)
258{
259 kmem_cache_free(inode_cachep, inode);
260}
261EXPORT_SYMBOL(free_inode_nonrcu);
262
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300263void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200265 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400267 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400268#ifdef CONFIG_FS_POSIX_ACL
269 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
270 posix_acl_release(inode->i_acl);
271 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
272 posix_acl_release(inode->i_default_acl);
273#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100274 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300275}
276EXPORT_SYMBOL(__destroy_inode);
277
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100278static void i_callback(struct rcu_head *head)
279{
280 struct inode *inode = container_of(head, struct inode, i_rcu);
281 INIT_LIST_HEAD(&inode->i_dentry);
282 kmem_cache_free(inode_cachep, inode);
283}
284
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200285static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300286{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100287 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300288 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (inode->i_sb->s_op->destroy_inode)
290 inode->i_sb->s_op->destroy_inode(inode);
291 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100292 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100295void address_space_init_once(struct address_space *mapping)
296{
297 memset(mapping, 0, sizeof(*mapping));
298 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
299 spin_lock_init(&mapping->tree_lock);
300 spin_lock_init(&mapping->i_mmap_lock);
301 INIT_LIST_HEAD(&mapping->private_list);
302 spin_lock_init(&mapping->private_lock);
303 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
304 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
305 mutex_init(&mapping->unmap_mutex);
306}
307EXPORT_SYMBOL(address_space_init_once);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * These are initializations that only need to be done
311 * once, because the fields are idempotent across use
312 * of the inode, so let the slab aware of that.
313 */
314void inode_init_once(struct inode *inode)
315{
316 memset(inode, 0, sizeof(*inode));
317 INIT_HLIST_NODE(&inode->i_hash);
318 INIT_LIST_HEAD(&inode->i_dentry);
319 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100320 INIT_LIST_HEAD(&inode->i_wb_list);
321 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100322 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400324#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500325 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328EXPORT_SYMBOL(inode_init_once);
329
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700330static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530332 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Christoph Lametera35afb82007-05-16 22:10:57 -0700334 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/*
338 * inode_lock must be held
339 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530340void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Nick Piggin9e38d862010-10-23 06:55:17 -0400342 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Al Viro7de9c6ee2010-10-23 11:11:40 -0400345/*
346 * get additional reference to inode; caller must already hold one.
347 */
348void ihold(struct inode *inode)
349{
350 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
351}
352EXPORT_SYMBOL(ihold);
353
Nick Piggin9e38d862010-10-23 06:55:17 -0400354static void inode_lru_list_add(struct inode *inode)
355{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100356 if (list_empty(&inode->i_lru)) {
357 list_add(&inode->i_lru, &inode_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100358 inodes_stat.nr_unused++;
Nick Piggin9e38d862010-10-23 06:55:17 -0400359 }
360}
361
362static void inode_lru_list_del(struct inode *inode)
363{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100364 if (!list_empty(&inode->i_lru)) {
365 list_del_init(&inode->i_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100366 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Christoph Hellwig646ec462010-10-23 07:15:32 -0400370static inline void __inode_sb_list_add(struct inode *inode)
371{
372 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
373}
374
375/**
376 * inode_sb_list_add - add inode to the superblock list of inodes
377 * @inode: inode to add
378 */
379void inode_sb_list_add(struct inode *inode)
380{
381 spin_lock(&inode_lock);
382 __inode_sb_list_add(inode);
383 spin_unlock(&inode_lock);
384}
385EXPORT_SYMBOL_GPL(inode_sb_list_add);
386
387static inline void __inode_sb_list_del(struct inode *inode)
388{
389 list_del_init(&inode->i_sb_list);
390}
391
Dave Chinner4c51acb2010-10-23 06:58:09 -0400392static unsigned long hash(struct super_block *sb, unsigned long hashval)
393{
394 unsigned long tmp;
395
396 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
397 L1_CACHE_BYTES;
398 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
399 return tmp & I_HASHMASK;
400}
401
402/**
403 * __insert_inode_hash - hash an inode
404 * @inode: unhashed inode
405 * @hashval: unsigned long value used to locate this object in the
406 * inode_hashtable.
407 *
408 * Add an inode to the inode hash for this superblock.
409 */
410void __insert_inode_hash(struct inode *inode, unsigned long hashval)
411{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400412 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
413
Dave Chinner4c51acb2010-10-23 06:58:09 -0400414 spin_lock(&inode_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400415 hlist_add_head(&inode->i_hash, b);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400416 spin_unlock(&inode_lock);
417}
418EXPORT_SYMBOL(__insert_inode_hash);
419
420/**
421 * __remove_inode_hash - remove an inode from the hash
422 * @inode: inode to unhash
423 *
424 * Remove an inode from the superblock.
425 */
426static void __remove_inode_hash(struct inode *inode)
427{
428 hlist_del_init(&inode->i_hash);
429}
430
431/**
432 * remove_inode_hash - remove an inode from the hash
433 * @inode: inode to unhash
434 *
435 * Remove an inode from the superblock.
436 */
437void remove_inode_hash(struct inode *inode)
438{
439 spin_lock(&inode_lock);
440 hlist_del_init(&inode->i_hash);
441 spin_unlock(&inode_lock);
442}
443EXPORT_SYMBOL(remove_inode_hash);
444
Al Virob0683aa2010-06-04 20:55:25 -0400445void end_writeback(struct inode *inode)
446{
447 might_sleep();
448 BUG_ON(inode->i_data.nrpages);
449 BUG_ON(!list_empty(&inode->i_data.private_list));
450 BUG_ON(!(inode->i_state & I_FREEING));
451 BUG_ON(inode->i_state & I_CLEAR);
452 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100453 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400454 inode->i_state = I_FREEING | I_CLEAR;
455}
456EXPORT_SYMBOL(end_writeback);
457
Al Viro644da592010-06-07 13:21:05 -0400458static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400459{
460 const struct super_operations *op = inode->i_sb->s_op;
461
Al Virobe7ce412010-06-04 19:40:39 -0400462 if (op->evict_inode) {
463 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400464 } else {
465 if (inode->i_data.nrpages)
466 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400467 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400468 }
Al Viro661074e2010-06-04 20:19:55 -0400469 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
470 bd_forget(inode);
471 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
472 cd_forget(inode);
Al Virob4272d42010-06-04 19:33:20 -0400473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/*
476 * dispose_list - dispose of the contents of a local list
477 * @head: the head of the list to free
478 *
479 * Dispose-list gets a local list with local inodes in it, so it doesn't
480 * need to worry about list corruption and SMP locks.
481 */
482static void dispose_list(struct list_head *head)
483{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 while (!list_empty(head)) {
485 struct inode *inode;
486
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100487 inode = list_first_entry(head, struct inode, i_lru);
488 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Al Viro644da592010-06-07 13:21:05 -0400490 evict(inode);
Artem B. Bityuckiy4120db42005-07-12 13:58:12 -0700491
492 spin_lock(&inode_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400493 __remove_inode_hash(inode);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400494 __inode_sb_list_del(inode);
Artem B. Bityuckiy4120db42005-07-12 13:58:12 -0700495 spin_unlock(&inode_lock);
496
497 wake_up_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/**
Al Viro63997e92010-10-25 20:49:35 -0400503 * evict_inodes - evict all evictable inodes for a superblock
504 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *
Al Viro63997e92010-10-25 20:49:35 -0400506 * Make sure that no inodes with zero refcount are retained. This is
507 * called by superblock shutdown after having MS_ACTIVE flag removed,
508 * so any inode reaching zero refcount during or after that call will
509 * be immediately evicted.
510 */
511void evict_inodes(struct super_block *sb)
512{
513 struct inode *inode, *next;
514 LIST_HEAD(dispose);
515
Al Viro63997e92010-10-25 20:49:35 -0400516 spin_lock(&inode_lock);
517 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
518 if (atomic_read(&inode->i_count))
519 continue;
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100520 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE))
Al Viro63997e92010-10-25 20:49:35 -0400521 continue;
Al Viro63997e92010-10-25 20:49:35 -0400522
523 inode->i_state |= I_FREEING;
524
525 /*
526 * Move the inode off the IO lists and LRU once I_FREEING is
527 * set so that it won't get moved back on there if it is dirty.
528 */
529 list_move(&inode->i_lru, &dispose);
530 list_del_init(&inode->i_wb_list);
531 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
Nick Piggin86c87492011-01-07 17:49:18 +1100532 inodes_stat.nr_unused--;
Al Viro63997e92010-10-25 20:49:35 -0400533 }
534 spin_unlock(&inode_lock);
535
536 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100537
538 /*
539 * Cycle through iprune_sem to make sure any inode that prune_icache
540 * moved off the list before we took the lock has been fully torn
541 * down.
542 */
543 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400544 up_write(&iprune_sem);
545}
546
547/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200548 * invalidate_inodes - attempt to free all inodes on a superblock
549 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100550 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200551 *
552 * Attempts to free all inodes for a given superblock. If there were any
553 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100554 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
555 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
NeilBrown93b270f2011-02-24 17:25:47 +1100557int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400559 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200560 struct inode *inode, *next;
561 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 spin_lock(&inode_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200564 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Al Viro63997e92010-10-25 20:49:35 -0400565 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 continue;
NeilBrown93b270f2011-02-24 17:25:47 +1100567 if (inode->i_state & I_DIRTY && !kill_dirty) {
568 busy = 1;
569 continue;
570 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200571 if (atomic_read(&inode->i_count)) {
572 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 continue;
574 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200575
Christoph Hellwig99a38912010-10-23 19:07:20 +0200576 inode->i_state |= I_FREEING;
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100577
578 /*
579 * Move the inode off the IO lists and LRU once I_FREEING is
580 * set so that it won't get moved back on there if it is dirty.
581 */
Christoph Hellwiga0318782010-10-24 19:40:33 +0200582 list_move(&inode->i_lru, &dispose);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100583 list_del_init(&inode->i_wb_list);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200584 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
Nick Piggin86c87492011-01-07 17:49:18 +1100585 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_unlock(&inode_lock);
588
Christoph Hellwiga0318782010-10-24 19:40:33 +0200589 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return busy;
592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594static int can_unuse(struct inode *inode)
595{
Nick Piggin9e38d862010-10-23 06:55:17 -0400596 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return 0;
598 if (inode_has_buffers(inode))
599 return 0;
600 if (atomic_read(&inode->i_count))
601 return 0;
602 if (inode->i_data.nrpages)
603 return 0;
604 return 1;
605}
606
607/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400608 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
609 * temporary list and then are freed outside inode_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
611 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400612 * pagecache removed. If the inode has metadata buffers attached to
613 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400615 * If the inode has the I_REFERENCED flag set, then it means that it has been
616 * used recently - the flag is set in iput_final(). When we encounter such an
617 * inode, clear the flag and move it to the back of the LRU so it gets another
618 * pass through the LRU before it gets reclaimed. This is necessary because of
619 * the fact we are doing lazy LRU updates to minimise lock contention so the
620 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
621 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623static void prune_icache(int nr_to_scan)
624{
625 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int nr_scanned;
627 unsigned long reap = 0;
628
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700629 down_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 spin_lock(&inode_lock);
631 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
632 struct inode *inode;
633
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100634 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 break;
636
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100637 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Nick Piggin9e38d862010-10-23 06:55:17 -0400639 /*
640 * Referenced or dirty inodes are still in use. Give them
641 * another pass through the LRU as we canot reclaim them now.
642 */
643 if (atomic_read(&inode->i_count) ||
644 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100645 list_del_init(&inode->i_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100646 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400647 continue;
648 }
649
650 /* recently referenced inodes get one more pass */
651 if (inode->i_state & I_REFERENCED) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100652 list_move(&inode->i_lru, &inode_lru);
Nick Piggin9e38d862010-10-23 06:55:17 -0400653 inode->i_state &= ~I_REFERENCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 }
656 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
657 __iget(inode);
658 spin_unlock(&inode_lock);
659 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800660 reap += invalidate_mapping_pages(&inode->i_data,
661 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 iput(inode);
663 spin_lock(&inode_lock);
664
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100665 if (inode != list_entry(inode_lru.next,
666 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 continue; /* wrong inode or list_empty */
668 if (!can_unuse(inode))
669 continue;
670 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700671 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 inode->i_state |= I_FREEING;
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100673
674 /*
675 * Move the inode off the IO lists and LRU once I_FREEING is
676 * set so that it won't get moved back on there if it is dirty.
677 */
678 list_move(&inode->i_lru, &freeable);
679 list_del_init(&inode->i_wb_list);
Nick Piggin86c87492011-01-07 17:49:18 +1100680 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700682 if (current_is_kswapd())
683 __count_vm_events(KSWAPD_INODESTEAL, reap);
684 else
685 __count_vm_events(PGINODESTEAL, reap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 spin_unlock(&inode_lock);
687
688 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700689 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692/*
693 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
694 * "unused" means that no dentries are referring to the inodes: the files are
695 * not open and the dcache references to those inodes have already been
696 * reclaimed.
697 *
698 * This function is passed the number of inodes to scan, and it returns the
699 * total number of remaining possibly-reclaimable inodes.
700 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000701static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 if (nr) {
704 /*
705 * Nasty deadlock avoidance. We may hold various FS locks,
706 * and we don't want to recurse into the FS that called us
707 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (!(gfp_mask & __GFP_FS))
710 return -1;
711 prune_icache(nr);
712 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400713 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Rusty Russell8e1f9362007-07-17 04:03:17 -0700716static struct shrinker icache_shrinker = {
717 .shrink = shrink_icache_memory,
718 .seeks = DEFAULT_SEEKS,
719};
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721static void __wait_on_freeing_inode(struct inode *inode);
722/*
723 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530725static struct inode *find_inode(struct super_block *sb,
726 struct hlist_head *head,
727 int (*test)(struct inode *, void *),
728 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530731 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700734 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (inode->i_sb != sb)
736 continue;
737 if (!test(inode, data))
738 continue;
Al Viroa4ffdde2010-06-02 17:38:30 -0400739 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 __wait_on_freeing_inode(inode);
741 goto repeat;
742 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400743 __iget(inode);
744 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400746 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * find_inode_fast is the fast path version of find_inode, see the comment at
751 * iget_locked for details.
752 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530753static struct inode *find_inode_fast(struct super_block *sb,
754 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530757 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700760 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (inode->i_ino != ino)
762 continue;
763 if (inode->i_sb != sb)
764 continue;
Al Viroa4ffdde2010-06-02 17:38:30 -0400765 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 __wait_on_freeing_inode(inode);
767 goto repeat;
768 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400769 __iget(inode);
770 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400772 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Eric Dumazetf991bd22010-10-23 11:18:01 -0400775/*
776 * Each cpu owns a range of LAST_INO_BATCH numbers.
777 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
778 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100779 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400780 * This does not significantly increase overflow rate because every CPU can
781 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
782 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
783 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
784 * overflow rate by 2x, which does not seem too significant.
785 *
786 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
787 * error if st_ino won't fit in target struct field. Use 32bit counter
788 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100789 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400790#define LAST_INO_BATCH 1024
791static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100792
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400793unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400794{
795 unsigned int *p = &get_cpu_var(last_ino);
796 unsigned int res = *p;
797
798#ifdef CONFIG_SMP
799 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
800 static atomic_t shared_last_ino;
801 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
802
803 res = next - LAST_INO_BATCH;
804 }
805#endif
806
807 *p = ++res;
808 put_cpu_var(last_ino);
809 return res;
David Chinner8290c352008-10-30 17:35:24 +1100810}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400811EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/**
814 * new_inode - obtain an inode
815 * @sb: superblock
816 *
Mel Gorman769848c2007-07-17 04:03:05 -0700817 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800818 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700819 * If HIGHMEM pages are unsuitable or it is known that pages allocated
820 * for the page cache are not reclaimable or migratable,
821 * mapping_set_gfp_mask() must be called with suitable flags on the
822 * newly created inode's mapping
823 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
825struct inode *new_inode(struct super_block *sb)
826{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530827 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 spin_lock_prefetch(&inode_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 inode = alloc_inode(sb);
832 if (inode) {
833 spin_lock(&inode_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400834 __inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 inode->i_state = 0;
836 spin_unlock(&inode_lock);
837 }
838 return inode;
839}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840EXPORT_SYMBOL(new_inode);
841
842void unlock_new_inode(struct inode *inode)
843{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200844#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900845 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200846 struct file_system_type *type = inode->i_sb->s_type;
847
Jan Kara9a7aa122009-06-04 15:26:49 +0200848 /* Set new key only if filesystem hasn't already changed it */
849 if (!lockdep_match_class(&inode->i_mutex,
850 &type->i_mutex_key)) {
851 /*
852 * ensure nobody is actually holding i_mutex
853 */
854 mutex_destroy(&inode->i_mutex);
855 mutex_init(&inode->i_mutex);
856 lockdep_set_class(&inode->i_mutex,
857 &type->i_mutex_dir_key);
858 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200859 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200860#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100862 * This is special! We do not need the spinlock when clearing I_NEW,
Jan Kara580be082009-09-21 17:01:06 -0700863 * because we're guaranteed that nobody else tries to do anything about
864 * the state of the inode when it is locked, as we just created it (so
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100865 * there can be no old holders that haven't tested I_NEW).
Jan Kara580be082009-09-21 17:01:06 -0700866 * However we must emit the memory barrier so that other CPUs reliably
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100867 * see the clearing of I_NEW after the other inode initialisation has
Jan Kara580be082009-09-21 17:01:06 -0700868 * completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Jan Kara580be082009-09-21 17:01:06 -0700870 smp_mb();
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100871 WARN_ON(!(inode->i_state & I_NEW));
872 inode->i_state &= ~I_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 wake_up_inode(inode);
874}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875EXPORT_SYMBOL(unlock_new_inode);
876
877/*
878 * This is called without the inode lock held.. Be careful.
879 *
880 * We no longer cache the sb_flags in i_flags - see fs.h
881 * -- rmk@arm.uk.linux.org
882 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530883static struct inode *get_new_inode(struct super_block *sb,
884 struct hlist_head *head,
885 int (*test)(struct inode *, void *),
886 int (*set)(struct inode *, void *),
887 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530889 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 inode = alloc_inode(sb);
892 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530893 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 spin_lock(&inode_lock);
896 /* We released the lock, so.. */
897 old = find_inode(sb, head, test, data);
898 if (!old) {
899 if (set(inode, data))
900 goto set_failed;
901
Christoph Hellwig646ec462010-10-23 07:15:32 -0400902 hlist_add_head(&inode->i_hash, head);
903 __inode_sb_list_add(inode);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100904 inode->i_state = I_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_unlock(&inode_lock);
906
907 /* Return the locked inode with I_NEW set, the
908 * caller is responsible for filling in the contents
909 */
910 return inode;
911 }
912
913 /*
914 * Uhhuh, somebody else created the same inode under
915 * us. Use the old inode instead of the one we just
916 * allocated.
917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 spin_unlock(&inode_lock);
919 destroy_inode(inode);
920 inode = old;
921 wait_on_inode(inode);
922 }
923 return inode;
924
925set_failed:
926 spin_unlock(&inode_lock);
927 destroy_inode(inode);
928 return NULL;
929}
930
931/*
932 * get_new_inode_fast is the fast path version of get_new_inode, see the
933 * comment at iget_locked for details.
934 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530935static struct inode *get_new_inode_fast(struct super_block *sb,
936 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530938 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 inode = alloc_inode(sb);
941 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530942 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 spin_lock(&inode_lock);
945 /* We released the lock, so.. */
946 old = find_inode_fast(sb, head, ino);
947 if (!old) {
948 inode->i_ino = ino;
Christoph Hellwig646ec462010-10-23 07:15:32 -0400949 hlist_add_head(&inode->i_hash, head);
950 __inode_sb_list_add(inode);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100951 inode->i_state = I_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 spin_unlock(&inode_lock);
953
954 /* Return the locked inode with I_NEW set, the
955 * caller is responsible for filling in the contents
956 */
957 return inode;
958 }
959
960 /*
961 * Uhhuh, somebody else created the same inode under
962 * us. Use the old inode instead of the one we just
963 * allocated.
964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 spin_unlock(&inode_lock);
966 destroy_inode(inode);
967 inode = old;
968 wait_on_inode(inode);
969 }
970 return inode;
971}
972
Christoph Hellwigad5e1952010-10-23 07:00:16 -0400973/*
974 * search the inode cache for a matching inode number.
975 * If we find one, then the inode number we are trying to
976 * allocate is not unique and so we should not use it.
977 *
978 * Returns 1 if the inode number is unique, 0 if it is not.
979 */
980static int test_inode_iunique(struct super_block *sb, unsigned long ino)
981{
982 struct hlist_head *b = inode_hashtable + hash(sb, ino);
983 struct hlist_node *node;
984 struct inode *inode;
985
986 hlist_for_each_entry(inode, node, b, i_hash) {
987 if (inode->i_ino == ino && inode->i_sb == sb)
988 return 0;
989 }
990
991 return 1;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/**
995 * iunique - get a unique inode number
996 * @sb: superblock
997 * @max_reserved: highest reserved inode number
998 *
999 * Obtain an inode number that is unique on the system for a given
1000 * superblock. This is used by file systems that have no natural
1001 * permanent inode numbering system. An inode number is returned that
1002 * is higher than the reserved limit but unique.
1003 *
1004 * BUGS:
1005 * With a large number of inodes live on the file system this function
1006 * currently becomes quite slow.
1007 */
1008ino_t iunique(struct super_block *sb, ino_t max_reserved)
1009{
Jeff Layton866b04f2007-05-08 00:32:29 -07001010 /*
1011 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1012 * error if st_ino won't fit in target struct field. Use 32bit counter
1013 * here to attempt to avoid that.
1014 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001015 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001016 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001019 spin_lock(&inode_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001020 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001021 do {
1022 if (counter <= max_reserved)
1023 counter = max_reserved + 1;
1024 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001025 } while (!test_inode_iunique(sb, res));
1026 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001027 spin_unlock(&inode_lock);
1028
1029 return res;
1030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031EXPORT_SYMBOL(iunique);
1032
1033struct inode *igrab(struct inode *inode)
1034{
1035 spin_lock(&inode_lock);
Al Viroa4ffdde2010-06-02 17:38:30 -04001036 if (!(inode->i_state & (I_FREEING|I_WILL_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 __iget(inode);
1038 else
1039 /*
1040 * Handle the case where s_op->clear_inode is not been
1041 * called yet, and somebody is calling igrab
1042 * while the inode is getting freed.
1043 */
1044 inode = NULL;
1045 spin_unlock(&inode_lock);
1046 return inode;
1047}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048EXPORT_SYMBOL(igrab);
1049
1050/**
1051 * ifind - internal function, you want ilookup5() or iget5().
1052 * @sb: super block of file system to search
1053 * @head: the head of the list to search
1054 * @test: callback used for comparisons between inodes
1055 * @data: opaque data pointer to pass to @test
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001056 * @wait: if true wait for the inode to be unlocked, if false do not
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 *
1058 * ifind() searches for the inode specified by @data in the inode
1059 * cache. This is a generalized version of ifind_fast() for file systems where
1060 * the inode number is not sufficient for unique identification of an inode.
1061 *
1062 * If the inode is in the cache, the inode is returned with an incremented
1063 * reference count.
1064 *
1065 * Otherwise NULL is returned.
1066 *
1067 * Note, @test is called with the inode_lock held, so can't sleep.
1068 */
Matt Mackall5d2bea42006-01-08 01:05:21 -08001069static struct inode *ifind(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct hlist_head *head, int (*test)(struct inode *, void *),
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001071 void *data, const int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 struct inode *inode;
1074
1075 spin_lock(&inode_lock);
1076 inode = find_inode(sb, head, test, data);
1077 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 spin_unlock(&inode_lock);
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001079 if (likely(wait))
1080 wait_on_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return inode;
1082 }
1083 spin_unlock(&inode_lock);
1084 return NULL;
1085}
1086
1087/**
1088 * ifind_fast - internal function, you want ilookup() or iget().
1089 * @sb: super block of file system to search
1090 * @head: head of the list to search
1091 * @ino: inode number to search for
1092 *
1093 * ifind_fast() searches for the inode @ino in the inode cache. This is for
1094 * file systems where the inode number is sufficient for unique identification
1095 * of an inode.
1096 *
1097 * If the inode is in the cache, the inode is returned with an incremented
1098 * reference count.
1099 *
1100 * Otherwise NULL is returned.
1101 */
Matt Mackall5d2bea42006-01-08 01:05:21 -08001102static struct inode *ifind_fast(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct hlist_head *head, unsigned long ino)
1104{
1105 struct inode *inode;
1106
1107 spin_lock(&inode_lock);
1108 inode = find_inode_fast(sb, head, ino);
1109 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 spin_unlock(&inode_lock);
1111 wait_on_inode(inode);
1112 return inode;
1113 }
1114 spin_unlock(&inode_lock);
1115 return NULL;
1116}
1117
1118/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001119 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * @sb: super block of file system to search
1121 * @hashval: hash value (usually inode number) to search for
1122 * @test: callback used for comparisons between inodes
1123 * @data: opaque data pointer to pass to @test
1124 *
1125 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1126 * @data in the inode cache. This is a generalized version of ilookup() for
1127 * file systems where the inode number is not sufficient for unique
1128 * identification of an inode.
1129 *
1130 * If the inode is in the cache, the inode is returned with an incremented
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001131 * reference count. Note, the inode lock is not waited upon so you have to be
1132 * very careful what you do with the returned inode. You probably should be
1133 * using ilookup5() instead.
1134 *
1135 * Otherwise NULL is returned.
1136 *
1137 * Note, @test is called with the inode_lock held, so can't sleep.
1138 */
1139struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1140 int (*test)(struct inode *, void *), void *data)
1141{
1142 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1143
1144 return ifind(sb, head, test, data, 0);
1145}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001146EXPORT_SYMBOL(ilookup5_nowait);
1147
1148/**
1149 * ilookup5 - search for an inode in the inode cache
1150 * @sb: super block of file system to search
1151 * @hashval: hash value (usually inode number) to search for
1152 * @test: callback used for comparisons between inodes
1153 * @data: opaque data pointer to pass to @test
1154 *
1155 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1156 * @data in the inode cache. This is a generalized version of ilookup() for
1157 * file systems where the inode number is not sufficient for unique
1158 * identification of an inode.
1159 *
1160 * If the inode is in the cache, the inode lock is waited upon and the inode is
1161 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *
1163 * Otherwise NULL is returned.
1164 *
1165 * Note, @test is called with the inode_lock held, so can't sleep.
1166 */
1167struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1168 int (*test)(struct inode *, void *), void *data)
1169{
1170 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1171
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001172 return ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174EXPORT_SYMBOL(ilookup5);
1175
1176/**
1177 * ilookup - search for an inode in the inode cache
1178 * @sb: super block of file system to search
1179 * @ino: inode number to search for
1180 *
1181 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
1182 * This is for file systems where the inode number is sufficient for unique
1183 * identification of an inode.
1184 *
1185 * If the inode is in the cache, the inode is returned with an incremented
1186 * reference count.
1187 *
1188 * Otherwise NULL is returned.
1189 */
1190struct inode *ilookup(struct super_block *sb, unsigned long ino)
1191{
1192 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1193
1194 return ifind_fast(sb, head, ino);
1195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196EXPORT_SYMBOL(ilookup);
1197
1198/**
1199 * iget5_locked - obtain an inode from a mounted file system
1200 * @sb: super block of file system
1201 * @hashval: hash value (usually inode number) to get
1202 * @test: callback used for comparisons between inodes
1203 * @set: callback used to initialize a new struct inode
1204 * @data: opaque data pointer to pass to @test and @set
1205 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * iget5_locked() uses ifind() to search for the inode specified by @hashval
1207 * and @data in the inode cache and if present it is returned with an increased
1208 * reference count. This is a generalized version of iget_locked() for file
1209 * systems where the inode number is not sufficient for unique identification
1210 * of an inode.
1211 *
1212 * If the inode is not in cache, get_new_inode() is called to allocate a new
1213 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1214 * file system gets to fill it in before unlocking it via unlock_new_inode().
1215 *
1216 * Note both @test and @set are called with the inode_lock held, so can't sleep.
1217 */
1218struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1219 int (*test)(struct inode *, void *),
1220 int (*set)(struct inode *, void *), void *data)
1221{
1222 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1223 struct inode *inode;
1224
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001225 inode = ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (inode)
1227 return inode;
1228 /*
1229 * get_new_inode() will do the right thing, re-trying the search
1230 * in case it had to block at any point.
1231 */
1232 return get_new_inode(sb, head, test, set, data);
1233}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234EXPORT_SYMBOL(iget5_locked);
1235
1236/**
1237 * iget_locked - obtain an inode from a mounted file system
1238 * @sb: super block of file system
1239 * @ino: inode number to get
1240 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1242 * the inode cache and if present it is returned with an increased reference
1243 * count. This is for file systems where the inode number is sufficient for
1244 * unique identification of an inode.
1245 *
1246 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1247 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1248 * The file system gets to fill it in before unlocking it via
1249 * unlock_new_inode().
1250 */
1251struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1252{
1253 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1254 struct inode *inode;
1255
1256 inode = ifind_fast(sb, head, ino);
1257 if (inode)
1258 return inode;
1259 /*
1260 * get_new_inode_fast() will do the right thing, re-trying the search
1261 * in case it had to block at any point.
1262 */
1263 return get_new_inode_fast(sb, head, ino);
1264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265EXPORT_SYMBOL(iget_locked);
1266
Al Viro261bca82008-12-30 01:48:21 -05001267int insert_inode_locked(struct inode *inode)
1268{
1269 struct super_block *sb = inode->i_sb;
1270 ino_t ino = inode->i_ino;
1271 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001272
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001273 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001274 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001275 struct hlist_node *node;
1276 struct inode *old = NULL;
Al Viro261bca82008-12-30 01:48:21 -05001277 spin_lock(&inode_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001278 hlist_for_each_entry(old, node, head, i_hash) {
1279 if (old->i_ino != ino)
1280 continue;
1281 if (old->i_sb != sb)
1282 continue;
Al Viroa4ffdde2010-06-02 17:38:30 -04001283 if (old->i_state & (I_FREEING|I_WILL_FREE))
Al Viro72a43d62009-05-13 19:13:40 +01001284 continue;
1285 break;
1286 }
1287 if (likely(!node)) {
Al Viro261bca82008-12-30 01:48:21 -05001288 hlist_add_head(&inode->i_hash, head);
1289 spin_unlock(&inode_lock);
1290 return 0;
1291 }
1292 __iget(old);
1293 spin_unlock(&inode_lock);
1294 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001295 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001296 iput(old);
1297 return -EBUSY;
1298 }
1299 iput(old);
1300 }
1301}
Al Viro261bca82008-12-30 01:48:21 -05001302EXPORT_SYMBOL(insert_inode_locked);
1303
1304int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1305 int (*test)(struct inode *, void *), void *data)
1306{
1307 struct super_block *sb = inode->i_sb;
1308 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001309
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001310 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001311
1312 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001313 struct hlist_node *node;
1314 struct inode *old = NULL;
1315
Al Viro261bca82008-12-30 01:48:21 -05001316 spin_lock(&inode_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001317 hlist_for_each_entry(old, node, head, i_hash) {
1318 if (old->i_sb != sb)
1319 continue;
1320 if (!test(old, data))
1321 continue;
Al Viroa4ffdde2010-06-02 17:38:30 -04001322 if (old->i_state & (I_FREEING|I_WILL_FREE))
Al Viro72a43d62009-05-13 19:13:40 +01001323 continue;
1324 break;
1325 }
1326 if (likely(!node)) {
Al Viro261bca82008-12-30 01:48:21 -05001327 hlist_add_head(&inode->i_hash, head);
1328 spin_unlock(&inode_lock);
1329 return 0;
1330 }
1331 __iget(old);
1332 spin_unlock(&inode_lock);
1333 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001334 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001335 iput(old);
1336 return -EBUSY;
1337 }
1338 iput(old);
1339 }
1340}
Al Viro261bca82008-12-30 01:48:21 -05001341EXPORT_SYMBOL(insert_inode_locked4);
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Al Viro45321ac2010-06-07 13:43:19 -04001344int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Al Viro45321ac2010-06-07 13:43:19 -04001346 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348EXPORT_SYMBOL(generic_delete_inode);
1349
Al Viro45321ac2010-06-07 13:43:19 -04001350/*
1351 * Normal UNIX filesystem behaviour: delete the
1352 * inode when the usage count drops to zero, and
1353 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001354 */
Al Viro45321ac2010-06-07 13:43:19 -04001355int generic_drop_inode(struct inode *inode)
1356{
Al Viro1d3382cb2010-10-23 15:19:20 -04001357 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001358}
1359EXPORT_SYMBOL_GPL(generic_drop_inode);
1360
1361/*
1362 * Called when we're dropping the last reference
1363 * to an inode.
1364 *
1365 * Call the FS "drop_inode()" function, defaulting to
1366 * the legacy UNIX filesystem behaviour. If it tells
1367 * us to evict inode, do so. Otherwise, retain inode
1368 * in cache if fs is alive, sync and evict if fs is
1369 * shutting down.
1370 */
1371static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001374 const struct super_operations *op = inode->i_sb->s_op;
1375 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Al Viro45321ac2010-06-07 13:43:19 -04001377 if (op && op->drop_inode)
1378 drop = op->drop_inode(inode);
1379 else
1380 drop = generic_drop_inode(inode);
1381
1382 if (!drop) {
Christoph Hellwigacb0c852007-05-08 00:25:52 -07001383 if (sb->s_flags & MS_ACTIVE) {
Nick Piggin9e38d862010-10-23 06:55:17 -04001384 inode->i_state |= I_REFERENCED;
1385 if (!(inode->i_state & (I_DIRTY|I_SYNC))) {
1386 inode_lru_list_add(inode);
1387 }
Alexander Viro991114c2005-06-23 00:09:01 -07001388 spin_unlock(&inode_lock);
Al Viro45321ac2010-06-07 13:43:19 -04001389 return;
Alexander Viro991114c2005-06-23 00:09:01 -07001390 }
Nick Piggin7ef0d732009-03-12 14:31:38 -07001391 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001392 inode->i_state |= I_WILL_FREE;
1393 spin_unlock(&inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 write_inode_now(inode, 1);
1395 spin_lock(&inode_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001396 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001397 inode->i_state &= ~I_WILL_FREE;
Dave Chinner4c51acb2010-10-23 06:58:09 -04001398 __remove_inode_hash(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001400
Nick Piggin7ef0d732009-03-12 14:31:38 -07001401 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001402 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001403
1404 /*
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001405 * Move the inode off the IO lists and LRU once I_FREEING is
1406 * set so that it won't get moved back on there if it is dirty.
Nick Piggin9e38d862010-10-23 06:55:17 -04001407 */
1408 inode_lru_list_del(inode);
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001409 list_del_init(&inode->i_wb_list);
Nick Piggin9e38d862010-10-23 06:55:17 -04001410
Christoph Hellwig646ec462010-10-23 07:15:32 -04001411 __inode_sb_list_del(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 spin_unlock(&inode_lock);
Al Viro644da592010-06-07 13:21:05 -04001413 evict(inode);
Dave Chinner4c51acb2010-10-23 06:58:09 -04001414 remove_inode_hash(inode);
Andrea Arcangeli7f04c262005-10-30 15:03:05 -08001415 wake_up_inode(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001416 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 destroy_inode(inode);
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301421 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * @inode: inode to put
1423 *
1424 * Puts an inode, dropping its usage count. If the inode use count hits
1425 * zero, the inode is then freed and may also be destroyed.
1426 *
1427 * Consequently, iput() can sleep.
1428 */
1429void iput(struct inode *inode)
1430{
1431 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001432 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1435 iput_final(inode);
1436 }
1437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438EXPORT_SYMBOL(iput);
1439
1440/**
1441 * bmap - find a block number in a file
1442 * @inode: inode of file
1443 * @block: block to find
1444 *
1445 * Returns the block number on the device holding the inode that
1446 * is the disk block number for the block of the file requested.
1447 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301448 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 * file.
1450 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301451sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 sector_t res = 0;
1454 if (inode->i_mapping->a_ops->bmap)
1455 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1456 return res;
1457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458EXPORT_SYMBOL(bmap);
1459
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001460/*
1461 * With relative atime, only update atime if the previous atime is
1462 * earlier than either the ctime or mtime or if at least a day has
1463 * passed since the last atime update.
1464 */
1465static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1466 struct timespec now)
1467{
1468
1469 if (!(mnt->mnt_flags & MNT_RELATIME))
1470 return 1;
1471 /*
1472 * Is mtime younger than atime? If yes, update atime:
1473 */
1474 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1475 return 1;
1476 /*
1477 * Is ctime younger than atime? If yes, update atime:
1478 */
1479 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1480 return 1;
1481
1482 /*
1483 * Is the previous atime value older than a day? If yes,
1484 * update atime:
1485 */
1486 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1487 return 1;
1488 /*
1489 * Good, we can skip the atime update:
1490 */
1491 return 0;
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001495 * touch_atime - update the access time
1496 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001497 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 * Update the accessed time on an inode and mark it for writeback.
1500 * This function automatically handles read only file systems and media,
1501 * as well as the "noatime" flag and inode specific "noatime" markers.
1502 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001503void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001505 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct timespec now;
1507
Andrew Mortonb2276132006-12-13 00:34:33 -08001508 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001509 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001510 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001511 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001512 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001513 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001514
Dave Hansencdb70f32008-02-15 14:37:41 -08001515 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001516 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001517 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001518 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001521
1522 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001523 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001524
Valerie Henson47ae32d2006-12-13 00:34:34 -08001525 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001526 return;
1527
1528 if (mnt_want_write(mnt))
1529 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001530
1531 inode->i_atime = now;
1532 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001533 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001535EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001538 * file_update_time - update mtime and ctime time
1539 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001541 * Update the mtime and ctime members of an inode and mark the inode
1542 * for writeback. Note that this function is meant exclusively for
1543 * usage in the file write path of filesystems, and filesystems may
1544 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001545 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001546 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
1548
Christoph Hellwig870f4812006-01-09 20:52:01 -08001549void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001551 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001553 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Andi Kleence06e0b2009-09-18 13:05:48 -07001555 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (IS_NOCMTIME(inode))
1557 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001558
Andi Kleence06e0b2009-09-18 13:05:48 -07001559 now = current_fs_time(inode->i_sb);
1560 if (!timespec_equal(&inode->i_mtime, &now))
1561 sync_it = S_MTIME;
1562
1563 if (!timespec_equal(&inode->i_ctime, &now))
1564 sync_it |= S_CTIME;
1565
1566 if (IS_I_VERSION(inode))
1567 sync_it |= S_VERSION;
1568
1569 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return;
1571
Andi Kleence06e0b2009-09-18 13:05:48 -07001572 /* Finally allowed to write? Takes lock. */
1573 if (mnt_want_write_file(file))
1574 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Andi Kleence06e0b2009-09-18 13:05:48 -07001576 /* Only change inode inside the lock region */
1577 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001578 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001579 if (sync_it & S_CTIME)
1580 inode->i_ctime = now;
1581 if (sync_it & S_MTIME)
1582 inode->i_mtime = now;
1583 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001584 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001586EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588int inode_needs_sync(struct inode *inode)
1589{
1590 if (IS_SYNC(inode))
1591 return 1;
1592 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1593 return 1;
1594 return 0;
1595}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596EXPORT_SYMBOL(inode_needs_sync);
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598int inode_wait(void *word)
1599{
1600 schedule();
1601 return 0;
1602}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001603EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001606 * If we try to find an inode in the inode hash while it is being
1607 * deleted, we have to wait until the filesystem completes its
1608 * deletion before reporting that it isn't found. This function waits
1609 * until the deletion _might_ have completed. Callers are responsible
1610 * to recheck inode state.
1611 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001612 * It doesn't matter if I_NEW is not set initially, a call to
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001613 * wake_up_inode() after removing from the hash list will DTRT.
1614 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 * This is called with inode_lock held.
1616 */
1617static void __wait_on_freeing_inode(struct inode *inode)
1618{
1619 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001620 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1621 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1623 spin_unlock(&inode_lock);
1624 schedule();
1625 finish_wait(wq, &wait.wait);
1626 spin_lock(&inode_lock);
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629static __initdata unsigned long ihash_entries;
1630static int __init set_ihash_entries(char *str)
1631{
1632 if (!str)
1633 return 0;
1634 ihash_entries = simple_strtoul(str, &str, 0);
1635 return 1;
1636}
1637__setup("ihash_entries=", set_ihash_entries);
1638
1639/*
1640 * Initialize the waitqueues and inode hash table.
1641 */
1642void __init inode_init_early(void)
1643{
1644 int loop;
1645
1646 /* If hashes are distributed across NUMA nodes, defer
1647 * hash allocation until vmalloc space is available.
1648 */
1649 if (hashdist)
1650 return;
1651
1652 inode_hashtable =
1653 alloc_large_system_hash("Inode-cache",
1654 sizeof(struct hlist_head),
1655 ihash_entries,
1656 14,
1657 HASH_EARLY,
1658 &i_hash_shift,
1659 &i_hash_mask,
1660 0);
1661
1662 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1663 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1664}
1665
Denis Cheng74bf17c2007-10-16 23:26:30 -07001666void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 int loop;
1669
1670 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001671 inode_cachep = kmem_cache_create("inode_cache",
1672 sizeof(struct inode),
1673 0,
1674 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1675 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001676 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001677 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 /* Hash may have been set up in inode_init_early */
1680 if (!hashdist)
1681 return;
1682
1683 inode_hashtable =
1684 alloc_large_system_hash("Inode-cache",
1685 sizeof(struct hlist_head),
1686 ihash_entries,
1687 14,
1688 0,
1689 &i_hash_shift,
1690 &i_hash_mask,
1691 0);
1692
1693 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1694 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1695}
1696
1697void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1698{
1699 inode->i_mode = mode;
1700 if (S_ISCHR(mode)) {
1701 inode->i_fop = &def_chr_fops;
1702 inode->i_rdev = rdev;
1703 } else if (S_ISBLK(mode)) {
1704 inode->i_fop = &def_blk_fops;
1705 inode->i_rdev = rdev;
1706 } else if (S_ISFIFO(mode))
1707 inode->i_fop = &def_fifo_fops;
1708 else if (S_ISSOCK(mode))
1709 inode->i_fop = &bad_sock_fops;
1710 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001711 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1712 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1713 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001716
1717/**
1718 * Init uid,gid,mode for new inode according to posix standards
1719 * @inode: New inode
1720 * @dir: Directory inode
1721 * @mode: mode of the new inode
1722 */
1723void inode_init_owner(struct inode *inode, const struct inode *dir,
1724 mode_t mode)
1725{
1726 inode->i_uid = current_fsuid();
1727 if (dir && dir->i_mode & S_ISGID) {
1728 inode->i_gid = dir->i_gid;
1729 if (S_ISDIR(mode))
1730 mode |= S_ISGID;
1731 } else
1732 inode->i_gid = current_fsgid();
1733 inode->i_mode = mode;
1734}
1735EXPORT_SYMBOL(inode_init_owner);