blob: 43566d17d1b85643820f0c943f35f866869b9a80 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 1997 Linus Torvalds
Christoph Hellwig4b4563d2011-05-27 09:28:01 -04003 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/fs.h>
6#include <linux/mm.h>
7#include <linux/dcache.h>
8#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
10#include <linux/writeback.h>
11#include <linux/module.h>
12#include <linux/backing-dev.h>
13#include <linux/wait.h>
Nick Piggin88e0fbc2009-09-22 16:43:50 -070014#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/hash.h>
16#include <linux/swap.h>
17#include <linux/security.h>
18#include <linux/pagemap.h>
19#include <linux/cdev.h>
20#include <linux/bootmem.h>
Eric Paris3be25f42009-05-21 17:01:26 -040021#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080022#include <linux/mount.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080023#include <linux/async.h>
Al Virof19d4a82009-06-08 19:50:45 -040024#include <linux/posix_acl.h>
Heiko Carstens9ce6e0b2011-05-22 18:54:21 +020025#include <linux/prefetch.h>
Eric Parisa178d202010-10-25 14:41:59 -040026#include <linux/ima.h>
Serge E. Hallyne795b712011-03-23 16:43:25 -070027#include <linux/cred.h>
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040028#include <linux/buffer_head.h> /* for inode_has_buffers */
Dave Chinnera66979a2011-03-22 22:23:41 +110029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040032 * Inode locking rules:
Dave Chinner250df6e2011-03-22 22:23:36 +110033 *
34 * inode->i_lock protects:
35 * inode->i_state, inode->i_hash, __iget()
Dave Chinner02afc412011-03-22 22:23:38 +110036 * inode_lru_lock protects:
37 * inode_lru, inode->i_lru
Dave Chinner55fa6092011-03-22 22:23:40 +110038 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
Dave Chinnera66979a2011-03-22 22:23:41 +110040 * inode_wb_list_lock protects:
41 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
Dave Chinner67a23c42011-03-22 22:23:42 +110042 * inode_hash_lock protects:
43 * inode_hashtable, inode->i_hash
Dave Chinner250df6e2011-03-22 22:23:36 +110044 *
45 * Lock ordering:
Dave Chinner55fa6092011-03-22 22:23:40 +110046 *
47 * inode_sb_list_lock
48 * inode->i_lock
Dave Chinner02afc412011-03-22 22:23:38 +110049 * inode_lru_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110050 *
51 * inode_wb_list_lock
52 * inode->i_lock
Dave Chinner67a23c42011-03-22 22:23:42 +110053 *
54 * inode_hash_lock
55 * inode_sb_list_lock
56 * inode->i_lock
57 *
58 * iunique_lock
59 * inode_hash_lock
Dave Chinner250df6e2011-03-22 22:23:36 +110060 */
61
Eric Dumazetfa3536c2006-03-26 01:37:24 -080062static unsigned int i_hash_mask __read_mostly;
63static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110064static struct hlist_head *inode_hashtable __read_mostly;
65static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Nick Piggin7ccf19a2010-10-21 11:49:30 +110067static LIST_HEAD(inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +110068static DEFINE_SPINLOCK(inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dave Chinner55fa6092011-03-22 22:23:40 +110070__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
Dave Chinnera66979a2011-03-22 22:23:41 +110071__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +110072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Christoph Hellwigbab1d942011-03-15 21:51:24 +010074 * iprune_sem provides exclusion between the icache shrinking and the
75 * umount path.
Nick Piggin88e0fbc2009-09-22 16:43:50 -070076 *
Christoph Hellwigbab1d942011-03-15 21:51:24 +010077 * We don't actually need it to protect anything in the umount path,
78 * but only need to cycle through it to make sure any inode that
79 * prune_icache took off the LRU list has been fully torn down by the
80 * time we are past evict_inodes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
Nick Piggin88e0fbc2009-09-22 16:43:50 -070082static DECLARE_RWSEM(iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020085 * Empty aops. Can be used for the cases where the user does not
86 * define any of the address_space operations.
87 */
88const struct address_space_operations empty_aops = {
89};
90EXPORT_SYMBOL(empty_aops);
91
92/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Statistics gathering..
94 */
95struct inodes_stat_t inodes_stat;
96
Nick Piggin3e880fb2011-01-07 17:49:19 +110097static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040098
Manish Katiyar6b3304b2009-03-31 19:35:54 +053099static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Nick Piggin3e880fb2011-01-07 17:49:19 +1100101static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400102{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100103 int i;
104 int sum = 0;
105 for_each_possible_cpu(i)
106 sum += per_cpu(nr_inodes, i);
107 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400108}
109
110static inline int get_nr_inodes_unused(void)
111{
Nick Piggin86c87492011-01-07 17:49:18 +1100112 return inodes_stat.nr_unused;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400113}
114
115int get_nr_dirty_inodes(void)
116{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100117 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400118 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
119 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400120}
121
122/*
123 * Handle nr_inode sysctl
124 */
125#ifdef CONFIG_SYSCTL
126int proc_nr_inodes(ctl_table *table, int write,
127 void __user *buffer, size_t *lenp, loff_t *ppos)
128{
129 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400130 return proc_dointvec(table, write, buffer, lenp, ppos);
131}
132#endif
133
David Chinner2cb15992008-10-30 17:32:23 +1100134/**
135 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800136 * @sb: superblock inode belongs to
137 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100138 *
139 * These are initializations that need to be done on every inode
140 * allocation as the fields are not initialised by slab allocation.
141 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300142int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700144 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800145 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530146 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100147
148 inode->i_sb = sb;
149 inode->i_blkbits = sb->s_blocksize_bits;
150 inode->i_flags = 0;
151 atomic_set(&inode->i_count, 1);
152 inode->i_op = &empty_iops;
153 inode->i_fop = &empty_fops;
154 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500155 inode->i_uid = 0;
156 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100157 atomic_set(&inode->i_writecount, 0);
158 inode->i_size = 0;
159 inode->i_blocks = 0;
160 inode->i_bytes = 0;
161 inode->i_generation = 0;
162#ifdef CONFIG_QUOTA
163 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
164#endif
165 inode->i_pipe = NULL;
166 inode->i_bdev = NULL;
167 inode->i_cdev = NULL;
168 inode->i_rdev = 0;
169 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500170
171 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300172 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100173 spin_lock_init(&inode->i_lock);
174 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
175
176 mutex_init(&inode->i_mutex);
177 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
178
179 init_rwsem(&inode->i_alloc_sem);
180 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
181
182 mapping->a_ops = &empty_aops;
183 mapping->host = inode;
184 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800185 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100186 mapping->assoc_mapping = NULL;
187 mapping->backing_dev_info = &default_backing_dev_info;
188 mapping->writeback_index = 0;
189
190 /*
191 * If the block_device provides a backing_dev_info for client
192 * inodes then use that. Otherwise the inode share the bdev's
193 * backing_dev_info.
194 */
195 if (sb->s_bdev) {
196 struct backing_dev_info *bdi;
197
Jens Axboe2c96ce92009-09-15 09:43:56 +0200198 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100199 mapping->backing_dev_info = bdi;
200 }
201 inode->i_private = NULL;
202 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400203#ifdef CONFIG_FS_POSIX_ACL
204 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
205#endif
David Chinner2cb15992008-10-30 17:32:23 +1100206
Eric Paris3be25f42009-05-21 17:01:26 -0400207#ifdef CONFIG_FSNOTIFY
208 inode->i_fsnotify_mask = 0;
209#endif
210
Nick Piggin3e880fb2011-01-07 17:49:19 +1100211 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400212
Christoph Hellwig54e34622009-08-07 14:38:25 -0300213 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300214out:
215 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100216}
217EXPORT_SYMBOL(inode_init_always);
218
219static struct inode *alloc_inode(struct super_block *sb)
220{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct inode *inode;
222
223 if (sb->s_op->alloc_inode)
224 inode = sb->s_op->alloc_inode(sb);
225 else
David Chinner2cb15992008-10-30 17:32:23 +1100226 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Christoph Hellwig54e34622009-08-07 14:38:25 -0300228 if (!inode)
229 return NULL;
230
231 if (unlikely(inode_init_always(sb, inode))) {
232 if (inode->i_sb->s_op->destroy_inode)
233 inode->i_sb->s_op->destroy_inode(inode);
234 else
235 kmem_cache_free(inode_cachep, inode);
236 return NULL;
237 }
238
239 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Nick Pigginff0c7d12011-01-07 17:49:50 +1100242void free_inode_nonrcu(struct inode *inode)
243{
244 kmem_cache_free(inode_cachep, inode);
245}
246EXPORT_SYMBOL(free_inode_nonrcu);
247
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300248void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200250 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400252 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400253#ifdef CONFIG_FS_POSIX_ACL
254 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
255 posix_acl_release(inode->i_acl);
256 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
257 posix_acl_release(inode->i_default_acl);
258#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100259 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300260}
261EXPORT_SYMBOL(__destroy_inode);
262
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100263static void i_callback(struct rcu_head *head)
264{
265 struct inode *inode = container_of(head, struct inode, i_rcu);
266 INIT_LIST_HEAD(&inode->i_dentry);
267 kmem_cache_free(inode_cachep, inode);
268}
269
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200270static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300271{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100272 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300273 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (inode->i_sb->s_op->destroy_inode)
275 inode->i_sb->s_op->destroy_inode(inode);
276 else
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100277 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100280void address_space_init_once(struct address_space *mapping)
281{
282 memset(mapping, 0, sizeof(*mapping));
283 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
284 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700285 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100286 INIT_LIST_HEAD(&mapping->private_list);
287 spin_lock_init(&mapping->private_lock);
288 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
289 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100290}
291EXPORT_SYMBOL(address_space_init_once);
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/*
294 * These are initializations that only need to be done
295 * once, because the fields are idempotent across use
296 * of the inode, so let the slab aware of that.
297 */
298void inode_init_once(struct inode *inode)
299{
300 memset(inode, 0, sizeof(*inode));
301 INIT_HLIST_NODE(&inode->i_hash);
302 INIT_LIST_HEAD(&inode->i_dentry);
303 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100304 INIT_LIST_HEAD(&inode->i_wb_list);
305 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100306 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400308#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500309 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312EXPORT_SYMBOL(inode_init_once);
313
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700314static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530316 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Christoph Lametera35afb82007-05-16 22:10:57 -0700318 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100322 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530324void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Nick Piggin9e38d862010-10-23 06:55:17 -0400326 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Al Viro7de9c6e2010-10-23 11:11:40 -0400329/*
330 * get additional reference to inode; caller must already hold one.
331 */
332void ihold(struct inode *inode)
333{
334 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
335}
336EXPORT_SYMBOL(ihold);
337
Nick Piggin9e38d862010-10-23 06:55:17 -0400338static void inode_lru_list_add(struct inode *inode)
339{
Dave Chinner02afc412011-03-22 22:23:38 +1100340 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100341 if (list_empty(&inode->i_lru)) {
342 list_add(&inode->i_lru, &inode_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100343 inodes_stat.nr_unused++;
Nick Piggin9e38d862010-10-23 06:55:17 -0400344 }
Dave Chinner02afc412011-03-22 22:23:38 +1100345 spin_unlock(&inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400346}
347
348static void inode_lru_list_del(struct inode *inode)
349{
Dave Chinner02afc412011-03-22 22:23:38 +1100350 spin_lock(&inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100351 if (!list_empty(&inode->i_lru)) {
352 list_del_init(&inode->i_lru);
Nick Piggin86c87492011-01-07 17:49:18 +1100353 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400354 }
Dave Chinner02afc412011-03-22 22:23:38 +1100355 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Christoph Hellwig646ec462010-10-23 07:15:32 -0400358/**
359 * inode_sb_list_add - add inode to the superblock list of inodes
360 * @inode: inode to add
361 */
362void inode_sb_list_add(struct inode *inode)
363{
Dave Chinner55fa6092011-03-22 22:23:40 +1100364 spin_lock(&inode_sb_list_lock);
365 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
366 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400367}
368EXPORT_SYMBOL_GPL(inode_sb_list_add);
369
Dave Chinner55fa6092011-03-22 22:23:40 +1100370static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400371{
Dave Chinner55fa6092011-03-22 22:23:40 +1100372 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400373 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100374 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400375}
376
Dave Chinner4c51acb2010-10-23 06:58:09 -0400377static unsigned long hash(struct super_block *sb, unsigned long hashval)
378{
379 unsigned long tmp;
380
381 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
382 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400383 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
384 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400385}
386
387/**
388 * __insert_inode_hash - hash an inode
389 * @inode: unhashed inode
390 * @hashval: unsigned long value used to locate this object in the
391 * inode_hashtable.
392 *
393 * Add an inode to the inode hash for this superblock.
394 */
395void __insert_inode_hash(struct inode *inode, unsigned long hashval)
396{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400397 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
398
Dave Chinner67a23c42011-03-22 22:23:42 +1100399 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100400 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400401 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100402 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100403 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400404}
405EXPORT_SYMBOL(__insert_inode_hash);
406
407/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400408 * remove_inode_hash - remove an inode from the hash
409 * @inode: inode to unhash
410 *
411 * Remove an inode from the superblock.
412 */
413void remove_inode_hash(struct inode *inode)
414{
Dave Chinner67a23c42011-03-22 22:23:42 +1100415 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100416 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400417 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100418 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100419 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400420}
421EXPORT_SYMBOL(remove_inode_hash);
422
Al Virob0683aa2010-06-04 20:55:25 -0400423void end_writeback(struct inode *inode)
424{
425 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700426 /*
427 * We have to cycle tree_lock here because reclaim can be still in the
428 * process of removing the last page (in __delete_from_page_cache())
429 * and we must not free mapping under it.
430 */
431 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400432 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700433 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400434 BUG_ON(!list_empty(&inode->i_data.private_list));
435 BUG_ON(!(inode->i_state & I_FREEING));
436 BUG_ON(inode->i_state & I_CLEAR);
437 inode_sync_wait(inode);
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100438 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400439 inode->i_state = I_FREEING | I_CLEAR;
440}
441EXPORT_SYMBOL(end_writeback);
442
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100443/*
444 * Free the inode passed in, removing it from the lists it is still connected
445 * to. We remove any pages still attached to the inode and wait for any IO that
446 * is still in progress before finally destroying the inode.
447 *
448 * An inode must already be marked I_FREEING so that we avoid the inode being
449 * moved back onto lists if we race with other code that manipulates the lists
450 * (e.g. writeback_single_inode). The caller is responsible for setting this.
451 *
452 * An inode must already be removed from the LRU list before being evicted from
453 * the cache. This should occur atomically with setting the I_FREEING state
454 * flag, so no inodes here should ever be on the LRU when being evicted.
455 */
Al Viro644da592010-06-07 13:21:05 -0400456static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400457{
458 const struct super_operations *op = inode->i_sb->s_op;
459
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100460 BUG_ON(!(inode->i_state & I_FREEING));
461 BUG_ON(!list_empty(&inode->i_lru));
462
Dave Chinnera66979a2011-03-22 22:23:41 +1100463 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100464 inode_sb_list_del(inode);
465
Al Virobe7ce412010-06-04 19:40:39 -0400466 if (op->evict_inode) {
467 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400468 } else {
469 if (inode->i_data.nrpages)
470 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400471 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400472 }
Al Viro661074e2010-06-04 20:19:55 -0400473 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
474 bd_forget(inode);
475 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
476 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100477
478 remove_inode_hash(inode);
479
480 spin_lock(&inode->i_lock);
481 wake_up_bit(&inode->i_state, __I_NEW);
482 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
483 spin_unlock(&inode->i_lock);
484
485 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*
489 * dispose_list - dispose of the contents of a local list
490 * @head: the head of the list to free
491 *
492 * Dispose-list gets a local list with local inodes in it, so it doesn't
493 * need to worry about list corruption and SMP locks.
494 */
495static void dispose_list(struct list_head *head)
496{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 while (!list_empty(head)) {
498 struct inode *inode;
499
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100500 inode = list_first_entry(head, struct inode, i_lru);
501 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Al Viro644da592010-06-07 13:21:05 -0400503 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/**
Al Viro63997e92010-10-25 20:49:35 -0400508 * evict_inodes - evict all evictable inodes for a superblock
509 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
Al Viro63997e92010-10-25 20:49:35 -0400511 * Make sure that no inodes with zero refcount are retained. This is
512 * called by superblock shutdown after having MS_ACTIVE flag removed,
513 * so any inode reaching zero refcount during or after that call will
514 * be immediately evicted.
515 */
516void evict_inodes(struct super_block *sb)
517{
518 struct inode *inode, *next;
519 LIST_HEAD(dispose);
520
Dave Chinner55fa6092011-03-22 22:23:40 +1100521 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400522 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
523 if (atomic_read(&inode->i_count))
524 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100525
526 spin_lock(&inode->i_lock);
527 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
528 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400529 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100530 }
Al Viro63997e92010-10-25 20:49:35 -0400531
532 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100533 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100534 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100535 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400536 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100537 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400538
539 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100540
541 /*
542 * Cycle through iprune_sem to make sure any inode that prune_icache
543 * moved off the list before we took the lock has been fully torn
544 * down.
545 */
546 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400547 up_write(&iprune_sem);
548}
549
550/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200551 * invalidate_inodes - attempt to free all inodes on a superblock
552 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100553 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200554 *
555 * Attempts to free all inodes for a given superblock. If there were any
556 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100557 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
558 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
NeilBrown93b270f2011-02-24 17:25:47 +1100560int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400562 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200563 struct inode *inode, *next;
564 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Dave Chinner55fa6092011-03-22 22:23:40 +1100566 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200567 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100568 spin_lock(&inode->i_lock);
569 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
570 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100572 }
NeilBrown93b270f2011-02-24 17:25:47 +1100573 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100574 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100575 busy = 1;
576 continue;
577 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200578 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100579 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200580 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 continue;
582 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200583
Christoph Hellwig99a38912010-10-23 19:07:20 +0200584 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100585 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100586 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100587 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100589 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Christoph Hellwiga0318782010-10-24 19:40:33 +0200591 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 return busy;
594}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596static int can_unuse(struct inode *inode)
597{
Nick Piggin9e38d862010-10-23 06:55:17 -0400598 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return 0;
600 if (inode_has_buffers(inode))
601 return 0;
602 if (atomic_read(&inode->i_count))
603 return 0;
604 if (inode->i_data.nrpages)
605 return 0;
606 return 1;
607}
608
609/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400610 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100611 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 *
613 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400614 * pagecache removed. If the inode has metadata buffers attached to
615 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400617 * If the inode has the I_REFERENCED flag set, then it means that it has been
618 * used recently - the flag is set in iput_final(). When we encounter such an
619 * inode, clear the flag and move it to the back of the LRU so it gets another
620 * pass through the LRU before it gets reclaimed. This is necessary because of
621 * the fact we are doing lazy LRU updates to minimise lock contention so the
622 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
623 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
625static void prune_icache(int nr_to_scan)
626{
627 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int nr_scanned;
629 unsigned long reap = 0;
630
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700631 down_read(&iprune_sem);
Dave Chinner02afc412011-03-22 22:23:38 +1100632 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
634 struct inode *inode;
635
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100636 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100639 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Nick Piggin9e38d862010-10-23 06:55:17 -0400641 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100642 * we are inverting the inode_lru_lock/inode->i_lock here,
643 * so use a trylock. If we fail to get the lock, just move the
644 * inode to the back of the list so we don't spin on it.
645 */
646 if (!spin_trylock(&inode->i_lock)) {
647 list_move(&inode->i_lru, &inode_lru);
648 continue;
649 }
650
651 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400652 * Referenced or dirty inodes are still in use. Give them
653 * another pass through the LRU as we canot reclaim them now.
654 */
655 if (atomic_read(&inode->i_count) ||
656 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100657 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100658 spin_unlock(&inode->i_lock);
Nick Piggin86c87492011-01-07 17:49:18 +1100659 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400660 continue;
661 }
662
663 /* recently referenced inodes get one more pass */
664 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400665 inode->i_state &= ~I_REFERENCED;
Dave Chinner250df6e2011-03-22 22:23:36 +1100666 list_move(&inode->i_lru, &inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100667 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 continue;
669 }
670 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
671 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100672 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100673 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800675 reap += invalidate_mapping_pages(&inode->i_data,
676 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100678 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100680 if (inode != list_entry(inode_lru.next,
681 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100683 /* avoid lock inversions with trylock */
684 if (!spin_trylock(&inode->i_lock))
685 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100686 if (!can_unuse(inode)) {
687 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700691 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100693 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100694
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100695 list_move(&inode->i_lru, &freeable);
Nick Piggin86c87492011-01-07 17:49:18 +1100696 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700698 if (current_is_kswapd())
699 __count_vm_events(KSWAPD_INODESTEAL, reap);
700 else
701 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100702 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700705 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/*
709 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
710 * "unused" means that no dentries are referring to the inodes: the files are
711 * not open and the dcache references to those inodes have already been
712 * reclaimed.
713 *
714 * This function is passed the number of inodes to scan, and it returns the
715 * total number of remaining possibly-reclaimable inodes.
716 */
Ying Han1495f232011-05-24 17:12:27 -0700717static int shrink_icache_memory(struct shrinker *shrink,
718 struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Ying Han1495f232011-05-24 17:12:27 -0700720 int nr = sc->nr_to_scan;
721 gfp_t gfp_mask = sc->gfp_mask;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (nr) {
724 /*
725 * Nasty deadlock avoidance. We may hold various FS locks,
726 * and we don't want to recurse into the FS that called us
727 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (!(gfp_mask & __GFP_FS))
730 return -1;
731 prune_icache(nr);
732 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400733 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Rusty Russell8e1f9362007-07-17 04:03:17 -0700736static struct shrinker icache_shrinker = {
737 .shrink = shrink_icache_memory,
738 .seeks = DEFAULT_SEEKS,
739};
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static void __wait_on_freeing_inode(struct inode *inode);
742/*
743 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530745static struct inode *find_inode(struct super_block *sb,
746 struct hlist_head *head,
747 int (*test)(struct inode *, void *),
748 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530751 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700754 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100755 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100756 if (inode->i_sb != sb) {
757 spin_unlock(&inode->i_lock);
758 continue;
759 }
760 if (!test(inode, data)) {
761 spin_unlock(&inode->i_lock);
762 continue;
763 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400764 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 __wait_on_freeing_inode(inode);
766 goto repeat;
767 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400768 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100769 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400770 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400772 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/*
776 * find_inode_fast is the fast path version of find_inode, see the comment at
777 * iget_locked for details.
778 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530779static struct inode *find_inode_fast(struct super_block *sb,
780 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530783 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700786 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100787 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100788 if (inode->i_ino != ino) {
789 spin_unlock(&inode->i_lock);
790 continue;
791 }
792 if (inode->i_sb != sb) {
793 spin_unlock(&inode->i_lock);
794 continue;
795 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400796 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 __wait_on_freeing_inode(inode);
798 goto repeat;
799 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400800 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100801 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400802 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400804 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Eric Dumazetf991bd22010-10-23 11:18:01 -0400807/*
808 * Each cpu owns a range of LAST_INO_BATCH numbers.
809 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
810 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100811 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400812 * This does not significantly increase overflow rate because every CPU can
813 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
814 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
815 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
816 * overflow rate by 2x, which does not seem too significant.
817 *
818 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
819 * error if st_ino won't fit in target struct field. Use 32bit counter
820 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100821 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400822#define LAST_INO_BATCH 1024
823static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100824
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400825unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400826{
827 unsigned int *p = &get_cpu_var(last_ino);
828 unsigned int res = *p;
829
830#ifdef CONFIG_SMP
831 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
832 static atomic_t shared_last_ino;
833 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
834
835 res = next - LAST_INO_BATCH;
836 }
837#endif
838
839 *p = ++res;
840 put_cpu_var(last_ino);
841 return res;
David Chinner8290c352008-10-30 17:35:24 +1100842}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400843EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/**
846 * new_inode - obtain an inode
847 * @sb: superblock
848 *
Mel Gorman769848c2007-07-17 04:03:05 -0700849 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800850 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700851 * If HIGHMEM pages are unsuitable or it is known that pages allocated
852 * for the page cache are not reclaimable or migratable,
853 * mapping_set_gfp_mask() must be called with suitable flags on the
854 * newly created inode's mapping
855 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
857struct inode *new_inode(struct super_block *sb)
858{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530859 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dave Chinner55fa6092011-03-22 22:23:40 +1100861 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 inode = alloc_inode(sb);
864 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100865 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100867 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100868 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 return inode;
871}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872EXPORT_SYMBOL(new_inode);
873
Dave Chinner250df6e2011-03-22 22:23:36 +1100874/**
875 * unlock_new_inode - clear the I_NEW state and wake up any waiters
876 * @inode: new inode to unlock
877 *
878 * Called when the inode is fully initialised to clear the new state of the
879 * inode and wake up anyone waiting for the inode to finish initialisation.
880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881void unlock_new_inode(struct inode *inode)
882{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200883#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900884 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200885 struct file_system_type *type = inode->i_sb->s_type;
886
Jan Kara9a7aa122009-06-04 15:26:49 +0200887 /* Set new key only if filesystem hasn't already changed it */
888 if (!lockdep_match_class(&inode->i_mutex,
889 &type->i_mutex_key)) {
890 /*
891 * ensure nobody is actually holding i_mutex
892 */
893 mutex_destroy(&inode->i_mutex);
894 mutex_init(&inode->i_mutex);
895 lockdep_set_class(&inode->i_mutex,
896 &type->i_mutex_dir_key);
897 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200898 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200899#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100900 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100901 WARN_ON(!(inode->i_state & I_NEW));
902 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100903 wake_up_bit(&inode->i_state, __I_NEW);
904 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906EXPORT_SYMBOL(unlock_new_inode);
907
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400908/**
909 * iget5_locked - obtain an inode from a mounted file system
910 * @sb: super block of file system
911 * @hashval: hash value (usually inode number) to get
912 * @test: callback used for comparisons between inodes
913 * @set: callback used to initialize a new struct inode
914 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400916 * Search for the inode specified by @hashval and @data in the inode cache,
917 * and if present it is return it with an increased reference count. This is
918 * a generalized version of iget_locked() for file systems where the inode
919 * number is not sufficient for unique identification of an inode.
920 *
921 * If the inode is not in cache, allocate a new inode and return it locked,
922 * hashed, and with the I_NEW flag set. The file system gets to fill it in
923 * before unlocking it via unlock_new_inode().
924 *
925 * Note both @test and @set are called with the inode_hash_lock held, so can't
926 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400928struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
929 int (*test)(struct inode *, void *),
930 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400932 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530933 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400935 spin_lock(&inode_hash_lock);
936 inode = find_inode(sb, head, test, data);
937 spin_unlock(&inode_hash_lock);
938
939 if (inode) {
940 wait_on_inode(inode);
941 return inode;
942 }
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 inode = alloc_inode(sb);
945 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530946 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dave Chinner67a23c42011-03-22 22:23:42 +1100948 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* We released the lock, so.. */
950 old = find_inode(sb, head, test, data);
951 if (!old) {
952 if (set(inode, data))
953 goto set_failed;
954
Dave Chinner250df6e2011-03-22 22:23:36 +1100955 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100956 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100957 hlist_add_head(&inode->i_hash, head);
958 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100959 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100960 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /* Return the locked inode with I_NEW set, the
963 * caller is responsible for filling in the contents
964 */
965 return inode;
966 }
967
968 /*
969 * Uhhuh, somebody else created the same inode under
970 * us. Use the old inode instead of the one we just
971 * allocated.
972 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100973 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 destroy_inode(inode);
975 inode = old;
976 wait_on_inode(inode);
977 }
978 return inode;
979
980set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100981 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 destroy_inode(inode);
983 return NULL;
984}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400985EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400987/**
988 * iget_locked - obtain an inode from a mounted file system
989 * @sb: super block of file system
990 * @ino: inode number to get
991 *
992 * Search for the inode specified by @ino in the inode cache and if present
993 * return it with an increased reference count. This is for file systems
994 * where the inode number is sufficient for unique identification of an inode.
995 *
996 * If the inode is not in cache, allocate a new inode and return it locked,
997 * hashed, and with the I_NEW flag set. The file system gets to fill it in
998 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001000struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001002 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301003 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001005 spin_lock(&inode_hash_lock);
1006 inode = find_inode_fast(sb, head, ino);
1007 spin_unlock(&inode_hash_lock);
1008 if (inode) {
1009 wait_on_inode(inode);
1010 return inode;
1011 }
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 inode = alloc_inode(sb);
1014 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301015 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Dave Chinner67a23c42011-03-22 22:23:42 +11001017 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* We released the lock, so.. */
1019 old = find_inode_fast(sb, head, ino);
1020 if (!old) {
1021 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001022 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001023 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001024 hlist_add_head(&inode->i_hash, head);
1025 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001026 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001027 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /* Return the locked inode with I_NEW set, the
1030 * caller is responsible for filling in the contents
1031 */
1032 return inode;
1033 }
1034
1035 /*
1036 * Uhhuh, somebody else created the same inode under
1037 * us. Use the old inode instead of the one we just
1038 * allocated.
1039 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001040 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 destroy_inode(inode);
1042 inode = old;
1043 wait_on_inode(inode);
1044 }
1045 return inode;
1046}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001047EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001049/*
1050 * search the inode cache for a matching inode number.
1051 * If we find one, then the inode number we are trying to
1052 * allocate is not unique and so we should not use it.
1053 *
1054 * Returns 1 if the inode number is unique, 0 if it is not.
1055 */
1056static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1057{
1058 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1059 struct hlist_node *node;
1060 struct inode *inode;
1061
Dave Chinner67a23c42011-03-22 22:23:42 +11001062 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001063 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001064 if (inode->i_ino == ino && inode->i_sb == sb) {
1065 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001066 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001067 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001068 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001069 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001070
1071 return 1;
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/**
1075 * iunique - get a unique inode number
1076 * @sb: superblock
1077 * @max_reserved: highest reserved inode number
1078 *
1079 * Obtain an inode number that is unique on the system for a given
1080 * superblock. This is used by file systems that have no natural
1081 * permanent inode numbering system. An inode number is returned that
1082 * is higher than the reserved limit but unique.
1083 *
1084 * BUGS:
1085 * With a large number of inodes live on the file system this function
1086 * currently becomes quite slow.
1087 */
1088ino_t iunique(struct super_block *sb, ino_t max_reserved)
1089{
Jeff Layton866b04f2007-05-08 00:32:29 -07001090 /*
1091 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1092 * error if st_ino won't fit in target struct field. Use 32bit counter
1093 * here to attempt to avoid that.
1094 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001095 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001096 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001099 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001100 do {
1101 if (counter <= max_reserved)
1102 counter = max_reserved + 1;
1103 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001104 } while (!test_inode_iunique(sb, res));
1105 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001106
1107 return res;
1108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109EXPORT_SYMBOL(iunique);
1110
1111struct inode *igrab(struct inode *inode)
1112{
Dave Chinner250df6e2011-03-22 22:23:36 +11001113 spin_lock(&inode->i_lock);
1114 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001116 spin_unlock(&inode->i_lock);
1117 } else {
1118 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Handle the case where s_op->clear_inode is not been
1121 * called yet, and somebody is calling igrab
1122 * while the inode is getting freed.
1123 */
1124 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return inode;
1127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128EXPORT_SYMBOL(igrab);
1129
1130/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001131 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * @sb: super block of file system to search
1133 * @hashval: hash value (usually inode number) to search for
1134 * @test: callback used for comparisons between inodes
1135 * @data: opaque data pointer to pass to @test
1136 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001137 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001139 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001140 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001141 * Note: I_NEW is not waited upon so you have to be very careful what you do
1142 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001143 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001144 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001145 */
1146struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1147 int (*test)(struct inode *, void *), void *data)
1148{
1149 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001150 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001151
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001152 spin_lock(&inode_hash_lock);
1153 inode = find_inode(sb, head, test, data);
1154 spin_unlock(&inode_hash_lock);
1155
1156 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001157}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001158EXPORT_SYMBOL(ilookup5_nowait);
1159
1160/**
1161 * ilookup5 - search for an inode in the inode cache
1162 * @sb: super block of file system to search
1163 * @hashval: hash value (usually inode number) to search for
1164 * @test: callback used for comparisons between inodes
1165 * @data: opaque data pointer to pass to @test
1166 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001167 * Search for the inode specified by @hashval and @data in the inode cache,
1168 * and if the inode is in the cache, return the inode with an incremented
1169 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001170 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001172 * This is a generalized version of ilookup() for file systems where the
1173 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001175 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 */
1177struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1178 int (*test)(struct inode *, void *), void *data)
1179{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001180 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001182 if (inode)
1183 wait_on_inode(inode);
1184 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186EXPORT_SYMBOL(ilookup5);
1187
1188/**
1189 * ilookup - search for an inode in the inode cache
1190 * @sb: super block of file system to search
1191 * @ino: inode number to search for
1192 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001193 * Search for the inode @ino in the inode cache, and if the inode is in the
1194 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
1196struct inode *ilookup(struct super_block *sb, unsigned long ino)
1197{
1198 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001199 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001201 spin_lock(&inode_hash_lock);
1202 inode = find_inode_fast(sb, head, ino);
1203 spin_unlock(&inode_hash_lock);
1204
1205 if (inode)
1206 wait_on_inode(inode);
1207 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209EXPORT_SYMBOL(ilookup);
1210
Al Viro261bca82008-12-30 01:48:21 -05001211int insert_inode_locked(struct inode *inode)
1212{
1213 struct super_block *sb = inode->i_sb;
1214 ino_t ino = inode->i_ino;
1215 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001216
Al Viro261bca82008-12-30 01:48:21 -05001217 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001218 struct hlist_node *node;
1219 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001220 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001221 hlist_for_each_entry(old, node, head, i_hash) {
1222 if (old->i_ino != ino)
1223 continue;
1224 if (old->i_sb != sb)
1225 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001226 spin_lock(&old->i_lock);
1227 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1228 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001229 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001230 }
Al Viro72a43d62009-05-13 19:13:40 +01001231 break;
1232 }
1233 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001234 spin_lock(&inode->i_lock);
1235 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001236 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001237 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001238 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001239 return 0;
1240 }
1241 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001242 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001243 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001244 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001245 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001246 iput(old);
1247 return -EBUSY;
1248 }
1249 iput(old);
1250 }
1251}
Al Viro261bca82008-12-30 01:48:21 -05001252EXPORT_SYMBOL(insert_inode_locked);
1253
1254int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1255 int (*test)(struct inode *, void *), void *data)
1256{
1257 struct super_block *sb = inode->i_sb;
1258 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001259
Al Viro261bca82008-12-30 01:48:21 -05001260 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001261 struct hlist_node *node;
1262 struct inode *old = NULL;
1263
Dave Chinner67a23c42011-03-22 22:23:42 +11001264 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001265 hlist_for_each_entry(old, node, head, i_hash) {
1266 if (old->i_sb != sb)
1267 continue;
1268 if (!test(old, data))
1269 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001270 spin_lock(&old->i_lock);
1271 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1272 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001273 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001274 }
Al Viro72a43d62009-05-13 19:13:40 +01001275 break;
1276 }
1277 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001278 spin_lock(&inode->i_lock);
1279 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001280 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001281 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001282 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001283 return 0;
1284 }
1285 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001286 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001287 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001288 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001289 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001290 iput(old);
1291 return -EBUSY;
1292 }
1293 iput(old);
1294 }
1295}
Al Viro261bca82008-12-30 01:48:21 -05001296EXPORT_SYMBOL(insert_inode_locked4);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Al Viro45321ac2010-06-07 13:43:19 -04001299int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Al Viro45321ac2010-06-07 13:43:19 -04001301 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303EXPORT_SYMBOL(generic_delete_inode);
1304
Al Viro45321ac2010-06-07 13:43:19 -04001305/*
1306 * Normal UNIX filesystem behaviour: delete the
1307 * inode when the usage count drops to zero, and
1308 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001309 */
Al Viro45321ac2010-06-07 13:43:19 -04001310int generic_drop_inode(struct inode *inode)
1311{
Al Viro1d3382c2010-10-23 15:19:20 -04001312 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001313}
1314EXPORT_SYMBOL_GPL(generic_drop_inode);
1315
1316/*
1317 * Called when we're dropping the last reference
1318 * to an inode.
1319 *
1320 * Call the FS "drop_inode()" function, defaulting to
1321 * the legacy UNIX filesystem behaviour. If it tells
1322 * us to evict inode, do so. Otherwise, retain inode
1323 * in cache if fs is alive, sync and evict if fs is
1324 * shutting down.
1325 */
1326static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001329 const struct super_operations *op = inode->i_sb->s_op;
1330 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Dave Chinner250df6e2011-03-22 22:23:36 +11001332 WARN_ON(inode->i_state & I_NEW);
1333
Al Viro45321ac2010-06-07 13:43:19 -04001334 if (op && op->drop_inode)
1335 drop = op->drop_inode(inode);
1336 else
1337 drop = generic_drop_inode(inode);
1338
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001339 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1340 inode->i_state |= I_REFERENCED;
1341 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1342 inode_lru_list_add(inode);
1343 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001344 return;
1345 }
1346
Al Viro45321ac2010-06-07 13:43:19 -04001347 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001348 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001349 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001351 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001352 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001353 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001355
Alexander Viro991114c2005-06-23 00:09:01 -07001356 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001357 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001358 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001359
1360 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301364 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * @inode: inode to put
1366 *
1367 * Puts an inode, dropping its usage count. If the inode use count hits
1368 * zero, the inode is then freed and may also be destroyed.
1369 *
1370 * Consequently, iput() can sleep.
1371 */
1372void iput(struct inode *inode)
1373{
1374 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001375 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Dave Chinnerf283c862011-03-22 22:23:39 +11001377 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 iput_final(inode);
1379 }
1380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381EXPORT_SYMBOL(iput);
1382
1383/**
1384 * bmap - find a block number in a file
1385 * @inode: inode of file
1386 * @block: block to find
1387 *
1388 * Returns the block number on the device holding the inode that
1389 * is the disk block number for the block of the file requested.
1390 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301391 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * file.
1393 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301394sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 sector_t res = 0;
1397 if (inode->i_mapping->a_ops->bmap)
1398 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1399 return res;
1400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401EXPORT_SYMBOL(bmap);
1402
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001403/*
1404 * With relative atime, only update atime if the previous atime is
1405 * earlier than either the ctime or mtime or if at least a day has
1406 * passed since the last atime update.
1407 */
1408static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1409 struct timespec now)
1410{
1411
1412 if (!(mnt->mnt_flags & MNT_RELATIME))
1413 return 1;
1414 /*
1415 * Is mtime younger than atime? If yes, update atime:
1416 */
1417 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1418 return 1;
1419 /*
1420 * Is ctime younger than atime? If yes, update atime:
1421 */
1422 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1423 return 1;
1424
1425 /*
1426 * Is the previous atime value older than a day? If yes,
1427 * update atime:
1428 */
1429 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1430 return 1;
1431 /*
1432 * Good, we can skip the atime update:
1433 */
1434 return 0;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001438 * touch_atime - update the access time
1439 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001440 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 *
1442 * Update the accessed time on an inode and mark it for writeback.
1443 * This function automatically handles read only file systems and media,
1444 * as well as the "noatime" flag and inode specific "noatime" markers.
1445 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001446void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001448 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 struct timespec now;
1450
Andrew Mortonb2276132006-12-13 00:34:33 -08001451 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001452 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001453 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001454 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001455 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001456 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001457
Dave Hansencdb70f32008-02-15 14:37:41 -08001458 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001459 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001460 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001461 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001464
1465 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001466 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001467
Valerie Henson47ae32d2006-12-13 00:34:34 -08001468 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001469 return;
1470
1471 if (mnt_want_write(mnt))
1472 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001473
1474 inode->i_atime = now;
1475 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001476 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001478EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001481 * file_update_time - update mtime and ctime time
1482 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001484 * Update the mtime and ctime members of an inode and mark the inode
1485 * for writeback. Note that this function is meant exclusively for
1486 * usage in the file write path of filesystems, and filesystems may
1487 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001488 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001489 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
1491
Christoph Hellwig870f4812006-01-09 20:52:01 -08001492void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001494 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001496 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Andi Kleence06e0b2009-09-18 13:05:48 -07001498 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (IS_NOCMTIME(inode))
1500 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001501
Andi Kleence06e0b2009-09-18 13:05:48 -07001502 now = current_fs_time(inode->i_sb);
1503 if (!timespec_equal(&inode->i_mtime, &now))
1504 sync_it = S_MTIME;
1505
1506 if (!timespec_equal(&inode->i_ctime, &now))
1507 sync_it |= S_CTIME;
1508
1509 if (IS_I_VERSION(inode))
1510 sync_it |= S_VERSION;
1511
1512 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return;
1514
Andi Kleence06e0b2009-09-18 13:05:48 -07001515 /* Finally allowed to write? Takes lock. */
1516 if (mnt_want_write_file(file))
1517 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Andi Kleence06e0b2009-09-18 13:05:48 -07001519 /* Only change inode inside the lock region */
1520 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001521 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001522 if (sync_it & S_CTIME)
1523 inode->i_ctime = now;
1524 if (sync_it & S_MTIME)
1525 inode->i_mtime = now;
1526 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001527 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001529EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531int inode_needs_sync(struct inode *inode)
1532{
1533 if (IS_SYNC(inode))
1534 return 1;
1535 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1536 return 1;
1537 return 0;
1538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539EXPORT_SYMBOL(inode_needs_sync);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541int inode_wait(void *word)
1542{
1543 schedule();
1544 return 0;
1545}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001546EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001549 * If we try to find an inode in the inode hash while it is being
1550 * deleted, we have to wait until the filesystem completes its
1551 * deletion before reporting that it isn't found. This function waits
1552 * until the deletion _might_ have completed. Callers are responsible
1553 * to recheck inode state.
1554 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001555 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001556 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1557 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
1559static void __wait_on_freeing_inode(struct inode *inode)
1560{
1561 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001562 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1563 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001565 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001566 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 schedule();
1568 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001569 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572static __initdata unsigned long ihash_entries;
1573static int __init set_ihash_entries(char *str)
1574{
1575 if (!str)
1576 return 0;
1577 ihash_entries = simple_strtoul(str, &str, 0);
1578 return 1;
1579}
1580__setup("ihash_entries=", set_ihash_entries);
1581
1582/*
1583 * Initialize the waitqueues and inode hash table.
1584 */
1585void __init inode_init_early(void)
1586{
1587 int loop;
1588
1589 /* If hashes are distributed across NUMA nodes, defer
1590 * hash allocation until vmalloc space is available.
1591 */
1592 if (hashdist)
1593 return;
1594
1595 inode_hashtable =
1596 alloc_large_system_hash("Inode-cache",
1597 sizeof(struct hlist_head),
1598 ihash_entries,
1599 14,
1600 HASH_EARLY,
1601 &i_hash_shift,
1602 &i_hash_mask,
1603 0);
1604
1605 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1606 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1607}
1608
Denis Cheng74bf17c2007-10-16 23:26:30 -07001609void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 int loop;
1612
1613 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001614 inode_cachep = kmem_cache_create("inode_cache",
1615 sizeof(struct inode),
1616 0,
1617 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1618 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001619 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001620 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /* Hash may have been set up in inode_init_early */
1623 if (!hashdist)
1624 return;
1625
1626 inode_hashtable =
1627 alloc_large_system_hash("Inode-cache",
1628 sizeof(struct hlist_head),
1629 ihash_entries,
1630 14,
1631 0,
1632 &i_hash_shift,
1633 &i_hash_mask,
1634 0);
1635
1636 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1637 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1638}
1639
1640void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1641{
1642 inode->i_mode = mode;
1643 if (S_ISCHR(mode)) {
1644 inode->i_fop = &def_chr_fops;
1645 inode->i_rdev = rdev;
1646 } else if (S_ISBLK(mode)) {
1647 inode->i_fop = &def_blk_fops;
1648 inode->i_rdev = rdev;
1649 } else if (S_ISFIFO(mode))
1650 inode->i_fop = &def_fifo_fops;
1651 else if (S_ISSOCK(mode))
1652 inode->i_fop = &bad_sock_fops;
1653 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001654 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1655 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1656 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001659
1660/**
Ben Hutchingseaae6682011-02-15 12:48:09 +00001661 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001662 * @inode: New inode
1663 * @dir: Directory inode
1664 * @mode: mode of the new inode
1665 */
1666void inode_init_owner(struct inode *inode, const struct inode *dir,
1667 mode_t mode)
1668{
1669 inode->i_uid = current_fsuid();
1670 if (dir && dir->i_mode & S_ISGID) {
1671 inode->i_gid = dir->i_gid;
1672 if (S_ISDIR(mode))
1673 mode |= S_ISGID;
1674 } else
1675 inode->i_gid = current_fsgid();
1676 inode->i_mode = mode;
1677}
1678EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001679
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001680/**
1681 * inode_owner_or_capable - check current task permissions to inode
1682 * @inode: inode being checked
1683 *
1684 * Return true if current either has CAP_FOWNER to the inode, or
1685 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001686 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001687bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001688{
1689 struct user_namespace *ns = inode_userns(inode);
1690
1691 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1692 return true;
1693 if (ns_capable(ns, CAP_FOWNER))
1694 return true;
1695 return false;
1696}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001697EXPORT_SYMBOL(inode_owner_or_capable);