2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/fs_context.h>
15 #define FUSE_CTL_SUPER_MAGIC 0x65735543
18 * This is non-NULL when the single instance of the control filesystem
19 * exists. Protected by fuse_mutex
21 static struct super_block *fuse_control_sb;
23 static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
26 mutex_lock(&fuse_mutex);
27 fc = file_inode(file)->i_private;
29 fc = fuse_conn_get(fc);
30 mutex_unlock(&fuse_mutex);
34 static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
35 size_t count, loff_t *ppos)
37 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
47 static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
48 size_t len, loff_t *ppos)
55 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
59 value = atomic_read(&fc->num_waiting);
60 file->private_data = (void *)value;
63 size = sprintf(tmp, "%ld\n", (long)file->private_data);
64 return simple_read_from_buffer(buf, len, ppos, tmp, size);
67 static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
68 size_t len, loff_t *ppos, unsigned val)
71 size_t size = sprintf(tmp, "%u\n", val);
73 return simple_read_from_buffer(buf, len, ppos, tmp, size);
76 static 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)
81 unsigned limit = (1 << 16) - 1;
87 err = kstrtoul_from_user(buf, count, 0, &t);
91 if (!capable(CAP_SYS_ADMIN))
92 limit = min(limit, global_limit);
102 static ssize_t fuse_conn_max_background_read(struct file *file,
103 char __user *buf, size_t len,
106 struct fuse_conn *fc;
109 fc = fuse_ctl_file_conn_get(file);
113 val = READ_ONCE(fc->max_background);
116 return fuse_conn_limit_read(file, buf, len, ppos, val);
119 static ssize_t fuse_conn_max_background_write(struct file *file,
120 const char __user *buf,
121 size_t count, loff_t *ppos)
126 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
129 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
131 spin_lock(&fc->bg_lock);
132 fc->max_background = val;
133 fc->blocked = fc->num_background >= fc->max_background;
135 wake_up(&fc->blocked_waitq);
136 spin_unlock(&fc->bg_lock);
144 static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
145 char __user *buf, size_t len,
148 struct fuse_conn *fc;
151 fc = fuse_ctl_file_conn_get(file);
155 val = READ_ONCE(fc->congestion_threshold);
158 return fuse_conn_limit_read(file, buf, len, ppos, val);
161 static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
162 const char __user *buf,
163 size_t count, loff_t *ppos)
166 struct fuse_conn *fc;
169 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
170 max_user_congthresh);
173 fc = fuse_ctl_file_conn_get(file);
177 WRITE_ONCE(fc->congestion_threshold, val);
183 static const struct file_operations fuse_ctl_abort_ops = {
184 .open = nonseekable_open,
185 .write = fuse_conn_abort_write,
188 static const struct file_operations fuse_ctl_waiting_ops = {
189 .open = nonseekable_open,
190 .read = fuse_conn_waiting_read,
193 static const struct file_operations fuse_conn_max_background_ops = {
194 .open = nonseekable_open,
195 .read = fuse_conn_max_background_read,
196 .write = fuse_conn_max_background_write,
199 static const struct file_operations fuse_conn_congestion_threshold_ops = {
200 .open = nonseekable_open,
201 .read = fuse_conn_congestion_threshold_read,
202 .write = fuse_conn_congestion_threshold_write,
205 static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
206 struct fuse_conn *fc,
209 const struct inode_operations *iop,
210 const struct file_operations *fop)
212 struct dentry *dentry;
215 BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
216 dentry = d_alloc_name(parent, name);
220 inode = new_inode(fuse_control_sb);
226 inode->i_ino = get_next_ino();
227 inode->i_mode = mode;
228 inode->i_uid = fc->user_id;
229 inode->i_gid = fc->group_id;
230 simple_inode_init_ts(inode);
231 /* setting ->i_op to NULL is not allowed */
235 set_nlink(inode, nlink);
236 inode->i_private = fc;
237 d_add(dentry, inode);
239 fc->ctl_dentry[fc->ctl_ndents++] = dentry;
245 * Add a connection to the control filesystem (if it exists). Caller
246 * must hold fuse_mutex
248 int fuse_ctl_add_conn(struct fuse_conn *fc)
250 struct dentry *parent;
253 if (!fuse_control_sb || fc->no_control)
256 parent = fuse_control_sb->s_root;
257 inc_nlink(d_inode(parent));
258 sprintf(name, "%u", fc->dev);
259 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
260 &simple_dir_inode_operations,
261 &simple_dir_operations);
265 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
266 NULL, &fuse_ctl_waiting_ops) ||
267 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
268 NULL, &fuse_ctl_abort_ops) ||
269 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
270 1, NULL, &fuse_conn_max_background_ops) ||
271 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
272 S_IFREG | 0600, 1, NULL,
273 &fuse_conn_congestion_threshold_ops))
279 fuse_ctl_remove_conn(fc);
284 * Remove a connection from the control filesystem (if it exists).
285 * Caller must hold fuse_mutex
287 void fuse_ctl_remove_conn(struct fuse_conn *fc)
291 if (!fuse_control_sb || fc->no_control)
294 for (i = fc->ctl_ndents - 1; i >= 0; i--) {
295 struct dentry *dentry = fc->ctl_dentry[i];
296 d_inode(dentry)->i_private = NULL;
298 /* Get rid of submounts: */
299 d_invalidate(dentry);
303 drop_nlink(d_inode(fuse_control_sb->s_root));
306 static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fsc)
308 static const struct tree_descr empty_descr = {""};
309 struct fuse_conn *fc;
312 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
316 mutex_lock(&fuse_mutex);
317 BUG_ON(fuse_control_sb);
318 fuse_control_sb = sb;
319 list_for_each_entry(fc, &fuse_conn_list, entry) {
320 err = fuse_ctl_add_conn(fc);
322 fuse_control_sb = NULL;
323 mutex_unlock(&fuse_mutex);
327 mutex_unlock(&fuse_mutex);
332 static int fuse_ctl_get_tree(struct fs_context *fsc)
334 return get_tree_single(fsc, fuse_ctl_fill_super);
337 static const struct fs_context_operations fuse_ctl_context_ops = {
338 .get_tree = fuse_ctl_get_tree,
341 static int fuse_ctl_init_fs_context(struct fs_context *fsc)
343 fsc->ops = &fuse_ctl_context_ops;
347 static void fuse_ctl_kill_sb(struct super_block *sb)
349 struct fuse_conn *fc;
351 mutex_lock(&fuse_mutex);
352 fuse_control_sb = NULL;
353 list_for_each_entry(fc, &fuse_conn_list, entry)
355 mutex_unlock(&fuse_mutex);
357 kill_litter_super(sb);
360 static struct file_system_type fuse_ctl_fs_type = {
361 .owner = THIS_MODULE,
363 .init_fs_context = fuse_ctl_init_fs_context,
364 .kill_sb = fuse_ctl_kill_sb,
366 MODULE_ALIAS_FS("fusectl");
368 int __init fuse_ctl_init(void)
370 return register_filesystem(&fuse_ctl_fs_type);
373 void __exit fuse_ctl_cleanup(void)
375 unregister_filesystem(&fuse_ctl_fs_type);