blob: e964818b610346152ce62bdedcecc3cb5fb3e1cb [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{
Eric Dumazet88b3f3d2011-07-26 11:36:34 +0200372 if (!list_empty(&inode->i_sb_list)) {
373 spin_lock(&inode_sb_list_lock);
374 list_del_init(&inode->i_sb_list);
375 spin_unlock(&inode_sb_list_lock);
376 }
Christoph Hellwig646ec462010-10-23 07:15:32 -0400377}
378
Dave Chinner4c51acb2010-10-23 06:58:09 -0400379static unsigned long hash(struct super_block *sb, unsigned long hashval)
380{
381 unsigned long tmp;
382
383 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
384 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400385 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
386 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400387}
388
389/**
390 * __insert_inode_hash - hash an inode
391 * @inode: unhashed inode
392 * @hashval: unsigned long value used to locate this object in the
393 * inode_hashtable.
394 *
395 * Add an inode to the inode hash for this superblock.
396 */
397void __insert_inode_hash(struct inode *inode, unsigned long hashval)
398{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400399 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
400
Dave Chinner67a23c42011-03-22 22:23:42 +1100401 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100402 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400403 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100404 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100405 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400406}
407EXPORT_SYMBOL(__insert_inode_hash);
408
409/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400410 * remove_inode_hash - remove an inode from the hash
411 * @inode: inode to unhash
412 *
413 * Remove an inode from the superblock.
414 */
415void remove_inode_hash(struct inode *inode)
416{
Dave Chinner67a23c42011-03-22 22:23:42 +1100417 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100418 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400419 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100420 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100421 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400422}
423EXPORT_SYMBOL(remove_inode_hash);
424
Al Virob0683aa2010-06-04 20:55:25 -0400425void end_writeback(struct inode *inode)
426{
427 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700428 /*
429 * We have to cycle tree_lock here because reclaim can be still in the
430 * process of removing the last page (in __delete_from_page_cache())
431 * and we must not free mapping under it.
432 */
433 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400434 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700435 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400436 BUG_ON(!list_empty(&inode->i_data.private_list));
437 BUG_ON(!(inode->i_state & I_FREEING));
438 BUG_ON(inode->i_state & I_CLEAR);
439 inode_sync_wait(inode);
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100440 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400441 inode->i_state = I_FREEING | I_CLEAR;
442}
443EXPORT_SYMBOL(end_writeback);
444
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100445/*
446 * Free the inode passed in, removing it from the lists it is still connected
447 * to. We remove any pages still attached to the inode and wait for any IO that
448 * is still in progress before finally destroying the inode.
449 *
450 * An inode must already be marked I_FREEING so that we avoid the inode being
451 * moved back onto lists if we race with other code that manipulates the lists
452 * (e.g. writeback_single_inode). The caller is responsible for setting this.
453 *
454 * An inode must already be removed from the LRU list before being evicted from
455 * the cache. This should occur atomically with setting the I_FREEING state
456 * flag, so no inodes here should ever be on the LRU when being evicted.
457 */
Al Viro644da592010-06-07 13:21:05 -0400458static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400459{
460 const struct super_operations *op = inode->i_sb->s_op;
461
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100462 BUG_ON(!(inode->i_state & I_FREEING));
463 BUG_ON(!list_empty(&inode->i_lru));
464
Dave Chinnera66979a2011-03-22 22:23:41 +1100465 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100466 inode_sb_list_del(inode);
467
Al Virobe7ce412010-06-04 19:40:39 -0400468 if (op->evict_inode) {
469 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400470 } else {
471 if (inode->i_data.nrpages)
472 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400473 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400474 }
Al Viro661074e2010-06-04 20:19:55 -0400475 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
476 bd_forget(inode);
477 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
478 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100479
480 remove_inode_hash(inode);
481
482 spin_lock(&inode->i_lock);
483 wake_up_bit(&inode->i_state, __I_NEW);
484 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
485 spin_unlock(&inode->i_lock);
486
487 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/*
491 * dispose_list - dispose of the contents of a local list
492 * @head: the head of the list to free
493 *
494 * Dispose-list gets a local list with local inodes in it, so it doesn't
495 * need to worry about list corruption and SMP locks.
496 */
497static void dispose_list(struct list_head *head)
498{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 while (!list_empty(head)) {
500 struct inode *inode;
501
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100502 inode = list_first_entry(head, struct inode, i_lru);
503 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Al Viro644da592010-06-07 13:21:05 -0400505 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/**
Al Viro63997e92010-10-25 20:49:35 -0400510 * evict_inodes - evict all evictable inodes for a superblock
511 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *
Al Viro63997e92010-10-25 20:49:35 -0400513 * Make sure that no inodes with zero refcount are retained. This is
514 * called by superblock shutdown after having MS_ACTIVE flag removed,
515 * so any inode reaching zero refcount during or after that call will
516 * be immediately evicted.
517 */
518void evict_inodes(struct super_block *sb)
519{
520 struct inode *inode, *next;
521 LIST_HEAD(dispose);
522
Dave Chinner55fa6092011-03-22 22:23:40 +1100523 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400524 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
525 if (atomic_read(&inode->i_count))
526 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100527
528 spin_lock(&inode->i_lock);
529 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
530 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400531 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100532 }
Al Viro63997e92010-10-25 20:49:35 -0400533
534 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100535 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100536 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100537 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400538 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100539 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400540
541 dispose_list(&dispose);
Christoph Hellwigbab1d942011-03-15 21:51:24 +0100542
543 /*
544 * Cycle through iprune_sem to make sure any inode that prune_icache
545 * moved off the list before we took the lock has been fully torn
546 * down.
547 */
548 down_write(&iprune_sem);
Al Viro63997e92010-10-25 20:49:35 -0400549 up_write(&iprune_sem);
550}
551
552/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200553 * invalidate_inodes - attempt to free all inodes on a superblock
554 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100555 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200556 *
557 * Attempts to free all inodes for a given superblock. If there were any
558 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100559 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
560 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
NeilBrown93b270f2011-02-24 17:25:47 +1100562int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400564 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200565 struct inode *inode, *next;
566 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Dave Chinner55fa6092011-03-22 22:23:40 +1100568 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200569 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100570 spin_lock(&inode->i_lock);
571 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
572 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100574 }
NeilBrown93b270f2011-02-24 17:25:47 +1100575 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100576 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100577 busy = 1;
578 continue;
579 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200580 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100581 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200582 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 continue;
584 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200585
Christoph Hellwig99a38912010-10-23 19:07:20 +0200586 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100587 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100588 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100589 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100591 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Christoph Hellwiga0318782010-10-24 19:40:33 +0200593 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 return busy;
596}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598static int can_unuse(struct inode *inode)
599{
Nick Piggin9e38d862010-10-23 06:55:17 -0400600 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602 if (inode_has_buffers(inode))
603 return 0;
604 if (atomic_read(&inode->i_count))
605 return 0;
606 if (inode->i_data.nrpages)
607 return 0;
608 return 1;
609}
610
611/*
Nick Piggin9e38d862010-10-23 06:55:17 -0400612 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
Dave Chinner02afc412011-03-22 22:23:38 +1100613 * temporary list and then are freed outside inode_lru_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
615 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400616 * pagecache removed. If the inode has metadata buffers attached to
617 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400619 * If the inode has the I_REFERENCED flag set, then it means that it has been
620 * used recently - the flag is set in iput_final(). When we encounter such an
621 * inode, clear the flag and move it to the back of the LRU so it gets another
622 * pass through the LRU before it gets reclaimed. This is necessary because of
623 * the fact we are doing lazy LRU updates to minimise lock contention so the
624 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
625 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
627static void prune_icache(int nr_to_scan)
628{
629 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int nr_scanned;
631 unsigned long reap = 0;
632
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700633 down_read(&iprune_sem);
Dave Chinner02afc412011-03-22 22:23:38 +1100634 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
636 struct inode *inode;
637
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100638 if (list_empty(&inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100641 inode = list_entry(inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Nick Piggin9e38d862010-10-23 06:55:17 -0400643 /*
Dave Chinner02afc412011-03-22 22:23:38 +1100644 * we are inverting the inode_lru_lock/inode->i_lock here,
645 * so use a trylock. If we fail to get the lock, just move the
646 * inode to the back of the list so we don't spin on it.
647 */
648 if (!spin_trylock(&inode->i_lock)) {
649 list_move(&inode->i_lru, &inode_lru);
650 continue;
651 }
652
653 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400654 * Referenced or dirty inodes are still in use. Give them
655 * another pass through the LRU as we canot reclaim them now.
656 */
657 if (atomic_read(&inode->i_count) ||
658 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100659 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100660 spin_unlock(&inode->i_lock);
Nick Piggin86c87492011-01-07 17:49:18 +1100661 inodes_stat.nr_unused--;
Nick Piggin9e38d862010-10-23 06:55:17 -0400662 continue;
663 }
664
665 /* recently referenced inodes get one more pass */
666 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400667 inode->i_state &= ~I_REFERENCED;
Dave Chinner250df6e2011-03-22 22:23:36 +1100668 list_move(&inode->i_lru, &inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100669 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 continue;
671 }
672 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
673 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100674 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100675 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800677 reap += invalidate_mapping_pages(&inode->i_data,
678 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 iput(inode);
Dave Chinner02afc412011-03-22 22:23:38 +1100680 spin_lock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100682 if (inode != list_entry(inode_lru.next,
683 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100685 /* avoid lock inversions with trylock */
686 if (!spin_trylock(&inode->i_lock))
687 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100688 if (!can_unuse(inode)) {
689 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700693 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100695 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100696
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100697 list_move(&inode->i_lru, &freeable);
Nick Piggin86c87492011-01-07 17:49:18 +1100698 inodes_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700700 if (current_is_kswapd())
701 __count_vm_events(KSWAPD_INODESTEAL, reap);
702 else
703 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner02afc412011-03-22 22:23:38 +1100704 spin_unlock(&inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 dispose_list(&freeable);
Nick Piggin88e0fbc2009-09-22 16:43:50 -0700707 up_read(&iprune_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
712 * "unused" means that no dentries are referring to the inodes: the files are
713 * not open and the dcache references to those inodes have already been
714 * reclaimed.
715 *
716 * This function is passed the number of inodes to scan, and it returns the
717 * total number of remaining possibly-reclaimable inodes.
718 */
Ying Han1495f232011-05-24 17:12:27 -0700719static int shrink_icache_memory(struct shrinker *shrink,
720 struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Ying Han1495f232011-05-24 17:12:27 -0700722 int nr = sc->nr_to_scan;
723 gfp_t gfp_mask = sc->gfp_mask;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (nr) {
726 /*
727 * Nasty deadlock avoidance. We may hold various FS locks,
728 * and we don't want to recurse into the FS that called us
729 * in clear_inode() and friends..
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!(gfp_mask & __GFP_FS))
732 return -1;
733 prune_icache(nr);
734 }
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400735 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Rusty Russell8e1f9362007-07-17 04:03:17 -0700738static struct shrinker icache_shrinker = {
739 .shrink = shrink_icache_memory,
740 .seeks = DEFAULT_SEEKS,
741};
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743static void __wait_on_freeing_inode(struct inode *inode);
744/*
745 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530747static struct inode *find_inode(struct super_block *sb,
748 struct hlist_head *head,
749 int (*test)(struct inode *, void *),
750 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530753 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700756 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100757 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100758 if (inode->i_sb != sb) {
759 spin_unlock(&inode->i_lock);
760 continue;
761 }
762 if (!test(inode, data)) {
763 spin_unlock(&inode->i_lock);
764 continue;
765 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400766 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 __wait_on_freeing_inode(inode);
768 goto repeat;
769 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400770 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100771 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400772 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400774 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/*
778 * find_inode_fast is the fast path version of find_inode, see the comment at
779 * iget_locked for details.
780 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530781static struct inode *find_inode_fast(struct super_block *sb,
782 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530785 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700788 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100789 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100790 if (inode->i_ino != ino) {
791 spin_unlock(&inode->i_lock);
792 continue;
793 }
794 if (inode->i_sb != sb) {
795 spin_unlock(&inode->i_lock);
796 continue;
797 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400798 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 __wait_on_freeing_inode(inode);
800 goto repeat;
801 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400802 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100803 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400804 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400806 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Eric Dumazetf991bd22010-10-23 11:18:01 -0400809/*
810 * Each cpu owns a range of LAST_INO_BATCH numbers.
811 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
812 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100813 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400814 * This does not significantly increase overflow rate because every CPU can
815 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
816 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
817 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
818 * overflow rate by 2x, which does not seem too significant.
819 *
820 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
821 * error if st_ino won't fit in target struct field. Use 32bit counter
822 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100823 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400824#define LAST_INO_BATCH 1024
825static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100826
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400827unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400828{
829 unsigned int *p = &get_cpu_var(last_ino);
830 unsigned int res = *p;
831
832#ifdef CONFIG_SMP
833 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
834 static atomic_t shared_last_ino;
835 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
836
837 res = next - LAST_INO_BATCH;
838 }
839#endif
840
841 *p = ++res;
842 put_cpu_var(last_ino);
843 return res;
David Chinner8290c352008-10-30 17:35:24 +1100844}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400845EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/**
Eric Dumazet88b3f3d2011-07-26 11:36:34 +0200848 * new_inode_pseudo - obtain an inode
849 * @sb: superblock
850 *
851 * Allocates a new inode for given superblock.
852 * Inode wont be chained in superblock s_inodes list
853 * This means :
854 * - fs can't be unmount
855 * - quotas, fsnotify, writeback can't work
856 */
857struct inode *new_inode_pseudo(struct super_block *sb)
858{
859 struct inode *inode = alloc_inode(sb);
860
861 if (inode) {
862 spin_lock(&inode->i_lock);
863 inode->i_state = 0;
864 spin_unlock(&inode->i_lock);
865 INIT_LIST_HEAD(&inode->i_sb_list);
866 }
867 return inode;
868}
869
870/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 * new_inode - obtain an inode
872 * @sb: superblock
873 *
Mel Gorman769848c2007-07-17 04:03:05 -0700874 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800875 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700876 * If HIGHMEM pages are unsuitable or it is known that pages allocated
877 * for the page cache are not reclaimable or migratable,
878 * mapping_set_gfp_mask() must be called with suitable flags on the
879 * newly created inode's mapping
880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
882struct inode *new_inode(struct super_block *sb)
883{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530884 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Dave Chinner55fa6092011-03-22 22:23:40 +1100886 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530887
Eric Dumazet88b3f3d2011-07-26 11:36:34 +0200888 inode = new_inode_pseudo(sb);
889 if (inode)
Dave Chinner55fa6092011-03-22 22:23:40 +1100890 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return inode;
892}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893EXPORT_SYMBOL(new_inode);
894
Dave Chinner250df6e2011-03-22 22:23:36 +1100895/**
896 * unlock_new_inode - clear the I_NEW state and wake up any waiters
897 * @inode: new inode to unlock
898 *
899 * Called when the inode is fully initialised to clear the new state of the
900 * inode and wake up anyone waiting for the inode to finish initialisation.
901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902void unlock_new_inode(struct inode *inode)
903{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200904#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900905 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200906 struct file_system_type *type = inode->i_sb->s_type;
907
Jan Kara9a7aa122009-06-04 15:26:49 +0200908 /* Set new key only if filesystem hasn't already changed it */
909 if (!lockdep_match_class(&inode->i_mutex,
910 &type->i_mutex_key)) {
911 /*
912 * ensure nobody is actually holding i_mutex
913 */
914 mutex_destroy(&inode->i_mutex);
915 mutex_init(&inode->i_mutex);
916 lockdep_set_class(&inode->i_mutex,
917 &type->i_mutex_dir_key);
918 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200919 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200920#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100921 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100922 WARN_ON(!(inode->i_state & I_NEW));
923 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100924 wake_up_bit(&inode->i_state, __I_NEW);
925 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927EXPORT_SYMBOL(unlock_new_inode);
928
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400929/**
930 * iget5_locked - obtain an inode from a mounted file system
931 * @sb: super block of file system
932 * @hashval: hash value (usually inode number) to get
933 * @test: callback used for comparisons between inodes
934 * @set: callback used to initialize a new struct inode
935 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400937 * Search for the inode specified by @hashval and @data in the inode cache,
938 * and if present it is return it with an increased reference count. This is
939 * a generalized version of iget_locked() for file systems where the inode
940 * number is not sufficient for unique identification of an inode.
941 *
942 * If the inode is not in cache, allocate a new inode and return it locked,
943 * hashed, and with the I_NEW flag set. The file system gets to fill it in
944 * before unlocking it via unlock_new_inode().
945 *
946 * Note both @test and @set are called with the inode_hash_lock held, so can't
947 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400949struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
950 int (*test)(struct inode *, void *),
951 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400953 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530954 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400956 spin_lock(&inode_hash_lock);
957 inode = find_inode(sb, head, test, data);
958 spin_unlock(&inode_hash_lock);
959
960 if (inode) {
961 wait_on_inode(inode);
962 return inode;
963 }
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 inode = alloc_inode(sb);
966 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530967 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dave Chinner67a23c42011-03-22 22:23:42 +1100969 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* We released the lock, so.. */
971 old = find_inode(sb, head, test, data);
972 if (!old) {
973 if (set(inode, data))
974 goto set_failed;
975
Dave Chinner250df6e2011-03-22 22:23:36 +1100976 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100977 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100978 hlist_add_head(&inode->i_hash, head);
979 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100980 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100981 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /* Return the locked inode with I_NEW set, the
984 * caller is responsible for filling in the contents
985 */
986 return inode;
987 }
988
989 /*
990 * Uhhuh, somebody else created the same inode under
991 * us. Use the old inode instead of the one we just
992 * allocated.
993 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100994 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 destroy_inode(inode);
996 inode = old;
997 wait_on_inode(inode);
998 }
999 return inode;
1000
1001set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +11001002 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 destroy_inode(inode);
1004 return NULL;
1005}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001006EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001008/**
1009 * iget_locked - obtain an inode from a mounted file system
1010 * @sb: super block of file system
1011 * @ino: inode number to get
1012 *
1013 * Search for the inode specified by @ino in the inode cache and if present
1014 * return it with an increased reference count. This is for file systems
1015 * where the inode number is sufficient for unique identification of an inode.
1016 *
1017 * If the inode is not in cache, allocate a new inode and return it locked,
1018 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1019 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001021struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001023 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301024 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001026 spin_lock(&inode_hash_lock);
1027 inode = find_inode_fast(sb, head, ino);
1028 spin_unlock(&inode_hash_lock);
1029 if (inode) {
1030 wait_on_inode(inode);
1031 return inode;
1032 }
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 inode = alloc_inode(sb);
1035 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301036 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Dave Chinner67a23c42011-03-22 22:23:42 +11001038 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* We released the lock, so.. */
1040 old = find_inode_fast(sb, head, ino);
1041 if (!old) {
1042 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001043 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001044 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001045 hlist_add_head(&inode->i_hash, head);
1046 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001047 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001048 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /* Return the locked inode with I_NEW set, the
1051 * caller is responsible for filling in the contents
1052 */
1053 return inode;
1054 }
1055
1056 /*
1057 * Uhhuh, somebody else created the same inode under
1058 * us. Use the old inode instead of the one we just
1059 * allocated.
1060 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001061 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 destroy_inode(inode);
1063 inode = old;
1064 wait_on_inode(inode);
1065 }
1066 return inode;
1067}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001068EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001070/*
1071 * search the inode cache for a matching inode number.
1072 * If we find one, then the inode number we are trying to
1073 * allocate is not unique and so we should not use it.
1074 *
1075 * Returns 1 if the inode number is unique, 0 if it is not.
1076 */
1077static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1078{
1079 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1080 struct hlist_node *node;
1081 struct inode *inode;
1082
Dave Chinner67a23c42011-03-22 22:23:42 +11001083 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001084 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001085 if (inode->i_ino == ino && inode->i_sb == sb) {
1086 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001087 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001088 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001089 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001090 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001091
1092 return 1;
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095/**
1096 * iunique - get a unique inode number
1097 * @sb: superblock
1098 * @max_reserved: highest reserved inode number
1099 *
1100 * Obtain an inode number that is unique on the system for a given
1101 * superblock. This is used by file systems that have no natural
1102 * permanent inode numbering system. An inode number is returned that
1103 * is higher than the reserved limit but unique.
1104 *
1105 * BUGS:
1106 * With a large number of inodes live on the file system this function
1107 * currently becomes quite slow.
1108 */
1109ino_t iunique(struct super_block *sb, ino_t max_reserved)
1110{
Jeff Layton866b04f2007-05-08 00:32:29 -07001111 /*
1112 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1113 * error if st_ino won't fit in target struct field. Use 32bit counter
1114 * here to attempt to avoid that.
1115 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001116 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001117 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001120 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001121 do {
1122 if (counter <= max_reserved)
1123 counter = max_reserved + 1;
1124 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001125 } while (!test_inode_iunique(sb, res));
1126 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001127
1128 return res;
1129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130EXPORT_SYMBOL(iunique);
1131
1132struct inode *igrab(struct inode *inode)
1133{
Dave Chinner250df6e2011-03-22 22:23:36 +11001134 spin_lock(&inode->i_lock);
1135 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001137 spin_unlock(&inode->i_lock);
1138 } else {
1139 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /*
1141 * Handle the case where s_op->clear_inode is not been
1142 * called yet, and somebody is calling igrab
1143 * while the inode is getting freed.
1144 */
1145 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return inode;
1148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149EXPORT_SYMBOL(igrab);
1150
1151/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001152 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 * @sb: super block of file system to search
1154 * @hashval: hash value (usually inode number) to search for
1155 * @test: callback used for comparisons between inodes
1156 * @data: opaque data pointer to pass to @test
1157 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001158 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001160 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001161 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001162 * Note: I_NEW is not waited upon so you have to be very careful what you do
1163 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001164 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001165 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001166 */
1167struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1168 int (*test)(struct inode *, void *), void *data)
1169{
1170 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001171 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001172
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001173 spin_lock(&inode_hash_lock);
1174 inode = find_inode(sb, head, test, data);
1175 spin_unlock(&inode_hash_lock);
1176
1177 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001178}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001179EXPORT_SYMBOL(ilookup5_nowait);
1180
1181/**
1182 * ilookup5 - search for an inode in the inode cache
1183 * @sb: super block of file system to search
1184 * @hashval: hash value (usually inode number) to search for
1185 * @test: callback used for comparisons between inodes
1186 * @data: opaque data pointer to pass to @test
1187 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001188 * Search for the inode specified by @hashval and @data in the inode cache,
1189 * and if the inode is in the cache, return the inode with an incremented
1190 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001191 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001193 * This is a generalized version of ilookup() for file systems where the
1194 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001196 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
1198struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1199 int (*test)(struct inode *, void *), void *data)
1200{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001201 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001203 if (inode)
1204 wait_on_inode(inode);
1205 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207EXPORT_SYMBOL(ilookup5);
1208
1209/**
1210 * ilookup - search for an inode in the inode cache
1211 * @sb: super block of file system to search
1212 * @ino: inode number to search for
1213 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001214 * Search for the inode @ino in the inode cache, and if the inode is in the
1215 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
1217struct inode *ilookup(struct super_block *sb, unsigned long ino)
1218{
1219 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001220 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001222 spin_lock(&inode_hash_lock);
1223 inode = find_inode_fast(sb, head, ino);
1224 spin_unlock(&inode_hash_lock);
1225
1226 if (inode)
1227 wait_on_inode(inode);
1228 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230EXPORT_SYMBOL(ilookup);
1231
Al Viro261bca82008-12-30 01:48:21 -05001232int insert_inode_locked(struct inode *inode)
1233{
1234 struct super_block *sb = inode->i_sb;
1235 ino_t ino = inode->i_ino;
1236 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001237
Al Viro261bca82008-12-30 01:48:21 -05001238 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001239 struct hlist_node *node;
1240 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001241 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001242 hlist_for_each_entry(old, node, head, i_hash) {
1243 if (old->i_ino != ino)
1244 continue;
1245 if (old->i_sb != sb)
1246 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001247 spin_lock(&old->i_lock);
1248 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1249 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001250 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001251 }
Al Viro72a43d62009-05-13 19:13:40 +01001252 break;
1253 }
1254 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001255 spin_lock(&inode->i_lock);
1256 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001257 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001258 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001259 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001260 return 0;
1261 }
1262 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001263 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001264 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001265 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001266 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001267 iput(old);
1268 return -EBUSY;
1269 }
1270 iput(old);
1271 }
1272}
Al Viro261bca82008-12-30 01:48:21 -05001273EXPORT_SYMBOL(insert_inode_locked);
1274
1275int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1276 int (*test)(struct inode *, void *), void *data)
1277{
1278 struct super_block *sb = inode->i_sb;
1279 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001280
Al Viro261bca82008-12-30 01:48:21 -05001281 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001282 struct hlist_node *node;
1283 struct inode *old = NULL;
1284
Dave Chinner67a23c42011-03-22 22:23:42 +11001285 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001286 hlist_for_each_entry(old, node, head, i_hash) {
1287 if (old->i_sb != sb)
1288 continue;
1289 if (!test(old, data))
1290 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001291 spin_lock(&old->i_lock);
1292 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1293 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001294 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001295 }
Al Viro72a43d62009-05-13 19:13:40 +01001296 break;
1297 }
1298 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001299 spin_lock(&inode->i_lock);
1300 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001301 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001302 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001303 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001304 return 0;
1305 }
1306 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001307 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001308 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001309 wait_on_inode(old);
Al Viro1d3382c2010-10-23 15:19:20 -04001310 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001311 iput(old);
1312 return -EBUSY;
1313 }
1314 iput(old);
1315 }
1316}
Al Viro261bca82008-12-30 01:48:21 -05001317EXPORT_SYMBOL(insert_inode_locked4);
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Al Viro45321ac2010-06-07 13:43:19 -04001320int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Al Viro45321ac2010-06-07 13:43:19 -04001322 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324EXPORT_SYMBOL(generic_delete_inode);
1325
Al Viro45321ac2010-06-07 13:43:19 -04001326/*
1327 * Normal UNIX filesystem behaviour: delete the
1328 * inode when the usage count drops to zero, and
1329 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001330 */
Al Viro45321ac2010-06-07 13:43:19 -04001331int generic_drop_inode(struct inode *inode)
1332{
Al Viro1d3382c2010-10-23 15:19:20 -04001333 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001334}
1335EXPORT_SYMBOL_GPL(generic_drop_inode);
1336
1337/*
1338 * Called when we're dropping the last reference
1339 * to an inode.
1340 *
1341 * Call the FS "drop_inode()" function, defaulting to
1342 * the legacy UNIX filesystem behaviour. If it tells
1343 * us to evict inode, do so. Otherwise, retain inode
1344 * in cache if fs is alive, sync and evict if fs is
1345 * shutting down.
1346 */
1347static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001350 const struct super_operations *op = inode->i_sb->s_op;
1351 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Dave Chinner250df6e2011-03-22 22:23:36 +11001353 WARN_ON(inode->i_state & I_NEW);
1354
Al Viro45321ac2010-06-07 13:43:19 -04001355 if (op && op->drop_inode)
1356 drop = op->drop_inode(inode);
1357 else
1358 drop = generic_drop_inode(inode);
1359
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001360 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1361 inode->i_state |= I_REFERENCED;
1362 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1363 inode_lru_list_add(inode);
1364 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001365 return;
1366 }
1367
Al Viro45321ac2010-06-07 13:43:19 -04001368 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001369 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001370 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001372 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001373 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001374 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001376
Alexander Viro991114c2005-06-23 00:09:01 -07001377 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001378 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001379 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001380
1381 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301385 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * @inode: inode to put
1387 *
1388 * Puts an inode, dropping its usage count. If the inode use count hits
1389 * zero, the inode is then freed and may also be destroyed.
1390 *
1391 * Consequently, iput() can sleep.
1392 */
1393void iput(struct inode *inode)
1394{
1395 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001396 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Dave Chinnerf283c862011-03-22 22:23:39 +11001398 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 iput_final(inode);
1400 }
1401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402EXPORT_SYMBOL(iput);
1403
1404/**
1405 * bmap - find a block number in a file
1406 * @inode: inode of file
1407 * @block: block to find
1408 *
1409 * Returns the block number on the device holding the inode that
1410 * is the disk block number for the block of the file requested.
1411 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301412 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * file.
1414 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301415sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 sector_t res = 0;
1418 if (inode->i_mapping->a_ops->bmap)
1419 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1420 return res;
1421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422EXPORT_SYMBOL(bmap);
1423
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001424/*
1425 * With relative atime, only update atime if the previous atime is
1426 * earlier than either the ctime or mtime or if at least a day has
1427 * passed since the last atime update.
1428 */
1429static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1430 struct timespec now)
1431{
1432
1433 if (!(mnt->mnt_flags & MNT_RELATIME))
1434 return 1;
1435 /*
1436 * Is mtime younger than atime? If yes, update atime:
1437 */
1438 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1439 return 1;
1440 /*
1441 * Is ctime younger than atime? If yes, update atime:
1442 */
1443 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1444 return 1;
1445
1446 /*
1447 * Is the previous atime value older than a day? If yes,
1448 * update atime:
1449 */
1450 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1451 return 1;
1452 /*
1453 * Good, we can skip the atime update:
1454 */
1455 return 0;
1456}
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001459 * touch_atime - update the access time
1460 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001461 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 *
1463 * Update the accessed time on an inode and mark it for writeback.
1464 * This function automatically handles read only file systems and media,
1465 * as well as the "noatime" flag and inode specific "noatime" markers.
1466 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001467void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001469 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct timespec now;
1471
Andrew Mortonb2276132006-12-13 00:34:33 -08001472 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001473 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001474 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001475 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001476 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001477 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001478
Dave Hansencdb70f32008-02-15 14:37:41 -08001479 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001480 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001481 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001482 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001485
1486 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001487 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001488
Valerie Henson47ae32d2006-12-13 00:34:34 -08001489 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001490 return;
1491
1492 if (mnt_want_write(mnt))
1493 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001494
1495 inode->i_atime = now;
1496 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001497 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001499EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001502 * file_update_time - update mtime and ctime time
1503 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001505 * Update the mtime and ctime members of an inode and mark the inode
1506 * for writeback. Note that this function is meant exclusively for
1507 * usage in the file write path of filesystems, and filesystems may
1508 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001509 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001510 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512
Christoph Hellwig870f4812006-01-09 20:52:01 -08001513void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001515 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001517 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Andi Kleence06e0b2009-09-18 13:05:48 -07001519 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (IS_NOCMTIME(inode))
1521 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001522
Andi Kleence06e0b2009-09-18 13:05:48 -07001523 now = current_fs_time(inode->i_sb);
1524 if (!timespec_equal(&inode->i_mtime, &now))
1525 sync_it = S_MTIME;
1526
1527 if (!timespec_equal(&inode->i_ctime, &now))
1528 sync_it |= S_CTIME;
1529
1530 if (IS_I_VERSION(inode))
1531 sync_it |= S_VERSION;
1532
1533 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return;
1535
Andi Kleence06e0b2009-09-18 13:05:48 -07001536 /* Finally allowed to write? Takes lock. */
1537 if (mnt_want_write_file(file))
1538 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Andi Kleence06e0b2009-09-18 13:05:48 -07001540 /* Only change inode inside the lock region */
1541 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001542 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001543 if (sync_it & S_CTIME)
1544 inode->i_ctime = now;
1545 if (sync_it & S_MTIME)
1546 inode->i_mtime = now;
1547 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001548 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001550EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552int inode_needs_sync(struct inode *inode)
1553{
1554 if (IS_SYNC(inode))
1555 return 1;
1556 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1557 return 1;
1558 return 0;
1559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560EXPORT_SYMBOL(inode_needs_sync);
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562int inode_wait(void *word)
1563{
1564 schedule();
1565 return 0;
1566}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001567EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001570 * If we try to find an inode in the inode hash while it is being
1571 * deleted, we have to wait until the filesystem completes its
1572 * deletion before reporting that it isn't found. This function waits
1573 * until the deletion _might_ have completed. Callers are responsible
1574 * to recheck inode state.
1575 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001576 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001577 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1578 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 */
1580static void __wait_on_freeing_inode(struct inode *inode)
1581{
1582 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001583 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1584 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001586 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001587 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 schedule();
1589 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001590 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593static __initdata unsigned long ihash_entries;
1594static int __init set_ihash_entries(char *str)
1595{
1596 if (!str)
1597 return 0;
1598 ihash_entries = simple_strtoul(str, &str, 0);
1599 return 1;
1600}
1601__setup("ihash_entries=", set_ihash_entries);
1602
1603/*
1604 * Initialize the waitqueues and inode hash table.
1605 */
1606void __init inode_init_early(void)
1607{
1608 int loop;
1609
1610 /* If hashes are distributed across NUMA nodes, defer
1611 * hash allocation until vmalloc space is available.
1612 */
1613 if (hashdist)
1614 return;
1615
1616 inode_hashtable =
1617 alloc_large_system_hash("Inode-cache",
1618 sizeof(struct hlist_head),
1619 ihash_entries,
1620 14,
1621 HASH_EARLY,
1622 &i_hash_shift,
1623 &i_hash_mask,
1624 0);
1625
1626 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1627 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1628}
1629
Denis Cheng74bf17c2007-10-16 23:26:30 -07001630void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 int loop;
1633
1634 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001635 inode_cachep = kmem_cache_create("inode_cache",
1636 sizeof(struct inode),
1637 0,
1638 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1639 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001640 init_once);
Rusty Russell8e1f9362007-07-17 04:03:17 -07001641 register_shrinker(&icache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* Hash may have been set up in inode_init_early */
1644 if (!hashdist)
1645 return;
1646
1647 inode_hashtable =
1648 alloc_large_system_hash("Inode-cache",
1649 sizeof(struct hlist_head),
1650 ihash_entries,
1651 14,
1652 0,
1653 &i_hash_shift,
1654 &i_hash_mask,
1655 0);
1656
1657 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1658 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1659}
1660
1661void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1662{
1663 inode->i_mode = mode;
1664 if (S_ISCHR(mode)) {
1665 inode->i_fop = &def_chr_fops;
1666 inode->i_rdev = rdev;
1667 } else if (S_ISBLK(mode)) {
1668 inode->i_fop = &def_blk_fops;
1669 inode->i_rdev = rdev;
1670 } else if (S_ISFIFO(mode))
1671 inode->i_fop = &def_fifo_fops;
1672 else if (S_ISSOCK(mode))
1673 inode->i_fop = &bad_sock_fops;
1674 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001675 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1676 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1677 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001680
1681/**
Ben Hutchingseaae6682011-02-15 12:48:09 +00001682 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001683 * @inode: New inode
1684 * @dir: Directory inode
1685 * @mode: mode of the new inode
1686 */
1687void inode_init_owner(struct inode *inode, const struct inode *dir,
1688 mode_t mode)
1689{
1690 inode->i_uid = current_fsuid();
1691 if (dir && dir->i_mode & S_ISGID) {
1692 inode->i_gid = dir->i_gid;
1693 if (S_ISDIR(mode))
1694 mode |= S_ISGID;
1695 } else
1696 inode->i_gid = current_fsgid();
1697 inode->i_mode = mode;
1698}
1699EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001700
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001701/**
1702 * inode_owner_or_capable - check current task permissions to inode
1703 * @inode: inode being checked
1704 *
1705 * Return true if current either has CAP_FOWNER to the inode, or
1706 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001707 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001708bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001709{
1710 struct user_namespace *ns = inode_userns(inode);
1711
1712 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1713 return true;
1714 if (ns_capable(ns, CAP_FOWNER))
1715 return true;
1716 return false;
1717}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001718EXPORT_SYMBOL(inode_owner_or_capable);