at master 8.4 kB view raw
1/* 2 FUSE: Filesystem in Userspace 3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> 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#include <linux/fs_context.h> 14#include <linux/namei.h> 15 16#define FUSE_CTL_SUPER_MAGIC 0x65735543 17 18/* 19 * This is non-NULL when the single instance of the control filesystem 20 * exists. Protected by fuse_mutex 21 */ 22static struct super_block *fuse_control_sb; 23 24static struct fuse_conn *fuse_ctl_file_conn_get(struct file *file) 25{ 26 struct fuse_conn *fc; 27 mutex_lock(&fuse_mutex); 28 fc = file_inode(file)->i_private; 29 if (fc) 30 fc = fuse_conn_get(fc); 31 mutex_unlock(&fuse_mutex); 32 return fc; 33} 34 35static ssize_t fuse_conn_abort_write(struct file *file, const char __user *buf, 36 size_t count, loff_t *ppos) 37{ 38 struct fuse_conn *fc = fuse_ctl_file_conn_get(file); 39 if (fc) { 40 if (fc->abort_err) 41 fc->aborted = true; 42 fuse_abort_conn(fc); 43 fuse_conn_put(fc); 44 } 45 return count; 46} 47 48static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf, 49 size_t len, loff_t *ppos) 50{ 51 char tmp[32]; 52 size_t size; 53 54 if (!*ppos) { 55 long value; 56 struct fuse_conn *fc = fuse_ctl_file_conn_get(file); 57 if (!fc) 58 return 0; 59 60 value = atomic_read(&fc->num_waiting); 61 file->private_data = (void *)value; 62 fuse_conn_put(fc); 63 } 64 size = sprintf(tmp, "%ld\n", (long)file->private_data); 65 return simple_read_from_buffer(buf, len, ppos, tmp, size); 66} 67 68static ssize_t fuse_conn_limit_read(struct file *file, char __user *buf, 69 size_t len, loff_t *ppos, unsigned val) 70{ 71 char tmp[32]; 72 size_t size = sprintf(tmp, "%u\n", val); 73 74 return simple_read_from_buffer(buf, len, ppos, tmp, size); 75} 76 77static ssize_t fuse_conn_limit_write(struct file *file, const char __user *buf, 78 size_t count, loff_t *ppos, unsigned *val, 79 unsigned global_limit) 80{ 81 unsigned long t; 82 unsigned limit = (1 << 16) - 1; 83 int err; 84 85 if (*ppos) 86 return -EINVAL; 87 88 err = kstrtoul_from_user(buf, count, 0, &t); 89 if (err) 90 return err; 91 92 if (!capable(CAP_SYS_ADMIN)) 93 limit = min(limit, global_limit); 94 95 if (t > limit) 96 return -EINVAL; 97 98 *val = t; 99 100 return count; 101} 102 103static ssize_t fuse_conn_max_background_read(struct file *file, 104 char __user *buf, size_t len, 105 loff_t *ppos) 106{ 107 struct fuse_conn *fc; 108 unsigned val; 109 110 fc = fuse_ctl_file_conn_get(file); 111 if (!fc) 112 return 0; 113 114 val = READ_ONCE(fc->max_background); 115 fuse_conn_put(fc); 116 117 return fuse_conn_limit_read(file, buf, len, ppos, val); 118} 119 120static ssize_t fuse_conn_max_background_write(struct file *file, 121 const char __user *buf, 122 size_t count, loff_t *ppos) 123{ 124 unsigned val; 125 ssize_t ret; 126 127 ret = fuse_conn_limit_write(file, buf, count, ppos, &val, 128 max_user_bgreq); 129 if (ret > 0) { 130 struct fuse_conn *fc = fuse_ctl_file_conn_get(file); 131 if (fc) { 132 spin_lock(&fc->bg_lock); 133 fc->max_background = val; 134 fc->blocked = fc->num_background >= fc->max_background; 135 if (!fc->blocked) 136 wake_up(&fc->blocked_waitq); 137 spin_unlock(&fc->bg_lock); 138 fuse_conn_put(fc); 139 } 140 } 141 142 return ret; 143} 144 145static ssize_t fuse_conn_congestion_threshold_read(struct file *file, 146 char __user *buf, size_t len, 147 loff_t *ppos) 148{ 149 struct fuse_conn *fc; 150 unsigned val; 151 152 fc = fuse_ctl_file_conn_get(file); 153 if (!fc) 154 return 0; 155 156 val = READ_ONCE(fc->congestion_threshold); 157 fuse_conn_put(fc); 158 159 return fuse_conn_limit_read(file, buf, len, ppos, val); 160} 161 162static ssize_t fuse_conn_congestion_threshold_write(struct file *file, 163 const char __user *buf, 164 size_t count, loff_t *ppos) 165{ 166 unsigned val; 167 struct fuse_conn *fc; 168 ssize_t ret; 169 170 ret = fuse_conn_limit_write(file, buf, count, ppos, &val, 171 max_user_congthresh); 172 if (ret <= 0) 173 goto out; 174 fc = fuse_ctl_file_conn_get(file); 175 if (!fc) 176 goto out; 177 178 WRITE_ONCE(fc->congestion_threshold, val); 179 fuse_conn_put(fc); 180out: 181 return ret; 182} 183 184static const struct file_operations fuse_ctl_abort_ops = { 185 .open = nonseekable_open, 186 .write = fuse_conn_abort_write, 187}; 188 189static const struct file_operations fuse_ctl_waiting_ops = { 190 .open = nonseekable_open, 191 .read = fuse_conn_waiting_read, 192}; 193 194static const struct file_operations fuse_conn_max_background_ops = { 195 .open = nonseekable_open, 196 .read = fuse_conn_max_background_read, 197 .write = fuse_conn_max_background_write, 198}; 199 200static const struct file_operations fuse_conn_congestion_threshold_ops = { 201 .open = nonseekable_open, 202 .read = fuse_conn_congestion_threshold_read, 203 .write = fuse_conn_congestion_threshold_write, 204}; 205 206static struct dentry *fuse_ctl_add_dentry(struct dentry *parent, 207 struct fuse_conn *fc, 208 const char *name, int mode, 209 const struct inode_operations *iop, 210 const struct file_operations *fop) 211{ 212 struct dentry *dentry; 213 struct inode *inode; 214 215 dentry = d_alloc_name(parent, name); 216 if (!dentry) 217 return NULL; 218 219 inode = new_inode(fuse_control_sb); 220 if (!inode) { 221 dput(dentry); 222 return NULL; 223 } 224 225 inode->i_ino = get_next_ino(); 226 inode->i_mode = mode; 227 inode->i_uid = fc->user_id; 228 inode->i_gid = fc->group_id; 229 simple_inode_init_ts(inode); 230 /* setting ->i_op to NULL is not allowed */ 231 if (iop) 232 inode->i_op = iop; 233 inode->i_fop = fop; 234 if (S_ISDIR(mode)) { 235 inc_nlink(d_inode(parent)); 236 inc_nlink(inode); 237 } 238 inode->i_private = fc; 239 d_make_persistent(dentry, inode); 240 dput(dentry); 241 242 /* 243 * We are returning a borrowed reference here - it's only good while 244 * fuse_mutex is held. Actually it's d_make_persistent() return 245 * value... 246 */ 247 return dentry; 248} 249 250/* 251 * Add a connection to the control filesystem (if it exists). Caller 252 * must hold fuse_mutex 253 */ 254int fuse_ctl_add_conn(struct fuse_conn *fc) 255{ 256 struct dentry *parent; 257 char name[32]; 258 259 if (!fuse_control_sb || fc->no_control) 260 return 0; 261 262 parent = fuse_control_sb->s_root; 263 sprintf(name, "%u", fc->dev); 264 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 265 &simple_dir_inode_operations, 266 &simple_dir_operations); 267 if (!parent) 268 goto err; 269 270 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 271 NULL, &fuse_ctl_waiting_ops) || 272 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 273 NULL, &fuse_ctl_abort_ops) || 274 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600, 275 NULL, &fuse_conn_max_background_ops) || 276 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold", 277 S_IFREG | 0600, NULL, 278 &fuse_conn_congestion_threshold_ops)) 279 goto err; 280 281 return 0; 282 283 err: 284 fuse_ctl_remove_conn(fc); 285 return -ENOMEM; 286} 287 288static void remove_one(struct dentry *dentry) 289{ 290 d_inode(dentry)->i_private = NULL; 291} 292 293/* 294 * Remove a connection from the control filesystem (if it exists). 295 * Caller must hold fuse_mutex 296 */ 297void fuse_ctl_remove_conn(struct fuse_conn *fc) 298{ 299 char name[32]; 300 301 if (!fuse_control_sb || fc->no_control) 302 return; 303 304 sprintf(name, "%u", fc->dev); 305 simple_remove_by_name(fuse_control_sb->s_root, name, remove_one); 306} 307 308static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fsc) 309{ 310 static const struct tree_descr empty_descr = {""}; 311 struct fuse_conn *fc; 312 int err; 313 314 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr); 315 if (err) 316 return err; 317 318 mutex_lock(&fuse_mutex); 319 BUG_ON(fuse_control_sb); 320 fuse_control_sb = sb; 321 list_for_each_entry(fc, &fuse_conn_list, entry) { 322 err = fuse_ctl_add_conn(fc); 323 if (err) { 324 fuse_control_sb = NULL; 325 mutex_unlock(&fuse_mutex); 326 return err; 327 } 328 } 329 mutex_unlock(&fuse_mutex); 330 331 return 0; 332} 333 334static int fuse_ctl_get_tree(struct fs_context *fsc) 335{ 336 return get_tree_single(fsc, fuse_ctl_fill_super); 337} 338 339static const struct fs_context_operations fuse_ctl_context_ops = { 340 .get_tree = fuse_ctl_get_tree, 341}; 342 343static int fuse_ctl_init_fs_context(struct fs_context *fsc) 344{ 345 fsc->ops = &fuse_ctl_context_ops; 346 return 0; 347} 348 349static void fuse_ctl_kill_sb(struct super_block *sb) 350{ 351 mutex_lock(&fuse_mutex); 352 fuse_control_sb = NULL; 353 mutex_unlock(&fuse_mutex); 354 355 kill_anon_super(sb); 356} 357 358static struct file_system_type fuse_ctl_fs_type = { 359 .owner = THIS_MODULE, 360 .name = "fusectl", 361 .init_fs_context = fuse_ctl_init_fs_context, 362 .kill_sb = fuse_ctl_kill_sb, 363}; 364MODULE_ALIAS_FS("fusectl"); 365 366int __init fuse_ctl_init(void) 367{ 368 return register_filesystem(&fuse_ctl_fs_type); 369} 370 371void __exit fuse_ctl_cleanup(void) 372{ 373 unregister_filesystem(&fuse_ctl_fs_type); 374}