RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / fuse / control.c
blob105d4a271e073b0c7d60660057d9716d798f9d1d
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 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>
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)
24 struct fuse_conn *fc;
25 mutex_lock(&fuse_mutex);
26 fc = file->f_path.dentry->d_inode->i_private;
27 if (fc)
28 fc = fuse_conn_get(fc);
29 mutex_unlock(&fuse_mutex);
30 return fc;
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);
37 if (fc) {
38 fuse_abort_conn(fc);
39 fuse_conn_put(fc);
41 return count;
44 static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
45 size_t len, loff_t *ppos)
47 char tmp[32];
48 size_t size;
50 if (!*ppos) {
51 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
52 if (!fc)
53 return 0;
55 file->private_data=(void *)(long)atomic_read(&fc->num_waiting);
56 fuse_conn_put(fc);
58 size = sprintf(tmp, "%ld\n", (long)file->private_data);
59 return simple_read_from_buffer(buf, len, ppos, tmp, size);
62 static const struct file_operations fuse_ctl_abort_ops = {
63 .open = nonseekable_open,
64 .write = fuse_conn_abort_write,
67 static const struct file_operations fuse_ctl_waiting_ops = {
68 .open = nonseekable_open,
69 .read = fuse_conn_waiting_read,
72 static struct dentry *fuse_ctl_add_dentry(struct dentry *parent,
73 struct fuse_conn *fc,
74 const char *name,
75 int mode, int nlink,
76 const struct inode_operations *iop,
77 const struct file_operations *fop)
79 struct dentry *dentry;
80 struct inode *inode;
82 BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES);
83 dentry = d_alloc_name(parent, name);
84 if (!dentry)
85 return NULL;
87 fc->ctl_dentry[fc->ctl_ndents++] = dentry;
88 inode = new_inode(fuse_control_sb);
89 if (!inode)
90 return NULL;
92 inode->i_mode = mode;
93 inode->i_uid = fc->user_id;
94 inode->i_gid = fc->group_id;
95 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
96 /* setting ->i_op to NULL is not allowed */
97 if (iop)
98 inode->i_op = iop;
99 inode->i_fop = fop;
100 inode->i_nlink = nlink;
101 inode->i_private = fc;
102 d_add(dentry, inode);
103 return dentry;
107 * Add a connection to the control filesystem (if it exists). Caller
108 * must hold fuse_mutex
110 int fuse_ctl_add_conn(struct fuse_conn *fc)
112 struct dentry *parent;
113 char name[32];
115 if (!fuse_control_sb)
116 return 0;
118 parent = fuse_control_sb->s_root;
119 inc_nlink(parent->d_inode);
120 sprintf(name, "%llu", (unsigned long long) fc->id);
121 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2,
122 &simple_dir_inode_operations,
123 &simple_dir_operations);
124 if (!parent)
125 goto err;
127 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1,
128 NULL, &fuse_ctl_waiting_ops) ||
129 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1,
130 NULL, &fuse_ctl_abort_ops))
131 goto err;
133 return 0;
135 err:
136 fuse_ctl_remove_conn(fc);
137 return -ENOMEM;
141 * Remove a connection from the control filesystem (if it exists).
142 * Caller must hold fuse_mutex
144 void fuse_ctl_remove_conn(struct fuse_conn *fc)
146 int i;
148 if (!fuse_control_sb)
149 return;
151 for (i = fc->ctl_ndents - 1; i >= 0; i--) {
152 struct dentry *dentry = fc->ctl_dentry[i];
153 dentry->d_inode->i_private = NULL;
154 d_drop(dentry);
155 dput(dentry);
157 fuse_control_sb->s_root->d_inode->i_nlink--;
160 static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent)
162 struct tree_descr empty_descr = {""};
163 struct fuse_conn *fc;
164 int err;
166 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr);
167 if (err)
168 return err;
170 mutex_lock(&fuse_mutex);
171 BUG_ON(fuse_control_sb);
172 fuse_control_sb = sb;
173 list_for_each_entry(fc, &fuse_conn_list, entry) {
174 err = fuse_ctl_add_conn(fc);
175 if (err) {
176 fuse_control_sb = NULL;
177 mutex_unlock(&fuse_mutex);
178 return err;
181 mutex_unlock(&fuse_mutex);
183 return 0;
186 static int fuse_ctl_get_sb(struct file_system_type *fs_type, int flags,
187 const char *dev_name, void *raw_data,
188 struct vfsmount *mnt)
190 return get_sb_single(fs_type, flags, raw_data,
191 fuse_ctl_fill_super, mnt);
194 static void fuse_ctl_kill_sb(struct super_block *sb)
196 struct fuse_conn *fc;
198 mutex_lock(&fuse_mutex);
199 fuse_control_sb = NULL;
200 list_for_each_entry(fc, &fuse_conn_list, entry)
201 fc->ctl_ndents = 0;
202 mutex_unlock(&fuse_mutex);
204 kill_litter_super(sb);
207 static struct file_system_type fuse_ctl_fs_type = {
208 .owner = THIS_MODULE,
209 .name = "fusectl",
210 .get_sb = fuse_ctl_get_sb,
211 .kill_sb = fuse_ctl_kill_sb,
214 int __init fuse_ctl_init(void)
216 return register_filesystem(&fuse_ctl_fs_type);
219 void fuse_ctl_cleanup(void)
221 unregister_filesystem(&fuse_ctl_fs_type);