fuse: Allow fuse_fill_super_common() for submounts
[linux-block.git] / fs / fuse / inode.c
CommitLineData
d8a5ba45
MS
1/*
2 FUSE: Filesystem in Userspace
1729a16c 3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
d8a5ba45
MS
4
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>
d8a5ba45
MS
14#include <linux/seq_file.h>
15#include <linux/init.h>
16#include <linux/module.h>
487ea5af 17#include <linux/moduleparam.h>
c30da2e9
DH
18#include <linux/fs_context.h>
19#include <linux/fs_parser.h>
d8a5ba45 20#include <linux/statfs.h>
9c8ef561 21#include <linux/random.h>
e8edc6e0 22#include <linux/sched.h>
dbd561d2 23#include <linux/exportfs.h>
60bcc88a 24#include <linux/posix_acl.h>
0b6e9ea0 25#include <linux/pid_namespace.h>
d8a5ba45
MS
26
27MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
28MODULE_DESCRIPTION("Filesystem in Userspace");
29MODULE_LICENSE("GPL");
30
e18b890b 31static struct kmem_cache *fuse_inode_cachep;
bafa9654
MS
32struct list_head fuse_conn_list;
33DEFINE_MUTEX(fuse_mutex);
d8a5ba45 34
e4dca7b7 35static int set_global_limit(const char *val, const struct kernel_param *kp);
487ea5af 36
79a9d994 37unsigned max_user_bgreq;
487ea5af
CH
38module_param_call(max_user_bgreq, set_global_limit, param_get_uint,
39 &max_user_bgreq, 0644);
40__MODULE_PARM_TYPE(max_user_bgreq, "uint");
41MODULE_PARM_DESC(max_user_bgreq,
42 "Global limit for the maximum number of backgrounded requests an "
43 "unprivileged user can set");
44
79a9d994 45unsigned max_user_congthresh;
487ea5af
CH
46module_param_call(max_user_congthresh, set_global_limit, param_get_uint,
47 &max_user_congthresh, 0644);
48__MODULE_PARM_TYPE(max_user_congthresh, "uint");
49MODULE_PARM_DESC(max_user_congthresh,
50 "Global limit for the maximum congestion threshold an "
51 "unprivileged user can set");
52
d8a5ba45
MS
53#define FUSE_SUPER_MAGIC 0x65735546
54
d1875dba
MS
55#define FUSE_DEFAULT_BLKSIZE 512
56
7a6d3c8b
CH
57/** Maximum number of outstanding background requests */
58#define FUSE_DEFAULT_MAX_BACKGROUND 12
59
60/** Congestion starts at 75% of maximum */
61#define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
62
c30da2e9
DH
63#ifdef CONFIG_BLOCK
64static struct file_system_type fuseblk_fs_type;
65#endif
66
a2daff68 67struct fuse_forget_link *fuse_alloc_forget(void)
07e77dca 68{
dc69e98c 69 return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
07e77dca
MS
70}
71
d8a5ba45
MS
72static struct inode *fuse_alloc_inode(struct super_block *sb)
73{
d8a5ba45
MS
74 struct fuse_inode *fi;
75
9031a69c 76 fi = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
77 if (!fi)
d8a5ba45
MS
78 return NULL;
79
0a0898cf 80 fi->i_time = 0;
2f1e8196 81 fi->inval_mask = 0;
d8a5ba45 82 fi->nodeid = 0;
9e6268db 83 fi->nlookup = 0;
fbee36b9 84 fi->attr_version = 0;
45c72cd7 85 fi->orig_ino = 0;
4582a4ab 86 fi->state = 0;
5c672ab3 87 mutex_init(&fi->mutex);
6ae330ca 88 init_rwsem(&fi->i_mmap_sem);
f15ecfef 89 spin_lock_init(&fi->lock);
07e77dca 90 fi->forget = fuse_alloc_forget();
c2d0ad00
VG
91 if (!fi->forget)
92 goto out_free;
93
94 if (IS_ENABLED(CONFIG_FUSE_DAX) && !fuse_dax_inode_alloc(sb, fi))
95 goto out_free_forget;
d8a5ba45 96
9031a69c 97 return &fi->inode;
c2d0ad00
VG
98
99out_free_forget:
100 kfree(fi->forget);
101out_free:
102 kmem_cache_free(fuse_inode_cachep, fi);
103 return NULL;
d8a5ba45
MS
104}
105
9baf28bb 106static void fuse_free_inode(struct inode *inode)
d8a5ba45 107{
e5e5558e 108 struct fuse_inode *fi = get_fuse_inode(inode);
9baf28bb 109
5c672ab3 110 mutex_destroy(&fi->mutex);
07e77dca 111 kfree(fi->forget);
c2d0ad00
VG
112#ifdef CONFIG_FUSE_DAX
113 kfree(fi->dax);
114#endif
9baf28bb 115 kmem_cache_free(fuse_inode_cachep, fi);
d8a5ba45
MS
116}
117
b57922d9 118static void fuse_evict_inode(struct inode *inode)
d8a5ba45 119{
9baf28bb
AV
120 struct fuse_inode *fi = get_fuse_inode(inode);
121
91b0abe3 122 truncate_inode_pages_final(&inode->i_data);
dbd5768f 123 clear_inode(inode);
1751e8a6 124 if (inode->i_sb->s_flags & SB_ACTIVE) {
1e9a4ed9 125 struct fuse_conn *fc = get_fuse_conn(inode);
c2d0ad00
VG
126
127 if (FUSE_IS_DAX(inode))
128 fuse_dax_inode_cleanup(inode);
1866d779
MR
129 if (fi->nlookup) {
130 fuse_queue_forget(fc, fi->forget, fi->nodeid,
131 fi->nlookup);
132 fi->forget = NULL;
133 }
e5e5558e 134 }
9baf28bb
AV
135 if (S_ISREG(inode->i_mode) && !is_bad_inode(inode)) {
136 WARN_ON(!list_empty(&fi->write_files));
137 WARN_ON(!list_empty(&fi->queued_writes));
138 }
d8a5ba45
MS
139}
140
0189a2d3 141static int fuse_reconfigure(struct fs_context *fc)
71421259 142{
0189a2d3
MS
143 struct super_block *sb = fc->root->d_sb;
144
02b9984d 145 sync_filesystem(sb);
0189a2d3 146 if (fc->sb_flags & SB_MANDLOCK)
71421259
MS
147 return -EINVAL;
148
149 return 0;
150}
151
45c72cd7
PS
152/*
153 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
154 * so that it will fit.
155 */
156static ino_t fuse_squash_ino(u64 ino64)
157{
158 ino_t ino = (ino_t) ino64;
159 if (sizeof(ino_t) < sizeof(u64))
160 ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8;
161 return ino;
162}
163
3be5a52b
MS
164void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
165 u64 attr_valid)
d8a5ba45 166{
9ffbb916 167 struct fuse_conn *fc = get_fuse_conn(inode);
ebc14c4d 168 struct fuse_inode *fi = get_fuse_inode(inode);
d8a5ba45 169
f15ecfef
KT
170 lockdep_assert_held(&fi->lock);
171
4510d86f 172 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1fb69e78 173 fi->i_time = attr_valid;
2f1e8196 174 WRITE_ONCE(fi->inval_mask, 0);
1fb69e78 175
45c72cd7 176 inode->i_ino = fuse_squash_ino(attr->ino);
ebc14c4d 177 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
bfe86848 178 set_nlink(inode, attr->nlink);
8cb08329
EB
179 inode->i_uid = make_kuid(fc->user_ns, attr->uid);
180 inode->i_gid = make_kgid(fc->user_ns, attr->gid);
d8a5ba45
MS
181 inode->i_blocks = attr->blocks;
182 inode->i_atime.tv_sec = attr->atime;
183 inode->i_atime.tv_nsec = attr->atimensec;
b0aa7606
MP
184 /* mtime from server may be stale due to local buffered write */
185 if (!fc->writeback_cache || !S_ISREG(inode->i_mode)) {
186 inode->i_mtime.tv_sec = attr->mtime;
187 inode->i_mtime.tv_nsec = attr->mtimensec;
31f3267b
MP
188 inode->i_ctime.tv_sec = attr->ctime;
189 inode->i_ctime.tv_nsec = attr->ctimensec;
b0aa7606 190 }
e00d2c2d 191
0e9663ee
MS
192 if (attr->blksize != 0)
193 inode->i_blkbits = ilog2(attr->blksize);
194 else
195 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
196
ebc14c4d
MS
197 /*
198 * Don't set the sticky bit in i_mode, unless we want the VFS
199 * to check permissions. This prevents failures due to the
200 * check in may_delete().
201 */
202 fi->orig_i_mode = inode->i_mode;
29433a29 203 if (!fc->default_permissions)
ebc14c4d 204 inode->i_mode &= ~S_ISVTX;
45c72cd7
PS
205
206 fi->orig_ino = attr->ino;
3be5a52b
MS
207}
208
209void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
210 u64 attr_valid, u64 attr_version)
211{
212 struct fuse_conn *fc = get_fuse_conn(inode);
213 struct fuse_inode *fi = get_fuse_inode(inode);
8373200b 214 bool is_wb = fc->writeback_cache;
3be5a52b 215 loff_t oldsize;
a64ba10f 216 struct timespec64 old_mtime;
3be5a52b 217
f15ecfef 218 spin_lock(&fi->lock);
06a7c3c2
MP
219 if ((attr_version != 0 && fi->attr_version > attr_version) ||
220 test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
f15ecfef 221 spin_unlock(&fi->lock);
3be5a52b
MS
222 return;
223 }
224
a64ba10f 225 old_mtime = inode->i_mtime;
3be5a52b 226 fuse_change_attributes_common(inode, attr, attr_valid);
ebc14c4d 227
e00d2c2d 228 oldsize = inode->i_size;
8373200b
PE
229 /*
230 * In case of writeback_cache enabled, the cached writes beyond EOF
231 * extend local i_size without keeping userspace server in sync. So,
232 * attr->size coming from server can be stale. We cannot trust it.
233 */
234 if (!is_wb || !S_ISREG(inode->i_mode))
235 i_size_write(inode, attr->size);
f15ecfef 236 spin_unlock(&fi->lock);
e00d2c2d 237
8373200b 238 if (!is_wb && S_ISREG(inode->i_mode)) {
eed2179e
BF
239 bool inval = false;
240
241 if (oldsize != attr->size) {
7caef267 242 truncate_pagecache(inode, attr->size);
ad2ba64d
KS
243 if (!fc->explicit_inval_data)
244 inval = true;
eed2179e 245 } else if (fc->auto_inval_data) {
a64ba10f 246 struct timespec64 new_mtime = {
eed2179e
BF
247 .tv_sec = attr->mtime,
248 .tv_nsec = attr->mtimensec,
249 };
250
251 /*
252 * Auto inval mode also checks and invalidates if mtime
253 * has changed.
254 */
a64ba10f 255 if (!timespec64_equal(&old_mtime, &new_mtime))
eed2179e
BF
256 inval = true;
257 }
258
259 if (inval)
260 invalidate_inode_pages2(inode->i_mapping);
e00d2c2d 261 }
d8a5ba45
MS
262}
263
264static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
265{
266 inode->i_mode = attr->mode & S_IFMT;
9ffbb916 267 inode->i_size = attr->size;
b0aa7606
MP
268 inode->i_mtime.tv_sec = attr->mtime;
269 inode->i_mtime.tv_nsec = attr->mtimensec;
31f3267b
MP
270 inode->i_ctime.tv_sec = attr->ctime;
271 inode->i_ctime.tv_nsec = attr->ctimensec;
e5e5558e
MS
272 if (S_ISREG(inode->i_mode)) {
273 fuse_init_common(inode);
b6aeaded 274 fuse_init_file_inode(inode);
e5e5558e
MS
275 } else if (S_ISDIR(inode->i_mode))
276 fuse_init_dir(inode);
277 else if (S_ISLNK(inode->i_mode))
278 fuse_init_symlink(inode);
279 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
280 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
281 fuse_init_common(inode);
282 init_special_inode(inode, inode->i_mode,
283 new_decode_dev(attr->rdev));
39ee059a
MS
284 } else
285 BUG();
d8a5ba45
MS
286}
287
fcee216b 288static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
d8a5ba45 289{
b48badf0 290 u64 nodeid = *(u64 *) _nodeidp;
d8a5ba45
MS
291 if (get_node_id(inode) == nodeid)
292 return 1;
293 else
294 return 0;
295}
296
297static int fuse_inode_set(struct inode *inode, void *_nodeidp)
298{
b48badf0 299 u64 nodeid = *(u64 *) _nodeidp;
d8a5ba45
MS
300 get_fuse_inode(inode)->nodeid = nodeid;
301 return 0;
302}
303
b48badf0 304struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
1fb69e78
MS
305 int generation, struct fuse_attr *attr,
306 u64 attr_valid, u64 attr_version)
d8a5ba45
MS
307{
308 struct inode *inode;
9e6268db 309 struct fuse_inode *fi;
d8a5ba45 310 struct fuse_conn *fc = get_fuse_conn_super(sb);
d8a5ba45
MS
311
312 retry:
313 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
314 if (!inode)
315 return NULL;
316
317 if ((inode->i_state & I_NEW)) {
b0aa7606 318 inode->i_flags |= S_NOATIME;
d31433c8 319 if (!fc->writeback_cache || !S_ISREG(attr->mode))
b0aa7606 320 inode->i_flags |= S_NOCMTIME;
d8a5ba45 321 inode->i_generation = generation;
d8a5ba45
MS
322 fuse_init_inode(inode, attr);
323 unlock_new_inode(inode);
324 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
d8a5ba45
MS
325 /* Inode has changed type, any I/O on the old should fail */
326 make_bad_inode(inode);
327 iput(inode);
d8a5ba45
MS
328 goto retry;
329 }
330
9e6268db 331 fi = get_fuse_inode(inode);
c9d8f5f0 332 spin_lock(&fi->lock);
1729a16c 333 fi->nlookup++;
c9d8f5f0 334 spin_unlock(&fi->lock);
1fb69e78
MS
335 fuse_change_attributes(inode, attr, attr_valid, attr_version);
336
d8a5ba45
MS
337 return inode;
338}
339
fcee216b
MR
340struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
341 struct fuse_mount **fm)
342{
343 struct fuse_mount *fm_iter;
344 struct inode *inode;
345
346 WARN_ON(!rwsem_is_locked(&fc->killsb));
347 list_for_each_entry(fm_iter, &fc->mounts, fc_entry) {
348 if (!fm_iter->sb)
349 continue;
350
351 inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &nodeid);
352 if (inode) {
353 if (fm)
354 *fm = fm_iter;
355 return inode;
356 }
357 }
358
359 return NULL;
360}
361
362int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
3b463ae0
JM
363 loff_t offset, loff_t len)
364{
5ddd9ced 365 struct fuse_inode *fi;
3b463ae0
JM
366 struct inode *inode;
367 pgoff_t pg_start;
368 pgoff_t pg_end;
369
fcee216b 370 inode = fuse_ilookup(fc, nodeid, NULL);
3b463ae0
JM
371 if (!inode)
372 return -ENOENT;
373
5ddd9ced
MS
374 fi = get_fuse_inode(inode);
375 spin_lock(&fi->lock);
376 fi->attr_version = atomic64_inc_return(&fc->attr_version);
377 spin_unlock(&fi->lock);
378
3b463ae0 379 fuse_invalidate_attr(inode);
60bcc88a 380 forget_all_cached_acls(inode);
3b463ae0 381 if (offset >= 0) {
09cbfeaf 382 pg_start = offset >> PAGE_SHIFT;
3b463ae0
JM
383 if (len <= 0)
384 pg_end = -1;
385 else
09cbfeaf 386 pg_end = (offset + len - 1) >> PAGE_SHIFT;
3b463ae0
JM
387 invalidate_inode_pages2_range(inode->i_mapping,
388 pg_start, pg_end);
389 }
390 iput(inode);
391 return 0;
392}
393
63576c13 394bool fuse_lock_inode(struct inode *inode)
5c672ab3 395{
63576c13
MS
396 bool locked = false;
397
398 if (!get_fuse_conn(inode)->parallel_dirops) {
5c672ab3 399 mutex_lock(&get_fuse_inode(inode)->mutex);
63576c13
MS
400 locked = true;
401 }
402
403 return locked;
5c672ab3
MS
404}
405
63576c13 406void fuse_unlock_inode(struct inode *inode, bool locked)
5c672ab3 407{
63576c13 408 if (locked)
5c672ab3
MS
409 mutex_unlock(&get_fuse_inode(inode)->mutex);
410}
411
42faad99 412static void fuse_umount_begin(struct super_block *sb)
69a53bf2 413{
15c8e72e
VG
414 struct fuse_conn *fc = get_fuse_conn_super(sb);
415
416 if (!fc->no_force_umount)
417 fuse_abort_conn(fc);
69a53bf2
MS
418}
419
fcee216b 420static void fuse_send_destroy(struct fuse_mount *fm)
0ec7ca41 421{
fcee216b 422 if (fm->fc->conn_init) {
1ccd1ea2
MS
423 FUSE_ARGS(args);
424
425 args.opcode = FUSE_DESTROY;
426 args.force = true;
427 args.nocreds = true;
fcee216b 428 fuse_simple_request(fm, &args);
0ec7ca41
MS
429 }
430}
431
a325f9b9
TH
432static void fuse_put_super(struct super_block *sb)
433{
fcee216b 434 struct fuse_mount *fm = get_fuse_mount_super(sb);
a325f9b9 435
fcee216b 436 fuse_mount_put(fm);
d8a5ba45
MS
437}
438
e5e5558e
MS
439static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
440{
441 stbuf->f_type = FUSE_SUPER_MAGIC;
442 stbuf->f_bsize = attr->bsize;
de5f1202 443 stbuf->f_frsize = attr->frsize;
e5e5558e
MS
444 stbuf->f_blocks = attr->blocks;
445 stbuf->f_bfree = attr->bfree;
446 stbuf->f_bavail = attr->bavail;
447 stbuf->f_files = attr->files;
448 stbuf->f_ffree = attr->ffree;
449 stbuf->f_namelen = attr->namelen;
450 /* fsid is left zero */
451}
452
726c3342 453static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
e5e5558e 454{
726c3342 455 struct super_block *sb = dentry->d_sb;
fcee216b 456 struct fuse_mount *fm = get_fuse_mount_super(sb);
7078187a 457 FUSE_ARGS(args);
e5e5558e
MS
458 struct fuse_statfs_out outarg;
459 int err;
460
fcee216b 461 if (!fuse_allow_current_process(fm->fc)) {
e57ac683
MS
462 buf->f_type = FUSE_SUPER_MAGIC;
463 return 0;
464 }
465
de5f1202 466 memset(&outarg, 0, sizeof(outarg));
d5b48543
MS
467 args.in_numargs = 0;
468 args.opcode = FUSE_STATFS;
469 args.nodeid = get_node_id(d_inode(dentry));
470 args.out_numargs = 1;
471 args.out_args[0].size = sizeof(outarg);
472 args.out_args[0].value = &outarg;
fcee216b 473 err = fuse_simple_request(fm, &args);
e5e5558e
MS
474 if (!err)
475 convert_fuse_statfs(buf, &outarg.st);
e5e5558e
MS
476 return err;
477}
478
d8a5ba45 479enum {
c30da2e9
DH
480 OPT_SOURCE,
481 OPT_SUBTYPE,
d8a5ba45
MS
482 OPT_FD,
483 OPT_ROOTMODE,
484 OPT_USER_ID,
87729a55 485 OPT_GROUP_ID,
d8a5ba45
MS
486 OPT_DEFAULT_PERMISSIONS,
487 OPT_ALLOW_OTHER,
db50b96c 488 OPT_MAX_READ,
d8091614 489 OPT_BLKSIZE,
d8a5ba45
MS
490 OPT_ERR
491};
492
d7167b14 493static const struct fs_parameter_spec fuse_fs_parameters[] = {
c30da2e9
DH
494 fsparam_string ("source", OPT_SOURCE),
495 fsparam_u32 ("fd", OPT_FD),
496 fsparam_u32oct ("rootmode", OPT_ROOTMODE),
497 fsparam_u32 ("user_id", OPT_USER_ID),
498 fsparam_u32 ("group_id", OPT_GROUP_ID),
499 fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS),
500 fsparam_flag ("allow_other", OPT_ALLOW_OTHER),
501 fsparam_u32 ("max_read", OPT_MAX_READ),
502 fsparam_u32 ("blksize", OPT_BLKSIZE),
c7eb6869 503 fsparam_string ("subtype", OPT_SUBTYPE),
c30da2e9
DH
504 {}
505};
506
c30da2e9 507static int fuse_parse_param(struct fs_context *fc, struct fs_parameter *param)
233a01fa 508{
c30da2e9
DH
509 struct fs_parse_result result;
510 struct fuse_fs_context *ctx = fc->fs_private;
511 int opt;
512
b330966f
MS
513 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
514 /*
515 * Ignore options coming from mount(MS_REMOUNT) for backward
516 * compatibility.
517 */
518 if (fc->oldapi)
519 return 0;
520
521 return invalfc(fc, "No changes allowed in reconfigure");
522 }
e8b20a47 523
d7167b14 524 opt = fs_parse(fc, fuse_fs_parameters, param, &result);
c30da2e9
DH
525 if (opt < 0)
526 return opt;
527
528 switch (opt) {
529 case OPT_SOURCE:
530 if (fc->source)
2e28c49e 531 return invalfc(fc, "Multiple sources specified");
c30da2e9
DH
532 fc->source = param->string;
533 param->string = NULL;
534 break;
535
536 case OPT_SUBTYPE:
537 if (ctx->subtype)
2e28c49e 538 return invalfc(fc, "Multiple subtypes specified");
c30da2e9
DH
539 ctx->subtype = param->string;
540 param->string = NULL;
541 return 0;
542
543 case OPT_FD:
544 ctx->fd = result.uint_32;
cabdb4fa 545 ctx->fd_present = true;
c30da2e9
DH
546 break;
547
548 case OPT_ROOTMODE:
549 if (!fuse_valid_type(result.uint_32))
2e28c49e 550 return invalfc(fc, "Invalid rootmode");
c30da2e9 551 ctx->rootmode = result.uint_32;
cabdb4fa 552 ctx->rootmode_present = true;
c30da2e9
DH
553 break;
554
555 case OPT_USER_ID:
556 ctx->user_id = make_kuid(fc->user_ns, result.uint_32);
557 if (!uid_valid(ctx->user_id))
2e28c49e 558 return invalfc(fc, "Invalid user_id");
cabdb4fa 559 ctx->user_id_present = true;
c30da2e9
DH
560 break;
561
562 case OPT_GROUP_ID:
563 ctx->group_id = make_kgid(fc->user_ns, result.uint_32);
564 if (!gid_valid(ctx->group_id))
2e28c49e 565 return invalfc(fc, "Invalid group_id");
cabdb4fa 566 ctx->group_id_present = true;
c30da2e9
DH
567 break;
568
569 case OPT_DEFAULT_PERMISSIONS:
cabdb4fa 570 ctx->default_permissions = true;
c30da2e9
DH
571 break;
572
573 case OPT_ALLOW_OTHER:
cabdb4fa 574 ctx->allow_other = true;
c30da2e9
DH
575 break;
576
577 case OPT_MAX_READ:
578 ctx->max_read = result.uint_32;
579 break;
580
581 case OPT_BLKSIZE:
582 if (!ctx->is_bdev)
2e28c49e 583 return invalfc(fc, "blksize only supported for fuseblk");
c30da2e9
DH
584 ctx->blksize = result.uint_32;
585 break;
586
587 default:
588 return -EINVAL;
233a01fa 589 }
c30da2e9
DH
590
591 return 0;
233a01fa
MS
592}
593
c30da2e9 594static void fuse_free_fc(struct fs_context *fc)
d8a5ba45 595{
c30da2e9 596 struct fuse_fs_context *ctx = fc->fs_private;
5a533682 597
c30da2e9
DH
598 if (ctx) {
599 kfree(ctx->subtype);
600 kfree(ctx);
601 }
d8a5ba45
MS
602}
603
34c80b1d 604static int fuse_show_options(struct seq_file *m, struct dentry *root)
d8a5ba45 605{
34c80b1d
AV
606 struct super_block *sb = root->d_sb;
607 struct fuse_conn *fc = get_fuse_conn_super(sb);
d8a5ba45 608
f4fd4ae3
VG
609 if (fc->legacy_opts_show) {
610 seq_printf(m, ",user_id=%u",
611 from_kuid_munged(fc->user_ns, fc->user_id));
612 seq_printf(m, ",group_id=%u",
613 from_kgid_munged(fc->user_ns, fc->group_id));
614 if (fc->default_permissions)
615 seq_puts(m, ",default_permissions");
616 if (fc->allow_other)
617 seq_puts(m, ",allow_other");
618 if (fc->max_read != ~0)
619 seq_printf(m, ",max_read=%u", fc->max_read);
620 if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
621 seq_printf(m, ",blksize=%lu", sb->s_blocksize);
622 }
1dd53957
VG
623#ifdef CONFIG_FUSE_DAX
624 if (fc->dax)
625 seq_puts(m, ",dax");
626#endif
627
d8a5ba45
MS
628 return 0;
629}
630
ae3aad77
SH
631static void fuse_iqueue_init(struct fuse_iqueue *fiq,
632 const struct fuse_iqueue_ops *ops,
633 void *priv)
f88996a9
MS
634{
635 memset(fiq, 0, sizeof(struct fuse_iqueue));
76e43c8c 636 spin_lock_init(&fiq->lock);
f88996a9
MS
637 init_waitqueue_head(&fiq->waitq);
638 INIT_LIST_HEAD(&fiq->pending);
639 INIT_LIST_HEAD(&fiq->interrupts);
640 fiq->forget_list_tail = &fiq->forget_list_head;
e16714d8 641 fiq->connected = 1;
ae3aad77
SH
642 fiq->ops = ops;
643 fiq->priv = priv;
f88996a9
MS
644}
645
3a2b5b9c
MS
646static void fuse_pqueue_init(struct fuse_pqueue *fpq)
647{
be2ff42c
KT
648 unsigned int i;
649
45a91cb1 650 spin_lock_init(&fpq->lock);
be2ff42c
KT
651 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
652 INIT_LIST_HEAD(&fpq->processing[i]);
3a2b5b9c 653 INIT_LIST_HEAD(&fpq->io);
e96edd94 654 fpq->connected = 1;
3a2b5b9c
MS
655}
656
fcee216b
MR
657void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
658 struct user_namespace *user_ns,
ae3aad77 659 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
d8a5ba45 660{
0d179aa5
TH
661 memset(fc, 0, sizeof(*fc));
662 spin_lock_init(&fc->lock);
ae2dffa3 663 spin_lock_init(&fc->bg_lock);
3b463ae0 664 init_rwsem(&fc->killsb);
095fc40a 665 refcount_set(&fc->count, 1);
c3696046 666 atomic_set(&fc->dev_count, 1);
0d179aa5 667 init_waitqueue_head(&fc->blocked_waitq);
ae3aad77 668 fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
0d179aa5
TH
669 INIT_LIST_HEAD(&fc->bg_queue);
670 INIT_LIST_HEAD(&fc->entry);
cc080e9e 671 INIT_LIST_HEAD(&fc->devices);
0d179aa5 672 atomic_set(&fc->num_waiting, 0);
7a6d3c8b
CH
673 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
674 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
75126f55 675 atomic64_set(&fc->khctr, 0);
0d179aa5 676 fc->polled_files = RB_ROOT;
0aada884 677 fc->blocked = 0;
796523fb 678 fc->initialized = 0;
e16714d8 679 fc->connected = 1;
4510d86f 680 atomic64_set(&fc->attr_version, 1);
0d179aa5 681 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
0b6e9ea0 682 fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
8cb08329 683 fc->user_ns = get_user_ns(user_ns);
8a3177db 684 fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
fcee216b
MR
685
686 INIT_LIST_HEAD(&fc->mounts);
687 list_add(&fm->fc_entry, &fc->mounts);
688 fm->fc = fc;
689 refcount_set(&fm->count, 1);
d8a5ba45 690}
0d179aa5 691EXPORT_SYMBOL_GPL(fuse_conn_init);
d8a5ba45 692
bafa9654
MS
693void fuse_conn_put(struct fuse_conn *fc)
694{
095fc40a 695 if (refcount_dec_and_test(&fc->count)) {
a62a8ef9
SH
696 struct fuse_iqueue *fiq = &fc->iq;
697
1dd53957
VG
698 if (IS_ENABLED(CONFIG_FUSE_DAX))
699 fuse_dax_conn_free(fc);
a62a8ef9
SH
700 if (fiq->ops->release)
701 fiq->ops->release(fiq);
0b6e9ea0 702 put_pid_ns(fc->pid_ns);
8cb08329 703 put_user_ns(fc->user_ns);
43901aab 704 fc->release(fc);
d2a85164 705 }
bafa9654 706}
08cbf542 707EXPORT_SYMBOL_GPL(fuse_conn_put);
bafa9654
MS
708
709struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
710{
095fc40a 711 refcount_inc(&fc->count);
bafa9654
MS
712 return fc;
713}
08cbf542 714EXPORT_SYMBOL_GPL(fuse_conn_get);
bafa9654 715
fcee216b
MR
716void fuse_mount_put(struct fuse_mount *fm)
717{
718 if (refcount_dec_and_test(&fm->count)) {
719 if (fm->fc)
720 fuse_conn_put(fm->fc);
721 kfree(fm);
722 }
723}
724EXPORT_SYMBOL_GPL(fuse_mount_put);
725
726struct fuse_mount *fuse_mount_get(struct fuse_mount *fm)
727{
728 refcount_inc(&fm->count);
729 return fm;
730}
731EXPORT_SYMBOL_GPL(fuse_mount_get);
732
b93f858a 733static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
d8a5ba45
MS
734{
735 struct fuse_attr attr;
736 memset(&attr, 0, sizeof(attr));
737
738 attr.mode = mode;
739 attr.ino = FUSE_ROOT_ID;
074406fa 740 attr.nlink = 1;
1fb69e78 741 return fuse_iget(sb, 1, 0, &attr, 0, 0);
d8a5ba45
MS
742}
743
1729a16c 744struct fuse_inode_handle {
dbd561d2
MS
745 u64 nodeid;
746 u32 generation;
747};
748
749static struct dentry *fuse_get_dentry(struct super_block *sb,
750 struct fuse_inode_handle *handle)
751{
33670fa2 752 struct fuse_conn *fc = get_fuse_conn_super(sb);
dbd561d2
MS
753 struct inode *inode;
754 struct dentry *entry;
755 int err = -ESTALE;
756
757 if (handle->nodeid == 0)
758 goto out_err;
759
760 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
33670fa2
MS
761 if (!inode) {
762 struct fuse_entry_out outarg;
13983d06 763 const struct qstr name = QSTR_INIT(".", 1);
33670fa2
MS
764
765 if (!fc->export_support)
766 goto out_err;
767
33670fa2
MS
768 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
769 &inode);
770 if (err && err != -ENOENT)
771 goto out_err;
772 if (err || !inode) {
773 err = -ESTALE;
774 goto out_err;
775 }
776 err = -EIO;
777 if (get_node_id(inode) != handle->nodeid)
778 goto out_iput;
779 }
dbd561d2
MS
780 err = -ESTALE;
781 if (inode->i_generation != handle->generation)
782 goto out_iput;
783
44003728 784 entry = d_obtain_alias(inode);
c35eebe9 785 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID)
dbd561d2 786 fuse_invalidate_entry_cache(entry);
dbd561d2
MS
787
788 return entry;
789
790 out_iput:
791 iput(inode);
792 out_err:
793 return ERR_PTR(err);
794}
795
b0b0382b
AV
796static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len,
797 struct inode *parent)
dbd561d2 798{
b0b0382b 799 int len = parent ? 6 : 3;
dbd561d2
MS
800 u64 nodeid;
801 u32 generation;
802
5fe0c237
AK
803 if (*max_len < len) {
804 *max_len = len;
94e07a75 805 return FILEID_INVALID;
5fe0c237 806 }
dbd561d2
MS
807
808 nodeid = get_fuse_inode(inode)->nodeid;
809 generation = inode->i_generation;
810
811 fh[0] = (u32)(nodeid >> 32);
812 fh[1] = (u32)(nodeid & 0xffffffff);
813 fh[2] = generation;
814
b0b0382b 815 if (parent) {
dbd561d2
MS
816 nodeid = get_fuse_inode(parent)->nodeid;
817 generation = parent->i_generation;
dbd561d2
MS
818
819 fh[3] = (u32)(nodeid >> 32);
820 fh[4] = (u32)(nodeid & 0xffffffff);
821 fh[5] = generation;
822 }
823
824 *max_len = len;
b0b0382b 825 return parent ? 0x82 : 0x81;
dbd561d2
MS
826}
827
828static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
829 struct fid *fid, int fh_len, int fh_type)
830{
831 struct fuse_inode_handle handle;
832
833 if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
834 return NULL;
835
836 handle.nodeid = (u64) fid->raw[0] << 32;
837 handle.nodeid |= (u64) fid->raw[1];
838 handle.generation = fid->raw[2];
839 return fuse_get_dentry(sb, &handle);
840}
841
842static struct dentry *fuse_fh_to_parent(struct super_block *sb,
843 struct fid *fid, int fh_len, int fh_type)
844{
845 struct fuse_inode_handle parent;
846
847 if (fh_type != 0x82 || fh_len < 6)
848 return NULL;
849
850 parent.nodeid = (u64) fid->raw[3] << 32;
851 parent.nodeid |= (u64) fid->raw[4];
852 parent.generation = fid->raw[5];
853 return fuse_get_dentry(sb, &parent);
854}
855
33670fa2
MS
856static struct dentry *fuse_get_parent(struct dentry *child)
857{
2b0143b5 858 struct inode *child_inode = d_inode(child);
33670fa2
MS
859 struct fuse_conn *fc = get_fuse_conn(child_inode);
860 struct inode *inode;
861 struct dentry *parent;
862 struct fuse_entry_out outarg;
13983d06 863 const struct qstr name = QSTR_INIT("..", 2);
33670fa2
MS
864 int err;
865
866 if (!fc->export_support)
867 return ERR_PTR(-ESTALE);
868
33670fa2
MS
869 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
870 &name, &outarg, &inode);
44003728
CH
871 if (err) {
872 if (err == -ENOENT)
873 return ERR_PTR(-ESTALE);
33670fa2 874 return ERR_PTR(err);
33670fa2 875 }
44003728
CH
876
877 parent = d_obtain_alias(inode);
c35eebe9 878 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID)
33670fa2 879 fuse_invalidate_entry_cache(parent);
33670fa2
MS
880
881 return parent;
882}
dbd561d2
MS
883
884static const struct export_operations fuse_export_operations = {
885 .fh_to_dentry = fuse_fh_to_dentry,
886 .fh_to_parent = fuse_fh_to_parent,
887 .encode_fh = fuse_encode_fh,
33670fa2 888 .get_parent = fuse_get_parent,
dbd561d2
MS
889};
890
ee9b6d61 891static const struct super_operations fuse_super_operations = {
d8a5ba45 892 .alloc_inode = fuse_alloc_inode,
9baf28bb 893 .free_inode = fuse_free_inode,
b57922d9 894 .evict_inode = fuse_evict_inode,
1e18bda8 895 .write_inode = fuse_write_inode,
ead5f0b5 896 .drop_inode = generic_delete_inode,
d8a5ba45 897 .put_super = fuse_put_super,
69a53bf2 898 .umount_begin = fuse_umount_begin,
e5e5558e 899 .statfs = fuse_statfs,
d8a5ba45
MS
900 .show_options = fuse_show_options,
901};
902
487ea5af
CH
903static void sanitize_global_limit(unsigned *limit)
904{
f22f812d
MS
905 /*
906 * The default maximum number of async requests is calculated to consume
907 * 1/2^13 of the total memory, assuming 392 bytes per request.
908 */
487ea5af 909 if (*limit == 0)
f22f812d 910 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
487ea5af
CH
911
912 if (*limit >= 1 << 16)
913 *limit = (1 << 16) - 1;
914}
915
e4dca7b7 916static int set_global_limit(const char *val, const struct kernel_param *kp)
487ea5af
CH
917{
918 int rv;
919
920 rv = param_set_uint(val, kp);
921 if (rv)
922 return rv;
923
924 sanitize_global_limit((unsigned *)kp->arg);
925
926 return 0;
927}
928
929static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg)
930{
931 int cap_sys_admin = capable(CAP_SYS_ADMIN);
932
933 if (arg->minor < 13)
934 return;
935
936 sanitize_global_limit(&max_user_bgreq);
937 sanitize_global_limit(&max_user_congthresh);
938
ae2dffa3 939 spin_lock(&fc->bg_lock);
487ea5af
CH
940 if (arg->max_background) {
941 fc->max_background = arg->max_background;
942
943 if (!cap_sys_admin && fc->max_background > max_user_bgreq)
944 fc->max_background = max_user_bgreq;
945 }
946 if (arg->congestion_threshold) {
947 fc->congestion_threshold = arg->congestion_threshold;
948
949 if (!cap_sys_admin &&
950 fc->congestion_threshold > max_user_congthresh)
951 fc->congestion_threshold = max_user_congthresh;
952 }
ae2dffa3 953 spin_unlock(&fc->bg_lock);
487ea5af
CH
954}
955
615047ef
MS
956struct fuse_init_args {
957 struct fuse_args args;
958 struct fuse_init_in in;
959 struct fuse_init_out out;
960};
961
fcee216b 962static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
615047ef 963 int error)
9b9a0469 964{
fcee216b 965 struct fuse_conn *fc = fm->fc;
615047ef
MS
966 struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
967 struct fuse_init_out *arg = &ia->out;
fd1a1dc6 968 bool ok = true;
9b9a0469 969
615047ef 970 if (error || arg->major != FUSE_KERNEL_VERSION)
fd1a1dc6 971 ok = false;
9b9a0469 972 else {
9cd68455
MS
973 unsigned long ra_pages;
974
487ea5af
CH
975 process_init_limits(fc, arg);
976
9cd68455 977 if (arg->minor >= 6) {
09cbfeaf 978 ra_pages = arg->max_readahead / PAGE_SIZE;
9cd68455
MS
979 if (arg->flags & FUSE_ASYNC_READ)
980 fc->async_read = 1;
71421259
MS
981 if (!(arg->flags & FUSE_POSIX_LOCKS))
982 fc->no_lock = 1;
37fb3a30
MS
983 if (arg->minor >= 17) {
984 if (!(arg->flags & FUSE_FLOCK_LOCKS))
985 fc->no_flock = 1;
24114504
MS
986 } else {
987 if (!(arg->flags & FUSE_POSIX_LOCKS))
988 fc->no_flock = 1;
37fb3a30 989 }
6ff958ed
MS
990 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
991 fc->atomic_o_trunc = 1;
33670fa2
MS
992 if (arg->minor >= 9) {
993 /* LOOKUP has dependency on proto version */
994 if (arg->flags & FUSE_EXPORT_SUPPORT)
995 fc->export_support = 1;
996 }
78bb6cb9
MS
997 if (arg->flags & FUSE_BIG_WRITES)
998 fc->big_writes = 1;
e0a43ddc
MS
999 if (arg->flags & FUSE_DONT_MASK)
1000 fc->dont_mask = 1;
72d0d248
BF
1001 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1002 fc->auto_inval_data = 1;
ad2ba64d
KS
1003 else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
1004 fc->explicit_inval_data = 1;
28420dad 1005 if (arg->flags & FUSE_DO_READDIRPLUS) {
0b05b183 1006 fc->do_readdirplus = 1;
28420dad
MS
1007 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1008 fc->readdirplus_auto = 1;
1009 }
60b9df7a
MS
1010 if (arg->flags & FUSE_ASYNC_DIO)
1011 fc->async_dio = 1;
4d99ff8f
PE
1012 if (arg->flags & FUSE_WRITEBACK_CACHE)
1013 fc->writeback_cache = 1;
5c672ab3
MS
1014 if (arg->flags & FUSE_PARALLEL_DIROPS)
1015 fc->parallel_dirops = 1;
5e940c1d
MS
1016 if (arg->flags & FUSE_HANDLE_KILLPRIV)
1017 fc->handle_killpriv = 1;
e27c9d38 1018 if (arg->time_gran && arg->time_gran <= 1000000000)
fcee216b 1019 fm->sb->s_time_gran = arg->time_gran;
60bcc88a 1020 if ((arg->flags & FUSE_POSIX_ACL)) {
29433a29 1021 fc->default_permissions = 1;
60bcc88a 1022 fc->posix_acl = 1;
fcee216b 1023 fm->sb->s_xattr = fuse_acl_xattr_handlers;
60bcc88a 1024 }
5571f1e6
DS
1025 if (arg->flags & FUSE_CACHE_SYMLINKS)
1026 fc->cache_symlinks = 1;
3b7008b2
SL
1027 if (arg->flags & FUSE_ABORT_ERROR)
1028 fc->abort_err = 1;
5da784cc
CS
1029 if (arg->flags & FUSE_MAX_PAGES) {
1030 fc->max_pages =
1031 min_t(unsigned int, FUSE_MAX_MAX_PAGES,
1032 max_t(unsigned int, arg->max_pages, 1));
1033 }
fd1a1dc6
SH
1034 if (IS_ENABLED(CONFIG_FUSE_DAX) &&
1035 arg->flags & FUSE_MAP_ALIGNMENT &&
1036 !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1037 ok = false;
1038 }
71421259 1039 } else {
09cbfeaf 1040 ra_pages = fc->max_read / PAGE_SIZE;
71421259 1041 fc->no_lock = 1;
37fb3a30 1042 fc->no_flock = 1;
71421259 1043 }
9cd68455 1044
fcee216b
MR
1045 fm->sb->s_bdi->ra_pages =
1046 min(fm->sb->s_bdi->ra_pages, ra_pages);
9b9a0469
MS
1047 fc->minor = arg->minor;
1048 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
f948d564 1049 fc->max_write = max_t(unsigned, 4096, fc->max_write);
0ec7ca41 1050 fc->conn_init = 1;
9b9a0469 1051 }
615047ef
MS
1052 kfree(ia);
1053
fd1a1dc6
SH
1054 if (!ok) {
1055 fc->conn_init = 0;
1056 fc->conn_error = 1;
1057 }
1058
9759bd51 1059 fuse_set_initialized(fc);
08a53cdc 1060 wake_up_all(&fc->blocked_waitq);
9b9a0469
MS
1061}
1062
fcee216b 1063void fuse_send_init(struct fuse_mount *fm)
9b9a0469 1064{
615047ef 1065 struct fuse_init_args *ia;
095da6cb 1066
615047ef
MS
1067 ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1068
1069 ia->in.major = FUSE_KERNEL_VERSION;
1070 ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
fcee216b 1071 ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
615047ef
MS
1072 ia->in.flags |=
1073 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
37fb3a30 1074 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
69fe05c9 1075 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
9446385f 1076 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
4d99ff8f 1077 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
5c672ab3 1078 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
3b7008b2 1079 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
d9a9ea94 1080 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
ad2ba64d 1081 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA;
fd1a1dc6 1082#ifdef CONFIG_FUSE_DAX
fcee216b 1083 if (fm->fc->dax)
fd1a1dc6
SH
1084 ia->in.flags |= FUSE_MAP_ALIGNMENT;
1085#endif
615047ef
MS
1086 ia->args.opcode = FUSE_INIT;
1087 ia->args.in_numargs = 1;
1088 ia->args.in_args[0].size = sizeof(ia->in);
1089 ia->args.in_args[0].value = &ia->in;
1090 ia->args.out_numargs = 1;
3ad2f3fb 1091 /* Variable length argument used for backward compatibility
9b9a0469
MS
1092 with interface version < 7.5. Rest of init_out is zeroed
1093 by do_get_request(), so a short reply is not a problem */
cabdb4fa 1094 ia->args.out_argvar = true;
615047ef
MS
1095 ia->args.out_args[0].size = sizeof(ia->out);
1096 ia->args.out_args[0].value = &ia->out;
1097 ia->args.force = true;
1098 ia->args.nocreds = true;
1099 ia->args.end = process_init_reply;
1100
fcee216b
MR
1101 if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1102 process_init_reply(fm, &ia->args, -ENOTCONN);
9b9a0469 1103}
95a84cdb 1104EXPORT_SYMBOL_GPL(fuse_send_init);
9b9a0469 1105
783863d6 1106void fuse_free_conn(struct fuse_conn *fc)
43901aab 1107{
cc080e9e 1108 WARN_ON(!list_empty(&fc->devices));
dd3e2c55 1109 kfree_rcu(fc, rcu);
43901aab 1110}
783863d6 1111EXPORT_SYMBOL_GPL(fuse_free_conn);
43901aab 1112
a325f9b9
TH
1113static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
1114{
1115 int err;
5f7f7543 1116 char *suffix = "";
a325f9b9 1117
69c8ebf8 1118 if (sb->s_bdev) {
5f7f7543 1119 suffix = "-fuseblk";
69c8ebf8
JK
1120 /*
1121 * sb->s_bdi points to blkdev's bdi however we want to redirect
1122 * it to our private bdi...
1123 */
1124 bdi_put(sb->s_bdi);
1125 sb->s_bdi = &noop_backing_dev_info;
1126 }
5f7f7543
JK
1127 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev),
1128 MINOR(fc->dev), suffix);
a325f9b9
TH
1129 if (err)
1130 return err;
1131
b5420237 1132 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
5f7f7543
JK
1133 /* fuse does it's own writeback accounting */
1134 sb->s_bdi->capabilities = BDI_CAP_NO_ACCT_WB | BDI_CAP_STRICTLIMIT;
a325f9b9 1135
a325f9b9
TH
1136 /*
1137 * For a single fuse filesystem use max 1% of dirty +
1138 * writeback threshold.
1139 *
1140 * This gives about 1M of write buffer for memory maps on a
1141 * machine with 1G and 10% dirty_ratio, which should be more
1142 * than enough.
1143 *
1144 * Privileged users can raise it by writing to
1145 *
1146 * /sys/class/bdi/<bdi>/max_ratio
1147 */
5f7f7543 1148 bdi_set_max_ratio(sb->s_bdi, 1);
a325f9b9
TH
1149
1150 return 0;
1151}
1152
0cd1eb9a 1153struct fuse_dev *fuse_dev_alloc(void)
cc080e9e
MS
1154{
1155 struct fuse_dev *fud;
be2ff42c 1156 struct list_head *pq;
cc080e9e
MS
1157
1158 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
be2ff42c
KT
1159 if (!fud)
1160 return NULL;
cc080e9e 1161
be2ff42c
KT
1162 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1163 if (!pq) {
1164 kfree(fud);
1165 return NULL;
cc080e9e
MS
1166 }
1167
be2ff42c 1168 fud->pq.processing = pq;
be2ff42c
KT
1169 fuse_pqueue_init(&fud->pq);
1170
0cd1eb9a
VG
1171 return fud;
1172}
1173EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1174
1175void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1176{
1177 fud->fc = fuse_conn_get(fc);
be2ff42c
KT
1178 spin_lock(&fc->lock);
1179 list_add_tail(&fud->entry, &fc->devices);
1180 spin_unlock(&fc->lock);
0cd1eb9a
VG
1181}
1182EXPORT_SYMBOL_GPL(fuse_dev_install);
be2ff42c 1183
0cd1eb9a
VG
1184struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1185{
1186 struct fuse_dev *fud;
1187
1188 fud = fuse_dev_alloc();
1189 if (!fud)
1190 return NULL;
1191
1192 fuse_dev_install(fud, fc);
cc080e9e
MS
1193 return fud;
1194}
0cd1eb9a 1195EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
cc080e9e
MS
1196
1197void fuse_dev_free(struct fuse_dev *fud)
1198{
1199 struct fuse_conn *fc = fud->fc;
1200
1201 if (fc) {
1202 spin_lock(&fc->lock);
1203 list_del(&fud->entry);
1204 spin_unlock(&fc->lock);
1205
1206 fuse_conn_put(fc);
1207 }
d72f70da 1208 kfree(fud->pq.processing);
cc080e9e
MS
1209 kfree(fud);
1210}
1211EXPORT_SYMBOL_GPL(fuse_dev_free);
1212
1866d779
MR
1213static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
1214 const struct fuse_inode *fi)
1215{
1216 *attr = (struct fuse_attr){
1217 .ino = fi->inode.i_ino,
1218 .size = fi->inode.i_size,
1219 .blocks = fi->inode.i_blocks,
1220 .atime = fi->inode.i_atime.tv_sec,
1221 .mtime = fi->inode.i_mtime.tv_sec,
1222 .ctime = fi->inode.i_ctime.tv_sec,
1223 .atimensec = fi->inode.i_atime.tv_nsec,
1224 .mtimensec = fi->inode.i_mtime.tv_nsec,
1225 .ctimensec = fi->inode.i_ctime.tv_nsec,
1226 .mode = fi->inode.i_mode,
1227 .nlink = fi->inode.i_nlink,
1228 .uid = fi->inode.i_uid.val,
1229 .gid = fi->inode.i_gid.val,
1230 .rdev = fi->inode.i_rdev,
1231 .blksize = 1u << fi->inode.i_blkbits,
1232 };
1233}
1234
1235static void fuse_sb_defaults(struct super_block *sb)
1236{
1237 sb->s_magic = FUSE_SUPER_MAGIC;
1238 sb->s_op = &fuse_super_operations;
1239 sb->s_xattr = fuse_xattr_handlers;
1240 sb->s_maxbytes = MAX_LFS_FILESIZE;
1241 sb->s_time_gran = 1;
1242 sb->s_export_op = &fuse_export_operations;
1243 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1244 if (sb->s_user_ns != &init_user_ns)
1245 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1246 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1247
1248 /*
1249 * If we are not in the initial user namespace posix
1250 * acls must be translated.
1251 */
1252 if (sb->s_user_ns != &init_user_ns)
1253 sb->s_xattr = fuse_no_acl_xattr_handlers;
1254}
1255
1256int fuse_fill_super_submount(struct super_block *sb,
1257 struct fuse_inode *parent_fi)
1258{
1259 struct fuse_mount *fm = get_fuse_mount_super(sb);
1260 struct super_block *parent_sb = parent_fi->inode.i_sb;
1261 struct fuse_attr root_attr;
1262 struct inode *root;
1263
1264 fuse_sb_defaults(sb);
1265 fm->sb = sb;
1266
1267 WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1268 sb->s_bdi = bdi_get(parent_sb->s_bdi);
1269
1270 sb->s_xattr = parent_sb->s_xattr;
1271 sb->s_time_gran = parent_sb->s_time_gran;
1272 sb->s_blocksize = parent_sb->s_blocksize;
1273 sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1274 sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1275 if (parent_sb->s_subtype && !sb->s_subtype)
1276 return -ENOMEM;
1277
1278 fuse_fill_attr_from_inode(&root_attr, parent_fi);
1279 root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
1280 /*
1281 * This inode is just a duplicate, so it is not looked up and
1282 * its nlookup should not be incremented. fuse_iget() does
1283 * that, though, so undo it here.
1284 */
1285 get_fuse_inode(root)->nlookup--;
1286 sb->s_d_op = &fuse_dentry_operations;
1287 sb->s_root = d_make_root(root);
1288 if (!sb->s_root)
1289 return -ENOMEM;
1290
1291 return 0;
1292}
1293
0cc2656c 1294int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
d8a5ba45 1295{
7fd3abfa 1296 struct fuse_dev *fud = NULL;
fcee216b
MR
1297 struct fuse_mount *fm = get_fuse_mount_super(sb);
1298 struct fuse_conn *fc = fm->fc;
d8a5ba45 1299 struct inode *root;
f543f253 1300 struct dentry *root_dentry;
d8a5ba45
MS
1301 int err;
1302
c2b8f006 1303 err = -EINVAL;
1751e8a6 1304 if (sb->s_flags & SB_MANDLOCK)
c2b8f006 1305 goto err;
71421259 1306
1866d779 1307 fuse_sb_defaults(sb);
9e1f1de0 1308
0cc2656c 1309 if (ctx->is_bdev) {
875d95ec 1310#ifdef CONFIG_BLOCK
c2b8f006 1311 err = -EINVAL;
c30da2e9 1312 if (!sb_set_blocksize(sb, ctx->blksize))
c2b8f006 1313 goto err;
875d95ec 1314#endif
d8091614 1315 } else {
09cbfeaf
KS
1316 sb->s_blocksize = PAGE_SIZE;
1317 sb->s_blocksize_bits = PAGE_SHIFT;
d8091614 1318 }
c30da2e9
DH
1319
1320 sb->s_subtype = ctx->subtype;
1321 ctx->subtype = NULL;
1dd53957
VG
1322 if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1323 err = fuse_dax_conn_alloc(fc, ctx->dax_dev);
1324 if (err)
1325 goto err;
1326 }
1327
7fd3abfa
VG
1328 if (ctx->fudptr) {
1329 err = -ENOMEM;
1330 fud = fuse_dev_alloc_install(fc);
1331 if (!fud)
1dd53957 1332 goto err_free_dax;
7fd3abfa 1333 }
cc080e9e 1334
a325f9b9 1335 fc->dev = sb->s_dev;
fcee216b 1336 fm->sb = sb;
a325f9b9
TH
1337 err = fuse_bdi_init(fc, sb);
1338 if (err)
cc080e9e 1339 goto err_dev_free;
0d179aa5 1340
e0a43ddc 1341 /* Handle umasking inside the fuse code */
1751e8a6 1342 if (sb->s_flags & SB_POSIXACL)
e0a43ddc 1343 fc->dont_mask = 1;
1751e8a6 1344 sb->s_flags |= SB_POSIXACL;
e0a43ddc 1345
c30da2e9
DH
1346 fc->default_permissions = ctx->default_permissions;
1347 fc->allow_other = ctx->allow_other;
1348 fc->user_id = ctx->user_id;
1349 fc->group_id = ctx->group_id;
f4fd4ae3 1350 fc->legacy_opts_show = ctx->legacy_opts_show;
1866d779 1351 fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
783863d6 1352 fc->destroy = ctx->destroy;
15c8e72e
VG
1353 fc->no_control = ctx->no_control;
1354 fc->no_force_umount = ctx->no_force_umount;
f543f253 1355
d8a5ba45 1356 err = -ENOMEM;
c30da2e9 1357 root = fuse_get_root_inode(sb, ctx->rootmode);
0ce267ff 1358 sb->s_d_op = &fuse_root_dentry_operations;
48fde701
AV
1359 root_dentry = d_make_root(root);
1360 if (!root_dentry)
cc080e9e 1361 goto err_dev_free;
0ce267ff 1362 /* Root dentry doesn't have .d_revalidate */
c35eebe9 1363 sb->s_d_op = &fuse_dentry_operations;
f543f253 1364
bafa9654 1365 mutex_lock(&fuse_mutex);
8aa09a50 1366 err = -EINVAL;
7fd3abfa 1367 if (ctx->fudptr && *ctx->fudptr)
bafa9654 1368 goto err_unlock;
8aa09a50 1369
bafa9654
MS
1370 err = fuse_ctl_add_conn(fc);
1371 if (err)
1372 goto err_unlock;
1373
1374 list_add_tail(&fc->entry, &fuse_conn_list);
f543f253 1375 sb->s_root = root_dentry;
7fd3abfa
VG
1376 if (ctx->fudptr)
1377 *ctx->fudptr = fud;
bafa9654 1378 mutex_unlock(&fuse_mutex);
0cc2656c
SH
1379 return 0;
1380
1381 err_unlock:
1382 mutex_unlock(&fuse_mutex);
1383 dput(root_dentry);
1384 err_dev_free:
7fd3abfa
VG
1385 if (fud)
1386 fuse_dev_free(fud);
1dd53957
VG
1387 err_free_dax:
1388 if (IS_ENABLED(CONFIG_FUSE_DAX))
1389 fuse_dax_conn_free(fc);
0cc2656c
SH
1390 err:
1391 return err;
1392}
1393EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1394
1395static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1396{
1397 struct fuse_fs_context *ctx = fsc->fs_private;
1398 struct file *file;
1399 int err;
1400 struct fuse_conn *fc;
fcee216b 1401 struct fuse_mount *fm;
0cc2656c
SH
1402
1403 err = -EINVAL;
1404 file = fget(ctx->fd);
1405 if (!file)
1406 goto err;
1407
1408 /*
1409 * Require mount to happen from the same user namespace which
1410 * opened /dev/fuse to prevent potential attacks.
1411 */
1412 if ((file->f_op != &fuse_dev_operations) ||
1413 (file->f_cred->user_ns != sb->s_user_ns))
1414 goto err_fput;
1415 ctx->fudptr = &file->private_data;
1416
1417 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1418 err = -ENOMEM;
1419 if (!fc)
1420 goto err_fput;
1421
fcee216b
MR
1422 fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1423 if (!fm) {
1424 kfree(fc);
1425 goto err_fput;
1426 }
1427
1428 fuse_conn_init(fc, fm, sb->s_user_ns, &fuse_dev_fiq_ops, NULL);
0cc2656c 1429 fc->release = fuse_free_conn;
fcee216b
MR
1430
1431 sb->s_fs_info = fm;
0cc2656c
SH
1432
1433 err = fuse_fill_super_common(sb, ctx);
1434 if (err)
1435 goto err_put_conn;
0720b315
MS
1436 /*
1437 * atomic_dec_and_test() in fput() provides the necessary
1438 * memory barrier for file->private_data to be visible on all
1439 * CPUs after this
1440 */
1441 fput(file);
fcee216b 1442 fuse_send_init(get_fuse_mount_super(sb));
d8a5ba45
MS
1443 return 0;
1444
c2b8f006 1445 err_put_conn:
fcee216b 1446 fuse_mount_put(fm);
543b8f86 1447 sb->s_fs_info = NULL;
c2b8f006
MS
1448 err_fput:
1449 fput(file);
1450 err:
d8a5ba45
MS
1451 return err;
1452}
1453
c30da2e9 1454static int fuse_get_tree(struct fs_context *fc)
d8a5ba45 1455{
c30da2e9
DH
1456 struct fuse_fs_context *ctx = fc->fs_private;
1457
1458 if (!ctx->fd_present || !ctx->rootmode_present ||
1459 !ctx->user_id_present || !ctx->group_id_present)
1460 return -EINVAL;
1461
1462#ifdef CONFIG_BLOCK
1463 if (ctx->is_bdev)
1464 return get_tree_bdev(fc, fuse_fill_super);
1465#endif
1466
1467 return get_tree_nodev(fc, fuse_fill_super);
1468}
1469
1470static const struct fs_context_operations fuse_context_ops = {
1471 .free = fuse_free_fc,
1472 .parse_param = fuse_parse_param,
0189a2d3 1473 .reconfigure = fuse_reconfigure,
c30da2e9
DH
1474 .get_tree = fuse_get_tree,
1475};
1476
1477/*
1478 * Set up the filesystem mount context.
1479 */
1480static int fuse_init_fs_context(struct fs_context *fc)
1481{
1482 struct fuse_fs_context *ctx;
1483
1484 ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1485 if (!ctx)
1486 return -ENOMEM;
1487
1488 ctx->max_read = ~0;
1489 ctx->blksize = FUSE_DEFAULT_BLKSIZE;
f4fd4ae3 1490 ctx->legacy_opts_show = true;
c30da2e9
DH
1491
1492#ifdef CONFIG_BLOCK
783863d6 1493 if (fc->fs_type == &fuseblk_fs_type) {
c30da2e9 1494 ctx->is_bdev = true;
783863d6
MS
1495 ctx->destroy = true;
1496 }
c30da2e9
DH
1497#endif
1498
1499 fc->fs_private = ctx;
1500 fc->ops = &fuse_context_ops;
1501 return 0;
d8a5ba45
MS
1502}
1503
fcee216b 1504bool fuse_mount_remove(struct fuse_mount *fm)
3b463ae0 1505{
fcee216b
MR
1506 struct fuse_conn *fc = fm->fc;
1507 bool last = false;
3b463ae0 1508
fcee216b
MR
1509 down_write(&fc->killsb);
1510 list_del_init(&fm->fc_entry);
1511 if (list_empty(&fc->mounts))
1512 last = true;
1513 up_write(&fc->killsb);
e8f3bd77 1514
fcee216b
MR
1515 return last;
1516}
1517EXPORT_SYMBOL_GPL(fuse_mount_remove);
e8f3bd77 1518
fcee216b
MR
1519void fuse_conn_destroy(struct fuse_mount *fm)
1520{
1521 struct fuse_conn *fc = fm->fc;
1522
1523 if (fc->destroy)
1524 fuse_send_destroy(fm);
1525
1526 fuse_abort_conn(fc);
1527 fuse_wait_aborted(fc);
e8f3bd77 1528}
fcee216b 1529EXPORT_SYMBOL_GPL(fuse_conn_destroy);
3b463ae0 1530
fcee216b 1531static void fuse_kill_sb_anon(struct super_block *sb)
e8f3bd77 1532{
fcee216b
MR
1533 struct fuse_mount *fm = get_fuse_mount_super(sb);
1534 bool last;
1535
1536 if (fm) {
1537 last = fuse_mount_remove(fm);
1538 if (last)
1539 fuse_conn_destroy(fm);
1540 }
3b463ae0
JM
1541 kill_anon_super(sb);
1542}
1543
875d95ec
MS
1544static struct file_system_type fuse_fs_type = {
1545 .owner = THIS_MODULE,
1546 .name = "fuse",
4ad769f3 1547 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
c30da2e9 1548 .init_fs_context = fuse_init_fs_context,
d7167b14 1549 .parameters = fuse_fs_parameters,
3b463ae0 1550 .kill_sb = fuse_kill_sb_anon,
875d95ec 1551};
7f78e035 1552MODULE_ALIAS_FS("fuse");
875d95ec
MS
1553
1554#ifdef CONFIG_BLOCK
3b463ae0
JM
1555static void fuse_kill_sb_blk(struct super_block *sb)
1556{
fcee216b
MR
1557 struct fuse_mount *fm = get_fuse_mount_super(sb);
1558 bool last;
1559
1560 if (fm) {
1561 last = fuse_mount_remove(fm);
1562 if (last)
1563 fuse_conn_destroy(fm);
1564 }
3b463ae0
JM
1565 kill_block_super(sb);
1566}
1567
d6392f87
MS
1568static struct file_system_type fuseblk_fs_type = {
1569 .owner = THIS_MODULE,
1570 .name = "fuseblk",
c30da2e9 1571 .init_fs_context = fuse_init_fs_context,
d7167b14 1572 .parameters = fuse_fs_parameters,
3b463ae0 1573 .kill_sb = fuse_kill_sb_blk,
edad01e2 1574 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
d6392f87 1575};
7f78e035 1576MODULE_ALIAS_FS("fuseblk");
d6392f87 1577
875d95ec
MS
1578static inline int register_fuseblk(void)
1579{
1580 return register_filesystem(&fuseblk_fs_type);
1581}
1582
1583static inline void unregister_fuseblk(void)
1584{
1585 unregister_filesystem(&fuseblk_fs_type);
1586}
1587#else
1588static inline int register_fuseblk(void)
1589{
1590 return 0;
1591}
1592
1593static inline void unregister_fuseblk(void)
1594{
1595}
1596#endif
1597
51cc5068 1598static void fuse_inode_init_once(void *foo)
d8a5ba45 1599{
1729a16c 1600 struct inode *inode = foo;
d8a5ba45 1601
a35afb83 1602 inode_init_once(inode);
d8a5ba45
MS
1603}
1604
1605static int __init fuse_fs_init(void)
1606{
1607 int err;
1608
d6392f87 1609 fuse_inode_cachep = kmem_cache_create("fuse_inode",
df206988
JW
1610 sizeof(struct fuse_inode), 0,
1611 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT,
1612 fuse_inode_init_once);
d6392f87
MS
1613 err = -ENOMEM;
1614 if (!fuse_inode_cachep)
988f0325
AV
1615 goto out;
1616
1617 err = register_fuseblk();
1618 if (err)
1619 goto out2;
1620
1621 err = register_filesystem(&fuse_fs_type);
1622 if (err)
1623 goto out3;
d6392f87
MS
1624
1625 return 0;
d8a5ba45 1626
988f0325 1627 out3:
875d95ec 1628 unregister_fuseblk();
988f0325
AV
1629 out2:
1630 kmem_cache_destroy(fuse_inode_cachep);
d6392f87 1631 out:
d8a5ba45
MS
1632 return err;
1633}
1634
1635static void fuse_fs_cleanup(void)
1636{
1637 unregister_filesystem(&fuse_fs_type);
875d95ec 1638 unregister_fuseblk();
8c0a8537
KS
1639
1640 /*
1641 * Make sure all delayed rcu free inodes are flushed before we
1642 * destroy cache.
1643 */
1644 rcu_barrier();
d8a5ba45
MS
1645 kmem_cache_destroy(fuse_inode_cachep);
1646}
1647
5c89e17e 1648static struct kobject *fuse_kobj;
5c89e17e 1649
f543f253
MS
1650static int fuse_sysfs_init(void)
1651{
1652 int err;
1653
00d26666 1654 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
5c89e17e
GKH
1655 if (!fuse_kobj) {
1656 err = -ENOMEM;
f543f253 1657 goto out_err;
5c89e17e 1658 }
f543f253 1659
f9bb4882
EB
1660 err = sysfs_create_mount_point(fuse_kobj, "connections");
1661 if (err)
f543f253
MS
1662 goto out_fuse_unregister;
1663
1664 return 0;
1665
1666 out_fuse_unregister:
197b12d6 1667 kobject_put(fuse_kobj);
f543f253
MS
1668 out_err:
1669 return err;
1670}
1671
1672static void fuse_sysfs_cleanup(void)
1673{
f9bb4882 1674 sysfs_remove_mount_point(fuse_kobj, "connections");
197b12d6 1675 kobject_put(fuse_kobj);
f543f253
MS
1676}
1677
d8a5ba45
MS
1678static int __init fuse_init(void)
1679{
1680 int res;
1681
f2294482
KS
1682 pr_info("init (API version %i.%i)\n",
1683 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
d8a5ba45 1684
bafa9654 1685 INIT_LIST_HEAD(&fuse_conn_list);
d8a5ba45
MS
1686 res = fuse_fs_init();
1687 if (res)
1688 goto err;
1689
334f485d
MS
1690 res = fuse_dev_init();
1691 if (res)
1692 goto err_fs_cleanup;
1693
f543f253
MS
1694 res = fuse_sysfs_init();
1695 if (res)
1696 goto err_dev_cleanup;
1697
bafa9654
MS
1698 res = fuse_ctl_init();
1699 if (res)
1700 goto err_sysfs_cleanup;
1701
487ea5af
CH
1702 sanitize_global_limit(&max_user_bgreq);
1703 sanitize_global_limit(&max_user_congthresh);
1704
d8a5ba45
MS
1705 return 0;
1706
bafa9654
MS
1707 err_sysfs_cleanup:
1708 fuse_sysfs_cleanup();
f543f253
MS
1709 err_dev_cleanup:
1710 fuse_dev_cleanup();
334f485d
MS
1711 err_fs_cleanup:
1712 fuse_fs_cleanup();
d8a5ba45
MS
1713 err:
1714 return res;
1715}
1716
1717static void __exit fuse_exit(void)
1718{
f2294482 1719 pr_debug("exit\n");
d8a5ba45 1720
bafa9654 1721 fuse_ctl_cleanup();
f543f253 1722 fuse_sysfs_cleanup();
d8a5ba45 1723 fuse_fs_cleanup();
334f485d 1724 fuse_dev_cleanup();
d8a5ba45
MS
1725}
1726
1727module_init(fuse_init);
1728module_exit(fuse_exit);