hc
2024-05-10 10ebd8556b7990499c896a550e3d416b444211e6
kernel/fs/fuse/inode.c
....@@ -15,7 +15,8 @@
1515 #include <linux/init.h>
1616 #include <linux/module.h>
1717 #include <linux/moduleparam.h>
18
-#include <linux/parser.h>
18
+#include <linux/fs_context.h>
19
+#include <linux/fs_parser.h>
1920 #include <linux/statfs.h>
2021 #include <linux/random.h>
2122 #include <linux/sched.h>
....@@ -26,6 +27,7 @@
2627 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2728 MODULE_DESCRIPTION("Filesystem in Userspace");
2829 MODULE_LICENSE("GPL");
30
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
2931
3032 static struct kmem_cache *fuse_inode_cachep;
3133 struct list_head fuse_conn_list;
....@@ -59,89 +61,93 @@
5961 /** Congestion starts at 75% of maximum */
6062 #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
6163
62
-struct fuse_mount_data {
63
- int fd;
64
- unsigned rootmode;
65
- kuid_t user_id;
66
- kgid_t group_id;
67
- unsigned fd_present:1;
68
- unsigned rootmode_present:1;
69
- unsigned user_id_present:1;
70
- unsigned group_id_present:1;
71
- unsigned default_permissions:1;
72
- unsigned allow_other:1;
73
- unsigned max_read;
74
- unsigned blksize;
75
-};
64
+#ifdef CONFIG_BLOCK
65
+static struct file_system_type fuseblk_fs_type;
66
+#endif
7667
7768 struct fuse_forget_link *fuse_alloc_forget(void)
7869 {
79
- return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL);
70
+ return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
8071 }
8172
8273 static struct inode *fuse_alloc_inode(struct super_block *sb)
8374 {
84
- struct inode *inode;
8575 struct fuse_inode *fi;
8676
87
- inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
88
- if (!inode)
77
+ fi = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
78
+ if (!fi)
8979 return NULL;
9080
91
- fi = get_fuse_inode(inode);
9281 fi->i_time = 0;
82
+ fi->inval_mask = 0;
9383 fi->nodeid = 0;
9484 fi->nlookup = 0;
9585 fi->attr_version = 0;
96
- fi->writectr = 0;
9786 fi->orig_ino = 0;
9887 fi->state = 0;
99
- INIT_LIST_HEAD(&fi->write_files);
100
- INIT_LIST_HEAD(&fi->queued_writes);
101
- INIT_LIST_HEAD(&fi->writepages);
102
- init_waitqueue_head(&fi->page_waitq);
10388 mutex_init(&fi->mutex);
89
+ init_rwsem(&fi->i_mmap_sem);
90
+ spin_lock_init(&fi->lock);
10491 fi->forget = fuse_alloc_forget();
105
- if (!fi->forget) {
106
- kmem_cache_free(fuse_inode_cachep, inode);
107
- return NULL;
108
- }
92
+ if (!fi->forget)
93
+ goto out_free;
10994
110
- return inode;
95
+ if (IS_ENABLED(CONFIG_FUSE_DAX) && !fuse_dax_inode_alloc(sb, fi))
96
+ goto out_free_forget;
97
+
98
+ return &fi->inode;
99
+
100
+out_free_forget:
101
+ kfree(fi->forget);
102
+out_free:
103
+ kmem_cache_free(fuse_inode_cachep, fi);
104
+ return NULL;
111105 }
112106
113
-static void fuse_i_callback(struct rcu_head *head)
114
-{
115
- struct inode *inode = container_of(head, struct inode, i_rcu);
116
- kmem_cache_free(fuse_inode_cachep, inode);
117
-}
118
-
119
-static void fuse_destroy_inode(struct inode *inode)
107
+static void fuse_free_inode(struct inode *inode)
120108 {
121109 struct fuse_inode *fi = get_fuse_inode(inode);
122
- BUG_ON(!list_empty(&fi->write_files));
123
- BUG_ON(!list_empty(&fi->queued_writes));
110
+
124111 mutex_destroy(&fi->mutex);
125112 kfree(fi->forget);
126
- call_rcu(&inode->i_rcu, fuse_i_callback);
113
+#ifdef CONFIG_FUSE_DAX
114
+ kfree(fi->dax);
115
+#endif
116
+ kmem_cache_free(fuse_inode_cachep, fi);
127117 }
128118
129119 static void fuse_evict_inode(struct inode *inode)
130120 {
121
+ struct fuse_inode *fi = get_fuse_inode(inode);
122
+
123
+ /* Will write inode on close/munmap and in all other dirtiers */
124
+ WARN_ON(inode->i_state & I_DIRTY_INODE);
125
+
131126 truncate_inode_pages_final(&inode->i_data);
132127 clear_inode(inode);
133128 if (inode->i_sb->s_flags & SB_ACTIVE) {
134129 struct fuse_conn *fc = get_fuse_conn(inode);
135
- struct fuse_inode *fi = get_fuse_inode(inode);
136
- fuse_queue_forget(fc, fi->forget, fi->nodeid, fi->nlookup);
137
- fi->forget = NULL;
130
+
131
+ if (FUSE_IS_DAX(inode))
132
+ fuse_dax_inode_cleanup(inode);
133
+ if (fi->nlookup) {
134
+ fuse_queue_forget(fc, fi->forget, fi->nodeid,
135
+ fi->nlookup);
136
+ fi->forget = NULL;
137
+ }
138
+ }
139
+ if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
140
+ WARN_ON(!list_empty(&fi->write_files));
141
+ WARN_ON(!list_empty(&fi->queued_writes));
138142 }
139143 }
140144
141
-static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
145
+static int fuse_reconfigure(struct fs_context *fc)
142146 {
147
+ struct super_block *sb = fc->root->d_sb;
148
+
143149 sync_filesystem(sb);
144
- if (*flags & SB_MANDLOCK)
150
+ if (fc->sb_flags & SB_MANDLOCK)
145151 return -EINVAL;
146152
147153 return 0;
....@@ -165,8 +171,11 @@
165171 struct fuse_conn *fc = get_fuse_conn(inode);
166172 struct fuse_inode *fi = get_fuse_inode(inode);
167173
168
- fi->attr_version = ++fc->attr_version;
174
+ lockdep_assert_held(&fi->lock);
175
+
176
+ fi->attr_version = atomic64_inc_return(&fc->attr_version);
169177 fi->i_time = attr_valid;
178
+ WRITE_ONCE(fi->inval_mask, 0);
170179
171180 inode->i_ino = fuse_squash_ino(attr->ino);
172181 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
....@@ -174,6 +183,12 @@
174183 inode->i_uid = make_kuid(fc->user_ns, attr->uid);
175184 inode->i_gid = make_kgid(fc->user_ns, attr->gid);
176185 inode->i_blocks = attr->blocks;
186
+
187
+ /* Sanitize nsecs */
188
+ attr->atimensec = min_t(u32, attr->atimensec, NSEC_PER_SEC - 1);
189
+ attr->mtimensec = min_t(u32, attr->mtimensec, NSEC_PER_SEC - 1);
190
+ attr->ctimensec = min_t(u32, attr->ctimensec, NSEC_PER_SEC - 1);
191
+
177192 inode->i_atime.tv_sec = attr->atime;
178193 inode->i_atime.tv_nsec = attr->atimensec;
179194 /* mtime from server may be stale due to local buffered write */
....@@ -210,10 +225,10 @@
210225 loff_t oldsize;
211226 struct timespec64 old_mtime;
212227
213
- spin_lock(&fc->lock);
228
+ spin_lock(&fi->lock);
214229 if ((attr_version != 0 && fi->attr_version > attr_version) ||
215230 test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
216
- spin_unlock(&fc->lock);
231
+ spin_unlock(&fi->lock);
217232 return;
218233 }
219234
....@@ -228,14 +243,15 @@
228243 */
229244 if (!is_wb || !S_ISREG(inode->i_mode))
230245 i_size_write(inode, attr->size);
231
- spin_unlock(&fc->lock);
246
+ spin_unlock(&fi->lock);
232247
233248 if (!is_wb && S_ISREG(inode->i_mode)) {
234249 bool inval = false;
235250
236251 if (oldsize != attr->size) {
237252 truncate_pagecache(inode, attr->size);
238
- inval = true;
253
+ if (!fc->explicit_inval_data)
254
+ inval = true;
239255 } else if (fc->auto_inval_data) {
240256 struct timespec64 new_mtime = {
241257 .tv_sec = attr->mtime,
....@@ -279,7 +295,7 @@
279295 BUG();
280296 }
281297
282
-int fuse_inode_eq(struct inode *inode, void *_nodeidp)
298
+static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
283299 {
284300 u64 nodeid = *(u64 *) _nodeidp;
285301 if (get_node_id(inode) == nodeid)
....@@ -303,7 +319,26 @@
303319 struct fuse_inode *fi;
304320 struct fuse_conn *fc = get_fuse_conn_super(sb);
305321
306
- retry:
322
+ /*
323
+ * Auto mount points get their node id from the submount root, which is
324
+ * not a unique identifier within this filesystem.
325
+ *
326
+ * To avoid conflicts, do not place submount points into the inode hash
327
+ * table.
328
+ */
329
+ if (fc->auto_submounts && (attr->flags & FUSE_ATTR_SUBMOUNT) &&
330
+ S_ISDIR(attr->mode)) {
331
+ inode = new_inode(sb);
332
+ if (!inode)
333
+ return NULL;
334
+
335
+ fuse_init_inode(inode, attr);
336
+ get_fuse_inode(inode)->nodeid = nodeid;
337
+ inode->i_flags |= S_AUTOMOUNT;
338
+ goto done;
339
+ }
340
+
341
+retry:
307342 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
308343 if (!inode)
309344 return NULL;
....@@ -315,32 +350,60 @@
315350 inode->i_generation = generation;
316351 fuse_init_inode(inode, attr);
317352 unlock_new_inode(inode);
318
- } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
319
- /* Inode has changed type, any I/O on the old should fail */
353
+ } else if (fuse_stale_inode(inode, generation, attr)) {
354
+ /* nodeid was reused, any I/O on the old inode should fail */
320355 fuse_make_bad(inode);
321356 iput(inode);
322357 goto retry;
323358 }
324
-
359
+done:
325360 fi = get_fuse_inode(inode);
326
- spin_lock(&fc->lock);
361
+ spin_lock(&fi->lock);
327362 fi->nlookup++;
328
- spin_unlock(&fc->lock);
363
+ spin_unlock(&fi->lock);
329364 fuse_change_attributes(inode, attr, attr_valid, attr_version);
330365
331366 return inode;
332367 }
333368
334
-int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
369
+struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
370
+ struct fuse_mount **fm)
371
+{
372
+ struct fuse_mount *fm_iter;
373
+ struct inode *inode;
374
+
375
+ WARN_ON(!rwsem_is_locked(&fc->killsb));
376
+ list_for_each_entry(fm_iter, &fc->mounts, fc_entry) {
377
+ if (!fm_iter->sb)
378
+ continue;
379
+
380
+ inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &nodeid);
381
+ if (inode) {
382
+ if (fm)
383
+ *fm = fm_iter;
384
+ return inode;
385
+ }
386
+ }
387
+
388
+ return NULL;
389
+}
390
+
391
+int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
335392 loff_t offset, loff_t len)
336393 {
394
+ struct fuse_inode *fi;
337395 struct inode *inode;
338396 pgoff_t pg_start;
339397 pgoff_t pg_end;
340398
341
- inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
399
+ inode = fuse_ilookup(fc, nodeid, NULL);
342400 if (!inode)
343401 return -ENOENT;
402
+
403
+ fi = get_fuse_inode(inode);
404
+ spin_lock(&fi->lock);
405
+ fi->attr_version = atomic64_inc_return(&fc->attr_version);
406
+ spin_unlock(&fi->lock);
344407
345408 fuse_invalidate_attr(inode);
346409 forget_all_cached_acls(inode);
....@@ -377,32 +440,29 @@
377440
378441 static void fuse_umount_begin(struct super_block *sb)
379442 {
380
- fuse_abort_conn(get_fuse_conn_super(sb), false);
443
+ struct fuse_conn *fc = get_fuse_conn_super(sb);
444
+
445
+ if (!fc->no_force_umount)
446
+ fuse_abort_conn(fc);
381447 }
382448
383
-static void fuse_send_destroy(struct fuse_conn *fc)
449
+static void fuse_send_destroy(struct fuse_mount *fm)
384450 {
385
- struct fuse_req *req = fc->destroy_req;
386
- if (req && fc->conn_init) {
387
- fc->destroy_req = NULL;
388
- req->in.h.opcode = FUSE_DESTROY;
389
- __set_bit(FR_FORCE, &req->flags);
390
- __clear_bit(FR_BACKGROUND, &req->flags);
391
- fuse_request_send(fc, req);
392
- fuse_put_request(fc, req);
451
+ if (fm->fc->conn_init) {
452
+ FUSE_ARGS(args);
453
+
454
+ args.opcode = FUSE_DESTROY;
455
+ args.force = true;
456
+ args.nocreds = true;
457
+ fuse_simple_request(fm, &args);
393458 }
394459 }
395460
396461 static void fuse_put_super(struct super_block *sb)
397462 {
398
- struct fuse_conn *fc = get_fuse_conn_super(sb);
463
+ struct fuse_mount *fm = get_fuse_mount_super(sb);
399464
400
- mutex_lock(&fuse_mutex);
401
- list_del(&fc->entry);
402
- fuse_ctl_remove_conn(fc);
403
- mutex_unlock(&fuse_mutex);
404
-
405
- fuse_conn_put(fc);
465
+ fuse_mount_put(fm);
406466 }
407467
408468 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
....@@ -422,30 +482,32 @@
422482 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
423483 {
424484 struct super_block *sb = dentry->d_sb;
425
- struct fuse_conn *fc = get_fuse_conn_super(sb);
485
+ struct fuse_mount *fm = get_fuse_mount_super(sb);
426486 FUSE_ARGS(args);
427487 struct fuse_statfs_out outarg;
428488 int err;
429489
430
- if (!fuse_allow_current_process(fc)) {
490
+ if (!fuse_allow_current_process(fm->fc)) {
431491 buf->f_type = FUSE_SUPER_MAGIC;
432492 return 0;
433493 }
434494
435495 memset(&outarg, 0, sizeof(outarg));
436
- args.in.numargs = 0;
437
- args.in.h.opcode = FUSE_STATFS;
438
- args.in.h.nodeid = get_node_id(d_inode(dentry));
439
- args.out.numargs = 1;
440
- args.out.args[0].size = sizeof(outarg);
441
- args.out.args[0].value = &outarg;
442
- err = fuse_simple_request(fc, &args);
496
+ args.in_numargs = 0;
497
+ args.opcode = FUSE_STATFS;
498
+ args.nodeid = get_node_id(d_inode(dentry));
499
+ args.out_numargs = 1;
500
+ args.out_args[0].size = sizeof(outarg);
501
+ args.out_args[0].value = &outarg;
502
+ err = fuse_simple_request(fm, &args);
443503 if (!err)
444504 convert_fuse_statfs(buf, &outarg.st);
445505 return err;
446506 }
447507
448508 enum {
509
+ OPT_SOURCE,
510
+ OPT_SUBTYPE,
449511 OPT_FD,
450512 OPT_ROOTMODE,
451513 OPT_USER_ID,
....@@ -457,111 +519,115 @@
457519 OPT_ERR
458520 };
459521
460
-static const match_table_t tokens = {
461
- {OPT_FD, "fd=%u"},
462
- {OPT_ROOTMODE, "rootmode=%o"},
463
- {OPT_USER_ID, "user_id=%u"},
464
- {OPT_GROUP_ID, "group_id=%u"},
465
- {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
466
- {OPT_ALLOW_OTHER, "allow_other"},
467
- {OPT_MAX_READ, "max_read=%u"},
468
- {OPT_BLKSIZE, "blksize=%u"},
469
- {OPT_ERR, NULL}
522
+static const struct fs_parameter_spec fuse_fs_parameters[] = {
523
+ fsparam_string ("source", OPT_SOURCE),
524
+ fsparam_u32 ("fd", OPT_FD),
525
+ fsparam_u32oct ("rootmode", OPT_ROOTMODE),
526
+ fsparam_u32 ("user_id", OPT_USER_ID),
527
+ fsparam_u32 ("group_id", OPT_GROUP_ID),
528
+ fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS),
529
+ fsparam_flag ("allow_other", OPT_ALLOW_OTHER),
530
+ fsparam_u32 ("max_read", OPT_MAX_READ),
531
+ fsparam_u32 ("blksize", OPT_BLKSIZE),
532
+ fsparam_string ("subtype", OPT_SUBTYPE),
533
+ {}
470534 };
471535
472
-static int fuse_match_uint(substring_t *s, unsigned int *res)
536
+static int fuse_parse_param(struct fs_context *fc, struct fs_parameter *param)
473537 {
474
- int err = -ENOMEM;
475
- char *buf = match_strdup(s);
476
- if (buf) {
477
- err = kstrtouint(buf, 10, res);
478
- kfree(buf);
479
- }
480
- return err;
481
-}
538
+ struct fs_parse_result result;
539
+ struct fuse_fs_context *ctx = fc->fs_private;
540
+ int opt;
482541
483
-static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev,
484
- struct user_namespace *user_ns)
485
-{
486
- char *p;
487
- memset(d, 0, sizeof(struct fuse_mount_data));
488
- d->max_read = ~0;
489
- d->blksize = FUSE_DEFAULT_BLKSIZE;
490
-
491
- while ((p = strsep(&opt, ",")) != NULL) {
492
- int token;
493
- int value;
494
- unsigned uv;
495
- substring_t args[MAX_OPT_ARGS];
496
- if (!*p)
497
- continue;
498
-
499
- token = match_token(p, tokens, args);
500
- switch (token) {
501
- case OPT_FD:
502
- if (match_int(&args[0], &value))
503
- return 0;
504
- d->fd = value;
505
- d->fd_present = 1;
506
- break;
507
-
508
- case OPT_ROOTMODE:
509
- if (match_octal(&args[0], &value))
510
- return 0;
511
- if (!fuse_valid_type(value))
512
- return 0;
513
- d->rootmode = value;
514
- d->rootmode_present = 1;
515
- break;
516
-
517
- case OPT_USER_ID:
518
- if (fuse_match_uint(&args[0], &uv))
519
- return 0;
520
- d->user_id = make_kuid(user_ns, uv);
521
- if (!uid_valid(d->user_id))
522
- return 0;
523
- d->user_id_present = 1;
524
- break;
525
-
526
- case OPT_GROUP_ID:
527
- if (fuse_match_uint(&args[0], &uv))
528
- return 0;
529
- d->group_id = make_kgid(user_ns, uv);
530
- if (!gid_valid(d->group_id))
531
- return 0;
532
- d->group_id_present = 1;
533
- break;
534
-
535
- case OPT_DEFAULT_PERMISSIONS:
536
- d->default_permissions = 1;
537
- break;
538
-
539
- case OPT_ALLOW_OTHER:
540
- d->allow_other = 1;
541
- break;
542
-
543
- case OPT_MAX_READ:
544
- if (match_int(&args[0], &value))
545
- return 0;
546
- d->max_read = value;
547
- break;
548
-
549
- case OPT_BLKSIZE:
550
- if (!is_bdev || match_int(&args[0], &value))
551
- return 0;
552
- d->blksize = value;
553
- break;
554
-
555
- default:
542
+ if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
543
+ /*
544
+ * Ignore options coming from mount(MS_REMOUNT) for backward
545
+ * compatibility.
546
+ */
547
+ if (fc->oldapi)
556548 return 0;
557
- }
549
+
550
+ return invalfc(fc, "No changes allowed in reconfigure");
558551 }
559552
560
- if (!d->fd_present || !d->rootmode_present ||
561
- !d->user_id_present || !d->group_id_present)
553
+ opt = fs_parse(fc, fuse_fs_parameters, param, &result);
554
+ if (opt < 0)
555
+ return opt;
556
+
557
+ switch (opt) {
558
+ case OPT_SOURCE:
559
+ if (fc->source)
560
+ return invalfc(fc, "Multiple sources specified");
561
+ fc->source = param->string;
562
+ param->string = NULL;
563
+ break;
564
+
565
+ case OPT_SUBTYPE:
566
+ if (ctx->subtype)
567
+ return invalfc(fc, "Multiple subtypes specified");
568
+ ctx->subtype = param->string;
569
+ param->string = NULL;
562570 return 0;
563571
564
- return 1;
572
+ case OPT_FD:
573
+ ctx->fd = result.uint_32;
574
+ ctx->fd_present = true;
575
+ break;
576
+
577
+ case OPT_ROOTMODE:
578
+ if (!fuse_valid_type(result.uint_32))
579
+ return invalfc(fc, "Invalid rootmode");
580
+ ctx->rootmode = result.uint_32;
581
+ ctx->rootmode_present = true;
582
+ break;
583
+
584
+ case OPT_USER_ID:
585
+ ctx->user_id = make_kuid(fc->user_ns, result.uint_32);
586
+ if (!uid_valid(ctx->user_id))
587
+ return invalfc(fc, "Invalid user_id");
588
+ ctx->user_id_present = true;
589
+ break;
590
+
591
+ case OPT_GROUP_ID:
592
+ ctx->group_id = make_kgid(fc->user_ns, result.uint_32);
593
+ if (!gid_valid(ctx->group_id))
594
+ return invalfc(fc, "Invalid group_id");
595
+ ctx->group_id_present = true;
596
+ break;
597
+
598
+ case OPT_DEFAULT_PERMISSIONS:
599
+ ctx->default_permissions = true;
600
+ break;
601
+
602
+ case OPT_ALLOW_OTHER:
603
+ ctx->allow_other = true;
604
+ break;
605
+
606
+ case OPT_MAX_READ:
607
+ ctx->max_read = result.uint_32;
608
+ break;
609
+
610
+ case OPT_BLKSIZE:
611
+ if (!ctx->is_bdev)
612
+ return invalfc(fc, "blksize only supported for fuseblk");
613
+ ctx->blksize = result.uint_32;
614
+ break;
615
+
616
+ default:
617
+ return -EINVAL;
618
+ }
619
+
620
+ return 0;
621
+}
622
+
623
+static void fuse_free_fc(struct fs_context *fc)
624
+{
625
+ struct fuse_fs_context *ctx = fc->fs_private;
626
+
627
+ if (ctx) {
628
+ kfree(ctx->subtype);
629
+ kfree(ctx);
630
+ }
565631 }
566632
567633 static int fuse_show_options(struct seq_file *m, struct dentry *root)
....@@ -569,20 +635,31 @@
569635 struct super_block *sb = root->d_sb;
570636 struct fuse_conn *fc = get_fuse_conn_super(sb);
571637
572
- seq_printf(m, ",user_id=%u", from_kuid_munged(fc->user_ns, fc->user_id));
573
- seq_printf(m, ",group_id=%u", from_kgid_munged(fc->user_ns, fc->group_id));
574
- if (fc->default_permissions)
575
- seq_puts(m, ",default_permissions");
576
- if (fc->allow_other)
577
- seq_puts(m, ",allow_other");
578
- if (fc->max_read != ~0)
579
- seq_printf(m, ",max_read=%u", fc->max_read);
580
- if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
581
- seq_printf(m, ",blksize=%lu", sb->s_blocksize);
638
+ if (fc->legacy_opts_show) {
639
+ seq_printf(m, ",user_id=%u",
640
+ from_kuid_munged(fc->user_ns, fc->user_id));
641
+ seq_printf(m, ",group_id=%u",
642
+ from_kgid_munged(fc->user_ns, fc->group_id));
643
+ if (fc->default_permissions)
644
+ seq_puts(m, ",default_permissions");
645
+ if (fc->allow_other)
646
+ seq_puts(m, ",allow_other");
647
+ if (fc->max_read != ~0)
648
+ seq_printf(m, ",max_read=%u", fc->max_read);
649
+ if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
650
+ seq_printf(m, ",blksize=%lu", sb->s_blocksize);
651
+ }
652
+#ifdef CONFIG_FUSE_DAX
653
+ if (fc->dax)
654
+ seq_puts(m, ",dax");
655
+#endif
656
+
582657 return 0;
583658 }
584659
585
-static void fuse_iqueue_init(struct fuse_iqueue *fiq)
660
+static void fuse_iqueue_init(struct fuse_iqueue *fiq,
661
+ const struct fuse_iqueue_ops *ops,
662
+ void *priv)
586663 {
587664 memset(fiq, 0, sizeof(struct fuse_iqueue));
588665 spin_lock_init(&fiq->lock);
....@@ -591,50 +668,69 @@
591668 INIT_LIST_HEAD(&fiq->interrupts);
592669 fiq->forget_list_tail = &fiq->forget_list_head;
593670 fiq->connected = 1;
671
+ fiq->ops = ops;
672
+ fiq->priv = priv;
594673 }
595674
596675 static void fuse_pqueue_init(struct fuse_pqueue *fpq)
597676 {
598
- memset(fpq, 0, sizeof(struct fuse_pqueue));
677
+ unsigned int i;
678
+
599679 spin_lock_init(&fpq->lock);
600
- INIT_LIST_HEAD(&fpq->processing);
680
+ for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
681
+ INIT_LIST_HEAD(&fpq->processing[i]);
601682 INIT_LIST_HEAD(&fpq->io);
602683 fpq->connected = 1;
603684 }
604685
605
-void fuse_conn_init(struct fuse_conn *fc, struct user_namespace *user_ns)
686
+void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
687
+ struct user_namespace *user_ns,
688
+ const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv)
606689 {
607690 memset(fc, 0, sizeof(*fc));
608691 spin_lock_init(&fc->lock);
692
+ spin_lock_init(&fc->bg_lock);
693
+ spin_lock_init(&fc->passthrough_req_lock);
609694 init_rwsem(&fc->killsb);
610695 refcount_set(&fc->count, 1);
611696 atomic_set(&fc->dev_count, 1);
612697 init_waitqueue_head(&fc->blocked_waitq);
613
- init_waitqueue_head(&fc->reserved_req_waitq);
614
- fuse_iqueue_init(&fc->iq);
698
+ fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv);
615699 INIT_LIST_HEAD(&fc->bg_queue);
616700 INIT_LIST_HEAD(&fc->entry);
617701 INIT_LIST_HEAD(&fc->devices);
702
+ idr_init(&fc->passthrough_req);
618703 atomic_set(&fc->num_waiting, 0);
619704 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
620705 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
621
- fc->khctr = 0;
706
+ atomic64_set(&fc->khctr, 0);
622707 fc->polled_files = RB_ROOT;
623708 fc->blocked = 0;
624709 fc->initialized = 0;
625710 fc->connected = 1;
626
- fc->attr_version = 1;
711
+ atomic64_set(&fc->attr_version, 1);
627712 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
628713 fc->pid_ns = get_pid_ns(task_active_pid_ns(current));
629714 fc->user_ns = get_user_ns(user_ns);
715
+ fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
716
+ fc->max_pages_limit = FUSE_MAX_MAX_PAGES;
717
+
718
+ INIT_LIST_HEAD(&fc->mounts);
719
+ list_add(&fm->fc_entry, &fc->mounts);
720
+ fm->fc = fc;
721
+ refcount_set(&fm->count, 1);
630722 }
631723 EXPORT_SYMBOL_GPL(fuse_conn_init);
632724
633725 void fuse_conn_put(struct fuse_conn *fc)
634726 {
635727 if (refcount_dec_and_test(&fc->count)) {
636
- if (fc->destroy_req)
637
- fuse_request_free(fc->destroy_req);
728
+ struct fuse_iqueue *fiq = &fc->iq;
729
+
730
+ if (IS_ENABLED(CONFIG_FUSE_DAX))
731
+ fuse_dax_conn_free(fc);
732
+ if (fiq->ops->release)
733
+ fiq->ops->release(fiq);
638734 put_pid_ns(fc->pid_ns);
639735 put_user_ns(fc->user_ns);
640736 fc->release(fc);
....@@ -648,6 +744,23 @@
648744 return fc;
649745 }
650746 EXPORT_SYMBOL_GPL(fuse_conn_get);
747
+
748
+void fuse_mount_put(struct fuse_mount *fm)
749
+{
750
+ if (refcount_dec_and_test(&fm->count)) {
751
+ if (fm->fc)
752
+ fuse_conn_put(fm->fc);
753
+ kfree(fm);
754
+ }
755
+}
756
+EXPORT_SYMBOL_GPL(fuse_mount_put);
757
+
758
+struct fuse_mount *fuse_mount_get(struct fuse_mount *fm)
759
+{
760
+ refcount_inc(&fm->count);
761
+ return fm;
762
+}
763
+EXPORT_SYMBOL_GPL(fuse_mount_get);
651764
652765 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
653766 {
....@@ -809,11 +922,10 @@
809922
810923 static const struct super_operations fuse_super_operations = {
811924 .alloc_inode = fuse_alloc_inode,
812
- .destroy_inode = fuse_destroy_inode,
925
+ .free_inode = fuse_free_inode,
813926 .evict_inode = fuse_evict_inode,
814927 .write_inode = fuse_write_inode,
815928 .drop_inode = generic_delete_inode,
816
- .remount_fs = fuse_remount_fs,
817929 .put_super = fuse_put_super,
818930 .umount_begin = fuse_umount_begin,
819931 .statfs = fuse_statfs,
....@@ -822,9 +934,12 @@
822934
823935 static void sanitize_global_limit(unsigned *limit)
824936 {
937
+ /*
938
+ * The default maximum number of async requests is calculated to consume
939
+ * 1/2^13 of the total memory, assuming 392 bytes per request.
940
+ */
825941 if (*limit == 0)
826
- *limit = ((totalram_pages << PAGE_SHIFT) >> 13) /
827
- sizeof(struct fuse_req);
942
+ *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392;
828943
829944 if (*limit >= 1 << 16)
830945 *limit = (1 << 16) - 1;
....@@ -853,6 +968,7 @@
853968 sanitize_global_limit(&max_user_bgreq);
854969 sanitize_global_limit(&max_user_congthresh);
855970
971
+ spin_lock(&fc->bg_lock);
856972 if (arg->max_background) {
857973 fc->max_background = arg->max_background;
858974
....@@ -866,14 +982,25 @@
866982 fc->congestion_threshold > max_user_congthresh)
867983 fc->congestion_threshold = max_user_congthresh;
868984 }
985
+ spin_unlock(&fc->bg_lock);
869986 }
870987
871
-static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
872
-{
873
- struct fuse_init_out *arg = &req->misc.init_out;
988
+struct fuse_init_args {
989
+ struct fuse_args args;
990
+ struct fuse_init_in in;
991
+ struct fuse_init_out out;
992
+};
874993
875
- if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
876
- fc->conn_error = 1;
994
+static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args,
995
+ int error)
996
+{
997
+ struct fuse_conn *fc = fm->fc;
998
+ struct fuse_init_args *ia = container_of(args, typeof(*ia), args);
999
+ struct fuse_init_out *arg = &ia->out;
1000
+ bool ok = true;
1001
+
1002
+ if (error || arg->major != FUSE_KERNEL_VERSION)
1003
+ ok = false;
8771004 else {
8781005 unsigned long ra_pages;
8791006
....@@ -905,6 +1032,8 @@
9051032 fc->dont_mask = 1;
9061033 if (arg->flags & FUSE_AUTO_INVAL_DATA)
9071034 fc->auto_inval_data = 1;
1035
+ else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
1036
+ fc->explicit_inval_data = 1;
9081037 if (arg->flags & FUSE_DO_READDIRPLUS) {
9091038 fc->do_readdirplus = 1;
9101039 if (arg->flags & FUSE_READDIRPLUS_AUTO)
....@@ -919,66 +1048,121 @@
9191048 if (arg->flags & FUSE_HANDLE_KILLPRIV)
9201049 fc->handle_killpriv = 1;
9211050 if (arg->time_gran && arg->time_gran <= 1000000000)
922
- fc->sb->s_time_gran = arg->time_gran;
1051
+ fm->sb->s_time_gran = arg->time_gran;
9231052 if ((arg->flags & FUSE_POSIX_ACL)) {
9241053 fc->default_permissions = 1;
9251054 fc->posix_acl = 1;
926
- fc->sb->s_xattr = fuse_acl_xattr_handlers;
1055
+ fm->sb->s_xattr = fuse_acl_xattr_handlers;
9271056 }
1057
+ if (arg->flags & FUSE_CACHE_SYMLINKS)
1058
+ fc->cache_symlinks = 1;
9281059 if (arg->flags & FUSE_ABORT_ERROR)
9291060 fc->abort_err = 1;
1061
+ if (arg->flags & FUSE_MAX_PAGES) {
1062
+ fc->max_pages =
1063
+ min_t(unsigned int, fc->max_pages_limit,
1064
+ max_t(unsigned int, arg->max_pages, 1));
1065
+ }
1066
+ if (IS_ENABLED(CONFIG_FUSE_DAX) &&
1067
+ arg->flags & FUSE_MAP_ALIGNMENT &&
1068
+ !fuse_dax_check_alignment(fc, arg->map_alignment)) {
1069
+ ok = false;
1070
+ }
1071
+ if (arg->flags & FUSE_PASSTHROUGH) {
1072
+ fc->passthrough = 1;
1073
+ /* Prevent further stacking */
1074
+ fm->sb->s_stack_depth =
1075
+ FILESYSTEM_MAX_STACK_DEPTH;
1076
+ }
9301077 } else {
9311078 ra_pages = fc->max_read / PAGE_SIZE;
9321079 fc->no_lock = 1;
9331080 fc->no_flock = 1;
9341081 }
9351082
936
- fc->sb->s_bdi->ra_pages =
937
- min(fc->sb->s_bdi->ra_pages, ra_pages);
1083
+ fm->sb->s_bdi->ra_pages =
1084
+ min(fm->sb->s_bdi->ra_pages, ra_pages);
9381085 fc->minor = arg->minor;
9391086 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
9401087 fc->max_write = max_t(unsigned, 4096, fc->max_write);
9411088 fc->conn_init = 1;
9421089 }
1090
+ kfree(ia);
1091
+
1092
+ if (!ok) {
1093
+ fc->conn_init = 0;
1094
+ fc->conn_error = 1;
1095
+ }
1096
+
9431097 fuse_set_initialized(fc);
9441098 wake_up_all(&fc->blocked_waitq);
9451099 }
9461100
947
-static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
1101
+void fuse_send_init(struct fuse_mount *fm)
9481102 {
949
- struct fuse_init_in *arg = &req->misc.init_in;
1103
+ struct fuse_init_args *ia;
9501104
951
- arg->major = FUSE_KERNEL_VERSION;
952
- arg->minor = FUSE_KERNEL_MINOR_VERSION;
953
- arg->max_readahead = fc->sb->s_bdi->ra_pages * PAGE_SIZE;
954
- arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
1105
+ ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL);
1106
+
1107
+ ia->in.major = FUSE_KERNEL_VERSION;
1108
+ ia->in.minor = FUSE_KERNEL_MINOR_VERSION;
1109
+ ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE;
1110
+ ia->in.flags |=
1111
+ FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
9551112 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK |
9561113 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ |
9571114 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA |
9581115 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO |
9591116 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT |
9601117 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL |
961
- FUSE_ABORT_ERROR;
962
- req->in.h.opcode = FUSE_INIT;
963
- req->in.numargs = 1;
964
- req->in.args[0].size = sizeof(*arg);
965
- req->in.args[0].value = arg;
966
- req->out.numargs = 1;
1118
+ FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS |
1119
+ FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA |
1120
+ FUSE_PASSTHROUGH;
1121
+#ifdef CONFIG_FUSE_DAX
1122
+ if (fm->fc->dax)
1123
+ ia->in.flags |= FUSE_MAP_ALIGNMENT;
1124
+#endif
1125
+ if (fm->fc->auto_submounts)
1126
+ ia->in.flags |= FUSE_SUBMOUNTS;
1127
+
1128
+ ia->args.opcode = FUSE_INIT;
1129
+ ia->args.in_numargs = 1;
1130
+ ia->args.in_args[0].size = sizeof(ia->in);
1131
+ ia->args.in_args[0].value = &ia->in;
1132
+ ia->args.out_numargs = 1;
9671133 /* Variable length argument used for backward compatibility
9681134 with interface version < 7.5. Rest of init_out is zeroed
9691135 by do_get_request(), so a short reply is not a problem */
970
- req->out.argvar = 1;
971
- req->out.args[0].size = sizeof(struct fuse_init_out);
972
- req->out.args[0].value = &req->misc.init_out;
973
- req->end = process_init_reply;
974
- fuse_request_send_background(fc, req);
1136
+ ia->args.out_argvar = true;
1137
+ ia->args.out_args[0].size = sizeof(ia->out);
1138
+ ia->args.out_args[0].value = &ia->out;
1139
+ ia->args.force = true;
1140
+ ia->args.nocreds = true;
1141
+ ia->args.end = process_init_reply;
1142
+
1143
+ if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0)
1144
+ process_init_reply(fm, &ia->args, -ENOTCONN);
1145
+}
1146
+EXPORT_SYMBOL_GPL(fuse_send_init);
1147
+
1148
+static int free_fuse_passthrough(int id, void *p, void *data)
1149
+{
1150
+ struct fuse_passthrough *passthrough = (struct fuse_passthrough *)p;
1151
+
1152
+ fuse_passthrough_release(passthrough);
1153
+ kfree(p);
1154
+
1155
+ return 0;
9751156 }
9761157
977
-static void fuse_free_conn(struct fuse_conn *fc)
1158
+void fuse_free_conn(struct fuse_conn *fc)
9781159 {
9791160 WARN_ON(!list_empty(&fc->devices));
1161
+ idr_for_each(&fc->passthrough_req, free_fuse_passthrough, NULL);
1162
+ idr_destroy(&fc->passthrough_req);
9801163 kfree_rcu(fc, rcu);
9811164 }
1165
+EXPORT_SYMBOL_GPL(fuse_free_conn);
9821166
9831167 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
9841168 {
....@@ -999,9 +1183,9 @@
9991183 if (err)
10001184 return err;
10011185
1002
- sb->s_bdi->ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
10031186 /* fuse does it's own writeback accounting */
1004
- sb->s_bdi->capabilities = BDI_CAP_NO_ACCT_WB | BDI_CAP_STRICTLIMIT;
1187
+ sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT;
1188
+ sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT;
10051189
10061190 /*
10071191 * For a single fuse filesystem use max 1% of dirty +
....@@ -1020,23 +1204,49 @@
10201204 return 0;
10211205 }
10221206
1023
-struct fuse_dev *fuse_dev_alloc(struct fuse_conn *fc)
1207
+struct fuse_dev *fuse_dev_alloc(void)
10241208 {
10251209 struct fuse_dev *fud;
1210
+ struct list_head *pq;
10261211
10271212 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL);
1028
- if (fud) {
1029
- fud->fc = fuse_conn_get(fc);
1030
- fuse_pqueue_init(&fud->pq);
1213
+ if (!fud)
1214
+ return NULL;
10311215
1032
- spin_lock(&fc->lock);
1033
- list_add_tail(&fud->entry, &fc->devices);
1034
- spin_unlock(&fc->lock);
1216
+ pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL);
1217
+ if (!pq) {
1218
+ kfree(fud);
1219
+ return NULL;
10351220 }
1221
+
1222
+ fud->pq.processing = pq;
1223
+ fuse_pqueue_init(&fud->pq);
10361224
10371225 return fud;
10381226 }
10391227 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
1228
+
1229
+void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
1230
+{
1231
+ fud->fc = fuse_conn_get(fc);
1232
+ spin_lock(&fc->lock);
1233
+ list_add_tail(&fud->entry, &fc->devices);
1234
+ spin_unlock(&fc->lock);
1235
+}
1236
+EXPORT_SYMBOL_GPL(fuse_dev_install);
1237
+
1238
+struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc)
1239
+{
1240
+ struct fuse_dev *fud;
1241
+
1242
+ fud = fuse_dev_alloc();
1243
+ if (!fud)
1244
+ return NULL;
1245
+
1246
+ fuse_dev_install(fud, fc);
1247
+ return fud;
1248
+}
1249
+EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
10401250
10411251 void fuse_dev_free(struct fuse_dev *fud)
10421252 {
....@@ -1049,41 +1259,35 @@
10491259
10501260 fuse_conn_put(fc);
10511261 }
1262
+ kfree(fud->pq.processing);
10521263 kfree(fud);
10531264 }
10541265 EXPORT_SYMBOL_GPL(fuse_dev_free);
10551266
1056
-static int fuse_fill_super(struct super_block *sb, void *data, int silent)
1267
+static void fuse_fill_attr_from_inode(struct fuse_attr *attr,
1268
+ const struct fuse_inode *fi)
10571269 {
1058
- struct fuse_dev *fud;
1059
- struct fuse_conn *fc;
1060
- struct inode *root;
1061
- struct fuse_mount_data d;
1062
- struct file *file;
1063
- struct dentry *root_dentry;
1064
- struct fuse_req *init_req;
1065
- int err;
1066
- int is_bdev = sb->s_bdev != NULL;
1270
+ *attr = (struct fuse_attr){
1271
+ .ino = fi->inode.i_ino,
1272
+ .size = fi->inode.i_size,
1273
+ .blocks = fi->inode.i_blocks,
1274
+ .atime = fi->inode.i_atime.tv_sec,
1275
+ .mtime = fi->inode.i_mtime.tv_sec,
1276
+ .ctime = fi->inode.i_ctime.tv_sec,
1277
+ .atimensec = fi->inode.i_atime.tv_nsec,
1278
+ .mtimensec = fi->inode.i_mtime.tv_nsec,
1279
+ .ctimensec = fi->inode.i_ctime.tv_nsec,
1280
+ .mode = fi->inode.i_mode,
1281
+ .nlink = fi->inode.i_nlink,
1282
+ .uid = fi->inode.i_uid.val,
1283
+ .gid = fi->inode.i_gid.val,
1284
+ .rdev = fi->inode.i_rdev,
1285
+ .blksize = 1u << fi->inode.i_blkbits,
1286
+ };
1287
+}
10671288
1068
- err = -EINVAL;
1069
- if (sb->s_flags & SB_MANDLOCK)
1070
- goto err;
1071
-
1072
- sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
1073
-
1074
- if (!parse_fuse_opt(data, &d, is_bdev, sb->s_user_ns))
1075
- goto err;
1076
-
1077
- if (is_bdev) {
1078
-#ifdef CONFIG_BLOCK
1079
- err = -EINVAL;
1080
- if (!sb_set_blocksize(sb, d.blksize))
1081
- goto err;
1082
-#endif
1083
- } else {
1084
- sb->s_blocksize = PAGE_SIZE;
1085
- sb->s_blocksize_bits = PAGE_SHIFT;
1086
- }
1289
+static void fuse_sb_defaults(struct super_block *sb)
1290
+{
10871291 sb->s_magic = FUSE_SUPER_MAGIC;
10881292 sb->s_op = &fuse_super_operations;
10891293 sb->s_xattr = fuse_xattr_handlers;
....@@ -1093,19 +1297,7 @@
10931297 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE;
10941298 if (sb->s_user_ns != &init_user_ns)
10951299 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER;
1096
-
1097
- file = fget(d.fd);
1098
- err = -EINVAL;
1099
- if (!file)
1100
- goto err;
1101
-
1102
- /*
1103
- * Require mount to happen from the same user namespace which
1104
- * opened /dev/fuse to prevent potential attacks.
1105
- */
1106
- if (file->f_op != &fuse_dev_operations ||
1107
- file->f_cred->user_ns != sb->s_user_ns)
1108
- goto err_fput;
1300
+ sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION);
11091301
11101302 /*
11111303 * If we are not in the initial user namespace posix
....@@ -1113,21 +1305,89 @@
11131305 */
11141306 if (sb->s_user_ns != &init_user_ns)
11151307 sb->s_xattr = fuse_no_acl_xattr_handlers;
1308
+}
11161309
1117
- fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1118
- err = -ENOMEM;
1119
- if (!fc)
1120
- goto err_fput;
1310
+int fuse_fill_super_submount(struct super_block *sb,
1311
+ struct fuse_inode *parent_fi)
1312
+{
1313
+ struct fuse_mount *fm = get_fuse_mount_super(sb);
1314
+ struct super_block *parent_sb = parent_fi->inode.i_sb;
1315
+ struct fuse_attr root_attr;
1316
+ struct inode *root;
11211317
1122
- fuse_conn_init(fc, sb->s_user_ns);
1123
- fc->release = fuse_free_conn;
1318
+ fuse_sb_defaults(sb);
1319
+ fm->sb = sb;
11241320
1125
- fud = fuse_dev_alloc(fc);
1126
- if (!fud)
1127
- goto err_put_conn;
1321
+ WARN_ON(sb->s_bdi != &noop_backing_dev_info);
1322
+ sb->s_bdi = bdi_get(parent_sb->s_bdi);
1323
+
1324
+ sb->s_xattr = parent_sb->s_xattr;
1325
+ sb->s_time_gran = parent_sb->s_time_gran;
1326
+ sb->s_blocksize = parent_sb->s_blocksize;
1327
+ sb->s_blocksize_bits = parent_sb->s_blocksize_bits;
1328
+ sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL);
1329
+ if (parent_sb->s_subtype && !sb->s_subtype)
1330
+ return -ENOMEM;
1331
+
1332
+ fuse_fill_attr_from_inode(&root_attr, parent_fi);
1333
+ root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0);
1334
+ /*
1335
+ * This inode is just a duplicate, so it is not looked up and
1336
+ * its nlookup should not be incremented. fuse_iget() does
1337
+ * that, though, so undo it here.
1338
+ */
1339
+ get_fuse_inode(root)->nlookup--;
1340
+ sb->s_d_op = &fuse_dentry_operations;
1341
+ sb->s_root = d_make_root(root);
1342
+ if (!sb->s_root)
1343
+ return -ENOMEM;
1344
+
1345
+ return 0;
1346
+}
1347
+
1348
+int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
1349
+{
1350
+ struct fuse_dev *fud = NULL;
1351
+ struct fuse_mount *fm = get_fuse_mount_super(sb);
1352
+ struct fuse_conn *fc = fm->fc;
1353
+ struct inode *root;
1354
+ struct dentry *root_dentry;
1355
+ int err;
1356
+
1357
+ err = -EINVAL;
1358
+ if (sb->s_flags & SB_MANDLOCK)
1359
+ goto err;
1360
+
1361
+ fuse_sb_defaults(sb);
1362
+
1363
+ if (ctx->is_bdev) {
1364
+#ifdef CONFIG_BLOCK
1365
+ err = -EINVAL;
1366
+ if (!sb_set_blocksize(sb, ctx->blksize))
1367
+ goto err;
1368
+#endif
1369
+ } else {
1370
+ sb->s_blocksize = PAGE_SIZE;
1371
+ sb->s_blocksize_bits = PAGE_SHIFT;
1372
+ }
1373
+
1374
+ sb->s_subtype = ctx->subtype;
1375
+ ctx->subtype = NULL;
1376
+ if (IS_ENABLED(CONFIG_FUSE_DAX)) {
1377
+ err = fuse_dax_conn_alloc(fc, ctx->dax_dev);
1378
+ if (err)
1379
+ goto err;
1380
+ }
1381
+
1382
+ if (ctx->fudptr) {
1383
+ err = -ENOMEM;
1384
+ fud = fuse_dev_alloc_install(fc);
1385
+ if (!fud)
1386
+ goto err_free_dax;
1387
+ }
11281388
11291389 fc->dev = sb->s_dev;
1130
- fc->sb = sb;
1390
+ fm->sb = sb;
11311391 err = fuse_bdi_init(fc, sb);
11321392 if (err)
11331393 goto err_dev_free;
....@@ -1137,17 +1397,18 @@
11371397 fc->dont_mask = 1;
11381398 sb->s_flags |= SB_POSIXACL;
11391399
1140
- fc->default_permissions = d.default_permissions;
1141
- fc->allow_other = d.allow_other;
1142
- fc->user_id = d.user_id;
1143
- fc->group_id = d.group_id;
1144
- fc->max_read = max_t(unsigned, 4096, d.max_read);
1145
-
1146
- /* Used by get_root_inode() */
1147
- sb->s_fs_info = fc;
1400
+ fc->default_permissions = ctx->default_permissions;
1401
+ fc->allow_other = ctx->allow_other;
1402
+ fc->user_id = ctx->user_id;
1403
+ fc->group_id = ctx->group_id;
1404
+ fc->legacy_opts_show = ctx->legacy_opts_show;
1405
+ fc->max_read = max_t(unsigned int, 4096, ctx->max_read);
1406
+ fc->destroy = ctx->destroy;
1407
+ fc->no_control = ctx->no_control;
1408
+ fc->no_force_umount = ctx->no_force_umount;
11481409
11491410 err = -ENOMEM;
1150
- root = fuse_get_root_inode(sb, d.rootmode);
1411
+ root = fuse_get_root_inode(sb, ctx->rootmode);
11511412 sb->s_d_op = &fuse_root_dentry_operations;
11521413 root_dentry = d_make_root(root);
11531414 if (!root_dentry)
....@@ -1155,20 +1416,9 @@
11551416 /* Root dentry doesn't have .d_revalidate */
11561417 sb->s_d_op = &fuse_dentry_operations;
11571418
1158
- init_req = fuse_request_alloc(0);
1159
- if (!init_req)
1160
- goto err_put_root;
1161
- __set_bit(FR_BACKGROUND, &init_req->flags);
1162
-
1163
- if (is_bdev) {
1164
- fc->destroy_req = fuse_request_alloc(0);
1165
- if (!fc->destroy_req)
1166
- goto err_free_init_req;
1167
- }
1168
-
11691419 mutex_lock(&fuse_mutex);
11701420 err = -EINVAL;
1171
- if (file->private_data)
1421
+ if (ctx->fudptr && *ctx->fudptr)
11721422 goto err_unlock;
11731423
11741424 err = fuse_ctl_add_conn(fc);
....@@ -1177,29 +1427,77 @@
11771427
11781428 list_add_tail(&fc->entry, &fuse_conn_list);
11791429 sb->s_root = root_dentry;
1180
- file->private_data = fud;
1430
+ if (ctx->fudptr)
1431
+ *ctx->fudptr = fud;
11811432 mutex_unlock(&fuse_mutex);
1433
+ return 0;
1434
+
1435
+ err_unlock:
1436
+ mutex_unlock(&fuse_mutex);
1437
+ dput(root_dentry);
1438
+ err_dev_free:
1439
+ if (fud)
1440
+ fuse_dev_free(fud);
1441
+ err_free_dax:
1442
+ if (IS_ENABLED(CONFIG_FUSE_DAX))
1443
+ fuse_dax_conn_free(fc);
1444
+ err:
1445
+ return err;
1446
+}
1447
+EXPORT_SYMBOL_GPL(fuse_fill_super_common);
1448
+
1449
+static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
1450
+{
1451
+ struct fuse_fs_context *ctx = fsc->fs_private;
1452
+ struct file *file;
1453
+ int err;
1454
+ struct fuse_conn *fc;
1455
+ struct fuse_mount *fm;
1456
+
1457
+ err = -EINVAL;
1458
+ file = fget(ctx->fd);
1459
+ if (!file)
1460
+ goto err;
1461
+
1462
+ /*
1463
+ * Require mount to happen from the same user namespace which
1464
+ * opened /dev/fuse to prevent potential attacks.
1465
+ */
1466
+ if ((file->f_op != &fuse_dev_operations) ||
1467
+ (file->f_cred->user_ns != sb->s_user_ns))
1468
+ goto err_fput;
1469
+ ctx->fudptr = &file->private_data;
1470
+
1471
+ fc = kmalloc(sizeof(*fc), GFP_KERNEL);
1472
+ err = -ENOMEM;
1473
+ if (!fc)
1474
+ goto err_fput;
1475
+
1476
+ fm = kzalloc(sizeof(*fm), GFP_KERNEL);
1477
+ if (!fm) {
1478
+ kfree(fc);
1479
+ goto err_fput;
1480
+ }
1481
+
1482
+ fuse_conn_init(fc, fm, sb->s_user_ns, &fuse_dev_fiq_ops, NULL);
1483
+ fc->release = fuse_free_conn;
1484
+
1485
+ sb->s_fs_info = fm;
1486
+
1487
+ err = fuse_fill_super_common(sb, ctx);
1488
+ if (err)
1489
+ goto err_put_conn;
11821490 /*
11831491 * atomic_dec_and_test() in fput() provides the necessary
11841492 * memory barrier for file->private_data to be visible on all
11851493 * CPUs after this
11861494 */
11871495 fput(file);
1188
-
1189
- fuse_send_init(fc, init_req);
1190
-
1496
+ fuse_send_init(get_fuse_mount_super(sb));
11911497 return 0;
11921498
1193
- err_unlock:
1194
- mutex_unlock(&fuse_mutex);
1195
- err_free_init_req:
1196
- fuse_request_free(init_req);
1197
- err_put_root:
1198
- dput(root_dentry);
1199
- err_dev_free:
1200
- fuse_dev_free(fud);
12011499 err_put_conn:
1202
- fuse_conn_put(fc);
1500
+ fuse_mount_put(fm);
12031501 sb->s_fs_info = NULL;
12041502 err_fput:
12051503 fput(file);
....@@ -1207,32 +1505,100 @@
12071505 return err;
12081506 }
12091507
1210
-static struct dentry *fuse_mount(struct file_system_type *fs_type,
1211
- int flags, const char *dev_name,
1212
- void *raw_data)
1508
+static int fuse_get_tree(struct fs_context *fc)
12131509 {
1214
- return mount_nodev(fs_type, flags, raw_data, fuse_fill_super);
1510
+ struct fuse_fs_context *ctx = fc->fs_private;
1511
+
1512
+ if (!ctx->fd_present || !ctx->rootmode_present ||
1513
+ !ctx->user_id_present || !ctx->group_id_present)
1514
+ return -EINVAL;
1515
+
1516
+#ifdef CONFIG_BLOCK
1517
+ if (ctx->is_bdev)
1518
+ return get_tree_bdev(fc, fuse_fill_super);
1519
+#endif
1520
+
1521
+ return get_tree_nodev(fc, fuse_fill_super);
12151522 }
12161523
1217
-static void fuse_sb_destroy(struct super_block *sb)
1524
+static const struct fs_context_operations fuse_context_ops = {
1525
+ .free = fuse_free_fc,
1526
+ .parse_param = fuse_parse_param,
1527
+ .reconfigure = fuse_reconfigure,
1528
+ .get_tree = fuse_get_tree,
1529
+};
1530
+
1531
+/*
1532
+ * Set up the filesystem mount context.
1533
+ */
1534
+static int fuse_init_fs_context(struct fs_context *fc)
12181535 {
1219
- struct fuse_conn *fc = get_fuse_conn_super(sb);
1536
+ struct fuse_fs_context *ctx;
12201537
1221
- if (fc) {
1222
- fuse_send_destroy(fc);
1538
+ ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL);
1539
+ if (!ctx)
1540
+ return -ENOMEM;
12231541
1224
- fuse_abort_conn(fc, false);
1225
- fuse_wait_aborted(fc);
1542
+ ctx->max_read = ~0;
1543
+ ctx->blksize = FUSE_DEFAULT_BLKSIZE;
1544
+ ctx->legacy_opts_show = true;
12261545
1227
- down_write(&fc->killsb);
1228
- fc->sb = NULL;
1229
- up_write(&fc->killsb);
1546
+#ifdef CONFIG_BLOCK
1547
+ if (fc->fs_type == &fuseblk_fs_type) {
1548
+ ctx->is_bdev = true;
1549
+ ctx->destroy = true;
1550
+ }
1551
+#endif
1552
+
1553
+ fc->fs_private = ctx;
1554
+ fc->ops = &fuse_context_ops;
1555
+ return 0;
1556
+}
1557
+
1558
+bool fuse_mount_remove(struct fuse_mount *fm)
1559
+{
1560
+ struct fuse_conn *fc = fm->fc;
1561
+ bool last = false;
1562
+
1563
+ down_write(&fc->killsb);
1564
+ list_del_init(&fm->fc_entry);
1565
+ if (list_empty(&fc->mounts))
1566
+ last = true;
1567
+ up_write(&fc->killsb);
1568
+
1569
+ return last;
1570
+}
1571
+EXPORT_SYMBOL_GPL(fuse_mount_remove);
1572
+
1573
+void fuse_conn_destroy(struct fuse_mount *fm)
1574
+{
1575
+ struct fuse_conn *fc = fm->fc;
1576
+
1577
+ if (fc->destroy)
1578
+ fuse_send_destroy(fm);
1579
+
1580
+ fuse_abort_conn(fc);
1581
+ fuse_wait_aborted(fc);
1582
+
1583
+ if (!list_empty(&fc->entry)) {
1584
+ mutex_lock(&fuse_mutex);
1585
+ list_del(&fc->entry);
1586
+ fuse_ctl_remove_conn(fc);
1587
+ mutex_unlock(&fuse_mutex);
12301588 }
12311589 }
1590
+EXPORT_SYMBOL_GPL(fuse_conn_destroy);
12321591
12331592 static void fuse_kill_sb_anon(struct super_block *sb)
12341593 {
1235
- fuse_sb_destroy(sb);
1594
+ struct fuse_mount *fm = get_fuse_mount_super(sb);
1595
+ bool last;
1596
+
1597
+ if (fm) {
1598
+ last = fuse_mount_remove(fm);
1599
+ if (last)
1600
+ fuse_conn_destroy(fm);
1601
+ }
12361602 kill_anon_super(sb);
12371603 }
12381604
....@@ -1240,29 +1606,31 @@
12401606 .owner = THIS_MODULE,
12411607 .name = "fuse",
12421608 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT,
1243
- .mount = fuse_mount,
1609
+ .init_fs_context = fuse_init_fs_context,
1610
+ .parameters = fuse_fs_parameters,
12441611 .kill_sb = fuse_kill_sb_anon,
12451612 };
12461613 MODULE_ALIAS_FS("fuse");
12471614
12481615 #ifdef CONFIG_BLOCK
1249
-static struct dentry *fuse_mount_blk(struct file_system_type *fs_type,
1250
- int flags, const char *dev_name,
1251
- void *raw_data)
1252
-{
1253
- return mount_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super);
1254
-}
1255
-
12561616 static void fuse_kill_sb_blk(struct super_block *sb)
12571617 {
1258
- fuse_sb_destroy(sb);
1618
+ struct fuse_mount *fm = get_fuse_mount_super(sb);
1619
+ bool last;
1620
+
1621
+ if (sb->s_root) {
1622
+ last = fuse_mount_remove(fm);
1623
+ if (last)
1624
+ fuse_conn_destroy(fm);
1625
+ }
12591626 kill_block_super(sb);
12601627 }
12611628
12621629 static struct file_system_type fuseblk_fs_type = {
12631630 .owner = THIS_MODULE,
12641631 .name = "fuseblk",
1265
- .mount = fuse_mount_blk,
1632
+ .init_fs_context = fuse_init_fs_context,
1633
+ .parameters = fuse_fs_parameters,
12661634 .kill_sb = fuse_kill_sb_blk,
12671635 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
12681636 };
....@@ -1372,8 +1740,8 @@
13721740 {
13731741 int res;
13741742
1375
- printk(KERN_INFO "fuse init (API version %i.%i)\n",
1376
- FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1743
+ pr_info("init (API version %i.%i)\n",
1744
+ FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
13771745
13781746 INIT_LIST_HEAD(&fuse_conn_list);
13791747 res = fuse_fs_init();
....@@ -1409,7 +1777,7 @@
14091777
14101778 static void __exit fuse_exit(void)
14111779 {
1412
- printk(KERN_DEBUG "fuse exit\n");
1780
+ pr_debug("exit\n");
14131781
14141782 fuse_ctl_cleanup();
14151783 fuse_sysfs_cleanup();