blob: 1fdbb64a952f3fefde204d964cf141fe5c284085 [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 Chinner09cc9fc2011-07-08 14:14:40 +100036 * inode->i_sb->s_inode_lru_lock protects:
Dave Chinner98b745c2011-07-08 14:14:39 +100037 * inode->i_sb->s_inode_lru, inode->i_lru
Dave Chinner55fa6092011-03-22 22:23:40 +110038 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
Dave Chinnera66979a2011-03-22 22:23:41 +110040 * inode_wb_list_lock protects:
41 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
Dave Chinner67a23c42011-03-22 22:23:42 +110042 * inode_hash_lock protects:
43 * inode_hashtable, inode->i_hash
Dave Chinner250df6e2011-03-22 22:23:36 +110044 *
45 * Lock ordering:
Dave Chinner55fa6092011-03-22 22:23:40 +110046 *
47 * inode_sb_list_lock
48 * inode->i_lock
Dave Chinner09cc9fc2011-07-08 14:14:40 +100049 * inode->i_sb->s_inode_lru_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110050 *
51 * inode_wb_list_lock
52 * inode->i_lock
Dave Chinner67a23c42011-03-22 22:23:42 +110053 *
54 * inode_hash_lock
55 * inode_sb_list_lock
56 * inode->i_lock
57 *
58 * iunique_lock
59 * inode_hash_lock
Dave Chinner250df6e2011-03-22 22:23:36 +110060 */
61
Eric Dumazetfa3536c2006-03-26 01:37:24 -080062static unsigned int i_hash_mask __read_mostly;
63static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110064static struct hlist_head *inode_hashtable __read_mostly;
65static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Chinner55fa6092011-03-22 22:23:40 +110067__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
Dave Chinnera66979a2011-03-22 22:23:41 +110068__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +110069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +010071 * iprune_sem provides exclusion between the icache shrinking and the
72 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -070073 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +010074 * We don't actually need it to protect anything in the umount path,
75 * but only need to cycle through it to make sure any inode that
Dave Chinnerb0d40c92011-07-08 14:14:42 +100076 * prune_icache_sb took off the LRU list has been fully torn down by the
Christoph Hellwigbab1d942011-03-15 21:51:24 +010077 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -070079static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020082 * Empty aops. Can be used for the cases where the user does not
83 * define any of the address_space operations.
84 */
85const struct address_space_operations empty_aops = {
86};
87EXPORT_SYMBOL(empty_aops);
88
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Statistics gathering..
91 */
92struct inodes_stat_t inodes_stat;
93
Nick Piggin3e880fb2011-01-07 17:49:19 +110094static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnerfcb94f72011-07-08 14:14:38 +100095static DEFINE_PER_CPU(unsigned int, nr_unused);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040096
Manish Katiyar6b3304b2009-03-31 19:35:54 +053097static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Nick Piggin3e880fb2011-01-07 17:49:19 +110099static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400100{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100101 int i;
102 int sum = 0;
103 for_each_possible_cpu(i)
104 sum += per_cpu(nr_inodes, i);
105 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400106}
107
108static inline int get_nr_inodes_unused(void)
109{
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000110 int i;
111 int sum = 0;
112 for_each_possible_cpu(i)
113 sum += per_cpu(nr_unused, i);
114 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400115}
116
117int get_nr_dirty_inodes(void)
118{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100119 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400120 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
121 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400122}
123
124/*
125 * Handle nr_inode sysctl
126 */
127#ifdef CONFIG_SYSCTL
128int proc_nr_inodes(ctl_table *table, int write,
129 void __user *buffer, size_t *lenp, loff_t *ppos)
130{
131 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000132 inodes_stat.nr_unused = get_nr_inodes_unused();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400133 return proc_dointvec(table, write, buffer, lenp, ppos);
134}
135#endif
136
David Chinner2cb15992008-10-30 17:32:23 +1100137/**
138 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800139 * @sb: superblock inode belongs to
140 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100141 *
142 * These are initializations that need to be done on every inode
143 * allocation as the fields are not initialised by slab allocation.
144 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300145int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700147 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800148 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530149 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100150
151 inode->i_sb = sb;
152 inode->i_blkbits = sb->s_blocksize_bits;
153 inode->i_flags = 0;
154 atomic_set(&inode->i_count, 1);
155 inode->i_op = &empty_iops;
156 inode->i_fop = &empty_fops;
157 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500158 inode->i_uid = 0;
159 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100160 atomic_set(&inode->i_writecount, 0);
161 inode->i_size = 0;
162 inode->i_blocks = 0;
163 inode->i_bytes = 0;
164 inode->i_generation = 0;
165#ifdef CONFIG_QUOTA
166 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
167#endif
168 inode->i_pipe = NULL;
169 inode->i_bdev = NULL;
170 inode->i_cdev = NULL;
171 inode->i_rdev = 0;
172 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500173
174 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300175 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100176 spin_lock_init(&inode->i_lock);
177 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
178
179 mutex_init(&inode->i_mutex);
180 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
181
182 init_rwsem(&inode->i_alloc_sem);
183 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
184
185 mapping->a_ops = &empty_aops;
186 mapping->host = inode;
187 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800188 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100189 mapping->assoc_mapping = NULL;
190 mapping->backing_dev_info = &default_backing_dev_info;
191 mapping->writeback_index = 0;
192
193 /*
194 * If the block_device provides a backing_dev_info for client
195 * inodes then use that. Otherwise the inode share the bdev's
196 * backing_dev_info.
197 */
198 if (sb->s_bdev) {
199 struct backing_dev_info *bdi;
200
Jens Axboe2c96ce92009-09-15 09:43:56 +0200201 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100202 mapping->backing_dev_info = bdi;
203 }
204 inode->i_private = NULL;
205 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400206#ifdef CONFIG_FS_POSIX_ACL
207 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
208#endif
David Chinner2cb15992008-10-30 17:32:23 +1100209
Eric Paris3be25f42009-05-21 17:01:26 -0400210#ifdef CONFIG_FSNOTIFY
211 inode->i_fsnotify_mask = 0;
212#endif
213
Nick Piggin3e880fb2011-01-07 17:49:19 +1100214 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400215
Christoph Hellwig54e34622009-08-07 14:38:25 -0300216 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300217out:
218 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100219}
220EXPORT_SYMBOL(inode_init_always);
221
222static struct inode *alloc_inode(struct super_block *sb)
223{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct inode *inode;
225
226 if (sb->s_op->alloc_inode)
227 inode = sb->s_op->alloc_inode(sb);
228 else
David Chinner2cb15992008-10-30 17:32:23 +1100229 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Christoph Hellwig54e34622009-08-07 14:38:25 -0300231 if (!inode)
232 return NULL;
233
234 if (unlikely(inode_init_always(sb, inode))) {
235 if (inode->i_sb->s_op->destroy_inode)
236 inode->i_sb->s_op->destroy_inode(inode);
237 else
238 kmem_cache_free(inode_cachep, inode);
239 return NULL;
240 }
241
242 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Nick Pigginff0c7d12011-01-07 17:49:50 +1100245void free_inode_nonrcu(struct inode *inode)
246{
247 kmem_cache_free(inode_cachep, inode);
248}
249EXPORT_SYMBOL(free_inode_nonrcu);
250
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300251void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200253 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400255 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400256#ifdef CONFIG_FS_POSIX_ACL
257 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
258 posix_acl_release(inode->i_acl);
259 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
260 posix_acl_release(inode->i_default_acl);
261#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100262 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300263}
264EXPORT_SYMBOL(__destroy_inode);
265
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100266static void i_callback(struct rcu_head *head)
267{
268 struct inode *inode = container_of(head, struct inode, i_rcu);
269 INIT_LIST_HEAD(&inode->i_dentry);
270 kmem_cache_free(inode_cachep, inode);
271}
272
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200273static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300274{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100275 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300276 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (inode->i_sb->s_op->destroy_inode)
278 inode->i_sb->s_op->destroy_inode(inode);
279 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100280 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100283void address_space_init_once(struct address_space *mapping)
284{
285 memset(mapping, 0, sizeof(*mapping));
286 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
287 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700288 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100289 INIT_LIST_HEAD(&mapping->private_list);
290 spin_lock_init(&mapping->private_lock);
291 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
292 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100293}
294EXPORT_SYMBOL(address_space_init_once);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
297 * These are initializations that only need to be done
298 * once, because the fields are idempotent across use
299 * of the inode, so let the slab aware of that.
300 */
301void inode_init_once(struct inode *inode)
302{
303 memset(inode, 0, sizeof(*inode));
304 INIT_HLIST_NODE(&inode->i_hash);
305 INIT_LIST_HEAD(&inode->i_dentry);
306 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100307 INIT_LIST_HEAD(&inode->i_wb_list);
308 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100309 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400311#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500312 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400313#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315EXPORT_SYMBOL(inode_init_once);
316
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700317static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530319 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Christoph Lametera35afb82007-05-16 22:10:57 -0700321 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100325 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530327void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Nick Piggin9e38d862010-10-23 06:55:17 -0400329 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Al Viro7de9c6ee2010-10-23 11:11:40 -0400332/*
333 * get additional reference to inode; caller must already hold one.
334 */
335void ihold(struct inode *inode)
336{
337 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
338}
339EXPORT_SYMBOL(ihold);
340
Nick Piggin9e38d862010-10-23 06:55:17 -0400341static void inode_lru_list_add(struct inode *inode)
342{
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000343 spin_lock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100344 if (list_empty(&inode->i_lru)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000345 list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
346 inode->i_sb->s_nr_inodes_unused++;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000347 this_cpu_inc(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400348 }
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000349 spin_unlock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400350}
351
352static void inode_lru_list_del(struct inode *inode)
353{
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000354 spin_lock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100355 if (!list_empty(&inode->i_lru)) {
356 list_del_init(&inode->i_lru);
Dave Chinner98b745c2011-07-08 14:14:39 +1000357 inode->i_sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000358 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400359 }
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000360 spin_unlock(&inode->i_sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Christoph Hellwig646ec462010-10-23 07:15:32 -0400363/**
364 * inode_sb_list_add - add inode to the superblock list of inodes
365 * @inode: inode to add
366 */
367void inode_sb_list_add(struct inode *inode)
368{
Dave Chinner55fa6092011-03-22 22:23:40 +1100369 spin_lock(&inode_sb_list_lock);
370 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
371 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400372}
373EXPORT_SYMBOL_GPL(inode_sb_list_add);
374
Dave Chinner55fa6092011-03-22 22:23:40 +1100375static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400376{
Dave Chinner55fa6092011-03-22 22:23:40 +1100377 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400378 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100379 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400380}
381
Dave Chinner4c51acb2010-10-23 06:58:09 -0400382static unsigned long hash(struct super_block *sb, unsigned long hashval)
383{
384 unsigned long tmp;
385
386 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
387 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400388 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
389 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400390}
391
392/**
393 * __insert_inode_hash - hash an inode
394 * @inode: unhashed inode
395 * @hashval: unsigned long value used to locate this object in the
396 * inode_hashtable.
397 *
398 * Add an inode to the inode hash for this superblock.
399 */
400void __insert_inode_hash(struct inode *inode, unsigned long hashval)
401{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400402 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
403
Dave Chinner67a23c42011-03-22 22:23:42 +1100404 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100405 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400406 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100407 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100408 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400409}
410EXPORT_SYMBOL(__insert_inode_hash);
411
412/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400413 * remove_inode_hash - remove an inode from the hash
414 * @inode: inode to unhash
415 *
416 * Remove an inode from the superblock.
417 */
418void remove_inode_hash(struct inode *inode)
419{
Dave Chinner67a23c42011-03-22 22:23:42 +1100420 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100421 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400422 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100423 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100424 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400425}
426EXPORT_SYMBOL(remove_inode_hash);
427
Al Virob0683aa2010-06-04 20:55:25 -0400428void end_writeback(struct inode *inode)
429{
430 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700431 /*
432 * We have to cycle tree_lock here because reclaim can be still in the
433 * process of removing the last page (in __delete_from_page_cache())
434 * and we must not free mapping under it.
435 */
436 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400437 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700438 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400439 BUG_ON(!list_empty(&inode->i_data.private_list));
440 BUG_ON(!(inode->i_state & I_FREEING));
441 BUG_ON(inode->i_state & I_CLEAR);
442 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100443 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400444 inode->i_state = I_FREEING | I_CLEAR;
445}
446EXPORT_SYMBOL(end_writeback);
447
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100448/*
449 * Free the inode passed in, removing it from the lists it is still connected
450 * to. We remove any pages still attached to the inode and wait for any IO that
451 * is still in progress before finally destroying the inode.
452 *
453 * An inode must already be marked I_FREEING so that we avoid the inode being
454 * moved back onto lists if we race with other code that manipulates the lists
455 * (e.g. writeback_single_inode). The caller is responsible for setting this.
456 *
457 * An inode must already be removed from the LRU list before being evicted from
458 * the cache. This should occur atomically with setting the I_FREEING state
459 * flag, so no inodes here should ever be on the LRU when being evicted.
460 */
Al Viro644da592010-06-07 13:21:05 -0400461static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400462{
463 const struct super_operations *op = inode->i_sb->s_op;
464
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100465 BUG_ON(!(inode->i_state & I_FREEING));
466 BUG_ON(!list_empty(&inode->i_lru));
467
Dave Chinnera66979a2011-03-22 22:23:41 +1100468 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100469 inode_sb_list_del(inode);
470
Al Virobe7ce412010-06-04 19:40:39 -0400471 if (op->evict_inode) {
472 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400473 } else {
474 if (inode->i_data.nrpages)
475 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400476 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400477 }
Al Viro661074e2010-06-04 20:19:55 -0400478 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
479 bd_forget(inode);
480 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
481 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100482
483 remove_inode_hash(inode);
484
485 spin_lock(&inode->i_lock);
486 wake_up_bit(&inode->i_state, __I_NEW);
487 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
488 spin_unlock(&inode->i_lock);
489
490 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
494 * dispose_list - dispose of the contents of a local list
495 * @head: the head of the list to free
496 *
497 * Dispose-list gets a local list with local inodes in it, so it doesn't
498 * need to worry about list corruption and SMP locks.
499 */
500static void dispose_list(struct list_head *head)
501{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 while (!list_empty(head)) {
503 struct inode *inode;
504
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100505 inode = list_first_entry(head, struct inode, i_lru);
506 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Al Viro644da592010-06-07 13:21:05 -0400508 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/**
Al Viro63997e92010-10-25 20:49:35 -0400513 * evict_inodes - evict all evictable inodes for a superblock
514 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *
Al Viro63997e92010-10-25 20:49:35 -0400516 * Make sure that no inodes with zero refcount are retained. This is
517 * called by superblock shutdown after having MS_ACTIVE flag removed,
518 * so any inode reaching zero refcount during or after that call will
519 * be immediately evicted.
520 */
521void evict_inodes(struct super_block *sb)
522{
523 struct inode *inode, *next;
524 LIST_HEAD(dispose);
525
Dave Chinner55fa6092011-03-22 22:23:40 +1100526 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400527 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
528 if (atomic_read(&inode->i_count))
529 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100530
531 spin_lock(&inode->i_lock);
532 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
533 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400534 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100535 }
Al Viro63997e92010-10-25 20:49:35 -0400536
537 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100538 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100539 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100540 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400541 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100542 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400543
544 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100545
546 /*
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000547 * Cycle through iprune_sem to make sure any inode that prune_icache_sb
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100548 * moved off the list before we took the lock has been fully torn
549 * down.
550 */
551 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400552 up_write(&iprune_sem);
553}
554
555/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200556 * invalidate_inodes - attempt to free all inodes on a superblock
557 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100558 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200559 *
560 * Attempts to free all inodes for a given superblock. If there were any
561 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100562 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
563 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
NeilBrown93b270f2011-02-24 17:25:47 +1100565int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400567 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200568 struct inode *inode, *next;
569 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dave Chinner55fa6092011-03-22 22:23:40 +1100571 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200572 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100573 spin_lock(&inode->i_lock);
574 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
575 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100577 }
NeilBrown93b270f2011-02-24 17:25:47 +1100578 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100579 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100580 busy = 1;
581 continue;
582 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200583 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100584 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200585 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 continue;
587 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200588
Christoph Hellwig99a38912010-10-23 19:07:20 +0200589 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100590 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100591 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100592 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100594 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Christoph Hellwiga0318782010-10-24 19:40:33 +0200596 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 return busy;
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601static int can_unuse(struct inode *inode)
602{
Nick Piggin9e38d862010-10-23 06:55:17 -0400603 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 0;
605 if (inode_has_buffers(inode))
606 return 0;
607 if (atomic_read(&inode->i_count))
608 return 0;
609 if (inode->i_data.nrpages)
610 return 0;
611 return 1;
612}
613
614/*
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000615 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
616 * This is called from the superblock shrinker function with a number of inodes
617 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
618 * then are freed outside inode_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 *
620 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400621 * pagecache removed. If the inode has metadata buffers attached to
622 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400624 * If the inode has the I_REFERENCED flag set, then it means that it has been
625 * used recently - the flag is set in iput_final(). When we encounter such an
626 * inode, clear the flag and move it to the back of the LRU so it gets another
627 * pass through the LRU before it gets reclaimed. This is necessary because of
628 * the fact we are doing lazy LRU updates to minimise lock contention so the
629 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
630 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000632void prune_icache_sb(struct super_block *sb, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int nr_scanned;
636 unsigned long reap = 0;
637
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000638 down_read(&iprune_sem);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000639 spin_lock(&sb->s_inode_lru_lock);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000640 for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct inode *inode;
642
Dave Chinner98b745c2011-07-08 14:14:39 +1000643 if (list_empty(&sb->s_inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 break;
645
Dave Chinner98b745c2011-07-08 14:14:39 +1000646 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Nick Piggin9e38d862010-10-23 06:55:17 -0400648 /*
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000649 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
Dave Chinner02afc412011-03-22 22:23:38 +1100650 * so use a trylock. If we fail to get the lock, just move the
651 * inode to the back of the list so we don't spin on it.
652 */
653 if (!spin_trylock(&inode->i_lock)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000654 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +1100655 continue;
656 }
657
658 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400659 * Referenced or dirty inodes are still in use. Give them
660 * another pass through the LRU as we canot reclaim them now.
661 */
662 if (atomic_read(&inode->i_count) ||
663 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100664 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100665 spin_unlock(&inode->i_lock);
Dave Chinner98b745c2011-07-08 14:14:39 +1000666 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000667 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400668 continue;
669 }
670
671 /* recently referenced inodes get one more pass */
672 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400673 inode->i_state &= ~I_REFERENCED;
Dave Chinner98b745c2011-07-08 14:14:39 +1000674 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100675 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 continue;
677 }
678 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
679 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100680 spin_unlock(&inode->i_lock);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000681 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800683 reap += invalidate_mapping_pages(&inode->i_data,
684 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 iput(inode);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000686 spin_lock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dave Chinner98b745c2011-07-08 14:14:39 +1000688 if (inode != list_entry(sb->s_inode_lru.next,
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100689 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100691 /* avoid lock inversions with trylock */
692 if (!spin_trylock(&inode->i_lock))
693 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100694 if (!can_unuse(inode)) {
695 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700699 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100701 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100702
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100703 list_move(&inode->i_lru, &freeable);
Dave Chinner98b745c2011-07-08 14:14:39 +1000704 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000705 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700707 if (current_is_kswapd())
708 __count_vm_events(KSWAPD_INODESTEAL, reap);
709 else
710 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000711 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700714 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static void __wait_on_freeing_inode(struct inode *inode);
718/*
719 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530721static struct inode *find_inode(struct super_block *sb,
722 struct hlist_head *head,
723 int (*test)(struct inode *, void *),
724 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530727 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700730 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100731 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100732 if (inode->i_sb != sb) {
733 spin_unlock(&inode->i_lock);
734 continue;
735 }
736 if (!test(inode, data)) {
737 spin_unlock(&inode->i_lock);
738 continue;
739 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400740 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 __wait_on_freeing_inode(inode);
742 goto repeat;
743 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400744 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100745 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400746 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400748 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
752 * find_inode_fast is the fast path version of find_inode, see the comment at
753 * iget_locked for details.
754 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530755static struct inode *find_inode_fast(struct super_block *sb,
756 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530759 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700762 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100763 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100764 if (inode->i_ino != ino) {
765 spin_unlock(&inode->i_lock);
766 continue;
767 }
768 if (inode->i_sb != sb) {
769 spin_unlock(&inode->i_lock);
770 continue;
771 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400772 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 __wait_on_freeing_inode(inode);
774 goto repeat;
775 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400776 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100777 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400778 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400780 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Eric Dumazetf991bd22010-10-23 11:18:01 -0400783/*
784 * Each cpu owns a range of LAST_INO_BATCH numbers.
785 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
786 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100787 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400788 * This does not significantly increase overflow rate because every CPU can
789 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
790 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
791 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
792 * overflow rate by 2x, which does not seem too significant.
793 *
794 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
795 * error if st_ino won't fit in target struct field. Use 32bit counter
796 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100797 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400798#define LAST_INO_BATCH 1024
799static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100800
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400801unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400802{
803 unsigned int *p = &get_cpu_var(last_ino);
804 unsigned int res = *p;
805
806#ifdef CONFIG_SMP
807 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
808 static atomic_t shared_last_ino;
809 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
810
811 res = next - LAST_INO_BATCH;
812 }
813#endif
814
815 *p = ++res;
816 put_cpu_var(last_ino);
817 return res;
David Chinner8290c352008-10-30 17:35:24 +1100818}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400819EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/**
822 * new_inode - obtain an inode
823 * @sb: superblock
824 *
Mel Gorman769848c2007-07-17 04:03:05 -0700825 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800826 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700827 * If HIGHMEM pages are unsuitable or it is known that pages allocated
828 * for the page cache are not reclaimable or migratable,
829 * mapping_set_gfp_mask() must be called with suitable flags on the
830 * newly created inode's mapping
831 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
833struct inode *new_inode(struct super_block *sb)
834{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530835 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Dave Chinner55fa6092011-03-22 22:23:40 +1100837 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 inode = alloc_inode(sb);
840 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100841 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100843 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100844 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 return inode;
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848EXPORT_SYMBOL(new_inode);
849
Dave Chinner250df6e2011-03-22 22:23:36 +1100850/**
851 * unlock_new_inode - clear the I_NEW state and wake up any waiters
852 * @inode: new inode to unlock
853 *
854 * Called when the inode is fully initialised to clear the new state of the
855 * inode and wake up anyone waiting for the inode to finish initialisation.
856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857void unlock_new_inode(struct inode *inode)
858{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200859#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900860 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200861 struct file_system_type *type = inode->i_sb->s_type;
862
Jan Kara9a7aa122009-06-04 15:26:49 +0200863 /* Set new key only if filesystem hasn't already changed it */
864 if (!lockdep_match_class(&inode->i_mutex,
865 &type->i_mutex_key)) {
866 /*
867 * ensure nobody is actually holding i_mutex
868 */
869 mutex_destroy(&inode->i_mutex);
870 mutex_init(&inode->i_mutex);
871 lockdep_set_class(&inode->i_mutex,
872 &type->i_mutex_dir_key);
873 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200874 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200875#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100876 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100877 WARN_ON(!(inode->i_state & I_NEW));
878 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100879 wake_up_bit(&inode->i_state, __I_NEW);
880 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882EXPORT_SYMBOL(unlock_new_inode);
883
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400884/**
885 * iget5_locked - obtain an inode from a mounted file system
886 * @sb: super block of file system
887 * @hashval: hash value (usually inode number) to get
888 * @test: callback used for comparisons between inodes
889 * @set: callback used to initialize a new struct inode
890 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400892 * Search for the inode specified by @hashval and @data in the inode cache,
893 * and if present it is return it with an increased reference count. This is
894 * a generalized version of iget_locked() for file systems where the inode
895 * number is not sufficient for unique identification of an inode.
896 *
897 * If the inode is not in cache, allocate a new inode and return it locked,
898 * hashed, and with the I_NEW flag set. The file system gets to fill it in
899 * before unlocking it via unlock_new_inode().
900 *
901 * Note both @test and @set are called with the inode_hash_lock held, so can't
902 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400904struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
905 int (*test)(struct inode *, void *),
906 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400908 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530909 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400911 spin_lock(&inode_hash_lock);
912 inode = find_inode(sb, head, test, data);
913 spin_unlock(&inode_hash_lock);
914
915 if (inode) {
916 wait_on_inode(inode);
917 return inode;
918 }
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 inode = alloc_inode(sb);
921 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530922 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dave Chinner67a23c42011-03-22 22:23:42 +1100924 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /* We released the lock, so.. */
926 old = find_inode(sb, head, test, data);
927 if (!old) {
928 if (set(inode, data))
929 goto set_failed;
930
Dave Chinner250df6e2011-03-22 22:23:36 +1100931 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100932 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100933 hlist_add_head(&inode->i_hash, head);
934 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100935 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100936 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /* Return the locked inode with I_NEW set, the
939 * caller is responsible for filling in the contents
940 */
941 return inode;
942 }
943
944 /*
945 * Uhhuh, somebody else created the same inode under
946 * us. Use the old inode instead of the one we just
947 * allocated.
948 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100949 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 destroy_inode(inode);
951 inode = old;
952 wait_on_inode(inode);
953 }
954 return inode;
955
956set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100957 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 destroy_inode(inode);
959 return NULL;
960}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400961EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400963/**
964 * iget_locked - obtain an inode from a mounted file system
965 * @sb: super block of file system
966 * @ino: inode number to get
967 *
968 * Search for the inode specified by @ino in the inode cache and if present
969 * return it with an increased reference count. This is for file systems
970 * where the inode number is sufficient for unique identification of an inode.
971 *
972 * If the inode is not in cache, allocate a new inode and return it locked,
973 * hashed, and with the I_NEW flag set. The file system gets to fill it in
974 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400976struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400978 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530979 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400981 spin_lock(&inode_hash_lock);
982 inode = find_inode_fast(sb, head, ino);
983 spin_unlock(&inode_hash_lock);
984 if (inode) {
985 wait_on_inode(inode);
986 return inode;
987 }
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 inode = alloc_inode(sb);
990 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530991 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Dave Chinner67a23c42011-03-22 22:23:42 +1100993 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* We released the lock, so.. */
995 old = find_inode_fast(sb, head, ino);
996 if (!old) {
997 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +1100998 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100999 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001000 hlist_add_head(&inode->i_hash, head);
1001 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001002 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001003 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /* Return the locked inode with I_NEW set, the
1006 * caller is responsible for filling in the contents
1007 */
1008 return inode;
1009 }
1010
1011 /*
1012 * Uhhuh, somebody else created the same inode under
1013 * us. Use the old inode instead of the one we just
1014 * allocated.
1015 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001016 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 destroy_inode(inode);
1018 inode = old;
1019 wait_on_inode(inode);
1020 }
1021 return inode;
1022}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001023EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001025/*
1026 * search the inode cache for a matching inode number.
1027 * If we find one, then the inode number we are trying to
1028 * allocate is not unique and so we should not use it.
1029 *
1030 * Returns 1 if the inode number is unique, 0 if it is not.
1031 */
1032static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1033{
1034 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1035 struct hlist_node *node;
1036 struct inode *inode;
1037
Dave Chinner67a23c42011-03-22 22:23:42 +11001038 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001039 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001040 if (inode->i_ino == ino && inode->i_sb == sb) {
1041 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001042 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001043 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001044 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001045 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001046
1047 return 1;
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/**
1051 * iunique - get a unique inode number
1052 * @sb: superblock
1053 * @max_reserved: highest reserved inode number
1054 *
1055 * Obtain an inode number that is unique on the system for a given
1056 * superblock. This is used by file systems that have no natural
1057 * permanent inode numbering system. An inode number is returned that
1058 * is higher than the reserved limit but unique.
1059 *
1060 * BUGS:
1061 * With a large number of inodes live on the file system this function
1062 * currently becomes quite slow.
1063 */
1064ino_t iunique(struct super_block *sb, ino_t max_reserved)
1065{
Jeff Layton866b04f2007-05-08 00:32:29 -07001066 /*
1067 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1068 * error if st_ino won't fit in target struct field. Use 32bit counter
1069 * here to attempt to avoid that.
1070 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001071 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001072 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001075 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001076 do {
1077 if (counter <= max_reserved)
1078 counter = max_reserved + 1;
1079 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001080 } while (!test_inode_iunique(sb, res));
1081 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001082
1083 return res;
1084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085EXPORT_SYMBOL(iunique);
1086
1087struct inode *igrab(struct inode *inode)
1088{
Dave Chinner250df6e2011-03-22 22:23:36 +11001089 spin_lock(&inode->i_lock);
1090 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001092 spin_unlock(&inode->i_lock);
1093 } else {
1094 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /*
1096 * Handle the case where s_op->clear_inode is not been
1097 * called yet, and somebody is calling igrab
1098 * while the inode is getting freed.
1099 */
1100 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return inode;
1103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104EXPORT_SYMBOL(igrab);
1105
1106/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001107 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * @sb: super block of file system to search
1109 * @hashval: hash value (usually inode number) to search for
1110 * @test: callback used for comparisons between inodes
1111 * @data: opaque data pointer to pass to @test
1112 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001113 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001115 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001116 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001117 * Note: I_NEW is not waited upon so you have to be very careful what you do
1118 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001119 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001120 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001121 */
1122struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1123 int (*test)(struct inode *, void *), void *data)
1124{
1125 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001126 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001127
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001128 spin_lock(&inode_hash_lock);
1129 inode = find_inode(sb, head, test, data);
1130 spin_unlock(&inode_hash_lock);
1131
1132 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001133}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001134EXPORT_SYMBOL(ilookup5_nowait);
1135
1136/**
1137 * ilookup5 - search for an inode in the inode cache
1138 * @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,
1144 * and if the inode is in the cache, return the inode with an incremented
1145 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001146 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001148 * This is a generalized version of ilookup() for file systems where the
1149 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001151 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
1153struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1154 int (*test)(struct inode *, void *), void *data)
1155{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001156 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001158 if (inode)
1159 wait_on_inode(inode);
1160 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162EXPORT_SYMBOL(ilookup5);
1163
1164/**
1165 * ilookup - search for an inode in the inode cache
1166 * @sb: super block of file system to search
1167 * @ino: inode number to search for
1168 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001169 * Search for the inode @ino in the inode cache, and if the inode is in the
1170 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
1172struct inode *ilookup(struct super_block *sb, unsigned long ino)
1173{
1174 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001175 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001177 spin_lock(&inode_hash_lock);
1178 inode = find_inode_fast(sb, head, ino);
1179 spin_unlock(&inode_hash_lock);
1180
1181 if (inode)
1182 wait_on_inode(inode);
1183 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185EXPORT_SYMBOL(ilookup);
1186
Al Viro261bca82008-12-30 01:48:21 -05001187int insert_inode_locked(struct inode *inode)
1188{
1189 struct super_block *sb = inode->i_sb;
1190 ino_t ino = inode->i_ino;
1191 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001192
Al Viro261bca82008-12-30 01:48:21 -05001193 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001194 struct hlist_node *node;
1195 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001196 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001197 hlist_for_each_entry(old, node, head, i_hash) {
1198 if (old->i_ino != ino)
1199 continue;
1200 if (old->i_sb != sb)
1201 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001202 spin_lock(&old->i_lock);
1203 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1204 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001205 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001206 }
Al Viro72a43d62009-05-13 19:13:40 +01001207 break;
1208 }
1209 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001210 spin_lock(&inode->i_lock);
1211 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001212 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001213 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001214 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001215 return 0;
1216 }
1217 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001218 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001219 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001220 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001221 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001222 iput(old);
1223 return -EBUSY;
1224 }
1225 iput(old);
1226 }
1227}
Al Viro261bca82008-12-30 01:48:21 -05001228EXPORT_SYMBOL(insert_inode_locked);
1229
1230int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1231 int (*test)(struct inode *, void *), void *data)
1232{
1233 struct super_block *sb = inode->i_sb;
1234 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001235
Al Viro261bca82008-12-30 01:48:21 -05001236 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001237 struct hlist_node *node;
1238 struct inode *old = NULL;
1239
Dave Chinner67a23c42011-03-22 22:23:42 +11001240 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001241 hlist_for_each_entry(old, node, head, i_hash) {
1242 if (old->i_sb != sb)
1243 continue;
1244 if (!test(old, data))
1245 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001246 spin_lock(&old->i_lock);
1247 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1248 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001249 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001250 }
Al Viro72a43d62009-05-13 19:13:40 +01001251 break;
1252 }
1253 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001254 spin_lock(&inode->i_lock);
1255 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001256 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001257 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001258 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001259 return 0;
1260 }
1261 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001262 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001263 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001264 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001265 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001266 iput(old);
1267 return -EBUSY;
1268 }
1269 iput(old);
1270 }
1271}
Al Viro261bca82008-12-30 01:48:21 -05001272EXPORT_SYMBOL(insert_inode_locked4);
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Al Viro45321ac2010-06-07 13:43:19 -04001275int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Al Viro45321ac2010-06-07 13:43:19 -04001277 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279EXPORT_SYMBOL(generic_delete_inode);
1280
Al Viro45321ac2010-06-07 13:43:19 -04001281/*
1282 * Normal UNIX filesystem behaviour: delete the
1283 * inode when the usage count drops to zero, and
1284 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001285 */
Al Viro45321ac2010-06-07 13:43:19 -04001286int generic_drop_inode(struct inode *inode)
1287{
Al Viro1d3382cb2010-10-23 15:19:20 -04001288 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001289}
1290EXPORT_SYMBOL_GPL(generic_drop_inode);
1291
1292/*
1293 * Called when we're dropping the last reference
1294 * to an inode.
1295 *
1296 * Call the FS "drop_inode()" function, defaulting to
1297 * the legacy UNIX filesystem behaviour. If it tells
1298 * us to evict inode, do so. Otherwise, retain inode
1299 * in cache if fs is alive, sync and evict if fs is
1300 * shutting down.
1301 */
1302static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001305 const struct super_operations *op = inode->i_sb->s_op;
1306 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Dave Chinner250df6e2011-03-22 22:23:36 +11001308 WARN_ON(inode->i_state & I_NEW);
1309
Al Viroe7f59092011-07-07 15:45:59 -04001310 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001311 drop = op->drop_inode(inode);
1312 else
1313 drop = generic_drop_inode(inode);
1314
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001315 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1316 inode->i_state |= I_REFERENCED;
1317 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1318 inode_lru_list_add(inode);
1319 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001320 return;
1321 }
1322
Al Viro45321ac2010-06-07 13:43:19 -04001323 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001324 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001325 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001327 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001328 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001329 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001331
Alexander Viro991114c2005-06-23 00:09:01 -07001332 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001333 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001334 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001335
1336 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301340 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * @inode: inode to put
1342 *
1343 * Puts an inode, dropping its usage count. If the inode use count hits
1344 * zero, the inode is then freed and may also be destroyed.
1345 *
1346 * Consequently, iput() can sleep.
1347 */
1348void iput(struct inode *inode)
1349{
1350 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001351 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Dave Chinnerf283c862011-03-22 22:23:39 +11001353 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 iput_final(inode);
1355 }
1356}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357EXPORT_SYMBOL(iput);
1358
1359/**
1360 * bmap - find a block number in a file
1361 * @inode: inode of file
1362 * @block: block to find
1363 *
1364 * Returns the block number on the device holding the inode that
1365 * is the disk block number for the block of the file requested.
1366 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301367 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * file.
1369 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301370sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 sector_t res = 0;
1373 if (inode->i_mapping->a_ops->bmap)
1374 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1375 return res;
1376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377EXPORT_SYMBOL(bmap);
1378
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001379/*
1380 * With relative atime, only update atime if the previous atime is
1381 * earlier than either the ctime or mtime or if at least a day has
1382 * passed since the last atime update.
1383 */
1384static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1385 struct timespec now)
1386{
1387
1388 if (!(mnt->mnt_flags & MNT_RELATIME))
1389 return 1;
1390 /*
1391 * Is mtime younger than atime? If yes, update atime:
1392 */
1393 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1394 return 1;
1395 /*
1396 * Is ctime younger than atime? If yes, update atime:
1397 */
1398 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1399 return 1;
1400
1401 /*
1402 * Is the previous atime value older than a day? If yes,
1403 * update atime:
1404 */
1405 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1406 return 1;
1407 /*
1408 * Good, we can skip the atime update:
1409 */
1410 return 0;
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001414 * touch_atime - update the access time
1415 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001416 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 *
1418 * Update the accessed time on an inode and mark it for writeback.
1419 * This function automatically handles read only file systems and media,
1420 * as well as the "noatime" flag and inode specific "noatime" markers.
1421 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001422void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001424 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct timespec now;
1426
Andrew Mortonb2276132006-12-13 00:34:33 -08001427 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001428 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001429 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001430 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001431 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001432 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001433
Dave Hansencdb70f32008-02-15 14:37:41 -08001434 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001435 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001436 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001437 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001440
1441 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001442 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001443
Valerie Henson47ae32d2006-12-13 00:34:34 -08001444 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001445 return;
1446
1447 if (mnt_want_write(mnt))
1448 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001449
1450 inode->i_atime = now;
1451 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001452 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001454EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001457 * file_update_time - update mtime and ctime time
1458 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001460 * Update the mtime and ctime members of an inode and mark the inode
1461 * for writeback. Note that this function is meant exclusively for
1462 * usage in the file write path of filesystems, and filesystems may
1463 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001464 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001465 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 */
1467
Christoph Hellwig870f4812006-01-09 20:52:01 -08001468void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001470 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001472 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Andi Kleence06e0b2009-09-18 13:05:48 -07001474 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (IS_NOCMTIME(inode))
1476 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001477
Andi Kleence06e0b2009-09-18 13:05:48 -07001478 now = current_fs_time(inode->i_sb);
1479 if (!timespec_equal(&inode->i_mtime, &now))
1480 sync_it = S_MTIME;
1481
1482 if (!timespec_equal(&inode->i_ctime, &now))
1483 sync_it |= S_CTIME;
1484
1485 if (IS_I_VERSION(inode))
1486 sync_it |= S_VERSION;
1487
1488 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
1490
Andi Kleence06e0b2009-09-18 13:05:48 -07001491 /* Finally allowed to write? Takes lock. */
1492 if (mnt_want_write_file(file))
1493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andi Kleence06e0b2009-09-18 13:05:48 -07001495 /* Only change inode inside the lock region */
1496 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001497 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001498 if (sync_it & S_CTIME)
1499 inode->i_ctime = now;
1500 if (sync_it & S_MTIME)
1501 inode->i_mtime = now;
1502 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001503 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001505EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507int inode_needs_sync(struct inode *inode)
1508{
1509 if (IS_SYNC(inode))
1510 return 1;
1511 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1512 return 1;
1513 return 0;
1514}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515EXPORT_SYMBOL(inode_needs_sync);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517int inode_wait(void *word)
1518{
1519 schedule();
1520 return 0;
1521}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001522EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001525 * If we try to find an inode in the inode hash while it is being
1526 * deleted, we have to wait until the filesystem completes its
1527 * deletion before reporting that it isn't found. This function waits
1528 * until the deletion _might_ have completed. Callers are responsible
1529 * to recheck inode state.
1530 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001531 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001532 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1533 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 */
1535static void __wait_on_freeing_inode(struct inode *inode)
1536{
1537 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001538 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1539 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001541 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001542 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 schedule();
1544 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001545 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548static __initdata unsigned long ihash_entries;
1549static int __init set_ihash_entries(char *str)
1550{
1551 if (!str)
1552 return 0;
1553 ihash_entries = simple_strtoul(str, &str, 0);
1554 return 1;
1555}
1556__setup("ihash_entries=", set_ihash_entries);
1557
1558/*
1559 * Initialize the waitqueues and inode hash table.
1560 */
1561void __init inode_init_early(void)
1562{
1563 int loop;
1564
1565 /* If hashes are distributed across NUMA nodes, defer
1566 * hash allocation until vmalloc space is available.
1567 */
1568 if (hashdist)
1569 return;
1570
1571 inode_hashtable =
1572 alloc_large_system_hash("Inode-cache",
1573 sizeof(struct hlist_head),
1574 ihash_entries,
1575 14,
1576 HASH_EARLY,
1577 &i_hash_shift,
1578 &i_hash_mask,
1579 0);
1580
1581 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1582 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1583}
1584
Denis Cheng74bf17c2007-10-16 23:26:30 -07001585void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 int loop;
1588
1589 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001590 inode_cachep = kmem_cache_create("inode_cache",
1591 sizeof(struct inode),
1592 0,
1593 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1594 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001595 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /* Hash may have been set up in inode_init_early */
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 0,
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
1615void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1616{
1617 inode->i_mode = mode;
1618 if (S_ISCHR(mode)) {
1619 inode->i_fop = &def_chr_fops;
1620 inode->i_rdev = rdev;
1621 } else if (S_ISBLK(mode)) {
1622 inode->i_fop = &def_blk_fops;
1623 inode->i_rdev = rdev;
1624 } else if (S_ISFIFO(mode))
1625 inode->i_fop = &def_fifo_fops;
1626 else if (S_ISSOCK(mode))
1627 inode->i_fop = &bad_sock_fops;
1628 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001629 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1630 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1631 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001634
1635/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00001636 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001637 * @inode: New inode
1638 * @dir: Directory inode
1639 * @mode: mode of the new inode
1640 */
1641void inode_init_owner(struct inode *inode, const struct inode *dir,
1642 mode_t mode)
1643{
1644 inode->i_uid = current_fsuid();
1645 if (dir && dir->i_mode & S_ISGID) {
1646 inode->i_gid = dir->i_gid;
1647 if (S_ISDIR(mode))
1648 mode |= S_ISGID;
1649 } else
1650 inode->i_gid = current_fsgid();
1651 inode->i_mode = mode;
1652}
1653EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001654
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001655/**
1656 * inode_owner_or_capable - check current task permissions to inode
1657 * @inode: inode being checked
1658 *
1659 * Return true if current either has CAP_FOWNER to the inode, or
1660 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001661 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001662bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001663{
1664 struct user_namespace *ns = inode_userns(inode);
1665
1666 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1667 return true;
1668 if (ns_capable(ns, CAP_FOWNER))
1669 return true;
1670 return false;
1671}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001672EXPORT_SYMBOL(inode_owner_or_capable);