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 const struct file_operations fuse_ctl_abort_ops
= {
65 .open
= nonseekable_open
,
66 .write
= fuse_conn_abort_write
,
69 static const struct file_operations fuse_ctl_waiting_ops
= {
70 .open
= nonseekable_open
,
71 .read
= fuse_conn_waiting_read
,
74 static struct dentry
*fuse_ctl_add_dentry(struct dentry
*parent
,
78 const struct inode_operations
*iop
,
79 const struct file_operations
*fop
)
81 struct dentry
*dentry
;
84 BUG_ON(fc
->ctl_ndents
>= FUSE_CTL_NUM_DENTRIES
);
85 dentry
= d_alloc_name(parent
, name
);
89 fc
->ctl_dentry
[fc
->ctl_ndents
++] = dentry
;
90 inode
= new_inode(fuse_control_sb
);
95 inode
->i_uid
= fc
->user_id
;
96 inode
->i_gid
= fc
->group_id
;
97 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
98 /* setting ->i_op to NULL is not allowed */
102 inode
->i_nlink
= nlink
;
103 inode
->i_private
= fc
;
104 d_add(dentry
, inode
);
109 * Add a connection to the control filesystem (if it exists). Caller
110 * must hold fuse_mutex
112 int fuse_ctl_add_conn(struct fuse_conn
*fc
)
114 struct dentry
*parent
;
117 if (!fuse_control_sb
)
120 parent
= fuse_control_sb
->s_root
;
121 inc_nlink(parent
->d_inode
);
122 sprintf(name
, "%u", fc
->dev
);
123 parent
= fuse_ctl_add_dentry(parent
, fc
, name
, S_IFDIR
| 0500, 2,
124 &simple_dir_inode_operations
,
125 &simple_dir_operations
);
129 if (!fuse_ctl_add_dentry(parent
, fc
, "waiting", S_IFREG
| 0400, 1,
130 NULL
, &fuse_ctl_waiting_ops
) ||
131 !fuse_ctl_add_dentry(parent
, fc
, "abort", S_IFREG
| 0200, 1,
132 NULL
, &fuse_ctl_abort_ops
))
138 fuse_ctl_remove_conn(fc
);
143 * Remove a connection from the control filesystem (if it exists).
144 * Caller must hold fuse_mutex
146 void fuse_ctl_remove_conn(struct fuse_conn
*fc
)
150 if (!fuse_control_sb
)
153 for (i
= fc
->ctl_ndents
- 1; i
>= 0; i
--) {
154 struct dentry
*dentry
= fc
->ctl_dentry
[i
];
155 dentry
->d_inode
->i_private
= NULL
;
159 fuse_control_sb
->s_root
->d_inode
->i_nlink
--;
162 static int fuse_ctl_fill_super(struct super_block
*sb
, void *data
, int silent
)
164 struct tree_descr empty_descr
= {""};
165 struct fuse_conn
*fc
;
168 err
= simple_fill_super(sb
, FUSE_CTL_SUPER_MAGIC
, &empty_descr
);
172 mutex_lock(&fuse_mutex
);
173 BUG_ON(fuse_control_sb
);
174 fuse_control_sb
= sb
;
175 list_for_each_entry(fc
, &fuse_conn_list
, entry
) {
176 err
= fuse_ctl_add_conn(fc
);
178 fuse_control_sb
= NULL
;
179 mutex_unlock(&fuse_mutex
);
183 mutex_unlock(&fuse_mutex
);
188 static int fuse_ctl_get_sb(struct file_system_type
*fs_type
, int flags
,
189 const char *dev_name
, void *raw_data
,
190 struct vfsmount
*mnt
)
192 return get_sb_single(fs_type
, flags
, raw_data
,
193 fuse_ctl_fill_super
, mnt
);
196 static void fuse_ctl_kill_sb(struct super_block
*sb
)
198 struct fuse_conn
*fc
;
200 mutex_lock(&fuse_mutex
);
201 fuse_control_sb
= NULL
;
202 list_for_each_entry(fc
, &fuse_conn_list
, entry
)
204 mutex_unlock(&fuse_mutex
);
206 kill_litter_super(sb
);
209 static struct file_system_type fuse_ctl_fs_type
= {
210 .owner
= THIS_MODULE
,
212 .get_sb
= fuse_ctl_get_sb
,
213 .kill_sb
= fuse_ctl_kill_sb
,
216 int __init
fuse_ctl_init(void)
218 return register_filesystem(&fuse_ctl_fs_type
);
221 void fuse_ctl_cleanup(void)
223 unregister_filesystem(&fuse_ctl_fs_type
);