blob: a48fa5355fb44ef5a8e5a3a1acbcdaea095d829e [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{
Dave Chinner55fa6092011-03-22 22:23:40 +1100364 spin_lock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400365 list_del_init(&inode->i_sb_list);
Dave Chinner55fa6092011-03-22 22:23:40 +1100366 spin_unlock(&inode_sb_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400367}
368
Dave Chinner4c51acb2010-10-23 06:58:09 -0400369static unsigned long hash(struct super_block *sb, unsigned long hashval)
370{
371 unsigned long tmp;
372
373 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
374 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400375 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
376 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400377}
378
379/**
380 * __insert_inode_hash - hash an inode
381 * @inode: unhashed inode
382 * @hashval: unsigned long value used to locate this object in the
383 * inode_hashtable.
384 *
385 * Add an inode to the inode hash for this superblock.
386 */
387void __insert_inode_hash(struct inode *inode, unsigned long hashval)
388{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400389 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
390
Dave Chinner67a23c42011-03-22 22:23:42 +1100391 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100392 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400393 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100394 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100395 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400396}
397EXPORT_SYMBOL(__insert_inode_hash);
398
399/**
Dave Chinner4c51acb2010-10-23 06:58:09 -0400400 * remove_inode_hash - remove an inode from the hash
401 * @inode: inode to unhash
402 *
403 * Remove an inode from the superblock.
404 */
405void remove_inode_hash(struct inode *inode)
406{
Dave Chinner67a23c42011-03-22 22:23:42 +1100407 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100408 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400409 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100410 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100411 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400412}
413EXPORT_SYMBOL(remove_inode_hash);
414
Al Virob0683aa2010-06-04 20:55:25 -0400415void end_writeback(struct inode *inode)
416{
417 might_sleep();
Jan Kara08142572011-06-27 16:18:10 -0700418 /*
419 * We have to cycle tree_lock here because reclaim can be still in the
420 * process of removing the last page (in __delete_from_page_cache())
421 * and we must not free mapping under it.
422 */
423 spin_lock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400424 BUG_ON(inode->i_data.nrpages);
Jan Kara08142572011-06-27 16:18:10 -0700425 spin_unlock_irq(&inode->i_data.tree_lock);
Al Virob0683aa2010-06-04 20:55:25 -0400426 BUG_ON(!list_empty(&inode->i_data.private_list));
427 BUG_ON(!(inode->i_state & I_FREEING));
428 BUG_ON(inode->i_state & I_CLEAR);
429 inode_sync_wait(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100430 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400431 inode->i_state = I_FREEING | I_CLEAR;
432}
433EXPORT_SYMBOL(end_writeback);
434
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100435/*
436 * Free the inode passed in, removing it from the lists it is still connected
437 * to. We remove any pages still attached to the inode and wait for any IO that
438 * is still in progress before finally destroying the inode.
439 *
440 * An inode must already be marked I_FREEING so that we avoid the inode being
441 * moved back onto lists if we race with other code that manipulates the lists
442 * (e.g. writeback_single_inode). The caller is responsible for setting this.
443 *
444 * An inode must already be removed from the LRU list before being evicted from
445 * the cache. This should occur atomically with setting the I_FREEING state
446 * flag, so no inodes here should ever be on the LRU when being evicted.
447 */
Al Viro644da592010-06-07 13:21:05 -0400448static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400449{
450 const struct super_operations *op = inode->i_sb->s_op;
451
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100452 BUG_ON(!(inode->i_state & I_FREEING));
453 BUG_ON(!list_empty(&inode->i_lru));
454
Dave Chinnera66979a2011-03-22 22:23:41 +1100455 inode_wb_list_del(inode);
Dave Chinner55fa6092011-03-22 22:23:40 +1100456 inode_sb_list_del(inode);
457
Al Virobe7ce412010-06-04 19:40:39 -0400458 if (op->evict_inode) {
459 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400460 } else {
461 if (inode->i_data.nrpages)
462 truncate_inode_pages(&inode->i_data, 0);
Al Viro30140832010-06-07 13:23:20 -0400463 end_writeback(inode);
Al Virob4272d42010-06-04 19:33:20 -0400464 }
Al Viro661074e2010-06-04 20:19:55 -0400465 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
466 bd_forget(inode);
467 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
468 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100469
470 remove_inode_hash(inode);
471
472 spin_lock(&inode->i_lock);
473 wake_up_bit(&inode->i_state, __I_NEW);
474 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
475 spin_unlock(&inode->i_lock);
476
477 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * dispose_list - dispose of the contents of a local list
482 * @head: the head of the list to free
483 *
484 * Dispose-list gets a local list with local inodes in it, so it doesn't
485 * need to worry about list corruption and SMP locks.
486 */
487static void dispose_list(struct list_head *head)
488{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 while (!list_empty(head)) {
490 struct inode *inode;
491
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100492 inode = list_first_entry(head, struct inode, i_lru);
493 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Al Viro644da592010-06-07 13:21:05 -0400495 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/**
Al Viro63997e92010-10-25 20:49:35 -0400500 * evict_inodes - evict all evictable inodes for a superblock
501 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
Al Viro63997e92010-10-25 20:49:35 -0400503 * Make sure that no inodes with zero refcount are retained. This is
504 * called by superblock shutdown after having MS_ACTIVE flag removed,
505 * so any inode reaching zero refcount during or after that call will
506 * be immediately evicted.
507 */
508void evict_inodes(struct super_block *sb)
509{
510 struct inode *inode, *next;
511 LIST_HEAD(dispose);
512
Dave Chinner55fa6092011-03-22 22:23:40 +1100513 spin_lock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400514 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
515 if (atomic_read(&inode->i_count))
516 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100517
518 spin_lock(&inode->i_lock);
519 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
520 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400521 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100522 }
Al Viro63997e92010-10-25 20:49:35 -0400523
524 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100525 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100526 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100527 list_add(&inode->i_lru, &dispose);
Al Viro63997e92010-10-25 20:49:35 -0400528 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100529 spin_unlock(&inode_sb_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400530
531 dispose_list(&dispose);
Al Viro63997e92010-10-25 20:49:35 -0400532}
533
534/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200535 * invalidate_inodes - attempt to free all inodes on a superblock
536 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100537 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200538 *
539 * Attempts to free all inodes for a given superblock. If there were any
540 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100541 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
542 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
NeilBrown93b270f2011-02-24 17:25:47 +1100544int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400546 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200547 struct inode *inode, *next;
548 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Dave Chinner55fa6092011-03-22 22:23:40 +1100550 spin_lock(&inode_sb_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200551 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100552 spin_lock(&inode->i_lock);
553 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
554 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100556 }
NeilBrown93b270f2011-02-24 17:25:47 +1100557 if (inode->i_state & I_DIRTY && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100558 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100559 busy = 1;
560 continue;
561 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200562 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100563 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200564 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 continue;
566 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200567
Christoph Hellwig99a38912010-10-23 19:07:20 +0200568 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100569 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100570 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100571 list_add(&inode->i_lru, &dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Dave Chinner55fa6092011-03-22 22:23:40 +1100573 spin_unlock(&inode_sb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Christoph Hellwiga0318782010-10-24 19:40:33 +0200575 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return busy;
578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580static int can_unuse(struct inode *inode)
581{
Nick Piggin9e38d862010-10-23 06:55:17 -0400582 if (inode->i_state & ~I_REFERENCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584 if (inode_has_buffers(inode))
585 return 0;
586 if (atomic_read(&inode->i_count))
587 return 0;
588 if (inode->i_data.nrpages)
589 return 0;
590 return 1;
591}
592
593/*
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000594 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
595 * This is called from the superblock shrinker function with a number of inodes
596 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
597 * then are freed outside inode_lock by dispose_list().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
599 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400600 * pagecache removed. If the inode has metadata buffers attached to
601 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400603 * If the inode has the I_REFERENCED flag set, then it means that it has been
604 * used recently - the flag is set in iput_final(). When we encounter such an
605 * inode, clear the flag and move it to the back of the LRU so it gets another
606 * pass through the LRU before it gets reclaimed. This is necessary because of
607 * the fact we are doing lazy LRU updates to minimise lock contention so the
608 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
609 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000611void prune_icache_sb(struct super_block *sb, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 LIST_HEAD(freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int nr_scanned;
615 unsigned long reap = 0;
616
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000617 spin_lock(&sb->s_inode_lru_lock);
Dave Chinnerb0d40c92011-07-08 14:14:42 +1000618 for (nr_scanned = nr_to_scan; nr_scanned >= 0; nr_scanned--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct inode *inode;
620
Dave Chinner98b745c2011-07-08 14:14:39 +1000621 if (list_empty(&sb->s_inode_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
623
Dave Chinner98b745c2011-07-08 14:14:39 +1000624 inode = list_entry(sb->s_inode_lru.prev, struct inode, i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Nick Piggin9e38d862010-10-23 06:55:17 -0400626 /*
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000627 * we are inverting the sb->s_inode_lru_lock/inode->i_lock here,
Dave Chinner02afc412011-03-22 22:23:38 +1100628 * so use a trylock. If we fail to get the lock, just move the
629 * inode to the back of the list so we don't spin on it.
630 */
631 if (!spin_trylock(&inode->i_lock)) {
Dave Chinner98b745c2011-07-08 14:14:39 +1000632 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinner02afc412011-03-22 22:23:38 +1100633 continue;
634 }
635
636 /*
Nick Piggin9e38d862010-10-23 06:55:17 -0400637 * Referenced or dirty inodes are still in use. Give them
638 * another pass through the LRU as we canot reclaim them now.
639 */
640 if (atomic_read(&inode->i_count) ||
641 (inode->i_state & ~I_REFERENCED)) {
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100642 list_del_init(&inode->i_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100643 spin_unlock(&inode->i_lock);
Dave Chinner98b745c2011-07-08 14:14:39 +1000644 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000645 this_cpu_dec(nr_unused);
Nick Piggin9e38d862010-10-23 06:55:17 -0400646 continue;
647 }
648
649 /* recently referenced inodes get one more pass */
650 if (inode->i_state & I_REFERENCED) {
Nick Piggin9e38d862010-10-23 06:55:17 -0400651 inode->i_state &= ~I_REFERENCED;
Dave Chinner98b745c2011-07-08 14:14:39 +1000652 list_move(&inode->i_lru, &sb->s_inode_lru);
Dave Chinnerf283c862011-03-22 22:23:39 +1100653 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 }
656 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
657 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100658 spin_unlock(&inode->i_lock);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000659 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (remove_inode_buffers(inode))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800661 reap += invalidate_mapping_pages(&inode->i_data,
662 0, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 iput(inode);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000664 spin_lock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dave Chinner98b745c2011-07-08 14:14:39 +1000666 if (inode != list_entry(sb->s_inode_lru.next,
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100667 struct inode, i_lru))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 continue; /* wrong inode or list_empty */
Dave Chinner02afc412011-03-22 22:23:38 +1100669 /* avoid lock inversions with trylock */
670 if (!spin_trylock(&inode->i_lock))
671 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100672 if (!can_unuse(inode)) {
673 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Nick Piggin7ef0d732009-03-12 14:31:38 -0700677 WARN_ON(inode->i_state & I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 inode->i_state |= I_FREEING;
Dave Chinner250df6e2011-03-22 22:23:36 +1100679 spin_unlock(&inode->i_lock);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100680
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100681 list_move(&inode->i_lru, &freeable);
Dave Chinner98b745c2011-07-08 14:14:39 +1000682 sb->s_nr_inodes_unused--;
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000683 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Christoph Lameterf8891e52006-06-30 01:55:45 -0700685 if (current_is_kswapd())
686 __count_vm_events(KSWAPD_INODESTEAL, reap);
687 else
688 __count_vm_events(PGINODESTEAL, reap);
Dave Chinner09cc9fc2011-07-08 14:14:40 +1000689 spin_unlock(&sb->s_inode_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 dispose_list(&freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static void __wait_on_freeing_inode(struct inode *inode);
695/*
696 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530698static struct inode *find_inode(struct super_block *sb,
699 struct hlist_head *head,
700 int (*test)(struct inode *, void *),
701 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530704 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700707 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100708 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100709 if (inode->i_sb != sb) {
710 spin_unlock(&inode->i_lock);
711 continue;
712 }
713 if (!test(inode, data)) {
714 spin_unlock(&inode->i_lock);
715 continue;
716 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400717 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 __wait_on_freeing_inode(inode);
719 goto repeat;
720 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400721 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100722 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400723 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400725 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/*
729 * find_inode_fast is the fast path version of find_inode, see the comment at
730 * iget_locked for details.
731 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530732static struct inode *find_inode_fast(struct super_block *sb,
733 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 struct hlist_node *node;
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530736 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738repeat:
Matthias Kaehlckec5c8be32008-04-29 00:59:40 -0700739 hlist_for_each_entry(inode, node, head, i_hash) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100740 spin_lock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100741 if (inode->i_ino != ino) {
742 spin_unlock(&inode->i_lock);
743 continue;
744 }
745 if (inode->i_sb != sb) {
746 spin_unlock(&inode->i_lock);
747 continue;
748 }
Al Viroa4ffdde2010-06-02 17:38:30 -0400749 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 __wait_on_freeing_inode(inode);
751 goto repeat;
752 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400753 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100754 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400755 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400757 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Eric Dumazetf991bd22010-10-23 11:18:01 -0400760/*
761 * Each cpu owns a range of LAST_INO_BATCH numbers.
762 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
763 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100764 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400765 * This does not significantly increase overflow rate because every CPU can
766 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
767 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
768 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
769 * overflow rate by 2x, which does not seem too significant.
770 *
771 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
772 * error if st_ino won't fit in target struct field. Use 32bit counter
773 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100774 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400775#define LAST_INO_BATCH 1024
776static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100777
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400778unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400779{
780 unsigned int *p = &get_cpu_var(last_ino);
781 unsigned int res = *p;
782
783#ifdef CONFIG_SMP
784 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
785 static atomic_t shared_last_ino;
786 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
787
788 res = next - LAST_INO_BATCH;
789 }
790#endif
791
792 *p = ++res;
793 put_cpu_var(last_ino);
794 return res;
David Chinner8290c352008-10-30 17:35:24 +1100795}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400796EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/**
799 * new_inode - obtain an inode
800 * @sb: superblock
801 *
Mel Gorman769848c2007-07-17 04:03:05 -0700802 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800803 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700804 * If HIGHMEM pages are unsuitable or it is known that pages allocated
805 * for the page cache are not reclaimable or migratable,
806 * mapping_set_gfp_mask() must be called with suitable flags on the
807 * newly created inode's mapping
808 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
810struct inode *new_inode(struct super_block *sb)
811{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530812 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Dave Chinner55fa6092011-03-22 22:23:40 +1100814 spin_lock_prefetch(&inode_sb_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 inode = alloc_inode(sb);
817 if (inode) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100818 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 inode->i_state = 0;
Dave Chinner250df6e2011-03-22 22:23:36 +1100820 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100821 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 return inode;
824}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825EXPORT_SYMBOL(new_inode);
826
Dave Chinner250df6e2011-03-22 22:23:36 +1100827/**
828 * unlock_new_inode - clear the I_NEW state and wake up any waiters
829 * @inode: new inode to unlock
830 *
831 * Called when the inode is fully initialised to clear the new state of the
832 * inode and wake up anyone waiting for the inode to finish initialisation.
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834void unlock_new_inode(struct inode *inode)
835{
Peter Zijlstra14358e62007-10-14 01:38:33 +0200836#ifdef CONFIG_DEBUG_LOCK_ALLOC
Namhyung Kima3314a02010-10-11 22:38:00 +0900837 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200838 struct file_system_type *type = inode->i_sb->s_type;
839
Jan Kara9a7aa122009-06-04 15:26:49 +0200840 /* Set new key only if filesystem hasn't already changed it */
841 if (!lockdep_match_class(&inode->i_mutex,
842 &type->i_mutex_key)) {
843 /*
844 * ensure nobody is actually holding i_mutex
845 */
846 mutex_destroy(&inode->i_mutex);
847 mutex_init(&inode->i_mutex);
848 lockdep_set_class(&inode->i_mutex,
849 &type->i_mutex_dir_key);
850 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200851 }
Peter Zijlstra14358e62007-10-14 01:38:33 +0200852#endif
Dave Chinner250df6e2011-03-22 22:23:36 +1100853 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100854 WARN_ON(!(inode->i_state & I_NEW));
855 inode->i_state &= ~I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100856 wake_up_bit(&inode->i_state, __I_NEW);
857 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859EXPORT_SYMBOL(unlock_new_inode);
860
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400861/**
862 * iget5_locked - obtain an inode from a mounted file system
863 * @sb: super block of file system
864 * @hashval: hash value (usually inode number) to get
865 * @test: callback used for comparisons between inodes
866 * @set: callback used to initialize a new struct inode
867 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400869 * Search for the inode specified by @hashval and @data in the inode cache,
870 * and if present it is return it with an increased reference count. This is
871 * a generalized version of iget_locked() for file systems where the inode
872 * number is not sufficient for unique identification of an inode.
873 *
874 * If the inode is not in cache, allocate a new inode and return it locked,
875 * hashed, and with the I_NEW flag set. The file system gets to fill it in
876 * before unlocking it via unlock_new_inode().
877 *
878 * Note both @test and @set are called with the inode_hash_lock held, so can't
879 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400881struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
882 int (*test)(struct inode *, void *),
883 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400885 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530886 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400888 spin_lock(&inode_hash_lock);
889 inode = find_inode(sb, head, test, data);
890 spin_unlock(&inode_hash_lock);
891
892 if (inode) {
893 wait_on_inode(inode);
894 return inode;
895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 inode = alloc_inode(sb);
898 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530899 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Dave Chinner67a23c42011-03-22 22:23:42 +1100901 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* We released the lock, so.. */
903 old = find_inode(sb, head, test, data);
904 if (!old) {
905 if (set(inode, data))
906 goto set_failed;
907
Dave Chinner250df6e2011-03-22 22:23:36 +1100908 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100909 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100910 hlist_add_head(&inode->i_hash, head);
911 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100912 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100913 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Return the locked inode with I_NEW set, the
916 * caller is responsible for filling in the contents
917 */
918 return inode;
919 }
920
921 /*
922 * Uhhuh, somebody else created the same inode under
923 * us. Use the old inode instead of the one we just
924 * allocated.
925 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100926 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 destroy_inode(inode);
928 inode = old;
929 wait_on_inode(inode);
930 }
931 return inode;
932
933set_failed:
Dave Chinner67a23c42011-03-22 22:23:42 +1100934 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 destroy_inode(inode);
936 return NULL;
937}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400938EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400940/**
941 * iget_locked - obtain an inode from a mounted file system
942 * @sb: super block of file system
943 * @ino: inode number to get
944 *
945 * Search for the inode specified by @ino in the inode cache and if present
946 * return it with an increased reference count. This is for file systems
947 * where the inode number is sufficient for unique identification of an inode.
948 *
949 * If the inode is not in cache, allocate a new inode and return it locked,
950 * hashed, and with the I_NEW flag set. The file system gets to fill it in
951 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400953struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400955 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530956 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Christoph Hellwig0b2d0722011-03-23 15:03:28 -0400958 spin_lock(&inode_hash_lock);
959 inode = find_inode_fast(sb, head, ino);
960 spin_unlock(&inode_hash_lock);
961 if (inode) {
962 wait_on_inode(inode);
963 return inode;
964 }
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 inode = alloc_inode(sb);
967 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530968 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Dave Chinner67a23c42011-03-22 22:23:42 +1100970 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* We released the lock, so.. */
972 old = find_inode_fast(sb, head, ino);
973 if (!old) {
974 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +1100975 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100976 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +1100977 hlist_add_head(&inode->i_hash, head);
978 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100979 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +1100980 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /* Return the locked inode with I_NEW set, the
983 * caller is responsible for filling in the contents
984 */
985 return inode;
986 }
987
988 /*
989 * Uhhuh, somebody else created the same inode under
990 * us. Use the old inode instead of the one we just
991 * allocated.
992 */
Dave Chinner67a23c42011-03-22 22:23:42 +1100993 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 destroy_inode(inode);
995 inode = old;
996 wait_on_inode(inode);
997 }
998 return inode;
999}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001000EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001002/*
1003 * search the inode cache for a matching inode number.
1004 * If we find one, then the inode number we are trying to
1005 * allocate is not unique and so we should not use it.
1006 *
1007 * Returns 1 if the inode number is unique, 0 if it is not.
1008 */
1009static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1010{
1011 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1012 struct hlist_node *node;
1013 struct inode *inode;
1014
Dave Chinner67a23c42011-03-22 22:23:42 +11001015 spin_lock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001016 hlist_for_each_entry(inode, node, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001017 if (inode->i_ino == ino && inode->i_sb == sb) {
1018 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001019 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001020 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001021 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001022 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001023
1024 return 1;
1025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/**
1028 * iunique - get a unique inode number
1029 * @sb: superblock
1030 * @max_reserved: highest reserved inode number
1031 *
1032 * Obtain an inode number that is unique on the system for a given
1033 * superblock. This is used by file systems that have no natural
1034 * permanent inode numbering system. An inode number is returned that
1035 * is higher than the reserved limit but unique.
1036 *
1037 * BUGS:
1038 * With a large number of inodes live on the file system this function
1039 * currently becomes quite slow.
1040 */
1041ino_t iunique(struct super_block *sb, ino_t max_reserved)
1042{
Jeff Layton866b04f2007-05-08 00:32:29 -07001043 /*
1044 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1045 * error if st_ino won't fit in target struct field. Use 32bit counter
1046 * here to attempt to avoid that.
1047 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001048 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001049 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001052 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001053 do {
1054 if (counter <= max_reserved)
1055 counter = max_reserved + 1;
1056 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001057 } while (!test_inode_iunique(sb, res));
1058 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001059
1060 return res;
1061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062EXPORT_SYMBOL(iunique);
1063
1064struct inode *igrab(struct inode *inode)
1065{
Dave Chinner250df6e2011-03-22 22:23:36 +11001066 spin_lock(&inode->i_lock);
1067 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001069 spin_unlock(&inode->i_lock);
1070 } else {
1071 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /*
1073 * Handle the case where s_op->clear_inode is not been
1074 * called yet, and somebody is calling igrab
1075 * while the inode is getting freed.
1076 */
1077 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return inode;
1080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081EXPORT_SYMBOL(igrab);
1082
1083/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001084 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * @sb: super block of file system to search
1086 * @hashval: hash value (usually inode number) to search for
1087 * @test: callback used for comparisons between inodes
1088 * @data: opaque data pointer to pass to @test
1089 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001090 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001092 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001093 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001094 * Note: I_NEW is not waited upon so you have to be very careful what you do
1095 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001096 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001097 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001098 */
1099struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1100 int (*test)(struct inode *, void *), void *data)
1101{
1102 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001103 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001104
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001105 spin_lock(&inode_hash_lock);
1106 inode = find_inode(sb, head, test, data);
1107 spin_unlock(&inode_hash_lock);
1108
1109 return inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001110}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001111EXPORT_SYMBOL(ilookup5_nowait);
1112
1113/**
1114 * ilookup5 - search for an inode in the inode cache
1115 * @sb: super block of file system to search
1116 * @hashval: hash value (usually inode number) to search for
1117 * @test: callback used for comparisons between inodes
1118 * @data: opaque data pointer to pass to @test
1119 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001120 * Search for the inode specified by @hashval and @data in the inode cache,
1121 * and if the inode is in the cache, return the inode with an incremented
1122 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001123 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001125 * This is a generalized version of ilookup() for file systems where the
1126 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001128 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
1130struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1131 int (*test)(struct inode *, void *), void *data)
1132{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001133 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001135 if (inode)
1136 wait_on_inode(inode);
1137 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139EXPORT_SYMBOL(ilookup5);
1140
1141/**
1142 * ilookup - search for an inode in the inode cache
1143 * @sb: super block of file system to search
1144 * @ino: inode number to search for
1145 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001146 * Search for the inode @ino in the inode cache, and if the inode is in the
1147 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
1149struct inode *ilookup(struct super_block *sb, unsigned long ino)
1150{
1151 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001152 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001154 spin_lock(&inode_hash_lock);
1155 inode = find_inode_fast(sb, head, ino);
1156 spin_unlock(&inode_hash_lock);
1157
1158 if (inode)
1159 wait_on_inode(inode);
1160 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162EXPORT_SYMBOL(ilookup);
1163
Al Viro261bca82008-12-30 01:48:21 -05001164int insert_inode_locked(struct inode *inode)
1165{
1166 struct super_block *sb = inode->i_sb;
1167 ino_t ino = inode->i_ino;
1168 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001169
Al Viro261bca82008-12-30 01:48:21 -05001170 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001171 struct hlist_node *node;
1172 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001173 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001174 hlist_for_each_entry(old, node, head, i_hash) {
1175 if (old->i_ino != ino)
1176 continue;
1177 if (old->i_sb != sb)
1178 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001179 spin_lock(&old->i_lock);
1180 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1181 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001182 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001183 }
Al Viro72a43d62009-05-13 19:13:40 +01001184 break;
1185 }
1186 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001187 spin_lock(&inode->i_lock);
1188 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001189 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001190 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001191 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001192 return 0;
1193 }
1194 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001195 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001196 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001197 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001198 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001199 iput(old);
1200 return -EBUSY;
1201 }
1202 iput(old);
1203 }
1204}
Al Viro261bca82008-12-30 01:48:21 -05001205EXPORT_SYMBOL(insert_inode_locked);
1206
1207int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1208 int (*test)(struct inode *, void *), void *data)
1209{
1210 struct super_block *sb = inode->i_sb;
1211 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Al Viro261bca82008-12-30 01:48:21 -05001212
Al Viro261bca82008-12-30 01:48:21 -05001213 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001214 struct hlist_node *node;
1215 struct inode *old = NULL;
1216
Dave Chinner67a23c42011-03-22 22:23:42 +11001217 spin_lock(&inode_hash_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001218 hlist_for_each_entry(old, node, head, i_hash) {
1219 if (old->i_sb != sb)
1220 continue;
1221 if (!test(old, data))
1222 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001223 spin_lock(&old->i_lock);
1224 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1225 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001226 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001227 }
Al Viro72a43d62009-05-13 19:13:40 +01001228 break;
1229 }
1230 if (likely(!node)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001231 spin_lock(&inode->i_lock);
1232 inode->i_state |= I_NEW;
Al Viro261bca82008-12-30 01:48:21 -05001233 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001234 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001235 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001236 return 0;
1237 }
1238 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001239 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001240 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001241 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001242 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001243 iput(old);
1244 return -EBUSY;
1245 }
1246 iput(old);
1247 }
1248}
Al Viro261bca82008-12-30 01:48:21 -05001249EXPORT_SYMBOL(insert_inode_locked4);
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Al Viro45321ac2010-06-07 13:43:19 -04001252int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Al Viro45321ac2010-06-07 13:43:19 -04001254 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256EXPORT_SYMBOL(generic_delete_inode);
1257
Al Viro45321ac2010-06-07 13:43:19 -04001258/*
1259 * Normal UNIX filesystem behaviour: delete the
1260 * inode when the usage count drops to zero, and
1261 * i_nlink is zero.
Jan Kara22fe40422009-09-18 13:05:44 -07001262 */
Al Viro45321ac2010-06-07 13:43:19 -04001263int generic_drop_inode(struct inode *inode)
1264{
Al Viro1d3382cb2010-10-23 15:19:20 -04001265 return !inode->i_nlink || inode_unhashed(inode);
Al Viro45321ac2010-06-07 13:43:19 -04001266}
1267EXPORT_SYMBOL_GPL(generic_drop_inode);
1268
1269/*
1270 * Called when we're dropping the last reference
1271 * to an inode.
1272 *
1273 * Call the FS "drop_inode()" function, defaulting to
1274 * the legacy UNIX filesystem behaviour. If it tells
1275 * us to evict inode, do so. Otherwise, retain inode
1276 * in cache if fs is alive, sync and evict if fs is
1277 * shutting down.
1278 */
1279static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001282 const struct super_operations *op = inode->i_sb->s_op;
1283 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Dave Chinner250df6e2011-03-22 22:23:36 +11001285 WARN_ON(inode->i_state & I_NEW);
1286
Al Viroe7f59092011-07-07 15:45:59 -04001287 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001288 drop = op->drop_inode(inode);
1289 else
1290 drop = generic_drop_inode(inode);
1291
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001292 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1293 inode->i_state |= I_REFERENCED;
1294 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1295 inode_lru_list_add(inode);
1296 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001297 return;
1298 }
1299
Al Viro45321ac2010-06-07 13:43:19 -04001300 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001301 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001302 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001304 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001305 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001306 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001308
Alexander Viro991114c2005-06-23 00:09:01 -07001309 inode->i_state |= I_FREEING;
Nick Piggin9e38d862010-10-23 06:55:17 -04001310 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001311 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001312
1313 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301317 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * @inode: inode to put
1319 *
1320 * Puts an inode, dropping its usage count. If the inode use count hits
1321 * zero, the inode is then freed and may also be destroyed.
1322 *
1323 * Consequently, iput() can sleep.
1324 */
1325void iput(struct inode *inode)
1326{
1327 if (inode) {
Al Viroa4ffdde2010-06-02 17:38:30 -04001328 BUG_ON(inode->i_state & I_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Dave Chinnerf283c862011-03-22 22:23:39 +11001330 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 iput_final(inode);
1332 }
1333}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334EXPORT_SYMBOL(iput);
1335
1336/**
1337 * bmap - find a block number in a file
1338 * @inode: inode of file
1339 * @block: block to find
1340 *
1341 * Returns the block number on the device holding the inode that
1342 * is the disk block number for the block of the file requested.
1343 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301344 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * file.
1346 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301347sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 sector_t res = 0;
1350 if (inode->i_mapping->a_ops->bmap)
1351 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1352 return res;
1353}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354EXPORT_SYMBOL(bmap);
1355
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001356/*
1357 * With relative atime, only update atime if the previous atime is
1358 * earlier than either the ctime or mtime or if at least a day has
1359 * passed since the last atime update.
1360 */
1361static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1362 struct timespec now)
1363{
1364
1365 if (!(mnt->mnt_flags & MNT_RELATIME))
1366 return 1;
1367 /*
1368 * Is mtime younger than atime? If yes, update atime:
1369 */
1370 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1371 return 1;
1372 /*
1373 * Is ctime younger than atime? If yes, update atime:
1374 */
1375 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1376 return 1;
1377
1378 /*
1379 * Is the previous atime value older than a day? If yes,
1380 * update atime:
1381 */
1382 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1383 return 1;
1384 /*
1385 * Good, we can skip the atime update:
1386 */
1387 return 0;
1388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001391 * touch_atime - update the access time
1392 * @mnt: mount the inode is accessed on
Martin Waitz7045f372006-02-01 03:06:57 -08001393 * @dentry: dentry accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *
1395 * Update the accessed time on an inode and mark it for writeback.
1396 * This function automatically handles read only file systems and media,
1397 * as well as the "noatime" flag and inode specific "noatime" markers.
1398 */
Christoph Hellwig869243a2006-01-09 20:52:03 -08001399void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Christoph Hellwig869243a2006-01-09 20:52:03 -08001401 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct timespec now;
1403
Andrew Mortonb2276132006-12-13 00:34:33 -08001404 if (inode->i_flags & S_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001405 return;
Eric Dumazet37756ce2007-02-10 01:44:49 -08001406 if (IS_NOATIME(inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001407 return;
Andrew Mortonb2276132006-12-13 00:34:33 -08001408 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001409 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001410
Dave Hansencdb70f32008-02-15 14:37:41 -08001411 if (mnt->mnt_flags & MNT_NOATIME)
Andi Kleenb12536c2009-09-18 13:05:47 -07001412 return;
Dave Hansencdb70f32008-02-15 14:37:41 -08001413 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001414 return;
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -08001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 now = current_fs_time(inode->i_sb);
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001417
1418 if (!relatime_need_update(mnt, inode, now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001419 return;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001420
Valerie Henson47ae32d2006-12-13 00:34:34 -08001421 if (timespec_equal(&inode->i_atime, &now))
Andi Kleenb12536c2009-09-18 13:05:47 -07001422 return;
1423
1424 if (mnt_want_write(mnt))
1425 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001426
1427 inode->i_atime = now;
1428 mark_inode_dirty_sync(inode);
Dave Hansencdb70f32008-02-15 14:37:41 -08001429 mnt_drop_write(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001431EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001434 * file_update_time - update mtime and ctime time
1435 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001437 * Update the mtime and ctime members of an inode and mark the inode
1438 * for writeback. Note that this function is meant exclusively for
1439 * usage in the file write path of filesystems, and filesystems may
1440 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001441 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Christoph Hellwig870f4812006-01-09 20:52:01 -08001442 * timestamps are handled by the server.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444
Christoph Hellwig870f4812006-01-09 20:52:01 -08001445void file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001447 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct timespec now;
Andi Kleence06e0b2009-09-18 13:05:48 -07001449 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Andi Kleence06e0b2009-09-18 13:05:48 -07001451 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (IS_NOCMTIME(inode))
1453 return;
Dave Hansen20ddee22008-02-15 14:37:43 -08001454
Andi Kleence06e0b2009-09-18 13:05:48 -07001455 now = current_fs_time(inode->i_sb);
1456 if (!timespec_equal(&inode->i_mtime, &now))
1457 sync_it = S_MTIME;
1458
1459 if (!timespec_equal(&inode->i_ctime, &now))
1460 sync_it |= S_CTIME;
1461
1462 if (IS_I_VERSION(inode))
1463 sync_it |= S_VERSION;
1464
1465 if (!sync_it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return;
1467
Andi Kleence06e0b2009-09-18 13:05:48 -07001468 /* Finally allowed to write? Takes lock. */
1469 if (mnt_want_write_file(file))
1470 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Andi Kleence06e0b2009-09-18 13:05:48 -07001472 /* Only change inode inside the lock region */
1473 if (sync_it & S_VERSION)
Jean Noel Cordenner7a224222008-01-28 23:58:27 -05001474 inode_inc_iversion(inode);
Andi Kleence06e0b2009-09-18 13:05:48 -07001475 if (sync_it & S_CTIME)
1476 inode->i_ctime = now;
1477 if (sync_it & S_MTIME)
1478 inode->i_mtime = now;
1479 mark_inode_dirty_sync(inode);
Dave Hansen20ddee22008-02-15 14:37:43 -08001480 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001482EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484int inode_needs_sync(struct inode *inode)
1485{
1486 if (IS_SYNC(inode))
1487 return 1;
1488 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1489 return 1;
1490 return 0;
1491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492EXPORT_SYMBOL(inode_needs_sync);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494int inode_wait(void *word)
1495{
1496 schedule();
1497 return 0;
1498}
Stephen Rothwelld44dab82008-11-10 17:06:05 +11001499EXPORT_SYMBOL(inode_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001502 * If we try to find an inode in the inode hash while it is being
1503 * deleted, we have to wait until the filesystem completes its
1504 * deletion before reporting that it isn't found. This function waits
1505 * until the deletion _might_ have completed. Callers are responsible
1506 * to recheck inode state.
1507 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001508 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001509 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1510 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512static void __wait_on_freeing_inode(struct inode *inode)
1513{
1514 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001515 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1516 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001518 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001519 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 schedule();
1521 finish_wait(wq, &wait.wait);
Dave Chinner67a23c42011-03-22 22:23:42 +11001522 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525static __initdata unsigned long ihash_entries;
1526static int __init set_ihash_entries(char *str)
1527{
1528 if (!str)
1529 return 0;
1530 ihash_entries = simple_strtoul(str, &str, 0);
1531 return 1;
1532}
1533__setup("ihash_entries=", set_ihash_entries);
1534
1535/*
1536 * Initialize the waitqueues and inode hash table.
1537 */
1538void __init inode_init_early(void)
1539{
1540 int loop;
1541
1542 /* If hashes are distributed across NUMA nodes, defer
1543 * hash allocation until vmalloc space is available.
1544 */
1545 if (hashdist)
1546 return;
1547
1548 inode_hashtable =
1549 alloc_large_system_hash("Inode-cache",
1550 sizeof(struct hlist_head),
1551 ihash_entries,
1552 14,
1553 HASH_EARLY,
1554 &i_hash_shift,
1555 &i_hash_mask,
1556 0);
1557
1558 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1559 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1560}
1561
Denis Cheng74bf17c2007-10-16 23:26:30 -07001562void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 int loop;
1565
1566 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001567 inode_cachep = kmem_cache_create("inode_cache",
1568 sizeof(struct inode),
1569 0,
1570 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1571 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001572 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 /* Hash may have been set up in inode_init_early */
1575 if (!hashdist)
1576 return;
1577
1578 inode_hashtable =
1579 alloc_large_system_hash("Inode-cache",
1580 sizeof(struct hlist_head),
1581 ihash_entries,
1582 14,
1583 0,
1584 &i_hash_shift,
1585 &i_hash_mask,
1586 0);
1587
1588 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1589 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1590}
1591
1592void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1593{
1594 inode->i_mode = mode;
1595 if (S_ISCHR(mode)) {
1596 inode->i_fop = &def_chr_fops;
1597 inode->i_rdev = rdev;
1598 } else if (S_ISBLK(mode)) {
1599 inode->i_fop = &def_blk_fops;
1600 inode->i_rdev = rdev;
1601 } else if (S_ISFIFO(mode))
1602 inode->i_fop = &def_fifo_fops;
1603 else if (S_ISSOCK(mode))
1604 inode->i_fop = &bad_sock_fops;
1605 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07001606 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1607 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1608 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001611
1612/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00001613 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03001614 * @inode: New inode
1615 * @dir: Directory inode
1616 * @mode: mode of the new inode
1617 */
1618void inode_init_owner(struct inode *inode, const struct inode *dir,
1619 mode_t mode)
1620{
1621 inode->i_uid = current_fsuid();
1622 if (dir && dir->i_mode & S_ISGID) {
1623 inode->i_gid = dir->i_gid;
1624 if (S_ISDIR(mode))
1625 mode |= S_ISGID;
1626 } else
1627 inode->i_gid = current_fsgid();
1628 inode->i_mode = mode;
1629}
1630EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07001631
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001632/**
1633 * inode_owner_or_capable - check current task permissions to inode
1634 * @inode: inode being checked
1635 *
1636 * Return true if current either has CAP_FOWNER to the inode, or
1637 * owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07001638 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001639bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07001640{
1641 struct user_namespace *ns = inode_userns(inode);
1642
1643 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1644 return true;
1645 if (ns_capable(ns, CAP_FOWNER))
1646 return true;
1647 return false;
1648}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001649EXPORT_SYMBOL(inode_owner_or_capable);