xref: /openbmc/linux/fs/fuse/inode.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
1d8a5ba45SMiklos Szeredi /*
2d8a5ba45SMiklos Szeredi   FUSE: Filesystem in Userspace
31729a16cSMiklos Szeredi   Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
4d8a5ba45SMiklos Szeredi 
5d8a5ba45SMiklos Szeredi   This program can be distributed under the terms of the GNU GPL.
6d8a5ba45SMiklos Szeredi   See the file COPYING.
7d8a5ba45SMiklos Szeredi */
8d8a5ba45SMiklos Szeredi 
9d8a5ba45SMiklos Szeredi #include "fuse_i.h"
10d8a5ba45SMiklos Szeredi 
11d8a5ba45SMiklos Szeredi #include <linux/pagemap.h>
12d8a5ba45SMiklos Szeredi #include <linux/slab.h>
13d8a5ba45SMiklos Szeredi #include <linux/file.h>
14d8a5ba45SMiklos Szeredi #include <linux/seq_file.h>
15d8a5ba45SMiklos Szeredi #include <linux/init.h>
16d8a5ba45SMiklos Szeredi #include <linux/module.h>
17487ea5afSCsaba Henk #include <linux/moduleparam.h>
18c30da2e9SDavid Howells #include <linux/fs_context.h>
19c30da2e9SDavid Howells #include <linux/fs_parser.h>
20d8a5ba45SMiklos Szeredi #include <linux/statfs.h>
219c8ef561SMiklos Szeredi #include <linux/random.h>
22e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
23dbd561d2SMiklos Szeredi #include <linux/exportfs.h>
2460bcc88aSSeth Forshee #include <linux/posix_acl.h>
250b6e9ea0SSeth Forshee #include <linux/pid_namespace.h>
26c086df49SJeff Layton #include <uapi/linux/magic.h>
27d8a5ba45SMiklos Szeredi 
28d8a5ba45SMiklos Szeredi MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
29d8a5ba45SMiklos Szeredi MODULE_DESCRIPTION("Filesystem in Userspace");
30d8a5ba45SMiklos Szeredi MODULE_LICENSE("GPL");
31d8a5ba45SMiklos Szeredi 
32e18b890bSChristoph Lameter static struct kmem_cache *fuse_inode_cachep;
33bafa9654SMiklos Szeredi struct list_head fuse_conn_list;
34bafa9654SMiklos Szeredi DEFINE_MUTEX(fuse_mutex);
35d8a5ba45SMiklos Szeredi 
36e4dca7b7SKees Cook static int set_global_limit(const char *val, const struct kernel_param *kp);
37487ea5afSCsaba Henk 
3879a9d994SCsaba Henk unsigned max_user_bgreq;
39487ea5afSCsaba Henk module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
40487ea5afSCsaba Henk 		  &max_user_bgreq, 0644);
41487ea5afSCsaba Henk __MODULE_PARM_TYPE(max_user_bgreq, "uint");
42487ea5afSCsaba Henk MODULE_PARM_DESC(max_user_bgreq,
43487ea5afSCsaba Henk  "Global limit for the maximum number of backgrounded requests an "
44487ea5afSCsaba Henk  "unprivileged user can set");
45487ea5afSCsaba Henk 
4679a9d994SCsaba Henk unsigned max_user_congthresh;
47487ea5afSCsaba Henk module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
48487ea5afSCsaba Henk 		  &max_user_congthresh, 0644);
49487ea5afSCsaba Henk __MODULE_PARM_TYPE(max_user_congthresh, "uint");
50487ea5afSCsaba Henk MODULE_PARM_DESC(max_user_congthresh,
51487ea5afSCsaba Henk  "Global limit for the maximum congestion threshold an "
52487ea5afSCsaba Henk  "unprivileged user can set");
53487ea5afSCsaba Henk 
54d1875dbaSMiklos Szeredi #define FUSE_DEFAULT_BLKSIZE 512
55d1875dbaSMiklos Szeredi 
567a6d3c8bSCsaba Henk /** Maximum number of outstanding background requests */
577a6d3c8bSCsaba Henk #define FUSE_DEFAULT_MAX_BACKGROUND 12
587a6d3c8bSCsaba Henk 
597a6d3c8bSCsaba Henk /** Congestion starts at 75% of maximum */
607a6d3c8bSCsaba Henk #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
617a6d3c8bSCsaba Henk 
62c30da2e9SDavid Howells #ifdef CONFIG_BLOCK
63c30da2e9SDavid Howells static struct file_system_type fuseblk_fs_type;
64c30da2e9SDavid Howells #endif
65c30da2e9SDavid Howells 
fuse_alloc_forget(void)66a2daff68SRandy Dunlap struct fuse_forget_link *fuse_alloc_forget(void)
6707e77dcaSMiklos Szeredi {
68dc69e98cSKhazhismel Kumykov 	return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
6907e77dcaSMiklos Szeredi }
7007e77dcaSMiklos Szeredi 
fuse_alloc_submount_lookup(void)712939dd30SKrister Johansen static struct fuse_submount_lookup *fuse_alloc_submount_lookup(void)
722939dd30SKrister Johansen {
732939dd30SKrister Johansen 	struct fuse_submount_lookup *sl;
742939dd30SKrister Johansen 
752939dd30SKrister Johansen 	sl = kzalloc(sizeof(struct fuse_submount_lookup), GFP_KERNEL_ACCOUNT);
762939dd30SKrister Johansen 	if (!sl)
772939dd30SKrister Johansen 		return NULL;
782939dd30SKrister Johansen 	sl->forget = fuse_alloc_forget();
792939dd30SKrister Johansen 	if (!sl->forget)
802939dd30SKrister Johansen 		goto out_free;
812939dd30SKrister Johansen 
822939dd30SKrister Johansen 	return sl;
832939dd30SKrister Johansen 
842939dd30SKrister Johansen out_free:
852939dd30SKrister Johansen 	kfree(sl);
862939dd30SKrister Johansen 	return NULL;
872939dd30SKrister Johansen }
882939dd30SKrister Johansen 
fuse_alloc_inode(struct super_block * sb)89d8a5ba45SMiklos Szeredi static struct inode *fuse_alloc_inode(struct super_block *sb)
90d8a5ba45SMiklos Szeredi {
91d8a5ba45SMiklos Szeredi 	struct fuse_inode *fi;
92d8a5ba45SMiklos Szeredi 
93fd60b288SMuchun Song 	fi = alloc_inode_sb(sb, fuse_inode_cachep, GFP_KERNEL);
949031a69cSzhangliguang 	if (!fi)
95d8a5ba45SMiklos Szeredi 		return NULL;
96d8a5ba45SMiklos Szeredi 
970a0898cfSMiklos Szeredi 	fi->i_time = 0;
98d3045530SMiklos Szeredi 	fi->inval_mask = ~0;
99d8a5ba45SMiklos Szeredi 	fi->nodeid = 0;
1009e6268dbSMiklos Szeredi 	fi->nlookup = 0;
101fbee36b9SJohn Muir 	fi->attr_version = 0;
10245c72cd7SPavel Shilovsky 	fi->orig_ino = 0;
1034582a4abSFeng Shuo 	fi->state = 0;
1042939dd30SKrister Johansen 	fi->submount_lookup = NULL;
1055c672ab3SMiklos Szeredi 	mutex_init(&fi->mutex);
106f15ecfefSKirill Tkhai 	spin_lock_init(&fi->lock);
10707e77dcaSMiklos Szeredi 	fi->forget = fuse_alloc_forget();
108c2d0ad00SVivek Goyal 	if (!fi->forget)
109c2d0ad00SVivek Goyal 		goto out_free;
110c2d0ad00SVivek Goyal 
111c2d0ad00SVivek Goyal 	if (IS_ENABLED(CONFIG_FUSE_DAX) && !fuse_dax_inode_alloc(sb, fi))
112c2d0ad00SVivek Goyal 		goto out_free_forget;
113d8a5ba45SMiklos Szeredi 
1149031a69cSzhangliguang 	return &fi->inode;
115c2d0ad00SVivek Goyal 
116c2d0ad00SVivek Goyal out_free_forget:
117c2d0ad00SVivek Goyal 	kfree(fi->forget);
118c2d0ad00SVivek Goyal out_free:
119c2d0ad00SVivek Goyal 	kmem_cache_free(fuse_inode_cachep, fi);
120c2d0ad00SVivek Goyal 	return NULL;
121d8a5ba45SMiklos Szeredi }
122d8a5ba45SMiklos Szeredi 
fuse_free_inode(struct inode * inode)1239baf28bbSAl Viro static void fuse_free_inode(struct inode *inode)
124d8a5ba45SMiklos Szeredi {
125e5e5558eSMiklos Szeredi 	struct fuse_inode *fi = get_fuse_inode(inode);
1269baf28bbSAl Viro 
1275c672ab3SMiklos Szeredi 	mutex_destroy(&fi->mutex);
12807e77dcaSMiklos Szeredi 	kfree(fi->forget);
129c2d0ad00SVivek Goyal #ifdef CONFIG_FUSE_DAX
130c2d0ad00SVivek Goyal 	kfree(fi->dax);
131c2d0ad00SVivek Goyal #endif
1329baf28bbSAl Viro 	kmem_cache_free(fuse_inode_cachep, fi);
133d8a5ba45SMiklos Szeredi }
134d8a5ba45SMiklos Szeredi 
fuse_cleanup_submount_lookup(struct fuse_conn * fc,struct fuse_submount_lookup * sl)1352939dd30SKrister Johansen static void fuse_cleanup_submount_lookup(struct fuse_conn *fc,
1362939dd30SKrister Johansen 					 struct fuse_submount_lookup *sl)
1372939dd30SKrister Johansen {
1382939dd30SKrister Johansen 	if (!refcount_dec_and_test(&sl->count))
1392939dd30SKrister Johansen 		return;
1402939dd30SKrister Johansen 
1412939dd30SKrister Johansen 	fuse_queue_forget(fc, sl->forget, sl->nodeid, 1);
1422939dd30SKrister Johansen 	sl->forget = NULL;
1432939dd30SKrister Johansen 	kfree(sl);
1442939dd30SKrister Johansen }
1452939dd30SKrister Johansen 
fuse_evict_inode(struct inode * inode)146b57922d9SAl Viro static void fuse_evict_inode(struct inode *inode)
147d8a5ba45SMiklos Szeredi {
1489baf28bbSAl Viro 	struct fuse_inode *fi = get_fuse_inode(inode);
1499baf28bbSAl Viro 
1505c791fe1SMiklos Szeredi 	/* Will write inode on close/munmap and in all other dirtiers */
1515c791fe1SMiklos Szeredi 	WARN_ON(inode->i_state & I_DIRTY_INODE);
1525c791fe1SMiklos Szeredi 
15391b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
154dbd5768fSJan Kara 	clear_inode(inode);
1551751e8a6SLinus Torvalds 	if (inode->i_sb->s_flags & SB_ACTIVE) {
156e5e5558eSMiklos Szeredi 		struct fuse_conn *fc = get_fuse_conn(inode);
157c2d0ad00SVivek Goyal 
158c2d0ad00SVivek Goyal 		if (FUSE_IS_DAX(inode))
159c2d0ad00SVivek Goyal 			fuse_dax_inode_cleanup(inode);
1601866d779SMax Reitz 		if (fi->nlookup) {
1611866d779SMax Reitz 			fuse_queue_forget(fc, fi->forget, fi->nodeid,
1621866d779SMax Reitz 					  fi->nlookup);
16307e77dcaSMiklos Szeredi 			fi->forget = NULL;
164e5e5558eSMiklos Szeredi 		}
1652939dd30SKrister Johansen 
1662939dd30SKrister Johansen 		if (fi->submount_lookup) {
1672939dd30SKrister Johansen 			fuse_cleanup_submount_lookup(fc, fi->submount_lookup);
1682939dd30SKrister Johansen 			fi->submount_lookup = NULL;
1692939dd30SKrister Johansen 		}
1701866d779SMax Reitz 	}
1715d069dbeSMiklos Szeredi 	if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
1729baf28bbSAl Viro 		WARN_ON(!list_empty(&fi->write_files));
1739baf28bbSAl Viro 		WARN_ON(!list_empty(&fi->queued_writes));
1749baf28bbSAl Viro 	}
175d8a5ba45SMiklos Szeredi }
176d8a5ba45SMiklos Szeredi 
fuse_reconfigure(struct fs_context * fsc)17784c21507SMiklos Szeredi static int fuse_reconfigure(struct fs_context *fsc)
17871421259SMiklos Szeredi {
17984c21507SMiklos Szeredi 	struct super_block *sb = fsc->root->d_sb;
1800189a2d3SMiklos Szeredi 
18102b9984dSTheodore Ts'o 	sync_filesystem(sb);
18284c21507SMiklos Szeredi 	if (fsc->sb_flags & SB_MANDLOCK)
18371421259SMiklos Szeredi 		return -EINVAL;
18471421259SMiklos Szeredi 
18571421259SMiklos Szeredi 	return 0;
18671421259SMiklos Szeredi }
18771421259SMiklos Szeredi 
18845c72cd7SPavel Shilovsky /*
18945c72cd7SPavel Shilovsky  * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
19045c72cd7SPavel Shilovsky  * so that it will fit.
19145c72cd7SPavel Shilovsky  */
fuse_squash_ino(u64 ino64)19245c72cd7SPavel Shilovsky static ino_t fuse_squash_ino(u64 ino64)
19345c72cd7SPavel Shilovsky {
19445c72cd7SPavel Shilovsky 	ino_t ino = (ino_t) ino64;
19545c72cd7SPavel Shilovsky 	if (sizeof(ino_t) < sizeof(u64))
19645c72cd7SPavel Shilovsky 		ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
19745c72cd7SPavel Shilovsky 	return ino;
19845c72cd7SPavel Shilovsky }
19945c72cd7SPavel Shilovsky 
fuse_change_attributes_common(struct inode * inode,struct fuse_attr * attr,struct fuse_statx * sx,u64 attr_valid,u32 cache_mask)2003be5a52bSMiklos Szeredi void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
201972f4c46SMiklos Szeredi 				   struct fuse_statx *sx,
2024b52f059SMiklos Szeredi 				   u64 attr_valid, u32 cache_mask)
203d8a5ba45SMiklos Szeredi {
2049ffbb916SMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn(inode);
205ebc14c4dSMiklos Szeredi 	struct fuse_inode *fi = get_fuse_inode(inode);
206d8a5ba45SMiklos Szeredi 
207f15ecfefSKirill Tkhai 	lockdep_assert_held(&fi->lock);
208f15ecfefSKirill Tkhai 
2094510d86fSKirill Tkhai 	fi->attr_version = atomic64_inc_return(&fc->attr_version);
2101fb69e78SMiklos Szeredi 	fi->i_time = attr_valid;
211d3045530SMiklos Szeredi 	/* Clear basic stats from invalid mask */
212d3045530SMiklos Szeredi 	set_mask_bits(&fi->inval_mask, STATX_BASIC_STATS, 0);
2131fb69e78SMiklos Szeredi 
21445c72cd7SPavel Shilovsky 	inode->i_ino     = fuse_squash_ino(attr->ino);
215ebc14c4dSMiklos Szeredi 	inode->i_mode    = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
216bfe86848SMiklos Szeredi 	set_nlink(inode, attr->nlink);
2178cb08329SEric W. Biederman 	inode->i_uid     = make_kuid(fc->user_ns, attr->uid);
2188cb08329SEric W. Biederman 	inode->i_gid     = make_kgid(fc->user_ns, attr->gid);
219d8a5ba45SMiklos Szeredi 	inode->i_blocks  = attr->blocks;
22047912eaaSMiklos Szeredi 
22147912eaaSMiklos Szeredi 	/* Sanitize nsecs */
22247912eaaSMiklos Szeredi 	attr->atimensec = min_t(u32, attr->atimensec, NSEC_PER_SEC - 1);
22347912eaaSMiklos Szeredi 	attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
22447912eaaSMiklos Szeredi 	attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
22547912eaaSMiklos Szeredi 
226d8a5ba45SMiklos Szeredi 	inode->i_atime.tv_sec   = attr->atime;
227d8a5ba45SMiklos Szeredi 	inode->i_atime.tv_nsec  = attr->atimensec;
228b0aa7606SMaxim Patlasov 	/* mtime from server may be stale due to local buffered write */
2294b52f059SMiklos Szeredi 	if (!(cache_mask & STATX_MTIME)) {
230d8a5ba45SMiklos Szeredi 		inode->i_mtime.tv_sec   = attr->mtime;
231d8a5ba45SMiklos Szeredi 		inode->i_mtime.tv_nsec  = attr->mtimensec;
2324b52f059SMiklos Szeredi 	}
2334b52f059SMiklos Szeredi 	if (!(cache_mask & STATX_CTIME)) {
234ceb2d5e9SJeff Layton 		inode_set_ctime(inode, attr->ctime, attr->ctimensec);
23531f3267bSMaxim Patlasov 	}
236972f4c46SMiklos Szeredi 	if (sx) {
237972f4c46SMiklos Szeredi 		/* Sanitize nsecs */
238972f4c46SMiklos Szeredi 		sx->btime.tv_nsec =
239972f4c46SMiklos Szeredi 			min_t(u32, sx->btime.tv_nsec, NSEC_PER_SEC - 1);
240972f4c46SMiklos Szeredi 
241972f4c46SMiklos Szeredi 		/*
242972f4c46SMiklos Szeredi 		 * Btime has been queried, cache is valid (whether or not btime
243972f4c46SMiklos Szeredi 		 * is available or not) so clear STATX_BTIME from inval_mask.
244972f4c46SMiklos Szeredi 		 *
245972f4c46SMiklos Szeredi 		 * Availability of the btime attribute is indicated in
246972f4c46SMiklos Szeredi 		 * FUSE_I_BTIME
247972f4c46SMiklos Szeredi 		 */
248972f4c46SMiklos Szeredi 		set_mask_bits(&fi->inval_mask, STATX_BTIME, 0);
249972f4c46SMiklos Szeredi 		if (sx->mask & STATX_BTIME) {
250972f4c46SMiklos Szeredi 			set_bit(FUSE_I_BTIME, &fi->state);
251972f4c46SMiklos Szeredi 			fi->i_btime.tv_sec = sx->btime.tv_sec;
252972f4c46SMiklos Szeredi 			fi->i_btime.tv_nsec = sx->btime.tv_nsec;
253972f4c46SMiklos Szeredi 		}
254972f4c46SMiklos Szeredi 	}
255e00d2c2dSMiklos Szeredi 
2560e9663eeSMiklos Szeredi 	if (attr->blksize != 0)
2570e9663eeSMiklos Szeredi 		inode->i_blkbits = ilog2(attr->blksize);
2580e9663eeSMiklos Szeredi 	else
2590e9663eeSMiklos Szeredi 		inode->i_blkbits = inode->i_sb->s_blocksize_bits;
2600e9663eeSMiklos Szeredi 
261ebc14c4dSMiklos Szeredi 	/*
262ebc14c4dSMiklos Szeredi 	 * Don't set the sticky bit in i_mode, unless we want the VFS
263ebc14c4dSMiklos Szeredi 	 * to check permissions.  This prevents failures due to the
264ebc14c4dSMiklos Szeredi 	 * check in may_delete().
265ebc14c4dSMiklos Szeredi 	 */
266ebc14c4dSMiklos Szeredi 	fi->orig_i_mode = inode->i_mode;
26729433a29SMiklos Szeredi 	if (!fc->default_permissions)
268ebc14c4dSMiklos Szeredi 		inode->i_mode &= ~S_ISVTX;
26945c72cd7SPavel Shilovsky 
27045c72cd7SPavel Shilovsky 	fi->orig_ino = attr->ino;
2719d769e6aSVivek Goyal 
2729d769e6aSVivek Goyal 	/*
2739d769e6aSVivek Goyal 	 * We are refreshing inode data and it is possible that another
2749d769e6aSVivek Goyal 	 * client set suid/sgid or security.capability xattr. So clear
2759d769e6aSVivek Goyal 	 * S_NOSEC. Ideally, we could have cleared it only if suid/sgid
2769d769e6aSVivek Goyal 	 * was set or if security.capability xattr was set. But we don't
2779d769e6aSVivek Goyal 	 * know if security.capability has been set or not. So clear it
2789d769e6aSVivek Goyal 	 * anyway. Its less efficient but should be safe.
2799d769e6aSVivek Goyal 	 */
2809d769e6aSVivek Goyal 	inode->i_flags &= ~S_NOSEC;
2813be5a52bSMiklos Szeredi }
2823be5a52bSMiklos Szeredi 
fuse_get_cache_mask(struct inode * inode)2834b52f059SMiklos Szeredi u32 fuse_get_cache_mask(struct inode *inode)
2844b52f059SMiklos Szeredi {
2854b52f059SMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn(inode);
2864b52f059SMiklos Szeredi 
2874b52f059SMiklos Szeredi 	if (!fc->writeback_cache || !S_ISREG(inode->i_mode))
2884b52f059SMiklos Szeredi 		return 0;
2894b52f059SMiklos Szeredi 
2904b52f059SMiklos Szeredi 	return STATX_MTIME | STATX_CTIME | STATX_SIZE;
2914b52f059SMiklos Szeredi }
2924b52f059SMiklos Szeredi 
fuse_change_attributes(struct inode * inode,struct fuse_attr * attr,struct fuse_statx * sx,u64 attr_valid,u64 attr_version)2933be5a52bSMiklos Szeredi void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
294972f4c46SMiklos Szeredi 			    struct fuse_statx *sx,
2953be5a52bSMiklos Szeredi 			    u64 attr_valid, u64 attr_version)
2963be5a52bSMiklos Szeredi {
2973be5a52bSMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn(inode);
2983be5a52bSMiklos Szeredi 	struct fuse_inode *fi = get_fuse_inode(inode);
2994b52f059SMiklos Szeredi 	u32 cache_mask;
3003be5a52bSMiklos Szeredi 	loff_t oldsize;
301a64ba10fSArnd Bergmann 	struct timespec64 old_mtime;
3023be5a52bSMiklos Szeredi 
303f15ecfefSKirill Tkhai 	spin_lock(&fi->lock);
30404d82db0SMiklos Szeredi 	/*
30504d82db0SMiklos Szeredi 	 * In case of writeback_cache enabled, writes update mtime, ctime and
30604d82db0SMiklos Szeredi 	 * may update i_size.  In these cases trust the cached value in the
30704d82db0SMiklos Szeredi 	 * inode.
30804d82db0SMiklos Szeredi 	 */
3094b52f059SMiklos Szeredi 	cache_mask = fuse_get_cache_mask(inode);
3104b52f059SMiklos Szeredi 	if (cache_mask & STATX_SIZE)
31104d82db0SMiklos Szeredi 		attr->size = i_size_read(inode);
3124b52f059SMiklos Szeredi 
3134b52f059SMiklos Szeredi 	if (cache_mask & STATX_MTIME) {
31404d82db0SMiklos Szeredi 		attr->mtime = inode->i_mtime.tv_sec;
31504d82db0SMiklos Szeredi 		attr->mtimensec = inode->i_mtime.tv_nsec;
3164b52f059SMiklos Szeredi 	}
3174b52f059SMiklos Szeredi 	if (cache_mask & STATX_CTIME) {
318ceb2d5e9SJeff Layton 		attr->ctime = inode_get_ctime(inode).tv_sec;
319ceb2d5e9SJeff Layton 		attr->ctimensec = inode_get_ctime(inode).tv_nsec;
32004d82db0SMiklos Szeredi 	}
32104d82db0SMiklos Szeredi 
32206a7c3c2SMaxim Patlasov 	if ((attr_version != 0 && fi->attr_version > attr_version) ||
32306a7c3c2SMaxim Patlasov 	    test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
324f15ecfefSKirill Tkhai 		spin_unlock(&fi->lock);
3253be5a52bSMiklos Szeredi 		return;
3263be5a52bSMiklos Szeredi 	}
3273be5a52bSMiklos Szeredi 
328a64ba10fSArnd Bergmann 	old_mtime = inode->i_mtime;
329972f4c46SMiklos Szeredi 	fuse_change_attributes_common(inode, attr, sx, attr_valid, cache_mask);
330ebc14c4dSMiklos Szeredi 
331e00d2c2dSMiklos Szeredi 	oldsize = inode->i_size;
3328373200bSPavel Emelyanov 	/*
3338373200bSPavel Emelyanov 	 * In case of writeback_cache enabled, the cached writes beyond EOF
3348373200bSPavel Emelyanov 	 * extend local i_size without keeping userspace server in sync. So,
3358373200bSPavel Emelyanov 	 * attr->size coming from server can be stale. We cannot trust it.
3368373200bSPavel Emelyanov 	 */
3374b52f059SMiklos Szeredi 	if (!(cache_mask & STATX_SIZE))
338e00d2c2dSMiklos Szeredi 		i_size_write(inode, attr->size);
339f15ecfefSKirill Tkhai 	spin_unlock(&fi->lock);
340e00d2c2dSMiklos Szeredi 
3414b52f059SMiklos Szeredi 	if (!cache_mask && S_ISREG(inode->i_mode)) {
342eed2179eSBrian Foster 		bool inval = false;
343eed2179eSBrian Foster 
344eed2179eSBrian Foster 		if (oldsize != attr->size) {
3457caef267SKirill A. Shutemov 			truncate_pagecache(inode, attr->size);
346ad2ba64dSKirill Smelkov 			if (!fc->explicit_inval_data)
347eed2179eSBrian Foster 				inval = true;
348eed2179eSBrian Foster 		} else if (fc->auto_inval_data) {
349a64ba10fSArnd Bergmann 			struct timespec64 new_mtime = {
350eed2179eSBrian Foster 				.tv_sec = attr->mtime,
351eed2179eSBrian Foster 				.tv_nsec = attr->mtimensec,
352eed2179eSBrian Foster 			};
353eed2179eSBrian Foster 
354eed2179eSBrian Foster 			/*
355eed2179eSBrian Foster 			 * Auto inval mode also checks and invalidates if mtime
356eed2179eSBrian Foster 			 * has changed.
357eed2179eSBrian Foster 			 */
358a64ba10fSArnd Bergmann 			if (!timespec64_equal(&old_mtime, &new_mtime))
359eed2179eSBrian Foster 				inval = true;
360eed2179eSBrian Foster 		}
361eed2179eSBrian Foster 
362eed2179eSBrian Foster 		if (inval)
363b1009979SMiklos Szeredi 			invalidate_inode_pages2(inode->i_mapping);
364e00d2c2dSMiklos Szeredi 	}
365c3cb6f93SJeffle Xu 
366c3cb6f93SJeffle Xu 	if (IS_ENABLED(CONFIG_FUSE_DAX))
367c3cb6f93SJeffle Xu 		fuse_dax_dontcache(inode, attr->flags);
368d8a5ba45SMiklos Szeredi }
369d8a5ba45SMiklos Szeredi 
fuse_init_submount_lookup(struct fuse_submount_lookup * sl,u64 nodeid)3702939dd30SKrister Johansen static void fuse_init_submount_lookup(struct fuse_submount_lookup *sl,
3712939dd30SKrister Johansen 				      u64 nodeid)
3722939dd30SKrister Johansen {
3732939dd30SKrister Johansen 	sl->nodeid = nodeid;
3742939dd30SKrister Johansen 	refcount_set(&sl->count, 1);
3752939dd30SKrister Johansen }
3762939dd30SKrister Johansen 
fuse_init_inode(struct inode * inode,struct fuse_attr * attr,struct fuse_conn * fc)377facd6105SChristian Brauner static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr,
378facd6105SChristian Brauner 			    struct fuse_conn *fc)
379d8a5ba45SMiklos Szeredi {
380d8a5ba45SMiklos Szeredi 	inode->i_mode = attr->mode & S_IFMT;
3819ffbb916SMiklos Szeredi 	inode->i_size = attr->size;
382b0aa7606SMaxim Patlasov 	inode->i_mtime.tv_sec  = attr->mtime;
383b0aa7606SMaxim Patlasov 	inode->i_mtime.tv_nsec = attr->mtimensec;
384ceb2d5e9SJeff Layton 	inode_set_ctime(inode, attr->ctime, attr->ctimensec);
385e5e5558eSMiklos Szeredi 	if (S_ISREG(inode->i_mode)) {
386e5e5558eSMiklos Szeredi 		fuse_init_common(inode);
38793a497b9SJeffle Xu 		fuse_init_file_inode(inode, attr->flags);
388e5e5558eSMiklos Szeredi 	} else if (S_ISDIR(inode->i_mode))
389e5e5558eSMiklos Szeredi 		fuse_init_dir(inode);
390e5e5558eSMiklos Szeredi 	else if (S_ISLNK(inode->i_mode))
391e5e5558eSMiklos Szeredi 		fuse_init_symlink(inode);
392e5e5558eSMiklos Szeredi 	else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
393e5e5558eSMiklos Szeredi 		 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
394e5e5558eSMiklos Szeredi 		fuse_init_common(inode);
395e5e5558eSMiklos Szeredi 		init_special_inode(inode, inode->i_mode,
396e5e5558eSMiklos Szeredi 				   new_decode_dev(attr->rdev));
39739ee059aSMiklos Szeredi 	} else
39839ee059aSMiklos Szeredi 		BUG();
399facd6105SChristian Brauner 	/*
400facd6105SChristian Brauner 	 * Ensure that we don't cache acls for daemons without FUSE_POSIX_ACL
401facd6105SChristian Brauner 	 * so they see the exact same behavior as before.
402facd6105SChristian Brauner 	 */
403facd6105SChristian Brauner 	if (!fc->posix_acl)
404facd6105SChristian Brauner 		inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
405d8a5ba45SMiklos Szeredi }
406d8a5ba45SMiklos Szeredi 
fuse_inode_eq(struct inode * inode,void * _nodeidp)407fcee216bSMax Reitz static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
408d8a5ba45SMiklos Szeredi {
409b48badf0SMiklos Szeredi 	u64 nodeid = *(u64 *) _nodeidp;
410d8a5ba45SMiklos Szeredi 	if (get_node_id(inode) == nodeid)
411d8a5ba45SMiklos Szeredi 		return 1;
412d8a5ba45SMiklos Szeredi 	else
413d8a5ba45SMiklos Szeredi 		return 0;
414d8a5ba45SMiklos Szeredi }
415d8a5ba45SMiklos Szeredi 
fuse_inode_set(struct inode * inode,void * _nodeidp)416d8a5ba45SMiklos Szeredi static int fuse_inode_set(struct inode *inode, void *_nodeidp)
417d8a5ba45SMiklos Szeredi {
418b48badf0SMiklos Szeredi 	u64 nodeid = *(u64 *) _nodeidp;
419d8a5ba45SMiklos Szeredi 	get_fuse_inode(inode)->nodeid = nodeid;
420d8a5ba45SMiklos Szeredi 	return 0;
421d8a5ba45SMiklos Szeredi }
422d8a5ba45SMiklos Szeredi 
fuse_iget(struct super_block * sb,u64 nodeid,int generation,struct fuse_attr * attr,u64 attr_valid,u64 attr_version)423b48badf0SMiklos Szeredi struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
4241fb69e78SMiklos Szeredi 			int generation, struct fuse_attr *attr,
4251fb69e78SMiklos Szeredi 			u64 attr_valid, u64 attr_version)
426d8a5ba45SMiklos Szeredi {
427d8a5ba45SMiklos Szeredi 	struct inode *inode;
4289e6268dbSMiklos Szeredi 	struct fuse_inode *fi;
429d8a5ba45SMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn_super(sb);
430d8a5ba45SMiklos Szeredi 
431bf109c64SMax Reitz 	/*
432bf109c64SMax Reitz 	 * Auto mount points get their node id from the submount root, which is
433bf109c64SMax Reitz 	 * not a unique identifier within this filesystem.
434bf109c64SMax Reitz 	 *
435bf109c64SMax Reitz 	 * To avoid conflicts, do not place submount points into the inode hash
436bf109c64SMax Reitz 	 * table.
437bf109c64SMax Reitz 	 */
438bf109c64SMax Reitz 	if (fc->auto_submounts && (attr->flags & FUSE_ATTR_SUBMOUNT) &&
439bf109c64SMax Reitz 	    S_ISDIR(attr->mode)) {
4402939dd30SKrister Johansen 		struct fuse_inode *fi;
4412939dd30SKrister Johansen 
442bf109c64SMax Reitz 		inode = new_inode(sb);
443bf109c64SMax Reitz 		if (!inode)
444bf109c64SMax Reitz 			return NULL;
445bf109c64SMax Reitz 
446facd6105SChristian Brauner 		fuse_init_inode(inode, attr, fc);
4472939dd30SKrister Johansen 		fi = get_fuse_inode(inode);
4482939dd30SKrister Johansen 		fi->nodeid = nodeid;
4492939dd30SKrister Johansen 		fi->submount_lookup = fuse_alloc_submount_lookup();
4502939dd30SKrister Johansen 		if (!fi->submount_lookup) {
4512939dd30SKrister Johansen 			iput(inode);
4522939dd30SKrister Johansen 			return NULL;
4532939dd30SKrister Johansen 		}
4542939dd30SKrister Johansen 		/* Sets nlookup = 1 on fi->submount_lookup->nlookup */
4552939dd30SKrister Johansen 		fuse_init_submount_lookup(fi->submount_lookup, nodeid);
456bf109c64SMax Reitz 		inode->i_flags |= S_AUTOMOUNT;
457bf109c64SMax Reitz 		goto done;
458bf109c64SMax Reitz 	}
459bf109c64SMax Reitz 
460d8a5ba45SMiklos Szeredi retry:
461d8a5ba45SMiklos Szeredi 	inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
462d8a5ba45SMiklos Szeredi 	if (!inode)
463d8a5ba45SMiklos Szeredi 		return NULL;
464d8a5ba45SMiklos Szeredi 
465d8a5ba45SMiklos Szeredi 	if ((inode->i_state & I_NEW)) {
466b0aa7606SMaxim Patlasov 		inode->i_flags |= S_NOATIME;
467d31433c8SMaxim Patlasov 		if (!fc->writeback_cache || !S_ISREG(attr->mode))
468b0aa7606SMaxim Patlasov 			inode->i_flags |= S_NOCMTIME;
469d8a5ba45SMiklos Szeredi 		inode->i_generation = generation;
470facd6105SChristian Brauner 		fuse_init_inode(inode, attr, fc);
471d8a5ba45SMiklos Szeredi 		unlock_new_inode(inode);
47215db1683SAmir Goldstein 	} else if (fuse_stale_inode(inode, generation, attr)) {
47315db1683SAmir Goldstein 		/* nodeid was reused, any I/O on the old inode should fail */
4745d069dbeSMiklos Szeredi 		fuse_make_bad(inode);
4758b86779aSMiklos Szeredi 		if (inode != d_inode(sb->s_root)) {
4768b86779aSMiklos Szeredi 			remove_inode_hash(inode);
477d8a5ba45SMiklos Szeredi 			iput(inode);
478d8a5ba45SMiklos Szeredi 			goto retry;
479d8a5ba45SMiklos Szeredi 		}
4808b86779aSMiklos Szeredi 	}
4819e6268dbSMiklos Szeredi 	fi = get_fuse_inode(inode);
482c9d8f5f0SKirill Tkhai 	spin_lock(&fi->lock);
4839e6268dbSMiklos Szeredi 	fi->nlookup++;
484c9d8f5f0SKirill Tkhai 	spin_unlock(&fi->lock);
4852939dd30SKrister Johansen done:
486972f4c46SMiklos Szeredi 	fuse_change_attributes(inode, attr, NULL, attr_valid, attr_version);
4871fb69e78SMiklos Szeredi 
488d8a5ba45SMiklos Szeredi 	return inode;
489d8a5ba45SMiklos Szeredi }
490d8a5ba45SMiklos Szeredi 
fuse_ilookup(struct fuse_conn * fc,u64 nodeid,struct fuse_mount ** fm)491fcee216bSMax Reitz struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
492fcee216bSMax Reitz 			   struct fuse_mount **fm)
493fcee216bSMax Reitz {
494fcee216bSMax Reitz 	struct fuse_mount *fm_iter;
495fcee216bSMax Reitz 	struct inode *inode;
496fcee216bSMax Reitz 
497fcee216bSMax Reitz 	WARN_ON(!rwsem_is_locked(&fc->killsb));
498fcee216bSMax Reitz 	list_for_each_entry(fm_iter, &fc->mounts, fc_entry) {
499fcee216bSMax Reitz 		if (!fm_iter->sb)
500fcee216bSMax Reitz 			continue;
501fcee216bSMax Reitz 
502fcee216bSMax Reitz 		inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &nodeid);
503fcee216bSMax Reitz 		if (inode) {
504fcee216bSMax Reitz 			if (fm)
505fcee216bSMax Reitz 				*fm = fm_iter;
506fcee216bSMax Reitz 			return inode;
507fcee216bSMax Reitz 		}
508fcee216bSMax Reitz 	}
509fcee216bSMax Reitz 
510fcee216bSMax Reitz 	return NULL;
511fcee216bSMax Reitz }
512fcee216bSMax Reitz 
fuse_reverse_inval_inode(struct fuse_conn * fc,u64 nodeid,loff_t offset,loff_t len)513fcee216bSMax Reitz int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
5143b463ae0SJohn Muir 			     loff_t offset, loff_t len)
5153b463ae0SJohn Muir {
5165ddd9cedSMiklos Szeredi 	struct fuse_inode *fi;
5173b463ae0SJohn Muir 	struct inode *inode;
5183b463ae0SJohn Muir 	pgoff_t pg_start;
5193b463ae0SJohn Muir 	pgoff_t pg_end;
5203b463ae0SJohn Muir 
521fcee216bSMax Reitz 	inode = fuse_ilookup(fc, nodeid, NULL);
5223b463ae0SJohn Muir 	if (!inode)
5233b463ae0SJohn Muir 		return -ENOENT;
5243b463ae0SJohn Muir 
5255ddd9cedSMiklos Szeredi 	fi = get_fuse_inode(inode);
5265ddd9cedSMiklos Szeredi 	spin_lock(&fi->lock);
5275ddd9cedSMiklos Szeredi 	fi->attr_version = atomic64_inc_return(&fc->attr_version);
5285ddd9cedSMiklos Szeredi 	spin_unlock(&fi->lock);
5295ddd9cedSMiklos Szeredi 
5303b463ae0SJohn Muir 	fuse_invalidate_attr(inode);
53160bcc88aSSeth Forshee 	forget_all_cached_acls(inode);
5323b463ae0SJohn Muir 	if (offset >= 0) {
53309cbfeafSKirill A. Shutemov 		pg_start = offset >> PAGE_SHIFT;
5343b463ae0SJohn Muir 		if (len <= 0)
5353b463ae0SJohn Muir 			pg_end = -1;
5363b463ae0SJohn Muir 		else
53709cbfeafSKirill A. Shutemov 			pg_end = (offset + len - 1) >> PAGE_SHIFT;
5383b463ae0SJohn Muir 		invalidate_inode_pages2_range(inode->i_mapping,
5393b463ae0SJohn Muir 					      pg_start, pg_end);
5403b463ae0SJohn Muir 	}
5413b463ae0SJohn Muir 	iput(inode);
5423b463ae0SJohn Muir 	return 0;
5433b463ae0SJohn Muir }
5443b463ae0SJohn Muir 
fuse_lock_inode(struct inode * inode)54563576c13SMiklos Szeredi bool fuse_lock_inode(struct inode *inode)
5465c672ab3SMiklos Szeredi {
54763576c13SMiklos Szeredi 	bool locked = false;
54863576c13SMiklos Szeredi 
54963576c13SMiklos Szeredi 	if (!get_fuse_conn(inode)->parallel_dirops) {
5505c672ab3SMiklos Szeredi 		mutex_lock(&get_fuse_inode(inode)->mutex);
55163576c13SMiklos Szeredi 		locked = true;
5525c672ab3SMiklos Szeredi 	}
5535c672ab3SMiklos Szeredi 
55463576c13SMiklos Szeredi 	return locked;
55563576c13SMiklos Szeredi }
55663576c13SMiklos Szeredi 
fuse_unlock_inode(struct inode * inode,bool locked)55763576c13SMiklos Szeredi void fuse_unlock_inode(struct inode *inode, bool locked)
5585c672ab3SMiklos Szeredi {
55963576c13SMiklos Szeredi 	if (locked)
5605c672ab3SMiklos Szeredi 		mutex_unlock(&get_fuse_inode(inode)->mutex);
5615c672ab3SMiklos Szeredi }
5625c672ab3SMiklos Szeredi 
fuse_umount_begin(struct super_block * sb)56342faad99SAl Viro static void fuse_umount_begin(struct super_block *sb)
56469a53bf2SMiklos Szeredi {
56515c8e72eSVivek Goyal 	struct fuse_conn *fc = get_fuse_conn_super(sb);
56615c8e72eSVivek Goyal 
567247861c3SDaniil Lunev 	if (fc->no_force_umount)
568247861c3SDaniil Lunev 		return;
569247861c3SDaniil Lunev 
57015c8e72eSVivek Goyal 	fuse_abort_conn(fc);
571247861c3SDaniil Lunev 
572247861c3SDaniil Lunev 	// Only retire block-device-based superblocks.
573247861c3SDaniil Lunev 	if (sb->s_bdev != NULL)
574247861c3SDaniil Lunev 		retire_super(sb);
57569a53bf2SMiklos Szeredi }
57669a53bf2SMiklos Szeredi 
fuse_send_destroy(struct fuse_mount * fm)577fcee216bSMax Reitz static void fuse_send_destroy(struct fuse_mount *fm)
5780ec7ca41SMiklos Szeredi {
579fcee216bSMax Reitz 	if (fm->fc->conn_init) {
5801ccd1ea2SMiklos Szeredi 		FUSE_ARGS(args);
5811ccd1ea2SMiklos Szeredi 
5821ccd1ea2SMiklos Szeredi 		args.opcode = FUSE_DESTROY;
5831ccd1ea2SMiklos Szeredi 		args.force = true;
5841ccd1ea2SMiklos Szeredi 		args.nocreds = true;
585fcee216bSMax Reitz 		fuse_simple_request(fm, &args);
5860ec7ca41SMiklos Szeredi 	}
5870ec7ca41SMiklos Szeredi }
5880ec7ca41SMiklos Szeredi 
convert_fuse_statfs(struct kstatfs * stbuf,struct fuse_kstatfs * attr)589e5e5558eSMiklos Szeredi static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
590e5e5558eSMiklos Szeredi {
591e5e5558eSMiklos Szeredi 	stbuf->f_type    = FUSE_SUPER_MAGIC;
592e5e5558eSMiklos Szeredi 	stbuf->f_bsize   = attr->bsize;
593de5f1202SMiklos Szeredi 	stbuf->f_frsize  = attr->frsize;
594e5e5558eSMiklos Szeredi 	stbuf->f_blocks  = attr->blocks;
595e5e5558eSMiklos Szeredi 	stbuf->f_bfree   = attr->bfree;
596e5e5558eSMiklos Szeredi 	stbuf->f_bavail  = attr->bavail;
597e5e5558eSMiklos Szeredi 	stbuf->f_files   = attr->files;
598e5e5558eSMiklos Szeredi 	stbuf->f_ffree   = attr->ffree;
599e5e5558eSMiklos Szeredi 	stbuf->f_namelen = attr->namelen;
600e5e5558eSMiklos Szeredi 	/* fsid is left zero */
601e5e5558eSMiklos Szeredi }
602e5e5558eSMiklos Szeredi 
fuse_statfs(struct dentry * dentry,struct kstatfs * buf)603726c3342SDavid Howells static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
604e5e5558eSMiklos Szeredi {
605726c3342SDavid Howells 	struct super_block *sb = dentry->d_sb;
606fcee216bSMax Reitz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
6077078187aSMiklos Szeredi 	FUSE_ARGS(args);
608e5e5558eSMiklos Szeredi 	struct fuse_statfs_out outarg;
609e5e5558eSMiklos Szeredi 	int err;
610e5e5558eSMiklos Szeredi 
611fcee216bSMax Reitz 	if (!fuse_allow_current_process(fm->fc)) {
612e57ac683SMiklos Szeredi 		buf->f_type = FUSE_SUPER_MAGIC;
613e57ac683SMiklos Szeredi 		return 0;
614e57ac683SMiklos Szeredi 	}
615e57ac683SMiklos Szeredi 
616de5f1202SMiklos Szeredi 	memset(&outarg, 0, sizeof(outarg));
617d5b48543SMiklos Szeredi 	args.in_numargs = 0;
618d5b48543SMiklos Szeredi 	args.opcode = FUSE_STATFS;
619d5b48543SMiklos Szeredi 	args.nodeid = get_node_id(d_inode(dentry));
620d5b48543SMiklos Szeredi 	args.out_numargs = 1;
621d5b48543SMiklos Szeredi 	args.out_args[0].size = sizeof(outarg);
622d5b48543SMiklos Szeredi 	args.out_args[0].value = &outarg;
623fcee216bSMax Reitz 	err = fuse_simple_request(fm, &args);
624e5e5558eSMiklos Szeredi 	if (!err)
625e5e5558eSMiklos Szeredi 		convert_fuse_statfs(buf, &outarg.st);
626e5e5558eSMiklos Szeredi 	return err;
627e5e5558eSMiklos Szeredi }
628e5e5558eSMiklos Szeredi 
fuse_sync_bucket_alloc(void)629660585b5SMiklos Szeredi static struct fuse_sync_bucket *fuse_sync_bucket_alloc(void)
630660585b5SMiklos Szeredi {
631660585b5SMiklos Szeredi 	struct fuse_sync_bucket *bucket;
632660585b5SMiklos Szeredi 
633660585b5SMiklos Szeredi 	bucket = kzalloc(sizeof(*bucket), GFP_KERNEL | __GFP_NOFAIL);
634660585b5SMiklos Szeredi 	if (bucket) {
635660585b5SMiklos Szeredi 		init_waitqueue_head(&bucket->waitq);
636660585b5SMiklos Szeredi 		/* Initial active count */
637660585b5SMiklos Szeredi 		atomic_set(&bucket->count, 1);
638660585b5SMiklos Szeredi 	}
639660585b5SMiklos Szeredi 	return bucket;
640660585b5SMiklos Szeredi }
641660585b5SMiklos Szeredi 
fuse_sync_fs_writes(struct fuse_conn * fc)642660585b5SMiklos Szeredi static void fuse_sync_fs_writes(struct fuse_conn *fc)
643660585b5SMiklos Szeredi {
644660585b5SMiklos Szeredi 	struct fuse_sync_bucket *bucket, *new_bucket;
645660585b5SMiklos Szeredi 	int count;
646660585b5SMiklos Szeredi 
647660585b5SMiklos Szeredi 	new_bucket = fuse_sync_bucket_alloc();
648660585b5SMiklos Szeredi 	spin_lock(&fc->lock);
649660585b5SMiklos Szeredi 	bucket = rcu_dereference_protected(fc->curr_bucket, 1);
650660585b5SMiklos Szeredi 	count = atomic_read(&bucket->count);
651660585b5SMiklos Szeredi 	WARN_ON(count < 1);
652660585b5SMiklos Szeredi 	/* No outstanding writes? */
653660585b5SMiklos Szeredi 	if (count == 1) {
654660585b5SMiklos Szeredi 		spin_unlock(&fc->lock);
655660585b5SMiklos Szeredi 		kfree(new_bucket);
656660585b5SMiklos Szeredi 		return;
657660585b5SMiklos Szeredi 	}
658660585b5SMiklos Szeredi 
659660585b5SMiklos Szeredi 	/*
660660585b5SMiklos Szeredi 	 * Completion of new bucket depends on completion of this bucket, so add
661660585b5SMiklos Szeredi 	 * one more count.
662660585b5SMiklos Szeredi 	 */
663660585b5SMiklos Szeredi 	atomic_inc(&new_bucket->count);
664660585b5SMiklos Szeredi 	rcu_assign_pointer(fc->curr_bucket, new_bucket);
665660585b5SMiklos Szeredi 	spin_unlock(&fc->lock);
666660585b5SMiklos Szeredi 	/*
667660585b5SMiklos Szeredi 	 * Drop initial active count.  At this point if all writes in this and
668660585b5SMiklos Szeredi 	 * ancestor buckets complete, the count will go to zero and this task
669660585b5SMiklos Szeredi 	 * will be woken up.
670660585b5SMiklos Szeredi 	 */
671660585b5SMiklos Szeredi 	atomic_dec(&bucket->count);
672660585b5SMiklos Szeredi 
673660585b5SMiklos Szeredi 	wait_event(bucket->waitq, atomic_read(&bucket->count) == 0);
674660585b5SMiklos Szeredi 
675660585b5SMiklos Szeredi 	/* Drop temp count on descendant bucket */
676660585b5SMiklos Szeredi 	fuse_sync_bucket_dec(new_bucket);
677660585b5SMiklos Szeredi 	kfree_rcu(bucket, rcu);
678660585b5SMiklos Szeredi }
679660585b5SMiklos Szeredi 
fuse_sync_fs(struct super_block * sb,int wait)6802d82ab25SGreg Kurz static int fuse_sync_fs(struct super_block *sb, int wait)
6812d82ab25SGreg Kurz {
6822d82ab25SGreg Kurz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
6832d82ab25SGreg Kurz 	struct fuse_conn *fc = fm->fc;
6842d82ab25SGreg Kurz 	struct fuse_syncfs_in inarg;
6852d82ab25SGreg Kurz 	FUSE_ARGS(args);
6862d82ab25SGreg Kurz 	int err;
6872d82ab25SGreg Kurz 
6882d82ab25SGreg Kurz 	/*
6892d82ab25SGreg Kurz 	 * Userspace cannot handle the wait == 0 case.  Avoid a
6902d82ab25SGreg Kurz 	 * gratuitous roundtrip.
6912d82ab25SGreg Kurz 	 */
6922d82ab25SGreg Kurz 	if (!wait)
6932d82ab25SGreg Kurz 		return 0;
6942d82ab25SGreg Kurz 
6952d82ab25SGreg Kurz 	/* The filesystem is being unmounted.  Nothing to do. */
6962d82ab25SGreg Kurz 	if (!sb->s_root)
6972d82ab25SGreg Kurz 		return 0;
6982d82ab25SGreg Kurz 
6992d82ab25SGreg Kurz 	if (!fc->sync_fs)
7002d82ab25SGreg Kurz 		return 0;
7012d82ab25SGreg Kurz 
702660585b5SMiklos Szeredi 	fuse_sync_fs_writes(fc);
703660585b5SMiklos Szeredi 
7042d82ab25SGreg Kurz 	memset(&inarg, 0, sizeof(inarg));
7052d82ab25SGreg Kurz 	args.in_numargs = 1;
7062d82ab25SGreg Kurz 	args.in_args[0].size = sizeof(inarg);
7072d82ab25SGreg Kurz 	args.in_args[0].value = &inarg;
7082d82ab25SGreg Kurz 	args.opcode = FUSE_SYNCFS;
7092d82ab25SGreg Kurz 	args.nodeid = get_node_id(sb->s_root->d_inode);
7102d82ab25SGreg Kurz 	args.out_numargs = 0;
7112d82ab25SGreg Kurz 
7122d82ab25SGreg Kurz 	err = fuse_simple_request(fm, &args);
7132d82ab25SGreg Kurz 	if (err == -ENOSYS) {
7142d82ab25SGreg Kurz 		fc->sync_fs = 0;
7152d82ab25SGreg Kurz 		err = 0;
7162d82ab25SGreg Kurz 	}
7172d82ab25SGreg Kurz 
7182d82ab25SGreg Kurz 	return err;
7192d82ab25SGreg Kurz }
7202d82ab25SGreg Kurz 
721d8a5ba45SMiklos Szeredi enum {
722c30da2e9SDavid Howells 	OPT_SOURCE,
723c30da2e9SDavid Howells 	OPT_SUBTYPE,
724d8a5ba45SMiklos Szeredi 	OPT_FD,
725d8a5ba45SMiklos Szeredi 	OPT_ROOTMODE,
726d8a5ba45SMiklos Szeredi 	OPT_USER_ID,
72787729a55SMiklos Szeredi 	OPT_GROUP_ID,
728d8a5ba45SMiklos Szeredi 	OPT_DEFAULT_PERMISSIONS,
729d8a5ba45SMiklos Szeredi 	OPT_ALLOW_OTHER,
730db50b96cSMiklos Szeredi 	OPT_MAX_READ,
731d8091614SMiklos Szeredi 	OPT_BLKSIZE,
732d8a5ba45SMiklos Szeredi 	OPT_ERR
733d8a5ba45SMiklos Szeredi };
734d8a5ba45SMiklos Szeredi 
735d7167b14SAl Viro static const struct fs_parameter_spec fuse_fs_parameters[] = {
736c30da2e9SDavid Howells 	fsparam_string	("source",		OPT_SOURCE),
737c30da2e9SDavid Howells 	fsparam_u32	("fd",			OPT_FD),
738c30da2e9SDavid Howells 	fsparam_u32oct	("rootmode",		OPT_ROOTMODE),
739c30da2e9SDavid Howells 	fsparam_u32	("user_id",		OPT_USER_ID),
740c30da2e9SDavid Howells 	fsparam_u32	("group_id",		OPT_GROUP_ID),
741c30da2e9SDavid Howells 	fsparam_flag	("default_permissions",	OPT_DEFAULT_PERMISSIONS),
742c30da2e9SDavid Howells 	fsparam_flag	("allow_other",		OPT_ALLOW_OTHER),
743c30da2e9SDavid Howells 	fsparam_u32	("max_read",		OPT_MAX_READ),
744c30da2e9SDavid Howells 	fsparam_u32	("blksize",		OPT_BLKSIZE),
745c7eb6869SDavid Howells 	fsparam_string	("subtype",		OPT_SUBTYPE),
746c30da2e9SDavid Howells 	{}
747d8a5ba45SMiklos Szeredi };
748d8a5ba45SMiklos Szeredi 
fuse_parse_param(struct fs_context * fsc,struct fs_parameter * param)74984c21507SMiklos Szeredi static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
750233a01faSMiklos Szeredi {
751c30da2e9SDavid Howells 	struct fs_parse_result result;
75284c21507SMiklos Szeredi 	struct fuse_fs_context *ctx = fsc->fs_private;
753c30da2e9SDavid Howells 	int opt;
754fcebdc0dSEric Sandeen 	kuid_t kuid;
755fcebdc0dSEric Sandeen 	kgid_t kgid;
756233a01faSMiklos Szeredi 
75784c21507SMiklos Szeredi 	if (fsc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
758e8b20a47SMiklos Szeredi 		/*
759e8b20a47SMiklos Szeredi 		 * Ignore options coming from mount(MS_REMOUNT) for backward
760e8b20a47SMiklos Szeredi 		 * compatibility.
761e8b20a47SMiklos Szeredi 		 */
76284c21507SMiklos Szeredi 		if (fsc->oldapi)
763e8b20a47SMiklos Szeredi 			return 0;
764e8b20a47SMiklos Szeredi 
76584c21507SMiklos Szeredi 		return invalfc(fsc, "No changes allowed in reconfigure");
766b330966fSMiklos Szeredi 	}
767b330966fSMiklos Szeredi 
76884c21507SMiklos Szeredi 	opt = fs_parse(fsc, fuse_fs_parameters, param, &result);
769c30da2e9SDavid Howells 	if (opt < 0)
770c30da2e9SDavid Howells 		return opt;
771d8a5ba45SMiklos Szeredi 
772c30da2e9SDavid Howells 	switch (opt) {
773c30da2e9SDavid Howells 	case OPT_SOURCE:
77484c21507SMiklos Szeredi 		if (fsc->source)
77584c21507SMiklos Szeredi 			return invalfc(fsc, "Multiple sources specified");
77684c21507SMiklos Szeredi 		fsc->source = param->string;
777c30da2e9SDavid Howells 		param->string = NULL;
778c30da2e9SDavid Howells 		break;
779d8a5ba45SMiklos Szeredi 
780c30da2e9SDavid Howells 	case OPT_SUBTYPE:
781c30da2e9SDavid Howells 		if (ctx->subtype)
78284c21507SMiklos Szeredi 			return invalfc(fsc, "Multiple subtypes specified");
783c30da2e9SDavid Howells 		ctx->subtype = param->string;
784c30da2e9SDavid Howells 		param->string = NULL;
785d8a5ba45SMiklos Szeredi 		return 0;
786c30da2e9SDavid Howells 
787c30da2e9SDavid Howells 	case OPT_FD:
788c30da2e9SDavid Howells 		ctx->fd = result.uint_32;
789cabdb4faSzhengbin 		ctx->fd_present = true;
790d8a5ba45SMiklos Szeredi 		break;
791d8a5ba45SMiklos Szeredi 
792d8a5ba45SMiklos Szeredi 	case OPT_ROOTMODE:
793c30da2e9SDavid Howells 		if (!fuse_valid_type(result.uint_32))
79484c21507SMiklos Szeredi 			return invalfc(fsc, "Invalid rootmode");
795c30da2e9SDavid Howells 		ctx->rootmode = result.uint_32;
796cabdb4faSzhengbin 		ctx->rootmode_present = true;
797d8a5ba45SMiklos Szeredi 		break;
798d8a5ba45SMiklos Szeredi 
799d8a5ba45SMiklos Szeredi 	case OPT_USER_ID:
800fcebdc0dSEric Sandeen 		kuid =  make_kuid(fsc->user_ns, result.uint_32);
801fcebdc0dSEric Sandeen 		if (!uid_valid(kuid))
80284c21507SMiklos Szeredi 			return invalfc(fsc, "Invalid user_id");
803fcebdc0dSEric Sandeen 		/*
804fcebdc0dSEric Sandeen 		 * The requested uid must be representable in the
805fcebdc0dSEric Sandeen 		 * filesystem's idmapping.
806fcebdc0dSEric Sandeen 		 */
807fcebdc0dSEric Sandeen 		if (!kuid_has_mapping(fsc->user_ns, kuid))
808fcebdc0dSEric Sandeen 			return invalfc(fsc, "Invalid user_id");
809fcebdc0dSEric Sandeen 		ctx->user_id = kuid;
810cabdb4faSzhengbin 		ctx->user_id_present = true;
811d8a5ba45SMiklos Szeredi 		break;
812d8a5ba45SMiklos Szeredi 
81387729a55SMiklos Szeredi 	case OPT_GROUP_ID:
814fcebdc0dSEric Sandeen 		kgid = make_kgid(fsc->user_ns, result.uint_32);;
815fcebdc0dSEric Sandeen 		if (!gid_valid(kgid))
81684c21507SMiklos Szeredi 			return invalfc(fsc, "Invalid group_id");
817fcebdc0dSEric Sandeen 		/*
818fcebdc0dSEric Sandeen 		 * The requested gid must be representable in the
819fcebdc0dSEric Sandeen 		 * filesystem's idmapping.
820fcebdc0dSEric Sandeen 		 */
821fcebdc0dSEric Sandeen 		if (!kgid_has_mapping(fsc->user_ns, kgid))
822fcebdc0dSEric Sandeen 			return invalfc(fsc, "Invalid group_id");
823fcebdc0dSEric Sandeen 		ctx->group_id = kgid;
824cabdb4faSzhengbin 		ctx->group_id_present = true;
82587729a55SMiklos Szeredi 		break;
82687729a55SMiklos Szeredi 
8271e9a4ed9SMiklos Szeredi 	case OPT_DEFAULT_PERMISSIONS:
828cabdb4faSzhengbin 		ctx->default_permissions = true;
8291e9a4ed9SMiklos Szeredi 		break;
8301e9a4ed9SMiklos Szeredi 
8311e9a4ed9SMiklos Szeredi 	case OPT_ALLOW_OTHER:
832cabdb4faSzhengbin 		ctx->allow_other = true;
8331e9a4ed9SMiklos Szeredi 		break;
8341e9a4ed9SMiklos Szeredi 
835db50b96cSMiklos Szeredi 	case OPT_MAX_READ:
836c30da2e9SDavid Howells 		ctx->max_read = result.uint_32;
837db50b96cSMiklos Szeredi 		break;
838db50b96cSMiklos Szeredi 
839d8091614SMiklos Szeredi 	case OPT_BLKSIZE:
840c30da2e9SDavid Howells 		if (!ctx->is_bdev)
84184c21507SMiklos Szeredi 			return invalfc(fsc, "blksize only supported for fuseblk");
842c30da2e9SDavid Howells 		ctx->blksize = result.uint_32;
843d8091614SMiklos Szeredi 		break;
844d8091614SMiklos Szeredi 
845d8a5ba45SMiklos Szeredi 	default:
846c30da2e9SDavid Howells 		return -EINVAL;
847d8a5ba45SMiklos Szeredi 	}
8485a533682SMiklos Szeredi 
849d8a5ba45SMiklos Szeredi 	return 0;
850c30da2e9SDavid Howells }
851d8a5ba45SMiklos Szeredi 
fuse_free_fsc(struct fs_context * fsc)85284c21507SMiklos Szeredi static void fuse_free_fsc(struct fs_context *fsc)
853c30da2e9SDavid Howells {
85484c21507SMiklos Szeredi 	struct fuse_fs_context *ctx = fsc->fs_private;
855c30da2e9SDavid Howells 
856c30da2e9SDavid Howells 	if (ctx) {
857c30da2e9SDavid Howells 		kfree(ctx->subtype);
858c30da2e9SDavid Howells 		kfree(ctx);
859c30da2e9SDavid Howells 	}
860d8a5ba45SMiklos Szeredi }
861d8a5ba45SMiklos Szeredi 
fuse_show_options(struct seq_file * m,struct dentry * root)86234c80b1dSAl Viro static int fuse_show_options(struct seq_file *m, struct dentry *root)
863d8a5ba45SMiklos Szeredi {
86434c80b1dSAl Viro 	struct super_block *sb = root->d_sb;
86534c80b1dSAl Viro 	struct fuse_conn *fc = get_fuse_conn_super(sb);
866d8a5ba45SMiklos Szeredi 
867f4fd4ae3SVivek Goyal 	if (fc->legacy_opts_show) {
868f4fd4ae3SVivek Goyal 		seq_printf(m, ",user_id=%u",
869f4fd4ae3SVivek Goyal 			   from_kuid_munged(fc->user_ns, fc->user_id));
870f4fd4ae3SVivek Goyal 		seq_printf(m, ",group_id=%u",
871f4fd4ae3SVivek Goyal 			   from_kgid_munged(fc->user_ns, fc->group_id));
87229433a29SMiklos Szeredi 		if (fc->default_permissions)
8731e9a4ed9SMiklos Szeredi 			seq_puts(m, ",default_permissions");
87429433a29SMiklos Szeredi 		if (fc->allow_other)
8751e9a4ed9SMiklos Szeredi 			seq_puts(m, ",allow_other");
876db50b96cSMiklos Szeredi 		if (fc->max_read != ~0)
877db50b96cSMiklos Szeredi 			seq_printf(m, ",max_read=%u", fc->max_read);
87834c80b1dSAl Viro 		if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
87934c80b1dSAl Viro 			seq_printf(m, ",blksize=%lu", sb->s_blocksize);
880f4fd4ae3SVivek Goyal 	}
8811dd53957SVivek Goyal #ifdef CONFIG_FUSE_DAX
882780b1b95SJeffle Xu 	if (fc->dax_mode == FUSE_DAX_ALWAYS)
883780b1b95SJeffle Xu 		seq_puts(m, ",dax=always");
884780b1b95SJeffle Xu 	else if (fc->dax_mode == FUSE_DAX_NEVER)
885780b1b95SJeffle Xu 		seq_puts(m, ",dax=never");
886780b1b95SJeffle Xu 	else if (fc->dax_mode == FUSE_DAX_INODE_USER)
887780b1b95SJeffle Xu 		seq_puts(m, ",dax=inode");
8881dd53957SVivek Goyal #endif
8891dd53957SVivek Goyal 
890d8a5ba45SMiklos Szeredi 	return 0;
891d8a5ba45SMiklos Szeredi }
892d8a5ba45SMiklos Szeredi 
fuse_iqueue_init(struct fuse_iqueue * fiq,const struct fuse_iqueue_ops * ops,void * priv)893ae3aad77SStefan Hajnoczi static void fuse_iqueue_init(struct fuse_iqueue *fiq,
894ae3aad77SStefan Hajnoczi 			     const struct fuse_iqueue_ops *ops,
895ae3aad77SStefan Hajnoczi 			     void *priv)
896f88996a9SMiklos Szeredi {
897f88996a9SMiklos Szeredi 	memset(fiq, 0, sizeof(struct fuse_iqueue));
89876e43c8cSEric Biggers 	spin_lock_init(&fiq->lock);
899f88996a9SMiklos Szeredi 	init_waitqueue_head(&fiq->waitq);
900f88996a9SMiklos Szeredi 	INIT_LIST_HEAD(&fiq->pending);
901f88996a9SMiklos Szeredi 	INIT_LIST_HEAD(&fiq->interrupts);
902f88996a9SMiklos Szeredi 	fiq->forget_list_tail = &fiq->forget_list_head;
903e16714d8SMiklos Szeredi 	fiq->connected = 1;
904ae3aad77SStefan Hajnoczi 	fiq->ops = ops;
905ae3aad77SStefan Hajnoczi 	fiq->priv = priv;
906f88996a9SMiklos Szeredi }
907f88996a9SMiklos Szeredi 
fuse_pqueue_init(struct fuse_pqueue * fpq)9083a2b5b9cSMiklos Szeredi static void fuse_pqueue_init(struct fuse_pqueue *fpq)
9093a2b5b9cSMiklos Szeredi {
910be2ff42cSKirill Tkhai 	unsigned int i;
911be2ff42cSKirill Tkhai 
91245a91cb1SMiklos Szeredi 	spin_lock_init(&fpq->lock);
913be2ff42cSKirill Tkhai 	for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
914be2ff42cSKirill Tkhai 		INIT_LIST_HEAD(&fpq->processing[i]);
9153a2b5b9cSMiklos Szeredi 	INIT_LIST_HEAD(&fpq->io);
916e96edd94SMiklos Szeredi 	fpq->connected = 1;
9173a2b5b9cSMiklos Szeredi }
9183a2b5b9cSMiklos Szeredi 
fuse_conn_init(struct fuse_conn * fc,struct fuse_mount * fm,struct user_namespace * user_ns,const struct fuse_iqueue_ops * fiq_ops,void * fiq_priv)919fcee216bSMax Reitz void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
920fcee216bSMax Reitz 		    struct user_namespace *user_ns,
921ae3aad77SStefan Hajnoczi 		    const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
922d8a5ba45SMiklos Szeredi {
9230d179aa5STejun Heo 	memset(fc, 0, sizeof(*fc));
924d7133114SMiklos Szeredi 	spin_lock_init(&fc->lock);
925ae2dffa3SKirill Tkhai 	spin_lock_init(&fc->bg_lock);
9263b463ae0SJohn Muir 	init_rwsem(&fc->killsb);
927095fc40aSElena Reshetova 	refcount_set(&fc->count, 1);
928c3696046SMiklos Szeredi 	atomic_set(&fc->dev_count, 1);
92908a53cdcSMiklos Szeredi 	init_waitqueue_head(&fc->blocked_waitq);
930ae3aad77SStefan Hajnoczi 	fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
931d12def1bSMiklos Szeredi 	INIT_LIST_HEAD(&fc->bg_queue);
9320d179aa5STejun Heo 	INIT_LIST_HEAD(&fc->entry);
933cc080e9eSMiklos Szeredi 	INIT_LIST_HEAD(&fc->devices);
934095da6cbSMiklos Szeredi 	atomic_set(&fc->num_waiting, 0);
9357a6d3c8bSCsaba Henk 	fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
9367a6d3c8bSCsaba Henk 	fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
93775126f55SMiklos Szeredi 	atomic64_set(&fc->khctr, 0);
93895668a69STejun Heo 	fc->polled_files = RB_ROOT;
9390aada884SMaxim Patlasov 	fc->blocked = 0;
940796523fbSMaxim Patlasov 	fc->initialized = 0;
941e16714d8SMiklos Szeredi 	fc->connected = 1;
9424510d86fSKirill Tkhai 	atomic64_set(&fc->attr_version, 1);
9439c8ef561SMiklos Szeredi 	get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
9440b6e9ea0SSeth Forshee 	fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
9458cb08329SEric W. Biederman 	fc->user_ns = get_user_ns(user_ns);
9468a3177dbSMiklos Szeredi 	fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
947a7f0d7aaSConnor Kuehl 	fc->max_pages_limit = FUSE_MAX_MAX_PAGES;
948fcee216bSMax Reitz 
949fcee216bSMax Reitz 	INIT_LIST_HEAD(&fc->mounts);
950fcee216bSMax Reitz 	list_add(&fm->fc_entry, &fc->mounts);
951fcee216bSMax Reitz 	fm->fc = fc;
952d8a5ba45SMiklos Szeredi }
9530d179aa5STejun Heo EXPORT_SYMBOL_GPL(fuse_conn_init);
954d8a5ba45SMiklos Szeredi 
delayed_release(struct rcu_head * p)955*535e9bd0SAl Viro static void delayed_release(struct rcu_head *p)
956*535e9bd0SAl Viro {
957*535e9bd0SAl Viro 	struct fuse_conn *fc = container_of(p, struct fuse_conn, rcu);
958*535e9bd0SAl Viro 
959*535e9bd0SAl Viro 	put_user_ns(fc->user_ns);
960*535e9bd0SAl Viro 	fc->release(fc);
961*535e9bd0SAl Viro }
962*535e9bd0SAl Viro 
fuse_conn_put(struct fuse_conn * fc)963bafa9654SMiklos Szeredi void fuse_conn_put(struct fuse_conn *fc)
964bafa9654SMiklos Szeredi {
965095fc40aSElena Reshetova 	if (refcount_dec_and_test(&fc->count)) {
966a62a8ef9SStefan Hajnoczi 		struct fuse_iqueue *fiq = &fc->iq;
967660585b5SMiklos Szeredi 		struct fuse_sync_bucket *bucket;
968a62a8ef9SStefan Hajnoczi 
9691dd53957SVivek Goyal 		if (IS_ENABLED(CONFIG_FUSE_DAX))
9701dd53957SVivek Goyal 			fuse_dax_conn_free(fc);
971a62a8ef9SStefan Hajnoczi 		if (fiq->ops->release)
972a62a8ef9SStefan Hajnoczi 			fiq->ops->release(fiq);
9730b6e9ea0SSeth Forshee 		put_pid_ns(fc->pid_ns);
974660585b5SMiklos Szeredi 		bucket = rcu_dereference_protected(fc->curr_bucket, 1);
975660585b5SMiklos Szeredi 		if (bucket) {
976660585b5SMiklos Szeredi 			WARN_ON(atomic_read(&bucket->count) != 1);
977660585b5SMiklos Szeredi 			kfree(bucket);
978660585b5SMiklos Szeredi 		}
979*535e9bd0SAl Viro 		call_rcu(&fc->rcu, delayed_release);
980bafa9654SMiklos Szeredi 	}
981d2a85164SMiklos Szeredi }
98208cbf542STejun Heo EXPORT_SYMBOL_GPL(fuse_conn_put);
983bafa9654SMiklos Szeredi 
fuse_conn_get(struct fuse_conn * fc)984bafa9654SMiklos Szeredi struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
985bafa9654SMiklos Szeredi {
986095fc40aSElena Reshetova 	refcount_inc(&fc->count);
987bafa9654SMiklos Szeredi 	return fc;
988bafa9654SMiklos Szeredi }
98908cbf542STejun Heo EXPORT_SYMBOL_GPL(fuse_conn_get);
990bafa9654SMiklos Szeredi 
fuse_get_root_inode(struct super_block * sb,unsigned mode)991b93f858aSTejun Heo static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
992d8a5ba45SMiklos Szeredi {
993d8a5ba45SMiklos Szeredi 	struct fuse_attr attr;
994d8a5ba45SMiklos Szeredi 	memset(&attr, 0, sizeof(attr));
995d8a5ba45SMiklos Szeredi 
996d8a5ba45SMiklos Szeredi 	attr.mode = mode;
997d8a5ba45SMiklos Szeredi 	attr.ino = FUSE_ROOT_ID;
998074406faSMiklos Szeredi 	attr.nlink = 1;
9991fb69e78SMiklos Szeredi 	return fuse_iget(sb, 1, 0, &attr, 0, 0);
1000d8a5ba45SMiklos Szeredi }
1001d8a5ba45SMiklos Szeredi 
10021729a16cSMiklos Szeredi struct fuse_inode_handle {
1003dbd561d2SMiklos Szeredi 	u64 nodeid;
1004dbd561d2SMiklos Szeredi 	u32 generation;
1005dbd561d2SMiklos Szeredi };
1006dbd561d2SMiklos Szeredi 
fuse_get_dentry(struct super_block * sb,struct fuse_inode_handle * handle)1007dbd561d2SMiklos Szeredi static struct dentry *fuse_get_dentry(struct super_block *sb,
1008dbd561d2SMiklos Szeredi 				      struct fuse_inode_handle *handle)
1009dbd561d2SMiklos Szeredi {
101033670fa2SMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn_super(sb);
1011dbd561d2SMiklos Szeredi 	struct inode *inode;
1012dbd561d2SMiklos Szeredi 	struct dentry *entry;
1013dbd561d2SMiklos Szeredi 	int err = -ESTALE;
1014dbd561d2SMiklos Szeredi 
1015dbd561d2SMiklos Szeredi 	if (handle->nodeid == 0)
1016dbd561d2SMiklos Szeredi 		goto out_err;
1017dbd561d2SMiklos Szeredi 
1018dbd561d2SMiklos Szeredi 	inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
101933670fa2SMiklos Szeredi 	if (!inode) {
102033670fa2SMiklos Szeredi 		struct fuse_entry_out outarg;
102113983d06SAl Viro 		const struct qstr name = QSTR_INIT(".", 1);
102233670fa2SMiklos Szeredi 
102333670fa2SMiklos Szeredi 		if (!fc->export_support)
1024dbd561d2SMiklos Szeredi 			goto out_err;
102533670fa2SMiklos Szeredi 
102633670fa2SMiklos Szeredi 		err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
102733670fa2SMiklos Szeredi 				       &inode);
102833670fa2SMiklos Szeredi 		if (err && err != -ENOENT)
102933670fa2SMiklos Szeredi 			goto out_err;
103033670fa2SMiklos Szeredi 		if (err || !inode) {
103133670fa2SMiklos Szeredi 			err = -ESTALE;
103233670fa2SMiklos Szeredi 			goto out_err;
103333670fa2SMiklos Szeredi 		}
103433670fa2SMiklos Szeredi 		err = -EIO;
103533670fa2SMiklos Szeredi 		if (get_node_id(inode) != handle->nodeid)
103633670fa2SMiklos Szeredi 			goto out_iput;
103733670fa2SMiklos Szeredi 	}
1038dbd561d2SMiklos Szeredi 	err = -ESTALE;
1039dbd561d2SMiklos Szeredi 	if (inode->i_generation != handle->generation)
1040dbd561d2SMiklos Szeredi 		goto out_iput;
1041dbd561d2SMiklos Szeredi 
104244003728SChristoph Hellwig 	entry = d_obtain_alias(inode);
1043c35eebe9SAl Viro 	if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
1044dbd561d2SMiklos Szeredi 		fuse_invalidate_entry_cache(entry);
1045dbd561d2SMiklos Szeredi 
1046dbd561d2SMiklos Szeredi 	return entry;
1047dbd561d2SMiklos Szeredi 
1048dbd561d2SMiklos Szeredi  out_iput:
1049dbd561d2SMiklos Szeredi 	iput(inode);
1050dbd561d2SMiklos Szeredi  out_err:
1051dbd561d2SMiklos Szeredi 	return ERR_PTR(err);
1052dbd561d2SMiklos Szeredi }
1053dbd561d2SMiklos Szeredi 
fuse_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)1054b0b0382bSAl Viro static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
1055b0b0382bSAl Viro 			   struct inode *parent)
1056dbd561d2SMiklos Szeredi {
1057b0b0382bSAl Viro 	int len = parent ? 6 : 3;
1058dbd561d2SMiklos Szeredi 	u64 nodeid;
1059dbd561d2SMiklos Szeredi 	u32 generation;
1060dbd561d2SMiklos Szeredi 
10615fe0c237SAneesh Kumar K.V 	if (*max_len < len) {
10625fe0c237SAneesh Kumar K.V 		*max_len = len;
106394e07a75SNamjae Jeon 		return  FILEID_INVALID;
10645fe0c237SAneesh Kumar K.V 	}
1065dbd561d2SMiklos Szeredi 
1066dbd561d2SMiklos Szeredi 	nodeid = get_fuse_inode(inode)->nodeid;
1067dbd561d2SMiklos Szeredi 	generation = inode->i_generation;
1068dbd561d2SMiklos Szeredi 
1069dbd561d2SMiklos Szeredi 	fh[0] = (u32)(nodeid >> 32);
1070dbd561d2SMiklos Szeredi 	fh[1] = (u32)(nodeid & 0xffffffff);
1071dbd561d2SMiklos Szeredi 	fh[2] = generation;
1072dbd561d2SMiklos Szeredi 
1073b0b0382bSAl Viro 	if (parent) {
1074dbd561d2SMiklos Szeredi 		nodeid = get_fuse_inode(parent)->nodeid;
1075dbd561d2SMiklos Szeredi 		generation = parent->i_generation;
1076dbd561d2SMiklos Szeredi 
1077dbd561d2SMiklos Szeredi 		fh[3] = (u32)(nodeid >> 32);
1078dbd561d2SMiklos Szeredi 		fh[4] = (u32)(nodeid & 0xffffffff);
1079dbd561d2SMiklos Szeredi 		fh[5] = generation;
1080dbd561d2SMiklos Szeredi 	}
1081dbd561d2SMiklos Szeredi 
1082dbd561d2SMiklos Szeredi 	*max_len = len;
1083b0b0382bSAl Viro 	return parent ? 0x82 : 0x81;
1084dbd561d2SMiklos Szeredi }
1085dbd561d2SMiklos Szeredi 
fuse_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1086dbd561d2SMiklos Szeredi static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
1087dbd561d2SMiklos Szeredi 		struct fid *fid, int fh_len, int fh_type)
1088dbd561d2SMiklos Szeredi {
1089dbd561d2SMiklos Szeredi 	struct fuse_inode_handle handle;
1090dbd561d2SMiklos Szeredi 
1091dbd561d2SMiklos Szeredi 	if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
1092dbd561d2SMiklos Szeredi 		return NULL;
1093dbd561d2SMiklos Szeredi 
1094dbd561d2SMiklos Szeredi 	handle.nodeid = (u64) fid->raw[0] << 32;
1095dbd561d2SMiklos Szeredi 	handle.nodeid |= (u64) fid->raw[1];
1096dbd561d2SMiklos Szeredi 	handle.generation = fid->raw[2];
1097dbd561d2SMiklos Szeredi 	return fuse_get_dentry(sb, &handle);
1098dbd561d2SMiklos Szeredi }
1099dbd561d2SMiklos Szeredi 
fuse_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1100dbd561d2SMiklos Szeredi static struct dentry *fuse_fh_to_parent(struct super_block *sb,
1101dbd561d2SMiklos Szeredi 		struct fid *fid, int fh_len, int fh_type)
1102dbd561d2SMiklos Szeredi {
1103dbd561d2SMiklos Szeredi 	struct fuse_inode_handle parent;
1104dbd561d2SMiklos Szeredi 
1105dbd561d2SMiklos Szeredi 	if (fh_type != 0x82 || fh_len < 6)
1106dbd561d2SMiklos Szeredi 		return NULL;
1107dbd561d2SMiklos Szeredi 
1108dbd561d2SMiklos Szeredi 	parent.nodeid = (u64) fid->raw[3] << 32;
1109dbd561d2SMiklos Szeredi 	parent.nodeid |= (u64) fid->raw[4];
1110dbd561d2SMiklos Szeredi 	parent.generation = fid->raw[5];
1111dbd561d2SMiklos Szeredi 	return fuse_get_dentry(sb, &parent);
1112dbd561d2SMiklos Szeredi }
1113dbd561d2SMiklos Szeredi 
fuse_get_parent(struct dentry * child)111433670fa2SMiklos Szeredi static struct dentry *fuse_get_parent(struct dentry *child)
111533670fa2SMiklos Szeredi {
11162b0143b5SDavid Howells 	struct inode *child_inode = d_inode(child);
111733670fa2SMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn(child_inode);
111833670fa2SMiklos Szeredi 	struct inode *inode;
111933670fa2SMiklos Szeredi 	struct dentry *parent;
112033670fa2SMiklos Szeredi 	struct fuse_entry_out outarg;
112133670fa2SMiklos Szeredi 	int err;
112233670fa2SMiklos Szeredi 
112333670fa2SMiklos Szeredi 	if (!fc->export_support)
112433670fa2SMiklos Szeredi 		return ERR_PTR(-ESTALE);
112533670fa2SMiklos Szeredi 
112633670fa2SMiklos Szeredi 	err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
112780e5d1ffSAl Viro 			       &dotdot_name, &outarg, &inode);
112844003728SChristoph Hellwig 	if (err) {
112944003728SChristoph Hellwig 		if (err == -ENOENT)
113033670fa2SMiklos Szeredi 			return ERR_PTR(-ESTALE);
113144003728SChristoph Hellwig 		return ERR_PTR(err);
113233670fa2SMiklos Szeredi 	}
113344003728SChristoph Hellwig 
113444003728SChristoph Hellwig 	parent = d_obtain_alias(inode);
1135c35eebe9SAl Viro 	if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
113633670fa2SMiklos Szeredi 		fuse_invalidate_entry_cache(parent);
113733670fa2SMiklos Szeredi 
113833670fa2SMiklos Szeredi 	return parent;
113933670fa2SMiklos Szeredi }
1140dbd561d2SMiklos Szeredi 
1141dbd561d2SMiklos Szeredi static const struct export_operations fuse_export_operations = {
1142dbd561d2SMiklos Szeredi 	.fh_to_dentry	= fuse_fh_to_dentry,
1143dbd561d2SMiklos Szeredi 	.fh_to_parent	= fuse_fh_to_parent,
1144dbd561d2SMiklos Szeredi 	.encode_fh	= fuse_encode_fh,
114533670fa2SMiklos Szeredi 	.get_parent	= fuse_get_parent,
1146dbd561d2SMiklos Szeredi };
1147dbd561d2SMiklos Szeredi 
1148ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations fuse_super_operations = {
1149d8a5ba45SMiklos Szeredi 	.alloc_inode    = fuse_alloc_inode,
11509baf28bbSAl Viro 	.free_inode     = fuse_free_inode,
1151b57922d9SAl Viro 	.evict_inode	= fuse_evict_inode,
11521e18bda8SMiklos Szeredi 	.write_inode	= fuse_write_inode,
1153ead5f0b5SMiklos Szeredi 	.drop_inode	= generic_delete_inode,
115469a53bf2SMiklos Szeredi 	.umount_begin	= fuse_umount_begin,
1155e5e5558eSMiklos Szeredi 	.statfs		= fuse_statfs,
11562d82ab25SGreg Kurz 	.sync_fs	= fuse_sync_fs,
1157d8a5ba45SMiklos Szeredi 	.show_options	= fuse_show_options,
1158d8a5ba45SMiklos Szeredi };
1159d8a5ba45SMiklos Szeredi 
sanitize_global_limit(unsigned * limit)1160487ea5afSCsaba Henk static void sanitize_global_limit(unsigned *limit)
1161487ea5afSCsaba Henk {
1162f22f812dSMiklos Szeredi 	/*
1163f22f812dSMiklos Szeredi 	 * The default maximum number of async requests is calculated to consume
1164f22f812dSMiklos Szeredi 	 * 1/2^13 of the total memory, assuming 392 bytes per request.
1165f22f812dSMiklos Szeredi 	 */
1166487ea5afSCsaba Henk 	if (*limit == 0)
1167f22f812dSMiklos Szeredi 		*limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
1168487ea5afSCsaba Henk 
1169487ea5afSCsaba Henk 	if (*limit >= 1 << 16)
1170487ea5afSCsaba Henk 		*limit = (1 << 16) - 1;
1171487ea5afSCsaba Henk }
1172487ea5afSCsaba Henk 
set_global_limit(const char * val,const struct kernel_param * kp)1173e4dca7b7SKees Cook static int set_global_limit(const char *val, const struct kernel_param *kp)
1174487ea5afSCsaba Henk {
1175487ea5afSCsaba Henk 	int rv;
1176487ea5afSCsaba Henk 
1177487ea5afSCsaba Henk 	rv = param_set_uint(val, kp);
1178487ea5afSCsaba Henk 	if (rv)
1179487ea5afSCsaba Henk 		return rv;
1180487ea5afSCsaba Henk 
1181487ea5afSCsaba Henk 	sanitize_global_limit((unsigned *)kp->arg);
1182487ea5afSCsaba Henk 
1183487ea5afSCsaba Henk 	return 0;
1184487ea5afSCsaba Henk }
1185487ea5afSCsaba Henk 
process_init_limits(struct fuse_conn * fc,struct fuse_init_out * arg)1186487ea5afSCsaba Henk static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
1187487ea5afSCsaba Henk {
1188487ea5afSCsaba Henk 	int cap_sys_admin = capable(CAP_SYS_ADMIN);
1189487ea5afSCsaba Henk 
1190487ea5afSCsaba Henk 	if (arg->minor < 13)
1191487ea5afSCsaba Henk 		return;
1192487ea5afSCsaba Henk 
1193487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_bgreq);
1194487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_congthresh);
1195487ea5afSCsaba Henk 
1196ae2dffa3SKirill Tkhai 	spin_lock(&fc->bg_lock);
1197487ea5afSCsaba Henk 	if (arg->max_background) {
1198487ea5afSCsaba Henk 		fc->max_background = arg->max_background;
1199487ea5afSCsaba Henk 
1200487ea5afSCsaba Henk 		if (!cap_sys_admin && fc->max_background > max_user_bgreq)
1201487ea5afSCsaba Henk 			fc->max_background = max_user_bgreq;
1202487ea5afSCsaba Henk 	}
1203487ea5afSCsaba Henk 	if (arg->congestion_threshold) {
1204487ea5afSCsaba Henk 		fc->congestion_threshold = arg->congestion_threshold;
1205487ea5afSCsaba Henk 
1206487ea5afSCsaba Henk 		if (!cap_sys_admin &&
1207487ea5afSCsaba Henk 		    fc->congestion_threshold > max_user_congthresh)
1208487ea5afSCsaba Henk 			fc->congestion_threshold = max_user_congthresh;
1209487ea5afSCsaba Henk 	}
1210ae2dffa3SKirill Tkhai 	spin_unlock(&fc->bg_lock);
1211487ea5afSCsaba Henk }
1212487ea5afSCsaba Henk 
1213615047efSMiklos Szeredi struct fuse_init_args {
1214615047efSMiklos Szeredi 	struct fuse_args args;
1215615047efSMiklos Szeredi 	struct fuse_init_in in;
1216615047efSMiklos Szeredi 	struct fuse_init_out out;
1217615047efSMiklos Szeredi };
12189b9a0469SMiklos Szeredi 
process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)1219fcee216bSMax Reitz static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
1220615047efSMiklos Szeredi 			       int error)
1221615047efSMiklos Szeredi {
1222fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1223615047efSMiklos Szeredi 	struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
1224615047efSMiklos Szeredi 	struct fuse_init_out *arg = &ia->out;
1225fd1a1dc6SStefan Hajnoczi 	bool ok = true;
1226615047efSMiklos Szeredi 
1227615047efSMiklos Szeredi 	if (error || arg->major != FUSE_KERNEL_VERSION)
1228fd1a1dc6SStefan Hajnoczi 		ok = false;
12299b9a0469SMiklos Szeredi 	else {
12309cd68455SMiklos Szeredi 		unsigned long ra_pages;
12319cd68455SMiklos Szeredi 
1232487ea5afSCsaba Henk 		process_init_limits(fc, arg);
1233487ea5afSCsaba Henk 
12349cd68455SMiklos Szeredi 		if (arg->minor >= 6) {
12353066ff93SBernd Schubert 			u64 flags = arg->flags;
12363066ff93SBernd Schubert 
12373066ff93SBernd Schubert 			if (flags & FUSE_INIT_EXT)
12383066ff93SBernd Schubert 				flags |= (u64) arg->flags2 << 32;
123953db2893SMiklos Szeredi 
124009cbfeafSKirill A. Shutemov 			ra_pages = arg->max_readahead / PAGE_SIZE;
124153db2893SMiklos Szeredi 			if (flags & FUSE_ASYNC_READ)
12429cd68455SMiklos Szeredi 				fc->async_read = 1;
124353db2893SMiklos Szeredi 			if (!(flags & FUSE_POSIX_LOCKS))
124471421259SMiklos Szeredi 				fc->no_lock = 1;
124537fb3a30SMiklos Szeredi 			if (arg->minor >= 17) {
124653db2893SMiklos Szeredi 				if (!(flags & FUSE_FLOCK_LOCKS))
124737fb3a30SMiklos Szeredi 					fc->no_flock = 1;
124824114504SMiklos Szeredi 			} else {
124953db2893SMiklos Szeredi 				if (!(flags & FUSE_POSIX_LOCKS))
125024114504SMiklos Szeredi 					fc->no_flock = 1;
125137fb3a30SMiklos Szeredi 			}
125253db2893SMiklos Szeredi 			if (flags & FUSE_ATOMIC_O_TRUNC)
12536ff958edSMiklos Szeredi 				fc->atomic_o_trunc = 1;
125433670fa2SMiklos Szeredi 			if (arg->minor >= 9) {
125533670fa2SMiklos Szeredi 				/* LOOKUP has dependency on proto version */
125653db2893SMiklos Szeredi 				if (flags & FUSE_EXPORT_SUPPORT)
125733670fa2SMiklos Szeredi 					fc->export_support = 1;
125833670fa2SMiklos Szeredi 			}
125953db2893SMiklos Szeredi 			if (flags & FUSE_BIG_WRITES)
126078bb6cb9SMiklos Szeredi 				fc->big_writes = 1;
126153db2893SMiklos Szeredi 			if (flags & FUSE_DONT_MASK)
1262e0a43ddcSMiklos Szeredi 				fc->dont_mask = 1;
126353db2893SMiklos Szeredi 			if (flags & FUSE_AUTO_INVAL_DATA)
126472d0d248SBrian Foster 				fc->auto_inval_data = 1;
126553db2893SMiklos Szeredi 			else if (flags & FUSE_EXPLICIT_INVAL_DATA)
1266ad2ba64dSKirill Smelkov 				fc->explicit_inval_data = 1;
126753db2893SMiklos Szeredi 			if (flags & FUSE_DO_READDIRPLUS) {
12680b05b183SAnand V. Avati 				fc->do_readdirplus = 1;
126953db2893SMiklos Szeredi 				if (flags & FUSE_READDIRPLUS_AUTO)
1270634734b6SEric Wong 					fc->readdirplus_auto = 1;
127128420dadSMiklos Szeredi 			}
127253db2893SMiklos Szeredi 			if (flags & FUSE_ASYNC_DIO)
127360b9df7aSMiklos Szeredi 				fc->async_dio = 1;
127453db2893SMiklos Szeredi 			if (flags & FUSE_WRITEBACK_CACHE)
12754d99ff8fSPavel Emelyanov 				fc->writeback_cache = 1;
127653db2893SMiklos Szeredi 			if (flags & FUSE_PARALLEL_DIROPS)
12775c672ab3SMiklos Szeredi 				fc->parallel_dirops = 1;
127853db2893SMiklos Szeredi 			if (flags & FUSE_HANDLE_KILLPRIV)
12795e940c1dSMiklos Szeredi 				fc->handle_killpriv = 1;
1280e27c9d38SMiklos Szeredi 			if (arg->time_gran && arg->time_gran <= 1000000000)
1281fcee216bSMax Reitz 				fm->sb->s_time_gran = arg->time_gran;
128253db2893SMiklos Szeredi 			if ((flags & FUSE_POSIX_ACL)) {
128329433a29SMiklos Szeredi 				fc->default_permissions = 1;
128460bcc88aSSeth Forshee 				fc->posix_acl = 1;
128560bcc88aSSeth Forshee 			}
128653db2893SMiklos Szeredi 			if (flags & FUSE_CACHE_SYMLINKS)
12875571f1e6SDan Schatzberg 				fc->cache_symlinks = 1;
128853db2893SMiklos Szeredi 			if (flags & FUSE_ABORT_ERROR)
12893b7008b2SSzymon Lukasz 				fc->abort_err = 1;
129053db2893SMiklos Szeredi 			if (flags & FUSE_MAX_PAGES) {
12915da784ccSConstantine Shulyupin 				fc->max_pages =
1292a7f0d7aaSConnor Kuehl 					min_t(unsigned int, fc->max_pages_limit,
12935da784ccSConstantine Shulyupin 					max_t(unsigned int, arg->max_pages, 1));
12945da784ccSConstantine Shulyupin 			}
12952ee019faSJeffle Xu 			if (IS_ENABLED(CONFIG_FUSE_DAX)) {
12962ee019faSJeffle Xu 				if (flags & FUSE_MAP_ALIGNMENT &&
1297fd1a1dc6SStefan Hajnoczi 				    !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1298fd1a1dc6SStefan Hajnoczi 					ok = false;
1299fd1a1dc6SStefan Hajnoczi 				}
13002ee019faSJeffle Xu 				if (flags & FUSE_HAS_INODE_DAX)
13012ee019faSJeffle Xu 					fc->inode_dax = 1;
13022ee019faSJeffle Xu 			}
130353db2893SMiklos Szeredi 			if (flags & FUSE_HANDLE_KILLPRIV_V2) {
130463f9909fSVivek Goyal 				fc->handle_killpriv_v2 = 1;
13059d769e6aSVivek Goyal 				fm->sb->s_flags |= SB_NOSEC;
13069d769e6aSVivek Goyal 			}
130753db2893SMiklos Szeredi 			if (flags & FUSE_SETXATTR_EXT)
130852a4c95fSVivek Goyal 				fc->setxattr_ext = 1;
13093e2b6fdbSVivek Goyal 			if (flags & FUSE_SECURITY_CTX)
13103e2b6fdbSVivek Goyal 				fc->init_security = 1;
13118ed7cb3fSMiklos Szeredi 			if (flags & FUSE_CREATE_SUPP_GROUP)
13128ed7cb3fSMiklos Szeredi 				fc->create_supp_group = 1;
13139f36c1c5STyler Fanelli 			if (flags & FUSE_DIRECT_IO_ALLOW_MMAP)
13149f36c1c5STyler Fanelli 				fc->direct_io_allow_mmap = 1;
131571421259SMiklos Szeredi 		} else {
131609cbfeafSKirill A. Shutemov 			ra_pages = fc->max_read / PAGE_SIZE;
131771421259SMiklos Szeredi 			fc->no_lock = 1;
131837fb3a30SMiklos Szeredi 			fc->no_flock = 1;
131971421259SMiklos Szeredi 		}
13209cd68455SMiklos Szeredi 
1321fcee216bSMax Reitz 		fm->sb->s_bdi->ra_pages =
1322fcee216bSMax Reitz 				min(fm->sb->s_bdi->ra_pages, ra_pages);
13239b9a0469SMiklos Szeredi 		fc->minor = arg->minor;
13249b9a0469SMiklos Szeredi 		fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
1325f948d564SMiklos Szeredi 		fc->max_write = max_t(unsigned, 4096, fc->max_write);
13260ec7ca41SMiklos Szeredi 		fc->conn_init = 1;
13279b9a0469SMiklos Szeredi 	}
1328615047efSMiklos Szeredi 	kfree(ia);
1329615047efSMiklos Szeredi 
1330fd1a1dc6SStefan Hajnoczi 	if (!ok) {
1331fd1a1dc6SStefan Hajnoczi 		fc->conn_init = 0;
1332fd1a1dc6SStefan Hajnoczi 		fc->conn_error = 1;
1333fd1a1dc6SStefan Hajnoczi 	}
1334fd1a1dc6SStefan Hajnoczi 
13359759bd51SMiklos Szeredi 	fuse_set_initialized(fc);
133608a53cdcSMiklos Szeredi 	wake_up_all(&fc->blocked_waitq);
13379b9a0469SMiklos Szeredi }
13389b9a0469SMiklos Szeredi 
fuse_send_init(struct fuse_mount * fm)1339fcee216bSMax Reitz void fuse_send_init(struct fuse_mount *fm)
13409b9a0469SMiklos Szeredi {
1341615047efSMiklos Szeredi 	struct fuse_init_args *ia;
134253db2893SMiklos Szeredi 	u64 flags;
1343095da6cbSMiklos Szeredi 
1344615047efSMiklos Szeredi 	ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1345615047efSMiklos Szeredi 
1346615047efSMiklos Szeredi 	ia->in.major = FUSE_KERNEL_VERSION;
1347615047efSMiklos Szeredi 	ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1348fcee216bSMax Reitz 	ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
134953db2893SMiklos Szeredi 	flags =
1350615047efSMiklos Szeredi 		FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
135137fb3a30SMiklos Szeredi 		FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
135269fe05c9SMiklos Szeredi 		FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
13539446385fSWei Fang 		FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
13544d99ff8fSPavel Emelyanov 		FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
13555c672ab3SMiklos Szeredi 		FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
13563b7008b2SSzymon Lukasz 		FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1357d9a9ea94SChad Austin 		FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
135863f9909fSVivek Goyal 		FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
13593e2b6fdbSVivek Goyal 		FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
13605cadfbd5SMiklos Szeredi 		FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
13619f36c1c5STyler Fanelli 		FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP;
1362fd1a1dc6SStefan Hajnoczi #ifdef CONFIG_FUSE_DAX
1363fcee216bSMax Reitz 	if (fm->fc->dax)
136453db2893SMiklos Szeredi 		flags |= FUSE_MAP_ALIGNMENT;
13652ee019faSJeffle Xu 	if (fuse_is_inode_dax_mode(fm->fc->dax_mode))
13662ee019faSJeffle Xu 		flags |= FUSE_HAS_INODE_DAX;
1367fd1a1dc6SStefan Hajnoczi #endif
1368bf109c64SMax Reitz 	if (fm->fc->auto_submounts)
136953db2893SMiklos Szeredi 		flags |= FUSE_SUBMOUNTS;
137053db2893SMiklos Szeredi 
137153db2893SMiklos Szeredi 	ia->in.flags = flags;
137253db2893SMiklos Szeredi 	ia->in.flags2 = flags >> 32;
1373bf109c64SMax Reitz 
1374615047efSMiklos Szeredi 	ia->args.opcode = FUSE_INIT;
1375615047efSMiklos Szeredi 	ia->args.in_numargs = 1;
1376615047efSMiklos Szeredi 	ia->args.in_args[0].size = sizeof(ia->in);
1377615047efSMiklos Szeredi 	ia->args.in_args[0].value = &ia->in;
1378615047efSMiklos Szeredi 	ia->args.out_numargs = 1;
13793ad2f3fbSDaniel Mack 	/* Variable length argument used for backward compatibility
13809b9a0469SMiklos Szeredi 	   with interface version < 7.5.  Rest of init_out is zeroed
13819b9a0469SMiklos Szeredi 	   by do_get_request(), so a short reply is not a problem */
1382cabdb4faSzhengbin 	ia->args.out_argvar = true;
1383615047efSMiklos Szeredi 	ia->args.out_args[0].size = sizeof(ia->out);
1384615047efSMiklos Szeredi 	ia->args.out_args[0].value = &ia->out;
1385615047efSMiklos Szeredi 	ia->args.force = true;
1386615047efSMiklos Szeredi 	ia->args.nocreds = true;
1387615047efSMiklos Szeredi 	ia->args.end = process_init_reply;
1388615047efSMiklos Szeredi 
1389fcee216bSMax Reitz 	if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1390fcee216bSMax Reitz 		process_init_reply(fm, &ia->args, -ENOTCONN);
13919b9a0469SMiklos Szeredi }
139295a84cdbSVivek Goyal EXPORT_SYMBOL_GPL(fuse_send_init);
13939b9a0469SMiklos Szeredi 
fuse_free_conn(struct fuse_conn * fc)1394783863d6SMiklos Szeredi void fuse_free_conn(struct fuse_conn *fc)
139543901aabSTejun Heo {
1396cc080e9eSMiklos Szeredi 	WARN_ON(!list_empty(&fc->devices));
1397*535e9bd0SAl Viro 	kfree(fc);
139843901aabSTejun Heo }
1399783863d6SMiklos Szeredi EXPORT_SYMBOL_GPL(fuse_free_conn);
140043901aabSTejun Heo 
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1401a325f9b9STejun Heo static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1402a325f9b9STejun Heo {
1403a325f9b9STejun Heo 	int err;
14045f7f7543SJan Kara 	char *suffix = "";
1405a325f9b9STejun Heo 
140669c8ebf8SJan Kara 	if (sb->s_bdev) {
14075f7f7543SJan Kara 		suffix = "-fuseblk";
140869c8ebf8SJan Kara 		/*
140969c8ebf8SJan Kara 		 * sb->s_bdi points to blkdev's bdi however we want to redirect
141069c8ebf8SJan Kara 		 * it to our private bdi...
141169c8ebf8SJan Kara 		 */
141269c8ebf8SJan Kara 		bdi_put(sb->s_bdi);
141369c8ebf8SJan Kara 		sb->s_bdi = &noop_backing_dev_info;
141469c8ebf8SJan Kara 	}
14155f7f7543SJan Kara 	err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
14165f7f7543SJan Kara 				   MINOR(fc->dev), suffix);
1417a325f9b9STejun Heo 	if (err)
1418a325f9b9STejun Heo 		return err;
1419a325f9b9STejun Heo 
14205f7f7543SJan Kara 	/* fuse does it's own writeback accounting */
1421823423efSChristoph Hellwig 	sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT;
1422823423efSChristoph Hellwig 	sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
14235f7f7543SJan Kara 
1424a325f9b9STejun Heo 	/*
1425a325f9b9STejun Heo 	 * For a single fuse filesystem use max 1% of dirty +
1426a325f9b9STejun Heo 	 * writeback threshold.
1427a325f9b9STejun Heo 	 *
1428a325f9b9STejun Heo 	 * This gives about 1M of write buffer for memory maps on a
1429a325f9b9STejun Heo 	 * machine with 1G and 10% dirty_ratio, which should be more
1430a325f9b9STejun Heo 	 * than enough.
1431a325f9b9STejun Heo 	 *
1432a325f9b9STejun Heo 	 * Privileged users can raise it by writing to
1433a325f9b9STejun Heo 	 *
1434a325f9b9STejun Heo 	 *    /sys/class/bdi/<bdi>/max_ratio
1435a325f9b9STejun Heo 	 */
14365f7f7543SJan Kara 	bdi_set_max_ratio(sb->s_bdi, 1);
1437a325f9b9STejun Heo 
1438a325f9b9STejun Heo 	return 0;
1439a325f9b9STejun Heo }
1440a325f9b9STejun Heo 
fuse_dev_alloc(void)14410cd1eb9aSVivek Goyal struct fuse_dev *fuse_dev_alloc(void)
1442cc080e9eSMiklos Szeredi {
1443cc080e9eSMiklos Szeredi 	struct fuse_dev *fud;
1444be2ff42cSKirill Tkhai 	struct list_head *pq;
1445cc080e9eSMiklos Szeredi 
1446cc080e9eSMiklos Szeredi 	fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1447be2ff42cSKirill Tkhai 	if (!fud)
1448be2ff42cSKirill Tkhai 		return NULL;
1449be2ff42cSKirill Tkhai 
1450be2ff42cSKirill Tkhai 	pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1451be2ff42cSKirill Tkhai 	if (!pq) {
1452be2ff42cSKirill Tkhai 		kfree(fud);
1453be2ff42cSKirill Tkhai 		return NULL;
1454be2ff42cSKirill Tkhai 	}
1455be2ff42cSKirill Tkhai 
1456be2ff42cSKirill Tkhai 	fud->pq.processing = pq;
1457c3696046SMiklos Szeredi 	fuse_pqueue_init(&fud->pq);
1458cc080e9eSMiklos Szeredi 
1459cc080e9eSMiklos Szeredi 	return fud;
1460cc080e9eSMiklos Szeredi }
1461cc080e9eSMiklos Szeredi EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1462cc080e9eSMiklos Szeredi 
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)14630cd1eb9aSVivek Goyal void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
14640cd1eb9aSVivek Goyal {
14650cd1eb9aSVivek Goyal 	fud->fc = fuse_conn_get(fc);
14660cd1eb9aSVivek Goyal 	spin_lock(&fc->lock);
14670cd1eb9aSVivek Goyal 	list_add_tail(&fud->entry, &fc->devices);
14680cd1eb9aSVivek Goyal 	spin_unlock(&fc->lock);
14690cd1eb9aSVivek Goyal }
14700cd1eb9aSVivek Goyal EXPORT_SYMBOL_GPL(fuse_dev_install);
14710cd1eb9aSVivek Goyal 
fuse_dev_alloc_install(struct fuse_conn * fc)14720cd1eb9aSVivek Goyal struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
14730cd1eb9aSVivek Goyal {
14740cd1eb9aSVivek Goyal 	struct fuse_dev *fud;
14750cd1eb9aSVivek Goyal 
14760cd1eb9aSVivek Goyal 	fud = fuse_dev_alloc();
14770cd1eb9aSVivek Goyal 	if (!fud)
14780cd1eb9aSVivek Goyal 		return NULL;
14790cd1eb9aSVivek Goyal 
14800cd1eb9aSVivek Goyal 	fuse_dev_install(fud, fc);
14810cd1eb9aSVivek Goyal 	return fud;
14820cd1eb9aSVivek Goyal }
14830cd1eb9aSVivek Goyal EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
14840cd1eb9aSVivek Goyal 
fuse_dev_free(struct fuse_dev * fud)1485cc080e9eSMiklos Szeredi void fuse_dev_free(struct fuse_dev *fud)
1486cc080e9eSMiklos Szeredi {
1487cc080e9eSMiklos Szeredi 	struct fuse_conn *fc = fud->fc;
1488cc080e9eSMiklos Szeredi 
1489cc080e9eSMiklos Szeredi 	if (fc) {
1490cc080e9eSMiklos Szeredi 		spin_lock(&fc->lock);
1491cc080e9eSMiklos Szeredi 		list_del(&fud->entry);
1492cc080e9eSMiklos Szeredi 		spin_unlock(&fc->lock);
1493cc080e9eSMiklos Szeredi 
1494cc080e9eSMiklos Szeredi 		fuse_conn_put(fc);
1495cc080e9eSMiklos Szeredi 	}
1496d72f70daSTakeshi Misawa 	kfree(fud->pq.processing);
1497cc080e9eSMiklos Szeredi 	kfree(fud);
1498cc080e9eSMiklos Szeredi }
1499cc080e9eSMiklos Szeredi EXPORT_SYMBOL_GPL(fuse_dev_free);
1500cc080e9eSMiklos Szeredi 
fuse_fill_attr_from_inode(struct fuse_attr * attr,const struct fuse_inode * fi)15011866d779SMax Reitz static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
15021866d779SMax Reitz 				      const struct fuse_inode *fi)
15031866d779SMax Reitz {
1504ceb2d5e9SJeff Layton 	struct timespec64 ctime = inode_get_ctime(&fi->inode);
1505ceb2d5e9SJeff Layton 
15061866d779SMax Reitz 	*attr = (struct fuse_attr){
15071866d779SMax Reitz 		.ino		= fi->inode.i_ino,
15081866d779SMax Reitz 		.size		= fi->inode.i_size,
15091866d779SMax Reitz 		.blocks		= fi->inode.i_blocks,
15101866d779SMax Reitz 		.atime		= fi->inode.i_atime.tv_sec,
15111866d779SMax Reitz 		.mtime		= fi->inode.i_mtime.tv_sec,
1512ceb2d5e9SJeff Layton 		.ctime		= ctime.tv_sec,
15131866d779SMax Reitz 		.atimensec	= fi->inode.i_atime.tv_nsec,
15141866d779SMax Reitz 		.mtimensec	= fi->inode.i_mtime.tv_nsec,
1515ceb2d5e9SJeff Layton 		.ctimensec	= ctime.tv_nsec,
15161866d779SMax Reitz 		.mode		= fi->inode.i_mode,
15171866d779SMax Reitz 		.nlink		= fi->inode.i_nlink,
15181866d779SMax Reitz 		.uid		= fi->inode.i_uid.val,
15191866d779SMax Reitz 		.gid		= fi->inode.i_gid.val,
15201866d779SMax Reitz 		.rdev		= fi->inode.i_rdev,
15211866d779SMax Reitz 		.blksize	= 1u << fi->inode.i_blkbits,
15221866d779SMax Reitz 	};
15231866d779SMax Reitz }
15241866d779SMax Reitz 
fuse_sb_defaults(struct super_block * sb)15251866d779SMax Reitz static void fuse_sb_defaults(struct super_block *sb)
15261866d779SMax Reitz {
15271866d779SMax Reitz 	sb->s_magic = FUSE_SUPER_MAGIC;
15281866d779SMax Reitz 	sb->s_op = &fuse_super_operations;
15291866d779SMax Reitz 	sb->s_xattr = fuse_xattr_handlers;
15301866d779SMax Reitz 	sb->s_maxbytes = MAX_LFS_FILESIZE;
15311866d779SMax Reitz 	sb->s_time_gran = 1;
15321866d779SMax Reitz 	sb->s_export_op = &fuse_export_operations;
15331866d779SMax Reitz 	sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
15341866d779SMax Reitz 	if (sb->s_user_ns != &init_user_ns)
15351866d779SMax Reitz 		sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
15361866d779SMax Reitz 	sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
15371866d779SMax Reitz }
15381866d779SMax Reitz 
fuse_fill_super_submount(struct super_block * sb,struct fuse_inode * parent_fi)15391b539917SGreg Kurz static int fuse_fill_super_submount(struct super_block *sb,
15401866d779SMax Reitz 				    struct fuse_inode *parent_fi)
15411866d779SMax Reitz {
15421866d779SMax Reitz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
15431866d779SMax Reitz 	struct super_block *parent_sb = parent_fi->inode.i_sb;
15441866d779SMax Reitz 	struct fuse_attr root_attr;
15451866d779SMax Reitz 	struct inode *root;
15462939dd30SKrister Johansen 	struct fuse_submount_lookup *sl;
15472939dd30SKrister Johansen 	struct fuse_inode *fi;
15481866d779SMax Reitz 
15491866d779SMax Reitz 	fuse_sb_defaults(sb);
15501866d779SMax Reitz 	fm->sb = sb;
15511866d779SMax Reitz 
15521866d779SMax Reitz 	WARN_ON(sb->s_bdi != &noop_backing_dev_info);
15531866d779SMax Reitz 	sb->s_bdi = bdi_get(parent_sb->s_bdi);
15541866d779SMax Reitz 
15551866d779SMax Reitz 	sb->s_xattr = parent_sb->s_xattr;
15561866d779SMax Reitz 	sb->s_time_gran = parent_sb->s_time_gran;
15571866d779SMax Reitz 	sb->s_blocksize = parent_sb->s_blocksize;
15581866d779SMax Reitz 	sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
15591866d779SMax Reitz 	sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
15601866d779SMax Reitz 	if (parent_sb->s_subtype && !sb->s_subtype)
15611866d779SMax Reitz 		return -ENOMEM;
15621866d779SMax Reitz 
15631866d779SMax Reitz 	fuse_fill_attr_from_inode(&root_attr, parent_fi);
15641866d779SMax Reitz 	root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
15651866d779SMax Reitz 	/*
15661866d779SMax Reitz 	 * This inode is just a duplicate, so it is not looked up and
15671866d779SMax Reitz 	 * its nlookup should not be incremented.  fuse_iget() does
15681866d779SMax Reitz 	 * that, though, so undo it here.
15691866d779SMax Reitz 	 */
15702939dd30SKrister Johansen 	fi = get_fuse_inode(root);
15712939dd30SKrister Johansen 	fi->nlookup--;
15722939dd30SKrister Johansen 
15731866d779SMax Reitz 	sb->s_d_op = &fuse_dentry_operations;
15741866d779SMax Reitz 	sb->s_root = d_make_root(root);
15751866d779SMax Reitz 	if (!sb->s_root)
15761866d779SMax Reitz 		return -ENOMEM;
15771866d779SMax Reitz 
15782939dd30SKrister Johansen 	/*
15792939dd30SKrister Johansen 	 * Grab the parent's submount_lookup pointer and take a
15802939dd30SKrister Johansen 	 * reference on the shared nlookup from the parent.  This is to
15812939dd30SKrister Johansen 	 * prevent the last forget for this nodeid from getting
15822939dd30SKrister Johansen 	 * triggered until all users have finished with it.
15832939dd30SKrister Johansen 	 */
15842939dd30SKrister Johansen 	sl = parent_fi->submount_lookup;
15852939dd30SKrister Johansen 	WARN_ON(!sl);
15862939dd30SKrister Johansen 	if (sl) {
15872939dd30SKrister Johansen 		refcount_inc(&sl->count);
15882939dd30SKrister Johansen 		fi->submount_lookup = sl;
15892939dd30SKrister Johansen 	}
15902939dd30SKrister Johansen 
15911866d779SMax Reitz 	return 0;
15921866d779SMax Reitz }
15931866d779SMax Reitz 
1594266eb3f2SGreg Kurz /* Filesystem context private data holds the FUSE inode of the mount point */
fuse_get_tree_submount(struct fs_context * fsc)1595fe0a7bd8SGreg Kurz static int fuse_get_tree_submount(struct fs_context *fsc)
1596fe0a7bd8SGreg Kurz {
1597266eb3f2SGreg Kurz 	struct fuse_mount *fm;
1598266eb3f2SGreg Kurz 	struct fuse_inode *mp_fi = fsc->fs_private;
1599266eb3f2SGreg Kurz 	struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
1600266eb3f2SGreg Kurz 	struct super_block *sb;
1601266eb3f2SGreg Kurz 	int err;
1602266eb3f2SGreg Kurz 
1603266eb3f2SGreg Kurz 	fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1604266eb3f2SGreg Kurz 	if (!fm)
1605266eb3f2SGreg Kurz 		return -ENOMEM;
1606266eb3f2SGreg Kurz 
1607c191cd07SMiklos Szeredi 	fm->fc = fuse_conn_get(fc);
1608266eb3f2SGreg Kurz 	fsc->s_fs_info = fm;
1609266eb3f2SGreg Kurz 	sb = sget_fc(fsc, NULL, set_anon_super_fc);
1610c191cd07SMiklos Szeredi 	if (fsc->s_fs_info)
1611c191cd07SMiklos Szeredi 		fuse_mount_destroy(fm);
1612c191cd07SMiklos Szeredi 	if (IS_ERR(sb))
1613266eb3f2SGreg Kurz 		return PTR_ERR(sb);
1614266eb3f2SGreg Kurz 
1615266eb3f2SGreg Kurz 	/* Initialize superblock, making @mp_fi its root */
1616266eb3f2SGreg Kurz 	err = fuse_fill_super_submount(sb, mp_fi);
1617266eb3f2SGreg Kurz 	if (err) {
1618266eb3f2SGreg Kurz 		deactivate_locked_super(sb);
1619266eb3f2SGreg Kurz 		return err;
1620266eb3f2SGreg Kurz 	}
1621266eb3f2SGreg Kurz 
1622266eb3f2SGreg Kurz 	down_write(&fc->killsb);
1623266eb3f2SGreg Kurz 	list_add_tail(&fm->fc_entry, &fc->mounts);
1624266eb3f2SGreg Kurz 	up_write(&fc->killsb);
1625266eb3f2SGreg Kurz 
1626266eb3f2SGreg Kurz 	sb->s_flags |= SB_ACTIVE;
1627266eb3f2SGreg Kurz 	fsc->root = dget(sb->s_root);
1628266eb3f2SGreg Kurz 
1629fe0a7bd8SGreg Kurz 	return 0;
1630fe0a7bd8SGreg Kurz }
1631fe0a7bd8SGreg Kurz 
1632fe0a7bd8SGreg Kurz static const struct fs_context_operations fuse_context_submount_ops = {
1633fe0a7bd8SGreg Kurz 	.get_tree	= fuse_get_tree_submount,
1634fe0a7bd8SGreg Kurz };
1635fe0a7bd8SGreg Kurz 
fuse_init_fs_context_submount(struct fs_context * fsc)1636fe0a7bd8SGreg Kurz int fuse_init_fs_context_submount(struct fs_context *fsc)
1637fe0a7bd8SGreg Kurz {
1638fe0a7bd8SGreg Kurz 	fsc->ops = &fuse_context_submount_ops;
1639fe0a7bd8SGreg Kurz 	return 0;
1640fe0a7bd8SGreg Kurz }
1641fe0a7bd8SGreg Kurz EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
1642fe0a7bd8SGreg Kurz 
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)16430cc2656cSStefan Hajnoczi int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1644d8a5ba45SMiklos Szeredi {
16457fd3abfaSVivek Goyal 	struct fuse_dev *fud = NULL;
1646fcee216bSMax Reitz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1647fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1648d8a5ba45SMiklos Szeredi 	struct inode *root;
1649f543f253SMiklos Szeredi 	struct dentry *root_dentry;
1650d8a5ba45SMiklos Szeredi 	int err;
1651d8a5ba45SMiklos Szeredi 
1652c2b8f006SMiklos Szeredi 	err = -EINVAL;
16531751e8a6SLinus Torvalds 	if (sb->s_flags & SB_MANDLOCK)
1654c2b8f006SMiklos Szeredi 		goto err;
165571421259SMiklos Szeredi 
1656660585b5SMiklos Szeredi 	rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
16571866d779SMax Reitz 	fuse_sb_defaults(sb);
16589e1f1de0SAl Viro 
16590cc2656cSStefan Hajnoczi 	if (ctx->is_bdev) {
1660875d95ecSMiklos Szeredi #ifdef CONFIG_BLOCK
1661c2b8f006SMiklos Szeredi 		err = -EINVAL;
1662c30da2e9SDavid Howells 		if (!sb_set_blocksize(sb, ctx->blksize))
1663c2b8f006SMiklos Szeredi 			goto err;
1664875d95ecSMiklos Szeredi #endif
1665d8091614SMiklos Szeredi 	} else {
166609cbfeafSKirill A. Shutemov 		sb->s_blocksize = PAGE_SIZE;
166709cbfeafSKirill A. Shutemov 		sb->s_blocksize_bits = PAGE_SHIFT;
1668d8091614SMiklos Szeredi 	}
1669c30da2e9SDavid Howells 
1670c30da2e9SDavid Howells 	sb->s_subtype = ctx->subtype;
1671c30da2e9SDavid Howells 	ctx->subtype = NULL;
16721dd53957SVivek Goyal 	if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1673780b1b95SJeffle Xu 		err = fuse_dax_conn_alloc(fc, ctx->dax_mode, ctx->dax_dev);
16741dd53957SVivek Goyal 		if (err)
16751dd53957SVivek Goyal 			goto err;
16761dd53957SVivek Goyal 	}
1677e45b2546SEric W. Biederman 
16787fd3abfaSVivek Goyal 	if (ctx->fudptr) {
16797fd3abfaSVivek Goyal 		err = -ENOMEM;
16800cd1eb9aSVivek Goyal 		fud = fuse_dev_alloc_install(fc);
1681cc080e9eSMiklos Szeredi 		if (!fud)
16821dd53957SVivek Goyal 			goto err_free_dax;
16837fd3abfaSVivek Goyal 	}
1684cc080e9eSMiklos Szeredi 
1685a325f9b9STejun Heo 	fc->dev = sb->s_dev;
1686fcee216bSMax Reitz 	fm->sb = sb;
1687a325f9b9STejun Heo 	err = fuse_bdi_init(fc, sb);
1688a325f9b9STejun Heo 	if (err)
1689cc080e9eSMiklos Szeredi 		goto err_dev_free;
16900d179aa5STejun Heo 
1691e0a43ddcSMiklos Szeredi 	/* Handle umasking inside the fuse code */
16921751e8a6SLinus Torvalds 	if (sb->s_flags & SB_POSIXACL)
1693e0a43ddcSMiklos Szeredi 		fc->dont_mask = 1;
16941751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
1695e0a43ddcSMiklos Szeredi 
1696c30da2e9SDavid Howells 	fc->default_permissions = ctx->default_permissions;
1697c30da2e9SDavid Howells 	fc->allow_other = ctx->allow_other;
1698c30da2e9SDavid Howells 	fc->user_id = ctx->user_id;
1699c30da2e9SDavid Howells 	fc->group_id = ctx->group_id;
1700f4fd4ae3SVivek Goyal 	fc->legacy_opts_show = ctx->legacy_opts_show;
17011866d779SMax Reitz 	fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1702783863d6SMiklos Szeredi 	fc->destroy = ctx->destroy;
170315c8e72eSVivek Goyal 	fc->no_control = ctx->no_control;
170415c8e72eSVivek Goyal 	fc->no_force_umount = ctx->no_force_umount;
1705f543f253SMiklos Szeredi 
1706d8a5ba45SMiklos Szeredi 	err = -ENOMEM;
1707c30da2e9SDavid Howells 	root = fuse_get_root_inode(sb, ctx->rootmode);
17080ce267ffSMiklos Szeredi 	sb->s_d_op = &fuse_root_dentry_operations;
170948fde701SAl Viro 	root_dentry = d_make_root(root);
171048fde701SAl Viro 	if (!root_dentry)
1711cc080e9eSMiklos Szeredi 		goto err_dev_free;
17120ce267ffSMiklos Szeredi 	/* Root dentry doesn't have .d_revalidate */
1713c35eebe9SAl Viro 	sb->s_d_op = &fuse_dentry_operations;
1714f543f253SMiklos Szeredi 
1715bafa9654SMiklos Szeredi 	mutex_lock(&fuse_mutex);
17168aa09a50SMiklos Szeredi 	err = -EINVAL;
17177fd3abfaSVivek Goyal 	if (ctx->fudptr && *ctx->fudptr)
1718bafa9654SMiklos Szeredi 		goto err_unlock;
17198aa09a50SMiklos Szeredi 
1720bafa9654SMiklos Szeredi 	err = fuse_ctl_add_conn(fc);
1721bafa9654SMiklos Szeredi 	if (err)
1722bafa9654SMiklos Szeredi 		goto err_unlock;
1723bafa9654SMiklos Szeredi 
1724bafa9654SMiklos Szeredi 	list_add_tail(&fc->entry, &fuse_conn_list);
1725f543f253SMiklos Szeredi 	sb->s_root = root_dentry;
17267fd3abfaSVivek Goyal 	if (ctx->fudptr)
17270cc2656cSStefan Hajnoczi 		*ctx->fudptr = fud;
1728bafa9654SMiklos Szeredi 	mutex_unlock(&fuse_mutex);
1729d8a5ba45SMiklos Szeredi 	return 0;
1730d8a5ba45SMiklos Szeredi 
1731bafa9654SMiklos Szeredi  err_unlock:
1732bafa9654SMiklos Szeredi 	mutex_unlock(&fuse_mutex);
1733f543f253SMiklos Szeredi 	dput(root_dentry);
1734cc080e9eSMiklos Szeredi  err_dev_free:
17357fd3abfaSVivek Goyal 	if (fud)
1736cc080e9eSMiklos Szeredi 		fuse_dev_free(fud);
17371dd53957SVivek Goyal  err_free_dax:
17381dd53957SVivek Goyal 	if (IS_ENABLED(CONFIG_FUSE_DAX))
17391dd53957SVivek Goyal 		fuse_dax_conn_free(fc);
17400cc2656cSStefan Hajnoczi  err:
17410cc2656cSStefan Hajnoczi 	return err;
17420cc2656cSStefan Hajnoczi }
17430cc2656cSStefan Hajnoczi EXPORT_SYMBOL_GPL(fuse_fill_super_common);
17440cc2656cSStefan Hajnoczi 
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)17450cc2656cSStefan Hajnoczi static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
17460cc2656cSStefan Hajnoczi {
17470cc2656cSStefan Hajnoczi 	struct fuse_fs_context *ctx = fsc->fs_private;
17480cc2656cSStefan Hajnoczi 	int err;
17490cc2656cSStefan Hajnoczi 
175062dd1fc8SMiklos Szeredi 	if (!ctx->file || !ctx->rootmode_present ||
1751badc7414SMiklos Szeredi 	    !ctx->user_id_present || !ctx->group_id_present)
1752badc7414SMiklos Szeredi 		return -EINVAL;
17530cc2656cSStefan Hajnoczi 
17540cc2656cSStefan Hajnoczi 	/*
17550cc2656cSStefan Hajnoczi 	 * Require mount to happen from the same user namespace which
17560cc2656cSStefan Hajnoczi 	 * opened /dev/fuse to prevent potential attacks.
17570cc2656cSStefan Hajnoczi 	 */
175862dd1fc8SMiklos Szeredi 	if ((ctx->file->f_op != &fuse_dev_operations) ||
175962dd1fc8SMiklos Szeredi 	    (ctx->file->f_cred->user_ns != sb->s_user_ns))
1760964d32e5SMiklos Szeredi 		return -EINVAL;
176162dd1fc8SMiklos Szeredi 	ctx->fudptr = &ctx->file->private_data;
17620cc2656cSStefan Hajnoczi 
17630cc2656cSStefan Hajnoczi 	err = fuse_fill_super_common(sb, ctx);
17640cc2656cSStefan Hajnoczi 	if (err)
1765964d32e5SMiklos Szeredi 		return err;
176662dd1fc8SMiklos Szeredi 	/* file->private_data shall be visible on all CPUs after this */
176762dd1fc8SMiklos Szeredi 	smp_mb();
1768fcee216bSMax Reitz 	fuse_send_init(get_fuse_mount_super(sb));
17690cc2656cSStefan Hajnoczi 	return 0;
1770d8a5ba45SMiklos Szeredi }
1771d8a5ba45SMiklos Szeredi 
17725d5b74aaSMiklos Szeredi /*
17735d5b74aaSMiklos Szeredi  * This is the path where user supplied an already initialized fuse dev.  In
17745d5b74aaSMiklos Szeredi  * this case never create a new super if the old one is gone.
17755d5b74aaSMiklos Szeredi  */
fuse_set_no_super(struct super_block * sb,struct fs_context * fsc)17765d5b74aaSMiklos Szeredi static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
1777d8a5ba45SMiklos Szeredi {
17785d5b74aaSMiklos Szeredi 	return -ENOTCONN;
17795d5b74aaSMiklos Szeredi }
1780c30da2e9SDavid Howells 
fuse_test_super(struct super_block * sb,struct fs_context * fsc)17815d5b74aaSMiklos Szeredi static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
17825d5b74aaSMiklos Szeredi {
17835d5b74aaSMiklos Szeredi 
17845d5b74aaSMiklos Szeredi 	return fsc->sget_key == get_fuse_conn_super(sb);
17855d5b74aaSMiklos Szeredi }
17865d5b74aaSMiklos Szeredi 
fuse_get_tree(struct fs_context * fsc)178784c21507SMiklos Szeredi static int fuse_get_tree(struct fs_context *fsc)
1788d8a5ba45SMiklos Szeredi {
178984c21507SMiklos Szeredi 	struct fuse_fs_context *ctx = fsc->fs_private;
17905d5b74aaSMiklos Szeredi 	struct fuse_dev *fud;
179180019f11SMiklos Szeredi 	struct fuse_conn *fc;
179280019f11SMiklos Szeredi 	struct fuse_mount *fm;
17935d5b74aaSMiklos Szeredi 	struct super_block *sb;
179462dd1fc8SMiklos Szeredi 	int err;
179562dd1fc8SMiklos Szeredi 
179680019f11SMiklos Szeredi 	fc = kmalloc(sizeof(*fc), GFP_KERNEL);
179780019f11SMiklos Szeredi 	if (!fc)
179880019f11SMiklos Szeredi 		return -ENOMEM;
179980019f11SMiklos Szeredi 
180080019f11SMiklos Szeredi 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
180180019f11SMiklos Szeredi 	if (!fm) {
180280019f11SMiklos Szeredi 		kfree(fc);
180380019f11SMiklos Szeredi 		return -ENOMEM;
180480019f11SMiklos Szeredi 	}
180580019f11SMiklos Szeredi 
180680019f11SMiklos Szeredi 	fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
180780019f11SMiklos Szeredi 	fc->release = fuse_free_conn;
180880019f11SMiklos Szeredi 
180980019f11SMiklos Szeredi 	fsc->s_fs_info = fm;
181080019f11SMiklos Szeredi 
181162dd1fc8SMiklos Szeredi 	if (ctx->fd_present)
181262dd1fc8SMiklos Szeredi 		ctx->file = fget(ctx->fd);
1813c30da2e9SDavid Howells 
1814badc7414SMiklos Szeredi 	if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
181562dd1fc8SMiklos Szeredi 		err = get_tree_bdev(fsc, fuse_fill_super);
181680019f11SMiklos Szeredi 		goto out;
1817badc7414SMiklos Szeredi 	}
18185d5b74aaSMiklos Szeredi 	/*
18195d5b74aaSMiklos Szeredi 	 * While block dev mount can be initialized with a dummy device fd
18205d5b74aaSMiklos Szeredi 	 * (found by device name), normal fuse mounts can't
18215d5b74aaSMiklos Szeredi 	 */
182280019f11SMiklos Szeredi 	err = -EINVAL;
18235d5b74aaSMiklos Szeredi 	if (!ctx->file)
182480019f11SMiklos Szeredi 		goto out;
1825c30da2e9SDavid Howells 
18265d5b74aaSMiklos Szeredi 	/*
18275d5b74aaSMiklos Szeredi 	 * Allow creating a fuse mount with an already initialized fuse
18285d5b74aaSMiklos Szeredi 	 * connection
18295d5b74aaSMiklos Szeredi 	 */
18305d5b74aaSMiklos Szeredi 	fud = READ_ONCE(ctx->file->private_data);
18315d5b74aaSMiklos Szeredi 	if (ctx->file->f_op == &fuse_dev_operations && fud) {
18325d5b74aaSMiklos Szeredi 		fsc->sget_key = fud->fc;
18335d5b74aaSMiklos Szeredi 		sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
18345d5b74aaSMiklos Szeredi 		err = PTR_ERR_OR_ZERO(sb);
18355d5b74aaSMiklos Szeredi 		if (!IS_ERR(sb))
18365d5b74aaSMiklos Szeredi 			fsc->root = dget(sb->s_root);
18375d5b74aaSMiklos Szeredi 	} else {
183862dd1fc8SMiklos Szeredi 		err = get_tree_nodev(fsc, fuse_fill_super);
18395d5b74aaSMiklos Szeredi 	}
184080019f11SMiklos Szeredi out:
184180019f11SMiklos Szeredi 	if (fsc->s_fs_info)
184280019f11SMiklos Szeredi 		fuse_mount_destroy(fm);
184362dd1fc8SMiklos Szeredi 	if (ctx->file)
184462dd1fc8SMiklos Szeredi 		fput(ctx->file);
184562dd1fc8SMiklos Szeredi 	return err;
1846c30da2e9SDavid Howells }
1847c30da2e9SDavid Howells 
1848c30da2e9SDavid Howells static const struct fs_context_operations fuse_context_ops = {
184984c21507SMiklos Szeredi 	.free		= fuse_free_fsc,
1850c30da2e9SDavid Howells 	.parse_param	= fuse_parse_param,
18510189a2d3SMiklos Szeredi 	.reconfigure	= fuse_reconfigure,
1852c30da2e9SDavid Howells 	.get_tree	= fuse_get_tree,
1853c30da2e9SDavid Howells };
1854c30da2e9SDavid Howells 
1855c30da2e9SDavid Howells /*
1856c30da2e9SDavid Howells  * Set up the filesystem mount context.
1857c30da2e9SDavid Howells  */
fuse_init_fs_context(struct fs_context * fsc)185884c21507SMiklos Szeredi static int fuse_init_fs_context(struct fs_context *fsc)
1859c30da2e9SDavid Howells {
1860c30da2e9SDavid Howells 	struct fuse_fs_context *ctx;
1861c30da2e9SDavid Howells 
1862c30da2e9SDavid Howells 	ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1863c30da2e9SDavid Howells 	if (!ctx)
1864c30da2e9SDavid Howells 		return -ENOMEM;
1865c30da2e9SDavid Howells 
1866c30da2e9SDavid Howells 	ctx->max_read = ~0;
1867c30da2e9SDavid Howells 	ctx->blksize = FUSE_DEFAULT_BLKSIZE;
1868f4fd4ae3SVivek Goyal 	ctx->legacy_opts_show = true;
1869c30da2e9SDavid Howells 
1870c30da2e9SDavid Howells #ifdef CONFIG_BLOCK
187184c21507SMiklos Szeredi 	if (fsc->fs_type == &fuseblk_fs_type) {
1872c30da2e9SDavid Howells 		ctx->is_bdev = true;
1873783863d6SMiklos Szeredi 		ctx->destroy = true;
1874783863d6SMiklos Szeredi 	}
1875c30da2e9SDavid Howells #endif
1876c30da2e9SDavid Howells 
187784c21507SMiklos Szeredi 	fsc->fs_private = ctx;
187884c21507SMiklos Szeredi 	fsc->ops = &fuse_context_ops;
1879c30da2e9SDavid Howells 	return 0;
1880d8a5ba45SMiklos Szeredi }
1881d8a5ba45SMiklos Szeredi 
fuse_mount_remove(struct fuse_mount * fm)1882fcee216bSMax Reitz bool fuse_mount_remove(struct fuse_mount *fm)
18833b463ae0SJohn Muir {
1884fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1885fcee216bSMax Reitz 	bool last = false;
18863b463ae0SJohn Muir 
1887fcee216bSMax Reitz 	down_write(&fc->killsb);
1888fcee216bSMax Reitz 	list_del_init(&fm->fc_entry);
1889fcee216bSMax Reitz 	if (list_empty(&fc->mounts))
1890fcee216bSMax Reitz 		last = true;
1891fcee216bSMax Reitz 	up_write(&fc->killsb);
1892fcee216bSMax Reitz 
1893fcee216bSMax Reitz 	return last;
1894fcee216bSMax Reitz }
1895fcee216bSMax Reitz EXPORT_SYMBOL_GPL(fuse_mount_remove);
1896fcee216bSMax Reitz 
fuse_conn_destroy(struct fuse_mount * fm)1897fcee216bSMax Reitz void fuse_conn_destroy(struct fuse_mount *fm)
1898fcee216bSMax Reitz {
1899fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1900fcee216bSMax Reitz 
19011ccd1ea2SMiklos Szeredi 	if (fc->destroy)
1902fcee216bSMax Reitz 		fuse_send_destroy(fm);
1903e8f3bd77SMiklos Szeredi 
1904eb98e3bdSMiklos Szeredi 	fuse_abort_conn(fc);
1905e8f3bd77SMiklos Szeredi 	fuse_wait_aborted(fc);
1906e8f3bd77SMiklos Szeredi 
1907413daa1aSMiklos Szeredi 	if (!list_empty(&fc->entry)) {
1908413daa1aSMiklos Szeredi 		mutex_lock(&fuse_mutex);
1909413daa1aSMiklos Szeredi 		list_del(&fc->entry);
1910413daa1aSMiklos Szeredi 		fuse_ctl_remove_conn(fc);
1911413daa1aSMiklos Szeredi 		mutex_unlock(&fuse_mutex);
19123b463ae0SJohn Muir 	}
1913e8f3bd77SMiklos Szeredi }
1914fcee216bSMax Reitz EXPORT_SYMBOL_GPL(fuse_conn_destroy);
19153b463ae0SJohn Muir 
fuse_sb_destroy(struct super_block * sb)19166a68d1e1SMiklos Szeredi static void fuse_sb_destroy(struct super_block *sb)
1917e8f3bd77SMiklos Szeredi {
1918fcee216bSMax Reitz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1919fcee216bSMax Reitz 	bool last;
1920fcee216bSMax Reitz 
1921d534d31dSMiklos Szeredi 	if (sb->s_root) {
1922fcee216bSMax Reitz 		last = fuse_mount_remove(fm);
1923fcee216bSMax Reitz 		if (last)
1924fcee216bSMax Reitz 			fuse_conn_destroy(fm);
1925fcee216bSMax Reitz 	}
19266a68d1e1SMiklos Szeredi }
19276a68d1e1SMiklos Szeredi 
fuse_mount_destroy(struct fuse_mount * fm)1928a27c061aSMiklos Szeredi void fuse_mount_destroy(struct fuse_mount *fm)
1929a27c061aSMiklos Szeredi {
1930a27c061aSMiklos Szeredi 	fuse_conn_put(fm->fc);
1931*535e9bd0SAl Viro 	kfree_rcu(fm, rcu);
1932a27c061aSMiklos Szeredi }
1933a27c061aSMiklos Szeredi EXPORT_SYMBOL(fuse_mount_destroy);
1934a27c061aSMiklos Szeredi 
fuse_kill_sb_anon(struct super_block * sb)19356a68d1e1SMiklos Szeredi static void fuse_kill_sb_anon(struct super_block *sb)
19366a68d1e1SMiklos Szeredi {
19376a68d1e1SMiklos Szeredi 	fuse_sb_destroy(sb);
19383b463ae0SJohn Muir 	kill_anon_super(sb);
1939a27c061aSMiklos Szeredi 	fuse_mount_destroy(get_fuse_mount_super(sb));
19403b463ae0SJohn Muir }
19413b463ae0SJohn Muir 
1942875d95ecSMiklos Szeredi static struct file_system_type fuse_fs_type = {
1943875d95ecSMiklos Szeredi 	.owner		= THIS_MODULE,
1944875d95ecSMiklos Szeredi 	.name		= "fuse",
19454ad769f3SEric W. Biederman 	.fs_flags	= FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
1946c30da2e9SDavid Howells 	.init_fs_context = fuse_init_fs_context,
1947d7167b14SAl Viro 	.parameters	= fuse_fs_parameters,
19483b463ae0SJohn Muir 	.kill_sb	= fuse_kill_sb_anon,
1949875d95ecSMiklos Szeredi };
19507f78e035SEric W. Biederman MODULE_ALIAS_FS("fuse");
1951875d95ecSMiklos Szeredi 
1952875d95ecSMiklos Szeredi #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)19533b463ae0SJohn Muir static void fuse_kill_sb_blk(struct super_block *sb)
19543b463ae0SJohn Muir {
19556a68d1e1SMiklos Szeredi 	fuse_sb_destroy(sb);
19563b463ae0SJohn Muir 	kill_block_super(sb);
1957a27c061aSMiklos Szeredi 	fuse_mount_destroy(get_fuse_mount_super(sb));
19583b463ae0SJohn Muir }
19593b463ae0SJohn Muir 
1960d6392f87SMiklos Szeredi static struct file_system_type fuseblk_fs_type = {
1961d6392f87SMiklos Szeredi 	.owner		= THIS_MODULE,
1962d6392f87SMiklos Szeredi 	.name		= "fuseblk",
1963c30da2e9SDavid Howells 	.init_fs_context = fuse_init_fs_context,
1964d7167b14SAl Viro 	.parameters	= fuse_fs_parameters,
19653b463ae0SJohn Muir 	.kill_sb	= fuse_kill_sb_blk,
1966edad01e2SAlexey Dobriyan 	.fs_flags	= FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
1967d6392f87SMiklos Szeredi };
19687f78e035SEric W. Biederman MODULE_ALIAS_FS("fuseblk");
1969d6392f87SMiklos Szeredi 
register_fuseblk(void)1970875d95ecSMiklos Szeredi static inline int register_fuseblk(void)
1971875d95ecSMiklos Szeredi {
1972875d95ecSMiklos Szeredi 	return register_filesystem(&fuseblk_fs_type);
1973875d95ecSMiklos Szeredi }
1974875d95ecSMiklos Szeredi 
unregister_fuseblk(void)1975875d95ecSMiklos Szeredi static inline void unregister_fuseblk(void)
1976875d95ecSMiklos Szeredi {
1977875d95ecSMiklos Szeredi 	unregister_filesystem(&fuseblk_fs_type);
1978875d95ecSMiklos Szeredi }
1979875d95ecSMiklos Szeredi #else
register_fuseblk(void)1980875d95ecSMiklos Szeredi static inline int register_fuseblk(void)
1981875d95ecSMiklos Szeredi {
1982875d95ecSMiklos Szeredi 	return 0;
1983875d95ecSMiklos Szeredi }
1984875d95ecSMiklos Szeredi 
unregister_fuseblk(void)1985875d95ecSMiklos Szeredi static inline void unregister_fuseblk(void)
1986875d95ecSMiklos Szeredi {
1987875d95ecSMiklos Szeredi }
1988875d95ecSMiklos Szeredi #endif
1989875d95ecSMiklos Szeredi 
fuse_inode_init_once(void * foo)199051cc5068SAlexey Dobriyan static void fuse_inode_init_once(void *foo)
1991d8a5ba45SMiklos Szeredi {
1992d8a5ba45SMiklos Szeredi 	struct inode *inode = foo;
1993d8a5ba45SMiklos Szeredi 
1994d8a5ba45SMiklos Szeredi 	inode_init_once(inode);
1995d8a5ba45SMiklos Szeredi }
1996d8a5ba45SMiklos Szeredi 
fuse_fs_init(void)1997d8a5ba45SMiklos Szeredi static int __init fuse_fs_init(void)
1998d8a5ba45SMiklos Szeredi {
1999d8a5ba45SMiklos Szeredi 	int err;
2000d8a5ba45SMiklos Szeredi 
2001d8a5ba45SMiklos Szeredi 	fuse_inode_cachep = kmem_cache_create("fuse_inode",
20025d097056SVladimir Davydov 			sizeof(struct fuse_inode), 0,
2003df206988SJohannes Weiner 			SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
200420c2df83SPaul Mundt 			fuse_inode_init_once);
2005d8a5ba45SMiklos Szeredi 	err = -ENOMEM;
2006d6392f87SMiklos Szeredi 	if (!fuse_inode_cachep)
2007988f0325SAl Viro 		goto out;
2008988f0325SAl Viro 
2009988f0325SAl Viro 	err = register_fuseblk();
2010988f0325SAl Viro 	if (err)
2011988f0325SAl Viro 		goto out2;
2012988f0325SAl Viro 
2013988f0325SAl Viro 	err = register_filesystem(&fuse_fs_type);
2014988f0325SAl Viro 	if (err)
2015988f0325SAl Viro 		goto out3;
2016d8a5ba45SMiklos Szeredi 
2017d6392f87SMiklos Szeredi 	return 0;
2018d6392f87SMiklos Szeredi 
2019988f0325SAl Viro  out3:
2020875d95ecSMiklos Szeredi 	unregister_fuseblk();
2021988f0325SAl Viro  out2:
2022988f0325SAl Viro 	kmem_cache_destroy(fuse_inode_cachep);
2023d6392f87SMiklos Szeredi  out:
2024d8a5ba45SMiklos Szeredi 	return err;
2025d8a5ba45SMiklos Szeredi }
2026d8a5ba45SMiklos Szeredi 
fuse_fs_cleanup(void)2027d8a5ba45SMiklos Szeredi static void fuse_fs_cleanup(void)
2028d8a5ba45SMiklos Szeredi {
2029d8a5ba45SMiklos Szeredi 	unregister_filesystem(&fuse_fs_type);
2030875d95ecSMiklos Szeredi 	unregister_fuseblk();
20318c0a8537SKirill A. Shutemov 
20328c0a8537SKirill A. Shutemov 	/*
20338c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
20348c0a8537SKirill A. Shutemov 	 * destroy cache.
20358c0a8537SKirill A. Shutemov 	 */
20368c0a8537SKirill A. Shutemov 	rcu_barrier();
2037d8a5ba45SMiklos Szeredi 	kmem_cache_destroy(fuse_inode_cachep);
2038d8a5ba45SMiklos Szeredi }
2039d8a5ba45SMiklos Szeredi 
20405c89e17eSGreg Kroah-Hartman static struct kobject *fuse_kobj;
20415c89e17eSGreg Kroah-Hartman 
fuse_sysfs_init(void)2042f543f253SMiklos Szeredi static int fuse_sysfs_init(void)
2043f543f253SMiklos Szeredi {
2044f543f253SMiklos Szeredi 	int err;
2045f543f253SMiklos Szeredi 
204600d26666SGreg Kroah-Hartman 	fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
20475c89e17eSGreg Kroah-Hartman 	if (!fuse_kobj) {
20485c89e17eSGreg Kroah-Hartman 		err = -ENOMEM;
2049f543f253SMiklos Szeredi 		goto out_err;
20505c89e17eSGreg Kroah-Hartman 	}
2051f543f253SMiklos Szeredi 
2052f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fuse_kobj, "connections");
2053f9bb4882SEric W. Biederman 	if (err)
2054f543f253SMiklos Szeredi 		goto out_fuse_unregister;
2055f543f253SMiklos Szeredi 
2056f543f253SMiklos Szeredi 	return 0;
2057f543f253SMiklos Szeredi 
2058f543f253SMiklos Szeredi  out_fuse_unregister:
2059197b12d6SGreg Kroah-Hartman 	kobject_put(fuse_kobj);
2060f543f253SMiklos Szeredi  out_err:
2061f543f253SMiklos Szeredi 	return err;
2062f543f253SMiklos Szeredi }
2063f543f253SMiklos Szeredi 
fuse_sysfs_cleanup(void)2064f543f253SMiklos Szeredi static void fuse_sysfs_cleanup(void)
2065f543f253SMiklos Szeredi {
2066f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fuse_kobj, "connections");
2067197b12d6SGreg Kroah-Hartman 	kobject_put(fuse_kobj);
2068f543f253SMiklos Szeredi }
2069f543f253SMiklos Szeredi 
fuse_init(void)2070d8a5ba45SMiklos Szeredi static int __init fuse_init(void)
2071d8a5ba45SMiklos Szeredi {
2072d8a5ba45SMiklos Szeredi 	int res;
2073d8a5ba45SMiklos Szeredi 
2074f2294482SKirill Smelkov 	pr_info("init (API version %i.%i)\n",
2075d8a5ba45SMiklos Szeredi 		FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2076d8a5ba45SMiklos Szeredi 
2077bafa9654SMiklos Szeredi 	INIT_LIST_HEAD(&fuse_conn_list);
2078d8a5ba45SMiklos Szeredi 	res = fuse_fs_init();
2079d8a5ba45SMiklos Szeredi 	if (res)
2080d8a5ba45SMiklos Szeredi 		goto err;
2081d8a5ba45SMiklos Szeredi 
2082334f485dSMiklos Szeredi 	res = fuse_dev_init();
2083334f485dSMiklos Szeredi 	if (res)
2084334f485dSMiklos Szeredi 		goto err_fs_cleanup;
2085334f485dSMiklos Szeredi 
2086f543f253SMiklos Szeredi 	res = fuse_sysfs_init();
2087f543f253SMiklos Szeredi 	if (res)
2088f543f253SMiklos Szeredi 		goto err_dev_cleanup;
2089f543f253SMiklos Szeredi 
2090bafa9654SMiklos Szeredi 	res = fuse_ctl_init();
2091bafa9654SMiklos Szeredi 	if (res)
2092bafa9654SMiklos Szeredi 		goto err_sysfs_cleanup;
2093bafa9654SMiklos Szeredi 
2094487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_bgreq);
2095487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_congthresh);
2096487ea5afSCsaba Henk 
2097d8a5ba45SMiklos Szeredi 	return 0;
2098d8a5ba45SMiklos Szeredi 
2099bafa9654SMiklos Szeredi  err_sysfs_cleanup:
2100bafa9654SMiklos Szeredi 	fuse_sysfs_cleanup();
2101f543f253SMiklos Szeredi  err_dev_cleanup:
2102f543f253SMiklos Szeredi 	fuse_dev_cleanup();
2103334f485dSMiklos Szeredi  err_fs_cleanup:
2104334f485dSMiklos Szeredi 	fuse_fs_cleanup();
2105d8a5ba45SMiklos Szeredi  err:
2106d8a5ba45SMiklos Szeredi 	return res;
2107d8a5ba45SMiklos Szeredi }
2108d8a5ba45SMiklos Szeredi 
fuse_exit(void)2109d8a5ba45SMiklos Szeredi static void __exit fuse_exit(void)
2110d8a5ba45SMiklos Szeredi {
2111f2294482SKirill Smelkov 	pr_debug("exit\n");
2112d8a5ba45SMiklos Szeredi 
2113bafa9654SMiklos Szeredi 	fuse_ctl_cleanup();
2114f543f253SMiklos Szeredi 	fuse_sysfs_cleanup();
2115d8a5ba45SMiklos Szeredi 	fuse_fs_cleanup();
2116334f485dSMiklos Szeredi 	fuse_dev_cleanup();
2117d8a5ba45SMiklos Szeredi }
2118d8a5ba45SMiklos Szeredi 
2119d8a5ba45SMiklos Szeredi module_init(fuse_init);
2120d8a5ba45SMiklos Szeredi module_exit(fuse_exit);
2121