blob: 4be128cbc7543e36d33ad86304fda15ffe7851fe [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
Christoph Hellwigf758eea2011-04-21 18:19:44 -060040 * bdi->wb.list_lock protects:
Dave Chinnera66979a2011-03-22 22:23:41 +110041 * 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 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060051 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110052 * 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);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +010073 * iprune_sem provides exclusion between the icache shrinking and the
74 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -070075 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +010076 * We don't actually need it to protect anything in the umount path,
77 * but only need to cycle through it to make sure any inode that
78 * prune_icache took off the LRU list has been fully torn down by the
79 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -070081static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020084 * Empty aops. Can be used for the cases where the user does not
85 * define any of the address_space operations.
86 */
87const struct address_space_operations empty_aops = {
88};
89EXPORT_SYMBOL(empty_aops);
90
91/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * Statistics gathering..
93 */
94struct inodes_stat_t inodes_stat;
95
Nick Piggin3e880fb2011-01-07 17:49:19 +110096static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040097
Manish Katiyar6b3304b2009-03-31 19:35:54 +053098static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Nick Piggin3e880fb2011-01-07 17:49:19 +1100100static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400101{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100102 int i;
103 int sum = 0;
104 for_each_possible_cpu(i)
105 sum += per_cpu(nr_inodes, i);
106 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400107}
108
109static inline int get_nr_inodes_unused(void)
110{
Nick Piggin86c87492011-01-07 17:49:18 +1100111 return inodes_stat.nr_unused;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400112}
113
114int get_nr_dirty_inodes(void)
115{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100116 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400117 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
118 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400119}
120
121/*
122 * Handle nr_inode sysctl
123 */
124#ifdef CONFIG_SYSCTL
125int proc_nr_inodes(ctl_table *table, int write,
126 void __user *buffer, size_t *lenp, loff_t *ppos)
127{
128 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400129 return proc_dointvec(table, write, buffer, lenp, ppos);
130}
131#endif
132
David Chinner2cb15992008-10-30 17:32:23 +1100133/**
134 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800135 * @sb: superblock inode belongs to
136 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100137 *
138 * These are initializations that need to be done on every inode
139 * allocation as the fields are not initialised by slab allocation.
140 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300141int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700143 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800144 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530145 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100146
147 inode->i_sb = sb;
148 inode->i_blkbits = sb->s_blocksize_bits;
149 inode->i_flags = 0;
150 atomic_set(&inode->i_count, 1);
151 inode->i_op = &empty_iops;
152 inode->i_fop = &empty_fops;
153 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500154 inode->i_uid = 0;
155 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100156 atomic_set(&inode->i_writecount, 0);
157 inode->i_size = 0;
158 inode->i_blocks = 0;
159 inode->i_bytes = 0;
160 inode->i_generation = 0;
161#ifdef CONFIG_QUOTA
162 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
163#endif
164 inode->i_pipe = NULL;
165 inode->i_bdev = NULL;
166 inode->i_cdev = NULL;
167 inode->i_rdev = 0;
168 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500169
170 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300171 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100172 spin_lock_init(&inode->i_lock);
173 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
174
175 mutex_init(&inode->i_mutex);
176 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
177
178 init_rwsem(&inode->i_alloc_sem);
179 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
180
181 mapping->a_ops = &empty_aops;
182 mapping->host = inode;
183 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800184 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100185 mapping->assoc_mapping = NULL;
186 mapping->backing_dev_info = &default_backing_dev_info;
187 mapping->writeback_index = 0;
188
189 /*
190 * If the block_device provides a backing_dev_info for client
191 * inodes then use that. Otherwise the inode share the bdev's
192 * backing_dev_info.
193 */
194 if (sb->s_bdev) {
195 struct backing_dev_info *bdi;
196
Jens Axboe2c96ce92009-09-15 09:43:56 +0200197 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100198 mapping->backing_dev_info = bdi;
199 }
200 inode->i_private = NULL;
201 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400202#ifdef CONFIG_FS_POSIX_ACL
203 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
204#endif
David Chinner2cb15992008-10-30 17:32:23 +1100205
Eric Paris3be25f42009-05-21 17:01:26 -0400206#ifdef CONFIG_FSNOTIFY
207 inode->i_fsnotify_mask = 0;
208#endif
209
Nick Piggin3e880fb2011-01-07 17:49:19 +1100210 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400211
Christoph Hellwig54e34622009-08-07 14:38:25 -0300212 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300213out:
214 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100215}
216EXPORT_SYMBOL(inode_init_always);
217
218static struct inode *alloc_inode(struct super_block *sb)
219{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct inode *inode;
221
222 if (sb->s_op->alloc_inode)
223 inode = sb->s_op->alloc_inode(sb);
224 else
David Chinner2cb15992008-10-30 17:32:23 +1100225 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Christoph Hellwig54e34622009-08-07 14:38:25 -0300227 if (!inode)
228 return NULL;
229
230 if (unlikely(inode_init_always(sb, inode))) {
231 if (inode->i_sb->s_op->destroy_inode)
232 inode->i_sb->s_op->destroy_inode(inode);
233 else
234 kmem_cache_free(inode_cachep, inode);
235 return NULL;
236 }
237
238 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Nick Pigginff0c7d12011-01-07 17:49:50 +1100241void free_inode_nonrcu(struct inode *inode)
242{
243 kmem_cache_free(inode_cachep, inode);
244}
245EXPORT_SYMBOL(free_inode_nonrcu);
246
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300247void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200249 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400251 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400252#ifdef CONFIG_FS_POSIX_ACL
253 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
254 posix_acl_release(inode->i_acl);
255 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
256 posix_acl_release(inode->i_default_acl);
257#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100258 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300259}
260EXPORT_SYMBOL(__destroy_inode);
261
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100262static void i_callback(struct rcu_head *head)
263{
264 struct inode *inode = container_of(head, struct inode, i_rcu);
265 INIT_LIST_HEAD(&inode->i_dentry);
266 kmem_cache_free(inode_cachep, inode);
267}
268
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200269static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300270{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100271 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300272 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (inode->i_sb->s_op->destroy_inode)
274 inode->i_sb->s_op->destroy_inode(inode);
275 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100276 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100279void address_space_init_once(struct address_space *mapping)
280{
281 memset(mapping, 0, sizeof(*mapping));
282 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
283 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700284 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100285 INIT_LIST_HEAD(&mapping->private_list);
286 spin_lock_init(&mapping->private_lock);
287 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
288 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100289}
290EXPORT_SYMBOL(address_space_init_once);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * These are initializations that only need to be done
294 * once, because the fields are idempotent across use
295 * of the inode, so let the slab aware of that.
296 */
297void inode_init_once(struct inode *inode)
298{
299 memset(inode, 0, sizeof(*inode));
300 INIT_HLIST_NODE(&inode->i_hash);
301 INIT_LIST_HEAD(&inode->i_dentry);
302 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100303 INIT_LIST_HEAD(&inode->i_wb_list);
304 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100305 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400307#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500308 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400309#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311EXPORT_SYMBOL(inode_init_once);
312
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700313static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530315 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Christoph Lametera35afb82007-05-16 22:10:57 -0700317 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100321 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530323void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Nick Piggin9e38d862010-10-23 06:55:17 -0400325 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Al Viro7de9c6ee2010-10-23 11:11:40 -0400328/*
329 * get additional reference to inode; caller must already hold one.
330 */
331void ihold(struct inode *inode)
332{
333 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
334}
335EXPORT_SYMBOL(ihold);
336
Nick Piggin9e38d862010-10-23 06:55:17 -0400337static void inode_lru_list_add(struct inode *inode)
338{
Dave Chinner02afc412011-03-22 22:23:38 +1100339 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100340 if (list_empty(&inode->i_lru)) {
341 list_add(&inode->i_lru, &inode_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100342 inodes_stat.nr_unused++;
Nick Piggin9e38d862010-10-23 06:55:17 -0400343 }
Dave Chinner02afc412011-03-22 22:23:38 +1100344 spin_unlock(&inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400345}
346
347static void inode_lru_list_del(struct inode *inode)
348{
Dave Chinner02afc412011-03-22 22:23:38 +1100349 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100350 if (!list_empty(&inode->i_lru)) {
351 list_del_init(&inode->i_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100352 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400353 }
Dave Chinner02afc412011-03-22 22:23:38 +1100354 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Christoph Hellwig646ec462010-10-23 07:15:32 -0400357/**
358 * inode_sb_list_add - add inode to the superblock list of inodes
359 * @inode: inode to add
360 */
361void inode_sb_list_add(struct inode *inode)
362{
Dave Chinner55fa6092011-03-22 22:23:40 +1100363 spin_lock(&inode_sb_list_lock);
364 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
365 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400366}
367EXPORT_SYMBOL_GPL(inode_sb_list_add);
368
Dave Chinner55fa6092011-03-22 22:23:40 +1100369static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400370{
Dave Chinner55fa6092011-03-22 22:23:40 +1100371 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400372 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100373 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400374}
375
Dave Chinner4c51acb2010-10-23 06:58:09 -0400376static unsigned long hash(struct super_block *sb, unsigned long hashval)
377{
378 unsigned long tmp;
379
380 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
381 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400382 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
383 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400384}
385
386/**
387 * __insert_inode_hash - hash an inode
388 * @inode: unhashed inode
389 * @hashval: unsigned long value used to locate this object in the
390 * inode_hashtable.
391 *
392 * Add an inode to the inode hash for this superblock.
393 */
394void __insert_inode_hash(struct inode *inode, unsigned long hashval)
395{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400396 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
397
Dave Chinner67a23c42011-03-22 22:23:42 +1100398 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100399 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400400 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100401 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100402 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400403}
404EXPORT_SYMBOL(__insert_inode_hash);
405
406/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400407 * remove_inode_hash - remove an inode from the hash
408 * @inode: inode to unhash
409 *
410 * Remove an inode from the superblock.
411 */
412void remove_inode_hash(struct inode *inode)
413{
Dave Chinner67a23c42011-03-22 22:23:42 +1100414 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100415 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400416 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100417 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100418 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400419}
420EXPORT_SYMBOL(remove_inode_hash);
421
Al Virob0683aa2010-06-04 20:55:25 -0400422void end_writeback(struct inode *inode)
423{
424 might_sleep();
425 BUG_ON(inode->i_data.nrpages);
426 BUG_ON(!list_empty(&inode->i_data.private_list));
427 BUG_ON(!(inode->i_state & I_FREEING));
428 BUG_ON(inode->i_state & I_CLEAR);
429 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100430 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400431 inode->i_state = I_FREEING | I_CLEAR;
432}
433EXPORT_SYMBOL(end_writeback);
434
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100435/*
436 * Free the inode passed in, removing it from the lists it is still connected
437 * to. We remove any pages still attached to the inode and wait for any IO that
438 * is still in progress before finally destroying the inode.
439 *
440 * An inode must already be marked I_FREEING so that we avoid the inode being
441 * moved back onto lists if we race with other code that manipulates the lists
442 * (e.g. writeback_single_inode). The caller is responsible for setting this.
443 *
444 * An inode must already be removed from the LRU list before being evicted from
445 * the cache. This should occur atomically with setting the I_FREEING state
446 * flag, so no inodes here should ever be on the LRU when being evicted.
447 */
Al Viro644da592010-06-07 13:21:05 -0400448static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400449{
450 const struct super_operations *op = inode->i_sb->s_op;
451
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100452 BUG_ON(!(inode->i_state & I_FREEING));
453 BUG_ON(!list_empty(&inode->i_lru));
454
Dave Chinnera66979a2011-03-22 22:23:41 +1100455 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100456 inode_sb_list_del(inode);
457
Al Virobe7ce412010-06-04 19:40:39 -0400458 if (op->evict_inode) {
459 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400460 } else {
461 if (inode->i_data.nrpages)
462 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400463 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400464 }
Al Viro661074e2010-06-04 20:19:55 -0400465 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
466 bd_forget(inode);
467 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
468 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100469
470 remove_inode_hash(inode);
471
472 spin_lock(&inode->i_lock);
473 wake_up_bit(&inode->i_state, __I_NEW);
474 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
475 spin_unlock(&inode->i_lock);
476
477 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * dispose_list - dispose of the contents of a local list
482 * @head: the head of the list to free
483 *
484 * Dispose-list gets a local list with local inodes in it, so it doesn't
485 * need to worry about list corruption and SMP locks.
486 */
487static void dispose_list(struct list_head *head)
488{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 while (!list_empty(head)) {
490 struct inode *inode;
491
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100492 inode = list_first_entry(head, struct inode, i_lru);
493 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Al Viro644da592010-06-07 13:21:05 -0400495 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/**
Al Viro63997e92010-10-25 20:49:35 -0400500 * evict_inodes - evict all evictable inodes for a superblock
501 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
Al Viro63997e92010-10-25 20:49:35 -0400503 * Make sure that no inodes with zero refcount are retained. This is
504 * called by superblock shutdown after having MS_ACTIVE flag removed,
505 * so any inode reaching zero refcount during or after that call will
506 * be immediately evicted.
507 */
508void evict_inodes(struct super_block *sb)
509{
510 struct inode *inode, *next;
511 LIST_HEAD(dispose);
512
Dave Chinner55fa6092011-03-22 22:23:40 +1100513 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400514 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
515 if (atomic_read(&inode->i_count))
516 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100517
518 spin_lock(&inode->i_lock);
519 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
520 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400521 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100522 }
Al Viro63997e92010-10-25 20:49:35 -0400523
524 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100525 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100526 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100527 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400528 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100529 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400530
531 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100532
533 /*
534 * Cycle through iprune_sem to make sure any inode that prune_icache
535 * moved off the list before we took the lock has been fully torn
536 * down.
537 */
538 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400539 up_write(&iprune_sem);
540}
541
542/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200543 * invalidate_inodes - attempt to free all inodes on a superblock
544 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100545 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200546 *
547 * Attempts to free all inodes for a given superblock. If there were any
548 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100549 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
550 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
NeilBrown93b270f2011-02-24 17:25:47 +1100552int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400554 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200555 struct inode *inode, *next;
556 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Dave Chinner55fa6092011-03-22 22:23:40 +1100558 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200559 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100560 spin_lock(&inode->i_lock);
561 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
562 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100564 }
NeilBrown93b270f2011-02-24 17:25:47 +1100565 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100566 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100567 busy = 1;
568 continue;
569 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200570 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100571 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200572 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 continue;
574 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200575
Christoph Hellwig99a38912010-10-23 19:07:20 +0200576 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100577 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100578 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100579 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100581 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Christoph Hellwiga0318782010-10-24 19:40:33 +0200583 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 return busy;
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588static int can_unuse(struct inode *inode)
589{
Nick Piggin9e38d862010-10-23 06:55:17 -0400590 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
592 if (inode_has_buffers(inode))
593 return 0;
594 if (atomic_read(&inode->i_count))
595 return 0;
596 if (inode->i_data.nrpages)
597 return 0;
598 return 1;
599}
600
601/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400602 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100603 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 *
605 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400606 * pagecache removed. If the inode has metadata buffers attached to
607 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400609 * If the inode has the I_REFERENCED flag set, then it means that it has been
610 * used recently - the flag is set in iput_final(). When we encounter such an
611 * inode, clear the flag and move it to the back of the LRU so it gets another
612 * pass through the LRU before it gets reclaimed. This is necessary because of
613 * the fact we are doing lazy LRU updates to minimise lock contention so the
614 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
615 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
617static void prune_icache(int nr_to_scan)
618{
619 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int nr_scanned;
621 unsigned long reap = 0;
622
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700623 down_read(&iprune_sem);
Dave Chinner02afc412011-03-22 22:23:38 +1100624 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
626 struct inode *inode;
627
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100628 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
630
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100631 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Nick Piggin9e38d862010-10-23 06:55:17 -0400633 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100634 * we are inverting the inode_lru_lock/inode->i_lock here,
635 * so use a trylock. If we fail to get the lock, just move the
636 * inode to the back of the list so we don't spin on it.
637 */
638 if (!spin_trylock(&inode->i_lock)) {
639 list_move(&inode->i_lru, &inode_lru);
640 continue;
641 }
642
643 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400644 * Referenced or dirty inodes are still in use. Give them
645 * another pass through the LRU as we canot reclaim them now.
646 */
647 if (atomic_read(&inode->i_count) ||
648 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100649 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100650 spin_unlock(&inode->i_lock);
Nick Piggin86c87492011-01-07 17:49:18 +1100651 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400652 continue;
653 }
654
655 /* recently referenced inodes get one more pass */
656 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400657 inode->i_state &= ~I_REFERENCED;
Dave Chinner250df6e2011-03-22 22:23:36 +1100658 list_move(&inode->i_lru, &inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100659 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 continue;
661 }
662 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
663 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100664 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100665 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800667 reap += invalidate_mapping_pages(&inode->i_data,
668 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100670 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100672 if (inode != list_entry(inode_lru.next,
673 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100675 /* avoid lock inversions with trylock */
676 if (!spin_trylock(&inode->i_lock))
677 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100678 if (!can_unuse(inode)) {
679 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700683 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100685 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100686
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100687 list_move(&inode->i_lru, &freeable);
Nick Piggin86c87492011-01-07 17:49:18 +1100688 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700690 if (current_is_kswapd())
691 __count_vm_events(KSWAPD_INODESTEAL, reap);
692 else
693 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100694 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700697 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/*
701 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
702 * "unused" means that no dentries are referring to the inodes: the files are
703 * not open and the dcache references to those inodes have already been
704 * reclaimed.
705 *
706 * This function is passed the number of inodes to scan, and it returns the
707 * total number of remaining possibly-reclaimable inodes.
708 */
Ying Han1495f232011-05-24 17:12:27 -0700709static int shrink_icache_memory(struct shrinker *shrink,
710 struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Ying Han1495f232011-05-24 17:12:27 -0700712 int nr = sc->nr_to_scan;
713 gfp_t gfp_mask = sc->gfp_mask;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (nr) {
716 /*
717 * Nasty deadlock avoidance. We may hold various FS locks,
718 * and we don't want to recurse into the FS that called us
719 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!(gfp_mask & __GFP_FS))
722 return -1;
723 prune_icache(nr);
724 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400725 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Rusty Russell8e1f9362007-07-17 04:03:17 -0700728static struct shrinker icache_shrinker = {
729 .shrink = shrink_icache_memory,
730 .seeks = DEFAULT_SEEKS,
731};
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static void __wait_on_freeing_inode(struct inode *inode);
734/*
735 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530737static struct inode *find_inode(struct super_block *sb,
738 struct hlist_head *head,
739 int (*test)(struct inode *, void *),
740 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530743 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700746 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100747 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100748 if (inode->i_sb != sb) {
749 spin_unlock(&inode->i_lock);
750 continue;
751 }
752 if (!test(inode, data)) {
753 spin_unlock(&inode->i_lock);
754 continue;
755 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400756 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 __wait_on_freeing_inode(inode);
758 goto repeat;
759 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400760 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100761 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400762 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400764 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/*
768 * find_inode_fast is the fast path version of find_inode, see the comment at
769 * iget_locked for details.
770 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530771static struct inode *find_inode_fast(struct super_block *sb,
772 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530775 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700778 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100779 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100780 if (inode->i_ino != ino) {
781 spin_unlock(&inode->i_lock);
782 continue;
783 }
784 if (inode->i_sb != sb) {
785 spin_unlock(&inode->i_lock);
786 continue;
787 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400788 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 __wait_on_freeing_inode(inode);
790 goto repeat;
791 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400792 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100793 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400794 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400796 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Eric Dumazetf991bd22010-10-23 11:18:01 -0400799/*
800 * Each cpu owns a range of LAST_INO_BATCH numbers.
801 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
802 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100803 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400804 * This does not significantly increase overflow rate because every CPU can
805 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
806 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
807 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
808 * overflow rate by 2x, which does not seem too significant.
809 *
810 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
811 * error if st_ino won't fit in target struct field. Use 32bit counter
812 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100813 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400814#define LAST_INO_BATCH 1024
815static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100816
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400817unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400818{
819 unsigned int *p = &get_cpu_var(last_ino);
820 unsigned int res = *p;
821
822#ifdef CONFIG_SMP
823 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
824 static atomic_t shared_last_ino;
825 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
826
827 res = next - LAST_INO_BATCH;
828 }
829#endif
830
831 *p = ++res;
832 put_cpu_var(last_ino);
833 return res;
David Chinner8290c352008-10-30 17:35:24 +1100834}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400835EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/**
838 * new_inode - obtain an inode
839 * @sb: superblock
840 *
Mel Gorman769848c2007-07-17 04:03:05 -0700841 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800842 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700843 * If HIGHMEM pages are unsuitable or it is known that pages allocated
844 * for the page cache are not reclaimable or migratable,
845 * mapping_set_gfp_mask() must be called with suitable flags on the
846 * newly created inode's mapping
847 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
849struct inode *new_inode(struct super_block *sb)
850{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530851 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dave Chinner55fa6092011-03-22 22:23:40 +1100853 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 inode = alloc_inode(sb);
856 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100857 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100859 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100860 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 return inode;
863}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864EXPORT_SYMBOL(new_inode);
865
Dave Chinner250df6e2011-03-22 22:23:36 +1100866/**
867 * unlock_new_inode - clear the I_NEW state and wake up any waiters
868 * @inode: new inode to unlock
869 *
870 * Called when the inode is fully initialised to clear the new state of the
871 * inode and wake up anyone waiting for the inode to finish initialisation.
872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873void unlock_new_inode(struct inode *inode)
874{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200875#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900876 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200877 struct file_system_type *type = inode->i_sb->s_type;
878
Jan Kara9a7aa122009-06-04 15:26:49 +0200879 /* Set new key only if filesystem hasn't already changed it */
880 if (!lockdep_match_class(&inode->i_mutex,
881 &type->i_mutex_key)) {
882 /*
883 * ensure nobody is actually holding i_mutex
884 */
885 mutex_destroy(&inode->i_mutex);
886 mutex_init(&inode->i_mutex);
887 lockdep_set_class(&inode->i_mutex,
888 &type->i_mutex_dir_key);
889 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200890 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200891#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100892 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100893 WARN_ON(!(inode->i_state & I_NEW));
894 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100895 wake_up_bit(&inode->i_state, __I_NEW);
896 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898EXPORT_SYMBOL(unlock_new_inode);
899
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400900/**
901 * iget5_locked - obtain an inode from a mounted file system
902 * @sb: super block of file system
903 * @hashval: hash value (usually inode number) to get
904 * @test: callback used for comparisons between inodes
905 * @set: callback used to initialize a new struct inode
906 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400908 * Search for the inode specified by @hashval and @data in the inode cache,
909 * and if present it is return it with an increased reference count. This is
910 * a generalized version of iget_locked() for file systems where the inode
911 * number is not sufficient for unique identification of an inode.
912 *
913 * If the inode is not in cache, allocate a new inode and return it locked,
914 * hashed, and with the I_NEW flag set. The file system gets to fill it in
915 * before unlocking it via unlock_new_inode().
916 *
917 * Note both @test and @set are called with the inode_hash_lock held, so can't
918 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400920struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
921 int (*test)(struct inode *, void *),
922 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400924 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530925 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400927 spin_lock(&inode_hash_lock);
928 inode = find_inode(sb, head, test, data);
929 spin_unlock(&inode_hash_lock);
930
931 if (inode) {
932 wait_on_inode(inode);
933 return inode;
934 }
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 inode = alloc_inode(sb);
937 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530938 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Dave Chinner67a23c42011-03-22 22:23:42 +1100940 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* We released the lock, so.. */
942 old = find_inode(sb, head, test, data);
943 if (!old) {
944 if (set(inode, data))
945 goto set_failed;
946
Dave Chinner250df6e2011-03-22 22:23:36 +1100947 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100948 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100949 hlist_add_head(&inode->i_hash, head);
950 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100951 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100952 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /* Return the locked inode with I_NEW set, the
955 * caller is responsible for filling in the contents
956 */
957 return inode;
958 }
959
960 /*
961 * Uhhuh, somebody else created the same inode under
962 * us. Use the old inode instead of the one we just
963 * allocated.
964 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100965 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 destroy_inode(inode);
967 inode = old;
968 wait_on_inode(inode);
969 }
970 return inode;
971
972set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100973 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 destroy_inode(inode);
975 return NULL;
976}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400977EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400979/**
980 * iget_locked - obtain an inode from a mounted file system
981 * @sb: super block of file system
982 * @ino: inode number to get
983 *
984 * Search for the inode specified by @ino in the inode cache and if present
985 * return it with an increased reference count. This is for file systems
986 * where the inode number is sufficient for unique identification of an inode.
987 *
988 * If the inode is not in cache, allocate a new inode and return it locked,
989 * hashed, and with the I_NEW flag set. The file system gets to fill it in
990 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400992struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400994 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530995 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400997 spin_lock(&inode_hash_lock);
998 inode = find_inode_fast(sb, head, ino);
999 spin_unlock(&inode_hash_lock);
1000 if (inode) {
1001 wait_on_inode(inode);
1002 return inode;
1003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 inode = alloc_inode(sb);
1006 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301007 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Dave Chinner67a23c42011-03-22 22:23:42 +11001009 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* We released the lock, so.. */
1011 old = find_inode_fast(sb, head, ino);
1012 if (!old) {
1013 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001014 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001015 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001016 hlist_add_head(&inode->i_hash, head);
1017 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001018 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001019 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* Return the locked inode with I_NEW set, the
1022 * caller is responsible for filling in the contents
1023 */
1024 return inode;
1025 }
1026
1027 /*
1028 * Uhhuh, somebody else created the same inode under
1029 * us. Use the old inode instead of the one we just
1030 * allocated.
1031 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001032 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 destroy_inode(inode);
1034 inode = old;
1035 wait_on_inode(inode);
1036 }
1037 return inode;
1038}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001039EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001041/*
1042 * search the inode cache for a matching inode number.
1043 * If we find one, then the inode number we are trying to
1044 * allocate is not unique and so we should not use it.
1045 *
1046 * Returns 1 if the inode number is unique, 0 if it is not.
1047 */
1048static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1049{
1050 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1051 struct hlist_node *node;
1052 struct inode *inode;
1053
Dave Chinner67a23c42011-03-22 22:23:42 +11001054 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001055 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001056 if (inode->i_ino == ino && inode->i_sb == sb) {
1057 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001058 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001059 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001060 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001061 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001062
1063 return 1;
1064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/**
1067 * iunique - get a unique inode number
1068 * @sb: superblock
1069 * @max_reserved: highest reserved inode number
1070 *
1071 * Obtain an inode number that is unique on the system for a given
1072 * superblock. This is used by file systems that have no natural
1073 * permanent inode numbering system. An inode number is returned that
1074 * is higher than the reserved limit but unique.
1075 *
1076 * BUGS:
1077 * With a large number of inodes live on the file system this function
1078 * currently becomes quite slow.
1079 */
1080ino_t iunique(struct super_block *sb, ino_t max_reserved)
1081{
Jeff Layton866b04f2007-05-08 00:32:29 -07001082 /*
1083 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1084 * error if st_ino won't fit in target struct field. Use 32bit counter
1085 * here to attempt to avoid that.
1086 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001087 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001088 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001091 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001092 do {
1093 if (counter <= max_reserved)
1094 counter = max_reserved + 1;
1095 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001096 } while (!test_inode_iunique(sb, res));
1097 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001098
1099 return res;
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101EXPORT_SYMBOL(iunique);
1102
1103struct inode *igrab(struct inode *inode)
1104{
Dave Chinner250df6e2011-03-22 22:23:36 +11001105 spin_lock(&inode->i_lock);
1106 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001108 spin_unlock(&inode->i_lock);
1109 } else {
1110 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /*
1112 * Handle the case where s_op->clear_inode is not been
1113 * called yet, and somebody is calling igrab
1114 * while the inode is getting freed.
1115 */
1116 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return inode;
1119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120EXPORT_SYMBOL(igrab);
1121
1122/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001123 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 * @sb: super block of file system to search
1125 * @hashval: hash value (usually inode number) to search for
1126 * @test: callback used for comparisons between inodes
1127 * @data: opaque data pointer to pass to @test
1128 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001129 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001131 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001132 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001133 * Note: I_NEW is not waited upon so you have to be very careful what you do
1134 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001135 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001136 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001137 */
1138struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1139 int (*test)(struct inode *, void *), void *data)
1140{
1141 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001142 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001143
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001144 spin_lock(&inode_hash_lock);
1145 inode = find_inode(sb, head, test, data);
1146 spin_unlock(&inode_hash_lock);
1147
1148 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001149}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001150EXPORT_SYMBOL(ilookup5_nowait);
1151
1152/**
1153 * ilookup5 - search for an inode in the inode cache
1154 * @sb: super block of file system to search
1155 * @hashval: hash value (usually inode number) to search for
1156 * @test: callback used for comparisons between inodes
1157 * @data: opaque data pointer to pass to @test
1158 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001159 * Search for the inode specified by @hashval and @data in the inode cache,
1160 * and if the inode is in the cache, return the inode with an incremented
1161 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001162 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001164 * This is a generalized version of ilookup() for file systems where the
1165 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001167 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
1169struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1170 int (*test)(struct inode *, void *), void *data)
1171{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001172 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001174 if (inode)
1175 wait_on_inode(inode);
1176 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178EXPORT_SYMBOL(ilookup5);
1179
1180/**
1181 * ilookup - search for an inode in the inode cache
1182 * @sb: super block of file system to search
1183 * @ino: inode number to search for
1184 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001185 * Search for the inode @ino in the inode cache, and if the inode is in the
1186 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
1188struct inode *ilookup(struct super_block *sb, unsigned long ino)
1189{
1190 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001191 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001193 spin_lock(&inode_hash_lock);
1194 inode = find_inode_fast(sb, head, ino);
1195 spin_unlock(&inode_hash_lock);
1196
1197 if (inode)
1198 wait_on_inode(inode);
1199 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201EXPORT_SYMBOL(ilookup);
1202
Al Viro261bca82008-12-30 01:48:21 -05001203int insert_inode_locked(struct inode *inode)
1204{
1205 struct super_block *sb = inode->i_sb;
1206 ino_t ino = inode->i_ino;
1207 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001208
Al Viro261bca82008-12-30 01:48:21 -05001209 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001210 struct hlist_node *node;
1211 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001212 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001213 hlist_for_each_entry(old, node, head, i_hash) {
1214 if (old->i_ino != ino)
1215 continue;
1216 if (old->i_sb != sb)
1217 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001218 spin_lock(&old->i_lock);
1219 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1220 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001221 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001222 }
Al Viro72a43d62009-05-13 19:13:40 +01001223 break;
1224 }
1225 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001226 spin_lock(&inode->i_lock);
1227 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001228 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001229 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001230 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001231 return 0;
1232 }
1233 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001234 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001235 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001236 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001237 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001238 iput(old);
1239 return -EBUSY;
1240 }
1241 iput(old);
1242 }
1243}
Al Viro261bca82008-12-30 01:48:21 -05001244EXPORT_SYMBOL(insert_inode_locked);
1245
1246int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1247 int (*test)(struct inode *, void *), void *data)
1248{
1249 struct super_block *sb = inode->i_sb;
1250 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001251
Al Viro261bca82008-12-30 01:48:21 -05001252 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001253 struct hlist_node *node;
1254 struct inode *old = NULL;
1255
Dave Chinner67a23c42011-03-22 22:23:42 +11001256 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001257 hlist_for_each_entry(old, node, head, i_hash) {
1258 if (old->i_sb != sb)
1259 continue;
1260 if (!test(old, data))
1261 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001262 spin_lock(&old->i_lock);
1263 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1264 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001265 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001266 }
Al Viro72a43d62009-05-13 19:13:40 +01001267 break;
1268 }
1269 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001270 spin_lock(&inode->i_lock);
1271 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001272 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001273 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001274 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001275 return 0;
1276 }
1277 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001278 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001279 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001280 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001281 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001282 iput(old);
1283 return -EBUSY;
1284 }
1285 iput(old);
1286 }
1287}
Al Viro261bca82008-12-30 01:48:21 -05001288EXPORT_SYMBOL(insert_inode_locked4);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Al Viro45321ac2010-06-07 13:43:19 -04001291int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Al Viro45321ac2010-06-07 13:43:19 -04001293 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295EXPORT_SYMBOL(generic_delete_inode);
1296
Al Viro45321ac2010-06-07 13:43:19 -04001297/*
1298 * Normal UNIX filesystem behaviour: delete the
1299 * inode when the usage count drops to zero, and
1300 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001301 */
Al Viro45321ac2010-06-07 13:43:19 -04001302int generic_drop_inode(struct inode *inode)
1303{
Al Viro1d3382cb2010-10-23 15:19:20 -04001304 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001305}
1306EXPORT_SYMBOL_GPL(generic_drop_inode);
1307
1308/*
1309 * Called when we're dropping the last reference
1310 * to an inode.
1311 *
1312 * Call the FS "drop_inode()" function, defaulting to
1313 * the legacy UNIX filesystem behaviour. If it tells
1314 * us to evict inode, do so. Otherwise, retain inode
1315 * in cache if fs is alive, sync and evict if fs is
1316 * shutting down.
1317 */
1318static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001321 const struct super_operations *op = inode->i_sb->s_op;
1322 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Dave Chinner250df6e2011-03-22 22:23:36 +11001324 WARN_ON(inode->i_state & I_NEW);
1325
Al Viro45321ac2010-06-07 13:43:19 -04001326 if (op && op->drop_inode)
1327 drop = op->drop_inode(inode);
1328 else
1329 drop = generic_drop_inode(inode);
1330
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001331 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1332 inode->i_state |= I_REFERENCED;
1333 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1334 inode_lru_list_add(inode);
1335 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001336 return;
1337 }
1338
Al Viro45321ac2010-06-07 13:43:19 -04001339 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001340 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001341 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001343 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001344 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001345 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001347
Alexander Viro991114c2005-06-23 00:09:01 -07001348 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001349 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001350 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001351
1352 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301356 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * @inode: inode to put
1358 *
1359 * Puts an inode, dropping its usage count. If the inode use count hits
1360 * zero, the inode is then freed and may also be destroyed.
1361 *
1362 * Consequently, iput() can sleep.
1363 */
1364void iput(struct inode *inode)
1365{
1366 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001367 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Dave Chinnerf283c862011-03-22 22:23:39 +11001369 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 iput_final(inode);
1371 }
1372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373EXPORT_SYMBOL(iput);
1374
1375/**
1376 * bmap - find a block number in a file
1377 * @inode: inode of file
1378 * @block: block to find
1379 *
1380 * Returns the block number on the device holding the inode that
1381 * is the disk block number for the block of the file requested.
1382 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301383 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * file.
1385 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301386sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 sector_t res = 0;
1389 if (inode->i_mapping->a_ops->bmap)
1390 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1391 return res;
1392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393EXPORT_SYMBOL(bmap);
1394
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001395/*
1396 * With relative atime, only update atime if the previous atime is
1397 * earlier than either the ctime or mtime or if at least a day has
1398 * passed since the last atime update.
1399 */
1400static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1401 struct timespec now)
1402{
1403
1404 if (!(mnt->mnt_flags & MNT_RELATIME))
1405 return 1;
1406 /*
1407 * Is mtime younger than atime? If yes, update atime:
1408 */
1409 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1410 return 1;
1411 /*
1412 * Is ctime younger than atime? If yes, update atime:
1413 */
1414 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1415 return 1;
1416
1417 /*
1418 * Is the previous atime value older than a day? If yes,
1419 * update atime:
1420 */
1421 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1422 return 1;
1423 /*
1424 * Good, we can skip the atime update:
1425 */
1426 return 0;
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001430 * touch_atime - update the access time
1431 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001432 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 *
1434 * Update the accessed time on an inode and mark it for writeback.
1435 * This function automatically handles read only file systems and media,
1436 * as well as the "noatime" flag and inode specific "noatime" markers.
1437 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001438void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001440 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 struct timespec now;
1442
Andrew Mortonb2276132006-12-13 00:34:33 -08001443 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001444 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001445 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001446 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001447 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001448 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001449
Dave Hansencdb70f32008-02-15 14:37:41 -08001450 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001451 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001452 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001453 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001456
1457 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001458 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001459
Valerie Henson47ae32d2006-12-13 00:34:34 -08001460 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001461 return;
1462
1463 if (mnt_want_write(mnt))
1464 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001465
1466 inode->i_atime = now;
1467 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001468 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001470EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001473 * file_update_time - update mtime and ctime time
1474 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001476 * Update the mtime and ctime members of an inode and mark the inode
1477 * for writeback. Note that this function is meant exclusively for
1478 * usage in the file write path of filesystems, and filesystems may
1479 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001480 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001481 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483
Christoph Hellwig870f4812006-01-09 20:52:01 -08001484void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001486 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001488 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Andi Kleence06e0b2009-09-18 13:05:48 -07001490 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (IS_NOCMTIME(inode))
1492 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001493
Andi Kleence06e0b2009-09-18 13:05:48 -07001494 now = current_fs_time(inode->i_sb);
1495 if (!timespec_equal(&inode->i_mtime, &now))
1496 sync_it = S_MTIME;
1497
1498 if (!timespec_equal(&inode->i_ctime, &now))
1499 sync_it |= S_CTIME;
1500
1501 if (IS_I_VERSION(inode))
1502 sync_it |= S_VERSION;
1503
1504 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return;
1506
Andi Kleence06e0b2009-09-18 13:05:48 -07001507 /* Finally allowed to write? Takes lock. */
1508 if (mnt_want_write_file(file))
1509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Andi Kleence06e0b2009-09-18 13:05:48 -07001511 /* Only change inode inside the lock region */
1512 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001513 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001514 if (sync_it & S_CTIME)
1515 inode->i_ctime = now;
1516 if (sync_it & S_MTIME)
1517 inode->i_mtime = now;
1518 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001519 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001521EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523int inode_needs_sync(struct inode *inode)
1524{
1525 if (IS_SYNC(inode))
1526 return 1;
1527 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1528 return 1;
1529 return 0;
1530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531EXPORT_SYMBOL(inode_needs_sync);
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533int inode_wait(void *word)
1534{
1535 schedule();
1536 return 0;
1537}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001538EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001541 * If we try to find an inode in the inode hash while it is being
1542 * deleted, we have to wait until the filesystem completes its
1543 * deletion before reporting that it isn't found. This function waits
1544 * until the deletion _might_ have completed. Callers are responsible
1545 * to recheck inode state.
1546 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001547 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001548 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1549 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
1551static void __wait_on_freeing_inode(struct inode *inode)
1552{
1553 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001554 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1555 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001557 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001558 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 schedule();
1560 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001561 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static __initdata unsigned long ihash_entries;
1565static int __init set_ihash_entries(char *str)
1566{
1567 if (!str)
1568 return 0;
1569 ihash_entries = simple_strtoul(str, &str, 0);
1570 return 1;
1571}
1572__setup("ihash_entries=", set_ihash_entries);
1573
1574/*
1575 * Initialize the waitqueues and inode hash table.
1576 */
1577void __init inode_init_early(void)
1578{
1579 int loop;
1580
1581 /* If hashes are distributed across NUMA nodes, defer
1582 * hash allocation until vmalloc space is available.
1583 */
1584 if (hashdist)
1585 return;
1586
1587 inode_hashtable =
1588 alloc_large_system_hash("Inode-cache",
1589 sizeof(struct hlist_head),
1590 ihash_entries,
1591 14,
1592 HASH_EARLY,
1593 &i_hash_shift,
1594 &i_hash_mask,
1595 0);
1596
1597 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1598 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1599}
1600
Denis Cheng74bf17c2007-10-16 23:26:30 -07001601void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 int loop;
1604
1605 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001606 inode_cachep = kmem_cache_create("inode_cache",
1607 sizeof(struct inode),
1608 0,
1609 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1610 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001611 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001612 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* Hash may have been set up in inode_init_early */
1615 if (!hashdist)
1616 return;
1617
1618 inode_hashtable =
1619 alloc_large_system_hash("Inode-cache",
1620 sizeof(struct hlist_head),
1621 ihash_entries,
1622 14,
1623 0,
1624 &i_hash_shift,
1625 &i_hash_mask,
1626 0);
1627
1628 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1629 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1630}
1631
1632void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1633{
1634 inode->i_mode = mode;
1635 if (S_ISCHR(mode)) {
1636 inode->i_fop = &def_chr_fops;
1637 inode->i_rdev = rdev;
1638 } else if (S_ISBLK(mode)) {
1639 inode->i_fop = &def_blk_fops;
1640 inode->i_rdev = rdev;
1641 } else if (S_ISFIFO(mode))
1642 inode->i_fop = &def_fifo_fops;
1643 else if (S_ISSOCK(mode))
1644 inode->i_fop = &bad_sock_fops;
1645 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001646 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1647 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1648 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001651
1652/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00001653 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001654 * @inode: New inode
1655 * @dir: Directory inode
1656 * @mode: mode of the new inode
1657 */
1658void inode_init_owner(struct inode *inode, const struct inode *dir,
1659 mode_t mode)
1660{
1661 inode->i_uid = current_fsuid();
1662 if (dir && dir->i_mode & S_ISGID) {
1663 inode->i_gid = dir->i_gid;
1664 if (S_ISDIR(mode))
1665 mode |= S_ISGID;
1666 } else
1667 inode->i_gid = current_fsgid();
1668 inode->i_mode = mode;
1669}
1670EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001671
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001672/**
1673 * inode_owner_or_capable - check current task permissions to inode
1674 * @inode: inode being checked
1675 *
1676 * Return true if current either has CAP_FOWNER to the inode, or
1677 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001678 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001679bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001680{
1681 struct user_namespace *ns = inode_userns(inode);
1682
1683 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1684 return true;
1685 if (ns_capable(ns, CAP_FOWNER))
1686 return true;
1687 return false;
1688}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001689EXPORT_SYMBOL(inode_owner_or_capable);