VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   57 Tags
457c899653991 (Thomas Gleixner         2019-05-19 13:08:55 +0100    1) // SPDX-License-Identifier: GPL-2.0-only
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    2) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    3)  * (C) 1997 Linus Torvalds
4b4563dc80594 (Christoph Hellwig       2011-05-27 09:28:01 -0400    4)  * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    5)  */
e59cc473cc603 (Al Viro                 2011-12-07 13:17:19 -0500    6) #include <linux/export.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    7) #include <linux/fs.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    8) #include <linux/mm.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700    9) #include <linux/backing-dev.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   10) #include <linux/hash.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   11) #include <linux/swap.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   12) #include <linux/security.h>
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   13) #include <linux/cdev.h>
57c8a661d95df (Mike Rapoport           2018-10-30 15:09:49 -0700   14) #include <linux/memblock.h>
3be25f49b9d6a (Eric Paris              2009-05-21 17:01:26 -0400   15) #include <linux/fsnotify.h>
fc33a7bb9c6dd (Christoph Hellwig       2006-01-09 20:52:17 -0800   16) #include <linux/mount.h>
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400   17) #include <linux/posix_acl.h>
9ce6e0be0626a (Heiko Carstens          2011-05-22 18:54:21 +0200   18) #include <linux/prefetch.h>
4b4563dc80594 (Christoph Hellwig       2011-05-27 09:28:01 -0400   19) #include <linux/buffer_head.h> /* for inode_has_buffers */
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100   20) #include <linux/ratelimit.h>
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000   21) #include <linux/list_lru.h>
ae5e165d855dd (Jeff Layton             2018-01-29 06:41:30 -0500   22) #include <linux/iversion.h>
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500   23) #include <trace/events/writeback.h>
a66979abad090 (Dave Chinner            2011-03-22 22:23:41 +1100   24) #include "internal.h"
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   25) 
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   26) /*
4b4563dc80594 (Christoph Hellwig       2011-05-27 09:28:01 -0400   27)  * Inode locking rules:
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   28)  *
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   29)  * inode->i_lock protects:
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   30)  *   inode->i_state, inode->i_hash, __iget()
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000   31)  * Inode LRU list locks protect:
98b745c647a5a (Dave Chinner            2011-07-08 14:14:39 +1000   32)  *   inode->i_sb->s_inode_lru, inode->i_lru
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500   33)  * inode->i_sb->s_inode_list_lock protects:
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500   34)  *   inode->i_sb->s_inodes, inode->i_sb_list
f758eeabeb96f (Christoph Hellwig       2011-04-21 18:19:44 -0600   35)  * bdi->wb.list_lock protects:
c7f5408493aeb (Dave Chinner            2015-03-04 14:07:22 -0500   36)  *   bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   37)  * inode_hash_lock protects:
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   38)  *   inode_hashtable, inode->i_hash
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   39)  *
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   40)  * Lock ordering:
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100   41)  *
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500   42)  * inode->i_sb->s_inode_list_lock
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100   43)  *   inode->i_lock
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000   44)  *     Inode LRU list locks
a66979abad090 (Dave Chinner            2011-03-22 22:23:41 +1100   45)  *
f758eeabeb96f (Christoph Hellwig       2011-04-21 18:19:44 -0600   46)  * bdi->wb.list_lock
a66979abad090 (Dave Chinner            2011-03-22 22:23:41 +1100   47)  *   inode->i_lock
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   48)  *
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   49)  * inode_hash_lock
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500   50)  *   inode->i_sb->s_inode_list_lock
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   51)  *   inode->i_lock
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   52)  *
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   53)  * iunique_lock
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   54)  *   inode_hash_lock
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   55)  */
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100   56) 
fa3536cc144c1 (Eric Dumazet            2006-03-26 01:37:24 -0800   57) static unsigned int i_hash_mask __read_mostly;
fa3536cc144c1 (Eric Dumazet            2006-03-26 01:37:24 -0800   58) static unsigned int i_hash_shift __read_mostly;
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   59) static struct hlist_head *inode_hashtable __read_mostly;
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100   60) static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   61) 
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   62) /*
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   63)  * Empty aops. Can be used for the cases where the user does not
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   64)  * define any of the address_space operations.
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   65)  */
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   66) const struct address_space_operations empty_aops = {
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   67) };
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   68) EXPORT_SYMBOL(empty_aops);
7dcda1c96d7c6 (Jens Axboe              2011-04-05 23:51:48 +0200   69) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   70) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   71)  * Statistics gathering..
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   72)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   73) struct inodes_stat_t inodes_stat;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   74) 
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000   75) static DEFINE_PER_CPU(unsigned long, nr_inodes);
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000   76) static DEFINE_PER_CPU(unsigned long, nr_unused);
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   77) 
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530   78) static struct kmem_cache *inode_cachep __read_mostly;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700   79) 
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000   80) static long get_nr_inodes(void)
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   81) {
3e880fb5e4bb6 (Nicholas Piggin         2011-01-07 17:49:19 +1100   82) 	int i;
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000   83) 	long sum = 0;
3e880fb5e4bb6 (Nicholas Piggin         2011-01-07 17:49:19 +1100   84) 	for_each_possible_cpu(i)
3e880fb5e4bb6 (Nicholas Piggin         2011-01-07 17:49:19 +1100   85) 		sum += per_cpu(nr_inodes, i);
3e880fb5e4bb6 (Nicholas Piggin         2011-01-07 17:49:19 +1100   86) 	return sum < 0 ? 0 : sum;
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   87) }
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   88) 
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000   89) static inline long get_nr_inodes_unused(void)
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   90) {
fcb94f72d3e0f (Dave Chinner            2011-07-08 14:14:38 +1000   91) 	int i;
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000   92) 	long sum = 0;
fcb94f72d3e0f (Dave Chinner            2011-07-08 14:14:38 +1000   93) 	for_each_possible_cpu(i)
fcb94f72d3e0f (Dave Chinner            2011-07-08 14:14:38 +1000   94) 		sum += per_cpu(nr_unused, i);
fcb94f72d3e0f (Dave Chinner            2011-07-08 14:14:38 +1000   95) 	return sum < 0 ? 0 : sum;
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   96) }
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   97) 
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000   98) long get_nr_dirty_inodes(void)
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400   99) {
3e880fb5e4bb6 (Nicholas Piggin         2011-01-07 17:49:19 +1100  100) 	/* not actually dirty inodes, but a wild approximation */
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000  101) 	long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  102) 	return nr_dirty > 0 ? nr_dirty : 0;
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  103) }
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  104) 
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  105) /*
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  106)  * Handle nr_inode sysctl
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  107)  */
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  108) #ifdef CONFIG_SYSCTL
1f7e0616cd4f5 (Joe Perches             2014-06-06 14:38:05 -0700  109) int proc_nr_inodes(struct ctl_table *table, int write,
32927393dc1cc (Christoph Hellwig       2020-04-24 08:43:38 +0200  110) 		   void *buffer, size_t *lenp, loff_t *ppos)
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  111) {
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  112) 	inodes_stat.nr_inodes = get_nr_inodes();
fcb94f72d3e0f (Dave Chinner            2011-07-08 14:14:38 +1000  113) 	inodes_stat.nr_unused = get_nr_inodes_unused();
3942c07ccf98e (Glauber Costa           2013-08-28 10:17:53 +1000  114) 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  115) }
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  116) #endif
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  117) 
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500  118) static int no_open(struct inode *inode, struct file *file)
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500  119) {
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500  120) 	return -ENXIO;
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500  121) }
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500  122) 
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  123) /**
6e7c2b4dd36d8 (Masahiro Yamada         2017-05-08 15:57:53 -0700  124)  * inode_init_always - perform inode structure initialisation
0bc02f3fa433a (Randy Dunlap            2009-01-06 14:41:13 -0800  125)  * @sb: superblock inode belongs to
0bc02f3fa433a (Randy Dunlap            2009-01-06 14:41:13 -0800  126)  * @inode: inode to initialise
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  127)  *
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  128)  * These are initializations that need to be done on every inode
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  129)  * allocation as the fields are not initialised by slab allocation.
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  130)  */
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  131) int inode_init_always(struct super_block *sb, struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  132) {
6e1d5dcc2bbbe (Alexey Dobriyan         2009-09-21 17:01:11 -0700  133) 	static const struct inode_operations empty_iops;
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500  134) 	static const struct file_operations no_open_fops = {.open = no_open};
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  135) 	struct address_space *const mapping = &inode->i_data;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  136) 
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  137) 	inode->i_sb = sb;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  138) 	inode->i_blkbits = sb->s_blocksize_bits;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  139) 	inode->i_flags = 0;
8019ad13ef7f6 (Peter Zijlstra          2020-03-04 11:28:31 +0100  140) 	atomic64_set(&inode->i_sequence, 0);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  141) 	atomic_set(&inode->i_count, 1);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  142) 	inode->i_op = &empty_iops;
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500  143) 	inode->i_fop = &no_open_fops;
edbb35cc6bdfc (Eric Biggers            2020-10-30 17:44:20 -0700  144) 	inode->i_ino = 0;
a78ef704a8dd4 (Miklos Szeredi          2011-10-28 14:13:30 +0200  145) 	inode->__i_nlink = 1;
3ddcd0569cd68 (Linus Torvalds          2011-08-06 22:45:50 -0700  146) 	inode->i_opflags = 0;
d0a5b995a3083 (Andreas Gruenbacher     2016-09-29 17:48:39 +0200  147) 	if (sb->s_xattr)
d0a5b995a3083 (Andreas Gruenbacher     2016-09-29 17:48:39 +0200  148) 		inode->i_opflags |= IOP_XATTR;
92361636e0153 (Eric W. Biederman       2012-02-08 07:07:50 -0800  149) 	i_uid_write(inode, 0);
92361636e0153 (Eric W. Biederman       2012-02-08 07:07:50 -0800  150) 	i_gid_write(inode, 0);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  151) 	atomic_set(&inode->i_writecount, 0);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  152) 	inode->i_size = 0;
c75b1d9421f80 (Jens Axboe              2017-06-27 11:47:04 -0600  153) 	inode->i_write_hint = WRITE_LIFE_NOT_SET;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  154) 	inode->i_blocks = 0;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  155) 	inode->i_bytes = 0;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  156) 	inode->i_generation = 0;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  157) 	inode->i_pipe = NULL;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  158) 	inode->i_cdev = NULL;
61ba64fc07688 (Al Viro                 2015-05-02 09:54:06 -0400  159) 	inode->i_link = NULL;
84e710da2a1df (Al Viro                 2016-04-15 00:58:55 -0400  160) 	inode->i_dir_seq = 0;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  161) 	inode->i_rdev = 0;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  162) 	inode->dirtied_when = 0;
6146f0d5e47ca (Mimi Zohar              2009-02-04 09:06:57 -0500  163) 
3d65ae4634ed8 (Tahsin Erdogan          2016-02-16 13:34:39 -0800  164) #ifdef CONFIG_CGROUP_WRITEBACK
3d65ae4634ed8 (Tahsin Erdogan          2016-02-16 13:34:39 -0800  165) 	inode->i_wb_frn_winner = 0;
3d65ae4634ed8 (Tahsin Erdogan          2016-02-16 13:34:39 -0800  166) 	inode->i_wb_frn_avg_time = 0;
3d65ae4634ed8 (Tahsin Erdogan          2016-02-16 13:34:39 -0800  167) 	inode->i_wb_frn_history = 0;
3d65ae4634ed8 (Tahsin Erdogan          2016-02-16 13:34:39 -0800  168) #endif
3d65ae4634ed8 (Tahsin Erdogan          2016-02-16 13:34:39 -0800  169) 
6146f0d5e47ca (Mimi Zohar              2009-02-04 09:06:57 -0500  170) 	if (security_inode_alloc(inode))
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  171) 		goto out;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  172) 	spin_lock_init(&inode->i_lock);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  173) 	lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  174) 
9902af79c01a8 (Al Viro                 2016-04-15 15:08:36 -0400  175) 	init_rwsem(&inode->i_rwsem);
9902af79c01a8 (Al Viro                 2016-04-15 15:08:36 -0400  176) 	lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  177) 
bd5fe6c5eb9c5 (Christoph Hellwig       2011-06-24 14:29:43 -0400  178) 	atomic_set(&inode->i_dio_count, 0);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  179) 
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  180) 	mapping->a_ops = &empty_aops;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  181) 	mapping->host = inode;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  182) 	mapping->flags = 0;
01c70267053d6 (Matthew Wilcox (Oracle) 2020-10-15 20:06:00 -0700  183) 	if (sb->s_type->fs_flags & FS_THP_SUPPORT)
01c70267053d6 (Matthew Wilcox (Oracle) 2020-10-15 20:06:00 -0700  184) 		__set_bit(AS_THP_SUPPORT, &mapping->flags);
829bc787c1a04 (Darrick J. Wong         2018-05-30 19:43:53 -0700  185) 	mapping->wb_err = 0;
4bb5f5d9395bc (David Rheinsberg        2014-08-08 14:25:25 -0700  186) 	atomic_set(&mapping->i_mmap_writable, 0);
09d91cda0e820 (Song Liu                2019-09-23 15:38:03 -0700  187) #ifdef CONFIG_READ_ONLY_THP_FOR_FS
09d91cda0e820 (Song Liu                2019-09-23 15:38:03 -0700  188) 	atomic_set(&mapping->nr_thps, 0);
09d91cda0e820 (Song Liu                2019-09-23 15:38:03 -0700  189) #endif
3c1d43787b48c (Hugh Dickins            2009-01-06 14:39:23 -0800  190) 	mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
252aa6f5be64c (Rafael Aquini           2012-12-11 16:02:35 -0800  191) 	mapping->private_data = NULL;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  192) 	mapping->writeback_index = 0;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  193) 	inode->i_private = NULL;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  194) 	inode->i_mapping = mapping;
b3d9b7a3c752d (Al Viro                 2012-06-09 13:51:19 -0400  195) 	INIT_HLIST_HEAD(&inode->i_dentry);	/* buggered by rcu freeing */
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400  196) #ifdef CONFIG_FS_POSIX_ACL
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400  197) 	inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400  198) #endif
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  199) 
3be25f49b9d6a (Eric Paris              2009-05-21 17:01:26 -0400  200) #ifdef CONFIG_FSNOTIFY
3be25f49b9d6a (Eric Paris              2009-05-21 17:01:26 -0400  201) 	inode->i_fsnotify_mask = 0;
3be25f49b9d6a (Eric Paris              2009-05-21 17:01:26 -0400  202) #endif
4a075e39c8649 (Jeff Layton             2015-01-16 15:05:54 -0500  203) 	inode->i_flctx = NULL;
3e880fb5e4bb6 (Nicholas Piggin         2011-01-07 17:49:19 +1100  204) 	this_cpu_inc(nr_inodes);
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  205) 
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  206) 	return 0;
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  207) out:
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  208) 	return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  209) }
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  210) EXPORT_SYMBOL(inode_init_always);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  211) 
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  212) void free_inode_nonrcu(struct inode *inode)
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  213) {
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  214) 	kmem_cache_free(inode_cachep, inode);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  215) }
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  216) EXPORT_SYMBOL(free_inode_nonrcu);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  217) 
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  218) static void i_callback(struct rcu_head *head)
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  219) {
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  220) 	struct inode *inode = container_of(head, struct inode, i_rcu);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  221) 	if (inode->free_inode)
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  222) 		inode->free_inode(inode);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  223) 	else
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  224) 		free_inode_nonrcu(inode);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  225) }
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  226) 
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  227) static struct inode *alloc_inode(struct super_block *sb)
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  228) {
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  229) 	const struct super_operations *ops = sb->s_op;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  230) 	struct inode *inode;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  231) 
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  232) 	if (ops->alloc_inode)
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  233) 		inode = ops->alloc_inode(sb);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  234) 	else
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  235) 		inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  236) 
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  237) 	if (!inode)
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  238) 		return NULL;
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  239) 
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  240) 	if (unlikely(inode_init_always(sb, inode))) {
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  241) 		if (ops->destroy_inode) {
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  242) 			ops->destroy_inode(inode);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  243) 			if (!ops->free_inode)
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  244) 				return NULL;
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  245) 		}
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  246) 		inode->free_inode = ops->free_inode;
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  247) 		i_callback(&inode->i_rcu);
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  248) 		return NULL;
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  249) 	}
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  250) 
54e346215e4fe (Christoph Hellwig       2009-08-07 14:38:25 -0300  251) 	return inode;
2cb1599f9b2ec (David Chinner           2008-10-30 17:32:23 +1100  252) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  253) 
2e00c97e2c1d2 (Christoph Hellwig       2009-08-07 14:38:29 -0300  254) void __destroy_inode(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  255) {
b7542f8c7eb40 (Eric Sesterhenn         2006-04-02 13:38:18 +0200  256) 	BUG_ON(inode_has_buffers(inode));
52ebea749aaed (Tejun Heo               2015-05-22 17:13:37 -0400  257) 	inode_detach_wb(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  258) 	security_inode_free(inode);
3be25f49b9d6a (Eric Paris              2009-05-21 17:01:26 -0400  259) 	fsnotify_inode_delete(inode);
f27a0fe083bf4 (Jeff Layton             2016-01-07 15:08:51 -0500  260) 	locks_free_lock_context(inode);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  261) 	if (!inode->i_nlink) {
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  262) 		WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  263) 		atomic_long_dec(&inode->i_sb->s_remove_count);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  264) 	}
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  265) 
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400  266) #ifdef CONFIG_FS_POSIX_ACL
b8a7a3a667472 (Andreas Gruenbacher     2016-03-24 14:38:37 +0100  267) 	if (inode->i_acl && !is_uncached_acl(inode->i_acl))
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400  268) 		posix_acl_release(inode->i_acl);
b8a7a3a667472 (Andreas Gruenbacher     2016-03-24 14:38:37 +0100  269) 	if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl))
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400  270) 		posix_acl_release(inode->i_default_acl);
f19d4a8fa6f9b (Al Viro                 2009-06-08 19:50:45 -0400  271) #endif
3e880fb5e4bb6 (Nicholas Piggin         2011-01-07 17:49:19 +1100  272) 	this_cpu_dec(nr_inodes);
2e00c97e2c1d2 (Christoph Hellwig       2009-08-07 14:38:29 -0300  273) }
2e00c97e2c1d2 (Christoph Hellwig       2009-08-07 14:38:29 -0300  274) EXPORT_SYMBOL(__destroy_inode);
2e00c97e2c1d2 (Christoph Hellwig       2009-08-07 14:38:29 -0300  275) 
56b0dacfa2b84 (Christoph Hellwig       2010-10-06 10:48:55 +0200  276) static void destroy_inode(struct inode *inode)
2e00c97e2c1d2 (Christoph Hellwig       2009-08-07 14:38:29 -0300  277) {
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  278) 	const struct super_operations *ops = inode->i_sb->s_op;
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  279) 
7ccf19a8042e3 (Nicholas Piggin         2010-10-21 11:49:30 +1100  280) 	BUG_ON(!list_empty(&inode->i_lru));
2e00c97e2c1d2 (Christoph Hellwig       2009-08-07 14:38:29 -0300  281) 	__destroy_inode(inode);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  282) 	if (ops->destroy_inode) {
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  283) 		ops->destroy_inode(inode);
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  284) 		if (!ops->free_inode)
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  285) 			return;
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  286) 	}
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  287) 	inode->free_inode = ops->free_inode;
fdb0da89f4ba0 (Al Viro                 2019-04-10 14:43:44 -0400  288) 	call_rcu(&inode->i_rcu, i_callback);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  289) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  290) 
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  291) /**
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  292)  * drop_nlink - directly drop an inode's link count
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  293)  * @inode: inode
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  294)  *
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  295)  * This is a low-level filesystem helper to replace any
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  296)  * direct filesystem manipulation of i_nlink.  In cases
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  297)  * where we are attempting to track writes to the
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  298)  * filesystem, a decrement to zero means an imminent
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  299)  * write when the file is truncated and actually unlinked
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  300)  * on the filesystem.
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  301)  */
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  302) void drop_nlink(struct inode *inode)
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  303) {
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  304) 	WARN_ON(inode->i_nlink == 0);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  305) 	inode->__i_nlink--;
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  306) 	if (!inode->i_nlink)
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  307) 		atomic_long_inc(&inode->i_sb->s_remove_count);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  308) }
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  309) EXPORT_SYMBOL(drop_nlink);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  310) 
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  311) /**
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  312)  * clear_nlink - directly zero an inode's link count
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  313)  * @inode: inode
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  314)  *
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  315)  * This is a low-level filesystem helper to replace any
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  316)  * direct filesystem manipulation of i_nlink.  See
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  317)  * drop_nlink() for why we care about i_nlink hitting zero.
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  318)  */
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  319) void clear_nlink(struct inode *inode)
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  320) {
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  321) 	if (inode->i_nlink) {
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  322) 		inode->__i_nlink = 0;
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  323) 		atomic_long_inc(&inode->i_sb->s_remove_count);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  324) 	}
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  325) }
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  326) EXPORT_SYMBOL(clear_nlink);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  327) 
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  328) /**
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  329)  * set_nlink - directly set an inode's link count
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  330)  * @inode: inode
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  331)  * @nlink: new nlink (should be non-zero)
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  332)  *
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  333)  * This is a low-level filesystem helper to replace any
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  334)  * direct filesystem manipulation of i_nlink.
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  335)  */
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  336) void set_nlink(struct inode *inode, unsigned int nlink)
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  337) {
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  338) 	if (!nlink) {
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  339) 		clear_nlink(inode);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  340) 	} else {
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  341) 		/* Yes, some filesystems do change nlink from zero to one */
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  342) 		if (inode->i_nlink == 0)
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  343) 			atomic_long_dec(&inode->i_sb->s_remove_count);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  344) 
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  345) 		inode->__i_nlink = nlink;
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  346) 	}
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  347) }
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  348) EXPORT_SYMBOL(set_nlink);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  349) 
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  350) /**
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  351)  * inc_nlink - directly increment an inode's link count
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  352)  * @inode: inode
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  353)  *
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  354)  * This is a low-level filesystem helper to replace any
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  355)  * direct filesystem manipulation of i_nlink.  Currently,
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  356)  * it is only here for parity with dec_nlink().
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  357)  */
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  358) void inc_nlink(struct inode *inode)
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  359) {
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400  360) 	if (unlikely(inode->i_nlink == 0)) {
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400  361) 		WARN_ON(!(inode->i_state & I_LINKABLE));
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  362) 		atomic_long_dec(&inode->i_sb->s_remove_count);
f4e0c30c191f8 (Al Viro                 2013-06-11 08:34:36 +0400  363) 	}
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  364) 
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  365) 	inode->__i_nlink++;
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  366) }
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  367) EXPORT_SYMBOL(inc_nlink);
7ada4db886344 (Miklos Szeredi          2011-11-21 12:11:32 +0100  368) 
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  369) static void __address_space_init_once(struct address_space *mapping)
2aa15890f3c19 (Miklos Szeredi          2011-02-23 13:49:47 +0100  370) {
7b785645e8f13 (Johannes Weiner         2019-05-24 10:12:46 -0400  371) 	xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
c8c06efa8b552 (Davidlohr Bueso         2014-12-12 16:54:24 -0800  372) 	init_rwsem(&mapping->i_mmap_rwsem);
2aa15890f3c19 (Miklos Szeredi          2011-02-23 13:49:47 +0100  373) 	INIT_LIST_HEAD(&mapping->private_list);
2aa15890f3c19 (Miklos Szeredi          2011-02-23 13:49:47 +0100  374) 	spin_lock_init(&mapping->private_lock);
f808c13fd3738 (Davidlohr Bueso         2017-09-08 16:15:08 -0700  375) 	mapping->i_mmap = RB_ROOT_CACHED;
2aa15890f3c19 (Miklos Szeredi          2011-02-23 13:49:47 +0100  376) }
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  377) 
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  378) void address_space_init_once(struct address_space *mapping)
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  379) {
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  380) 	memset(mapping, 0, sizeof(*mapping));
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  381) 	__address_space_init_once(mapping);
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  382) }
2aa15890f3c19 (Miklos Szeredi          2011-02-23 13:49:47 +0100  383) EXPORT_SYMBOL(address_space_init_once);
2aa15890f3c19 (Miklos Szeredi          2011-02-23 13:49:47 +0100  384) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  385) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  386)  * These are initializations that only need to be done
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  387)  * once, because the fields are idempotent across use
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  388)  * of the inode, so let the slab aware of that.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  389)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  390) void inode_init_once(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  391) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  392) 	memset(inode, 0, sizeof(*inode));
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  393) 	INIT_HLIST_NODE(&inode->i_hash);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  394) 	INIT_LIST_HEAD(&inode->i_devices);
c7f5408493aeb (Dave Chinner            2015-03-04 14:07:22 -0500  395) 	INIT_LIST_HEAD(&inode->i_io_list);
6c60d2b5746cf (Dave Chinner            2016-07-26 15:21:50 -0700  396) 	INIT_LIST_HEAD(&inode->i_wb_list);
7ccf19a8042e3 (Nicholas Piggin         2010-10-21 11:49:30 +1100  397) 	INIT_LIST_HEAD(&inode->i_lru);
ae23395d8858a (Dave Chinner            2018-03-06 17:30:34 -0800  398) 	__address_space_init_once(&inode->i_data);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  399) 	i_size_ordered_init(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  400) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  401) EXPORT_SYMBOL(inode_init_once);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  402) 
51cc50685a427 (Alexey Dobriyan         2008-07-25 19:45:34 -0700  403) static void init_once(void *foo)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  404) {
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  405) 	struct inode *inode = (struct inode *) foo;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  406) 
a35afb830f8d7 (Christoph Lameter       2007-05-16 22:10:57 -0700  407) 	inode_init_once(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  408) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  409) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  410) /*
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  411)  * inode->i_lock must be held
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  412)  */
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  413) void __iget(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  414) {
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  415) 	atomic_inc(&inode->i_count);
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  416) }
2e147f1ef7d59 (Richard Kennedy         2010-05-14 10:49:22 +0100  417) 
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  418) /*
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  419)  * get additional reference to inode; caller must already hold one.
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  420)  */
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  421) void ihold(struct inode *inode)
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  422) {
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  423) 	WARN_ON(atomic_inc_return(&inode->i_count) < 2);
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  424) }
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  425) EXPORT_SYMBOL(ihold);
7de9c6ee3ecff (Al Viro                 2010-10-23 11:11:40 -0400  426) 
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  427) static void inode_lru_list_add(struct inode *inode)
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  428) {
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  429) 	if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru))
fcb94f72d3e0f (Dave Chinner            2011-07-08 14:14:38 +1000  430) 		this_cpu_inc(nr_unused);
563f40019d16e (Josef Bacik             2017-04-18 16:04:17 -0400  431) 	else
563f40019d16e (Josef Bacik             2017-04-18 16:04:17 -0400  432) 		inode->i_state |= I_REFERENCED;
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  433) }
2e147f1ef7d59 (Richard Kennedy         2010-05-14 10:49:22 +0100  434) 
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  435) /*
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  436)  * Add inode to LRU if needed (inode is unused and clean).
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  437)  *
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  438)  * Needs inode->i_lock held.
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  439)  */
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  440) void inode_add_lru(struct inode *inode)
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  441) {
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500  442) 	if (!(inode->i_state & (I_DIRTY_ALL | I_SYNC |
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500  443) 				I_FREEING | I_WILL_FREE)) &&
1751e8a6cb935 (Linus Torvalds          2017-11-27 13:05:09 -0800  444) 	    !atomic_read(&inode->i_count) && inode->i_sb->s_flags & SB_ACTIVE)
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  445) 		inode_lru_list_add(inode);
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  446) }
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  447) 
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800  448) 
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  449) static void inode_lru_list_del(struct inode *inode)
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  450) {
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  451) 
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  452) 	if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru))
fcb94f72d3e0f (Dave Chinner            2011-07-08 14:14:38 +1000  453) 		this_cpu_dec(nr_unused);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  454) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  455) 
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  456) /**
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  457)  * inode_sb_list_add - add inode to the superblock list of inodes
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  458)  * @inode: inode to add
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  459)  */
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  460) void inode_sb_list_add(struct inode *inode)
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  461) {
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  462) 	spin_lock(&inode->i_sb->s_inode_list_lock);
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100  463) 	list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  464) 	spin_unlock(&inode->i_sb->s_inode_list_lock);
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  465) }
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  466) EXPORT_SYMBOL_GPL(inode_sb_list_add);
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  467) 
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100  468) static inline void inode_sb_list_del(struct inode *inode)
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  469) {
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  470) 	if (!list_empty(&inode->i_sb_list)) {
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  471) 		spin_lock(&inode->i_sb->s_inode_list_lock);
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  472) 		list_del_init(&inode->i_sb_list);
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  473) 		spin_unlock(&inode->i_sb->s_inode_list_lock);
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  474) 	}
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  475) }
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  476) 
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  477) static unsigned long hash(struct super_block *sb, unsigned long hashval)
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  478) {
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  479) 	unsigned long tmp;
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  480) 
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  481) 	tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  482) 			L1_CACHE_BYTES;
4b4563dc80594 (Christoph Hellwig       2011-05-27 09:28:01 -0400  483) 	tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
4b4563dc80594 (Christoph Hellwig       2011-05-27 09:28:01 -0400  484) 	return tmp & i_hash_mask;
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  485) }
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  486) 
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  487) /**
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  488)  *	__insert_inode_hash - hash an inode
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  489)  *	@inode: unhashed inode
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  490)  *	@hashval: unsigned long value used to locate this object in the
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  491)  *		inode_hashtable.
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  492)  *
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  493)  *	Add an inode to the inode hash for this superblock.
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  494)  */
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  495) void __insert_inode_hash(struct inode *inode, unsigned long hashval)
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  496) {
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  497) 	struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
646ec4615cd05 (Christoph Hellwig       2010-10-23 07:15:32 -0400  498) 
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100  499) 	spin_lock(&inode_hash_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  500) 	spin_lock(&inode->i_lock);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000  501) 	hlist_add_head_rcu(&inode->i_hash, b);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  502) 	spin_unlock(&inode->i_lock);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100  503) 	spin_unlock(&inode_hash_lock);
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  504) }
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  505) EXPORT_SYMBOL(__insert_inode_hash);
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  506) 
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  507) /**
f2ee7abf4c40c (Eric Dumazet            2011-07-28 06:41:09 +0200  508)  *	__remove_inode_hash - remove an inode from the hash
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  509)  *	@inode: inode to unhash
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  510)  *
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  511)  *	Remove an inode from the superblock.
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  512)  */
f2ee7abf4c40c (Eric Dumazet            2011-07-28 06:41:09 +0200  513) void __remove_inode_hash(struct inode *inode)
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  514) {
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100  515) 	spin_lock(&inode_hash_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  516) 	spin_lock(&inode->i_lock);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000  517) 	hlist_del_init_rcu(&inode->i_hash);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  518) 	spin_unlock(&inode->i_lock);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100  519) 	spin_unlock(&inode_hash_lock);
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  520) }
f2ee7abf4c40c (Eric Dumazet            2011-07-28 06:41:09 +0200  521) EXPORT_SYMBOL(__remove_inode_hash);
4c51acbc66f75 (Dave Chinner            2010-10-23 06:58:09 -0400  522) 
dbd5768f87ff6 (Jan Kara                2012-05-03 14:48:02 +0200  523) void clear_inode(struct inode *inode)
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  524) {
08142579b6ca3 (Jan Kara                2011-06-27 16:18:10 -0700  525) 	/*
b93b016313b3b (Matthew Wilcox          2018-04-10 16:36:56 -0700  526) 	 * We have to cycle the i_pages lock here because reclaim can be in the
08142579b6ca3 (Jan Kara                2011-06-27 16:18:10 -0700  527) 	 * process of removing the last page (in __delete_from_page_cache())
b93b016313b3b (Matthew Wilcox          2018-04-10 16:36:56 -0700  528) 	 * and we must not free the mapping under it.
08142579b6ca3 (Jan Kara                2011-06-27 16:18:10 -0700  529) 	 */
b93b016313b3b (Matthew Wilcox          2018-04-10 16:36:56 -0700  530) 	xa_lock_irq(&inode->i_data.i_pages);
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  531) 	BUG_ON(inode->i_data.nrpages);
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  532) 	/*
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  533) 	 * Almost always, mapping_empty(&inode->i_data) here; but there are
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  534) 	 * two known and long-standing ways in which nodes may get left behind
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  535) 	 * (when deep radix-tree node allocation failed partway; or when THP
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  536) 	 * collapse_file() failed). Until those two known cases are cleaned up,
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  537) 	 * or a cleanup function is called here, do not BUG_ON(!mapping_empty),
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  538) 	 * nor even WARN_ON(!mapping_empty).
786b31121a2ce (Hugh Dickins            2021-05-04 18:32:57 -0700  539) 	 */
b93b016313b3b (Matthew Wilcox          2018-04-10 16:36:56 -0700  540) 	xa_unlock_irq(&inode->i_data.i_pages);
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  541) 	BUG_ON(!list_empty(&inode->i_data.private_list));
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  542) 	BUG_ON(!(inode->i_state & I_FREEING));
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  543) 	BUG_ON(inode->i_state & I_CLEAR);
6c60d2b5746cf (Dave Chinner            2016-07-26 15:21:50 -0700  544) 	BUG_ON(!list_empty(&inode->i_wb_list));
fa0d7e3de6d6f (Nicholas Piggin         2011-01-07 17:49:49 +1100  545) 	/* don't need i_lock here, no concurrent mods to i_state */
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  546) 	inode->i_state = I_FREEING | I_CLEAR;
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  547) }
dbd5768f87ff6 (Jan Kara                2012-05-03 14:48:02 +0200  548) EXPORT_SYMBOL(clear_inode);
b0683aa638b33 (Al Viro                 2010-06-04 20:55:25 -0400  549) 
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  550) /*
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  551)  * Free the inode passed in, removing it from the lists it is still connected
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  552)  * to. We remove any pages still attached to the inode and wait for any IO that
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  553)  * is still in progress before finally destroying the inode.
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  554)  *
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  555)  * An inode must already be marked I_FREEING so that we avoid the inode being
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  556)  * moved back onto lists if we race with other code that manipulates the lists
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  557)  * (e.g. writeback_single_inode). The caller is responsible for setting this.
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  558)  *
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  559)  * An inode must already be removed from the LRU list before being evicted from
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  560)  * the cache. This should occur atomically with setting the I_FREEING state
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  561)  * flag, so no inodes here should ever be on the LRU when being evicted.
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  562)  */
644da5960ded1 (Al Viro                 2010-06-07 13:21:05 -0400  563) static void evict(struct inode *inode)
b4272d4c81053 (Al Viro                 2010-06-04 19:33:20 -0400  564) {
b4272d4c81053 (Al Viro                 2010-06-04 19:33:20 -0400  565) 	const struct super_operations *op = inode->i_sb->s_op;
b4272d4c81053 (Al Viro                 2010-06-04 19:33:20 -0400  566) 
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  567) 	BUG_ON(!(inode->i_state & I_FREEING));
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  568) 	BUG_ON(!list_empty(&inode->i_lru));
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  569) 
c7f5408493aeb (Dave Chinner            2015-03-04 14:07:22 -0500  570) 	if (!list_empty(&inode->i_io_list))
c7f5408493aeb (Dave Chinner            2015-03-04 14:07:22 -0500  571) 		inode_io_list_del(inode);
b12362bdb61a2 (Eric Dumazet            2011-07-28 06:11:47 +0200  572) 
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100  573) 	inode_sb_list_del(inode);
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100  574) 
169ebd90131b2 (Jan Kara                2012-05-03 14:48:03 +0200  575) 	/*
169ebd90131b2 (Jan Kara                2012-05-03 14:48:03 +0200  576) 	 * Wait for flusher thread to be done with the inode so that filesystem
169ebd90131b2 (Jan Kara                2012-05-03 14:48:03 +0200  577) 	 * does not start destroying it while writeback is still running. Since
169ebd90131b2 (Jan Kara                2012-05-03 14:48:03 +0200  578) 	 * the inode has I_FREEING set, flusher thread won't start new work on
169ebd90131b2 (Jan Kara                2012-05-03 14:48:03 +0200  579) 	 * the inode.  We just have to wait for running writeback to finish.
169ebd90131b2 (Jan Kara                2012-05-03 14:48:03 +0200  580) 	 */
169ebd90131b2 (Jan Kara                2012-05-03 14:48:03 +0200  581) 	inode_wait_for_writeback(inode);
7994e6f725435 (Jan Kara                2012-05-03 14:48:01 +0200  582) 
be7ce4161f9e6 (Al Viro                 2010-06-04 19:40:39 -0400  583) 	if (op->evict_inode) {
be7ce4161f9e6 (Al Viro                 2010-06-04 19:40:39 -0400  584) 		op->evict_inode(inode);
b4272d4c81053 (Al Viro                 2010-06-04 19:33:20 -0400  585) 	} else {
91b0abe36a7b2 (Johannes Weiner         2014-04-03 14:47:49 -0700  586) 		truncate_inode_pages_final(&inode->i_data);
dbd5768f87ff6 (Jan Kara                2012-05-03 14:48:02 +0200  587) 		clear_inode(inode);
b4272d4c81053 (Al Viro                 2010-06-04 19:33:20 -0400  588) 	}
661074e91b1da (Al Viro                 2010-06-04 20:19:55 -0400  589) 	if (S_ISCHR(inode->i_mode) && inode->i_cdev)
661074e91b1da (Al Viro                 2010-06-04 20:19:55 -0400  590) 		cd_forget(inode);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  591) 
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  592) 	remove_inode_hash(inode);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  593) 
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  594) 	spin_lock(&inode->i_lock);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  595) 	wake_up_bit(&inode->i_state, __I_NEW);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  596) 	BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  597) 	spin_unlock(&inode->i_lock);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  598) 
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100  599) 	destroy_inode(inode);
b4272d4c81053 (Al Viro                 2010-06-04 19:33:20 -0400  600) }
b4272d4c81053 (Al Viro                 2010-06-04 19:33:20 -0400  601) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  602) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  603)  * dispose_list - dispose of the contents of a local list
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  604)  * @head: the head of the list to free
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  605)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  606)  * Dispose-list gets a local list with local inodes in it, so it doesn't
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  607)  * need to worry about list corruption and SMP locks.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  608)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  609) static void dispose_list(struct list_head *head)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  610) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  611) 	while (!list_empty(head)) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  612) 		struct inode *inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  613) 
7ccf19a8042e3 (Nicholas Piggin         2010-10-21 11:49:30 +1100  614) 		inode = list_first_entry(head, struct inode, i_lru);
7ccf19a8042e3 (Nicholas Piggin         2010-10-21 11:49:30 +1100  615) 		list_del_init(&inode->i_lru);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  616) 
644da5960ded1 (Al Viro                 2010-06-07 13:21:05 -0400  617) 		evict(inode);
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  618) 		cond_resched();
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  619) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  620) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  621) 
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  622) /**
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  623)  * evict_inodes	- evict all evictable inodes for a superblock
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  624)  * @sb:		superblock to operate on
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  625)  *
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  626)  * Make sure that no inodes with zero refcount are retained.  This is
1751e8a6cb935 (Linus Torvalds          2017-11-27 13:05:09 -0800  627)  * called by superblock shutdown after having SB_ACTIVE flag removed,
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  628)  * so any inode reaching zero refcount during or after that call will
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  629)  * be immediately evicted.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  630)  */
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  631) void evict_inodes(struct super_block *sb)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  632) {
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  633) 	struct inode *inode, *next;
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  634) 	LIST_HEAD(dispose);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  635) 
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  636) again:
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  637) 	spin_lock(&sb->s_inode_list_lock);
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  638) 	list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  639) 		if (atomic_read(&inode->i_count))
aabb8fdb41128 (Nicholas Piggin         2009-03-11 13:17:36 -0700  640) 			continue;
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  641) 
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  642) 		spin_lock(&inode->i_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  643) 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  644) 			spin_unlock(&inode->i_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  645) 			continue;
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  646) 		}
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  647) 
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  648) 		inode->i_state |= I_FREEING;
02afc410f363f (Dave Chinner            2011-03-22 22:23:38 +1100  649) 		inode_lru_list_del(inode);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  650) 		spin_unlock(&inode->i_lock);
02afc410f363f (Dave Chinner            2011-03-22 22:23:38 +1100  651) 		list_add(&inode->i_lru, &dispose);
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  652) 
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  653) 		/*
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  654) 		 * We can have a ton of inodes to evict at unmount time given
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  655) 		 * enough memory, check to see if we need to go to sleep for a
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  656) 		 * bit so we don't livelock.
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  657) 		 */
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  658) 		if (need_resched()) {
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  659) 			spin_unlock(&sb->s_inode_list_lock);
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  660) 			cond_resched();
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  661) 			dispose_list(&dispose);
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  662) 			goto again;
ac05fbb400624 (Josef Bacik             2015-03-04 16:52:52 -0500  663) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  664) 	}
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  665) 	spin_unlock(&sb->s_inode_list_lock);
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  666) 
63997e98a3be6 (Al Viro                 2010-10-25 20:49:35 -0400  667) 	dispose_list(&dispose);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  668) }
799ea9e9c5994 (Darrick J. Wong         2017-08-18 18:08:25 -0700  669) EXPORT_SYMBOL_GPL(evict_inodes);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  670) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  671) /**
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  672)  * invalidate_inodes	- attempt to free all inodes on a superblock
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  673)  * @sb:		superblock to operate on
93b270f76e7ef (NeilBrown               2011-02-24 17:25:47 +1100  674)  * @kill_dirty: flag to guide handling of dirty inodes
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  675)  *
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  676)  * Attempts to free all inodes for a given superblock.  If there were any
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  677)  * busy inodes return a non-zero value, else zero.
93b270f76e7ef (NeilBrown               2011-02-24 17:25:47 +1100  678)  * If @kill_dirty is set, discard dirty inodes too, otherwise treat
93b270f76e7ef (NeilBrown               2011-02-24 17:25:47 +1100  679)  * them as busy.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  680)  */
93b270f76e7ef (NeilBrown               2011-02-24 17:25:47 +1100  681) int invalidate_inodes(struct super_block *sb, bool kill_dirty)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  682) {
cffbc8aa334f5 (Dave Chinner            2010-10-23 05:03:02 -0400  683) 	int busy = 0;
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  684) 	struct inode *inode, *next;
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  685) 	LIST_HEAD(dispose);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  686) 
04646aebd30b9 (Eric Sandeen            2019-12-06 10:54:23 -0600  687) again:
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  688) 	spin_lock(&sb->s_inode_list_lock);
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  689) 	list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  690) 		spin_lock(&inode->i_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  691) 		if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  692) 			spin_unlock(&inode->i_lock);
aabb8fdb41128 (Nicholas Piggin         2009-03-11 13:17:36 -0700  693) 			continue;
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  694) 		}
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500  695) 		if (inode->i_state & I_DIRTY_ALL && !kill_dirty) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  696) 			spin_unlock(&inode->i_lock);
93b270f76e7ef (NeilBrown               2011-02-24 17:25:47 +1100  697) 			busy = 1;
93b270f76e7ef (NeilBrown               2011-02-24 17:25:47 +1100  698) 			continue;
93b270f76e7ef (NeilBrown               2011-02-24 17:25:47 +1100  699) 		}
99a38919241fd (Christoph Hellwig       2010-10-23 19:07:20 +0200  700) 		if (atomic_read(&inode->i_count)) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  701) 			spin_unlock(&inode->i_lock);
99a38919241fd (Christoph Hellwig       2010-10-23 19:07:20 +0200  702) 			busy = 1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  703) 			continue;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  704) 		}
99a38919241fd (Christoph Hellwig       2010-10-23 19:07:20 +0200  705) 
99a38919241fd (Christoph Hellwig       2010-10-23 19:07:20 +0200  706) 		inode->i_state |= I_FREEING;
02afc410f363f (Dave Chinner            2011-03-22 22:23:38 +1100  707) 		inode_lru_list_del(inode);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  708) 		spin_unlock(&inode->i_lock);
02afc410f363f (Dave Chinner            2011-03-22 22:23:38 +1100  709) 		list_add(&inode->i_lru, &dispose);
04646aebd30b9 (Eric Sandeen            2019-12-06 10:54:23 -0600  710) 		if (need_resched()) {
04646aebd30b9 (Eric Sandeen            2019-12-06 10:54:23 -0600  711) 			spin_unlock(&sb->s_inode_list_lock);
04646aebd30b9 (Eric Sandeen            2019-12-06 10:54:23 -0600  712) 			cond_resched();
04646aebd30b9 (Eric Sandeen            2019-12-06 10:54:23 -0600  713) 			dispose_list(&dispose);
04646aebd30b9 (Eric Sandeen            2019-12-06 10:54:23 -0600  714) 			goto again;
04646aebd30b9 (Eric Sandeen            2019-12-06 10:54:23 -0600  715) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  716) 	}
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  717) 	spin_unlock(&sb->s_inode_list_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  718) 
a031878670ac8 (Christoph Hellwig       2010-10-24 19:40:33 +0200  719) 	dispose_list(&dispose);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  720) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  721) 	return busy;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  722) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  723) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  724) /*
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  725)  * Isolate the inode from the LRU in preparation for freeing it.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  726)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  727)  * Any inodes which are pinned purely because of attached pagecache have their
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  728)  * pagecache removed.  If the inode has metadata buffers attached to
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  729)  * mapping->private_list then try to remove them.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  730)  *
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  731)  * If the inode has the I_REFERENCED flag set, then it means that it has been
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  732)  * used recently - the flag is set in iput_final(). When we encounter such an
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  733)  * inode, clear the flag and move it to the back of the LRU so it gets another
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  734)  * pass through the LRU before it gets reclaimed. This is necessary because of
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  735)  * the fact we are doing lazy LRU updates to minimise lock contention so the
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  736)  * LRU does not have strict ordering. Hence we don't want to reclaim inodes
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  737)  * with this flag set because they are the inodes that are out of order.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  738)  */
3f97b163207c6 (Vladimir Davydov        2015-02-12 14:59:35 -0800  739) static enum lru_status inode_lru_isolate(struct list_head *item,
3f97b163207c6 (Vladimir Davydov        2015-02-12 14:59:35 -0800  740) 		struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  741) {
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  742) 	struct list_head *freeable = arg;
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  743) 	struct inode	*inode = container_of(item, struct inode, i_lru);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  744) 
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  745) 	/*
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  746) 	 * we are inverting the lru lock/inode->i_lock here, so use a trylock.
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  747) 	 * If we fail to get the lock, just skip it.
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  748) 	 */
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  749) 	if (!spin_trylock(&inode->i_lock))
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  750) 		return LRU_SKIP;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  751) 
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  752) 	/*
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  753) 	 * Referenced or dirty inodes are still in use. Give them another pass
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  754) 	 * through the LRU as we canot reclaim them now.
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  755) 	 */
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  756) 	if (atomic_read(&inode->i_count) ||
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  757) 	    (inode->i_state & ~I_REFERENCED)) {
3f97b163207c6 (Vladimir Davydov        2015-02-12 14:59:35 -0800  758) 		list_lru_isolate(lru, &inode->i_lru);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  759) 		spin_unlock(&inode->i_lock);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  760) 		this_cpu_dec(nr_unused);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  761) 		return LRU_REMOVED;
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  762) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  763) 
69056ee6a8a3d (Dave Chinner            2019-02-12 15:35:51 -0800  764) 	/* recently referenced inodes get one more pass */
69056ee6a8a3d (Dave Chinner            2019-02-12 15:35:51 -0800  765) 	if (inode->i_state & I_REFERENCED) {
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  766) 		inode->i_state &= ~I_REFERENCED;
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  767) 		spin_unlock(&inode->i_lock);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  768) 		return LRU_ROTATE;
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  769) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  770) 
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  771) 	if (inode_has_buffers(inode) || inode->i_data.nrpages) {
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  772) 		__iget(inode);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  773) 		spin_unlock(&inode->i_lock);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  774) 		spin_unlock(lru_lock);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  775) 		if (remove_inode_buffers(inode)) {
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  776) 			unsigned long reap;
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  777) 			reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  778) 			if (current_is_kswapd())
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  779) 				__count_vm_events(KSWAPD_INODESTEAL, reap);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  780) 			else
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  781) 				__count_vm_events(PGINODESTEAL, reap);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  782) 			if (current->reclaim_state)
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  783) 				current->reclaim_state->reclaimed_slab += reap;
02afc410f363f (Dave Chinner            2011-03-22 22:23:38 +1100  784) 		}
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  785) 		iput(inode);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  786) 		spin_lock(lru_lock);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  787) 		return LRU_RETRY;
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  788) 	}
02afc410f363f (Dave Chinner            2011-03-22 22:23:38 +1100  789) 
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  790) 	WARN_ON(inode->i_state & I_NEW);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  791) 	inode->i_state |= I_FREEING;
3f97b163207c6 (Vladimir Davydov        2015-02-12 14:59:35 -0800  792) 	list_lru_isolate_move(lru, &inode->i_lru, freeable);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  793) 	spin_unlock(&inode->i_lock);
9e38d86ff2d8a (Nicholas Piggin         2010-10-23 06:55:17 -0400  794) 
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  795) 	this_cpu_dec(nr_unused);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  796) 	return LRU_REMOVED;
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  797) }
7ccf19a8042e3 (Nicholas Piggin         2010-10-21 11:49:30 +1100  798) 
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  799) /*
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  800)  * Walk the superblock inode LRU for freeable inodes and attempt to free them.
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  801)  * This is called from the superblock shrinker function with a number of inodes
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  802)  * to trim from the LRU. Inodes to be freed are moved to a temporary list and
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  803)  * then are freed outside inode_lock by dispose_list().
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  804)  */
503c358cf1925 (Vladimir Davydov        2015-02-12 14:58:47 -0800  805) long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  806) {
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  807) 	LIST_HEAD(freeable);
bc3b14cb2d505 (Dave Chinner            2013-08-28 10:17:58 +1000  808) 	long freed;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  809) 
503c358cf1925 (Vladimir Davydov        2015-02-12 14:58:47 -0800  810) 	freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
503c358cf1925 (Vladimir Davydov        2015-02-12 14:58:47 -0800  811) 				     inode_lru_isolate, &freeable);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  812) 	dispose_list(&freeable);
0a234c6dcb79a (Dave Chinner            2013-08-28 10:17:57 +1000  813) 	return freed;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  814) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  815) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  816) static void __wait_on_freeing_inode(struct inode *inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  817) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  818)  * Called with the inode lock held.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  819)  */
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  820) static struct inode *find_inode(struct super_block *sb,
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  821) 				struct hlist_head *head,
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  822) 				int (*test)(struct inode *, void *),
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  823) 				void *data)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  824) {
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  825) 	struct inode *inode = NULL;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  826) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  827) repeat:
b67bfe0d42cac (Sasha Levin             2013-02-27 17:06:00 -0800  828) 	hlist_for_each_entry(inode, head, i_hash) {
5a3cd99285dc7 (Al Viro                 2013-11-06 09:54:52 -0500  829) 		if (inode->i_sb != sb)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  830) 			continue;
5a3cd99285dc7 (Al Viro                 2013-11-06 09:54:52 -0500  831) 		if (!test(inode, data))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  832) 			continue;
5a3cd99285dc7 (Al Viro                 2013-11-06 09:54:52 -0500  833) 		spin_lock(&inode->i_lock);
a4ffdde6e56fd (Al Viro                 2010-06-02 17:38:30 -0400  834) 		if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  835) 			__wait_on_freeing_inode(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  836) 			goto repeat;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  837) 		}
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  838) 		if (unlikely(inode->i_state & I_CREATING)) {
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  839) 			spin_unlock(&inode->i_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  840) 			return ERR_PTR(-ESTALE);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  841) 		}
f7899bd5472e8 (Christoph Hellwig       2010-10-23 07:09:06 -0400  842) 		__iget(inode);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  843) 		spin_unlock(&inode->i_lock);
f7899bd5472e8 (Christoph Hellwig       2010-10-23 07:09:06 -0400  844) 		return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  845) 	}
f7899bd5472e8 (Christoph Hellwig       2010-10-23 07:09:06 -0400  846) 	return NULL;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  847) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  848) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  849) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  850)  * find_inode_fast is the fast path version of find_inode, see the comment at
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  851)  * iget_locked for details.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  852)  */
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  853) static struct inode *find_inode_fast(struct super_block *sb,
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  854) 				struct hlist_head *head, unsigned long ino)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  855) {
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  856) 	struct inode *inode = NULL;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  857) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  858) repeat:
b67bfe0d42cac (Sasha Levin             2013-02-27 17:06:00 -0800  859) 	hlist_for_each_entry(inode, head, i_hash) {
5a3cd99285dc7 (Al Viro                 2013-11-06 09:54:52 -0500  860) 		if (inode->i_ino != ino)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  861) 			continue;
5a3cd99285dc7 (Al Viro                 2013-11-06 09:54:52 -0500  862) 		if (inode->i_sb != sb)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  863) 			continue;
5a3cd99285dc7 (Al Viro                 2013-11-06 09:54:52 -0500  864) 		spin_lock(&inode->i_lock);
a4ffdde6e56fd (Al Viro                 2010-06-02 17:38:30 -0400  865) 		if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  866) 			__wait_on_freeing_inode(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  867) 			goto repeat;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  868) 		}
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  869) 		if (unlikely(inode->i_state & I_CREATING)) {
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  870) 			spin_unlock(&inode->i_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  871) 			return ERR_PTR(-ESTALE);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400  872) 		}
f7899bd5472e8 (Christoph Hellwig       2010-10-23 07:09:06 -0400  873) 		__iget(inode);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100  874) 		spin_unlock(&inode->i_lock);
f7899bd5472e8 (Christoph Hellwig       2010-10-23 07:09:06 -0400  875) 		return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  876) 	}
f7899bd5472e8 (Christoph Hellwig       2010-10-23 07:09:06 -0400  877) 	return NULL;
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  878) }
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  879) 
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  880) /*
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  881)  * Each cpu owns a range of LAST_INO_BATCH numbers.
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  882)  * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  883)  * to renew the exhausted range.
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  884)  *
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  885)  * This does not significantly increase overflow rate because every CPU can
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  886)  * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  887)  * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  888)  * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  889)  * overflow rate by 2x, which does not seem too significant.
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  890)  *
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  891)  * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  892)  * error if st_ino won't fit in target struct field. Use 32bit counter
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  893)  * here to attempt to avoid that.
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  894)  */
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  895) #define LAST_INO_BATCH 1024
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  896) static DEFINE_PER_CPU(unsigned int, last_ino);
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  897) 
85fe4025c616a (Christoph Hellwig       2010-10-23 11:19:54 -0400  898) unsigned int get_next_ino(void)
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  899) {
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  900) 	unsigned int *p = &get_cpu_var(last_ino);
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  901) 	unsigned int res = *p;
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  902) 
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  903) #ifdef CONFIG_SMP
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  904) 	if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  905) 		static atomic_t shared_last_ino;
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  906) 		int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  907) 
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  908) 		res = next - LAST_INO_BATCH;
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  909) 	}
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  910) #endif
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  911) 
2adc376c55194 (Carlos Maiolino         2015-06-25 12:25:58 -0300  912) 	res++;
2adc376c55194 (Carlos Maiolino         2015-06-25 12:25:58 -0300  913) 	/* get_next_ino should not provide a 0 inode number */
2adc376c55194 (Carlos Maiolino         2015-06-25 12:25:58 -0300  914) 	if (unlikely(!res))
2adc376c55194 (Carlos Maiolino         2015-06-25 12:25:58 -0300  915) 		res++;
2adc376c55194 (Carlos Maiolino         2015-06-25 12:25:58 -0300  916) 	*p = res;
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  917) 	put_cpu_var(last_ino);
f991bd2e14210 (Eric Dumazet            2010-10-23 11:18:01 -0400  918) 	return res;
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  919) }
85fe4025c616a (Christoph Hellwig       2010-10-23 11:19:54 -0400  920) EXPORT_SYMBOL(get_next_ino);
8290c35f87304 (David Chinner           2008-10-30 17:35:24 +1100  921) 
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  922) /**
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  923)  *	new_inode_pseudo 	- obtain an inode
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  924)  *	@sb: superblock
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  925)  *
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  926)  *	Allocates a new inode for given superblock.
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  927)  *	Inode wont be chained in superblock s_inodes list
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  928)  *	This means :
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  929)  *	- fs can't be unmount
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  930)  *	- quotas, fsnotify, writeback can't work
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  931)  */
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  932) struct inode *new_inode_pseudo(struct super_block *sb)
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  933) {
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  934) 	struct inode *inode = alloc_inode(sb);
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  935) 
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  936) 	if (inode) {
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  937) 		spin_lock(&inode->i_lock);
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  938) 		inode->i_state = 0;
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  939) 		spin_unlock(&inode->i_lock);
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  940) 		INIT_LIST_HEAD(&inode->i_sb_list);
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  941) 	}
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  942) 	return inode;
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  943) }
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  944) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  945) /**
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  946)  *	new_inode 	- obtain an inode
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  947)  *	@sb: superblock
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  948)  *
769848c03895b (Mel Gorman              2007-07-17 04:03:05 -0700  949)  *	Allocates a new inode for given superblock. The default gfp_mask
3c1d43787b48c (Hugh Dickins            2009-01-06 14:39:23 -0800  950)  *	for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
769848c03895b (Mel Gorman              2007-07-17 04:03:05 -0700  951)  *	If HIGHMEM pages are unsuitable or it is known that pages allocated
769848c03895b (Mel Gorman              2007-07-17 04:03:05 -0700  952)  *	for the page cache are not reclaimable or migratable,
769848c03895b (Mel Gorman              2007-07-17 04:03:05 -0700  953)  *	mapping_set_gfp_mask() must be called with suitable flags on the
769848c03895b (Mel Gorman              2007-07-17 04:03:05 -0700  954)  *	newly created inode's mapping
769848c03895b (Mel Gorman              2007-07-17 04:03:05 -0700  955)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  956)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  957) struct inode *new_inode(struct super_block *sb)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  958) {
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  959) 	struct inode *inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  960) 
74278da9f70d8 (Dave Chinner            2015-03-04 12:37:22 -0500  961) 	spin_lock_prefetch(&sb->s_inode_list_lock);
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530  962) 
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  963) 	inode = new_inode_pseudo(sb);
a209dfc7b0d94 (Eric Dumazet            2011-07-26 11:36:34 +0200  964) 	if (inode)
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100  965) 		inode_sb_list_add(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  966) 	return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  967) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  968) EXPORT_SYMBOL(new_inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700  969) 
14358e6ddaed2 (Peter Zijlstra          2007-10-14 01:38:33 +0200  970) #ifdef CONFIG_DEBUG_LOCK_ALLOC
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  971) void lockdep_annotate_inode_mutex_key(struct inode *inode)
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  972) {
a3314a0ed389f (Namhyung Kim            2010-10-11 22:38:00 +0900  973) 	if (S_ISDIR(inode->i_mode)) {
1e89a5e15a2c8 (Peter Zijlstra          2007-10-16 06:47:54 +0200  974) 		struct file_system_type *type = inode->i_sb->s_type;
1e89a5e15a2c8 (Peter Zijlstra          2007-10-16 06:47:54 +0200  975) 
9a7aa12f39118 (Jan Kara                2009-06-04 15:26:49 +0200  976) 		/* Set new key only if filesystem hasn't already changed it */
9902af79c01a8 (Al Viro                 2016-04-15 15:08:36 -0400  977) 		if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) {
9a7aa12f39118 (Jan Kara                2009-06-04 15:26:49 +0200  978) 			/*
9a7aa12f39118 (Jan Kara                2009-06-04 15:26:49 +0200  979) 			 * ensure nobody is actually holding i_mutex
9a7aa12f39118 (Jan Kara                2009-06-04 15:26:49 +0200  980) 			 */
9902af79c01a8 (Al Viro                 2016-04-15 15:08:36 -0400  981) 			// mutex_destroy(&inode->i_mutex);
9902af79c01a8 (Al Viro                 2016-04-15 15:08:36 -0400  982) 			init_rwsem(&inode->i_rwsem);
9902af79c01a8 (Al Viro                 2016-04-15 15:08:36 -0400  983) 			lockdep_set_class(&inode->i_rwsem,
9a7aa12f39118 (Jan Kara                2009-06-04 15:26:49 +0200  984) 					  &type->i_mutex_dir_key);
9a7aa12f39118 (Jan Kara                2009-06-04 15:26:49 +0200  985) 		}
1e89a5e15a2c8 (Peter Zijlstra          2007-10-16 06:47:54 +0200  986) 	}
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  987) }
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  988) EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
14358e6ddaed2 (Peter Zijlstra          2007-10-14 01:38:33 +0200  989) #endif
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  990) 
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  991) /**
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  992)  * unlock_new_inode - clear the I_NEW state and wake up any waiters
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  993)  * @inode:	new inode to unlock
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  994)  *
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  995)  * Called when the inode is fully initialised to clear the new state of the
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  996)  * inode and wake up anyone waiting for the inode to finish initialisation.
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  997)  */
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  998) void unlock_new_inode(struct inode *inode)
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400  999) {
e096d0c7e2e4e (Josh Boyer              2011-08-25 07:48:12 -0400 1000) 	lockdep_annotate_inode_mutex_key(inode);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1001) 	spin_lock(&inode->i_lock);
eaff8079d4f10 (Christoph Hellwig       2009-12-17 14:25:01 +0100 1002) 	WARN_ON(!(inode->i_state & I_NEW));
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1003) 	inode->i_state &= ~I_NEW & ~I_CREATING;
310fa7a367220 (Al Viro                 2012-03-10 17:07:28 -0500 1004) 	smp_mb();
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1005) 	wake_up_bit(&inode->i_state, __I_NEW);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1006) 	spin_unlock(&inode->i_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1007) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1008) EXPORT_SYMBOL(unlock_new_inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1009) 
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1010) void discard_new_inode(struct inode *inode)
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1011) {
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1012) 	lockdep_annotate_inode_mutex_key(inode);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1013) 	spin_lock(&inode->i_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1014) 	WARN_ON(!(inode->i_state & I_NEW));
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1015) 	inode->i_state &= ~I_NEW;
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1016) 	smp_mb();
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1017) 	wake_up_bit(&inode->i_state, __I_NEW);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1018) 	spin_unlock(&inode->i_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1019) 	iput(inode);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1020) }
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1021) EXPORT_SYMBOL(discard_new_inode);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1022) 
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1023) /**
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1024)  * lock_two_nondirectories - take two i_mutexes on non-directory objects
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1025)  *
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1026)  * Lock any non-NULL argument that is not a directory.
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1027)  * Zero, one or two objects may be locked by this function.
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1028)  *
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1029)  * @inode1: first inode to lock
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1030)  * @inode2: second inode to lock
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1031)  */
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1032) void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1033) {
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1034) 	if (inode1 > inode2)
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1035) 		swap(inode1, inode2);
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1036) 
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1037) 	if (inode1 && !S_ISDIR(inode1->i_mode))
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 1038) 		inode_lock(inode1);
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1039) 	if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 1040) 		inode_lock_nested(inode2, I_MUTEX_NONDIR2);
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1041) }
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1042) EXPORT_SYMBOL(lock_two_nondirectories);
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1043) 
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1044) /**
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1045)  * unlock_two_nondirectories - release locks from lock_two_nondirectories()
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1046)  * @inode1: first inode to unlock
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1047)  * @inode2: second inode to unlock
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1048)  */
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1049) void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1050) {
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1051) 	if (inode1 && !S_ISDIR(inode1->i_mode))
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 1052) 		inode_unlock(inode1);
4fd699ae3fbca (J. Bruce Fields         2014-04-01 17:08:43 +0200 1053) 	if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
5955102c9984f (Al Viro                 2016-01-22 15:40:57 -0500 1054) 		inode_unlock(inode2);
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1055) }
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1056) EXPORT_SYMBOL(unlock_two_nondirectories);
375e289ea8516 (J. Bruce Fields         2012-04-18 15:16:33 -0400 1057) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1058) /**
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1059)  * inode_insert5 - obtain an inode from a mounted file system
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1060)  * @inode:	pre-allocated inode to use for insert to cache
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1061)  * @hashval:	hash value (usually inode number) to get
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1062)  * @test:	callback used for comparisons between inodes
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1063)  * @set:	callback used to initialize a new struct inode
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1064)  * @data:	opaque data pointer to pass to @test and @set
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1065)  *
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1066)  * Search for the inode specified by @hashval and @data in the inode cache,
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1067)  * and if present it is return it with an increased reference count. This is
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1068)  * a variant of iget5_locked() for callers that don't want to fail on memory
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1069)  * allocation of inode.
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1070)  *
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1071)  * If the inode is not in cache, insert the pre-allocated inode to cache and
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1072)  * return it locked, hashed, and with the I_NEW flag set. The file system gets
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1073)  * to fill it in before unlocking it via unlock_new_inode().
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1074)  *
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1075)  * Note both @test and @set are called with the inode_hash_lock held, so can't
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1076)  * sleep.
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1077)  */
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1078) struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1079) 			    int (*test)(struct inode *, void *),
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1080) 			    int (*set)(struct inode *, void *), void *data)
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1081) {
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1082) 	struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1083) 	struct inode *old;
e950564b97fd0 (Miklos Szeredi          2018-07-24 15:01:55 +0200 1084) 	bool creating = inode->i_state & I_CREATING;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1085) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1086) again:
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1087) 	spin_lock(&inode_hash_lock);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1088) 	old = find_inode(inode->i_sb, head, test, data);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1089) 	if (unlikely(old)) {
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1090) 		/*
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1091) 		 * Uhhuh, somebody else created the same inode under us.
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1092) 		 * Use the old inode instead of the preallocated one.
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1093) 		 */
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1094) 		spin_unlock(&inode_hash_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1095) 		if (IS_ERR(old))
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1096) 			return NULL;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1097) 		wait_on_inode(old);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1098) 		if (unlikely(inode_unhashed(old))) {
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1099) 			iput(old);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1100) 			goto again;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1101) 		}
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1102) 		return old;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1103) 	}
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1104) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1105) 	if (set && unlikely(set(inode, data))) {
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1106) 		inode = NULL;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1107) 		goto unlock;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1108) 	}
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1109) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1110) 	/*
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1111) 	 * Return the locked inode with I_NEW set, the
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1112) 	 * caller is responsible for filling in the contents
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1113) 	 */
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1114) 	spin_lock(&inode->i_lock);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1115) 	inode->i_state |= I_NEW;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1116) 	hlist_add_head_rcu(&inode->i_hash, head);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1117) 	spin_unlock(&inode->i_lock);
e950564b97fd0 (Miklos Szeredi          2018-07-24 15:01:55 +0200 1118) 	if (!creating)
e950564b97fd0 (Miklos Szeredi          2018-07-24 15:01:55 +0200 1119) 		inode_sb_list_add(inode);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1120) unlock:
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1121) 	spin_unlock(&inode_hash_lock);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1122) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1123) 	return inode;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1124) }
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1125) EXPORT_SYMBOL(inode_insert5);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1126) 
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1127) /**
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1128)  * iget5_locked - obtain an inode from a mounted file system
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1129)  * @sb:		super block of file system
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1130)  * @hashval:	hash value (usually inode number) to get
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1131)  * @test:	callback used for comparisons between inodes
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1132)  * @set:	callback used to initialize a new struct inode
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1133)  * @data:	opaque data pointer to pass to @test and @set
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1134)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1135)  * Search for the inode specified by @hashval and @data in the inode cache,
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1136)  * and if present it is return it with an increased reference count. This is
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1137)  * a generalized version of iget_locked() for file systems where the inode
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1138)  * number is not sufficient for unique identification of an inode.
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1139)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1140)  * If the inode is not in cache, allocate a new inode and return it locked,
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1141)  * hashed, and with the I_NEW flag set. The file system gets to fill it in
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1142)  * before unlocking it via unlock_new_inode().
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1143)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1144)  * Note both @test and @set are called with the inode_hash_lock held, so can't
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1145)  * sleep.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1146)  */
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1147) struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1148) 		int (*test)(struct inode *, void *),
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1149) 		int (*set)(struct inode *, void *), void *data)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1150) {
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1151) 	struct inode *inode = ilookup5(sb, hashval, test, data);
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1152) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1153) 	if (!inode) {
e950564b97fd0 (Miklos Szeredi          2018-07-24 15:01:55 +0200 1154) 		struct inode *new = alloc_inode(sb);
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1155) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1156) 		if (new) {
e950564b97fd0 (Miklos Szeredi          2018-07-24 15:01:55 +0200 1157) 			new->i_state = 0;
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1158) 			inode = inode_insert5(new, hashval, test, set, data);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1159) 			if (unlikely(inode != new))
e950564b97fd0 (Miklos Szeredi          2018-07-24 15:01:55 +0200 1160) 				destroy_inode(new);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1161) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1162) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1163) 	return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1164) }
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1165) EXPORT_SYMBOL(iget5_locked);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1166) 
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1167) /**
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1168)  * iget_locked - obtain an inode from a mounted file system
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1169)  * @sb:		super block of file system
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1170)  * @ino:	inode number to get
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1171)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1172)  * Search for the inode specified by @ino in the inode cache and if present
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1173)  * return it with an increased reference count. This is for file systems
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1174)  * where the inode number is sufficient for unique identification of an inode.
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1175)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1176)  * If the inode is not in cache, allocate a new inode and return it locked,
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1177)  * hashed, and with the I_NEW flag set.  The file system gets to fill it in
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1178)  * before unlocking it via unlock_new_inode().
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1179)  */
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1180) struct inode *iget_locked(struct super_block *sb, unsigned long ino)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1181) {
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1182) 	struct hlist_head *head = inode_hashtable + hash(sb, ino);
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530 1183) 	struct inode *inode;
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1184) again:
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1185) 	spin_lock(&inode_hash_lock);
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1186) 	inode = find_inode_fast(sb, head, ino);
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1187) 	spin_unlock(&inode_hash_lock);
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1188) 	if (inode) {
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1189) 		if (IS_ERR(inode))
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1190) 			return NULL;
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1191) 		wait_on_inode(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1192) 		if (unlikely(inode_unhashed(inode))) {
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1193) 			iput(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1194) 			goto again;
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1195) 		}
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1196) 		return inode;
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1197) 	}
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1198) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1199) 	inode = alloc_inode(sb);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1200) 	if (inode) {
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530 1201) 		struct inode *old;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1202) 
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1203) 		spin_lock(&inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1204) 		/* We released the lock, so.. */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1205) 		old = find_inode_fast(sb, head, ino);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1206) 		if (!old) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1207) 			inode->i_ino = ino;
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1208) 			spin_lock(&inode->i_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1209) 			inode->i_state = I_NEW;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1210) 			hlist_add_head_rcu(&inode->i_hash, head);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1211) 			spin_unlock(&inode->i_lock);
55fa6091d8316 (Dave Chinner            2011-03-22 22:23:40 +1100 1212) 			inode_sb_list_add(inode);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1213) 			spin_unlock(&inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1214) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1215) 			/* Return the locked inode with I_NEW set, the
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1216) 			 * caller is responsible for filling in the contents
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1217) 			 */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1218) 			return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1219) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1220) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1221) 		/*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1222) 		 * Uhhuh, somebody else created the same inode under
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1223) 		 * us. Use the old inode instead of the one we just
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1224) 		 * allocated.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1225) 		 */
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1226) 		spin_unlock(&inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1227) 		destroy_inode(inode);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1228) 		if (IS_ERR(old))
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1229) 			return NULL;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1230) 		inode = old;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1231) 		wait_on_inode(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1232) 		if (unlikely(inode_unhashed(inode))) {
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1233) 			iput(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1234) 			goto again;
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1235) 		}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1236) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1237) 	return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1238) }
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1239) EXPORT_SYMBOL(iget_locked);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1240) 
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1241) /*
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1242)  * search the inode cache for a matching inode number.
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1243)  * If we find one, then the inode number we are trying to
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1244)  * allocate is not unique and so we should not use it.
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1245)  *
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1246)  * Returns 1 if the inode number is unique, 0 if it is not.
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1247)  */
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1248) static int test_inode_iunique(struct super_block *sb, unsigned long ino)
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1249) {
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1250) 	struct hlist_head *b = inode_hashtable + hash(sb, ino);
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1251) 	struct inode *inode;
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1252) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1253) 	hlist_for_each_entry_rcu(inode, b, i_hash) {
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1254) 		if (inode->i_ino == ino && inode->i_sb == sb)
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1255) 			return 0;
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1256) 	}
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1257) 	return 1;
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1258) }
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1259) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1260) /**
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1261)  *	iunique - get a unique inode number
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1262)  *	@sb: superblock
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1263)  *	@max_reserved: highest reserved inode number
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1264)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1265)  *	Obtain an inode number that is unique on the system for a given
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1266)  *	superblock. This is used by file systems that have no natural
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1267)  *	permanent inode numbering system. An inode number is returned that
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1268)  *	is higher than the reserved limit but unique.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1269)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1270)  *	BUGS:
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1271)  *	With a large number of inodes live on the file system this function
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1272)  *	currently becomes quite slow.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1273)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1274) ino_t iunique(struct super_block *sb, ino_t max_reserved)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1275) {
866b04fccbf12 (Jeff Layton             2007-05-08 00:32:29 -0700 1276) 	/*
866b04fccbf12 (Jeff Layton             2007-05-08 00:32:29 -0700 1277) 	 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
866b04fccbf12 (Jeff Layton             2007-05-08 00:32:29 -0700 1278) 	 * error if st_ino won't fit in target struct field. Use 32bit counter
866b04fccbf12 (Jeff Layton             2007-05-08 00:32:29 -0700 1279) 	 * here to attempt to avoid that.
866b04fccbf12 (Jeff Layton             2007-05-08 00:32:29 -0700 1280) 	 */
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1281) 	static DEFINE_SPINLOCK(iunique_lock);
866b04fccbf12 (Jeff Layton             2007-05-08 00:32:29 -0700 1282) 	static unsigned int counter;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1283) 	ino_t res;
3361c7bebbf20 (Jeff Layton             2007-05-08 00:29:48 -0700 1284) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1285) 	rcu_read_lock();
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1286) 	spin_lock(&iunique_lock);
3361c7bebbf20 (Jeff Layton             2007-05-08 00:29:48 -0700 1287) 	do {
3361c7bebbf20 (Jeff Layton             2007-05-08 00:29:48 -0700 1288) 		if (counter <= max_reserved)
3361c7bebbf20 (Jeff Layton             2007-05-08 00:29:48 -0700 1289) 			counter = max_reserved + 1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1290) 		res = counter++;
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1291) 	} while (!test_inode_iunique(sb, res));
ad5e195ac9fdf (Christoph Hellwig       2010-10-23 07:00:16 -0400 1292) 	spin_unlock(&iunique_lock);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1293) 	rcu_read_unlock();
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1294) 
3361c7bebbf20 (Jeff Layton             2007-05-08 00:29:48 -0700 1295) 	return res;
3361c7bebbf20 (Jeff Layton             2007-05-08 00:29:48 -0700 1296) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1297) EXPORT_SYMBOL(iunique);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1298) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1299) struct inode *igrab(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1300) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1301) 	spin_lock(&inode->i_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1302) 	if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1303) 		__iget(inode);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1304) 		spin_unlock(&inode->i_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1305) 	} else {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1306) 		spin_unlock(&inode->i_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1307) 		/*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1308) 		 * Handle the case where s_op->clear_inode is not been
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1309) 		 * called yet, and somebody is calling igrab
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1310) 		 * while the inode is getting freed.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1311) 		 */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1312) 		inode = NULL;
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1313) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1314) 	return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1315) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1316) EXPORT_SYMBOL(igrab);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1317) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1318) /**
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1319)  * ilookup5_nowait - search for an inode in the inode cache
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1320)  * @sb:		super block of file system to search
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1321)  * @hashval:	hash value (usually inode number) to search for
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1322)  * @test:	callback used for comparisons between inodes
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1323)  * @data:	opaque data pointer to pass to @test
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1324)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1325)  * Search for the inode specified by @hashval and @data in the inode cache.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1326)  * If the inode is in the cache, the inode is returned with an incremented
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1327)  * reference count.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1328)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1329)  * Note: I_NEW is not waited upon so you have to be very careful what you do
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1330)  * with the returned inode.  You probably should be using ilookup5() instead.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1331)  *
b6d0ad686da95 (Randy Dunlap            2011-03-26 13:27:47 -0700 1332)  * Note2: @test is called with the inode_hash_lock held, so can't sleep.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1333)  */
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1334) struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1335) 		int (*test)(struct inode *, void *), void *data)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1336) {
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1337) 	struct hlist_head *head = inode_hashtable + hash(sb, hashval);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1338) 	struct inode *inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1339) 
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1340) 	spin_lock(&inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1341) 	inode = find_inode(sb, head, test, data);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1342) 	spin_unlock(&inode_hash_lock);
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1343) 
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1344) 	return IS_ERR(inode) ? NULL : inode;
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1345) }
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1346) EXPORT_SYMBOL(ilookup5_nowait);
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1347) 
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1348) /**
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1349)  * ilookup5 - search for an inode in the inode cache
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1350)  * @sb:		super block of file system to search
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1351)  * @hashval:	hash value (usually inode number) to search for
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1352)  * @test:	callback used for comparisons between inodes
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1353)  * @data:	opaque data pointer to pass to @test
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1354)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1355)  * Search for the inode specified by @hashval and @data in the inode cache,
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1356)  * and if the inode is in the cache, return the inode with an incremented
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1357)  * reference count.  Waits on I_NEW before returning the inode.
88bd5121d6351 (Anton Altaparmakov      2005-07-13 01:10:44 -0700 1358)  * returned with an incremented reference count.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1359)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1360)  * This is a generalized version of ilookup() for file systems where the
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1361)  * inode number is not sufficient for unique identification of an inode.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1362)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1363)  * Note: @test is called with the inode_hash_lock held, so can't sleep.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1364)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1365) struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1366) 		int (*test)(struct inode *, void *), void *data)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1367) {
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1368) 	struct inode *inode;
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1369) again:
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1370) 	inode = ilookup5_nowait(sb, hashval, test, data);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1371) 	if (inode) {
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1372) 		wait_on_inode(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1373) 		if (unlikely(inode_unhashed(inode))) {
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1374) 			iput(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1375) 			goto again;
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1376) 		}
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1377) 	}
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1378) 	return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1379) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1380) EXPORT_SYMBOL(ilookup5);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1381) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1382) /**
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1383)  * ilookup - search for an inode in the inode cache
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1384)  * @sb:		super block of file system to search
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1385)  * @ino:	inode number to search for
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1386)  *
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1387)  * Search for the inode @ino in the inode cache, and if the inode is in the
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1388)  * cache, the inode is returned with an incremented reference count.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1389)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1390) struct inode *ilookup(struct super_block *sb, unsigned long ino)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1391) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1392) 	struct hlist_head *head = inode_hashtable + hash(sb, ino);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1393) 	struct inode *inode;
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1394) again:
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1395) 	spin_lock(&inode_hash_lock);
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1396) 	inode = find_inode_fast(sb, head, ino);
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1397) 	spin_unlock(&inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1398) 
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1399) 	if (inode) {
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1400) 		if (IS_ERR(inode))
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1401) 			return NULL;
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1402) 		wait_on_inode(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1403) 		if (unlikely(inode_unhashed(inode))) {
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1404) 			iput(inode);
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1405) 			goto again;
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1406) 		}
2864f30142422 (Al Viro                 2016-07-03 23:15:21 -0400 1407) 	}
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1408) 	return inode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1409) }
0b2d0724e26a3 (Christoph Hellwig       2011-03-23 15:03:28 -0400 1410) EXPORT_SYMBOL(ilookup);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1411) 
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1412) /**
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1413)  * find_inode_nowait - find an inode in the inode cache
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1414)  * @sb:		super block of file system to search
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1415)  * @hashval:	hash value (usually inode number) to search for
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1416)  * @match:	callback used for comparisons between inodes
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1417)  * @data:	opaque data pointer to pass to @match
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1418)  *
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1419)  * Search for the inode specified by @hashval and @data in the inode
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1420)  * cache, where the helper function @match will return 0 if the inode
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1421)  * does not match, 1 if the inode does match, and -1 if the search
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1422)  * should be stopped.  The @match function must be responsible for
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1423)  * taking the i_lock spin_lock and checking i_state for an inode being
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1424)  * freed or being initialized, and incrementing the reference count
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1425)  * before returning 1.  It also must not sleep, since it is called with
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1426)  * the inode_hash_lock spinlock held.
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1427)  *
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1428)  * This is a even more generalized version of ilookup5() when the
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1429)  * function must never block --- find_inode() can block in
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1430)  * __wait_on_freeing_inode() --- or when the caller can not increment
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1431)  * the reference count because the resulting iput() might cause an
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1432)  * inode eviction.  The tradeoff is that the @match funtion must be
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1433)  * very carefully implemented.
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1434)  */
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1435) struct inode *find_inode_nowait(struct super_block *sb,
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1436) 				unsigned long hashval,
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1437) 				int (*match)(struct inode *, unsigned long,
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1438) 					     void *),
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1439) 				void *data)
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1440) {
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1441) 	struct hlist_head *head = inode_hashtable + hash(sb, hashval);
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1442) 	struct inode *inode, *ret_inode = NULL;
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1443) 	int mval;
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1444) 
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1445) 	spin_lock(&inode_hash_lock);
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1446) 	hlist_for_each_entry(inode, head, i_hash) {
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1447) 		if (inode->i_sb != sb)
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1448) 			continue;
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1449) 		mval = match(inode, hashval, data);
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1450) 		if (mval == 0)
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1451) 			continue;
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1452) 		if (mval == 1)
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1453) 			ret_inode = inode;
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1454) 		goto out;
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1455) 	}
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1456) out:
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1457) 	spin_unlock(&inode_hash_lock);
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1458) 	return ret_inode;
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1459) }
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1460) EXPORT_SYMBOL(find_inode_nowait);
fe032c422c5ba (Theodore Ts'o           2015-02-02 00:37:01 -0500 1461) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1462) /**
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1463)  * find_inode_rcu - find an inode in the inode cache
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1464)  * @sb:		Super block of file system to search
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1465)  * @hashval:	Key to hash
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1466)  * @test:	Function to test match on an inode
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1467)  * @data:	Data for test function
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1468)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1469)  * Search for the inode specified by @hashval and @data in the inode cache,
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1470)  * where the helper function @test will return 0 if the inode does not match
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1471)  * and 1 if it does.  The @test function must be responsible for taking the
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1472)  * i_lock spin_lock and checking i_state for an inode being freed or being
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1473)  * initialized.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1474)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1475)  * If successful, this will return the inode for which the @test function
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1476)  * returned 1 and NULL otherwise.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1477)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1478)  * The @test function is not permitted to take a ref on any inode presented.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1479)  * It is also not permitted to sleep.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1480)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1481)  * The caller must hold the RCU read lock.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1482)  */
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1483) struct inode *find_inode_rcu(struct super_block *sb, unsigned long hashval,
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1484) 			     int (*test)(struct inode *, void *), void *data)
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1485) {
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1486) 	struct hlist_head *head = inode_hashtable + hash(sb, hashval);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1487) 	struct inode *inode;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1488) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1489) 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1490) 			 "suspicious find_inode_rcu() usage");
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1491) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1492) 	hlist_for_each_entry_rcu(inode, head, i_hash) {
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1493) 		if (inode->i_sb == sb &&
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1494) 		    !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)) &&
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1495) 		    test(inode, data))
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1496) 			return inode;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1497) 	}
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1498) 	return NULL;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1499) }
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1500) EXPORT_SYMBOL(find_inode_rcu);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1501) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1502) /**
961f3c898e86d (Mauro Carvalho Chehab   2021-01-14 09:04:39 +0100 1503)  * find_inode_by_ino_rcu - Find an inode in the inode cache
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1504)  * @sb:		Super block of file system to search
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1505)  * @ino:	The inode number to match
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1506)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1507)  * Search for the inode specified by @hashval and @data in the inode cache,
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1508)  * where the helper function @test will return 0 if the inode does not match
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1509)  * and 1 if it does.  The @test function must be responsible for taking the
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1510)  * i_lock spin_lock and checking i_state for an inode being freed or being
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1511)  * initialized.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1512)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1513)  * If successful, this will return the inode for which the @test function
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1514)  * returned 1 and NULL otherwise.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1515)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1516)  * The @test function is not permitted to take a ref on any inode presented.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1517)  * It is also not permitted to sleep.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1518)  *
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1519)  * The caller must hold the RCU read lock.
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1520)  */
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1521) struct inode *find_inode_by_ino_rcu(struct super_block *sb,
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1522) 				    unsigned long ino)
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1523) {
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1524) 	struct hlist_head *head = inode_hashtable + hash(sb, ino);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1525) 	struct inode *inode;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1526) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1527) 	RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1528) 			 "suspicious find_inode_by_ino_rcu() usage");
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1529) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1530) 	hlist_for_each_entry_rcu(inode, head, i_hash) {
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1531) 		if (inode->i_ino == ino &&
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1532) 		    inode->i_sb == sb &&
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1533) 		    !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)))
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1534) 		    return inode;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1535) 	}
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1536) 	return NULL;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1537) }
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1538) EXPORT_SYMBOL(find_inode_by_ino_rcu);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1539) 
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1540) int insert_inode_locked(struct inode *inode)
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1541) {
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1542) 	struct super_block *sb = inode->i_sb;
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1543) 	ino_t ino = inode->i_ino;
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1544) 	struct hlist_head *head = inode_hashtable + hash(sb, ino);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1545) 
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1546) 	while (1) {
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1547) 		struct inode *old = NULL;
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1548) 		spin_lock(&inode_hash_lock);
b67bfe0d42cac (Sasha Levin             2013-02-27 17:06:00 -0800 1549) 		hlist_for_each_entry(old, head, i_hash) {
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1550) 			if (old->i_ino != ino)
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1551) 				continue;
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1552) 			if (old->i_sb != sb)
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1553) 				continue;
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1554) 			spin_lock(&old->i_lock);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1555) 			if (old->i_state & (I_FREEING|I_WILL_FREE)) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1556) 				spin_unlock(&old->i_lock);
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1557) 				continue;
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1558) 			}
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1559) 			break;
72a43d63cb510 (Al Viro                 2009-05-13 19:13:40 +0100 1560) 		}
b67bfe0d42cac (Sasha Levin             2013-02-27 17:06:00 -0800 1561) 		if (likely(!old)) {
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1562) 			spin_lock(&inode->i_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1563) 			inode->i_state |= I_NEW | I_CREATING;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1564) 			hlist_add_head_rcu(&inode->i_hash, head);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1565) 			spin_unlock(&inode->i_lock);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1566) 			spin_unlock(&inode_hash_lock);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1567) 			return 0;
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1568) 		}
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1569) 		if (unlikely(old->i_state & I_CREATING)) {
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1570) 			spin_unlock(&old->i_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1571) 			spin_unlock(&inode_hash_lock);
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1572) 			return -EBUSY;
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1573) 		}
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1574) 		__iget(old);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1575) 		spin_unlock(&old->i_lock);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 1576) 		spin_unlock(&inode_hash_lock);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1577) 		wait_on_inode(old);
1d3382cbf0298 (Al Viro                 2010-10-23 15:19:20 -0400 1578) 		if (unlikely(!inode_unhashed(old))) {
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1579) 			iput(old);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1580) 			return -EBUSY;
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1581) 		}
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1582) 		iput(old);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1583) 	}
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1584) }
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1585) EXPORT_SYMBOL(insert_inode_locked);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1586) 
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1587) int insert_inode_locked4(struct inode *inode, unsigned long hashval,
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1588) 		int (*test)(struct inode *, void *), void *data)
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1589) {
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1590) 	struct inode *old;
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1591) 
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1592) 	inode->i_state |= I_CREATING;
c2b6d621c4ffe (Al Viro                 2018-06-28 15:53:17 -0400 1593) 	old = inode_insert5(inode, hashval, test, NULL, data);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1594) 
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1595) 	if (old != inode) {
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1596) 		iput(old);
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1597) 		return -EBUSY;
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1598) 	}
80ea09a002bf4 (Miklos Szeredi          2018-05-17 10:53:05 +0200 1599) 	return 0;
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1600) }
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1601) EXPORT_SYMBOL(insert_inode_locked4);
261bca86ed4f7 (Al Viro                 2008-12-30 01:48:21 -0500 1602) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1603) 
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1604) int generic_delete_inode(struct inode *inode)
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1605) {
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1606) 	return 1;
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1607) }
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1608) EXPORT_SYMBOL(generic_delete_inode);
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1609) 
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1610) /*
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1611)  * Called when we're dropping the last reference
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1612)  * to an inode.
22fe404218156 (Jan Kara                2009-09-18 13:05:44 -0700 1613)  *
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1614)  * Call the FS "drop_inode()" function, defaulting to
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1615)  * the legacy UNIX filesystem behaviour.  If it tells
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1616)  * us to evict inode, do so.  Otherwise, retain inode
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1617)  * in cache if fs is alive, sync and evict if fs is
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1618)  * shutting down.
22fe404218156 (Jan Kara                2009-09-18 13:05:44 -0700 1619)  */
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1620) static void iput_final(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1621) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1622) 	struct super_block *sb = inode->i_sb;
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1623) 	const struct super_operations *op = inode->i_sb->s_op;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1624) 	unsigned long state;
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1625) 	int drop;
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1626) 
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1627) 	WARN_ON(inode->i_state & I_NEW);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1628) 
e7f59097071f2 (Al Viro                 2011-07-07 15:45:59 -0400 1629) 	if (op->drop_inode)
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1630) 		drop = op->drop_inode(inode);
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1631) 	else
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1632) 		drop = generic_drop_inode(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1633) 
88149082bb8ef (Hao Li                  2020-12-08 10:08:43 +0800 1634) 	if (!drop &&
88149082bb8ef (Hao Li                  2020-12-08 10:08:43 +0800 1635) 	    !(inode->i_state & I_DONTCACHE) &&
88149082bb8ef (Hao Li                  2020-12-08 10:08:43 +0800 1636) 	    (sb->s_flags & SB_ACTIVE)) {
4eff96dd5283a (Jan Kara                2012-11-26 16:29:51 -0800 1637) 		inode_add_lru(inode);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100 1638) 		spin_unlock(&inode->i_lock);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100 1639) 		return;
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100 1640) 	}
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100 1641) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1642) 	state = inode->i_state;
45321ac54316e (Al Viro                 2010-06-07 13:43:19 -0400 1643) 	if (!drop) {
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1644) 		WRITE_ONCE(inode->i_state, state | I_WILL_FREE);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1645) 		spin_unlock(&inode->i_lock);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1646) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1647) 		write_inode_now(inode, 1);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1648) 
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 1649) 		spin_lock(&inode->i_lock);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1650) 		state = inode->i_state;
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1651) 		WARN_ON(state & I_NEW);
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1652) 		state &= ~I_WILL_FREE;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1653) 	}
7ccf19a8042e3 (Nicholas Piggin         2010-10-21 11:49:30 +1100 1654) 
3f19b2ab97a97 (David Howells           2017-12-01 11:40:16 +0000 1655) 	WRITE_ONCE(inode->i_state, state | I_FREEING);
c4ae0c65455c1 (Eric Dumazet            2011-07-28 06:55:13 +0200 1656) 	if (!list_empty(&inode->i_lru))
c4ae0c65455c1 (Eric Dumazet            2011-07-28 06:55:13 +0200 1657) 		inode_lru_list_del(inode);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100 1658) 	spin_unlock(&inode->i_lock);
b2b2af8e614b4 (Dave Chinner            2011-03-22 22:23:37 +1100 1659) 
644da5960ded1 (Al Viro                 2010-06-07 13:21:05 -0400 1660) 	evict(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1661) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1662) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1663) /**
6b3304b531704 (Manish Katiyar          2009-03-31 19:35:54 +0530 1664)  *	iput	- put an inode
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1665)  *	@inode: inode to put
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1666)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1667)  *	Puts an inode, dropping its usage count. If the inode use count hits
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1668)  *	zero, the inode is then freed and may also be destroyed.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1669)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1670)  *	Consequently, iput() can sleep.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1671)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1672) void iput(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1673) {
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1674) 	if (!inode)
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1675) 		return;
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1676) 	BUG_ON(inode->i_state & I_CLEAR);
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1677) retry:
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1678) 	if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1679) 		if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1680) 			atomic_inc(&inode->i_count);
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1681) 			spin_unlock(&inode->i_lock);
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1682) 			trace_writeback_lazytime_iput(inode);
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1683) 			mark_inode_dirty_sync(inode);
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1684) 			goto retry;
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1685) 		}
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1686) 		iput_final(inode);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1687) 	}
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1688) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1689) EXPORT_SYMBOL(iput);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1690) 
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1691) #ifdef CONFIG_BLOCK
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1692) /**
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1693)  *	bmap	- find a block number in a file
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1694)  *	@inode:  inode owning the block number being requested
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1695)  *	@block: pointer containing the block to find
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1696)  *
2b8e8b5599a18 (Mauro Carvalho Chehab   2020-04-14 18:48:57 +0200 1697)  *	Replaces the value in ``*block`` with the block number on the device holding
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1698)  *	corresponding to the requested block number in the file.
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1699)  *	That is, asked for block 4 of inode 1 the function will replace the
2b8e8b5599a18 (Mauro Carvalho Chehab   2020-04-14 18:48:57 +0200 1700)  *	4 in ``*block``, with disk block relative to the disk start that holds that
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1701)  *	block of the file.
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1702)  *
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1703)  *	Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a
2b8e8b5599a18 (Mauro Carvalho Chehab   2020-04-14 18:48:57 +0200 1704)  *	hole, returns 0 and ``*block`` is also set to 0.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1705)  */
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1706) int bmap(struct inode *inode, sector_t *block)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1707) {
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1708) 	if (!inode->i_mapping->a_ops->bmap)
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1709) 		return -EINVAL;
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1710) 
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1711) 	*block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block);
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1712) 	return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1713) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1714) EXPORT_SYMBOL(bmap);
30460e1ea3e62 (Carlos Maiolino         2020-01-09 14:30:41 +0100 1715) #endif
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1716) 
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1717) /*
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1718)  * With relative atime, only update atime if the previous atime is
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1719)  * earlier than either the ctime or mtime or if at least a day has
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1720)  * passed since the last atime update.
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1721)  */
c6718543463db (Miklos Szeredi          2018-07-18 15:44:43 +0200 1722) static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
6f22b6649edf1 (Arnd Bergmann           2019-04-26 16:50:41 +0200 1723) 			     struct timespec64 now)
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1724) {
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1725) 
c6718543463db (Miklos Szeredi          2018-07-18 15:44:43 +0200 1726) 	if (!(mnt->mnt_flags & MNT_RELATIME))
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1727) 		return 1;
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1728) 	/*
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1729) 	 * Is mtime younger than atime? If yes, update atime:
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1730) 	 */
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1731) 	if (timespec64_compare(&inode->i_mtime, &inode->i_atime) >= 0)
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1732) 		return 1;
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1733) 	/*
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1734) 	 * Is ctime younger than atime? If yes, update atime:
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1735) 	 */
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1736) 	if (timespec64_compare(&inode->i_ctime, &inode->i_atime) >= 0)
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1737) 		return 1;
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1738) 
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1739) 	/*
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1740) 	 * Is the previous atime value older than a day? If yes,
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1741) 	 * update atime:
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1742) 	 */
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1743) 	if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1744) 		return 1;
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1745) 	/*
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1746) 	 * Good, we can skip the atime update:
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1747) 	 */
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1748) 	return 0;
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1749) }
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1750) 
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1751) int generic_update_time(struct inode *inode, struct timespec64 *time, int flags)
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1752) {
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1753) 	int dirty_flags = 0;
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1754) 
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1755) 	if (flags & (S_ATIME | S_CTIME | S_MTIME)) {
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1756) 		if (flags & S_ATIME)
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1757) 			inode->i_atime = *time;
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1758) 		if (flags & S_CTIME)
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1759) 			inode->i_ctime = *time;
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1760) 		if (flags & S_MTIME)
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1761) 			inode->i_mtime = *time;
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1762) 
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1763) 		if (inode->i_sb->s_flags & SB_LAZYTIME)
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1764) 			dirty_flags |= I_DIRTY_TIME;
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1765) 		else
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1766) 			dirty_flags |= I_DIRTY_SYNC;
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1767) 	}
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1768) 
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1769) 	if ((flags & S_VERSION) && inode_maybe_inc_iversion(inode, false))
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1770) 		dirty_flags |= I_DIRTY_SYNC;
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1771) 
e20b14db050ae (Eric Biggers            2021-01-12 11:02:45 -0800 1772) 	__mark_inode_dirty(inode, dirty_flags);
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1773) 	return 0;
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1774) }
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1775) EXPORT_SYMBOL(generic_update_time);
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1776) 
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1777) /*
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1778)  * This does the actual work of updating an inodes time or version.  Must have
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1779)  * had called mnt_want_write() before calling this.
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1780)  */
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1781) static int update_time(struct inode *inode, struct timespec64 *time, int flags)
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1782) {
23b424d9c36e6 (Deepa Dinamani          2019-12-02 21:19:45 -0800 1783) 	if (inode->i_op->update_time)
23b424d9c36e6 (Deepa Dinamani          2019-12-02 21:19:45 -0800 1784) 		return inode->i_op->update_time(inode, time, flags);
23b424d9c36e6 (Deepa Dinamani          2019-12-02 21:19:45 -0800 1785) 	return generic_update_time(inode, time, flags);
0ae45f63d4ef8 (Theodore Ts'o           2015-02-02 00:37:00 -0500 1786) }
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1787) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1788) /**
961f3c898e86d (Mauro Carvalho Chehab   2021-01-14 09:04:39 +0100 1789)  *	atime_needs_update	-	update the access time
185553b22436f (Randy Dunlap            2012-04-17 17:03:25 -0700 1790)  *	@path: the &struct path to update
30fdc8ee0ef0d (Randy Dunlap            2015-11-09 14:57:58 -0800 1791)  *	@inode: inode to update
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1792)  *
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1793)  *	Update the accessed time on an inode and mark it for writeback.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1794)  *	This function automatically handles read only file systems and media,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1795)  *	as well as the "noatime" flag and inode specific "noatime" markers.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1796)  */
c6718543463db (Miklos Szeredi          2018-07-18 15:44:43 +0200 1797) bool atime_needs_update(const struct path *path, struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1798) {
68ac1234fb949 (Al Viro                 2012-03-15 08:21:57 -0400 1799) 	struct vfsmount *mnt = path->mnt;
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1800) 	struct timespec64 now;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1801) 
cdb70f3f74b31 (Dave Hansen             2008-02-15 14:37:41 -0800 1802) 	if (inode->i_flags & S_NOATIME)
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1803) 		return false;
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 1804) 
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 1805) 	/* Atime updates will likely cause i_uid and i_gid to be written
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 1806) 	 * back improprely if their true value is unknown to the vfs.
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 1807) 	 */
ba73d98745be1 (Christian Brauner       2021-01-21 14:19:31 +0100 1808) 	if (HAS_UNMAPPED_ID(mnt_user_ns(mnt), inode))
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 1809) 		return false;
0bd23d09b874e (Eric W. Biederman       2016-06-29 14:54:46 -0500 1810) 
37756ced1f145 (Eric Dumazet            2007-02-10 01:44:49 -0800 1811) 	if (IS_NOATIME(inode))
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1812) 		return false;
1751e8a6cb935 (Linus Torvalds          2017-11-27 13:05:09 -0800 1813) 	if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1814) 		return false;
47ae32d6a5495 (Valerie Henson          2006-12-13 00:34:34 -0800 1815) 
cdb70f3f74b31 (Dave Hansen             2008-02-15 14:37:41 -0800 1816) 	if (mnt->mnt_flags & MNT_NOATIME)
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1817) 		return false;
cdb70f3f74b31 (Dave Hansen             2008-02-15 14:37:41 -0800 1818) 	if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1819) 		return false;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1820) 
c2050a454c7f1 (Deepa Dinamani          2016-09-14 07:48:06 -0700 1821) 	now = current_time(inode);
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1822) 
6f22b6649edf1 (Arnd Bergmann           2019-04-26 16:50:41 +0200 1823) 	if (!relatime_need_update(mnt, inode, now))
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1824) 		return false;
11ff6f05f1e83 (Matthew Garrett         2009-03-26 17:32:14 +0000 1825) 
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1826) 	if (timespec64_equal(&inode->i_atime, &now))
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1827) 		return false;
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1828) 
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1829) 	return true;
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1830) }
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1831) 
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1832) void touch_atime(const struct path *path)
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1833) {
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1834) 	struct vfsmount *mnt = path->mnt;
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1835) 	struct inode *inode = d_inode(path->dentry);
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1836) 	struct timespec64 now;
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1837) 
c6718543463db (Miklos Szeredi          2018-07-18 15:44:43 +0200 1838) 	if (!atime_needs_update(path, inode))
b12536c27043f (Andi Kleen              2009-09-18 13:05:47 -0700 1839) 		return;
b12536c27043f (Andi Kleen              2009-09-18 13:05:47 -0700 1840) 
5d37e9e6dec65 (Jan Kara                2012-06-12 16:20:36 +0200 1841) 	if (!sb_start_write_trylock(inode->i_sb))
b12536c27043f (Andi Kleen              2009-09-18 13:05:47 -0700 1842) 		return;
47ae32d6a5495 (Valerie Henson          2006-12-13 00:34:34 -0800 1843) 
8fa9dd24667f2 (NeilBrown               2015-03-23 13:37:40 +1100 1844) 	if (__mnt_want_write(mnt) != 0)
5d37e9e6dec65 (Jan Kara                2012-06-12 16:20:36 +0200 1845) 		goto skip_update;
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1846) 	/*
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1847) 	 * File systems can error out when updating inodes if they need to
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1848) 	 * allocate new space to modify an inode (such is the case for
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1849) 	 * Btrfs), but since we touch atime while walking down the path we
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1850) 	 * really don't care if we failed to update the atime of the file,
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1851) 	 * so just ignore the return value.
2bc5565286121 (Alexander Block         2012-06-15 09:49:33 +0200 1852) 	 * We may also fail on filesystems that have the ability to make parts
2bc5565286121 (Alexander Block         2012-06-15 09:49:33 +0200 1853) 	 * of the fs read only, e.g. subvolumes in Btrfs.
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1854) 	 */
c2050a454c7f1 (Deepa Dinamani          2016-09-14 07:48:06 -0700 1855) 	now = current_time(inode);
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1856) 	update_time(inode, &now, S_ATIME);
5d37e9e6dec65 (Jan Kara                2012-06-12 16:20:36 +0200 1857) 	__mnt_drop_write(mnt);
5d37e9e6dec65 (Jan Kara                2012-06-12 16:20:36 +0200 1858) skip_update:
5d37e9e6dec65 (Jan Kara                2012-06-12 16:20:36 +0200 1859) 	sb_end_write(inode->i_sb);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1860) }
869243a0f6143 (Christoph Hellwig       2006-01-09 20:52:03 -0800 1861) EXPORT_SYMBOL(touch_atime);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1862) 
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1863) /*
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1864)  * The logic we want is
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1865)  *
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1866)  *	if suid or (sgid and xgrp)
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1867)  *		remove privs
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1868)  */
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1869) int should_remove_suid(struct dentry *dentry)
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1870) {
df2b1afde178f (David Howells           2015-03-17 22:26:15 +0000 1871) 	umode_t mode = d_inode(dentry)->i_mode;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1872) 	int kill = 0;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1873) 
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1874) 	/* suid always must be killed */
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1875) 	if (unlikely(mode & S_ISUID))
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1876) 		kill = ATTR_KILL_SUID;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1877) 
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1878) 	/*
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1879) 	 * sgid without any exec bits is just a mandatory locking mark; leave
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1880) 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1881) 	 */
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1882) 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1883) 		kill |= ATTR_KILL_SGID;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1884) 
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1885) 	if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1886) 		return kill;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1887) 
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1888) 	return 0;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1889) }
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1890) EXPORT_SYMBOL(should_remove_suid);
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1891) 
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1892) /*
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1893)  * Return mask of changes for notify_change() that need to be done as a
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1894)  * response to write or truncate. Return 0 if nothing has to be changed.
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1895)  * Negative value on error (change should be denied).
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1896)  */
45f147a1bc97c (Jan Kara                2015-05-21 16:05:55 +0200 1897) int dentry_needs_remove_privs(struct dentry *dentry)
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1898) {
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1899) 	struct inode *inode = d_inode(dentry);
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1900) 	int mask = 0;
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1901) 	int ret;
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1902) 
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1903) 	if (IS_NOSEC(inode))
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1904) 		return 0;
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1905) 
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1906) 	mask = should_remove_suid(dentry);
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1907) 	ret = security_inode_need_killpriv(dentry);
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1908) 	if (ret < 0)
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1909) 		return ret;
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1910) 	if (ret)
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1911) 		mask |= ATTR_KILL_PRIV;
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1912) 	return mask;
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1913) }
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1914) 
643fe55a0679a (Christian Brauner       2021-01-21 14:19:34 +0100 1915) static int __remove_privs(struct user_namespace *mnt_userns,
643fe55a0679a (Christian Brauner       2021-01-21 14:19:34 +0100 1916) 			  struct dentry *dentry, int kill)
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1917) {
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1918) 	struct iattr newattrs;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1919) 
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1920) 	newattrs.ia_valid = ATTR_FORCE | kill;
27ac0ffeac80b (J. Bruce Fields         2011-09-20 17:19:26 -0400 1921) 	/*
27ac0ffeac80b (J. Bruce Fields         2011-09-20 17:19:26 -0400 1922) 	 * Note we call this on write, so notify_change will not
27ac0ffeac80b (J. Bruce Fields         2011-09-20 17:19:26 -0400 1923) 	 * encounter any conflicting delegations:
27ac0ffeac80b (J. Bruce Fields         2011-09-20 17:19:26 -0400 1924) 	 */
643fe55a0679a (Christian Brauner       2021-01-21 14:19:34 +0100 1925) 	return notify_change(mnt_userns, dentry, &newattrs, NULL);
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1926) }
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1927) 
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 1928) /*
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 1929)  * Remove special file priviledges (suid, capabilities) when file is written
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 1930)  * to or truncated.
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 1931)  */
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 1932) int file_remove_privs(struct file *file)
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1933) {
c1892c37769cf (Miklos Szeredi          2016-08-03 13:44:27 +0200 1934) 	struct dentry *dentry = file_dentry(file);
c1892c37769cf (Miklos Szeredi          2016-08-03 13:44:27 +0200 1935) 	struct inode *inode = file_inode(file);
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1936) 	int kill;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1937) 	int error = 0;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1938) 
f69e749a49353 (Alexander Lochmann      2018-12-14 11:55:52 +0100 1939) 	/*
f69e749a49353 (Alexander Lochmann      2018-12-14 11:55:52 +0100 1940) 	 * Fast path for nothing security related.
f69e749a49353 (Alexander Lochmann      2018-12-14 11:55:52 +0100 1941) 	 * As well for non-regular files, e.g. blkdev inodes.
f69e749a49353 (Alexander Lochmann      2018-12-14 11:55:52 +0100 1942) 	 * For example, blkdev_write_iter() might get here
f69e749a49353 (Alexander Lochmann      2018-12-14 11:55:52 +0100 1943) 	 * trying to remove privs which it is not allowed to.
f69e749a49353 (Alexander Lochmann      2018-12-14 11:55:52 +0100 1944) 	 */
f69e749a49353 (Alexander Lochmann      2018-12-14 11:55:52 +0100 1945) 	if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode))
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1946) 		return 0;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1947) 
c1892c37769cf (Miklos Szeredi          2016-08-03 13:44:27 +0200 1948) 	kill = dentry_needs_remove_privs(dentry);
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1949) 	if (kill < 0)
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1950) 		return kill;
dbfae0cdcd876 (Jan Kara                2015-05-21 16:05:54 +0200 1951) 	if (kill)
643fe55a0679a (Christian Brauner       2021-01-21 14:19:34 +0100 1952) 		error = __remove_privs(file_mnt_user_ns(file), dentry, kill);
2426f3910069e (Jan Kara                2015-05-21 16:05:52 +0200 1953) 	if (!error)
2426f3910069e (Jan Kara                2015-05-21 16:05:52 +0200 1954) 		inode_has_no_xattr(inode);
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1955) 
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1956) 	return error;
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1957) }
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 1958) EXPORT_SYMBOL(file_remove_privs);
3ed37648e1cbf (Cong Wang               2012-05-15 14:57:33 +0800 1959) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1960) /**
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 1961)  *	file_update_time	-	update mtime and ctime time
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 1962)  *	@file: file accessed
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1963)  *
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 1964)  *	Update the mtime and ctime members of an inode and mark the inode
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 1965)  *	for writeback.  Note that this function is meant exclusively for
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 1966)  *	usage in the file write path of filesystems, and filesystems may
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 1967)  *	choose to explicitly ignore update via this function with the
2eadfc0ed6869 (Wolfram Sang            2009-04-02 15:23:37 +0200 1968)  *	S_NOCMTIME inode flag, e.g. for network filesystem where these
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1969)  *	timestamps are handled by the server.  This can return an error for
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1970)  *	file systems who need to allocate space in order to update an inode.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1971)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1972) 
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1973) int file_update_time(struct file *file)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1974) {
496ad9aa8ef44 (Al Viro                 2013-01-23 17:07:38 -0500 1975) 	struct inode *inode = file_inode(file);
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1976) 	struct timespec64 now;
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1977) 	int sync_it = 0;
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1978) 	int ret;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1979) 
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 1980) 	/* First try to exhaust all avenues to not sync */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1981) 	if (IS_NOCMTIME(inode))
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1982) 		return 0;
20ddee2c75339 (Dave Hansen             2008-02-15 14:37:43 -0800 1983) 
c2050a454c7f1 (Deepa Dinamani          2016-09-14 07:48:06 -0700 1984) 	now = current_time(inode);
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1985) 	if (!timespec64_equal(&inode->i_mtime, &now))
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 1986) 		sync_it = S_MTIME;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 1987) 
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 1988) 	if (!timespec64_equal(&inode->i_ctime, &now))
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 1989) 		sync_it |= S_CTIME;
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 1990) 
e38cf302b2e92 (Jeff Layton             2017-12-11 06:35:22 -0500 1991) 	if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode))
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 1992) 		sync_it |= S_VERSION;
7a224228ed79d (Jean Noel Cordenner     2008-01-28 23:58:27 -0500 1993) 
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 1994) 	if (!sync_it)
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1995) 		return 0;
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 1996) 
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 1997) 	/* Finally allowed to write? Takes lock. */
eb04c28288bb0 (Jan Kara                2012-06-12 16:20:35 +0200 1998) 	if (__mnt_want_write_file(file))
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 1999) 		return 0;
ce06e0b21d673 (Andi Kleen              2009-09-18 13:05:48 -0700 2000) 
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 2001) 	ret = update_time(inode, &now, sync_it);
eb04c28288bb0 (Jan Kara                2012-06-12 16:20:35 +0200 2002) 	__mnt_drop_write_file(file);
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 2003) 
c3b2da3148344 (Josef Bacik             2012-03-26 09:59:21 -0400 2004) 	return ret;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2005) }
870f481793b58 (Christoph Hellwig       2006-01-09 20:52:01 -0800 2006) EXPORT_SYMBOL(file_update_time);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2007) 
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2008) /* Caller must hold the file's inode lock */
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2009) int file_modified(struct file *file)
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2010) {
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2011) 	int err;
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2012) 
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2013) 	/*
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2014) 	 * Clear the security bits if the process is not being run by root.
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2015) 	 * This keeps people from modifying setuid and setgid binaries.
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2016) 	 */
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2017) 	err = file_remove_privs(file);
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2018) 	if (err)
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2019) 		return err;
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2020) 
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2021) 	if (unlikely(file->f_mode & FMODE_NOCMTIME))
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2022) 		return 0;
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2023) 
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2024) 	return file_update_time(file);
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2025) }
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2026) EXPORT_SYMBOL(file_modified);
e38f7f53c3521 (Amir Goldstein          2019-06-05 08:04:49 -0700 2027) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2028) int inode_needs_sync(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2029) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2030) 	if (IS_SYNC(inode))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2031) 		return 1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2032) 	if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2033) 		return 1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2034) 	return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2035) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2036) EXPORT_SYMBOL(inode_needs_sync);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2037) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2038) /*
168a9fd6a1bf9 (Miklos Szeredi          2005-07-12 13:58:10 -0700 2039)  * If we try to find an inode in the inode hash while it is being
168a9fd6a1bf9 (Miklos Szeredi          2005-07-12 13:58:10 -0700 2040)  * deleted, we have to wait until the filesystem completes its
168a9fd6a1bf9 (Miklos Szeredi          2005-07-12 13:58:10 -0700 2041)  * deletion before reporting that it isn't found.  This function waits
168a9fd6a1bf9 (Miklos Szeredi          2005-07-12 13:58:10 -0700 2042)  * until the deletion _might_ have completed.  Callers are responsible
168a9fd6a1bf9 (Miklos Szeredi          2005-07-12 13:58:10 -0700 2043)  * to recheck inode state.
168a9fd6a1bf9 (Miklos Szeredi          2005-07-12 13:58:10 -0700 2044)  *
eaff8079d4f10 (Christoph Hellwig       2009-12-17 14:25:01 +0100 2045)  * It doesn't matter if I_NEW is not set initially, a call to
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 2046)  * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 2047)  * will DTRT.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2048)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2049) static void __wait_on_freeing_inode(struct inode *inode)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2050) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2051) 	wait_queue_head_t *wq;
eaff8079d4f10 (Christoph Hellwig       2009-12-17 14:25:01 +0100 2052) 	DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
eaff8079d4f10 (Christoph Hellwig       2009-12-17 14:25:01 +0100 2053) 	wq = bit_waitqueue(&inode->i_state, __I_NEW);
2141713616c65 (Ingo Molnar             2017-03-05 11:25:39 +0100 2054) 	prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
250df6ed274d7 (Dave Chinner            2011-03-22 22:23:36 +1100 2055) 	spin_unlock(&inode->i_lock);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 2056) 	spin_unlock(&inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2057) 	schedule();
2141713616c65 (Ingo Molnar             2017-03-05 11:25:39 +0100 2058) 	finish_wait(wq, &wait.wq_entry);
67a23c494621f (Dave Chinner            2011-03-22 22:23:42 +1100 2059) 	spin_lock(&inode_hash_lock);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2060) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2061) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2062) static __initdata unsigned long ihash_entries;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2063) static int __init set_ihash_entries(char *str)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2064) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2065) 	if (!str)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2066) 		return 0;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2067) 	ihash_entries = simple_strtoul(str, &str, 0);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2068) 	return 1;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2069) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2070) __setup("ihash_entries=", set_ihash_entries);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2071) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2072) /*
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2073)  * Initialize the waitqueues and inode hash table.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2074)  */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2075) void __init inode_init_early(void)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2076) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2077) 	/* If hashes are distributed across NUMA nodes, defer
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2078) 	 * hash allocation until vmalloc space is available.
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2079) 	 */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2080) 	if (hashdist)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2081) 		return;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2082) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2083) 	inode_hashtable =
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2084) 		alloc_large_system_hash("Inode-cache",
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2085) 					sizeof(struct hlist_head),
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2086) 					ihash_entries,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2087) 					14,
3d375d78593cd (Pavel Tatashin          2017-07-06 15:39:11 -0700 2088) 					HASH_EARLY | HASH_ZERO,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2089) 					&i_hash_shift,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2090) 					&i_hash_mask,
31fe62b958664 (Tim Bird                2012-05-23 13:33:35 +0000 2091) 					0,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2092) 					0);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2093) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2094) 
74bf17cffc325 (Denis Cheng             2007-10-16 23:26:30 -0700 2095) void __init inode_init(void)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2096) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2097) 	/* inode slab cache */
b0196009d8c3e (Paul Jackson            2006-03-24 03:16:09 -0800 2098) 	inode_cachep = kmem_cache_create("inode_cache",
b0196009d8c3e (Paul Jackson            2006-03-24 03:16:09 -0800 2099) 					 sizeof(struct inode),
b0196009d8c3e (Paul Jackson            2006-03-24 03:16:09 -0800 2100) 					 0,
b0196009d8c3e (Paul Jackson            2006-03-24 03:16:09 -0800 2101) 					 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
5d097056c9a01 (Vladimir Davydov        2016-01-14 15:18:21 -0800 2102) 					 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
20c2df83d25c6 (Paul Mundt              2007-07-20 10:11:58 +0900 2103) 					 init_once);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2104) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2105) 	/* Hash may have been set up in inode_init_early */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2106) 	if (!hashdist)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2107) 		return;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2108) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2109) 	inode_hashtable =
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2110) 		alloc_large_system_hash("Inode-cache",
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2111) 					sizeof(struct hlist_head),
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2112) 					ihash_entries,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2113) 					14,
3d375d78593cd (Pavel Tatashin          2017-07-06 15:39:11 -0700 2114) 					HASH_ZERO,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2115) 					&i_hash_shift,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2116) 					&i_hash_mask,
31fe62b958664 (Tim Bird                2012-05-23 13:33:35 +0000 2117) 					0,
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2118) 					0);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2119) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2120) 
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2121) void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2122) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2123) 	inode->i_mode = mode;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2124) 	if (S_ISCHR(mode)) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2125) 		inode->i_fop = &def_chr_fops;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2126) 		inode->i_rdev = rdev;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2127) 	} else if (S_ISBLK(mode)) {
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2128) 		inode->i_fop = &def_blk_fops;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2129) 		inode->i_rdev = rdev;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2130) 	} else if (S_ISFIFO(mode))
599a0ac14e065 (Al Viro                 2013-03-12 09:58:10 -0400 2131) 		inode->i_fop = &pipefifo_fops;
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2132) 	else if (S_ISSOCK(mode))
bd9b51e79cb0b (Al Viro                 2014-11-18 23:38:21 -0500 2133) 		;	/* leave it no_open_fops */
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2134) 	else
af0d9ae811d11 (Manish Katiyar          2009-09-18 13:05:43 -0700 2135) 		printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
af0d9ae811d11 (Manish Katiyar          2009-09-18 13:05:43 -0700 2136) 				  " inode %s:%lu\n", mode, inode->i_sb->s_id,
af0d9ae811d11 (Manish Katiyar          2009-09-18 13:05:43 -0700 2137) 				  inode->i_ino);
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2138) }
^1da177e4c3f4 (Linus Torvalds          2005-04-16 15:20:36 -0700 2139) EXPORT_SYMBOL(init_special_inode);
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2140) 
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2141) /**
eaae668d01e15 (Ben Hutchings           2011-02-15 12:48:09 +0000 2142)  * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2143)  * @mnt_userns:	User namespace of the mount the inode was created from
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2144)  * @inode: New inode
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2145)  * @dir: Directory inode
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2146)  * @mode: mode of the new inode
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2147)  *
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2148)  * If the inode has been created through an idmapped mount the user namespace of
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2149)  * the vfsmount must be passed through @mnt_userns. This function will then take
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2150)  * care to map the inode according to @mnt_userns before checking permissions
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2151)  * and initializing i_uid and i_gid. On non-idmapped mounts or if permission
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2152)  * checking is to be performed on the raw inode simply passs init_user_ns.
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2153)  */
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2154) void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2155) 		      const struct inode *dir, umode_t mode)
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2156) {
db998553cf11d (Christian Brauner       2021-03-20 13:26:24 +0100 2157) 	inode_fsuid_set(inode, mnt_userns);
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2158) 	if (dir && dir->i_mode & S_ISGID) {
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2159) 		inode->i_gid = dir->i_gid;
0fa3ecd87848c (Linus Torvalds          2018-07-03 17:10:19 -0700 2160) 
0fa3ecd87848c (Linus Torvalds          2018-07-03 17:10:19 -0700 2161) 		/* Directories are special, and always inherit S_ISGID */
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2162) 		if (S_ISDIR(mode))
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2163) 			mode |= S_ISGID;
0fa3ecd87848c (Linus Torvalds          2018-07-03 17:10:19 -0700 2164) 		else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2165) 			 !in_group_p(i_gid_into_mnt(mnt_userns, dir)) &&
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2166) 			 !capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
0fa3ecd87848c (Linus Torvalds          2018-07-03 17:10:19 -0700 2167) 			mode &= ~S_ISGID;
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2168) 	} else
db998553cf11d (Christian Brauner       2021-03-20 13:26:24 +0100 2169) 		inode_fsgid_set(inode, mnt_userns);
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2170) 	inode->i_mode = mode;
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2171) }
a1bd120d13e58 (Dmitry Monakhov         2010-03-04 17:29:14 +0300 2172) EXPORT_SYMBOL(inode_init_owner);
e795b71799ff0 (Serge E. Hallyn         2011-03-23 16:43:25 -0700 2173) 
2e1496707560e (Serge E. Hallyn         2011-03-23 16:43:26 -0700 2174) /**
2e1496707560e (Serge E. Hallyn         2011-03-23 16:43:26 -0700 2175)  * inode_owner_or_capable - check current task permissions to inode
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2176)  * @mnt_userns:	user namespace of the mount the inode was found from
2e1496707560e (Serge E. Hallyn         2011-03-23 16:43:26 -0700 2177)  * @inode: inode being checked
2e1496707560e (Serge E. Hallyn         2011-03-23 16:43:26 -0700 2178)  *
23adbe12ef7d3 (Andy Lutomirski         2014-06-10 12:45:42 -0700 2179)  * Return true if current either has CAP_FOWNER in a namespace with the
23adbe12ef7d3 (Andy Lutomirski         2014-06-10 12:45:42 -0700 2180)  * inode owner uid mapped, or owns the file.
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2181)  *
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2182)  * If the inode has been found through an idmapped mount the user namespace of
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2183)  * the vfsmount must be passed through @mnt_userns. This function will then take
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2184)  * care to map the inode according to @mnt_userns before checking permissions.
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2185)  * On non-idmapped mounts or if permission checking is to be performed on the
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2186)  * raw inode simply passs init_user_ns.
e795b71799ff0 (Serge E. Hallyn         2011-03-23 16:43:25 -0700 2187)  */
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2188) bool inode_owner_or_capable(struct user_namespace *mnt_userns,
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2189) 			    const struct inode *inode)
e795b71799ff0 (Serge E. Hallyn         2011-03-23 16:43:25 -0700 2190) {
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2191) 	kuid_t i_uid;
23adbe12ef7d3 (Andy Lutomirski         2014-06-10 12:45:42 -0700 2192) 	struct user_namespace *ns;
23adbe12ef7d3 (Andy Lutomirski         2014-06-10 12:45:42 -0700 2193) 
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2194) 	i_uid = i_uid_into_mnt(mnt_userns, inode);
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2195) 	if (uid_eq(current_fsuid(), i_uid))
e795b71799ff0 (Serge E. Hallyn         2011-03-23 16:43:25 -0700 2196) 		return true;
23adbe12ef7d3 (Andy Lutomirski         2014-06-10 12:45:42 -0700 2197) 
23adbe12ef7d3 (Andy Lutomirski         2014-06-10 12:45:42 -0700 2198) 	ns = current_user_ns();
21cb47be6fb9e (Christian Brauner       2021-01-21 14:19:25 +0100 2199) 	if (kuid_has_mapping(ns, i_uid) && ns_capable(ns, CAP_FOWNER))
e795b71799ff0 (Serge E. Hallyn         2011-03-23 16:43:25 -0700 2200) 		return true;
e795b71799ff0 (Serge E. Hallyn         2011-03-23 16:43:25 -0700 2201) 	return false;
e795b71799ff0 (Serge E. Hallyn         2011-03-23 16:43:25 -0700 2202) }
2e1496707560e (Serge E. Hallyn         2011-03-23 16:43:26 -0700 2203) EXPORT_SYMBOL(inode_owner_or_capable);
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2204) 
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2205) /*
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2206)  * Direct i/o helper functions
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2207)  */
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2208) static void __inode_dio_wait(struct inode *inode)
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2209) {
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2210) 	wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2211) 	DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2212) 
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2213) 	do {
2141713616c65 (Ingo Molnar             2017-03-05 11:25:39 +0100 2214) 		prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE);
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2215) 		if (atomic_read(&inode->i_dio_count))
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2216) 			schedule();
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2217) 	} while (atomic_read(&inode->i_dio_count));
2141713616c65 (Ingo Molnar             2017-03-05 11:25:39 +0100 2218) 	finish_wait(wq, &q.wq_entry);
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2219) }
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2220) 
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2221) /**
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2222)  * inode_dio_wait - wait for outstanding DIO requests to finish
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2223)  * @inode: inode to wait for
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2224)  *
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2225)  * Waits for all pending direct I/O requests to finish so that we can
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2226)  * proceed with a truncate or equivalent operation.
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2227)  *
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2228)  * Must be called under a lock that serializes taking new references
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2229)  * to i_dio_count, usually by inode->i_mutex.
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2230)  */
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2231) void inode_dio_wait(struct inode *inode)
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2232) {
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2233) 	if (atomic_read(&inode->i_dio_count))
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2234) 		__inode_dio_wait(inode);
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2235) }
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2236) EXPORT_SYMBOL(inode_dio_wait);
1d59d61f60654 (Trond Myklebust         2012-05-31 12:22:33 -0400 2237) 
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2238) /*
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2239)  * inode_set_flags - atomically set some inode flags
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2240)  *
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2241)  * Note: the caller should be holding i_mutex, or else be sure that
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2242)  * they have exclusive access to the inode structure (i.e., while the
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2243)  * inode is being instantiated).  The reason for the cmpxchg() loop
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2244)  * --- which wouldn't be necessary if all code paths which modify
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2245)  * i_flags actually followed this rule, is that there is at least one
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 2246)  * code path which doesn't today so we use cmpxchg() out of an abundance
5fa8e0a1c6a76 (Jan Kara                2015-05-21 16:05:53 +0200 2247)  * of caution.
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2248)  *
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2249)  * In the long run, i_mutex is overkill, and we should probably look
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2250)  * at using the i_lock spinlock to protect i_flags, and then make sure
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2251)  * it is so documented in include/linux/fs.h and that all code follows
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2252)  * the locking convention!!
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2253)  */
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2254) void inode_set_flags(struct inode *inode, unsigned int flags,
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2255) 		     unsigned int mask)
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2256) {
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2257) 	WARN_ON_ONCE(flags & ~mask);
a905737fdd767 (Vineet Gupta            2019-03-05 15:41:52 -0800 2258) 	set_mask_bits(&inode->i_flags, mask, flags);
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2259) }
5f16f3225b062 (Theodore Ts'o           2014-03-24 14:43:12 -0400 2260) EXPORT_SYMBOL(inode_set_flags);
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 2261) 
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 2262) void inode_nohighmem(struct inode *inode)
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 2263) {
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 2264) 	mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 2265) }
21fc61c73c390 (Al Viro                 2015-11-17 01:07:57 -0500 2266) EXPORT_SYMBOL(inode_nohighmem);
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2267) 
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2268) /**
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2269)  * timestamp_truncate - Truncate timespec to a granularity
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2270)  * @t: Timespec
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2271)  * @inode: inode being updated
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2272)  *
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2273)  * Truncate a timespec to the granularity supported by the fs
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2274)  * containing the inode. Always rounds down. gran must
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2275)  * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2276)  */
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2277) struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2278) {
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2279) 	struct super_block *sb = inode->i_sb;
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2280) 	unsigned int gran = sb->s_time_gran;
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2281) 
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2282) 	t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max);
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2283) 	if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min))
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2284) 		t.tv_nsec = 0;
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2285) 
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2286) 	/* Avoid division in the common cases 1 ns and 1 s. */
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2287) 	if (gran == 1)
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2288) 		; /* nothing */
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2289) 	else if (gran == NSEC_PER_SEC)
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2290) 		t.tv_nsec = 0;
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2291) 	else if (gran > 1 && gran < NSEC_PER_SEC)
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2292) 		t.tv_nsec -= t.tv_nsec % gran;
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2293) 	else
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2294) 		WARN(1, "invalid file time granularity: %u", gran);
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2295) 	return t;
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2296) }
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2297) EXPORT_SYMBOL(timestamp_truncate);
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2298) 
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2299) /**
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2300)  * current_time - Return FS time
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2301)  * @inode: inode.
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2302)  *
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2303)  * Return the current time truncated to the time granularity supported by
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2304)  * the fs.
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2305)  *
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2306)  * Note that inode and inode->sb cannot be NULL.
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2307)  * Otherwise, the function warns and returns time without truncation.
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2308)  */
95582b0083883 (Deepa Dinamani          2018-05-08 19:36:02 -0700 2309) struct timespec64 current_time(struct inode *inode)
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2310) {
d651d1607f22f (Arnd Bergmann           2018-12-05 11:14:27 +1100 2311) 	struct timespec64 now;
d651d1607f22f (Arnd Bergmann           2018-12-05 11:14:27 +1100 2312) 
d651d1607f22f (Arnd Bergmann           2018-12-05 11:14:27 +1100 2313) 	ktime_get_coarse_real_ts64(&now);
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2314) 
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2315) 	if (unlikely(!inode->i_sb)) {
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2316) 		WARN(1, "current_time() called with uninitialized super_block in the inode");
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2317) 		return now;
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2318) 	}
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2319) 
50e17c000c467 (Deepa Dinamani          2018-01-21 18:04:25 -0800 2320) 	return timestamp_truncate(now, inode);
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2321) }
3cd886666ff19 (Deepa Dinamani          2016-09-14 07:48:02 -0700 2322) EXPORT_SYMBOL(current_time);