blob: 24d02907e19617751ca83a7eb6e5eca2ca66bcf7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 1997 Linus Torvalds
Christoph Hellwig4b4563d2011-05-27 09:28:01 -04003 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/fs.h>
6#include <linux/mm.h>
7#include <linux/dcache.h>
8#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
10#include <linux/writeback.h>
11#include <linux/module.h>
12#include <linux/backing-dev.h>
13#include <linux/wait.h>
Nick Piggin88e0fbc2009-09-22 16:43:50 -070014#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/hash.h>
16#include <linux/swap.h>
17#include <linux/security.h>
18#include <linux/pagemap.h>
19#include <linux/cdev.h>
20#include <linux/bootmem.h>
Eric Paris3be25f42009-05-21 17:01:26 -040021#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080022#include <linux/mount.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080023#include <linux/async.h>
Al Virof19d4a82009-06-08 19:50:45 -040024#include <linux/posix_acl.h>
Heiko Carstens9ce6e0b2011-05-22 18:54:21 +020025#include <linux/prefetch.h>
Eric Parisa178d202010-10-25 14:41:59 -040026#include <linux/ima.h>
Serge E. Hallyne795b712011-03-23 16:43:25 -070027#include <linux/cred.h>
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040028#include <linux/buffer_head.h> /* for inode_has_buffers */
Dave Chinnera66979a2011-03-22 22:23:41 +110029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040032 * Inode locking rules:
Dave Chinner250df6e2011-03-22 22:23:36 +110033 *
34 * inode->i_lock protects:
35 * inode->i_state, inode->i_hash, __iget()
Dave Chinner09cc9fc2011-07-08 14:14:40 +100036 * inode->i_sb->s_inode_lru_lock protects:
Dave Chinner98b745c2011-07-08 14:14:39 +100037 * inode->i_sb->s_inode_lru, inode->i_lru
Dave Chinner55fa6092011-03-22 22:23:40 +110038 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
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 Chinner09cc9fc2011-07-08 14:14:40 +100049 * inode->i_sb->s_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
Dave Chinner55fa6092011-03-22 22:23:40 +110067__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020070 * Empty aops. Can be used for the cases where the user does not
71 * define any of the address_space operations.
72 */
73const struct address_space_operations empty_aops = {
74};
75EXPORT_SYMBOL(empty_aops);
76
77/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * Statistics gathering..
79 */
80struct inodes_stat_t inodes_stat;
81
Nick Piggin3e880fb2011-01-07 17:49:19 +110082static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnerfcb94f72011-07-08 14:14:38 +100083static DEFINE_PER_CPU(unsigned int, nr_unused);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040084
Manish Katiyar6b3304b2009-03-31 19:35:54 +053085static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Nick Piggin3e880fb2011-01-07 17:49:19 +110087static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040088{
Nick Piggin3e880fb2011-01-07 17:49:19 +110089 int i;
90 int sum = 0;
91 for_each_possible_cpu(i)
92 sum += per_cpu(nr_inodes, i);
93 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -040094}
95
96static inline int get_nr_inodes_unused(void)
97{
Dave Chinnerfcb94f72011-07-08 14:14:38 +100098 int i;
99 int sum = 0;
100 for_each_possible_cpu(i)
101 sum += per_cpu(nr_unused, i);
102 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400103}
104
105int get_nr_dirty_inodes(void)
106{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100107 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400108 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
109 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400110}
111
112/*
113 * Handle nr_inode sysctl
114 */
115#ifdef CONFIG_SYSCTL
116int proc_nr_inodes(ctl_table *table, int write,
117 void __user *buffer, size_t *lenp, loff_t *ppos)
118{
119 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000120 inodes_stat.nr_unused = get_nr_inodes_unused();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400121 return proc_dointvec(table, write, buffer, lenp, ppos);
122}
123#endif
124
David Chinner2cb15992008-10-30 17:32:23 +1100125/**
126 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800127 * @sb: superblock inode belongs to
128 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100129 *
130 * These are initializations that need to be done on every inode
131 * allocation as the fields are not initialised by slab allocation.
132 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300133int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700135 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800136 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530137 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100138
139 inode->i_sb = sb;
140 inode->i_blkbits = sb->s_blocksize_bits;
141 inode->i_flags = 0;
142 atomic_set(&inode->i_count, 1);
143 inode->i_op = &empty_iops;
144 inode->i_fop = &empty_fops;
Miklos Szeredia78ef702011-10-28 14:13:30 +0200145 inode->__i_nlink = 1;
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700146 inode->i_opflags = 0;
Al Viro56ff5ef2008-12-09 09:34:39 -0500147 inode->i_uid = 0;
148 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100149 atomic_set(&inode->i_writecount, 0);
150 inode->i_size = 0;
151 inode->i_blocks = 0;
152 inode->i_bytes = 0;
153 inode->i_generation = 0;
154#ifdef CONFIG_QUOTA
155 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
156#endif
157 inode->i_pipe = NULL;
158 inode->i_bdev = NULL;
159 inode->i_cdev = NULL;
160 inode->i_rdev = 0;
161 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500162
163 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300164 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100165 spin_lock_init(&inode->i_lock);
166 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
167
168 mutex_init(&inode->i_mutex);
169 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
170
Christoph Hellwigbd5fe6c2011-06-24 14:29:43 -0400171 atomic_set(&inode->i_dio_count, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100172
173 mapping->a_ops = &empty_aops;
174 mapping->host = inode;
175 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800176 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100177 mapping->assoc_mapping = NULL;
178 mapping->backing_dev_info = &default_backing_dev_info;
179 mapping->writeback_index = 0;
180
181 /*
182 * If the block_device provides a backing_dev_info for client
183 * inodes then use that. Otherwise the inode share the bdev's
184 * backing_dev_info.
185 */
186 if (sb->s_bdev) {
187 struct backing_dev_info *bdi;
188
Jens Axboe2c96ce92009-09-15 09:43:56 +0200189 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100190 mapping->backing_dev_info = bdi;
191 }
192 inode->i_private = NULL;
193 inode->i_mapping = mapping;
Al Viro6b520e02011-12-12 15:51:45 -0500194 INIT_LIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */
Al Virof19d4a82009-06-08 19:50:45 -0400195#ifdef CONFIG_FS_POSIX_ACL
196 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
197#endif
David Chinner2cb15992008-10-30 17:32:23 +1100198
Eric Paris3be25f42009-05-21 17:01:26 -0400199#ifdef CONFIG_FSNOTIFY
200 inode->i_fsnotify_mask = 0;
201#endif
202
Nick Piggin3e880fb2011-01-07 17:49:19 +1100203 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400204
Christoph Hellwig54e34622009-08-07 14:38:25 -0300205 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300206out:
207 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100208}
209EXPORT_SYMBOL(inode_init_always);
210
211static struct inode *alloc_inode(struct super_block *sb)
212{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct inode *inode;
214
215 if (sb->s_op->alloc_inode)
216 inode = sb->s_op->alloc_inode(sb);
217 else
David Chinner2cb15992008-10-30 17:32:23 +1100218 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Christoph Hellwig54e34622009-08-07 14:38:25 -0300220 if (!inode)
221 return NULL;
222
223 if (unlikely(inode_init_always(sb, inode))) {
224 if (inode->i_sb->s_op->destroy_inode)
225 inode->i_sb->s_op->destroy_inode(inode);
226 else
227 kmem_cache_free(inode_cachep, inode);
228 return NULL;
229 }
230
231 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Nick Pigginff0c7d12011-01-07 17:49:50 +1100234void free_inode_nonrcu(struct inode *inode)
235{
236 kmem_cache_free(inode_cachep, inode);
237}
238EXPORT_SYMBOL(free_inode_nonrcu);
239
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300240void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200242 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400244 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400245#ifdef CONFIG_FS_POSIX_ACL
246 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
247 posix_acl_release(inode->i_acl);
248 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
249 posix_acl_release(inode->i_default_acl);
250#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100251 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300252}
253EXPORT_SYMBOL(__destroy_inode);
254
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100255static void i_callback(struct rcu_head *head)
256{
257 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100258 kmem_cache_free(inode_cachep, inode);
259}
260
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200261static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300262{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100263 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300264 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (inode->i_sb->s_op->destroy_inode)
266 inode->i_sb->s_op->destroy_inode(inode);
267 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100268 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100271void address_space_init_once(struct address_space *mapping)
272{
273 memset(mapping, 0, sizeof(*mapping));
274 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
275 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700276 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100277 INIT_LIST_HEAD(&mapping->private_list);
278 spin_lock_init(&mapping->private_lock);
279 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
280 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100281}
282EXPORT_SYMBOL(address_space_init_once);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * These are initializations that only need to be done
286 * once, because the fields are idempotent across use
287 * of the inode, so let the slab aware of that.
288 */
289void inode_init_once(struct inode *inode)
290{
291 memset(inode, 0, sizeof(*inode));
292 INIT_HLIST_NODE(&inode->i_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100294 INIT_LIST_HEAD(&inode->i_wb_list);
295 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100296 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400298#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500299 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302EXPORT_SYMBOL(inode_init_once);
303
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700304static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530306 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Christoph Lametera35afb82007-05-16 22:10:57 -0700308 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100312 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530314void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Nick Piggin9e38d862010-10-23 06:55:17 -0400316 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Al Viro7de9c6ee2010-10-23 11:11:40 -0400319/*
320 * get additional reference to inode; caller must already hold one.
321 */
322void ihold(struct inode *inode)
323{
324 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
325}
326EXPORT_SYMBOL(ihold);
327
Nick Piggin9e38d862010-10-23 06:55:17 -0400328static void inode_lru_list_add(struct inode *inode)
329{
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000330 spin_lock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100331 if (list_empty(&inode->i_lru)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000332 list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
333 inode->i_sb->s_nr_inodes_unused++;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000334 this_cpu_inc(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400335 }
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000336 spin_unlock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400337}
338
339static void inode_lru_list_del(struct inode *inode)
340{
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000341 spin_lock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100342 if (!list_empty(&inode->i_lru)) {
343 list_del_init(&inode->i_lru);
Dave Chinner98b745c2011-07-08 14:14:39 +1000344 inode->i_sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000345 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400346 }
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000347 spin_unlock(&inode->i_sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Christoph Hellwig646ec462010-10-23 07:15:32 -0400350/**
351 * inode_sb_list_add - add inode to the superblock list of inodes
352 * @inode: inode to add
353 */
354void inode_sb_list_add(struct inode *inode)
355{
Dave Chinner55fa6092011-03-22 22:23:40 +1100356 spin_lock(&inode_sb_list_lock);
357 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
358 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400359}
360EXPORT_SYMBOL_GPL(inode_sb_list_add);
361
Dave Chinner55fa6092011-03-22 22:23:40 +1100362static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400363{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200364 if (!list_empty(&inode->i_sb_list)) {
365 spin_lock(&inode_sb_list_lock);
366 list_del_init(&inode->i_sb_list);
367 spin_unlock(&inode_sb_list_lock);
368 }
Christoph Hellwig646ec462010-10-23 07:15:32 -0400369}
370
Dave Chinner4c51acb2010-10-23 06:58:09 -0400371static unsigned long hash(struct super_block *sb, unsigned long hashval)
372{
373 unsigned long tmp;
374
375 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
376 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400377 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
378 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400379}
380
381/**
382 * __insert_inode_hash - hash an inode
383 * @inode: unhashed inode
384 * @hashval: unsigned long value used to locate this object in the
385 * inode_hashtable.
386 *
387 * Add an inode to the inode hash for this superblock.
388 */
389void __insert_inode_hash(struct inode *inode, unsigned long hashval)
390{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400391 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
392
Dave Chinner67a23c42011-03-22 22:23:42 +1100393 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100394 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400395 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100396 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100397 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400398}
399EXPORT_SYMBOL(__insert_inode_hash);
400
401/**
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200402 * __remove_inode_hash - remove an inode from the hash
Dave Chinner4c51acb2010-10-23 06:58:09 -0400403 * @inode: inode to unhash
404 *
405 * Remove an inode from the superblock.
406 */
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200407void __remove_inode_hash(struct inode *inode)
Dave Chinner4c51acb2010-10-23 06:58:09 -0400408{
Dave Chinner67a23c42011-03-22 22:23:42 +1100409 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100410 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400411 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100412 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100413 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400414}
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200415EXPORT_SYMBOL(__remove_inode_hash);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400416
Al Virob0683aa2010-06-04 20:55:25 -0400417void end_writeback(struct inode *inode)
418{
419 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700420 /*
421 * We have to cycle tree_lock here because reclaim can be still in the
422 * process of removing the last page (in __delete_from_page_cache())
423 * and we must not free mapping under it.
424 */
425 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400426 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700427 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400428 BUG_ON(!list_empty(&inode->i_data.private_list));
429 BUG_ON(!(inode->i_state & I_FREEING));
430 BUG_ON(inode->i_state & I_CLEAR);
431 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100432 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400433 inode->i_state = I_FREEING | I_CLEAR;
434}
435EXPORT_SYMBOL(end_writeback);
436
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100437/*
438 * Free the inode passed in, removing it from the lists it is still connected
439 * to. We remove any pages still attached to the inode and wait for any IO that
440 * is still in progress before finally destroying the inode.
441 *
442 * An inode must already be marked I_FREEING so that we avoid the inode being
443 * moved back onto lists if we race with other code that manipulates the lists
444 * (e.g. writeback_single_inode). The caller is responsible for setting this.
445 *
446 * An inode must already be removed from the LRU list before being evicted from
447 * the cache. This should occur atomically with setting the I_FREEING state
448 * flag, so no inodes here should ever be on the LRU when being evicted.
449 */
Al Viro644da592010-06-07 13:21:05 -0400450static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400451{
452 const struct super_operations *op = inode->i_sb->s_op;
453
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100454 BUG_ON(!(inode->i_state & I_FREEING));
455 BUG_ON(!list_empty(&inode->i_lru));
456
Eric Dumazetb12362b2011-07-28 06:11:47 +0200457 if (!list_empty(&inode->i_wb_list))
458 inode_wb_list_del(inode);
459
Dave Chinner55fa6092011-03-22 22:23:40 +1100460 inode_sb_list_del(inode);
461
Al Virobe7ce412010-06-04 19:40:39 -0400462 if (op->evict_inode) {
463 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400464 } else {
465 if (inode->i_data.nrpages)
466 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400467 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400468 }
Al Viro661074e2010-06-04 20:19:55 -0400469 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
470 bd_forget(inode);
471 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
472 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100473
474 remove_inode_hash(inode);
475
476 spin_lock(&inode->i_lock);
477 wake_up_bit(&inode->i_state, __I_NEW);
478 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
479 spin_unlock(&inode->i_lock);
480
481 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * dispose_list - dispose of the contents of a local list
486 * @head: the head of the list to free
487 *
488 * Dispose-list gets a local list with local inodes in it, so it doesn't
489 * need to worry about list corruption and SMP locks.
490 */
491static void dispose_list(struct list_head *head)
492{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 while (!list_empty(head)) {
494 struct inode *inode;
495
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100496 inode = list_first_entry(head, struct inode, i_lru);
497 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Al Viro644da592010-06-07 13:21:05 -0400499 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/**
Al Viro63997e92010-10-25 20:49:35 -0400504 * evict_inodes - evict all evictable inodes for a superblock
505 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
Al Viro63997e92010-10-25 20:49:35 -0400507 * Make sure that no inodes with zero refcount are retained. This is
508 * called by superblock shutdown after having MS_ACTIVE flag removed,
509 * so any inode reaching zero refcount during or after that call will
510 * be immediately evicted.
511 */
512void evict_inodes(struct super_block *sb)
513{
514 struct inode *inode, *next;
515 LIST_HEAD(dispose);
516
Dave Chinner55fa6092011-03-22 22:23:40 +1100517 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400518 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
519 if (atomic_read(&inode->i_count))
520 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100521
522 spin_lock(&inode->i_lock);
523 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
524 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400525 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100526 }
Al Viro63997e92010-10-25 20:49:35 -0400527
528 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100529 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100530 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100531 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400532 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100533 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400534
535 dispose_list(&dispose);
Al Viro63997e92010-10-25 20:49:35 -0400536}
537
538/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200539 * invalidate_inodes - attempt to free all inodes on a superblock
540 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100541 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200542 *
543 * Attempts to free all inodes for a given superblock. If there were any
544 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100545 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
546 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
NeilBrown93b270f2011-02-24 17:25:47 +1100548int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400550 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200551 struct inode *inode, *next;
552 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Dave Chinner55fa6092011-03-22 22:23:40 +1100554 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200555 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100556 spin_lock(&inode->i_lock);
557 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
558 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100560 }
NeilBrown93b270f2011-02-24 17:25:47 +1100561 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100562 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100563 busy = 1;
564 continue;
565 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200566 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100567 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200568 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 continue;
570 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200571
Christoph Hellwig99a38912010-10-23 19:07:20 +0200572 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100573 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100574 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100575 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100577 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Christoph Hellwiga0318782010-10-24 19:40:33 +0200579 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 return busy;
582}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584static int can_unuse(struct inode *inode)
585{
Nick Piggin9e38d862010-10-23 06:55:17 -0400586 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
588 if (inode_has_buffers(inode))
589 return 0;
590 if (atomic_read(&inode->i_count))
591 return 0;
592 if (inode->i_data.nrpages)
593 return 0;
594 return 1;
595}
596
597/*
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000598 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
599 * This is called from the superblock shrinker function with a number of inodes
600 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
601 * then are freed outside inode_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *
603 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400604 * pagecache removed. If the inode has metadata buffers attached to
605 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400607 * If the inode has the I_REFERENCED flag set, then it means that it has been
608 * used recently - the flag is set in iput_final(). When we encounter such an
609 * inode, clear the flag and move it to the back of the LRU so it gets another
610 * pass through the LRU before it gets reclaimed. This is necessary because of
611 * the fact we are doing lazy LRU updates to minimise lock contention so the
612 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
613 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000615void prune_icache_sb(struct super_block *sb, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int nr_scanned;
619 unsigned long reap = 0;
620
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000621 spin_lock(&sb->s_inode_lru_lock);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000622 for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct inode *inode;
624
Dave Chinner98b745c2011-07-08 14:14:39 +1000625 if (list_empty(&sb->s_inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
627
Dave Chinner98b745c2011-07-08 14:14:39 +1000628 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Nick Piggin9e38d862010-10-23 06:55:17 -0400630 /*
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000631 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
Dave Chinner02afc412011-03-22 22:23:38 +1100632 * so use a trylock. If we fail to get the lock, just move the
633 * inode to the back of the list so we don't spin on it.
634 */
635 if (!spin_trylock(&inode->i_lock)) {
Christoph Hellwig62a3dde2011-10-28 10:03:41 +0200636 list_move_tail(&inode->i_lru, &sb->s_inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +1100637 continue;
638 }
639
640 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400641 * Referenced or dirty inodes are still in use. Give them
642 * another pass through the LRU as we canot reclaim them now.
643 */
644 if (atomic_read(&inode->i_count) ||
645 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100646 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100647 spin_unlock(&inode->i_lock);
Dave Chinner98b745c2011-07-08 14:14:39 +1000648 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000649 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400650 continue;
651 }
652
653 /* recently referenced inodes get one more pass */
654 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400655 inode->i_state &= ~I_REFERENCED;
Dave Chinner98b745c2011-07-08 14:14:39 +1000656 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100657 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 continue;
659 }
660 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
661 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100662 spin_unlock(&inode->i_lock);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000663 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800665 reap += invalidate_mapping_pages(&inode->i_data,
666 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 iput(inode);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000668 spin_lock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Dave Chinner98b745c2011-07-08 14:14:39 +1000670 if (inode != list_entry(sb->s_inode_lru.next,
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100671 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100673 /* avoid lock inversions with trylock */
674 if (!spin_trylock(&inode->i_lock))
675 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100676 if (!can_unuse(inode)) {
677 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700681 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100683 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100684
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100685 list_move(&inode->i_lru, &freeable);
Dave Chinner98b745c2011-07-08 14:14:39 +1000686 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000687 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700689 if (current_is_kswapd())
690 __count_vm_events(KSWAPD_INODESTEAL, reap);
691 else
692 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000693 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 dispose_list(&freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static void __wait_on_freeing_inode(struct inode *inode);
699/*
700 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530702static struct inode *find_inode(struct super_block *sb,
703 struct hlist_head *head,
704 int (*test)(struct inode *, void *),
705 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530708 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700711 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100712 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100713 if (inode->i_sb != sb) {
714 spin_unlock(&inode->i_lock);
715 continue;
716 }
717 if (!test(inode, data)) {
718 spin_unlock(&inode->i_lock);
719 continue;
720 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400721 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 __wait_on_freeing_inode(inode);
723 goto repeat;
724 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400725 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100726 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400727 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400729 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/*
733 * find_inode_fast is the fast path version of find_inode, see the comment at
734 * iget_locked for details.
735 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530736static struct inode *find_inode_fast(struct super_block *sb,
737 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530740 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700743 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100744 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100745 if (inode->i_ino != ino) {
746 spin_unlock(&inode->i_lock);
747 continue;
748 }
749 if (inode->i_sb != sb) {
750 spin_unlock(&inode->i_lock);
751 continue;
752 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400753 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 __wait_on_freeing_inode(inode);
755 goto repeat;
756 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400757 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100758 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400759 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400761 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Eric Dumazetf991bd22010-10-23 11:18:01 -0400764/*
765 * Each cpu owns a range of LAST_INO_BATCH numbers.
766 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
767 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100768 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400769 * This does not significantly increase overflow rate because every CPU can
770 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
771 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
772 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
773 * overflow rate by 2x, which does not seem too significant.
774 *
775 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
776 * error if st_ino won't fit in target struct field. Use 32bit counter
777 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100778 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400779#define LAST_INO_BATCH 1024
780static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100781
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400782unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400783{
784 unsigned int *p = &get_cpu_var(last_ino);
785 unsigned int res = *p;
786
787#ifdef CONFIG_SMP
788 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
789 static atomic_t shared_last_ino;
790 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
791
792 res = next - LAST_INO_BATCH;
793 }
794#endif
795
796 *p = ++res;
797 put_cpu_var(last_ino);
798 return res;
David Chinner8290c352008-10-30 17:35:24 +1100799}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400800EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/**
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200803 * new_inode_pseudo - obtain an inode
804 * @sb: superblock
805 *
806 * Allocates a new inode for given superblock.
807 * Inode wont be chained in superblock s_inodes list
808 * This means :
809 * - fs can't be unmount
810 * - quotas, fsnotify, writeback can't work
811 */
812struct inode *new_inode_pseudo(struct super_block *sb)
813{
814 struct inode *inode = alloc_inode(sb);
815
816 if (inode) {
817 spin_lock(&inode->i_lock);
818 inode->i_state = 0;
819 spin_unlock(&inode->i_lock);
820 INIT_LIST_HEAD(&inode->i_sb_list);
821 }
822 return inode;
823}
824
825/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * new_inode - obtain an inode
827 * @sb: superblock
828 *
Mel Gorman769848c2007-07-17 04:03:05 -0700829 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800830 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700831 * If HIGHMEM pages are unsuitable or it is known that pages allocated
832 * for the page cache are not reclaimable or migratable,
833 * mapping_set_gfp_mask() must be called with suitable flags on the
834 * newly created inode's mapping
835 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
837struct inode *new_inode(struct super_block *sb)
838{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530839 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dave Chinner55fa6092011-03-22 22:23:40 +1100841 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530842
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200843 inode = new_inode_pseudo(sb);
844 if (inode)
Dave Chinner55fa6092011-03-22 22:23:40 +1100845 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return inode;
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848EXPORT_SYMBOL(new_inode);
849
Peter Zijlstra14358e62007-10-14 01:38:33 +0200850#ifdef CONFIG_DEBUG_LOCK_ALLOC
Josh Boyere096d0c2011-08-25 07:48:12 -0400851void lockdep_annotate_inode_mutex_key(struct inode *inode)
852{
Namhyung Kima3314a02010-10-11 22:38:00 +0900853 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200854 struct file_system_type *type = inode->i_sb->s_type;
855
Jan Kara9a7aa122009-06-04 15:26:49 +0200856 /* Set new key only if filesystem hasn't already changed it */
857 if (!lockdep_match_class(&inode->i_mutex,
858 &type->i_mutex_key)) {
859 /*
860 * ensure nobody is actually holding i_mutex
861 */
862 mutex_destroy(&inode->i_mutex);
863 mutex_init(&inode->i_mutex);
864 lockdep_set_class(&inode->i_mutex,
865 &type->i_mutex_dir_key);
866 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200867 }
Josh Boyere096d0c2011-08-25 07:48:12 -0400868}
869EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
Peter Zijlstra14358e62007-10-14 01:38:33 +0200870#endif
Josh Boyere096d0c2011-08-25 07:48:12 -0400871
872/**
873 * unlock_new_inode - clear the I_NEW state and wake up any waiters
874 * @inode: new inode to unlock
875 *
876 * Called when the inode is fully initialised to clear the new state of the
877 * inode and wake up anyone waiting for the inode to finish initialisation.
878 */
879void unlock_new_inode(struct inode *inode)
880{
881 lockdep_annotate_inode_mutex_key(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100882 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100883 WARN_ON(!(inode->i_state & I_NEW));
884 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100885 wake_up_bit(&inode->i_state, __I_NEW);
886 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888EXPORT_SYMBOL(unlock_new_inode);
889
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400890/**
891 * iget5_locked - obtain an inode from a mounted file system
892 * @sb: super block of file system
893 * @hashval: hash value (usually inode number) to get
894 * @test: callback used for comparisons between inodes
895 * @set: callback used to initialize a new struct inode
896 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400898 * Search for the inode specified by @hashval and @data in the inode cache,
899 * and if present it is return it with an increased reference count. This is
900 * a generalized version of iget_locked() for file systems where the inode
901 * number is not sufficient for unique identification of an inode.
902 *
903 * If the inode is not in cache, allocate a new inode and return it locked,
904 * hashed, and with the I_NEW flag set. The file system gets to fill it in
905 * before unlocking it via unlock_new_inode().
906 *
907 * Note both @test and @set are called with the inode_hash_lock held, so can't
908 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400910struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
911 int (*test)(struct inode *, void *),
912 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400914 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530915 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400917 spin_lock(&inode_hash_lock);
918 inode = find_inode(sb, head, test, data);
919 spin_unlock(&inode_hash_lock);
920
921 if (inode) {
922 wait_on_inode(inode);
923 return inode;
924 }
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 inode = alloc_inode(sb);
927 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530928 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dave Chinner67a23c42011-03-22 22:23:42 +1100930 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* We released the lock, so.. */
932 old = find_inode(sb, head, test, data);
933 if (!old) {
934 if (set(inode, data))
935 goto set_failed;
936
Dave Chinner250df6e2011-03-22 22:23:36 +1100937 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100938 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100939 hlist_add_head(&inode->i_hash, head);
940 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100941 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100942 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* Return the locked inode with I_NEW set, the
945 * caller is responsible for filling in the contents
946 */
947 return inode;
948 }
949
950 /*
951 * Uhhuh, somebody else created the same inode under
952 * us. Use the old inode instead of the one we just
953 * allocated.
954 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100955 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 destroy_inode(inode);
957 inode = old;
958 wait_on_inode(inode);
959 }
960 return inode;
961
962set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100963 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 destroy_inode(inode);
965 return NULL;
966}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400967EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400969/**
970 * iget_locked - obtain an inode from a mounted file system
971 * @sb: super block of file system
972 * @ino: inode number to get
973 *
974 * Search for the inode specified by @ino in the inode cache and if present
975 * return it with an increased reference count. This is for file systems
976 * where the inode number is sufficient for unique identification of an inode.
977 *
978 * If the inode is not in cache, allocate a new inode and return it locked,
979 * hashed, and with the I_NEW flag set. The file system gets to fill it in
980 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400982struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400984 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530985 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400987 spin_lock(&inode_hash_lock);
988 inode = find_inode_fast(sb, head, ino);
989 spin_unlock(&inode_hash_lock);
990 if (inode) {
991 wait_on_inode(inode);
992 return inode;
993 }
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 inode = alloc_inode(sb);
996 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530997 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Dave Chinner67a23c42011-03-22 22:23:42 +1100999 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* We released the lock, so.. */
1001 old = find_inode_fast(sb, head, ino);
1002 if (!old) {
1003 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001004 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001005 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001006 hlist_add_head(&inode->i_hash, head);
1007 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001008 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001009 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /* Return the locked inode with I_NEW set, the
1012 * caller is responsible for filling in the contents
1013 */
1014 return inode;
1015 }
1016
1017 /*
1018 * Uhhuh, somebody else created the same inode under
1019 * us. Use the old inode instead of the one we just
1020 * allocated.
1021 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001022 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 destroy_inode(inode);
1024 inode = old;
1025 wait_on_inode(inode);
1026 }
1027 return inode;
1028}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001029EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001031/*
1032 * search the inode cache for a matching inode number.
1033 * If we find one, then the inode number we are trying to
1034 * allocate is not unique and so we should not use it.
1035 *
1036 * Returns 1 if the inode number is unique, 0 if it is not.
1037 */
1038static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1039{
1040 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1041 struct hlist_node *node;
1042 struct inode *inode;
1043
Dave Chinner67a23c42011-03-22 22:23:42 +11001044 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001045 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001046 if (inode->i_ino == ino && inode->i_sb == sb) {
1047 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001048 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001049 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001050 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001051 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001052
1053 return 1;
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/**
1057 * iunique - get a unique inode number
1058 * @sb: superblock
1059 * @max_reserved: highest reserved inode number
1060 *
1061 * Obtain an inode number that is unique on the system for a given
1062 * superblock. This is used by file systems that have no natural
1063 * permanent inode numbering system. An inode number is returned that
1064 * is higher than the reserved limit but unique.
1065 *
1066 * BUGS:
1067 * With a large number of inodes live on the file system this function
1068 * currently becomes quite slow.
1069 */
1070ino_t iunique(struct super_block *sb, ino_t max_reserved)
1071{
Jeff Layton866b04f2007-05-08 00:32:29 -07001072 /*
1073 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1074 * error if st_ino won't fit in target struct field. Use 32bit counter
1075 * here to attempt to avoid that.
1076 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001077 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001078 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001081 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001082 do {
1083 if (counter <= max_reserved)
1084 counter = max_reserved + 1;
1085 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001086 } while (!test_inode_iunique(sb, res));
1087 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001088
1089 return res;
1090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091EXPORT_SYMBOL(iunique);
1092
1093struct inode *igrab(struct inode *inode)
1094{
Dave Chinner250df6e2011-03-22 22:23:36 +11001095 spin_lock(&inode->i_lock);
1096 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001098 spin_unlock(&inode->i_lock);
1099 } else {
1100 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * Handle the case where s_op->clear_inode is not been
1103 * called yet, and somebody is calling igrab
1104 * while the inode is getting freed.
1105 */
1106 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return inode;
1109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110EXPORT_SYMBOL(igrab);
1111
1112/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001113 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * @sb: super block of file system to search
1115 * @hashval: hash value (usually inode number) to search for
1116 * @test: callback used for comparisons between inodes
1117 * @data: opaque data pointer to pass to @test
1118 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001119 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001121 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001122 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001123 * Note: I_NEW is not waited upon so you have to be very careful what you do
1124 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001125 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001126 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001127 */
1128struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1129 int (*test)(struct inode *, void *), void *data)
1130{
1131 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001132 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001133
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001134 spin_lock(&inode_hash_lock);
1135 inode = find_inode(sb, head, test, data);
1136 spin_unlock(&inode_hash_lock);
1137
1138 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001139}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001140EXPORT_SYMBOL(ilookup5_nowait);
1141
1142/**
1143 * ilookup5 - search for an inode in the inode cache
1144 * @sb: super block of file system to search
1145 * @hashval: hash value (usually inode number) to search for
1146 * @test: callback used for comparisons between inodes
1147 * @data: opaque data pointer to pass to @test
1148 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001149 * Search for the inode specified by @hashval and @data in the inode cache,
1150 * and if the inode is in the cache, return the inode with an incremented
1151 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001152 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001154 * This is a generalized version of ilookup() for file systems where the
1155 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001157 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
1159struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1160 int (*test)(struct inode *, void *), void *data)
1161{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001162 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001164 if (inode)
1165 wait_on_inode(inode);
1166 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168EXPORT_SYMBOL(ilookup5);
1169
1170/**
1171 * ilookup - search for an inode in the inode cache
1172 * @sb: super block of file system to search
1173 * @ino: inode number to search for
1174 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001175 * Search for the inode @ino in the inode cache, and if the inode is in the
1176 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
1178struct inode *ilookup(struct super_block *sb, unsigned long ino)
1179{
1180 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001181 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001183 spin_lock(&inode_hash_lock);
1184 inode = find_inode_fast(sb, head, ino);
1185 spin_unlock(&inode_hash_lock);
1186
1187 if (inode)
1188 wait_on_inode(inode);
1189 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191EXPORT_SYMBOL(ilookup);
1192
Al Viro261bca82008-12-30 01:48:21 -05001193int insert_inode_locked(struct inode *inode)
1194{
1195 struct super_block *sb = inode->i_sb;
1196 ino_t ino = inode->i_ino;
1197 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001198
Al Viro261bca82008-12-30 01:48:21 -05001199 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001200 struct hlist_node *node;
1201 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001202 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001203 hlist_for_each_entry(old, node, head, i_hash) {
1204 if (old->i_ino != ino)
1205 continue;
1206 if (old->i_sb != sb)
1207 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001208 spin_lock(&old->i_lock);
1209 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1210 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001211 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001212 }
Al Viro72a43d62009-05-13 19:13:40 +01001213 break;
1214 }
1215 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001216 spin_lock(&inode->i_lock);
1217 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001218 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001219 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001220 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001221 return 0;
1222 }
1223 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001224 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001225 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001226 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001227 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001228 iput(old);
1229 return -EBUSY;
1230 }
1231 iput(old);
1232 }
1233}
Al Viro261bca82008-12-30 01:48:21 -05001234EXPORT_SYMBOL(insert_inode_locked);
1235
1236int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1237 int (*test)(struct inode *, void *), void *data)
1238{
1239 struct super_block *sb = inode->i_sb;
1240 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001241
Al Viro261bca82008-12-30 01:48:21 -05001242 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001243 struct hlist_node *node;
1244 struct inode *old = NULL;
1245
Dave Chinner67a23c42011-03-22 22:23:42 +11001246 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001247 hlist_for_each_entry(old, node, head, i_hash) {
1248 if (old->i_sb != sb)
1249 continue;
1250 if (!test(old, data))
1251 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001252 spin_lock(&old->i_lock);
1253 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1254 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001255 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001256 }
Al Viro72a43d62009-05-13 19:13:40 +01001257 break;
1258 }
1259 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001260 spin_lock(&inode->i_lock);
1261 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001262 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001263 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001264 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001265 return 0;
1266 }
1267 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001268 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001269 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001270 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001271 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001272 iput(old);
1273 return -EBUSY;
1274 }
1275 iput(old);
1276 }
1277}
Al Viro261bca82008-12-30 01:48:21 -05001278EXPORT_SYMBOL(insert_inode_locked4);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Al Viro45321ac2010-06-07 13:43:19 -04001281int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Al Viro45321ac2010-06-07 13:43:19 -04001283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285EXPORT_SYMBOL(generic_delete_inode);
1286
Al Viro45321ac2010-06-07 13:43:19 -04001287/*
1288 * Normal UNIX filesystem behaviour: delete the
1289 * inode when the usage count drops to zero, and
1290 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001291 */
Al Viro45321ac2010-06-07 13:43:19 -04001292int generic_drop_inode(struct inode *inode)
1293{
Al Viro1d3382cb2010-10-23 15:19:20 -04001294 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001295}
1296EXPORT_SYMBOL_GPL(generic_drop_inode);
1297
1298/*
1299 * Called when we're dropping the last reference
1300 * to an inode.
1301 *
1302 * Call the FS "drop_inode()" function, defaulting to
1303 * the legacy UNIX filesystem behaviour. If it tells
1304 * us to evict inode, do so. Otherwise, retain inode
1305 * in cache if fs is alive, sync and evict if fs is
1306 * shutting down.
1307 */
1308static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001311 const struct super_operations *op = inode->i_sb->s_op;
1312 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Dave Chinner250df6e2011-03-22 22:23:36 +11001314 WARN_ON(inode->i_state & I_NEW);
1315
Al Viroe7f59092011-07-07 15:45:59 -04001316 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001317 drop = op->drop_inode(inode);
1318 else
1319 drop = generic_drop_inode(inode);
1320
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001321 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1322 inode->i_state |= I_REFERENCED;
1323 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1324 inode_lru_list_add(inode);
1325 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001326 return;
1327 }
1328
Al Viro45321ac2010-06-07 13:43:19 -04001329 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001330 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001331 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001333 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001334 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001335 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001337
Alexander Viro991114c2005-06-23 00:09:01 -07001338 inode->i_state |= I_FREEING;
Eric Dumazetc4ae0c62011-07-28 06:55:13 +02001339 if (!list_empty(&inode->i_lru))
1340 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001341 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001342
1343 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301347 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * @inode: inode to put
1349 *
1350 * Puts an inode, dropping its usage count. If the inode use count hits
1351 * zero, the inode is then freed and may also be destroyed.
1352 *
1353 * Consequently, iput() can sleep.
1354 */
1355void iput(struct inode *inode)
1356{
1357 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001358 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Dave Chinnerf283c862011-03-22 22:23:39 +11001360 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 iput_final(inode);
1362 }
1363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364EXPORT_SYMBOL(iput);
1365
1366/**
1367 * bmap - find a block number in a file
1368 * @inode: inode of file
1369 * @block: block to find
1370 *
1371 * Returns the block number on the device holding the inode that
1372 * is the disk block number for the block of the file requested.
1373 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301374 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * file.
1376 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301377sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 sector_t res = 0;
1380 if (inode->i_mapping->a_ops->bmap)
1381 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1382 return res;
1383}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384EXPORT_SYMBOL(bmap);
1385
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001386/*
1387 * With relative atime, only update atime if the previous atime is
1388 * earlier than either the ctime or mtime or if at least a day has
1389 * passed since the last atime update.
1390 */
1391static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1392 struct timespec now)
1393{
1394
1395 if (!(mnt->mnt_flags & MNT_RELATIME))
1396 return 1;
1397 /*
1398 * Is mtime younger than atime? If yes, update atime:
1399 */
1400 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1401 return 1;
1402 /*
1403 * Is ctime younger than atime? If yes, update atime:
1404 */
1405 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1406 return 1;
1407
1408 /*
1409 * Is the previous atime value older than a day? If yes,
1410 * update atime:
1411 */
1412 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1413 return 1;
1414 /*
1415 * Good, we can skip the atime update:
1416 */
1417 return 0;
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001421 * touch_atime - update the access time
1422 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001423 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 *
1425 * Update the accessed time on an inode and mark it for writeback.
1426 * This function automatically handles read only file systems and media,
1427 * as well as the "noatime" flag and inode specific "noatime" markers.
1428 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001429void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001431 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct timespec now;
1433
Andrew Mortonb2276132006-12-13 00:34:33 -08001434 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001435 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001436 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001437 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001438 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001439 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001440
Dave Hansencdb70f32008-02-15 14:37:41 -08001441 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001442 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001443 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001444 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001447
1448 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001449 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001450
Valerie Henson47ae32d2006-12-13 00:34:34 -08001451 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001452 return;
1453
1454 if (mnt_want_write(mnt))
1455 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001456
1457 inode->i_atime = now;
1458 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001459 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001461EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001464 * file_update_time - update mtime and ctime time
1465 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001467 * Update the mtime and ctime members of an inode and mark the inode
1468 * for writeback. Note that this function is meant exclusively for
1469 * usage in the file write path of filesystems, and filesystems may
1470 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001471 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001472 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474
Christoph Hellwig870f4812006-01-09 20:52:01 -08001475void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001477 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001479 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Andi Kleence06e0b2009-09-18 13:05:48 -07001481 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (IS_NOCMTIME(inode))
1483 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001484
Andi Kleence06e0b2009-09-18 13:05:48 -07001485 now = current_fs_time(inode->i_sb);
1486 if (!timespec_equal(&inode->i_mtime, &now))
1487 sync_it = S_MTIME;
1488
1489 if (!timespec_equal(&inode->i_ctime, &now))
1490 sync_it |= S_CTIME;
1491
1492 if (IS_I_VERSION(inode))
1493 sync_it |= S_VERSION;
1494
1495 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return;
1497
Andi Kleence06e0b2009-09-18 13:05:48 -07001498 /* Finally allowed to write? Takes lock. */
1499 if (mnt_want_write_file(file))
1500 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Andi Kleence06e0b2009-09-18 13:05:48 -07001502 /* Only change inode inside the lock region */
1503 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001504 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001505 if (sync_it & S_CTIME)
1506 inode->i_ctime = now;
1507 if (sync_it & S_MTIME)
1508 inode->i_mtime = now;
1509 mark_inode_dirty_sync(inode);
Al Viro2a79f172011-12-09 08:06:57 -05001510 mnt_drop_write_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001512EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514int inode_needs_sync(struct inode *inode)
1515{
1516 if (IS_SYNC(inode))
1517 return 1;
1518 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1519 return 1;
1520 return 0;
1521}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522EXPORT_SYMBOL(inode_needs_sync);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524int inode_wait(void *word)
1525{
1526 schedule();
1527 return 0;
1528}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001529EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001532 * If we try to find an inode in the inode hash while it is being
1533 * deleted, we have to wait until the filesystem completes its
1534 * deletion before reporting that it isn't found. This function waits
1535 * until the deletion _might_ have completed. Callers are responsible
1536 * to recheck inode state.
1537 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001538 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001539 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1540 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
1542static void __wait_on_freeing_inode(struct inode *inode)
1543{
1544 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001545 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1546 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001548 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001549 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 schedule();
1551 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001552 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555static __initdata unsigned long ihash_entries;
1556static int __init set_ihash_entries(char *str)
1557{
1558 if (!str)
1559 return 0;
1560 ihash_entries = simple_strtoul(str, &str, 0);
1561 return 1;
1562}
1563__setup("ihash_entries=", set_ihash_entries);
1564
1565/*
1566 * Initialize the waitqueues and inode hash table.
1567 */
1568void __init inode_init_early(void)
1569{
1570 int loop;
1571
1572 /* If hashes are distributed across NUMA nodes, defer
1573 * hash allocation until vmalloc space is available.
1574 */
1575 if (hashdist)
1576 return;
1577
1578 inode_hashtable =
1579 alloc_large_system_hash("Inode-cache",
1580 sizeof(struct hlist_head),
1581 ihash_entries,
1582 14,
1583 HASH_EARLY,
1584 &i_hash_shift,
1585 &i_hash_mask,
1586 0);
1587
1588 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1589 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1590}
1591
Denis Cheng74bf17c2007-10-16 23:26:30 -07001592void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 int loop;
1595
1596 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001597 inode_cachep = kmem_cache_create("inode_cache",
1598 sizeof(struct inode),
1599 0,
1600 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1601 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001602 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /* Hash may have been set up in inode_init_early */
1605 if (!hashdist)
1606 return;
1607
1608 inode_hashtable =
1609 alloc_large_system_hash("Inode-cache",
1610 sizeof(struct hlist_head),
1611 ihash_entries,
1612 14,
1613 0,
1614 &i_hash_shift,
1615 &i_hash_mask,
1616 0);
1617
1618 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1619 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1620}
1621
1622void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1623{
1624 inode->i_mode = mode;
1625 if (S_ISCHR(mode)) {
1626 inode->i_fop = &def_chr_fops;
1627 inode->i_rdev = rdev;
1628 } else if (S_ISBLK(mode)) {
1629 inode->i_fop = &def_blk_fops;
1630 inode->i_rdev = rdev;
1631 } else if (S_ISFIFO(mode))
1632 inode->i_fop = &def_fifo_fops;
1633 else if (S_ISSOCK(mode))
1634 inode->i_fop = &bad_sock_fops;
1635 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001636 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1637 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1638 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001641
1642/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00001643 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001644 * @inode: New inode
1645 * @dir: Directory inode
1646 * @mode: mode of the new inode
1647 */
1648void inode_init_owner(struct inode *inode, const struct inode *dir,
1649 mode_t mode)
1650{
1651 inode->i_uid = current_fsuid();
1652 if (dir && dir->i_mode & S_ISGID) {
1653 inode->i_gid = dir->i_gid;
1654 if (S_ISDIR(mode))
1655 mode |= S_ISGID;
1656 } else
1657 inode->i_gid = current_fsgid();
1658 inode->i_mode = mode;
1659}
1660EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001661
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001662/**
1663 * inode_owner_or_capable - check current task permissions to inode
1664 * @inode: inode being checked
1665 *
1666 * Return true if current either has CAP_FOWNER to the inode, or
1667 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001668 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001669bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001670{
1671 struct user_namespace *ns = inode_userns(inode);
1672
1673 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1674 return true;
1675 if (ns_capable(ns, CAP_FOWNER))
1676 return true;
1677 return false;
1678}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001679EXPORT_SYMBOL(inode_owner_or_capable);