| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * CUSE: Character device in Userspace |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2008-2009 SUSE Linux Products GmbH |
|---|
| 5 | 6 | * Copyright (C) 2008-2009 Tejun Heo <tj@kernel.org> |
|---|
| 6 | | - * |
|---|
| 7 | | - * This file is released under the GPLv2. |
|---|
| 8 | 7 | * |
|---|
| 9 | 8 | * CUSE enables character devices to be implemented from userland much |
|---|
| 10 | 9 | * like FUSE allows filesystems. On initialization /dev/cuse is |
|---|
| .. | .. |
|---|
| 33 | 32 | * closed. |
|---|
| 34 | 33 | */ |
|---|
| 35 | 34 | |
|---|
| 35 | +#define pr_fmt(fmt) "CUSE: " fmt |
|---|
| 36 | + |
|---|
| 36 | 37 | #include <linux/fuse.h> |
|---|
| 37 | 38 | #include <linux/cdev.h> |
|---|
| 38 | 39 | #include <linux/device.h> |
|---|
| .. | .. |
|---|
| 56 | 57 | |
|---|
| 57 | 58 | struct cuse_conn { |
|---|
| 58 | 59 | struct list_head list; /* linked on cuse_conntbl */ |
|---|
| 60 | + struct fuse_mount fm; /* Dummy mount referencing fc */ |
|---|
| 59 | 61 | struct fuse_conn fc; /* fuse connection */ |
|---|
| 60 | 62 | struct cdev *cdev; /* associated character device */ |
|---|
| 61 | 63 | struct device *dev; /* device representing @cdev */ |
|---|
| .. | .. |
|---|
| 133 | 135 | * Generic permission check is already done against the chrdev |
|---|
| 134 | 136 | * file, proceed to open. |
|---|
| 135 | 137 | */ |
|---|
| 136 | | - rc = fuse_do_open(&cc->fc, 0, file, 0); |
|---|
| 138 | + rc = fuse_do_open(&cc->fm, 0, file, 0); |
|---|
| 137 | 139 | if (rc) |
|---|
| 138 | 140 | fuse_conn_put(&cc->fc); |
|---|
| 139 | 141 | return rc; |
|---|
| .. | .. |
|---|
| 142 | 144 | static int cuse_release(struct inode *inode, struct file *file) |
|---|
| 143 | 145 | { |
|---|
| 144 | 146 | struct fuse_file *ff = file->private_data; |
|---|
| 145 | | - struct fuse_conn *fc = ff->fc; |
|---|
| 147 | + struct fuse_mount *fm = ff->fm; |
|---|
| 146 | 148 | |
|---|
| 147 | | - fuse_sync_release(ff, file->f_flags); |
|---|
| 148 | | - fuse_conn_put(fc); |
|---|
| 149 | + fuse_sync_release(NULL, ff, file->f_flags); |
|---|
| 150 | + fuse_conn_put(fm->fc); |
|---|
| 149 | 151 | |
|---|
| 150 | 152 | return 0; |
|---|
| 151 | 153 | } |
|---|
| .. | .. |
|---|
| 154 | 156 | unsigned long arg) |
|---|
| 155 | 157 | { |
|---|
| 156 | 158 | struct fuse_file *ff = file->private_data; |
|---|
| 157 | | - struct cuse_conn *cc = fc_to_cc(ff->fc); |
|---|
| 159 | + struct cuse_conn *cc = fc_to_cc(ff->fm->fc); |
|---|
| 158 | 160 | unsigned int flags = 0; |
|---|
| 159 | 161 | |
|---|
| 160 | 162 | if (cc->unrestricted_ioctl) |
|---|
| .. | .. |
|---|
| 167 | 169 | unsigned long arg) |
|---|
| 168 | 170 | { |
|---|
| 169 | 171 | struct fuse_file *ff = file->private_data; |
|---|
| 170 | | - struct cuse_conn *cc = fc_to_cc(ff->fc); |
|---|
| 172 | + struct cuse_conn *cc = fc_to_cc(ff->fm->fc); |
|---|
| 171 | 173 | unsigned int flags = FUSE_IOCTL_COMPAT; |
|---|
| 172 | 174 | |
|---|
| 173 | 175 | if (cc->unrestricted_ioctl) |
|---|
| .. | .. |
|---|
| 224 | 226 | return 0; |
|---|
| 225 | 227 | |
|---|
| 226 | 228 | if (end[-1] != '\0') { |
|---|
| 227 | | - printk(KERN_ERR "CUSE: info not properly terminated\n"); |
|---|
| 229 | + pr_err("info not properly terminated\n"); |
|---|
| 228 | 230 | return -EINVAL; |
|---|
| 229 | 231 | } |
|---|
| 230 | 232 | |
|---|
| .. | .. |
|---|
| 241 | 243 | key = strstrip(key); |
|---|
| 242 | 244 | |
|---|
| 243 | 245 | if (!strlen(key)) { |
|---|
| 244 | | - printk(KERN_ERR "CUSE: zero length info key specified\n"); |
|---|
| 246 | + pr_err("zero length info key specified\n"); |
|---|
| 245 | 247 | return -EINVAL; |
|---|
| 246 | 248 | } |
|---|
| 247 | 249 | |
|---|
| .. | .. |
|---|
| 269 | 271 | static int cuse_parse_devinfo(char *p, size_t len, struct cuse_devinfo *devinfo) |
|---|
| 270 | 272 | { |
|---|
| 271 | 273 | char *end = p + len; |
|---|
| 272 | | - char *uninitialized_var(key), *uninitialized_var(val); |
|---|
| 274 | + char *key, *val; |
|---|
| 273 | 275 | int rc; |
|---|
| 274 | 276 | |
|---|
| 275 | 277 | while (true) { |
|---|
| .. | .. |
|---|
| 281 | 283 | if (strcmp(key, "DEVNAME") == 0) |
|---|
| 282 | 284 | devinfo->name = val; |
|---|
| 283 | 285 | else |
|---|
| 284 | | - printk(KERN_WARNING "CUSE: unknown device info \"%s\"\n", |
|---|
| 285 | | - key); |
|---|
| 286 | + pr_warn("unknown device info \"%s\"\n", key); |
|---|
| 286 | 287 | } |
|---|
| 287 | 288 | |
|---|
| 288 | 289 | if (!devinfo->name || !strlen(devinfo->name)) { |
|---|
| 289 | | - printk(KERN_ERR "CUSE: DEVNAME unspecified\n"); |
|---|
| 290 | + pr_err("DEVNAME unspecified\n"); |
|---|
| 290 | 291 | return -EINVAL; |
|---|
| 291 | 292 | } |
|---|
| 292 | 293 | |
|---|
| .. | .. |
|---|
| 298 | 299 | kfree(dev); |
|---|
| 299 | 300 | } |
|---|
| 300 | 301 | |
|---|
| 302 | +struct cuse_init_args { |
|---|
| 303 | + struct fuse_args_pages ap; |
|---|
| 304 | + struct cuse_init_in in; |
|---|
| 305 | + struct cuse_init_out out; |
|---|
| 306 | + struct page *page; |
|---|
| 307 | + struct fuse_page_desc desc; |
|---|
| 308 | +}; |
|---|
| 309 | + |
|---|
| 301 | 310 | /** |
|---|
| 302 | 311 | * cuse_process_init_reply - finish initializing CUSE channel |
|---|
| 303 | 312 | * |
|---|
| .. | .. |
|---|
| 305 | 314 | * required data structures for it. Please read the comment at the |
|---|
| 306 | 315 | * top of this file for high level overview. |
|---|
| 307 | 316 | */ |
|---|
| 308 | | -static void cuse_process_init_reply(struct fuse_conn *fc, struct fuse_req *req) |
|---|
| 317 | +static void cuse_process_init_reply(struct fuse_mount *fm, |
|---|
| 318 | + struct fuse_args *args, int error) |
|---|
| 309 | 319 | { |
|---|
| 320 | + struct fuse_conn *fc = fm->fc; |
|---|
| 321 | + struct cuse_init_args *ia = container_of(args, typeof(*ia), ap.args); |
|---|
| 322 | + struct fuse_args_pages *ap = &ia->ap; |
|---|
| 310 | 323 | struct cuse_conn *cc = fc_to_cc(fc), *pos; |
|---|
| 311 | | - struct cuse_init_out *arg = req->out.args[0].value; |
|---|
| 312 | | - struct page *page = req->pages[0]; |
|---|
| 324 | + struct cuse_init_out *arg = &ia->out; |
|---|
| 325 | + struct page *page = ap->pages[0]; |
|---|
| 313 | 326 | struct cuse_devinfo devinfo = { }; |
|---|
| 314 | 327 | struct device *dev; |
|---|
| 315 | 328 | struct cdev *cdev; |
|---|
| 316 | 329 | dev_t devt; |
|---|
| 317 | 330 | int rc, i; |
|---|
| 318 | 331 | |
|---|
| 319 | | - if (req->out.h.error || |
|---|
| 320 | | - arg->major != FUSE_KERNEL_VERSION || arg->minor < 11) { |
|---|
| 332 | + if (error || arg->major != FUSE_KERNEL_VERSION || arg->minor < 11) |
|---|
| 321 | 333 | goto err; |
|---|
| 322 | | - } |
|---|
| 323 | 334 | |
|---|
| 324 | 335 | fc->minor = arg->minor; |
|---|
| 325 | 336 | fc->max_read = max_t(unsigned, arg->max_read, 4096); |
|---|
| .. | .. |
|---|
| 328 | 339 | /* parse init reply */ |
|---|
| 329 | 340 | cc->unrestricted_ioctl = arg->flags & CUSE_UNRESTRICTED_IOCTL; |
|---|
| 330 | 341 | |
|---|
| 331 | | - rc = cuse_parse_devinfo(page_address(page), req->out.args[1].size, |
|---|
| 342 | + rc = cuse_parse_devinfo(page_address(page), ap->args.out_args[1].size, |
|---|
| 332 | 343 | &devinfo); |
|---|
| 333 | 344 | if (rc) |
|---|
| 334 | 345 | goto err; |
|---|
| .. | .. |
|---|
| 340 | 351 | else |
|---|
| 341 | 352 | rc = register_chrdev_region(devt, 1, devinfo.name); |
|---|
| 342 | 353 | if (rc) { |
|---|
| 343 | | - printk(KERN_ERR "CUSE: failed to register chrdev region\n"); |
|---|
| 354 | + pr_err("failed to register chrdev region\n"); |
|---|
| 344 | 355 | goto err; |
|---|
| 345 | 356 | } |
|---|
| 346 | 357 | |
|---|
| .. | .. |
|---|
| 395 | 406 | dev_set_uevent_suppress(dev, 0); |
|---|
| 396 | 407 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
|---|
| 397 | 408 | out: |
|---|
| 398 | | - kfree(arg); |
|---|
| 409 | + kfree(ia); |
|---|
| 399 | 410 | __free_page(page); |
|---|
| 400 | 411 | return; |
|---|
| 401 | 412 | |
|---|
| .. | .. |
|---|
| 407 | 418 | err_region: |
|---|
| 408 | 419 | unregister_chrdev_region(devt, 1); |
|---|
| 409 | 420 | err: |
|---|
| 410 | | - fuse_abort_conn(fc, false); |
|---|
| 421 | + fuse_abort_conn(fc); |
|---|
| 411 | 422 | goto out; |
|---|
| 412 | 423 | } |
|---|
| 413 | 424 | |
|---|
| 414 | 425 | static int cuse_send_init(struct cuse_conn *cc) |
|---|
| 415 | 426 | { |
|---|
| 416 | 427 | int rc; |
|---|
| 417 | | - struct fuse_req *req; |
|---|
| 418 | 428 | struct page *page; |
|---|
| 419 | | - struct fuse_conn *fc = &cc->fc; |
|---|
| 420 | | - struct cuse_init_in *arg; |
|---|
| 421 | | - void *outarg; |
|---|
| 429 | + struct fuse_mount *fm = &cc->fm; |
|---|
| 430 | + struct cuse_init_args *ia; |
|---|
| 431 | + struct fuse_args_pages *ap; |
|---|
| 422 | 432 | |
|---|
| 423 | 433 | BUILD_BUG_ON(CUSE_INIT_INFO_MAX > PAGE_SIZE); |
|---|
| 424 | | - |
|---|
| 425 | | - req = fuse_get_req_for_background(fc, 1); |
|---|
| 426 | | - if (IS_ERR(req)) { |
|---|
| 427 | | - rc = PTR_ERR(req); |
|---|
| 428 | | - goto err; |
|---|
| 429 | | - } |
|---|
| 430 | 434 | |
|---|
| 431 | 435 | rc = -ENOMEM; |
|---|
| 432 | 436 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
|---|
| 433 | 437 | if (!page) |
|---|
| 434 | | - goto err_put_req; |
|---|
| 438 | + goto err; |
|---|
| 435 | 439 | |
|---|
| 436 | | - outarg = kzalloc(sizeof(struct cuse_init_out), GFP_KERNEL); |
|---|
| 437 | | - if (!outarg) |
|---|
| 440 | + ia = kzalloc(sizeof(*ia), GFP_KERNEL); |
|---|
| 441 | + if (!ia) |
|---|
| 438 | 442 | goto err_free_page; |
|---|
| 439 | 443 | |
|---|
| 440 | | - arg = &req->misc.cuse_init_in; |
|---|
| 441 | | - arg->major = FUSE_KERNEL_VERSION; |
|---|
| 442 | | - arg->minor = FUSE_KERNEL_MINOR_VERSION; |
|---|
| 443 | | - arg->flags |= CUSE_UNRESTRICTED_IOCTL; |
|---|
| 444 | | - req->in.h.opcode = CUSE_INIT; |
|---|
| 445 | | - req->in.numargs = 1; |
|---|
| 446 | | - req->in.args[0].size = sizeof(struct cuse_init_in); |
|---|
| 447 | | - req->in.args[0].value = arg; |
|---|
| 448 | | - req->out.numargs = 2; |
|---|
| 449 | | - req->out.args[0].size = sizeof(struct cuse_init_out); |
|---|
| 450 | | - req->out.args[0].value = outarg; |
|---|
| 451 | | - req->out.args[1].size = CUSE_INIT_INFO_MAX; |
|---|
| 452 | | - req->out.argvar = 1; |
|---|
| 453 | | - req->out.argpages = 1; |
|---|
| 454 | | - req->pages[0] = page; |
|---|
| 455 | | - req->page_descs[0].length = req->out.args[1].size; |
|---|
| 456 | | - req->num_pages = 1; |
|---|
| 457 | | - req->end = cuse_process_init_reply; |
|---|
| 458 | | - fuse_request_send_background(fc, req); |
|---|
| 444 | + ap = &ia->ap; |
|---|
| 445 | + ia->in.major = FUSE_KERNEL_VERSION; |
|---|
| 446 | + ia->in.minor = FUSE_KERNEL_MINOR_VERSION; |
|---|
| 447 | + ia->in.flags |= CUSE_UNRESTRICTED_IOCTL; |
|---|
| 448 | + ap->args.opcode = CUSE_INIT; |
|---|
| 449 | + ap->args.in_numargs = 1; |
|---|
| 450 | + ap->args.in_args[0].size = sizeof(ia->in); |
|---|
| 451 | + ap->args.in_args[0].value = &ia->in; |
|---|
| 452 | + ap->args.out_numargs = 2; |
|---|
| 453 | + ap->args.out_args[0].size = sizeof(ia->out); |
|---|
| 454 | + ap->args.out_args[0].value = &ia->out; |
|---|
| 455 | + ap->args.out_args[1].size = CUSE_INIT_INFO_MAX; |
|---|
| 456 | + ap->args.out_argvar = true; |
|---|
| 457 | + ap->args.out_pages = true; |
|---|
| 458 | + ap->num_pages = 1; |
|---|
| 459 | + ap->pages = &ia->page; |
|---|
| 460 | + ap->descs = &ia->desc; |
|---|
| 461 | + ia->page = page; |
|---|
| 462 | + ia->desc.length = ap->args.out_args[1].size; |
|---|
| 463 | + ap->args.end = cuse_process_init_reply; |
|---|
| 459 | 464 | |
|---|
| 460 | | - return 0; |
|---|
| 461 | | - |
|---|
| 465 | + rc = fuse_simple_background(fm, &ap->args, GFP_KERNEL); |
|---|
| 466 | + if (rc) { |
|---|
| 467 | + kfree(ia); |
|---|
| 462 | 468 | err_free_page: |
|---|
| 463 | | - __free_page(page); |
|---|
| 464 | | -err_put_req: |
|---|
| 465 | | - fuse_put_request(fc, req); |
|---|
| 469 | + __free_page(page); |
|---|
| 470 | + } |
|---|
| 466 | 471 | err: |
|---|
| 467 | 472 | return rc; |
|---|
| 468 | 473 | } |
|---|
| .. | .. |
|---|
| 503 | 508 | * Limit the cuse channel to requests that can |
|---|
| 504 | 509 | * be represented in file->f_cred->user_ns. |
|---|
| 505 | 510 | */ |
|---|
| 506 | | - fuse_conn_init(&cc->fc, file->f_cred->user_ns); |
|---|
| 511 | + fuse_conn_init(&cc->fc, &cc->fm, file->f_cred->user_ns, |
|---|
| 512 | + &fuse_dev_fiq_ops, NULL); |
|---|
| 507 | 513 | |
|---|
| 508 | | - fud = fuse_dev_alloc(&cc->fc); |
|---|
| 514 | + fud = fuse_dev_alloc_install(&cc->fc); |
|---|
| 509 | 515 | if (!fud) { |
|---|
| 510 | 516 | kfree(cc); |
|---|
| 511 | 517 | return -ENOMEM; |
|---|
| .. | .. |
|---|
| 587 | 593 | { |
|---|
| 588 | 594 | struct cuse_conn *cc = dev_get_drvdata(dev); |
|---|
| 589 | 595 | |
|---|
| 590 | | - fuse_abort_conn(&cc->fc, false); |
|---|
| 596 | + fuse_abort_conn(&cc->fc); |
|---|
| 591 | 597 | return count; |
|---|
| 592 | 598 | } |
|---|
| 593 | 599 | static DEVICE_ATTR(abort, 0200, NULL, cuse_class_abort_store); |
|---|