blob: 42593c587d48509604f3b9cfd8c0f9ffefe9ba0e [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>
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);
Josef Sipek7706a9d2006-12-08 02:37:02 -080026 fc = file->f_path.dentry->d_inode->i_private;
Miklos Szeredibafa9652006-06-25 05:48:51 -070027 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) {
Miklos Szeredi1729a162008-11-26 12:03:54 +010051 long value;
Miklos Szeredibafa9652006-06-25 05:48:51 -070052 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
53 if (!fc)
54 return 0;
55
Miklos Szeredi1729a162008-11-26 12:03:54 +010056 value = atomic_read(&fc->num_waiting);
57 file->private_data = (void *)value;
Miklos Szeredibafa9652006-06-25 05:48:51 -070058 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
Csaba Henk79a9d992009-08-26 19:18:24 +020064static 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 char tmp[32];
79 unsigned limit = (1 << 16) - 1;
80 int err;
81
82 if (*ppos || count >= sizeof(tmp) - 1)
83 return -EINVAL;
84
85 if (copy_from_user(tmp, buf, count))
86 return -EINVAL;
87
88 tmp[count] = '\0';
89
90 err = strict_strtoul(tmp, 0, &t);
91 if (err)
92 return err;
93
94 if (!capable(CAP_SYS_ADMIN))
95 limit = min(limit, global_limit);
96
97 if (t > limit)
98 return -EINVAL;
99
100 *val = t;
101
102 return count;
103}
104
105static ssize_t fuse_conn_max_background_read(struct file *file,
106 char __user *buf, size_t len,
107 loff_t *ppos)
108{
109 struct fuse_conn *fc;
110 unsigned val;
111
112 fc = fuse_ctl_file_conn_get(file);
113 if (!fc)
114 return 0;
115
116 val = fc->max_background;
117 fuse_conn_put(fc);
118
119 return fuse_conn_limit_read(file, buf, len, ppos, val);
120}
121
122static ssize_t fuse_conn_max_background_write(struct file *file,
123 const char __user *buf,
124 size_t count, loff_t *ppos)
125{
126 unsigned val;
127 ssize_t ret;
128
129 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
130 max_user_bgreq);
131 if (ret > 0) {
132 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
133 if (fc) {
134 fc->max_background = val;
135 fuse_conn_put(fc);
136 }
137 }
138
139 return ret;
140}
141
142static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
143 char __user *buf, size_t len,
144 loff_t *ppos)
145{
146 struct fuse_conn *fc;
147 unsigned val;
148
149 fc = fuse_ctl_file_conn_get(file);
150 if (!fc)
151 return 0;
152
153 val = fc->congestion_threshold;
154 fuse_conn_put(fc);
155
156 return fuse_conn_limit_read(file, buf, len, ppos, val);
157}
158
159static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
160 const char __user *buf,
161 size_t count, loff_t *ppos)
162{
163 unsigned val;
164 ssize_t ret;
165
166 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
167 max_user_congthresh);
168 if (ret > 0) {
169 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
170 if (fc) {
171 fc->congestion_threshold = val;
172 fuse_conn_put(fc);
173 }
174 }
175
176 return ret;
177}
178
Miklos Szeredibafa9652006-06-25 05:48:51 -0700179static const struct file_operations fuse_ctl_abort_ops = {
180 .open = nonseekable_open,
181 .write = fuse_conn_abort_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200182 .llseek = no_llseek,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700183};
184
185static const struct file_operations fuse_ctl_waiting_ops = {
186 .open = nonseekable_open,
187 .read = fuse_conn_waiting_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200188 .llseek = no_llseek,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700189};
190
Csaba Henk79a9d992009-08-26 19:18:24 +0200191static const struct file_operations fuse_conn_max_background_ops = {
192 .open = nonseekable_open,
193 .read = fuse_conn_max_background_read,
194 .write = fuse_conn_max_background_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200195 .llseek = no_llseek,
Csaba Henk79a9d992009-08-26 19:18:24 +0200196};
197
198static const struct file_operations fuse_conn_congestion_threshold_ops = {
199 .open = nonseekable_open,
200 .read = fuse_conn_congestion_threshold_read,
201 .write = fuse_conn_congestion_threshold_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200202 .llseek = no_llseek,
Csaba Henk79a9d992009-08-26 19:18:24 +0200203};
204
Miklos Szeredibafa9652006-06-25 05:48:51 -0700205static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
206 struct fuse_conn *fc,
207 const char *name,
208 int mode, int nlink,
Arjan van de Ven754661f2007-02-12 00:55:38 -0800209 const struct inode_operations *iop,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700210 const struct file_operations *fop)
211{
212 struct dentry *dentry;
213 struct inode *inode;
214
215 BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
216 dentry = d_alloc_name(parent, name);
217 if (!dentry)
218 return NULL;
219
220 fc->ctl_dentry[fc->ctl_ndents++] = dentry;
221 inode = new_inode(fuse_control_sb);
222 if (!inode)
223 return NULL;
224
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400225 inode->i_ino = get_next_ino();
Miklos Szeredibafa9652006-06-25 05:48:51 -0700226 inode->i_mode = mode;
227 inode->i_uid = fc->user_id;
228 inode->i_gid = fc->group_id;
229 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
230 /* setting ->i_op to NULL is not allowed */
231 if (iop)
232 inode->i_op = iop;
233 inode->i_fop = fop;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200234 set_nlink(inode, nlink);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700235 inode->i_private = fc;
Miklos Szeredibafa9652006-06-25 05:48:51 -0700236 d_add(dentry, inode);
237 return dentry;
238}
239
240/*
241 * Add a connection to the control filesystem (if it exists). Caller
Miklos Szeredi873302c2006-07-30 03:04:10 -0700242 * must hold fuse_mutex
Miklos Szeredibafa9652006-06-25 05:48:51 -0700243 */
244int fuse_ctl_add_conn(struct fuse_conn *fc)
245{
246 struct dentry *parent;
247 char name[32];
248
249 if (!fuse_control_sb)
250 return 0;
251
252 parent = fuse_control_sb->s_root;
Dave Hansend8c76e62006-09-30 23:29:04 -0700253 inc_nlink(parent->d_inode);
Miklos Szeredib6f2fcb2008-04-30 00:54:34 -0700254 sprintf(name, "%u", fc->dev);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700255 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
256 &simple_dir_inode_operations,
257 &simple_dir_operations);
258 if (!parent)
259 goto err;
260
261 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
Csaba Henk79a9d992009-08-26 19:18:24 +0200262 NULL, &fuse_ctl_waiting_ops) ||
Miklos Szeredibafa9652006-06-25 05:48:51 -0700263 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
Csaba Henk79a9d992009-08-26 19:18:24 +0200264 NULL, &fuse_ctl_abort_ops) ||
265 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
266 1, NULL, &fuse_conn_max_background_ops) ||
267 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
268 S_IFREG | 0600, 1, NULL,
269 &fuse_conn_congestion_threshold_ops))
Miklos Szeredibafa9652006-06-25 05:48:51 -0700270 goto err;
271
272 return 0;
273
274 err:
275 fuse_ctl_remove_conn(fc);
276 return -ENOMEM;
277}
278
279/*
280 * Remove a connection from the control filesystem (if it exists).
Miklos Szeredi873302c2006-07-30 03:04:10 -0700281 * Caller must hold fuse_mutex
Miklos Szeredibafa9652006-06-25 05:48:51 -0700282 */
283void fuse_ctl_remove_conn(struct fuse_conn *fc)
284{
285 int i;
286
287 if (!fuse_control_sb)
288 return;
289
290 for (i = fc->ctl_ndents - 1; i >= 0; i--) {
291 struct dentry *dentry = fc->ctl_dentry[i];
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700292 dentry->d_inode->i_private = NULL;
Miklos Szeredibafa9652006-06-25 05:48:51 -0700293 d_drop(dentry);
294 dput(dentry);
295 }
Csaba Henkd6db07d2009-08-24 06:14:07 +0200296 drop_nlink(fuse_control_sb->s_root->d_inode);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700297}
298
299static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
300{
301 struct tree_descr empty_descr = {""};
302 struct fuse_conn *fc;
303 int err;
304
305 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
306 if (err)
307 return err;
308
309 mutex_lock(&fuse_mutex);
310 BUG_ON(fuse_control_sb);
311 fuse_control_sb = sb;
312 list_for_each_entry(fc, &fuse_conn_list, entry) {
313 err = fuse_ctl_add_conn(fc);
314 if (err) {
315 fuse_control_sb = NULL;
316 mutex_unlock(&fuse_mutex);
317 return err;
318 }
319 }
320 mutex_unlock(&fuse_mutex);
321
322 return 0;
323}
324
Al Virofc14f2f2010-07-25 01:48:30 +0400325static struct dentry *fuse_ctl_mount(struct file_system_type *fs_type,
326 int flags, const char *dev_name, void *raw_data)
Miklos Szeredibafa9652006-06-25 05:48:51 -0700327{
Al Virofc14f2f2010-07-25 01:48:30 +0400328 return mount_single(fs_type, flags, raw_data, fuse_ctl_fill_super);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700329}
330
331static void fuse_ctl_kill_sb(struct super_block *sb)
332{
Miklos Szerediff795442007-01-29 13:19:54 -0800333 struct fuse_conn *fc;
334
Miklos Szeredibafa9652006-06-25 05:48:51 -0700335 mutex_lock(&fuse_mutex);
336 fuse_control_sb = NULL;
Miklos Szerediff795442007-01-29 13:19:54 -0800337 list_for_each_entry(fc, &fuse_conn_list, entry)
338 fc->ctl_ndents = 0;
Miklos Szeredibafa9652006-06-25 05:48:51 -0700339 mutex_unlock(&fuse_mutex);
340
341 kill_litter_super(sb);
342}
343
344static struct file_system_type fuse_ctl_fs_type = {
345 .owner = THIS_MODULE,
346 .name = "fusectl",
Al Virofc14f2f2010-07-25 01:48:30 +0400347 .mount = fuse_ctl_mount,
Miklos Szeredibafa9652006-06-25 05:48:51 -0700348 .kill_sb = fuse_ctl_kill_sb,
349};
350
351int __init fuse_ctl_init(void)
352{
353 return register_filesystem(&fuse_ctl_fs_type);
354}
355
356void fuse_ctl_cleanup(void)
357{
358 unregister_filesystem(&fuse_ctl_fs_type);
359}