blob: 9e46350dd2a679d279db11d5844cb69c3ad9dcf2 [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 */
Al Viroe59cc472011-12-07 13:17:19 -05005#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/fs.h>
7#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/hash.h>
10#include <linux/swap.h>
11#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/cdev.h>
13#include <linux/bootmem.h>
Daniel Rosenbergf4951342020-01-20 14:31:56 -080014#include <linux/fscrypt.h>
Eric Paris3be25f42009-05-21 17:01:26 -040015#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080016#include <linux/mount.h>
Al Virof19d4a82009-06-08 19:50:45 -040017#include <linux/posix_acl.h>
Heiko Carstens9ce6e0b2011-05-22 18:54:21 +020018#include <linux/prefetch.h>
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040019#include <linux/buffer_head.h> /* for inode_has_buffers */
Miklos Szeredi7ada4db2011-11-21 12:11:32 +010020#include <linux/ratelimit.h>
Dave Chinnerbc3b14c2013-08-28 10:17:58 +100021#include <linux/list_lru.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050022#include <linux/iversion.h>
Theodore Ts'o0ae45f62015-02-02 00:37:00 -050023#include <trace/events/writeback.h>
Dave Chinnera66979a2011-03-22 22:23:41 +110024#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
Christoph Hellwig4b4563d2011-05-27 09:28:01 -040027 * Inode locking rules:
Dave Chinner250df6e2011-03-22 22:23:36 +110028 *
29 * inode->i_lock protects:
30 * inode->i_state, inode->i_hash, __iget()
Dave Chinnerbc3b14c2013-08-28 10:17:58 +100031 * Inode LRU list locks protect:
Dave Chinner98b745c2011-07-08 14:14:39 +100032 * inode->i_sb->s_inode_lru, inode->i_lru
Dave Chinner74278da2015-03-04 12:37:22 -050033 * inode->i_sb->s_inode_list_lock protects:
34 * inode->i_sb->s_inodes, inode->i_sb_list
Christoph Hellwigf758eea2011-04-21 18:19:44 -060035 * bdi->wb.list_lock protects:
Dave Chinnerc7f54082015-03-04 14:07:22 -050036 * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list
Dave Chinner67a23c42011-03-22 22:23:42 +110037 * inode_hash_lock protects:
38 * inode_hashtable, inode->i_hash
Dave Chinner250df6e2011-03-22 22:23:36 +110039 *
40 * Lock ordering:
Dave Chinner55fa6092011-03-22 22:23:40 +110041 *
Dave Chinner74278da2015-03-04 12:37:22 -050042 * inode->i_sb->s_inode_list_lock
Dave Chinner55fa6092011-03-22 22:23:40 +110043 * inode->i_lock
Dave Chinnerbc3b14c2013-08-28 10:17:58 +100044 * Inode LRU list locks
Dave Chinnera66979a2011-03-22 22:23:41 +110045 *
Christoph Hellwigf758eea2011-04-21 18:19:44 -060046 * bdi->wb.list_lock
Dave Chinnera66979a2011-03-22 22:23:41 +110047 * inode->i_lock
Dave Chinner67a23c42011-03-22 22:23:42 +110048 *
49 * inode_hash_lock
Dave Chinner74278da2015-03-04 12:37:22 -050050 * inode->i_sb->s_inode_list_lock
Dave Chinner67a23c42011-03-22 22:23:42 +110051 * inode->i_lock
52 *
53 * iunique_lock
54 * inode_hash_lock
Dave Chinner250df6e2011-03-22 22:23:36 +110055 */
56
Eric Dumazetfa3536c2006-03-26 01:37:24 -080057static unsigned int i_hash_mask __read_mostly;
58static unsigned int i_hash_shift __read_mostly;
Dave Chinner67a23c42011-03-22 22:23:42 +110059static struct hlist_head *inode_hashtable __read_mostly;
60static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Jens Axboe7dcda1c2011-04-05 23:51:48 +020063 * Empty aops. Can be used for the cases where the user does not
64 * define any of the address_space operations.
65 */
66const struct address_space_operations empty_aops = {
67};
68EXPORT_SYMBOL(empty_aops);
69
70/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * Statistics gathering..
72 */
73struct inodes_stat_t inodes_stat;
74
Glauber Costa3942c072013-08-28 10:17:53 +100075static DEFINE_PER_CPU(unsigned long, nr_inodes);
76static DEFINE_PER_CPU(unsigned long, nr_unused);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040077
Manish Katiyar6b3304b2009-03-31 19:35:54 +053078static struct kmem_cache *inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Glauber Costa3942c072013-08-28 10:17:53 +100080static long get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040081{
Nick Piggin3e880fb2011-01-07 17:49:19 +110082 int i;
Glauber Costa3942c072013-08-28 10:17:53 +100083 long sum = 0;
Nick Piggin3e880fb2011-01-07 17:49:19 +110084 for_each_possible_cpu(i)
85 sum += per_cpu(nr_inodes, i);
86 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -040087}
88
Glauber Costa3942c072013-08-28 10:17:53 +100089static inline long get_nr_inodes_unused(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040090{
Dave Chinnerfcb94f72011-07-08 14:14:38 +100091 int i;
Glauber Costa3942c072013-08-28 10:17:53 +100092 long sum = 0;
Dave Chinnerfcb94f72011-07-08 14:14:38 +100093 for_each_possible_cpu(i)
94 sum += per_cpu(nr_unused, i);
95 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -040096}
97
Glauber Costa3942c072013-08-28 10:17:53 +100098long get_nr_dirty_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040099{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100100 /* not actually dirty inodes, but a wild approximation */
Glauber Costa3942c072013-08-28 10:17:53 +1000101 long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400102 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400103}
104
105/*
106 * Handle nr_inode sysctl
107 */
108#ifdef CONFIG_SYSCTL
Joe Perches1f7e0612014-06-06 14:38:05 -0700109int proc_nr_inodes(struct ctl_table *table, int write,
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400110 void __user *buffer, size_t *lenp, loff_t *ppos)
111{
112 inodes_stat.nr_inodes = get_nr_inodes();
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000113 inodes_stat.nr_unused = get_nr_inodes_unused();
Glauber Costa3942c072013-08-28 10:17:53 +1000114 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400115}
116#endif
117
Al Virobd9b51e2014-11-18 23:38:21 -0500118static int no_open(struct inode *inode, struct file *file)
119{
120 return -ENXIO;
121}
122
David Chinner2cb15992008-10-30 17:32:23 +1100123/**
Masahiro Yamada6e7c2b42017-05-08 15:57:53 -0700124 * inode_init_always - perform inode structure initialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800125 * @sb: superblock inode belongs to
126 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100127 *
128 * These are initializations that need to be done on every inode
129 * allocation as the fields are not initialised by slab allocation.
130 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300131int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700133 static const struct inode_operations empty_iops;
Al Virobd9b51e2014-11-18 23:38:21 -0500134 static const struct file_operations no_open_fops = {.open = no_open};
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530135 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100136
137 inode->i_sb = sb;
138 inode->i_blkbits = sb->s_blocksize_bits;
139 inode->i_flags = 0;
Peter Zijlstrae6d506c2020-03-04 11:28:31 +0100140 atomic64_set(&inode->i_sequence, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100141 atomic_set(&inode->i_count, 1);
142 inode->i_op = &empty_iops;
Al Virobd9b51e2014-11-18 23:38:21 -0500143 inode->i_fop = &no_open_fops;
Miklos Szeredia78ef702011-10-28 14:13:30 +0200144 inode->__i_nlink = 1;
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700145 inode->i_opflags = 0;
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200146 if (sb->s_xattr)
147 inode->i_opflags |= IOP_XATTR;
Eric W. Biederman92361632012-02-08 07:07:50 -0800148 i_uid_write(inode, 0);
149 i_gid_write(inode, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100150 atomic_set(&inode->i_writecount, 0);
151 inode->i_size = 0;
Jens Axboec75b1d92017-06-27 11:47:04 -0600152 inode->i_write_hint = WRITE_LIFE_NOT_SET;
David Chinner2cb15992008-10-30 17:32:23 +1100153 inode->i_blocks = 0;
154 inode->i_bytes = 0;
155 inode->i_generation = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100156 inode->i_pipe = NULL;
157 inode->i_bdev = NULL;
158 inode->i_cdev = NULL;
Al Viro61ba64f2015-05-02 09:54:06 -0400159 inode->i_link = NULL;
Al Viro84e710d2016-04-15 00:58:55 -0400160 inode->i_dir_seq = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100161 inode->i_rdev = 0;
162 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500163
Tahsin Erdogan3d65ae42016-02-16 13:34:39 -0800164#ifdef CONFIG_CGROUP_WRITEBACK
165 inode->i_wb_frn_winner = 0;
166 inode->i_wb_frn_avg_time = 0;
167 inode->i_wb_frn_history = 0;
168#endif
169
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500170 if (security_inode_alloc(inode))
Christoph Hellwig54e34622009-08-07 14:38:25 -0300171 goto out;
David Chinner2cb15992008-10-30 17:32:23 +1100172 spin_lock_init(&inode->i_lock);
173 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
174
Al Viro9902af72016-04-15 15:08:36 -0400175 init_rwsem(&inode->i_rwsem);
176 lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key);
David Chinner2cb15992008-10-30 17:32:23 +1100177
Christoph Hellwigbd5fe6c2011-06-24 14:29:43 -0400178 atomic_set(&inode->i_dio_count, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100179
180 mapping->a_ops = &empty_aops;
181 mapping->host = inode;
182 mapping->flags = 0;
Darrick J. Wong829bc7872018-05-30 19:43:53 -0700183 mapping->wb_err = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700184 atomic_set(&mapping->i_mmap_writable, 0);
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800185 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800186 mapping->private_data = NULL;
David Chinner2cb15992008-10-30 17:32:23 +1100187 mapping->writeback_index = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100188 inode->i_private = NULL;
189 inode->i_mapping = mapping;
Al Virob3d9b7a2012-06-09 13:51:19 -0400190 INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */
Al Virof19d4a82009-06-08 19:50:45 -0400191#ifdef CONFIG_FS_POSIX_ACL
192 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
193#endif
David Chinner2cb15992008-10-30 17:32:23 +1100194
Eric Paris3be25f42009-05-21 17:01:26 -0400195#ifdef CONFIG_FSNOTIFY
196 inode->i_fsnotify_mask = 0;
197#endif
Jeff Layton4a075e32015-01-16 15:05:54 -0500198 inode->i_flctx = NULL;
Nick Piggin3e880fb2011-01-07 17:49:19 +1100199 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400200
Christoph Hellwig54e34622009-08-07 14:38:25 -0300201 return 0;
Christoph Hellwig54e34622009-08-07 14:38:25 -0300202out:
203 return -ENOMEM;
David Chinner2cb15992008-10-30 17:32:23 +1100204}
205EXPORT_SYMBOL(inode_init_always);
206
207static struct inode *alloc_inode(struct super_block *sb)
208{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct inode *inode;
210
211 if (sb->s_op->alloc_inode)
212 inode = sb->s_op->alloc_inode(sb);
213 else
David Chinner2cb15992008-10-30 17:32:23 +1100214 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Christoph Hellwig54e34622009-08-07 14:38:25 -0300216 if (!inode)
217 return NULL;
218
219 if (unlikely(inode_init_always(sb, inode))) {
220 if (inode->i_sb->s_op->destroy_inode)
221 inode->i_sb->s_op->destroy_inode(inode);
222 else
223 kmem_cache_free(inode_cachep, inode);
224 return NULL;
225 }
226
227 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Nick Pigginff0c7d12011-01-07 17:49:50 +1100230void free_inode_nonrcu(struct inode *inode)
231{
232 kmem_cache_free(inode_cachep, inode);
233}
234EXPORT_SYMBOL(free_inode_nonrcu);
235
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300236void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200238 BUG_ON(inode_has_buffers(inode));
Tejun Heo52ebea72015-05-22 17:13:37 -0400239 inode_detach_wb(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400241 fsnotify_inode_delete(inode);
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500242 locks_free_lock_context(inode);
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100243 if (!inode->i_nlink) {
244 WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
245 atomic_long_dec(&inode->i_sb->s_remove_count);
246 }
247
Al Virof19d4a82009-06-08 19:50:45 -0400248#ifdef CONFIG_FS_POSIX_ACL
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100249 if (inode->i_acl && !is_uncached_acl(inode->i_acl))
Al Virof19d4a82009-06-08 19:50:45 -0400250 posix_acl_release(inode->i_acl);
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100251 if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl))
Al Virof19d4a82009-06-08 19:50:45 -0400252 posix_acl_release(inode->i_default_acl);
253#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100254 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300255}
256EXPORT_SYMBOL(__destroy_inode);
257
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100258static void i_callback(struct rcu_head *head)
259{
260 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100261 kmem_cache_free(inode_cachep, inode);
262}
263
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200264static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300265{
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100266 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300267 __destroy_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (inode->i_sb->s_op->destroy_inode)
269 inode->i_sb->s_op->destroy_inode(inode);
270 else
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100271 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100274/**
275 * drop_nlink - directly drop an inode's link count
276 * @inode: inode
277 *
278 * This is a low-level filesystem helper to replace any
279 * direct filesystem manipulation of i_nlink. In cases
280 * where we are attempting to track writes to the
281 * filesystem, a decrement to zero means an imminent
282 * write when the file is truncated and actually unlinked
283 * on the filesystem.
284 */
285void drop_nlink(struct inode *inode)
286{
287 WARN_ON(inode->i_nlink == 0);
288 inode->__i_nlink--;
289 if (!inode->i_nlink)
290 atomic_long_inc(&inode->i_sb->s_remove_count);
291}
292EXPORT_SYMBOL(drop_nlink);
293
294/**
295 * clear_nlink - directly zero an inode's link count
296 * @inode: inode
297 *
298 * This is a low-level filesystem helper to replace any
299 * direct filesystem manipulation of i_nlink. See
300 * drop_nlink() for why we care about i_nlink hitting zero.
301 */
302void clear_nlink(struct inode *inode)
303{
304 if (inode->i_nlink) {
305 inode->__i_nlink = 0;
306 atomic_long_inc(&inode->i_sb->s_remove_count);
307 }
308}
309EXPORT_SYMBOL(clear_nlink);
310
311/**
312 * set_nlink - directly set an inode's link count
313 * @inode: inode
314 * @nlink: new nlink (should be non-zero)
315 *
316 * This is a low-level filesystem helper to replace any
317 * direct filesystem manipulation of i_nlink.
318 */
319void set_nlink(struct inode *inode, unsigned int nlink)
320{
321 if (!nlink) {
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100322 clear_nlink(inode);
323 } else {
324 /* Yes, some filesystems do change nlink from zero to one */
325 if (inode->i_nlink == 0)
326 atomic_long_dec(&inode->i_sb->s_remove_count);
327
328 inode->__i_nlink = nlink;
329 }
330}
331EXPORT_SYMBOL(set_nlink);
332
333/**
334 * inc_nlink - directly increment an inode's link count
335 * @inode: inode
336 *
337 * This is a low-level filesystem helper to replace any
338 * direct filesystem manipulation of i_nlink. Currently,
339 * it is only here for parity with dec_nlink().
340 */
341void inc_nlink(struct inode *inode)
342{
Al Virof4e0c302013-06-11 08:34:36 +0400343 if (unlikely(inode->i_nlink == 0)) {
344 WARN_ON(!(inode->i_state & I_LINKABLE));
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100345 atomic_long_dec(&inode->i_sb->s_remove_count);
Al Virof4e0c302013-06-11 08:34:36 +0400346 }
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100347
348 inode->__i_nlink++;
349}
350EXPORT_SYMBOL(inc_nlink);
351
Dave Chinnerae233952018-03-06 17:30:34 -0800352static void __address_space_init_once(struct address_space *mapping)
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100353{
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700354 INIT_RADIX_TREE(&mapping->i_pages, GFP_ATOMIC | __GFP_ACCOUNT);
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800355 init_rwsem(&mapping->i_mmap_rwsem);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100356 INIT_LIST_HEAD(&mapping->private_list);
357 spin_lock_init(&mapping->private_lock);
Davidlohr Buesof808c132017-09-08 16:15:08 -0700358 mapping->i_mmap = RB_ROOT_CACHED;
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100359}
Dave Chinnerae233952018-03-06 17:30:34 -0800360
361void address_space_init_once(struct address_space *mapping)
362{
363 memset(mapping, 0, sizeof(*mapping));
364 __address_space_init_once(mapping);
365}
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100366EXPORT_SYMBOL(address_space_init_once);
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * These are initializations that only need to be done
370 * once, because the fields are idempotent across use
371 * of the inode, so let the slab aware of that.
372 */
373void inode_init_once(struct inode *inode)
374{
375 memset(inode, 0, sizeof(*inode));
376 INIT_HLIST_NODE(&inode->i_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 INIT_LIST_HEAD(&inode->i_devices);
Dave Chinnerc7f54082015-03-04 14:07:22 -0500378 INIT_LIST_HEAD(&inode->i_io_list);
Dave Chinner6c60d2b2016-07-26 15:21:50 -0700379 INIT_LIST_HEAD(&inode->i_wb_list);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100380 INIT_LIST_HEAD(&inode->i_lru);
Dave Chinnerae233952018-03-06 17:30:34 -0800381 __address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 i_size_ordered_init(inode);
383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384EXPORT_SYMBOL(inode_init_once);
385
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700386static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530388 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Christoph Lametera35afb82007-05-16 22:10:57 -0700390 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100394 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530396void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Nick Piggin9e38d862010-10-23 06:55:17 -0400398 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Al Viro7de9c6ee2010-10-23 11:11:40 -0400401/*
402 * get additional reference to inode; caller must already hold one.
403 */
404void ihold(struct inode *inode)
405{
406 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
407}
408EXPORT_SYMBOL(ihold);
409
Nick Piggin9e38d862010-10-23 06:55:17 -0400410static void inode_lru_list_add(struct inode *inode)
411{
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000412 if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru))
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000413 this_cpu_inc(nr_unused);
Josef Bacik563f4002017-04-18 16:04:17 -0400414 else
415 inode->i_state |= I_REFERENCED;
Nick Piggin9e38d862010-10-23 06:55:17 -0400416}
417
Jan Kara4eff96d2012-11-26 16:29:51 -0800418/*
419 * Add inode to LRU if needed (inode is unused and clean).
420 *
421 * Needs inode->i_lock held.
422 */
423void inode_add_lru(struct inode *inode)
424{
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500425 if (!(inode->i_state & (I_DIRTY_ALL | I_SYNC |
426 I_FREEING | I_WILL_FREE)) &&
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800427 !atomic_read(&inode->i_count) && inode->i_sb->s_flags & SB_ACTIVE)
Jan Kara4eff96d2012-11-26 16:29:51 -0800428 inode_lru_list_add(inode);
429}
430
431
Nick Piggin9e38d862010-10-23 06:55:17 -0400432static void inode_lru_list_del(struct inode *inode)
433{
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000434
435 if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru))
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000436 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Christoph Hellwig646ec462010-10-23 07:15:32 -0400439/**
440 * inode_sb_list_add - add inode to the superblock list of inodes
441 * @inode: inode to add
442 */
443void inode_sb_list_add(struct inode *inode)
444{
Dave Chinner74278da2015-03-04 12:37:22 -0500445 spin_lock(&inode->i_sb->s_inode_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100446 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
Dave Chinner74278da2015-03-04 12:37:22 -0500447 spin_unlock(&inode->i_sb->s_inode_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400448}
449EXPORT_SYMBOL_GPL(inode_sb_list_add);
450
Dave Chinner55fa6092011-03-22 22:23:40 +1100451static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400452{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200453 if (!list_empty(&inode->i_sb_list)) {
Dave Chinner74278da2015-03-04 12:37:22 -0500454 spin_lock(&inode->i_sb->s_inode_list_lock);
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200455 list_del_init(&inode->i_sb_list);
Dave Chinner74278da2015-03-04 12:37:22 -0500456 spin_unlock(&inode->i_sb->s_inode_list_lock);
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200457 }
Christoph Hellwig646ec462010-10-23 07:15:32 -0400458}
459
Dave Chinner4c51acb2010-10-23 06:58:09 -0400460static unsigned long hash(struct super_block *sb, unsigned long hashval)
461{
462 unsigned long tmp;
463
464 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
465 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400466 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
467 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400468}
469
470/**
471 * __insert_inode_hash - hash an inode
472 * @inode: unhashed inode
473 * @hashval: unsigned long value used to locate this object in the
474 * inode_hashtable.
475 *
476 * Add an inode to the inode hash for this superblock.
477 */
478void __insert_inode_hash(struct inode *inode, unsigned long hashval)
479{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400480 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
481
Dave Chinner67a23c42011-03-22 22:23:42 +1100482 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100483 spin_lock(&inode->i_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400484 hlist_add_head(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100485 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100486 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400487}
488EXPORT_SYMBOL(__insert_inode_hash);
489
490/**
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200491 * __remove_inode_hash - remove an inode from the hash
Dave Chinner4c51acb2010-10-23 06:58:09 -0400492 * @inode: inode to unhash
493 *
494 * Remove an inode from the superblock.
495 */
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200496void __remove_inode_hash(struct inode *inode)
Dave Chinner4c51acb2010-10-23 06:58:09 -0400497{
Dave Chinner67a23c42011-03-22 22:23:42 +1100498 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100499 spin_lock(&inode->i_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400500 hlist_del_init(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100501 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100502 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400503}
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200504EXPORT_SYMBOL(__remove_inode_hash);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400505
Jan Karadbd57682012-05-03 14:48:02 +0200506void clear_inode(struct inode *inode)
Al Virob0683aa2010-06-04 20:55:25 -0400507{
Jan Kara08142572011-06-27 16:18:10 -0700508 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700509 * We have to cycle the i_pages lock here because reclaim can be in the
Jan Kara08142572011-06-27 16:18:10 -0700510 * process of removing the last page (in __delete_from_page_cache())
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700511 * and we must not free the mapping under it.
Jan Kara08142572011-06-27 16:18:10 -0700512 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700513 xa_lock_irq(&inode->i_data.i_pages);
Al Virob0683aa2010-06-04 20:55:25 -0400514 BUG_ON(inode->i_data.nrpages);
Ross Zwislerf9fe48b2016-01-22 15:10:40 -0800515 BUG_ON(inode->i_data.nrexceptional);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700516 xa_unlock_irq(&inode->i_data.i_pages);
Al Virob0683aa2010-06-04 20:55:25 -0400517 BUG_ON(!list_empty(&inode->i_data.private_list));
518 BUG_ON(!(inode->i_state & I_FREEING));
519 BUG_ON(inode->i_state & I_CLEAR);
Dave Chinner6c60d2b2016-07-26 15:21:50 -0700520 BUG_ON(!list_empty(&inode->i_wb_list));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100521 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400522 inode->i_state = I_FREEING | I_CLEAR;
523}
Jan Karadbd57682012-05-03 14:48:02 +0200524EXPORT_SYMBOL(clear_inode);
Al Virob0683aa2010-06-04 20:55:25 -0400525
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100526/*
527 * Free the inode passed in, removing it from the lists it is still connected
528 * to. We remove any pages still attached to the inode and wait for any IO that
529 * is still in progress before finally destroying the inode.
530 *
531 * An inode must already be marked I_FREEING so that we avoid the inode being
532 * moved back onto lists if we race with other code that manipulates the lists
533 * (e.g. writeback_single_inode). The caller is responsible for setting this.
534 *
535 * An inode must already be removed from the LRU list before being evicted from
536 * the cache. This should occur atomically with setting the I_FREEING state
537 * flag, so no inodes here should ever be on the LRU when being evicted.
538 */
Al Viro644da592010-06-07 13:21:05 -0400539static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400540{
541 const struct super_operations *op = inode->i_sb->s_op;
542
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100543 BUG_ON(!(inode->i_state & I_FREEING));
544 BUG_ON(!list_empty(&inode->i_lru));
545
Dave Chinnerc7f54082015-03-04 14:07:22 -0500546 if (!list_empty(&inode->i_io_list))
547 inode_io_list_del(inode);
Eric Dumazetb12362b2011-07-28 06:11:47 +0200548
Dave Chinner55fa6092011-03-22 22:23:40 +1100549 inode_sb_list_del(inode);
550
Jan Kara169ebd92012-05-03 14:48:03 +0200551 /*
552 * Wait for flusher thread to be done with the inode so that filesystem
553 * does not start destroying it while writeback is still running. Since
554 * the inode has I_FREEING set, flusher thread won't start new work on
555 * the inode. We just have to wait for running writeback to finish.
556 */
557 inode_wait_for_writeback(inode);
Jan Kara7994e6f2012-05-03 14:48:01 +0200558
Al Virobe7ce412010-06-04 19:40:39 -0400559 if (op->evict_inode) {
560 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400561 } else {
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700562 truncate_inode_pages_final(&inode->i_data);
Jan Karadbd57682012-05-03 14:48:02 +0200563 clear_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400564 }
Al Viro661074e2010-06-04 20:19:55 -0400565 if (S_ISBLK(inode->i_mode) && inode->i_bdev)
566 bd_forget(inode);
567 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
568 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100569
570 remove_inode_hash(inode);
571
572 spin_lock(&inode->i_lock);
573 wake_up_bit(&inode->i_state, __I_NEW);
574 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
575 spin_unlock(&inode->i_lock);
576
577 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*
581 * dispose_list - dispose of the contents of a local list
582 * @head: the head of the list to free
583 *
584 * Dispose-list gets a local list with local inodes in it, so it doesn't
585 * need to worry about list corruption and SMP locks.
586 */
587static void dispose_list(struct list_head *head)
588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 while (!list_empty(head)) {
590 struct inode *inode;
591
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100592 inode = list_first_entry(head, struct inode, i_lru);
593 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Al Viro644da592010-06-07 13:21:05 -0400595 evict(inode);
Josef Bacikac05fbb2015-03-04 16:52:52 -0500596 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
Al Viro63997e92010-10-25 20:49:35 -0400601 * evict_inodes - evict all evictable inodes for a superblock
602 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 *
Al Viro63997e92010-10-25 20:49:35 -0400604 * Make sure that no inodes with zero refcount are retained. This is
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800605 * called by superblock shutdown after having SB_ACTIVE flag removed,
Al Viro63997e92010-10-25 20:49:35 -0400606 * so any inode reaching zero refcount during or after that call will
607 * be immediately evicted.
608 */
609void evict_inodes(struct super_block *sb)
610{
611 struct inode *inode, *next;
612 LIST_HEAD(dispose);
613
Josef Bacikac05fbb2015-03-04 16:52:52 -0500614again:
Dave Chinner74278da2015-03-04 12:37:22 -0500615 spin_lock(&sb->s_inode_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400616 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
617 if (atomic_read(&inode->i_count))
618 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100619
620 spin_lock(&inode->i_lock);
621 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
622 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400623 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100624 }
Al Viro63997e92010-10-25 20:49:35 -0400625
626 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100627 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100628 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100629 list_add(&inode->i_lru, &dispose);
Josef Bacikac05fbb2015-03-04 16:52:52 -0500630
631 /*
632 * We can have a ton of inodes to evict at unmount time given
633 * enough memory, check to see if we need to go to sleep for a
634 * bit so we don't livelock.
635 */
636 if (need_resched()) {
637 spin_unlock(&sb->s_inode_list_lock);
638 cond_resched();
639 dispose_list(&dispose);
640 goto again;
641 }
Al Viro63997e92010-10-25 20:49:35 -0400642 }
Dave Chinner74278da2015-03-04 12:37:22 -0500643 spin_unlock(&sb->s_inode_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400644
645 dispose_list(&dispose);
Al Viro63997e92010-10-25 20:49:35 -0400646}
Darrick J. Wong799ea9e2017-08-18 18:08:25 -0700647EXPORT_SYMBOL_GPL(evict_inodes);
Al Viro63997e92010-10-25 20:49:35 -0400648
649/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200650 * invalidate_inodes - attempt to free all inodes on a superblock
651 * @sb: superblock to operate on
NeilBrown93b270f2011-02-24 17:25:47 +1100652 * @kill_dirty: flag to guide handling of dirty inodes
Christoph Hellwiga0318782010-10-24 19:40:33 +0200653 *
654 * Attempts to free all inodes for a given superblock. If there were any
655 * busy inodes return a non-zero value, else zero.
NeilBrown93b270f2011-02-24 17:25:47 +1100656 * If @kill_dirty is set, discard dirty inodes too, otherwise treat
657 * them as busy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
NeilBrown93b270f2011-02-24 17:25:47 +1100659int invalidate_inodes(struct super_block *sb, bool kill_dirty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400661 int busy = 0;
Christoph Hellwiga0318782010-10-24 19:40:33 +0200662 struct inode *inode, *next;
663 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Eric Sandeen77de8ee2019-12-06 10:54:23 -0600665again:
Dave Chinner74278da2015-03-04 12:37:22 -0500666 spin_lock(&sb->s_inode_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200667 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100668 spin_lock(&inode->i_lock);
669 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
670 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100672 }
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500673 if (inode->i_state & I_DIRTY_ALL && !kill_dirty) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100674 spin_unlock(&inode->i_lock);
NeilBrown93b270f2011-02-24 17:25:47 +1100675 busy = 1;
676 continue;
677 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200678 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100679 spin_unlock(&inode->i_lock);
Christoph Hellwig99a38912010-10-23 19:07:20 +0200680 busy = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 continue;
682 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200683
Christoph Hellwig99a38912010-10-23 19:07:20 +0200684 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100685 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100686 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100687 list_add(&inode->i_lru, &dispose);
Eric Sandeen77de8ee2019-12-06 10:54:23 -0600688 if (need_resched()) {
689 spin_unlock(&sb->s_inode_list_lock);
690 cond_resched();
691 dispose_list(&dispose);
692 goto again;
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Dave Chinner74278da2015-03-04 12:37:22 -0500695 spin_unlock(&sb->s_inode_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Christoph Hellwiga0318782010-10-24 19:40:33 +0200697 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 return busy;
700}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000703 * Isolate the inode from the LRU in preparation for freeing it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
705 * Any inodes which are pinned purely because of attached pagecache have their
Nick Piggin9e38d862010-10-23 06:55:17 -0400706 * pagecache removed. If the inode has metadata buffers attached to
707 * mapping->private_list then try to remove them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400709 * If the inode has the I_REFERENCED flag set, then it means that it has been
710 * used recently - the flag is set in iput_final(). When we encounter such an
711 * inode, clear the flag and move it to the back of the LRU so it gets another
712 * pass through the LRU before it gets reclaimed. This is necessary because of
713 * the fact we are doing lazy LRU updates to minimise lock contention so the
714 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
715 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800717static enum lru_status inode_lru_isolate(struct list_head *item,
718 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000719{
720 struct list_head *freeable = arg;
721 struct inode *inode = container_of(item, struct inode, i_lru);
722
723 /*
724 * we are inverting the lru lock/inode->i_lock here, so use a trylock.
725 * If we fail to get the lock, just skip it.
726 */
727 if (!spin_trylock(&inode->i_lock))
728 return LRU_SKIP;
729
730 /*
731 * Referenced or dirty inodes are still in use. Give them another pass
732 * through the LRU as we canot reclaim them now.
733 */
734 if (atomic_read(&inode->i_count) ||
735 (inode->i_state & ~I_REFERENCED)) {
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800736 list_lru_isolate(lru, &inode->i_lru);
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000737 spin_unlock(&inode->i_lock);
738 this_cpu_dec(nr_unused);
739 return LRU_REMOVED;
740 }
741
Dave Chinner8d485d32019-02-12 15:35:51 -0800742 /* recently referenced inodes get one more pass */
743 if (inode->i_state & I_REFERENCED) {
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000744 inode->i_state &= ~I_REFERENCED;
745 spin_unlock(&inode->i_lock);
746 return LRU_ROTATE;
747 }
748
749 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
750 __iget(inode);
751 spin_unlock(&inode->i_lock);
752 spin_unlock(lru_lock);
753 if (remove_inode_buffers(inode)) {
754 unsigned long reap;
755 reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
756 if (current_is_kswapd())
757 __count_vm_events(KSWAPD_INODESTEAL, reap);
758 else
759 __count_vm_events(PGINODESTEAL, reap);
760 if (current->reclaim_state)
761 current->reclaim_state->reclaimed_slab += reap;
762 }
763 iput(inode);
764 spin_lock(lru_lock);
765 return LRU_RETRY;
766 }
767
768 WARN_ON(inode->i_state & I_NEW);
769 inode->i_state |= I_FREEING;
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800770 list_lru_isolate_move(lru, &inode->i_lru, freeable);
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000771 spin_unlock(&inode->i_lock);
772
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000773 this_cpu_dec(nr_unused);
774 return LRU_REMOVED;
775}
776
777/*
778 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
779 * This is called from the superblock shrinker function with a number of inodes
780 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
781 * then are freed outside inode_lock by dispose_list().
782 */
Vladimir Davydov503c3582015-02-12 14:58:47 -0800783long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 LIST_HEAD(freeable);
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000786 long freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Vladimir Davydov503c3582015-02-12 14:58:47 -0800788 freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
789 inode_lru_isolate, &freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 dispose_list(&freeable);
Dave Chinner0a234c62013-08-28 10:17:57 +1000791 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794static void __wait_on_freeing_inode(struct inode *inode);
795/*
796 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530798static struct inode *find_inode(struct super_block *sb,
799 struct hlist_head *head,
800 int (*test)(struct inode *, void *),
801 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530803 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805repeat:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800806 hlist_for_each_entry(inode, head, i_hash) {
Al Viro5a3cd99282013-11-06 09:54:52 -0500807 if (inode->i_sb != sb)
808 continue;
809 if (!test(inode, data))
810 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100811 spin_lock(&inode->i_lock);
Al Viroa4ffdde2010-06-02 17:38:30 -0400812 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 __wait_on_freeing_inode(inode);
814 goto repeat;
815 }
Al Viroc2b6d622018-06-28 15:53:17 -0400816 if (unlikely(inode->i_state & I_CREATING)) {
817 spin_unlock(&inode->i_lock);
818 return ERR_PTR(-ESTALE);
819 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400820 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100821 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400822 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400824 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827/*
828 * find_inode_fast is the fast path version of find_inode, see the comment at
829 * iget_locked for details.
830 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530831static struct inode *find_inode_fast(struct super_block *sb,
832 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530834 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836repeat:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800837 hlist_for_each_entry(inode, head, i_hash) {
Al Viro5a3cd99282013-11-06 09:54:52 -0500838 if (inode->i_ino != ino)
839 continue;
840 if (inode->i_sb != sb)
841 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100842 spin_lock(&inode->i_lock);
Al Viroa4ffdde2010-06-02 17:38:30 -0400843 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 __wait_on_freeing_inode(inode);
845 goto repeat;
846 }
Al Viroc2b6d622018-06-28 15:53:17 -0400847 if (unlikely(inode->i_state & I_CREATING)) {
848 spin_unlock(&inode->i_lock);
849 return ERR_PTR(-ESTALE);
850 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400851 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100852 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400853 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400855 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Eric Dumazetf991bd22010-10-23 11:18:01 -0400858/*
859 * Each cpu owns a range of LAST_INO_BATCH numbers.
860 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
861 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100862 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400863 * This does not significantly increase overflow rate because every CPU can
864 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
865 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
866 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
867 * overflow rate by 2x, which does not seem too significant.
868 *
869 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
870 * error if st_ino won't fit in target struct field. Use 32bit counter
871 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100872 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400873#define LAST_INO_BATCH 1024
874static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100875
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400876unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400877{
878 unsigned int *p = &get_cpu_var(last_ino);
879 unsigned int res = *p;
880
881#ifdef CONFIG_SMP
882 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
883 static atomic_t shared_last_ino;
884 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
885
886 res = next - LAST_INO_BATCH;
887 }
888#endif
889
Carlos Maiolino2adc3762015-06-25 12:25:58 -0300890 res++;
891 /* get_next_ino should not provide a 0 inode number */
892 if (unlikely(!res))
893 res++;
894 *p = res;
Eric Dumazetf991bd22010-10-23 11:18:01 -0400895 put_cpu_var(last_ino);
896 return res;
David Chinner8290c352008-10-30 17:35:24 +1100897}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400898EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/**
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200901 * new_inode_pseudo - obtain an inode
902 * @sb: superblock
903 *
904 * Allocates a new inode for given superblock.
905 * Inode wont be chained in superblock s_inodes list
906 * This means :
907 * - fs can't be unmount
908 * - quotas, fsnotify, writeback can't work
909 */
910struct inode *new_inode_pseudo(struct super_block *sb)
911{
912 struct inode *inode = alloc_inode(sb);
913
914 if (inode) {
915 spin_lock(&inode->i_lock);
916 inode->i_state = 0;
917 spin_unlock(&inode->i_lock);
918 INIT_LIST_HEAD(&inode->i_sb_list);
919 }
920 return inode;
921}
922
923/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * new_inode - obtain an inode
925 * @sb: superblock
926 *
Mel Gorman769848c2007-07-17 04:03:05 -0700927 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800928 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -0700929 * If HIGHMEM pages are unsuitable or it is known that pages allocated
930 * for the page cache are not reclaimable or migratable,
931 * mapping_set_gfp_mask() must be called with suitable flags on the
932 * newly created inode's mapping
933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 */
935struct inode *new_inode(struct super_block *sb)
936{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530937 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dave Chinner74278da2015-03-04 12:37:22 -0500939 spin_lock_prefetch(&sb->s_inode_list_lock);
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530940
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200941 inode = new_inode_pseudo(sb);
942 if (inode)
Dave Chinner55fa6092011-03-22 22:23:40 +1100943 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return inode;
945}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946EXPORT_SYMBOL(new_inode);
947
Peter Zijlstra14358e62007-10-14 01:38:33 +0200948#ifdef CONFIG_DEBUG_LOCK_ALLOC
Josh Boyere096d0c2011-08-25 07:48:12 -0400949void lockdep_annotate_inode_mutex_key(struct inode *inode)
950{
Namhyung Kima3314a02010-10-11 22:38:00 +0900951 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200952 struct file_system_type *type = inode->i_sb->s_type;
953
Jan Kara9a7aa122009-06-04 15:26:49 +0200954 /* Set new key only if filesystem hasn't already changed it */
Al Viro9902af72016-04-15 15:08:36 -0400955 if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) {
Jan Kara9a7aa122009-06-04 15:26:49 +0200956 /*
957 * ensure nobody is actually holding i_mutex
958 */
Al Viro9902af72016-04-15 15:08:36 -0400959 // mutex_destroy(&inode->i_mutex);
960 init_rwsem(&inode->i_rwsem);
961 lockdep_set_class(&inode->i_rwsem,
Jan Kara9a7aa122009-06-04 15:26:49 +0200962 &type->i_mutex_dir_key);
963 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +0200964 }
Josh Boyere096d0c2011-08-25 07:48:12 -0400965}
966EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
Peter Zijlstra14358e62007-10-14 01:38:33 +0200967#endif
Josh Boyere096d0c2011-08-25 07:48:12 -0400968
969/**
970 * unlock_new_inode - clear the I_NEW state and wake up any waiters
971 * @inode: new inode to unlock
972 *
973 * Called when the inode is fully initialised to clear the new state of the
974 * inode and wake up anyone waiting for the inode to finish initialisation.
975 */
976void unlock_new_inode(struct inode *inode)
977{
978 lockdep_annotate_inode_mutex_key(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100979 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +0100980 WARN_ON(!(inode->i_state & I_NEW));
Al Viroc2b6d622018-06-28 15:53:17 -0400981 inode->i_state &= ~I_NEW & ~I_CREATING;
Al Viro310fa7a2012-03-10 17:07:28 -0500982 smp_mb();
Dave Chinner250df6e2011-03-22 22:23:36 +1100983 wake_up_bit(&inode->i_state, __I_NEW);
984 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986EXPORT_SYMBOL(unlock_new_inode);
987
Al Viroc2b6d622018-06-28 15:53:17 -0400988void discard_new_inode(struct inode *inode)
989{
990 lockdep_annotate_inode_mutex_key(inode);
991 spin_lock(&inode->i_lock);
992 WARN_ON(!(inode->i_state & I_NEW));
993 inode->i_state &= ~I_NEW;
994 smp_mb();
995 wake_up_bit(&inode->i_state, __I_NEW);
996 spin_unlock(&inode->i_lock);
997 iput(inode);
998}
999EXPORT_SYMBOL(discard_new_inode);
1000
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001001/**
J. Bruce Fields375e2892012-04-18 15:16:33 -04001002 * lock_two_nondirectories - take two i_mutexes on non-directory objects
J. Bruce Fields4fd699a2014-04-01 17:08:43 +02001003 *
1004 * Lock any non-NULL argument that is not a directory.
1005 * Zero, one or two objects may be locked by this function.
1006 *
J. Bruce Fields375e2892012-04-18 15:16:33 -04001007 * @inode1: first inode to lock
1008 * @inode2: second inode to lock
1009 */
1010void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1011{
J. Bruce Fields4fd699a2014-04-01 17:08:43 +02001012 if (inode1 > inode2)
1013 swap(inode1, inode2);
1014
1015 if (inode1 && !S_ISDIR(inode1->i_mode))
Al Viro59551022016-01-22 15:40:57 -05001016 inode_lock(inode1);
J. Bruce Fields4fd699a2014-04-01 17:08:43 +02001017 if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
Al Viro59551022016-01-22 15:40:57 -05001018 inode_lock_nested(inode2, I_MUTEX_NONDIR2);
J. Bruce Fields375e2892012-04-18 15:16:33 -04001019}
1020EXPORT_SYMBOL(lock_two_nondirectories);
1021
1022/**
1023 * unlock_two_nondirectories - release locks from lock_two_nondirectories()
1024 * @inode1: first inode to unlock
1025 * @inode2: second inode to unlock
1026 */
1027void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1028{
J. Bruce Fields4fd699a2014-04-01 17:08:43 +02001029 if (inode1 && !S_ISDIR(inode1->i_mode))
Al Viro59551022016-01-22 15:40:57 -05001030 inode_unlock(inode1);
J. Bruce Fields4fd699a2014-04-01 17:08:43 +02001031 if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
Al Viro59551022016-01-22 15:40:57 -05001032 inode_unlock(inode2);
J. Bruce Fields375e2892012-04-18 15:16:33 -04001033}
1034EXPORT_SYMBOL(unlock_two_nondirectories);
1035
1036/**
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001037 * inode_insert5 - obtain an inode from a mounted file system
1038 * @inode: pre-allocated inode to use for insert to cache
1039 * @hashval: hash value (usually inode number) to get
1040 * @test: callback used for comparisons between inodes
1041 * @set: callback used to initialize a new struct inode
1042 * @data: opaque data pointer to pass to @test and @set
1043 *
1044 * Search for the inode specified by @hashval and @data in the inode cache,
1045 * and if present it is return it with an increased reference count. This is
1046 * a variant of iget5_locked() for callers that don't want to fail on memory
1047 * allocation of inode.
1048 *
1049 * If the inode is not in cache, insert the pre-allocated inode to cache and
1050 * return it locked, hashed, and with the I_NEW flag set. The file system gets
1051 * to fill it in before unlocking it via unlock_new_inode().
1052 *
1053 * Note both @test and @set are called with the inode_hash_lock held, so can't
1054 * sleep.
1055 */
1056struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
1057 int (*test)(struct inode *, void *),
1058 int (*set)(struct inode *, void *), void *data)
1059{
1060 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1061 struct inode *old;
Miklos Szeredie9505642018-07-24 15:01:55 +02001062 bool creating = inode->i_state & I_CREATING;
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001063
1064again:
1065 spin_lock(&inode_hash_lock);
1066 old = find_inode(inode->i_sb, head, test, data);
1067 if (unlikely(old)) {
1068 /*
1069 * Uhhuh, somebody else created the same inode under us.
1070 * Use the old inode instead of the preallocated one.
1071 */
1072 spin_unlock(&inode_hash_lock);
Al Viroc2b6d622018-06-28 15:53:17 -04001073 if (IS_ERR(old))
1074 return NULL;
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001075 wait_on_inode(old);
1076 if (unlikely(inode_unhashed(old))) {
1077 iput(old);
1078 goto again;
1079 }
1080 return old;
1081 }
1082
1083 if (set && unlikely(set(inode, data))) {
1084 inode = NULL;
1085 goto unlock;
1086 }
1087
1088 /*
1089 * Return the locked inode with I_NEW set, the
1090 * caller is responsible for filling in the contents
1091 */
1092 spin_lock(&inode->i_lock);
1093 inode->i_state |= I_NEW;
1094 hlist_add_head(&inode->i_hash, head);
1095 spin_unlock(&inode->i_lock);
Miklos Szeredie9505642018-07-24 15:01:55 +02001096 if (!creating)
1097 inode_sb_list_add(inode);
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001098unlock:
1099 spin_unlock(&inode_hash_lock);
1100
1101 return inode;
1102}
1103EXPORT_SYMBOL(inode_insert5);
1104
1105/**
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001106 * iget5_locked - obtain an inode from a mounted file system
1107 * @sb: super block of file system
1108 * @hashval: hash value (usually inode number) to get
1109 * @test: callback used for comparisons between inodes
1110 * @set: callback used to initialize a new struct inode
1111 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001113 * Search for the inode specified by @hashval and @data in the inode cache,
1114 * and if present it is return it with an increased reference count. This is
1115 * a generalized version of iget_locked() for file systems where the inode
1116 * number is not sufficient for unique identification of an inode.
1117 *
1118 * If the inode is not in cache, allocate a new inode and return it locked,
1119 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1120 * before unlocking it via unlock_new_inode().
1121 *
1122 * Note both @test and @set are called with the inode_hash_lock held, so can't
1123 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001125struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1126 int (*test)(struct inode *, void *),
1127 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001129 struct inode *inode = ilookup5(sb, hashval, test, data);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001130
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001131 if (!inode) {
Miklos Szeredie9505642018-07-24 15:01:55 +02001132 struct inode *new = alloc_inode(sb);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001133
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001134 if (new) {
Miklos Szeredie9505642018-07-24 15:01:55 +02001135 new->i_state = 0;
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001136 inode = inode_insert5(new, hashval, test, set, data);
1137 if (unlikely(inode != new))
Miklos Szeredie9505642018-07-24 15:01:55 +02001138 destroy_inode(new);
Al Viro2864f302016-07-03 23:15:21 -04001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001143EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001145/**
1146 * iget_locked - obtain an inode from a mounted file system
1147 * @sb: super block of file system
1148 * @ino: inode number to get
1149 *
1150 * Search for the inode specified by @ino in the inode cache and if present
1151 * return it with an increased reference count. This is for file systems
1152 * where the inode number is sufficient for unique identification of an inode.
1153 *
1154 * If the inode is not in cache, allocate a new inode and return it locked,
1155 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1156 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001158struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001160 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301161 struct inode *inode;
Al Viro2864f302016-07-03 23:15:21 -04001162again:
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001163 spin_lock(&inode_hash_lock);
1164 inode = find_inode_fast(sb, head, ino);
1165 spin_unlock(&inode_hash_lock);
1166 if (inode) {
Al Viroc2b6d622018-06-28 15:53:17 -04001167 if (IS_ERR(inode))
1168 return NULL;
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001169 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001170 if (unlikely(inode_unhashed(inode))) {
1171 iput(inode);
1172 goto again;
1173 }
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001174 return inode;
1175 }
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 inode = alloc_inode(sb);
1178 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301179 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Dave Chinner67a23c42011-03-22 22:23:42 +11001181 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /* We released the lock, so.. */
1183 old = find_inode_fast(sb, head, ino);
1184 if (!old) {
1185 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001186 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001187 inode->i_state = I_NEW;
Dave Chinner250df6e2011-03-22 22:23:36 +11001188 hlist_add_head(&inode->i_hash, head);
1189 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001190 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001191 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /* Return the locked inode with I_NEW set, the
1194 * caller is responsible for filling in the contents
1195 */
1196 return inode;
1197 }
1198
1199 /*
1200 * Uhhuh, somebody else created the same inode under
1201 * us. Use the old inode instead of the one we just
1202 * allocated.
1203 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001204 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 destroy_inode(inode);
Al Viroc2b6d622018-06-28 15:53:17 -04001206 if (IS_ERR(old))
1207 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 inode = old;
1209 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001210 if (unlikely(inode_unhashed(inode))) {
1211 iput(inode);
1212 goto again;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 return inode;
1216}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001217EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001219/*
1220 * search the inode cache for a matching inode number.
1221 * If we find one, then the inode number we are trying to
1222 * allocate is not unique and so we should not use it.
1223 *
1224 * Returns 1 if the inode number is unique, 0 if it is not.
1225 */
1226static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1227{
1228 struct hlist_head *b = inode_hashtable + hash(sb, ino);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001229 struct inode *inode;
1230
Dave Chinner67a23c42011-03-22 22:23:42 +11001231 spin_lock(&inode_hash_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001232 hlist_for_each_entry(inode, b, i_hash) {
Dave Chinner67a23c42011-03-22 22:23:42 +11001233 if (inode->i_ino == ino && inode->i_sb == sb) {
1234 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001235 return 0;
Dave Chinner67a23c42011-03-22 22:23:42 +11001236 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001237 }
Dave Chinner67a23c42011-03-22 22:23:42 +11001238 spin_unlock(&inode_hash_lock);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001239
1240 return 1;
1241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/**
1244 * iunique - get a unique inode number
1245 * @sb: superblock
1246 * @max_reserved: highest reserved inode number
1247 *
1248 * Obtain an inode number that is unique on the system for a given
1249 * superblock. This is used by file systems that have no natural
1250 * permanent inode numbering system. An inode number is returned that
1251 * is higher than the reserved limit but unique.
1252 *
1253 * BUGS:
1254 * With a large number of inodes live on the file system this function
1255 * currently becomes quite slow.
1256 */
1257ino_t iunique(struct super_block *sb, ino_t max_reserved)
1258{
Jeff Layton866b04f2007-05-08 00:32:29 -07001259 /*
1260 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1261 * error if st_ino won't fit in target struct field. Use 32bit counter
1262 * here to attempt to avoid that.
1263 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001264 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001265 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001268 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001269 do {
1270 if (counter <= max_reserved)
1271 counter = max_reserved + 1;
1272 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001273 } while (!test_inode_iunique(sb, res));
1274 spin_unlock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001275
1276 return res;
1277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278EXPORT_SYMBOL(iunique);
1279
1280struct inode *igrab(struct inode *inode)
1281{
Dave Chinner250df6e2011-03-22 22:23:36 +11001282 spin_lock(&inode->i_lock);
1283 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001285 spin_unlock(&inode->i_lock);
1286 } else {
1287 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /*
1289 * Handle the case where s_op->clear_inode is not been
1290 * called yet, and somebody is calling igrab
1291 * while the inode is getting freed.
1292 */
1293 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return inode;
1296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297EXPORT_SYMBOL(igrab);
1298
1299/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001300 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * @sb: super block of file system to search
1302 * @hashval: hash value (usually inode number) to search for
1303 * @test: callback used for comparisons between inodes
1304 * @data: opaque data pointer to pass to @test
1305 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001306 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001308 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001309 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001310 * Note: I_NEW is not waited upon so you have to be very careful what you do
1311 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001312 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001313 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001314 */
1315struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1316 int (*test)(struct inode *, void *), void *data)
1317{
1318 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001319 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001320
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001321 spin_lock(&inode_hash_lock);
1322 inode = find_inode(sb, head, test, data);
1323 spin_unlock(&inode_hash_lock);
1324
Al Viroc2b6d622018-06-28 15:53:17 -04001325 return IS_ERR(inode) ? NULL : inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001326}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001327EXPORT_SYMBOL(ilookup5_nowait);
1328
1329/**
1330 * ilookup5 - search for an inode in the inode cache
1331 * @sb: super block of file system to search
1332 * @hashval: hash value (usually inode number) to search for
1333 * @test: callback used for comparisons between inodes
1334 * @data: opaque data pointer to pass to @test
1335 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001336 * Search for the inode specified by @hashval and @data in the inode cache,
1337 * and if the inode is in the cache, return the inode with an incremented
1338 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001339 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001341 * This is a generalized version of ilookup() for file systems where the
1342 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001344 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
1346struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1347 int (*test)(struct inode *, void *), void *data)
1348{
Al Viro2864f302016-07-03 23:15:21 -04001349 struct inode *inode;
1350again:
1351 inode = ilookup5_nowait(sb, hashval, test, data);
1352 if (inode) {
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001353 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001354 if (unlikely(inode_unhashed(inode))) {
1355 iput(inode);
1356 goto again;
1357 }
1358 }
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001359 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361EXPORT_SYMBOL(ilookup5);
1362
1363/**
1364 * ilookup - search for an inode in the inode cache
1365 * @sb: super block of file system to search
1366 * @ino: inode number to search for
1367 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001368 * Search for the inode @ino in the inode cache, and if the inode is in the
1369 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
1371struct inode *ilookup(struct super_block *sb, unsigned long ino)
1372{
1373 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001374 struct inode *inode;
Al Viro2864f302016-07-03 23:15:21 -04001375again:
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001376 spin_lock(&inode_hash_lock);
1377 inode = find_inode_fast(sb, head, ino);
1378 spin_unlock(&inode_hash_lock);
1379
Al Viro2864f302016-07-03 23:15:21 -04001380 if (inode) {
Al Viroc2b6d622018-06-28 15:53:17 -04001381 if (IS_ERR(inode))
1382 return NULL;
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001383 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001384 if (unlikely(inode_unhashed(inode))) {
1385 iput(inode);
1386 goto again;
1387 }
1388 }
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001389 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391EXPORT_SYMBOL(ilookup);
1392
Theodore Ts'ofe032c42015-02-02 00:37:01 -05001393/**
1394 * find_inode_nowait - find an inode in the inode cache
1395 * @sb: super block of file system to search
1396 * @hashval: hash value (usually inode number) to search for
1397 * @match: callback used for comparisons between inodes
1398 * @data: opaque data pointer to pass to @match
1399 *
1400 * Search for the inode specified by @hashval and @data in the inode
1401 * cache, where the helper function @match will return 0 if the inode
1402 * does not match, 1 if the inode does match, and -1 if the search
1403 * should be stopped. The @match function must be responsible for
1404 * taking the i_lock spin_lock and checking i_state for an inode being
1405 * freed or being initialized, and incrementing the reference count
1406 * before returning 1. It also must not sleep, since it is called with
1407 * the inode_hash_lock spinlock held.
1408 *
1409 * This is a even more generalized version of ilookup5() when the
1410 * function must never block --- find_inode() can block in
1411 * __wait_on_freeing_inode() --- or when the caller can not increment
1412 * the reference count because the resulting iput() might cause an
1413 * inode eviction. The tradeoff is that the @match funtion must be
1414 * very carefully implemented.
1415 */
1416struct inode *find_inode_nowait(struct super_block *sb,
1417 unsigned long hashval,
1418 int (*match)(struct inode *, unsigned long,
1419 void *),
1420 void *data)
1421{
1422 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1423 struct inode *inode, *ret_inode = NULL;
1424 int mval;
1425
1426 spin_lock(&inode_hash_lock);
1427 hlist_for_each_entry(inode, head, i_hash) {
1428 if (inode->i_sb != sb)
1429 continue;
1430 mval = match(inode, hashval, data);
1431 if (mval == 0)
1432 continue;
1433 if (mval == 1)
1434 ret_inode = inode;
1435 goto out;
1436 }
1437out:
1438 spin_unlock(&inode_hash_lock);
1439 return ret_inode;
1440}
1441EXPORT_SYMBOL(find_inode_nowait);
1442
Al Viro261bca82008-12-30 01:48:21 -05001443int insert_inode_locked(struct inode *inode)
1444{
1445 struct super_block *sb = inode->i_sb;
1446 ino_t ino = inode->i_ino;
1447 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001448
Al Viro261bca82008-12-30 01:48:21 -05001449 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001450 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001451 spin_lock(&inode_hash_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001452 hlist_for_each_entry(old, head, i_hash) {
Al Viro72a43d62009-05-13 19:13:40 +01001453 if (old->i_ino != ino)
1454 continue;
1455 if (old->i_sb != sb)
1456 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001457 spin_lock(&old->i_lock);
1458 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1459 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001460 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001461 }
Al Viro72a43d62009-05-13 19:13:40 +01001462 break;
1463 }
Sasha Levinb67bfe02013-02-27 17:06:00 -08001464 if (likely(!old)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001465 spin_lock(&inode->i_lock);
Al Viroc2b6d622018-06-28 15:53:17 -04001466 inode->i_state |= I_NEW | I_CREATING;
Al Viro261bca82008-12-30 01:48:21 -05001467 hlist_add_head(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001468 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001469 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001470 return 0;
1471 }
Al Viroc2b6d622018-06-28 15:53:17 -04001472 if (unlikely(old->i_state & I_CREATING)) {
1473 spin_unlock(&old->i_lock);
1474 spin_unlock(&inode_hash_lock);
1475 return -EBUSY;
1476 }
Al Viro261bca82008-12-30 01:48:21 -05001477 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001478 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001479 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001480 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001481 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001482 iput(old);
1483 return -EBUSY;
1484 }
1485 iput(old);
1486 }
1487}
Al Viro261bca82008-12-30 01:48:21 -05001488EXPORT_SYMBOL(insert_inode_locked);
1489
1490int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1491 int (*test)(struct inode *, void *), void *data)
1492{
Al Viroc2b6d622018-06-28 15:53:17 -04001493 struct inode *old;
1494
1495 inode->i_state |= I_CREATING;
1496 old = inode_insert5(inode, hashval, test, NULL, data);
Al Viro261bca82008-12-30 01:48:21 -05001497
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001498 if (old != inode) {
Al Viro261bca82008-12-30 01:48:21 -05001499 iput(old);
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001500 return -EBUSY;
Al Viro261bca82008-12-30 01:48:21 -05001501 }
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001502 return 0;
Al Viro261bca82008-12-30 01:48:21 -05001503}
Al Viro261bca82008-12-30 01:48:21 -05001504EXPORT_SYMBOL(insert_inode_locked4);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Al Viro45321ac2010-06-07 13:43:19 -04001507int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Al Viro45321ac2010-06-07 13:43:19 -04001509 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511EXPORT_SYMBOL(generic_delete_inode);
1512
Al Viro45321ac2010-06-07 13:43:19 -04001513/*
Al Viro45321ac2010-06-07 13:43:19 -04001514 * Called when we're dropping the last reference
1515 * to an inode.
1516 *
1517 * Call the FS "drop_inode()" function, defaulting to
1518 * the legacy UNIX filesystem behaviour. If it tells
1519 * us to evict inode, do so. Otherwise, retain inode
1520 * in cache if fs is alive, sync and evict if fs is
1521 * shutting down.
1522 */
1523static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001526 const struct super_operations *op = inode->i_sb->s_op;
1527 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Dave Chinner250df6e2011-03-22 22:23:36 +11001529 WARN_ON(inode->i_state & I_NEW);
1530
Al Viroe7f59092011-07-07 15:45:59 -04001531 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001532 drop = op->drop_inode(inode);
1533 else
1534 drop = generic_drop_inode(inode);
1535
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001536 if (!drop && (sb->s_flags & SB_ACTIVE)) {
Jan Kara4eff96d2012-11-26 16:29:51 -08001537 inode_add_lru(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001538 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001539 return;
1540 }
1541
Al Viro45321ac2010-06-07 13:43:19 -04001542 if (!drop) {
Alexander Viro991114c2005-06-23 00:09:01 -07001543 inode->i_state |= I_WILL_FREE;
Dave Chinner250df6e2011-03-22 22:23:36 +11001544 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 write_inode_now(inode, 1);
Dave Chinner250df6e2011-03-22 22:23:36 +11001546 spin_lock(&inode->i_lock);
Nick Piggin7ef0d732009-03-12 14:31:38 -07001547 WARN_ON(inode->i_state & I_NEW);
Alexander Viro991114c2005-06-23 00:09:01 -07001548 inode->i_state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001550
Alexander Viro991114c2005-06-23 00:09:01 -07001551 inode->i_state |= I_FREEING;
Eric Dumazetc4ae0c62011-07-28 06:55:13 +02001552 if (!list_empty(&inode->i_lru))
1553 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001554 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001555
1556 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301560 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 * @inode: inode to put
1562 *
1563 * Puts an inode, dropping its usage count. If the inode use count hits
1564 * zero, the inode is then freed and may also be destroyed.
1565 *
1566 * Consequently, iput() can sleep.
1567 */
1568void iput(struct inode *inode)
1569{
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001570 if (!inode)
1571 return;
1572 BUG_ON(inode->i_state & I_CLEAR);
1573retry:
1574 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
1575 if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
1576 atomic_inc(&inode->i_count);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001577 spin_unlock(&inode->i_lock);
1578 trace_writeback_lazytime_iput(inode);
1579 mark_inode_dirty_sync(inode);
1580 goto retry;
1581 }
1582 iput_final(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585EXPORT_SYMBOL(iput);
1586
1587/**
1588 * bmap - find a block number in a file
1589 * @inode: inode of file
1590 * @block: block to find
1591 *
1592 * Returns the block number on the device holding the inode that
1593 * is the disk block number for the block of the file requested.
1594 * That is, asked for block 4 of inode 1 the function will return the
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301595 * disk block relative to the disk start that holds that block of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 * file.
1597 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301598sector_t bmap(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 sector_t res = 0;
1601 if (inode->i_mapping->a_ops->bmap)
1602 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1603 return res;
1604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605EXPORT_SYMBOL(bmap);
1606
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001607/*
1608 * With relative atime, only update atime if the previous atime is
1609 * earlier than either the ctime or mtime or if at least a day has
1610 * passed since the last atime update.
1611 */
Miklos Szeredic6718542018-07-18 15:44:43 +02001612static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1613 struct timespec now)
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001614{
1615
Miklos Szeredic6718542018-07-18 15:44:43 +02001616 if (!(mnt->mnt_flags & MNT_RELATIME))
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001617 return 1;
1618 /*
1619 * Is mtime younger than atime? If yes, update atime:
1620 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07001621 if (timespec64_compare(&inode->i_mtime, &inode->i_atime) >= 0)
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001622 return 1;
1623 /*
1624 * Is ctime younger than atime? If yes, update atime:
1625 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07001626 if (timespec64_compare(&inode->i_ctime, &inode->i_atime) >= 0)
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001627 return 1;
1628
1629 /*
1630 * Is the previous atime value older than a day? If yes,
1631 * update atime:
1632 */
1633 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1634 return 1;
1635 /*
1636 * Good, we can skip the atime update:
1637 */
1638 return 0;
1639}
1640
Deepa Dinamani95582b02018-05-08 19:36:02 -07001641int generic_update_time(struct inode *inode, struct timespec64 *time, int flags)
Josef Bacikc3b2da32012-03-26 09:59:21 -04001642{
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001643 int iflags = I_DIRTY_TIME;
Jeff Laytone38cf302017-12-11 06:35:22 -05001644 bool dirty = false;
Josef Bacikc3b2da32012-03-26 09:59:21 -04001645
1646 if (flags & S_ATIME)
1647 inode->i_atime = *time;
1648 if (flags & S_VERSION)
Jeff Laytone38cf302017-12-11 06:35:22 -05001649 dirty = inode_maybe_inc_iversion(inode, false);
Josef Bacikc3b2da32012-03-26 09:59:21 -04001650 if (flags & S_CTIME)
1651 inode->i_ctime = *time;
1652 if (flags & S_MTIME)
1653 inode->i_mtime = *time;
Jeff Laytone38cf302017-12-11 06:35:22 -05001654 if ((flags & (S_ATIME | S_CTIME | S_MTIME)) &&
1655 !(inode->i_sb->s_flags & SB_LAZYTIME))
1656 dirty = true;
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001657
Jeff Laytone38cf302017-12-11 06:35:22 -05001658 if (dirty)
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001659 iflags |= I_DIRTY_SYNC;
1660 __mark_inode_dirty(inode, iflags);
Josef Bacikc3b2da32012-03-26 09:59:21 -04001661 return 0;
1662}
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001663EXPORT_SYMBOL(generic_update_time);
1664
1665/*
1666 * This does the actual work of updating an inodes time or version. Must have
1667 * had called mnt_want_write() before calling this.
1668 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07001669static int update_time(struct inode *inode, struct timespec64 *time, int flags)
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001670{
Deepa Dinamani95582b02018-05-08 19:36:02 -07001671 int (*update_time)(struct inode *, struct timespec64 *, int);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001672
1673 update_time = inode->i_op->update_time ? inode->i_op->update_time :
1674 generic_update_time;
1675
1676 return update_time(inode, time, flags);
1677}
Josef Bacikc3b2da32012-03-26 09:59:21 -04001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679/**
Christoph Hellwig869243a2006-01-09 20:52:03 -08001680 * touch_atime - update the access time
Randy Dunlap185553b2012-04-17 17:03:25 -07001681 * @path: the &struct path to update
Randy Dunlap30fdc8e2015-11-09 14:57:58 -08001682 * @inode: inode to update
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 *
1684 * Update the accessed time on an inode and mark it for writeback.
1685 * This function automatically handles read only file systems and media,
1686 * as well as the "noatime" flag and inode specific "noatime" markers.
1687 */
Miklos Szeredic6718542018-07-18 15:44:43 +02001688bool atime_needs_update(const struct path *path, struct inode *inode)
NeilBrown8fa9dd22015-03-23 13:37:40 +11001689{
1690 struct vfsmount *mnt = path->mnt;
Deepa Dinamani95582b02018-05-08 19:36:02 -07001691 struct timespec64 now;
NeilBrown8fa9dd22015-03-23 13:37:40 +11001692
1693 if (inode->i_flags & S_NOATIME)
1694 return false;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05001695
1696 /* Atime updates will likely cause i_uid and i_gid to be written
1697 * back improprely if their true value is unknown to the vfs.
1698 */
1699 if (HAS_UNMAPPED_ID(inode))
1700 return false;
1701
NeilBrown8fa9dd22015-03-23 13:37:40 +11001702 if (IS_NOATIME(inode))
1703 return false;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001704 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001705 return false;
1706
1707 if (mnt->mnt_flags & MNT_NOATIME)
1708 return false;
1709 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1710 return false;
1711
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001712 now = current_time(inode);
NeilBrown8fa9dd22015-03-23 13:37:40 +11001713
Miklos Szeredic6718542018-07-18 15:44:43 +02001714 if (!relatime_need_update(mnt, inode, timespec64_to_timespec(now)))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001715 return false;
1716
Deepa Dinamani95582b02018-05-08 19:36:02 -07001717 if (timespec64_equal(&inode->i_atime, &now))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001718 return false;
1719
1720 return true;
1721}
1722
Al Virobadcf2b2013-07-16 18:15:46 +04001723void touch_atime(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Al Viro68ac1232012-03-15 08:21:57 -04001725 struct vfsmount *mnt = path->mnt;
David Howellsdf2b1af2015-03-17 22:26:15 +00001726 struct inode *inode = d_inode(path->dentry);
Deepa Dinamani95582b02018-05-08 19:36:02 -07001727 struct timespec64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Miklos Szeredic6718542018-07-18 15:44:43 +02001729 if (!atime_needs_update(path, inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07001730 return;
1731
Jan Kara5d37e9e2012-06-12 16:20:36 +02001732 if (!sb_start_write_trylock(inode->i_sb))
Andi Kleenb12536c2009-09-18 13:05:47 -07001733 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08001734
NeilBrown8fa9dd22015-03-23 13:37:40 +11001735 if (__mnt_want_write(mnt) != 0)
Jan Kara5d37e9e2012-06-12 16:20:36 +02001736 goto skip_update;
Josef Bacikc3b2da32012-03-26 09:59:21 -04001737 /*
1738 * File systems can error out when updating inodes if they need to
1739 * allocate new space to modify an inode (such is the case for
1740 * Btrfs), but since we touch atime while walking down the path we
1741 * really don't care if we failed to update the atime of the file,
1742 * so just ignore the return value.
Alexander Block2bc5565282012-06-15 09:49:33 +02001743 * We may also fail on filesystems that have the ability to make parts
1744 * of the fs read only, e.g. subvolumes in Btrfs.
Josef Bacikc3b2da32012-03-26 09:59:21 -04001745 */
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001746 now = current_time(inode);
Josef Bacikc3b2da32012-03-26 09:59:21 -04001747 update_time(inode, &now, S_ATIME);
Jan Kara5d37e9e2012-06-12 16:20:36 +02001748 __mnt_drop_write(mnt);
1749skip_update:
1750 sb_end_write(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
Christoph Hellwig869243a2006-01-09 20:52:03 -08001752EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Cong Wang3ed37642012-05-15 14:57:33 +08001754/*
1755 * The logic we want is
1756 *
1757 * if suid or (sgid and xgrp)
1758 * remove privs
1759 */
1760int should_remove_suid(struct dentry *dentry)
1761{
David Howellsdf2b1af2015-03-17 22:26:15 +00001762 umode_t mode = d_inode(dentry)->i_mode;
Cong Wang3ed37642012-05-15 14:57:33 +08001763 int kill = 0;
1764
1765 /* suid always must be killed */
1766 if (unlikely(mode & S_ISUID))
1767 kill = ATTR_KILL_SUID;
1768
1769 /*
1770 * sgid without any exec bits is just a mandatory locking mark; leave
1771 * it alone. If some exec bits are set, it's a real sgid; kill it.
1772 */
1773 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1774 kill |= ATTR_KILL_SGID;
1775
1776 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
1777 return kill;
1778
1779 return 0;
1780}
1781EXPORT_SYMBOL(should_remove_suid);
1782
Jan Karadbfae0c2015-05-21 16:05:54 +02001783/*
1784 * Return mask of changes for notify_change() that need to be done as a
1785 * response to write or truncate. Return 0 if nothing has to be changed.
1786 * Negative value on error (change should be denied).
1787 */
Jan Kara45f147a2015-05-21 16:05:55 +02001788int dentry_needs_remove_privs(struct dentry *dentry)
Jan Karadbfae0c2015-05-21 16:05:54 +02001789{
Jan Karadbfae0c2015-05-21 16:05:54 +02001790 struct inode *inode = d_inode(dentry);
1791 int mask = 0;
1792 int ret;
1793
1794 if (IS_NOSEC(inode))
1795 return 0;
1796
1797 mask = should_remove_suid(dentry);
1798 ret = security_inode_need_killpriv(dentry);
1799 if (ret < 0)
1800 return ret;
1801 if (ret)
1802 mask |= ATTR_KILL_PRIV;
1803 return mask;
1804}
Jan Karadbfae0c2015-05-21 16:05:54 +02001805
Daniel Rosenberg74cca902016-10-26 16:33:11 -07001806static int __remove_privs(struct vfsmount *mnt, struct dentry *dentry, int kill)
Cong Wang3ed37642012-05-15 14:57:33 +08001807{
1808 struct iattr newattrs;
1809
1810 newattrs.ia_valid = ATTR_FORCE | kill;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -04001811 /*
1812 * Note we call this on write, so notify_change will not
1813 * encounter any conflicting delegations:
1814 */
Daniel Rosenberg74cca902016-10-26 16:33:11 -07001815 return notify_change2(mnt, dentry, &newattrs, NULL);
Cong Wang3ed37642012-05-15 14:57:33 +08001816}
1817
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001818/*
1819 * Remove special file priviledges (suid, capabilities) when file is written
1820 * to or truncated.
1821 */
1822int file_remove_privs(struct file *file)
Cong Wang3ed37642012-05-15 14:57:33 +08001823{
Miklos Szeredic1892c32016-08-03 13:44:27 +02001824 struct dentry *dentry = file_dentry(file);
1825 struct inode *inode = file_inode(file);
Jan Karadbfae0c2015-05-21 16:05:54 +02001826 int kill;
Cong Wang3ed37642012-05-15 14:57:33 +08001827 int error = 0;
1828
Alexander Lochmanne8e448b2018-12-14 11:55:52 +01001829 /*
1830 * Fast path for nothing security related.
1831 * As well for non-regular files, e.g. blkdev inodes.
1832 * For example, blkdev_write_iter() might get here
1833 * trying to remove privs which it is not allowed to.
1834 */
1835 if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode))
Cong Wang3ed37642012-05-15 14:57:33 +08001836 return 0;
1837
Miklos Szeredic1892c32016-08-03 13:44:27 +02001838 kill = dentry_needs_remove_privs(dentry);
Jan Karadbfae0c2015-05-21 16:05:54 +02001839 if (kill < 0)
1840 return kill;
1841 if (kill)
Daniel Rosenberg74cca902016-10-26 16:33:11 -07001842 error = __remove_privs(file->f_path.mnt, dentry, kill);
Jan Kara2426f392015-05-21 16:05:52 +02001843 if (!error)
1844 inode_has_no_xattr(inode);
Cong Wang3ed37642012-05-15 14:57:33 +08001845
1846 return error;
1847}
Jan Kara5fa8e0a2015-05-21 16:05:53 +02001848EXPORT_SYMBOL(file_remove_privs);
Cong Wang3ed37642012-05-15 14:57:33 +08001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850/**
Christoph Hellwig870f4812006-01-09 20:52:01 -08001851 * file_update_time - update mtime and ctime time
1852 * @file: file accessed
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 *
Christoph Hellwig870f4812006-01-09 20:52:01 -08001854 * Update the mtime and ctime members of an inode and mark the inode
1855 * for writeback. Note that this function is meant exclusively for
1856 * usage in the file write path of filesystems, and filesystems may
1857 * choose to explicitly ignore update via this function with the
Wolfram Sang2eadfc02009-04-02 15:23:37 +02001858 * S_NOCMTIME inode flag, e.g. for network filesystem where these
Josef Bacikc3b2da32012-03-26 09:59:21 -04001859 * timestamps are handled by the server. This can return an error for
1860 * file systems who need to allocate space in order to update an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 */
1862
Josef Bacikc3b2da32012-03-26 09:59:21 -04001863int file_update_time(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Al Viro496ad9a2013-01-23 17:07:38 -05001865 struct inode *inode = file_inode(file);
Deepa Dinamani95582b02018-05-08 19:36:02 -07001866 struct timespec64 now;
Josef Bacikc3b2da32012-03-26 09:59:21 -04001867 int sync_it = 0;
1868 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Andi Kleence06e0b2009-09-18 13:05:48 -07001870 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (IS_NOCMTIME(inode))
Josef Bacikc3b2da32012-03-26 09:59:21 -04001872 return 0;
Dave Hansen20ddee22008-02-15 14:37:43 -08001873
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001874 now = current_time(inode);
Deepa Dinamani95582b02018-05-08 19:36:02 -07001875 if (!timespec64_equal(&inode->i_mtime, &now))
Andi Kleence06e0b2009-09-18 13:05:48 -07001876 sync_it = S_MTIME;
1877
Deepa Dinamani95582b02018-05-08 19:36:02 -07001878 if (!timespec64_equal(&inode->i_ctime, &now))
Andi Kleence06e0b2009-09-18 13:05:48 -07001879 sync_it |= S_CTIME;
1880
Jeff Laytone38cf302017-12-11 06:35:22 -05001881 if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode))
Andi Kleence06e0b2009-09-18 13:05:48 -07001882 sync_it |= S_VERSION;
1883
1884 if (!sync_it)
Josef Bacikc3b2da32012-03-26 09:59:21 -04001885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Andi Kleence06e0b2009-09-18 13:05:48 -07001887 /* Finally allowed to write? Takes lock. */
Jan Karaeb04c282012-06-12 16:20:35 +02001888 if (__mnt_want_write_file(file))
Josef Bacikc3b2da32012-03-26 09:59:21 -04001889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Josef Bacikc3b2da32012-03-26 09:59:21 -04001891 ret = update_time(inode, &now, sync_it);
Jan Karaeb04c282012-06-12 16:20:35 +02001892 __mnt_drop_write_file(file);
Josef Bacikc3b2da32012-03-26 09:59:21 -04001893
1894 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
Christoph Hellwig870f4812006-01-09 20:52:01 -08001896EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898int inode_needs_sync(struct inode *inode)
1899{
1900 if (IS_SYNC(inode))
1901 return 1;
1902 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1903 return 1;
1904 return 0;
1905}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906EXPORT_SYMBOL(inode_needs_sync);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07001909 * If we try to find an inode in the inode hash while it is being
1910 * deleted, we have to wait until the filesystem completes its
1911 * deletion before reporting that it isn't found. This function waits
1912 * until the deletion _might_ have completed. Callers are responsible
1913 * to recheck inode state.
1914 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001915 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11001916 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1917 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 */
1919static void __wait_on_freeing_inode(struct inode *inode)
1920{
1921 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001922 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1923 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Ingo Molnar21417132017-03-05 11:25:39 +01001924 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001925 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001926 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 schedule();
Ingo Molnar21417132017-03-05 11:25:39 +01001928 finish_wait(wq, &wait.wq_entry);
Dave Chinner67a23c42011-03-22 22:23:42 +11001929 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932static __initdata unsigned long ihash_entries;
1933static int __init set_ihash_entries(char *str)
1934{
1935 if (!str)
1936 return 0;
1937 ihash_entries = simple_strtoul(str, &str, 0);
1938 return 1;
1939}
1940__setup("ihash_entries=", set_ihash_entries);
1941
1942/*
1943 * Initialize the waitqueues and inode hash table.
1944 */
1945void __init inode_init_early(void)
1946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* If hashes are distributed across NUMA nodes, defer
1948 * hash allocation until vmalloc space is available.
1949 */
1950 if (hashdist)
1951 return;
1952
1953 inode_hashtable =
1954 alloc_large_system_hash("Inode-cache",
1955 sizeof(struct hlist_head),
1956 ihash_entries,
1957 14,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07001958 HASH_EARLY | HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 &i_hash_shift,
1960 &i_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00001961 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Denis Cheng74bf17c2007-10-16 23:26:30 -07001965void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08001968 inode_cachep = kmem_cache_create("inode_cache",
1969 sizeof(struct inode),
1970 0,
1971 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
Vladimir Davydov5d097052016-01-14 15:18:21 -08001972 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09001973 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Hash may have been set up in inode_init_early */
1976 if (!hashdist)
1977 return;
1978
1979 inode_hashtable =
1980 alloc_large_system_hash("Inode-cache",
1981 sizeof(struct hlist_head),
1982 ihash_entries,
1983 14,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07001984 HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 &i_hash_shift,
1986 &i_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00001987 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1992{
1993 inode->i_mode = mode;
1994 if (S_ISCHR(mode)) {
1995 inode->i_fop = &def_chr_fops;
1996 inode->i_rdev = rdev;
1997 } else if (S_ISBLK(mode)) {
1998 inode->i_fop = &def_blk_fops;
1999 inode->i_rdev = rdev;
2000 } else if (S_ISFIFO(mode))
Al Viro599a0ac2013-03-12 09:58:10 -04002001 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 else if (S_ISSOCK(mode))
Al Virobd9b51e2014-11-18 23:38:21 -05002003 ; /* leave it no_open_fops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07002005 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
2006 " inode %s:%lu\n", mode, inode->i_sb->s_id,
2007 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002010
2011/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00002012 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002013 * @inode: New inode
2014 * @dir: Directory inode
2015 * @mode: mode of the new inode
2016 */
2017void inode_init_owner(struct inode *inode, const struct inode *dir,
Al Viro62bb1092011-07-24 23:20:18 -04002018 umode_t mode)
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002019{
2020 inode->i_uid = current_fsuid();
2021 if (dir && dir->i_mode & S_ISGID) {
2022 inode->i_gid = dir->i_gid;
Linus Torvalds0fa3ecd2018-07-03 17:10:19 -07002023
2024 /* Directories are special, and always inherit S_ISGID */
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002025 if (S_ISDIR(mode))
2026 mode |= S_ISGID;
Linus Torvalds0fa3ecd2018-07-03 17:10:19 -07002027 else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
2028 !in_group_p(inode->i_gid) &&
2029 !capable_wrt_inode_uidgid(dir, CAP_FSETID))
2030 mode &= ~S_ISGID;
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002031 } else
2032 inode->i_gid = current_fsgid();
2033 inode->i_mode = mode;
2034}
2035EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07002036
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002037/**
2038 * inode_owner_or_capable - check current task permissions to inode
2039 * @inode: inode being checked
2040 *
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002041 * Return true if current either has CAP_FOWNER in a namespace with the
2042 * inode owner uid mapped, or owns the file.
Serge E. Hallyne795b712011-03-23 16:43:25 -07002043 */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002044bool inode_owner_or_capable(const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07002045{
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002046 struct user_namespace *ns;
2047
Eric W. Biederman92361632012-02-08 07:07:50 -08002048 if (uid_eq(current_fsuid(), inode->i_uid))
Serge E. Hallyne795b712011-03-23 16:43:25 -07002049 return true;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002050
2051 ns = current_user_ns();
Kees Cookcc658db2017-06-21 09:53:06 -07002052 if (kuid_has_mapping(ns, inode->i_uid) && ns_capable(ns, CAP_FOWNER))
Serge E. Hallyne795b712011-03-23 16:43:25 -07002053 return true;
2054 return false;
2055}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002056EXPORT_SYMBOL(inode_owner_or_capable);
Trond Myklebust1d59d612012-05-31 12:22:33 -04002057
2058/*
2059 * Direct i/o helper functions
2060 */
2061static void __inode_dio_wait(struct inode *inode)
2062{
2063 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
2064 DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
2065
2066 do {
Ingo Molnar21417132017-03-05 11:25:39 +01002067 prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE);
Trond Myklebust1d59d612012-05-31 12:22:33 -04002068 if (atomic_read(&inode->i_dio_count))
2069 schedule();
2070 } while (atomic_read(&inode->i_dio_count));
Ingo Molnar21417132017-03-05 11:25:39 +01002071 finish_wait(wq, &q.wq_entry);
Trond Myklebust1d59d612012-05-31 12:22:33 -04002072}
2073
2074/**
2075 * inode_dio_wait - wait for outstanding DIO requests to finish
2076 * @inode: inode to wait for
2077 *
2078 * Waits for all pending direct I/O requests to finish so that we can
2079 * proceed with a truncate or equivalent operation.
2080 *
2081 * Must be called under a lock that serializes taking new references
2082 * to i_dio_count, usually by inode->i_mutex.
2083 */
2084void inode_dio_wait(struct inode *inode)
2085{
2086 if (atomic_read(&inode->i_dio_count))
2087 __inode_dio_wait(inode);
2088}
2089EXPORT_SYMBOL(inode_dio_wait);
2090
2091/*
Theodore Ts'o5f16f322014-03-24 14:43:12 -04002092 * inode_set_flags - atomically set some inode flags
2093 *
2094 * Note: the caller should be holding i_mutex, or else be sure that
2095 * they have exclusive access to the inode structure (i.e., while the
2096 * inode is being instantiated). The reason for the cmpxchg() loop
2097 * --- which wouldn't be necessary if all code paths which modify
2098 * i_flags actually followed this rule, is that there is at least one
Jan Kara5fa8e0a2015-05-21 16:05:53 +02002099 * code path which doesn't today so we use cmpxchg() out of an abundance
2100 * of caution.
Theodore Ts'o5f16f322014-03-24 14:43:12 -04002101 *
2102 * In the long run, i_mutex is overkill, and we should probably look
2103 * at using the i_lock spinlock to protect i_flags, and then make sure
2104 * it is so documented in include/linux/fs.h and that all code follows
2105 * the locking convention!!
2106 */
2107void inode_set_flags(struct inode *inode, unsigned int flags,
2108 unsigned int mask)
2109{
2110 unsigned int old_flags, new_flags;
2111
2112 WARN_ON_ONCE(flags & ~mask);
2113 do {
Mark Rutland6aa7de02017-10-23 14:07:29 -07002114 old_flags = READ_ONCE(inode->i_flags);
Theodore Ts'o5f16f322014-03-24 14:43:12 -04002115 new_flags = (old_flags & ~mask) | flags;
2116 } while (unlikely(cmpxchg(&inode->i_flags, old_flags,
2117 new_flags) != old_flags));
2118}
2119EXPORT_SYMBOL(inode_set_flags);
Al Viro21fc61c2015-11-17 01:07:57 -05002120
2121void inode_nohighmem(struct inode *inode)
2122{
2123 mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
2124}
2125EXPORT_SYMBOL(inode_nohighmem);
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002126
2127/**
Deepa Dinamani8efd6892018-04-22 20:18:46 -07002128 * timespec64_trunc - Truncate timespec64 to a granularity
2129 * @t: Timespec64
2130 * @gran: Granularity in ns.
2131 *
2132 * Truncate a timespec64 to a granularity. Always rounds down. gran must
2133 * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
2134 */
2135struct timespec64 timespec64_trunc(struct timespec64 t, unsigned gran)
2136{
2137 /* Avoid division in the common cases 1 ns and 1 s. */
2138 if (gran == 1) {
2139 /* nothing */
2140 } else if (gran == NSEC_PER_SEC) {
2141 t.tv_nsec = 0;
2142 } else if (gran > 1 && gran < NSEC_PER_SEC) {
2143 t.tv_nsec -= t.tv_nsec % gran;
2144 } else {
2145 WARN(1, "illegal file time granularity: %u", gran);
2146 }
2147 return t;
2148}
2149EXPORT_SYMBOL(timespec64_trunc);
2150
2151/**
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002152 * current_time - Return FS time
2153 * @inode: inode.
2154 *
2155 * Return the current time truncated to the time granularity supported by
2156 * the fs.
2157 *
2158 * Note that inode and inode->sb cannot be NULL.
2159 * Otherwise, the function warns and returns time without truncation.
2160 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07002161struct timespec64 current_time(struct inode *inode)
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002162{
Deepa Dinamani95582b02018-05-08 19:36:02 -07002163 struct timespec64 now = current_kernel_time64();
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002164
2165 if (unlikely(!inode->i_sb)) {
2166 WARN(1, "current_time() called with uninitialized super_block in the inode");
2167 return now;
2168 }
2169
Deepa Dinamani95582b02018-05-08 19:36:02 -07002170 return timespec64_trunc(now, inode->i_sb->s_time_gran);
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002171}
2172EXPORT_SYMBOL(current_time);
Eric Biggersf62199a2019-07-01 13:26:28 -07002173
2174/*
2175 * Generic function to check FS_IOC_SETFLAGS values and reject any invalid
2176 * configurations.
2177 *
2178 * Note: the caller should be holding i_mutex, or else be sure that they have
2179 * exclusive access to the inode structure.
2180 */
2181int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
2182 unsigned int flags)
2183{
2184 /*
2185 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
2186 * the relevant capability.
2187 *
2188 * This test looks nicer. Thanks to Pauline Middelink
2189 */
2190 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) &&
2191 !capable(CAP_LINUX_IMMUTABLE))
2192 return -EPERM;
2193
Daniel Rosenbergf4951342020-01-20 14:31:56 -08002194 return fscrypt_prepare_setflags(inode, oldflags, flags);
Eric Biggersf62199a2019-07-01 13:26:28 -07002195}
2196EXPORT_SYMBOL(vfs_ioc_setflags_prepare);
Eric Biggerse8314182019-07-01 13:26:29 -07002197
2198/*
2199 * Generic function to check FS_IOC_FSSETXATTR values and reject any invalid
2200 * configurations.
2201 *
2202 * Note: the caller should be holding i_mutex, or else be sure that they have
2203 * exclusive access to the inode structure.
2204 */
2205int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa,
2206 struct fsxattr *fa)
2207{
2208 /*
2209 * Can't modify an immutable/append-only file unless we have
2210 * appropriate permission.
2211 */
2212 if ((old_fa->fsx_xflags ^ fa->fsx_xflags) &
2213 (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND) &&
2214 !capable(CAP_LINUX_IMMUTABLE))
2215 return -EPERM;
2216
2217 /*
2218 * Project Quota ID state is only allowed to change from within the init
2219 * namespace. Enforce that restriction only if we are trying to change
2220 * the quota ID state. Everything else is allowed in user namespaces.
2221 */
2222 if (current_user_ns() != &init_user_ns) {
2223 if (old_fa->fsx_projid != fa->fsx_projid)
2224 return -EINVAL;
2225 if ((old_fa->fsx_xflags ^ fa->fsx_xflags) &
2226 FS_XFLAG_PROJINHERIT)
2227 return -EINVAL;
2228 }
2229
2230 /* Check extent size hints. */
2231 if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(inode->i_mode))
2232 return -EINVAL;
2233
2234 if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
2235 !S_ISDIR(inode->i_mode))
2236 return -EINVAL;
2237
2238 if ((fa->fsx_xflags & FS_XFLAG_COWEXTSIZE) &&
2239 !S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
2240 return -EINVAL;
2241
2242 /*
2243 * It is only valid to set the DAX flag on regular files and
2244 * directories on filesystems.
2245 */
2246 if ((fa->fsx_xflags & FS_XFLAG_DAX) &&
2247 !(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
2248 return -EINVAL;
2249
2250 /* Extent size hints of zero turn off the flags. */
2251 if (fa->fsx_extsize == 0)
2252 fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
2253 if (fa->fsx_cowextsize == 0)
2254 fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
2255
2256 return 0;
2257}
2258EXPORT_SYMBOL(vfs_ioc_fssetxattr_check);