blob: 8c3491302e0cd0f73ec235f1143b7f80ac9ec8e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 1997 Linus Torvalds
Christoph Hellwig4b4563d2011-05-27 09:28:01 -04003 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/fs.h>
6#include <linux/mm.h>
7#include <linux/dcache.h>
8#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
10#include <linux/writeback.h>
11#include <linux/module.h>
12#include <linux/backing-dev.h>
13#include <linux/wait.h>
Nick Piggin88e0fbc2009-09-22 16:43:50 -070014#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/hash.h>
16#include <linux/swap.h>
17#include <linux/security.h>
18#include <linux/pagemap.h>
19#include <linux/cdev.h>
20#include <linux/bootmem.h>
Eric Paris3be25f42009-05-21 17:01:26 -040021#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080022#include <linux/mount.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080023#include <linux/async.h>
Al Virof19d4a82009-06-08 19:50:45 -040024#include <linux/posix_acl.h>
Heiko Carstens9ce6e0b2011-05-22 18:54:21 +020025#include <linux/prefetch.h>
Eric Parisa178d202010-10-25 14:41:59 -040026#include <linux/ima.h>
Serge E. Hallyne795b712011-03-23 16:43:25 -070027#include <linux/cred.h>
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040028#include <linux/buffer_head.h> /* for inode_has_buffers */
Dave Chinnera66979a2011-03-22 22:23:41 +110029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040032 * Inode locking rules:
Dave Chinner250df6e2011-03-22 22:23:36 +110033 *
34 * inode->i_lock protects:
35 * inode->i_state, inode->i_hash, __iget()
Dave Chinner02afc412011-03-22 22:23:38 +110036 * inode_lru_lock protects:
Dave Chinner98b745c2011-07-08 14:14:39 +100037 * inode->i_sb->s_inode_lru, inode->i_lru
Dave Chinner55fa6092011-03-22 22:23:40 +110038 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
Dave Chinnera66979a2011-03-22 22:23:41 +110040 * inode_wb_list_lock protects:
41 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
Dave Chinner67a23c42011-03-22 22:23:42 +110042 * inode_hash_lock protects:
43 * inode_hashtable, inode->i_hash
Dave Chinner250df6e2011-03-22 22:23:36 +110044 *
45 * Lock ordering:
Dave Chinner55fa6092011-03-22 22:23:40 +110046 *
47 * inode_sb_list_lock
48 * inode->i_lock
Dave Chinner02afc412011-03-22 22:23:38 +110049 * inode_lru_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110050 *
51 * inode_wb_list_lock
52 * inode->i_lock
Dave Chinner67a23c42011-03-22 22:23:42 +110053 *
54 * inode_hash_lock
55 * inode_sb_list_lock
56 * inode->i_lock
57 *
58 * iunique_lock
59 * inode_hash_lock
Dave Chinner250df6e2011-03-22 22:23:36 +110060 */
61
Eric Dumazetfa3536c2006-03-26 01:37:24 -080062static unsigned int i_hash_mask __read_mostly;
63static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110064static struct hlist_head *inode_hashtable __read_mostly;
65static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Chinner02afc412011-03-22 22:23:38 +110067static DEFINE_SPINLOCK(inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dave Chinner55fa6092011-03-22 22:23:40 +110069__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
Dave Chinnera66979a2011-03-22 22:23:41 +110070__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +110071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +010073 * iprune_sem provides exclusion between the icache shrinking and the
74 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -070075 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +010076 * We don't actually need it to protect anything in the umount path,
77 * but only need to cycle through it to make sure any inode that
78 * prune_icache took off the LRU list has been fully torn down by the
79 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -070081static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020084 * Empty aops. Can be used for the cases where the user does not
85 * define any of the address_space operations.
86 */
87const struct address_space_operations empty_aops = {
88};
89EXPORT_SYMBOL(empty_aops);
90
91/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * Statistics gathering..
93 */
94struct inodes_stat_t inodes_stat;
95
Nick Piggin3e880fb2011-01-07 17:49:19 +110096static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnerfcb94f72011-07-08 14:14:38 +100097static DEFINE_PER_CPU(unsigned int, nr_unused);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040098
Manish Katiyar6b3304b2009-03-31 19:35:54 +053099static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Nick Piggin3e880fb2011-01-07 17:49:19 +1100101static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400102{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100103 int i;
104 int sum = 0;
105 for_each_possible_cpu(i)
106 sum += per_cpu(nr_inodes, i);
107 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400108}
109
110static inline int get_nr_inodes_unused(void)
111{
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000112 int i;
113 int sum = 0;
114 for_each_possible_cpu(i)
115 sum += per_cpu(nr_unused, i);
116 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400117}
118
119int get_nr_dirty_inodes(void)
120{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100121 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400122 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
123 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400124}
125
126/*
127 * Handle nr_inode sysctl
128 */
129#ifdef CONFIG_SYSCTL
130int proc_nr_inodes(ctl_table *table, int write,
131 void __user *buffer, size_t *lenp, loff_t *ppos)
132{
133 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000134 inodes_stat.nr_unused = get_nr_inodes_unused();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400135 return proc_dointvec(table, write, buffer, lenp, ppos);
136}
137#endif
138
David Chinner2cb15992008-10-30 17:32:23 +1100139/**
140 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800141 * @sb: superblock inode belongs to
142 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100143 *
144 * These are initializations that need to be done on every inode
145 * allocation as the fields are not initialised by slab allocation.
146 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300147int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700149 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800150 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530151 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100152
153 inode->i_sb = sb;
154 inode->i_blkbits = sb->s_blocksize_bits;
155 inode->i_flags = 0;
156 atomic_set(&inode->i_count, 1);
157 inode->i_op = &empty_iops;
158 inode->i_fop = &empty_fops;
159 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500160 inode->i_uid = 0;
161 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100162 atomic_set(&inode->i_writecount, 0);
163 inode->i_size = 0;
164 inode->i_blocks = 0;
165 inode->i_bytes = 0;
166 inode->i_generation = 0;
167#ifdef CONFIG_QUOTA
168 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
169#endif
170 inode->i_pipe = NULL;
171 inode->i_bdev = NULL;
172 inode->i_cdev = NULL;
173 inode->i_rdev = 0;
174 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500175
176 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300177 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100178 spin_lock_init(&inode->i_lock);
179 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
180
181 mutex_init(&inode->i_mutex);
182 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
183
184 init_rwsem(&inode->i_alloc_sem);
185 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
186
187 mapping->a_ops = &empty_aops;
188 mapping->host = inode;
189 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800190 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100191 mapping->assoc_mapping = NULL;
192 mapping->backing_dev_info = &default_backing_dev_info;
193 mapping->writeback_index = 0;
194
195 /*
196 * If the block_device provides a backing_dev_info for client
197 * inodes then use that. Otherwise the inode share the bdev's
198 * backing_dev_info.
199 */
200 if (sb->s_bdev) {
201 struct backing_dev_info *bdi;
202
Jens Axboe2c96ce92009-09-15 09:43:56 +0200203 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100204 mapping->backing_dev_info = bdi;
205 }
206 inode->i_private = NULL;
207 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400208#ifdef CONFIG_FS_POSIX_ACL
209 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
210#endif
David Chinner2cb15992008-10-30 17:32:23 +1100211
Eric Paris3be25f42009-05-21 17:01:26 -0400212#ifdef CONFIG_FSNOTIFY
213 inode->i_fsnotify_mask = 0;
214#endif
215
Nick Piggin3e880fb2011-01-07 17:49:19 +1100216 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400217
Christoph Hellwig54e34622009-08-07 14:38:25 -0300218 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300219out:
220 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100221}
222EXPORT_SYMBOL(inode_init_always);
223
224static struct inode *alloc_inode(struct super_block *sb)
225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct inode *inode;
227
228 if (sb->s_op->alloc_inode)
229 inode = sb->s_op->alloc_inode(sb);
230 else
David Chinner2cb15992008-10-30 17:32:23 +1100231 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Christoph Hellwig54e34622009-08-07 14:38:25 -0300233 if (!inode)
234 return NULL;
235
236 if (unlikely(inode_init_always(sb, inode))) {
237 if (inode->i_sb->s_op->destroy_inode)
238 inode->i_sb->s_op->destroy_inode(inode);
239 else
240 kmem_cache_free(inode_cachep, inode);
241 return NULL;
242 }
243
244 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Nick Pigginff0c7d12011-01-07 17:49:50 +1100247void free_inode_nonrcu(struct inode *inode)
248{
249 kmem_cache_free(inode_cachep, inode);
250}
251EXPORT_SYMBOL(free_inode_nonrcu);
252
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300253void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200255 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400257 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400258#ifdef CONFIG_FS_POSIX_ACL
259 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
260 posix_acl_release(inode->i_acl);
261 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
262 posix_acl_release(inode->i_default_acl);
263#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100264 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300265}
266EXPORT_SYMBOL(__destroy_inode);
267
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100268static void i_callback(struct rcu_head *head)
269{
270 struct inode *inode = container_of(head, struct inode, i_rcu);
271 INIT_LIST_HEAD(&inode->i_dentry);
272 kmem_cache_free(inode_cachep, inode);
273}
274
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200275static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300276{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100277 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300278 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (inode->i_sb->s_op->destroy_inode)
280 inode->i_sb->s_op->destroy_inode(inode);
281 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100282 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100285void address_space_init_once(struct address_space *mapping)
286{
287 memset(mapping, 0, sizeof(*mapping));
288 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
289 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700290 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100291 INIT_LIST_HEAD(&mapping->private_list);
292 spin_lock_init(&mapping->private_lock);
293 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
294 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100295}
296EXPORT_SYMBOL(address_space_init_once);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*
299 * These are initializations that only need to be done
300 * once, because the fields are idempotent across use
301 * of the inode, so let the slab aware of that.
302 */
303void inode_init_once(struct inode *inode)
304{
305 memset(inode, 0, sizeof(*inode));
306 INIT_HLIST_NODE(&inode->i_hash);
307 INIT_LIST_HEAD(&inode->i_dentry);
308 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100309 INIT_LIST_HEAD(&inode->i_wb_list);
310 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100311 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400313#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500314 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317EXPORT_SYMBOL(inode_init_once);
318
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700319static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530321 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Christoph Lametera35afb82007-05-16 22:10:57 -0700323 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100327 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530329void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Nick Piggin9e38d862010-10-23 06:55:17 -0400331 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Al Viro7de9c6ee2010-10-23 11:11:40 -0400334/*
335 * get additional reference to inode; caller must already hold one.
336 */
337void ihold(struct inode *inode)
338{
339 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
340}
341EXPORT_SYMBOL(ihold);
342
Nick Piggin9e38d862010-10-23 06:55:17 -0400343static void inode_lru_list_add(struct inode *inode)
344{
Dave Chinner02afc412011-03-22 22:23:38 +1100345 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100346 if (list_empty(&inode->i_lru)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000347 list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
348 inode->i_sb->s_nr_inodes_unused++;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000349 this_cpu_inc(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400350 }
Dave Chinner02afc412011-03-22 22:23:38 +1100351 spin_unlock(&inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400352}
353
354static void inode_lru_list_del(struct inode *inode)
355{
Dave Chinner02afc412011-03-22 22:23:38 +1100356 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100357 if (!list_empty(&inode->i_lru)) {
358 list_del_init(&inode->i_lru);
Dave Chinner98b745c2011-07-08 14:14:39 +1000359 inode->i_sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000360 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400361 }
Dave Chinner02afc412011-03-22 22:23:38 +1100362 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Christoph Hellwig646ec462010-10-23 07:15:32 -0400365/**
366 * inode_sb_list_add - add inode to the superblock list of inodes
367 * @inode: inode to add
368 */
369void inode_sb_list_add(struct inode *inode)
370{
Dave Chinner55fa6092011-03-22 22:23:40 +1100371 spin_lock(&inode_sb_list_lock);
372 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
373 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400374}
375EXPORT_SYMBOL_GPL(inode_sb_list_add);
376
Dave Chinner55fa6092011-03-22 22:23:40 +1100377static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400378{
Dave Chinner55fa6092011-03-22 22:23:40 +1100379 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400380 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100381 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400382}
383
Dave Chinner4c51acb2010-10-23 06:58:09 -0400384static unsigned long hash(struct super_block *sb, unsigned long hashval)
385{
386 unsigned long tmp;
387
388 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
389 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400390 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
391 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400392}
393
394/**
395 * __insert_inode_hash - hash an inode
396 * @inode: unhashed inode
397 * @hashval: unsigned long value used to locate this object in the
398 * inode_hashtable.
399 *
400 * Add an inode to the inode hash for this superblock.
401 */
402void __insert_inode_hash(struct inode *inode, unsigned long hashval)
403{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400404 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
405
Dave Chinner67a23c42011-03-22 22:23:42 +1100406 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100407 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400408 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100409 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100410 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400411}
412EXPORT_SYMBOL(__insert_inode_hash);
413
414/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400415 * remove_inode_hash - remove an inode from the hash
416 * @inode: inode to unhash
417 *
418 * Remove an inode from the superblock.
419 */
420void remove_inode_hash(struct inode *inode)
421{
Dave Chinner67a23c42011-03-22 22:23:42 +1100422 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100423 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400424 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100425 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100426 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400427}
428EXPORT_SYMBOL(remove_inode_hash);
429
Al Virob0683aa2010-06-04 20:55:25 -0400430void end_writeback(struct inode *inode)
431{
432 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700433 /*
434 * We have to cycle tree_lock here because reclaim can be still in the
435 * process of removing the last page (in __delete_from_page_cache())
436 * and we must not free mapping under it.
437 */
438 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400439 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700440 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400441 BUG_ON(!list_empty(&inode->i_data.private_list));
442 BUG_ON(!(inode->i_state & I_FREEING));
443 BUG_ON(inode->i_state & I_CLEAR);
444 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100445 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400446 inode->i_state = I_FREEING | I_CLEAR;
447}
448EXPORT_SYMBOL(end_writeback);
449
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100450/*
451 * Free the inode passed in, removing it from the lists it is still connected
452 * to. We remove any pages still attached to the inode and wait for any IO that
453 * is still in progress before finally destroying the inode.
454 *
455 * An inode must already be marked I_FREEING so that we avoid the inode being
456 * moved back onto lists if we race with other code that manipulates the lists
457 * (e.g. writeback_single_inode). The caller is responsible for setting this.
458 *
459 * An inode must already be removed from the LRU list before being evicted from
460 * the cache. This should occur atomically with setting the I_FREEING state
461 * flag, so no inodes here should ever be on the LRU when being evicted.
462 */
Al Viro644da592010-06-07 13:21:05 -0400463static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400464{
465 const struct super_operations *op = inode->i_sb->s_op;
466
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100467 BUG_ON(!(inode->i_state & I_FREEING));
468 BUG_ON(!list_empty(&inode->i_lru));
469
Dave Chinnera66979a2011-03-22 22:23:41 +1100470 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100471 inode_sb_list_del(inode);
472
Al Virobe7ce412010-06-04 19:40:39 -0400473 if (op->evict_inode) {
474 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400475 } else {
476 if (inode->i_data.nrpages)
477 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400478 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400479 }
Al Viro661074e2010-06-04 20:19:55 -0400480 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
481 bd_forget(inode);
482 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
483 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100484
485 remove_inode_hash(inode);
486
487 spin_lock(&inode->i_lock);
488 wake_up_bit(&inode->i_state, __I_NEW);
489 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
490 spin_unlock(&inode->i_lock);
491
492 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*
496 * dispose_list - dispose of the contents of a local list
497 * @head: the head of the list to free
498 *
499 * Dispose-list gets a local list with local inodes in it, so it doesn't
500 * need to worry about list corruption and SMP locks.
501 */
502static void dispose_list(struct list_head *head)
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 while (!list_empty(head)) {
505 struct inode *inode;
506
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100507 inode = list_first_entry(head, struct inode, i_lru);
508 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Al Viro644da592010-06-07 13:21:05 -0400510 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/**
Al Viro63997e92010-10-25 20:49:35 -0400515 * evict_inodes - evict all evictable inodes for a superblock
516 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *
Al Viro63997e92010-10-25 20:49:35 -0400518 * Make sure that no inodes with zero refcount are retained. This is
519 * called by superblock shutdown after having MS_ACTIVE flag removed,
520 * so any inode reaching zero refcount during or after that call will
521 * be immediately evicted.
522 */
523void evict_inodes(struct super_block *sb)
524{
525 struct inode *inode, *next;
526 LIST_HEAD(dispose);
527
Dave Chinner55fa6092011-03-22 22:23:40 +1100528 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400529 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
530 if (atomic_read(&inode->i_count))
531 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100532
533 spin_lock(&inode->i_lock);
534 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
535 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400536 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100537 }
Al Viro63997e92010-10-25 20:49:35 -0400538
539 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100540 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100541 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100542 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400543 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100544 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400545
546 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100547
548 /*
549 * Cycle through iprune_sem to make sure any inode that prune_icache
550 * moved off the list before we took the lock has been fully torn
551 * down.
552 */
553 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400554 up_write(&iprune_sem);
555}
556
557/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200558 * invalidate_inodes - attempt to free all inodes on a superblock
559 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100560 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200561 *
562 * Attempts to free all inodes for a given superblock. If there were any
563 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100564 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
565 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
NeilBrown93b270f2011-02-24 17:25:47 +1100567int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400569 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200570 struct inode *inode, *next;
571 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dave Chinner55fa6092011-03-22 22:23:40 +1100573 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200574 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100575 spin_lock(&inode->i_lock);
576 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
577 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100579 }
NeilBrown93b270f2011-02-24 17:25:47 +1100580 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100581 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100582 busy = 1;
583 continue;
584 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200585 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100586 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200587 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 continue;
589 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200590
Christoph Hellwig99a38912010-10-23 19:07:20 +0200591 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100592 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100593 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100594 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100596 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Christoph Hellwiga0318782010-10-24 19:40:33 +0200598 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 return busy;
601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603static int can_unuse(struct inode *inode)
604{
Nick Piggin9e38d862010-10-23 06:55:17 -0400605 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 0;
607 if (inode_has_buffers(inode))
608 return 0;
609 if (atomic_read(&inode->i_count))
610 return 0;
611 if (inode->i_data.nrpages)
612 return 0;
613 return 1;
614}
615
616/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400617 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100618 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 *
620 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400621 * pagecache removed. If the inode has metadata buffers attached to
622 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400624 * If the inode has the I_REFERENCED flag set, then it means that it has been
625 * used recently - the flag is set in iput_final(). When we encounter such an
626 * inode, clear the flag and move it to the back of the LRU so it gets another
627 * pass through the LRU before it gets reclaimed. This is necessary because of
628 * the fact we are doing lazy LRU updates to minimise lock contention so the
629 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
630 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Dave Chinner98b745c2011-07-08 14:14:39 +1000632static void shrink_icache_sb(struct super_block *sb, int *nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int nr_scanned;
636 unsigned long reap = 0;
637
Dave Chinner02afc412011-03-22 22:23:38 +1100638 spin_lock(&inode_lru_lock);
Dave Chinner98b745c2011-07-08 14:14:39 +1000639 for (nr_scanned = *nr_to_scan; nr_scanned >= 0; nr_scanned--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct inode *inode;
641
Dave Chinner98b745c2011-07-08 14:14:39 +1000642 if (list_empty(&sb->s_inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 break;
644
Dave Chinner98b745c2011-07-08 14:14:39 +1000645 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Nick Piggin9e38d862010-10-23 06:55:17 -0400647 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100648 * we are inverting the inode_lru_lock/inode->i_lock here,
649 * so use a trylock. If we fail to get the lock, just move the
650 * inode to the back of the list so we don't spin on it.
651 */
652 if (!spin_trylock(&inode->i_lock)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000653 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +1100654 continue;
655 }
656
657 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400658 * Referenced or dirty inodes are still in use. Give them
659 * another pass through the LRU as we canot reclaim them now.
660 */
661 if (atomic_read(&inode->i_count) ||
662 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100663 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100664 spin_unlock(&inode->i_lock);
Dave Chinner98b745c2011-07-08 14:14:39 +1000665 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000666 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400667 continue;
668 }
669
670 /* recently referenced inodes get one more pass */
671 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400672 inode->i_state &= ~I_REFERENCED;
Dave Chinner98b745c2011-07-08 14:14:39 +1000673 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100674 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 continue;
676 }
677 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
678 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100679 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100680 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800682 reap += invalidate_mapping_pages(&inode->i_data,
683 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100685 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Dave Chinner98b745c2011-07-08 14:14:39 +1000687 if (inode != list_entry(sb->s_inode_lru.next,
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100688 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100690 /* avoid lock inversions with trylock */
691 if (!spin_trylock(&inode->i_lock))
692 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100693 if (!can_unuse(inode)) {
694 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700698 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100700 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100701
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100702 list_move(&inode->i_lru, &freeable);
Dave Chinner98b745c2011-07-08 14:14:39 +1000703 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000704 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700706 if (current_is_kswapd())
707 __count_vm_events(KSWAPD_INODESTEAL, reap);
708 else
709 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100710 spin_unlock(&inode_lru_lock);
Dave Chinner98b745c2011-07-08 14:14:39 +1000711 *nr_to_scan = nr_scanned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 dispose_list(&freeable);
Dave Chinner98b745c2011-07-08 14:14:39 +1000714}
715
716static void prune_icache(int count)
717{
718 struct super_block *sb, *p = NULL;
719 int w_count;
720 int unused = inodes_stat.nr_unused;
721 int prune_ratio;
722 int pruned;
723
724 if (unused == 0 || count == 0)
725 return;
726 down_read(&iprune_sem);
727 if (count >= unused)
728 prune_ratio = 1;
729 else
730 prune_ratio = unused / count;
731 spin_lock(&sb_lock);
732 list_for_each_entry(sb, &super_blocks, s_list) {
733 if (list_empty(&sb->s_instances))
734 continue;
735 if (sb->s_nr_inodes_unused == 0)
736 continue;
737 sb->s_count++;
738 /* Now, we reclaim unused dentrins with fairness.
739 * We reclaim them same percentage from each superblock.
740 * We calculate number of dentries to scan on this sb
741 * as follows, but the implementation is arranged to avoid
742 * overflows:
743 * number of dentries to scan on this sb =
744 * count * (number of dentries on this sb /
745 * number of dentries in the machine)
746 */
747 spin_unlock(&sb_lock);
748 if (prune_ratio != 1)
749 w_count = (sb->s_nr_inodes_unused / prune_ratio) + 1;
750 else
751 w_count = sb->s_nr_inodes_unused;
752 pruned = w_count;
753 /*
754 * We need to be sure this filesystem isn't being unmounted,
755 * otherwise we could race with generic_shutdown_super(), and
756 * end up holding a reference to an inode while the filesystem
757 * is unmounted. So we try to get s_umount, and make sure
758 * s_root isn't NULL.
759 */
760 if (down_read_trylock(&sb->s_umount)) {
761 if ((sb->s_root != NULL) &&
762 (!list_empty(&sb->s_dentry_lru))) {
763 shrink_icache_sb(sb, &w_count);
764 pruned -= w_count;
765 }
766 up_read(&sb->s_umount);
767 }
768 spin_lock(&sb_lock);
769 if (p)
770 __put_super(p);
771 count -= pruned;
772 p = sb;
773 /* more work left to do? */
774 if (count <= 0)
775 break;
776 }
777 if (p)
778 __put_super(p);
779 spin_unlock(&sb_lock);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700780 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/*
784 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
785 * "unused" means that no dentries are referring to the inodes: the files are
786 * not open and the dcache references to those inodes have already been
787 * reclaimed.
788 *
789 * This function is passed the number of inodes to scan, and it returns the
790 * total number of remaining possibly-reclaimable inodes.
791 */
Ying Han1495f232011-05-24 17:12:27 -0700792static int shrink_icache_memory(struct shrinker *shrink,
793 struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Ying Han1495f232011-05-24 17:12:27 -0700795 int nr = sc->nr_to_scan;
796 gfp_t gfp_mask = sc->gfp_mask;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (nr) {
799 /*
800 * Nasty deadlock avoidance. We may hold various FS locks,
801 * and we don't want to recurse into the FS that called us
802 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!(gfp_mask & __GFP_FS))
805 return -1;
806 prune_icache(nr);
807 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400808 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Rusty Russell8e1f9362007-07-17 04:03:17 -0700811static struct shrinker icache_shrinker = {
812 .shrink = shrink_icache_memory,
813 .seeks = DEFAULT_SEEKS,
814};
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static void __wait_on_freeing_inode(struct inode *inode);
817/*
818 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530820static struct inode *find_inode(struct super_block *sb,
821 struct hlist_head *head,
822 int (*test)(struct inode *, void *),
823 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530826 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700829 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100830 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100831 if (inode->i_sb != sb) {
832 spin_unlock(&inode->i_lock);
833 continue;
834 }
835 if (!test(inode, data)) {
836 spin_unlock(&inode->i_lock);
837 continue;
838 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400839 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 __wait_on_freeing_inode(inode);
841 goto repeat;
842 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400843 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100844 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400845 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400847 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850/*
851 * find_inode_fast is the fast path version of find_inode, see the comment at
852 * iget_locked for details.
853 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530854static struct inode *find_inode_fast(struct super_block *sb,
855 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530858 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700861 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100862 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100863 if (inode->i_ino != ino) {
864 spin_unlock(&inode->i_lock);
865 continue;
866 }
867 if (inode->i_sb != sb) {
868 spin_unlock(&inode->i_lock);
869 continue;
870 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400871 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 __wait_on_freeing_inode(inode);
873 goto repeat;
874 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400875 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100876 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400877 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400879 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Eric Dumazetf991bd22010-10-23 11:18:01 -0400882/*
883 * Each cpu owns a range of LAST_INO_BATCH numbers.
884 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
885 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100886 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400887 * This does not significantly increase overflow rate because every CPU can
888 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
889 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
890 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
891 * overflow rate by 2x, which does not seem too significant.
892 *
893 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
894 * error if st_ino won't fit in target struct field. Use 32bit counter
895 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100896 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400897#define LAST_INO_BATCH 1024
898static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100899
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400900unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400901{
902 unsigned int *p = &get_cpu_var(last_ino);
903 unsigned int res = *p;
904
905#ifdef CONFIG_SMP
906 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
907 static atomic_t shared_last_ino;
908 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
909
910 res = next - LAST_INO_BATCH;
911 }
912#endif
913
914 *p = ++res;
915 put_cpu_var(last_ino);
916 return res;
David Chinner8290c352008-10-30 17:35:24 +1100917}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400918EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/**
921 * new_inode - obtain an inode
922 * @sb: superblock
923 *
Mel Gorman769848c2007-07-17 04:03:05 -0700924 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800925 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700926 * If HIGHMEM pages are unsuitable or it is known that pages allocated
927 * for the page cache are not reclaimable or migratable,
928 * mapping_set_gfp_mask() must be called with suitable flags on the
929 * newly created inode's mapping
930 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
932struct inode *new_inode(struct super_block *sb)
933{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530934 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Dave Chinner55fa6092011-03-22 22:23:40 +1100936 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 inode = alloc_inode(sb);
939 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100940 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100942 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100943 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 return inode;
946}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947EXPORT_SYMBOL(new_inode);
948
Dave Chinner250df6e2011-03-22 22:23:36 +1100949/**
950 * unlock_new_inode - clear the I_NEW state and wake up any waiters
951 * @inode: new inode to unlock
952 *
953 * Called when the inode is fully initialised to clear the new state of the
954 * inode and wake up anyone waiting for the inode to finish initialisation.
955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956void unlock_new_inode(struct inode *inode)
957{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200958#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900959 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200960 struct file_system_type *type = inode->i_sb->s_type;
961
Jan Kara9a7aa122009-06-04 15:26:49 +0200962 /* Set new key only if filesystem hasn't already changed it */
963 if (!lockdep_match_class(&inode->i_mutex,
964 &type->i_mutex_key)) {
965 /*
966 * ensure nobody is actually holding i_mutex
967 */
968 mutex_destroy(&inode->i_mutex);
969 mutex_init(&inode->i_mutex);
970 lockdep_set_class(&inode->i_mutex,
971 &type->i_mutex_dir_key);
972 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200973 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200974#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100975 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100976 WARN_ON(!(inode->i_state & I_NEW));
977 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100978 wake_up_bit(&inode->i_state, __I_NEW);
979 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981EXPORT_SYMBOL(unlock_new_inode);
982
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400983/**
984 * iget5_locked - obtain an inode from a mounted file system
985 * @sb: super block of file system
986 * @hashval: hash value (usually inode number) to get
987 * @test: callback used for comparisons between inodes
988 * @set: callback used to initialize a new struct inode
989 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400991 * Search for the inode specified by @hashval and @data in the inode cache,
992 * and if present it is return it with an increased reference count. This is
993 * a generalized version of iget_locked() for file systems where the inode
994 * number is not sufficient for unique identification of an inode.
995 *
996 * If the inode is not in cache, allocate a new inode and return it locked,
997 * hashed, and with the I_NEW flag set. The file system gets to fill it in
998 * before unlocking it via unlock_new_inode().
999 *
1000 * Note both @test and @set are called with the inode_hash_lock held, so can't
1001 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001003struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1004 int (*test)(struct inode *, void *),
1005 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001007 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301008 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001010 spin_lock(&inode_hash_lock);
1011 inode = find_inode(sb, head, test, data);
1012 spin_unlock(&inode_hash_lock);
1013
1014 if (inode) {
1015 wait_on_inode(inode);
1016 return inode;
1017 }
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 inode = alloc_inode(sb);
1020 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301021 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Dave Chinner67a23c42011-03-22 22:23:42 +11001023 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* We released the lock, so.. */
1025 old = find_inode(sb, head, test, data);
1026 if (!old) {
1027 if (set(inode, data))
1028 goto set_failed;
1029
Dave Chinner250df6e2011-03-22 22:23:36 +11001030 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001031 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001032 hlist_add_head(&inode->i_hash, head);
1033 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001034 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001035 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* Return the locked inode with I_NEW set, the
1038 * caller is responsible for filling in the contents
1039 */
1040 return inode;
1041 }
1042
1043 /*
1044 * Uhhuh, somebody else created the same inode under
1045 * us. Use the old inode instead of the one we just
1046 * allocated.
1047 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001048 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 destroy_inode(inode);
1050 inode = old;
1051 wait_on_inode(inode);
1052 }
1053 return inode;
1054
1055set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +11001056 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 destroy_inode(inode);
1058 return NULL;
1059}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001060EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001062/**
1063 * iget_locked - obtain an inode from a mounted file system
1064 * @sb: super block of file system
1065 * @ino: inode number to get
1066 *
1067 * Search for the inode specified by @ino in the inode cache and if present
1068 * return it with an increased reference count. This is for file systems
1069 * where the inode number is sufficient for unique identification of an inode.
1070 *
1071 * If the inode is not in cache, allocate a new inode and return it locked,
1072 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1073 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001075struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001077 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301078 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001080 spin_lock(&inode_hash_lock);
1081 inode = find_inode_fast(sb, head, ino);
1082 spin_unlock(&inode_hash_lock);
1083 if (inode) {
1084 wait_on_inode(inode);
1085 return inode;
1086 }
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 inode = alloc_inode(sb);
1089 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301090 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dave Chinner67a23c42011-03-22 22:23:42 +11001092 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* We released the lock, so.. */
1094 old = find_inode_fast(sb, head, ino);
1095 if (!old) {
1096 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001097 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001098 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001099 hlist_add_head(&inode->i_hash, head);
1100 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001101 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001102 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /* Return the locked inode with I_NEW set, the
1105 * caller is responsible for filling in the contents
1106 */
1107 return inode;
1108 }
1109
1110 /*
1111 * Uhhuh, somebody else created the same inode under
1112 * us. Use the old inode instead of the one we just
1113 * allocated.
1114 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001115 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 destroy_inode(inode);
1117 inode = old;
1118 wait_on_inode(inode);
1119 }
1120 return inode;
1121}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001122EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001124/*
1125 * search the inode cache for a matching inode number.
1126 * If we find one, then the inode number we are trying to
1127 * allocate is not unique and so we should not use it.
1128 *
1129 * Returns 1 if the inode number is unique, 0 if it is not.
1130 */
1131static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1132{
1133 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1134 struct hlist_node *node;
1135 struct inode *inode;
1136
Dave Chinner67a23c42011-03-22 22:23:42 +11001137 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001138 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001139 if (inode->i_ino == ino && inode->i_sb == sb) {
1140 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001141 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001142 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001143 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001144 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001145
1146 return 1;
1147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149/**
1150 * iunique - get a unique inode number
1151 * @sb: superblock
1152 * @max_reserved: highest reserved inode number
1153 *
1154 * Obtain an inode number that is unique on the system for a given
1155 * superblock. This is used by file systems that have no natural
1156 * permanent inode numbering system. An inode number is returned that
1157 * is higher than the reserved limit but unique.
1158 *
1159 * BUGS:
1160 * With a large number of inodes live on the file system this function
1161 * currently becomes quite slow.
1162 */
1163ino_t iunique(struct super_block *sb, ino_t max_reserved)
1164{
Jeff Layton866b04f2007-05-08 00:32:29 -07001165 /*
1166 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1167 * error if st_ino won't fit in target struct field. Use 32bit counter
1168 * here to attempt to avoid that.
1169 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001170 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001171 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001174 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001175 do {
1176 if (counter <= max_reserved)
1177 counter = max_reserved + 1;
1178 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001179 } while (!test_inode_iunique(sb, res));
1180 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001181
1182 return res;
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184EXPORT_SYMBOL(iunique);
1185
1186struct inode *igrab(struct inode *inode)
1187{
Dave Chinner250df6e2011-03-22 22:23:36 +11001188 spin_lock(&inode->i_lock);
1189 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001191 spin_unlock(&inode->i_lock);
1192 } else {
1193 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * Handle the case where s_op->clear_inode is not been
1196 * called yet, and somebody is calling igrab
1197 * while the inode is getting freed.
1198 */
1199 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return inode;
1202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203EXPORT_SYMBOL(igrab);
1204
1205/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001206 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 * @sb: super block of file system to search
1208 * @hashval: hash value (usually inode number) to search for
1209 * @test: callback used for comparisons between inodes
1210 * @data: opaque data pointer to pass to @test
1211 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001212 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001214 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001215 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001216 * Note: I_NEW is not waited upon so you have to be very careful what you do
1217 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001218 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001219 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001220 */
1221struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1222 int (*test)(struct inode *, void *), void *data)
1223{
1224 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001225 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001226
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001227 spin_lock(&inode_hash_lock);
1228 inode = find_inode(sb, head, test, data);
1229 spin_unlock(&inode_hash_lock);
1230
1231 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001232}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001233EXPORT_SYMBOL(ilookup5_nowait);
1234
1235/**
1236 * ilookup5 - search for an inode in the inode cache
1237 * @sb: super block of file system to search
1238 * @hashval: hash value (usually inode number) to search for
1239 * @test: callback used for comparisons between inodes
1240 * @data: opaque data pointer to pass to @test
1241 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001242 * Search for the inode specified by @hashval and @data in the inode cache,
1243 * and if the inode is in the cache, return the inode with an incremented
1244 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001245 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001247 * This is a generalized version of ilookup() for file systems where the
1248 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001250 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
1252struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1253 int (*test)(struct inode *, void *), void *data)
1254{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001255 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001257 if (inode)
1258 wait_on_inode(inode);
1259 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261EXPORT_SYMBOL(ilookup5);
1262
1263/**
1264 * ilookup - search for an inode in the inode cache
1265 * @sb: super block of file system to search
1266 * @ino: inode number to search for
1267 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001268 * Search for the inode @ino in the inode cache, and if the inode is in the
1269 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
1271struct inode *ilookup(struct super_block *sb, unsigned long ino)
1272{
1273 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001274 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001276 spin_lock(&inode_hash_lock);
1277 inode = find_inode_fast(sb, head, ino);
1278 spin_unlock(&inode_hash_lock);
1279
1280 if (inode)
1281 wait_on_inode(inode);
1282 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284EXPORT_SYMBOL(ilookup);
1285
Al Viro261bca82008-12-30 01:48:21 -05001286int insert_inode_locked(struct inode *inode)
1287{
1288 struct super_block *sb = inode->i_sb;
1289 ino_t ino = inode->i_ino;
1290 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001291
Al Viro261bca82008-12-30 01:48:21 -05001292 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001293 struct hlist_node *node;
1294 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001295 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001296 hlist_for_each_entry(old, node, head, i_hash) {
1297 if (old->i_ino != ino)
1298 continue;
1299 if (old->i_sb != sb)
1300 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001301 spin_lock(&old->i_lock);
1302 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1303 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001304 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001305 }
Al Viro72a43d62009-05-13 19:13:40 +01001306 break;
1307 }
1308 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001309 spin_lock(&inode->i_lock);
1310 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001311 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001312 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001313 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001314 return 0;
1315 }
1316 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001317 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001318 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001319 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001320 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001321 iput(old);
1322 return -EBUSY;
1323 }
1324 iput(old);
1325 }
1326}
Al Viro261bca82008-12-30 01:48:21 -05001327EXPORT_SYMBOL(insert_inode_locked);
1328
1329int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1330 int (*test)(struct inode *, void *), void *data)
1331{
1332 struct super_block *sb = inode->i_sb;
1333 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001334
Al Viro261bca82008-12-30 01:48:21 -05001335 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001336 struct hlist_node *node;
1337 struct inode *old = NULL;
1338
Dave Chinner67a23c42011-03-22 22:23:42 +11001339 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001340 hlist_for_each_entry(old, node, head, i_hash) {
1341 if (old->i_sb != sb)
1342 continue;
1343 if (!test(old, data))
1344 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001345 spin_lock(&old->i_lock);
1346 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1347 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001348 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001349 }
Al Viro72a43d62009-05-13 19:13:40 +01001350 break;
1351 }
1352 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001353 spin_lock(&inode->i_lock);
1354 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001355 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001356 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001357 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001358 return 0;
1359 }
1360 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001361 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001362 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001363 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001364 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001365 iput(old);
1366 return -EBUSY;
1367 }
1368 iput(old);
1369 }
1370}
Al Viro261bca82008-12-30 01:48:21 -05001371EXPORT_SYMBOL(insert_inode_locked4);
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Al Viro45321ac2010-06-07 13:43:19 -04001374int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Al Viro45321ac2010-06-07 13:43:19 -04001376 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378EXPORT_SYMBOL(generic_delete_inode);
1379
Al Viro45321ac2010-06-07 13:43:19 -04001380/*
1381 * Normal UNIX filesystem behaviour: delete the
1382 * inode when the usage count drops to zero, and
1383 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001384 */
Al Viro45321ac2010-06-07 13:43:19 -04001385int generic_drop_inode(struct inode *inode)
1386{
Al Viro1d3382cb2010-10-23 15:19:20 -04001387 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001388}
1389EXPORT_SYMBOL_GPL(generic_drop_inode);
1390
1391/*
1392 * Called when we're dropping the last reference
1393 * to an inode.
1394 *
1395 * Call the FS "drop_inode()" function, defaulting to
1396 * the legacy UNIX filesystem behaviour. If it tells
1397 * us to evict inode, do so. Otherwise, retain inode
1398 * in cache if fs is alive, sync and evict if fs is
1399 * shutting down.
1400 */
1401static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001404 const struct super_operations *op = inode->i_sb->s_op;
1405 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Dave Chinner250df6e2011-03-22 22:23:36 +11001407 WARN_ON(inode->i_state & I_NEW);
1408
Al Viroe7f59092011-07-07 15:45:59 -04001409 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001410 drop = op->drop_inode(inode);
1411 else
1412 drop = generic_drop_inode(inode);
1413
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001414 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1415 inode->i_state |= I_REFERENCED;
1416 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1417 inode_lru_list_add(inode);
1418 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001419 return;
1420 }
1421
Al Viro45321ac2010-06-07 13:43:19 -04001422 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001423 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001424 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001426 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001427 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001428 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001430
Alexander Viro991114c2005-06-23 00:09:01 -07001431 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001432 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001433 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001434
1435 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301439 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * @inode: inode to put
1441 *
1442 * Puts an inode, dropping its usage count. If the inode use count hits
1443 * zero, the inode is then freed and may also be destroyed.
1444 *
1445 * Consequently, iput() can sleep.
1446 */
1447void iput(struct inode *inode)
1448{
1449 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001450 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Dave Chinnerf283c862011-03-22 22:23:39 +11001452 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 iput_final(inode);
1454 }
1455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456EXPORT_SYMBOL(iput);
1457
1458/**
1459 * bmap - find a block number in a file
1460 * @inode: inode of file
1461 * @block: block to find
1462 *
1463 * Returns the block number on the device holding the inode that
1464 * is the disk block number for the block of the file requested.
1465 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301466 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * file.
1468 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301469sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 sector_t res = 0;
1472 if (inode->i_mapping->a_ops->bmap)
1473 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1474 return res;
1475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476EXPORT_SYMBOL(bmap);
1477
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001478/*
1479 * With relative atime, only update atime if the previous atime is
1480 * earlier than either the ctime or mtime or if at least a day has
1481 * passed since the last atime update.
1482 */
1483static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1484 struct timespec now)
1485{
1486
1487 if (!(mnt->mnt_flags & MNT_RELATIME))
1488 return 1;
1489 /*
1490 * Is mtime younger than atime? If yes, update atime:
1491 */
1492 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1493 return 1;
1494 /*
1495 * Is ctime younger than atime? If yes, update atime:
1496 */
1497 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1498 return 1;
1499
1500 /*
1501 * Is the previous atime value older than a day? If yes,
1502 * update atime:
1503 */
1504 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1505 return 1;
1506 /*
1507 * Good, we can skip the atime update:
1508 */
1509 return 0;
1510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001513 * touch_atime - update the access time
1514 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001515 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *
1517 * Update the accessed time on an inode and mark it for writeback.
1518 * This function automatically handles read only file systems and media,
1519 * as well as the "noatime" flag and inode specific "noatime" markers.
1520 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001521void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001523 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct timespec now;
1525
Andrew Mortonb2276132006-12-13 00:34:33 -08001526 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001527 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001528 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001529 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001530 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001531 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001532
Dave Hansencdb70f32008-02-15 14:37:41 -08001533 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001534 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001535 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001536 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001539
1540 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001541 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001542
Valerie Henson47ae32d2006-12-13 00:34:34 -08001543 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001544 return;
1545
1546 if (mnt_want_write(mnt))
1547 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001548
1549 inode->i_atime = now;
1550 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001551 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001553EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001556 * file_update_time - update mtime and ctime time
1557 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001559 * Update the mtime and ctime members of an inode and mark the inode
1560 * for writeback. Note that this function is meant exclusively for
1561 * usage in the file write path of filesystems, and filesystems may
1562 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001563 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001564 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
1566
Christoph Hellwig870f4812006-01-09 20:52:01 -08001567void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001569 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001571 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Andi Kleence06e0b2009-09-18 13:05:48 -07001573 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (IS_NOCMTIME(inode))
1575 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001576
Andi Kleence06e0b2009-09-18 13:05:48 -07001577 now = current_fs_time(inode->i_sb);
1578 if (!timespec_equal(&inode->i_mtime, &now))
1579 sync_it = S_MTIME;
1580
1581 if (!timespec_equal(&inode->i_ctime, &now))
1582 sync_it |= S_CTIME;
1583
1584 if (IS_I_VERSION(inode))
1585 sync_it |= S_VERSION;
1586
1587 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return;
1589
Andi Kleence06e0b2009-09-18 13:05:48 -07001590 /* Finally allowed to write? Takes lock. */
1591 if (mnt_want_write_file(file))
1592 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Andi Kleence06e0b2009-09-18 13:05:48 -07001594 /* Only change inode inside the lock region */
1595 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001596 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001597 if (sync_it & S_CTIME)
1598 inode->i_ctime = now;
1599 if (sync_it & S_MTIME)
1600 inode->i_mtime = now;
1601 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001602 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001604EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606int inode_needs_sync(struct inode *inode)
1607{
1608 if (IS_SYNC(inode))
1609 return 1;
1610 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1611 return 1;
1612 return 0;
1613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614EXPORT_SYMBOL(inode_needs_sync);
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616int inode_wait(void *word)
1617{
1618 schedule();
1619 return 0;
1620}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001621EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001624 * If we try to find an inode in the inode hash while it is being
1625 * deleted, we have to wait until the filesystem completes its
1626 * deletion before reporting that it isn't found. This function waits
1627 * until the deletion _might_ have completed. Callers are responsible
1628 * to recheck inode state.
1629 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001630 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001631 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1632 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 */
1634static void __wait_on_freeing_inode(struct inode *inode)
1635{
1636 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001637 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1638 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001640 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001641 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 schedule();
1643 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001644 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static __initdata unsigned long ihash_entries;
1648static int __init set_ihash_entries(char *str)
1649{
1650 if (!str)
1651 return 0;
1652 ihash_entries = simple_strtoul(str, &str, 0);
1653 return 1;
1654}
1655__setup("ihash_entries=", set_ihash_entries);
1656
1657/*
1658 * Initialize the waitqueues and inode hash table.
1659 */
1660void __init inode_init_early(void)
1661{
1662 int loop;
1663
1664 /* If hashes are distributed across NUMA nodes, defer
1665 * hash allocation until vmalloc space is available.
1666 */
1667 if (hashdist)
1668 return;
1669
1670 inode_hashtable =
1671 alloc_large_system_hash("Inode-cache",
1672 sizeof(struct hlist_head),
1673 ihash_entries,
1674 14,
1675 HASH_EARLY,
1676 &i_hash_shift,
1677 &i_hash_mask,
1678 0);
1679
1680 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1681 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1682}
1683
Denis Cheng74bf17c2007-10-16 23:26:30 -07001684void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 int loop;
1687
1688 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001689 inode_cachep = kmem_cache_create("inode_cache",
1690 sizeof(struct inode),
1691 0,
1692 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1693 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001694 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001695 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 /* Hash may have been set up in inode_init_early */
1698 if (!hashdist)
1699 return;
1700
1701 inode_hashtable =
1702 alloc_large_system_hash("Inode-cache",
1703 sizeof(struct hlist_head),
1704 ihash_entries,
1705 14,
1706 0,
1707 &i_hash_shift,
1708 &i_hash_mask,
1709 0);
1710
1711 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1712 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1713}
1714
1715void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1716{
1717 inode->i_mode = mode;
1718 if (S_ISCHR(mode)) {
1719 inode->i_fop = &def_chr_fops;
1720 inode->i_rdev = rdev;
1721 } else if (S_ISBLK(mode)) {
1722 inode->i_fop = &def_blk_fops;
1723 inode->i_rdev = rdev;
1724 } else if (S_ISFIFO(mode))
1725 inode->i_fop = &def_fifo_fops;
1726 else if (S_ISSOCK(mode))
1727 inode->i_fop = &bad_sock_fops;
1728 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001729 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1730 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1731 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001734
1735/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00001736 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001737 * @inode: New inode
1738 * @dir: Directory inode
1739 * @mode: mode of the new inode
1740 */
1741void inode_init_owner(struct inode *inode, const struct inode *dir,
1742 mode_t mode)
1743{
1744 inode->i_uid = current_fsuid();
1745 if (dir && dir->i_mode & S_ISGID) {
1746 inode->i_gid = dir->i_gid;
1747 if (S_ISDIR(mode))
1748 mode |= S_ISGID;
1749 } else
1750 inode->i_gid = current_fsgid();
1751 inode->i_mode = mode;
1752}
1753EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001754
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001755/**
1756 * inode_owner_or_capable - check current task permissions to inode
1757 * @inode: inode being checked
1758 *
1759 * Return true if current either has CAP_FOWNER to the inode, or
1760 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001761 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001762bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001763{
1764 struct user_namespace *ns = inode_userns(inode);
1765
1766 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1767 return true;
1768 if (ns_capable(ns, CAP_FOWNER))
1769 return true;
1770 return false;
1771}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001772EXPORT_SYMBOL(inode_owner_or_capable);