blob: 990d284877a12472393a8d444b732ba9ec105c7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/inode.c
3 *
4 * (C) 1997 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/fs.h>
8#include <linux/mm.h>
9#include <linux/dcache.h>
10#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/slab.h>
12#include <linux/writeback.h>
13#include <linux/module.h>
14#include <linux/backing-dev.h>
15#include <linux/wait.h>
Nick Piggin88e0fbc2009-09-22 16:43:50 -070016#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/hash.h>
18#include <linux/swap.h>
19#include <linux/security.h>
20#include <linux/pagemap.h>
21#include <linux/cdev.h>
22#include <linux/bootmem.h>
Eric Paris3be25f42009-05-21 17:01:26 -040023#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080024#include <linux/mount.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080025#include <linux/async.h>
Al Virof19d4a82009-06-08 19:50:45 -040026#include <linux/posix_acl.h>
Heiko Carstens9ce6e0b2011-05-22 18:54:21 +020027#include <linux/prefetch.h>
Eric Parisa178d202010-10-25 14:41:59 -040028#include <linux/ima.h>
Serge E. Hallyne795b712011-03-23 16:43:25 -070029#include <linux/cred.h>
Dave Chinnera66979a2011-03-22 22:23:41 +110030#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
Dave Chinner250df6e2011-03-22 22:23:36 +110033 * inode locking rules.
34 *
35 * inode->i_lock protects:
36 * inode->i_state, inode->i_hash, __iget()
Dave Chinner02afc412011-03-22 22:23:38 +110037 * inode_lru_lock protects:
38 * inode_lru, inode->i_lru
Dave Chinner55fa6092011-03-22 22:23:40 +110039 * inode_sb_list_lock protects:
40 * sb->s_inodes, inode->i_sb_list
Dave Chinnera66979a2011-03-22 22:23:41 +110041 * inode_wb_list_lock protects:
42 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
Dave Chinner67a23c42011-03-22 22:23:42 +110043 * inode_hash_lock protects:
44 * inode_hashtable, inode->i_hash
Dave Chinner250df6e2011-03-22 22:23:36 +110045 *
46 * Lock ordering:
Dave Chinner55fa6092011-03-22 22:23:40 +110047 *
48 * inode_sb_list_lock
49 * inode->i_lock
Dave Chinner02afc412011-03-22 22:23:38 +110050 * inode_lru_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110051 *
52 * inode_wb_list_lock
53 * inode->i_lock
Dave Chinner67a23c42011-03-22 22:23:42 +110054 *
55 * inode_hash_lock
56 * inode_sb_list_lock
57 * inode->i_lock
58 *
59 * iunique_lock
60 * inode_hash_lock
Dave Chinner250df6e2011-03-22 22:23:36 +110061 */
62
63/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * This is needed for the following functions:
65 * - inode_has_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * - invalidate_bdev
67 *
68 * FIXME: remove all knowledge of the buffer layer from this file
69 */
70#include <linux/buffer_head.h>
71
72/*
73 * New inode.c implementation.
74 *
75 * This implementation has the basic premise of trying
76 * to be extremely low-overhead and SMP-safe, yet be
77 * simple enough to be "obviously correct".
78 *
79 * Famous last words.
80 */
81
82/* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
83
84/* #define INODE_PARANOIA 1 */
85/* #define INODE_DEBUG 1 */
86
87/*
88 * Inode lookup is no longer as critical as it used to be:
89 * most of the lookups are going to be through the dcache.
90 */
91#define I_HASHBITS i_hash_shift
92#define I_HASHMASK i_hash_mask
93
Eric Dumazetfa3536c2006-03-26 01:37:24 -080094static unsigned int i_hash_mask __read_mostly;
95static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110096static struct hlist_head *inode_hashtable __read_mostly;
97static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * Each inode can be on two separate lists. One is
101 * the hash list of the inode, used for lookups. The
102 * other linked list is the "type" list:
103 * "in_use" - valid inode, i_count > 0, i_nlink > 0
104 * "dirty" - as "in_use" but also dirty
105 * "unused" - valid inode, i_count = 0
106 *
107 * A "dirty" list is maintained for each super block,
108 * allowing for low-overhead inode sync() operations.
109 */
110
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100111static LIST_HEAD(inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +1100112static DEFINE_SPINLOCK(inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dave Chinner55fa6092011-03-22 22:23:40 +1100114__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
Dave Chinnera66979a2011-03-22 22:23:41 +1100115__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100118 * iprune_sem provides exclusion between the icache shrinking and the
119 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700120 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100121 * We don't actually need it to protect anything in the umount path,
122 * but only need to cycle through it to make sure any inode that
123 * prune_icache took off the LRU list has been fully torn down by the
124 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700126static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +0200129 * Empty aops. Can be used for the cases where the user does not
130 * define any of the address_space operations.
131 */
132const struct address_space_operations empty_aops = {
133};
134EXPORT_SYMBOL(empty_aops);
135
136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * Statistics gathering..
138 */
139struct inodes_stat_t inodes_stat;
140
Nick Piggin3e880fb2011-01-07 17:49:19 +1100141static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400142
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530143static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Nick Piggin3e880fb2011-01-07 17:49:19 +1100145static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400146{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100147 int i;
148 int sum = 0;
149 for_each_possible_cpu(i)
150 sum += per_cpu(nr_inodes, i);
151 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400152}
153
154static inline int get_nr_inodes_unused(void)
155{
Nick Piggin86c87492011-01-07 17:49:18 +1100156 return inodes_stat.nr_unused;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400157}
158
159int get_nr_dirty_inodes(void)
160{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100161 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400162 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
163 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400164}
165
166/*
167 * Handle nr_inode sysctl
168 */
169#ifdef CONFIG_SYSCTL
170int proc_nr_inodes(ctl_table *table, int write,
171 void __user *buffer, size_t *lenp, loff_t *ppos)
172{
173 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400174 return proc_dointvec(table, write, buffer, lenp, ppos);
175}
176#endif
177
David Chinner2cb15992008-10-30 17:32:23 +1100178/**
179 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800180 * @sb: superblock inode belongs to
181 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100182 *
183 * These are initializations that need to be done on every inode
184 * allocation as the fields are not initialised by slab allocation.
185 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300186int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700188 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800189 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530190 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100191
192 inode->i_sb = sb;
193 inode->i_blkbits = sb->s_blocksize_bits;
194 inode->i_flags = 0;
195 atomic_set(&inode->i_count, 1);
196 inode->i_op = &empty_iops;
197 inode->i_fop = &empty_fops;
198 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500199 inode->i_uid = 0;
200 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100201 atomic_set(&inode->i_writecount, 0);
202 inode->i_size = 0;
203 inode->i_blocks = 0;
204 inode->i_bytes = 0;
205 inode->i_generation = 0;
206#ifdef CONFIG_QUOTA
207 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
208#endif
209 inode->i_pipe = NULL;
210 inode->i_bdev = NULL;
211 inode->i_cdev = NULL;
212 inode->i_rdev = 0;
213 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500214
215 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300216 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100217 spin_lock_init(&inode->i_lock);
218 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
219
220 mutex_init(&inode->i_mutex);
221 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
222
223 init_rwsem(&inode->i_alloc_sem);
224 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
225
226 mapping->a_ops = &empty_aops;
227 mapping->host = inode;
228 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800229 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100230 mapping->assoc_mapping = NULL;
231 mapping->backing_dev_info = &default_backing_dev_info;
232 mapping->writeback_index = 0;
233
234 /*
235 * If the block_device provides a backing_dev_info for client
236 * inodes then use that. Otherwise the inode share the bdev's
237 * backing_dev_info.
238 */
239 if (sb->s_bdev) {
240 struct backing_dev_info *bdi;
241
Jens Axboe2c96ce92009-09-15 09:43:56 +0200242 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100243 mapping->backing_dev_info = bdi;
244 }
245 inode->i_private = NULL;
246 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400247#ifdef CONFIG_FS_POSIX_ACL
248 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
249#endif
David Chinner2cb15992008-10-30 17:32:23 +1100250
Eric Paris3be25f42009-05-21 17:01:26 -0400251#ifdef CONFIG_FSNOTIFY
252 inode->i_fsnotify_mask = 0;
253#endif
254
Nick Piggin3e880fb2011-01-07 17:49:19 +1100255 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400256
Christoph Hellwig54e34622009-08-07 14:38:25 -0300257 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300258out:
259 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100260}
261EXPORT_SYMBOL(inode_init_always);
262
263static struct inode *alloc_inode(struct super_block *sb)
264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct inode *inode;
266
267 if (sb->s_op->alloc_inode)
268 inode = sb->s_op->alloc_inode(sb);
269 else
David Chinner2cb15992008-10-30 17:32:23 +1100270 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Hellwig54e34622009-08-07 14:38:25 -0300272 if (!inode)
273 return NULL;
274
275 if (unlikely(inode_init_always(sb, inode))) {
276 if (inode->i_sb->s_op->destroy_inode)
277 inode->i_sb->s_op->destroy_inode(inode);
278 else
279 kmem_cache_free(inode_cachep, inode);
280 return NULL;
281 }
282
283 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Nick Pigginff0c7d12011-01-07 17:49:50 +1100286void free_inode_nonrcu(struct inode *inode)
287{
288 kmem_cache_free(inode_cachep, inode);
289}
290EXPORT_SYMBOL(free_inode_nonrcu);
291
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300292void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200294 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400296 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400297#ifdef CONFIG_FS_POSIX_ACL
298 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
299 posix_acl_release(inode->i_acl);
300 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
301 posix_acl_release(inode->i_default_acl);
302#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100303 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300304}
305EXPORT_SYMBOL(__destroy_inode);
306
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100307static void i_callback(struct rcu_head *head)
308{
309 struct inode *inode = container_of(head, struct inode, i_rcu);
310 INIT_LIST_HEAD(&inode->i_dentry);
311 kmem_cache_free(inode_cachep, inode);
312}
313
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200314static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300315{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100316 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300317 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (inode->i_sb->s_op->destroy_inode)
319 inode->i_sb->s_op->destroy_inode(inode);
320 else
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100321 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100324void address_space_init_once(struct address_space *mapping)
325{
326 memset(mapping, 0, sizeof(*mapping));
327 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
328 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700329 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100330 INIT_LIST_HEAD(&mapping->private_list);
331 spin_lock_init(&mapping->private_lock);
332 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
333 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100334}
335EXPORT_SYMBOL(address_space_init_once);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337/*
338 * These are initializations that only need to be done
339 * once, because the fields are idempotent across use
340 * of the inode, so let the slab aware of that.
341 */
342void inode_init_once(struct inode *inode)
343{
344 memset(inode, 0, sizeof(*inode));
345 INIT_HLIST_NODE(&inode->i_hash);
346 INIT_LIST_HEAD(&inode->i_dentry);
347 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100348 INIT_LIST_HEAD(&inode->i_wb_list);
349 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100350 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400352#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500353 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400354#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356EXPORT_SYMBOL(inode_init_once);
357
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700358static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530360 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Christoph Lametera35afb82007-05-16 22:10:57 -0700362 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100366 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530368void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Nick Piggin9e38d862010-10-23 06:55:17 -0400370 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Al Viro7de9c6e2010-10-23 11:11:40 -0400373/*
374 * get additional reference to inode; caller must already hold one.
375 */
376void ihold(struct inode *inode)
377{
378 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
379}
380EXPORT_SYMBOL(ihold);
381
Nick Piggin9e38d862010-10-23 06:55:17 -0400382static void inode_lru_list_add(struct inode *inode)
383{
Dave Chinner02afc412011-03-22 22:23:38 +1100384 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100385 if (list_empty(&inode->i_lru)) {
386 list_add(&inode->i_lru, &inode_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100387 inodes_stat.nr_unused++;
Nick Piggin9e38d862010-10-23 06:55:17 -0400388 }
Dave Chinner02afc412011-03-22 22:23:38 +1100389 spin_unlock(&inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400390}
391
392static void inode_lru_list_del(struct inode *inode)
393{
Dave Chinner02afc412011-03-22 22:23:38 +1100394 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100395 if (!list_empty(&inode->i_lru)) {
396 list_del_init(&inode->i_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100397 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400398 }
Dave Chinner02afc412011-03-22 22:23:38 +1100399 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Christoph Hellwig646ec462010-10-23 07:15:32 -0400402/**
403 * inode_sb_list_add - add inode to the superblock list of inodes
404 * @inode: inode to add
405 */
406void inode_sb_list_add(struct inode *inode)
407{
Dave Chinner55fa6092011-03-22 22:23:40 +1100408 spin_lock(&inode_sb_list_lock);
409 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
410 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400411}
412EXPORT_SYMBOL_GPL(inode_sb_list_add);
413
Dave Chinner55fa6092011-03-22 22:23:40 +1100414static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400415{
Dave Chinner55fa6092011-03-22 22:23:40 +1100416 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400417 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100418 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400419}
420
Dave Chinner4c51acb2010-10-23 06:58:09 -0400421static unsigned long hash(struct super_block *sb, unsigned long hashval)
422{
423 unsigned long tmp;
424
425 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
426 L1_CACHE_BYTES;
427 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
428 return tmp & I_HASHMASK;
429}
430
431/**
432 * __insert_inode_hash - hash an inode
433 * @inode: unhashed inode
434 * @hashval: unsigned long value used to locate this object in the
435 * inode_hashtable.
436 *
437 * Add an inode to the inode hash for this superblock.
438 */
439void __insert_inode_hash(struct inode *inode, unsigned long hashval)
440{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400441 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
442
Dave Chinner67a23c42011-03-22 22:23:42 +1100443 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100444 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400445 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100446 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100447 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400448}
449EXPORT_SYMBOL(__insert_inode_hash);
450
451/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400452 * remove_inode_hash - remove an inode from the hash
453 * @inode: inode to unhash
454 *
455 * Remove an inode from the superblock.
456 */
457void remove_inode_hash(struct inode *inode)
458{
Dave Chinner67a23c42011-03-22 22:23:42 +1100459 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100460 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400461 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100462 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100463 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400464}
465EXPORT_SYMBOL(remove_inode_hash);
466
Al Virob0683aa2010-06-04 20:55:25 -0400467void end_writeback(struct inode *inode)
468{
469 might_sleep();
470 BUG_ON(inode->i_data.nrpages);
471 BUG_ON(!list_empty(&inode->i_data.private_list));
472 BUG_ON(!(inode->i_state & I_FREEING));
473 BUG_ON(inode->i_state & I_CLEAR);
474 inode_sync_wait(inode);
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100475 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400476 inode->i_state = I_FREEING | I_CLEAR;
477}
478EXPORT_SYMBOL(end_writeback);
479
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100480/*
481 * Free the inode passed in, removing it from the lists it is still connected
482 * to. We remove any pages still attached to the inode and wait for any IO that
483 * is still in progress before finally destroying the inode.
484 *
485 * An inode must already be marked I_FREEING so that we avoid the inode being
486 * moved back onto lists if we race with other code that manipulates the lists
487 * (e.g. writeback_single_inode). The caller is responsible for setting this.
488 *
489 * An inode must already be removed from the LRU list before being evicted from
490 * the cache. This should occur atomically with setting the I_FREEING state
491 * flag, so no inodes here should ever be on the LRU when being evicted.
492 */
Al Viro644da592010-06-07 13:21:05 -0400493static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400494{
495 const struct super_operations *op = inode->i_sb->s_op;
496
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100497 BUG_ON(!(inode->i_state & I_FREEING));
498 BUG_ON(!list_empty(&inode->i_lru));
499
Dave Chinnera66979a2011-03-22 22:23:41 +1100500 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100501 inode_sb_list_del(inode);
502
Al Virobe7ce412010-06-04 19:40:39 -0400503 if (op->evict_inode) {
504 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400505 } else {
506 if (inode->i_data.nrpages)
507 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400508 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400509 }
Al Viro661074e2010-06-04 20:19:55 -0400510 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
511 bd_forget(inode);
512 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
513 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100514
515 remove_inode_hash(inode);
516
517 spin_lock(&inode->i_lock);
518 wake_up_bit(&inode->i_state, __I_NEW);
519 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
520 spin_unlock(&inode->i_lock);
521
522 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
526 * dispose_list - dispose of the contents of a local list
527 * @head: the head of the list to free
528 *
529 * Dispose-list gets a local list with local inodes in it, so it doesn't
530 * need to worry about list corruption and SMP locks.
531 */
532static void dispose_list(struct list_head *head)
533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 while (!list_empty(head)) {
535 struct inode *inode;
536
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100537 inode = list_first_entry(head, struct inode, i_lru);
538 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Al Viro644da592010-06-07 13:21:05 -0400540 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/**
Al Viro63997e92010-10-25 20:49:35 -0400545 * evict_inodes - evict all evictable inodes for a superblock
546 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *
Al Viro63997e92010-10-25 20:49:35 -0400548 * Make sure that no inodes with zero refcount are retained. This is
549 * called by superblock shutdown after having MS_ACTIVE flag removed,
550 * so any inode reaching zero refcount during or after that call will
551 * be immediately evicted.
552 */
553void evict_inodes(struct super_block *sb)
554{
555 struct inode *inode, *next;
556 LIST_HEAD(dispose);
557
Dave Chinner55fa6092011-03-22 22:23:40 +1100558 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400559 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
560 if (atomic_read(&inode->i_count))
561 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100562
563 spin_lock(&inode->i_lock);
564 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
565 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400566 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100567 }
Al Viro63997e92010-10-25 20:49:35 -0400568
569 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100570 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100571 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100572 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400573 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100574 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400575
576 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100577
578 /*
579 * Cycle through iprune_sem to make sure any inode that prune_icache
580 * moved off the list before we took the lock has been fully torn
581 * down.
582 */
583 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400584 up_write(&iprune_sem);
585}
586
587/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200588 * invalidate_inodes - attempt to free all inodes on a superblock
589 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100590 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200591 *
592 * Attempts to free all inodes for a given superblock. If there were any
593 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100594 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
595 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
NeilBrown93b270f2011-02-24 17:25:47 +1100597int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400599 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200600 struct inode *inode, *next;
601 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Dave Chinner55fa6092011-03-22 22:23:40 +1100603 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200604 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100605 spin_lock(&inode->i_lock);
606 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
607 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100609 }
NeilBrown93b270f2011-02-24 17:25:47 +1100610 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100611 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100612 busy = 1;
613 continue;
614 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200615 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100616 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200617 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 continue;
619 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200620
Christoph Hellwig99a38912010-10-23 19:07:20 +0200621 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100622 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100623 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100624 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100626 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Christoph Hellwiga0318782010-10-24 19:40:33 +0200628 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 return busy;
631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633static int can_unuse(struct inode *inode)
634{
Nick Piggin9e38d862010-10-23 06:55:17 -0400635 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 0;
637 if (inode_has_buffers(inode))
638 return 0;
639 if (atomic_read(&inode->i_count))
640 return 0;
641 if (inode->i_data.nrpages)
642 return 0;
643 return 1;
644}
645
646/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400647 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100648 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 *
650 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400651 * pagecache removed. If the inode has metadata buffers attached to
652 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400654 * If the inode has the I_REFERENCED flag set, then it means that it has been
655 * used recently - the flag is set in iput_final(). When we encounter such an
656 * inode, clear the flag and move it to the back of the LRU so it gets another
657 * pass through the LRU before it gets reclaimed. This is necessary because of
658 * the fact we are doing lazy LRU updates to minimise lock contention so the
659 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
660 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
662static void prune_icache(int nr_to_scan)
663{
664 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int nr_scanned;
666 unsigned long reap = 0;
667
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700668 down_read(&iprune_sem);
Dave Chinner02afc412011-03-22 22:23:38 +1100669 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
671 struct inode *inode;
672
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100673 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
675
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100676 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Nick Piggin9e38d862010-10-23 06:55:17 -0400678 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100679 * we are inverting the inode_lru_lock/inode->i_lock here,
680 * so use a trylock. If we fail to get the lock, just move the
681 * inode to the back of the list so we don't spin on it.
682 */
683 if (!spin_trylock(&inode->i_lock)) {
684 list_move(&inode->i_lru, &inode_lru);
685 continue;
686 }
687
688 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400689 * Referenced or dirty inodes are still in use. Give them
690 * another pass through the LRU as we canot reclaim them now.
691 */
692 if (atomic_read(&inode->i_count) ||
693 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100694 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100695 spin_unlock(&inode->i_lock);
Nick Piggin86c87492011-01-07 17:49:18 +1100696 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400697 continue;
698 }
699
700 /* recently referenced inodes get one more pass */
701 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400702 inode->i_state &= ~I_REFERENCED;
Dave Chinner250df6e2011-03-22 22:23:36 +1100703 list_move(&inode->i_lru, &inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100704 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 continue;
706 }
707 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
708 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100709 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100710 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800712 reap += invalidate_mapping_pages(&inode->i_data,
713 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100715 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100717 if (inode != list_entry(inode_lru.next,
718 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100720 /* avoid lock inversions with trylock */
721 if (!spin_trylock(&inode->i_lock))
722 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100723 if (!can_unuse(inode)) {
724 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700728 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100730 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100731
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100732 list_move(&inode->i_lru, &freeable);
Nick Piggin86c87492011-01-07 17:49:18 +1100733 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700735 if (current_is_kswapd())
736 __count_vm_events(KSWAPD_INODESTEAL, reap);
737 else
738 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100739 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700742 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745/*
746 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
747 * "unused" means that no dentries are referring to the inodes: the files are
748 * not open and the dcache references to those inodes have already been
749 * reclaimed.
750 *
751 * This function is passed the number of inodes to scan, and it returns the
752 * total number of remaining possibly-reclaimable inodes.
753 */
Ying Han1495f232011-05-24 17:12:27 -0700754static int shrink_icache_memory(struct shrinker *shrink,
755 struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Ying Han1495f232011-05-24 17:12:27 -0700757 int nr = sc->nr_to_scan;
758 gfp_t gfp_mask = sc->gfp_mask;
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (nr) {
761 /*
762 * Nasty deadlock avoidance. We may hold various FS locks,
763 * and we don't want to recurse into the FS that called us
764 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!(gfp_mask & __GFP_FS))
767 return -1;
768 prune_icache(nr);
769 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400770 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Rusty Russell8e1f9362007-07-17 04:03:17 -0700773static struct shrinker icache_shrinker = {
774 .shrink = shrink_icache_memory,
775 .seeks = DEFAULT_SEEKS,
776};
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static void __wait_on_freeing_inode(struct inode *inode);
779/*
780 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530782static struct inode *find_inode(struct super_block *sb,
783 struct hlist_head *head,
784 int (*test)(struct inode *, void *),
785 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530788 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700791 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100792 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100793 if (inode->i_sb != sb) {
794 spin_unlock(&inode->i_lock);
795 continue;
796 }
797 if (!test(inode, data)) {
798 spin_unlock(&inode->i_lock);
799 continue;
800 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400801 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 __wait_on_freeing_inode(inode);
803 goto repeat;
804 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400805 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100806 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400807 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400809 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/*
813 * find_inode_fast is the fast path version of find_inode, see the comment at
814 * iget_locked for details.
815 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530816static struct inode *find_inode_fast(struct super_block *sb,
817 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530820 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700823 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100824 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100825 if (inode->i_ino != ino) {
826 spin_unlock(&inode->i_lock);
827 continue;
828 }
829 if (inode->i_sb != sb) {
830 spin_unlock(&inode->i_lock);
831 continue;
832 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400833 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 __wait_on_freeing_inode(inode);
835 goto repeat;
836 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400837 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100838 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400839 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400841 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Eric Dumazetf991bd22010-10-23 11:18:01 -0400844/*
845 * Each cpu owns a range of LAST_INO_BATCH numbers.
846 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
847 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100848 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400849 * This does not significantly increase overflow rate because every CPU can
850 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
851 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
852 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
853 * overflow rate by 2x, which does not seem too significant.
854 *
855 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
856 * error if st_ino won't fit in target struct field. Use 32bit counter
857 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100858 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400859#define LAST_INO_BATCH 1024
860static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100861
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400862unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400863{
864 unsigned int *p = &get_cpu_var(last_ino);
865 unsigned int res = *p;
866
867#ifdef CONFIG_SMP
868 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
869 static atomic_t shared_last_ino;
870 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
871
872 res = next - LAST_INO_BATCH;
873 }
874#endif
875
876 *p = ++res;
877 put_cpu_var(last_ino);
878 return res;
David Chinner8290c352008-10-30 17:35:24 +1100879}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400880EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/**
883 * new_inode - obtain an inode
884 * @sb: superblock
885 *
Mel Gorman769848c2007-07-17 04:03:05 -0700886 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800887 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700888 * If HIGHMEM pages are unsuitable or it is known that pages allocated
889 * for the page cache are not reclaimable or migratable,
890 * mapping_set_gfp_mask() must be called with suitable flags on the
891 * newly created inode's mapping
892 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
894struct inode *new_inode(struct super_block *sb)
895{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530896 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Dave Chinner55fa6092011-03-22 22:23:40 +1100898 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 inode = alloc_inode(sb);
901 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100902 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100904 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100905 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 return inode;
908}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909EXPORT_SYMBOL(new_inode);
910
Dave Chinner250df6e2011-03-22 22:23:36 +1100911/**
912 * unlock_new_inode - clear the I_NEW state and wake up any waiters
913 * @inode: new inode to unlock
914 *
915 * Called when the inode is fully initialised to clear the new state of the
916 * inode and wake up anyone waiting for the inode to finish initialisation.
917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918void unlock_new_inode(struct inode *inode)
919{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200920#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900921 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200922 struct file_system_type *type = inode->i_sb->s_type;
923
Jan Kara9a7aa122009-06-04 15:26:49 +0200924 /* Set new key only if filesystem hasn't already changed it */
925 if (!lockdep_match_class(&inode->i_mutex,
926 &type->i_mutex_key)) {
927 /*
928 * ensure nobody is actually holding i_mutex
929 */
930 mutex_destroy(&inode->i_mutex);
931 mutex_init(&inode->i_mutex);
932 lockdep_set_class(&inode->i_mutex,
933 &type->i_mutex_dir_key);
934 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200935 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200936#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100937 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100938 WARN_ON(!(inode->i_state & I_NEW));
939 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100940 wake_up_bit(&inode->i_state, __I_NEW);
941 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943EXPORT_SYMBOL(unlock_new_inode);
944
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400945/**
946 * iget5_locked - obtain an inode from a mounted file system
947 * @sb: super block of file system
948 * @hashval: hash value (usually inode number) to get
949 * @test: callback used for comparisons between inodes
950 * @set: callback used to initialize a new struct inode
951 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400953 * Search for the inode specified by @hashval and @data in the inode cache,
954 * and if present it is return it with an increased reference count. This is
955 * a generalized version of iget_locked() for file systems where the inode
956 * number is not sufficient for unique identification of an inode.
957 *
958 * If the inode is not in cache, allocate a new inode and return it locked,
959 * hashed, and with the I_NEW flag set. The file system gets to fill it in
960 * before unlocking it via unlock_new_inode().
961 *
962 * Note both @test and @set are called with the inode_hash_lock held, so can't
963 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400965struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
966 int (*test)(struct inode *, void *),
967 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400969 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530970 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400972 spin_lock(&inode_hash_lock);
973 inode = find_inode(sb, head, test, data);
974 spin_unlock(&inode_hash_lock);
975
976 if (inode) {
977 wait_on_inode(inode);
978 return inode;
979 }
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 inode = alloc_inode(sb);
982 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530983 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Dave Chinner67a23c42011-03-22 22:23:42 +1100985 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* We released the lock, so.. */
987 old = find_inode(sb, head, test, data);
988 if (!old) {
989 if (set(inode, data))
990 goto set_failed;
991
Dave Chinner250df6e2011-03-22 22:23:36 +1100992 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100993 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100994 hlist_add_head(&inode->i_hash, head);
995 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100996 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100997 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /* Return the locked inode with I_NEW set, the
1000 * caller is responsible for filling in the contents
1001 */
1002 return inode;
1003 }
1004
1005 /*
1006 * Uhhuh, somebody else created the same inode under
1007 * us. Use the old inode instead of the one we just
1008 * allocated.
1009 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001010 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 destroy_inode(inode);
1012 inode = old;
1013 wait_on_inode(inode);
1014 }
1015 return inode;
1016
1017set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +11001018 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 destroy_inode(inode);
1020 return NULL;
1021}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001022EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001024/**
1025 * iget_locked - obtain an inode from a mounted file system
1026 * @sb: super block of file system
1027 * @ino: inode number to get
1028 *
1029 * Search for the inode specified by @ino in the inode cache and if present
1030 * return it with an increased reference count. This is for file systems
1031 * where the inode number is sufficient for unique identification of an inode.
1032 *
1033 * If the inode is not in cache, allocate a new inode and return it locked,
1034 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1035 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001037struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001039 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301040 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001042 spin_lock(&inode_hash_lock);
1043 inode = find_inode_fast(sb, head, ino);
1044 spin_unlock(&inode_hash_lock);
1045 if (inode) {
1046 wait_on_inode(inode);
1047 return inode;
1048 }
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 inode = alloc_inode(sb);
1051 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301052 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Dave Chinner67a23c42011-03-22 22:23:42 +11001054 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* We released the lock, so.. */
1056 old = find_inode_fast(sb, head, ino);
1057 if (!old) {
1058 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001059 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001060 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001061 hlist_add_head(&inode->i_hash, head);
1062 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001063 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001064 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* Return the locked inode with I_NEW set, the
1067 * caller is responsible for filling in the contents
1068 */
1069 return inode;
1070 }
1071
1072 /*
1073 * Uhhuh, somebody else created the same inode under
1074 * us. Use the old inode instead of the one we just
1075 * allocated.
1076 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001077 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 destroy_inode(inode);
1079 inode = old;
1080 wait_on_inode(inode);
1081 }
1082 return inode;
1083}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001084EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001086/*
1087 * search the inode cache for a matching inode number.
1088 * If we find one, then the inode number we are trying to
1089 * allocate is not unique and so we should not use it.
1090 *
1091 * Returns 1 if the inode number is unique, 0 if it is not.
1092 */
1093static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1094{
1095 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1096 struct hlist_node *node;
1097 struct inode *inode;
1098
Dave Chinner67a23c42011-03-22 22:23:42 +11001099 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001100 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001101 if (inode->i_ino == ino && inode->i_sb == sb) {
1102 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001103 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001104 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001105 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001106 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001107
1108 return 1;
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/**
1112 * iunique - get a unique inode number
1113 * @sb: superblock
1114 * @max_reserved: highest reserved inode number
1115 *
1116 * Obtain an inode number that is unique on the system for a given
1117 * superblock. This is used by file systems that have no natural
1118 * permanent inode numbering system. An inode number is returned that
1119 * is higher than the reserved limit but unique.
1120 *
1121 * BUGS:
1122 * With a large number of inodes live on the file system this function
1123 * currently becomes quite slow.
1124 */
1125ino_t iunique(struct super_block *sb, ino_t max_reserved)
1126{
Jeff Layton866b04f2007-05-08 00:32:29 -07001127 /*
1128 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1129 * error if st_ino won't fit in target struct field. Use 32bit counter
1130 * here to attempt to avoid that.
1131 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001132 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001133 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001136 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001137 do {
1138 if (counter <= max_reserved)
1139 counter = max_reserved + 1;
1140 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001141 } while (!test_inode_iunique(sb, res));
1142 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001143
1144 return res;
1145}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146EXPORT_SYMBOL(iunique);
1147
1148struct inode *igrab(struct inode *inode)
1149{
Dave Chinner250df6e2011-03-22 22:23:36 +11001150 spin_lock(&inode->i_lock);
1151 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001153 spin_unlock(&inode->i_lock);
1154 } else {
1155 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Handle the case where s_op->clear_inode is not been
1158 * called yet, and somebody is calling igrab
1159 * while the inode is getting freed.
1160 */
1161 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return inode;
1164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165EXPORT_SYMBOL(igrab);
1166
1167/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001168 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * @sb: super block of file system to search
1170 * @hashval: hash value (usually inode number) to search for
1171 * @test: callback used for comparisons between inodes
1172 * @data: opaque data pointer to pass to @test
1173 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001174 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001176 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001177 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001178 * Note: I_NEW is not waited upon so you have to be very careful what you do
1179 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001180 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001181 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001182 */
1183struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1184 int (*test)(struct inode *, void *), void *data)
1185{
1186 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001187 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001188
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001189 spin_lock(&inode_hash_lock);
1190 inode = find_inode(sb, head, test, data);
1191 spin_unlock(&inode_hash_lock);
1192
1193 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001194}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001195EXPORT_SYMBOL(ilookup5_nowait);
1196
1197/**
1198 * ilookup5 - search for an inode in the inode cache
1199 * @sb: super block of file system to search
1200 * @hashval: hash value (usually inode number) to search for
1201 * @test: callback used for comparisons between inodes
1202 * @data: opaque data pointer to pass to @test
1203 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001204 * Search for the inode specified by @hashval and @data in the inode cache,
1205 * and if the inode is in the cache, return the inode with an incremented
1206 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001207 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001209 * This is a generalized version of ilookup() for file systems where the
1210 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001212 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
1214struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1215 int (*test)(struct inode *, void *), void *data)
1216{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001217 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001219 if (inode)
1220 wait_on_inode(inode);
1221 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223EXPORT_SYMBOL(ilookup5);
1224
1225/**
1226 * ilookup - search for an inode in the inode cache
1227 * @sb: super block of file system to search
1228 * @ino: inode number to search for
1229 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001230 * Search for the inode @ino in the inode cache, and if the inode is in the
1231 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
1233struct inode *ilookup(struct super_block *sb, unsigned long ino)
1234{
1235 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001236 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001238 spin_lock(&inode_hash_lock);
1239 inode = find_inode_fast(sb, head, ino);
1240 spin_unlock(&inode_hash_lock);
1241
1242 if (inode)
1243 wait_on_inode(inode);
1244 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246EXPORT_SYMBOL(ilookup);
1247
Al Viro261bca82008-12-30 01:48:21 -05001248int insert_inode_locked(struct inode *inode)
1249{
1250 struct super_block *sb = inode->i_sb;
1251 ino_t ino = inode->i_ino;
1252 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001253
Al Viro261bca82008-12-30 01:48:21 -05001254 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001255 struct hlist_node *node;
1256 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001257 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001258 hlist_for_each_entry(old, node, head, i_hash) {
1259 if (old->i_ino != ino)
1260 continue;
1261 if (old->i_sb != sb)
1262 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001263 spin_lock(&old->i_lock);
1264 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1265 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001266 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001267 }
Al Viro72a43d62009-05-13 19:13:40 +01001268 break;
1269 }
1270 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001271 spin_lock(&inode->i_lock);
1272 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001273 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001274 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001275 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001276 return 0;
1277 }
1278 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001279 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001280 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001281 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001282 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001283 iput(old);
1284 return -EBUSY;
1285 }
1286 iput(old);
1287 }
1288}
Al Viro261bca82008-12-30 01:48:21 -05001289EXPORT_SYMBOL(insert_inode_locked);
1290
1291int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1292 int (*test)(struct inode *, void *), void *data)
1293{
1294 struct super_block *sb = inode->i_sb;
1295 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001296
Al Viro261bca82008-12-30 01:48:21 -05001297 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001298 struct hlist_node *node;
1299 struct inode *old = NULL;
1300
Dave Chinner67a23c42011-03-22 22:23:42 +11001301 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001302 hlist_for_each_entry(old, node, head, i_hash) {
1303 if (old->i_sb != sb)
1304 continue;
1305 if (!test(old, data))
1306 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001307 spin_lock(&old->i_lock);
1308 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1309 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001310 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001311 }
Al Viro72a43d62009-05-13 19:13:40 +01001312 break;
1313 }
1314 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001315 spin_lock(&inode->i_lock);
1316 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001317 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001318 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001319 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001320 return 0;
1321 }
1322 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001323 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001324 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001325 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001326 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001327 iput(old);
1328 return -EBUSY;
1329 }
1330 iput(old);
1331 }
1332}
Al Viro261bca82008-12-30 01:48:21 -05001333EXPORT_SYMBOL(insert_inode_locked4);
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Al Viro45321ac2010-06-07 13:43:19 -04001336int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Al Viro45321ac2010-06-07 13:43:19 -04001338 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340EXPORT_SYMBOL(generic_delete_inode);
1341
Al Viro45321ac2010-06-07 13:43:19 -04001342/*
1343 * Normal UNIX filesystem behaviour: delete the
1344 * inode when the usage count drops to zero, and
1345 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001346 */
Al Viro45321ac2010-06-07 13:43:19 -04001347int generic_drop_inode(struct inode *inode)
1348{
Al Viro1d3382c2010-10-23 15:19:20 -04001349 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001350}
1351EXPORT_SYMBOL_GPL(generic_drop_inode);
1352
1353/*
1354 * Called when we're dropping the last reference
1355 * to an inode.
1356 *
1357 * Call the FS "drop_inode()" function, defaulting to
1358 * the legacy UNIX filesystem behaviour. If it tells
1359 * us to evict inode, do so. Otherwise, retain inode
1360 * in cache if fs is alive, sync and evict if fs is
1361 * shutting down.
1362 */
1363static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001366 const struct super_operations *op = inode->i_sb->s_op;
1367 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Dave Chinner250df6e2011-03-22 22:23:36 +11001369 WARN_ON(inode->i_state & I_NEW);
1370
Al Viro45321ac2010-06-07 13:43:19 -04001371 if (op && op->drop_inode)
1372 drop = op->drop_inode(inode);
1373 else
1374 drop = generic_drop_inode(inode);
1375
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001376 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1377 inode->i_state |= I_REFERENCED;
1378 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1379 inode_lru_list_add(inode);
1380 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001381 return;
1382 }
1383
Al Viro45321ac2010-06-07 13:43:19 -04001384 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001385 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001386 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001388 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001389 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001390 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001392
Alexander Viro991114c2005-06-23 00:09:01 -07001393 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001394 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001395 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001396
1397 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301401 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * @inode: inode to put
1403 *
1404 * Puts an inode, dropping its usage count. If the inode use count hits
1405 * zero, the inode is then freed and may also be destroyed.
1406 *
1407 * Consequently, iput() can sleep.
1408 */
1409void iput(struct inode *inode)
1410{
1411 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001412 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Dave Chinnerf283c862011-03-22 22:23:39 +11001414 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 iput_final(inode);
1416 }
1417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418EXPORT_SYMBOL(iput);
1419
1420/**
1421 * bmap - find a block number in a file
1422 * @inode: inode of file
1423 * @block: block to find
1424 *
1425 * Returns the block number on the device holding the inode that
1426 * is the disk block number for the block of the file requested.
1427 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301428 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * file.
1430 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301431sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 sector_t res = 0;
1434 if (inode->i_mapping->a_ops->bmap)
1435 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1436 return res;
1437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438EXPORT_SYMBOL(bmap);
1439
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001440/*
1441 * With relative atime, only update atime if the previous atime is
1442 * earlier than either the ctime or mtime or if at least a day has
1443 * passed since the last atime update.
1444 */
1445static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1446 struct timespec now)
1447{
1448
1449 if (!(mnt->mnt_flags & MNT_RELATIME))
1450 return 1;
1451 /*
1452 * Is mtime younger than atime? If yes, update atime:
1453 */
1454 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1455 return 1;
1456 /*
1457 * Is ctime younger than atime? If yes, update atime:
1458 */
1459 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1460 return 1;
1461
1462 /*
1463 * Is the previous atime value older than a day? If yes,
1464 * update atime:
1465 */
1466 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1467 return 1;
1468 /*
1469 * Good, we can skip the atime update:
1470 */
1471 return 0;
1472}
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001475 * touch_atime - update the access time
1476 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001477 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 *
1479 * Update the accessed time on an inode and mark it for writeback.
1480 * This function automatically handles read only file systems and media,
1481 * as well as the "noatime" flag and inode specific "noatime" markers.
1482 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001483void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001485 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct timespec now;
1487
Andrew Mortonb2276132006-12-13 00:34:33 -08001488 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001489 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001490 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001491 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001492 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001493 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001494
Dave Hansencdb70f32008-02-15 14:37:41 -08001495 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001496 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001497 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001498 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001501
1502 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001503 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001504
Valerie Henson47ae32d2006-12-13 00:34:34 -08001505 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001506 return;
1507
1508 if (mnt_want_write(mnt))
1509 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001510
1511 inode->i_atime = now;
1512 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001513 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001515EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001518 * file_update_time - update mtime and ctime time
1519 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001521 * Update the mtime and ctime members of an inode and mark the inode
1522 * for writeback. Note that this function is meant exclusively for
1523 * usage in the file write path of filesystems, and filesystems may
1524 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001525 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001526 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
1528
Christoph Hellwig870f4812006-01-09 20:52:01 -08001529void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001531 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001533 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Andi Kleence06e0b2009-09-18 13:05:48 -07001535 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (IS_NOCMTIME(inode))
1537 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001538
Andi Kleence06e0b2009-09-18 13:05:48 -07001539 now = current_fs_time(inode->i_sb);
1540 if (!timespec_equal(&inode->i_mtime, &now))
1541 sync_it = S_MTIME;
1542
1543 if (!timespec_equal(&inode->i_ctime, &now))
1544 sync_it |= S_CTIME;
1545
1546 if (IS_I_VERSION(inode))
1547 sync_it |= S_VERSION;
1548
1549 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return;
1551
Andi Kleence06e0b2009-09-18 13:05:48 -07001552 /* Finally allowed to write? Takes lock. */
1553 if (mnt_want_write_file(file))
1554 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Andi Kleence06e0b2009-09-18 13:05:48 -07001556 /* Only change inode inside the lock region */
1557 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001558 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001559 if (sync_it & S_CTIME)
1560 inode->i_ctime = now;
1561 if (sync_it & S_MTIME)
1562 inode->i_mtime = now;
1563 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001564 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001566EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568int inode_needs_sync(struct inode *inode)
1569{
1570 if (IS_SYNC(inode))
1571 return 1;
1572 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1573 return 1;
1574 return 0;
1575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576EXPORT_SYMBOL(inode_needs_sync);
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578int inode_wait(void *word)
1579{
1580 schedule();
1581 return 0;
1582}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001583EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001586 * If we try to find an inode in the inode hash while it is being
1587 * deleted, we have to wait until the filesystem completes its
1588 * deletion before reporting that it isn't found. This function waits
1589 * until the deletion _might_ have completed. Callers are responsible
1590 * to recheck inode state.
1591 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001592 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001593 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1594 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
1596static void __wait_on_freeing_inode(struct inode *inode)
1597{
1598 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001599 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1600 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001602 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001603 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 schedule();
1605 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001606 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static __initdata unsigned long ihash_entries;
1610static int __init set_ihash_entries(char *str)
1611{
1612 if (!str)
1613 return 0;
1614 ihash_entries = simple_strtoul(str, &str, 0);
1615 return 1;
1616}
1617__setup("ihash_entries=", set_ihash_entries);
1618
1619/*
1620 * Initialize the waitqueues and inode hash table.
1621 */
1622void __init inode_init_early(void)
1623{
1624 int loop;
1625
1626 /* If hashes are distributed across NUMA nodes, defer
1627 * hash allocation until vmalloc space is available.
1628 */
1629 if (hashdist)
1630 return;
1631
1632 inode_hashtable =
1633 alloc_large_system_hash("Inode-cache",
1634 sizeof(struct hlist_head),
1635 ihash_entries,
1636 14,
1637 HASH_EARLY,
1638 &i_hash_shift,
1639 &i_hash_mask,
1640 0);
1641
1642 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1643 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1644}
1645
Denis Cheng74bf17c2007-10-16 23:26:30 -07001646void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 int loop;
1649
1650 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001651 inode_cachep = kmem_cache_create("inode_cache",
1652 sizeof(struct inode),
1653 0,
1654 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1655 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001656 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001657 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /* Hash may have been set up in inode_init_early */
1660 if (!hashdist)
1661 return;
1662
1663 inode_hashtable =
1664 alloc_large_system_hash("Inode-cache",
1665 sizeof(struct hlist_head),
1666 ihash_entries,
1667 14,
1668 0,
1669 &i_hash_shift,
1670 &i_hash_mask,
1671 0);
1672
1673 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1674 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1675}
1676
1677void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1678{
1679 inode->i_mode = mode;
1680 if (S_ISCHR(mode)) {
1681 inode->i_fop = &def_chr_fops;
1682 inode->i_rdev = rdev;
1683 } else if (S_ISBLK(mode)) {
1684 inode->i_fop = &def_blk_fops;
1685 inode->i_rdev = rdev;
1686 } else if (S_ISFIFO(mode))
1687 inode->i_fop = &def_fifo_fops;
1688 else if (S_ISSOCK(mode))
1689 inode->i_fop = &bad_sock_fops;
1690 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001691 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1692 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1693 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001696
1697/**
Ben Hutchingseaae6682011-02-15 12:48:09 +00001698 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001699 * @inode: New inode
1700 * @dir: Directory inode
1701 * @mode: mode of the new inode
1702 */
1703void inode_init_owner(struct inode *inode, const struct inode *dir,
1704 mode_t mode)
1705{
1706 inode->i_uid = current_fsuid();
1707 if (dir && dir->i_mode & S_ISGID) {
1708 inode->i_gid = dir->i_gid;
1709 if (S_ISDIR(mode))
1710 mode |= S_ISGID;
1711 } else
1712 inode->i_gid = current_fsgid();
1713 inode->i_mode = mode;
1714}
1715EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001716
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001717/**
1718 * inode_owner_or_capable - check current task permissions to inode
1719 * @inode: inode being checked
1720 *
1721 * Return true if current either has CAP_FOWNER to the inode, or
1722 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001723 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001724bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001725{
1726 struct user_namespace *ns = inode_userns(inode);
1727
1728 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1729 return true;
1730 if (ns_capable(ns, CAP_FOWNER))
1731 return true;
1732 return false;
1733}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001734EXPORT_SYMBOL(inode_owner_or_capable);