blob: edcd8a61975f34c7a4cf467589848870430b3b8a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * (C) 1997 Linus Torvalds
Christoph Hellwig4b4563d2011-05-27 09:28:01 -04004 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Al Viroe59cc472011-12-07 13:17:19 -05006#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/fs.h>
Jeff Layton5970e152022-11-20 09:15:34 -05008#include <linux/filelock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/hash.h>
12#include <linux/swap.h>
13#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/cdev.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070015#include <linux/memblock.h>
Eric Paris3be25f42009-05-21 17:01:26 -040016#include <linux/fsnotify.h>
Christoph Hellwigfc33a7b2006-01-09 20:52:17 -080017#include <linux/mount.h>
Al Virof19d4a8f2009-06-08 19:50:45 -040018#include <linux/posix_acl.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:
Jchao Sun10e14072022-05-24 08:05:40 -070030 * inode->i_state, inode->i_hash, __iget(), inode->i_io_list
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 Chinner74278da92015-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 Chinner74278da92015-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 Chinner74278da92015-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
Alexey Dobriyan68279f92023-10-11 19:55:00 +030057static unsigned int i_hash_mask __ro_after_init;
58static unsigned int i_hash_shift __ro_after_init;
59static struct hlist_head *inode_hashtable __ro_after_init;
Dave Chinner67a23c42011-03-22 22:23:42 +110060static __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
Glauber Costa3942c072013-08-28 10:17:53 +100070static DEFINE_PER_CPU(unsigned long, nr_inodes);
71static DEFINE_PER_CPU(unsigned long, nr_unused);
Dave Chinnercffbc8a2010-10-23 05:03:02 -040072
Alexey Dobriyan68279f92023-10-11 19:55:00 +030073static struct kmem_cache *inode_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Glauber Costa3942c072013-08-28 10:17:53 +100075static long get_nr_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040076{
Nick Piggin3e880fb2011-01-07 17:49:19 +110077 int i;
Glauber Costa3942c072013-08-28 10:17:53 +100078 long sum = 0;
Nick Piggin3e880fb2011-01-07 17:49:19 +110079 for_each_possible_cpu(i)
80 sum += per_cpu(nr_inodes, i);
81 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -040082}
83
Glauber Costa3942c072013-08-28 10:17:53 +100084static inline long get_nr_inodes_unused(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040085{
Dave Chinnerfcb94f72011-07-08 14:14:38 +100086 int i;
Glauber Costa3942c072013-08-28 10:17:53 +100087 long sum = 0;
Dave Chinnerfcb94f72011-07-08 14:14:38 +100088 for_each_possible_cpu(i)
89 sum += per_cpu(nr_unused, i);
90 return sum < 0 ? 0 : sum;
Dave Chinnercffbc8a2010-10-23 05:03:02 -040091}
92
Glauber Costa3942c072013-08-28 10:17:53 +100093long get_nr_dirty_inodes(void)
Dave Chinnercffbc8a2010-10-23 05:03:02 -040094{
Nick Piggin3e880fb2011-01-07 17:49:19 +110095 /* not actually dirty inodes, but a wild approximation */
Glauber Costa3942c072013-08-28 10:17:53 +100096 long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
Dave Chinnercffbc8a2010-10-23 05:03:02 -040097 return nr_dirty > 0 ? nr_dirty : 0;
Dave Chinnercffbc8a2010-10-23 05:03:02 -040098}
99
100/*
101 * Handle nr_inode sysctl
102 */
103#ifdef CONFIG_SYSCTL
Luis Chamberlain1d67fe52022-01-21 22:12:52 -0800104/*
105 * Statistics gathering..
106 */
107static struct inodes_stat_t inodes_stat;
108
109static int proc_nr_inodes(struct ctl_table *table, int write, void *buffer,
110 size_t *lenp, loff_t *ppos)
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400111{
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}
Luis Chamberlain1d67fe52022-01-21 22:12:52 -0800116
117static struct ctl_table inodes_sysctls[] = {
118 {
119 .procname = "inode-nr",
120 .data = &inodes_stat,
121 .maxlen = 2*sizeof(long),
122 .mode = 0444,
123 .proc_handler = proc_nr_inodes,
124 },
125 {
126 .procname = "inode-state",
127 .data = &inodes_stat,
128 .maxlen = 7*sizeof(long),
129 .mode = 0444,
130 .proc_handler = proc_nr_inodes,
131 },
132 { }
133};
134
135static int __init init_fs_inode_sysctls(void)
136{
137 register_sysctl_init("fs", inodes_sysctls);
138 return 0;
139}
140early_initcall(init_fs_inode_sysctls);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400141#endif
142
Al Virobd9b51e2014-11-18 23:38:21 -0500143static int no_open(struct inode *inode, struct file *file)
144{
145 return -ENXIO;
146}
147
David Chinner2cb15992008-10-30 17:32:23 +1100148/**
Masahiro Yamada6e7c2b42017-05-08 15:57:53 -0700149 * inode_init_always - perform inode structure initialisation
Randy Dunlap0bc02f32009-01-06 14:41:13 -0800150 * @sb: superblock inode belongs to
151 * @inode: inode to initialise
David Chinner2cb15992008-10-30 17:32:23 +1100152 *
153 * These are initializations that need to be done on every inode
154 * allocation as the fields are not initialised by slab allocation.
155 */
Christoph Hellwig54e34622009-08-07 14:38:25 -0300156int inode_init_always(struct super_block *sb, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700158 static const struct inode_operations empty_iops;
Al Virobd9b51e2014-11-18 23:38:21 -0500159 static const struct file_operations no_open_fops = {.open = no_open};
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530160 struct address_space *const mapping = &inode->i_data;
David Chinner2cb15992008-10-30 17:32:23 +1100161
162 inode->i_sb = sb;
163 inode->i_blkbits = sb->s_blocksize_bits;
164 inode->i_flags = 0;
Peter Zijlstra8019ad12020-03-04 11:28:31 +0100165 atomic64_set(&inode->i_sequence, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100166 atomic_set(&inode->i_count, 1);
167 inode->i_op = &empty_iops;
Al Virobd9b51e2014-11-18 23:38:21 -0500168 inode->i_fop = &no_open_fops;
Eric Biggersedbb35c2020-10-30 17:44:20 -0700169 inode->i_ino = 0;
Miklos Szeredia78ef702011-10-28 14:13:30 +0200170 inode->__i_nlink = 1;
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700171 inode->i_opflags = 0;
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200172 if (sb->s_xattr)
173 inode->i_opflags |= IOP_XATTR;
Eric W. Biederman92361632012-02-08 07:07:50 -0800174 i_uid_write(inode, 0);
175 i_gid_write(inode, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100176 atomic_set(&inode->i_writecount, 0);
177 inode->i_size = 0;
Jens Axboec75b1d92017-06-27 11:47:04 -0600178 inode->i_write_hint = WRITE_LIFE_NOT_SET;
David Chinner2cb15992008-10-30 17:32:23 +1100179 inode->i_blocks = 0;
180 inode->i_bytes = 0;
181 inode->i_generation = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100182 inode->i_pipe = NULL;
David Chinner2cb15992008-10-30 17:32:23 +1100183 inode->i_cdev = NULL;
Al Viro61ba64f2015-05-02 09:54:06 -0400184 inode->i_link = NULL;
Al Viro84e710d2016-04-15 00:58:55 -0400185 inode->i_dir_seq = 0;
David Chinner2cb15992008-10-30 17:32:23 +1100186 inode->i_rdev = 0;
187 inode->dirtied_when = 0;
Mimi Zohar6146f0d2009-02-04 09:06:57 -0500188
Tahsin Erdogan3d65ae42016-02-16 13:34:39 -0800189#ifdef CONFIG_CGROUP_WRITEBACK
190 inode->i_wb_frn_winner = 0;
191 inode->i_wb_frn_avg_time = 0;
192 inode->i_wb_frn_history = 0;
193#endif
194
David Chinner2cb15992008-10-30 17:32:23 +1100195 spin_lock_init(&inode->i_lock);
196 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
197
Al Viro9902af72016-04-15 15:08:36 -0400198 init_rwsem(&inode->i_rwsem);
199 lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key);
David Chinner2cb15992008-10-30 17:32:23 +1100200
Christoph Hellwigbd5fe6c2011-06-24 14:29:43 -0400201 atomic_set(&inode->i_dio_count, 0);
David Chinner2cb15992008-10-30 17:32:23 +1100202
203 mapping->a_ops = &empty_aops;
204 mapping->host = inode;
205 mapping->flags = 0;
Darrick J. Wong829bc7872018-05-30 19:43:53 -0700206 mapping->wb_err = 0;
David Herrmann4bb5f5d2014-08-08 14:25:25 -0700207 atomic_set(&mapping->i_mmap_writable, 0);
Song Liu09d91cd2019-09-23 15:38:03 -0700208#ifdef CONFIG_READ_ONLY_THP_FOR_FS
209 atomic_set(&mapping->nr_thps, 0);
210#endif
Hugh Dickins3c1d4372009-01-06 14:39:23 -0800211 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
Rafael Aquini252aa6f2012-12-11 16:02:35 -0800212 mapping->private_data = NULL;
David Chinner2cb15992008-10-30 17:32:23 +1100213 mapping->writeback_index = 0;
Sebastian Andrzej Siewior23ca0672021-09-01 10:44:03 +0200214 init_rwsem(&mapping->invalidate_lock);
215 lockdep_set_class_and_name(&mapping->invalidate_lock,
216 &sb->s_type->invalidate_lock_key,
217 "mapping.invalidate_lock");
David Chinner2cb15992008-10-30 17:32:23 +1100218 inode->i_private = NULL;
219 inode->i_mapping = mapping;
Al Virob3d9b7a2012-06-09 13:51:19 -0400220 INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */
Al Virof19d4a8f2009-06-08 19:50:45 -0400221#ifdef CONFIG_FS_POSIX_ACL
222 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
223#endif
David Chinner2cb15992008-10-30 17:32:23 +1100224
Eric Paris3be25f42009-05-21 17:01:26 -0400225#ifdef CONFIG_FSNOTIFY
226 inode->i_fsnotify_mask = 0;
227#endif
Jeff Layton4a075e32015-01-16 15:05:54 -0500228 inode->i_flctx = NULL;
Dongliang Mu2e488f12022-08-16 12:08:58 +0800229
230 if (unlikely(security_inode_alloc(inode)))
231 return -ENOMEM;
Nick Piggin3e880fb2011-01-07 17:49:19 +1100232 this_cpu_inc(nr_inodes);
Dave Chinnercffbc8a2010-10-23 05:03:02 -0400233
Christoph Hellwig54e34622009-08-07 14:38:25 -0300234 return 0;
David Chinner2cb15992008-10-30 17:32:23 +1100235}
236EXPORT_SYMBOL(inode_init_always);
237
Al Virofdb0da82019-04-10 14:43:44 -0400238void free_inode_nonrcu(struct inode *inode)
239{
240 kmem_cache_free(inode_cachep, inode);
241}
242EXPORT_SYMBOL(free_inode_nonrcu);
243
244static void i_callback(struct rcu_head *head)
245{
246 struct inode *inode = container_of(head, struct inode, i_rcu);
247 if (inode->free_inode)
248 inode->free_inode(inode);
249 else
250 free_inode_nonrcu(inode);
251}
252
David Chinner2cb15992008-10-30 17:32:23 +1100253static struct inode *alloc_inode(struct super_block *sb)
254{
Al Virofdb0da82019-04-10 14:43:44 -0400255 const struct super_operations *ops = sb->s_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct inode *inode;
257
Al Virofdb0da82019-04-10 14:43:44 -0400258 if (ops->alloc_inode)
259 inode = ops->alloc_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 else
Muchun Song8b9f3ac2022-03-22 14:41:00 -0700261 inode = alloc_inode_sb(sb, inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Christoph Hellwig54e34622009-08-07 14:38:25 -0300263 if (!inode)
264 return NULL;
265
266 if (unlikely(inode_init_always(sb, inode))) {
Al Virofdb0da82019-04-10 14:43:44 -0400267 if (ops->destroy_inode) {
268 ops->destroy_inode(inode);
269 if (!ops->free_inode)
270 return NULL;
271 }
272 inode->free_inode = ops->free_inode;
273 i_callback(&inode->i_rcu);
Christoph Hellwig54e34622009-08-07 14:38:25 -0300274 return NULL;
275 }
276
277 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300280void __destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Eric Sesterhennb7542f82006-04-02 13:38:18 +0200282 BUG_ON(inode_has_buffers(inode));
Tejun Heo52ebea72015-05-22 17:13:37 -0400283 inode_detach_wb(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 security_inode_free(inode);
Eric Paris3be25f42009-05-21 17:01:26 -0400285 fsnotify_inode_delete(inode);
Jeff Laytonf27a0fe2016-01-07 15:08:51 -0500286 locks_free_lock_context(inode);
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100287 if (!inode->i_nlink) {
288 WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
289 atomic_long_dec(&inode->i_sb->s_remove_count);
290 }
291
Al Virof19d4a8f2009-06-08 19:50:45 -0400292#ifdef CONFIG_FS_POSIX_ACL
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100293 if (inode->i_acl && !is_uncached_acl(inode->i_acl))
Al Virof19d4a8f2009-06-08 19:50:45 -0400294 posix_acl_release(inode->i_acl);
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100295 if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl))
Al Virof19d4a8f2009-06-08 19:50:45 -0400296 posix_acl_release(inode->i_default_acl);
297#endif
Nick Piggin3e880fb2011-01-07 17:49:19 +1100298 this_cpu_dec(nr_inodes);
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300299}
300EXPORT_SYMBOL(__destroy_inode);
301
Christoph Hellwig56b0dac2010-10-06 10:48:55 +0200302static void destroy_inode(struct inode *inode)
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300303{
Al Virofdb0da82019-04-10 14:43:44 -0400304 const struct super_operations *ops = inode->i_sb->s_op;
305
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100306 BUG_ON(!list_empty(&inode->i_lru));
Christoph Hellwig2e00c972009-08-07 14:38:29 -0300307 __destroy_inode(inode);
Al Virofdb0da82019-04-10 14:43:44 -0400308 if (ops->destroy_inode) {
309 ops->destroy_inode(inode);
310 if (!ops->free_inode)
311 return;
312 }
313 inode->free_inode = ops->free_inode;
314 call_rcu(&inode->i_rcu, i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100317/**
318 * drop_nlink - directly drop an inode's link count
319 * @inode: inode
320 *
321 * This is a low-level filesystem helper to replace any
322 * direct filesystem manipulation of i_nlink. In cases
323 * where we are attempting to track writes to the
324 * filesystem, a decrement to zero means an imminent
325 * write when the file is truncated and actually unlinked
326 * on the filesystem.
327 */
328void drop_nlink(struct inode *inode)
329{
330 WARN_ON(inode->i_nlink == 0);
331 inode->__i_nlink--;
332 if (!inode->i_nlink)
333 atomic_long_inc(&inode->i_sb->s_remove_count);
334}
335EXPORT_SYMBOL(drop_nlink);
336
337/**
338 * clear_nlink - directly zero an inode's link count
339 * @inode: inode
340 *
341 * This is a low-level filesystem helper to replace any
342 * direct filesystem manipulation of i_nlink. See
343 * drop_nlink() for why we care about i_nlink hitting zero.
344 */
345void clear_nlink(struct inode *inode)
346{
347 if (inode->i_nlink) {
348 inode->__i_nlink = 0;
349 atomic_long_inc(&inode->i_sb->s_remove_count);
350 }
351}
352EXPORT_SYMBOL(clear_nlink);
353
354/**
355 * set_nlink - directly set an inode's link count
356 * @inode: inode
357 * @nlink: new nlink (should be non-zero)
358 *
359 * This is a low-level filesystem helper to replace any
360 * direct filesystem manipulation of i_nlink.
361 */
362void set_nlink(struct inode *inode, unsigned int nlink)
363{
364 if (!nlink) {
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100365 clear_nlink(inode);
366 } else {
367 /* Yes, some filesystems do change nlink from zero to one */
368 if (inode->i_nlink == 0)
369 atomic_long_dec(&inode->i_sb->s_remove_count);
370
371 inode->__i_nlink = nlink;
372 }
373}
374EXPORT_SYMBOL(set_nlink);
375
376/**
377 * inc_nlink - directly increment an inode's link count
378 * @inode: inode
379 *
380 * This is a low-level filesystem helper to replace any
381 * direct filesystem manipulation of i_nlink. Currently,
382 * it is only here for parity with dec_nlink().
383 */
384void inc_nlink(struct inode *inode)
385{
Al Virof4e0c302013-06-11 08:34:36 +0400386 if (unlikely(inode->i_nlink == 0)) {
387 WARN_ON(!(inode->i_state & I_LINKABLE));
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100388 atomic_long_dec(&inode->i_sb->s_remove_count);
Al Virof4e0c302013-06-11 08:34:36 +0400389 }
Miklos Szeredi7ada4db2011-11-21 12:11:32 +0100390
391 inode->__i_nlink++;
392}
393EXPORT_SYMBOL(inc_nlink);
394
Dave Chinnerae233952018-03-06 17:30:34 -0800395static void __address_space_init_once(struct address_space *mapping)
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100396{
Johannes Weiner7b785642019-05-24 10:12:46 -0400397 xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -0800398 init_rwsem(&mapping->i_mmap_rwsem);
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100399 INIT_LIST_HEAD(&mapping->private_list);
400 spin_lock_init(&mapping->private_lock);
Davidlohr Buesof808c132017-09-08 16:15:08 -0700401 mapping->i_mmap = RB_ROOT_CACHED;
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100402}
Dave Chinnerae233952018-03-06 17:30:34 -0800403
404void address_space_init_once(struct address_space *mapping)
405{
406 memset(mapping, 0, sizeof(*mapping));
407 __address_space_init_once(mapping);
408}
Miklos Szeredi2aa15892011-02-23 13:49:47 +0100409EXPORT_SYMBOL(address_space_init_once);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/*
412 * These are initializations that only need to be done
413 * once, because the fields are idempotent across use
414 * of the inode, so let the slab aware of that.
415 */
416void inode_init_once(struct inode *inode)
417{
418 memset(inode, 0, sizeof(*inode));
419 INIT_HLIST_NODE(&inode->i_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 INIT_LIST_HEAD(&inode->i_devices);
Dave Chinnerc7f54082015-03-04 14:07:22 -0500421 INIT_LIST_HEAD(&inode->i_io_list);
Dave Chinner6c60d2b2016-07-26 15:21:50 -0700422 INIT_LIST_HEAD(&inode->i_wb_list);
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100423 INIT_LIST_HEAD(&inode->i_lru);
Jeff Layton18cc9122022-03-31 16:29:00 -0400424 INIT_LIST_HEAD(&inode->i_sb_list);
Dave Chinnerae233952018-03-06 17:30:34 -0800425 __address_space_init_once(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 i_size_ordered_init(inode);
427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428EXPORT_SYMBOL(inode_init_once);
429
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700430static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530432 struct inode *inode = (struct inode *) foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Christoph Lametera35afb82007-05-16 22:10:57 -0700434 inode_init_once(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/*
Dave Chinner250df6e2011-03-22 22:23:36 +1100438 * inode->i_lock must be held
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530440void __iget(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Nick Piggin9e38d862010-10-23 06:55:17 -0400442 atomic_inc(&inode->i_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Al Viro7de9c6ee2010-10-23 11:11:40 -0400445/*
446 * get additional reference to inode; caller must already hold one.
447 */
448void ihold(struct inode *inode)
449{
450 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
451}
452EXPORT_SYMBOL(ihold);
453
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800454static void __inode_add_lru(struct inode *inode, bool rotate)
Nick Piggin9e38d862010-10-23 06:55:17 -0400455{
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800456 if (inode->i_state & (I_DIRTY_ALL | I_SYNC | I_FREEING | I_WILL_FREE))
457 return;
458 if (atomic_read(&inode->i_count))
459 return;
460 if (!(inode->i_sb->s_flags & SB_ACTIVE))
461 return;
462 if (!mapping_shrinkable(&inode->i_data))
463 return;
464
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000465 if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru))
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000466 this_cpu_inc(nr_unused);
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800467 else if (rotate)
Josef Bacik563f4002017-04-18 16:04:17 -0400468 inode->i_state |= I_REFERENCED;
Nick Piggin9e38d862010-10-23 06:55:17 -0400469}
470
Jan Kara4eff96d2012-11-26 16:29:51 -0800471/*
472 * Add inode to LRU if needed (inode is unused and clean).
473 *
474 * Needs inode->i_lock held.
475 */
476void inode_add_lru(struct inode *inode)
477{
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800478 __inode_add_lru(inode, false);
Jan Kara4eff96d2012-11-26 16:29:51 -0800479}
480
Nick Piggin9e38d862010-10-23 06:55:17 -0400481static void inode_lru_list_del(struct inode *inode)
482{
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000483 if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru))
Dave Chinnerfcb94f72011-07-08 14:14:38 +1000484 this_cpu_dec(nr_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Christoph Hellwig646ec462010-10-23 07:15:32 -0400487/**
488 * inode_sb_list_add - add inode to the superblock list of inodes
489 * @inode: inode to add
490 */
491void inode_sb_list_add(struct inode *inode)
492{
Dave Chinner74278da92015-03-04 12:37:22 -0500493 spin_lock(&inode->i_sb->s_inode_list_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +1100494 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
Dave Chinner74278da92015-03-04 12:37:22 -0500495 spin_unlock(&inode->i_sb->s_inode_list_lock);
Christoph Hellwig646ec462010-10-23 07:15:32 -0400496}
497EXPORT_SYMBOL_GPL(inode_sb_list_add);
498
Dave Chinner55fa6092011-03-22 22:23:40 +1100499static inline void inode_sb_list_del(struct inode *inode)
Christoph Hellwig646ec462010-10-23 07:15:32 -0400500{
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200501 if (!list_empty(&inode->i_sb_list)) {
Dave Chinner74278da92015-03-04 12:37:22 -0500502 spin_lock(&inode->i_sb->s_inode_list_lock);
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200503 list_del_init(&inode->i_sb_list);
Dave Chinner74278da92015-03-04 12:37:22 -0500504 spin_unlock(&inode->i_sb->s_inode_list_lock);
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200505 }
Christoph Hellwig646ec462010-10-23 07:15:32 -0400506}
507
Dave Chinner4c51acb2010-10-23 06:58:09 -0400508static unsigned long hash(struct super_block *sb, unsigned long hashval)
509{
510 unsigned long tmp;
511
512 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
513 L1_CACHE_BYTES;
Christoph Hellwig4b4563d2011-05-27 09:28:01 -0400514 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
515 return tmp & i_hash_mask;
Dave Chinner4c51acb2010-10-23 06:58:09 -0400516}
517
518/**
519 * __insert_inode_hash - hash an inode
520 * @inode: unhashed inode
521 * @hashval: unsigned long value used to locate this object in the
522 * inode_hashtable.
523 *
524 * Add an inode to the inode hash for this superblock.
525 */
526void __insert_inode_hash(struct inode *inode, unsigned long hashval)
527{
Christoph Hellwig646ec462010-10-23 07:15:32 -0400528 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
529
Dave Chinner67a23c42011-03-22 22:23:42 +1100530 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100531 spin_lock(&inode->i_lock);
David Howells3f19b2a2017-12-01 11:40:16 +0000532 hlist_add_head_rcu(&inode->i_hash, b);
Dave Chinner250df6e2011-03-22 22:23:36 +1100533 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100534 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400535}
536EXPORT_SYMBOL(__insert_inode_hash);
537
538/**
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200539 * __remove_inode_hash - remove an inode from the hash
Dave Chinner4c51acb2010-10-23 06:58:09 -0400540 * @inode: inode to unhash
541 *
542 * Remove an inode from the superblock.
543 */
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200544void __remove_inode_hash(struct inode *inode)
Dave Chinner4c51acb2010-10-23 06:58:09 -0400545{
Dave Chinner67a23c42011-03-22 22:23:42 +1100546 spin_lock(&inode_hash_lock);
Dave Chinner250df6e2011-03-22 22:23:36 +1100547 spin_lock(&inode->i_lock);
David Howells3f19b2a2017-12-01 11:40:16 +0000548 hlist_del_init_rcu(&inode->i_hash);
Dave Chinner250df6e2011-03-22 22:23:36 +1100549 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +1100550 spin_unlock(&inode_hash_lock);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400551}
Eric Dumazetf2ee7ab2011-07-28 06:41:09 +0200552EXPORT_SYMBOL(__remove_inode_hash);
Dave Chinner4c51acb2010-10-23 06:58:09 -0400553
Matthew Wilcox (Oracle)3e9d80a82022-01-14 14:05:04 -0800554void dump_mapping(const struct address_space *mapping)
555{
556 struct inode *host;
557 const struct address_space_operations *a_ops;
558 struct hlist_node *dentry_first;
559 struct dentry *dentry_ptr;
560 struct dentry dentry;
561 unsigned long ino;
562
563 /*
564 * If mapping is an invalid pointer, we don't want to crash
565 * accessing it, so probe everything depending on it carefully.
566 */
567 if (get_kernel_nofault(host, &mapping->host) ||
568 get_kernel_nofault(a_ops, &mapping->a_ops)) {
569 pr_warn("invalid mapping:%px\n", mapping);
570 return;
571 }
572
573 if (!host) {
574 pr_warn("aops:%ps\n", a_ops);
575 return;
576 }
577
578 if (get_kernel_nofault(dentry_first, &host->i_dentry.first) ||
579 get_kernel_nofault(ino, &host->i_ino)) {
580 pr_warn("aops:%ps invalid inode:%px\n", a_ops, host);
581 return;
582 }
583
584 if (!dentry_first) {
585 pr_warn("aops:%ps ino:%lx\n", a_ops, ino);
586 return;
587 }
588
589 dentry_ptr = container_of(dentry_first, struct dentry, d_u.d_alias);
590 if (get_kernel_nofault(dentry, dentry_ptr)) {
591 pr_warn("aops:%ps ino:%lx invalid dentry:%px\n",
592 a_ops, ino, dentry_ptr);
593 return;
594 }
595
596 /*
597 * if dentry is corrupted, the %pd handler may still crash,
598 * but it's unlikely that we reach here with a corrupt mapping
599 */
600 pr_warn("aops:%ps ino:%lx dentry name:\"%pd\"\n", a_ops, ino, &dentry);
601}
602
Jan Karadbd57682012-05-03 14:48:02 +0200603void clear_inode(struct inode *inode)
Al Virob0683aa2010-06-04 20:55:25 -0400604{
Jan Kara08142572011-06-27 16:18:10 -0700605 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700606 * We have to cycle the i_pages lock here because reclaim can be in the
Matthew Wilcox (Oracle)6ffcd822022-06-28 20:41:40 -0400607 * process of removing the last page (in __filemap_remove_folio())
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700608 * and we must not free the mapping under it.
Jan Kara08142572011-06-27 16:18:10 -0700609 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700610 xa_lock_irq(&inode->i_data.i_pages);
Al Virob0683aa2010-06-04 20:55:25 -0400611 BUG_ON(inode->i_data.nrpages);
Hugh Dickins786b3112021-05-04 18:32:57 -0700612 /*
613 * Almost always, mapping_empty(&inode->i_data) here; but there are
614 * two known and long-standing ways in which nodes may get left behind
615 * (when deep radix-tree node allocation failed partway; or when THP
616 * collapse_file() failed). Until those two known cases are cleaned up,
617 * or a cleanup function is called here, do not BUG_ON(!mapping_empty),
618 * nor even WARN_ON(!mapping_empty).
619 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700620 xa_unlock_irq(&inode->i_data.i_pages);
Al Virob0683aa2010-06-04 20:55:25 -0400621 BUG_ON(!list_empty(&inode->i_data.private_list));
622 BUG_ON(!(inode->i_state & I_FREEING));
623 BUG_ON(inode->i_state & I_CLEAR);
Dave Chinner6c60d2b2016-07-26 15:21:50 -0700624 BUG_ON(!list_empty(&inode->i_wb_list));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100625 /* don't need i_lock here, no concurrent mods to i_state */
Al Virob0683aa2010-06-04 20:55:25 -0400626 inode->i_state = I_FREEING | I_CLEAR;
627}
Jan Karadbd57682012-05-03 14:48:02 +0200628EXPORT_SYMBOL(clear_inode);
Al Virob0683aa2010-06-04 20:55:25 -0400629
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100630/*
631 * Free the inode passed in, removing it from the lists it is still connected
632 * to. We remove any pages still attached to the inode and wait for any IO that
633 * is still in progress before finally destroying the inode.
634 *
635 * An inode must already be marked I_FREEING so that we avoid the inode being
636 * moved back onto lists if we race with other code that manipulates the lists
637 * (e.g. writeback_single_inode). The caller is responsible for setting this.
638 *
639 * An inode must already be removed from the LRU list before being evicted from
640 * the cache. This should occur atomically with setting the I_FREEING state
641 * flag, so no inodes here should ever be on the LRU when being evicted.
642 */
Al Viro644da592010-06-07 13:21:05 -0400643static void evict(struct inode *inode)
Al Virob4272d42010-06-04 19:33:20 -0400644{
645 const struct super_operations *op = inode->i_sb->s_op;
646
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100647 BUG_ON(!(inode->i_state & I_FREEING));
648 BUG_ON(!list_empty(&inode->i_lru));
649
Dave Chinnerc7f54082015-03-04 14:07:22 -0500650 if (!list_empty(&inode->i_io_list))
651 inode_io_list_del(inode);
Eric Dumazetb12362b2011-07-28 06:11:47 +0200652
Dave Chinner55fa6092011-03-22 22:23:40 +1100653 inode_sb_list_del(inode);
654
Jan Kara169ebd92012-05-03 14:48:03 +0200655 /*
656 * Wait for flusher thread to be done with the inode so that filesystem
657 * does not start destroying it while writeback is still running. Since
658 * the inode has I_FREEING set, flusher thread won't start new work on
659 * the inode. We just have to wait for running writeback to finish.
660 */
661 inode_wait_for_writeback(inode);
Jan Kara7994e6f2012-05-03 14:48:01 +0200662
Al Virobe7ce412010-06-04 19:40:39 -0400663 if (op->evict_inode) {
664 op->evict_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400665 } else {
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700666 truncate_inode_pages_final(&inode->i_data);
Jan Karadbd57682012-05-03 14:48:02 +0200667 clear_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400668 }
Al Viro661074e2010-06-04 20:19:55 -0400669 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
670 cd_forget(inode);
Dave Chinnerb2b2af82011-03-22 22:23:37 +1100671
672 remove_inode_hash(inode);
673
674 spin_lock(&inode->i_lock);
675 wake_up_bit(&inode->i_state, __I_NEW);
676 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
677 spin_unlock(&inode->i_lock);
678
679 destroy_inode(inode);
Al Virob4272d42010-06-04 19:33:20 -0400680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/*
683 * dispose_list - dispose of the contents of a local list
684 * @head: the head of the list to free
685 *
686 * Dispose-list gets a local list with local inodes in it, so it doesn't
687 * need to worry about list corruption and SMP locks.
688 */
689static void dispose_list(struct list_head *head)
690{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 while (!list_empty(head)) {
692 struct inode *inode;
693
Nick Piggin7ccf19a2010-10-21 11:49:30 +1100694 inode = list_first_entry(head, struct inode, i_lru);
695 list_del_init(&inode->i_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Al Viro644da592010-06-07 13:21:05 -0400697 evict(inode);
Josef Bacikac05fbb2015-03-04 16:52:52 -0500698 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/**
Al Viro63997e92010-10-25 20:49:35 -0400703 * evict_inodes - evict all evictable inodes for a superblock
704 * @sb: superblock to operate on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *
Al Viro63997e92010-10-25 20:49:35 -0400706 * Make sure that no inodes with zero refcount are retained. This is
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800707 * called by superblock shutdown after having SB_ACTIVE flag removed,
Al Viro63997e92010-10-25 20:49:35 -0400708 * so any inode reaching zero refcount during or after that call will
709 * be immediately evicted.
710 */
711void evict_inodes(struct super_block *sb)
712{
713 struct inode *inode, *next;
714 LIST_HEAD(dispose);
715
Josef Bacikac05fbb2015-03-04 16:52:52 -0500716again:
Dave Chinner74278da92015-03-04 12:37:22 -0500717 spin_lock(&sb->s_inode_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400718 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
719 if (atomic_read(&inode->i_count))
720 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100721
722 spin_lock(&inode->i_lock);
723 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
724 spin_unlock(&inode->i_lock);
Al Viro63997e92010-10-25 20:49:35 -0400725 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100726 }
Al Viro63997e92010-10-25 20:49:35 -0400727
728 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100729 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100730 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100731 list_add(&inode->i_lru, &dispose);
Josef Bacikac05fbb2015-03-04 16:52:52 -0500732
733 /*
734 * We can have a ton of inodes to evict at unmount time given
735 * enough memory, check to see if we need to go to sleep for a
736 * bit so we don't livelock.
737 */
738 if (need_resched()) {
739 spin_unlock(&sb->s_inode_list_lock);
740 cond_resched();
741 dispose_list(&dispose);
742 goto again;
743 }
Al Viro63997e92010-10-25 20:49:35 -0400744 }
Dave Chinner74278da92015-03-04 12:37:22 -0500745 spin_unlock(&sb->s_inode_list_lock);
Al Viro63997e92010-10-25 20:49:35 -0400746
747 dispose_list(&dispose);
Al Viro63997e92010-10-25 20:49:35 -0400748}
Darrick J. Wong799ea9e2017-08-18 18:08:25 -0700749EXPORT_SYMBOL_GPL(evict_inodes);
Al Viro63997e92010-10-25 20:49:35 -0400750
751/**
Christoph Hellwiga0318782010-10-24 19:40:33 +0200752 * invalidate_inodes - attempt to free all inodes on a superblock
753 * @sb: superblock to operate on
754 *
Christoph Hellwige127b9b2023-08-11 12:08:28 +0200755 * Attempts to free all inodes (including dirty inodes) for a given superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
Christoph Hellwige127b9b2023-08-11 12:08:28 +0200757void invalidate_inodes(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Christoph Hellwiga0318782010-10-24 19:40:33 +0200759 struct inode *inode, *next;
760 LIST_HEAD(dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Eric Sandeen04646ae2019-12-06 10:54:23 -0600762again:
Dave Chinner74278da92015-03-04 12:37:22 -0500763 spin_lock(&sb->s_inode_list_lock);
Christoph Hellwiga0318782010-10-24 19:40:33 +0200764 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100765 spin_lock(&inode->i_lock);
766 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
767 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100769 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200770 if (atomic_read(&inode->i_count)) {
Dave Chinner250df6e2011-03-22 22:23:36 +1100771 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 continue;
773 }
Christoph Hellwig99a38912010-10-23 19:07:20 +0200774
Christoph Hellwig99a38912010-10-23 19:07:20 +0200775 inode->i_state |= I_FREEING;
Dave Chinner02afc412011-03-22 22:23:38 +1100776 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100777 spin_unlock(&inode->i_lock);
Dave Chinner02afc412011-03-22 22:23:38 +1100778 list_add(&inode->i_lru, &dispose);
Eric Sandeen04646ae2019-12-06 10:54:23 -0600779 if (need_resched()) {
780 spin_unlock(&sb->s_inode_list_lock);
781 cond_resched();
782 dispose_list(&dispose);
783 goto again;
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Dave Chinner74278da92015-03-04 12:37:22 -0500786 spin_unlock(&sb->s_inode_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Christoph Hellwiga0318782010-10-24 19:40:33 +0200788 dispose_list(&dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000792 * Isolate the inode from the LRU in preparation for freeing it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *
Nick Piggin9e38d862010-10-23 06:55:17 -0400794 * If the inode has the I_REFERENCED flag set, then it means that it has been
795 * used recently - the flag is set in iput_final(). When we encounter such an
796 * inode, clear the flag and move it to the back of the LRU so it gets another
797 * pass through the LRU before it gets reclaimed. This is necessary because of
798 * the fact we are doing lazy LRU updates to minimise lock contention so the
799 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
800 * with this flag set because they are the inodes that are out of order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800802static enum lru_status inode_lru_isolate(struct list_head *item,
803 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000804{
805 struct list_head *freeable = arg;
806 struct inode *inode = container_of(item, struct inode, i_lru);
807
808 /*
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800809 * We are inverting the lru lock/inode->i_lock here, so use a
810 * trylock. If we fail to get the lock, just skip it.
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000811 */
812 if (!spin_trylock(&inode->i_lock))
813 return LRU_SKIP;
814
815 /*
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800816 * Inodes can get referenced, redirtied, or repopulated while
817 * they're already on the LRU, and this can make them
818 * unreclaimable for a while. Remove them lazily here; iput,
819 * sync, or the last page cache deletion will requeue them.
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000820 */
821 if (atomic_read(&inode->i_count) ||
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800822 (inode->i_state & ~I_REFERENCED) ||
823 !mapping_shrinkable(&inode->i_data)) {
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800824 list_lru_isolate(lru, &inode->i_lru);
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000825 spin_unlock(&inode->i_lock);
826 this_cpu_dec(nr_unused);
827 return LRU_REMOVED;
828 }
829
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800830 /* Recently referenced inodes get one more pass */
Dave Chinner69056ee2019-02-12 15:35:51 -0800831 if (inode->i_state & I_REFERENCED) {
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000832 inode->i_state &= ~I_REFERENCED;
833 spin_unlock(&inode->i_lock);
834 return LRU_ROTATE;
835 }
836
Johannes Weiner51b8c1f2021-11-08 18:31:24 -0800837 /*
838 * On highmem systems, mapping_shrinkable() permits dropping
839 * page cache in order to free up struct inodes: lowmem might
840 * be under pressure before the cache inside the highmem zone.
841 */
Johannes Weiner7ae12c82021-09-02 14:53:24 -0700842 if (inode_has_buffers(inode) || !mapping_empty(&inode->i_data)) {
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000843 __iget(inode);
844 spin_unlock(&inode->i_lock);
845 spin_unlock(lru_lock);
846 if (remove_inode_buffers(inode)) {
847 unsigned long reap;
848 reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
849 if (current_is_kswapd())
850 __count_vm_events(KSWAPD_INODESTEAL, reap);
851 else
852 __count_vm_events(PGINODESTEAL, reap);
Yosry Ahmedc7b23b62023-04-13 10:40:34 +0000853 mm_account_reclaimed_pages(reap);
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000854 }
855 iput(inode);
856 spin_lock(lru_lock);
857 return LRU_RETRY;
858 }
859
860 WARN_ON(inode->i_state & I_NEW);
861 inode->i_state |= I_FREEING;
Vladimir Davydov3f97b162015-02-12 14:59:35 -0800862 list_lru_isolate_move(lru, &inode->i_lru, freeable);
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000863 spin_unlock(&inode->i_lock);
864
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000865 this_cpu_dec(nr_unused);
866 return LRU_REMOVED;
867}
868
869/*
870 * Walk the superblock inode LRU for freeable inodes and attempt to free them.
871 * This is called from the superblock shrinker function with a number of inodes
872 * to trim from the LRU. Inodes to be freed are moved to a temporary list and
873 * then are freed outside inode_lock by dispose_list().
874 */
Vladimir Davydov503c3582015-02-12 14:58:47 -0800875long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 LIST_HEAD(freeable);
Dave Chinnerbc3b14c2013-08-28 10:17:58 +1000878 long freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Vladimir Davydov503c3582015-02-12 14:58:47 -0800880 freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
881 inode_lru_isolate, &freeable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 dispose_list(&freeable);
Dave Chinner0a234c62013-08-28 10:17:57 +1000883 return freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static void __wait_on_freeing_inode(struct inode *inode);
887/*
888 * Called with the inode lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530890static struct inode *find_inode(struct super_block *sb,
891 struct hlist_head *head,
892 int (*test)(struct inode *, void *),
893 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530895 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897repeat:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800898 hlist_for_each_entry(inode, head, i_hash) {
Al Viro5a3cd99282013-11-06 09:54:52 -0500899 if (inode->i_sb != sb)
900 continue;
901 if (!test(inode, data))
902 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100903 spin_lock(&inode->i_lock);
Al Viroa4ffdde2010-06-02 17:38:30 -0400904 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 __wait_on_freeing_inode(inode);
906 goto repeat;
907 }
Al Viroc2b6d622018-06-28 15:53:17 -0400908 if (unlikely(inode->i_state & I_CREATING)) {
909 spin_unlock(&inode->i_lock);
910 return ERR_PTR(-ESTALE);
911 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400912 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100913 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400914 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400916 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
919/*
920 * find_inode_fast is the fast path version of find_inode, see the comment at
921 * iget_locked for details.
922 */
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530923static struct inode *find_inode_fast(struct super_block *sb,
924 struct hlist_head *head, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Manish Katiyar6b3304b2009-03-31 19:35:54 +0530926 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928repeat:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800929 hlist_for_each_entry(inode, head, i_hash) {
Al Viro5a3cd99282013-11-06 09:54:52 -0500930 if (inode->i_ino != ino)
931 continue;
932 if (inode->i_sb != sb)
933 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100934 spin_lock(&inode->i_lock);
Al Viroa4ffdde2010-06-02 17:38:30 -0400935 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 __wait_on_freeing_inode(inode);
937 goto repeat;
938 }
Al Viroc2b6d622018-06-28 15:53:17 -0400939 if (unlikely(inode->i_state & I_CREATING)) {
940 spin_unlock(&inode->i_lock);
941 return ERR_PTR(-ESTALE);
942 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400943 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100944 spin_unlock(&inode->i_lock);
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400945 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Christoph Hellwigf7899bd2010-10-23 07:09:06 -0400947 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Eric Dumazetf991bd22010-10-23 11:18:01 -0400950/*
951 * Each cpu owns a range of LAST_INO_BATCH numbers.
952 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
953 * to renew the exhausted range.
David Chinner8290c352008-10-30 17:35:24 +1100954 *
Eric Dumazetf991bd22010-10-23 11:18:01 -0400955 * This does not significantly increase overflow rate because every CPU can
956 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
957 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
958 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
959 * overflow rate by 2x, which does not seem too significant.
960 *
961 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
962 * error if st_ino won't fit in target struct field. Use 32bit counter
963 * here to attempt to avoid that.
David Chinner8290c352008-10-30 17:35:24 +1100964 */
Eric Dumazetf991bd22010-10-23 11:18:01 -0400965#define LAST_INO_BATCH 1024
966static DEFINE_PER_CPU(unsigned int, last_ino);
David Chinner8290c352008-10-30 17:35:24 +1100967
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400968unsigned int get_next_ino(void)
Eric Dumazetf991bd22010-10-23 11:18:01 -0400969{
970 unsigned int *p = &get_cpu_var(last_ino);
971 unsigned int res = *p;
972
973#ifdef CONFIG_SMP
974 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
975 static atomic_t shared_last_ino;
976 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
977
978 res = next - LAST_INO_BATCH;
979 }
980#endif
981
Carlos Maiolino2adc3762015-06-25 12:25:58 -0300982 res++;
983 /* get_next_ino should not provide a 0 inode number */
984 if (unlikely(!res))
985 res++;
986 *p = res;
Eric Dumazetf991bd22010-10-23 11:18:01 -0400987 put_cpu_var(last_ino);
988 return res;
David Chinner8290c352008-10-30 17:35:24 +1100989}
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400990EXPORT_SYMBOL(get_next_ino);
David Chinner8290c352008-10-30 17:35:24 +1100991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/**
Eric Dumazeta209dfc2011-07-26 11:36:34 +0200993 * new_inode_pseudo - obtain an inode
994 * @sb: superblock
995 *
996 * Allocates a new inode for given superblock.
997 * Inode wont be chained in superblock s_inodes list
998 * This means :
999 * - fs can't be unmount
1000 * - quotas, fsnotify, writeback can't work
1001 */
1002struct inode *new_inode_pseudo(struct super_block *sb)
1003{
1004 struct inode *inode = alloc_inode(sb);
1005
1006 if (inode) {
1007 spin_lock(&inode->i_lock);
1008 inode->i_state = 0;
1009 spin_unlock(&inode->i_lock);
Eric Dumazeta209dfc2011-07-26 11:36:34 +02001010 }
1011 return inode;
1012}
1013
1014/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * new_inode - obtain an inode
1016 * @sb: superblock
1017 *
Mel Gorman769848c2007-07-17 04:03:05 -07001018 * Allocates a new inode for given superblock. The default gfp_mask
Hugh Dickins3c1d4372009-01-06 14:39:23 -08001019 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
Mel Gorman769848c2007-07-17 04:03:05 -07001020 * If HIGHMEM pages are unsuitable or it is known that pages allocated
1021 * for the page cache are not reclaimable or migratable,
1022 * mapping_set_gfp_mask() must be called with suitable flags on the
1023 * newly created inode's mapping
1024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
1026struct inode *new_inode(struct super_block *sb)
1027{
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301028 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Eric Dumazeta209dfc2011-07-26 11:36:34 +02001030 inode = new_inode_pseudo(sb);
1031 if (inode)
Dave Chinner55fa6092011-03-22 22:23:40 +11001032 inode_sb_list_add(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return inode;
1034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035EXPORT_SYMBOL(new_inode);
1036
Peter Zijlstra14358e62007-10-14 01:38:33 +02001037#ifdef CONFIG_DEBUG_LOCK_ALLOC
Josh Boyere096d0c2011-08-25 07:48:12 -04001038void lockdep_annotate_inode_mutex_key(struct inode *inode)
1039{
Namhyung Kima3314a02010-10-11 22:38:00 +09001040 if (S_ISDIR(inode->i_mode)) {
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +02001041 struct file_system_type *type = inode->i_sb->s_type;
1042
Jan Kara9a7aa122009-06-04 15:26:49 +02001043 /* Set new key only if filesystem hasn't already changed it */
Al Viro9902af72016-04-15 15:08:36 -04001044 if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) {
Jan Kara9a7aa122009-06-04 15:26:49 +02001045 /*
1046 * ensure nobody is actually holding i_mutex
1047 */
Al Viro9902af72016-04-15 15:08:36 -04001048 // mutex_destroy(&inode->i_mutex);
1049 init_rwsem(&inode->i_rwsem);
1050 lockdep_set_class(&inode->i_rwsem,
Jan Kara9a7aa122009-06-04 15:26:49 +02001051 &type->i_mutex_dir_key);
1052 }
Peter Zijlstra1e89a5e2007-10-16 06:47:54 +02001053 }
Josh Boyere096d0c2011-08-25 07:48:12 -04001054}
1055EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
Peter Zijlstra14358e62007-10-14 01:38:33 +02001056#endif
Josh Boyere096d0c2011-08-25 07:48:12 -04001057
1058/**
1059 * unlock_new_inode - clear the I_NEW state and wake up any waiters
1060 * @inode: new inode to unlock
1061 *
1062 * Called when the inode is fully initialised to clear the new state of the
1063 * inode and wake up anyone waiting for the inode to finish initialisation.
1064 */
1065void unlock_new_inode(struct inode *inode)
1066{
1067 lockdep_annotate_inode_mutex_key(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001068 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001069 WARN_ON(!(inode->i_state & I_NEW));
Al Viroc2b6d622018-06-28 15:53:17 -04001070 inode->i_state &= ~I_NEW & ~I_CREATING;
Al Viro310fa7a2012-03-10 17:07:28 -05001071 smp_mb();
Dave Chinner250df6e2011-03-22 22:23:36 +11001072 wake_up_bit(&inode->i_state, __I_NEW);
1073 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075EXPORT_SYMBOL(unlock_new_inode);
1076
Al Viroc2b6d622018-06-28 15:53:17 -04001077void discard_new_inode(struct inode *inode)
1078{
1079 lockdep_annotate_inode_mutex_key(inode);
1080 spin_lock(&inode->i_lock);
1081 WARN_ON(!(inode->i_state & I_NEW));
1082 inode->i_state &= ~I_NEW;
1083 smp_mb();
1084 wake_up_bit(&inode->i_state, __I_NEW);
1085 spin_unlock(&inode->i_lock);
1086 iput(inode);
1087}
1088EXPORT_SYMBOL(discard_new_inode);
1089
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001090/**
Jan Karaf23ce752023-06-01 12:58:24 +02001091 * lock_two_inodes - lock two inodes (may be regular files but also dirs)
1092 *
1093 * Lock any non-NULL argument. The caller must make sure that if he is passing
1094 * in two directories, one is not ancestor of the other. Zero, one or two
1095 * objects may be locked by this function.
1096 *
1097 * @inode1: first inode to lock
1098 * @inode2: second inode to lock
1099 * @subclass1: inode lock subclass for the first lock obtained
1100 * @subclass2: inode lock subclass for the second lock obtained
1101 */
1102void lock_two_inodes(struct inode *inode1, struct inode *inode2,
1103 unsigned subclass1, unsigned subclass2)
1104{
1105 if (!inode1 || !inode2) {
1106 /*
1107 * Make sure @subclass1 will be used for the acquired lock.
1108 * This is not strictly necessary (no current caller cares) but
1109 * let's keep things consistent.
1110 */
1111 if (!inode1)
1112 swap(inode1, inode2);
1113 goto lock;
1114 }
1115
1116 /*
1117 * If one object is directory and the other is not, we must make sure
1118 * to lock directory first as the other object may be its child.
1119 */
1120 if (S_ISDIR(inode2->i_mode) == S_ISDIR(inode1->i_mode)) {
1121 if (inode1 > inode2)
1122 swap(inode1, inode2);
1123 } else if (!S_ISDIR(inode1->i_mode))
1124 swap(inode1, inode2);
1125lock:
1126 if (inode1)
1127 inode_lock_nested(inode1, subclass1);
1128 if (inode2 && inode2 != inode1)
1129 inode_lock_nested(inode2, subclass2);
1130}
1131
1132/**
J. Bruce Fields375e2892012-04-18 15:16:33 -04001133 * lock_two_nondirectories - take two i_mutexes on non-directory objects
J. Bruce Fields4fd699a2014-04-01 17:08:43 +02001134 *
Jan Kara2454ad82023-06-01 12:58:26 +02001135 * Lock any non-NULL argument. Passed objects must not be directories.
J. Bruce Fields4fd699a2014-04-01 17:08:43 +02001136 * Zero, one or two objects may be locked by this function.
1137 *
J. Bruce Fields375e2892012-04-18 15:16:33 -04001138 * @inode1: first inode to lock
1139 * @inode2: second inode to lock
1140 */
1141void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1142{
Christian Brauner33ab2312023-07-03 16:49:12 +02001143 if (inode1)
1144 WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
1145 if (inode2)
1146 WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
Jan Kara2454ad82023-06-01 12:58:26 +02001147 lock_two_inodes(inode1, inode2, I_MUTEX_NORMAL, I_MUTEX_NONDIR2);
J. Bruce Fields375e2892012-04-18 15:16:33 -04001148}
1149EXPORT_SYMBOL(lock_two_nondirectories);
1150
1151/**
1152 * unlock_two_nondirectories - release locks from lock_two_nondirectories()
1153 * @inode1: first inode to unlock
1154 * @inode2: second inode to unlock
1155 */
1156void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1157{
Jan Kara2454ad82023-06-01 12:58:26 +02001158 if (inode1) {
1159 WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
Al Viro59551022016-01-22 15:40:57 -05001160 inode_unlock(inode1);
Jan Kara2454ad82023-06-01 12:58:26 +02001161 }
1162 if (inode2 && inode2 != inode1) {
1163 WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
Al Viro59551022016-01-22 15:40:57 -05001164 inode_unlock(inode2);
Jan Kara2454ad82023-06-01 12:58:26 +02001165 }
J. Bruce Fields375e2892012-04-18 15:16:33 -04001166}
1167EXPORT_SYMBOL(unlock_two_nondirectories);
1168
1169/**
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001170 * inode_insert5 - obtain an inode from a mounted file system
1171 * @inode: pre-allocated inode to use for insert to cache
1172 * @hashval: hash value (usually inode number) to get
1173 * @test: callback used for comparisons between inodes
1174 * @set: callback used to initialize a new struct inode
1175 * @data: opaque data pointer to pass to @test and @set
1176 *
1177 * Search for the inode specified by @hashval and @data in the inode cache,
1178 * and if present it is return it with an increased reference count. This is
1179 * a variant of iget5_locked() for callers that don't want to fail on memory
1180 * allocation of inode.
1181 *
1182 * If the inode is not in cache, insert the pre-allocated inode to cache and
1183 * return it locked, hashed, and with the I_NEW flag set. The file system gets
1184 * to fill it in before unlocking it via unlock_new_inode().
1185 *
1186 * Note both @test and @set are called with the inode_hash_lock held, so can't
1187 * sleep.
1188 */
1189struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
1190 int (*test)(struct inode *, void *),
1191 int (*set)(struct inode *, void *), void *data)
1192{
1193 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1194 struct inode *old;
1195
1196again:
1197 spin_lock(&inode_hash_lock);
1198 old = find_inode(inode->i_sb, head, test, data);
1199 if (unlikely(old)) {
1200 /*
1201 * Uhhuh, somebody else created the same inode under us.
1202 * Use the old inode instead of the preallocated one.
1203 */
1204 spin_unlock(&inode_hash_lock);
Al Viroc2b6d622018-06-28 15:53:17 -04001205 if (IS_ERR(old))
1206 return NULL;
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001207 wait_on_inode(old);
1208 if (unlikely(inode_unhashed(old))) {
1209 iput(old);
1210 goto again;
1211 }
1212 return old;
1213 }
1214
1215 if (set && unlikely(set(inode, data))) {
1216 inode = NULL;
1217 goto unlock;
1218 }
1219
1220 /*
1221 * Return the locked inode with I_NEW set, the
1222 * caller is responsible for filling in the contents
1223 */
1224 spin_lock(&inode->i_lock);
1225 inode->i_state |= I_NEW;
David Howells3f19b2a2017-12-01 11:40:16 +00001226 hlist_add_head_rcu(&inode->i_hash, head);
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001227 spin_unlock(&inode->i_lock);
Jeff Layton18cc9122022-03-31 16:29:00 -04001228
1229 /*
1230 * Add inode to the sb list if it's not already. It has I_NEW at this
1231 * point, so it should be safe to test i_sb_list locklessly.
1232 */
1233 if (list_empty(&inode->i_sb_list))
Miklos Szeredie9505642018-07-24 15:01:55 +02001234 inode_sb_list_add(inode);
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001235unlock:
1236 spin_unlock(&inode_hash_lock);
1237
1238 return inode;
1239}
1240EXPORT_SYMBOL(inode_insert5);
1241
1242/**
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001243 * iget5_locked - obtain an inode from a mounted file system
1244 * @sb: super block of file system
1245 * @hashval: hash value (usually inode number) to get
1246 * @test: callback used for comparisons between inodes
1247 * @set: callback used to initialize a new struct inode
1248 * @data: opaque data pointer to pass to @test and @set
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001250 * Search for the inode specified by @hashval and @data in the inode cache,
1251 * and if present it is return it with an increased reference count. This is
1252 * a generalized version of iget_locked() for file systems where the inode
1253 * number is not sufficient for unique identification of an inode.
1254 *
1255 * If the inode is not in cache, allocate a new inode and return it locked,
1256 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1257 * before unlocking it via unlock_new_inode().
1258 *
1259 * Note both @test and @set are called with the inode_hash_lock held, so can't
1260 * sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001262struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1263 int (*test)(struct inode *, void *),
1264 int (*set)(struct inode *, void *), void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001266 struct inode *inode = ilookup5(sb, hashval, test, data);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001267
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001268 if (!inode) {
Miklos Szeredie9505642018-07-24 15:01:55 +02001269 struct inode *new = alloc_inode(sb);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001270
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001271 if (new) {
Miklos Szeredie9505642018-07-24 15:01:55 +02001272 new->i_state = 0;
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001273 inode = inode_insert5(new, hashval, test, set, data);
1274 if (unlikely(inode != new))
Miklos Szeredie9505642018-07-24 15:01:55 +02001275 destroy_inode(new);
Al Viro2864f302016-07-03 23:15:21 -04001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001280EXPORT_SYMBOL(iget5_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001282/**
1283 * iget_locked - obtain an inode from a mounted file system
1284 * @sb: super block of file system
1285 * @ino: inode number to get
1286 *
1287 * Search for the inode specified by @ino in the inode cache and if present
1288 * return it with an increased reference count. This is for file systems
1289 * where the inode number is sufficient for unique identification of an inode.
1290 *
1291 * If the inode is not in cache, allocate a new inode and return it locked,
1292 * hashed, and with the I_NEW flag set. The file system gets to fill it in
1293 * before unlocking it via unlock_new_inode().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001295struct inode *iget_locked(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001297 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301298 struct inode *inode;
Al Viro2864f302016-07-03 23:15:21 -04001299again:
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001300 spin_lock(&inode_hash_lock);
1301 inode = find_inode_fast(sb, head, ino);
1302 spin_unlock(&inode_hash_lock);
1303 if (inode) {
Al Viroc2b6d622018-06-28 15:53:17 -04001304 if (IS_ERR(inode))
1305 return NULL;
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001306 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001307 if (unlikely(inode_unhashed(inode))) {
1308 iput(inode);
1309 goto again;
1310 }
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001311 return inode;
1312 }
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 inode = alloc_inode(sb);
1315 if (inode) {
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301316 struct inode *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Dave Chinner67a23c42011-03-22 22:23:42 +11001318 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* We released the lock, so.. */
1320 old = find_inode_fast(sb, head, ino);
1321 if (!old) {
1322 inode->i_ino = ino;
Dave Chinner250df6e2011-03-22 22:23:36 +11001323 spin_lock(&inode->i_lock);
Christoph Hellwigeaff8072009-12-17 14:25:01 +01001324 inode->i_state = I_NEW;
David Howells3f19b2a2017-12-01 11:40:16 +00001325 hlist_add_head_rcu(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001326 spin_unlock(&inode->i_lock);
Dave Chinner55fa6092011-03-22 22:23:40 +11001327 inode_sb_list_add(inode);
Dave Chinner67a23c42011-03-22 22:23:42 +11001328 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Return the locked inode with I_NEW set, the
1331 * caller is responsible for filling in the contents
1332 */
1333 return inode;
1334 }
1335
1336 /*
1337 * Uhhuh, somebody else created the same inode under
1338 * us. Use the old inode instead of the one we just
1339 * allocated.
1340 */
Dave Chinner67a23c42011-03-22 22:23:42 +11001341 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 destroy_inode(inode);
Al Viroc2b6d622018-06-28 15:53:17 -04001343 if (IS_ERR(old))
1344 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 inode = old;
1346 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001347 if (unlikely(inode_unhashed(inode))) {
1348 iput(inode);
1349 goto again;
1350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 return inode;
1353}
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001354EXPORT_SYMBOL(iget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001356/*
1357 * search the inode cache for a matching inode number.
1358 * If we find one, then the inode number we are trying to
1359 * allocate is not unique and so we should not use it.
1360 *
1361 * Returns 1 if the inode number is unique, 0 if it is not.
1362 */
1363static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1364{
1365 struct hlist_head *b = inode_hashtable + hash(sb, ino);
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001366 struct inode *inode;
1367
David Howells3f19b2a2017-12-01 11:40:16 +00001368 hlist_for_each_entry_rcu(inode, b, i_hash) {
1369 if (inode->i_ino == ino && inode->i_sb == sb)
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001370 return 0;
1371 }
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001372 return 1;
1373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/**
1376 * iunique - get a unique inode number
1377 * @sb: superblock
1378 * @max_reserved: highest reserved inode number
1379 *
1380 * Obtain an inode number that is unique on the system for a given
1381 * superblock. This is used by file systems that have no natural
1382 * permanent inode numbering system. An inode number is returned that
1383 * is higher than the reserved limit but unique.
1384 *
1385 * BUGS:
1386 * With a large number of inodes live on the file system this function
1387 * currently becomes quite slow.
1388 */
1389ino_t iunique(struct super_block *sb, ino_t max_reserved)
1390{
Jeff Layton866b04f2007-05-08 00:32:29 -07001391 /*
1392 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1393 * error if st_ino won't fit in target struct field. Use 32bit counter
1394 * here to attempt to avoid that.
1395 */
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001396 static DEFINE_SPINLOCK(iunique_lock);
Jeff Layton866b04f2007-05-08 00:32:29 -07001397 static unsigned int counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 ino_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
David Howells3f19b2a2017-12-01 11:40:16 +00001400 rcu_read_lock();
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001401 spin_lock(&iunique_lock);
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001402 do {
1403 if (counter <= max_reserved)
1404 counter = max_reserved + 1;
1405 res = counter++;
Christoph Hellwigad5e1952010-10-23 07:00:16 -04001406 } while (!test_inode_iunique(sb, res));
1407 spin_unlock(&iunique_lock);
David Howells3f19b2a2017-12-01 11:40:16 +00001408 rcu_read_unlock();
Jeffrey Layton3361c7b2007-05-08 00:29:48 -07001409
1410 return res;
1411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412EXPORT_SYMBOL(iunique);
1413
1414struct inode *igrab(struct inode *inode)
1415{
Dave Chinner250df6e2011-03-22 22:23:36 +11001416 spin_lock(&inode->i_lock);
1417 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001419 spin_unlock(&inode->i_lock);
1420 } else {
1421 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /*
1423 * Handle the case where s_op->clear_inode is not been
1424 * called yet, and somebody is calling igrab
1425 * while the inode is getting freed.
1426 */
1427 inode = NULL;
Dave Chinner250df6e2011-03-22 22:23:36 +11001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return inode;
1430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431EXPORT_SYMBOL(igrab);
1432
1433/**
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001434 * ilookup5_nowait - search for an inode in the inode cache
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * @sb: super block of file system to search
1436 * @hashval: hash value (usually inode number) to search for
1437 * @test: callback used for comparisons between inodes
1438 * @data: opaque data pointer to pass to @test
1439 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001440 * Search for the inode specified by @hashval and @data in the inode cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * If the inode is in the cache, the inode is returned with an incremented
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001442 * reference count.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001443 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001444 * Note: I_NEW is not waited upon so you have to be very careful what you do
1445 * with the returned inode. You probably should be using ilookup5() instead.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001446 *
Randy Dunlapb6d0ad62011-03-26 13:27:47 -07001447 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001448 */
1449struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1450 int (*test)(struct inode *, void *), void *data)
1451{
1452 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001453 struct inode *inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001454
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001455 spin_lock(&inode_hash_lock);
1456 inode = find_inode(sb, head, test, data);
1457 spin_unlock(&inode_hash_lock);
1458
Al Viroc2b6d622018-06-28 15:53:17 -04001459 return IS_ERR(inode) ? NULL : inode;
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001460}
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001461EXPORT_SYMBOL(ilookup5_nowait);
1462
1463/**
1464 * ilookup5 - search for an inode in the inode cache
1465 * @sb: super block of file system to search
1466 * @hashval: hash value (usually inode number) to search for
1467 * @test: callback used for comparisons between inodes
1468 * @data: opaque data pointer to pass to @test
1469 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001470 * Search for the inode specified by @hashval and @data in the inode cache,
1471 * and if the inode is in the cache, return the inode with an incremented
1472 * reference count. Waits on I_NEW before returning the inode.
Anton Altaparmakov88bd5122005-07-13 01:10:44 -07001473 * returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001475 * This is a generalized version of ilookup() for file systems where the
1476 * inode number is not sufficient for unique identification of an inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001478 * Note: @test is called with the inode_hash_lock held, so can't sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
1480struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1481 int (*test)(struct inode *, void *), void *data)
1482{
Al Viro2864f302016-07-03 23:15:21 -04001483 struct inode *inode;
1484again:
1485 inode = ilookup5_nowait(sb, hashval, test, data);
1486 if (inode) {
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001487 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001488 if (unlikely(inode_unhashed(inode))) {
1489 iput(inode);
1490 goto again;
1491 }
1492 }
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001493 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495EXPORT_SYMBOL(ilookup5);
1496
1497/**
1498 * ilookup - search for an inode in the inode cache
1499 * @sb: super block of file system to search
1500 * @ino: inode number to search for
1501 *
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001502 * Search for the inode @ino in the inode cache, and if the inode is in the
1503 * cache, the inode is returned with an incremented reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
1505struct inode *ilookup(struct super_block *sb, unsigned long ino)
1506{
1507 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001508 struct inode *inode;
Al Viro2864f302016-07-03 23:15:21 -04001509again:
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001510 spin_lock(&inode_hash_lock);
1511 inode = find_inode_fast(sb, head, ino);
1512 spin_unlock(&inode_hash_lock);
1513
Al Viro2864f302016-07-03 23:15:21 -04001514 if (inode) {
Al Viroc2b6d622018-06-28 15:53:17 -04001515 if (IS_ERR(inode))
1516 return NULL;
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001517 wait_on_inode(inode);
Al Viro2864f302016-07-03 23:15:21 -04001518 if (unlikely(inode_unhashed(inode))) {
1519 iput(inode);
1520 goto again;
1521 }
1522 }
Christoph Hellwig0b2d0722011-03-23 15:03:28 -04001523 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525EXPORT_SYMBOL(ilookup);
1526
Theodore Ts'ofe032c42015-02-02 00:37:01 -05001527/**
1528 * find_inode_nowait - find an inode in the inode cache
1529 * @sb: super block of file system to search
1530 * @hashval: hash value (usually inode number) to search for
1531 * @match: callback used for comparisons between inodes
1532 * @data: opaque data pointer to pass to @match
1533 *
1534 * Search for the inode specified by @hashval and @data in the inode
1535 * cache, where the helper function @match will return 0 if the inode
1536 * does not match, 1 if the inode does match, and -1 if the search
1537 * should be stopped. The @match function must be responsible for
1538 * taking the i_lock spin_lock and checking i_state for an inode being
1539 * freed or being initialized, and incrementing the reference count
1540 * before returning 1. It also must not sleep, since it is called with
1541 * the inode_hash_lock spinlock held.
1542 *
1543 * This is a even more generalized version of ilookup5() when the
1544 * function must never block --- find_inode() can block in
1545 * __wait_on_freeing_inode() --- or when the caller can not increment
1546 * the reference count because the resulting iput() might cause an
1547 * inode eviction. The tradeoff is that the @match funtion must be
1548 * very carefully implemented.
1549 */
1550struct inode *find_inode_nowait(struct super_block *sb,
1551 unsigned long hashval,
1552 int (*match)(struct inode *, unsigned long,
1553 void *),
1554 void *data)
1555{
1556 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1557 struct inode *inode, *ret_inode = NULL;
1558 int mval;
1559
1560 spin_lock(&inode_hash_lock);
1561 hlist_for_each_entry(inode, head, i_hash) {
1562 if (inode->i_sb != sb)
1563 continue;
1564 mval = match(inode, hashval, data);
1565 if (mval == 0)
1566 continue;
1567 if (mval == 1)
1568 ret_inode = inode;
1569 goto out;
1570 }
1571out:
1572 spin_unlock(&inode_hash_lock);
1573 return ret_inode;
1574}
1575EXPORT_SYMBOL(find_inode_nowait);
1576
David Howells3f19b2a2017-12-01 11:40:16 +00001577/**
1578 * find_inode_rcu - find an inode in the inode cache
1579 * @sb: Super block of file system to search
1580 * @hashval: Key to hash
1581 * @test: Function to test match on an inode
1582 * @data: Data for test function
1583 *
1584 * Search for the inode specified by @hashval and @data in the inode cache,
1585 * where the helper function @test will return 0 if the inode does not match
1586 * and 1 if it does. The @test function must be responsible for taking the
1587 * i_lock spin_lock and checking i_state for an inode being freed or being
1588 * initialized.
1589 *
1590 * If successful, this will return the inode for which the @test function
1591 * returned 1 and NULL otherwise.
1592 *
1593 * The @test function is not permitted to take a ref on any inode presented.
1594 * It is also not permitted to sleep.
1595 *
1596 * The caller must hold the RCU read lock.
1597 */
1598struct inode *find_inode_rcu(struct super_block *sb, unsigned long hashval,
1599 int (*test)(struct inode *, void *), void *data)
1600{
1601 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1602 struct inode *inode;
1603
1604 RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1605 "suspicious find_inode_rcu() usage");
1606
1607 hlist_for_each_entry_rcu(inode, head, i_hash) {
1608 if (inode->i_sb == sb &&
1609 !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)) &&
1610 test(inode, data))
1611 return inode;
1612 }
1613 return NULL;
1614}
1615EXPORT_SYMBOL(find_inode_rcu);
1616
1617/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +01001618 * find_inode_by_ino_rcu - Find an inode in the inode cache
David Howells3f19b2a2017-12-01 11:40:16 +00001619 * @sb: Super block of file system to search
1620 * @ino: The inode number to match
1621 *
1622 * Search for the inode specified by @hashval and @data in the inode cache,
1623 * where the helper function @test will return 0 if the inode does not match
1624 * and 1 if it does. The @test function must be responsible for taking the
1625 * i_lock spin_lock and checking i_state for an inode being freed or being
1626 * initialized.
1627 *
1628 * If successful, this will return the inode for which the @test function
1629 * returned 1 and NULL otherwise.
1630 *
1631 * The @test function is not permitted to take a ref on any inode presented.
1632 * It is also not permitted to sleep.
1633 *
1634 * The caller must hold the RCU read lock.
1635 */
1636struct inode *find_inode_by_ino_rcu(struct super_block *sb,
1637 unsigned long ino)
1638{
1639 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1640 struct inode *inode;
1641
1642 RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1643 "suspicious find_inode_by_ino_rcu() usage");
1644
1645 hlist_for_each_entry_rcu(inode, head, i_hash) {
1646 if (inode->i_ino == ino &&
1647 inode->i_sb == sb &&
1648 !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)))
1649 return inode;
1650 }
1651 return NULL;
1652}
1653EXPORT_SYMBOL(find_inode_by_ino_rcu);
1654
Al Viro261bca82008-12-30 01:48:21 -05001655int insert_inode_locked(struct inode *inode)
1656{
1657 struct super_block *sb = inode->i_sb;
1658 ino_t ino = inode->i_ino;
1659 struct hlist_head *head = inode_hashtable + hash(sb, ino);
Al Viro261bca82008-12-30 01:48:21 -05001660
Al Viro261bca82008-12-30 01:48:21 -05001661 while (1) {
Al Viro72a43d62009-05-13 19:13:40 +01001662 struct inode *old = NULL;
Dave Chinner67a23c42011-03-22 22:23:42 +11001663 spin_lock(&inode_hash_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001664 hlist_for_each_entry(old, head, i_hash) {
Al Viro72a43d62009-05-13 19:13:40 +01001665 if (old->i_ino != ino)
1666 continue;
1667 if (old->i_sb != sb)
1668 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001669 spin_lock(&old->i_lock);
1670 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1671 spin_unlock(&old->i_lock);
Al Viro72a43d62009-05-13 19:13:40 +01001672 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +11001673 }
Al Viro72a43d62009-05-13 19:13:40 +01001674 break;
1675 }
Sasha Levinb67bfe02013-02-27 17:06:00 -08001676 if (likely(!old)) {
Dave Chinner250df6e2011-03-22 22:23:36 +11001677 spin_lock(&inode->i_lock);
Al Viroc2b6d622018-06-28 15:53:17 -04001678 inode->i_state |= I_NEW | I_CREATING;
David Howells3f19b2a2017-12-01 11:40:16 +00001679 hlist_add_head_rcu(&inode->i_hash, head);
Dave Chinner250df6e2011-03-22 22:23:36 +11001680 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001681 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001682 return 0;
1683 }
Al Viroc2b6d622018-06-28 15:53:17 -04001684 if (unlikely(old->i_state & I_CREATING)) {
1685 spin_unlock(&old->i_lock);
1686 spin_unlock(&inode_hash_lock);
1687 return -EBUSY;
1688 }
Al Viro261bca82008-12-30 01:48:21 -05001689 __iget(old);
Dave Chinner250df6e2011-03-22 22:23:36 +11001690 spin_unlock(&old->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11001691 spin_unlock(&inode_hash_lock);
Al Viro261bca82008-12-30 01:48:21 -05001692 wait_on_inode(old);
Al Viro1d3382cb2010-10-23 15:19:20 -04001693 if (unlikely(!inode_unhashed(old))) {
Al Viro261bca82008-12-30 01:48:21 -05001694 iput(old);
1695 return -EBUSY;
1696 }
1697 iput(old);
1698 }
1699}
Al Viro261bca82008-12-30 01:48:21 -05001700EXPORT_SYMBOL(insert_inode_locked);
1701
1702int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1703 int (*test)(struct inode *, void *), void *data)
1704{
Al Viroc2b6d622018-06-28 15:53:17 -04001705 struct inode *old;
1706
1707 inode->i_state |= I_CREATING;
1708 old = inode_insert5(inode, hashval, test, NULL, data);
Al Viro261bca82008-12-30 01:48:21 -05001709
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001710 if (old != inode) {
Al Viro261bca82008-12-30 01:48:21 -05001711 iput(old);
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001712 return -EBUSY;
Al Viro261bca82008-12-30 01:48:21 -05001713 }
Miklos Szeredi80ea09a2018-05-17 10:53:05 +02001714 return 0;
Al Viro261bca82008-12-30 01:48:21 -05001715}
Al Viro261bca82008-12-30 01:48:21 -05001716EXPORT_SYMBOL(insert_inode_locked4);
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Al Viro45321ac2010-06-07 13:43:19 -04001719int generic_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Al Viro45321ac2010-06-07 13:43:19 -04001721 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723EXPORT_SYMBOL(generic_delete_inode);
1724
Al Viro45321ac2010-06-07 13:43:19 -04001725/*
Al Viro45321ac2010-06-07 13:43:19 -04001726 * Called when we're dropping the last reference
1727 * to an inode.
1728 *
1729 * Call the FS "drop_inode()" function, defaulting to
1730 * the legacy UNIX filesystem behaviour. If it tells
1731 * us to evict inode, do so. Otherwise, retain inode
1732 * in cache if fs is alive, sync and evict if fs is
1733 * shutting down.
1734 */
1735static void iput_final(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct super_block *sb = inode->i_sb;
Al Viro45321ac2010-06-07 13:43:19 -04001738 const struct super_operations *op = inode->i_sb->s_op;
David Howells3f19b2a2017-12-01 11:40:16 +00001739 unsigned long state;
Al Viro45321ac2010-06-07 13:43:19 -04001740 int drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Dave Chinner250df6e2011-03-22 22:23:36 +11001742 WARN_ON(inode->i_state & I_NEW);
1743
Al Viroe7f59092011-07-07 15:45:59 -04001744 if (op->drop_inode)
Al Viro45321ac2010-06-07 13:43:19 -04001745 drop = op->drop_inode(inode);
1746 else
1747 drop = generic_drop_inode(inode);
1748
Hao Li88149082020-12-08 10:08:43 +08001749 if (!drop &&
1750 !(inode->i_state & I_DONTCACHE) &&
1751 (sb->s_flags & SB_ACTIVE)) {
Johannes Weiner51b8c1f2021-11-08 18:31:24 -08001752 __inode_add_lru(inode, true);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001753 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001754 return;
1755 }
1756
David Howells3f19b2a2017-12-01 11:40:16 +00001757 state = inode->i_state;
Al Viro45321ac2010-06-07 13:43:19 -04001758 if (!drop) {
David Howells3f19b2a2017-12-01 11:40:16 +00001759 WRITE_ONCE(inode->i_state, state | I_WILL_FREE);
Dave Chinner250df6e2011-03-22 22:23:36 +11001760 spin_unlock(&inode->i_lock);
David Howells3f19b2a2017-12-01 11:40:16 +00001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 write_inode_now(inode, 1);
David Howells3f19b2a2017-12-01 11:40:16 +00001763
Dave Chinner250df6e2011-03-22 22:23:36 +11001764 spin_lock(&inode->i_lock);
David Howells3f19b2a2017-12-01 11:40:16 +00001765 state = inode->i_state;
1766 WARN_ON(state & I_NEW);
1767 state &= ~I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Nick Piggin7ccf19a2010-10-21 11:49:30 +11001769
David Howells3f19b2a2017-12-01 11:40:16 +00001770 WRITE_ONCE(inode->i_state, state | I_FREEING);
Eric Dumazetc4ae0c62011-07-28 06:55:13 +02001771 if (!list_empty(&inode->i_lru))
1772 inode_lru_list_del(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +11001773 spin_unlock(&inode->i_lock);
Dave Chinnerb2b2af82011-03-22 22:23:37 +11001774
1775 evict(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/**
Manish Katiyar6b3304b2009-03-31 19:35:54 +05301779 * iput - put an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * @inode: inode to put
1781 *
1782 * Puts an inode, dropping its usage count. If the inode use count hits
1783 * zero, the inode is then freed and may also be destroyed.
1784 *
1785 * Consequently, iput() can sleep.
1786 */
1787void iput(struct inode *inode)
1788{
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001789 if (!inode)
1790 return;
1791 BUG_ON(inode->i_state & I_CLEAR);
1792retry:
1793 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
1794 if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
1795 atomic_inc(&inode->i_count);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001796 spin_unlock(&inode->i_lock);
1797 trace_writeback_lazytime_iput(inode);
1798 mark_inode_dirty_sync(inode);
1799 goto retry;
1800 }
1801 iput_final(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804EXPORT_SYMBOL(iput);
1805
Carlos Maiolino30460e12020-01-09 14:30:41 +01001806#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/**
1808 * bmap - find a block number in a file
Carlos Maiolino30460e12020-01-09 14:30:41 +01001809 * @inode: inode owning the block number being requested
1810 * @block: pointer containing the block to find
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 *
Mauro Carvalho Chehab2b8e8b52020-04-14 18:48:57 +02001812 * Replaces the value in ``*block`` with the block number on the device holding
Carlos Maiolino30460e12020-01-09 14:30:41 +01001813 * corresponding to the requested block number in the file.
1814 * That is, asked for block 4 of inode 1 the function will replace the
Mauro Carvalho Chehab2b8e8b52020-04-14 18:48:57 +02001815 * 4 in ``*block``, with disk block relative to the disk start that holds that
Carlos Maiolino30460e12020-01-09 14:30:41 +01001816 * block of the file.
1817 *
1818 * Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a
Mauro Carvalho Chehab2b8e8b52020-04-14 18:48:57 +02001819 * hole, returns 0 and ``*block`` is also set to 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Carlos Maiolino30460e12020-01-09 14:30:41 +01001821int bmap(struct inode *inode, sector_t *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Carlos Maiolino30460e12020-01-09 14:30:41 +01001823 if (!inode->i_mapping->a_ops->bmap)
1824 return -EINVAL;
1825
1826 *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block);
1827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829EXPORT_SYMBOL(bmap);
Carlos Maiolino30460e12020-01-09 14:30:41 +01001830#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001832/*
1833 * With relative atime, only update atime if the previous atime is
Stephen Kittd98ffa12023-03-25 09:22:32 +01001834 * earlier than or equal to either the ctime or mtime,
1835 * or if at least a day has passed since the last atime update.
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001836 */
Miklos Szeredic6718542018-07-18 15:44:43 +02001837static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
Arnd Bergmann6f22b662019-04-26 16:50:41 +02001838 struct timespec64 now)
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001839{
Jeff Layton16a94962023-10-04 14:52:38 -04001840 struct timespec64 atime, mtime, ctime;
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001841
Miklos Szeredic6718542018-07-18 15:44:43 +02001842 if (!(mnt->mnt_flags & MNT_RELATIME))
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001843 return 1;
1844 /*
Stephen Kittd98ffa12023-03-25 09:22:32 +01001845 * Is mtime younger than or equal to atime? If yes, update atime:
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001846 */
Jeff Layton16a94962023-10-04 14:52:38 -04001847 atime = inode_get_atime(inode);
1848 mtime = inode_get_mtime(inode);
1849 if (timespec64_compare(&mtime, &atime) >= 0)
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001850 return 1;
1851 /*
Stephen Kittd98ffa12023-03-25 09:22:32 +01001852 * Is ctime younger than or equal to atime? If yes, update atime:
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001853 */
Jeff Layton2276e5b2023-07-05 15:00:50 -04001854 ctime = inode_get_ctime(inode);
Jeff Layton16a94962023-10-04 14:52:38 -04001855 if (timespec64_compare(&ctime, &atime) >= 0)
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001856 return 1;
1857
1858 /*
1859 * Is the previous atime value older than a day? If yes,
1860 * update atime:
1861 */
Jeff Layton16a94962023-10-04 14:52:38 -04001862 if ((long)(now.tv_sec - atime.tv_sec) >= 24*60*60)
Matthew Garrett11ff6f052009-03-26 17:32:14 +00001863 return 1;
1864 /*
1865 * Good, we can skip the atime update:
1866 */
1867 return 0;
1868}
1869
Jeff Layton541d4c72023-08-07 15:38:34 -04001870/**
1871 * inode_update_timestamps - update the timestamps on the inode
1872 * @inode: inode to be updated
1873 * @flags: S_* flags that needed to be updated
1874 *
1875 * The update_time function is called when an inode's timestamps need to be
1876 * updated for a read or write operation. This function handles updating the
1877 * actual timestamps. It's up to the caller to ensure that the inode is marked
1878 * dirty appropriately.
1879 *
1880 * In the case where any of S_MTIME, S_CTIME, or S_VERSION need to be updated,
1881 * attempt to update all three of them. S_ATIME updates can be handled
1882 * independently of the rest.
1883 *
1884 * Returns a set of S_* flags indicating which values changed.
1885 */
1886int inode_update_timestamps(struct inode *inode, int flags)
Josef Bacikc3b2da32012-03-26 09:59:21 -04001887{
Jeff Layton541d4c72023-08-07 15:38:34 -04001888 int updated = 0;
1889 struct timespec64 now;
Josef Bacikc3b2da32012-03-26 09:59:21 -04001890
Jeff Layton541d4c72023-08-07 15:38:34 -04001891 if (flags & (S_MTIME|S_CTIME|S_VERSION)) {
1892 struct timespec64 ctime = inode_get_ctime(inode);
Jeff Layton16a94962023-10-04 14:52:38 -04001893 struct timespec64 mtime = inode_get_mtime(inode);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001894
Jeff Layton541d4c72023-08-07 15:38:34 -04001895 now = inode_set_ctime_current(inode);
1896 if (!timespec64_equal(&now, &ctime))
1897 updated |= S_CTIME;
Jeff Layton16a94962023-10-04 14:52:38 -04001898 if (!timespec64_equal(&now, &mtime)) {
1899 inode_set_mtime_to_ts(inode, now);
Jeff Layton541d4c72023-08-07 15:38:34 -04001900 updated |= S_MTIME;
1901 }
1902 if (IS_I_VERSION(inode) && inode_maybe_inc_iversion(inode, updated))
1903 updated |= S_VERSION;
1904 } else {
1905 now = current_time(inode);
Eric Biggerse20b14d2021-01-12 11:02:45 -08001906 }
1907
Jeff Layton541d4c72023-08-07 15:38:34 -04001908 if (flags & S_ATIME) {
Jeff Layton16a94962023-10-04 14:52:38 -04001909 struct timespec64 atime = inode_get_atime(inode);
1910
1911 if (!timespec64_equal(&now, &atime)) {
1912 inode_set_atime_to_ts(inode, now);
Jeff Layton541d4c72023-08-07 15:38:34 -04001913 updated |= S_ATIME;
1914 }
1915 }
1916 return updated;
1917}
1918EXPORT_SYMBOL(inode_update_timestamps);
Eric Biggerse20b14d2021-01-12 11:02:45 -08001919
Jeff Layton541d4c72023-08-07 15:38:34 -04001920/**
1921 * generic_update_time - update the timestamps on the inode
1922 * @inode: inode to be updated
1923 * @flags: S_* flags that needed to be updated
1924 *
1925 * The update_time function is called when an inode's timestamps need to be
1926 * updated for a read or write operation. In the case where any of S_MTIME, S_CTIME,
1927 * or S_VERSION need to be updated we attempt to update all three of them. S_ATIME
1928 * updates can be handled done independently of the rest.
1929 *
1930 * Returns a S_* mask indicating which fields were updated.
1931 */
1932int generic_update_time(struct inode *inode, int flags)
1933{
1934 int updated = inode_update_timestamps(inode, flags);
1935 int dirty_flags = 0;
1936
1937 if (updated & (S_ATIME|S_MTIME|S_CTIME))
1938 dirty_flags = inode->i_sb->s_flags & SB_LAZYTIME ? I_DIRTY_TIME : I_DIRTY_SYNC;
1939 if (updated & S_VERSION)
1940 dirty_flags |= I_DIRTY_SYNC;
Eric Biggerse20b14d2021-01-12 11:02:45 -08001941 __mark_inode_dirty(inode, dirty_flags);
Jeff Layton541d4c72023-08-07 15:38:34 -04001942 return updated;
Josef Bacikc3b2da32012-03-26 09:59:21 -04001943}
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001944EXPORT_SYMBOL(generic_update_time);
1945
1946/*
1947 * This does the actual work of updating an inodes time or version. Must have
1948 * had called mnt_want_write() before calling this.
1949 */
Jeff Layton913e9922023-08-07 15:38:39 -04001950int inode_update_time(struct inode *inode, int flags)
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001951{
Deepa Dinamani23b424d2019-12-02 21:19:45 -08001952 if (inode->i_op->update_time)
Jeff Layton913e9922023-08-07 15:38:39 -04001953 return inode->i_op->update_time(inode, flags);
Jeff Layton541d4c72023-08-07 15:38:34 -04001954 generic_update_time(inode, flags);
1955 return 0;
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05001956}
Josef Bacike60feb42021-10-14 13:11:00 -04001957EXPORT_SYMBOL(inode_update_time);
Josef Bacikc3b2da32012-03-26 09:59:21 -04001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/**
Mauro Carvalho Chehab961f3c82021-01-14 09:04:39 +01001960 * atime_needs_update - update the access time
Randy Dunlap185553b2012-04-17 17:03:25 -07001961 * @path: the &struct path to update
Randy Dunlap30fdc8e2015-11-09 14:57:58 -08001962 * @inode: inode to update
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 *
1964 * Update the accessed time on an inode and mark it for writeback.
1965 * This function automatically handles read only file systems and media,
1966 * as well as the "noatime" flag and inode specific "noatime" markers.
1967 */
Miklos Szeredic6718542018-07-18 15:44:43 +02001968bool atime_needs_update(const struct path *path, struct inode *inode)
NeilBrown8fa9dd22015-03-23 13:37:40 +11001969{
1970 struct vfsmount *mnt = path->mnt;
Jeff Layton16a94962023-10-04 14:52:38 -04001971 struct timespec64 now, atime;
NeilBrown8fa9dd22015-03-23 13:37:40 +11001972
1973 if (inode->i_flags & S_NOATIME)
1974 return false;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05001975
1976 /* Atime updates will likely cause i_uid and i_gid to be written
1977 * back improprely if their true value is unknown to the vfs.
1978 */
Christian Brauner4609e1f2023-01-13 12:49:22 +01001979 if (HAS_UNMAPPED_ID(mnt_idmap(mnt), inode))
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05001980 return false;
1981
NeilBrown8fa9dd22015-03-23 13:37:40 +11001982 if (IS_NOATIME(inode))
1983 return false;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001984 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001985 return false;
1986
1987 if (mnt->mnt_flags & MNT_NOATIME)
1988 return false;
1989 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1990 return false;
1991
Deepa Dinamanic2050a42016-09-14 07:48:06 -07001992 now = current_time(inode);
NeilBrown8fa9dd22015-03-23 13:37:40 +11001993
Arnd Bergmann6f22b662019-04-26 16:50:41 +02001994 if (!relatime_need_update(mnt, inode, now))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001995 return false;
1996
Jeff Layton16a94962023-10-04 14:52:38 -04001997 atime = inode_get_atime(inode);
1998 if (timespec64_equal(&atime, &now))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001999 return false;
2000
2001 return true;
2002}
2003
Al Virobadcf2b2013-07-16 18:15:46 +04002004void touch_atime(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Al Viro68ac1232012-03-15 08:21:57 -04002006 struct vfsmount *mnt = path->mnt;
David Howellsdf2b1af2015-03-17 22:26:15 +00002007 struct inode *inode = d_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Miklos Szeredic6718542018-07-18 15:44:43 +02002009 if (!atime_needs_update(path, inode))
Andi Kleenb12536c2009-09-18 13:05:47 -07002010 return;
2011
Jan Kara5d37e9e2012-06-12 16:20:36 +02002012 if (!sb_start_write_trylock(inode->i_sb))
Andi Kleenb12536c2009-09-18 13:05:47 -07002013 return;
Valerie Henson47ae32d2006-12-13 00:34:34 -08002014
Amir Goldstein3e15dcf2023-09-08 16:28:59 +03002015 if (mnt_get_write_access(mnt) != 0)
Jan Kara5d37e9e2012-06-12 16:20:36 +02002016 goto skip_update;
Josef Bacikc3b2da32012-03-26 09:59:21 -04002017 /*
2018 * File systems can error out when updating inodes if they need to
2019 * allocate new space to modify an inode (such is the case for
2020 * Btrfs), but since we touch atime while walking down the path we
2021 * really don't care if we failed to update the atime of the file,
2022 * so just ignore the return value.
Alexander Block2bc5565282012-06-15 09:49:33 +02002023 * We may also fail on filesystems that have the ability to make parts
2024 * of the fs read only, e.g. subvolumes in Btrfs.
Josef Bacikc3b2da32012-03-26 09:59:21 -04002025 */
Jeff Layton913e9922023-08-07 15:38:39 -04002026 inode_update_time(inode, S_ATIME);
Amir Goldstein3e15dcf2023-09-08 16:28:59 +03002027 mnt_put_write_access(mnt);
Jan Kara5d37e9e2012-06-12 16:20:36 +02002028skip_update:
2029 sb_end_write(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
Christoph Hellwig869243a2006-01-09 20:52:03 -08002031EXPORT_SYMBOL(touch_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Cong Wang3ed37642012-05-15 14:57:33 +08002033/*
Jan Karadbfae0c2015-05-21 16:05:54 +02002034 * Return mask of changes for notify_change() that need to be done as a
2035 * response to write or truncate. Return 0 if nothing has to be changed.
2036 * Negative value on error (change should be denied).
2037 */
Christian Brauner9452e932023-01-13 12:49:27 +01002038int dentry_needs_remove_privs(struct mnt_idmap *idmap,
Christian Braunered5a7042022-10-17 17:06:37 +02002039 struct dentry *dentry)
Jan Karadbfae0c2015-05-21 16:05:54 +02002040{
Jan Karadbfae0c2015-05-21 16:05:54 +02002041 struct inode *inode = d_inode(dentry);
2042 int mask = 0;
2043 int ret;
2044
2045 if (IS_NOSEC(inode))
2046 return 0;
2047
Christian Brauner9452e932023-01-13 12:49:27 +01002048 mask = setattr_should_drop_suidgid(idmap, inode);
Jan Karadbfae0c2015-05-21 16:05:54 +02002049 ret = security_inode_need_killpriv(dentry);
2050 if (ret < 0)
2051 return ret;
2052 if (ret)
2053 mask |= ATTR_KILL_PRIV;
2054 return mask;
2055}
Jan Karadbfae0c2015-05-21 16:05:54 +02002056
Christian Braunerabf08572023-01-13 12:49:10 +01002057static int __remove_privs(struct mnt_idmap *idmap,
Christian Brauner643fe552021-01-21 14:19:34 +01002058 struct dentry *dentry, int kill)
Cong Wang3ed37642012-05-15 14:57:33 +08002059{
2060 struct iattr newattrs;
2061
2062 newattrs.ia_valid = ATTR_FORCE | kill;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -04002063 /*
2064 * Note we call this on write, so notify_change will not
2065 * encounter any conflicting delegations:
2066 */
Christian Braunerabf08572023-01-13 12:49:10 +01002067 return notify_change(idmap, dentry, &newattrs, NULL);
Cong Wang3ed37642012-05-15 14:57:33 +08002068}
2069
Stefan Roeschfaf99b52022-06-23 10:51:51 -07002070static int __file_remove_privs(struct file *file, unsigned int flags)
Cong Wang3ed37642012-05-15 14:57:33 +08002071{
Miklos Szeredic1892c32016-08-03 13:44:27 +02002072 struct dentry *dentry = file_dentry(file);
2073 struct inode *inode = file_inode(file);
Stefan Roesch41191cf2022-08-16 08:31:58 -07002074 int error = 0;
Jan Karadbfae0c2015-05-21 16:05:54 +02002075 int kill;
Cong Wang3ed37642012-05-15 14:57:33 +08002076
Alexander Lochmannf69e7492018-12-14 11:55:52 +01002077 if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode))
Cong Wang3ed37642012-05-15 14:57:33 +08002078 return 0;
2079
Christian Brauner9452e932023-01-13 12:49:27 +01002080 kill = dentry_needs_remove_privs(file_mnt_idmap(file), dentry);
Stefan Roesch41191cf2022-08-16 08:31:58 -07002081 if (kill < 0)
Jan Karadbfae0c2015-05-21 16:05:54 +02002082 return kill;
Stefan Roeschfaf99b52022-06-23 10:51:51 -07002083
Stefan Roesch41191cf2022-08-16 08:31:58 -07002084 if (kill) {
2085 if (flags & IOCB_NOWAIT)
2086 return -EAGAIN;
Stefan Roeschfaf99b52022-06-23 10:51:51 -07002087
Christian Braunerabf08572023-01-13 12:49:10 +01002088 error = __remove_privs(file_mnt_idmap(file), dentry, kill);
Stefan Roesch41191cf2022-08-16 08:31:58 -07002089 }
2090
Jan Kara2426f392015-05-21 16:05:52 +02002091 if (!error)
2092 inode_has_no_xattr(inode);
Cong Wang3ed37642012-05-15 14:57:33 +08002093 return error;
2094}
Stefan Roeschfaf99b52022-06-23 10:51:51 -07002095
2096/**
2097 * file_remove_privs - remove special file privileges (suid, capabilities)
2098 * @file: file to remove privileges from
2099 *
2100 * When file is modified by a write or truncation ensure that special
2101 * file privileges are removed.
2102 *
2103 * Return: 0 on success, negative errno on failure.
2104 */
2105int file_remove_privs(struct file *file)
2106{
2107 return __file_remove_privs(file, 0);
2108}
Jan Kara5fa8e0a2015-05-21 16:05:53 +02002109EXPORT_SYMBOL(file_remove_privs);
Cong Wang3ed37642012-05-15 14:57:33 +08002110
Jeff Layton913e9922023-08-07 15:38:39 -04002111static int inode_needs_update_time(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Josef Bacikc3b2da32012-03-26 09:59:21 -04002113 int sync_it = 0;
Christian Brauner647aa762023-09-20 16:40:13 +02002114 struct timespec64 now = current_time(inode);
Jeff Layton16a94962023-10-04 14:52:38 -04002115 struct timespec64 ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Andi Kleence06e0b2009-09-18 13:05:48 -07002117 /* First try to exhaust all avenues to not sync */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (IS_NOCMTIME(inode))
Josef Bacikc3b2da32012-03-26 09:59:21 -04002119 return 0;
Dave Hansen20ddee22008-02-15 14:37:43 -08002120
Jeff Layton16a94962023-10-04 14:52:38 -04002121 ts = inode_get_mtime(inode);
2122 if (!timespec64_equal(&ts, &now))
Andi Kleence06e0b2009-09-18 13:05:48 -07002123 sync_it = S_MTIME;
2124
Jeff Layton16a94962023-10-04 14:52:38 -04002125 ts = inode_get_ctime(inode);
2126 if (!timespec64_equal(&ts, &now))
Andi Kleence06e0b2009-09-18 13:05:48 -07002127 sync_it |= S_CTIME;
2128
Jeff Laytone38cf302017-12-11 06:35:22 -05002129 if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode))
Andi Kleence06e0b2009-09-18 13:05:48 -07002130 sync_it |= S_VERSION;
2131
Stefan Roesch6a2aa5d2022-06-23 10:51:52 -07002132 return sync_it;
2133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Jeff Layton913e9922023-08-07 15:38:39 -04002135static int __file_update_time(struct file *file, int sync_mode)
Stefan Roesch6a2aa5d2022-06-23 10:51:52 -07002136{
2137 int ret = 0;
2138 struct inode *inode = file_inode(file);
2139
2140 /* try to update time settings */
Amir Goldstein3e15dcf2023-09-08 16:28:59 +03002141 if (!mnt_get_write_access_file(file)) {
Jeff Layton913e9922023-08-07 15:38:39 -04002142 ret = inode_update_time(inode, sync_mode);
Amir Goldstein3e15dcf2023-09-08 16:28:59 +03002143 mnt_put_write_access_file(file);
Stefan Roesch6a2aa5d2022-06-23 10:51:52 -07002144 }
Josef Bacikc3b2da32012-03-26 09:59:21 -04002145
2146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
Stefan Roesch6a2aa5d2022-06-23 10:51:52 -07002148
2149/**
2150 * file_update_time - update mtime and ctime time
2151 * @file: file accessed
2152 *
2153 * Update the mtime and ctime members of an inode and mark the inode for
2154 * writeback. Note that this function is meant exclusively for usage in
2155 * the file write path of filesystems, and filesystems may choose to
2156 * explicitly ignore updates via this function with the _NOCMTIME inode
2157 * flag, e.g. for network filesystem where these imestamps are handled
2158 * by the server. This can return an error for file systems who need to
2159 * allocate space in order to update an inode.
2160 *
2161 * Return: 0 on success, negative errno on failure.
2162 */
2163int file_update_time(struct file *file)
2164{
2165 int ret;
2166 struct inode *inode = file_inode(file);
Stefan Roesch6a2aa5d2022-06-23 10:51:52 -07002167
Jeff Layton913e9922023-08-07 15:38:39 -04002168 ret = inode_needs_update_time(inode);
Stefan Roesch6a2aa5d2022-06-23 10:51:52 -07002169 if (ret <= 0)
2170 return ret;
2171
Jeff Layton913e9922023-08-07 15:38:39 -04002172 return __file_update_time(file, ret);
Stefan Roesch6a2aa5d2022-06-23 10:51:52 -07002173}
Christoph Hellwig870f4812006-01-09 20:52:01 -08002174EXPORT_SYMBOL(file_update_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Stefan Roeschfaf99b52022-06-23 10:51:51 -07002176/**
Stefan Roesch66fa3ce2022-06-23 10:51:53 -07002177 * file_modified_flags - handle mandated vfs changes when modifying a file
2178 * @file: file that was modified
2179 * @flags: kiocb flags
2180 *
2181 * When file has been modified ensure that special
2182 * file privileges are removed and time settings are updated.
2183 *
2184 * If IOCB_NOWAIT is set, special file privileges will not be removed and
2185 * time settings will not be updated. It will return -EAGAIN.
2186 *
2187 * Context: Caller must hold the file's inode lock.
2188 *
2189 * Return: 0 on success, negative errno on failure.
2190 */
2191static int file_modified_flags(struct file *file, int flags)
2192{
2193 int ret;
2194 struct inode *inode = file_inode(file);
Stefan Roesch66fa3ce2022-06-23 10:51:53 -07002195
2196 /*
2197 * Clear the security bits if the process is not being run by root.
2198 * This keeps people from modifying setuid and setgid binaries.
2199 */
2200 ret = __file_remove_privs(file, flags);
2201 if (ret)
2202 return ret;
2203
2204 if (unlikely(file->f_mode & FMODE_NOCMTIME))
2205 return 0;
2206
Jeff Layton913e9922023-08-07 15:38:39 -04002207 ret = inode_needs_update_time(inode);
Stefan Roesch66fa3ce2022-06-23 10:51:53 -07002208 if (ret <= 0)
2209 return ret;
2210 if (flags & IOCB_NOWAIT)
2211 return -EAGAIN;
2212
Jeff Layton913e9922023-08-07 15:38:39 -04002213 return __file_update_time(file, ret);
Stefan Roesch66fa3ce2022-06-23 10:51:53 -07002214}
2215
2216/**
Stefan Roeschfaf99b52022-06-23 10:51:51 -07002217 * file_modified - handle mandated vfs changes when modifying a file
2218 * @file: file that was modified
2219 *
2220 * When file has been modified ensure that special
2221 * file privileges are removed and time settings are updated.
2222 *
2223 * Context: Caller must hold the file's inode lock.
2224 *
2225 * Return: 0 on success, negative errno on failure.
2226 */
Amir Goldsteine38f7f52019-06-05 08:04:49 -07002227int file_modified(struct file *file)
2228{
Stefan Roesch66fa3ce2022-06-23 10:51:53 -07002229 return file_modified_flags(file, 0);
Amir Goldsteine38f7f52019-06-05 08:04:49 -07002230}
2231EXPORT_SYMBOL(file_modified);
2232
Stefan Roesch66fa3ce2022-06-23 10:51:53 -07002233/**
2234 * kiocb_modified - handle mandated vfs changes when modifying a file
2235 * @iocb: iocb that was modified
2236 *
2237 * When file has been modified ensure that special
2238 * file privileges are removed and time settings are updated.
2239 *
2240 * Context: Caller must hold the file's inode lock.
2241 *
2242 * Return: 0 on success, negative errno on failure.
2243 */
2244int kiocb_modified(struct kiocb *iocb)
2245{
2246 return file_modified_flags(iocb->ki_filp, iocb->ki_flags);
2247}
2248EXPORT_SYMBOL_GPL(kiocb_modified);
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250int inode_needs_sync(struct inode *inode)
2251{
2252 if (IS_SYNC(inode))
2253 return 1;
2254 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
2255 return 1;
2256 return 0;
2257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258EXPORT_SYMBOL(inode_needs_sync);
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260/*
Miklos Szeredi168a9fd2005-07-12 13:58:10 -07002261 * If we try to find an inode in the inode hash while it is being
2262 * deleted, we have to wait until the filesystem completes its
2263 * deletion before reporting that it isn't found. This function waits
2264 * until the deletion _might_ have completed. Callers are responsible
2265 * to recheck inode state.
2266 *
Christoph Hellwigeaff8072009-12-17 14:25:01 +01002267 * It doesn't matter if I_NEW is not set initially, a call to
Dave Chinner250df6e2011-03-22 22:23:36 +11002268 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
2269 * will DTRT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 */
2271static void __wait_on_freeing_inode(struct inode *inode)
2272{
2273 wait_queue_head_t *wq;
Christoph Hellwigeaff8072009-12-17 14:25:01 +01002274 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
2275 wq = bit_waitqueue(&inode->i_state, __I_NEW);
Ingo Molnar21417132017-03-05 11:25:39 +01002276 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
Dave Chinner250df6e2011-03-22 22:23:36 +11002277 spin_unlock(&inode->i_lock);
Dave Chinner67a23c42011-03-22 22:23:42 +11002278 spin_unlock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 schedule();
Ingo Molnar21417132017-03-05 11:25:39 +01002280 finish_wait(wq, &wait.wq_entry);
Dave Chinner67a23c42011-03-22 22:23:42 +11002281 spin_lock(&inode_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284static __initdata unsigned long ihash_entries;
2285static int __init set_ihash_entries(char *str)
2286{
2287 if (!str)
2288 return 0;
2289 ihash_entries = simple_strtoul(str, &str, 0);
2290 return 1;
2291}
2292__setup("ihash_entries=", set_ihash_entries);
2293
2294/*
2295 * Initialize the waitqueues and inode hash table.
2296 */
2297void __init inode_init_early(void)
2298{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* If hashes are distributed across NUMA nodes, defer
2300 * hash allocation until vmalloc space is available.
2301 */
2302 if (hashdist)
2303 return;
2304
2305 inode_hashtable =
2306 alloc_large_system_hash("Inode-cache",
2307 sizeof(struct hlist_head),
2308 ihash_entries,
2309 14,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07002310 HASH_EARLY | HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 &i_hash_shift,
2312 &i_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00002313 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Denis Cheng74bf17c2007-10-16 23:26:30 -07002317void __init inode_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 /* inode slab cache */
Paul Jacksonb0196002006-03-24 03:16:09 -08002320 inode_cachep = kmem_cache_create("inode_cache",
2321 sizeof(struct inode),
2322 0,
2323 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
Vladimir Davydov5d097052016-01-14 15:18:21 -08002324 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09002325 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 /* Hash may have been set up in inode_init_early */
2328 if (!hashdist)
2329 return;
2330
2331 inode_hashtable =
2332 alloc_large_system_hash("Inode-cache",
2333 sizeof(struct hlist_head),
2334 ihash_entries,
2335 14,
Pavel Tatashin3d375d72017-07-06 15:39:11 -07002336 HASH_ZERO,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 &i_hash_shift,
2338 &i_hash_mask,
Tim Bird31fe62b2012-05-23 13:33:35 +00002339 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
2344{
2345 inode->i_mode = mode;
2346 if (S_ISCHR(mode)) {
2347 inode->i_fop = &def_chr_fops;
2348 inode->i_rdev = rdev;
2349 } else if (S_ISBLK(mode)) {
Christoph Hellwigbda27952023-05-08 07:44:05 -07002350 if (IS_ENABLED(CONFIG_BLOCK))
2351 inode->i_fop = &def_blk_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 inode->i_rdev = rdev;
2353 } else if (S_ISFIFO(mode))
Al Viro599a0ac2013-03-12 09:58:10 -04002354 inode->i_fop = &pipefifo_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 else if (S_ISSOCK(mode))
Al Virobd9b51e2014-11-18 23:38:21 -05002356 ; /* leave it no_open_fops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 else
Manish Katiyaraf0d9ae2009-09-18 13:05:43 -07002358 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
2359 " inode %s:%lu\n", mode, inode->i_sb->s_id,
2360 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362EXPORT_SYMBOL(init_special_inode);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002363
2364/**
Ben Hutchingseaae668d2011-02-15 12:48:09 +00002365 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
Christian Braunerf2d40142023-01-13 12:49:25 +01002366 * @idmap: idmap of the mount the inode was created from
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002367 * @inode: New inode
2368 * @dir: Directory inode
2369 * @mode: mode of the new inode
Christian Brauner21cb47b2021-01-21 14:19:25 +01002370 *
Christian Braunerf2d40142023-01-13 12:49:25 +01002371 * If the inode has been created through an idmapped mount the idmap of
2372 * the vfsmount must be passed through @idmap. This function will then take
2373 * care to map the inode according to @idmap before checking permissions
Christian Brauner21cb47b2021-01-21 14:19:25 +01002374 * and initializing i_uid and i_gid. On non-idmapped mounts or if permission
Christian Braunerf2d40142023-01-13 12:49:25 +01002375 * checking is to be performed on the raw inode simply pass @nop_mnt_idmap.
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002376 */
Christian Braunerf2d40142023-01-13 12:49:25 +01002377void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode,
Christian Brauner21cb47b2021-01-21 14:19:25 +01002378 const struct inode *dir, umode_t mode)
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002379{
Christian Braunerc14329d2023-01-13 12:49:31 +01002380 inode_fsuid_set(inode, idmap);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002381 if (dir && dir->i_mode & S_ISGID) {
2382 inode->i_gid = dir->i_gid;
Linus Torvalds0fa3ecd2018-07-03 17:10:19 -07002383
2384 /* Directories are special, and always inherit S_ISGID */
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002385 if (S_ISDIR(mode))
2386 mode |= S_ISGID;
2387 } else
Christian Braunerc14329d2023-01-13 12:49:31 +01002388 inode_fsgid_set(inode, idmap);
Dmitry Monakhova1bd1202010-03-04 17:29:14 +03002389 inode->i_mode = mode;
2390}
2391EXPORT_SYMBOL(inode_init_owner);
Serge E. Hallyne795b712011-03-23 16:43:25 -07002392
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002393/**
2394 * inode_owner_or_capable - check current task permissions to inode
Christian Brauner01beba72023-01-13 12:49:26 +01002395 * @idmap: idmap of the mount the inode was found from
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002396 * @inode: inode being checked
2397 *
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002398 * Return true if current either has CAP_FOWNER in a namespace with the
2399 * inode owner uid mapped, or owns the file.
Christian Brauner21cb47b2021-01-21 14:19:25 +01002400 *
Christian Brauner01beba72023-01-13 12:49:26 +01002401 * If the inode has been found through an idmapped mount the idmap of
2402 * the vfsmount must be passed through @idmap. This function will then take
2403 * care to map the inode according to @idmap before checking permissions.
Christian Brauner21cb47b2021-01-21 14:19:25 +01002404 * On non-idmapped mounts or if permission checking is to be performed on the
Christian Brauner01beba72023-01-13 12:49:26 +01002405 * raw inode simply passs @nop_mnt_idmap.
Serge E. Hallyne795b712011-03-23 16:43:25 -07002406 */
Christian Brauner01beba72023-01-13 12:49:26 +01002407bool inode_owner_or_capable(struct mnt_idmap *idmap,
Christian Brauner21cb47b2021-01-21 14:19:25 +01002408 const struct inode *inode)
Serge E. Hallyne795b712011-03-23 16:43:25 -07002409{
Christian Braunera2bd0962022-06-22 22:12:16 +02002410 vfsuid_t vfsuid;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002411 struct user_namespace *ns;
2412
Christian Braunere67fe632023-01-13 12:49:30 +01002413 vfsuid = i_uid_into_vfsuid(idmap, inode);
Christian Braunera2bd0962022-06-22 22:12:16 +02002414 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
Serge E. Hallyne795b712011-03-23 16:43:25 -07002415 return true;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002416
2417 ns = current_user_ns();
Christian Braunera2bd0962022-06-22 22:12:16 +02002418 if (vfsuid_has_mapping(ns, vfsuid) && ns_capable(ns, CAP_FOWNER))
Serge E. Hallyne795b712011-03-23 16:43:25 -07002419 return true;
2420 return false;
2421}
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002422EXPORT_SYMBOL(inode_owner_or_capable);
Trond Myklebust1d59d612012-05-31 12:22:33 -04002423
2424/*
2425 * Direct i/o helper functions
2426 */
2427static void __inode_dio_wait(struct inode *inode)
2428{
2429 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
2430 DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
2431
2432 do {
Ingo Molnar21417132017-03-05 11:25:39 +01002433 prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE);
Trond Myklebust1d59d612012-05-31 12:22:33 -04002434 if (atomic_read(&inode->i_dio_count))
2435 schedule();
2436 } while (atomic_read(&inode->i_dio_count));
Ingo Molnar21417132017-03-05 11:25:39 +01002437 finish_wait(wq, &q.wq_entry);
Trond Myklebust1d59d612012-05-31 12:22:33 -04002438}
2439
2440/**
2441 * inode_dio_wait - wait for outstanding DIO requests to finish
2442 * @inode: inode to wait for
2443 *
2444 * Waits for all pending direct I/O requests to finish so that we can
2445 * proceed with a truncate or equivalent operation.
2446 *
2447 * Must be called under a lock that serializes taking new references
2448 * to i_dio_count, usually by inode->i_mutex.
2449 */
2450void inode_dio_wait(struct inode *inode)
2451{
2452 if (atomic_read(&inode->i_dio_count))
2453 __inode_dio_wait(inode);
2454}
2455EXPORT_SYMBOL(inode_dio_wait);
2456
2457/*
Theodore Ts'o5f16f322014-03-24 14:43:12 -04002458 * inode_set_flags - atomically set some inode flags
2459 *
2460 * Note: the caller should be holding i_mutex, or else be sure that
2461 * they have exclusive access to the inode structure (i.e., while the
2462 * inode is being instantiated). The reason for the cmpxchg() loop
2463 * --- which wouldn't be necessary if all code paths which modify
2464 * i_flags actually followed this rule, is that there is at least one
Jan Kara5fa8e0a2015-05-21 16:05:53 +02002465 * code path which doesn't today so we use cmpxchg() out of an abundance
2466 * of caution.
Theodore Ts'o5f16f322014-03-24 14:43:12 -04002467 *
2468 * In the long run, i_mutex is overkill, and we should probably look
2469 * at using the i_lock spinlock to protect i_flags, and then make sure
2470 * it is so documented in include/linux/fs.h and that all code follows
2471 * the locking convention!!
2472 */
2473void inode_set_flags(struct inode *inode, unsigned int flags,
2474 unsigned int mask)
2475{
Theodore Ts'o5f16f322014-03-24 14:43:12 -04002476 WARN_ON_ONCE(flags & ~mask);
Vineet Guptaa9057372019-03-05 15:41:52 -08002477 set_mask_bits(&inode->i_flags, mask, flags);
Theodore Ts'o5f16f322014-03-24 14:43:12 -04002478}
2479EXPORT_SYMBOL(inode_set_flags);
Al Viro21fc61c2015-11-17 01:07:57 -05002480
2481void inode_nohighmem(struct inode *inode)
2482{
2483 mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
2484}
2485EXPORT_SYMBOL(inode_nohighmem);
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002486
2487/**
Deepa Dinamani50e17c002018-01-21 18:04:25 -08002488 * timestamp_truncate - Truncate timespec to a granularity
2489 * @t: Timespec
2490 * @inode: inode being updated
2491 *
2492 * Truncate a timespec to the granularity supported by the fs
2493 * containing the inode. Always rounds down. gran must
2494 * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
2495 */
2496struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
2497{
2498 struct super_block *sb = inode->i_sb;
2499 unsigned int gran = sb->s_time_gran;
2500
2501 t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max);
2502 if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min))
2503 t.tv_nsec = 0;
2504
2505 /* Avoid division in the common cases 1 ns and 1 s. */
2506 if (gran == 1)
2507 ; /* nothing */
2508 else if (gran == NSEC_PER_SEC)
2509 t.tv_nsec = 0;
2510 else if (gran > 1 && gran < NSEC_PER_SEC)
2511 t.tv_nsec -= t.tv_nsec % gran;
2512 else
2513 WARN(1, "invalid file time granularity: %u", gran);
2514 return t;
2515}
2516EXPORT_SYMBOL(timestamp_truncate);
2517
2518/**
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002519 * current_time - Return FS time
2520 * @inode: inode.
2521 *
2522 * Return the current time truncated to the time granularity supported by
2523 * the fs.
2524 *
2525 * Note that inode and inode->sb cannot be NULL.
2526 * Otherwise, the function warns and returns time without truncation.
2527 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07002528struct timespec64 current_time(struct inode *inode)
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002529{
Arnd Bergmannd651d162018-12-05 11:14:27 +11002530 struct timespec64 now;
2531
2532 ktime_get_coarse_real_ts64(&now);
Deepa Dinamani50e17c002018-01-21 18:04:25 -08002533 return timestamp_truncate(now, inode);
Deepa Dinamani3cd88662016-09-14 07:48:02 -07002534}
2535EXPORT_SYMBOL(current_time);
Yang Xu2b3416c2022-07-14 14:11:25 +08002536
2537/**
Jeff Layton9b6304c2023-07-05 14:58:10 -04002538 * inode_set_ctime_current - set the ctime to current_time
2539 * @inode: inode
2540 *
2541 * Set the inode->i_ctime to the current value for the inode. Returns
2542 * the current value that was assigned to i_ctime.
2543 */
2544struct timespec64 inode_set_ctime_current(struct inode *inode)
2545{
Christian Brauner647aa762023-09-20 16:40:13 +02002546 struct timespec64 now = current_time(inode);
Jeff Layton9b6304c2023-07-05 14:58:10 -04002547
Christian Brauner647aa762023-09-20 16:40:13 +02002548 inode_set_ctime(inode, now.tv_sec, now.tv_nsec);
Jeff Layton9b6304c2023-07-05 14:58:10 -04002549 return now;
2550}
2551EXPORT_SYMBOL(inode_set_ctime_current);
2552
2553/**
Christian Brauner11c2a872022-10-17 17:06:34 +02002554 * in_group_or_capable - check whether caller is CAP_FSETID privileged
Christian Brauner9452e932023-01-13 12:49:27 +01002555 * @idmap: idmap of the mount @inode was found from
Christian Brauner11c2a872022-10-17 17:06:34 +02002556 * @inode: inode to check
2557 * @vfsgid: the new/current vfsgid of @inode
2558 *
2559 * Check wether @vfsgid is in the caller's group list or if the caller is
2560 * privileged with CAP_FSETID over @inode. This can be used to determine
2561 * whether the setgid bit can be kept or must be dropped.
2562 *
2563 * Return: true if the caller is sufficiently privileged, false if not.
2564 */
Christian Brauner9452e932023-01-13 12:49:27 +01002565bool in_group_or_capable(struct mnt_idmap *idmap,
Christian Brauner11c2a872022-10-17 17:06:34 +02002566 const struct inode *inode, vfsgid_t vfsgid)
2567{
2568 if (vfsgid_in_group_p(vfsgid))
2569 return true;
Christian Brauner9452e932023-01-13 12:49:27 +01002570 if (capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
Christian Brauner11c2a872022-10-17 17:06:34 +02002571 return true;
2572 return false;
2573}
2574
2575/**
Yang Xu2b3416c2022-07-14 14:11:25 +08002576 * mode_strip_sgid - handle the sgid bit for non-directories
Christian Brauner9452e932023-01-13 12:49:27 +01002577 * @idmap: idmap of the mount the inode was created from
Yang Xu2b3416c2022-07-14 14:11:25 +08002578 * @dir: parent directory inode
2579 * @mode: mode of the file to be created in @dir
2580 *
2581 * If the @mode of the new file has both the S_ISGID and S_IXGRP bit
2582 * raised and @dir has the S_ISGID bit raised ensure that the caller is
2583 * either in the group of the parent directory or they have CAP_FSETID
2584 * in their user namespace and are privileged over the parent directory.
2585 * In all other cases, strip the S_ISGID bit from @mode.
2586 *
2587 * Return: the new mode to use for the file
2588 */
Christian Brauner9452e932023-01-13 12:49:27 +01002589umode_t mode_strip_sgid(struct mnt_idmap *idmap,
Yang Xu2b3416c2022-07-14 14:11:25 +08002590 const struct inode *dir, umode_t mode)
2591{
2592 if ((mode & (S_ISGID | S_IXGRP)) != (S_ISGID | S_IXGRP))
2593 return mode;
2594 if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID))
2595 return mode;
Christian Braunere67fe632023-01-13 12:49:30 +01002596 if (in_group_or_capable(idmap, dir, i_gid_into_vfsgid(idmap, dir)))
Yang Xu2b3416c2022-07-14 14:11:25 +08002597 return mode;
Yang Xu2b3416c2022-07-14 14:11:25 +08002598 return mode & ~S_ISGID;
2599}
2600EXPORT_SYMBOL(mode_strip_sgid);