blob: 9a03611217123b138f4010a94c1e2135d8d43709 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 1997 Linus Torvalds
Christoph Hellwig4b4563d2011-05-27 09:28:01 -04003 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/fs.h>
6#include <linux/mm.h>
7#include <linux/dcache.h>
8#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
10#include <linux/writeback.h>
11#include <linux/module.h>
12#include <linux/backing-dev.h>
13#include <linux/wait.h>
Nick Piggin88e0fbc2009-09-22 16:43:50 -070014#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/hash.h>
16#include <linux/swap.h>
17#include <linux/security.h>
18#include <linux/pagemap.h>
19#include <linux/cdev.h>
20#include <linux/bootmem.h>
Eric Paris3be25f42009-05-21 17:01:26 -040021#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080022#include <linux/mount.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080023#include <linux/async.h>
Al Virof19d4a82009-06-08 19:50:45 -040024#include <linux/posix_acl.h>
Heiko Carstens9ce6e0b2011-05-22 18:54:21 +020025#include <linux/prefetch.h>
Eric Parisa178d202010-10-25 14:41:59 -040026#include <linux/ima.h>
Serge E. Hallyne795b712011-03-23 16:43:25 -070027#include <linux/cred.h>
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040028#include <linux/buffer_head.h> /* for inode_has_buffers */
Dave Chinnera66979a2011-03-22 22:23:41 +110029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040032 * Inode locking rules:
Dave Chinner250df6e2011-03-22 22:23:36 +110033 *
34 * inode->i_lock protects:
35 * inode->i_state, inode->i_hash, __iget()
Dave Chinner02afc412011-03-22 22:23:38 +110036 * inode_lru_lock protects:
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
Eric Dumazetfa3536c2006-03-26 01:37:24 -080062static unsigned int i_hash_mask __read_mostly;
63static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110064static struct hlist_head *inode_hashtable __read_mostly;
65static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Nick Piggin7ccf19a2010-10-21 11:49:30 +110067static LIST_HEAD(inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +110068static DEFINE_SPINLOCK(inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dave Chinner55fa6092011-03-22 22:23:40 +110070__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
Dave Chinnera66979a2011-03-22 22:23:41 +110071__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +110072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +010074 * iprune_sem provides exclusion between the icache shrinking and the
75 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -070076 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +010077 * We don't actually need it to protect anything in the umount path,
78 * but only need to cycle through it to make sure any inode that
79 * prune_icache took off the LRU list has been fully torn down by the
80 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -070082static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020085 * Empty aops. Can be used for the cases where the user does not
86 * define any of the address_space operations.
87 */
88const struct address_space_operations empty_aops = {
89};
90EXPORT_SYMBOL(empty_aops);
91
92/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Statistics gathering..
94 */
95struct inodes_stat_t inodes_stat;
96
Nick Piggin3e880fb2011-01-07 17:49:19 +110097static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnerfcb94f72011-07-08 14:14:38 +100098static DEFINE_PER_CPU(unsigned int, nr_unused);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040099
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530100static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Nick Piggin3e880fb2011-01-07 17:49:19 +1100102static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400103{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100104 int i;
105 int sum = 0;
106 for_each_possible_cpu(i)
107 sum += per_cpu(nr_inodes, i);
108 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400109}
110
111static inline int get_nr_inodes_unused(void)
112{
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000113 int i;
114 int sum = 0;
115 for_each_possible_cpu(i)
116 sum += per_cpu(nr_unused, i);
117 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400118}
119
120int get_nr_dirty_inodes(void)
121{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100122 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400123 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
124 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400125}
126
127/*
128 * Handle nr_inode sysctl
129 */
130#ifdef CONFIG_SYSCTL
131int proc_nr_inodes(ctl_table *table, int write,
132 void __user *buffer, size_t *lenp, loff_t *ppos)
133{
134 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000135 inodes_stat.nr_unused = get_nr_inodes_unused();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400136 return proc_dointvec(table, write, buffer, lenp, ppos);
137}
138#endif
139
David Chinner2cb15992008-10-30 17:32:23 +1100140/**
141 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800142 * @sb: superblock inode belongs to
143 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100144 *
145 * These are initializations that need to be done on every inode
146 * allocation as the fields are not initialised by slab allocation.
147 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300148int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700150 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800151 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530152 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100153
154 inode->i_sb = sb;
155 inode->i_blkbits = sb->s_blocksize_bits;
156 inode->i_flags = 0;
157 atomic_set(&inode->i_count, 1);
158 inode->i_op = &empty_iops;
159 inode->i_fop = &empty_fops;
160 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500161 inode->i_uid = 0;
162 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100163 atomic_set(&inode->i_writecount, 0);
164 inode->i_size = 0;
165 inode->i_blocks = 0;
166 inode->i_bytes = 0;
167 inode->i_generation = 0;
168#ifdef CONFIG_QUOTA
169 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
170#endif
171 inode->i_pipe = NULL;
172 inode->i_bdev = NULL;
173 inode->i_cdev = NULL;
174 inode->i_rdev = 0;
175 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500176
177 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300178 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100179 spin_lock_init(&inode->i_lock);
180 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
181
182 mutex_init(&inode->i_mutex);
183 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
184
185 init_rwsem(&inode->i_alloc_sem);
186 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
187
188 mapping->a_ops = &empty_aops;
189 mapping->host = inode;
190 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800191 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100192 mapping->assoc_mapping = NULL;
193 mapping->backing_dev_info = &default_backing_dev_info;
194 mapping->writeback_index = 0;
195
196 /*
197 * If the block_device provides a backing_dev_info for client
198 * inodes then use that. Otherwise the inode share the bdev's
199 * backing_dev_info.
200 */
201 if (sb->s_bdev) {
202 struct backing_dev_info *bdi;
203
Jens Axboe2c96ce92009-09-15 09:43:56 +0200204 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100205 mapping->backing_dev_info = bdi;
206 }
207 inode->i_private = NULL;
208 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400209#ifdef CONFIG_FS_POSIX_ACL
210 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
211#endif
David Chinner2cb15992008-10-30 17:32:23 +1100212
Eric Paris3be25f42009-05-21 17:01:26 -0400213#ifdef CONFIG_FSNOTIFY
214 inode->i_fsnotify_mask = 0;
215#endif
216
Nick Piggin3e880fb2011-01-07 17:49:19 +1100217 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400218
Christoph Hellwig54e34622009-08-07 14:38:25 -0300219 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300220out:
221 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100222}
223EXPORT_SYMBOL(inode_init_always);
224
225static struct inode *alloc_inode(struct super_block *sb)
226{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct inode *inode;
228
229 if (sb->s_op->alloc_inode)
230 inode = sb->s_op->alloc_inode(sb);
231 else
David Chinner2cb15992008-10-30 17:32:23 +1100232 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Christoph Hellwig54e34622009-08-07 14:38:25 -0300234 if (!inode)
235 return NULL;
236
237 if (unlikely(inode_init_always(sb, inode))) {
238 if (inode->i_sb->s_op->destroy_inode)
239 inode->i_sb->s_op->destroy_inode(inode);
240 else
241 kmem_cache_free(inode_cachep, inode);
242 return NULL;
243 }
244
245 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Nick Pigginff0c7d12011-01-07 17:49:50 +1100248void free_inode_nonrcu(struct inode *inode)
249{
250 kmem_cache_free(inode_cachep, inode);
251}
252EXPORT_SYMBOL(free_inode_nonrcu);
253
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300254void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200256 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400258 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400259#ifdef CONFIG_FS_POSIX_ACL
260 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
261 posix_acl_release(inode->i_acl);
262 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
263 posix_acl_release(inode->i_default_acl);
264#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100265 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300266}
267EXPORT_SYMBOL(__destroy_inode);
268
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100269static void i_callback(struct rcu_head *head)
270{
271 struct inode *inode = container_of(head, struct inode, i_rcu);
272 INIT_LIST_HEAD(&inode->i_dentry);
273 kmem_cache_free(inode_cachep, inode);
274}
275
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200276static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300277{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100278 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300279 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (inode->i_sb->s_op->destroy_inode)
281 inode->i_sb->s_op->destroy_inode(inode);
282 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100283 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100286void address_space_init_once(struct address_space *mapping)
287{
288 memset(mapping, 0, sizeof(*mapping));
289 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
290 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700291 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100292 INIT_LIST_HEAD(&mapping->private_list);
293 spin_lock_init(&mapping->private_lock);
294 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
295 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100296}
297EXPORT_SYMBOL(address_space_init_once);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/*
300 * These are initializations that only need to be done
301 * once, because the fields are idempotent across use
302 * of the inode, so let the slab aware of that.
303 */
304void inode_init_once(struct inode *inode)
305{
306 memset(inode, 0, sizeof(*inode));
307 INIT_HLIST_NODE(&inode->i_hash);
308 INIT_LIST_HEAD(&inode->i_dentry);
309 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100310 INIT_LIST_HEAD(&inode->i_wb_list);
311 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100312 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400314#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500315 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318EXPORT_SYMBOL(inode_init_once);
319
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700320static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530322 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Christoph Lametera35afb82007-05-16 22:10:57 -0700324 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100328 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530330void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Nick Piggin9e38d862010-10-23 06:55:17 -0400332 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Al Viro7de9c6e2010-10-23 11:11:40 -0400335/*
336 * get additional reference to inode; caller must already hold one.
337 */
338void ihold(struct inode *inode)
339{
340 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
341}
342EXPORT_SYMBOL(ihold);
343
Nick Piggin9e38d862010-10-23 06:55:17 -0400344static void inode_lru_list_add(struct inode *inode)
345{
Dave Chinner02afc412011-03-22 22:23:38 +1100346 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100347 if (list_empty(&inode->i_lru)) {
348 list_add(&inode->i_lru, &inode_lru);
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000349 this_cpu_inc(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400350 }
Dave Chinner02afc412011-03-22 22:23:38 +1100351 spin_unlock(&inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400352}
353
354static void inode_lru_list_del(struct inode *inode)
355{
Dave Chinner02afc412011-03-22 22:23:38 +1100356 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100357 if (!list_empty(&inode->i_lru)) {
358 list_del_init(&inode->i_lru);
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000359 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400360 }
Dave Chinner02afc412011-03-22 22:23:38 +1100361 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Christoph Hellwig646ec462010-10-23 07:15:32 -0400364/**
365 * inode_sb_list_add - add inode to the superblock list of inodes
366 * @inode: inode to add
367 */
368void inode_sb_list_add(struct inode *inode)
369{
Dave Chinner55fa6092011-03-22 22:23:40 +1100370 spin_lock(&inode_sb_list_lock);
371 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
372 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400373}
374EXPORT_SYMBOL_GPL(inode_sb_list_add);
375
Dave Chinner55fa6092011-03-22 22:23:40 +1100376static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400377{
Dave Chinner55fa6092011-03-22 22:23:40 +1100378 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400379 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100380 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400381}
382
Dave Chinner4c51acb2010-10-23 06:58:09 -0400383static unsigned long hash(struct super_block *sb, unsigned long hashval)
384{
385 unsigned long tmp;
386
387 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
388 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400389 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
390 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400391}
392
393/**
394 * __insert_inode_hash - hash an inode
395 * @inode: unhashed inode
396 * @hashval: unsigned long value used to locate this object in the
397 * inode_hashtable.
398 *
399 * Add an inode to the inode hash for this superblock.
400 */
401void __insert_inode_hash(struct inode *inode, unsigned long hashval)
402{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400403 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
404
Dave Chinner67a23c42011-03-22 22:23:42 +1100405 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100406 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400407 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100408 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100409 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400410}
411EXPORT_SYMBOL(__insert_inode_hash);
412
413/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400414 * remove_inode_hash - remove an inode from the hash
415 * @inode: inode to unhash
416 *
417 * Remove an inode from the superblock.
418 */
419void remove_inode_hash(struct inode *inode)
420{
Dave Chinner67a23c42011-03-22 22:23:42 +1100421 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100422 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400423 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100424 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100425 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400426}
427EXPORT_SYMBOL(remove_inode_hash);
428
Al Virob0683aa2010-06-04 20:55:25 -0400429void end_writeback(struct inode *inode)
430{
431 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700432 /*
433 * We have to cycle tree_lock here because reclaim can be still in the
434 * process of removing the last page (in __delete_from_page_cache())
435 * and we must not free mapping under it.
436 */
437 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400438 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700439 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400440 BUG_ON(!list_empty(&inode->i_data.private_list));
441 BUG_ON(!(inode->i_state & I_FREEING));
442 BUG_ON(inode->i_state & I_CLEAR);
443 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100444 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400445 inode->i_state = I_FREEING | I_CLEAR;
446}
447EXPORT_SYMBOL(end_writeback);
448
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100449/*
450 * Free the inode passed in, removing it from the lists it is still connected
451 * to. We remove any pages still attached to the inode and wait for any IO that
452 * is still in progress before finally destroying the inode.
453 *
454 * An inode must already be marked I_FREEING so that we avoid the inode being
455 * moved back onto lists if we race with other code that manipulates the lists
456 * (e.g. writeback_single_inode). The caller is responsible for setting this.
457 *
458 * An inode must already be removed from the LRU list before being evicted from
459 * the cache. This should occur atomically with setting the I_FREEING state
460 * flag, so no inodes here should ever be on the LRU when being evicted.
461 */
Al Viro644da592010-06-07 13:21:05 -0400462static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400463{
464 const struct super_operations *op = inode->i_sb->s_op;
465
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100466 BUG_ON(!(inode->i_state & I_FREEING));
467 BUG_ON(!list_empty(&inode->i_lru));
468
Dave Chinnera66979a2011-03-22 22:23:41 +1100469 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100470 inode_sb_list_del(inode);
471
Al Virobe7ce412010-06-04 19:40:39 -0400472 if (op->evict_inode) {
473 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400474 } else {
475 if (inode->i_data.nrpages)
476 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400477 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400478 }
Al Viro661074e2010-06-04 20:19:55 -0400479 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
480 bd_forget(inode);
481 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
482 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100483
484 remove_inode_hash(inode);
485
486 spin_lock(&inode->i_lock);
487 wake_up_bit(&inode->i_state, __I_NEW);
488 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
489 spin_unlock(&inode->i_lock);
490
491 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * dispose_list - dispose of the contents of a local list
496 * @head: the head of the list to free
497 *
498 * Dispose-list gets a local list with local inodes in it, so it doesn't
499 * need to worry about list corruption and SMP locks.
500 */
501static void dispose_list(struct list_head *head)
502{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 while (!list_empty(head)) {
504 struct inode *inode;
505
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100506 inode = list_first_entry(head, struct inode, i_lru);
507 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Al Viro644da592010-06-07 13:21:05 -0400509 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/**
Al Viro63997e92010-10-25 20:49:35 -0400514 * evict_inodes - evict all evictable inodes for a superblock
515 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
Al Viro63997e92010-10-25 20:49:35 -0400517 * Make sure that no inodes with zero refcount are retained. This is
518 * called by superblock shutdown after having MS_ACTIVE flag removed,
519 * so any inode reaching zero refcount during or after that call will
520 * be immediately evicted.
521 */
522void evict_inodes(struct super_block *sb)
523{
524 struct inode *inode, *next;
525 LIST_HEAD(dispose);
526
Dave Chinner55fa6092011-03-22 22:23:40 +1100527 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400528 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
529 if (atomic_read(&inode->i_count))
530 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100531
532 spin_lock(&inode->i_lock);
533 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
534 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400535 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100536 }
Al Viro63997e92010-10-25 20:49:35 -0400537
538 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100539 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100540 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100541 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400542 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100543 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400544
545 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100546
547 /*
548 * Cycle through iprune_sem to make sure any inode that prune_icache
549 * moved off the list before we took the lock has been fully torn
550 * down.
551 */
552 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400553 up_write(&iprune_sem);
554}
555
556/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200557 * invalidate_inodes - attempt to free all inodes on a superblock
558 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100559 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200560 *
561 * Attempts to free all inodes for a given superblock. If there were any
562 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100563 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
564 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
NeilBrown93b270f2011-02-24 17:25:47 +1100566int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400568 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200569 struct inode *inode, *next;
570 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Dave Chinner55fa6092011-03-22 22:23:40 +1100572 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200573 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100574 spin_lock(&inode->i_lock);
575 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
576 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100578 }
NeilBrown93b270f2011-02-24 17:25:47 +1100579 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100580 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100581 busy = 1;
582 continue;
583 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200584 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100585 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200586 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 continue;
588 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200589
Christoph Hellwig99a38912010-10-23 19:07:20 +0200590 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100591 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100592 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100593 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100595 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Christoph Hellwiga0318782010-10-24 19:40:33 +0200597 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 return busy;
600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602static int can_unuse(struct inode *inode)
603{
Nick Piggin9e38d862010-10-23 06:55:17 -0400604 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return 0;
606 if (inode_has_buffers(inode))
607 return 0;
608 if (atomic_read(&inode->i_count))
609 return 0;
610 if (inode->i_data.nrpages)
611 return 0;
612 return 1;
613}
614
615/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400616 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100617 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *
619 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400620 * pagecache removed. If the inode has metadata buffers attached to
621 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400623 * If the inode has the I_REFERENCED flag set, then it means that it has been
624 * used recently - the flag is set in iput_final(). When we encounter such an
625 * inode, clear the flag and move it to the back of the LRU so it gets another
626 * pass through the LRU before it gets reclaimed. This is necessary because of
627 * the fact we are doing lazy LRU updates to minimise lock contention so the
628 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
629 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
631static void prune_icache(int nr_to_scan)
632{
633 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int nr_scanned;
635 unsigned long reap = 0;
636
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700637 down_read(&iprune_sem);
Dave Chinner02afc412011-03-22 22:23:38 +1100638 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
640 struct inode *inode;
641
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100642 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 break;
644
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100645 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Nick Piggin9e38d862010-10-23 06:55:17 -0400647 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100648 * we are inverting the inode_lru_lock/inode->i_lock here,
649 * so use a trylock. If we fail to get the lock, just move the
650 * inode to the back of the list so we don't spin on it.
651 */
652 if (!spin_trylock(&inode->i_lock)) {
653 list_move(&inode->i_lru, &inode_lru);
654 continue;
655 }
656
657 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400658 * Referenced or dirty inodes are still in use. Give them
659 * another pass through the LRU as we canot reclaim them now.
660 */
661 if (atomic_read(&inode->i_count) ||
662 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100663 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100664 spin_unlock(&inode->i_lock);
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000665 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400666 continue;
667 }
668
669 /* recently referenced inodes get one more pass */
670 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400671 inode->i_state &= ~I_REFERENCED;
Dave Chinner250df6e2011-03-22 22:23:36 +1100672 list_move(&inode->i_lru, &inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100673 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
675 }
676 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
677 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100678 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100679 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800681 reap += invalidate_mapping_pages(&inode->i_data,
682 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100684 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100686 if (inode != list_entry(inode_lru.next,
687 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100689 /* avoid lock inversions with trylock */
690 if (!spin_trylock(&inode->i_lock))
691 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100692 if (!can_unuse(inode)) {
693 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700697 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100699 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100700
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100701 list_move(&inode->i_lru, &freeable);
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000702 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700704 if (current_is_kswapd())
705 __count_vm_events(KSWAPD_INODESTEAL, reap);
706 else
707 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100708 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700711 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/*
715 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
716 * "unused" means that no dentries are referring to the inodes: the files are
717 * not open and the dcache references to those inodes have already been
718 * reclaimed.
719 *
720 * This function is passed the number of inodes to scan, and it returns the
721 * total number of remaining possibly-reclaimable inodes.
722 */
Ying Han1495f232011-05-24 17:12:27 -0700723static int shrink_icache_memory(struct shrinker *shrink,
724 struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Ying Han1495f232011-05-24 17:12:27 -0700726 int nr = sc->nr_to_scan;
727 gfp_t gfp_mask = sc->gfp_mask;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (nr) {
730 /*
731 * Nasty deadlock avoidance. We may hold various FS locks,
732 * and we don't want to recurse into the FS that called us
733 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!(gfp_mask & __GFP_FS))
736 return -1;
737 prune_icache(nr);
738 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400739 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Rusty Russell8e1f9362007-07-17 04:03:17 -0700742static struct shrinker icache_shrinker = {
743 .shrink = shrink_icache_memory,
744 .seeks = DEFAULT_SEEKS,
745};
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static void __wait_on_freeing_inode(struct inode *inode);
748/*
749 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530751static struct inode *find_inode(struct super_block *sb,
752 struct hlist_head *head,
753 int (*test)(struct inode *, void *),
754 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530757 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700760 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100761 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100762 if (inode->i_sb != sb) {
763 spin_unlock(&inode->i_lock);
764 continue;
765 }
766 if (!test(inode, data)) {
767 spin_unlock(&inode->i_lock);
768 continue;
769 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400770 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 __wait_on_freeing_inode(inode);
772 goto repeat;
773 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400774 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100775 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400776 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400778 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781/*
782 * find_inode_fast is the fast path version of find_inode, see the comment at
783 * iget_locked for details.
784 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530785static struct inode *find_inode_fast(struct super_block *sb,
786 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530789 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700792 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100793 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100794 if (inode->i_ino != ino) {
795 spin_unlock(&inode->i_lock);
796 continue;
797 }
798 if (inode->i_sb != sb) {
799 spin_unlock(&inode->i_lock);
800 continue;
801 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400802 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 __wait_on_freeing_inode(inode);
804 goto repeat;
805 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400806 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100807 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400808 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400810 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Eric Dumazetf991bd22010-10-23 11:18:01 -0400813/*
814 * Each cpu owns a range of LAST_INO_BATCH numbers.
815 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
816 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100817 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400818 * This does not significantly increase overflow rate because every CPU can
819 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
820 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
821 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
822 * overflow rate by 2x, which does not seem too significant.
823 *
824 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
825 * error if st_ino won't fit in target struct field. Use 32bit counter
826 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100827 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400828#define LAST_INO_BATCH 1024
829static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100830
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400831unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400832{
833 unsigned int *p = &get_cpu_var(last_ino);
834 unsigned int res = *p;
835
836#ifdef CONFIG_SMP
837 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
838 static atomic_t shared_last_ino;
839 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
840
841 res = next - LAST_INO_BATCH;
842 }
843#endif
844
845 *p = ++res;
846 put_cpu_var(last_ino);
847 return res;
David Chinner8290c352008-10-30 17:35:24 +1100848}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400849EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/**
852 * new_inode - obtain an inode
853 * @sb: superblock
854 *
Mel Gorman769848c2007-07-17 04:03:05 -0700855 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800856 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700857 * If HIGHMEM pages are unsuitable or it is known that pages allocated
858 * for the page cache are not reclaimable or migratable,
859 * mapping_set_gfp_mask() must be called with suitable flags on the
860 * newly created inode's mapping
861 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
863struct inode *new_inode(struct super_block *sb)
864{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530865 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Dave Chinner55fa6092011-03-22 22:23:40 +1100867 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 inode = alloc_inode(sb);
870 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100871 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100873 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100874 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 return inode;
877}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878EXPORT_SYMBOL(new_inode);
879
Dave Chinner250df6e2011-03-22 22:23:36 +1100880/**
881 * unlock_new_inode - clear the I_NEW state and wake up any waiters
882 * @inode: new inode to unlock
883 *
884 * Called when the inode is fully initialised to clear the new state of the
885 * inode and wake up anyone waiting for the inode to finish initialisation.
886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887void unlock_new_inode(struct inode *inode)
888{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200889#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900890 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200891 struct file_system_type *type = inode->i_sb->s_type;
892
Jan Kara9a7aa122009-06-04 15:26:49 +0200893 /* Set new key only if filesystem hasn't already changed it */
894 if (!lockdep_match_class(&inode->i_mutex,
895 &type->i_mutex_key)) {
896 /*
897 * ensure nobody is actually holding i_mutex
898 */
899 mutex_destroy(&inode->i_mutex);
900 mutex_init(&inode->i_mutex);
901 lockdep_set_class(&inode->i_mutex,
902 &type->i_mutex_dir_key);
903 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200904 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200905#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100906 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100907 WARN_ON(!(inode->i_state & I_NEW));
908 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100909 wake_up_bit(&inode->i_state, __I_NEW);
910 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912EXPORT_SYMBOL(unlock_new_inode);
913
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400914/**
915 * iget5_locked - obtain an inode from a mounted file system
916 * @sb: super block of file system
917 * @hashval: hash value (usually inode number) to get
918 * @test: callback used for comparisons between inodes
919 * @set: callback used to initialize a new struct inode
920 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400922 * Search for the inode specified by @hashval and @data in the inode cache,
923 * and if present it is return it with an increased reference count. This is
924 * a generalized version of iget_locked() for file systems where the inode
925 * number is not sufficient for unique identification of an inode.
926 *
927 * If the inode is not in cache, allocate a new inode and return it locked,
928 * hashed, and with the I_NEW flag set. The file system gets to fill it in
929 * before unlocking it via unlock_new_inode().
930 *
931 * Note both @test and @set are called with the inode_hash_lock held, so can't
932 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400934struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
935 int (*test)(struct inode *, void *),
936 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400938 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530939 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400941 spin_lock(&inode_hash_lock);
942 inode = find_inode(sb, head, test, data);
943 spin_unlock(&inode_hash_lock);
944
945 if (inode) {
946 wait_on_inode(inode);
947 return inode;
948 }
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 inode = alloc_inode(sb);
951 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530952 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dave Chinner67a23c42011-03-22 22:23:42 +1100954 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* We released the lock, so.. */
956 old = find_inode(sb, head, test, data);
957 if (!old) {
958 if (set(inode, data))
959 goto set_failed;
960
Dave Chinner250df6e2011-03-22 22:23:36 +1100961 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100962 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100963 hlist_add_head(&inode->i_hash, head);
964 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100965 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100966 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* Return the locked inode with I_NEW set, the
969 * caller is responsible for filling in the contents
970 */
971 return inode;
972 }
973
974 /*
975 * Uhhuh, somebody else created the same inode under
976 * us. Use the old inode instead of the one we just
977 * allocated.
978 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100979 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 destroy_inode(inode);
981 inode = old;
982 wait_on_inode(inode);
983 }
984 return inode;
985
986set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100987 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 destroy_inode(inode);
989 return NULL;
990}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400991EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400993/**
994 * iget_locked - obtain an inode from a mounted file system
995 * @sb: super block of file system
996 * @ino: inode number to get
997 *
998 * Search for the inode specified by @ino in the inode cache and if present
999 * return it with an increased reference count. This is for file systems
1000 * where the inode number is sufficient for unique identification of an inode.
1001 *
1002 * If the inode is not in cache, allocate a new inode and return it locked,
1003 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1004 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001006struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001008 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301009 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001011 spin_lock(&inode_hash_lock);
1012 inode = find_inode_fast(sb, head, ino);
1013 spin_unlock(&inode_hash_lock);
1014 if (inode) {
1015 wait_on_inode(inode);
1016 return inode;
1017 }
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 inode = alloc_inode(sb);
1020 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301021 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Dave Chinner67a23c42011-03-22 22:23:42 +11001023 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* We released the lock, so.. */
1025 old = find_inode_fast(sb, head, ino);
1026 if (!old) {
1027 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001028 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001029 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001030 hlist_add_head(&inode->i_hash, head);
1031 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001032 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001033 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* Return the locked inode with I_NEW set, the
1036 * caller is responsible for filling in the contents
1037 */
1038 return inode;
1039 }
1040
1041 /*
1042 * Uhhuh, somebody else created the same inode under
1043 * us. Use the old inode instead of the one we just
1044 * allocated.
1045 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001046 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 destroy_inode(inode);
1048 inode = old;
1049 wait_on_inode(inode);
1050 }
1051 return inode;
1052}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001053EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001055/*
1056 * search the inode cache for a matching inode number.
1057 * If we find one, then the inode number we are trying to
1058 * allocate is not unique and so we should not use it.
1059 *
1060 * Returns 1 if the inode number is unique, 0 if it is not.
1061 */
1062static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1063{
1064 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1065 struct hlist_node *node;
1066 struct inode *inode;
1067
Dave Chinner67a23c42011-03-22 22:23:42 +11001068 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001069 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001070 if (inode->i_ino == ino && inode->i_sb == sb) {
1071 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001072 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001073 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001074 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001075 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001076
1077 return 1;
1078}
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080/**
1081 * iunique - get a unique inode number
1082 * @sb: superblock
1083 * @max_reserved: highest reserved inode number
1084 *
1085 * Obtain an inode number that is unique on the system for a given
1086 * superblock. This is used by file systems that have no natural
1087 * permanent inode numbering system. An inode number is returned that
1088 * is higher than the reserved limit but unique.
1089 *
1090 * BUGS:
1091 * With a large number of inodes live on the file system this function
1092 * currently becomes quite slow.
1093 */
1094ino_t iunique(struct super_block *sb, ino_t max_reserved)
1095{
Jeff Layton866b04f2007-05-08 00:32:29 -07001096 /*
1097 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1098 * error if st_ino won't fit in target struct field. Use 32bit counter
1099 * here to attempt to avoid that.
1100 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001101 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001102 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001105 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001106 do {
1107 if (counter <= max_reserved)
1108 counter = max_reserved + 1;
1109 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001110 } while (!test_inode_iunique(sb, res));
1111 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001112
1113 return res;
1114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115EXPORT_SYMBOL(iunique);
1116
1117struct inode *igrab(struct inode *inode)
1118{
Dave Chinner250df6e2011-03-22 22:23:36 +11001119 spin_lock(&inode->i_lock);
1120 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001122 spin_unlock(&inode->i_lock);
1123 } else {
1124 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
1126 * Handle the case where s_op->clear_inode is not been
1127 * called yet, and somebody is calling igrab
1128 * while the inode is getting freed.
1129 */
1130 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return inode;
1133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134EXPORT_SYMBOL(igrab);
1135
1136/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001137 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * @sb: super block of file system to search
1139 * @hashval: hash value (usually inode number) to search for
1140 * @test: callback used for comparisons between inodes
1141 * @data: opaque data pointer to pass to @test
1142 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001143 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001145 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001146 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001147 * Note: I_NEW is not waited upon so you have to be very careful what you do
1148 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001149 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001150 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001151 */
1152struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1153 int (*test)(struct inode *, void *), void *data)
1154{
1155 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001156 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001157
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001158 spin_lock(&inode_hash_lock);
1159 inode = find_inode(sb, head, test, data);
1160 spin_unlock(&inode_hash_lock);
1161
1162 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001163}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001164EXPORT_SYMBOL(ilookup5_nowait);
1165
1166/**
1167 * ilookup5 - search for an inode in the inode cache
1168 * @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 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001173 * Search for the inode specified by @hashval and @data in the inode cache,
1174 * and if the inode is in the cache, return the inode with an incremented
1175 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001176 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001178 * This is a generalized version of ilookup() for file systems where the
1179 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001181 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
1183struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1184 int (*test)(struct inode *, void *), void *data)
1185{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001186 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001188 if (inode)
1189 wait_on_inode(inode);
1190 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192EXPORT_SYMBOL(ilookup5);
1193
1194/**
1195 * ilookup - search for an inode in the inode cache
1196 * @sb: super block of file system to search
1197 * @ino: inode number to search for
1198 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001199 * Search for the inode @ino in the inode cache, and if the inode is in the
1200 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 */
1202struct inode *ilookup(struct super_block *sb, unsigned long ino)
1203{
1204 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001205 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001207 spin_lock(&inode_hash_lock);
1208 inode = find_inode_fast(sb, head, ino);
1209 spin_unlock(&inode_hash_lock);
1210
1211 if (inode)
1212 wait_on_inode(inode);
1213 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215EXPORT_SYMBOL(ilookup);
1216
Al Viro261bca82008-12-30 01:48:21 -05001217int insert_inode_locked(struct inode *inode)
1218{
1219 struct super_block *sb = inode->i_sb;
1220 ino_t ino = inode->i_ino;
1221 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001222
Al Viro261bca82008-12-30 01:48:21 -05001223 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001224 struct hlist_node *node;
1225 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001226 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001227 hlist_for_each_entry(old, node, head, i_hash) {
1228 if (old->i_ino != ino)
1229 continue;
1230 if (old->i_sb != sb)
1231 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001232 spin_lock(&old->i_lock);
1233 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1234 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001235 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001236 }
Al Viro72a43d62009-05-13 19:13:40 +01001237 break;
1238 }
1239 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001240 spin_lock(&inode->i_lock);
1241 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001242 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001243 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001244 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001245 return 0;
1246 }
1247 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001248 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001249 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001250 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001251 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001252 iput(old);
1253 return -EBUSY;
1254 }
1255 iput(old);
1256 }
1257}
Al Viro261bca82008-12-30 01:48:21 -05001258EXPORT_SYMBOL(insert_inode_locked);
1259
1260int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1261 int (*test)(struct inode *, void *), void *data)
1262{
1263 struct super_block *sb = inode->i_sb;
1264 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001265
Al Viro261bca82008-12-30 01:48:21 -05001266 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001267 struct hlist_node *node;
1268 struct inode *old = NULL;
1269
Dave Chinner67a23c42011-03-22 22:23:42 +11001270 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001271 hlist_for_each_entry(old, node, head, i_hash) {
1272 if (old->i_sb != sb)
1273 continue;
1274 if (!test(old, data))
1275 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001276 spin_lock(&old->i_lock);
1277 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1278 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001279 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001280 }
Al Viro72a43d62009-05-13 19:13:40 +01001281 break;
1282 }
1283 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001284 spin_lock(&inode->i_lock);
1285 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001286 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001287 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001288 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001289 return 0;
1290 }
1291 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001292 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001293 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001294 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001295 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001296 iput(old);
1297 return -EBUSY;
1298 }
1299 iput(old);
1300 }
1301}
Al Viro261bca82008-12-30 01:48:21 -05001302EXPORT_SYMBOL(insert_inode_locked4);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Al Viro45321ac2010-06-07 13:43:19 -04001305int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Al Viro45321ac2010-06-07 13:43:19 -04001307 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309EXPORT_SYMBOL(generic_delete_inode);
1310
Al Viro45321ac2010-06-07 13:43:19 -04001311/*
1312 * Normal UNIX filesystem behaviour: delete the
1313 * inode when the usage count drops to zero, and
1314 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001315 */
Al Viro45321ac2010-06-07 13:43:19 -04001316int generic_drop_inode(struct inode *inode)
1317{
Al Viro1d3382c2010-10-23 15:19:20 -04001318 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001319}
1320EXPORT_SYMBOL_GPL(generic_drop_inode);
1321
1322/*
1323 * Called when we're dropping the last reference
1324 * to an inode.
1325 *
1326 * Call the FS "drop_inode()" function, defaulting to
1327 * the legacy UNIX filesystem behaviour. If it tells
1328 * us to evict inode, do so. Otherwise, retain inode
1329 * in cache if fs is alive, sync and evict if fs is
1330 * shutting down.
1331 */
1332static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001335 const struct super_operations *op = inode->i_sb->s_op;
1336 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Dave Chinner250df6e2011-03-22 22:23:36 +11001338 WARN_ON(inode->i_state & I_NEW);
1339
Al Viroe7f59092011-07-07 15:45:59 -04001340 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001341 drop = op->drop_inode(inode);
1342 else
1343 drop = generic_drop_inode(inode);
1344
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001345 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1346 inode->i_state |= I_REFERENCED;
1347 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1348 inode_lru_list_add(inode);
1349 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001350 return;
1351 }
1352
Al Viro45321ac2010-06-07 13:43:19 -04001353 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001354 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001355 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001357 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001358 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001359 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001361
Alexander Viro991114c2005-06-23 00:09:01 -07001362 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001363 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001364 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001365
1366 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301370 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * @inode: inode to put
1372 *
1373 * Puts an inode, dropping its usage count. If the inode use count hits
1374 * zero, the inode is then freed and may also be destroyed.
1375 *
1376 * Consequently, iput() can sleep.
1377 */
1378void iput(struct inode *inode)
1379{
1380 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001381 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Dave Chinnerf283c862011-03-22 22:23:39 +11001383 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 iput_final(inode);
1385 }
1386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387EXPORT_SYMBOL(iput);
1388
1389/**
1390 * bmap - find a block number in a file
1391 * @inode: inode of file
1392 * @block: block to find
1393 *
1394 * Returns the block number on the device holding the inode that
1395 * is the disk block number for the block of the file requested.
1396 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301397 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 * file.
1399 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301400sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 sector_t res = 0;
1403 if (inode->i_mapping->a_ops->bmap)
1404 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1405 return res;
1406}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407EXPORT_SYMBOL(bmap);
1408
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001409/*
1410 * With relative atime, only update atime if the previous atime is
1411 * earlier than either the ctime or mtime or if at least a day has
1412 * passed since the last atime update.
1413 */
1414static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1415 struct timespec now)
1416{
1417
1418 if (!(mnt->mnt_flags & MNT_RELATIME))
1419 return 1;
1420 /*
1421 * Is mtime younger than atime? If yes, update atime:
1422 */
1423 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1424 return 1;
1425 /*
1426 * Is ctime younger than atime? If yes, update atime:
1427 */
1428 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1429 return 1;
1430
1431 /*
1432 * Is the previous atime value older than a day? If yes,
1433 * update atime:
1434 */
1435 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1436 return 1;
1437 /*
1438 * Good, we can skip the atime update:
1439 */
1440 return 0;
1441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001444 * touch_atime - update the access time
1445 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001446 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 *
1448 * Update the accessed time on an inode and mark it for writeback.
1449 * This function automatically handles read only file systems and media,
1450 * as well as the "noatime" flag and inode specific "noatime" markers.
1451 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001452void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001454 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct timespec now;
1456
Andrew Mortonb2276132006-12-13 00:34:33 -08001457 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001458 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001459 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001460 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001461 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001462 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001463
Dave Hansencdb70f32008-02-15 14:37:41 -08001464 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001465 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001466 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001467 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001470
1471 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001472 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001473
Valerie Henson47ae32d2006-12-13 00:34:34 -08001474 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001475 return;
1476
1477 if (mnt_want_write(mnt))
1478 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001479
1480 inode->i_atime = now;
1481 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001482 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001484EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001487 * file_update_time - update mtime and ctime time
1488 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001490 * Update the mtime and ctime members of an inode and mark the inode
1491 * for writeback. Note that this function is meant exclusively for
1492 * usage in the file write path of filesystems, and filesystems may
1493 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001494 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001495 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 */
1497
Christoph Hellwig870f4812006-01-09 20:52:01 -08001498void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001500 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001502 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Andi Kleence06e0b2009-09-18 13:05:48 -07001504 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (IS_NOCMTIME(inode))
1506 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001507
Andi Kleence06e0b2009-09-18 13:05:48 -07001508 now = current_fs_time(inode->i_sb);
1509 if (!timespec_equal(&inode->i_mtime, &now))
1510 sync_it = S_MTIME;
1511
1512 if (!timespec_equal(&inode->i_ctime, &now))
1513 sync_it |= S_CTIME;
1514
1515 if (IS_I_VERSION(inode))
1516 sync_it |= S_VERSION;
1517
1518 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return;
1520
Andi Kleence06e0b2009-09-18 13:05:48 -07001521 /* Finally allowed to write? Takes lock. */
1522 if (mnt_want_write_file(file))
1523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Andi Kleence06e0b2009-09-18 13:05:48 -07001525 /* Only change inode inside the lock region */
1526 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001527 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001528 if (sync_it & S_CTIME)
1529 inode->i_ctime = now;
1530 if (sync_it & S_MTIME)
1531 inode->i_mtime = now;
1532 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001533 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001535EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537int inode_needs_sync(struct inode *inode)
1538{
1539 if (IS_SYNC(inode))
1540 return 1;
1541 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1542 return 1;
1543 return 0;
1544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545EXPORT_SYMBOL(inode_needs_sync);
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547int inode_wait(void *word)
1548{
1549 schedule();
1550 return 0;
1551}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001552EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001555 * If we try to find an inode in the inode hash while it is being
1556 * deleted, we have to wait until the filesystem completes its
1557 * deletion before reporting that it isn't found. This function waits
1558 * until the deletion _might_ have completed. Callers are responsible
1559 * to recheck inode state.
1560 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001561 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001562 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1563 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 */
1565static void __wait_on_freeing_inode(struct inode *inode)
1566{
1567 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001568 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1569 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001571 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001572 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 schedule();
1574 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001575 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578static __initdata unsigned long ihash_entries;
1579static int __init set_ihash_entries(char *str)
1580{
1581 if (!str)
1582 return 0;
1583 ihash_entries = simple_strtoul(str, &str, 0);
1584 return 1;
1585}
1586__setup("ihash_entries=", set_ihash_entries);
1587
1588/*
1589 * Initialize the waitqueues and inode hash table.
1590 */
1591void __init inode_init_early(void)
1592{
1593 int loop;
1594
1595 /* If hashes are distributed across NUMA nodes, defer
1596 * hash allocation until vmalloc space is available.
1597 */
1598 if (hashdist)
1599 return;
1600
1601 inode_hashtable =
1602 alloc_large_system_hash("Inode-cache",
1603 sizeof(struct hlist_head),
1604 ihash_entries,
1605 14,
1606 HASH_EARLY,
1607 &i_hash_shift,
1608 &i_hash_mask,
1609 0);
1610
1611 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1612 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1613}
1614
Denis Cheng74bf17c2007-10-16 23:26:30 -07001615void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 int loop;
1618
1619 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001620 inode_cachep = kmem_cache_create("inode_cache",
1621 sizeof(struct inode),
1622 0,
1623 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1624 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001625 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001626 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /* Hash may have been set up in inode_init_early */
1629 if (!hashdist)
1630 return;
1631
1632 inode_hashtable =
1633 alloc_large_system_hash("Inode-cache",
1634 sizeof(struct hlist_head),
1635 ihash_entries,
1636 14,
1637 0,
1638 &i_hash_shift,
1639 &i_hash_mask,
1640 0);
1641
1642 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1643 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1644}
1645
1646void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1647{
1648 inode->i_mode = mode;
1649 if (S_ISCHR(mode)) {
1650 inode->i_fop = &def_chr_fops;
1651 inode->i_rdev = rdev;
1652 } else if (S_ISBLK(mode)) {
1653 inode->i_fop = &def_blk_fops;
1654 inode->i_rdev = rdev;
1655 } else if (S_ISFIFO(mode))
1656 inode->i_fop = &def_fifo_fops;
1657 else if (S_ISSOCK(mode))
1658 inode->i_fop = &bad_sock_fops;
1659 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001660 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1661 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1662 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001665
1666/**
Ben Hutchingseaae6682011-02-15 12:48:09 +00001667 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001668 * @inode: New inode
1669 * @dir: Directory inode
1670 * @mode: mode of the new inode
1671 */
1672void inode_init_owner(struct inode *inode, const struct inode *dir,
1673 mode_t mode)
1674{
1675 inode->i_uid = current_fsuid();
1676 if (dir && dir->i_mode & S_ISGID) {
1677 inode->i_gid = dir->i_gid;
1678 if (S_ISDIR(mode))
1679 mode |= S_ISGID;
1680 } else
1681 inode->i_gid = current_fsgid();
1682 inode->i_mode = mode;
1683}
1684EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001685
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001686/**
1687 * inode_owner_or_capable - check current task permissions to inode
1688 * @inode: inode being checked
1689 *
1690 * Return true if current either has CAP_FOWNER to the inode, or
1691 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001692 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001693bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001694{
1695 struct user_namespace *ns = inode_userns(inode);
1696
1697 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1698 return true;
1699 if (ns_capable(ns, CAP_FOWNER))
1700 return true;
1701 return false;
1702}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001703EXPORT_SYMBOL(inode_owner_or_capable);