btrfs: use file:line format for assertion report
[linux/fpc-iii.git] / fs / fuse / control.c
blob14ce1e47f980e905b7ad806dbdcbeb6522ad8ffe
1 /*
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.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
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)
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;
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);
38 if (fc) {
39 if (fc->abort_err)
40 fc->aborted = true;
41 fuse_abort_conn(fc);
42 fuse_conn_put(fc);
44 return count;
47 static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
48 size_t len, loff_t *ppos)
50 char tmp[32];
51 size_t size;
53 if (!*ppos) {
54 long value;
55 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
56 if (!fc)
57 return 0;
59 value = atomic_read(&fc->num_waiting);
60 file->private_data = (void *)value;
61 fuse_conn_put(fc);
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)
70 char tmp[32];
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)
80 unsigned long t;
81 unsigned limit = (1 << 16) - 1;
82 int err;
84 if (*ppos)
85 return -EINVAL;
87 err = kstrtoul_from_user(buf, count, 0, &t);
88 if (err)
89 return err;
91 if (!capable(CAP_SYS_ADMIN))
92 limit = min(limit, global_limit);
94 if (t > limit)
95 return -EINVAL;
97 *val = t;
99 return count;
102 static ssize_t fuse_conn_max_background_read(struct file *file,
103 char __user *buf, size_t len,
104 loff_t *ppos)
106 struct fuse_conn *fc;
107 unsigned val;
109 fc = fuse_ctl_file_conn_get(file);
110 if (!fc)
111 return 0;
113 val = READ_ONCE(fc->max_background);
114 fuse_conn_put(fc);
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)
123 unsigned uninitialized_var(val);
124 ssize_t ret;
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);
141 return ret;
144 static ssize_t fuse_conn_congestion_threshold_read(struct file *file,
145 char __user *buf, size_t len,
146 loff_t *ppos)
148 struct fuse_conn *fc;
149 unsigned val;
151 fc = fuse_ctl_file_conn_get(file);
152 if (!fc)
153 return 0;
155 val = READ_ONCE(fc->congestion_threshold);
156 fuse_conn_put(fc);
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)
165 unsigned uninitialized_var(val);
166 struct fuse_conn *fc;
167 ssize_t ret;
169 ret = fuse_conn_limit_write(file, buf, count, ppos, &val,
170 max_user_congthresh);
171 if (ret <= 0)
172 goto out;
173 fc = fuse_ctl_file_conn_get(file);
174 if (!fc)
175 goto out;
177 spin_lock(&fc->bg_lock);
178 fc->congestion_threshold = val;
179 if (fc->sb) {
180 if (fc->num_background < fc->congestion_threshold) {
181 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
182 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
183 } else {
184 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
185 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
188 spin_unlock(&fc->bg_lock);
189 fuse_conn_put(fc);
190 out:
191 return ret;
194 static const struct file_operations fuse_ctl_abort_ops = {
195 .open = nonseekable_open,
196 .write = fuse_conn_abort_write,
197 .llseek = no_llseek,
200 static const struct file_operations fuse_ctl_waiting_ops = {
201 .open = nonseekable_open,
202 .read = fuse_conn_waiting_read,
203 .llseek = no_llseek,
206 static const struct file_operations fuse_conn_max_background_ops = {
207 .open = nonseekable_open,
208 .read = fuse_conn_max_background_read,
209 .write = fuse_conn_max_background_write,
210 .llseek = no_llseek,
213 static const struct file_operations fuse_conn_congestion_threshold_ops = {
214 .open = nonseekable_open,
215 .read = fuse_conn_congestion_threshold_read,
216 .write = fuse_conn_congestion_threshold_write,
217 .llseek = no_llseek,
220 static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
221 struct fuse_conn *fc,
222 const char *name,
223 int mode, int nlink,
224 const struct inode_operations *iop,
225 const struct file_operations *fop)
227 struct dentry *dentry;
228 struct inode *inode;
230 BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
231 dentry = d_alloc_name(parent, name);
232 if (!dentry)
233 return NULL;
235 inode = new_inode(fuse_control_sb);
236 if (!inode) {
237 dput(dentry);
238 return NULL;
241 inode->i_ino = get_next_ino();
242 inode->i_mode = mode;
243 inode->i_uid = fc->user_id;
244 inode->i_gid = fc->group_id;
245 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
246 /* setting ->i_op to NULL is not allowed */
247 if (iop)
248 inode->i_op = iop;
249 inode->i_fop = fop;
250 set_nlink(inode, nlink);
251 inode->i_private = fc;
252 d_add(dentry, inode);
254 fc->ctl_dentry[fc->ctl_ndents++] = dentry;
256 return dentry;
260 * Add a connection to the control filesystem (if it exists). Caller
261 * must hold fuse_mutex
263 int fuse_ctl_add_conn(struct fuse_conn *fc)
265 struct dentry *parent;
266 char name[32];
268 if (!fuse_control_sb)
269 return 0;
271 parent = fuse_control_sb->s_root;
272 inc_nlink(d_inode(parent));
273 sprintf(name, "%u", fc->dev);
274 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
275 &simple_dir_inode_operations,
276 &simple_dir_operations);
277 if (!parent)
278 goto err;
280 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
281 NULL, &fuse_ctl_waiting_ops) ||
282 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
283 NULL, &fuse_ctl_abort_ops) ||
284 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600,
285 1, NULL, &fuse_conn_max_background_ops) ||
286 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold",
287 S_IFREG | 0600, 1, NULL,
288 &fuse_conn_congestion_threshold_ops))
289 goto err;
291 return 0;
293 err:
294 fuse_ctl_remove_conn(fc);
295 return -ENOMEM;
299 * Remove a connection from the control filesystem (if it exists).
300 * Caller must hold fuse_mutex
302 void fuse_ctl_remove_conn(struct fuse_conn *fc)
304 int i;
306 if (!fuse_control_sb)
307 return;
309 for (i = fc->ctl_ndents - 1; i >= 0; i--) {
310 struct dentry *dentry = fc->ctl_dentry[i];
311 d_inode(dentry)->i_private = NULL;
312 if (!i) {
313 /* Get rid of submounts: */
314 d_invalidate(dentry);
316 dput(dentry);
318 drop_nlink(d_inode(fuse_control_sb->s_root));
321 static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fctx)
323 static const struct tree_descr empty_descr = {""};
324 struct fuse_conn *fc;
325 int err;
327 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
328 if (err)
329 return err;
331 mutex_lock(&fuse_mutex);
332 BUG_ON(fuse_control_sb);
333 fuse_control_sb = sb;
334 list_for_each_entry(fc, &fuse_conn_list, entry) {
335 err = fuse_ctl_add_conn(fc);
336 if (err) {
337 fuse_control_sb = NULL;
338 mutex_unlock(&fuse_mutex);
339 return err;
342 mutex_unlock(&fuse_mutex);
344 return 0;
347 static int fuse_ctl_get_tree(struct fs_context *fc)
349 return vfs_get_super(fc, vfs_get_single_super, fuse_ctl_fill_super);
352 static const struct fs_context_operations fuse_ctl_context_ops = {
353 .get_tree = fuse_ctl_get_tree,
356 static int fuse_ctl_init_fs_context(struct fs_context *fc)
358 fc->ops = &fuse_ctl_context_ops;
359 return 0;
362 static void fuse_ctl_kill_sb(struct super_block *sb)
364 struct fuse_conn *fc;
366 mutex_lock(&fuse_mutex);
367 fuse_control_sb = NULL;
368 list_for_each_entry(fc, &fuse_conn_list, entry)
369 fc->ctl_ndents = 0;
370 mutex_unlock(&fuse_mutex);
372 kill_litter_super(sb);
375 static struct file_system_type fuse_ctl_fs_type = {
376 .owner = THIS_MODULE,
377 .name = "fusectl",
378 .init_fs_context = fuse_ctl_init_fs_context,
379 .kill_sb = fuse_ctl_kill_sb,
381 MODULE_ALIAS_FS("fusectl");
383 int __init fuse_ctl_init(void)
385 return register_filesystem(&fuse_ctl_fs_type);
388 void __exit fuse_ctl_cleanup(void)
390 unregister_filesystem(&fuse_ctl_fs_type);