at v6.18-rc2 372 lines 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, 209 int mode, int nlink, 210 const struct inode_operations *iop, 211 const struct file_operations *fop) 212{ 213 struct dentry *dentry; 214 struct inode *inode; 215 216 dentry = d_alloc_name(parent, name); 217 if (!dentry) 218 return NULL; 219 220 inode = new_inode(fuse_control_sb); 221 if (!inode) { 222 dput(dentry); 223 return NULL; 224 } 225 226 inode->i_ino = get_next_ino(); 227 inode->i_mode = mode; 228 inode->i_uid = fc->user_id; 229 inode->i_gid = fc->group_id; 230 simple_inode_init_ts(inode); 231 /* setting ->i_op to NULL is not allowed */ 232 if (iop) 233 inode->i_op = iop; 234 inode->i_fop = fop; 235 set_nlink(inode, nlink); 236 inode->i_private = fc; 237 d_add(dentry, inode); 238 239 return dentry; 240} 241 242/* 243 * Add a connection to the control filesystem (if it exists). Caller 244 * must hold fuse_mutex 245 */ 246int fuse_ctl_add_conn(struct fuse_conn *fc) 247{ 248 struct dentry *parent; 249 char name[32]; 250 251 if (!fuse_control_sb || fc->no_control) 252 return 0; 253 254 parent = fuse_control_sb->s_root; 255 inc_nlink(d_inode(parent)); 256 sprintf(name, "%u", fc->dev); 257 parent = fuse_ctl_add_dentry(parent, fc, name, S_IFDIR | 0500, 2, 258 &simple_dir_inode_operations, 259 &simple_dir_operations); 260 if (!parent) 261 goto err; 262 263 if (!fuse_ctl_add_dentry(parent, fc, "waiting", S_IFREG | 0400, 1, 264 NULL, &fuse_ctl_waiting_ops) || 265 !fuse_ctl_add_dentry(parent, fc, "abort", S_IFREG | 0200, 1, 266 NULL, &fuse_ctl_abort_ops) || 267 !fuse_ctl_add_dentry(parent, fc, "max_background", S_IFREG | 0600, 268 1, NULL, &fuse_conn_max_background_ops) || 269 !fuse_ctl_add_dentry(parent, fc, "congestion_threshold", 270 S_IFREG | 0600, 1, NULL, 271 &fuse_conn_congestion_threshold_ops)) 272 goto err; 273 274 return 0; 275 276 err: 277 fuse_ctl_remove_conn(fc); 278 return -ENOMEM; 279} 280 281static void remove_one(struct dentry *dentry) 282{ 283 d_inode(dentry)->i_private = NULL; 284} 285 286/* 287 * Remove a connection from the control filesystem (if it exists). 288 * Caller must hold fuse_mutex 289 */ 290void fuse_ctl_remove_conn(struct fuse_conn *fc) 291{ 292 struct dentry *dentry; 293 char name[32]; 294 295 if (!fuse_control_sb || fc->no_control) 296 return; 297 298 sprintf(name, "%u", fc->dev); 299 dentry = lookup_noperm_positive_unlocked(&QSTR(name), fuse_control_sb->s_root); 300 if (!IS_ERR(dentry)) { 301 simple_recursive_removal(dentry, remove_one); 302 dput(dentry); // paired with lookup_noperm_positive_unlocked() 303 } 304} 305 306static int fuse_ctl_fill_super(struct super_block *sb, struct fs_context *fsc) 307{ 308 static const struct tree_descr empty_descr = {""}; 309 struct fuse_conn *fc; 310 int err; 311 312 err = simple_fill_super(sb, FUSE_CTL_SUPER_MAGIC, &empty_descr); 313 if (err) 314 return err; 315 316 mutex_lock(&fuse_mutex); 317 BUG_ON(fuse_control_sb); 318 fuse_control_sb = sb; 319 list_for_each_entry(fc, &fuse_conn_list, entry) { 320 err = fuse_ctl_add_conn(fc); 321 if (err) { 322 fuse_control_sb = NULL; 323 mutex_unlock(&fuse_mutex); 324 return err; 325 } 326 } 327 mutex_unlock(&fuse_mutex); 328 329 return 0; 330} 331 332static int fuse_ctl_get_tree(struct fs_context *fsc) 333{ 334 return get_tree_single(fsc, fuse_ctl_fill_super); 335} 336 337static const struct fs_context_operations fuse_ctl_context_ops = { 338 .get_tree = fuse_ctl_get_tree, 339}; 340 341static int fuse_ctl_init_fs_context(struct fs_context *fsc) 342{ 343 fsc->ops = &fuse_ctl_context_ops; 344 return 0; 345} 346 347static void fuse_ctl_kill_sb(struct super_block *sb) 348{ 349 mutex_lock(&fuse_mutex); 350 fuse_control_sb = NULL; 351 mutex_unlock(&fuse_mutex); 352 353 kill_litter_super(sb); 354} 355 356static struct file_system_type fuse_ctl_fs_type = { 357 .owner = THIS_MODULE, 358 .name = "fusectl", 359 .init_fs_context = fuse_ctl_init_fs_context, 360 .kill_sb = fuse_ctl_kill_sb, 361}; 362MODULE_ALIAS_FS("fusectl"); 363 364int __init fuse_ctl_init(void) 365{ 366 return register_filesystem(&fuse_ctl_fs_type); 367} 368 369void __exit fuse_ctl_cleanup(void) 370{ 371 unregister_filesystem(&fuse_ctl_fs_type); 372}