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/init.h>
12 #include <linux/module.h>
13 #include <linux/fs_context.h>
14 #include <linux/namei.h>
15
16 #define FUSE_CTL_SUPER_MAGIC 0x65735543
17
18 /*
19 * This is non-NULL when the single instance of the control filesystem
20 * exists. Protected by fuse_mutex
21 */
22 static struct super_block *fuse_control_sb;
23
fuse_ctl_file_conn_get(struct file * file)24 static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
25 {
26 struct fuse_conn *fc;
27 mutex_lock(&fuse_mutex);
28 fc = file_inode(file)->i_private;
29 if (fc)
30 fc = fuse_conn_get(fc);
31 mutex_unlock(&fuse_mutex);
32 return fc;
33 }
34
fuse_conn_abort_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)35 static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
36 size_t count, loff_t *ppos)
37 {
38 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
39 if (fc) {
40 if (fc->abort_err)
41 fc->aborted = true;
42 fuse_abort_conn(fc);
43 fuse_conn_put(fc);
44 }
45 return count;
46 }
47
fuse_conn_waiting_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)48 static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
49 size_t len, loff_t *ppos)
50 {
51 char tmp[32];
52 size_t size;
53
54 if (!*ppos) {
55 long value;
56 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
57 if (!fc)
58 return 0;
59
60 value = atomic_read(&fc->num_waiting);
61 file->private_data = (void *)value;
62 fuse_conn_put(fc);
63 }
64 size = sprintf(tmp, "%ld\n", (long)file->private_data);
65 return simple_read_from_buffer(buf, len, ppos, tmp, size);
66 }
67
fuse_conn_limit_read(struct file * file,char __user * buf,size_t len,loff_t * ppos,unsigned val)68 static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
69 size_t len, loff_t *ppos, unsigned val)
70 {
71 char tmp[32];
72 size_t size = sprintf(tmp, "%u\n", val);
73
74 return simple_read_from_buffer(buf, len, ppos, tmp, size);
75 }
76
fuse_conn_limit_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos,unsigned * val,unsigned global_limit)77 static ssize_t fuse_conn_limit_write(struct file *file, const char __user *buf,
78 size_t count, loff_t *ppos, unsigned *val,
79 unsigned global_limit)
80 {
81 unsigned long t;
82 unsigned limit = (1 << 16) - 1;
83 int err;
84
85 if (*ppos)
86 return -EINVAL;
87
88 err = kstrtoul_from_user(buf, count, 0, &t);
89 if (err)
90 return err;
91
92 if (!capable(CAP_SYS_ADMIN))
93 limit = min(limit, global_limit);
94
95 if (t > limit)
96 return -EINVAL;
97
98 *val = t;
99
100 return count;
101 }
102
fuse_conn_max_background_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)103 static ssize_t fuse_conn_max_background_read(struct file *file,
104 char __user *buf, size_t len,
105 loff_t *ppos)
106 {
107 struct fuse_conn *fc;
108 unsigned val;
109
110 fc = fuse_ctl_file_conn_get(file);
111 if (!fc)
112 return 0;
113
114 val = READ_ONCE(fc->max_background);
115 fuse_conn_put(fc);
116
117 return fuse_conn_limit_read(file, buf, len, ppos, val);
118 }
119
fuse_conn_max_background_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)120 static ssize_t fuse_conn_max_background_write(struct file *file,
121 const char __user *buf,
122 size_t count, loff_t *ppos)
123 {
124 unsigned val;
125 ssize_t ret;
126
127 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
128 max_user_bgreq);
129 if (ret > 0) {
130 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
131 if (fc) {
132 spin_lock(&fc->bg_lock);
133 fc->max_background = val;
134 fc->blocked = fc->num_background >= fc->max_background;
135 if (!fc->blocked)
136 wake_up(&fc->blocked_waitq);
137 spin_unlock(&fc->bg_lock);
138 fuse_conn_put(fc);
139 }
140 }
141
142 return ret;
143 }
144
fuse_conn_congestion_threshold_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)145 static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
146 char __user *buf, size_t len,
147 loff_t *ppos)
148 {
149 struct fuse_conn *fc;
150 unsigned val;
151
152 fc = fuse_ctl_file_conn_get(file);
153 if (!fc)
154 return 0;
155
156 val = READ_ONCE(fc->congestion_threshold);
157 fuse_conn_put(fc);
158
159 return fuse_conn_limit_read(file, buf, len, ppos, val);
160 }
161
fuse_conn_congestion_threshold_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)162 static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
163 const char __user *buf,
164 size_t count, loff_t *ppos)
165 {
166 unsigned val;
167 struct fuse_conn *fc;
168 ssize_t ret;
169
170 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
171 max_user_congthresh);
172 if (ret <= 0)
173 goto out;
174 fc = fuse_ctl_file_conn_get(file);
175 if (!fc)
176 goto out;
177
178 WRITE_ONCE(fc->congestion_threshold, val);
179 fuse_conn_put(fc);
180 out:
181 return ret;
182 }
183
184 static const struct file_operations fuse_ctl_abort_ops = {
185 .open = nonseekable_open,
186 .write = fuse_conn_abort_write,
187 };
188
189 static const struct file_operations fuse_ctl_waiting_ops = {
190 .open = nonseekable_open,
191 .read = fuse_conn_waiting_read,
192 };
193
194 static const struct file_operations fuse_conn_max_background_ops = {
195 .open = nonseekable_open,
196 .read = fuse_conn_max_background_read,
197 .write = fuse_conn_max_background_write,
198 };
199
200 static const struct file_operations fuse_conn_congestion_threshold_ops = {
201 .open = nonseekable_open,
202 .read = fuse_conn_congestion_threshold_read,
203 .write = fuse_conn_congestion_threshold_write,
204 };
205
fuse_ctl_add_dentry(struct dentry * parent,struct fuse_conn * fc,const char * name,int mode,const struct inode_operations * iop,const struct file_operations * fop)206 static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
207 struct fuse_conn *fc,
208 const char *name, int mode,
209 const struct inode_operations *iop,
210 const struct file_operations *fop)
211 {
212 struct dentry *dentry;
213 struct inode *inode;
214
215 dentry = d_alloc_name(parent, name);
216 if (!dentry)
217 return NULL;
218
219 inode = new_inode(fuse_control_sb);
220 if (!inode) {
221 dput(dentry);
222 return NULL;
223 }
224
225 inode->i_ino = get_next_ino();
226 inode->i_mode = mode;
227 inode->i_uid = fc->user_id;
228 inode->i_gid = fc->group_id;
229 simple_inode_init_ts(inode);
230 /* setting ->i_op to NULL is not allowed */
231 if (iop)
232 inode->i_op = iop;
233 inode->i_fop = fop;
234 if (S_ISDIR(mode)) {
235 inc_nlink(d_inode(parent));
236 inc_nlink(inode);
237 }
238 inode->i_private = fc;
239 d_make_persistent(dentry, inode);
240 dput(dentry);
241
242 /*
243 * We are returning a borrowed reference here - it's only good while
244 * fuse_mutex is held. Actually it's d_make_persistent() return
245 * value...
246 */
247 return dentry;
248 }
249
250 /*
251 * Add a connection to the control filesystem (if it exists). Caller
252 * must hold fuse_mutex
253 */
fuse_ctl_add_conn(struct fuse_conn * fc)254 int fuse_ctl_add_conn(struct fuse_conn *fc)
255 {
256 struct dentry *parent;
257 char name[32];
258
259 if (!fuse_control_sb || fc->no_control)
260 return 0;
261
262 parent = fuse_control_sb->s_root;
263 sprintf(name, "%u", fc->dev);
264 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500,
265 &simple_dir_inode_operations,
266 &simple_dir_operations);
267 if (!parent)
268 goto err;
269
270 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400,
271 NULL, &fuse_ctl_waiting_ops) ||
272 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200,
273 NULL, &fuse_ctl_abort_ops) ||
274 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
275 NULL, &fuse_conn_max_background_ops) ||
276 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
277 S_IFREG | 0600, NULL,
278 &fuse_conn_congestion_threshold_ops))
279 goto err;
280
281 return 0;
282
283 err:
284 fuse_ctl_remove_conn(fc);
285 return -ENOMEM;
286 }
287
remove_one(struct dentry * dentry)288 static void remove_one(struct dentry *dentry)
289 {
290 d_inode(dentry)->i_private = NULL;
291 }
292
293 /*
294 * Remove a connection from the control filesystem (if it exists).
295 * Caller must hold fuse_mutex
296 */
fuse_ctl_remove_conn(struct fuse_conn * fc)297 void fuse_ctl_remove_conn(struct fuse_conn *fc)
298 {
299 char name[32];
300
301 if (!fuse_control_sb || fc->no_control)
302 return;
303
304 sprintf(name, "%u", fc->dev);
305 simple_remove_by_name(fuse_control_sb->s_root, name, remove_one);
306 }
307
fuse_ctl_fill_super(struct super_block * sb,struct fs_context * fsc)308 static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fsc)
309 {
310 static const struct tree_descr empty_descr = {""};
311 struct fuse_conn *fc;
312 int err;
313
314 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
315 if (err)
316 return err;
317
318 mutex_lock(&fuse_mutex);
319 BUG_ON(fuse_control_sb);
320 fuse_control_sb = sb;
321 list_for_each_entry(fc, &fuse_conn_list, entry) {
322 err = fuse_ctl_add_conn(fc);
323 if (err) {
324 fuse_control_sb = NULL;
325 mutex_unlock(&fuse_mutex);
326 return err;
327 }
328 }
329 mutex_unlock(&fuse_mutex);
330
331 return 0;
332 }
333
fuse_ctl_get_tree(struct fs_context * fsc)334 static int fuse_ctl_get_tree(struct fs_context *fsc)
335 {
336 return get_tree_single(fsc, fuse_ctl_fill_super);
337 }
338
339 static const struct fs_context_operations fuse_ctl_context_ops = {
340 .get_tree = fuse_ctl_get_tree,
341 };
342
fuse_ctl_init_fs_context(struct fs_context * fsc)343 static int fuse_ctl_init_fs_context(struct fs_context *fsc)
344 {
345 fsc->ops = &fuse_ctl_context_ops;
346 return 0;
347 }
348
fuse_ctl_kill_sb(struct super_block * sb)349 static void fuse_ctl_kill_sb(struct super_block *sb)
350 {
351 mutex_lock(&fuse_mutex);
352 fuse_control_sb = NULL;
353 mutex_unlock(&fuse_mutex);
354
355 kill_anon_super(sb);
356 }
357
358 static struct file_system_type fuse_ctl_fs_type = {
359 .owner = THIS_MODULE,
360 .name = "fusectl",
361 .init_fs_context = fuse_ctl_init_fs_context,
362 .kill_sb = fuse_ctl_kill_sb,
363 };
364 MODULE_ALIAS_FS("fusectl");
365
fuse_ctl_init(void)366 int __init fuse_ctl_init(void)
367 {
368 return register_filesystem(&fuse_ctl_fs_type);
369 }
370
fuse_ctl_cleanup(void)371 void __exit fuse_ctl_cleanup(void)
372 {
373 unregister_filesystem(&fuse_ctl_fs_type);
374 }
375