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
->f_path
.dentry
->d_inode
->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
)
79 unsigned limit
= (1 << 16) - 1;
82 if (*ppos
|| count
>= sizeof(tmp
) - 1)
85 if (copy_from_user(tmp
, buf
, count
))
90 err
= strict_strtoul(tmp
, 0, &t
);
94 if (!capable(CAP_SYS_ADMIN
))
95 limit
= min(limit
, global_limit
);
105 static ssize_t
fuse_conn_max_background_read(struct file
*file
,
106 char __user
*buf
, size_t len
,
109 struct fuse_conn
*fc
;
112 fc
= fuse_ctl_file_conn_get(file
);
116 val
= fc
->max_background
;
119 return fuse_conn_limit_read(file
, buf
, len
, ppos
, val
);
122 static ssize_t
fuse_conn_max_background_write(struct file
*file
,
123 const char __user
*buf
,
124 size_t count
, loff_t
*ppos
)
129 ret
= fuse_conn_limit_write(file
, buf
, count
, ppos
, &val
,
132 struct fuse_conn
*fc
= fuse_ctl_file_conn_get(file
);
134 fc
->max_background
= val
;
142 static ssize_t
fuse_conn_congestion_threshold_read(struct file
*file
,
143 char __user
*buf
, size_t len
,
146 struct fuse_conn
*fc
;
149 fc
= fuse_ctl_file_conn_get(file
);
153 val
= fc
->congestion_threshold
;
156 return fuse_conn_limit_read(file
, buf
, len
, ppos
, val
);
159 static ssize_t
fuse_conn_congestion_threshold_write(struct file
*file
,
160 const char __user
*buf
,
161 size_t count
, loff_t
*ppos
)
166 ret
= fuse_conn_limit_write(file
, buf
, count
, ppos
, &val
,
167 max_user_congthresh
);
169 struct fuse_conn
*fc
= fuse_ctl_file_conn_get(file
);
171 fc
->congestion_threshold
= val
;
179 static const struct file_operations fuse_ctl_abort_ops
= {
180 .open
= nonseekable_open
,
181 .write
= fuse_conn_abort_write
,
184 static const struct file_operations fuse_ctl_waiting_ops
= {
185 .open
= nonseekable_open
,
186 .read
= fuse_conn_waiting_read
,
189 static const struct file_operations fuse_conn_max_background_ops
= {
190 .open
= nonseekable_open
,
191 .read
= fuse_conn_max_background_read
,
192 .write
= fuse_conn_max_background_write
,
195 static const struct file_operations fuse_conn_congestion_threshold_ops
= {
196 .open
= nonseekable_open
,
197 .read
= fuse_conn_congestion_threshold_read
,
198 .write
= fuse_conn_congestion_threshold_write
,
201 static struct dentry
*fuse_ctl_add_dentry(struct dentry
*parent
,
202 struct fuse_conn
*fc
,
205 const struct inode_operations
*iop
,
206 const struct file_operations
*fop
)
208 struct dentry
*dentry
;
211 BUG_ON(fc
->ctl_ndents
>= FUSE_CTL_NUM_DENTRIES
);
212 dentry
= d_alloc_name(parent
, name
);
216 fc
->ctl_dentry
[fc
->ctl_ndents
++] = dentry
;
217 inode
= new_inode(fuse_control_sb
);
221 inode
->i_mode
= mode
;
222 inode
->i_uid
= fc
->user_id
;
223 inode
->i_gid
= fc
->group_id
;
224 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
225 /* setting ->i_op to NULL is not allowed */
229 inode
->i_nlink
= nlink
;
230 inode
->i_private
= fc
;
231 d_add(dentry
, inode
);
236 * Add a connection to the control filesystem (if it exists). Caller
237 * must hold fuse_mutex
239 int fuse_ctl_add_conn(struct fuse_conn
*fc
)
241 struct dentry
*parent
;
244 if (!fuse_control_sb
)
247 parent
= fuse_control_sb
->s_root
;
248 inc_nlink(parent
->d_inode
);
249 sprintf(name
, "%u", fc
->dev
);
250 parent
= fuse_ctl_add_dentry(parent
, fc
, name
, S_IFDIR
| 0500, 2,
251 &simple_dir_inode_operations
,
252 &simple_dir_operations
);
256 if (!fuse_ctl_add_dentry(parent
, fc
, "waiting", S_IFREG
| 0400, 1,
257 NULL
, &fuse_ctl_waiting_ops
) ||
258 !fuse_ctl_add_dentry(parent
, fc
, "abort", S_IFREG
| 0200, 1,
259 NULL
, &fuse_ctl_abort_ops
) ||
260 !fuse_ctl_add_dentry(parent
, fc
, "max_background", S_IFREG
| 0600,
261 1, NULL
, &fuse_conn_max_background_ops
) ||
262 !fuse_ctl_add_dentry(parent
, fc
, "congestion_threshold",
263 S_IFREG
| 0600, 1, NULL
,
264 &fuse_conn_congestion_threshold_ops
))
270 fuse_ctl_remove_conn(fc
);
275 * Remove a connection from the control filesystem (if it exists).
276 * Caller must hold fuse_mutex
278 void fuse_ctl_remove_conn(struct fuse_conn
*fc
)
282 if (!fuse_control_sb
)
285 for (i
= fc
->ctl_ndents
- 1; i
>= 0; i
--) {
286 struct dentry
*dentry
= fc
->ctl_dentry
[i
];
287 dentry
->d_inode
->i_private
= NULL
;
291 drop_nlink(fuse_control_sb
->s_root
->d_inode
);
294 static int fuse_ctl_fill_super(struct super_block
*sb
, void *data
, int silent
)
296 struct tree_descr empty_descr
= {""};
297 struct fuse_conn
*fc
;
300 err
= simple_fill_super(sb
, FUSE_CTL_SUPER_MAGIC
, &empty_descr
);
304 mutex_lock(&fuse_mutex
);
305 BUG_ON(fuse_control_sb
);
306 fuse_control_sb
= sb
;
307 list_for_each_entry(fc
, &fuse_conn_list
, entry
) {
308 err
= fuse_ctl_add_conn(fc
);
310 fuse_control_sb
= NULL
;
311 mutex_unlock(&fuse_mutex
);
315 mutex_unlock(&fuse_mutex
);
320 static int fuse_ctl_get_sb(struct file_system_type
*fs_type
, int flags
,
321 const char *dev_name
, void *raw_data
,
322 struct vfsmount
*mnt
)
324 return get_sb_single(fs_type
, flags
, raw_data
,
325 fuse_ctl_fill_super
, mnt
);
328 static void fuse_ctl_kill_sb(struct super_block
*sb
)
330 struct fuse_conn
*fc
;
332 mutex_lock(&fuse_mutex
);
333 fuse_control_sb
= NULL
;
334 list_for_each_entry(fc
, &fuse_conn_list
, entry
)
336 mutex_unlock(&fuse_mutex
);
338 kill_litter_super(sb
);
341 static struct file_system_type fuse_ctl_fs_type
= {
342 .owner
= THIS_MODULE
,
344 .get_sb
= fuse_ctl_get_sb
,
345 .kill_sb
= fuse_ctl_kill_sb
,
348 int __init
fuse_ctl_init(void)
350 return register_filesystem(&fuse_ctl_fs_type
);
353 void fuse_ctl_cleanup(void)
355 unregister_filesystem(&fuse_ctl_fs_type
);