blob: d0c72ff6b30e6d259c80d4cd28197441f141fe43 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 1997 Linus Torvalds
Christoph Hellwig4b4563d2011-05-27 09:28:01 -04003 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/fs.h>
6#include <linux/mm.h>
7#include <linux/dcache.h>
8#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/slab.h>
10#include <linux/writeback.h>
11#include <linux/module.h>
12#include <linux/backing-dev.h>
13#include <linux/wait.h>
Nick Piggin88e0fbc2009-09-22 16:43:50 -070014#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/hash.h>
16#include <linux/swap.h>
17#include <linux/security.h>
18#include <linux/pagemap.h>
19#include <linux/cdev.h>
20#include <linux/bootmem.h>
Eric Paris3be25f42009-05-21 17:01:26 -040021#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080022#include <linux/mount.h>
Arjan van de Venefaee192009-01-06 07:20:54 -080023#include <linux/async.h>
Al Virof19d4a82009-06-08 19:50:45 -040024#include <linux/posix_acl.h>
Heiko Carstens9ce6e0b2011-05-22 18:54:21 +020025#include <linux/prefetch.h>
Eric Parisa178d202010-10-25 14:41:59 -040026#include <linux/ima.h>
Serge E. Hallyne795b712011-03-23 16:43:25 -070027#include <linux/cred.h>
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040028#include <linux/buffer_head.h> /* for inode_has_buffers */
Dave Chinnera66979a2011-03-22 22:23:41 +110029#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040032 * Inode locking rules:
Dave Chinner250df6e2011-03-22 22:23:36 +110033 *
34 * inode->i_lock protects:
35 * inode->i_state, inode->i_hash, __iget()
Dave Chinner09cc9fc2011-07-08 14:14:40 +100036 * inode->i_sb->s_inode_lru_lock protects:
Dave Chinner98b745c2011-07-08 14:14:39 +100037 * inode->i_sb->s_inode_lru, inode->i_lru
Dave Chinner55fa6092011-03-22 22:23:40 +110038 * inode_sb_list_lock protects:
39 * sb->s_inodes, inode->i_sb_list
Christoph Hellwigf758eea2011-04-21 18:19:44 -060040 * bdi->wb.list_lock protects:
Dave Chinnera66979a2011-03-22 22:23:41 +110041 * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
Dave Chinner67a23c42011-03-22 22:23:42 +110042 * inode_hash_lock protects:
43 * inode_hashtable, inode->i_hash
Dave Chinner250df6e2011-03-22 22:23:36 +110044 *
45 * Lock ordering:
Dave Chinner55fa6092011-03-22 22:23:40 +110046 *
47 * inode_sb_list_lock
48 * inode->i_lock
Dave Chinner09cc9fc2011-07-08 14:14:40 +100049 * inode->i_sb->s_inode_lru_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110050 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060051 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110052 * inode->i_lock
Dave Chinner67a23c42011-03-22 22:23:42 +110053 *
54 * inode_hash_lock
55 * inode_sb_list_lock
56 * inode->i_lock
57 *
58 * iunique_lock
59 * inode_hash_lock
Dave Chinner250df6e2011-03-22 22:23:36 +110060 */
61
Eric Dumazetfa3536c2006-03-26 01:37:24 -080062static unsigned int i_hash_mask __read_mostly;
63static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110064static struct hlist_head *inode_hashtable __read_mostly;
65static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Chinner55fa6092011-03-22 22:23:40 +110067__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020070 * Empty aops. Can be used for the cases where the user does not
71 * define any of the address_space operations.
72 */
73const struct address_space_operations empty_aops = {
74};
75EXPORT_SYMBOL(empty_aops);
76
77/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * Statistics gathering..
79 */
80struct inodes_stat_t inodes_stat;
81
Nick Piggin3e880fb2011-01-07 17:49:19 +110082static DEFINE_PER_CPU(unsigned int, nr_inodes);
Dave Chinnerfcb94f72011-07-08 14:14:38 +100083static DEFINE_PER_CPU(unsigned int, nr_unused);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040084
Manish Katiyar6b3304b2009-03-31 19:35:54 +053085static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Nick Piggin3e880fb2011-01-07 17:49:19 +110087static int get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040088{
Nick Piggin3e880fb2011-01-07 17:49:19 +110089 int i;
90 int sum = 0;
91 for_each_possible_cpu(i)
92 sum += per_cpu(nr_inodes, i);
93 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -040094}
95
96static inline int get_nr_inodes_unused(void)
97{
Dave Chinnerfcb94f72011-07-08 14:14:38 +100098 int i;
99 int sum = 0;
100 for_each_possible_cpu(i)
101 sum += per_cpu(nr_unused, i);
102 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400103}
104
105int get_nr_dirty_inodes(void)
106{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100107 /* not actually dirty inodes, but a wild approximation */
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400108 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
109 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400110}
111
112/*
113 * Handle nr_inode sysctl
114 */
115#ifdef CONFIG_SYSCTL
116int proc_nr_inodes(ctl_table *table, int write,
117 void __user *buffer, size_t *lenp, loff_t *ppos)
118{
119 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000120 inodes_stat.nr_unused = get_nr_inodes_unused();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400121 return proc_dointvec(table, write, buffer, lenp, ppos);
122}
123#endif
124
David Chinner2cb15992008-10-30 17:32:23 +1100125/**
126 * inode_init_always - perform inode structure intialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800127 * @sb: superblock inode belongs to
128 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100129 *
130 * These are initializations that need to be done on every inode
131 * allocation as the fields are not initialised by slab allocation.
132 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300133int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700135 static const struct inode_operations empty_iops;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800136 static const struct file_operations empty_fops;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530137 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100138
139 inode->i_sb = sb;
140 inode->i_blkbits = sb->s_blocksize_bits;
141 inode->i_flags = 0;
142 atomic_set(&inode->i_count, 1);
143 inode->i_op = &empty_iops;
144 inode->i_fop = &empty_fops;
145 inode->i_nlink = 1;
Al Viro56ff5ef2008-12-09 09:34:39 -0500146 inode->i_uid = 0;
147 inode->i_gid = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100148 atomic_set(&inode->i_writecount, 0);
149 inode->i_size = 0;
150 inode->i_blocks = 0;
151 inode->i_bytes = 0;
152 inode->i_generation = 0;
153#ifdef CONFIG_QUOTA
154 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
155#endif
156 inode->i_pipe = NULL;
157 inode->i_bdev = NULL;
158 inode->i_cdev = NULL;
159 inode->i_rdev = 0;
160 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500161
162 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300163 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100164 spin_lock_init(&inode->i_lock);
165 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
166
167 mutex_init(&inode->i_mutex);
168 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
169
Christoph Hellwigbd5fe6c2011-06-24 14:29:43 -0400170 atomic_set(&inode->i_dio_count, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100171
172 mapping->a_ops = &empty_aops;
173 mapping->host = inode;
174 mapping->flags = 0;
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800175 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
David Chinner2cb15992008-10-30 17:32:23 +1100176 mapping->assoc_mapping = NULL;
177 mapping->backing_dev_info = &default_backing_dev_info;
178 mapping->writeback_index = 0;
179
180 /*
181 * If the block_device provides a backing_dev_info for client
182 * inodes then use that. Otherwise the inode share the bdev's
183 * backing_dev_info.
184 */
185 if (sb->s_bdev) {
186 struct backing_dev_info *bdi;
187
Jens Axboe2c96ce92009-09-15 09:43:56 +0200188 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
David Chinner2cb15992008-10-30 17:32:23 +1100189 mapping->backing_dev_info = bdi;
190 }
191 inode->i_private = NULL;
192 inode->i_mapping = mapping;
Al Virof19d4a82009-06-08 19:50:45 -0400193#ifdef CONFIG_FS_POSIX_ACL
194 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
195#endif
David Chinner2cb15992008-10-30 17:32:23 +1100196
Eric Paris3be25f42009-05-21 17:01:26 -0400197#ifdef CONFIG_FSNOTIFY
198 inode->i_fsnotify_mask = 0;
199#endif
200
Nick Piggin3e880fb2011-01-07 17:49:19 +1100201 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400202
Christoph Hellwig54e34622009-08-07 14:38:25 -0300203 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300204out:
205 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100206}
207EXPORT_SYMBOL(inode_init_always);
208
209static struct inode *alloc_inode(struct super_block *sb)
210{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct inode *inode;
212
213 if (sb->s_op->alloc_inode)
214 inode = sb->s_op->alloc_inode(sb);
215 else
David Chinner2cb15992008-10-30 17:32:23 +1100216 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Christoph Hellwig54e34622009-08-07 14:38:25 -0300218 if (!inode)
219 return NULL;
220
221 if (unlikely(inode_init_always(sb, inode))) {
222 if (inode->i_sb->s_op->destroy_inode)
223 inode->i_sb->s_op->destroy_inode(inode);
224 else
225 kmem_cache_free(inode_cachep, inode);
226 return NULL;
227 }
228
229 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Nick Pigginff0c7d12011-01-07 17:49:50 +1100232void free_inode_nonrcu(struct inode *inode)
233{
234 kmem_cache_free(inode_cachep, inode);
235}
236EXPORT_SYMBOL(free_inode_nonrcu);
237
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300238void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200240 BUG_ON(inode_has_buffers(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400242 fsnotify_inode_delete(inode);
Al Virof19d4a82009-06-08 19:50:45 -0400243#ifdef CONFIG_FS_POSIX_ACL
244 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
245 posix_acl_release(inode->i_acl);
246 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
247 posix_acl_release(inode->i_default_acl);
248#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100249 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300250}
251EXPORT_SYMBOL(__destroy_inode);
252
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100253static void i_callback(struct rcu_head *head)
254{
255 struct inode *inode = container_of(head, struct inode, i_rcu);
256 INIT_LIST_HEAD(&inode->i_dentry);
257 kmem_cache_free(inode_cachep, inode);
258}
259
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200260static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300261{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100262 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300263 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (inode->i_sb->s_op->destroy_inode)
265 inode->i_sb->s_op->destroy_inode(inode);
266 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100267 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100270void address_space_init_once(struct address_space *mapping)
271{
272 memset(mapping, 0, sizeof(*mapping));
273 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
274 spin_lock_init(&mapping->tree_lock);
Peter Zijlstra3d48ae42011-05-24 17:12:06 -0700275 mutex_init(&mapping->i_mmap_mutex);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100276 INIT_LIST_HEAD(&mapping->private_list);
277 spin_lock_init(&mapping->private_lock);
278 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
279 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100280}
281EXPORT_SYMBOL(address_space_init_once);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
284 * These are initializations that only need to be done
285 * once, because the fields are idempotent across use
286 * of the inode, so let the slab aware of that.
287 */
288void inode_init_once(struct inode *inode)
289{
290 memset(inode, 0, sizeof(*inode));
291 INIT_HLIST_NODE(&inode->i_hash);
292 INIT_LIST_HEAD(&inode->i_dentry);
293 INIT_LIST_HEAD(&inode->i_devices);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100294 INIT_LIST_HEAD(&inode->i_wb_list);
295 INIT_LIST_HEAD(&inode->i_lru);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100296 address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 i_size_ordered_init(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400298#ifdef CONFIG_FSNOTIFY
Eric Parise61ce862009-12-17 21:24:24 -0500299 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302EXPORT_SYMBOL(inode_init_once);
303
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700304static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530306 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Christoph Lametera35afb82007-05-16 22:10:57 -0700308 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100312 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530314void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Nick Piggin9e38d862010-10-23 06:55:17 -0400316 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Al Viro7de9c6ee2010-10-23 11:11:40 -0400319/*
320 * get additional reference to inode; caller must already hold one.
321 */
322void ihold(struct inode *inode)
323{
324 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
325}
326EXPORT_SYMBOL(ihold);
327
Nick Piggin9e38d862010-10-23 06:55:17 -0400328static void inode_lru_list_add(struct inode *inode)
329{
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000330 spin_lock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100331 if (list_empty(&inode->i_lru)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000332 list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
333 inode->i_sb->s_nr_inodes_unused++;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000334 this_cpu_inc(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400335 }
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000336 spin_unlock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin9e38d862010-10-23 06:55:17 -0400337}
338
339static void inode_lru_list_del(struct inode *inode)
340{
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000341 spin_lock(&inode->i_sb->s_inode_lru_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100342 if (!list_empty(&inode->i_lru)) {
343 list_del_init(&inode->i_lru);
Dave Chinner98b745c2011-07-08 14:14:39 +1000344 inode->i_sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000345 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400346 }
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000347 spin_unlock(&inode->i_sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Christoph Hellwig646ec462010-10-23 07:15:32 -0400350/**
351 * inode_sb_list_add - add inode to the superblock list of inodes
352 * @inode: inode to add
353 */
354void inode_sb_list_add(struct inode *inode)
355{
Dave Chinner55fa6092011-03-22 22:23:40 +1100356 spin_lock(&inode_sb_list_lock);
357 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
358 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400359}
360EXPORT_SYMBOL_GPL(inode_sb_list_add);
361
Dave Chinner55fa6092011-03-22 22:23:40 +1100362static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400363{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200364 if (!list_empty(&inode->i_sb_list)) {
365 spin_lock(&inode_sb_list_lock);
366 list_del_init(&inode->i_sb_list);
367 spin_unlock(&inode_sb_list_lock);
368 }
Christoph Hellwig646ec462010-10-23 07:15:32 -0400369}
370
Dave Chinner4c51acb2010-10-23 06:58:09 -0400371static unsigned long hash(struct super_block *sb, unsigned long hashval)
372{
373 unsigned long tmp;
374
375 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
376 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400377 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
378 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400379}
380
381/**
382 * __insert_inode_hash - hash an inode
383 * @inode: unhashed inode
384 * @hashval: unsigned long value used to locate this object in the
385 * inode_hashtable.
386 *
387 * Add an inode to the inode hash for this superblock.
388 */
389void __insert_inode_hash(struct inode *inode, unsigned long hashval)
390{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400391 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
392
Dave Chinner67a23c42011-03-22 22:23:42 +1100393 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100394 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400395 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100396 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100397 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400398}
399EXPORT_SYMBOL(__insert_inode_hash);
400
401/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400402 * remove_inode_hash - remove an inode from the hash
403 * @inode: inode to unhash
404 *
405 * Remove an inode from the superblock.
406 */
407void remove_inode_hash(struct inode *inode)
408{
Dave Chinner67a23c42011-03-22 22:23:42 +1100409 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100410 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400411 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100412 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100413 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400414}
415EXPORT_SYMBOL(remove_inode_hash);
416
Al Virob0683aa2010-06-04 20:55:25 -0400417void end_writeback(struct inode *inode)
418{
419 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700420 /*
421 * We have to cycle tree_lock here because reclaim can be still in the
422 * process of removing the last page (in __delete_from_page_cache())
423 * and we must not free mapping under it.
424 */
425 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400426 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700427 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400428 BUG_ON(!list_empty(&inode->i_data.private_list));
429 BUG_ON(!(inode->i_state & I_FREEING));
430 BUG_ON(inode->i_state & I_CLEAR);
431 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100432 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400433 inode->i_state = I_FREEING | I_CLEAR;
434}
435EXPORT_SYMBOL(end_writeback);
436
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100437/*
438 * Free the inode passed in, removing it from the lists it is still connected
439 * to. We remove any pages still attached to the inode and wait for any IO that
440 * is still in progress before finally destroying the inode.
441 *
442 * An inode must already be marked I_FREEING so that we avoid the inode being
443 * moved back onto lists if we race with other code that manipulates the lists
444 * (e.g. writeback_single_inode). The caller is responsible for setting this.
445 *
446 * An inode must already be removed from the LRU list before being evicted from
447 * the cache. This should occur atomically with setting the I_FREEING state
448 * flag, so no inodes here should ever be on the LRU when being evicted.
449 */
Al Viro644da592010-06-07 13:21:05 -0400450static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400451{
452 const struct super_operations *op = inode->i_sb->s_op;
453
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100454 BUG_ON(!(inode->i_state & I_FREEING));
455 BUG_ON(!list_empty(&inode->i_lru));
456
Dave Chinnera66979a2011-03-22 22:23:41 +1100457 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100458 inode_sb_list_del(inode);
459
Al Virobe7ce412010-06-04 19:40:39 -0400460 if (op->evict_inode) {
461 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400462 } else {
463 if (inode->i_data.nrpages)
464 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400465 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400466 }
Al Viro661074e2010-06-04 20:19:55 -0400467 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
468 bd_forget(inode);
469 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
470 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100471
472 remove_inode_hash(inode);
473
474 spin_lock(&inode->i_lock);
475 wake_up_bit(&inode->i_state, __I_NEW);
476 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
477 spin_unlock(&inode->i_lock);
478
479 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/*
483 * dispose_list - dispose of the contents of a local list
484 * @head: the head of the list to free
485 *
486 * Dispose-list gets a local list with local inodes in it, so it doesn't
487 * need to worry about list corruption and SMP locks.
488 */
489static void dispose_list(struct list_head *head)
490{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 while (!list_empty(head)) {
492 struct inode *inode;
493
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100494 inode = list_first_entry(head, struct inode, i_lru);
495 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Al Viro644da592010-06-07 13:21:05 -0400497 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/**
Al Viro63997e92010-10-25 20:49:35 -0400502 * evict_inodes - evict all evictable inodes for a superblock
503 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
Al Viro63997e92010-10-25 20:49:35 -0400505 * Make sure that no inodes with zero refcount are retained. This is
506 * called by superblock shutdown after having MS_ACTIVE flag removed,
507 * so any inode reaching zero refcount during or after that call will
508 * be immediately evicted.
509 */
510void evict_inodes(struct super_block *sb)
511{
512 struct inode *inode, *next;
513 LIST_HEAD(dispose);
514
Dave Chinner55fa6092011-03-22 22:23:40 +1100515 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400516 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
517 if (atomic_read(&inode->i_count))
518 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100519
520 spin_lock(&inode->i_lock);
521 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
522 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400523 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100524 }
Al Viro63997e92010-10-25 20:49:35 -0400525
526 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100527 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100528 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100529 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400530 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100531 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400532
533 dispose_list(&dispose);
Al Viro63997e92010-10-25 20:49:35 -0400534}
535
536/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200537 * invalidate_inodes - attempt to free all inodes on a superblock
538 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100539 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200540 *
541 * Attempts to free all inodes for a given superblock. If there were any
542 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100543 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
544 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
NeilBrown93b270f2011-02-24 17:25:47 +1100546int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400548 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200549 struct inode *inode, *next;
550 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dave Chinner55fa6092011-03-22 22:23:40 +1100552 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200553 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100554 spin_lock(&inode->i_lock);
555 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
556 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100558 }
NeilBrown93b270f2011-02-24 17:25:47 +1100559 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100560 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100561 busy = 1;
562 continue;
563 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200564 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100565 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200566 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 continue;
568 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200569
Christoph Hellwig99a38912010-10-23 19:07:20 +0200570 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100571 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100572 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100573 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100575 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Christoph Hellwiga0318782010-10-24 19:40:33 +0200577 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 return busy;
580}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582static int can_unuse(struct inode *inode)
583{
Nick Piggin9e38d862010-10-23 06:55:17 -0400584 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586 if (inode_has_buffers(inode))
587 return 0;
588 if (atomic_read(&inode->i_count))
589 return 0;
590 if (inode->i_data.nrpages)
591 return 0;
592 return 1;
593}
594
595/*
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000596 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
597 * This is called from the superblock shrinker function with a number of inodes
598 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
599 * then are freed outside inode_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
601 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400602 * pagecache removed. If the inode has metadata buffers attached to
603 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400605 * If the inode has the I_REFERENCED flag set, then it means that it has been
606 * used recently - the flag is set in iput_final(). When we encounter such an
607 * inode, clear the flag and move it to the back of the LRU so it gets another
608 * pass through the LRU before it gets reclaimed. This is necessary because of
609 * the fact we are doing lazy LRU updates to minimise lock contention so the
610 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
611 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000613void prune_icache_sb(struct super_block *sb, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int nr_scanned;
617 unsigned long reap = 0;
618
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000619 spin_lock(&sb->s_inode_lru_lock);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000620 for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct inode *inode;
622
Dave Chinner98b745c2011-07-08 14:14:39 +1000623 if (list_empty(&sb->s_inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625
Dave Chinner98b745c2011-07-08 14:14:39 +1000626 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Nick Piggin9e38d862010-10-23 06:55:17 -0400628 /*
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000629 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
Dave Chinner02afc412011-03-22 22:23:38 +1100630 * so use a trylock. If we fail to get the lock, just move the
631 * inode to the back of the list so we don't spin on it.
632 */
633 if (!spin_trylock(&inode->i_lock)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000634 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +1100635 continue;
636 }
637
638 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400639 * Referenced or dirty inodes are still in use. Give them
640 * another pass through the LRU as we canot reclaim them now.
641 */
642 if (atomic_read(&inode->i_count) ||
643 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100644 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100645 spin_unlock(&inode->i_lock);
Dave Chinner98b745c2011-07-08 14:14:39 +1000646 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000647 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400648 continue;
649 }
650
651 /* recently referenced inodes get one more pass */
652 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400653 inode->i_state &= ~I_REFERENCED;
Dave Chinner98b745c2011-07-08 14:14:39 +1000654 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100655 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 continue;
657 }
658 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
659 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100660 spin_unlock(&inode->i_lock);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000661 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800663 reap += invalidate_mapping_pages(&inode->i_data,
664 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 iput(inode);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000666 spin_lock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dave Chinner98b745c2011-07-08 14:14:39 +1000668 if (inode != list_entry(sb->s_inode_lru.next,
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100669 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100671 /* avoid lock inversions with trylock */
672 if (!spin_trylock(&inode->i_lock))
673 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100674 if (!can_unuse(inode)) {
675 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700679 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100681 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100682
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100683 list_move(&inode->i_lru, &freeable);
Dave Chinner98b745c2011-07-08 14:14:39 +1000684 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000685 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700687 if (current_is_kswapd())
688 __count_vm_events(KSWAPD_INODESTEAL, reap);
689 else
690 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000691 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 dispose_list(&freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static void __wait_on_freeing_inode(struct inode *inode);
697/*
698 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530700static struct inode *find_inode(struct super_block *sb,
701 struct hlist_head *head,
702 int (*test)(struct inode *, void *),
703 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530706 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700709 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100710 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100711 if (inode->i_sb != sb) {
712 spin_unlock(&inode->i_lock);
713 continue;
714 }
715 if (!test(inode, data)) {
716 spin_unlock(&inode->i_lock);
717 continue;
718 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400719 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 __wait_on_freeing_inode(inode);
721 goto repeat;
722 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400723 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100724 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400725 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400727 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730/*
731 * find_inode_fast is the fast path version of find_inode, see the comment at
732 * iget_locked for details.
733 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530734static struct inode *find_inode_fast(struct super_block *sb,
735 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530738 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700741 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100742 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100743 if (inode->i_ino != ino) {
744 spin_unlock(&inode->i_lock);
745 continue;
746 }
747 if (inode->i_sb != sb) {
748 spin_unlock(&inode->i_lock);
749 continue;
750 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400751 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 __wait_on_freeing_inode(inode);
753 goto repeat;
754 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400755 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100756 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400757 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400759 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Eric Dumazetf991bd22010-10-23 11:18:01 -0400762/*
763 * Each cpu owns a range of LAST_INO_BATCH numbers.
764 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
765 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100766 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400767 * This does not significantly increase overflow rate because every CPU can
768 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
769 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
770 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
771 * overflow rate by 2x, which does not seem too significant.
772 *
773 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
774 * error if st_ino won't fit in target struct field. Use 32bit counter
775 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100776 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400777#define LAST_INO_BATCH 1024
778static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100779
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400780unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400781{
782 unsigned int *p = &get_cpu_var(last_ino);
783 unsigned int res = *p;
784
785#ifdef CONFIG_SMP
786 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
787 static atomic_t shared_last_ino;
788 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
789
790 res = next - LAST_INO_BATCH;
791 }
792#endif
793
794 *p = ++res;
795 put_cpu_var(last_ino);
796 return res;
David Chinner8290c352008-10-30 17:35:24 +1100797}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400798EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200801 * new_inode_pseudo - obtain an inode
802 * @sb: superblock
803 *
804 * Allocates a new inode for given superblock.
805 * Inode wont be chained in superblock s_inodes list
806 * This means :
807 * - fs can't be unmount
808 * - quotas, fsnotify, writeback can't work
809 */
810struct inode *new_inode_pseudo(struct super_block *sb)
811{
812 struct inode *inode = alloc_inode(sb);
813
814 if (inode) {
815 spin_lock(&inode->i_lock);
816 inode->i_state = 0;
817 spin_unlock(&inode->i_lock);
818 INIT_LIST_HEAD(&inode->i_sb_list);
819 }
820 return inode;
821}
822
823/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * new_inode - obtain an inode
825 * @sb: superblock
826 *
Mel Gorman769848c2007-07-17 04:03:05 -0700827 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800828 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700829 * If HIGHMEM pages are unsuitable or it is known that pages allocated
830 * for the page cache are not reclaimable or migratable,
831 * mapping_set_gfp_mask() must be called with suitable flags on the
832 * newly created inode's mapping
833 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
835struct inode *new_inode(struct super_block *sb)
836{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530837 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Dave Chinner55fa6092011-03-22 22:23:40 +1100839 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530840
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200841 inode = new_inode_pseudo(sb);
842 if (inode)
Dave Chinner55fa6092011-03-22 22:23:40 +1100843 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return inode;
845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846EXPORT_SYMBOL(new_inode);
847
Dave Chinner250df6e2011-03-22 22:23:36 +1100848/**
849 * unlock_new_inode - clear the I_NEW state and wake up any waiters
850 * @inode: new inode to unlock
851 *
852 * Called when the inode is fully initialised to clear the new state of the
853 * inode and wake up anyone waiting for the inode to finish initialisation.
854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855void unlock_new_inode(struct inode *inode)
856{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200857#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900858 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200859 struct file_system_type *type = inode->i_sb->s_type;
860
Jan Kara9a7aa122009-06-04 15:26:49 +0200861 /* Set new key only if filesystem hasn't already changed it */
862 if (!lockdep_match_class(&inode->i_mutex,
863 &type->i_mutex_key)) {
864 /*
865 * ensure nobody is actually holding i_mutex
866 */
867 mutex_destroy(&inode->i_mutex);
868 mutex_init(&inode->i_mutex);
869 lockdep_set_class(&inode->i_mutex,
870 &type->i_mutex_dir_key);
871 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200872 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200873#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100874 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100875 WARN_ON(!(inode->i_state & I_NEW));
876 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100877 wake_up_bit(&inode->i_state, __I_NEW);
878 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880EXPORT_SYMBOL(unlock_new_inode);
881
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400882/**
883 * iget5_locked - obtain an inode from a mounted file system
884 * @sb: super block of file system
885 * @hashval: hash value (usually inode number) to get
886 * @test: callback used for comparisons between inodes
887 * @set: callback used to initialize a new struct inode
888 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400890 * Search for the inode specified by @hashval and @data in the inode cache,
891 * and if present it is return it with an increased reference count. This is
892 * a generalized version of iget_locked() for file systems where the inode
893 * number is not sufficient for unique identification of an inode.
894 *
895 * If the inode is not in cache, allocate a new inode and return it locked,
896 * hashed, and with the I_NEW flag set. The file system gets to fill it in
897 * before unlocking it via unlock_new_inode().
898 *
899 * Note both @test and @set are called with the inode_hash_lock held, so can't
900 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400902struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
903 int (*test)(struct inode *, void *),
904 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400906 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530907 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400909 spin_lock(&inode_hash_lock);
910 inode = find_inode(sb, head, test, data);
911 spin_unlock(&inode_hash_lock);
912
913 if (inode) {
914 wait_on_inode(inode);
915 return inode;
916 }
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 inode = alloc_inode(sb);
919 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530920 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Dave Chinner67a23c42011-03-22 22:23:42 +1100922 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /* We released the lock, so.. */
924 old = find_inode(sb, head, test, data);
925 if (!old) {
926 if (set(inode, data))
927 goto set_failed;
928
Dave Chinner250df6e2011-03-22 22:23:36 +1100929 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100930 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100931 hlist_add_head(&inode->i_hash, head);
932 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100933 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100934 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* Return the locked inode with I_NEW set, the
937 * caller is responsible for filling in the contents
938 */
939 return inode;
940 }
941
942 /*
943 * Uhhuh, somebody else created the same inode under
944 * us. Use the old inode instead of the one we just
945 * allocated.
946 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100947 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 destroy_inode(inode);
949 inode = old;
950 wait_on_inode(inode);
951 }
952 return inode;
953
954set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100955 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 destroy_inode(inode);
957 return NULL;
958}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400959EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400961/**
962 * iget_locked - obtain an inode from a mounted file system
963 * @sb: super block of file system
964 * @ino: inode number to get
965 *
966 * Search for the inode specified by @ino in the inode cache and if present
967 * return it with an increased reference count. This is for file systems
968 * where the inode number is sufficient for unique identification of an inode.
969 *
970 * If the inode is not in cache, allocate a new inode and return it locked,
971 * hashed, and with the I_NEW flag set. The file system gets to fill it in
972 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400974struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400976 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530977 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400979 spin_lock(&inode_hash_lock);
980 inode = find_inode_fast(sb, head, ino);
981 spin_unlock(&inode_hash_lock);
982 if (inode) {
983 wait_on_inode(inode);
984 return inode;
985 }
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 inode = alloc_inode(sb);
988 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530989 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dave Chinner67a23c42011-03-22 22:23:42 +1100991 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* We released the lock, so.. */
993 old = find_inode_fast(sb, head, ino);
994 if (!old) {
995 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +1100996 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100997 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100998 hlist_add_head(&inode->i_hash, head);
999 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001000 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001001 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /* Return the locked inode with I_NEW set, the
1004 * caller is responsible for filling in the contents
1005 */
1006 return inode;
1007 }
1008
1009 /*
1010 * Uhhuh, somebody else created the same inode under
1011 * us. Use the old inode instead of the one we just
1012 * allocated.
1013 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001014 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 destroy_inode(inode);
1016 inode = old;
1017 wait_on_inode(inode);
1018 }
1019 return inode;
1020}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001021EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001023/*
1024 * search the inode cache for a matching inode number.
1025 * If we find one, then the inode number we are trying to
1026 * allocate is not unique and so we should not use it.
1027 *
1028 * Returns 1 if the inode number is unique, 0 if it is not.
1029 */
1030static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1031{
1032 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1033 struct hlist_node *node;
1034 struct inode *inode;
1035
Dave Chinner67a23c42011-03-22 22:23:42 +11001036 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001037 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001038 if (inode->i_ino == ino && inode->i_sb == sb) {
1039 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001040 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001041 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001042 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001043 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001044
1045 return 1;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/**
1049 * iunique - get a unique inode number
1050 * @sb: superblock
1051 * @max_reserved: highest reserved inode number
1052 *
1053 * Obtain an inode number that is unique on the system for a given
1054 * superblock. This is used by file systems that have no natural
1055 * permanent inode numbering system. An inode number is returned that
1056 * is higher than the reserved limit but unique.
1057 *
1058 * BUGS:
1059 * With a large number of inodes live on the file system this function
1060 * currently becomes quite slow.
1061 */
1062ino_t iunique(struct super_block *sb, ino_t max_reserved)
1063{
Jeff Layton866b04f2007-05-08 00:32:29 -07001064 /*
1065 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1066 * error if st_ino won't fit in target struct field. Use 32bit counter
1067 * here to attempt to avoid that.
1068 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001069 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001070 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001073 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001074 do {
1075 if (counter <= max_reserved)
1076 counter = max_reserved + 1;
1077 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001078 } while (!test_inode_iunique(sb, res));
1079 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001080
1081 return res;
1082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083EXPORT_SYMBOL(iunique);
1084
1085struct inode *igrab(struct inode *inode)
1086{
Dave Chinner250df6e2011-03-22 22:23:36 +11001087 spin_lock(&inode->i_lock);
1088 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001090 spin_unlock(&inode->i_lock);
1091 } else {
1092 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Handle the case where s_op->clear_inode is not been
1095 * called yet, and somebody is calling igrab
1096 * while the inode is getting freed.
1097 */
1098 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return inode;
1101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102EXPORT_SYMBOL(igrab);
1103
1104/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001105 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 * @sb: super block of file system to search
1107 * @hashval: hash value (usually inode number) to search for
1108 * @test: callback used for comparisons between inodes
1109 * @data: opaque data pointer to pass to @test
1110 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001111 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001113 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001114 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001115 * Note: I_NEW is not waited upon so you have to be very careful what you do
1116 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001117 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001118 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001119 */
1120struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1121 int (*test)(struct inode *, void *), void *data)
1122{
1123 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001124 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001125
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001126 spin_lock(&inode_hash_lock);
1127 inode = find_inode(sb, head, test, data);
1128 spin_unlock(&inode_hash_lock);
1129
1130 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001131}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001132EXPORT_SYMBOL(ilookup5_nowait);
1133
1134/**
1135 * ilookup5 - search for an inode in the inode cache
1136 * @sb: super block of file system to search
1137 * @hashval: hash value (usually inode number) to search for
1138 * @test: callback used for comparisons between inodes
1139 * @data: opaque data pointer to pass to @test
1140 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001141 * Search for the inode specified by @hashval and @data in the inode cache,
1142 * and if the inode is in the cache, return the inode with an incremented
1143 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001144 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001146 * This is a generalized version of ilookup() for file systems where the
1147 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001149 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
1151struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1152 int (*test)(struct inode *, void *), void *data)
1153{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001154 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001156 if (inode)
1157 wait_on_inode(inode);
1158 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160EXPORT_SYMBOL(ilookup5);
1161
1162/**
1163 * ilookup - search for an inode in the inode cache
1164 * @sb: super block of file system to search
1165 * @ino: inode number to search for
1166 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001167 * Search for the inode @ino in the inode cache, and if the inode is in the
1168 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170struct inode *ilookup(struct super_block *sb, unsigned long ino)
1171{
1172 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001173 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001175 spin_lock(&inode_hash_lock);
1176 inode = find_inode_fast(sb, head, ino);
1177 spin_unlock(&inode_hash_lock);
1178
1179 if (inode)
1180 wait_on_inode(inode);
1181 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183EXPORT_SYMBOL(ilookup);
1184
Al Viro261bca82008-12-30 01:48:21 -05001185int insert_inode_locked(struct inode *inode)
1186{
1187 struct super_block *sb = inode->i_sb;
1188 ino_t ino = inode->i_ino;
1189 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001190
Al Viro261bca82008-12-30 01:48:21 -05001191 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001192 struct hlist_node *node;
1193 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001194 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001195 hlist_for_each_entry(old, node, head, i_hash) {
1196 if (old->i_ino != ino)
1197 continue;
1198 if (old->i_sb != sb)
1199 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001200 spin_lock(&old->i_lock);
1201 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1202 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001203 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001204 }
Al Viro72a43d62009-05-13 19:13:40 +01001205 break;
1206 }
1207 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001208 spin_lock(&inode->i_lock);
1209 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001210 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001211 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001212 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001213 return 0;
1214 }
1215 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001216 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001217 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001218 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001219 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001220 iput(old);
1221 return -EBUSY;
1222 }
1223 iput(old);
1224 }
1225}
Al Viro261bca82008-12-30 01:48:21 -05001226EXPORT_SYMBOL(insert_inode_locked);
1227
1228int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1229 int (*test)(struct inode *, void *), void *data)
1230{
1231 struct super_block *sb = inode->i_sb;
1232 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001233
Al Viro261bca82008-12-30 01:48:21 -05001234 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001235 struct hlist_node *node;
1236 struct inode *old = NULL;
1237
Dave Chinner67a23c42011-03-22 22:23:42 +11001238 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001239 hlist_for_each_entry(old, node, head, i_hash) {
1240 if (old->i_sb != sb)
1241 continue;
1242 if (!test(old, data))
1243 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001244 spin_lock(&old->i_lock);
1245 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1246 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001247 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001248 }
Al Viro72a43d62009-05-13 19:13:40 +01001249 break;
1250 }
1251 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001252 spin_lock(&inode->i_lock);
1253 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001254 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001255 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001256 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001257 return 0;
1258 }
1259 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001260 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001261 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001262 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001263 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001264 iput(old);
1265 return -EBUSY;
1266 }
1267 iput(old);
1268 }
1269}
Al Viro261bca82008-12-30 01:48:21 -05001270EXPORT_SYMBOL(insert_inode_locked4);
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Al Viro45321ac2010-06-07 13:43:19 -04001273int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Al Viro45321ac2010-06-07 13:43:19 -04001275 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277EXPORT_SYMBOL(generic_delete_inode);
1278
Al Viro45321ac2010-06-07 13:43:19 -04001279/*
1280 * Normal UNIX filesystem behaviour: delete the
1281 * inode when the usage count drops to zero, and
1282 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001283 */
Al Viro45321ac2010-06-07 13:43:19 -04001284int generic_drop_inode(struct inode *inode)
1285{
Al Viro1d3382cb2010-10-23 15:19:20 -04001286 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001287}
1288EXPORT_SYMBOL_GPL(generic_drop_inode);
1289
1290/*
1291 * Called when we're dropping the last reference
1292 * to an inode.
1293 *
1294 * Call the FS "drop_inode()" function, defaulting to
1295 * the legacy UNIX filesystem behaviour. If it tells
1296 * us to evict inode, do so. Otherwise, retain inode
1297 * in cache if fs is alive, sync and evict if fs is
1298 * shutting down.
1299 */
1300static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001303 const struct super_operations *op = inode->i_sb->s_op;
1304 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Dave Chinner250df6e2011-03-22 22:23:36 +11001306 WARN_ON(inode->i_state & I_NEW);
1307
Al Viroe7f59092011-07-07 15:45:59 -04001308 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001309 drop = op->drop_inode(inode);
1310 else
1311 drop = generic_drop_inode(inode);
1312
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001313 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1314 inode->i_state |= I_REFERENCED;
1315 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1316 inode_lru_list_add(inode);
1317 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001318 return;
1319 }
1320
Al Viro45321ac2010-06-07 13:43:19 -04001321 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001322 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001323 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001325 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001326 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001327 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001329
Alexander Viro991114c2005-06-23 00:09:01 -07001330 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001331 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001332 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001333
1334 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301338 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * @inode: inode to put
1340 *
1341 * Puts an inode, dropping its usage count. If the inode use count hits
1342 * zero, the inode is then freed and may also be destroyed.
1343 *
1344 * Consequently, iput() can sleep.
1345 */
1346void iput(struct inode *inode)
1347{
1348 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001349 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Dave Chinnerf283c862011-03-22 22:23:39 +11001351 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 iput_final(inode);
1353 }
1354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355EXPORT_SYMBOL(iput);
1356
1357/**
1358 * bmap - find a block number in a file
1359 * @inode: inode of file
1360 * @block: block to find
1361 *
1362 * Returns the block number on the device holding the inode that
1363 * is the disk block number for the block of the file requested.
1364 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301365 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * file.
1367 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301368sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 sector_t res = 0;
1371 if (inode->i_mapping->a_ops->bmap)
1372 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1373 return res;
1374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375EXPORT_SYMBOL(bmap);
1376
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001377/*
1378 * With relative atime, only update atime if the previous atime is
1379 * earlier than either the ctime or mtime or if at least a day has
1380 * passed since the last atime update.
1381 */
1382static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1383 struct timespec now)
1384{
1385
1386 if (!(mnt->mnt_flags & MNT_RELATIME))
1387 return 1;
1388 /*
1389 * Is mtime younger than atime? If yes, update atime:
1390 */
1391 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1392 return 1;
1393 /*
1394 * Is ctime younger than atime? If yes, update atime:
1395 */
1396 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1397 return 1;
1398
1399 /*
1400 * Is the previous atime value older than a day? If yes,
1401 * update atime:
1402 */
1403 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1404 return 1;
1405 /*
1406 * Good, we can skip the atime update:
1407 */
1408 return 0;
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001412 * touch_atime - update the access time
1413 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001414 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 *
1416 * Update the accessed time on an inode and mark it for writeback.
1417 * This function automatically handles read only file systems and media,
1418 * as well as the "noatime" flag and inode specific "noatime" markers.
1419 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001420void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001422 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 struct timespec now;
1424
Andrew Mortonb2276132006-12-13 00:34:33 -08001425 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001426 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001427 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001428 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001429 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001430 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001431
Dave Hansencdb70f32008-02-15 14:37:41 -08001432 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001433 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001434 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001435 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001438
1439 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001440 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001441
Valerie Henson47ae32d2006-12-13 00:34:34 -08001442 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001443 return;
1444
1445 if (mnt_want_write(mnt))
1446 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001447
1448 inode->i_atime = now;
1449 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001450 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001452EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001455 * file_update_time - update mtime and ctime time
1456 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001458 * Update the mtime and ctime members of an inode and mark the inode
1459 * for writeback. Note that this function is meant exclusively for
1460 * usage in the file write path of filesystems, and filesystems may
1461 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001462 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001463 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
1465
Christoph Hellwig870f4812006-01-09 20:52:01 -08001466void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001468 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001470 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Andi Kleence06e0b2009-09-18 13:05:48 -07001472 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (IS_NOCMTIME(inode))
1474 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001475
Andi Kleence06e0b2009-09-18 13:05:48 -07001476 now = current_fs_time(inode->i_sb);
1477 if (!timespec_equal(&inode->i_mtime, &now))
1478 sync_it = S_MTIME;
1479
1480 if (!timespec_equal(&inode->i_ctime, &now))
1481 sync_it |= S_CTIME;
1482
1483 if (IS_I_VERSION(inode))
1484 sync_it |= S_VERSION;
1485
1486 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return;
1488
Andi Kleence06e0b2009-09-18 13:05:48 -07001489 /* Finally allowed to write? Takes lock. */
1490 if (mnt_want_write_file(file))
1491 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Andi Kleence06e0b2009-09-18 13:05:48 -07001493 /* Only change inode inside the lock region */
1494 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001495 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001496 if (sync_it & S_CTIME)
1497 inode->i_ctime = now;
1498 if (sync_it & S_MTIME)
1499 inode->i_mtime = now;
1500 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001501 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001503EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505int inode_needs_sync(struct inode *inode)
1506{
1507 if (IS_SYNC(inode))
1508 return 1;
1509 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1510 return 1;
1511 return 0;
1512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513EXPORT_SYMBOL(inode_needs_sync);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515int inode_wait(void *word)
1516{
1517 schedule();
1518 return 0;
1519}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001520EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001523 * If we try to find an inode in the inode hash while it is being
1524 * deleted, we have to wait until the filesystem completes its
1525 * deletion before reporting that it isn't found. This function waits
1526 * until the deletion _might_ have completed. Callers are responsible
1527 * to recheck inode state.
1528 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001529 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001530 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1531 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
1533static void __wait_on_freeing_inode(struct inode *inode)
1534{
1535 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001536 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1537 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001539 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001540 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 schedule();
1542 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001543 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546static __initdata unsigned long ihash_entries;
1547static int __init set_ihash_entries(char *str)
1548{
1549 if (!str)
1550 return 0;
1551 ihash_entries = simple_strtoul(str, &str, 0);
1552 return 1;
1553}
1554__setup("ihash_entries=", set_ihash_entries);
1555
1556/*
1557 * Initialize the waitqueues and inode hash table.
1558 */
1559void __init inode_init_early(void)
1560{
1561 int loop;
1562
1563 /* If hashes are distributed across NUMA nodes, defer
1564 * hash allocation until vmalloc space is available.
1565 */
1566 if (hashdist)
1567 return;
1568
1569 inode_hashtable =
1570 alloc_large_system_hash("Inode-cache",
1571 sizeof(struct hlist_head),
1572 ihash_entries,
1573 14,
1574 HASH_EARLY,
1575 &i_hash_shift,
1576 &i_hash_mask,
1577 0);
1578
1579 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1580 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1581}
1582
Denis Cheng74bf17c2007-10-16 23:26:30 -07001583void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 int loop;
1586
1587 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001588 inode_cachep = kmem_cache_create("inode_cache",
1589 sizeof(struct inode),
1590 0,
1591 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1592 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001593 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /* Hash may have been set up in inode_init_early */
1596 if (!hashdist)
1597 return;
1598
1599 inode_hashtable =
1600 alloc_large_system_hash("Inode-cache",
1601 sizeof(struct hlist_head),
1602 ihash_entries,
1603 14,
1604 0,
1605 &i_hash_shift,
1606 &i_hash_mask,
1607 0);
1608
1609 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1610 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1611}
1612
1613void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1614{
1615 inode->i_mode = mode;
1616 if (S_ISCHR(mode)) {
1617 inode->i_fop = &def_chr_fops;
1618 inode->i_rdev = rdev;
1619 } else if (S_ISBLK(mode)) {
1620 inode->i_fop = &def_blk_fops;
1621 inode->i_rdev = rdev;
1622 } else if (S_ISFIFO(mode))
1623 inode->i_fop = &def_fifo_fops;
1624 else if (S_ISSOCK(mode))
1625 inode->i_fop = &bad_sock_fops;
1626 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001627 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1628 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1629 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001632
1633/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00001634 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001635 * @inode: New inode
1636 * @dir: Directory inode
1637 * @mode: mode of the new inode
1638 */
1639void inode_init_owner(struct inode *inode, const struct inode *dir,
1640 mode_t mode)
1641{
1642 inode->i_uid = current_fsuid();
1643 if (dir && dir->i_mode & S_ISGID) {
1644 inode->i_gid = dir->i_gid;
1645 if (S_ISDIR(mode))
1646 mode |= S_ISGID;
1647 } else
1648 inode->i_gid = current_fsgid();
1649 inode->i_mode = mode;
1650}
1651EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001652
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001653/**
1654 * inode_owner_or_capable - check current task permissions to inode
1655 * @inode: inode being checked
1656 *
1657 * Return true if current either has CAP_FOWNER to the inode, or
1658 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001659 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001660bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001661{
1662 struct user_namespace *ns = inode_userns(inode);
1663
1664 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1665 return true;
1666 if (ns_capable(ns, CAP_FOWNER))
1667 return true;
1668 return false;
1669}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001670EXPORT_SYMBOL(inode_owner_or_capable);