Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 3 | Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | |
| 14 | #define FUSE_CTL_SUPER_MAGIC 0x65735543 |
| 15 | |
| 16 | /* |
| 17 | * This is non-NULL when the single instance of the control filesystem |
| 18 | * exists. Protected by fuse_mutex |
| 19 | */ |
| 20 | static struct super_block *fuse_control_sb; |
| 21 | |
| 22 | static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file) |
| 23 | { |
| 24 | struct fuse_conn *fc; |
| 25 | mutex_lock(&fuse_mutex); |
Josef Sipek | 7706a9d | 2006-12-08 02:37:02 -0800 | [diff] [blame] | 26 | fc = file->f_path.dentry->d_inode->i_private; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 27 | if (fc) |
| 28 | fc = fuse_conn_get(fc); |
| 29 | mutex_unlock(&fuse_mutex); |
| 30 | return fc; |
| 31 | } |
| 32 | |
| 33 | static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf, |
| 34 | size_t count, loff_t *ppos) |
| 35 | { |
| 36 | struct fuse_conn *fc = fuse_ctl_file_conn_get(file); |
| 37 | if (fc) { |
| 38 | fuse_abort_conn(fc); |
| 39 | fuse_conn_put(fc); |
| 40 | } |
| 41 | return count; |
| 42 | } |
| 43 | |
| 44 | static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf, |
| 45 | size_t len, loff_t *ppos) |
| 46 | { |
| 47 | char tmp[32]; |
| 48 | size_t size; |
| 49 | |
| 50 | if (!*ppos) { |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 51 | long value; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 52 | struct fuse_conn *fc = fuse_ctl_file_conn_get(file); |
| 53 | if (!fc) |
| 54 | return 0; |
| 55 | |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 56 | value = atomic_read(&fc->num_waiting); |
| 57 | file->private_data = (void *)value; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 58 | fuse_conn_put(fc); |
| 59 | } |
| 60 | size = sprintf(tmp, "%ld\n", (long)file->private_data); |
| 61 | return simple_read_from_buffer(buf, len, ppos, tmp, size); |
| 62 | } |
| 63 | |
| 64 | static const struct file_operations fuse_ctl_abort_ops = { |
| 65 | .open = nonseekable_open, |
| 66 | .write = fuse_conn_abort_write, |
| 67 | }; |
| 68 | |
| 69 | static const struct file_operations fuse_ctl_waiting_ops = { |
| 70 | .open = nonseekable_open, |
| 71 | .read = fuse_conn_waiting_read, |
| 72 | }; |
| 73 | |
| 74 | static struct dentry *fuse_ctl_add_dentry(struct dentry *parent, |
| 75 | struct fuse_conn *fc, |
| 76 | const char *name, |
| 77 | int mode, int nlink, |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 78 | const struct inode_operations *iop, |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 79 | const struct file_operations *fop) |
| 80 | { |
| 81 | struct dentry *dentry; |
| 82 | struct inode *inode; |
| 83 | |
| 84 | BUG_ON(fc->ctl_ndents >= FUSE_CTL_NUM_DENTRIES); |
| 85 | dentry = d_alloc_name(parent, name); |
| 86 | if (!dentry) |
| 87 | return NULL; |
| 88 | |
| 89 | fc->ctl_dentry[fc->ctl_ndents++] = dentry; |
| 90 | inode = new_inode(fuse_control_sb); |
| 91 | if (!inode) |
| 92 | return NULL; |
| 93 | |
| 94 | inode->i_mode = mode; |
| 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 */ |
| 99 | if (iop) |
| 100 | inode->i_op = iop; |
| 101 | inode->i_fop = fop; |
| 102 | inode->i_nlink = nlink; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 103 | inode->i_private = fc; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 104 | d_add(dentry, inode); |
| 105 | return dentry; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Add a connection to the control filesystem (if it exists). Caller |
Miklos Szeredi | 873302c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 110 | * must hold fuse_mutex |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 111 | */ |
| 112 | int fuse_ctl_add_conn(struct fuse_conn *fc) |
| 113 | { |
| 114 | struct dentry *parent; |
| 115 | char name[32]; |
| 116 | |
| 117 | if (!fuse_control_sb) |
| 118 | return 0; |
| 119 | |
| 120 | parent = fuse_control_sb->s_root; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 121 | inc_nlink(parent->d_inode); |
Miklos Szeredi | b6f2fcb | 2008-04-30 00:54:34 -0700 | [diff] [blame] | 122 | sprintf(name, "%u", fc->dev); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 123 | parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2, |
| 124 | &simple_dir_inode_operations, |
| 125 | &simple_dir_operations); |
| 126 | if (!parent) |
| 127 | goto err; |
| 128 | |
| 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)) |
| 133 | goto err; |
| 134 | |
| 135 | return 0; |
| 136 | |
| 137 | err: |
| 138 | fuse_ctl_remove_conn(fc); |
| 139 | return -ENOMEM; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Remove a connection from the control filesystem (if it exists). |
Miklos Szeredi | 873302c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 144 | * Caller must hold fuse_mutex |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 145 | */ |
| 146 | void fuse_ctl_remove_conn(struct fuse_conn *fc) |
| 147 | { |
| 148 | int i; |
| 149 | |
| 150 | if (!fuse_control_sb) |
| 151 | return; |
| 152 | |
| 153 | for (i = fc->ctl_ndents - 1; i >= 0; i--) { |
| 154 | struct dentry *dentry = fc->ctl_dentry[i]; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 155 | dentry->d_inode->i_private = NULL; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 156 | d_drop(dentry); |
| 157 | dput(dentry); |
| 158 | } |
| 159 | fuse_control_sb->s_root->d_inode->i_nlink--; |
| 160 | } |
| 161 | |
| 162 | static int fuse_ctl_fill_super(struct super_block *sb, void *data, int silent) |
| 163 | { |
| 164 | struct tree_descr empty_descr = {""}; |
| 165 | struct fuse_conn *fc; |
| 166 | int err; |
| 167 | |
| 168 | err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr); |
| 169 | if (err) |
| 170 | return err; |
| 171 | |
| 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); |
| 177 | if (err) { |
| 178 | fuse_control_sb = NULL; |
| 179 | mutex_unlock(&fuse_mutex); |
| 180 | return err; |
| 181 | } |
| 182 | } |
| 183 | mutex_unlock(&fuse_mutex); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 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) |
| 191 | { |
| 192 | return get_sb_single(fs_type, flags, raw_data, |
| 193 | fuse_ctl_fill_super, mnt); |
| 194 | } |
| 195 | |
| 196 | static void fuse_ctl_kill_sb(struct super_block *sb) |
| 197 | { |
Miklos Szeredi | ff79544 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 198 | struct fuse_conn *fc; |
| 199 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 200 | mutex_lock(&fuse_mutex); |
| 201 | fuse_control_sb = NULL; |
Miklos Szeredi | ff79544 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 202 | list_for_each_entry(fc, &fuse_conn_list, entry) |
| 203 | fc->ctl_ndents = 0; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 204 | mutex_unlock(&fuse_mutex); |
| 205 | |
| 206 | kill_litter_super(sb); |
| 207 | } |
| 208 | |
| 209 | static struct file_system_type fuse_ctl_fs_type = { |
| 210 | .owner = THIS_MODULE, |
| 211 | .name = "fusectl", |
| 212 | .get_sb = fuse_ctl_get_sb, |
| 213 | .kill_sb = fuse_ctl_kill_sb, |
| 214 | }; |
| 215 | |
| 216 | int __init fuse_ctl_init(void) |
| 217 | { |
| 218 | return register_filesystem(&fuse_ctl_fs_type); |
| 219 | } |
| 220 | |
| 221 | void fuse_ctl_cleanup(void) |
| 222 | { |
| 223 | unregister_filesystem(&fuse_ctl_fs_type); |
| 224 | } |