blob: f9ee4928358fb059663a7e22f18f6d9e01b1eb79 [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>
Dave Chinnera66979a2011-03-22 22:23:41 +110029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Dave Chinner250df6e2011-03-22 22:23:36 +110032 * inode locking rules.
33 *
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:
37 * 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
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * This is needed for the following functions:
64 * - inode_has_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * - invalidate_bdev
66 *
67 * FIXME: remove all knowledge of the buffer layer from this file
68 */
69#include <linux/buffer_head.h>
70
71/*
72 * New inode.c implementation.
73 *
74 * This implementation has the basic premise of trying
75 * to be extremely low-overhead and SMP-safe, yet be
76 * simple enough to be "obviously correct".
77 *
78 * Famous last words.
79 */
80
81/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
82
83/* #define INODE_PARANOIA 1 */
84/* #define INODE_DEBUG 1 */
85
86/*
87 * Inode lookup is no longer as critical as it used to be:
88 * most of the lookups are going to be through the dcache.
89 */
90#define I_HASHBITS i_hash_shift
91#define I_HASHMASK i_hash_mask
92
Eric Dumazetfa3536c2006-03-26 01:37:24 -080093static unsigned int i_hash_mask __read_mostly;
94static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110095static struct hlist_head *inode_hashtable __read_mostly;
96static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * Each inode can be on two separate lists. One is
100 * the hash list of the inode, used for lookups. The
101 * other linked list is the "type" list:
102 * "in_use" - valid inode, i_count > 0, i_nlink > 0
103 * "dirty" - as "in_use" but also dirty
104 * "unused" - valid inode, i_count = 0
105 *
106 * A "dirty" list is maintained for each super block,
107 * allowing for low-overhead inode sync() operations.
108 */
109
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100110static LIST_HEAD(inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +1100111static DEFINE_SPINLOCK(inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Dave Chinner55fa6092011-03-22 22:23:40 +1100113__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
Dave Chinnera66979a2011-03-22 22:23:41 +1100114__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100117 * iprune_sem provides exclusion between the icache shrinking and the
118 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700119 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100120 * We don't actually need it to protect anything in the umount path,
121 * but only need to cycle through it to make sure any inode that
122 * prune_icache took off the LRU list has been fully torn down by the
123 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700125static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/*
128 * Statistics gathering..
129 */
130struct inodes_stat_t inodes_stat;
131
Nick Piggin3e880fb2011-01-07 17:49:19 +1100132static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400133
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530134static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Nick Piggin3e880fb2011-01-07 17:49:19 +1100136static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400137{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100138 int i;
139 int sum = 0;
140 for_each_possible_cpu(i)
141 sum += per_cpu(nr_inodes, i);
142 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400143}
144
145static inline int get_nr_inodes_unused(void)
146{
Nick Piggin86c87492011-01-07 17:49:18 +1100147 return inodes_stat.nr_unused;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400148}
149
150int get_nr_dirty_inodes(void)
151{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100152 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400153 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
154 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400155}
156
157/*
158 * Handle nr_inode sysctl
159 */
160#ifdef CONFIG_SYSCTL
161int proc_nr_inodes(ctl_table *table, int write,
162 void __user *buffer, size_t *lenp, loff_t *ppos)
163{
164 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400165 return proc_dointvec(table, write, buffer, lenp, ppos);
166}
167#endif
168
David Chinner2cb15992008-10-30 17:32:23 +1100169/**
170 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800171 * @sb: superblock inode belongs to
172 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100173 *
174 * These are initializations that need to be done on every inode
175 * allocation as the fields are not initialised by slab allocation.
176 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300177int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700179 static const struct address_space_operations empty_aops;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700180 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800181 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530182 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100183
184 inode->i_sb = sb;
185 inode->i_blkbits = sb->s_blocksize_bits;
186 inode->i_flags = 0;
187 atomic_set(&inode->i_count, 1);
188 inode->i_op = &empty_iops;
189 inode->i_fop = &empty_fops;
190 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500191 inode->i_uid = 0;
192 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100193 atomic_set(&inode->i_writecount, 0);
194 inode->i_size = 0;
195 inode->i_blocks = 0;
196 inode->i_bytes = 0;
197 inode->i_generation = 0;
198#ifdef CONFIG_QUOTA
199 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
200#endif
201 inode->i_pipe = NULL;
202 inode->i_bdev = NULL;
203 inode->i_cdev = NULL;
204 inode->i_rdev = 0;
205 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500206
207 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300208 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100209 spin_lock_init(&inode->i_lock);
210 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
211
212 mutex_init(&inode->i_mutex);
213 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
214
215 init_rwsem(&inode->i_alloc_sem);
216 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
217
218 mapping->a_ops = &empty_aops;
219 mapping->host = inode;
220 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800221 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100222 mapping->assoc_mapping = NULL;
223 mapping->backing_dev_info = &default_backing_dev_info;
224 mapping->writeback_index = 0;
225
226 /*
227 * If the block_device provides a backing_dev_info for client
228 * inodes then use that. Otherwise the inode share the bdev's
229 * backing_dev_info.
230 */
231 if (sb->s_bdev) {
232 struct backing_dev_info *bdi;
233
Jens Axboe2c96ce92009-09-15 09:43:56 +0200234 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100235 mapping->backing_dev_info = bdi;
236 }
237 inode->i_private = NULL;
238 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400239#ifdef CONFIG_FS_POSIX_ACL
240 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
241#endif
David Chinner2cb15992008-10-30 17:32:23 +1100242
Eric Paris3be25f42009-05-21 17:01:26 -0400243#ifdef CONFIG_FSNOTIFY
244 inode->i_fsnotify_mask = 0;
245#endif
246
Nick Piggin3e880fb2011-01-07 17:49:19 +1100247 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400248
Christoph Hellwig54e34622009-08-07 14:38:25 -0300249 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300250out:
251 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100252}
253EXPORT_SYMBOL(inode_init_always);
254
255static struct inode *alloc_inode(struct super_block *sb)
256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct inode *inode;
258
259 if (sb->s_op->alloc_inode)
260 inode = sb->s_op->alloc_inode(sb);
261 else
David Chinner2cb15992008-10-30 17:32:23 +1100262 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Christoph Hellwig54e34622009-08-07 14:38:25 -0300264 if (!inode)
265 return NULL;
266
267 if (unlikely(inode_init_always(sb, inode))) {
268 if (inode->i_sb->s_op->destroy_inode)
269 inode->i_sb->s_op->destroy_inode(inode);
270 else
271 kmem_cache_free(inode_cachep, inode);
272 return NULL;
273 }
274
275 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Nick Pigginff0c7d12011-01-07 17:49:50 +1100278void free_inode_nonrcu(struct inode *inode)
279{
280 kmem_cache_free(inode_cachep, inode);
281}
282EXPORT_SYMBOL(free_inode_nonrcu);
283
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300284void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200286 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400288 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400289#ifdef CONFIG_FS_POSIX_ACL
290 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
291 posix_acl_release(inode->i_acl);
292 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
293 posix_acl_release(inode->i_default_acl);
294#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100295 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300296}
297EXPORT_SYMBOL(__destroy_inode);
298
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100299static void i_callback(struct rcu_head *head)
300{
301 struct inode *inode = container_of(head, struct inode, i_rcu);
302 INIT_LIST_HEAD(&inode->i_dentry);
303 kmem_cache_free(inode_cachep, inode);
304}
305
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200306static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300307{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100308 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300309 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (inode->i_sb->s_op->destroy_inode)
311 inode->i_sb->s_op->destroy_inode(inode);
312 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100313 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100316void address_space_init_once(struct address_space *mapping)
317{
318 memset(mapping, 0, sizeof(*mapping));
319 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
320 spin_lock_init(&mapping->tree_lock);
321 spin_lock_init(&mapping->i_mmap_lock);
322 INIT_LIST_HEAD(&mapping->private_list);
323 spin_lock_init(&mapping->private_lock);
324 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
325 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
326 mutex_init(&mapping->unmap_mutex);
327}
328EXPORT_SYMBOL(address_space_init_once);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*
331 * These are initializations that only need to be done
332 * once, because the fields are idempotent across use
333 * of the inode, so let the slab aware of that.
334 */
335void inode_init_once(struct inode *inode)
336{
337 memset(inode, 0, sizeof(*inode));
338 INIT_HLIST_NODE(&inode->i_hash);
339 INIT_LIST_HEAD(&inode->i_dentry);
340 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100341 INIT_LIST_HEAD(&inode->i_wb_list);
342 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100343 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400345#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500346 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349EXPORT_SYMBOL(inode_init_once);
350
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700351static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530353 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Christoph Lametera35afb82007-05-16 22:10:57 -0700355 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100359 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530361void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Nick Piggin9e38d862010-10-23 06:55:17 -0400363 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Al Viro7de9c6ee2010-10-23 11:11:40 -0400366/*
367 * get additional reference to inode; caller must already hold one.
368 */
369void ihold(struct inode *inode)
370{
371 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
372}
373EXPORT_SYMBOL(ihold);
374
Nick Piggin9e38d862010-10-23 06:55:17 -0400375static void inode_lru_list_add(struct inode *inode)
376{
Dave Chinner02afc412011-03-22 22:23:38 +1100377 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100378 if (list_empty(&inode->i_lru)) {
379 list_add(&inode->i_lru, &inode_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100380 inodes_stat.nr_unused++;
Nick Piggin9e38d862010-10-23 06:55:17 -0400381 }
Dave Chinner02afc412011-03-22 22:23:38 +1100382 spin_unlock(&inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400383}
384
385static void inode_lru_list_del(struct inode *inode)
386{
Dave Chinner02afc412011-03-22 22:23:38 +1100387 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100388 if (!list_empty(&inode->i_lru)) {
389 list_del_init(&inode->i_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100390 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400391 }
Dave Chinner02afc412011-03-22 22:23:38 +1100392 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Christoph Hellwig646ec462010-10-23 07:15:32 -0400395/**
396 * inode_sb_list_add - add inode to the superblock list of inodes
397 * @inode: inode to add
398 */
399void inode_sb_list_add(struct inode *inode)
400{
Dave Chinner55fa6092011-03-22 22:23:40 +1100401 spin_lock(&inode_sb_list_lock);
402 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
403 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400404}
405EXPORT_SYMBOL_GPL(inode_sb_list_add);
406
Dave Chinner55fa6092011-03-22 22:23:40 +1100407static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400408{
Dave Chinner55fa6092011-03-22 22:23:40 +1100409 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400410 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100411 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400412}
413
Dave Chinner4c51acb2010-10-23 06:58:09 -0400414static unsigned long hash(struct super_block *sb, unsigned long hashval)
415{
416 unsigned long tmp;
417
418 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
419 L1_CACHE_BYTES;
420 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
421 return tmp & I_HASHMASK;
422}
423
424/**
425 * __insert_inode_hash - hash an inode
426 * @inode: unhashed inode
427 * @hashval: unsigned long value used to locate this object in the
428 * inode_hashtable.
429 *
430 * Add an inode to the inode hash for this superblock.
431 */
432void __insert_inode_hash(struct inode *inode, unsigned long hashval)
433{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400434 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
435
Dave Chinner67a23c42011-03-22 22:23:42 +1100436 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100437 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400438 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100439 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100440 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400441}
442EXPORT_SYMBOL(__insert_inode_hash);
443
444/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400445 * remove_inode_hash - remove an inode from the hash
446 * @inode: inode to unhash
447 *
448 * Remove an inode from the superblock.
449 */
450void remove_inode_hash(struct inode *inode)
451{
Dave Chinner67a23c42011-03-22 22:23:42 +1100452 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100453 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400454 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100455 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100456 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400457}
458EXPORT_SYMBOL(remove_inode_hash);
459
Al Virob0683aa2010-06-04 20:55:25 -0400460void end_writeback(struct inode *inode)
461{
462 might_sleep();
463 BUG_ON(inode->i_data.nrpages);
464 BUG_ON(!list_empty(&inode->i_data.private_list));
465 BUG_ON(!(inode->i_state & I_FREEING));
466 BUG_ON(inode->i_state & I_CLEAR);
467 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100468 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400469 inode->i_state = I_FREEING | I_CLEAR;
470}
471EXPORT_SYMBOL(end_writeback);
472
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100473/*
474 * Free the inode passed in, removing it from the lists it is still connected
475 * to. We remove any pages still attached to the inode and wait for any IO that
476 * is still in progress before finally destroying the inode.
477 *
478 * An inode must already be marked I_FREEING so that we avoid the inode being
479 * moved back onto lists if we race with other code that manipulates the lists
480 * (e.g. writeback_single_inode). The caller is responsible for setting this.
481 *
482 * An inode must already be removed from the LRU list before being evicted from
483 * the cache. This should occur atomically with setting the I_FREEING state
484 * flag, so no inodes here should ever be on the LRU when being evicted.
485 */
Al Viro644da592010-06-07 13:21:05 -0400486static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400487{
488 const struct super_operations *op = inode->i_sb->s_op;
489
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100490 BUG_ON(!(inode->i_state & I_FREEING));
491 BUG_ON(!list_empty(&inode->i_lru));
492
Dave Chinnera66979a2011-03-22 22:23:41 +1100493 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100494 inode_sb_list_del(inode);
495
Al Virobe7ce412010-06-04 19:40:39 -0400496 if (op->evict_inode) {
497 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400498 } else {
499 if (inode->i_data.nrpages)
500 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400501 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400502 }
Al Viro661074e2010-06-04 20:19:55 -0400503 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
504 bd_forget(inode);
505 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
506 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100507
508 remove_inode_hash(inode);
509
510 spin_lock(&inode->i_lock);
511 wake_up_bit(&inode->i_state, __I_NEW);
512 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
513 spin_unlock(&inode->i_lock);
514
515 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/*
519 * dispose_list - dispose of the contents of a local list
520 * @head: the head of the list to free
521 *
522 * Dispose-list gets a local list with local inodes in it, so it doesn't
523 * need to worry about list corruption and SMP locks.
524 */
525static void dispose_list(struct list_head *head)
526{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 while (!list_empty(head)) {
528 struct inode *inode;
529
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100530 inode = list_first_entry(head, struct inode, i_lru);
531 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Al Viro644da592010-06-07 13:21:05 -0400533 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/**
Al Viro63997e92010-10-25 20:49:35 -0400538 * evict_inodes - evict all evictable inodes for a superblock
539 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *
Al Viro63997e92010-10-25 20:49:35 -0400541 * Make sure that no inodes with zero refcount are retained. This is
542 * called by superblock shutdown after having MS_ACTIVE flag removed,
543 * so any inode reaching zero refcount during or after that call will
544 * be immediately evicted.
545 */
546void evict_inodes(struct super_block *sb)
547{
548 struct inode *inode, *next;
549 LIST_HEAD(dispose);
550
Dave Chinner55fa6092011-03-22 22:23:40 +1100551 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400552 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
553 if (atomic_read(&inode->i_count))
554 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100555
556 spin_lock(&inode->i_lock);
557 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
558 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400559 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100560 }
Al Viro63997e92010-10-25 20:49:35 -0400561
562 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100563 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100564 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100565 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400566 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100567 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400568
569 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100570
571 /*
572 * Cycle through iprune_sem to make sure any inode that prune_icache
573 * moved off the list before we took the lock has been fully torn
574 * down.
575 */
576 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400577 up_write(&iprune_sem);
578}
579
580/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200581 * invalidate_inodes - attempt to free all inodes on a superblock
582 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100583 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200584 *
585 * Attempts to free all inodes for a given superblock. If there were any
586 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100587 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
588 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
NeilBrown93b270f2011-02-24 17:25:47 +1100590int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400592 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200593 struct inode *inode, *next;
594 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dave Chinner55fa6092011-03-22 22:23:40 +1100596 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200597 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100598 spin_lock(&inode->i_lock);
599 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
600 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100602 }
NeilBrown93b270f2011-02-24 17:25:47 +1100603 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100604 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100605 busy = 1;
606 continue;
607 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200608 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100609 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200610 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 continue;
612 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200613
Christoph Hellwig99a38912010-10-23 19:07:20 +0200614 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100615 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100616 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100617 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100619 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Christoph Hellwiga0318782010-10-24 19:40:33 +0200621 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 return busy;
624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626static int can_unuse(struct inode *inode)
627{
Nick Piggin9e38d862010-10-23 06:55:17 -0400628 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630 if (inode_has_buffers(inode))
631 return 0;
632 if (atomic_read(&inode->i_count))
633 return 0;
634 if (inode->i_data.nrpages)
635 return 0;
636 return 1;
637}
638
639/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400640 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100641 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400644 * pagecache removed. If the inode has metadata buffers attached to
645 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400647 * If the inode has the I_REFERENCED flag set, then it means that it has been
648 * used recently - the flag is set in iput_final(). When we encounter such an
649 * inode, clear the flag and move it to the back of the LRU so it gets another
650 * pass through the LRU before it gets reclaimed. This is necessary because of
651 * the fact we are doing lazy LRU updates to minimise lock contention so the
652 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
653 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
655static void prune_icache(int nr_to_scan)
656{
657 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int nr_scanned;
659 unsigned long reap = 0;
660
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700661 down_read(&iprune_sem);
Dave Chinner02afc412011-03-22 22:23:38 +1100662 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
664 struct inode *inode;
665
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100666 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 break;
668
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100669 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Nick Piggin9e38d862010-10-23 06:55:17 -0400671 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100672 * we are inverting the inode_lru_lock/inode->i_lock here,
673 * so use a trylock. If we fail to get the lock, just move the
674 * inode to the back of the list so we don't spin on it.
675 */
676 if (!spin_trylock(&inode->i_lock)) {
677 list_move(&inode->i_lru, &inode_lru);
678 continue;
679 }
680
681 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400682 * Referenced or dirty inodes are still in use. Give them
683 * another pass through the LRU as we canot reclaim them now.
684 */
685 if (atomic_read(&inode->i_count) ||
686 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100687 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100688 spin_unlock(&inode->i_lock);
Nick Piggin86c87492011-01-07 17:49:18 +1100689 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400690 continue;
691 }
692
693 /* recently referenced inodes get one more pass */
694 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400695 inode->i_state &= ~I_REFERENCED;
Dave Chinner250df6e2011-03-22 22:23:36 +1100696 list_move(&inode->i_lru, &inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100697 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 continue;
699 }
700 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
701 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100702 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100703 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800705 reap += invalidate_mapping_pages(&inode->i_data,
706 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100708 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100710 if (inode != list_entry(inode_lru.next,
711 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100713 /* avoid lock inversions with trylock */
714 if (!spin_trylock(&inode->i_lock))
715 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100716 if (!can_unuse(inode)) {
717 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700721 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100723 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100724
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100725 list_move(&inode->i_lru, &freeable);
Nick Piggin86c87492011-01-07 17:49:18 +1100726 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700728 if (current_is_kswapd())
729 __count_vm_events(KSWAPD_INODESTEAL, reap);
730 else
731 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100732 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700735 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738/*
739 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
740 * "unused" means that no dentries are referring to the inodes: the files are
741 * not open and the dcache references to those inodes have already been
742 * reclaimed.
743 *
744 * This function is passed the number of inodes to scan, and it returns the
745 * total number of remaining possibly-reclaimable inodes.
746 */
Dave Chinner7f8275d2010-07-19 14:56:17 +1000747static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 if (nr) {
750 /*
751 * Nasty deadlock avoidance. We may hold various FS locks,
752 * and we don't want to recurse into the FS that called us
753 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!(gfp_mask & __GFP_FS))
756 return -1;
757 prune_icache(nr);
758 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400759 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Rusty Russell8e1f9362007-07-17 04:03:17 -0700762static struct shrinker icache_shrinker = {
763 .shrink = shrink_icache_memory,
764 .seeks = DEFAULT_SEEKS,
765};
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static void __wait_on_freeing_inode(struct inode *inode);
768/*
769 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530771static struct inode *find_inode(struct super_block *sb,
772 struct hlist_head *head,
773 int (*test)(struct inode *, void *),
774 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530777 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700780 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100781 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100782 if (inode->i_sb != sb) {
783 spin_unlock(&inode->i_lock);
784 continue;
785 }
786 if (!test(inode, data)) {
787 spin_unlock(&inode->i_lock);
788 continue;
789 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400790 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 __wait_on_freeing_inode(inode);
792 goto repeat;
793 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400794 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100795 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400796 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400798 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801/*
802 * find_inode_fast is the fast path version of find_inode, see the comment at
803 * iget_locked for details.
804 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530805static struct inode *find_inode_fast(struct super_block *sb,
806 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530809 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700812 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100813 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100814 if (inode->i_ino != ino) {
815 spin_unlock(&inode->i_lock);
816 continue;
817 }
818 if (inode->i_sb != sb) {
819 spin_unlock(&inode->i_lock);
820 continue;
821 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400822 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 __wait_on_freeing_inode(inode);
824 goto repeat;
825 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400826 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100827 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400828 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400830 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Eric Dumazetf991bd22010-10-23 11:18:01 -0400833/*
834 * Each cpu owns a range of LAST_INO_BATCH numbers.
835 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
836 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100837 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400838 * This does not significantly increase overflow rate because every CPU can
839 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
840 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
841 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
842 * overflow rate by 2x, which does not seem too significant.
843 *
844 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
845 * error if st_ino won't fit in target struct field. Use 32bit counter
846 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100847 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400848#define LAST_INO_BATCH 1024
849static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100850
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400851unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400852{
853 unsigned int *p = &get_cpu_var(last_ino);
854 unsigned int res = *p;
855
856#ifdef CONFIG_SMP
857 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
858 static atomic_t shared_last_ino;
859 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
860
861 res = next - LAST_INO_BATCH;
862 }
863#endif
864
865 *p = ++res;
866 put_cpu_var(last_ino);
867 return res;
David Chinner8290c352008-10-30 17:35:24 +1100868}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400869EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/**
872 * new_inode - obtain an inode
873 * @sb: superblock
874 *
Mel Gorman769848c2007-07-17 04:03:05 -0700875 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800876 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700877 * If HIGHMEM pages are unsuitable or it is known that pages allocated
878 * for the page cache are not reclaimable or migratable,
879 * mapping_set_gfp_mask() must be called with suitable flags on the
880 * newly created inode's mapping
881 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 */
883struct inode *new_inode(struct super_block *sb)
884{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530885 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Dave Chinner55fa6092011-03-22 22:23:40 +1100887 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 inode = alloc_inode(sb);
890 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100891 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100893 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100894 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 return inode;
897}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898EXPORT_SYMBOL(new_inode);
899
Dave Chinner250df6e2011-03-22 22:23:36 +1100900/**
901 * unlock_new_inode - clear the I_NEW state and wake up any waiters
902 * @inode: new inode to unlock
903 *
904 * Called when the inode is fully initialised to clear the new state of the
905 * inode and wake up anyone waiting for the inode to finish initialisation.
906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907void unlock_new_inode(struct inode *inode)
908{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200909#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900910 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200911 struct file_system_type *type = inode->i_sb->s_type;
912
Jan Kara9a7aa122009-06-04 15:26:49 +0200913 /* Set new key only if filesystem hasn't already changed it */
914 if (!lockdep_match_class(&inode->i_mutex,
915 &type->i_mutex_key)) {
916 /*
917 * ensure nobody is actually holding i_mutex
918 */
919 mutex_destroy(&inode->i_mutex);
920 mutex_init(&inode->i_mutex);
921 lockdep_set_class(&inode->i_mutex,
922 &type->i_mutex_dir_key);
923 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200924 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200925#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100926 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100927 WARN_ON(!(inode->i_state & I_NEW));
928 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100929 wake_up_bit(&inode->i_state, __I_NEW);
930 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932EXPORT_SYMBOL(unlock_new_inode);
933
934/*
Dave Chinner67a23c42011-03-22 22:23:42 +1100935 * This is called without the inode hash lock held.. Be careful.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
937 * We no longer cache the sb_flags in i_flags - see fs.h
938 * -- rmk@arm.uk.linux.org
939 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530940static struct inode *get_new_inode(struct super_block *sb,
941 struct hlist_head *head,
942 int (*test)(struct inode *, void *),
943 int (*set)(struct inode *, void *),
944 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530946 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 inode = alloc_inode(sb);
949 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530950 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Dave Chinner67a23c42011-03-22 22:23:42 +1100952 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /* We released the lock, so.. */
954 old = find_inode(sb, head, test, data);
955 if (!old) {
956 if (set(inode, data))
957 goto set_failed;
958
Dave Chinner250df6e2011-03-22 22:23:36 +1100959 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100960 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100961 hlist_add_head(&inode->i_hash, head);
962 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100963 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100964 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /* Return the locked inode with I_NEW set, the
967 * caller is responsible for filling in the contents
968 */
969 return inode;
970 }
971
972 /*
973 * Uhhuh, somebody else created the same inode under
974 * us. Use the old inode instead of the one we just
975 * allocated.
976 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100977 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 destroy_inode(inode);
979 inode = old;
980 wait_on_inode(inode);
981 }
982 return inode;
983
984set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100985 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 destroy_inode(inode);
987 return NULL;
988}
989
990/*
991 * get_new_inode_fast is the fast path version of get_new_inode, see the
992 * comment at iget_locked for details.
993 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530994static struct inode *get_new_inode_fast(struct super_block *sb,
995 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530997 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 inode = alloc_inode(sb);
1000 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301001 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dave Chinner67a23c42011-03-22 22:23:42 +11001003 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* We released the lock, so.. */
1005 old = find_inode_fast(sb, head, ino);
1006 if (!old) {
1007 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001008 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001009 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001010 hlist_add_head(&inode->i_hash, head);
1011 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001012 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001013 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* Return the locked inode with I_NEW set, the
1016 * caller is responsible for filling in the contents
1017 */
1018 return inode;
1019 }
1020
1021 /*
1022 * Uhhuh, somebody else created the same inode under
1023 * us. Use the old inode instead of the one we just
1024 * allocated.
1025 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001026 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 destroy_inode(inode);
1028 inode = old;
1029 wait_on_inode(inode);
1030 }
1031 return inode;
1032}
1033
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001034/*
1035 * search the inode cache for a matching inode number.
1036 * If we find one, then the inode number we are trying to
1037 * allocate is not unique and so we should not use it.
1038 *
1039 * Returns 1 if the inode number is unique, 0 if it is not.
1040 */
1041static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1042{
1043 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1044 struct hlist_node *node;
1045 struct inode *inode;
1046
Dave Chinner67a23c42011-03-22 22:23:42 +11001047 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001048 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001049 if (inode->i_ino == ino && inode->i_sb == sb) {
1050 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001051 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001052 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001053 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001054 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001055
1056 return 1;
1057}
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059/**
1060 * iunique - get a unique inode number
1061 * @sb: superblock
1062 * @max_reserved: highest reserved inode number
1063 *
1064 * Obtain an inode number that is unique on the system for a given
1065 * superblock. This is used by file systems that have no natural
1066 * permanent inode numbering system. An inode number is returned that
1067 * is higher than the reserved limit but unique.
1068 *
1069 * BUGS:
1070 * With a large number of inodes live on the file system this function
1071 * currently becomes quite slow.
1072 */
1073ino_t iunique(struct super_block *sb, ino_t max_reserved)
1074{
Jeff Layton866b04f2007-05-08 00:32:29 -07001075 /*
1076 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1077 * error if st_ino won't fit in target struct field. Use 32bit counter
1078 * here to attempt to avoid that.
1079 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001080 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001081 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001084 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001085 do {
1086 if (counter <= max_reserved)
1087 counter = max_reserved + 1;
1088 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001089 } while (!test_inode_iunique(sb, res));
1090 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001091
1092 return res;
1093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094EXPORT_SYMBOL(iunique);
1095
1096struct inode *igrab(struct inode *inode)
1097{
Dave Chinner250df6e2011-03-22 22:23:36 +11001098 spin_lock(&inode->i_lock);
1099 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001101 spin_unlock(&inode->i_lock);
1102 } else {
1103 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * Handle the case where s_op->clear_inode is not been
1106 * called yet, and somebody is calling igrab
1107 * while the inode is getting freed.
1108 */
1109 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return inode;
1112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113EXPORT_SYMBOL(igrab);
1114
1115/**
1116 * ifind - internal function, you want ilookup5() or iget5().
1117 * @sb: super block of file system to search
1118 * @head: the head of the list to search
1119 * @test: callback used for comparisons between inodes
1120 * @data: opaque data pointer to pass to @test
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001121 * @wait: if true wait for the inode to be unlocked, if false do not
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *
1123 * ifind() searches for the inode specified by @data in the inode
1124 * cache. This is a generalized version of ifind_fast() for file systems where
1125 * the inode number is not sufficient for unique identification of an inode.
1126 *
1127 * If the inode is in the cache, the inode is returned with an incremented
1128 * reference count.
1129 *
1130 * Otherwise NULL is returned.
1131 *
Dave Chinner67a23c42011-03-22 22:23:42 +11001132 * Note, @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Matt Mackall5d2bea42006-01-08 01:05:21 -08001134static struct inode *ifind(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 struct hlist_head *head, int (*test)(struct inode *, void *),
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001136 void *data, const int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct inode *inode;
1139
Dave Chinner67a23c42011-03-22 22:23:42 +11001140 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 inode = find_inode(sb, head, test, data);
1142 if (inode) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001143 spin_unlock(&inode_hash_lock);
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001144 if (likely(wait))
1145 wait_on_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return inode;
1147 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001148 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return NULL;
1150}
1151
1152/**
1153 * ifind_fast - internal function, you want ilookup() or iget().
1154 * @sb: super block of file system to search
1155 * @head: head of the list to search
1156 * @ino: inode number to search for
1157 *
1158 * ifind_fast() searches for the inode @ino in the inode cache. This is for
1159 * file systems where the inode number is sufficient for unique identification
1160 * of an inode.
1161 *
1162 * If the inode is in the cache, the inode is returned with an incremented
1163 * reference count.
1164 *
1165 * Otherwise NULL is returned.
1166 */
Matt Mackall5d2bea42006-01-08 01:05:21 -08001167static struct inode *ifind_fast(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct hlist_head *head, unsigned long ino)
1169{
1170 struct inode *inode;
1171
Dave Chinner67a23c42011-03-22 22:23:42 +11001172 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 inode = find_inode_fast(sb, head, ino);
1174 if (inode) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001175 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 wait_on_inode(inode);
1177 return inode;
1178 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001179 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return NULL;
1181}
1182
1183/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001184 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * @sb: super block of file system to search
1186 * @hashval: hash value (usually inode number) to search for
1187 * @test: callback used for comparisons between inodes
1188 * @data: opaque data pointer to pass to @test
1189 *
1190 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1191 * @data in the inode cache. This is a generalized version of ilookup() for
1192 * file systems where the inode number is not sufficient for unique
1193 * identification of an inode.
1194 *
1195 * If the inode is in the cache, the inode is returned with an incremented
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001196 * reference count. Note, the inode lock is not waited upon so you have to be
1197 * very careful what you do with the returned inode. You probably should be
1198 * using ilookup5() instead.
1199 *
1200 * Otherwise NULL is returned.
1201 *
Dave Chinner67a23c42011-03-22 22:23:42 +11001202 * Note, @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001203 */
1204struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1205 int (*test)(struct inode *, void *), void *data)
1206{
1207 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1208
1209 return ifind(sb, head, test, data, 0);
1210}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001211EXPORT_SYMBOL(ilookup5_nowait);
1212
1213/**
1214 * ilookup5 - search for an inode in the inode cache
1215 * @sb: super block of file system to search
1216 * @hashval: hash value (usually inode number) to search for
1217 * @test: callback used for comparisons between inodes
1218 * @data: opaque data pointer to pass to @test
1219 *
1220 * ilookup5() uses ifind() to search for the inode specified by @hashval and
1221 * @data in the inode cache. This is a generalized version of ilookup() for
1222 * file systems where the inode number is not sufficient for unique
1223 * identification of an inode.
1224 *
1225 * If the inode is in the cache, the inode lock is waited upon and the inode is
1226 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 *
1228 * Otherwise NULL is returned.
1229 *
Dave Chinner67a23c42011-03-22 22:23:42 +11001230 * Note, @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
1232struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1233 int (*test)(struct inode *, void *), void *data)
1234{
1235 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1236
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001237 return ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239EXPORT_SYMBOL(ilookup5);
1240
1241/**
1242 * ilookup - search for an inode in the inode cache
1243 * @sb: super block of file system to search
1244 * @ino: inode number to search for
1245 *
1246 * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
1247 * This is for file systems where the inode number is sufficient for unique
1248 * identification of an inode.
1249 *
1250 * If the inode is in the cache, the inode is returned with an incremented
1251 * reference count.
1252 *
1253 * Otherwise NULL is returned.
1254 */
1255struct inode *ilookup(struct super_block *sb, unsigned long ino)
1256{
1257 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1258
1259 return ifind_fast(sb, head, ino);
1260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261EXPORT_SYMBOL(ilookup);
1262
1263/**
1264 * iget5_locked - obtain an inode from a mounted file system
1265 * @sb: super block of file system
1266 * @hashval: hash value (usually inode number) to get
1267 * @test: callback used for comparisons between inodes
1268 * @set: callback used to initialize a new struct inode
1269 * @data: opaque data pointer to pass to @test and @set
1270 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * iget5_locked() uses ifind() to search for the inode specified by @hashval
1272 * and @data in the inode cache and if present it is returned with an increased
1273 * reference count. This is a generalized version of iget_locked() for file
1274 * systems where the inode number is not sufficient for unique identification
1275 * of an inode.
1276 *
1277 * If the inode is not in cache, get_new_inode() is called to allocate a new
1278 * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1279 * file system gets to fill it in before unlocking it via unlock_new_inode().
1280 *
Dave Chinner67a23c42011-03-22 22:23:42 +11001281 * Note both @test and @set are called with the inode_hash_lock held, so can't
1282 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 */
1284struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1285 int (*test)(struct inode *, void *),
1286 int (*set)(struct inode *, void *), void *data)
1287{
1288 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1289 struct inode *inode;
1290
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001291 inode = ifind(sb, head, test, data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (inode)
1293 return inode;
1294 /*
1295 * get_new_inode() will do the right thing, re-trying the search
1296 * in case it had to block at any point.
1297 */
1298 return get_new_inode(sb, head, test, set, data);
1299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300EXPORT_SYMBOL(iget5_locked);
1301
1302/**
1303 * iget_locked - obtain an inode from a mounted file system
1304 * @sb: super block of file system
1305 * @ino: inode number to get
1306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1308 * the inode cache and if present it is returned with an increased reference
1309 * count. This is for file systems where the inode number is sufficient for
1310 * unique identification of an inode.
1311 *
1312 * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1313 * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1314 * The file system gets to fill it in before unlocking it via
1315 * unlock_new_inode().
1316 */
1317struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1318{
1319 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1320 struct inode *inode;
1321
1322 inode = ifind_fast(sb, head, ino);
1323 if (inode)
1324 return inode;
1325 /*
1326 * get_new_inode_fast() will do the right thing, re-trying the search
1327 * in case it had to block at any point.
1328 */
1329 return get_new_inode_fast(sb, head, ino);
1330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331EXPORT_SYMBOL(iget_locked);
1332
Al Viro261bca82008-12-30 01:48:21 -05001333int insert_inode_locked(struct inode *inode)
1334{
1335 struct super_block *sb = inode->i_sb;
1336 ino_t ino = inode->i_ino;
1337 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001338
Al Viro261bca82008-12-30 01:48:21 -05001339 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001340 struct hlist_node *node;
1341 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001342 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001343 hlist_for_each_entry(old, node, head, i_hash) {
1344 if (old->i_ino != ino)
1345 continue;
1346 if (old->i_sb != sb)
1347 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001348 spin_lock(&old->i_lock);
1349 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1350 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001351 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001352 }
Al Viro72a43d62009-05-13 19:13:40 +01001353 break;
1354 }
1355 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001356 spin_lock(&inode->i_lock);
1357 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001358 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001359 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001360 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001361 return 0;
1362 }
1363 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001364 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001365 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001366 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001367 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001368 iput(old);
1369 return -EBUSY;
1370 }
1371 iput(old);
1372 }
1373}
Al Viro261bca82008-12-30 01:48:21 -05001374EXPORT_SYMBOL(insert_inode_locked);
1375
1376int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1377 int (*test)(struct inode *, void *), void *data)
1378{
1379 struct super_block *sb = inode->i_sb;
1380 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001381
Al Viro261bca82008-12-30 01:48:21 -05001382 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001383 struct hlist_node *node;
1384 struct inode *old = NULL;
1385
Dave Chinner67a23c42011-03-22 22:23:42 +11001386 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001387 hlist_for_each_entry(old, node, head, i_hash) {
1388 if (old->i_sb != sb)
1389 continue;
1390 if (!test(old, data))
1391 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001392 spin_lock(&old->i_lock);
1393 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1394 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001395 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001396 }
Al Viro72a43d62009-05-13 19:13:40 +01001397 break;
1398 }
1399 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001400 spin_lock(&inode->i_lock);
1401 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001402 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001403 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001404 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001405 return 0;
1406 }
1407 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001408 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001409 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001410 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001411 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001412 iput(old);
1413 return -EBUSY;
1414 }
1415 iput(old);
1416 }
1417}
Al Viro261bca82008-12-30 01:48:21 -05001418EXPORT_SYMBOL(insert_inode_locked4);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Al Viro45321ac2010-06-07 13:43:19 -04001421int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Al Viro45321ac2010-06-07 13:43:19 -04001423 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425EXPORT_SYMBOL(generic_delete_inode);
1426
Al Viro45321ac2010-06-07 13:43:19 -04001427/*
1428 * Normal UNIX filesystem behaviour: delete the
1429 * inode when the usage count drops to zero, and
1430 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001431 */
Al Viro45321ac2010-06-07 13:43:19 -04001432int generic_drop_inode(struct inode *inode)
1433{
Al Viro1d3382cb2010-10-23 15:19:20 -04001434 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001435}
1436EXPORT_SYMBOL_GPL(generic_drop_inode);
1437
1438/*
1439 * Called when we're dropping the last reference
1440 * to an inode.
1441 *
1442 * Call the FS "drop_inode()" function, defaulting to
1443 * the legacy UNIX filesystem behaviour. If it tells
1444 * us to evict inode, do so. Otherwise, retain inode
1445 * in cache if fs is alive, sync and evict if fs is
1446 * shutting down.
1447 */
1448static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001451 const struct super_operations *op = inode->i_sb->s_op;
1452 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Dave Chinner250df6e2011-03-22 22:23:36 +11001454 WARN_ON(inode->i_state & I_NEW);
1455
Al Viro45321ac2010-06-07 13:43:19 -04001456 if (op && op->drop_inode)
1457 drop = op->drop_inode(inode);
1458 else
1459 drop = generic_drop_inode(inode);
1460
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001461 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1462 inode->i_state |= I_REFERENCED;
1463 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1464 inode_lru_list_add(inode);
1465 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001466 return;
1467 }
1468
Al Viro45321ac2010-06-07 13:43:19 -04001469 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001470 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001471 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001473 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001474 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001475 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001477
Alexander Viro991114c2005-06-23 00:09:01 -07001478 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001479 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001480 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001481
1482 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301486 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * @inode: inode to put
1488 *
1489 * Puts an inode, dropping its usage count. If the inode use count hits
1490 * zero, the inode is then freed and may also be destroyed.
1491 *
1492 * Consequently, iput() can sleep.
1493 */
1494void iput(struct inode *inode)
1495{
1496 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001497 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Dave Chinnerf283c862011-03-22 22:23:39 +11001499 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 iput_final(inode);
1501 }
1502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503EXPORT_SYMBOL(iput);
1504
1505/**
1506 * bmap - find a block number in a file
1507 * @inode: inode of file
1508 * @block: block to find
1509 *
1510 * Returns the block number on the device holding the inode that
1511 * is the disk block number for the block of the file requested.
1512 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301513 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 * file.
1515 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301516sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 sector_t res = 0;
1519 if (inode->i_mapping->a_ops->bmap)
1520 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1521 return res;
1522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523EXPORT_SYMBOL(bmap);
1524
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001525/*
1526 * With relative atime, only update atime if the previous atime is
1527 * earlier than either the ctime or mtime or if at least a day has
1528 * passed since the last atime update.
1529 */
1530static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1531 struct timespec now)
1532{
1533
1534 if (!(mnt->mnt_flags & MNT_RELATIME))
1535 return 1;
1536 /*
1537 * Is mtime younger than atime? If yes, update atime:
1538 */
1539 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1540 return 1;
1541 /*
1542 * Is ctime younger than atime? If yes, update atime:
1543 */
1544 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1545 return 1;
1546
1547 /*
1548 * Is the previous atime value older than a day? If yes,
1549 * update atime:
1550 */
1551 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1552 return 1;
1553 /*
1554 * Good, we can skip the atime update:
1555 */
1556 return 0;
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001560 * touch_atime - update the access time
1561 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001562 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 *
1564 * Update the accessed time on an inode and mark it for writeback.
1565 * This function automatically handles read only file systems and media,
1566 * as well as the "noatime" flag and inode specific "noatime" markers.
1567 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001568void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001570 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 struct timespec now;
1572
Andrew Mortonb2276132006-12-13 00:34:33 -08001573 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001574 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001575 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001576 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001577 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001578 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001579
Dave Hansencdb70f32008-02-15 14:37:41 -08001580 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001581 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001582 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001583 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001586
1587 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001588 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001589
Valerie Henson47ae32d2006-12-13 00:34:34 -08001590 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001591 return;
1592
1593 if (mnt_want_write(mnt))
1594 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001595
1596 inode->i_atime = now;
1597 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001598 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001600EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001603 * file_update_time - update mtime and ctime time
1604 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001606 * Update the mtime and ctime members of an inode and mark the inode
1607 * for writeback. Note that this function is meant exclusively for
1608 * usage in the file write path of filesystems, and filesystems may
1609 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001610 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001611 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 */
1613
Christoph Hellwig870f4812006-01-09 20:52:01 -08001614void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001616 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001618 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Andi Kleence06e0b2009-09-18 13:05:48 -07001620 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (IS_NOCMTIME(inode))
1622 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001623
Andi Kleence06e0b2009-09-18 13:05:48 -07001624 now = current_fs_time(inode->i_sb);
1625 if (!timespec_equal(&inode->i_mtime, &now))
1626 sync_it = S_MTIME;
1627
1628 if (!timespec_equal(&inode->i_ctime, &now))
1629 sync_it |= S_CTIME;
1630
1631 if (IS_I_VERSION(inode))
1632 sync_it |= S_VERSION;
1633
1634 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return;
1636
Andi Kleence06e0b2009-09-18 13:05:48 -07001637 /* Finally allowed to write? Takes lock. */
1638 if (mnt_want_write_file(file))
1639 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Andi Kleence06e0b2009-09-18 13:05:48 -07001641 /* Only change inode inside the lock region */
1642 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001643 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001644 if (sync_it & S_CTIME)
1645 inode->i_ctime = now;
1646 if (sync_it & S_MTIME)
1647 inode->i_mtime = now;
1648 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001649 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001651EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653int inode_needs_sync(struct inode *inode)
1654{
1655 if (IS_SYNC(inode))
1656 return 1;
1657 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1658 return 1;
1659 return 0;
1660}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661EXPORT_SYMBOL(inode_needs_sync);
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663int inode_wait(void *word)
1664{
1665 schedule();
1666 return 0;
1667}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001668EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001671 * If we try to find an inode in the inode hash while it is being
1672 * deleted, we have to wait until the filesystem completes its
1673 * deletion before reporting that it isn't found. This function waits
1674 * until the deletion _might_ have completed. Callers are responsible
1675 * to recheck inode state.
1676 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001677 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001678 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1679 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 */
1681static void __wait_on_freeing_inode(struct inode *inode)
1682{
1683 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001684 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1685 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001687 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001688 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 schedule();
1690 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001691 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static __initdata unsigned long ihash_entries;
1695static int __init set_ihash_entries(char *str)
1696{
1697 if (!str)
1698 return 0;
1699 ihash_entries = simple_strtoul(str, &str, 0);
1700 return 1;
1701}
1702__setup("ihash_entries=", set_ihash_entries);
1703
1704/*
1705 * Initialize the waitqueues and inode hash table.
1706 */
1707void __init inode_init_early(void)
1708{
1709 int loop;
1710
1711 /* If hashes are distributed across NUMA nodes, defer
1712 * hash allocation until vmalloc space is available.
1713 */
1714 if (hashdist)
1715 return;
1716
1717 inode_hashtable =
1718 alloc_large_system_hash("Inode-cache",
1719 sizeof(struct hlist_head),
1720 ihash_entries,
1721 14,
1722 HASH_EARLY,
1723 &i_hash_shift,
1724 &i_hash_mask,
1725 0);
1726
1727 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1728 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1729}
1730
Denis Cheng74bf17c2007-10-16 23:26:30 -07001731void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 int loop;
1734
1735 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001736 inode_cachep = kmem_cache_create("inode_cache",
1737 sizeof(struct inode),
1738 0,
1739 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1740 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001741 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001742 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /* Hash may have been set up in inode_init_early */
1745 if (!hashdist)
1746 return;
1747
1748 inode_hashtable =
1749 alloc_large_system_hash("Inode-cache",
1750 sizeof(struct hlist_head),
1751 ihash_entries,
1752 14,
1753 0,
1754 &i_hash_shift,
1755 &i_hash_mask,
1756 0);
1757
1758 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1759 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1760}
1761
1762void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1763{
1764 inode->i_mode = mode;
1765 if (S_ISCHR(mode)) {
1766 inode->i_fop = &def_chr_fops;
1767 inode->i_rdev = rdev;
1768 } else if (S_ISBLK(mode)) {
1769 inode->i_fop = &def_blk_fops;
1770 inode->i_rdev = rdev;
1771 } else if (S_ISFIFO(mode))
1772 inode->i_fop = &def_fifo_fops;
1773 else if (S_ISSOCK(mode))
1774 inode->i_fop = &bad_sock_fops;
1775 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001776 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1777 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1778 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001781
1782/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00001783 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001784 * @inode: New inode
1785 * @dir: Directory inode
1786 * @mode: mode of the new inode
1787 */
1788void inode_init_owner(struct inode *inode, const struct inode *dir,
1789 mode_t mode)
1790{
1791 inode->i_uid = current_fsuid();
1792 if (dir && dir->i_mode & S_ISGID) {
1793 inode->i_gid = dir->i_gid;
1794 if (S_ISDIR(mode))
1795 mode |= S_ISGID;
1796 } else
1797 inode->i_gid = current_fsgid();
1798 inode->i_mode = mode;
1799}
1800EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001801
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001802/**
1803 * inode_owner_or_capable - check current task permissions to inode
1804 * @inode: inode being checked
1805 *
1806 * Return true if current either has CAP_FOWNER to the inode, or
1807 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001808 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001809bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001810{
1811 struct user_namespace *ns = inode_userns(inode);
1812
1813 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1814 return true;
1815 if (ns_capable(ns, CAP_FOWNER))
1816 return true;
1817 return false;
1818}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001819EXPORT_SYMBOL(inode_owner_or_capable);