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