blob: 97ac994ff78f1daca5b8cc25ef2f5bb7210577db [file] [log] [blame]
Miklos Szeredibafa9652006-06-25 05:48:51 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredibafa9652006-06-25 05:48:51 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
David Howells29cc02d2019-03-27 23:44:19 +000013#include <linux/fs_context.h>
Miklos Szeredibafa9652006-06-25 05:48:51 -070014
15#define FUSE_CTL_SUPER_MAGIC 0x65735543
16
17/*
18 * This is non-NULL when the single instance of the control filesystem
19 * exists. Protected by fuse_mutex
20 */
21static struct super_block *fuse_control_sb;
22
23static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
24{
25 struct fuse_conn *fc;
26 mutex_lock(&fuse_mutex);
Al Viro496ad9a2013-01-23 17:07:38 -050027 fc = file_inode(file)->i_private;
Miklos Szeredibafa9652006-06-25 05:48:51 -070028 if (fc)
29 fc = fuse_conn_get(fc);
30 mutex_unlock(&fuse_mutex);
31 return fc;
32}
33
34static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
35 size_t count, loff_t *ppos)
36{
37 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
38 if (fc) {
Miklos Szeredieb98e3bd2019-01-24 10:40:16 +010039 if (fc->abort_err)
40 fc->aborted = true;
41 fuse_abort_conn(fc);
Miklos Szeredibafa9652006-06-25 05:48:51 -070042 fuse_conn_put(fc);
43 }
44 return count;
45}
46
47static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
48 size_t len, loff_t *ppos)
49{
50 char tmp[32];
51 size_t size;
52
53 if (!*ppos) {
Miklos Szeredi1729a162008-11-26 12:03:54 +010054 long value;
Miklos Szeredibafa9652006-06-25 05:48:51 -070055 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
56 if (!fc)
57 return 0;
58
Miklos Szeredi1729a162008-11-26 12:03:54 +010059 value = atomic_read(&fc->num_waiting);
60 file->private_data = (void *)value;
Miklos Szeredibafa9652006-06-25 05:48:51 -070061 fuse_conn_put(fc);
62 }
63 size = sprintf(tmp, "%ld\n", (long)file->private_data);
64 return simple_read_from_buffer(buf, len, ppos, tmp, size);
65}
66
Csaba Henk79a9d992009-08-26 19:18:24 +020067static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
68 size_t len, loff_t *ppos, unsigned val)
69{
70 char tmp[32];
71 size_t size = sprintf(tmp, "%u\n", val);
72
73 return simple_read_from_buffer(buf, len, ppos, tmp, size);
74}
75
76static ssize_t fuse_conn_limit_write(struct file *file, const char __user *buf,
77 size_t count, loff_t *ppos, unsigned *val,
78 unsigned global_limit)
79{
80 unsigned long t;
Csaba Henk79a9d992009-08-26 19:18:24 +020081 unsigned limit = (1 << 16) - 1;
82 int err;
83
Peter Huewee2690692012-04-15 02:20:27 +020084 if (*ppos)
Csaba Henk79a9d992009-08-26 19:18:24 +020085 return -EINVAL;
86
Peter Huewee2690692012-04-15 02:20:27 +020087 err = kstrtoul_from_user(buf, count, 0, &t);
Csaba Henk79a9d992009-08-26 19:18:24 +020088 if (err)
89 return err;
90
91 if (!capable(CAP_SYS_ADMIN))
92 limit = min(limit, global_limit);
93
94 if (t > limit)
95 return -EINVAL;
96
97 *val = t;
98
99 return count;
100}
101
102static ssize_t fuse_conn_max_background_read(struct file *file,
103 char __user *buf, size_t len,
104 loff_t *ppos)
105{
106 struct fuse_conn *fc;
107 unsigned val;
108
109 fc = fuse_ctl_file_conn_get(file);
110 if (!fc)
111 return 0;
112
Kirill Tkhai2a23f2b2018-08-27 18:29:29 +0300113 val = READ_ONCE(fc->max_background);
Csaba Henk79a9d992009-08-26 19:18:24 +0200114 fuse_conn_put(fc);
115
116 return fuse_conn_limit_read(file, buf, len, ppos, val);
117}
118
119static ssize_t fuse_conn_max_background_write(struct file *file,
120 const char __user *buf,
121 size_t count, loff_t *ppos)
122{
Kees Cook3f649ab2020-06-03 13:09:38 -0700123 unsigned val;
Csaba Henk79a9d992009-08-26 19:18:24 +0200124 ssize_t ret;
125
126 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
127 max_user_bgreq);
128 if (ret > 0) {
129 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
130 if (fc) {
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300131 spin_lock(&fc->bg_lock);
Csaba Henk79a9d992009-08-26 19:18:24 +0200132 fc->max_background = val;
Kirill Tkhai2b30a532018-08-27 18:29:37 +0300133 fc->blocked = fc->num_background >= fc->max_background;
134 if (!fc->blocked)
135 wake_up(&fc->blocked_waitq);
Kirill Tkhaiae2dffa2018-08-27 18:29:46 +0300136 spin_unlock(&fc->bg_lock);
Csaba Henk79a9d992009-08-26 19:18:24 +0200137 fuse_conn_put(fc);
138 }
139 }
140
141 return ret;
142}
143
144static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
145 char __user *buf, size_t len,
146 loff_t *ppos)
147{
148 struct fuse_conn *fc;
149 unsigned val;
150
151 fc = fuse_ctl_file_conn_get(file);
152 if (!fc)
153 return 0;
154
Kirill Tkhai2a23f2b2018-08-27 18:29:29 +0300155 val = READ_ONCE(fc->congestion_threshold);
Csaba Henk79a9d992009-08-26 19:18:24 +0200156 fuse_conn_put(fc);
157
158 return fuse_conn_limit_read(file, buf, len, ppos, val);
159}
160
161static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
162 const char __user *buf,
163 size_t count, loff_t *ppos)
164{
Kees Cook3f649ab2020-06-03 13:09:38 -0700165 unsigned val;
Kirill Tkhai2b30a532018-08-27 18:29:37 +0300166 struct fuse_conn *fc;
Csaba Henk79a9d992009-08-26 19:18:24 +0200167 ssize_t ret;
168
169 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
170 max_user_congthresh);
Kirill Tkhai2b30a532018-08-27 18:29:37 +0300171 if (ret <= 0)
172 goto out;
173 fc = fuse_ctl_file_conn_get(file);
174 if (!fc)
175 goto out;
176
Kemeng Shiefc41052023-10-07 23:39:56 +0800177 WRITE_ONCE(fc->congestion_threshold, val);
Kirill Tkhai2b30a532018-08-27 18:29:37 +0300178 fuse_conn_put(fc);
179out:
Csaba Henk79a9d992009-08-26 19:18:24 +0200180 return ret;
181}
182
Miklos Szeredibafa9652006-06-25 05:48:51 -0700183static const struct file_operations fuse_ctl_abort_ops = {
184 .open = nonseekable_open,
185 .write = fuse_conn_abort_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200186 .llseek = no_llseek,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700187};
188
189static const struct file_operations fuse_ctl_waiting_ops = {
190 .open = nonseekable_open,
191 .read = fuse_conn_waiting_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200192 .llseek = no_llseek,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700193};
194
Csaba Henk79a9d992009-08-26 19:18:24 +0200195static const struct file_operations fuse_conn_max_background_ops = {
196 .open = nonseekable_open,
197 .read = fuse_conn_max_background_read,
198 .write = fuse_conn_max_background_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200199 .llseek = no_llseek,
Csaba Henk79a9d992009-08-26 19:18:24 +0200200};
201
202static const struct file_operations fuse_conn_congestion_threshold_ops = {
203 .open = nonseekable_open,
204 .read = fuse_conn_congestion_threshold_read,
205 .write = fuse_conn_congestion_threshold_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200206 .llseek = no_llseek,
Csaba Henk79a9d992009-08-26 19:18:24 +0200207};
208
Miklos Szeredibafa9652006-06-25 05:48:51 -0700209static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
210 struct fuse_conn *fc,
211 const char *name,
212 int mode, int nlink,
Arjan van de Ven754661f2007-02-12 00:55:38 -0800213 const struct inode_operations *iop,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700214 const struct file_operations *fop)
215{
216 struct dentry *dentry;
217 struct inode *inode;
218
219 BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
220 dentry = d_alloc_name(parent, name);
221 if (!dentry)
222 return NULL;
223
Miklos Szeredibafa9652006-06-25 05:48:51 -0700224 inode = new_inode(fuse_control_sb);
Miklos Szeredi6becdb62018-05-31 12:26:10 +0200225 if (!inode) {
226 dput(dentry);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700227 return NULL;
Miklos Szeredi6becdb62018-05-31 12:26:10 +0200228 }
Miklos Szeredibafa9652006-06-25 05:48:51 -0700229
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400230 inode->i_ino = get_next_ino();
Miklos Szeredibafa9652006-06-25 05:48:51 -0700231 inode->i_mode = mode;
232 inode->i_uid = fc->user_id;
233 inode->i_gid = fc->group_id;
Jeff Layton3c0d5df2023-10-04 14:52:24 -0400234 simple_inode_init_ts(inode);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700235 /* setting ->i_op to NULL is not allowed */
236 if (iop)
237 inode->i_op = iop;
238 inode->i_fop = fop;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200239 set_nlink(inode, nlink);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700240 inode->i_private = fc;
Miklos Szeredibafa9652006-06-25 05:48:51 -0700241 d_add(dentry, inode);
Miklos Szeredi6becdb62018-05-31 12:26:10 +0200242
243 fc->ctl_dentry[fc->ctl_ndents++] = dentry;
244
Miklos Szeredibafa9652006-06-25 05:48:51 -0700245 return dentry;
246}
247
248/*
249 * Add a connection to the control filesystem (if it exists). Caller
Miklos Szeredi873302c2006-07-30 03:04:10 -0700250 * must hold fuse_mutex
Miklos Szeredibafa9652006-06-25 05:48:51 -0700251 */
252int fuse_ctl_add_conn(struct fuse_conn *fc)
253{
254 struct dentry *parent;
255 char name[32];
256
Xie Yongjic6479782022-07-18 16:50:12 +0800257 if (!fuse_control_sb || fc->no_control)
Miklos Szeredibafa9652006-06-25 05:48:51 -0700258 return 0;
259
260 parent = fuse_control_sb->s_root;
David Howells2b0143b2015-03-17 22:25:59 +0000261 inc_nlink(d_inode(parent));
Miklos Szeredib6f2fcb2008-04-30 00:54:34 -0700262 sprintf(name, "%u", fc->dev);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700263 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
264 &simple_dir_inode_operations,
265 &simple_dir_operations);
266 if (!parent)
267 goto err;
268
269 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
Csaba Henk79a9d992009-08-26 19:18:24 +0200270 NULL, &fuse_ctl_waiting_ops) ||
Miklos Szeredibafa9652006-06-25 05:48:51 -0700271 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
Csaba Henk79a9d992009-08-26 19:18:24 +0200272 NULL, &fuse_ctl_abort_ops) ||
273 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
274 1, NULL, &fuse_conn_max_background_ops) ||
275 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
276 S_IFREG | 0600, 1, NULL,
277 &fuse_conn_congestion_threshold_ops))
Miklos Szeredibafa9652006-06-25 05:48:51 -0700278 goto err;
279
280 return 0;
281
282 err:
283 fuse_ctl_remove_conn(fc);
284 return -ENOMEM;
285}
286
287/*
288 * Remove a connection from the control filesystem (if it exists).
Miklos Szeredi873302c2006-07-30 03:04:10 -0700289 * Caller must hold fuse_mutex
Miklos Szeredibafa9652006-06-25 05:48:51 -0700290 */
291void fuse_ctl_remove_conn(struct fuse_conn *fc)
292{
293 int i;
294
Xie Yongjic6479782022-07-18 16:50:12 +0800295 if (!fuse_control_sb || fc->no_control)
Miklos Szeredibafa9652006-06-25 05:48:51 -0700296 return;
297
298 for (i = fc->ctl_ndents - 1; i >= 0; i--) {
299 struct dentry *dentry = fc->ctl_dentry[i];
David Howells2b0143b2015-03-17 22:25:59 +0000300 d_inode(dentry)->i_private = NULL;
Miklos Szeredi6becdb62018-05-31 12:26:10 +0200301 if (!i) {
302 /* Get rid of submounts: */
303 d_invalidate(dentry);
304 }
Miklos Szeredibafa9652006-06-25 05:48:51 -0700305 dput(dentry);
306 }
David Howells2b0143b2015-03-17 22:25:59 +0000307 drop_nlink(d_inode(fuse_control_sb->s_root));
Miklos Szeredibafa9652006-06-25 05:48:51 -0700308}
309
Miklos Szeredi84c21502021-08-04 13:22:58 +0200310static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fsc)
Miklos Szeredibafa9652006-06-25 05:48:51 -0700311{
Eric Biggerscda37122017-03-25 21:15:37 -0700312 static const struct tree_descr empty_descr = {""};
Miklos Szeredibafa9652006-06-25 05:48:51 -0700313 struct fuse_conn *fc;
314 int err;
315
316 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
317 if (err)
318 return err;
319
320 mutex_lock(&fuse_mutex);
321 BUG_ON(fuse_control_sb);
322 fuse_control_sb = sb;
323 list_for_each_entry(fc, &fuse_conn_list, entry) {
324 err = fuse_ctl_add_conn(fc);
325 if (err) {
326 fuse_control_sb = NULL;
327 mutex_unlock(&fuse_mutex);
328 return err;
329 }
330 }
331 mutex_unlock(&fuse_mutex);
332
333 return 0;
334}
335
Miklos Szeredi84c21502021-08-04 13:22:58 +0200336static int fuse_ctl_get_tree(struct fs_context *fsc)
Miklos Szeredibafa9652006-06-25 05:48:51 -0700337{
Miklos Szeredi84c21502021-08-04 13:22:58 +0200338 return get_tree_single(fsc, fuse_ctl_fill_super);
David Howells29cc02d2019-03-27 23:44:19 +0000339}
340
341static const struct fs_context_operations fuse_ctl_context_ops = {
342 .get_tree = fuse_ctl_get_tree,
343};
344
Miklos Szeredi84c21502021-08-04 13:22:58 +0200345static int fuse_ctl_init_fs_context(struct fs_context *fsc)
David Howells29cc02d2019-03-27 23:44:19 +0000346{
Miklos Szeredi84c21502021-08-04 13:22:58 +0200347 fsc->ops = &fuse_ctl_context_ops;
David Howells29cc02d2019-03-27 23:44:19 +0000348 return 0;
Miklos Szeredibafa9652006-06-25 05:48:51 -0700349}
350
351static void fuse_ctl_kill_sb(struct super_block *sb)
352{
Miklos Szerediff795442007-01-29 13:19:54 -0800353 struct fuse_conn *fc;
354
Miklos Szeredibafa9652006-06-25 05:48:51 -0700355 mutex_lock(&fuse_mutex);
356 fuse_control_sb = NULL;
Miklos Szerediff795442007-01-29 13:19:54 -0800357 list_for_each_entry(fc, &fuse_conn_list, entry)
358 fc->ctl_ndents = 0;
Miklos Szeredibafa9652006-06-25 05:48:51 -0700359 mutex_unlock(&fuse_mutex);
360
361 kill_litter_super(sb);
362}
363
364static struct file_system_type fuse_ctl_fs_type = {
365 .owner = THIS_MODULE,
366 .name = "fusectl",
David Howells29cc02d2019-03-27 23:44:19 +0000367 .init_fs_context = fuse_ctl_init_fs_context,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700368 .kill_sb = fuse_ctl_kill_sb,
369};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800370MODULE_ALIAS_FS("fusectl");
Miklos Szeredibafa9652006-06-25 05:48:51 -0700371
372int __init fuse_ctl_init(void)
373{
374 return register_filesystem(&fuse_ctl_fs_type);
375}
376
Fabian Frederick7736e8c2014-04-23 18:14:42 +0200377void __exit fuse_ctl_cleanup(void)
Miklos Szeredibafa9652006-06-25 05:48:51 -0700378{
379 unregister_filesystem(&fuse_ctl_fs_type);
380}