xref: /openbmc/linux/fs/fuse/inode.c (revision 8b86779a)
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;
754233a01faSMiklos Szeredi 
75584c21507SMiklos Szeredi 	if (fsc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
756e8b20a47SMiklos Szeredi 		/*
757e8b20a47SMiklos Szeredi 		 * Ignore options coming from mount(MS_REMOUNT) for backward
758e8b20a47SMiklos Szeredi 		 * compatibility.
759e8b20a47SMiklos Szeredi 		 */
76084c21507SMiklos Szeredi 		if (fsc->oldapi)
761e8b20a47SMiklos Szeredi 			return 0;
762e8b20a47SMiklos Szeredi 
76384c21507SMiklos Szeredi 		return invalfc(fsc, "No changes allowed in reconfigure");
764b330966fSMiklos Szeredi 	}
765b330966fSMiklos Szeredi 
76684c21507SMiklos Szeredi 	opt = fs_parse(fsc, fuse_fs_parameters, param, &result);
767c30da2e9SDavid Howells 	if (opt < 0)
768c30da2e9SDavid Howells 		return opt;
769d8a5ba45SMiklos Szeredi 
770c30da2e9SDavid Howells 	switch (opt) {
771c30da2e9SDavid Howells 	case OPT_SOURCE:
77284c21507SMiklos Szeredi 		if (fsc->source)
77384c21507SMiklos Szeredi 			return invalfc(fsc, "Multiple sources specified");
77484c21507SMiklos Szeredi 		fsc->source = param->string;
775c30da2e9SDavid Howells 		param->string = NULL;
776c30da2e9SDavid Howells 		break;
777d8a5ba45SMiklos Szeredi 
778c30da2e9SDavid Howells 	case OPT_SUBTYPE:
779c30da2e9SDavid Howells 		if (ctx->subtype)
78084c21507SMiklos Szeredi 			return invalfc(fsc, "Multiple subtypes specified");
781c30da2e9SDavid Howells 		ctx->subtype = param->string;
782c30da2e9SDavid Howells 		param->string = NULL;
783d8a5ba45SMiklos Szeredi 		return 0;
784c30da2e9SDavid Howells 
785c30da2e9SDavid Howells 	case OPT_FD:
786c30da2e9SDavid Howells 		ctx->fd = result.uint_32;
787cabdb4faSzhengbin 		ctx->fd_present = true;
788d8a5ba45SMiklos Szeredi 		break;
789d8a5ba45SMiklos Szeredi 
790d8a5ba45SMiklos Szeredi 	case OPT_ROOTMODE:
791c30da2e9SDavid Howells 		if (!fuse_valid_type(result.uint_32))
79284c21507SMiklos Szeredi 			return invalfc(fsc, "Invalid rootmode");
793c30da2e9SDavid Howells 		ctx->rootmode = result.uint_32;
794cabdb4faSzhengbin 		ctx->rootmode_present = true;
795d8a5ba45SMiklos Szeredi 		break;
796d8a5ba45SMiklos Szeredi 
797d8a5ba45SMiklos Szeredi 	case OPT_USER_ID:
79884c21507SMiklos Szeredi 		ctx->user_id = make_kuid(fsc->user_ns, result.uint_32);
799c30da2e9SDavid Howells 		if (!uid_valid(ctx->user_id))
80084c21507SMiklos Szeredi 			return invalfc(fsc, "Invalid user_id");
801cabdb4faSzhengbin 		ctx->user_id_present = true;
802d8a5ba45SMiklos Szeredi 		break;
803d8a5ba45SMiklos Szeredi 
80487729a55SMiklos Szeredi 	case OPT_GROUP_ID:
80584c21507SMiklos Szeredi 		ctx->group_id = make_kgid(fsc->user_ns, result.uint_32);
806c30da2e9SDavid Howells 		if (!gid_valid(ctx->group_id))
80784c21507SMiklos Szeredi 			return invalfc(fsc, "Invalid group_id");
808cabdb4faSzhengbin 		ctx->group_id_present = true;
80987729a55SMiklos Szeredi 		break;
81087729a55SMiklos Szeredi 
8111e9a4ed9SMiklos Szeredi 	case OPT_DEFAULT_PERMISSIONS:
812cabdb4faSzhengbin 		ctx->default_permissions = true;
8131e9a4ed9SMiklos Szeredi 		break;
8141e9a4ed9SMiklos Szeredi 
8151e9a4ed9SMiklos Szeredi 	case OPT_ALLOW_OTHER:
816cabdb4faSzhengbin 		ctx->allow_other = true;
8171e9a4ed9SMiklos Szeredi 		break;
8181e9a4ed9SMiklos Szeredi 
819db50b96cSMiklos Szeredi 	case OPT_MAX_READ:
820c30da2e9SDavid Howells 		ctx->max_read = result.uint_32;
821db50b96cSMiklos Szeredi 		break;
822db50b96cSMiklos Szeredi 
823d8091614SMiklos Szeredi 	case OPT_BLKSIZE:
824c30da2e9SDavid Howells 		if (!ctx->is_bdev)
82584c21507SMiklos Szeredi 			return invalfc(fsc, "blksize only supported for fuseblk");
826c30da2e9SDavid Howells 		ctx->blksize = result.uint_32;
827d8091614SMiklos Szeredi 		break;
828d8091614SMiklos Szeredi 
829d8a5ba45SMiklos Szeredi 	default:
830c30da2e9SDavid Howells 		return -EINVAL;
831d8a5ba45SMiklos Szeredi 	}
8325a533682SMiklos Szeredi 
833d8a5ba45SMiklos Szeredi 	return 0;
834c30da2e9SDavid Howells }
835d8a5ba45SMiklos Szeredi 
fuse_free_fsc(struct fs_context * fsc)83684c21507SMiklos Szeredi static void fuse_free_fsc(struct fs_context *fsc)
837c30da2e9SDavid Howells {
83884c21507SMiklos Szeredi 	struct fuse_fs_context *ctx = fsc->fs_private;
839c30da2e9SDavid Howells 
840c30da2e9SDavid Howells 	if (ctx) {
841c30da2e9SDavid Howells 		kfree(ctx->subtype);
842c30da2e9SDavid Howells 		kfree(ctx);
843c30da2e9SDavid Howells 	}
844d8a5ba45SMiklos Szeredi }
845d8a5ba45SMiklos Szeredi 
fuse_show_options(struct seq_file * m,struct dentry * root)84634c80b1dSAl Viro static int fuse_show_options(struct seq_file *m, struct dentry *root)
847d8a5ba45SMiklos Szeredi {
84834c80b1dSAl Viro 	struct super_block *sb = root->d_sb;
84934c80b1dSAl Viro 	struct fuse_conn *fc = get_fuse_conn_super(sb);
850d8a5ba45SMiklos Szeredi 
851f4fd4ae3SVivek Goyal 	if (fc->legacy_opts_show) {
852f4fd4ae3SVivek Goyal 		seq_printf(m, ",user_id=%u",
853f4fd4ae3SVivek Goyal 			   from_kuid_munged(fc->user_ns, fc->user_id));
854f4fd4ae3SVivek Goyal 		seq_printf(m, ",group_id=%u",
855f4fd4ae3SVivek Goyal 			   from_kgid_munged(fc->user_ns, fc->group_id));
85629433a29SMiklos Szeredi 		if (fc->default_permissions)
8571e9a4ed9SMiklos Szeredi 			seq_puts(m, ",default_permissions");
85829433a29SMiklos Szeredi 		if (fc->allow_other)
8591e9a4ed9SMiklos Szeredi 			seq_puts(m, ",allow_other");
860db50b96cSMiklos Szeredi 		if (fc->max_read != ~0)
861db50b96cSMiklos Szeredi 			seq_printf(m, ",max_read=%u", fc->max_read);
86234c80b1dSAl Viro 		if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
86334c80b1dSAl Viro 			seq_printf(m, ",blksize=%lu", sb->s_blocksize);
864f4fd4ae3SVivek Goyal 	}
8651dd53957SVivek Goyal #ifdef CONFIG_FUSE_DAX
866780b1b95SJeffle Xu 	if (fc->dax_mode == FUSE_DAX_ALWAYS)
867780b1b95SJeffle Xu 		seq_puts(m, ",dax=always");
868780b1b95SJeffle Xu 	else if (fc->dax_mode == FUSE_DAX_NEVER)
869780b1b95SJeffle Xu 		seq_puts(m, ",dax=never");
870780b1b95SJeffle Xu 	else if (fc->dax_mode == FUSE_DAX_INODE_USER)
871780b1b95SJeffle Xu 		seq_puts(m, ",dax=inode");
8721dd53957SVivek Goyal #endif
8731dd53957SVivek Goyal 
874d8a5ba45SMiklos Szeredi 	return 0;
875d8a5ba45SMiklos Szeredi }
876d8a5ba45SMiklos Szeredi 
fuse_iqueue_init(struct fuse_iqueue * fiq,const struct fuse_iqueue_ops * ops,void * priv)877ae3aad77SStefan Hajnoczi static void fuse_iqueue_init(struct fuse_iqueue *fiq,
878ae3aad77SStefan Hajnoczi 			     const struct fuse_iqueue_ops *ops,
879ae3aad77SStefan Hajnoczi 			     void *priv)
880f88996a9SMiklos Szeredi {
881f88996a9SMiklos Szeredi 	memset(fiq, 0, sizeof(struct fuse_iqueue));
88276e43c8cSEric Biggers 	spin_lock_init(&fiq->lock);
883f88996a9SMiklos Szeredi 	init_waitqueue_head(&fiq->waitq);
884f88996a9SMiklos Szeredi 	INIT_LIST_HEAD(&fiq->pending);
885f88996a9SMiklos Szeredi 	INIT_LIST_HEAD(&fiq->interrupts);
886f88996a9SMiklos Szeredi 	fiq->forget_list_tail = &fiq->forget_list_head;
887e16714d8SMiklos Szeredi 	fiq->connected = 1;
888ae3aad77SStefan Hajnoczi 	fiq->ops = ops;
889ae3aad77SStefan Hajnoczi 	fiq->priv = priv;
890f88996a9SMiklos Szeredi }
891f88996a9SMiklos Szeredi 
fuse_pqueue_init(struct fuse_pqueue * fpq)8923a2b5b9cSMiklos Szeredi static void fuse_pqueue_init(struct fuse_pqueue *fpq)
8933a2b5b9cSMiklos Szeredi {
894be2ff42cSKirill Tkhai 	unsigned int i;
895be2ff42cSKirill Tkhai 
89645a91cb1SMiklos Szeredi 	spin_lock_init(&fpq->lock);
897be2ff42cSKirill Tkhai 	for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
898be2ff42cSKirill Tkhai 		INIT_LIST_HEAD(&fpq->processing[i]);
8993a2b5b9cSMiklos Szeredi 	INIT_LIST_HEAD(&fpq->io);
900e96edd94SMiklos Szeredi 	fpq->connected = 1;
9013a2b5b9cSMiklos Szeredi }
9023a2b5b9cSMiklos 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)903fcee216bSMax Reitz void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
904fcee216bSMax Reitz 		    struct user_namespace *user_ns,
905ae3aad77SStefan Hajnoczi 		    const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
906d8a5ba45SMiklos Szeredi {
9070d179aa5STejun Heo 	memset(fc, 0, sizeof(*fc));
908d7133114SMiklos Szeredi 	spin_lock_init(&fc->lock);
909ae2dffa3SKirill Tkhai 	spin_lock_init(&fc->bg_lock);
9103b463ae0SJohn Muir 	init_rwsem(&fc->killsb);
911095fc40aSElena Reshetova 	refcount_set(&fc->count, 1);
912c3696046SMiklos Szeredi 	atomic_set(&fc->dev_count, 1);
91308a53cdcSMiklos Szeredi 	init_waitqueue_head(&fc->blocked_waitq);
914ae3aad77SStefan Hajnoczi 	fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
915d12def1bSMiklos Szeredi 	INIT_LIST_HEAD(&fc->bg_queue);
9160d179aa5STejun Heo 	INIT_LIST_HEAD(&fc->entry);
917cc080e9eSMiklos Szeredi 	INIT_LIST_HEAD(&fc->devices);
918095da6cbSMiklos Szeredi 	atomic_set(&fc->num_waiting, 0);
9197a6d3c8bSCsaba Henk 	fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
9207a6d3c8bSCsaba Henk 	fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
92175126f55SMiklos Szeredi 	atomic64_set(&fc->khctr, 0);
92295668a69STejun Heo 	fc->polled_files = RB_ROOT;
9230aada884SMaxim Patlasov 	fc->blocked = 0;
924796523fbSMaxim Patlasov 	fc->initialized = 0;
925e16714d8SMiklos Szeredi 	fc->connected = 1;
9264510d86fSKirill Tkhai 	atomic64_set(&fc->attr_version, 1);
9279c8ef561SMiklos Szeredi 	get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
9280b6e9ea0SSeth Forshee 	fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
9298cb08329SEric W. Biederman 	fc->user_ns = get_user_ns(user_ns);
9308a3177dbSMiklos Szeredi 	fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
931a7f0d7aaSConnor Kuehl 	fc->max_pages_limit = FUSE_MAX_MAX_PAGES;
932fcee216bSMax Reitz 
933fcee216bSMax Reitz 	INIT_LIST_HEAD(&fc->mounts);
934fcee216bSMax Reitz 	list_add(&fm->fc_entry, &fc->mounts);
935fcee216bSMax Reitz 	fm->fc = fc;
936d8a5ba45SMiklos Szeredi }
9370d179aa5STejun Heo EXPORT_SYMBOL_GPL(fuse_conn_init);
938d8a5ba45SMiklos Szeredi 
fuse_conn_put(struct fuse_conn * fc)939bafa9654SMiklos Szeredi void fuse_conn_put(struct fuse_conn *fc)
940bafa9654SMiklos Szeredi {
941095fc40aSElena Reshetova 	if (refcount_dec_and_test(&fc->count)) {
942a62a8ef9SStefan Hajnoczi 		struct fuse_iqueue *fiq = &fc->iq;
943660585b5SMiklos Szeredi 		struct fuse_sync_bucket *bucket;
944a62a8ef9SStefan Hajnoczi 
9451dd53957SVivek Goyal 		if (IS_ENABLED(CONFIG_FUSE_DAX))
9461dd53957SVivek Goyal 			fuse_dax_conn_free(fc);
947a62a8ef9SStefan Hajnoczi 		if (fiq->ops->release)
948a62a8ef9SStefan Hajnoczi 			fiq->ops->release(fiq);
9490b6e9ea0SSeth Forshee 		put_pid_ns(fc->pid_ns);
9508cb08329SEric W. Biederman 		put_user_ns(fc->user_ns);
951660585b5SMiklos Szeredi 		bucket = rcu_dereference_protected(fc->curr_bucket, 1);
952660585b5SMiklos Szeredi 		if (bucket) {
953660585b5SMiklos Szeredi 			WARN_ON(atomic_read(&bucket->count) != 1);
954660585b5SMiklos Szeredi 			kfree(bucket);
955660585b5SMiklos Szeredi 		}
95643901aabSTejun Heo 		fc->release(fc);
957bafa9654SMiklos Szeredi 	}
958d2a85164SMiklos Szeredi }
95908cbf542STejun Heo EXPORT_SYMBOL_GPL(fuse_conn_put);
960bafa9654SMiklos Szeredi 
fuse_conn_get(struct fuse_conn * fc)961bafa9654SMiklos Szeredi struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
962bafa9654SMiklos Szeredi {
963095fc40aSElena Reshetova 	refcount_inc(&fc->count);
964bafa9654SMiklos Szeredi 	return fc;
965bafa9654SMiklos Szeredi }
96608cbf542STejun Heo EXPORT_SYMBOL_GPL(fuse_conn_get);
967bafa9654SMiklos Szeredi 
fuse_get_root_inode(struct super_block * sb,unsigned mode)968b93f858aSTejun Heo static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
969d8a5ba45SMiklos Szeredi {
970d8a5ba45SMiklos Szeredi 	struct fuse_attr attr;
971d8a5ba45SMiklos Szeredi 	memset(&attr, 0, sizeof(attr));
972d8a5ba45SMiklos Szeredi 
973d8a5ba45SMiklos Szeredi 	attr.mode = mode;
974d8a5ba45SMiklos Szeredi 	attr.ino = FUSE_ROOT_ID;
975074406faSMiklos Szeredi 	attr.nlink = 1;
9761fb69e78SMiklos Szeredi 	return fuse_iget(sb, 1, 0, &attr, 0, 0);
977d8a5ba45SMiklos Szeredi }
978d8a5ba45SMiklos Szeredi 
9791729a16cSMiklos Szeredi struct fuse_inode_handle {
980dbd561d2SMiklos Szeredi 	u64 nodeid;
981dbd561d2SMiklos Szeredi 	u32 generation;
982dbd561d2SMiklos Szeredi };
983dbd561d2SMiklos Szeredi 
fuse_get_dentry(struct super_block * sb,struct fuse_inode_handle * handle)984dbd561d2SMiklos Szeredi static struct dentry *fuse_get_dentry(struct super_block *sb,
985dbd561d2SMiklos Szeredi 				      struct fuse_inode_handle *handle)
986dbd561d2SMiklos Szeredi {
98733670fa2SMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn_super(sb);
988dbd561d2SMiklos Szeredi 	struct inode *inode;
989dbd561d2SMiklos Szeredi 	struct dentry *entry;
990dbd561d2SMiklos Szeredi 	int err = -ESTALE;
991dbd561d2SMiklos Szeredi 
992dbd561d2SMiklos Szeredi 	if (handle->nodeid == 0)
993dbd561d2SMiklos Szeredi 		goto out_err;
994dbd561d2SMiklos Szeredi 
995dbd561d2SMiklos Szeredi 	inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
99633670fa2SMiklos Szeredi 	if (!inode) {
99733670fa2SMiklos Szeredi 		struct fuse_entry_out outarg;
99813983d06SAl Viro 		const struct qstr name = QSTR_INIT(".", 1);
99933670fa2SMiklos Szeredi 
100033670fa2SMiklos Szeredi 		if (!fc->export_support)
1001dbd561d2SMiklos Szeredi 			goto out_err;
100233670fa2SMiklos Szeredi 
100333670fa2SMiklos Szeredi 		err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
100433670fa2SMiklos Szeredi 				       &inode);
100533670fa2SMiklos Szeredi 		if (err && err != -ENOENT)
100633670fa2SMiklos Szeredi 			goto out_err;
100733670fa2SMiklos Szeredi 		if (err || !inode) {
100833670fa2SMiklos Szeredi 			err = -ESTALE;
100933670fa2SMiklos Szeredi 			goto out_err;
101033670fa2SMiklos Szeredi 		}
101133670fa2SMiklos Szeredi 		err = -EIO;
101233670fa2SMiklos Szeredi 		if (get_node_id(inode) != handle->nodeid)
101333670fa2SMiklos Szeredi 			goto out_iput;
101433670fa2SMiklos Szeredi 	}
1015dbd561d2SMiklos Szeredi 	err = -ESTALE;
1016dbd561d2SMiklos Szeredi 	if (inode->i_generation != handle->generation)
1017dbd561d2SMiklos Szeredi 		goto out_iput;
1018dbd561d2SMiklos Szeredi 
101944003728SChristoph Hellwig 	entry = d_obtain_alias(inode);
1020c35eebe9SAl Viro 	if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
1021dbd561d2SMiklos Szeredi 		fuse_invalidate_entry_cache(entry);
1022dbd561d2SMiklos Szeredi 
1023dbd561d2SMiklos Szeredi 	return entry;
1024dbd561d2SMiklos Szeredi 
1025dbd561d2SMiklos Szeredi  out_iput:
1026dbd561d2SMiklos Szeredi 	iput(inode);
1027dbd561d2SMiklos Szeredi  out_err:
1028dbd561d2SMiklos Szeredi 	return ERR_PTR(err);
1029dbd561d2SMiklos Szeredi }
1030dbd561d2SMiklos Szeredi 
fuse_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)1031b0b0382bSAl Viro static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
1032b0b0382bSAl Viro 			   struct inode *parent)
1033dbd561d2SMiklos Szeredi {
1034b0b0382bSAl Viro 	int len = parent ? 6 : 3;
1035dbd561d2SMiklos Szeredi 	u64 nodeid;
1036dbd561d2SMiklos Szeredi 	u32 generation;
1037dbd561d2SMiklos Szeredi 
10385fe0c237SAneesh Kumar K.V 	if (*max_len < len) {
10395fe0c237SAneesh Kumar K.V 		*max_len = len;
104094e07a75SNamjae Jeon 		return  FILEID_INVALID;
10415fe0c237SAneesh Kumar K.V 	}
1042dbd561d2SMiklos Szeredi 
1043dbd561d2SMiklos Szeredi 	nodeid = get_fuse_inode(inode)->nodeid;
1044dbd561d2SMiklos Szeredi 	generation = inode->i_generation;
1045dbd561d2SMiklos Szeredi 
1046dbd561d2SMiklos Szeredi 	fh[0] = (u32)(nodeid >> 32);
1047dbd561d2SMiklos Szeredi 	fh[1] = (u32)(nodeid & 0xffffffff);
1048dbd561d2SMiklos Szeredi 	fh[2] = generation;
1049dbd561d2SMiklos Szeredi 
1050b0b0382bSAl Viro 	if (parent) {
1051dbd561d2SMiklos Szeredi 		nodeid = get_fuse_inode(parent)->nodeid;
1052dbd561d2SMiklos Szeredi 		generation = parent->i_generation;
1053dbd561d2SMiklos Szeredi 
1054dbd561d2SMiklos Szeredi 		fh[3] = (u32)(nodeid >> 32);
1055dbd561d2SMiklos Szeredi 		fh[4] = (u32)(nodeid & 0xffffffff);
1056dbd561d2SMiklos Szeredi 		fh[5] = generation;
1057dbd561d2SMiklos Szeredi 	}
1058dbd561d2SMiklos Szeredi 
1059dbd561d2SMiklos Szeredi 	*max_len = len;
1060b0b0382bSAl Viro 	return parent ? 0x82 : 0x81;
1061dbd561d2SMiklos Szeredi }
1062dbd561d2SMiklos Szeredi 
fuse_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1063dbd561d2SMiklos Szeredi static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
1064dbd561d2SMiklos Szeredi 		struct fid *fid, int fh_len, int fh_type)
1065dbd561d2SMiklos Szeredi {
1066dbd561d2SMiklos Szeredi 	struct fuse_inode_handle handle;
1067dbd561d2SMiklos Szeredi 
1068dbd561d2SMiklos Szeredi 	if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
1069dbd561d2SMiklos Szeredi 		return NULL;
1070dbd561d2SMiklos Szeredi 
1071dbd561d2SMiklos Szeredi 	handle.nodeid = (u64) fid->raw[0] << 32;
1072dbd561d2SMiklos Szeredi 	handle.nodeid |= (u64) fid->raw[1];
1073dbd561d2SMiklos Szeredi 	handle.generation = fid->raw[2];
1074dbd561d2SMiklos Szeredi 	return fuse_get_dentry(sb, &handle);
1075dbd561d2SMiklos Szeredi }
1076dbd561d2SMiklos Szeredi 
fuse_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1077dbd561d2SMiklos Szeredi static struct dentry *fuse_fh_to_parent(struct super_block *sb,
1078dbd561d2SMiklos Szeredi 		struct fid *fid, int fh_len, int fh_type)
1079dbd561d2SMiklos Szeredi {
1080dbd561d2SMiklos Szeredi 	struct fuse_inode_handle parent;
1081dbd561d2SMiklos Szeredi 
1082dbd561d2SMiklos Szeredi 	if (fh_type != 0x82 || fh_len < 6)
1083dbd561d2SMiklos Szeredi 		return NULL;
1084dbd561d2SMiklos Szeredi 
1085dbd561d2SMiklos Szeredi 	parent.nodeid = (u64) fid->raw[3] << 32;
1086dbd561d2SMiklos Szeredi 	parent.nodeid |= (u64) fid->raw[4];
1087dbd561d2SMiklos Szeredi 	parent.generation = fid->raw[5];
1088dbd561d2SMiklos Szeredi 	return fuse_get_dentry(sb, &parent);
1089dbd561d2SMiklos Szeredi }
1090dbd561d2SMiklos Szeredi 
fuse_get_parent(struct dentry * child)109133670fa2SMiklos Szeredi static struct dentry *fuse_get_parent(struct dentry *child)
109233670fa2SMiklos Szeredi {
10932b0143b5SDavid Howells 	struct inode *child_inode = d_inode(child);
109433670fa2SMiklos Szeredi 	struct fuse_conn *fc = get_fuse_conn(child_inode);
109533670fa2SMiklos Szeredi 	struct inode *inode;
109633670fa2SMiklos Szeredi 	struct dentry *parent;
109733670fa2SMiklos Szeredi 	struct fuse_entry_out outarg;
109833670fa2SMiklos Szeredi 	int err;
109933670fa2SMiklos Szeredi 
110033670fa2SMiklos Szeredi 	if (!fc->export_support)
110133670fa2SMiklos Szeredi 		return ERR_PTR(-ESTALE);
110233670fa2SMiklos Szeredi 
110333670fa2SMiklos Szeredi 	err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
110480e5d1ffSAl Viro 			       &dotdot_name, &outarg, &inode);
110544003728SChristoph Hellwig 	if (err) {
110644003728SChristoph Hellwig 		if (err == -ENOENT)
110733670fa2SMiklos Szeredi 			return ERR_PTR(-ESTALE);
110844003728SChristoph Hellwig 		return ERR_PTR(err);
110933670fa2SMiklos Szeredi 	}
111044003728SChristoph Hellwig 
111144003728SChristoph Hellwig 	parent = d_obtain_alias(inode);
1112c35eebe9SAl Viro 	if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
111333670fa2SMiklos Szeredi 		fuse_invalidate_entry_cache(parent);
111433670fa2SMiklos Szeredi 
111533670fa2SMiklos Szeredi 	return parent;
111633670fa2SMiklos Szeredi }
1117dbd561d2SMiklos Szeredi 
1118dbd561d2SMiklos Szeredi static const struct export_operations fuse_export_operations = {
1119dbd561d2SMiklos Szeredi 	.fh_to_dentry	= fuse_fh_to_dentry,
1120dbd561d2SMiklos Szeredi 	.fh_to_parent	= fuse_fh_to_parent,
1121dbd561d2SMiklos Szeredi 	.encode_fh	= fuse_encode_fh,
112233670fa2SMiklos Szeredi 	.get_parent	= fuse_get_parent,
1123dbd561d2SMiklos Szeredi };
1124dbd561d2SMiklos Szeredi 
1125ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations fuse_super_operations = {
1126d8a5ba45SMiklos Szeredi 	.alloc_inode    = fuse_alloc_inode,
11279baf28bbSAl Viro 	.free_inode     = fuse_free_inode,
1128b57922d9SAl Viro 	.evict_inode	= fuse_evict_inode,
11291e18bda8SMiklos Szeredi 	.write_inode	= fuse_write_inode,
1130ead5f0b5SMiklos Szeredi 	.drop_inode	= generic_delete_inode,
113169a53bf2SMiklos Szeredi 	.umount_begin	= fuse_umount_begin,
1132e5e5558eSMiklos Szeredi 	.statfs		= fuse_statfs,
11332d82ab25SGreg Kurz 	.sync_fs	= fuse_sync_fs,
1134d8a5ba45SMiklos Szeredi 	.show_options	= fuse_show_options,
1135d8a5ba45SMiklos Szeredi };
1136d8a5ba45SMiklos Szeredi 
sanitize_global_limit(unsigned * limit)1137487ea5afSCsaba Henk static void sanitize_global_limit(unsigned *limit)
1138487ea5afSCsaba Henk {
1139f22f812dSMiklos Szeredi 	/*
1140f22f812dSMiklos Szeredi 	 * The default maximum number of async requests is calculated to consume
1141f22f812dSMiklos Szeredi 	 * 1/2^13 of the total memory, assuming 392 bytes per request.
1142f22f812dSMiklos Szeredi 	 */
1143487ea5afSCsaba Henk 	if (*limit == 0)
1144f22f812dSMiklos Szeredi 		*limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
1145487ea5afSCsaba Henk 
1146487ea5afSCsaba Henk 	if (*limit >= 1 << 16)
1147487ea5afSCsaba Henk 		*limit = (1 << 16) - 1;
1148487ea5afSCsaba Henk }
1149487ea5afSCsaba Henk 
set_global_limit(const char * val,const struct kernel_param * kp)1150e4dca7b7SKees Cook static int set_global_limit(const char *val, const struct kernel_param *kp)
1151487ea5afSCsaba Henk {
1152487ea5afSCsaba Henk 	int rv;
1153487ea5afSCsaba Henk 
1154487ea5afSCsaba Henk 	rv = param_set_uint(val, kp);
1155487ea5afSCsaba Henk 	if (rv)
1156487ea5afSCsaba Henk 		return rv;
1157487ea5afSCsaba Henk 
1158487ea5afSCsaba Henk 	sanitize_global_limit((unsigned *)kp->arg);
1159487ea5afSCsaba Henk 
1160487ea5afSCsaba Henk 	return 0;
1161487ea5afSCsaba Henk }
1162487ea5afSCsaba Henk 
process_init_limits(struct fuse_conn * fc,struct fuse_init_out * arg)1163487ea5afSCsaba Henk static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
1164487ea5afSCsaba Henk {
1165487ea5afSCsaba Henk 	int cap_sys_admin = capable(CAP_SYS_ADMIN);
1166487ea5afSCsaba Henk 
1167487ea5afSCsaba Henk 	if (arg->minor < 13)
1168487ea5afSCsaba Henk 		return;
1169487ea5afSCsaba Henk 
1170487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_bgreq);
1171487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_congthresh);
1172487ea5afSCsaba Henk 
1173ae2dffa3SKirill Tkhai 	spin_lock(&fc->bg_lock);
1174487ea5afSCsaba Henk 	if (arg->max_background) {
1175487ea5afSCsaba Henk 		fc->max_background = arg->max_background;
1176487ea5afSCsaba Henk 
1177487ea5afSCsaba Henk 		if (!cap_sys_admin && fc->max_background > max_user_bgreq)
1178487ea5afSCsaba Henk 			fc->max_background = max_user_bgreq;
1179487ea5afSCsaba Henk 	}
1180487ea5afSCsaba Henk 	if (arg->congestion_threshold) {
1181487ea5afSCsaba Henk 		fc->congestion_threshold = arg->congestion_threshold;
1182487ea5afSCsaba Henk 
1183487ea5afSCsaba Henk 		if (!cap_sys_admin &&
1184487ea5afSCsaba Henk 		    fc->congestion_threshold > max_user_congthresh)
1185487ea5afSCsaba Henk 			fc->congestion_threshold = max_user_congthresh;
1186487ea5afSCsaba Henk 	}
1187ae2dffa3SKirill Tkhai 	spin_unlock(&fc->bg_lock);
1188487ea5afSCsaba Henk }
1189487ea5afSCsaba Henk 
1190615047efSMiklos Szeredi struct fuse_init_args {
1191615047efSMiklos Szeredi 	struct fuse_args args;
1192615047efSMiklos Szeredi 	struct fuse_init_in in;
1193615047efSMiklos Szeredi 	struct fuse_init_out out;
1194615047efSMiklos Szeredi };
11959b9a0469SMiklos Szeredi 
process_init_reply(struct fuse_mount * fm,struct fuse_args * args,int error)1196fcee216bSMax Reitz static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
1197615047efSMiklos Szeredi 			       int error)
1198615047efSMiklos Szeredi {
1199fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1200615047efSMiklos Szeredi 	struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
1201615047efSMiklos Szeredi 	struct fuse_init_out *arg = &ia->out;
1202fd1a1dc6SStefan Hajnoczi 	bool ok = true;
1203615047efSMiklos Szeredi 
1204615047efSMiklos Szeredi 	if (error || arg->major != FUSE_KERNEL_VERSION)
1205fd1a1dc6SStefan Hajnoczi 		ok = false;
12069b9a0469SMiklos Szeredi 	else {
12079cd68455SMiklos Szeredi 		unsigned long ra_pages;
12089cd68455SMiklos Szeredi 
1209487ea5afSCsaba Henk 		process_init_limits(fc, arg);
1210487ea5afSCsaba Henk 
12119cd68455SMiklos Szeredi 		if (arg->minor >= 6) {
12123066ff93SBernd Schubert 			u64 flags = arg->flags;
12133066ff93SBernd Schubert 
12143066ff93SBernd Schubert 			if (flags & FUSE_INIT_EXT)
12153066ff93SBernd Schubert 				flags |= (u64) arg->flags2 << 32;
121653db2893SMiklos Szeredi 
121709cbfeafSKirill A. Shutemov 			ra_pages = arg->max_readahead / PAGE_SIZE;
121853db2893SMiklos Szeredi 			if (flags & FUSE_ASYNC_READ)
12199cd68455SMiklos Szeredi 				fc->async_read = 1;
122053db2893SMiklos Szeredi 			if (!(flags & FUSE_POSIX_LOCKS))
122171421259SMiklos Szeredi 				fc->no_lock = 1;
122237fb3a30SMiklos Szeredi 			if (arg->minor >= 17) {
122353db2893SMiklos Szeredi 				if (!(flags & FUSE_FLOCK_LOCKS))
122437fb3a30SMiklos Szeredi 					fc->no_flock = 1;
122524114504SMiklos Szeredi 			} else {
122653db2893SMiklos Szeredi 				if (!(flags & FUSE_POSIX_LOCKS))
122724114504SMiklos Szeredi 					fc->no_flock = 1;
122837fb3a30SMiklos Szeredi 			}
122953db2893SMiklos Szeredi 			if (flags & FUSE_ATOMIC_O_TRUNC)
12306ff958edSMiklos Szeredi 				fc->atomic_o_trunc = 1;
123133670fa2SMiklos Szeredi 			if (arg->minor >= 9) {
123233670fa2SMiklos Szeredi 				/* LOOKUP has dependency on proto version */
123353db2893SMiklos Szeredi 				if (flags & FUSE_EXPORT_SUPPORT)
123433670fa2SMiklos Szeredi 					fc->export_support = 1;
123533670fa2SMiklos Szeredi 			}
123653db2893SMiklos Szeredi 			if (flags & FUSE_BIG_WRITES)
123778bb6cb9SMiklos Szeredi 				fc->big_writes = 1;
123853db2893SMiklos Szeredi 			if (flags & FUSE_DONT_MASK)
1239e0a43ddcSMiklos Szeredi 				fc->dont_mask = 1;
124053db2893SMiklos Szeredi 			if (flags & FUSE_AUTO_INVAL_DATA)
124172d0d248SBrian Foster 				fc->auto_inval_data = 1;
124253db2893SMiklos Szeredi 			else if (flags & FUSE_EXPLICIT_INVAL_DATA)
1243ad2ba64dSKirill Smelkov 				fc->explicit_inval_data = 1;
124453db2893SMiklos Szeredi 			if (flags & FUSE_DO_READDIRPLUS) {
12450b05b183SAnand V. Avati 				fc->do_readdirplus = 1;
124653db2893SMiklos Szeredi 				if (flags & FUSE_READDIRPLUS_AUTO)
1247634734b6SEric Wong 					fc->readdirplus_auto = 1;
124828420dadSMiklos Szeredi 			}
124953db2893SMiklos Szeredi 			if (flags & FUSE_ASYNC_DIO)
125060b9df7aSMiklos Szeredi 				fc->async_dio = 1;
125153db2893SMiklos Szeredi 			if (flags & FUSE_WRITEBACK_CACHE)
12524d99ff8fSPavel Emelyanov 				fc->writeback_cache = 1;
125353db2893SMiklos Szeredi 			if (flags & FUSE_PARALLEL_DIROPS)
12545c672ab3SMiklos Szeredi 				fc->parallel_dirops = 1;
125553db2893SMiklos Szeredi 			if (flags & FUSE_HANDLE_KILLPRIV)
12565e940c1dSMiklos Szeredi 				fc->handle_killpriv = 1;
1257e27c9d38SMiklos Szeredi 			if (arg->time_gran && arg->time_gran <= 1000000000)
1258fcee216bSMax Reitz 				fm->sb->s_time_gran = arg->time_gran;
125953db2893SMiklos Szeredi 			if ((flags & FUSE_POSIX_ACL)) {
126029433a29SMiklos Szeredi 				fc->default_permissions = 1;
126160bcc88aSSeth Forshee 				fc->posix_acl = 1;
126260bcc88aSSeth Forshee 			}
126353db2893SMiklos Szeredi 			if (flags & FUSE_CACHE_SYMLINKS)
12645571f1e6SDan Schatzberg 				fc->cache_symlinks = 1;
126553db2893SMiklos Szeredi 			if (flags & FUSE_ABORT_ERROR)
12663b7008b2SSzymon Lukasz 				fc->abort_err = 1;
126753db2893SMiklos Szeredi 			if (flags & FUSE_MAX_PAGES) {
12685da784ccSConstantine Shulyupin 				fc->max_pages =
1269a7f0d7aaSConnor Kuehl 					min_t(unsigned int, fc->max_pages_limit,
12705da784ccSConstantine Shulyupin 					max_t(unsigned int, arg->max_pages, 1));
12715da784ccSConstantine Shulyupin 			}
12722ee019faSJeffle Xu 			if (IS_ENABLED(CONFIG_FUSE_DAX)) {
12732ee019faSJeffle Xu 				if (flags & FUSE_MAP_ALIGNMENT &&
1274fd1a1dc6SStefan Hajnoczi 				    !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1275fd1a1dc6SStefan Hajnoczi 					ok = false;
1276fd1a1dc6SStefan Hajnoczi 				}
12772ee019faSJeffle Xu 				if (flags & FUSE_HAS_INODE_DAX)
12782ee019faSJeffle Xu 					fc->inode_dax = 1;
12792ee019faSJeffle Xu 			}
128053db2893SMiklos Szeredi 			if (flags & FUSE_HANDLE_KILLPRIV_V2) {
128163f9909fSVivek Goyal 				fc->handle_killpriv_v2 = 1;
12829d769e6aSVivek Goyal 				fm->sb->s_flags |= SB_NOSEC;
12839d769e6aSVivek Goyal 			}
128453db2893SMiklos Szeredi 			if (flags & FUSE_SETXATTR_EXT)
128552a4c95fSVivek Goyal 				fc->setxattr_ext = 1;
12863e2b6fdbSVivek Goyal 			if (flags & FUSE_SECURITY_CTX)
12873e2b6fdbSVivek Goyal 				fc->init_security = 1;
12888ed7cb3fSMiklos Szeredi 			if (flags & FUSE_CREATE_SUPP_GROUP)
12898ed7cb3fSMiklos Szeredi 				fc->create_supp_group = 1;
12909f36c1c5STyler Fanelli 			if (flags & FUSE_DIRECT_IO_ALLOW_MMAP)
12919f36c1c5STyler Fanelli 				fc->direct_io_allow_mmap = 1;
129271421259SMiklos Szeredi 		} else {
129309cbfeafSKirill A. Shutemov 			ra_pages = fc->max_read / PAGE_SIZE;
129471421259SMiklos Szeredi 			fc->no_lock = 1;
129537fb3a30SMiklos Szeredi 			fc->no_flock = 1;
129671421259SMiklos Szeredi 		}
12979cd68455SMiklos Szeredi 
1298fcee216bSMax Reitz 		fm->sb->s_bdi->ra_pages =
1299fcee216bSMax Reitz 				min(fm->sb->s_bdi->ra_pages, ra_pages);
13009b9a0469SMiklos Szeredi 		fc->minor = arg->minor;
13019b9a0469SMiklos Szeredi 		fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
1302f948d564SMiklos Szeredi 		fc->max_write = max_t(unsigned, 4096, fc->max_write);
13030ec7ca41SMiklos Szeredi 		fc->conn_init = 1;
13049b9a0469SMiklos Szeredi 	}
1305615047efSMiklos Szeredi 	kfree(ia);
1306615047efSMiklos Szeredi 
1307fd1a1dc6SStefan Hajnoczi 	if (!ok) {
1308fd1a1dc6SStefan Hajnoczi 		fc->conn_init = 0;
1309fd1a1dc6SStefan Hajnoczi 		fc->conn_error = 1;
1310fd1a1dc6SStefan Hajnoczi 	}
1311fd1a1dc6SStefan Hajnoczi 
13129759bd51SMiklos Szeredi 	fuse_set_initialized(fc);
131308a53cdcSMiklos Szeredi 	wake_up_all(&fc->blocked_waitq);
13149b9a0469SMiklos Szeredi }
13159b9a0469SMiklos Szeredi 
fuse_send_init(struct fuse_mount * fm)1316fcee216bSMax Reitz void fuse_send_init(struct fuse_mount *fm)
13179b9a0469SMiklos Szeredi {
1318615047efSMiklos Szeredi 	struct fuse_init_args *ia;
131953db2893SMiklos Szeredi 	u64 flags;
1320095da6cbSMiklos Szeredi 
1321615047efSMiklos Szeredi 	ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1322615047efSMiklos Szeredi 
1323615047efSMiklos Szeredi 	ia->in.major = FUSE_KERNEL_VERSION;
1324615047efSMiklos Szeredi 	ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1325fcee216bSMax Reitz 	ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
132653db2893SMiklos Szeredi 	flags =
1327615047efSMiklos Szeredi 		FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
132837fb3a30SMiklos Szeredi 		FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
132969fe05c9SMiklos Szeredi 		FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
13309446385fSWei Fang 		FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
13314d99ff8fSPavel Emelyanov 		FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
13325c672ab3SMiklos Szeredi 		FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
13333b7008b2SSzymon Lukasz 		FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
1334d9a9ea94SChad Austin 		FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
133563f9909fSVivek Goyal 		FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
13363e2b6fdbSVivek Goyal 		FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
13375cadfbd5SMiklos Szeredi 		FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP |
13389f36c1c5STyler Fanelli 		FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP;
1339fd1a1dc6SStefan Hajnoczi #ifdef CONFIG_FUSE_DAX
1340fcee216bSMax Reitz 	if (fm->fc->dax)
134153db2893SMiklos Szeredi 		flags |= FUSE_MAP_ALIGNMENT;
13422ee019faSJeffle Xu 	if (fuse_is_inode_dax_mode(fm->fc->dax_mode))
13432ee019faSJeffle Xu 		flags |= FUSE_HAS_INODE_DAX;
1344fd1a1dc6SStefan Hajnoczi #endif
1345bf109c64SMax Reitz 	if (fm->fc->auto_submounts)
134653db2893SMiklos Szeredi 		flags |= FUSE_SUBMOUNTS;
134753db2893SMiklos Szeredi 
134853db2893SMiklos Szeredi 	ia->in.flags = flags;
134953db2893SMiklos Szeredi 	ia->in.flags2 = flags >> 32;
1350bf109c64SMax Reitz 
1351615047efSMiklos Szeredi 	ia->args.opcode = FUSE_INIT;
1352615047efSMiklos Szeredi 	ia->args.in_numargs = 1;
1353615047efSMiklos Szeredi 	ia->args.in_args[0].size = sizeof(ia->in);
1354615047efSMiklos Szeredi 	ia->args.in_args[0].value = &ia->in;
1355615047efSMiklos Szeredi 	ia->args.out_numargs = 1;
13563ad2f3fbSDaniel Mack 	/* Variable length argument used for backward compatibility
13579b9a0469SMiklos Szeredi 	   with interface version < 7.5.  Rest of init_out is zeroed
13589b9a0469SMiklos Szeredi 	   by do_get_request(), so a short reply is not a problem */
1359cabdb4faSzhengbin 	ia->args.out_argvar = true;
1360615047efSMiklos Szeredi 	ia->args.out_args[0].size = sizeof(ia->out);
1361615047efSMiklos Szeredi 	ia->args.out_args[0].value = &ia->out;
1362615047efSMiklos Szeredi 	ia->args.force = true;
1363615047efSMiklos Szeredi 	ia->args.nocreds = true;
1364615047efSMiklos Szeredi 	ia->args.end = process_init_reply;
1365615047efSMiklos Szeredi 
1366fcee216bSMax Reitz 	if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1367fcee216bSMax Reitz 		process_init_reply(fm, &ia->args, -ENOTCONN);
13689b9a0469SMiklos Szeredi }
136995a84cdbSVivek Goyal EXPORT_SYMBOL_GPL(fuse_send_init);
13709b9a0469SMiklos Szeredi 
fuse_free_conn(struct fuse_conn * fc)1371783863d6SMiklos Szeredi void fuse_free_conn(struct fuse_conn *fc)
137243901aabSTejun Heo {
1373cc080e9eSMiklos Szeredi 	WARN_ON(!list_empty(&fc->devices));
1374dd3e2c55SAl Viro 	kfree_rcu(fc, rcu);
137543901aabSTejun Heo }
1376783863d6SMiklos Szeredi EXPORT_SYMBOL_GPL(fuse_free_conn);
137743901aabSTejun Heo 
fuse_bdi_init(struct fuse_conn * fc,struct super_block * sb)1378a325f9b9STejun Heo static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1379a325f9b9STejun Heo {
1380a325f9b9STejun Heo 	int err;
13815f7f7543SJan Kara 	char *suffix = "";
1382a325f9b9STejun Heo 
138369c8ebf8SJan Kara 	if (sb->s_bdev) {
13845f7f7543SJan Kara 		suffix = "-fuseblk";
138569c8ebf8SJan Kara 		/*
138669c8ebf8SJan Kara 		 * sb->s_bdi points to blkdev's bdi however we want to redirect
138769c8ebf8SJan Kara 		 * it to our private bdi...
138869c8ebf8SJan Kara 		 */
138969c8ebf8SJan Kara 		bdi_put(sb->s_bdi);
139069c8ebf8SJan Kara 		sb->s_bdi = &noop_backing_dev_info;
139169c8ebf8SJan Kara 	}
13925f7f7543SJan Kara 	err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
13935f7f7543SJan Kara 				   MINOR(fc->dev), suffix);
1394a325f9b9STejun Heo 	if (err)
1395a325f9b9STejun Heo 		return err;
1396a325f9b9STejun Heo 
13975f7f7543SJan Kara 	/* fuse does it's own writeback accounting */
1398823423efSChristoph Hellwig 	sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT;
1399823423efSChristoph Hellwig 	sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
14005f7f7543SJan Kara 
1401a325f9b9STejun Heo 	/*
1402a325f9b9STejun Heo 	 * For a single fuse filesystem use max 1% of dirty +
1403a325f9b9STejun Heo 	 * writeback threshold.
1404a325f9b9STejun Heo 	 *
1405a325f9b9STejun Heo 	 * This gives about 1M of write buffer for memory maps on a
1406a325f9b9STejun Heo 	 * machine with 1G and 10% dirty_ratio, which should be more
1407a325f9b9STejun Heo 	 * than enough.
1408a325f9b9STejun Heo 	 *
1409a325f9b9STejun Heo 	 * Privileged users can raise it by writing to
1410a325f9b9STejun Heo 	 *
1411a325f9b9STejun Heo 	 *    /sys/class/bdi/<bdi>/max_ratio
1412a325f9b9STejun Heo 	 */
14135f7f7543SJan Kara 	bdi_set_max_ratio(sb->s_bdi, 1);
1414a325f9b9STejun Heo 
1415a325f9b9STejun Heo 	return 0;
1416a325f9b9STejun Heo }
1417a325f9b9STejun Heo 
fuse_dev_alloc(void)14180cd1eb9aSVivek Goyal struct fuse_dev *fuse_dev_alloc(void)
1419cc080e9eSMiklos Szeredi {
1420cc080e9eSMiklos Szeredi 	struct fuse_dev *fud;
1421be2ff42cSKirill Tkhai 	struct list_head *pq;
1422cc080e9eSMiklos Szeredi 
1423cc080e9eSMiklos Szeredi 	fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1424be2ff42cSKirill Tkhai 	if (!fud)
1425be2ff42cSKirill Tkhai 		return NULL;
1426be2ff42cSKirill Tkhai 
1427be2ff42cSKirill Tkhai 	pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1428be2ff42cSKirill Tkhai 	if (!pq) {
1429be2ff42cSKirill Tkhai 		kfree(fud);
1430be2ff42cSKirill Tkhai 		return NULL;
1431be2ff42cSKirill Tkhai 	}
1432be2ff42cSKirill Tkhai 
1433be2ff42cSKirill Tkhai 	fud->pq.processing = pq;
1434c3696046SMiklos Szeredi 	fuse_pqueue_init(&fud->pq);
1435cc080e9eSMiklos Szeredi 
1436cc080e9eSMiklos Szeredi 	return fud;
1437cc080e9eSMiklos Szeredi }
1438cc080e9eSMiklos Szeredi EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1439cc080e9eSMiklos Szeredi 
fuse_dev_install(struct fuse_dev * fud,struct fuse_conn * fc)14400cd1eb9aSVivek Goyal void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
14410cd1eb9aSVivek Goyal {
14420cd1eb9aSVivek Goyal 	fud->fc = fuse_conn_get(fc);
14430cd1eb9aSVivek Goyal 	spin_lock(&fc->lock);
14440cd1eb9aSVivek Goyal 	list_add_tail(&fud->entry, &fc->devices);
14450cd1eb9aSVivek Goyal 	spin_unlock(&fc->lock);
14460cd1eb9aSVivek Goyal }
14470cd1eb9aSVivek Goyal EXPORT_SYMBOL_GPL(fuse_dev_install);
14480cd1eb9aSVivek Goyal 
fuse_dev_alloc_install(struct fuse_conn * fc)14490cd1eb9aSVivek Goyal struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
14500cd1eb9aSVivek Goyal {
14510cd1eb9aSVivek Goyal 	struct fuse_dev *fud;
14520cd1eb9aSVivek Goyal 
14530cd1eb9aSVivek Goyal 	fud = fuse_dev_alloc();
14540cd1eb9aSVivek Goyal 	if (!fud)
14550cd1eb9aSVivek Goyal 		return NULL;
14560cd1eb9aSVivek Goyal 
14570cd1eb9aSVivek Goyal 	fuse_dev_install(fud, fc);
14580cd1eb9aSVivek Goyal 	return fud;
14590cd1eb9aSVivek Goyal }
14600cd1eb9aSVivek Goyal EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
14610cd1eb9aSVivek Goyal 
fuse_dev_free(struct fuse_dev * fud)1462cc080e9eSMiklos Szeredi void fuse_dev_free(struct fuse_dev *fud)
1463cc080e9eSMiklos Szeredi {
1464cc080e9eSMiklos Szeredi 	struct fuse_conn *fc = fud->fc;
1465cc080e9eSMiklos Szeredi 
1466cc080e9eSMiklos Szeredi 	if (fc) {
1467cc080e9eSMiklos Szeredi 		spin_lock(&fc->lock);
1468cc080e9eSMiklos Szeredi 		list_del(&fud->entry);
1469cc080e9eSMiklos Szeredi 		spin_unlock(&fc->lock);
1470cc080e9eSMiklos Szeredi 
1471cc080e9eSMiklos Szeredi 		fuse_conn_put(fc);
1472cc080e9eSMiklos Szeredi 	}
1473d72f70daSTakeshi Misawa 	kfree(fud->pq.processing);
1474cc080e9eSMiklos Szeredi 	kfree(fud);
1475cc080e9eSMiklos Szeredi }
1476cc080e9eSMiklos Szeredi EXPORT_SYMBOL_GPL(fuse_dev_free);
1477cc080e9eSMiklos Szeredi 
fuse_fill_attr_from_inode(struct fuse_attr * attr,const struct fuse_inode * fi)14781866d779SMax Reitz static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
14791866d779SMax Reitz 				      const struct fuse_inode *fi)
14801866d779SMax Reitz {
1481ceb2d5e9SJeff Layton 	struct timespec64 ctime = inode_get_ctime(&fi->inode);
1482ceb2d5e9SJeff Layton 
14831866d779SMax Reitz 	*attr = (struct fuse_attr){
14841866d779SMax Reitz 		.ino		= fi->inode.i_ino,
14851866d779SMax Reitz 		.size		= fi->inode.i_size,
14861866d779SMax Reitz 		.blocks		= fi->inode.i_blocks,
14871866d779SMax Reitz 		.atime		= fi->inode.i_atime.tv_sec,
14881866d779SMax Reitz 		.mtime		= fi->inode.i_mtime.tv_sec,
1489ceb2d5e9SJeff Layton 		.ctime		= ctime.tv_sec,
14901866d779SMax Reitz 		.atimensec	= fi->inode.i_atime.tv_nsec,
14911866d779SMax Reitz 		.mtimensec	= fi->inode.i_mtime.tv_nsec,
1492ceb2d5e9SJeff Layton 		.ctimensec	= ctime.tv_nsec,
14931866d779SMax Reitz 		.mode		= fi->inode.i_mode,
14941866d779SMax Reitz 		.nlink		= fi->inode.i_nlink,
14951866d779SMax Reitz 		.uid		= fi->inode.i_uid.val,
14961866d779SMax Reitz 		.gid		= fi->inode.i_gid.val,
14971866d779SMax Reitz 		.rdev		= fi->inode.i_rdev,
14981866d779SMax Reitz 		.blksize	= 1u << fi->inode.i_blkbits,
14991866d779SMax Reitz 	};
15001866d779SMax Reitz }
15011866d779SMax Reitz 
fuse_sb_defaults(struct super_block * sb)15021866d779SMax Reitz static void fuse_sb_defaults(struct super_block *sb)
15031866d779SMax Reitz {
15041866d779SMax Reitz 	sb->s_magic = FUSE_SUPER_MAGIC;
15051866d779SMax Reitz 	sb->s_op = &fuse_super_operations;
15061866d779SMax Reitz 	sb->s_xattr = fuse_xattr_handlers;
15071866d779SMax Reitz 	sb->s_maxbytes = MAX_LFS_FILESIZE;
15081866d779SMax Reitz 	sb->s_time_gran = 1;
15091866d779SMax Reitz 	sb->s_export_op = &fuse_export_operations;
15101866d779SMax Reitz 	sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
15111866d779SMax Reitz 	if (sb->s_user_ns != &init_user_ns)
15121866d779SMax Reitz 		sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
15131866d779SMax Reitz 	sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
15141866d779SMax Reitz }
15151866d779SMax Reitz 
fuse_fill_super_submount(struct super_block * sb,struct fuse_inode * parent_fi)15161b539917SGreg Kurz static int fuse_fill_super_submount(struct super_block *sb,
15171866d779SMax Reitz 				    struct fuse_inode *parent_fi)
15181866d779SMax Reitz {
15191866d779SMax Reitz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
15201866d779SMax Reitz 	struct super_block *parent_sb = parent_fi->inode.i_sb;
15211866d779SMax Reitz 	struct fuse_attr root_attr;
15221866d779SMax Reitz 	struct inode *root;
15232939dd30SKrister Johansen 	struct fuse_submount_lookup *sl;
15242939dd30SKrister Johansen 	struct fuse_inode *fi;
15251866d779SMax Reitz 
15261866d779SMax Reitz 	fuse_sb_defaults(sb);
15271866d779SMax Reitz 	fm->sb = sb;
15281866d779SMax Reitz 
15291866d779SMax Reitz 	WARN_ON(sb->s_bdi != &noop_backing_dev_info);
15301866d779SMax Reitz 	sb->s_bdi = bdi_get(parent_sb->s_bdi);
15311866d779SMax Reitz 
15321866d779SMax Reitz 	sb->s_xattr = parent_sb->s_xattr;
15331866d779SMax Reitz 	sb->s_time_gran = parent_sb->s_time_gran;
15341866d779SMax Reitz 	sb->s_blocksize = parent_sb->s_blocksize;
15351866d779SMax Reitz 	sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
15361866d779SMax Reitz 	sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
15371866d779SMax Reitz 	if (parent_sb->s_subtype && !sb->s_subtype)
15381866d779SMax Reitz 		return -ENOMEM;
15391866d779SMax Reitz 
15401866d779SMax Reitz 	fuse_fill_attr_from_inode(&root_attr, parent_fi);
15411866d779SMax Reitz 	root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
15421866d779SMax Reitz 	/*
15431866d779SMax Reitz 	 * This inode is just a duplicate, so it is not looked up and
15441866d779SMax Reitz 	 * its nlookup should not be incremented.  fuse_iget() does
15451866d779SMax Reitz 	 * that, though, so undo it here.
15461866d779SMax Reitz 	 */
15472939dd30SKrister Johansen 	fi = get_fuse_inode(root);
15482939dd30SKrister Johansen 	fi->nlookup--;
15492939dd30SKrister Johansen 
15501866d779SMax Reitz 	sb->s_d_op = &fuse_dentry_operations;
15511866d779SMax Reitz 	sb->s_root = d_make_root(root);
15521866d779SMax Reitz 	if (!sb->s_root)
15531866d779SMax Reitz 		return -ENOMEM;
15541866d779SMax Reitz 
15552939dd30SKrister Johansen 	/*
15562939dd30SKrister Johansen 	 * Grab the parent's submount_lookup pointer and take a
15572939dd30SKrister Johansen 	 * reference on the shared nlookup from the parent.  This is to
15582939dd30SKrister Johansen 	 * prevent the last forget for this nodeid from getting
15592939dd30SKrister Johansen 	 * triggered until all users have finished with it.
15602939dd30SKrister Johansen 	 */
15612939dd30SKrister Johansen 	sl = parent_fi->submount_lookup;
15622939dd30SKrister Johansen 	WARN_ON(!sl);
15632939dd30SKrister Johansen 	if (sl) {
15642939dd30SKrister Johansen 		refcount_inc(&sl->count);
15652939dd30SKrister Johansen 		fi->submount_lookup = sl;
15662939dd30SKrister Johansen 	}
15672939dd30SKrister Johansen 
15681866d779SMax Reitz 	return 0;
15691866d779SMax Reitz }
15701866d779SMax Reitz 
1571266eb3f2SGreg Kurz /* Filesystem context private data holds the FUSE inode of the mount point */
fuse_get_tree_submount(struct fs_context * fsc)1572fe0a7bd8SGreg Kurz static int fuse_get_tree_submount(struct fs_context *fsc)
1573fe0a7bd8SGreg Kurz {
1574266eb3f2SGreg Kurz 	struct fuse_mount *fm;
1575266eb3f2SGreg Kurz 	struct fuse_inode *mp_fi = fsc->fs_private;
1576266eb3f2SGreg Kurz 	struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
1577266eb3f2SGreg Kurz 	struct super_block *sb;
1578266eb3f2SGreg Kurz 	int err;
1579266eb3f2SGreg Kurz 
1580266eb3f2SGreg Kurz 	fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1581266eb3f2SGreg Kurz 	if (!fm)
1582266eb3f2SGreg Kurz 		return -ENOMEM;
1583266eb3f2SGreg Kurz 
1584c191cd07SMiklos Szeredi 	fm->fc = fuse_conn_get(fc);
1585266eb3f2SGreg Kurz 	fsc->s_fs_info = fm;
1586266eb3f2SGreg Kurz 	sb = sget_fc(fsc, NULL, set_anon_super_fc);
1587c191cd07SMiklos Szeredi 	if (fsc->s_fs_info)
1588c191cd07SMiklos Szeredi 		fuse_mount_destroy(fm);
1589c191cd07SMiklos Szeredi 	if (IS_ERR(sb))
1590266eb3f2SGreg Kurz 		return PTR_ERR(sb);
1591266eb3f2SGreg Kurz 
1592266eb3f2SGreg Kurz 	/* Initialize superblock, making @mp_fi its root */
1593266eb3f2SGreg Kurz 	err = fuse_fill_super_submount(sb, mp_fi);
1594266eb3f2SGreg Kurz 	if (err) {
1595266eb3f2SGreg Kurz 		deactivate_locked_super(sb);
1596266eb3f2SGreg Kurz 		return err;
1597266eb3f2SGreg Kurz 	}
1598266eb3f2SGreg Kurz 
1599266eb3f2SGreg Kurz 	down_write(&fc->killsb);
1600266eb3f2SGreg Kurz 	list_add_tail(&fm->fc_entry, &fc->mounts);
1601266eb3f2SGreg Kurz 	up_write(&fc->killsb);
1602266eb3f2SGreg Kurz 
1603266eb3f2SGreg Kurz 	sb->s_flags |= SB_ACTIVE;
1604266eb3f2SGreg Kurz 	fsc->root = dget(sb->s_root);
1605266eb3f2SGreg Kurz 
1606fe0a7bd8SGreg Kurz 	return 0;
1607fe0a7bd8SGreg Kurz }
1608fe0a7bd8SGreg Kurz 
1609fe0a7bd8SGreg Kurz static const struct fs_context_operations fuse_context_submount_ops = {
1610fe0a7bd8SGreg Kurz 	.get_tree	= fuse_get_tree_submount,
1611fe0a7bd8SGreg Kurz };
1612fe0a7bd8SGreg Kurz 
fuse_init_fs_context_submount(struct fs_context * fsc)1613fe0a7bd8SGreg Kurz int fuse_init_fs_context_submount(struct fs_context *fsc)
1614fe0a7bd8SGreg Kurz {
1615fe0a7bd8SGreg Kurz 	fsc->ops = &fuse_context_submount_ops;
1616fe0a7bd8SGreg Kurz 	return 0;
1617fe0a7bd8SGreg Kurz }
1618fe0a7bd8SGreg Kurz EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
1619fe0a7bd8SGreg Kurz 
fuse_fill_super_common(struct super_block * sb,struct fuse_fs_context * ctx)16200cc2656cSStefan Hajnoczi int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1621d8a5ba45SMiklos Szeredi {
16227fd3abfaSVivek Goyal 	struct fuse_dev *fud = NULL;
1623fcee216bSMax Reitz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1624fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1625d8a5ba45SMiklos Szeredi 	struct inode *root;
1626f543f253SMiklos Szeredi 	struct dentry *root_dentry;
1627d8a5ba45SMiklos Szeredi 	int err;
1628d8a5ba45SMiklos Szeredi 
1629c2b8f006SMiklos Szeredi 	err = -EINVAL;
16301751e8a6SLinus Torvalds 	if (sb->s_flags & SB_MANDLOCK)
1631c2b8f006SMiklos Szeredi 		goto err;
163271421259SMiklos Szeredi 
1633660585b5SMiklos Szeredi 	rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
16341866d779SMax Reitz 	fuse_sb_defaults(sb);
16359e1f1de0SAl Viro 
16360cc2656cSStefan Hajnoczi 	if (ctx->is_bdev) {
1637875d95ecSMiklos Szeredi #ifdef CONFIG_BLOCK
1638c2b8f006SMiklos Szeredi 		err = -EINVAL;
1639c30da2e9SDavid Howells 		if (!sb_set_blocksize(sb, ctx->blksize))
1640c2b8f006SMiklos Szeredi 			goto err;
1641875d95ecSMiklos Szeredi #endif
1642d8091614SMiklos Szeredi 	} else {
164309cbfeafSKirill A. Shutemov 		sb->s_blocksize = PAGE_SIZE;
164409cbfeafSKirill A. Shutemov 		sb->s_blocksize_bits = PAGE_SHIFT;
1645d8091614SMiklos Szeredi 	}
1646c30da2e9SDavid Howells 
1647c30da2e9SDavid Howells 	sb->s_subtype = ctx->subtype;
1648c30da2e9SDavid Howells 	ctx->subtype = NULL;
16491dd53957SVivek Goyal 	if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1650780b1b95SJeffle Xu 		err = fuse_dax_conn_alloc(fc, ctx->dax_mode, ctx->dax_dev);
16511dd53957SVivek Goyal 		if (err)
16521dd53957SVivek Goyal 			goto err;
16531dd53957SVivek Goyal 	}
1654e45b2546SEric W. Biederman 
16557fd3abfaSVivek Goyal 	if (ctx->fudptr) {
16567fd3abfaSVivek Goyal 		err = -ENOMEM;
16570cd1eb9aSVivek Goyal 		fud = fuse_dev_alloc_install(fc);
1658cc080e9eSMiklos Szeredi 		if (!fud)
16591dd53957SVivek Goyal 			goto err_free_dax;
16607fd3abfaSVivek Goyal 	}
1661cc080e9eSMiklos Szeredi 
1662a325f9b9STejun Heo 	fc->dev = sb->s_dev;
1663fcee216bSMax Reitz 	fm->sb = sb;
1664a325f9b9STejun Heo 	err = fuse_bdi_init(fc, sb);
1665a325f9b9STejun Heo 	if (err)
1666cc080e9eSMiklos Szeredi 		goto err_dev_free;
16670d179aa5STejun Heo 
1668e0a43ddcSMiklos Szeredi 	/* Handle umasking inside the fuse code */
16691751e8a6SLinus Torvalds 	if (sb->s_flags & SB_POSIXACL)
1670e0a43ddcSMiklos Szeredi 		fc->dont_mask = 1;
16711751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
1672e0a43ddcSMiklos Szeredi 
1673c30da2e9SDavid Howells 	fc->default_permissions = ctx->default_permissions;
1674c30da2e9SDavid Howells 	fc->allow_other = ctx->allow_other;
1675c30da2e9SDavid Howells 	fc->user_id = ctx->user_id;
1676c30da2e9SDavid Howells 	fc->group_id = ctx->group_id;
1677f4fd4ae3SVivek Goyal 	fc->legacy_opts_show = ctx->legacy_opts_show;
16781866d779SMax Reitz 	fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1679783863d6SMiklos Szeredi 	fc->destroy = ctx->destroy;
168015c8e72eSVivek Goyal 	fc->no_control = ctx->no_control;
168115c8e72eSVivek Goyal 	fc->no_force_umount = ctx->no_force_umount;
1682f543f253SMiklos Szeredi 
1683d8a5ba45SMiklos Szeredi 	err = -ENOMEM;
1684c30da2e9SDavid Howells 	root = fuse_get_root_inode(sb, ctx->rootmode);
16850ce267ffSMiklos Szeredi 	sb->s_d_op = &fuse_root_dentry_operations;
168648fde701SAl Viro 	root_dentry = d_make_root(root);
168748fde701SAl Viro 	if (!root_dentry)
1688cc080e9eSMiklos Szeredi 		goto err_dev_free;
16890ce267ffSMiklos Szeredi 	/* Root dentry doesn't have .d_revalidate */
1690c35eebe9SAl Viro 	sb->s_d_op = &fuse_dentry_operations;
1691f543f253SMiklos Szeredi 
1692bafa9654SMiklos Szeredi 	mutex_lock(&fuse_mutex);
16938aa09a50SMiklos Szeredi 	err = -EINVAL;
16947fd3abfaSVivek Goyal 	if (ctx->fudptr && *ctx->fudptr)
1695bafa9654SMiklos Szeredi 		goto err_unlock;
16968aa09a50SMiklos Szeredi 
1697bafa9654SMiklos Szeredi 	err = fuse_ctl_add_conn(fc);
1698bafa9654SMiklos Szeredi 	if (err)
1699bafa9654SMiklos Szeredi 		goto err_unlock;
1700bafa9654SMiklos Szeredi 
1701bafa9654SMiklos Szeredi 	list_add_tail(&fc->entry, &fuse_conn_list);
1702f543f253SMiklos Szeredi 	sb->s_root = root_dentry;
17037fd3abfaSVivek Goyal 	if (ctx->fudptr)
17040cc2656cSStefan Hajnoczi 		*ctx->fudptr = fud;
1705bafa9654SMiklos Szeredi 	mutex_unlock(&fuse_mutex);
1706d8a5ba45SMiklos Szeredi 	return 0;
1707d8a5ba45SMiklos Szeredi 
1708bafa9654SMiklos Szeredi  err_unlock:
1709bafa9654SMiklos Szeredi 	mutex_unlock(&fuse_mutex);
1710f543f253SMiklos Szeredi 	dput(root_dentry);
1711cc080e9eSMiklos Szeredi  err_dev_free:
17127fd3abfaSVivek Goyal 	if (fud)
1713cc080e9eSMiklos Szeredi 		fuse_dev_free(fud);
17141dd53957SVivek Goyal  err_free_dax:
17151dd53957SVivek Goyal 	if (IS_ENABLED(CONFIG_FUSE_DAX))
17161dd53957SVivek Goyal 		fuse_dax_conn_free(fc);
17170cc2656cSStefan Hajnoczi  err:
17180cc2656cSStefan Hajnoczi 	return err;
17190cc2656cSStefan Hajnoczi }
17200cc2656cSStefan Hajnoczi EXPORT_SYMBOL_GPL(fuse_fill_super_common);
17210cc2656cSStefan Hajnoczi 
fuse_fill_super(struct super_block * sb,struct fs_context * fsc)17220cc2656cSStefan Hajnoczi static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
17230cc2656cSStefan Hajnoczi {
17240cc2656cSStefan Hajnoczi 	struct fuse_fs_context *ctx = fsc->fs_private;
17250cc2656cSStefan Hajnoczi 	int err;
17260cc2656cSStefan Hajnoczi 
172762dd1fc8SMiklos Szeredi 	if (!ctx->file || !ctx->rootmode_present ||
1728badc7414SMiklos Szeredi 	    !ctx->user_id_present || !ctx->group_id_present)
1729badc7414SMiklos Szeredi 		return -EINVAL;
17300cc2656cSStefan Hajnoczi 
17310cc2656cSStefan Hajnoczi 	/*
17320cc2656cSStefan Hajnoczi 	 * Require mount to happen from the same user namespace which
17330cc2656cSStefan Hajnoczi 	 * opened /dev/fuse to prevent potential attacks.
17340cc2656cSStefan Hajnoczi 	 */
173562dd1fc8SMiklos Szeredi 	if ((ctx->file->f_op != &fuse_dev_operations) ||
173662dd1fc8SMiklos Szeredi 	    (ctx->file->f_cred->user_ns != sb->s_user_ns))
1737964d32e5SMiklos Szeredi 		return -EINVAL;
173862dd1fc8SMiklos Szeredi 	ctx->fudptr = &ctx->file->private_data;
17390cc2656cSStefan Hajnoczi 
17400cc2656cSStefan Hajnoczi 	err = fuse_fill_super_common(sb, ctx);
17410cc2656cSStefan Hajnoczi 	if (err)
1742964d32e5SMiklos Szeredi 		return err;
174362dd1fc8SMiklos Szeredi 	/* file->private_data shall be visible on all CPUs after this */
174462dd1fc8SMiklos Szeredi 	smp_mb();
1745fcee216bSMax Reitz 	fuse_send_init(get_fuse_mount_super(sb));
17460cc2656cSStefan Hajnoczi 	return 0;
1747d8a5ba45SMiklos Szeredi }
1748d8a5ba45SMiklos Szeredi 
17495d5b74aaSMiklos Szeredi /*
17505d5b74aaSMiklos Szeredi  * This is the path where user supplied an already initialized fuse dev.  In
17515d5b74aaSMiklos Szeredi  * this case never create a new super if the old one is gone.
17525d5b74aaSMiklos Szeredi  */
fuse_set_no_super(struct super_block * sb,struct fs_context * fsc)17535d5b74aaSMiklos Szeredi static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
1754d8a5ba45SMiklos Szeredi {
17555d5b74aaSMiklos Szeredi 	return -ENOTCONN;
17565d5b74aaSMiklos Szeredi }
1757c30da2e9SDavid Howells 
fuse_test_super(struct super_block * sb,struct fs_context * fsc)17585d5b74aaSMiklos Szeredi static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
17595d5b74aaSMiklos Szeredi {
17605d5b74aaSMiklos Szeredi 
17615d5b74aaSMiklos Szeredi 	return fsc->sget_key == get_fuse_conn_super(sb);
17625d5b74aaSMiklos Szeredi }
17635d5b74aaSMiklos Szeredi 
fuse_get_tree(struct fs_context * fsc)176484c21507SMiklos Szeredi static int fuse_get_tree(struct fs_context *fsc)
1765d8a5ba45SMiklos Szeredi {
176684c21507SMiklos Szeredi 	struct fuse_fs_context *ctx = fsc->fs_private;
17675d5b74aaSMiklos Szeredi 	struct fuse_dev *fud;
176880019f11SMiklos Szeredi 	struct fuse_conn *fc;
176980019f11SMiklos Szeredi 	struct fuse_mount *fm;
17705d5b74aaSMiklos Szeredi 	struct super_block *sb;
177162dd1fc8SMiklos Szeredi 	int err;
177262dd1fc8SMiklos Szeredi 
177380019f11SMiklos Szeredi 	fc = kmalloc(sizeof(*fc), GFP_KERNEL);
177480019f11SMiklos Szeredi 	if (!fc)
177580019f11SMiklos Szeredi 		return -ENOMEM;
177680019f11SMiklos Szeredi 
177780019f11SMiklos Szeredi 	fm = kzalloc(sizeof(*fm), GFP_KERNEL);
177880019f11SMiklos Szeredi 	if (!fm) {
177980019f11SMiklos Szeredi 		kfree(fc);
178080019f11SMiklos Szeredi 		return -ENOMEM;
178180019f11SMiklos Szeredi 	}
178280019f11SMiklos Szeredi 
178380019f11SMiklos Szeredi 	fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
178480019f11SMiklos Szeredi 	fc->release = fuse_free_conn;
178580019f11SMiklos Szeredi 
178680019f11SMiklos Szeredi 	fsc->s_fs_info = fm;
178780019f11SMiklos Szeredi 
178862dd1fc8SMiklos Szeredi 	if (ctx->fd_present)
178962dd1fc8SMiklos Szeredi 		ctx->file = fget(ctx->fd);
1790c30da2e9SDavid Howells 
1791badc7414SMiklos Szeredi 	if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
179262dd1fc8SMiklos Szeredi 		err = get_tree_bdev(fsc, fuse_fill_super);
179380019f11SMiklos Szeredi 		goto out;
1794badc7414SMiklos Szeredi 	}
17955d5b74aaSMiklos Szeredi 	/*
17965d5b74aaSMiklos Szeredi 	 * While block dev mount can be initialized with a dummy device fd
17975d5b74aaSMiklos Szeredi 	 * (found by device name), normal fuse mounts can't
17985d5b74aaSMiklos Szeredi 	 */
179980019f11SMiklos Szeredi 	err = -EINVAL;
18005d5b74aaSMiklos Szeredi 	if (!ctx->file)
180180019f11SMiklos Szeredi 		goto out;
1802c30da2e9SDavid Howells 
18035d5b74aaSMiklos Szeredi 	/*
18045d5b74aaSMiklos Szeredi 	 * Allow creating a fuse mount with an already initialized fuse
18055d5b74aaSMiklos Szeredi 	 * connection
18065d5b74aaSMiklos Szeredi 	 */
18075d5b74aaSMiklos Szeredi 	fud = READ_ONCE(ctx->file->private_data);
18085d5b74aaSMiklos Szeredi 	if (ctx->file->f_op == &fuse_dev_operations && fud) {
18095d5b74aaSMiklos Szeredi 		fsc->sget_key = fud->fc;
18105d5b74aaSMiklos Szeredi 		sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
18115d5b74aaSMiklos Szeredi 		err = PTR_ERR_OR_ZERO(sb);
18125d5b74aaSMiklos Szeredi 		if (!IS_ERR(sb))
18135d5b74aaSMiklos Szeredi 			fsc->root = dget(sb->s_root);
18145d5b74aaSMiklos Szeredi 	} else {
181562dd1fc8SMiklos Szeredi 		err = get_tree_nodev(fsc, fuse_fill_super);
18165d5b74aaSMiklos Szeredi 	}
181780019f11SMiklos Szeredi out:
181880019f11SMiklos Szeredi 	if (fsc->s_fs_info)
181980019f11SMiklos Szeredi 		fuse_mount_destroy(fm);
182062dd1fc8SMiklos Szeredi 	if (ctx->file)
182162dd1fc8SMiklos Szeredi 		fput(ctx->file);
182262dd1fc8SMiklos Szeredi 	return err;
1823c30da2e9SDavid Howells }
1824c30da2e9SDavid Howells 
1825c30da2e9SDavid Howells static const struct fs_context_operations fuse_context_ops = {
182684c21507SMiklos Szeredi 	.free		= fuse_free_fsc,
1827c30da2e9SDavid Howells 	.parse_param	= fuse_parse_param,
18280189a2d3SMiklos Szeredi 	.reconfigure	= fuse_reconfigure,
1829c30da2e9SDavid Howells 	.get_tree	= fuse_get_tree,
1830c30da2e9SDavid Howells };
1831c30da2e9SDavid Howells 
1832c30da2e9SDavid Howells /*
1833c30da2e9SDavid Howells  * Set up the filesystem mount context.
1834c30da2e9SDavid Howells  */
fuse_init_fs_context(struct fs_context * fsc)183584c21507SMiklos Szeredi static int fuse_init_fs_context(struct fs_context *fsc)
1836c30da2e9SDavid Howells {
1837c30da2e9SDavid Howells 	struct fuse_fs_context *ctx;
1838c30da2e9SDavid Howells 
1839c30da2e9SDavid Howells 	ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1840c30da2e9SDavid Howells 	if (!ctx)
1841c30da2e9SDavid Howells 		return -ENOMEM;
1842c30da2e9SDavid Howells 
1843c30da2e9SDavid Howells 	ctx->max_read = ~0;
1844c30da2e9SDavid Howells 	ctx->blksize = FUSE_DEFAULT_BLKSIZE;
1845f4fd4ae3SVivek Goyal 	ctx->legacy_opts_show = true;
1846c30da2e9SDavid Howells 
1847c30da2e9SDavid Howells #ifdef CONFIG_BLOCK
184884c21507SMiklos Szeredi 	if (fsc->fs_type == &fuseblk_fs_type) {
1849c30da2e9SDavid Howells 		ctx->is_bdev = true;
1850783863d6SMiklos Szeredi 		ctx->destroy = true;
1851783863d6SMiklos Szeredi 	}
1852c30da2e9SDavid Howells #endif
1853c30da2e9SDavid Howells 
185484c21507SMiklos Szeredi 	fsc->fs_private = ctx;
185584c21507SMiklos Szeredi 	fsc->ops = &fuse_context_ops;
1856c30da2e9SDavid Howells 	return 0;
1857d8a5ba45SMiklos Szeredi }
1858d8a5ba45SMiklos Szeredi 
fuse_mount_remove(struct fuse_mount * fm)1859fcee216bSMax Reitz bool fuse_mount_remove(struct fuse_mount *fm)
18603b463ae0SJohn Muir {
1861fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1862fcee216bSMax Reitz 	bool last = false;
18633b463ae0SJohn Muir 
1864fcee216bSMax Reitz 	down_write(&fc->killsb);
1865fcee216bSMax Reitz 	list_del_init(&fm->fc_entry);
1866fcee216bSMax Reitz 	if (list_empty(&fc->mounts))
1867fcee216bSMax Reitz 		last = true;
1868fcee216bSMax Reitz 	up_write(&fc->killsb);
1869fcee216bSMax Reitz 
1870fcee216bSMax Reitz 	return last;
1871fcee216bSMax Reitz }
1872fcee216bSMax Reitz EXPORT_SYMBOL_GPL(fuse_mount_remove);
1873fcee216bSMax Reitz 
fuse_conn_destroy(struct fuse_mount * fm)1874fcee216bSMax Reitz void fuse_conn_destroy(struct fuse_mount *fm)
1875fcee216bSMax Reitz {
1876fcee216bSMax Reitz 	struct fuse_conn *fc = fm->fc;
1877fcee216bSMax Reitz 
18781ccd1ea2SMiklos Szeredi 	if (fc->destroy)
1879fcee216bSMax Reitz 		fuse_send_destroy(fm);
1880e8f3bd77SMiklos Szeredi 
1881eb98e3bdSMiklos Szeredi 	fuse_abort_conn(fc);
1882e8f3bd77SMiklos Szeredi 	fuse_wait_aborted(fc);
1883e8f3bd77SMiklos Szeredi 
1884413daa1aSMiklos Szeredi 	if (!list_empty(&fc->entry)) {
1885413daa1aSMiklos Szeredi 		mutex_lock(&fuse_mutex);
1886413daa1aSMiklos Szeredi 		list_del(&fc->entry);
1887413daa1aSMiklos Szeredi 		fuse_ctl_remove_conn(fc);
1888413daa1aSMiklos Szeredi 		mutex_unlock(&fuse_mutex);
18893b463ae0SJohn Muir 	}
1890e8f3bd77SMiklos Szeredi }
1891fcee216bSMax Reitz EXPORT_SYMBOL_GPL(fuse_conn_destroy);
18923b463ae0SJohn Muir 
fuse_sb_destroy(struct super_block * sb)18936a68d1e1SMiklos Szeredi static void fuse_sb_destroy(struct super_block *sb)
1894e8f3bd77SMiklos Szeredi {
1895fcee216bSMax Reitz 	struct fuse_mount *fm = get_fuse_mount_super(sb);
1896fcee216bSMax Reitz 	bool last;
1897fcee216bSMax Reitz 
1898d534d31dSMiklos Szeredi 	if (sb->s_root) {
1899fcee216bSMax Reitz 		last = fuse_mount_remove(fm);
1900fcee216bSMax Reitz 		if (last)
1901fcee216bSMax Reitz 			fuse_conn_destroy(fm);
1902fcee216bSMax Reitz 	}
19036a68d1e1SMiklos Szeredi }
19046a68d1e1SMiklos Szeredi 
fuse_mount_destroy(struct fuse_mount * fm)1905a27c061aSMiklos Szeredi void fuse_mount_destroy(struct fuse_mount *fm)
1906a27c061aSMiklos Szeredi {
1907a27c061aSMiklos Szeredi 	fuse_conn_put(fm->fc);
1908a27c061aSMiklos Szeredi 	kfree(fm);
1909a27c061aSMiklos Szeredi }
1910a27c061aSMiklos Szeredi EXPORT_SYMBOL(fuse_mount_destroy);
1911a27c061aSMiklos Szeredi 
fuse_kill_sb_anon(struct super_block * sb)19126a68d1e1SMiklos Szeredi static void fuse_kill_sb_anon(struct super_block *sb)
19136a68d1e1SMiklos Szeredi {
19146a68d1e1SMiklos Szeredi 	fuse_sb_destroy(sb);
19153b463ae0SJohn Muir 	kill_anon_super(sb);
1916a27c061aSMiklos Szeredi 	fuse_mount_destroy(get_fuse_mount_super(sb));
19173b463ae0SJohn Muir }
19183b463ae0SJohn Muir 
1919875d95ecSMiklos Szeredi static struct file_system_type fuse_fs_type = {
1920875d95ecSMiklos Szeredi 	.owner		= THIS_MODULE,
1921875d95ecSMiklos Szeredi 	.name		= "fuse",
19224ad769f3SEric W. Biederman 	.fs_flags	= FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
1923c30da2e9SDavid Howells 	.init_fs_context = fuse_init_fs_context,
1924d7167b14SAl Viro 	.parameters	= fuse_fs_parameters,
19253b463ae0SJohn Muir 	.kill_sb	= fuse_kill_sb_anon,
1926875d95ecSMiklos Szeredi };
19277f78e035SEric W. Biederman MODULE_ALIAS_FS("fuse");
1928875d95ecSMiklos Szeredi 
1929875d95ecSMiklos Szeredi #ifdef CONFIG_BLOCK
fuse_kill_sb_blk(struct super_block * sb)19303b463ae0SJohn Muir static void fuse_kill_sb_blk(struct super_block *sb)
19313b463ae0SJohn Muir {
19326a68d1e1SMiklos Szeredi 	fuse_sb_destroy(sb);
19333b463ae0SJohn Muir 	kill_block_super(sb);
1934a27c061aSMiklos Szeredi 	fuse_mount_destroy(get_fuse_mount_super(sb));
19353b463ae0SJohn Muir }
19363b463ae0SJohn Muir 
1937d6392f87SMiklos Szeredi static struct file_system_type fuseblk_fs_type = {
1938d6392f87SMiklos Szeredi 	.owner		= THIS_MODULE,
1939d6392f87SMiklos Szeredi 	.name		= "fuseblk",
1940c30da2e9SDavid Howells 	.init_fs_context = fuse_init_fs_context,
1941d7167b14SAl Viro 	.parameters	= fuse_fs_parameters,
19423b463ae0SJohn Muir 	.kill_sb	= fuse_kill_sb_blk,
1943edad01e2SAlexey Dobriyan 	.fs_flags	= FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
1944d6392f87SMiklos Szeredi };
19457f78e035SEric W. Biederman MODULE_ALIAS_FS("fuseblk");
1946d6392f87SMiklos Szeredi 
register_fuseblk(void)1947875d95ecSMiklos Szeredi static inline int register_fuseblk(void)
1948875d95ecSMiklos Szeredi {
1949875d95ecSMiklos Szeredi 	return register_filesystem(&fuseblk_fs_type);
1950875d95ecSMiklos Szeredi }
1951875d95ecSMiklos Szeredi 
unregister_fuseblk(void)1952875d95ecSMiklos Szeredi static inline void unregister_fuseblk(void)
1953875d95ecSMiklos Szeredi {
1954875d95ecSMiklos Szeredi 	unregister_filesystem(&fuseblk_fs_type);
1955875d95ecSMiklos Szeredi }
1956875d95ecSMiklos Szeredi #else
register_fuseblk(void)1957875d95ecSMiklos Szeredi static inline int register_fuseblk(void)
1958875d95ecSMiklos Szeredi {
1959875d95ecSMiklos Szeredi 	return 0;
1960875d95ecSMiklos Szeredi }
1961875d95ecSMiklos Szeredi 
unregister_fuseblk(void)1962875d95ecSMiklos Szeredi static inline void unregister_fuseblk(void)
1963875d95ecSMiklos Szeredi {
1964875d95ecSMiklos Szeredi }
1965875d95ecSMiklos Szeredi #endif
1966875d95ecSMiklos Szeredi 
fuse_inode_init_once(void * foo)196751cc5068SAlexey Dobriyan static void fuse_inode_init_once(void *foo)
1968d8a5ba45SMiklos Szeredi {
1969d8a5ba45SMiklos Szeredi 	struct inode *inode = foo;
1970d8a5ba45SMiklos Szeredi 
1971d8a5ba45SMiklos Szeredi 	inode_init_once(inode);
1972d8a5ba45SMiklos Szeredi }
1973d8a5ba45SMiklos Szeredi 
fuse_fs_init(void)1974d8a5ba45SMiklos Szeredi static int __init fuse_fs_init(void)
1975d8a5ba45SMiklos Szeredi {
1976d8a5ba45SMiklos Szeredi 	int err;
1977d8a5ba45SMiklos Szeredi 
1978d8a5ba45SMiklos Szeredi 	fuse_inode_cachep = kmem_cache_create("fuse_inode",
19795d097056SVladimir Davydov 			sizeof(struct fuse_inode), 0,
1980df206988SJohannes Weiner 			SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
198120c2df83SPaul Mundt 			fuse_inode_init_once);
1982d8a5ba45SMiklos Szeredi 	err = -ENOMEM;
1983d6392f87SMiklos Szeredi 	if (!fuse_inode_cachep)
1984988f0325SAl Viro 		goto out;
1985988f0325SAl Viro 
1986988f0325SAl Viro 	err = register_fuseblk();
1987988f0325SAl Viro 	if (err)
1988988f0325SAl Viro 		goto out2;
1989988f0325SAl Viro 
1990988f0325SAl Viro 	err = register_filesystem(&fuse_fs_type);
1991988f0325SAl Viro 	if (err)
1992988f0325SAl Viro 		goto out3;
1993d8a5ba45SMiklos Szeredi 
1994d6392f87SMiklos Szeredi 	return 0;
1995d6392f87SMiklos Szeredi 
1996988f0325SAl Viro  out3:
1997875d95ecSMiklos Szeredi 	unregister_fuseblk();
1998988f0325SAl Viro  out2:
1999988f0325SAl Viro 	kmem_cache_destroy(fuse_inode_cachep);
2000d6392f87SMiklos Szeredi  out:
2001d8a5ba45SMiklos Szeredi 	return err;
2002d8a5ba45SMiklos Szeredi }
2003d8a5ba45SMiklos Szeredi 
fuse_fs_cleanup(void)2004d8a5ba45SMiklos Szeredi static void fuse_fs_cleanup(void)
2005d8a5ba45SMiklos Szeredi {
2006d8a5ba45SMiklos Szeredi 	unregister_filesystem(&fuse_fs_type);
2007875d95ecSMiklos Szeredi 	unregister_fuseblk();
20088c0a8537SKirill A. Shutemov 
20098c0a8537SKirill A. Shutemov 	/*
20108c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
20118c0a8537SKirill A. Shutemov 	 * destroy cache.
20128c0a8537SKirill A. Shutemov 	 */
20138c0a8537SKirill A. Shutemov 	rcu_barrier();
2014d8a5ba45SMiklos Szeredi 	kmem_cache_destroy(fuse_inode_cachep);
2015d8a5ba45SMiklos Szeredi }
2016d8a5ba45SMiklos Szeredi 
20175c89e17eSGreg Kroah-Hartman static struct kobject *fuse_kobj;
20185c89e17eSGreg Kroah-Hartman 
fuse_sysfs_init(void)2019f543f253SMiklos Szeredi static int fuse_sysfs_init(void)
2020f543f253SMiklos Szeredi {
2021f543f253SMiklos Szeredi 	int err;
2022f543f253SMiklos Szeredi 
202300d26666SGreg Kroah-Hartman 	fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
20245c89e17eSGreg Kroah-Hartman 	if (!fuse_kobj) {
20255c89e17eSGreg Kroah-Hartman 		err = -ENOMEM;
2026f543f253SMiklos Szeredi 		goto out_err;
20275c89e17eSGreg Kroah-Hartman 	}
2028f543f253SMiklos Szeredi 
2029f9bb4882SEric W. Biederman 	err = sysfs_create_mount_point(fuse_kobj, "connections");
2030f9bb4882SEric W. Biederman 	if (err)
2031f543f253SMiklos Szeredi 		goto out_fuse_unregister;
2032f543f253SMiklos Szeredi 
2033f543f253SMiklos Szeredi 	return 0;
2034f543f253SMiklos Szeredi 
2035f543f253SMiklos Szeredi  out_fuse_unregister:
2036197b12d6SGreg Kroah-Hartman 	kobject_put(fuse_kobj);
2037f543f253SMiklos Szeredi  out_err:
2038f543f253SMiklos Szeredi 	return err;
2039f543f253SMiklos Szeredi }
2040f543f253SMiklos Szeredi 
fuse_sysfs_cleanup(void)2041f543f253SMiklos Szeredi static void fuse_sysfs_cleanup(void)
2042f543f253SMiklos Szeredi {
2043f9bb4882SEric W. Biederman 	sysfs_remove_mount_point(fuse_kobj, "connections");
2044197b12d6SGreg Kroah-Hartman 	kobject_put(fuse_kobj);
2045f543f253SMiklos Szeredi }
2046f543f253SMiklos Szeredi 
fuse_init(void)2047d8a5ba45SMiklos Szeredi static int __init fuse_init(void)
2048d8a5ba45SMiklos Szeredi {
2049d8a5ba45SMiklos Szeredi 	int res;
2050d8a5ba45SMiklos Szeredi 
2051f2294482SKirill Smelkov 	pr_info("init (API version %i.%i)\n",
2052d8a5ba45SMiklos Szeredi 		FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2053d8a5ba45SMiklos Szeredi 
2054bafa9654SMiklos Szeredi 	INIT_LIST_HEAD(&fuse_conn_list);
2055d8a5ba45SMiklos Szeredi 	res = fuse_fs_init();
2056d8a5ba45SMiklos Szeredi 	if (res)
2057d8a5ba45SMiklos Szeredi 		goto err;
2058d8a5ba45SMiklos Szeredi 
2059334f485dSMiklos Szeredi 	res = fuse_dev_init();
2060334f485dSMiklos Szeredi 	if (res)
2061334f485dSMiklos Szeredi 		goto err_fs_cleanup;
2062334f485dSMiklos Szeredi 
2063f543f253SMiklos Szeredi 	res = fuse_sysfs_init();
2064f543f253SMiklos Szeredi 	if (res)
2065f543f253SMiklos Szeredi 		goto err_dev_cleanup;
2066f543f253SMiklos Szeredi 
2067bafa9654SMiklos Szeredi 	res = fuse_ctl_init();
2068bafa9654SMiklos Szeredi 	if (res)
2069bafa9654SMiklos Szeredi 		goto err_sysfs_cleanup;
2070bafa9654SMiklos Szeredi 
2071487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_bgreq);
2072487ea5afSCsaba Henk 	sanitize_global_limit(&max_user_congthresh);
2073487ea5afSCsaba Henk 
2074d8a5ba45SMiklos Szeredi 	return 0;
2075d8a5ba45SMiklos Szeredi 
2076bafa9654SMiklos Szeredi  err_sysfs_cleanup:
2077bafa9654SMiklos Szeredi 	fuse_sysfs_cleanup();
2078f543f253SMiklos Szeredi  err_dev_cleanup:
2079f543f253SMiklos Szeredi 	fuse_dev_cleanup();
2080334f485dSMiklos Szeredi  err_fs_cleanup:
2081334f485dSMiklos Szeredi 	fuse_fs_cleanup();
2082d8a5ba45SMiklos Szeredi  err:
2083d8a5ba45SMiklos Szeredi 	return res;
2084d8a5ba45SMiklos Szeredi }
2085d8a5ba45SMiklos Szeredi 
fuse_exit(void)2086d8a5ba45SMiklos Szeredi static void __exit fuse_exit(void)
2087d8a5ba45SMiklos Szeredi {
2088f2294482SKirill Smelkov 	pr_debug("exit\n");
2089d8a5ba45SMiklos Szeredi 
2090bafa9654SMiklos Szeredi 	fuse_ctl_cleanup();
2091f543f253SMiklos Szeredi 	fuse_sysfs_cleanup();
2092d8a5ba45SMiklos Szeredi 	fuse_fs_cleanup();
2093334f485dSMiklos Szeredi 	fuse_dev_cleanup();
2094d8a5ba45SMiklos Szeredi }
2095d8a5ba45SMiklos Szeredi 
2096d8a5ba45SMiklos Szeredi module_init(fuse_init);
2097d8a5ba45SMiklos Szeredi module_exit(fuse_exit);
2098