blob: 389f5a247599ee36ef633ca4f4afaa9efe5a5d65 [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>
Serge E. Hallyne795b712011-03-23 16:43:25 -070028#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
Dave Chinner250df6e2011-03-22 22:23:36 +110031 * inode locking rules.
32 *
33 * inode->i_lock protects:
34 * inode->i_state, inode->i_hash, __iget()
Dave Chinner02afc412011-03-22 22:23:38 +110035 * inode_lru_lock protects:
36 * inode_lru, inode->i_lru
Dave Chinner250df6e2011-03-22 22:23:36 +110037 *
38 * Lock ordering:
39 * inode_lock
40 * inode->i_lock
Dave Chinner02afc412011-03-22 22:23:38 +110041 * inode_lru_lock
Dave Chinner250df6e2011-03-22 22:23:36 +110042 */
43
44/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * This is needed for the following functions:
46 * - inode_has_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * - invalidate_bdev
48 *
49 * FIXME: remove all knowledge of the buffer layer from this file
50 */
51#include <linux/buffer_head.h>
52
53/*
54 * New inode.c implementation.
55 *
56 * This implementation has the basic premise of trying
57 * to be extremely low-overhead and SMP-safe, yet be
58 * simple enough to be "obviously correct".
59 *
60 * Famous last words.
61 */
62
63/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
64
65/* #define INODE_PARANOIA 1 */
66/* #define INODE_DEBUG 1 */
67
68/*
69 * Inode lookup is no longer as critical as it used to be:
70 * most of the lookups are going to be through the dcache.
71 */
72#define I_HASHBITS i_hash_shift
73#define I_HASHMASK i_hash_mask
74
Eric Dumazetfa3536c2006-03-26 01:37:24 -080075static unsigned int i_hash_mask __read_mostly;
76static unsigned int i_hash_shift __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * Each inode can be on two separate lists. One is
80 * the hash list of the inode, used for lookups. The
81 * other linked list is the "type" list:
82 * "in_use" - valid inode, i_count > 0, i_nlink > 0
83 * "dirty" - as "in_use" but also dirty
84 * "unused" - valid inode, i_count = 0
85 *
86 * A "dirty" list is maintained for each super block,
87 * allowing for low-overhead inode sync() operations.
88 */
89
Nick Piggin7ccf19a2010-10-21 11:49:30 +110090static LIST_HEAD(inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +110091static DEFINE_SPINLOCK(inode_lru_lock);
Eric Dumazetfa3536c2006-03-26 01:37:24 -080092static struct hlist_head *inode_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * A simple spinlock to protect the list manipulations.
96 *
97 * NOTE! You also have to own the lock if you change
98 * the i_state of an inode while it is in use..
99 */
100DEFINE_SPINLOCK(inode_lock);
101
102/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100103 * iprune_sem provides exclusion between the icache shrinking and the
104 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700105 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100106 * We don't actually need it to protect anything in the umount path,
107 * but only need to cycle through it to make sure any inode that
108 * prune_icache took off the LRU list has been fully torn down by the
109 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700111static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
114 * Statistics gathering..
115 */
116struct inodes_stat_t inodes_stat;
117
Nick Piggin3e880fb2011-01-07 17:49:19 +1100118static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400119
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530120static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Nick Piggin3e880fb2011-01-07 17:49:19 +1100122static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400123{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100124 int i;
125 int sum = 0;
126 for_each_possible_cpu(i)
127 sum += per_cpu(nr_inodes, i);
128 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400129}
130
131static inline int get_nr_inodes_unused(void)
132{
Nick Piggin86c87492011-01-07 17:49:18 +1100133 return inodes_stat.nr_unused;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400134}
135
136int get_nr_dirty_inodes(void)
137{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100138 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400139 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
140 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400141}
142
143/*
144 * Handle nr_inode sysctl
145 */
146#ifdef CONFIG_SYSCTL
147int proc_nr_inodes(ctl_table *table, int write,
148 void __user *buffer, size_t *lenp, loff_t *ppos)
149{
150 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400151 return proc_dointvec(table, write, buffer, lenp, ppos);
152}
153#endif
154
David Chinner2cb15992008-10-30 17:32:23 +1100155/**
156 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800157 * @sb: superblock inode belongs to
158 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100159 *
160 * These are initializations that need to be done on every inode
161 * allocation as the fields are not initialised by slab allocation.
162 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300163int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700165 static const struct address_space_operations empty_aops;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700166 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800167 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530168 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100169
170 inode->i_sb = sb;
171 inode->i_blkbits = sb->s_blocksize_bits;
172 inode->i_flags = 0;
173 atomic_set(&inode->i_count, 1);
174 inode->i_op = &empty_iops;
175 inode->i_fop = &empty_fops;
176 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500177 inode->i_uid = 0;
178 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100179 atomic_set(&inode->i_writecount, 0);
180 inode->i_size = 0;
181 inode->i_blocks = 0;
182 inode->i_bytes = 0;
183 inode->i_generation = 0;
184#ifdef CONFIG_QUOTA
185 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
186#endif
187 inode->i_pipe = NULL;
188 inode->i_bdev = NULL;
189 inode->i_cdev = NULL;
190 inode->i_rdev = 0;
191 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500192
193 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300194 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100195 spin_lock_init(&inode->i_lock);
196 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
197
198 mutex_init(&inode->i_mutex);
199 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
200
201 init_rwsem(&inode->i_alloc_sem);
202 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
203
204 mapping->a_ops = &empty_aops;
205 mapping->host = inode;
206 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800207 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100208 mapping->assoc_mapping = NULL;
209 mapping->backing_dev_info = &default_backing_dev_info;
210 mapping->writeback_index = 0;
211
212 /*
213 * If the block_device provides a backing_dev_info for client
214 * inodes then use that. Otherwise the inode share the bdev's
215 * backing_dev_info.
216 */
217 if (sb->s_bdev) {
218 struct backing_dev_info *bdi;
219
Jens Axboe2c96ce92009-09-15 09:43:56 +0200220 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100221 mapping->backing_dev_info = bdi;
222 }
223 inode->i_private = NULL;
224 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400225#ifdef CONFIG_FS_POSIX_ACL
226 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
227#endif
David Chinner2cb15992008-10-30 17:32:23 +1100228
Eric Paris3be25f42009-05-21 17:01:26 -0400229#ifdef CONFIG_FSNOTIFY
230 inode->i_fsnotify_mask = 0;
231#endif
232
Nick Piggin3e880fb2011-01-07 17:49:19 +1100233 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400234
Christoph Hellwig54e34622009-08-07 14:38:25 -0300235 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300236out:
237 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100238}
239EXPORT_SYMBOL(inode_init_always);
240
241static struct inode *alloc_inode(struct super_block *sb)
242{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct inode *inode;
244
245 if (sb->s_op->alloc_inode)
246 inode = sb->s_op->alloc_inode(sb);
247 else
David Chinner2cb15992008-10-30 17:32:23 +1100248 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Christoph Hellwig54e34622009-08-07 14:38:25 -0300250 if (!inode)
251 return NULL;
252
253 if (unlikely(inode_init_always(sb, inode))) {
254 if (inode->i_sb->s_op->destroy_inode)
255 inode->i_sb->s_op->destroy_inode(inode);
256 else
257 kmem_cache_free(inode_cachep, inode);
258 return NULL;
259 }
260
261 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Nick Pigginff0c7d12011-01-07 17:49:50 +1100264void free_inode_nonrcu(struct inode *inode)
265{
266 kmem_cache_free(inode_cachep, inode);
267}
268EXPORT_SYMBOL(free_inode_nonrcu);
269
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300270void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200272 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400274 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400275#ifdef CONFIG_FS_POSIX_ACL
276 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
277 posix_acl_release(inode->i_acl);
278 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
279 posix_acl_release(inode->i_default_acl);
280#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100281 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300282}
283EXPORT_SYMBOL(__destroy_inode);
284
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100285static void i_callback(struct rcu_head *head)
286{
287 struct inode *inode = container_of(head, struct inode, i_rcu);
288 INIT_LIST_HEAD(&inode->i_dentry);
289 kmem_cache_free(inode_cachep, inode);
290}
291
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200292static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300293{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100294 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300295 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (inode->i_sb->s_op->destroy_inode)
297 inode->i_sb->s_op->destroy_inode(inode);
298 else
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100299 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100302void address_space_init_once(struct address_space *mapping)
303{
304 memset(mapping, 0, sizeof(*mapping));
305 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
306 spin_lock_init(&mapping->tree_lock);
307 spin_lock_init(&mapping->i_mmap_lock);
308 INIT_LIST_HEAD(&mapping->private_list);
309 spin_lock_init(&mapping->private_lock);
310 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
311 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
312 mutex_init(&mapping->unmap_mutex);
313}
314EXPORT_SYMBOL(address_space_init_once);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
317 * These are initializations that only need to be done
318 * once, because the fields are idempotent across use
319 * of the inode, so let the slab aware of that.
320 */
321void inode_init_once(struct inode *inode)
322{
323 memset(inode, 0, sizeof(*inode));
324 INIT_HLIST_NODE(&inode->i_hash);
325 INIT_LIST_HEAD(&inode->i_dentry);
326 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100327 INIT_LIST_HEAD(&inode->i_wb_list);
328 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100329 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400331#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500332 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400333#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335EXPORT_SYMBOL(inode_init_once);
336
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700337static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530339 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Christoph Lametera35afb82007-05-16 22:10:57 -0700341 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100345 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530347void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Nick Piggin9e38d862010-10-23 06:55:17 -0400349 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Al Viro7de9c6e2010-10-23 11:11:40 -0400352/*
353 * get additional reference to inode; caller must already hold one.
354 */
355void ihold(struct inode *inode)
356{
357 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
358}
359EXPORT_SYMBOL(ihold);
360
Nick Piggin9e38d862010-10-23 06:55:17 -0400361static void inode_lru_list_add(struct inode *inode)
362{
Dave Chinner02afc412011-03-22 22:23:38 +1100363 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100364 if (list_empty(&inode->i_lru)) {
365 list_add(&inode->i_lru, &inode_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100366 inodes_stat.nr_unused++;
Nick Piggin9e38d862010-10-23 06:55:17 -0400367 }
Dave Chinner02afc412011-03-22 22:23:38 +1100368 spin_unlock(&inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400369}
370
371static void inode_lru_list_del(struct inode *inode)
372{
Dave Chinner02afc412011-03-22 22:23:38 +1100373 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100374 if (!list_empty(&inode->i_lru)) {
375 list_del_init(&inode->i_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100376 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400377 }
Dave Chinner02afc412011-03-22 22:23:38 +1100378 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Christoph Hellwig646ec462010-10-23 07:15:32 -0400381static inline void __inode_sb_list_add(struct inode *inode)
382{
383 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
384}
385
386/**
387 * inode_sb_list_add - add inode to the superblock list of inodes
388 * @inode: inode to add
389 */
390void inode_sb_list_add(struct inode *inode)
391{
392 spin_lock(&inode_lock);
393 __inode_sb_list_add(inode);
394 spin_unlock(&inode_lock);
395}
396EXPORT_SYMBOL_GPL(inode_sb_list_add);
397
398static inline void __inode_sb_list_del(struct inode *inode)
399{
400 list_del_init(&inode->i_sb_list);
401}
402
Dave Chinner4c51acb2010-10-23 06:58:09 -0400403static unsigned long hash(struct super_block *sb, unsigned long hashval)
404{
405 unsigned long tmp;
406
407 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
408 L1_CACHE_BYTES;
409 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
410 return tmp & I_HASHMASK;
411}
412
413/**
414 * __insert_inode_hash - hash an inode
415 * @inode: unhashed inode
416 * @hashval: unsigned long value used to locate this object in the
417 * inode_hashtable.
418 *
419 * Add an inode to the inode hash for this superblock.
420 */
421void __insert_inode_hash(struct inode *inode, unsigned long hashval)
422{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400423 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
424
Dave Chinner4c51acb2010-10-23 06:58:09 -0400425 spin_lock(&inode_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100426 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400427 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100428 spin_unlock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400429 spin_unlock(&inode_lock);
430}
431EXPORT_SYMBOL(__insert_inode_hash);
432
433/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400434 * remove_inode_hash - remove an inode from the hash
435 * @inode: inode to unhash
436 *
437 * Remove an inode from the superblock.
438 */
439void remove_inode_hash(struct inode *inode)
440{
441 spin_lock(&inode_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100442 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400443 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100444 spin_unlock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400445 spin_unlock(&inode_lock);
446}
447EXPORT_SYMBOL(remove_inode_hash);
448
Al Virob0683aa2010-06-04 20:55:25 -0400449void end_writeback(struct inode *inode)
450{
451 might_sleep();
452 BUG_ON(inode->i_data.nrpages);
453 BUG_ON(!list_empty(&inode->i_data.private_list));
454 BUG_ON(!(inode->i_state & I_FREEING));
455 BUG_ON(inode->i_state & I_CLEAR);
456 inode_sync_wait(inode);
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100457 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400458 inode->i_state = I_FREEING | I_CLEAR;
459}
460EXPORT_SYMBOL(end_writeback);
461
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100462/*
463 * Free the inode passed in, removing it from the lists it is still connected
464 * to. We remove any pages still attached to the inode and wait for any IO that
465 * is still in progress before finally destroying the inode.
466 *
467 * An inode must already be marked I_FREEING so that we avoid the inode being
468 * moved back onto lists if we race with other code that manipulates the lists
469 * (e.g. writeback_single_inode). The caller is responsible for setting this.
470 *
471 * An inode must already be removed from the LRU list before being evicted from
472 * the cache. This should occur atomically with setting the I_FREEING state
473 * flag, so no inodes here should ever be on the LRU when being evicted.
474 */
Al Viro644da592010-06-07 13:21:05 -0400475static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400476{
477 const struct super_operations *op = inode->i_sb->s_op;
478
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100479 BUG_ON(!(inode->i_state & I_FREEING));
480 BUG_ON(!list_empty(&inode->i_lru));
481
482 spin_lock(&inode_lock);
483 list_del_init(&inode->i_wb_list);
484 __inode_sb_list_del(inode);
485 spin_unlock(&inode_lock);
486
Al Virobe7ce412010-06-04 19:40:39 -0400487 if (op->evict_inode) {
488 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400489 } else {
490 if (inode->i_data.nrpages)
491 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400492 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400493 }
Al Viro661074e2010-06-04 20:19:55 -0400494 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
495 bd_forget(inode);
496 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
497 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100498
499 remove_inode_hash(inode);
500
501 spin_lock(&inode->i_lock);
502 wake_up_bit(&inode->i_state, __I_NEW);
503 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
504 spin_unlock(&inode->i_lock);
505
506 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
510 * dispose_list - dispose of the contents of a local list
511 * @head: the head of the list to free
512 *
513 * Dispose-list gets a local list with local inodes in it, so it doesn't
514 * need to worry about list corruption and SMP locks.
515 */
516static void dispose_list(struct list_head *head)
517{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 while (!list_empty(head)) {
519 struct inode *inode;
520
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100521 inode = list_first_entry(head, struct inode, i_lru);
522 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Al Viro644da592010-06-07 13:21:05 -0400524 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/**
Al Viro63997e92010-10-25 20:49:35 -0400529 * evict_inodes - evict all evictable inodes for a superblock
530 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *
Al Viro63997e92010-10-25 20:49:35 -0400532 * Make sure that no inodes with zero refcount are retained. This is
533 * called by superblock shutdown after having MS_ACTIVE flag removed,
534 * so any inode reaching zero refcount during or after that call will
535 * be immediately evicted.
536 */
537void evict_inodes(struct super_block *sb)
538{
539 struct inode *inode, *next;
540 LIST_HEAD(dispose);
541
Al Viro63997e92010-10-25 20:49:35 -0400542 spin_lock(&inode_lock);
543 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
544 if (atomic_read(&inode->i_count))
545 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100546
547 spin_lock(&inode->i_lock);
548 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
549 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400550 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100551 }
Al Viro63997e92010-10-25 20:49:35 -0400552
553 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100554 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100555 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100556 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400557 }
558 spin_unlock(&inode_lock);
559
560 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100561
562 /*
563 * Cycle through iprune_sem to make sure any inode that prune_icache
564 * moved off the list before we took the lock has been fully torn
565 * down.
566 */
567 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400568 up_write(&iprune_sem);
569}
570
571/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200572 * invalidate_inodes - attempt to free all inodes on a superblock
573 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100574 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200575 *
576 * Attempts to free all inodes for a given superblock. If there were any
577 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100578 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
579 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 */
NeilBrown93b270f2011-02-24 17:25:47 +1100581int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400583 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200584 struct inode *inode, *next;
585 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_lock(&inode_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200588 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100589 spin_lock(&inode->i_lock);
590 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
591 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100593 }
NeilBrown93b270f2011-02-24 17:25:47 +1100594 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100595 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100596 busy = 1;
597 continue;
598 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200599 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100600 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200601 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 continue;
603 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200604
Christoph Hellwig99a38912010-10-23 19:07:20 +0200605 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100606 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100607 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100608 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 spin_unlock(&inode_lock);
611
Christoph Hellwiga0318782010-10-24 19:40:33 +0200612 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 return busy;
615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617static int can_unuse(struct inode *inode)
618{
Nick Piggin9e38d862010-10-23 06:55:17 -0400619 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return 0;
621 if (inode_has_buffers(inode))
622 return 0;
623 if (atomic_read(&inode->i_count))
624 return 0;
625 if (inode->i_data.nrpages)
626 return 0;
627 return 1;
628}
629
630/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400631 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100632 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 *
634 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400635 * pagecache removed. If the inode has metadata buffers attached to
636 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400638 * If the inode has the I_REFERENCED flag set, then it means that it has been
639 * used recently - the flag is set in iput_final(). When we encounter such an
640 * inode, clear the flag and move it to the back of the LRU so it gets another
641 * pass through the LRU before it gets reclaimed. This is necessary because of
642 * the fact we are doing lazy LRU updates to minimise lock contention so the
643 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
644 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
646static void prune_icache(int nr_to_scan)
647{
648 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int nr_scanned;
650 unsigned long reap = 0;
651
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700652 down_read(&iprune_sem);
Dave Chinner02afc412011-03-22 22:23:38 +1100653 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
655 struct inode *inode;
656
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100657 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100660 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Nick Piggin9e38d862010-10-23 06:55:17 -0400662 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100663 * we are inverting the inode_lru_lock/inode->i_lock here,
664 * so use a trylock. If we fail to get the lock, just move the
665 * inode to the back of the list so we don't spin on it.
666 */
667 if (!spin_trylock(&inode->i_lock)) {
668 list_move(&inode->i_lru, &inode_lru);
669 continue;
670 }
671
672 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400673 * Referenced or dirty inodes are still in use. Give them
674 * another pass through the LRU as we canot reclaim them now.
675 */
676 if (atomic_read(&inode->i_count) ||
677 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100678 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100679 spin_unlock(&inode->i_lock);
Nick Piggin86c87492011-01-07 17:49:18 +1100680 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400681 continue;
682 }
683
684 /* recently referenced inodes get one more pass */
685 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400686 inode->i_state &= ~I_REFERENCED;
Dave Chinner250df6e2011-03-22 22:23:36 +1100687 list_move(&inode->i_lru, &inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100688 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 continue;
690 }
691 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
692 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100693 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100694 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800696 reap += invalidate_mapping_pages(&inode->i_data,
697 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100699 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100701 if (inode != list_entry(inode_lru.next,
702 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100704 /* avoid lock inversions with trylock */
705 if (!spin_trylock(&inode->i_lock))
706 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100707 if (!can_unuse(inode)) {
708 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700712 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100714 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100715
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100716 list_move(&inode->i_lru, &freeable);
Nick Piggin86c87492011-01-07 17:49:18 +1100717 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700719 if (current_is_kswapd())
720 __count_vm_events(KSWAPD_INODESTEAL, reap);
721 else
722 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100723 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700726 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/*
730 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
731 * "unused" means that no dentries are referring to the inodes: the files are
732 * not open and the dcache references to those inodes have already been
733 * reclaimed.
734 *
735 * This function is passed the number of inodes to scan, and it returns the
736 * total number of remaining possibly-reclaimable inodes.
737 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000738static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 if (nr) {
741 /*
742 * Nasty deadlock avoidance. We may hold various FS locks,
743 * and we don't want to recurse into the FS that called us
744 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!(gfp_mask & __GFP_FS))
747 return -1;
748 prune_icache(nr);
749 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400750 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Rusty Russell8e1f9362007-07-17 04:03:17 -0700753static struct shrinker icache_shrinker = {
754 .shrink = shrink_icache_memory,
755 .seeks = DEFAULT_SEEKS,
756};
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758static void __wait_on_freeing_inode(struct inode *inode);
759/*
760 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530762static struct inode *find_inode(struct super_block *sb,
763 struct hlist_head *head,
764 int (*test)(struct inode *, void *),
765 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530768 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700771 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (inode->i_sb != sb)
773 continue;
774 if (!test(inode, data))
775 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100776 spin_lock(&inode->i_lock);
Al Viroa4ffdde2010-06-02 17:38:30 -0400777 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 __wait_on_freeing_inode(inode);
779 goto repeat;
780 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400781 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100782 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400783 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400785 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788/*
789 * find_inode_fast is the fast path version of find_inode, see the comment at
790 * iget_locked for details.
791 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530792static struct inode *find_inode_fast(struct super_block *sb,
793 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530796 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700799 hlist_for_each_entry(inode, node, head, i_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (inode->i_ino != ino)
801 continue;
802 if (inode->i_sb != sb)
803 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100804 spin_lock(&inode->i_lock);
Al Viroa4ffdde2010-06-02 17:38:30 -0400805 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 __wait_on_freeing_inode(inode);
807 goto repeat;
808 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400809 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100810 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400811 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400813 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Eric Dumazetf991bd22010-10-23 11:18:01 -0400816/*
817 * Each cpu owns a range of LAST_INO_BATCH numbers.
818 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
819 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100820 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400821 * This does not significantly increase overflow rate because every CPU can
822 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
823 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
824 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
825 * overflow rate by 2x, which does not seem too significant.
826 *
827 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
828 * error if st_ino won't fit in target struct field. Use 32bit counter
829 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100830 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400831#define LAST_INO_BATCH 1024
832static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100833
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400834unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400835{
836 unsigned int *p = &get_cpu_var(last_ino);
837 unsigned int res = *p;
838
839#ifdef CONFIG_SMP
840 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
841 static atomic_t shared_last_ino;
842 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
843
844 res = next - LAST_INO_BATCH;
845 }
846#endif
847
848 *p = ++res;
849 put_cpu_var(last_ino);
850 return res;
David Chinner8290c352008-10-30 17:35:24 +1100851}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400852EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/**
855 * new_inode - obtain an inode
856 * @sb: superblock
857 *
Mel Gorman769848c2007-07-17 04:03:05 -0700858 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800859 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700860 * If HIGHMEM pages are unsuitable or it is known that pages allocated
861 * for the page cache are not reclaimable or migratable,
862 * mapping_set_gfp_mask() must be called with suitable flags on the
863 * newly created inode's mapping
864 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
866struct inode *new_inode(struct super_block *sb)
867{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530868 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 spin_lock_prefetch(&inode_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 inode = alloc_inode(sb);
873 if (inode) {
874 spin_lock(&inode_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100875 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100877 spin_unlock(&inode->i_lock);
878 __inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 spin_unlock(&inode_lock);
880 }
881 return inode;
882}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883EXPORT_SYMBOL(new_inode);
884
Dave Chinner250df6e2011-03-22 22:23:36 +1100885/**
886 * unlock_new_inode - clear the I_NEW state and wake up any waiters
887 * @inode: new inode to unlock
888 *
889 * Called when the inode is fully initialised to clear the new state of the
890 * inode and wake up anyone waiting for the inode to finish initialisation.
891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892void unlock_new_inode(struct inode *inode)
893{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200894#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900895 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200896 struct file_system_type *type = inode->i_sb->s_type;
897
Jan Kara9a7aa122009-06-04 15:26:49 +0200898 /* Set new key only if filesystem hasn't already changed it */
899 if (!lockdep_match_class(&inode->i_mutex,
900 &type->i_mutex_key)) {
901 /*
902 * ensure nobody is actually holding i_mutex
903 */
904 mutex_destroy(&inode->i_mutex);
905 mutex_init(&inode->i_mutex);
906 lockdep_set_class(&inode->i_mutex,
907 &type->i_mutex_dir_key);
908 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200909 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200910#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100911 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100912 WARN_ON(!(inode->i_state & I_NEW));
913 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100914 wake_up_bit(&inode->i_state, __I_NEW);
915 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917EXPORT_SYMBOL(unlock_new_inode);
918
919/*
920 * This is called without the inode lock held.. Be careful.
921 *
922 * We no longer cache the sb_flags in i_flags - see fs.h
923 * -- rmk@arm.uk.linux.org
924 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530925static struct inode *get_new_inode(struct super_block *sb,
926 struct hlist_head *head,
927 int (*test)(struct inode *, void *),
928 int (*set)(struct inode *, void *),
929 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530931 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 inode = alloc_inode(sb);
934 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530935 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 spin_lock(&inode_lock);
938 /* We released the lock, so.. */
939 old = find_inode(sb, head, test, data);
940 if (!old) {
941 if (set(inode, data))
942 goto set_failed;
943
Dave Chinner250df6e2011-03-22 22:23:36 +1100944 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100945 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100946 hlist_add_head(&inode->i_hash, head);
947 spin_unlock(&inode->i_lock);
948 __inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 spin_unlock(&inode_lock);
950
951 /* Return the locked inode with I_NEW set, the
952 * caller is responsible for filling in the contents
953 */
954 return inode;
955 }
956
957 /*
958 * Uhhuh, somebody else created the same inode under
959 * us. Use the old inode instead of the one we just
960 * allocated.
961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 spin_unlock(&inode_lock);
963 destroy_inode(inode);
964 inode = old;
965 wait_on_inode(inode);
966 }
967 return inode;
968
969set_failed:
970 spin_unlock(&inode_lock);
971 destroy_inode(inode);
972 return NULL;
973}
974
975/*
976 * get_new_inode_fast is the fast path version of get_new_inode, see the
977 * comment at iget_locked for details.
978 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530979static struct inode *get_new_inode_fast(struct super_block *sb,
980 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530982 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 inode = alloc_inode(sb);
985 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530986 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 spin_lock(&inode_lock);
989 /* We released the lock, so.. */
990 old = find_inode_fast(sb, head, ino);
991 if (!old) {
992 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +1100993 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100994 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100995 hlist_add_head(&inode->i_hash, head);
996 spin_unlock(&inode->i_lock);
997 __inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 spin_unlock(&inode_lock);
999
1000 /* Return the locked inode with I_NEW set, the
1001 * caller is responsible for filling in the contents
1002 */
1003 return inode;
1004 }
1005
1006 /*
1007 * Uhhuh, somebody else created the same inode under
1008 * us. Use the old inode instead of the one we just
1009 * allocated.
1010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 spin_unlock(&inode_lock);
1012 destroy_inode(inode);
1013 inode = old;
1014 wait_on_inode(inode);
1015 }
1016 return inode;
1017}
1018
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001019/*
1020 * search the inode cache for a matching inode number.
1021 * If we find one, then the inode number we are trying to
1022 * allocate is not unique and so we should not use it.
1023 *
1024 * Returns 1 if the inode number is unique, 0 if it is not.
1025 */
1026static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1027{
1028 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1029 struct hlist_node *node;
1030 struct inode *inode;
1031
1032 hlist_for_each_entry(inode, node, b, i_hash) {
1033 if (inode->i_ino == ino && inode->i_sb == sb)
1034 return 0;
1035 }
1036
1037 return 1;
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/**
1041 * iunique - get a unique inode number
1042 * @sb: superblock
1043 * @max_reserved: highest reserved inode number
1044 *
1045 * Obtain an inode number that is unique on the system for a given
1046 * superblock. This is used by file systems that have no natural
1047 * permanent inode numbering system. An inode number is returned that
1048 * is higher than the reserved limit but unique.
1049 *
1050 * BUGS:
1051 * With a large number of inodes live on the file system this function
1052 * currently becomes quite slow.
1053 */
1054ino_t iunique(struct super_block *sb, ino_t max_reserved)
1055{
Jeff Layton866b04f2007-05-08 00:32:29 -07001056 /*
1057 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1058 * error if st_ino won't fit in target struct field. Use 32bit counter
1059 * here to attempt to avoid that.
1060 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001061 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001062 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001065 spin_lock(&inode_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001066 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001067 do {
1068 if (counter <= max_reserved)
1069 counter = max_reserved + 1;
1070 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001071 } while (!test_inode_iunique(sb, res));
1072 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001073 spin_unlock(&inode_lock);
1074
1075 return res;
1076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077EXPORT_SYMBOL(iunique);
1078
1079struct inode *igrab(struct inode *inode)
1080{
Dave Chinner250df6e2011-03-22 22:23:36 +11001081 spin_lock(&inode->i_lock);
1082 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001084 spin_unlock(&inode->i_lock);
1085 } else {
1086 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /*
1088 * Handle the case where s_op->clear_inode is not been
1089 * called yet, and somebody is calling igrab
1090 * while the inode is getting freed.
1091 */
1092 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return inode;
1095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096EXPORT_SYMBOL(igrab);
1097
1098/**
1099 * ifind - internal function, you want ilookup5() or iget5().
1100 * @sb: super block of file system to search
1101 * @head: the head of the list to search
1102 * @test: callback used for comparisons between inodes
1103 * @data: opaque data pointer to pass to @test
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001104 * @wait: if true wait for the inode to be unlocked, if false do not
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *
1106 * ifind() searches for the inode specified by @data in the inode
1107 * cache. This is a generalized version of ifind_fast() for file systems where
1108 * the inode number is not sufficient for unique identification of an inode.
1109 *
1110 * If the inode is in the cache, the inode is returned with an incremented
1111 * reference count.
1112 *
1113 * Otherwise NULL is returned.
1114 *
1115 * Note, @test is called with the inode_lock held, so can't sleep.
1116 */
Matt Mackall5d2bea42006-01-08 01:05:21 -08001117static struct inode *ifind(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct hlist_head *head, int (*test)(struct inode *, void *),
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001119 void *data, const int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 struct inode *inode;
1122
1123 spin_lock(&inode_lock);
1124 inode = find_inode(sb, head, test, data);
1125 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 spin_unlock(&inode_lock);
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001127 if (likely(wait))
1128 wait_on_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return inode;
1130 }
1131 spin_unlock(&inode_lock);
1132 return NULL;
1133}
1134
1135/**
1136 * ifind_fast - internal function, you want ilookup() or iget().
1137 * @sb: super block of file system to search
1138 * @head: head of the list to search
1139 * @ino: inode number to search for
1140 *
1141 * ifind_fast() searches for the inode @ino in the inode cache. This is for
1142 * file systems where the inode number is sufficient for unique identification
1143 * of an inode.
1144 *
1145 * If the inode is in the cache, the inode is returned with an incremented
1146 * reference count.
1147 *
1148 * Otherwise NULL is returned.
1149 */
Matt Mackall5d2bea42006-01-08 01:05:21 -08001150static struct inode *ifind_fast(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 struct hlist_head *head, unsigned long ino)
1152{
1153 struct inode *inode;
1154
1155 spin_lock(&inode_lock);
1156 inode = find_inode_fast(sb, head, ino);
1157 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_unlock(&inode_lock);
1159 wait_on_inode(inode);
1160 return inode;
1161 }
1162 spin_unlock(&inode_lock);
1163 return NULL;
1164}
1165
1166/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001167 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 * @sb: super block of file system to search
1169 * @hashval: hash value (usually inode number) to search for
1170 * @test: callback used for comparisons between inodes
1171 * @data: opaque data pointer to pass to @test
1172 *
1173 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1174 * @data in the inode cache. This is a generalized version of ilookup() for
1175 * file systems where the inode number is not sufficient for unique
1176 * identification of an inode.
1177 *
1178 * If the inode is in the cache, the inode is returned with an incremented
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001179 * reference count. Note, the inode lock is not waited upon so you have to be
1180 * very careful what you do with the returned inode. You probably should be
1181 * using ilookup5() instead.
1182 *
1183 * Otherwise NULL is returned.
1184 *
1185 * Note, @test is called with the inode_lock held, so can't sleep.
1186 */
1187struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1188 int (*test)(struct inode *, void *), void *data)
1189{
1190 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1191
1192 return ifind(sb, head, test, data, 0);
1193}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001194EXPORT_SYMBOL(ilookup5_nowait);
1195
1196/**
1197 * ilookup5 - search for an inode in the inode cache
1198 * @sb: super block of file system to search
1199 * @hashval: hash value (usually inode number) to search for
1200 * @test: callback used for comparisons between inodes
1201 * @data: opaque data pointer to pass to @test
1202 *
1203 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1204 * @data in the inode cache. This is a generalized version of ilookup() for
1205 * file systems where the inode number is not sufficient for unique
1206 * identification of an inode.
1207 *
1208 * If the inode is in the cache, the inode lock is waited upon and the inode is
1209 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 *
1211 * Otherwise NULL is returned.
1212 *
1213 * Note, @test is called with the inode_lock held, so can't sleep.
1214 */
1215struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1216 int (*test)(struct inode *, void *), void *data)
1217{
1218 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1219
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001220 return ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222EXPORT_SYMBOL(ilookup5);
1223
1224/**
1225 * ilookup - search for an inode in the inode cache
1226 * @sb: super block of file system to search
1227 * @ino: inode number to search for
1228 *
1229 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
1230 * This is for file systems where the inode number is sufficient for unique
1231 * identification of an inode.
1232 *
1233 * If the inode is in the cache, the inode is returned with an incremented
1234 * reference count.
1235 *
1236 * Otherwise NULL is returned.
1237 */
1238struct inode *ilookup(struct super_block *sb, unsigned long ino)
1239{
1240 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1241
1242 return ifind_fast(sb, head, ino);
1243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244EXPORT_SYMBOL(ilookup);
1245
1246/**
1247 * iget5_locked - obtain an inode from a mounted file system
1248 * @sb: super block of file system
1249 * @hashval: hash value (usually inode number) to get
1250 * @test: callback used for comparisons between inodes
1251 * @set: callback used to initialize a new struct inode
1252 * @data: opaque data pointer to pass to @test and @set
1253 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * iget5_locked() uses ifind() to search for the inode specified by @hashval
1255 * and @data in the inode cache and if present it is returned with an increased
1256 * reference count. This is a generalized version of iget_locked() for file
1257 * systems where the inode number is not sufficient for unique identification
1258 * of an inode.
1259 *
1260 * If the inode is not in cache, get_new_inode() is called to allocate a new
1261 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1262 * file system gets to fill it in before unlocking it via unlock_new_inode().
1263 *
1264 * Note both @test and @set are called with the inode_lock held, so can't sleep.
1265 */
1266struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1267 int (*test)(struct inode *, void *),
1268 int (*set)(struct inode *, void *), void *data)
1269{
1270 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1271 struct inode *inode;
1272
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001273 inode = ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (inode)
1275 return inode;
1276 /*
1277 * get_new_inode() will do the right thing, re-trying the search
1278 * in case it had to block at any point.
1279 */
1280 return get_new_inode(sb, head, test, set, data);
1281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282EXPORT_SYMBOL(iget5_locked);
1283
1284/**
1285 * iget_locked - obtain an inode from a mounted file system
1286 * @sb: super block of file system
1287 * @ino: inode number to get
1288 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1290 * the inode cache and if present it is returned with an increased reference
1291 * count. This is for file systems where the inode number is sufficient for
1292 * unique identification of an inode.
1293 *
1294 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1295 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1296 * The file system gets to fill it in before unlocking it via
1297 * unlock_new_inode().
1298 */
1299struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1300{
1301 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1302 struct inode *inode;
1303
1304 inode = ifind_fast(sb, head, ino);
1305 if (inode)
1306 return inode;
1307 /*
1308 * get_new_inode_fast() will do the right thing, re-trying the search
1309 * in case it had to block at any point.
1310 */
1311 return get_new_inode_fast(sb, head, ino);
1312}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313EXPORT_SYMBOL(iget_locked);
1314
Al Viro261bca82008-12-30 01:48:21 -05001315int insert_inode_locked(struct inode *inode)
1316{
1317 struct super_block *sb = inode->i_sb;
1318 ino_t ino = inode->i_ino;
1319 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001320
Al Viro261bca82008-12-30 01:48:21 -05001321 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001322 struct hlist_node *node;
1323 struct inode *old = NULL;
Al Viro261bca82008-12-30 01:48:21 -05001324 spin_lock(&inode_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001325 hlist_for_each_entry(old, node, head, i_hash) {
1326 if (old->i_ino != ino)
1327 continue;
1328 if (old->i_sb != sb)
1329 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001330 spin_lock(&old->i_lock);
1331 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1332 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001333 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001334 }
Al Viro72a43d62009-05-13 19:13:40 +01001335 break;
1336 }
1337 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001338 spin_lock(&inode->i_lock);
1339 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001340 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001341 spin_unlock(&inode->i_lock);
Al Viro261bca82008-12-30 01:48:21 -05001342 spin_unlock(&inode_lock);
1343 return 0;
1344 }
1345 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001346 spin_unlock(&old->i_lock);
Al Viro261bca82008-12-30 01:48:21 -05001347 spin_unlock(&inode_lock);
1348 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001349 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001350 iput(old);
1351 return -EBUSY;
1352 }
1353 iput(old);
1354 }
1355}
Al Viro261bca82008-12-30 01:48:21 -05001356EXPORT_SYMBOL(insert_inode_locked);
1357
1358int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1359 int (*test)(struct inode *, void *), void *data)
1360{
1361 struct super_block *sb = inode->i_sb;
1362 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001363
Al Viro261bca82008-12-30 01:48:21 -05001364 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001365 struct hlist_node *node;
1366 struct inode *old = NULL;
1367
Al Viro261bca82008-12-30 01:48:21 -05001368 spin_lock(&inode_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001369 hlist_for_each_entry(old, node, head, i_hash) {
1370 if (old->i_sb != sb)
1371 continue;
1372 if (!test(old, data))
1373 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001374 spin_lock(&old->i_lock);
1375 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1376 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001377 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001378 }
Al Viro72a43d62009-05-13 19:13:40 +01001379 break;
1380 }
1381 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001382 spin_lock(&inode->i_lock);
1383 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001384 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001385 spin_unlock(&inode->i_lock);
Al Viro261bca82008-12-30 01:48:21 -05001386 spin_unlock(&inode_lock);
1387 return 0;
1388 }
1389 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001390 spin_unlock(&old->i_lock);
Al Viro261bca82008-12-30 01:48:21 -05001391 spin_unlock(&inode_lock);
1392 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001393 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001394 iput(old);
1395 return -EBUSY;
1396 }
1397 iput(old);
1398 }
1399}
Al Viro261bca82008-12-30 01:48:21 -05001400EXPORT_SYMBOL(insert_inode_locked4);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Al Viro45321ac2010-06-07 13:43:19 -04001403int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Al Viro45321ac2010-06-07 13:43:19 -04001405 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407EXPORT_SYMBOL(generic_delete_inode);
1408
Al Viro45321ac2010-06-07 13:43:19 -04001409/*
1410 * Normal UNIX filesystem behaviour: delete the
1411 * inode when the usage count drops to zero, and
1412 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001413 */
Al Viro45321ac2010-06-07 13:43:19 -04001414int generic_drop_inode(struct inode *inode)
1415{
Al Viro1d3382c2010-10-23 15:19:20 -04001416 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001417}
1418EXPORT_SYMBOL_GPL(generic_drop_inode);
1419
1420/*
1421 * Called when we're dropping the last reference
1422 * to an inode.
1423 *
1424 * Call the FS "drop_inode()" function, defaulting to
1425 * the legacy UNIX filesystem behaviour. If it tells
1426 * us to evict inode, do so. Otherwise, retain inode
1427 * in cache if fs is alive, sync and evict if fs is
1428 * shutting down.
1429 */
1430static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001433 const struct super_operations *op = inode->i_sb->s_op;
1434 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Dave Chinner250df6e2011-03-22 22:23:36 +11001436 WARN_ON(inode->i_state & I_NEW);
1437
Al Viro45321ac2010-06-07 13:43:19 -04001438 if (op && op->drop_inode)
1439 drop = op->drop_inode(inode);
1440 else
1441 drop = generic_drop_inode(inode);
1442
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001443 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1444 inode->i_state |= I_REFERENCED;
1445 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1446 inode_lru_list_add(inode);
1447 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001448 return;
1449 }
1450
Al Viro45321ac2010-06-07 13:43:19 -04001451 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001452 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001453 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001455 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001456 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001457 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001459
Alexander Viro991114c2005-06-23 00:09:01 -07001460 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001461 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001462 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001463
1464 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301468 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 * @inode: inode to put
1470 *
1471 * Puts an inode, dropping its usage count. If the inode use count hits
1472 * zero, the inode is then freed and may also be destroyed.
1473 *
1474 * Consequently, iput() can sleep.
1475 */
1476void iput(struct inode *inode)
1477{
1478 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001479 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Dave Chinnerf283c862011-03-22 22:23:39 +11001481 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 iput_final(inode);
1483 }
1484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485EXPORT_SYMBOL(iput);
1486
1487/**
1488 * bmap - find a block number in a file
1489 * @inode: inode of file
1490 * @block: block to find
1491 *
1492 * Returns the block number on the device holding the inode that
1493 * is the disk block number for the block of the file requested.
1494 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301495 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * file.
1497 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301498sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 sector_t res = 0;
1501 if (inode->i_mapping->a_ops->bmap)
1502 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1503 return res;
1504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505EXPORT_SYMBOL(bmap);
1506
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001507/*
1508 * With relative atime, only update atime if the previous atime is
1509 * earlier than either the ctime or mtime or if at least a day has
1510 * passed since the last atime update.
1511 */
1512static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1513 struct timespec now)
1514{
1515
1516 if (!(mnt->mnt_flags & MNT_RELATIME))
1517 return 1;
1518 /*
1519 * Is mtime younger than atime? If yes, update atime:
1520 */
1521 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1522 return 1;
1523 /*
1524 * Is ctime younger than atime? If yes, update atime:
1525 */
1526 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1527 return 1;
1528
1529 /*
1530 * Is the previous atime value older than a day? If yes,
1531 * update atime:
1532 */
1533 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1534 return 1;
1535 /*
1536 * Good, we can skip the atime update:
1537 */
1538 return 0;
1539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001542 * touch_atime - update the access time
1543 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001544 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 *
1546 * Update the accessed time on an inode and mark it for writeback.
1547 * This function automatically handles read only file systems and media,
1548 * as well as the "noatime" flag and inode specific "noatime" markers.
1549 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001550void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001552 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 struct timespec now;
1554
Andrew Mortonb2276132006-12-13 00:34:33 -08001555 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001556 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001557 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001558 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001559 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001560 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001561
Dave Hansencdb70f32008-02-15 14:37:41 -08001562 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001563 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001564 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001565 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001568
1569 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001570 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001571
Valerie Henson47ae32d2006-12-13 00:34:34 -08001572 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001573 return;
1574
1575 if (mnt_want_write(mnt))
1576 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001577
1578 inode->i_atime = now;
1579 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001580 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001582EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001585 * file_update_time - update mtime and ctime time
1586 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001588 * Update the mtime and ctime members of an inode and mark the inode
1589 * for writeback. Note that this function is meant exclusively for
1590 * usage in the file write path of filesystems, and filesystems may
1591 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001592 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001593 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
1595
Christoph Hellwig870f4812006-01-09 20:52:01 -08001596void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001598 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001600 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Andi Kleence06e0b2009-09-18 13:05:48 -07001602 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (IS_NOCMTIME(inode))
1604 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001605
Andi Kleence06e0b2009-09-18 13:05:48 -07001606 now = current_fs_time(inode->i_sb);
1607 if (!timespec_equal(&inode->i_mtime, &now))
1608 sync_it = S_MTIME;
1609
1610 if (!timespec_equal(&inode->i_ctime, &now))
1611 sync_it |= S_CTIME;
1612
1613 if (IS_I_VERSION(inode))
1614 sync_it |= S_VERSION;
1615
1616 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return;
1618
Andi Kleence06e0b2009-09-18 13:05:48 -07001619 /* Finally allowed to write? Takes lock. */
1620 if (mnt_want_write_file(file))
1621 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Andi Kleence06e0b2009-09-18 13:05:48 -07001623 /* Only change inode inside the lock region */
1624 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001625 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001626 if (sync_it & S_CTIME)
1627 inode->i_ctime = now;
1628 if (sync_it & S_MTIME)
1629 inode->i_mtime = now;
1630 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001631 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001633EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635int inode_needs_sync(struct inode *inode)
1636{
1637 if (IS_SYNC(inode))
1638 return 1;
1639 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1640 return 1;
1641 return 0;
1642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643EXPORT_SYMBOL(inode_needs_sync);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645int inode_wait(void *word)
1646{
1647 schedule();
1648 return 0;
1649}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001650EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001653 * If we try to find an inode in the inode hash while it is being
1654 * deleted, we have to wait until the filesystem completes its
1655 * deletion before reporting that it isn't found. This function waits
1656 * until the deletion _might_ have completed. Callers are responsible
1657 * to recheck inode state.
1658 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001659 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001660 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1661 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 */
1663static void __wait_on_freeing_inode(struct inode *inode)
1664{
1665 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001666 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1667 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001669 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 spin_unlock(&inode_lock);
1671 schedule();
1672 finish_wait(wq, &wait.wait);
1673 spin_lock(&inode_lock);
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static __initdata unsigned long ihash_entries;
1677static int __init set_ihash_entries(char *str)
1678{
1679 if (!str)
1680 return 0;
1681 ihash_entries = simple_strtoul(str, &str, 0);
1682 return 1;
1683}
1684__setup("ihash_entries=", set_ihash_entries);
1685
1686/*
1687 * Initialize the waitqueues and inode hash table.
1688 */
1689void __init inode_init_early(void)
1690{
1691 int loop;
1692
1693 /* If hashes are distributed across NUMA nodes, defer
1694 * hash allocation until vmalloc space is available.
1695 */
1696 if (hashdist)
1697 return;
1698
1699 inode_hashtable =
1700 alloc_large_system_hash("Inode-cache",
1701 sizeof(struct hlist_head),
1702 ihash_entries,
1703 14,
1704 HASH_EARLY,
1705 &i_hash_shift,
1706 &i_hash_mask,
1707 0);
1708
1709 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1710 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1711}
1712
Denis Cheng74bf17c2007-10-16 23:26:30 -07001713void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 int loop;
1716
1717 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001718 inode_cachep = kmem_cache_create("inode_cache",
1719 sizeof(struct inode),
1720 0,
1721 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1722 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001723 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001724 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* Hash may have been set up in inode_init_early */
1727 if (!hashdist)
1728 return;
1729
1730 inode_hashtable =
1731 alloc_large_system_hash("Inode-cache",
1732 sizeof(struct hlist_head),
1733 ihash_entries,
1734 14,
1735 0,
1736 &i_hash_shift,
1737 &i_hash_mask,
1738 0);
1739
1740 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1741 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1742}
1743
1744void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1745{
1746 inode->i_mode = mode;
1747 if (S_ISCHR(mode)) {
1748 inode->i_fop = &def_chr_fops;
1749 inode->i_rdev = rdev;
1750 } else if (S_ISBLK(mode)) {
1751 inode->i_fop = &def_blk_fops;
1752 inode->i_rdev = rdev;
1753 } else if (S_ISFIFO(mode))
1754 inode->i_fop = &def_fifo_fops;
1755 else if (S_ISSOCK(mode))
1756 inode->i_fop = &bad_sock_fops;
1757 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001758 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1759 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1760 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001763
1764/**
Ben Hutchingseaae6682011-02-15 12:48:09 +00001765 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001766 * @inode: New inode
1767 * @dir: Directory inode
1768 * @mode: mode of the new inode
1769 */
1770void inode_init_owner(struct inode *inode, const struct inode *dir,
1771 mode_t mode)
1772{
1773 inode->i_uid = current_fsuid();
1774 if (dir && dir->i_mode & S_ISGID) {
1775 inode->i_gid = dir->i_gid;
1776 if (S_ISDIR(mode))
1777 mode |= S_ISGID;
1778 } else
1779 inode->i_gid = current_fsgid();
1780 inode->i_mode = mode;
1781}
1782EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001783
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001784/**
1785 * inode_owner_or_capable - check current task permissions to inode
1786 * @inode: inode being checked
1787 *
1788 * Return true if current either has CAP_FOWNER to the inode, or
1789 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001790 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001791bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001792{
1793 struct user_namespace *ns = inode_userns(inode);
1794
1795 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1796 return true;
1797 if (ns_capable(ns, CAP_FOWNER))
1798 return true;
1799 return false;
1800}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001801EXPORT_SYMBOL(inode_owner_or_capable);