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