2 FUSE: Filesystem in Userspace
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #define FUSE_CTL_SUPER_MAGIC 0x65735543
17 * This is non-NULL when the single instance of the control filesystem
18 * exists. Protected by fuse_mutex
20 static struct super_block *fuse_control_sb;
22 static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file)
25 mutex_lock(&fuse_mutex);
26 fc = file_inode(file)->i_private;
28 fc = fuse_conn_get(fc);
29 mutex_unlock(&fuse_mutex);
33 static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf,
34 size_t count, loff_t *ppos)
36 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
38 fuse_abort_conn(fc, true);
44 static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
45 size_t len, loff_t *ppos)
52 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
56 value = atomic_read(&fc->num_waiting);
57 file->private_data = (void *)value;
60 size = sprintf(tmp, "%ld\n", (long)file->private_data);
61 return simple_read_from_buffer(buf, len, ppos, tmp, size);
64 static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf,
65 size_t len, loff_t *ppos, unsigned val)
68 size_t size = sprintf(tmp, "%u\n", val);
70 return simple_read_from_buffer(buf, len, ppos, tmp, size);
73 static 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)
78 unsigned limit = (1 << 16) - 1;
84 err = kstrtoul_from_user(buf, count, 0, &t);
88 if (!capable(CAP_SYS_ADMIN))
89 limit = min(limit, global_limit);
99 static ssize_t fuse_conn_max_background_read(struct file *file,
100 char __user *buf, size_t len,
103 struct fuse_conn *fc;
106 fc = fuse_ctl_file_conn_get(file);
110 val = READ_ONCE(fc->max_background);
113 return fuse_conn_limit_read(file, buf, len, ppos, val);
116 static ssize_t fuse_conn_max_background_write(struct file *file,
117 const char __user *buf,
118 size_t count, loff_t *ppos)
120 unsigned uninitialized_var(val);
123 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
126 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
128 spin_lock(&fc->bg_lock);
129 fc->max_background = val;
130 fc->blocked = fc->num_background >= fc->max_background;
132 wake_up(&fc->blocked_waitq);
133 spin_unlock(&fc->bg_lock);
141 static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
142 char __user *buf, size_t len,
145 struct fuse_conn *fc;
148 fc = fuse_ctl_file_conn_get(file);
152 val = READ_ONCE(fc->congestion_threshold);
155 return fuse_conn_limit_read(file, buf, len, ppos, val);
158 static ssize_t fuse_conn_congestion_threshold_write(struct file *file,
159 const char __user *buf,
160 size_t count, loff_t *ppos)
162 unsigned uninitialized_var(val);
163 struct fuse_conn *fc;
166 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
167 max_user_congthresh);
170 fc = fuse_ctl_file_conn_get(file);
174 spin_lock(&fc->bg_lock);
175 fc->congestion_threshold = val;
177 if (fc->num_background < fc->congestion_threshold) {
178 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
179 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
181 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
182 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
185 spin_unlock(&fc->bg_lock);
191 static const struct file_operations fuse_ctl_abort_ops = {
192 .open = nonseekable_open,
193 .write = fuse_conn_abort_write,
197 static const struct file_operations fuse_ctl_waiting_ops = {
198 .open = nonseekable_open,
199 .read = fuse_conn_waiting_read,
203 static const struct file_operations fuse_conn_max_background_ops = {
204 .open = nonseekable_open,
205 .read = fuse_conn_max_background_read,
206 .write = fuse_conn_max_background_write,
210 static const struct file_operations fuse_conn_congestion_threshold_ops = {
211 .open = nonseekable_open,
212 .read = fuse_conn_congestion_threshold_read,
213 .write = fuse_conn_congestion_threshold_write,
217 static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
218 struct fuse_conn *fc,
221 const struct inode_operations *iop,
222 const struct file_operations *fop)
224 struct dentry *dentry;
227 BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
228 dentry = d_alloc_name(parent, name);
232 inode = new_inode(fuse_control_sb);
238 inode->i_ino = get_next_ino();
239 inode->i_mode = mode;
240 inode->i_uid = fc->user_id;
241 inode->i_gid = fc->group_id;
242 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
243 /* setting ->i_op to NULL is not allowed */
247 set_nlink(inode, nlink);
248 inode->i_private = fc;
249 d_add(dentry, inode);
251 fc->ctl_dentry[fc->ctl_ndents++] = dentry;
257 * Add a connection to the control filesystem (if it exists). Caller
258 * must hold fuse_mutex
260 int fuse_ctl_add_conn(struct fuse_conn *fc)
262 struct dentry *parent;
265 if (!fuse_control_sb)
268 parent = fuse_control_sb->s_root;
269 inc_nlink(d_inode(parent));
270 sprintf(name, "%u", fc->dev);
271 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
272 &simple_dir_inode_operations,
273 &simple_dir_operations);
277 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
278 NULL, &fuse_ctl_waiting_ops) ||
279 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
280 NULL, &fuse_ctl_abort_ops) ||
281 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
282 1, NULL, &fuse_conn_max_background_ops) ||
283 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
284 S_IFREG | 0600, 1, NULL,
285 &fuse_conn_congestion_threshold_ops))
291 fuse_ctl_remove_conn(fc);
296 * Remove a connection from the control filesystem (if it exists).
297 * Caller must hold fuse_mutex
299 void fuse_ctl_remove_conn(struct fuse_conn *fc)
303 if (!fuse_control_sb)
306 for (i = fc->ctl_ndents - 1; i >= 0; i--) {
307 struct dentry *dentry = fc->ctl_dentry[i];
308 d_inode(dentry)->i_private = NULL;
310 /* Get rid of submounts: */
311 d_invalidate(dentry);
315 drop_nlink(d_inode(fuse_control_sb->s_root));
318 static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
320 static const struct tree_descr empty_descr = {""};
321 struct fuse_conn *fc;
324 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
328 mutex_lock(&fuse_mutex);
329 BUG_ON(fuse_control_sb);
330 fuse_control_sb = sb;
331 list_for_each_entry(fc, &fuse_conn_list, entry) {
332 err = fuse_ctl_add_conn(fc);
334 fuse_control_sb = NULL;
335 mutex_unlock(&fuse_mutex);
339 mutex_unlock(&fuse_mutex);
344 static struct dentry *fuse_ctl_mount(struct file_system_type *fs_type,
345 int flags, const char *dev_name, void *raw_data)
347 return mount_single(fs_type, flags, raw_data, fuse_ctl_fill_super);
350 static void fuse_ctl_kill_sb(struct super_block *sb)
352 struct fuse_conn *fc;
354 mutex_lock(&fuse_mutex);
355 fuse_control_sb = NULL;
356 list_for_each_entry(fc, &fuse_conn_list, entry)
358 mutex_unlock(&fuse_mutex);
360 kill_litter_super(sb);
363 static struct file_system_type fuse_ctl_fs_type = {
364 .owner = THIS_MODULE,
366 .mount = fuse_ctl_mount,
367 .kill_sb = fuse_ctl_kill_sb,
369 MODULE_ALIAS_FS("fusectl");
371 int __init fuse_ctl_init(void)
373 return register_filesystem(&fuse_ctl_fs_type);
376 void __exit fuse_ctl_cleanup(void)
378 unregister_filesystem(&fuse_ctl_fs_type);