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>
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
);
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
= 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 fc
->max_background
= val
;
136 static ssize_t
fuse_conn_congestion_threshold_read(struct file
*file
,
137 char __user
*buf
, size_t len
,
140 struct fuse_conn
*fc
;
143 fc
= fuse_ctl_file_conn_get(file
);
147 val
= fc
->congestion_threshold
;
150 return fuse_conn_limit_read(file
, buf
, len
, ppos
, val
);
153 static ssize_t
fuse_conn_congestion_threshold_write(struct file
*file
,
154 const char __user
*buf
,
155 size_t count
, loff_t
*ppos
)
157 unsigned uninitialized_var(val
);
160 ret
= fuse_conn_limit_write(file
, buf
, count
, ppos
, &val
,
161 max_user_congthresh
);
163 struct fuse_conn
*fc
= fuse_ctl_file_conn_get(file
);
165 fc
->congestion_threshold
= val
;
173 static const struct file_operations fuse_ctl_abort_ops
= {
174 .open
= nonseekable_open
,
175 .write
= fuse_conn_abort_write
,
179 static const struct file_operations fuse_ctl_waiting_ops
= {
180 .open
= nonseekable_open
,
181 .read
= fuse_conn_waiting_read
,
185 static 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
,
192 static 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
,
199 static struct dentry
*fuse_ctl_add_dentry(struct dentry
*parent
,
200 struct fuse_conn
*fc
,
203 const struct inode_operations
*iop
,
204 const struct file_operations
*fop
)
206 struct dentry
*dentry
;
209 BUG_ON(fc
->ctl_ndents
>= FUSE_CTL_NUM_DENTRIES
);
210 dentry
= d_alloc_name(parent
, name
);
214 fc
->ctl_dentry
[fc
->ctl_ndents
++] = dentry
;
215 inode
= new_inode(fuse_control_sb
);
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 */
228 set_nlink(inode
, nlink
);
229 inode
->i_private
= fc
;
230 d_add(dentry
, inode
);
235 * Add a connection to the control filesystem (if it exists). Caller
236 * must hold fuse_mutex
238 int fuse_ctl_add_conn(struct fuse_conn
*fc
)
240 struct dentry
*parent
;
243 if (!fuse_control_sb
)
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
);
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
))
269 fuse_ctl_remove_conn(fc
);
274 * Remove a connection from the control filesystem (if it exists).
275 * Caller must hold fuse_mutex
277 void fuse_ctl_remove_conn(struct fuse_conn
*fc
)
281 if (!fuse_control_sb
)
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
;
290 drop_nlink(fuse_control_sb
->s_root
->d_inode
);
293 static int fuse_ctl_fill_super(struct super_block
*sb
, void *data
, int silent
)
295 struct tree_descr empty_descr
= {""};
296 struct fuse_conn
*fc
;
299 err
= simple_fill_super(sb
, FUSE_CTL_SUPER_MAGIC
, &empty_descr
);
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
);
309 fuse_control_sb
= NULL
;
310 mutex_unlock(&fuse_mutex
);
314 mutex_unlock(&fuse_mutex
);
319 static struct dentry
*fuse_ctl_mount(struct file_system_type
*fs_type
,
320 int flags
, const char *dev_name
, void *raw_data
)
322 return mount_single(fs_type
, flags
, raw_data
, fuse_ctl_fill_super
);
325 static void fuse_ctl_kill_sb(struct super_block
*sb
)
327 struct fuse_conn
*fc
;
329 mutex_lock(&fuse_mutex
);
330 fuse_control_sb
= NULL
;
331 list_for_each_entry(fc
, &fuse_conn_list
, entry
)
333 mutex_unlock(&fuse_mutex
);
335 kill_litter_super(sb
);
338 static struct file_system_type fuse_ctl_fs_type
= {
339 .owner
= THIS_MODULE
,
341 .mount
= fuse_ctl_mount
,
342 .kill_sb
= fuse_ctl_kill_sb
,
344 MODULE_ALIAS_FS("fusectl");
346 int __init
fuse_ctl_init(void)
348 return register_filesystem(&fuse_ctl_fs_type
);
351 void __exit
fuse_ctl_cleanup(void)
353 unregister_filesystem(&fuse_ctl_fs_type
);