lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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/pagemap.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/file.h> |
| 14 | #include <linux/seq_file.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/moduleparam.h> |
| 18 | #include <linux/parser.h> |
| 19 | #include <linux/statfs.h> |
| 20 | #include <linux/random.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/exportfs.h> |
| 23 | |
| 24 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 25 | MODULE_DESCRIPTION("Filesystem in Userspace"); |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | |
| 28 | static struct kmem_cache *fuse_inode_cachep; |
| 29 | struct list_head fuse_conn_list; |
| 30 | DEFINE_MUTEX(fuse_mutex); |
| 31 | |
| 32 | static int set_global_limit(const char *val, struct kernel_param *kp); |
| 33 | |
| 34 | unsigned max_user_bgreq; |
| 35 | module_param_call(max_user_bgreq, set_global_limit, param_get_uint, |
| 36 | &max_user_bgreq, 0644); |
| 37 | __MODULE_PARM_TYPE(max_user_bgreq, "uint"); |
| 38 | MODULE_PARM_DESC(max_user_bgreq, |
| 39 | "Global limit for the maximum number of backgrounded requests an " |
| 40 | "unprivileged user can set"); |
| 41 | |
| 42 | unsigned max_user_congthresh; |
| 43 | module_param_call(max_user_congthresh, set_global_limit, param_get_uint, |
| 44 | &max_user_congthresh, 0644); |
| 45 | __MODULE_PARM_TYPE(max_user_congthresh, "uint"); |
| 46 | MODULE_PARM_DESC(max_user_congthresh, |
| 47 | "Global limit for the maximum congestion threshold an " |
| 48 | "unprivileged user can set"); |
| 49 | |
| 50 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 51 | |
| 52 | #define FUSE_DEFAULT_BLKSIZE 512 |
| 53 | |
| 54 | /** Maximum number of outstanding background requests */ |
| 55 | #define FUSE_DEFAULT_MAX_BACKGROUND 12 |
| 56 | |
| 57 | /** Congestion starts at 75% of maximum */ |
| 58 | #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4) |
| 59 | |
| 60 | struct fuse_mount_data { |
| 61 | int fd; |
| 62 | unsigned rootmode; |
| 63 | unsigned user_id; |
| 64 | unsigned group_id; |
| 65 | unsigned fd_present:1; |
| 66 | unsigned rootmode_present:1; |
| 67 | unsigned user_id_present:1; |
| 68 | unsigned group_id_present:1; |
| 69 | unsigned flags; |
| 70 | unsigned max_read; |
| 71 | unsigned blksize; |
| 72 | }; |
| 73 | |
| 74 | struct fuse_forget_link *fuse_alloc_forget(void) |
| 75 | { |
| 76 | return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL); |
| 77 | } |
| 78 | |
| 79 | static struct inode *fuse_alloc_inode(struct super_block *sb) |
| 80 | { |
| 81 | struct inode *inode; |
| 82 | struct fuse_inode *fi; |
| 83 | |
| 84 | inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL); |
| 85 | if (!inode) |
| 86 | return NULL; |
| 87 | |
| 88 | fi = get_fuse_inode(inode); |
| 89 | fi->i_time = 0; |
| 90 | fi->nodeid = 0; |
| 91 | fi->nlookup = 0; |
| 92 | fi->attr_version = 0; |
| 93 | fi->writectr = 0; |
| 94 | fi->orig_ino = 0; |
| 95 | fi->state = 0; |
| 96 | INIT_LIST_HEAD(&fi->write_files); |
| 97 | INIT_LIST_HEAD(&fi->queued_writes); |
| 98 | INIT_LIST_HEAD(&fi->writepages); |
| 99 | init_waitqueue_head(&fi->page_waitq); |
| 100 | fi->forget = fuse_alloc_forget(); |
| 101 | if (!fi->forget) { |
| 102 | kmem_cache_free(fuse_inode_cachep, inode); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | return inode; |
| 107 | } |
| 108 | |
| 109 | static void fuse_i_callback(struct rcu_head *head) |
| 110 | { |
| 111 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 112 | kmem_cache_free(fuse_inode_cachep, inode); |
| 113 | } |
| 114 | |
| 115 | static void fuse_destroy_inode(struct inode *inode) |
| 116 | { |
| 117 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 118 | BUG_ON(!list_empty(&fi->write_files)); |
| 119 | BUG_ON(!list_empty(&fi->queued_writes)); |
| 120 | kfree(fi->forget); |
| 121 | call_rcu(&inode->i_rcu, fuse_i_callback); |
| 122 | } |
| 123 | |
| 124 | static void fuse_evict_inode(struct inode *inode) |
| 125 | { |
| 126 | truncate_inode_pages(&inode->i_data, 0); |
| 127 | end_writeback(inode); |
| 128 | if (inode->i_sb->s_flags & MS_ACTIVE) { |
| 129 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 130 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 131 | fuse_queue_forget(fc, fi->forget, fi->nodeid, fi->nlookup); |
| 132 | fi->forget = NULL; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static int fuse_remount_fs(struct super_block *sb, int *flags, char *data) |
| 137 | { |
| 138 | if (*flags & MS_MANDLOCK) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down |
| 146 | * so that it will fit. |
| 147 | */ |
| 148 | static ino_t fuse_squash_ino(u64 ino64) |
| 149 | { |
| 150 | ino_t ino = (ino_t) ino64; |
| 151 | if (sizeof(ino_t) < sizeof(u64)) |
| 152 | ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8; |
| 153 | return ino; |
| 154 | } |
| 155 | |
| 156 | void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr, |
| 157 | u64 attr_valid) |
| 158 | { |
| 159 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 160 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 161 | |
| 162 | fi->attr_version = ++fc->attr_version; |
| 163 | fi->i_time = attr_valid; |
| 164 | |
| 165 | inode->i_ino = fuse_squash_ino(attr->ino); |
| 166 | inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); |
| 167 | set_nlink(inode, attr->nlink); |
| 168 | inode->i_uid = attr->uid; |
| 169 | inode->i_gid = attr->gid; |
| 170 | inode->i_blocks = attr->blocks; |
| 171 | inode->i_atime.tv_sec = attr->atime; |
| 172 | inode->i_atime.tv_nsec = attr->atimensec; |
| 173 | inode->i_mtime.tv_sec = attr->mtime; |
| 174 | inode->i_mtime.tv_nsec = attr->mtimensec; |
| 175 | inode->i_ctime.tv_sec = attr->ctime; |
| 176 | inode->i_ctime.tv_nsec = attr->ctimensec; |
| 177 | |
| 178 | if (attr->blksize != 0) |
| 179 | inode->i_blkbits = ilog2(attr->blksize); |
| 180 | else |
| 181 | inode->i_blkbits = inode->i_sb->s_blocksize_bits; |
| 182 | |
| 183 | /* |
| 184 | * Don't set the sticky bit in i_mode, unless we want the VFS |
| 185 | * to check permissions. This prevents failures due to the |
| 186 | * check in may_delete(). |
| 187 | */ |
| 188 | fi->orig_i_mode = inode->i_mode; |
| 189 | if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS)) |
| 190 | inode->i_mode &= ~S_ISVTX; |
| 191 | |
| 192 | fi->orig_ino = attr->ino; |
| 193 | } |
| 194 | |
| 195 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr, |
| 196 | u64 attr_valid, u64 attr_version) |
| 197 | { |
| 198 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 199 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 200 | loff_t oldsize; |
| 201 | |
| 202 | spin_lock(&fc->lock); |
| 203 | if ((attr_version != 0 && fi->attr_version > attr_version) || |
| 204 | test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { |
| 205 | spin_unlock(&fc->lock); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | fuse_change_attributes_common(inode, attr, attr_valid); |
| 210 | |
| 211 | oldsize = inode->i_size; |
| 212 | i_size_write(inode, attr->size); |
| 213 | spin_unlock(&fc->lock); |
| 214 | |
| 215 | if (S_ISREG(inode->i_mode) && oldsize != attr->size) { |
| 216 | truncate_pagecache(inode, oldsize, attr->size); |
| 217 | invalidate_inode_pages2(inode->i_mapping); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) |
| 222 | { |
| 223 | inode->i_mode = attr->mode & S_IFMT; |
| 224 | inode->i_size = attr->size; |
| 225 | if (S_ISREG(inode->i_mode)) { |
| 226 | fuse_init_common(inode); |
| 227 | fuse_init_file_inode(inode); |
| 228 | } else if (S_ISDIR(inode->i_mode)) |
| 229 | fuse_init_dir(inode); |
| 230 | else if (S_ISLNK(inode->i_mode)) |
| 231 | fuse_init_symlink(inode); |
| 232 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 233 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 234 | fuse_init_common(inode); |
| 235 | init_special_inode(inode, inode->i_mode, |
| 236 | new_decode_dev(attr->rdev)); |
| 237 | } else |
| 238 | BUG(); |
| 239 | } |
| 240 | |
| 241 | int fuse_inode_eq(struct inode *inode, void *_nodeidp) |
| 242 | { |
| 243 | u64 nodeid = *(u64 *) _nodeidp; |
| 244 | if (get_node_id(inode) == nodeid) |
| 245 | return 1; |
| 246 | else |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) |
| 251 | { |
| 252 | u64 nodeid = *(u64 *) _nodeidp; |
| 253 | get_fuse_inode(inode)->nodeid = nodeid; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | struct inode *fuse_iget(struct super_block *sb, u64 nodeid, |
| 258 | int generation, struct fuse_attr *attr, |
| 259 | u64 attr_valid, u64 attr_version) |
| 260 | { |
| 261 | struct inode *inode; |
| 262 | struct fuse_inode *fi; |
| 263 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 264 | |
| 265 | retry: |
| 266 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); |
| 267 | if (!inode) |
| 268 | return NULL; |
| 269 | |
| 270 | if ((inode->i_state & I_NEW)) { |
| 271 | inode->i_flags |= S_NOATIME|S_NOCMTIME; |
| 272 | inode->i_generation = generation; |
| 273 | inode->i_data.backing_dev_info = &fc->bdi; |
| 274 | fuse_init_inode(inode, attr); |
| 275 | unlock_new_inode(inode); |
| 276 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { |
| 277 | /* Inode has changed type, any I/O on the old should fail */ |
| 278 | make_bad_inode(inode); |
| 279 | iput(inode); |
| 280 | goto retry; |
| 281 | } |
| 282 | |
| 283 | fi = get_fuse_inode(inode); |
| 284 | spin_lock(&fc->lock); |
| 285 | fi->nlookup++; |
| 286 | spin_unlock(&fc->lock); |
| 287 | fuse_change_attributes(inode, attr, attr_valid, attr_version); |
| 288 | |
| 289 | return inode; |
| 290 | } |
| 291 | |
| 292 | int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid, |
| 293 | loff_t offset, loff_t len) |
| 294 | { |
| 295 | struct inode *inode; |
| 296 | pgoff_t pg_start; |
| 297 | pgoff_t pg_end; |
| 298 | |
| 299 | inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid); |
| 300 | if (!inode) |
| 301 | return -ENOENT; |
| 302 | |
| 303 | fuse_invalidate_attr(inode); |
| 304 | if (offset >= 0) { |
| 305 | pg_start = offset >> PAGE_CACHE_SHIFT; |
| 306 | if (len <= 0) |
| 307 | pg_end = -1; |
| 308 | else |
| 309 | pg_end = (offset + len - 1) >> PAGE_CACHE_SHIFT; |
| 310 | invalidate_inode_pages2_range(inode->i_mapping, |
| 311 | pg_start, pg_end); |
| 312 | } |
| 313 | iput(inode); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static void fuse_umount_begin(struct super_block *sb) |
| 318 | { |
| 319 | fuse_abort_conn(get_fuse_conn_super(sb)); |
| 320 | } |
| 321 | |
| 322 | static void fuse_send_destroy(struct fuse_conn *fc) |
| 323 | { |
| 324 | struct fuse_req *req = fc->destroy_req; |
| 325 | if (req && fc->conn_init) { |
| 326 | fc->destroy_req = NULL; |
| 327 | req->in.h.opcode = FUSE_DESTROY; |
| 328 | req->force = 1; |
| 329 | fuse_request_send(fc, req); |
| 330 | fuse_put_request(fc, req); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static void fuse_bdi_destroy(struct fuse_conn *fc) |
| 335 | { |
| 336 | if (fc->bdi_initialized) |
| 337 | bdi_destroy(&fc->bdi); |
| 338 | } |
| 339 | |
| 340 | void fuse_conn_kill(struct fuse_conn *fc) |
| 341 | { |
| 342 | spin_lock(&fc->lock); |
| 343 | fc->connected = 0; |
| 344 | fc->blocked = 0; |
| 345 | spin_unlock(&fc->lock); |
| 346 | /* Flush all readers on this fs */ |
| 347 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
| 348 | wake_up_all(&fc->waitq); |
| 349 | wake_up_all(&fc->blocked_waitq); |
| 350 | wake_up_all(&fc->reserved_req_waitq); |
| 351 | mutex_lock(&fuse_mutex); |
| 352 | list_del(&fc->entry); |
| 353 | fuse_ctl_remove_conn(fc); |
| 354 | mutex_unlock(&fuse_mutex); |
| 355 | fuse_bdi_destroy(fc); |
| 356 | } |
| 357 | EXPORT_SYMBOL_GPL(fuse_conn_kill); |
| 358 | |
| 359 | static void fuse_put_super(struct super_block *sb) |
| 360 | { |
| 361 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 362 | |
| 363 | fuse_send_destroy(fc); |
| 364 | fuse_conn_kill(fc); |
| 365 | fuse_conn_put(fc); |
| 366 | } |
| 367 | |
| 368 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
| 369 | { |
| 370 | stbuf->f_type = FUSE_SUPER_MAGIC; |
| 371 | stbuf->f_bsize = attr->bsize; |
| 372 | stbuf->f_frsize = attr->frsize; |
| 373 | stbuf->f_blocks = attr->blocks; |
| 374 | stbuf->f_bfree = attr->bfree; |
| 375 | stbuf->f_bavail = attr->bavail; |
| 376 | stbuf->f_files = attr->files; |
| 377 | stbuf->f_ffree = attr->ffree; |
| 378 | stbuf->f_namelen = attr->namelen; |
| 379 | /* fsid is left zero */ |
| 380 | } |
| 381 | |
| 382 | static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 383 | { |
| 384 | struct super_block *sb = dentry->d_sb; |
| 385 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 386 | struct fuse_req *req; |
| 387 | struct fuse_statfs_out outarg; |
| 388 | int err; |
| 389 | |
| 390 | if (!fuse_allow_task(fc, current)) { |
| 391 | buf->f_type = FUSE_SUPER_MAGIC; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | req = fuse_get_req(fc); |
| 396 | if (IS_ERR(req)) |
| 397 | return PTR_ERR(req); |
| 398 | |
| 399 | memset(&outarg, 0, sizeof(outarg)); |
| 400 | req->in.numargs = 0; |
| 401 | req->in.h.opcode = FUSE_STATFS; |
| 402 | req->in.h.nodeid = get_node_id(dentry->d_inode); |
| 403 | req->out.numargs = 1; |
| 404 | req->out.args[0].size = |
| 405 | fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg); |
| 406 | req->out.args[0].value = &outarg; |
| 407 | fuse_request_send(fc, req); |
| 408 | err = req->out.h.error; |
| 409 | if (!err) |
| 410 | convert_fuse_statfs(buf, &outarg.st); |
| 411 | fuse_put_request(fc, req); |
| 412 | return err; |
| 413 | } |
| 414 | |
| 415 | enum { |
| 416 | OPT_FD, |
| 417 | OPT_ROOTMODE, |
| 418 | OPT_USER_ID, |
| 419 | OPT_GROUP_ID, |
| 420 | OPT_DEFAULT_PERMISSIONS, |
| 421 | OPT_ALLOW_OTHER, |
| 422 | OPT_MAX_READ, |
| 423 | OPT_BLKSIZE, |
| 424 | OPT_ERR |
| 425 | }; |
| 426 | |
| 427 | static const match_table_t tokens = { |
| 428 | {OPT_FD, "fd=%u"}, |
| 429 | {OPT_ROOTMODE, "rootmode=%o"}, |
| 430 | {OPT_USER_ID, "user_id=%u"}, |
| 431 | {OPT_GROUP_ID, "group_id=%u"}, |
| 432 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
| 433 | {OPT_ALLOW_OTHER, "allow_other"}, |
| 434 | {OPT_MAX_READ, "max_read=%u"}, |
| 435 | {OPT_BLKSIZE, "blksize=%u"}, |
| 436 | {OPT_ERR, NULL} |
| 437 | }; |
| 438 | |
| 439 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev) |
| 440 | { |
| 441 | char *p; |
| 442 | memset(d, 0, sizeof(struct fuse_mount_data)); |
| 443 | d->max_read = ~0; |
| 444 | d->blksize = FUSE_DEFAULT_BLKSIZE; |
| 445 | |
| 446 | while ((p = strsep(&opt, ",")) != NULL) { |
| 447 | int token; |
| 448 | int value; |
| 449 | substring_t args[MAX_OPT_ARGS]; |
| 450 | if (!*p) |
| 451 | continue; |
| 452 | |
| 453 | token = match_token(p, tokens, args); |
| 454 | switch (token) { |
| 455 | case OPT_FD: |
| 456 | if (match_int(&args[0], &value)) |
| 457 | return 0; |
| 458 | d->fd = value; |
| 459 | d->fd_present = 1; |
| 460 | break; |
| 461 | |
| 462 | case OPT_ROOTMODE: |
| 463 | if (match_octal(&args[0], &value)) |
| 464 | return 0; |
| 465 | if (!fuse_valid_type(value)) |
| 466 | return 0; |
| 467 | d->rootmode = value; |
| 468 | d->rootmode_present = 1; |
| 469 | break; |
| 470 | |
| 471 | case OPT_USER_ID: |
| 472 | if (match_int(&args[0], &value)) |
| 473 | return 0; |
| 474 | d->user_id = value; |
| 475 | d->user_id_present = 1; |
| 476 | break; |
| 477 | |
| 478 | case OPT_GROUP_ID: |
| 479 | if (match_int(&args[0], &value)) |
| 480 | return 0; |
| 481 | d->group_id = value; |
| 482 | d->group_id_present = 1; |
| 483 | break; |
| 484 | |
| 485 | case OPT_DEFAULT_PERMISSIONS: |
| 486 | d->flags |= FUSE_DEFAULT_PERMISSIONS; |
| 487 | break; |
| 488 | |
| 489 | case OPT_ALLOW_OTHER: |
| 490 | d->flags |= FUSE_ALLOW_OTHER; |
| 491 | break; |
| 492 | |
| 493 | case OPT_MAX_READ: |
| 494 | if (match_int(&args[0], &value)) |
| 495 | return 0; |
| 496 | d->max_read = value; |
| 497 | break; |
| 498 | |
| 499 | case OPT_BLKSIZE: |
| 500 | if (!is_bdev || match_int(&args[0], &value)) |
| 501 | return 0; |
| 502 | d->blksize = value; |
| 503 | break; |
| 504 | |
| 505 | default: |
| 506 | return 0; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (!d->fd_present || !d->rootmode_present || |
| 511 | !d->user_id_present || !d->group_id_present) |
| 512 | return 0; |
| 513 | |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | static int fuse_show_options(struct seq_file *m, struct dentry *root) |
| 518 | { |
| 519 | struct super_block *sb = root->d_sb; |
| 520 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 521 | |
| 522 | seq_printf(m, ",user_id=%u", fc->user_id); |
| 523 | seq_printf(m, ",group_id=%u", fc->group_id); |
| 524 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) |
| 525 | seq_puts(m, ",default_permissions"); |
| 526 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 527 | seq_puts(m, ",allow_other"); |
| 528 | if (fc->max_read != ~0) |
| 529 | seq_printf(m, ",max_read=%u", fc->max_read); |
| 530 | if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE) |
| 531 | seq_printf(m, ",blksize=%lu", sb->s_blocksize); |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | void fuse_conn_init(struct fuse_conn *fc) |
| 536 | { |
| 537 | memset(fc, 0, sizeof(*fc)); |
| 538 | spin_lock_init(&fc->lock); |
| 539 | mutex_init(&fc->inst_mutex); |
| 540 | init_rwsem(&fc->killsb); |
| 541 | atomic_set(&fc->count, 1); |
| 542 | init_waitqueue_head(&fc->waitq); |
| 543 | init_waitqueue_head(&fc->blocked_waitq); |
| 544 | init_waitqueue_head(&fc->reserved_req_waitq); |
| 545 | INIT_LIST_HEAD(&fc->pending); |
| 546 | INIT_LIST_HEAD(&fc->processing); |
| 547 | INIT_LIST_HEAD(&fc->io); |
| 548 | INIT_LIST_HEAD(&fc->interrupts); |
| 549 | INIT_LIST_HEAD(&fc->bg_queue); |
| 550 | INIT_LIST_HEAD(&fc->entry); |
| 551 | fc->forget_list_tail = &fc->forget_list_head; |
| 552 | atomic_set(&fc->num_waiting, 0); |
| 553 | fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND; |
| 554 | fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD; |
| 555 | fc->khctr = 0; |
| 556 | fc->polled_files = RB_ROOT; |
| 557 | fc->reqctr = 0; |
| 558 | fc->blocked = 1; |
| 559 | fc->attr_version = 1; |
| 560 | get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key)); |
| 561 | } |
| 562 | EXPORT_SYMBOL_GPL(fuse_conn_init); |
| 563 | |
| 564 | void fuse_conn_put(struct fuse_conn *fc) |
| 565 | { |
| 566 | if (atomic_dec_and_test(&fc->count)) { |
| 567 | if (fc->destroy_req) |
| 568 | fuse_request_free(fc->destroy_req); |
| 569 | mutex_destroy(&fc->inst_mutex); |
| 570 | fc->release(fc); |
| 571 | } |
| 572 | } |
| 573 | EXPORT_SYMBOL_GPL(fuse_conn_put); |
| 574 | |
| 575 | struct fuse_conn *fuse_conn_get(struct fuse_conn *fc) |
| 576 | { |
| 577 | atomic_inc(&fc->count); |
| 578 | return fc; |
| 579 | } |
| 580 | EXPORT_SYMBOL_GPL(fuse_conn_get); |
| 581 | |
| 582 | static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode) |
| 583 | { |
| 584 | struct fuse_attr attr; |
| 585 | memset(&attr, 0, sizeof(attr)); |
| 586 | |
| 587 | attr.mode = mode; |
| 588 | attr.ino = FUSE_ROOT_ID; |
| 589 | attr.nlink = 1; |
| 590 | return fuse_iget(sb, 1, 0, &attr, 0, 0); |
| 591 | } |
| 592 | |
| 593 | struct fuse_inode_handle { |
| 594 | u64 nodeid; |
| 595 | u32 generation; |
| 596 | }; |
| 597 | |
| 598 | static struct dentry *fuse_get_dentry(struct super_block *sb, |
| 599 | struct fuse_inode_handle *handle) |
| 600 | { |
| 601 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 602 | struct inode *inode; |
| 603 | struct dentry *entry; |
| 604 | int err = -ESTALE; |
| 605 | |
| 606 | if (handle->nodeid == 0) |
| 607 | goto out_err; |
| 608 | |
| 609 | inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid); |
| 610 | if (!inode) { |
| 611 | struct fuse_entry_out outarg; |
| 612 | struct qstr name; |
| 613 | |
| 614 | if (!fc->export_support) |
| 615 | goto out_err; |
| 616 | |
| 617 | name.len = 1; |
| 618 | name.name = "."; |
| 619 | err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg, |
| 620 | &inode); |
| 621 | if (err && err != -ENOENT) |
| 622 | goto out_err; |
| 623 | if (err || !inode) { |
| 624 | err = -ESTALE; |
| 625 | goto out_err; |
| 626 | } |
| 627 | err = -EIO; |
| 628 | if (get_node_id(inode) != handle->nodeid) |
| 629 | goto out_iput; |
| 630 | } |
| 631 | err = -ESTALE; |
| 632 | if (inode->i_generation != handle->generation) |
| 633 | goto out_iput; |
| 634 | |
| 635 | entry = d_obtain_alias(inode); |
| 636 | if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) |
| 637 | fuse_invalidate_entry_cache(entry); |
| 638 | |
| 639 | return entry; |
| 640 | |
| 641 | out_iput: |
| 642 | iput(inode); |
| 643 | out_err: |
| 644 | return ERR_PTR(err); |
| 645 | } |
| 646 | |
| 647 | static int fuse_encode_fh(struct dentry *dentry, u32 *fh, int *max_len, |
| 648 | int connectable) |
| 649 | { |
| 650 | struct inode *inode = dentry->d_inode; |
| 651 | bool encode_parent = connectable && !S_ISDIR(inode->i_mode); |
| 652 | int len = encode_parent ? 6 : 3; |
| 653 | u64 nodeid; |
| 654 | u32 generation; |
| 655 | |
| 656 | if (*max_len < len) { |
| 657 | *max_len = len; |
| 658 | return 255; |
| 659 | } |
| 660 | |
| 661 | nodeid = get_fuse_inode(inode)->nodeid; |
| 662 | generation = inode->i_generation; |
| 663 | |
| 664 | fh[0] = (u32)(nodeid >> 32); |
| 665 | fh[1] = (u32)(nodeid & 0xffffffff); |
| 666 | fh[2] = generation; |
| 667 | |
| 668 | if (encode_parent) { |
| 669 | struct inode *parent; |
| 670 | |
| 671 | spin_lock(&dentry->d_lock); |
| 672 | parent = dentry->d_parent->d_inode; |
| 673 | nodeid = get_fuse_inode(parent)->nodeid; |
| 674 | generation = parent->i_generation; |
| 675 | spin_unlock(&dentry->d_lock); |
| 676 | |
| 677 | fh[3] = (u32)(nodeid >> 32); |
| 678 | fh[4] = (u32)(nodeid & 0xffffffff); |
| 679 | fh[5] = generation; |
| 680 | } |
| 681 | |
| 682 | *max_len = len; |
| 683 | return encode_parent ? 0x82 : 0x81; |
| 684 | } |
| 685 | |
| 686 | static struct dentry *fuse_fh_to_dentry(struct super_block *sb, |
| 687 | struct fid *fid, int fh_len, int fh_type) |
| 688 | { |
| 689 | struct fuse_inode_handle handle; |
| 690 | |
| 691 | if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3) |
| 692 | return NULL; |
| 693 | |
| 694 | handle.nodeid = (u64) fid->raw[0] << 32; |
| 695 | handle.nodeid |= (u64) fid->raw[1]; |
| 696 | handle.generation = fid->raw[2]; |
| 697 | return fuse_get_dentry(sb, &handle); |
| 698 | } |
| 699 | |
| 700 | static struct dentry *fuse_fh_to_parent(struct super_block *sb, |
| 701 | struct fid *fid, int fh_len, int fh_type) |
| 702 | { |
| 703 | struct fuse_inode_handle parent; |
| 704 | |
| 705 | if (fh_type != 0x82 || fh_len < 6) |
| 706 | return NULL; |
| 707 | |
| 708 | parent.nodeid = (u64) fid->raw[3] << 32; |
| 709 | parent.nodeid |= (u64) fid->raw[4]; |
| 710 | parent.generation = fid->raw[5]; |
| 711 | return fuse_get_dentry(sb, &parent); |
| 712 | } |
| 713 | |
| 714 | static struct dentry *fuse_get_parent(struct dentry *child) |
| 715 | { |
| 716 | struct inode *child_inode = child->d_inode; |
| 717 | struct fuse_conn *fc = get_fuse_conn(child_inode); |
| 718 | struct inode *inode; |
| 719 | struct dentry *parent; |
| 720 | struct fuse_entry_out outarg; |
| 721 | struct qstr name; |
| 722 | int err; |
| 723 | |
| 724 | if (!fc->export_support) |
| 725 | return ERR_PTR(-ESTALE); |
| 726 | |
| 727 | name.len = 2; |
| 728 | name.name = ".."; |
| 729 | err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode), |
| 730 | &name, &outarg, &inode); |
| 731 | if (err) { |
| 732 | if (err == -ENOENT) |
| 733 | return ERR_PTR(-ESTALE); |
| 734 | return ERR_PTR(err); |
| 735 | } |
| 736 | |
| 737 | parent = d_obtain_alias(inode); |
| 738 | if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) |
| 739 | fuse_invalidate_entry_cache(parent); |
| 740 | |
| 741 | return parent; |
| 742 | } |
| 743 | |
| 744 | static const struct export_operations fuse_export_operations = { |
| 745 | .fh_to_dentry = fuse_fh_to_dentry, |
| 746 | .fh_to_parent = fuse_fh_to_parent, |
| 747 | .encode_fh = fuse_encode_fh, |
| 748 | .get_parent = fuse_get_parent, |
| 749 | }; |
| 750 | |
| 751 | static const struct super_operations fuse_super_operations = { |
| 752 | .alloc_inode = fuse_alloc_inode, |
| 753 | .destroy_inode = fuse_destroy_inode, |
| 754 | .evict_inode = fuse_evict_inode, |
| 755 | .drop_inode = generic_delete_inode, |
| 756 | .remount_fs = fuse_remount_fs, |
| 757 | .put_super = fuse_put_super, |
| 758 | .umount_begin = fuse_umount_begin, |
| 759 | .statfs = fuse_statfs, |
| 760 | .show_options = fuse_show_options, |
| 761 | }; |
| 762 | |
| 763 | static void sanitize_global_limit(unsigned *limit) |
| 764 | { |
| 765 | if (*limit == 0) |
| 766 | *limit = ((num_physpages << PAGE_SHIFT) >> 13) / |
| 767 | sizeof(struct fuse_req); |
| 768 | |
| 769 | if (*limit >= 1 << 16) |
| 770 | *limit = (1 << 16) - 1; |
| 771 | } |
| 772 | |
| 773 | static int set_global_limit(const char *val, struct kernel_param *kp) |
| 774 | { |
| 775 | int rv; |
| 776 | |
| 777 | rv = param_set_uint(val, kp); |
| 778 | if (rv) |
| 779 | return rv; |
| 780 | |
| 781 | sanitize_global_limit((unsigned *)kp->arg); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg) |
| 787 | { |
| 788 | int cap_sys_admin = capable(CAP_SYS_ADMIN); |
| 789 | |
| 790 | if (arg->minor < 13) |
| 791 | return; |
| 792 | |
| 793 | sanitize_global_limit(&max_user_bgreq); |
| 794 | sanitize_global_limit(&max_user_congthresh); |
| 795 | |
| 796 | if (arg->max_background) { |
| 797 | fc->max_background = arg->max_background; |
| 798 | |
| 799 | if (!cap_sys_admin && fc->max_background > max_user_bgreq) |
| 800 | fc->max_background = max_user_bgreq; |
| 801 | } |
| 802 | if (arg->congestion_threshold) { |
| 803 | fc->congestion_threshold = arg->congestion_threshold; |
| 804 | |
| 805 | if (!cap_sys_admin && |
| 806 | fc->congestion_threshold > max_user_congthresh) |
| 807 | fc->congestion_threshold = max_user_congthresh; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req) |
| 812 | { |
| 813 | struct fuse_init_out *arg = &req->misc.init_out; |
| 814 | |
| 815 | if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION) |
| 816 | fc->conn_error = 1; |
| 817 | else { |
| 818 | unsigned long ra_pages; |
| 819 | |
| 820 | process_init_limits(fc, arg); |
| 821 | |
| 822 | if (arg->minor >= 6) { |
| 823 | ra_pages = arg->max_readahead / PAGE_CACHE_SIZE; |
| 824 | if (arg->flags & FUSE_ASYNC_READ) |
| 825 | fc->async_read = 1; |
| 826 | if (!(arg->flags & FUSE_POSIX_LOCKS)) |
| 827 | fc->no_lock = 1; |
| 828 | if (arg->minor >= 17) { |
| 829 | if (!(arg->flags & FUSE_FLOCK_LOCKS)) |
| 830 | fc->no_flock = 1; |
| 831 | } else { |
| 832 | if (!(arg->flags & FUSE_POSIX_LOCKS)) |
| 833 | fc->no_flock = 1; |
| 834 | } |
| 835 | if (arg->flags & FUSE_ATOMIC_O_TRUNC) |
| 836 | fc->atomic_o_trunc = 1; |
| 837 | if (arg->minor >= 9) { |
| 838 | /* LOOKUP has dependency on proto version */ |
| 839 | if (arg->flags & FUSE_EXPORT_SUPPORT) |
| 840 | fc->export_support = 1; |
| 841 | } |
| 842 | if (arg->flags & FUSE_BIG_WRITES) |
| 843 | fc->big_writes = 1; |
| 844 | if (arg->flags & FUSE_DONT_MASK) |
| 845 | fc->dont_mask = 1; |
| 846 | } else { |
| 847 | ra_pages = fc->max_read / PAGE_CACHE_SIZE; |
| 848 | fc->no_lock = 1; |
| 849 | fc->no_flock = 1; |
| 850 | } |
| 851 | |
| 852 | fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages); |
| 853 | fc->minor = arg->minor; |
| 854 | fc->max_write = arg->minor < 5 ? 4096 : arg->max_write; |
| 855 | fc->max_write = max_t(unsigned, 4096, fc->max_write); |
| 856 | fc->conn_init = 1; |
| 857 | } |
| 858 | fc->blocked = 0; |
| 859 | wake_up_all(&fc->blocked_waitq); |
| 860 | } |
| 861 | |
| 862 | static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req) |
| 863 | { |
| 864 | struct fuse_init_in *arg = &req->misc.init_in; |
| 865 | |
| 866 | arg->major = FUSE_KERNEL_VERSION; |
| 867 | arg->minor = FUSE_KERNEL_MINOR_VERSION; |
| 868 | arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE; |
| 869 | arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC | |
| 870 | FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK | |
| 871 | FUSE_FLOCK_LOCKS; |
| 872 | req->in.h.opcode = FUSE_INIT; |
| 873 | req->in.numargs = 1; |
| 874 | req->in.args[0].size = sizeof(*arg); |
| 875 | req->in.args[0].value = arg; |
| 876 | req->out.numargs = 1; |
| 877 | /* Variable length argument used for backward compatibility |
| 878 | with interface version < 7.5. Rest of init_out is zeroed |
| 879 | by do_get_request(), so a short reply is not a problem */ |
| 880 | req->out.argvar = 1; |
| 881 | req->out.args[0].size = sizeof(struct fuse_init_out); |
| 882 | req->out.args[0].value = &req->misc.init_out; |
| 883 | req->end = process_init_reply; |
| 884 | fuse_request_send_background(fc, req); |
| 885 | } |
| 886 | |
| 887 | static void fuse_free_conn(struct fuse_conn *fc) |
| 888 | { |
| 889 | kfree(fc); |
| 890 | } |
| 891 | |
| 892 | static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb) |
| 893 | { |
| 894 | int err; |
| 895 | |
| 896 | fc->bdi.name = "fuse"; |
| 897 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
| 898 | /* fuse does it's own writeback accounting */ |
| 899 | fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB; |
| 900 | |
| 901 | err = bdi_init(&fc->bdi); |
| 902 | if (err) |
| 903 | return err; |
| 904 | |
| 905 | fc->bdi_initialized = 1; |
| 906 | |
| 907 | if (sb->s_bdev) { |
| 908 | err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk", |
| 909 | MAJOR(fc->dev), MINOR(fc->dev)); |
| 910 | } else { |
| 911 | err = bdi_register_dev(&fc->bdi, fc->dev); |
| 912 | } |
| 913 | |
| 914 | if (err) |
| 915 | return err; |
| 916 | |
| 917 | /* |
| 918 | * For a single fuse filesystem use max 1% of dirty + |
| 919 | * writeback threshold. |
| 920 | * |
| 921 | * This gives about 1M of write buffer for memory maps on a |
| 922 | * machine with 1G and 10% dirty_ratio, which should be more |
| 923 | * than enough. |
| 924 | * |
| 925 | * Privileged users can raise it by writing to |
| 926 | * |
| 927 | * /sys/class/bdi/<bdi>/max_ratio |
| 928 | */ |
| 929 | bdi_set_max_ratio(&fc->bdi, 1); |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static int fuse_fill_super(struct super_block *sb, void *data, int silent) |
| 935 | { |
| 936 | struct fuse_conn *fc; |
| 937 | struct inode *root; |
| 938 | struct fuse_mount_data d; |
| 939 | struct file *file; |
| 940 | struct dentry *root_dentry; |
| 941 | struct fuse_req *init_req; |
| 942 | int err; |
| 943 | int is_bdev = sb->s_bdev != NULL; |
| 944 | |
| 945 | err = -EINVAL; |
| 946 | if (sb->s_flags & MS_MANDLOCK) |
| 947 | goto err; |
| 948 | |
| 949 | sb->s_flags &= ~MS_NOSEC; |
| 950 | |
| 951 | if (!parse_fuse_opt((char *) data, &d, is_bdev)) |
| 952 | goto err; |
| 953 | |
| 954 | if (is_bdev) { |
| 955 | #ifdef CONFIG_BLOCK |
| 956 | err = -EINVAL; |
| 957 | if (!sb_set_blocksize(sb, d.blksize)) |
| 958 | goto err; |
| 959 | #endif |
| 960 | } else { |
| 961 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 962 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 963 | } |
| 964 | sb->s_magic = FUSE_SUPER_MAGIC; |
| 965 | sb->s_op = &fuse_super_operations; |
| 966 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 967 | sb->s_time_gran = 1; |
| 968 | sb->s_export_op = &fuse_export_operations; |
| 969 | |
| 970 | file = fget(d.fd); |
| 971 | err = -EINVAL; |
| 972 | if (!file) |
| 973 | goto err; |
| 974 | |
| 975 | if (file->f_op != &fuse_dev_operations) |
| 976 | goto err_fput; |
| 977 | |
| 978 | fc = kmalloc(sizeof(*fc), GFP_KERNEL); |
| 979 | err = -ENOMEM; |
| 980 | if (!fc) |
| 981 | goto err_fput; |
| 982 | |
| 983 | fuse_conn_init(fc); |
| 984 | fc->release = fuse_free_conn; |
| 985 | |
| 986 | fc->dev = sb->s_dev; |
| 987 | fc->sb = sb; |
| 988 | err = fuse_bdi_init(fc, sb); |
| 989 | if (err) |
| 990 | goto err_put_conn; |
| 991 | |
| 992 | sb->s_bdi = &fc->bdi; |
| 993 | |
| 994 | /* Handle umasking inside the fuse code */ |
| 995 | if (sb->s_flags & MS_POSIXACL) |
| 996 | fc->dont_mask = 1; |
| 997 | sb->s_flags |= MS_POSIXACL; |
| 998 | |
| 999 | fc->flags = d.flags; |
| 1000 | fc->user_id = d.user_id; |
| 1001 | fc->group_id = d.group_id; |
| 1002 | fc->max_read = max_t(unsigned, 4096, d.max_read); |
| 1003 | |
| 1004 | /* Used by get_root_inode() */ |
| 1005 | sb->s_fs_info = fc; |
| 1006 | |
| 1007 | err = -ENOMEM; |
| 1008 | root = fuse_get_root_inode(sb, d.rootmode); |
| 1009 | root_dentry = d_make_root(root); |
| 1010 | if (!root_dentry) |
| 1011 | goto err_put_conn; |
| 1012 | /* only now - we want root dentry with NULL ->d_op */ |
| 1013 | sb->s_d_op = &fuse_dentry_operations; |
| 1014 | |
| 1015 | init_req = fuse_request_alloc(); |
| 1016 | if (!init_req) |
| 1017 | goto err_put_root; |
| 1018 | |
| 1019 | if (is_bdev) { |
| 1020 | fc->destroy_req = fuse_request_alloc(); |
| 1021 | if (!fc->destroy_req) |
| 1022 | goto err_free_init_req; |
| 1023 | } |
| 1024 | |
| 1025 | mutex_lock(&fuse_mutex); |
| 1026 | err = -EINVAL; |
| 1027 | if (file->private_data) |
| 1028 | goto err_unlock; |
| 1029 | |
| 1030 | err = fuse_ctl_add_conn(fc); |
| 1031 | if (err) |
| 1032 | goto err_unlock; |
| 1033 | |
| 1034 | list_add_tail(&fc->entry, &fuse_conn_list); |
| 1035 | sb->s_root = root_dentry; |
| 1036 | fc->connected = 1; |
| 1037 | file->private_data = fuse_conn_get(fc); |
| 1038 | mutex_unlock(&fuse_mutex); |
| 1039 | /* |
| 1040 | * atomic_dec_and_test() in fput() provides the necessary |
| 1041 | * memory barrier for file->private_data to be visible on all |
| 1042 | * CPUs after this |
| 1043 | */ |
| 1044 | fput(file); |
| 1045 | |
| 1046 | fuse_send_init(fc, init_req); |
| 1047 | |
| 1048 | return 0; |
| 1049 | |
| 1050 | err_unlock: |
| 1051 | mutex_unlock(&fuse_mutex); |
| 1052 | err_free_init_req: |
| 1053 | fuse_request_free(init_req); |
| 1054 | err_put_root: |
| 1055 | dput(root_dentry); |
| 1056 | err_put_conn: |
| 1057 | fuse_bdi_destroy(fc); |
| 1058 | fuse_conn_put(fc); |
| 1059 | err_fput: |
| 1060 | fput(file); |
| 1061 | err: |
| 1062 | return err; |
| 1063 | } |
| 1064 | |
| 1065 | static struct dentry *fuse_mount(struct file_system_type *fs_type, |
| 1066 | int flags, const char *dev_name, |
| 1067 | void *raw_data) |
| 1068 | { |
| 1069 | return mount_nodev(fs_type, flags, raw_data, fuse_fill_super); |
| 1070 | } |
| 1071 | |
| 1072 | static void fuse_kill_sb_anon(struct super_block *sb) |
| 1073 | { |
| 1074 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 1075 | |
| 1076 | if (fc) { |
| 1077 | down_write(&fc->killsb); |
| 1078 | fc->sb = NULL; |
| 1079 | up_write(&fc->killsb); |
| 1080 | } |
| 1081 | |
| 1082 | kill_anon_super(sb); |
| 1083 | } |
| 1084 | |
| 1085 | static struct file_system_type fuse_fs_type = { |
| 1086 | .owner = THIS_MODULE, |
| 1087 | .name = "fuse", |
| 1088 | .fs_flags = FS_HAS_SUBTYPE, |
| 1089 | .mount = fuse_mount, |
| 1090 | .kill_sb = fuse_kill_sb_anon, |
| 1091 | }; |
| 1092 | |
| 1093 | #ifdef CONFIG_BLOCK |
| 1094 | static struct dentry *fuse_mount_blk(struct file_system_type *fs_type, |
| 1095 | int flags, const char *dev_name, |
| 1096 | void *raw_data) |
| 1097 | { |
| 1098 | return mount_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super); |
| 1099 | } |
| 1100 | |
| 1101 | static void fuse_kill_sb_blk(struct super_block *sb) |
| 1102 | { |
| 1103 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 1104 | |
| 1105 | if (fc) { |
| 1106 | down_write(&fc->killsb); |
| 1107 | fc->sb = NULL; |
| 1108 | up_write(&fc->killsb); |
| 1109 | } |
| 1110 | |
| 1111 | kill_block_super(sb); |
| 1112 | } |
| 1113 | |
| 1114 | static struct file_system_type fuseblk_fs_type = { |
| 1115 | .owner = THIS_MODULE, |
| 1116 | .name = "fuseblk", |
| 1117 | .mount = fuse_mount_blk, |
| 1118 | .kill_sb = fuse_kill_sb_blk, |
| 1119 | .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE, |
| 1120 | }; |
| 1121 | |
| 1122 | static inline int register_fuseblk(void) |
| 1123 | { |
| 1124 | return register_filesystem(&fuseblk_fs_type); |
| 1125 | } |
| 1126 | |
| 1127 | static inline void unregister_fuseblk(void) |
| 1128 | { |
| 1129 | unregister_filesystem(&fuseblk_fs_type); |
| 1130 | } |
| 1131 | #else |
| 1132 | static inline int register_fuseblk(void) |
| 1133 | { |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | static inline void unregister_fuseblk(void) |
| 1138 | { |
| 1139 | } |
| 1140 | #endif |
| 1141 | |
| 1142 | static void fuse_inode_init_once(void *foo) |
| 1143 | { |
| 1144 | struct inode *inode = foo; |
| 1145 | |
| 1146 | inode_init_once(inode); |
| 1147 | } |
| 1148 | |
| 1149 | static int __init fuse_fs_init(void) |
| 1150 | { |
| 1151 | int err; |
| 1152 | |
| 1153 | fuse_inode_cachep = kmem_cache_create("fuse_inode", |
| 1154 | sizeof(struct fuse_inode), |
| 1155 | 0, SLAB_HWCACHE_ALIGN, |
| 1156 | fuse_inode_init_once); |
| 1157 | err = -ENOMEM; |
| 1158 | if (!fuse_inode_cachep) |
| 1159 | goto out; |
| 1160 | |
| 1161 | err = register_fuseblk(); |
| 1162 | if (err) |
| 1163 | goto out2; |
| 1164 | |
| 1165 | err = register_filesystem(&fuse_fs_type); |
| 1166 | if (err) |
| 1167 | goto out3; |
| 1168 | |
| 1169 | return 0; |
| 1170 | |
| 1171 | out3: |
| 1172 | unregister_fuseblk(); |
| 1173 | out2: |
| 1174 | kmem_cache_destroy(fuse_inode_cachep); |
| 1175 | out: |
| 1176 | return err; |
| 1177 | } |
| 1178 | |
| 1179 | static void fuse_fs_cleanup(void) |
| 1180 | { |
| 1181 | unregister_filesystem(&fuse_fs_type); |
| 1182 | unregister_fuseblk(); |
| 1183 | kmem_cache_destroy(fuse_inode_cachep); |
| 1184 | } |
| 1185 | |
| 1186 | static struct kobject *fuse_kobj; |
| 1187 | static struct kobject *connections_kobj; |
| 1188 | |
| 1189 | static int fuse_sysfs_init(void) |
| 1190 | { |
| 1191 | int err; |
| 1192 | |
| 1193 | fuse_kobj = kobject_create_and_add("fuse", fs_kobj); |
| 1194 | if (!fuse_kobj) { |
| 1195 | err = -ENOMEM; |
| 1196 | goto out_err; |
| 1197 | } |
| 1198 | |
| 1199 | connections_kobj = kobject_create_and_add("connections", fuse_kobj); |
| 1200 | if (!connections_kobj) { |
| 1201 | err = -ENOMEM; |
| 1202 | goto out_fuse_unregister; |
| 1203 | } |
| 1204 | |
| 1205 | return 0; |
| 1206 | |
| 1207 | out_fuse_unregister: |
| 1208 | kobject_put(fuse_kobj); |
| 1209 | out_err: |
| 1210 | return err; |
| 1211 | } |
| 1212 | |
| 1213 | static void fuse_sysfs_cleanup(void) |
| 1214 | { |
| 1215 | kobject_put(connections_kobj); |
| 1216 | kobject_put(fuse_kobj); |
| 1217 | } |
| 1218 | |
| 1219 | static int __init fuse_init(void) |
| 1220 | { |
| 1221 | int res; |
| 1222 | |
| 1223 | printk(KERN_INFO "fuse init (API version %i.%i)\n", |
| 1224 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1225 | |
| 1226 | INIT_LIST_HEAD(&fuse_conn_list); |
| 1227 | res = fuse_fs_init(); |
| 1228 | if (res) |
| 1229 | goto err; |
| 1230 | |
| 1231 | res = fuse_dev_init(); |
| 1232 | if (res) |
| 1233 | goto err_fs_cleanup; |
| 1234 | |
| 1235 | res = fuse_sysfs_init(); |
| 1236 | if (res) |
| 1237 | goto err_dev_cleanup; |
| 1238 | |
| 1239 | res = fuse_ctl_init(); |
| 1240 | if (res) |
| 1241 | goto err_sysfs_cleanup; |
| 1242 | |
| 1243 | sanitize_global_limit(&max_user_bgreq); |
| 1244 | sanitize_global_limit(&max_user_congthresh); |
| 1245 | |
| 1246 | return 0; |
| 1247 | |
| 1248 | err_sysfs_cleanup: |
| 1249 | fuse_sysfs_cleanup(); |
| 1250 | err_dev_cleanup: |
| 1251 | fuse_dev_cleanup(); |
| 1252 | err_fs_cleanup: |
| 1253 | fuse_fs_cleanup(); |
| 1254 | err: |
| 1255 | return res; |
| 1256 | } |
| 1257 | |
| 1258 | static void __exit fuse_exit(void) |
| 1259 | { |
| 1260 | printk(KERN_DEBUG "fuse exit\n"); |
| 1261 | |
| 1262 | fuse_ctl_cleanup(); |
| 1263 | fuse_sysfs_cleanup(); |
| 1264 | fuse_fs_cleanup(); |
| 1265 | fuse_dev_cleanup(); |
| 1266 | } |
| 1267 | |
| 1268 | module_init(fuse_init); |
| 1269 | module_exit(fuse_exit); |