blob: 6b3beda16c1ba0a4394d30db7671edd73f2cc034 [file] [log] [blame]
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/file.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070014#include <linux/seq_file.h>
15#include <linux/init.h>
16#include <linux/module.h>
Csaba Henk487ea5a2009-08-26 19:17:22 +020017#include <linux/moduleparam.h>
David Howellsc30da2e2019-03-25 16:38:31 +000018#include <linux/fs_context.h>
19#include <linux/fs_parser.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070020#include <linux/statfs.h>
Miklos Szeredi9c8ef562006-06-25 05:48:55 -070021#include <linux/random.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040022#include <linux/sched.h>
Miklos Szeredidbd561d2008-07-25 01:49:00 -070023#include <linux/exportfs.h>
Seth Forshee60bcc882016-08-29 08:46:37 -050024#include <linux/posix_acl.h>
Seth Forshee0b6e9ea2014-07-02 16:29:19 -050025#include <linux/pid_namespace.h>
Jeff Laytonc086df42022-01-10 18:52:52 -050026#include <uapi/linux/magic.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070027
28MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
29MODULE_DESCRIPTION("Filesystem in Userspace");
30MODULE_LICENSE("GPL");
31
Christoph Lametere18b8902006-12-06 20:33:20 -080032static struct kmem_cache *fuse_inode_cachep;
Miklos Szeredibafa9652006-06-25 05:48:51 -070033struct list_head fuse_conn_list;
34DEFINE_MUTEX(fuse_mutex);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070035
Kees Cooke4dca7b2017-10-17 19:04:42 -070036static int set_global_limit(const char *val, const struct kernel_param *kp);
Csaba Henk487ea5a2009-08-26 19:17:22 +020037
Csaba Henk79a9d992009-08-26 19:18:24 +020038unsigned max_user_bgreq;
Csaba Henk487ea5a2009-08-26 19:17:22 +020039module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
40 &max_user_bgreq, 0644);
41__MODULE_PARM_TYPE(max_user_bgreq, "uint");
42MODULE_PARM_DESC(max_user_bgreq,
43 "Global limit for the maximum number of backgrounded requests an "
44 "unprivileged user can set");
45
Csaba Henk79a9d992009-08-26 19:18:24 +020046unsigned max_user_congthresh;
Csaba Henk487ea5a2009-08-26 19:17:22 +020047module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
48 &max_user_congthresh, 0644);
49__MODULE_PARM_TYPE(max_user_congthresh, "uint");
50MODULE_PARM_DESC(max_user_congthresh,
51 "Global limit for the maximum congestion threshold an "
52 "unprivileged user can set");
53
Miklos Szeredid1875db2008-02-08 04:21:43 -080054#define FUSE_DEFAULT_BLKSIZE 512
55
Csaba Henk7a6d3c82009-07-01 17:28:41 -070056/** Maximum number of outstanding background requests */
57#define FUSE_DEFAULT_MAX_BACKGROUND 12
58
59/** Congestion starts at 75% of maximum */
60#define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
61
David Howellsc30da2e2019-03-25 16:38:31 +000062#ifdef CONFIG_BLOCK
63static struct file_system_type fuseblk_fs_type;
64#endif
65
Randy Dunlapa2daff62011-05-31 14:09:00 -070066struct fuse_forget_link *fuse_alloc_forget(void)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +010067{
Khazhismel Kumykovdc69e98c2019-09-17 12:35:33 -070068 return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +010069}
70
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070071static struct inode *fuse_alloc_inode(struct super_block *sb)
72{
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070073 struct fuse_inode *fi;
74
Muchun Songfd60b282022-03-22 14:41:03 -070075 fi = alloc_inode_sb(sb, fuse_inode_cachep, GFP_KERNEL);
zhangliguang9031a692019-05-06 16:52:25 +080076 if (!fi)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070077 return NULL;
78
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070079 fi->i_time = 0;
Miklos Szeredi2f1e8192018-10-15 15:43:06 +020080 fi->inval_mask = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070081 fi->nodeid = 0;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070082 fi->nlookup = 0;
John Muirfbee36b2007-11-28 16:22:02 -080083 fi->attr_version = 0;
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +040084 fi->orig_ino = 0;
Feng Shuo4582a4a2013-01-15 11:23:28 +080085 fi->state = 0;
Miklos Szeredi5c672ab2016-06-30 13:10:49 +020086 mutex_init(&fi->mutex);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +030087 spin_lock_init(&fi->lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +010088 fi->forget = fuse_alloc_forget();
Vivek Goyalc2d0ad02020-08-19 18:19:51 -040089 if (!fi->forget)
90 goto out_free;
91
92 if (IS_ENABLED(CONFIG_FUSE_DAX) && !fuse_dax_inode_alloc(sb, fi))
93 goto out_free_forget;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070094
zhangliguang9031a692019-05-06 16:52:25 +080095 return &fi->inode;
Vivek Goyalc2d0ad02020-08-19 18:19:51 -040096
97out_free_forget:
98 kfree(fi->forget);
99out_free:
100 kmem_cache_free(fuse_inode_cachep, fi);
101 return NULL;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700102}
103
Al Viro9baf28b2019-04-15 19:37:09 -0400104static void fuse_free_inode(struct inode *inode)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700105{
Miklos Szeredie5e55582005-09-09 13:10:28 -0700106 struct fuse_inode *fi = get_fuse_inode(inode);
Al Viro9baf28b2019-04-15 19:37:09 -0400107
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200108 mutex_destroy(&fi->mutex);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100109 kfree(fi->forget);
Vivek Goyalc2d0ad02020-08-19 18:19:51 -0400110#ifdef CONFIG_FUSE_DAX
111 kfree(fi->dax);
112#endif
Al Viro9baf28b2019-04-15 19:37:09 -0400113 kmem_cache_free(fuse_inode_cachep, fi);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700114}
115
Al Virob57922d2010-06-07 14:34:48 -0400116static void fuse_evict_inode(struct inode *inode)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700117{
Al Viro9baf28b2019-04-15 19:37:09 -0400118 struct fuse_inode *fi = get_fuse_inode(inode);
119
Miklos Szeredi5c791fe2021-10-22 17:03:01 +0200120 /* Will write inode on close/munmap and in all other dirtiers */
121 WARN_ON(inode->i_state & I_DIRTY_INODE);
122
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700123 truncate_inode_pages_final(&inode->i_data);
Jan Karadbd57682012-05-03 14:48:02 +0200124 clear_inode(inode);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800125 if (inode->i_sb->s_flags & SB_ACTIVE) {
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700126 struct fuse_conn *fc = get_fuse_conn(inode);
Vivek Goyalc2d0ad02020-08-19 18:19:51 -0400127
128 if (FUSE_IS_DAX(inode))
129 fuse_dax_inode_cleanup(inode);
Max Reitz1866d772020-09-09 17:52:17 +0200130 if (fi->nlookup) {
131 fuse_queue_forget(fc, fi->forget, fi->nodeid,
132 fi->nlookup);
133 fi->forget = NULL;
134 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700135 }
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100136 if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
Al Viro9baf28b2019-04-15 19:37:09 -0400137 WARN_ON(!list_empty(&fi->write_files));
138 WARN_ON(!list_empty(&fi->queued_writes));
139 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700140}
141
Miklos Szeredi84c21502021-08-04 13:22:58 +0200142static int fuse_reconfigure(struct fs_context *fsc)
Miklos Szeredi71421252006-06-25 05:48:52 -0700143{
Miklos Szeredi84c21502021-08-04 13:22:58 +0200144 struct super_block *sb = fsc->root->d_sb;
Miklos Szeredi0189a2d2020-07-14 14:45:41 +0200145
Theodore Ts'o02b99842014-03-13 10:14:33 -0400146 sync_filesystem(sb);
Miklos Szeredi84c21502021-08-04 13:22:58 +0200147 if (fsc->sb_flags & SB_MANDLOCK)
Miklos Szeredi71421252006-06-25 05:48:52 -0700148 return -EINVAL;
149
150 return 0;
151}
152
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +0400153/*
154 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
155 * so that it will fit.
156 */
157static ino_t fuse_squash_ino(u64 ino64)
158{
159 ino_t ino = (ino_t) ino64;
160 if (sizeof(ino_t) < sizeof(u64))
161 ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
162 return ino;
163}
164
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700165void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200166 u64 attr_valid, u32 cache_mask)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700167{
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700168 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szerediebc14c42007-10-16 23:31:03 -0700169 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700170
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300171 lockdep_assert_held(&fi->lock);
172
Kirill Tkhai4510d862018-11-09 13:33:17 +0300173 fi->attr_version = atomic64_inc_return(&fc->attr_version);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700174 fi->i_time = attr_valid;
Miklos Szeredi2f1e8192018-10-15 15:43:06 +0200175 WRITE_ONCE(fi->inval_mask, 0);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700176
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +0400177 inode->i_ino = fuse_squash_ino(attr->ino);
Miklos Szerediebc14c42007-10-16 23:31:03 -0700178 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200179 set_nlink(inode, attr->nlink);
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600180 inode->i_uid = make_kuid(fc->user_ns, attr->uid);
181 inode->i_gid = make_kgid(fc->user_ns, attr->gid);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700182 inode->i_blocks = attr->blocks;
Miklos Szeredi47912eaa2022-07-21 16:06:18 +0200183
184 /* Sanitize nsecs */
185 attr->atimensec = min_t(u32, attr->atimensec, NSEC_PER_SEC - 1);
186 attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
187 attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
188
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700189 inode->i_atime.tv_sec = attr->atime;
190 inode->i_atime.tv_nsec = attr->atimensec;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400191 /* mtime from server may be stale due to local buffered write */
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200192 if (!(cache_mask & STATX_MTIME)) {
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400193 inode->i_mtime.tv_sec = attr->mtime;
194 inode->i_mtime.tv_nsec = attr->mtimensec;
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200195 }
196 if (!(cache_mask & STATX_CTIME)) {
Maxim Patlasov31f32672014-04-28 14:19:24 +0200197 inode->i_ctime.tv_sec = attr->ctime;
198 inode->i_ctime.tv_nsec = attr->ctimensec;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400199 }
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700200
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700201 if (attr->blksize != 0)
202 inode->i_blkbits = ilog2(attr->blksize);
203 else
204 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
205
Miklos Szerediebc14c42007-10-16 23:31:03 -0700206 /*
207 * Don't set the sticky bit in i_mode, unless we want the VFS
208 * to check permissions. This prevents failures due to the
209 * check in may_delete().
210 */
211 fi->orig_i_mode = inode->i_mode;
Miklos Szeredi29433a22016-10-01 07:32:32 +0200212 if (!fc->default_permissions)
Miklos Szerediebc14c42007-10-16 23:31:03 -0700213 inode->i_mode &= ~S_ISVTX;
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +0400214
215 fi->orig_ino = attr->ino;
Vivek Goyal9d769e62020-10-09 14:15:12 -0400216
217 /*
218 * We are refreshing inode data and it is possible that another
219 * client set suid/sgid or security.capability xattr. So clear
220 * S_NOSEC. Ideally, we could have cleared it only if suid/sgid
221 * was set or if security.capability xattr was set. But we don't
222 * know if security.capability has been set or not. So clear it
223 * anyway. Its less efficient but should be safe.
224 */
225 inode->i_flags &= ~S_NOSEC;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700226}
227
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200228u32 fuse_get_cache_mask(struct inode *inode)
229{
230 struct fuse_conn *fc = get_fuse_conn(inode);
231
232 if (!fc->writeback_cache || !S_ISREG(inode->i_mode))
233 return 0;
234
235 return STATX_MTIME | STATX_CTIME | STATX_SIZE;
236}
237
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700238void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
239 u64 attr_valid, u64 attr_version)
240{
241 struct fuse_conn *fc = get_fuse_conn(inode);
242 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200243 u32 cache_mask;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700244 loff_t oldsize;
Arnd Bergmanna64ba102018-07-13 16:35:10 +0200245 struct timespec64 old_mtime;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700246
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300247 spin_lock(&fi->lock);
Miklos Szeredi04d82db2021-10-22 17:03:03 +0200248 /*
249 * In case of writeback_cache enabled, writes update mtime, ctime and
250 * may update i_size. In these cases trust the cached value in the
251 * inode.
252 */
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200253 cache_mask = fuse_get_cache_mask(inode);
254 if (cache_mask & STATX_SIZE)
Miklos Szeredi04d82db2021-10-22 17:03:03 +0200255 attr->size = i_size_read(inode);
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200256
257 if (cache_mask & STATX_MTIME) {
Miklos Szeredi04d82db2021-10-22 17:03:03 +0200258 attr->mtime = inode->i_mtime.tv_sec;
259 attr->mtimensec = inode->i_mtime.tv_nsec;
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200260 }
261 if (cache_mask & STATX_CTIME) {
Miklos Szeredi04d82db2021-10-22 17:03:03 +0200262 attr->ctime = inode->i_ctime.tv_sec;
263 attr->ctimensec = inode->i_ctime.tv_nsec;
264 }
265
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400266 if ((attr_version != 0 && fi->attr_version > attr_version) ||
267 test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300268 spin_unlock(&fi->lock);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700269 return;
270 }
271
Arnd Bergmanna64ba102018-07-13 16:35:10 +0200272 old_mtime = inode->i_mtime;
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200273 fuse_change_attributes_common(inode, attr, attr_valid, cache_mask);
Miklos Szerediebc14c42007-10-16 23:31:03 -0700274
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700275 oldsize = inode->i_size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400276 /*
277 * In case of writeback_cache enabled, the cached writes beyond EOF
278 * extend local i_size without keeping userspace server in sync. So,
279 * attr->size coming from server can be stale. We cannot trust it.
280 */
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200281 if (!(cache_mask & STATX_SIZE))
Pavel Emelyanov83732002013-10-10 17:10:46 +0400282 i_size_write(inode, attr->size);
Kirill Tkhaif15ecfe2018-11-09 13:33:22 +0300283 spin_unlock(&fi->lock);
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700284
Miklos Szeredi4b52f052021-10-22 17:03:03 +0200285 if (!cache_mask && S_ISREG(inode->i_mode)) {
Brian Fostereed21792012-07-16 15:23:49 -0400286 bool inval = false;
287
288 if (oldsize != attr->size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700289 truncate_pagecache(inode, attr->size);
Kirill Smelkovad2ba642019-03-27 11:14:15 +0000290 if (!fc->explicit_inval_data)
291 inval = true;
Brian Fostereed21792012-07-16 15:23:49 -0400292 } else if (fc->auto_inval_data) {
Arnd Bergmanna64ba102018-07-13 16:35:10 +0200293 struct timespec64 new_mtime = {
Brian Fostereed21792012-07-16 15:23:49 -0400294 .tv_sec = attr->mtime,
295 .tv_nsec = attr->mtimensec,
296 };
297
298 /*
299 * Auto inval mode also checks and invalidates if mtime
300 * has changed.
301 */
Arnd Bergmanna64ba102018-07-13 16:35:10 +0200302 if (!timespec64_equal(&old_mtime, &new_mtime))
Brian Fostereed21792012-07-16 15:23:49 -0400303 inval = true;
304 }
305
306 if (inval)
307 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700308 }
Jeffle Xuc3cb6f92021-11-25 15:05:29 +0800309
310 if (IS_ENABLED(CONFIG_FUSE_DAX))
311 fuse_dax_dontcache(inode, attr->flags);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700312}
313
314static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
315{
316 inode->i_mode = attr->mode & S_IFMT;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700317 inode->i_size = attr->size;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400318 inode->i_mtime.tv_sec = attr->mtime;
319 inode->i_mtime.tv_nsec = attr->mtimensec;
Maxim Patlasov31f32672014-04-28 14:19:24 +0200320 inode->i_ctime.tv_sec = attr->ctime;
321 inode->i_ctime.tv_nsec = attr->ctimensec;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700322 if (S_ISREG(inode->i_mode)) {
323 fuse_init_common(inode);
Jeffle Xu93a497b2021-11-25 15:05:27 +0800324 fuse_init_file_inode(inode, attr->flags);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700325 } else if (S_ISDIR(inode->i_mode))
326 fuse_init_dir(inode);
327 else if (S_ISLNK(inode->i_mode))
328 fuse_init_symlink(inode);
329 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
330 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
331 fuse_init_common(inode);
332 init_special_inode(inode, inode->i_mode,
333 new_decode_dev(attr->rdev));
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800334 } else
335 BUG();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700336}
337
Max Reitzfcee2162020-05-06 17:44:12 +0200338static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700339{
Miklos Szeredib48badf2008-04-30 00:54:44 -0700340 u64 nodeid = *(u64 *) _nodeidp;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700341 if (get_node_id(inode) == nodeid)
342 return 1;
343 else
344 return 0;
345}
346
347static int fuse_inode_set(struct inode *inode, void *_nodeidp)
348{
Miklos Szeredib48badf2008-04-30 00:54:44 -0700349 u64 nodeid = *(u64 *) _nodeidp;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700350 get_fuse_inode(inode)->nodeid = nodeid;
351 return 0;
352}
353
Miklos Szeredib48badf2008-04-30 00:54:44 -0700354struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700355 int generation, struct fuse_attr *attr,
356 u64 attr_valid, u64 attr_version)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700357{
358 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700359 struct fuse_inode *fi;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700360 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700361
Max Reitzbf109c62020-04-21 14:47:15 +0200362 /*
363 * Auto mount points get their node id from the submount root, which is
364 * not a unique identifier within this filesystem.
365 *
366 * To avoid conflicts, do not place submount points into the inode hash
367 * table.
368 */
369 if (fc->auto_submounts && (attr->flags & FUSE_ATTR_SUBMOUNT) &&
370 S_ISDIR(attr->mode)) {
371 inode = new_inode(sb);
372 if (!inode)
373 return NULL;
374
375 fuse_init_inode(inode, attr);
376 get_fuse_inode(inode)->nodeid = nodeid;
377 inode->i_flags |= S_AUTOMOUNT;
378 goto done;
379 }
380
381retry:
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700382 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
383 if (!inode)
384 return NULL;
385
386 if ((inode->i_state & I_NEW)) {
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400387 inode->i_flags |= S_NOATIME;
Maxim Patlasovd31433c2014-04-28 14:19:21 +0200388 if (!fc->writeback_cache || !S_ISREG(attr->mode))
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400389 inode->i_flags |= S_NOCMTIME;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700390 inode->i_generation = generation;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700391 fuse_init_inode(inode, attr);
392 unlock_new_inode(inode);
Amir Goldstein15db1682021-06-21 14:03:53 +0300393 } else if (fuse_stale_inode(inode, generation, attr)) {
394 /* nodeid was reused, any I/O on the old inode should fail */
Miklos Szeredi5d069db2020-12-10 15:33:14 +0100395 fuse_make_bad(inode);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700396 iput(inode);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700397 goto retry;
398 }
Max Reitzbf109c62020-04-21 14:47:15 +0200399done:
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700400 fi = get_fuse_inode(inode);
Kirill Tkhaic9d8f5f2018-11-09 13:33:27 +0300401 spin_lock(&fi->lock);
Miklos Szeredi1729a162008-11-26 12:03:54 +0100402 fi->nlookup++;
Kirill Tkhaic9d8f5f2018-11-09 13:33:27 +0300403 spin_unlock(&fi->lock);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700404 fuse_change_attributes(inode, attr, attr_valid, attr_version);
405
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700406 return inode;
407}
408
Max Reitzfcee2162020-05-06 17:44:12 +0200409struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
410 struct fuse_mount **fm)
411{
412 struct fuse_mount *fm_iter;
413 struct inode *inode;
414
415 WARN_ON(!rwsem_is_locked(&fc->killsb));
416 list_for_each_entry(fm_iter, &fc->mounts, fc_entry) {
417 if (!fm_iter->sb)
418 continue;
419
420 inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &nodeid);
421 if (inode) {
422 if (fm)
423 *fm = fm_iter;
424 return inode;
425 }
426 }
427
428 return NULL;
429}
430
431int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
John Muir3b463ae2009-05-31 11:13:57 -0400432 loff_t offset, loff_t len)
433{
Miklos Szeredi5ddd9ce2020-05-19 14:50:38 +0200434 struct fuse_inode *fi;
John Muir3b463ae2009-05-31 11:13:57 -0400435 struct inode *inode;
436 pgoff_t pg_start;
437 pgoff_t pg_end;
438
Max Reitzfcee2162020-05-06 17:44:12 +0200439 inode = fuse_ilookup(fc, nodeid, NULL);
John Muir3b463ae2009-05-31 11:13:57 -0400440 if (!inode)
441 return -ENOENT;
442
Miklos Szeredi5ddd9ce2020-05-19 14:50:38 +0200443 fi = get_fuse_inode(inode);
444 spin_lock(&fi->lock);
445 fi->attr_version = atomic64_inc_return(&fc->attr_version);
446 spin_unlock(&fi->lock);
447
John Muir3b463ae2009-05-31 11:13:57 -0400448 fuse_invalidate_attr(inode);
Seth Forshee60bcc882016-08-29 08:46:37 -0500449 forget_all_cached_acls(inode);
John Muir3b463ae2009-05-31 11:13:57 -0400450 if (offset >= 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300451 pg_start = offset >> PAGE_SHIFT;
John Muir3b463ae2009-05-31 11:13:57 -0400452 if (len <= 0)
453 pg_end = -1;
454 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300455 pg_end = (offset + len - 1) >> PAGE_SHIFT;
John Muir3b463ae2009-05-31 11:13:57 -0400456 invalidate_inode_pages2_range(inode->i_mapping,
457 pg_start, pg_end);
458 }
459 iput(inode);
460 return 0;
461}
462
Miklos Szeredi63576c12018-07-26 16:13:11 +0200463bool fuse_lock_inode(struct inode *inode)
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200464{
Miklos Szeredi63576c12018-07-26 16:13:11 +0200465 bool locked = false;
466
467 if (!get_fuse_conn(inode)->parallel_dirops) {
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200468 mutex_lock(&get_fuse_inode(inode)->mutex);
Miklos Szeredi63576c12018-07-26 16:13:11 +0200469 locked = true;
470 }
471
472 return locked;
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200473}
474
Miklos Szeredi63576c12018-07-26 16:13:11 +0200475void fuse_unlock_inode(struct inode *inode, bool locked)
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200476{
Miklos Szeredi63576c12018-07-26 16:13:11 +0200477 if (locked)
Miklos Szeredi5c672ab2016-06-30 13:10:49 +0200478 mutex_unlock(&get_fuse_inode(inode)->mutex);
479}
480
Al Viro42faad92008-04-24 07:21:56 -0400481static void fuse_umount_begin(struct super_block *sb)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800482{
Vivek Goyal15c8e722019-05-06 15:35:43 -0400483 struct fuse_conn *fc = get_fuse_conn_super(sb);
484
Daniil Lunev247861c2022-07-27 16:44:25 +1000485 if (fc->no_force_umount)
486 return;
487
488 fuse_abort_conn(fc);
489
490 // Only retire block-device-based superblocks.
491 if (sb->s_bdev != NULL)
492 retire_super(sb);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800493}
494
Max Reitzfcee2162020-05-06 17:44:12 +0200495static void fuse_send_destroy(struct fuse_mount *fm)
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800496{
Max Reitzfcee2162020-05-06 17:44:12 +0200497 if (fm->fc->conn_init) {
Miklos Szeredi1ccd1ea2019-09-10 15:04:09 +0200498 FUSE_ARGS(args);
499
500 args.opcode = FUSE_DESTROY;
501 args.force = true;
502 args.nocreds = true;
Max Reitzfcee2162020-05-06 17:44:12 +0200503 fuse_simple_request(fm, &args);
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800504 }
505}
506
Miklos Szeredie5e55582005-09-09 13:10:28 -0700507static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
508{
509 stbuf->f_type = FUSE_SUPER_MAGIC;
510 stbuf->f_bsize = attr->bsize;
Miklos Szeredide5f1202006-01-06 00:19:37 -0800511 stbuf->f_frsize = attr->frsize;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700512 stbuf->f_blocks = attr->blocks;
513 stbuf->f_bfree = attr->bfree;
514 stbuf->f_bavail = attr->bavail;
515 stbuf->f_files = attr->files;
516 stbuf->f_ffree = attr->ffree;
517 stbuf->f_namelen = attr->namelen;
518 /* fsid is left zero */
519}
520
David Howells726c3342006-06-23 02:02:58 -0700521static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700522{
David Howells726c3342006-06-23 02:02:58 -0700523 struct super_block *sb = dentry->d_sb;
Max Reitzfcee2162020-05-06 17:44:12 +0200524 struct fuse_mount *fm = get_fuse_mount_super(sb);
Miklos Szeredi70781872014-12-12 09:49:05 +0100525 FUSE_ARGS(args);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700526 struct fuse_statfs_out outarg;
527 int err;
528
Max Reitzfcee2162020-05-06 17:44:12 +0200529 if (!fuse_allow_current_process(fm->fc)) {
Miklos Szeredie57ac682007-10-18 03:06:58 -0700530 buf->f_type = FUSE_SUPER_MAGIC;
531 return 0;
532 }
533
Miklos Szeredide5f1202006-01-06 00:19:37 -0800534 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredid5b48542019-09-10 15:04:08 +0200535 args.in_numargs = 0;
536 args.opcode = FUSE_STATFS;
537 args.nodeid = get_node_id(d_inode(dentry));
538 args.out_numargs = 1;
539 args.out_args[0].size = sizeof(outarg);
540 args.out_args[0].value = &outarg;
Max Reitzfcee2162020-05-06 17:44:12 +0200541 err = fuse_simple_request(fm, &args);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700542 if (!err)
543 convert_fuse_statfs(buf, &outarg.st);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700544 return err;
545}
546
Miklos Szeredi660585b2021-09-01 12:39:02 +0200547static struct fuse_sync_bucket *fuse_sync_bucket_alloc(void)
548{
549 struct fuse_sync_bucket *bucket;
550
551 bucket = kzalloc(sizeof(*bucket), GFP_KERNEL | __GFP_NOFAIL);
552 if (bucket) {
553 init_waitqueue_head(&bucket->waitq);
554 /* Initial active count */
555 atomic_set(&bucket->count, 1);
556 }
557 return bucket;
558}
559
560static void fuse_sync_fs_writes(struct fuse_conn *fc)
561{
562 struct fuse_sync_bucket *bucket, *new_bucket;
563 int count;
564
565 new_bucket = fuse_sync_bucket_alloc();
566 spin_lock(&fc->lock);
567 bucket = rcu_dereference_protected(fc->curr_bucket, 1);
568 count = atomic_read(&bucket->count);
569 WARN_ON(count < 1);
570 /* No outstanding writes? */
571 if (count == 1) {
572 spin_unlock(&fc->lock);
573 kfree(new_bucket);
574 return;
575 }
576
577 /*
578 * Completion of new bucket depends on completion of this bucket, so add
579 * one more count.
580 */
581 atomic_inc(&new_bucket->count);
582 rcu_assign_pointer(fc->curr_bucket, new_bucket);
583 spin_unlock(&fc->lock);
584 /*
585 * Drop initial active count. At this point if all writes in this and
586 * ancestor buckets complete, the count will go to zero and this task
587 * will be woken up.
588 */
589 atomic_dec(&bucket->count);
590
591 wait_event(bucket->waitq, atomic_read(&bucket->count) == 0);
592
593 /* Drop temp count on descendant bucket */
594 fuse_sync_bucket_dec(new_bucket);
595 kfree_rcu(bucket, rcu);
596}
597
Greg Kurz2d82ab22021-05-20 17:46:54 +0200598static int fuse_sync_fs(struct super_block *sb, int wait)
599{
600 struct fuse_mount *fm = get_fuse_mount_super(sb);
601 struct fuse_conn *fc = fm->fc;
602 struct fuse_syncfs_in inarg;
603 FUSE_ARGS(args);
604 int err;
605
606 /*
607 * Userspace cannot handle the wait == 0 case. Avoid a
608 * gratuitous roundtrip.
609 */
610 if (!wait)
611 return 0;
612
613 /* The filesystem is being unmounted. Nothing to do. */
614 if (!sb->s_root)
615 return 0;
616
617 if (!fc->sync_fs)
618 return 0;
619
Miklos Szeredi660585b2021-09-01 12:39:02 +0200620 fuse_sync_fs_writes(fc);
621
Greg Kurz2d82ab22021-05-20 17:46:54 +0200622 memset(&inarg, 0, sizeof(inarg));
623 args.in_numargs = 1;
624 args.in_args[0].size = sizeof(inarg);
625 args.in_args[0].value = &inarg;
626 args.opcode = FUSE_SYNCFS;
627 args.nodeid = get_node_id(sb->s_root->d_inode);
628 args.out_numargs = 0;
629
630 err = fuse_simple_request(fm, &args);
631 if (err == -ENOSYS) {
632 fc->sync_fs = 0;
633 err = 0;
634 }
635
636 return err;
637}
638
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700639enum {
David Howellsc30da2e2019-03-25 16:38:31 +0000640 OPT_SOURCE,
641 OPT_SUBTYPE,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700642 OPT_FD,
643 OPT_ROOTMODE,
644 OPT_USER_ID,
Miklos Szeredi87729a52005-09-09 13:10:34 -0700645 OPT_GROUP_ID,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700646 OPT_DEFAULT_PERMISSIONS,
647 OPT_ALLOW_OTHER,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700648 OPT_MAX_READ,
Miklos Szeredid8091612006-12-06 20:35:48 -0800649 OPT_BLKSIZE,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700650 OPT_ERR
651};
652
Al Virod7167b12019-09-07 07:23:15 -0400653static const struct fs_parameter_spec fuse_fs_parameters[] = {
David Howellsc30da2e2019-03-25 16:38:31 +0000654 fsparam_string ("source", OPT_SOURCE),
655 fsparam_u32 ("fd", OPT_FD),
656 fsparam_u32oct ("rootmode", OPT_ROOTMODE),
657 fsparam_u32 ("user_id", OPT_USER_ID),
658 fsparam_u32 ("group_id", OPT_GROUP_ID),
659 fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS),
660 fsparam_flag ("allow_other", OPT_ALLOW_OTHER),
661 fsparam_u32 ("max_read", OPT_MAX_READ),
662 fsparam_u32 ("blksize", OPT_BLKSIZE),
David Howellsc7eb6862019-03-25 16:38:31 +0000663 fsparam_string ("subtype", OPT_SUBTYPE),
David Howellsc30da2e2019-03-25 16:38:31 +0000664 {}
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700665};
666
Miklos Szeredi84c21502021-08-04 13:22:58 +0200667static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param)
Miklos Szeredi233a01f2014-07-07 15:28:51 +0200668{
David Howellsc30da2e2019-03-25 16:38:31 +0000669 struct fs_parse_result result;
Miklos Szeredi84c21502021-08-04 13:22:58 +0200670 struct fuse_fs_context *ctx = fsc->fs_private;
David Howellsc30da2e2019-03-25 16:38:31 +0000671 int opt;
Miklos Szeredi233a01f2014-07-07 15:28:51 +0200672
Miklos Szeredi84c21502021-08-04 13:22:58 +0200673 if (fsc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
Miklos Szeredib3309662020-07-14 14:45:41 +0200674 /*
675 * Ignore options coming from mount(MS_REMOUNT) for backward
676 * compatibility.
677 */
Miklos Szeredi84c21502021-08-04 13:22:58 +0200678 if (fsc->oldapi)
Miklos Szeredib3309662020-07-14 14:45:41 +0200679 return 0;
680
Miklos Szeredi84c21502021-08-04 13:22:58 +0200681 return invalfc(fsc, "No changes allowed in reconfigure");
Miklos Szeredib3309662020-07-14 14:45:41 +0200682 }
Miklos Szeredie8b20a42020-07-14 14:45:41 +0200683
Miklos Szeredi84c21502021-08-04 13:22:58 +0200684 opt = fs_parse(fsc, fuse_fs_parameters, param, &result);
David Howellsc30da2e2019-03-25 16:38:31 +0000685 if (opt < 0)
686 return opt;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700687
David Howellsc30da2e2019-03-25 16:38:31 +0000688 switch (opt) {
689 case OPT_SOURCE:
Miklos Szeredi84c21502021-08-04 13:22:58 +0200690 if (fsc->source)
691 return invalfc(fsc, "Multiple sources specified");
692 fsc->source = param->string;
David Howellsc30da2e2019-03-25 16:38:31 +0000693 param->string = NULL;
694 break;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700695
David Howellsc30da2e2019-03-25 16:38:31 +0000696 case OPT_SUBTYPE:
697 if (ctx->subtype)
Miklos Szeredi84c21502021-08-04 13:22:58 +0200698 return invalfc(fsc, "Multiple subtypes specified");
David Howellsc30da2e2019-03-25 16:38:31 +0000699 ctx->subtype = param->string;
700 param->string = NULL;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700701 return 0;
702
David Howellsc30da2e2019-03-25 16:38:31 +0000703 case OPT_FD:
704 ctx->fd = result.uint_32;
zhengbincabdb4f2020-01-14 20:39:45 +0800705 ctx->fd_present = true;
David Howellsc30da2e2019-03-25 16:38:31 +0000706 break;
707
708 case OPT_ROOTMODE:
709 if (!fuse_valid_type(result.uint_32))
Miklos Szeredi84c21502021-08-04 13:22:58 +0200710 return invalfc(fsc, "Invalid rootmode");
David Howellsc30da2e2019-03-25 16:38:31 +0000711 ctx->rootmode = result.uint_32;
zhengbincabdb4f2020-01-14 20:39:45 +0800712 ctx->rootmode_present = true;
David Howellsc30da2e2019-03-25 16:38:31 +0000713 break;
714
715 case OPT_USER_ID:
Miklos Szeredi84c21502021-08-04 13:22:58 +0200716 ctx->user_id = make_kuid(fsc->user_ns, result.uint_32);
David Howellsc30da2e2019-03-25 16:38:31 +0000717 if (!uid_valid(ctx->user_id))
Miklos Szeredi84c21502021-08-04 13:22:58 +0200718 return invalfc(fsc, "Invalid user_id");
zhengbincabdb4f2020-01-14 20:39:45 +0800719 ctx->user_id_present = true;
David Howellsc30da2e2019-03-25 16:38:31 +0000720 break;
721
722 case OPT_GROUP_ID:
Miklos Szeredi84c21502021-08-04 13:22:58 +0200723 ctx->group_id = make_kgid(fsc->user_ns, result.uint_32);
David Howellsc30da2e2019-03-25 16:38:31 +0000724 if (!gid_valid(ctx->group_id))
Miklos Szeredi84c21502021-08-04 13:22:58 +0200725 return invalfc(fsc, "Invalid group_id");
zhengbincabdb4f2020-01-14 20:39:45 +0800726 ctx->group_id_present = true;
David Howellsc30da2e2019-03-25 16:38:31 +0000727 break;
728
729 case OPT_DEFAULT_PERMISSIONS:
zhengbincabdb4f2020-01-14 20:39:45 +0800730 ctx->default_permissions = true;
David Howellsc30da2e2019-03-25 16:38:31 +0000731 break;
732
733 case OPT_ALLOW_OTHER:
zhengbincabdb4f2020-01-14 20:39:45 +0800734 ctx->allow_other = true;
David Howellsc30da2e2019-03-25 16:38:31 +0000735 break;
736
737 case OPT_MAX_READ:
738 ctx->max_read = result.uint_32;
739 break;
740
741 case OPT_BLKSIZE:
742 if (!ctx->is_bdev)
Miklos Szeredi84c21502021-08-04 13:22:58 +0200743 return invalfc(fsc, "blksize only supported for fuseblk");
David Howellsc30da2e2019-03-25 16:38:31 +0000744 ctx->blksize = result.uint_32;
745 break;
746
747 default:
748 return -EINVAL;
749 }
750
751 return 0;
752}
753
Miklos Szeredi84c21502021-08-04 13:22:58 +0200754static void fuse_free_fsc(struct fs_context *fsc)
David Howellsc30da2e2019-03-25 16:38:31 +0000755{
Miklos Szeredi84c21502021-08-04 13:22:58 +0200756 struct fuse_fs_context *ctx = fsc->fs_private;
David Howellsc30da2e2019-03-25 16:38:31 +0000757
758 if (ctx) {
759 kfree(ctx->subtype);
760 kfree(ctx);
761 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700762}
763
Al Viro34c80b12011-12-08 21:32:45 -0500764static int fuse_show_options(struct seq_file *m, struct dentry *root)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700765{
Al Viro34c80b12011-12-08 21:32:45 -0500766 struct super_block *sb = root->d_sb;
767 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700768
Vivek Goyalf4fd4ae2020-08-19 18:19:45 -0400769 if (fc->legacy_opts_show) {
770 seq_printf(m, ",user_id=%u",
771 from_kuid_munged(fc->user_ns, fc->user_id));
772 seq_printf(m, ",group_id=%u",
773 from_kgid_munged(fc->user_ns, fc->group_id));
774 if (fc->default_permissions)
775 seq_puts(m, ",default_permissions");
776 if (fc->allow_other)
777 seq_puts(m, ",allow_other");
778 if (fc->max_read != ~0)
779 seq_printf(m, ",max_read=%u", fc->max_read);
780 if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
781 seq_printf(m, ",blksize=%lu", sb->s_blocksize);
782 }
Vivek Goyal1dd53952020-08-19 18:19:47 -0400783#ifdef CONFIG_FUSE_DAX
Jeffle Xu780b1b92021-11-25 15:05:25 +0800784 if (fc->dax_mode == FUSE_DAX_ALWAYS)
785 seq_puts(m, ",dax=always");
786 else if (fc->dax_mode == FUSE_DAX_NEVER)
787 seq_puts(m, ",dax=never");
788 else if (fc->dax_mode == FUSE_DAX_INODE_USER)
789 seq_puts(m, ",dax=inode");
Vivek Goyal1dd53952020-08-19 18:19:47 -0400790#endif
Miklos Szeredi3f22c742019-10-15 16:11:41 +0200791
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700792 return 0;
793}
794
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100795static void fuse_iqueue_init(struct fuse_iqueue *fiq,
796 const struct fuse_iqueue_ops *ops,
797 void *priv)
Miklos Szeredif88996a2015-07-01 16:26:01 +0200798{
799 memset(fiq, 0, sizeof(struct fuse_iqueue));
Eric Biggers76e43c82019-09-08 20:15:18 -0700800 spin_lock_init(&fiq->lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200801 init_waitqueue_head(&fiq->waitq);
802 INIT_LIST_HEAD(&fiq->pending);
803 INIT_LIST_HEAD(&fiq->interrupts);
804 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredie16714d2015-07-01 16:26:01 +0200805 fiq->connected = 1;
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100806 fiq->ops = ops;
807 fiq->priv = priv;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200808}
809
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +0200810static void fuse_pqueue_init(struct fuse_pqueue *fpq)
811{
Kirill Tkhaibe2ff422018-09-11 13:12:14 +0300812 unsigned int i;
813
Miklos Szeredi45a91cb2015-07-01 16:26:06 +0200814 spin_lock_init(&fpq->lock);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +0300815 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
816 INIT_LIST_HEAD(&fpq->processing[i]);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +0200817 INIT_LIST_HEAD(&fpq->io);
Miklos Szeredie96edd92015-07-01 16:26:04 +0200818 fpq->connected = 1;
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +0200819}
820
Max Reitzfcee2162020-05-06 17:44:12 +0200821void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
822 struct user_namespace *user_ns,
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100823 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700824{
Tejun Heo0d179aa2008-11-26 12:03:55 +0100825 memset(fc, 0, sizeof(*fc));
826 spin_lock_init(&fc->lock);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300827 spin_lock_init(&fc->bg_lock);
John Muir3b463ae2009-05-31 11:13:57 -0400828 init_rwsem(&fc->killsb);
Elena Reshetova095fc40a2017-03-03 11:04:05 +0200829 refcount_set(&fc->count, 1);
Miklos Szeredic36960462015-07-01 16:26:09 +0200830 atomic_set(&fc->dev_count, 1);
Tejun Heo0d179aa2008-11-26 12:03:55 +0100831 init_waitqueue_head(&fc->blocked_waitq);
Stefan Hajnocziae3aad72018-06-18 15:53:19 +0100832 fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
Tejun Heo0d179aa2008-11-26 12:03:55 +0100833 INIT_LIST_HEAD(&fc->bg_queue);
834 INIT_LIST_HEAD(&fc->entry);
Miklos Szeredicc080e92015-07-01 16:26:08 +0200835 INIT_LIST_HEAD(&fc->devices);
Tejun Heo0d179aa2008-11-26 12:03:55 +0100836 atomic_set(&fc->num_waiting, 0);
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700837 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
838 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
Miklos Szeredi75126f52019-01-24 10:40:17 +0100839 atomic64_set(&fc->khctr, 0);
Tejun Heo0d179aa2008-11-26 12:03:55 +0100840 fc->polled_files = RB_ROOT;
Maxim Patlasov0aada882013-03-21 18:02:28 +0400841 fc->blocked = 0;
Maxim Patlasov796523fb2013-03-21 18:02:15 +0400842 fc->initialized = 0;
Miklos Szeredie16714d2015-07-01 16:26:01 +0200843 fc->connected = 1;
Kirill Tkhai4510d862018-11-09 13:33:17 +0300844 atomic64_set(&fc->attr_version, 1);
Tejun Heo0d179aa2008-11-26 12:03:55 +0100845 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
Seth Forshee0b6e9ea2014-07-02 16:29:19 -0500846 fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600847 fc->user_ns = get_user_ns(user_ns);
Miklos Szeredi8a3177d2019-01-16 10:27:59 +0100848 fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
Connor Kuehla7f0d7a2021-03-18 08:52:22 -0500849 fc->max_pages_limit = FUSE_MAX_MAX_PAGES;
Max Reitzfcee2162020-05-06 17:44:12 +0200850
851 INIT_LIST_HEAD(&fc->mounts);
852 list_add(&fm->fc_entry, &fc->mounts);
853 fm->fc = fc;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700854}
Tejun Heo0d179aa2008-11-26 12:03:55 +0100855EXPORT_SYMBOL_GPL(fuse_conn_init);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700856
Miklos Szeredibafa9652006-06-25 05:48:51 -0700857void fuse_conn_put(struct fuse_conn *fc)
858{
Elena Reshetova095fc40a2017-03-03 11:04:05 +0200859 if (refcount_dec_and_test(&fc->count)) {
Stefan Hajnoczia62a8ef2018-06-12 09:41:17 +0100860 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi660585b2021-09-01 12:39:02 +0200861 struct fuse_sync_bucket *bucket;
Stefan Hajnoczia62a8ef2018-06-12 09:41:17 +0100862
Vivek Goyal1dd53952020-08-19 18:19:47 -0400863 if (IS_ENABLED(CONFIG_FUSE_DAX))
864 fuse_dax_conn_free(fc);
Stefan Hajnoczia62a8ef2018-06-12 09:41:17 +0100865 if (fiq->ops->release)
866 fiq->ops->release(fiq);
Seth Forshee0b6e9ea2014-07-02 16:29:19 -0500867 put_pid_ns(fc->pid_ns);
Eric W. Biederman8cb08322018-02-21 11:18:07 -0600868 put_user_ns(fc->user_ns);
Miklos Szeredi660585b2021-09-01 12:39:02 +0200869 bucket = rcu_dereference_protected(fc->curr_bucket, 1);
870 if (bucket) {
871 WARN_ON(atomic_read(&bucket->count) != 1);
872 kfree(bucket);
873 }
Tejun Heo43901aa2008-11-26 12:03:56 +0100874 fc->release(fc);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700875 }
Miklos Szeredibafa9652006-06-25 05:48:51 -0700876}
Tejun Heo08cbf5422009-04-14 10:54:53 +0900877EXPORT_SYMBOL_GPL(fuse_conn_put);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700878
879struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
880{
Elena Reshetova095fc40a2017-03-03 11:04:05 +0200881 refcount_inc(&fc->count);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700882 return fc;
883}
Tejun Heo08cbf5422009-04-14 10:54:53 +0900884EXPORT_SYMBOL_GPL(fuse_conn_get);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700885
Tejun Heob93f8582008-11-26 12:03:55 +0100886static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700887{
888 struct fuse_attr attr;
889 memset(&attr, 0, sizeof(attr));
890
891 attr.mode = mode;
892 attr.ino = FUSE_ROOT_ID;
Miklos Szeredi074406f2007-10-16 23:31:02 -0700893 attr.nlink = 1;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700894 return fuse_iget(sb, 1, 0, &attr, 0, 0);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700895}
896
Miklos Szeredi1729a162008-11-26 12:03:54 +0100897struct fuse_inode_handle {
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700898 u64 nodeid;
899 u32 generation;
900};
901
902static struct dentry *fuse_get_dentry(struct super_block *sb,
903 struct fuse_inode_handle *handle)
904{
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700905 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700906 struct inode *inode;
907 struct dentry *entry;
908 int err = -ESTALE;
909
910 if (handle->nodeid == 0)
911 goto out_err;
912
913 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700914 if (!inode) {
915 struct fuse_entry_out outarg;
Al Viro13983d02016-07-20 22:34:44 -0400916 const struct qstr name = QSTR_INIT(".", 1);
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700917
918 if (!fc->export_support)
919 goto out_err;
920
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700921 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
922 &inode);
923 if (err && err != -ENOENT)
924 goto out_err;
925 if (err || !inode) {
926 err = -ESTALE;
927 goto out_err;
928 }
929 err = -EIO;
930 if (get_node_id(inode) != handle->nodeid)
931 goto out_iput;
932 }
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700933 err = -ESTALE;
934 if (inode->i_generation != handle->generation)
935 goto out_iput;
936
Christoph Hellwig44003722008-08-11 15:49:04 +0200937 entry = d_obtain_alias(inode);
Al Viroc35eebe2010-12-18 11:15:22 -0500938 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700939 fuse_invalidate_entry_cache(entry);
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700940
941 return entry;
942
943 out_iput:
944 iput(inode);
945 out_err:
946 return ERR_PTR(err);
947}
948
Al Virob0b03822012-04-02 14:34:06 -0400949static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
950 struct inode *parent)
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700951{
Al Virob0b03822012-04-02 14:34:06 -0400952 int len = parent ? 6 : 3;
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700953 u64 nodeid;
954 u32 generation;
955
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530956 if (*max_len < len) {
957 *max_len = len;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900958 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +0530959 }
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700960
961 nodeid = get_fuse_inode(inode)->nodeid;
962 generation = inode->i_generation;
963
964 fh[0] = (u32)(nodeid >> 32);
965 fh[1] = (u32)(nodeid & 0xffffffff);
966 fh[2] = generation;
967
Al Virob0b03822012-04-02 14:34:06 -0400968 if (parent) {
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700969 nodeid = get_fuse_inode(parent)->nodeid;
970 generation = parent->i_generation;
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700971
972 fh[3] = (u32)(nodeid >> 32);
973 fh[4] = (u32)(nodeid & 0xffffffff);
974 fh[5] = generation;
975 }
976
977 *max_len = len;
Al Virob0b03822012-04-02 14:34:06 -0400978 return parent ? 0x82 : 0x81;
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700979}
980
981static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
982 struct fid *fid, int fh_len, int fh_type)
983{
984 struct fuse_inode_handle handle;
985
986 if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
987 return NULL;
988
989 handle.nodeid = (u64) fid->raw[0] << 32;
990 handle.nodeid |= (u64) fid->raw[1];
991 handle.generation = fid->raw[2];
992 return fuse_get_dentry(sb, &handle);
993}
994
995static struct dentry *fuse_fh_to_parent(struct super_block *sb,
996 struct fid *fid, int fh_len, int fh_type)
997{
998 struct fuse_inode_handle parent;
999
1000 if (fh_type != 0x82 || fh_len < 6)
1001 return NULL;
1002
1003 parent.nodeid = (u64) fid->raw[3] << 32;
1004 parent.nodeid |= (u64) fid->raw[4];
1005 parent.generation = fid->raw[5];
1006 return fuse_get_dentry(sb, &parent);
1007}
1008
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001009static struct dentry *fuse_get_parent(struct dentry *child)
1010{
David Howells2b0143b2015-03-17 22:25:59 +00001011 struct inode *child_inode = d_inode(child);
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001012 struct fuse_conn *fc = get_fuse_conn(child_inode);
1013 struct inode *inode;
1014 struct dentry *parent;
1015 struct fuse_entry_out outarg;
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001016 int err;
1017
1018 if (!fc->export_support)
1019 return ERR_PTR(-ESTALE);
1020
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001021 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
Al Viro80e5d1ff52021-04-15 19:46:50 -04001022 &dotdot_name, &outarg, &inode);
Christoph Hellwig44003722008-08-11 15:49:04 +02001023 if (err) {
1024 if (err == -ENOENT)
1025 return ERR_PTR(-ESTALE);
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001026 return ERR_PTR(err);
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001027 }
Christoph Hellwig44003722008-08-11 15:49:04 +02001028
1029 parent = d_obtain_alias(inode);
Al Viroc35eebe2010-12-18 11:15:22 -05001030 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001031 fuse_invalidate_entry_cache(parent);
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001032
1033 return parent;
1034}
Miklos Szeredidbd561d2008-07-25 01:49:00 -07001035
1036static const struct export_operations fuse_export_operations = {
1037 .fh_to_dentry = fuse_fh_to_dentry,
1038 .fh_to_parent = fuse_fh_to_parent,
1039 .encode_fh = fuse_encode_fh,
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001040 .get_parent = fuse_get_parent,
Miklos Szeredidbd561d2008-07-25 01:49:00 -07001041};
1042
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001043static const struct super_operations fuse_super_operations = {
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001044 .alloc_inode = fuse_alloc_inode,
Al Viro9baf28b2019-04-15 19:37:09 -04001045 .free_inode = fuse_free_inode,
Al Virob57922d2010-06-07 14:34:48 -04001046 .evict_inode = fuse_evict_inode,
Miklos Szeredi1e18bda2014-04-28 14:19:23 +02001047 .write_inode = fuse_write_inode,
Miklos Szerediead5f0b2007-05-23 13:57:54 -07001048 .drop_inode = generic_delete_inode,
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001049 .umount_begin = fuse_umount_begin,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001050 .statfs = fuse_statfs,
Greg Kurz2d82ab22021-05-20 17:46:54 +02001051 .sync_fs = fuse_sync_fs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001052 .show_options = fuse_show_options,
1053};
1054
Csaba Henk487ea5a2009-08-26 19:17:22 +02001055static void sanitize_global_limit(unsigned *limit)
1056{
Miklos Szeredif22f8122019-09-12 14:28:13 +02001057 /*
1058 * The default maximum number of async requests is calculated to consume
1059 * 1/2^13 of the total memory, assuming 392 bytes per request.
1060 */
Csaba Henk487ea5a2009-08-26 19:17:22 +02001061 if (*limit == 0)
Miklos Szeredif22f8122019-09-12 14:28:13 +02001062 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
Csaba Henk487ea5a2009-08-26 19:17:22 +02001063
1064 if (*limit >= 1 << 16)
1065 *limit = (1 << 16) - 1;
1066}
1067
Kees Cooke4dca7b2017-10-17 19:04:42 -07001068static int set_global_limit(const char *val, const struct kernel_param *kp)
Csaba Henk487ea5a2009-08-26 19:17:22 +02001069{
1070 int rv;
1071
1072 rv = param_set_uint(val, kp);
1073 if (rv)
1074 return rv;
1075
1076 sanitize_global_limit((unsigned *)kp->arg);
1077
1078 return 0;
1079}
1080
1081static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
1082{
1083 int cap_sys_admin = capable(CAP_SYS_ADMIN);
1084
1085 if (arg->minor < 13)
1086 return;
1087
1088 sanitize_global_limit(&max_user_bgreq);
1089 sanitize_global_limit(&max_user_congthresh);
1090
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03001091 spin_lock(&fc->bg_lock);
Csaba Henk487ea5a2009-08-26 19:17:22 +02001092 if (arg->max_background) {
1093 fc->max_background = arg->max_background;
1094
1095 if (!cap_sys_admin && fc->max_background > max_user_bgreq)
1096 fc->max_background = max_user_bgreq;
1097 }
1098 if (arg->congestion_threshold) {
1099 fc->congestion_threshold = arg->congestion_threshold;
1100
1101 if (!cap_sys_admin &&
1102 fc->congestion_threshold > max_user_congthresh)
1103 fc->congestion_threshold = max_user_congthresh;
1104 }
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +03001105 spin_unlock(&fc->bg_lock);
Csaba Henk487ea5a2009-08-26 19:17:22 +02001106}
1107
Miklos Szeredi615047e2019-09-10 15:04:10 +02001108struct fuse_init_args {
1109 struct fuse_args args;
1110 struct fuse_init_in in;
1111 struct fuse_init_out out;
1112};
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001113
Max Reitzfcee2162020-05-06 17:44:12 +02001114static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
Miklos Szeredi615047e2019-09-10 15:04:10 +02001115 int error)
1116{
Max Reitzfcee2162020-05-06 17:44:12 +02001117 struct fuse_conn *fc = fm->fc;
Miklos Szeredi615047e2019-09-10 15:04:10 +02001118 struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
1119 struct fuse_init_out *arg = &ia->out;
Stefan Hajnoczifd1a1dc2020-08-19 18:19:49 -04001120 bool ok = true;
Miklos Szeredi615047e2019-09-10 15:04:10 +02001121
1122 if (error || arg->major != FUSE_KERNEL_VERSION)
Stefan Hajnoczifd1a1dc2020-08-19 18:19:49 -04001123 ok = false;
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001124 else {
Miklos Szeredi9cd68452006-02-01 03:04:40 -08001125 unsigned long ra_pages;
1126
Csaba Henk487ea5a2009-08-26 19:17:22 +02001127 process_init_limits(fc, arg);
1128
Miklos Szeredi9cd68452006-02-01 03:04:40 -08001129 if (arg->minor >= 6) {
Miklos Szeredi53db2892021-11-25 14:05:18 +01001130 u64 flags = arg->flags | (u64) arg->flags2 << 32;
1131
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001132 ra_pages = arg->max_readahead / PAGE_SIZE;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001133 if (flags & FUSE_ASYNC_READ)
Miklos Szeredi9cd68452006-02-01 03:04:40 -08001134 fc->async_read = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001135 if (!(flags & FUSE_POSIX_LOCKS))
Miklos Szeredi71421252006-06-25 05:48:52 -07001136 fc->no_lock = 1;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001137 if (arg->minor >= 17) {
Miklos Szeredi53db2892021-11-25 14:05:18 +01001138 if (!(flags & FUSE_FLOCK_LOCKS))
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001139 fc->no_flock = 1;
Miklos Szeredi24114502011-09-12 09:31:49 +02001140 } else {
Miklos Szeredi53db2892021-11-25 14:05:18 +01001141 if (!(flags & FUSE_POSIX_LOCKS))
Miklos Szeredi24114502011-09-12 09:31:49 +02001142 fc->no_flock = 1;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001143 }
Miklos Szeredi53db2892021-11-25 14:05:18 +01001144 if (flags & FUSE_ATOMIC_O_TRUNC)
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001145 fc->atomic_o_trunc = 1;
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001146 if (arg->minor >= 9) {
1147 /* LOOKUP has dependency on proto version */
Miklos Szeredi53db2892021-11-25 14:05:18 +01001148 if (flags & FUSE_EXPORT_SUPPORT)
Miklos Szeredi33670fa2008-07-25 01:49:02 -07001149 fc->export_support = 1;
1150 }
Miklos Szeredi53db2892021-11-25 14:05:18 +01001151 if (flags & FUSE_BIG_WRITES)
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001152 fc->big_writes = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001153 if (flags & FUSE_DONT_MASK)
Miklos Szeredie0a43dd2009-06-30 20:12:23 +02001154 fc->dont_mask = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001155 if (flags & FUSE_AUTO_INVAL_DATA)
Brian Foster72d0d242012-07-16 15:23:48 -04001156 fc->auto_inval_data = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001157 else if (flags & FUSE_EXPLICIT_INVAL_DATA)
Kirill Smelkovad2ba642019-03-27 11:14:15 +00001158 fc->explicit_inval_data = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001159 if (flags & FUSE_DO_READDIRPLUS) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001160 fc->do_readdirplus = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001161 if (flags & FUSE_READDIRPLUS_AUTO)
Miklos Szeredi28420da2013-06-03 14:40:22 +02001162 fc->readdirplus_auto = 1;
1163 }
Miklos Szeredi53db2892021-11-25 14:05:18 +01001164 if (flags & FUSE_ASYNC_DIO)
Miklos Szeredi60b9df72013-05-01 14:37:21 +02001165 fc->async_dio = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001166 if (flags & FUSE_WRITEBACK_CACHE)
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001167 fc->writeback_cache = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001168 if (flags & FUSE_PARALLEL_DIROPS)
Miklos Szeredi5c672ab2016-06-30 13:10:49 +02001169 fc->parallel_dirops = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001170 if (flags & FUSE_HANDLE_KILLPRIV)
Miklos Szeredi5e940c12016-10-01 07:32:32 +02001171 fc->handle_killpriv = 1;
Miklos Szeredie27c9d32014-04-28 14:19:23 +02001172 if (arg->time_gran && arg->time_gran <= 1000000000)
Max Reitzfcee2162020-05-06 17:44:12 +02001173 fm->sb->s_time_gran = arg->time_gran;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001174 if ((flags & FUSE_POSIX_ACL)) {
Miklos Szeredi29433a22016-10-01 07:32:32 +02001175 fc->default_permissions = 1;
Seth Forshee60bcc882016-08-29 08:46:37 -05001176 fc->posix_acl = 1;
Max Reitzfcee2162020-05-06 17:44:12 +02001177 fm->sb->s_xattr = fuse_acl_xattr_handlers;
Seth Forshee60bcc882016-08-29 08:46:37 -05001178 }
Miklos Szeredi53db2892021-11-25 14:05:18 +01001179 if (flags & FUSE_CACHE_SYMLINKS)
Dan Schatzberg5571f1e2018-10-11 08:17:00 -07001180 fc->cache_symlinks = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001181 if (flags & FUSE_ABORT_ERROR)
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001182 fc->abort_err = 1;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001183 if (flags & FUSE_MAX_PAGES) {
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001184 fc->max_pages =
Connor Kuehla7f0d7a2021-03-18 08:52:22 -05001185 min_t(unsigned int, fc->max_pages_limit,
Constantine Shulyupin5da784c2018-09-06 15:37:06 +03001186 max_t(unsigned int, arg->max_pages, 1));
1187 }
Jeffle Xu2ee019f2021-11-25 15:05:28 +08001188 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1189 if (flags & FUSE_MAP_ALIGNMENT &&
1190 !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1191 ok = false;
1192 }
1193 if (flags & FUSE_HAS_INODE_DAX)
1194 fc->inode_dax = 1;
Stefan Hajnoczifd1a1dc2020-08-19 18:19:49 -04001195 }
Miklos Szeredi53db2892021-11-25 14:05:18 +01001196 if (flags & FUSE_HANDLE_KILLPRIV_V2) {
Vivek Goyal63f99092020-10-09 14:15:07 -04001197 fc->handle_killpriv_v2 = 1;
Vivek Goyal9d769e62020-10-09 14:15:12 -04001198 fm->sb->s_flags |= SB_NOSEC;
1199 }
Miklos Szeredi53db2892021-11-25 14:05:18 +01001200 if (flags & FUSE_SETXATTR_EXT)
Vivek Goyal52a4c952021-03-25 11:18:22 -04001201 fc->setxattr_ext = 1;
Vivek Goyal3e2b6fd2021-11-11 09:32:49 -05001202 if (flags & FUSE_SECURITY_CTX)
1203 fc->init_security = 1;
Miklos Szeredi71421252006-06-25 05:48:52 -07001204 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001205 ra_pages = fc->max_read / PAGE_SIZE;
Miklos Szeredi71421252006-06-25 05:48:52 -07001206 fc->no_lock = 1;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001207 fc->no_flock = 1;
Miklos Szeredi71421252006-06-25 05:48:52 -07001208 }
Miklos Szeredi9cd68452006-02-01 03:04:40 -08001209
Max Reitzfcee2162020-05-06 17:44:12 +02001210 fm->sb->s_bdi->ra_pages =
1211 min(fm->sb->s_bdi->ra_pages, ra_pages);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001212 fc->minor = arg->minor;
1213 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
Miklos Szeredif948d562008-06-17 18:05:40 +02001214 fc->max_write = max_t(unsigned, 4096, fc->max_write);
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -08001215 fc->conn_init = 1;
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001216 }
Miklos Szeredi615047e2019-09-10 15:04:10 +02001217 kfree(ia);
1218
Stefan Hajnoczifd1a1dc2020-08-19 18:19:49 -04001219 if (!ok) {
1220 fc->conn_init = 0;
1221 fc->conn_error = 1;
1222 }
1223
Miklos Szeredi9759bd512015-01-06 10:45:35 +01001224 fuse_set_initialized(fc);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -07001225 wake_up_all(&fc->blocked_waitq);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001226}
1227
Max Reitzfcee2162020-05-06 17:44:12 +02001228void fuse_send_init(struct fuse_mount *fm)
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001229{
Miklos Szeredi615047e2019-09-10 15:04:10 +02001230 struct fuse_init_args *ia;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001231 u64 flags;
Miklos Szeredi095da6c2006-01-16 22:14:52 -08001232
Miklos Szeredi615047e2019-09-10 15:04:10 +02001233 ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1234
1235 ia->in.major = FUSE_KERNEL_VERSION;
1236 ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
Max Reitzfcee2162020-05-06 17:44:12 +02001237 ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
Miklos Szeredi53db2892021-11-25 14:05:18 +01001238 flags =
Miklos Szeredi615047e2019-09-10 15:04:10 +02001239 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001240 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
Miklos Szeredi69fe05c2012-07-18 16:09:40 +02001241 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
Wei Fang94463852016-07-25 21:17:04 +08001242 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001243 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
Miklos Szeredi5c672ab2016-06-30 13:10:49 +02001244 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
Szymon Lukasz3b7008b2017-11-09 21:23:35 +01001245 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
Chad Austind9a9ea92019-01-07 16:53:17 -08001246 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
Vivek Goyal63f99092020-10-09 14:15:07 -04001247 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
Vivek Goyal3e2b6fd2021-11-11 09:32:49 -05001248 FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT | FUSE_INIT_EXT |
1249 FUSE_SECURITY_CTX;
Stefan Hajnoczifd1a1dc2020-08-19 18:19:49 -04001250#ifdef CONFIG_FUSE_DAX
Max Reitzfcee2162020-05-06 17:44:12 +02001251 if (fm->fc->dax)
Miklos Szeredi53db2892021-11-25 14:05:18 +01001252 flags |= FUSE_MAP_ALIGNMENT;
Jeffle Xu2ee019f2021-11-25 15:05:28 +08001253 if (fuse_is_inode_dax_mode(fm->fc->dax_mode))
1254 flags |= FUSE_HAS_INODE_DAX;
Stefan Hajnoczifd1a1dc2020-08-19 18:19:49 -04001255#endif
Max Reitzbf109c62020-04-21 14:47:15 +02001256 if (fm->fc->auto_submounts)
Miklos Szeredi53db2892021-11-25 14:05:18 +01001257 flags |= FUSE_SUBMOUNTS;
1258
1259 ia->in.flags = flags;
1260 ia->in.flags2 = flags >> 32;
Max Reitzbf109c62020-04-21 14:47:15 +02001261
Miklos Szeredi615047e2019-09-10 15:04:10 +02001262 ia->args.opcode = FUSE_INIT;
1263 ia->args.in_numargs = 1;
1264 ia->args.in_args[0].size = sizeof(ia->in);
1265 ia->args.in_args[0].value = &ia->in;
1266 ia->args.out_numargs = 1;
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08001267 /* Variable length argument used for backward compatibility
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001268 with interface version < 7.5. Rest of init_out is zeroed
1269 by do_get_request(), so a short reply is not a problem */
zhengbincabdb4f2020-01-14 20:39:45 +08001270 ia->args.out_argvar = true;
Miklos Szeredi615047e2019-09-10 15:04:10 +02001271 ia->args.out_args[0].size = sizeof(ia->out);
1272 ia->args.out_args[0].value = &ia->out;
1273 ia->args.force = true;
1274 ia->args.nocreds = true;
1275 ia->args.end = process_init_reply;
1276
Max Reitzfcee2162020-05-06 17:44:12 +02001277 if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1278 process_init_reply(fm, &ia->args, -ENOTCONN);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001279}
Vivek Goyal95a84cdb2019-03-06 16:51:39 -05001280EXPORT_SYMBOL_GPL(fuse_send_init);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -08001281
Miklos Szeredi783863d2019-08-29 11:01:20 +02001282void fuse_free_conn(struct fuse_conn *fc)
Tejun Heo43901aa2008-11-26 12:03:56 +01001283{
Miklos Szeredicc080e92015-07-01 16:26:08 +02001284 WARN_ON(!list_empty(&fc->devices));
Al Virodd3e2c552013-10-03 21:21:39 -04001285 kfree_rcu(fc, rcu);
Tejun Heo43901aa2008-11-26 12:03:56 +01001286}
Miklos Szeredi783863d2019-08-29 11:01:20 +02001287EXPORT_SYMBOL_GPL(fuse_free_conn);
Tejun Heo43901aa2008-11-26 12:03:56 +01001288
Tejun Heoa325f9b2009-04-14 10:54:52 +09001289static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1290{
1291 int err;
Jan Kara5f7f7542017-04-12 12:24:40 +02001292 char *suffix = "";
Tejun Heoa325f9b2009-04-14 10:54:52 +09001293
Jan Kara69c8ebf2017-05-16 12:22:22 +02001294 if (sb->s_bdev) {
Jan Kara5f7f7542017-04-12 12:24:40 +02001295 suffix = "-fuseblk";
Jan Kara69c8ebf2017-05-16 12:22:22 +02001296 /*
1297 * sb->s_bdi points to blkdev's bdi however we want to redirect
1298 * it to our private bdi...
1299 */
1300 bdi_put(sb->s_bdi);
1301 sb->s_bdi = &noop_backing_dev_info;
1302 }
Jan Kara5f7f7542017-04-12 12:24:40 +02001303 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1304 MINOR(fc->dev), suffix);
Tejun Heoa325f9b2009-04-14 10:54:52 +09001305 if (err)
1306 return err;
1307
Jan Kara5f7f7542017-04-12 12:24:40 +02001308 /* fuse does it's own writeback accounting */
Christoph Hellwig823423e2020-09-24 08:51:39 +02001309 sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT;
1310 sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
Jan Kara5f7f7542017-04-12 12:24:40 +02001311
Tejun Heoa325f9b2009-04-14 10:54:52 +09001312 /*
1313 * For a single fuse filesystem use max 1% of dirty +
1314 * writeback threshold.
1315 *
1316 * This gives about 1M of write buffer for memory maps on a
1317 * machine with 1G and 10% dirty_ratio, which should be more
1318 * than enough.
1319 *
1320 * Privileged users can raise it by writing to
1321 *
1322 * /sys/class/bdi/<bdi>/max_ratio
1323 */
Jan Kara5f7f7542017-04-12 12:24:40 +02001324 bdi_set_max_ratio(sb->s_bdi, 1);
Tejun Heoa325f9b2009-04-14 10:54:52 +09001325
1326 return 0;
1327}
1328
Vivek Goyal0cd1eb9a2019-03-06 16:51:40 -05001329struct fuse_dev *fuse_dev_alloc(void)
Miklos Szeredicc080e92015-07-01 16:26:08 +02001330{
1331 struct fuse_dev *fud;
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001332 struct list_head *pq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001333
1334 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001335 if (!fud)
1336 return NULL;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001337
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001338 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1339 if (!pq) {
1340 kfree(fud);
1341 return NULL;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001342 }
1343
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001344 fud->pq.processing = pq;
Kirill Tkhaibe2ff422018-09-11 13:12:14 +03001345 fuse_pqueue_init(&fud->pq);
1346
Miklos Szeredicc080e92015-07-01 16:26:08 +02001347 return fud;
1348}
1349EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1350
Vivek Goyal0cd1eb9a2019-03-06 16:51:40 -05001351void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1352{
1353 fud->fc = fuse_conn_get(fc);
1354 spin_lock(&fc->lock);
1355 list_add_tail(&fud->entry, &fc->devices);
1356 spin_unlock(&fc->lock);
1357}
1358EXPORT_SYMBOL_GPL(fuse_dev_install);
1359
1360struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1361{
1362 struct fuse_dev *fud;
1363
1364 fud = fuse_dev_alloc();
1365 if (!fud)
1366 return NULL;
1367
1368 fuse_dev_install(fud, fc);
1369 return fud;
1370}
1371EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
1372
Miklos Szeredicc080e92015-07-01 16:26:08 +02001373void fuse_dev_free(struct fuse_dev *fud)
1374{
1375 struct fuse_conn *fc = fud->fc;
1376
1377 if (fc) {
1378 spin_lock(&fc->lock);
1379 list_del(&fud->entry);
1380 spin_unlock(&fc->lock);
1381
1382 fuse_conn_put(fc);
1383 }
Takeshi Misawad72f70d2018-12-09 14:30:15 +09001384 kfree(fud->pq.processing);
Miklos Szeredicc080e92015-07-01 16:26:08 +02001385 kfree(fud);
1386}
1387EXPORT_SYMBOL_GPL(fuse_dev_free);
1388
Max Reitz1866d772020-09-09 17:52:17 +02001389static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
1390 const struct fuse_inode *fi)
1391{
1392 *attr = (struct fuse_attr){
1393 .ino = fi->inode.i_ino,
1394 .size = fi->inode.i_size,
1395 .blocks = fi->inode.i_blocks,
1396 .atime = fi->inode.i_atime.tv_sec,
1397 .mtime = fi->inode.i_mtime.tv_sec,
1398 .ctime = fi->inode.i_ctime.tv_sec,
1399 .atimensec = fi->inode.i_atime.tv_nsec,
1400 .mtimensec = fi->inode.i_mtime.tv_nsec,
1401 .ctimensec = fi->inode.i_ctime.tv_nsec,
1402 .mode = fi->inode.i_mode,
1403 .nlink = fi->inode.i_nlink,
1404 .uid = fi->inode.i_uid.val,
1405 .gid = fi->inode.i_gid.val,
1406 .rdev = fi->inode.i_rdev,
1407 .blksize = 1u << fi->inode.i_blkbits,
1408 };
1409}
1410
1411static void fuse_sb_defaults(struct super_block *sb)
1412{
1413 sb->s_magic = FUSE_SUPER_MAGIC;
1414 sb->s_op = &fuse_super_operations;
1415 sb->s_xattr = fuse_xattr_handlers;
1416 sb->s_maxbytes = MAX_LFS_FILESIZE;
1417 sb->s_time_gran = 1;
1418 sb->s_export_op = &fuse_export_operations;
1419 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1420 if (sb->s_user_ns != &init_user_ns)
1421 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1422 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1423
1424 /*
1425 * If we are not in the initial user namespace posix
1426 * acls must be translated.
1427 */
1428 if (sb->s_user_ns != &init_user_ns)
1429 sb->s_xattr = fuse_no_acl_xattr_handlers;
1430}
1431
Greg Kurz1b539912021-06-04 18:11:56 +02001432static int fuse_fill_super_submount(struct super_block *sb,
1433 struct fuse_inode *parent_fi)
Max Reitz1866d772020-09-09 17:52:17 +02001434{
1435 struct fuse_mount *fm = get_fuse_mount_super(sb);
1436 struct super_block *parent_sb = parent_fi->inode.i_sb;
1437 struct fuse_attr root_attr;
1438 struct inode *root;
1439
1440 fuse_sb_defaults(sb);
1441 fm->sb = sb;
1442
1443 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1444 sb->s_bdi = bdi_get(parent_sb->s_bdi);
1445
1446 sb->s_xattr = parent_sb->s_xattr;
1447 sb->s_time_gran = parent_sb->s_time_gran;
1448 sb->s_blocksize = parent_sb->s_blocksize;
1449 sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1450 sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1451 if (parent_sb->s_subtype && !sb->s_subtype)
1452 return -ENOMEM;
1453
1454 fuse_fill_attr_from_inode(&root_attr, parent_fi);
1455 root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
1456 /*
1457 * This inode is just a duplicate, so it is not looked up and
1458 * its nlookup should not be incremented. fuse_iget() does
1459 * that, though, so undo it here.
1460 */
1461 get_fuse_inode(root)->nlookup--;
1462 sb->s_d_op = &fuse_dentry_operations;
1463 sb->s_root = d_make_root(root);
1464 if (!sb->s_root)
1465 return -ENOMEM;
1466
1467 return 0;
1468}
1469
Greg Kurz266eb3f22021-06-04 18:11:54 +02001470/* Filesystem context private data holds the FUSE inode of the mount point */
Greg Kurzfe0a7bd2021-06-04 18:11:53 +02001471static int fuse_get_tree_submount(struct fs_context *fsc)
1472{
Greg Kurz266eb3f22021-06-04 18:11:54 +02001473 struct fuse_mount *fm;
1474 struct fuse_inode *mp_fi = fsc->fs_private;
1475 struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode);
1476 struct super_block *sb;
1477 int err;
1478
1479 fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL);
1480 if (!fm)
1481 return -ENOMEM;
1482
Miklos Szeredic191cd02021-10-21 10:01:39 +02001483 fm->fc = fuse_conn_get(fc);
Greg Kurz266eb3f22021-06-04 18:11:54 +02001484 fsc->s_fs_info = fm;
1485 sb = sget_fc(fsc, NULL, set_anon_super_fc);
Miklos Szeredic191cd02021-10-21 10:01:39 +02001486 if (fsc->s_fs_info)
1487 fuse_mount_destroy(fm);
1488 if (IS_ERR(sb))
Greg Kurz266eb3f22021-06-04 18:11:54 +02001489 return PTR_ERR(sb);
Greg Kurz266eb3f22021-06-04 18:11:54 +02001490
1491 /* Initialize superblock, making @mp_fi its root */
1492 err = fuse_fill_super_submount(sb, mp_fi);
1493 if (err) {
Greg Kurz266eb3f22021-06-04 18:11:54 +02001494 deactivate_locked_super(sb);
1495 return err;
1496 }
1497
1498 down_write(&fc->killsb);
1499 list_add_tail(&fm->fc_entry, &fc->mounts);
1500 up_write(&fc->killsb);
1501
1502 sb->s_flags |= SB_ACTIVE;
1503 fsc->root = dget(sb->s_root);
1504
Greg Kurzfe0a7bd2021-06-04 18:11:53 +02001505 return 0;
1506}
1507
1508static const struct fs_context_operations fuse_context_submount_ops = {
1509 .get_tree = fuse_get_tree_submount,
1510};
1511
1512int fuse_init_fs_context_submount(struct fs_context *fsc)
1513{
1514 fsc->ops = &fuse_context_submount_ops;
1515 return 0;
1516}
1517EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
1518
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001519int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001520{
Vivek Goyal7fd3abf2020-05-04 14:33:15 -04001521 struct fuse_dev *fud = NULL;
Max Reitzfcee2162020-05-06 17:44:12 +02001522 struct fuse_mount *fm = get_fuse_mount_super(sb);
1523 struct fuse_conn *fc = fm->fc;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001524 struct inode *root;
Miklos Szeredif543f252006-01-16 22:14:35 -08001525 struct dentry *root_dentry;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001526 int err;
1527
Miklos Szeredic2b8f002009-01-26 15:00:58 +01001528 err = -EINVAL;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001529 if (sb->s_flags & SB_MANDLOCK)
Miklos Szeredic2b8f002009-01-26 15:00:58 +01001530 goto err;
Miklos Szeredi71421252006-06-25 05:48:52 -07001531
Miklos Szeredi660585b2021-09-01 12:39:02 +02001532 rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc());
Max Reitz1866d772020-09-09 17:52:17 +02001533 fuse_sb_defaults(sb);
Al Viro9e1f1de2011-06-03 18:24:58 -04001534
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001535 if (ctx->is_bdev) {
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001536#ifdef CONFIG_BLOCK
Miklos Szeredic2b8f002009-01-26 15:00:58 +01001537 err = -EINVAL;
David Howellsc30da2e2019-03-25 16:38:31 +00001538 if (!sb_set_blocksize(sb, ctx->blksize))
Miklos Szeredic2b8f002009-01-26 15:00:58 +01001539 goto err;
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001540#endif
Miklos Szeredid8091612006-12-06 20:35:48 -08001541 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001542 sb->s_blocksize = PAGE_SIZE;
1543 sb->s_blocksize_bits = PAGE_SHIFT;
Miklos Szeredid8091612006-12-06 20:35:48 -08001544 }
David Howellsc30da2e2019-03-25 16:38:31 +00001545
1546 sb->s_subtype = ctx->subtype;
1547 ctx->subtype = NULL;
Vivek Goyal1dd53952020-08-19 18:19:47 -04001548 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
Jeffle Xu780b1b92021-11-25 15:05:25 +08001549 err = fuse_dax_conn_alloc(fc, ctx->dax_mode, ctx->dax_dev);
Vivek Goyal1dd53952020-08-19 18:19:47 -04001550 if (err)
1551 goto err;
1552 }
Eric W. Biedermane45b2542018-05-04 11:47:28 -05001553
Vivek Goyal7fd3abf2020-05-04 14:33:15 -04001554 if (ctx->fudptr) {
1555 err = -ENOMEM;
1556 fud = fuse_dev_alloc_install(fc);
1557 if (!fud)
Vivek Goyal1dd53952020-08-19 18:19:47 -04001558 goto err_free_dax;
Vivek Goyal7fd3abf2020-05-04 14:33:15 -04001559 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02001560
Tejun Heoa325f9b2009-04-14 10:54:52 +09001561 fc->dev = sb->s_dev;
Max Reitzfcee2162020-05-06 17:44:12 +02001562 fm->sb = sb;
Tejun Heoa325f9b2009-04-14 10:54:52 +09001563 err = fuse_bdi_init(fc, sb);
1564 if (err)
Miklos Szeredicc080e92015-07-01 16:26:08 +02001565 goto err_dev_free;
Tejun Heo0d179aa2008-11-26 12:03:55 +01001566
Miklos Szeredie0a43dd2009-06-30 20:12:23 +02001567 /* Handle umasking inside the fuse code */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001568 if (sb->s_flags & SB_POSIXACL)
Miklos Szeredie0a43dd2009-06-30 20:12:23 +02001569 fc->dont_mask = 1;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001570 sb->s_flags |= SB_POSIXACL;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +02001571
David Howellsc30da2e2019-03-25 16:38:31 +00001572 fc->default_permissions = ctx->default_permissions;
1573 fc->allow_other = ctx->allow_other;
1574 fc->user_id = ctx->user_id;
1575 fc->group_id = ctx->group_id;
Vivek Goyalf4fd4ae2020-08-19 18:19:45 -04001576 fc->legacy_opts_show = ctx->legacy_opts_show;
Max Reitz1866d772020-09-09 17:52:17 +02001577 fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
Miklos Szeredi783863d2019-08-29 11:01:20 +02001578 fc->destroy = ctx->destroy;
Vivek Goyal15c8e722019-05-06 15:35:43 -04001579 fc->no_control = ctx->no_control;
1580 fc->no_force_umount = ctx->no_force_umount;
Miklos Szeredif543f252006-01-16 22:14:35 -08001581
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001582 err = -ENOMEM;
David Howellsc30da2e2019-03-25 16:38:31 +00001583 root = fuse_get_root_inode(sb, ctx->rootmode);
Miklos Szeredi0ce267f2016-10-18 15:36:48 +02001584 sb->s_d_op = &fuse_root_dentry_operations;
Al Viro48fde702012-01-08 22:15:13 -05001585 root_dentry = d_make_root(root);
1586 if (!root_dentry)
Miklos Szeredicc080e92015-07-01 16:26:08 +02001587 goto err_dev_free;
Miklos Szeredi0ce267f2016-10-18 15:36:48 +02001588 /* Root dentry doesn't have .d_revalidate */
Al Viroc35eebe2010-12-18 11:15:22 -05001589 sb->s_d_op = &fuse_dentry_operations;
Miklos Szeredif543f252006-01-16 22:14:35 -08001590
Miklos Szeredibafa9652006-06-25 05:48:51 -07001591 mutex_lock(&fuse_mutex);
Miklos Szeredi8aa09a52006-04-26 10:49:16 +02001592 err = -EINVAL;
Vivek Goyal7fd3abf2020-05-04 14:33:15 -04001593 if (ctx->fudptr && *ctx->fudptr)
Miklos Szeredibafa9652006-06-25 05:48:51 -07001594 goto err_unlock;
Miklos Szeredi8aa09a52006-04-26 10:49:16 +02001595
Miklos Szeredibafa9652006-06-25 05:48:51 -07001596 err = fuse_ctl_add_conn(fc);
1597 if (err)
1598 goto err_unlock;
1599
1600 list_add_tail(&fc->entry, &fuse_conn_list);
Miklos Szeredif543f252006-01-16 22:14:35 -08001601 sb->s_root = root_dentry;
Vivek Goyal7fd3abf2020-05-04 14:33:15 -04001602 if (ctx->fudptr)
1603 *ctx->fudptr = fud;
Miklos Szeredibafa9652006-06-25 05:48:51 -07001604 mutex_unlock(&fuse_mutex);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001605 return 0;
1606
Miklos Szeredibafa9652006-06-25 05:48:51 -07001607 err_unlock:
1608 mutex_unlock(&fuse_mutex);
Miklos Szeredif543f252006-01-16 22:14:35 -08001609 dput(root_dentry);
Miklos Szeredicc080e92015-07-01 16:26:08 +02001610 err_dev_free:
Vivek Goyal7fd3abf2020-05-04 14:33:15 -04001611 if (fud)
1612 fuse_dev_free(fud);
Vivek Goyal1dd53952020-08-19 18:19:47 -04001613 err_free_dax:
1614 if (IS_ENABLED(CONFIG_FUSE_DAX))
1615 fuse_dax_conn_free(fc);
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001616 err:
1617 return err;
1618}
1619EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1620
1621static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1622{
1623 struct fuse_fs_context *ctx = fsc->fs_private;
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001624 int err;
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001625
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001626 if (!ctx->file || !ctx->rootmode_present ||
Miklos Szeredibadc7412021-08-04 13:22:58 +02001627 !ctx->user_id_present || !ctx->group_id_present)
1628 return -EINVAL;
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001629
1630 /*
1631 * Require mount to happen from the same user namespace which
1632 * opened /dev/fuse to prevent potential attacks.
1633 */
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001634 if ((ctx->file->f_op != &fuse_dev_operations) ||
1635 (ctx->file->f_cred->user_ns != sb->s_user_ns))
Miklos Szeredi964d32e2021-10-21 10:01:39 +02001636 return -EINVAL;
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001637 ctx->fudptr = &ctx->file->private_data;
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001638
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001639 err = fuse_fill_super_common(sb, ctx);
1640 if (err)
Miklos Szeredi964d32e2021-10-21 10:01:39 +02001641 return err;
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001642 /* file->private_data shall be visible on all CPUs after this */
1643 smp_mb();
Max Reitzfcee2162020-05-06 17:44:12 +02001644 fuse_send_init(get_fuse_mount_super(sb));
Stefan Hajnoczi0cc26562018-06-13 10:23:04 +01001645 return 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001646}
1647
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001648/*
1649 * This is the path where user supplied an already initialized fuse dev. In
1650 * this case never create a new super if the old one is gone.
1651 */
1652static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001653{
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001654 return -ENOTCONN;
1655}
David Howellsc30da2e2019-03-25 16:38:31 +00001656
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001657static int fuse_test_super(struct super_block *sb, struct fs_context *fsc)
1658{
1659
1660 return fsc->sget_key == get_fuse_conn_super(sb);
1661}
1662
Miklos Szeredi84c21502021-08-04 13:22:58 +02001663static int fuse_get_tree(struct fs_context *fsc)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001664{
Miklos Szeredi84c21502021-08-04 13:22:58 +02001665 struct fuse_fs_context *ctx = fsc->fs_private;
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001666 struct fuse_dev *fud;
Miklos Szeredi80019f12021-10-21 10:01:39 +02001667 struct fuse_conn *fc;
1668 struct fuse_mount *fm;
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001669 struct super_block *sb;
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001670 int err;
1671
Miklos Szeredi80019f12021-10-21 10:01:39 +02001672 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1673 if (!fc)
1674 return -ENOMEM;
1675
1676 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1677 if (!fm) {
1678 kfree(fc);
1679 return -ENOMEM;
1680 }
1681
1682 fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL);
1683 fc->release = fuse_free_conn;
1684
1685 fsc->s_fs_info = fm;
1686
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001687 if (ctx->fd_present)
1688 ctx->file = fget(ctx->fd);
David Howellsc30da2e2019-03-25 16:38:31 +00001689
Miklos Szeredibadc7412021-08-04 13:22:58 +02001690 if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) {
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001691 err = get_tree_bdev(fsc, fuse_fill_super);
Miklos Szeredi80019f12021-10-21 10:01:39 +02001692 goto out;
Miklos Szeredibadc7412021-08-04 13:22:58 +02001693 }
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001694 /*
1695 * While block dev mount can be initialized with a dummy device fd
1696 * (found by device name), normal fuse mounts can't
1697 */
Miklos Szeredi80019f12021-10-21 10:01:39 +02001698 err = -EINVAL;
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001699 if (!ctx->file)
Miklos Szeredi80019f12021-10-21 10:01:39 +02001700 goto out;
David Howellsc30da2e2019-03-25 16:38:31 +00001701
Miklos Szeredi5d5b74a2021-08-05 05:57:27 +02001702 /*
1703 * Allow creating a fuse mount with an already initialized fuse
1704 * connection
1705 */
1706 fud = READ_ONCE(ctx->file->private_data);
1707 if (ctx->file->f_op == &fuse_dev_operations && fud) {
1708 fsc->sget_key = fud->fc;
1709 sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super);
1710 err = PTR_ERR_OR_ZERO(sb);
1711 if (!IS_ERR(sb))
1712 fsc->root = dget(sb->s_root);
1713 } else {
1714 err = get_tree_nodev(fsc, fuse_fill_super);
1715 }
Miklos Szeredi80019f12021-10-21 10:01:39 +02001716out:
1717 if (fsc->s_fs_info)
1718 fuse_mount_destroy(fm);
Miklos Szeredi62dd1fc2021-08-05 05:57:27 +02001719 if (ctx->file)
1720 fput(ctx->file);
1721 return err;
David Howellsc30da2e2019-03-25 16:38:31 +00001722}
1723
1724static const struct fs_context_operations fuse_context_ops = {
Miklos Szeredi84c21502021-08-04 13:22:58 +02001725 .free = fuse_free_fsc,
David Howellsc30da2e2019-03-25 16:38:31 +00001726 .parse_param = fuse_parse_param,
Miklos Szeredi0189a2d2020-07-14 14:45:41 +02001727 .reconfigure = fuse_reconfigure,
David Howellsc30da2e2019-03-25 16:38:31 +00001728 .get_tree = fuse_get_tree,
1729};
1730
1731/*
1732 * Set up the filesystem mount context.
1733 */
Miklos Szeredi84c21502021-08-04 13:22:58 +02001734static int fuse_init_fs_context(struct fs_context *fsc)
David Howellsc30da2e2019-03-25 16:38:31 +00001735{
1736 struct fuse_fs_context *ctx;
1737
1738 ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1739 if (!ctx)
1740 return -ENOMEM;
1741
1742 ctx->max_read = ~0;
1743 ctx->blksize = FUSE_DEFAULT_BLKSIZE;
Vivek Goyalf4fd4ae2020-08-19 18:19:45 -04001744 ctx->legacy_opts_show = true;
David Howellsc30da2e2019-03-25 16:38:31 +00001745
1746#ifdef CONFIG_BLOCK
Miklos Szeredi84c21502021-08-04 13:22:58 +02001747 if (fsc->fs_type == &fuseblk_fs_type) {
David Howellsc30da2e2019-03-25 16:38:31 +00001748 ctx->is_bdev = true;
Miklos Szeredi783863d2019-08-29 11:01:20 +02001749 ctx->destroy = true;
1750 }
David Howellsc30da2e2019-03-25 16:38:31 +00001751#endif
1752
Miklos Szeredi84c21502021-08-04 13:22:58 +02001753 fsc->fs_private = ctx;
1754 fsc->ops = &fuse_context_ops;
David Howellsc30da2e2019-03-25 16:38:31 +00001755 return 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001756}
1757
Max Reitzfcee2162020-05-06 17:44:12 +02001758bool fuse_mount_remove(struct fuse_mount *fm)
John Muir3b463ae2009-05-31 11:13:57 -04001759{
Max Reitzfcee2162020-05-06 17:44:12 +02001760 struct fuse_conn *fc = fm->fc;
1761 bool last = false;
John Muir3b463ae2009-05-31 11:13:57 -04001762
Max Reitzfcee2162020-05-06 17:44:12 +02001763 down_write(&fc->killsb);
1764 list_del_init(&fm->fc_entry);
1765 if (list_empty(&fc->mounts))
1766 last = true;
1767 up_write(&fc->killsb);
Miklos Szeredie8f3bd72018-07-26 16:13:11 +02001768
Max Reitzfcee2162020-05-06 17:44:12 +02001769 return last;
Miklos Szeredie8f3bd72018-07-26 16:13:11 +02001770}
Max Reitzfcee2162020-05-06 17:44:12 +02001771EXPORT_SYMBOL_GPL(fuse_mount_remove);
John Muir3b463ae2009-05-31 11:13:57 -04001772
Max Reitzfcee2162020-05-06 17:44:12 +02001773void fuse_conn_destroy(struct fuse_mount *fm)
Miklos Szeredie8f3bd72018-07-26 16:13:11 +02001774{
Max Reitzfcee2162020-05-06 17:44:12 +02001775 struct fuse_conn *fc = fm->fc;
1776
1777 if (fc->destroy)
1778 fuse_send_destroy(fm);
1779
1780 fuse_abort_conn(fc);
1781 fuse_wait_aborted(fc);
Miklos Szeredi413daa12020-10-09 12:40:11 +02001782
1783 if (!list_empty(&fc->entry)) {
1784 mutex_lock(&fuse_mutex);
1785 list_del(&fc->entry);
1786 fuse_ctl_remove_conn(fc);
1787 mutex_unlock(&fuse_mutex);
John Muir3b463ae2009-05-31 11:13:57 -04001788 }
1789}
Max Reitzfcee2162020-05-06 17:44:12 +02001790EXPORT_SYMBOL_GPL(fuse_conn_destroy);
John Muir3b463ae2009-05-31 11:13:57 -04001791
Miklos Szeredi6a68d1e2020-11-11 17:22:32 +01001792static void fuse_sb_destroy(struct super_block *sb)
Miklos Szeredie8f3bd72018-07-26 16:13:11 +02001793{
Max Reitzfcee2162020-05-06 17:44:12 +02001794 struct fuse_mount *fm = get_fuse_mount_super(sb);
1795 bool last;
1796
Miklos Szeredid534d312021-10-21 10:01:38 +02001797 if (sb->s_root) {
Max Reitzfcee2162020-05-06 17:44:12 +02001798 last = fuse_mount_remove(fm);
1799 if (last)
1800 fuse_conn_destroy(fm);
1801 }
Miklos Szeredi6a68d1e2020-11-11 17:22:32 +01001802}
1803
Miklos Szeredia27c0612021-10-21 10:01:38 +02001804void fuse_mount_destroy(struct fuse_mount *fm)
1805{
Miklos Szeredi80019f12021-10-21 10:01:39 +02001806 fuse_conn_put(fm->fc);
1807 kfree(fm);
Miklos Szeredia27c0612021-10-21 10:01:38 +02001808}
1809EXPORT_SYMBOL(fuse_mount_destroy);
1810
Miklos Szeredi6a68d1e2020-11-11 17:22:32 +01001811static void fuse_kill_sb_anon(struct super_block *sb)
1812{
1813 fuse_sb_destroy(sb);
John Muir3b463ae2009-05-31 11:13:57 -04001814 kill_anon_super(sb);
Miklos Szeredia27c0612021-10-21 10:01:38 +02001815 fuse_mount_destroy(get_fuse_mount_super(sb));
John Muir3b463ae2009-05-31 11:13:57 -04001816}
1817
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001818static struct file_system_type fuse_fs_type = {
1819 .owner = THIS_MODULE,
1820 .name = "fuse",
Eric W. Biederman4ad769f2018-05-29 09:04:46 -05001821 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
David Howellsc30da2e2019-03-25 16:38:31 +00001822 .init_fs_context = fuse_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04001823 .parameters = fuse_fs_parameters,
John Muir3b463ae2009-05-31 11:13:57 -04001824 .kill_sb = fuse_kill_sb_anon,
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001825};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001826MODULE_ALIAS_FS("fuse");
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001827
1828#ifdef CONFIG_BLOCK
John Muir3b463ae2009-05-31 11:13:57 -04001829static void fuse_kill_sb_blk(struct super_block *sb)
1830{
Miklos Szeredi6a68d1e2020-11-11 17:22:32 +01001831 fuse_sb_destroy(sb);
John Muir3b463ae2009-05-31 11:13:57 -04001832 kill_block_super(sb);
Miklos Szeredia27c0612021-10-21 10:01:38 +02001833 fuse_mount_destroy(get_fuse_mount_super(sb));
John Muir3b463ae2009-05-31 11:13:57 -04001834}
1835
Miklos Szeredid6392f82006-12-06 20:35:44 -08001836static struct file_system_type fuseblk_fs_type = {
1837 .owner = THIS_MODULE,
1838 .name = "fuseblk",
David Howellsc30da2e2019-03-25 16:38:31 +00001839 .init_fs_context = fuse_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04001840 .parameters = fuse_fs_parameters,
John Muir3b463ae2009-05-31 11:13:57 -04001841 .kill_sb = fuse_kill_sb_blk,
Alexey Dobriyanedad01e2007-06-16 10:16:05 -07001842 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
Miklos Szeredid6392f82006-12-06 20:35:44 -08001843};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001844MODULE_ALIAS_FS("fuseblk");
Miklos Szeredid6392f82006-12-06 20:35:44 -08001845
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001846static inline int register_fuseblk(void)
1847{
1848 return register_filesystem(&fuseblk_fs_type);
1849}
1850
1851static inline void unregister_fuseblk(void)
1852{
1853 unregister_filesystem(&fuseblk_fs_type);
1854}
1855#else
1856static inline int register_fuseblk(void)
1857{
1858 return 0;
1859}
1860
1861static inline void unregister_fuseblk(void)
1862{
1863}
1864#endif
1865
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001866static void fuse_inode_init_once(void *foo)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001867{
Miklos Szeredi1729a162008-11-26 12:03:54 +01001868 struct inode *inode = foo;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001869
Christoph Lametera35afb82007-05-16 22:10:57 -07001870 inode_init_once(inode);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001871}
1872
1873static int __init fuse_fs_init(void)
1874{
1875 int err;
1876
Miklos Szeredid6392f82006-12-06 20:35:44 -08001877 fuse_inode_cachep = kmem_cache_create("fuse_inode",
Johannes Weinerdf206982017-11-15 17:38:34 -08001878 sizeof(struct fuse_inode), 0,
1879 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
1880 fuse_inode_init_once);
Miklos Szeredid6392f82006-12-06 20:35:44 -08001881 err = -ENOMEM;
1882 if (!fuse_inode_cachep)
Al Viro988f0322011-12-13 12:25:27 -05001883 goto out;
1884
1885 err = register_fuseblk();
1886 if (err)
1887 goto out2;
1888
1889 err = register_filesystem(&fuse_fs_type);
1890 if (err)
1891 goto out3;
Miklos Szeredid6392f82006-12-06 20:35:44 -08001892
1893 return 0;
1894
Al Viro988f0322011-12-13 12:25:27 -05001895 out3:
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001896 unregister_fuseblk();
Al Viro988f0322011-12-13 12:25:27 -05001897 out2:
1898 kmem_cache_destroy(fuse_inode_cachep);
Miklos Szeredid6392f82006-12-06 20:35:44 -08001899 out:
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001900 return err;
1901}
1902
1903static void fuse_fs_cleanup(void)
1904{
1905 unregister_filesystem(&fuse_fs_type);
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001906 unregister_fuseblk();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001907
1908 /*
1909 * Make sure all delayed rcu free inodes are flushed before we
1910 * destroy cache.
1911 */
1912 rcu_barrier();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001913 kmem_cache_destroy(fuse_inode_cachep);
1914}
1915
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001916static struct kobject *fuse_kobj;
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001917
Miklos Szeredif543f252006-01-16 22:14:35 -08001918static int fuse_sysfs_init(void)
1919{
1920 int err;
1921
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06001922 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001923 if (!fuse_kobj) {
1924 err = -ENOMEM;
Miklos Szeredif543f252006-01-16 22:14:35 -08001925 goto out_err;
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001926 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001927
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001928 err = sysfs_create_mount_point(fuse_kobj, "connections");
1929 if (err)
Miklos Szeredif543f252006-01-16 22:14:35 -08001930 goto out_fuse_unregister;
1931
1932 return 0;
1933
1934 out_fuse_unregister:
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -08001935 kobject_put(fuse_kobj);
Miklos Szeredif543f252006-01-16 22:14:35 -08001936 out_err:
1937 return err;
1938}
1939
1940static void fuse_sysfs_cleanup(void)
1941{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -05001942 sysfs_remove_mount_point(fuse_kobj, "connections");
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -08001943 kobject_put(fuse_kobj);
Miklos Szeredif543f252006-01-16 22:14:35 -08001944}
1945
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001946static int __init fuse_init(void)
1947{
1948 int res;
1949
Kirill Smelkovf2294482019-03-27 09:15:17 +00001950 pr_info("init (API version %i.%i)\n",
1951 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001952
Miklos Szeredibafa9652006-06-25 05:48:51 -07001953 INIT_LIST_HEAD(&fuse_conn_list);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001954 res = fuse_fs_init();
1955 if (res)
1956 goto err;
1957
Miklos Szeredi334f4852005-09-09 13:10:27 -07001958 res = fuse_dev_init();
1959 if (res)
1960 goto err_fs_cleanup;
1961
Miklos Szeredif543f252006-01-16 22:14:35 -08001962 res = fuse_sysfs_init();
1963 if (res)
1964 goto err_dev_cleanup;
1965
Miklos Szeredibafa9652006-06-25 05:48:51 -07001966 res = fuse_ctl_init();
1967 if (res)
1968 goto err_sysfs_cleanup;
1969
Csaba Henk487ea5a2009-08-26 19:17:22 +02001970 sanitize_global_limit(&max_user_bgreq);
1971 sanitize_global_limit(&max_user_congthresh);
1972
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001973 return 0;
1974
Miklos Szeredibafa9652006-06-25 05:48:51 -07001975 err_sysfs_cleanup:
1976 fuse_sysfs_cleanup();
Miklos Szeredif543f252006-01-16 22:14:35 -08001977 err_dev_cleanup:
1978 fuse_dev_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -07001979 err_fs_cleanup:
1980 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001981 err:
1982 return res;
1983}
1984
1985static void __exit fuse_exit(void)
1986{
Kirill Smelkovf2294482019-03-27 09:15:17 +00001987 pr_debug("exit\n");
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001988
Miklos Szeredibafa9652006-06-25 05:48:51 -07001989 fuse_ctl_cleanup();
Miklos Szeredif543f252006-01-16 22:14:35 -08001990 fuse_sysfs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001991 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -07001992 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001993}
1994
1995module_init(fuse_init);
1996module_exit(fuse_exit);