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