1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * virtio-fs: Virtio Filesystem 4 * Copyright (C) 2018 Red Hat, Inc. 5 */ 6 7 #include <linux/fs.h> 8 #include <linux/dax.h> 9 #include <linux/pci.h> 10 #include <linux/interrupt.h> 11 #include <linux/group_cpus.h> 12 #include <linux/memremap.h> 13 #include <linux/module.h> 14 #include <linux/virtio.h> 15 #include <linux/virtio_fs.h> 16 #include <linux/delay.h> 17 #include <linux/fs_context.h> 18 #include <linux/fs_parser.h> 19 #include <linux/highmem.h> 20 #include <linux/cleanup.h> 21 #include <linux/uio.h> 22 #include "dev.h" 23 #include "fuse_i.h" 24 #include "fuse_dev_i.h" 25 26 /* Used to help calculate the FUSE connection's max_pages limit for a request's 27 * size. Parts of the struct fuse_req are sliced into scattergather lists in 28 * addition to the pages used, so this can help account for that overhead. 29 */ 30 #define FUSE_HEADER_OVERHEAD 4 31 32 /* List of virtio-fs device instances and a lock for the list. Also provides 33 * mutual exclusion in device removal and mounting path 34 */ 35 static DEFINE_MUTEX(virtio_fs_mutex); 36 static LIST_HEAD(virtio_fs_instances); 37 38 /* The /sys/fs/virtio_fs/ kset */ 39 static struct kset *virtio_fs_kset; 40 41 enum { 42 VQ_HIPRIO, 43 VQ_REQUEST 44 }; 45 46 #define VQ_NAME_LEN 24 47 48 /* Per-virtqueue state */ 49 struct virtio_fs_vq { 50 spinlock_t lock; 51 struct virtqueue *vq; /* protected by ->lock */ 52 struct work_struct done_work; 53 struct list_head queued_reqs; 54 struct list_head end_reqs; /* End these requests */ 55 struct work_struct dispatch_work; 56 struct fuse_dev *fud; 57 bool connected; 58 long in_flight; 59 struct completion in_flight_zero; /* No inflight requests */ 60 struct kobject *kobj; 61 char name[VQ_NAME_LEN]; 62 } ____cacheline_aligned_in_smp; 63 64 /* A virtio-fs device instance */ 65 struct virtio_fs { 66 struct kobject kobj; 67 struct kobject *mqs_kobj; 68 struct list_head list; /* on virtio_fs_instances */ 69 char *tag; 70 struct virtio_fs_vq *vqs; 71 unsigned int nvqs; /* number of virtqueues */ 72 unsigned int num_request_queues; /* number of request queues */ 73 struct dax_device *dax_dev; 74 75 unsigned int *mq_map; /* index = cpu id, value = request vq id */ 76 77 /* DAX memory window where file contents are mapped */ 78 void *window_kaddr; 79 phys_addr_t window_phys_addr; 80 size_t window_len; 81 }; 82 83 struct virtio_fs_forget_req { 84 struct fuse_in_header ih; 85 struct fuse_forget_in arg; 86 }; 87 88 struct virtio_fs_forget { 89 /* This request can be temporarily queued on virt queue */ 90 struct list_head list; 91 struct virtio_fs_forget_req req; 92 }; 93 94 struct virtio_fs_req_work { 95 struct fuse_req *req; 96 struct virtio_fs_vq *fsvq; 97 struct work_struct done_work; 98 }; 99 100 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq, 101 struct fuse_req *req, bool in_flight, 102 gfp_t gfp); 103 104 static const struct constant_table dax_param_enums[] = { 105 {"always", FUSE_DAX_ALWAYS }, 106 {"never", FUSE_DAX_NEVER }, 107 {"inode", FUSE_DAX_INODE_USER }, 108 {} 109 }; 110 111 enum { 112 OPT_DAX, 113 OPT_DAX_ENUM, 114 }; 115 116 static const struct fs_parameter_spec virtio_fs_parameters[] = { 117 fsparam_flag("dax", OPT_DAX), 118 fsparam_enum("dax", OPT_DAX_ENUM, dax_param_enums), 119 {} 120 }; 121 122 static int virtio_fs_parse_param(struct fs_context *fsc, 123 struct fs_parameter *param) 124 { 125 struct fs_parse_result result; 126 struct fuse_fs_context *ctx = fsc->fs_private; 127 int opt; 128 129 opt = fs_parse(fsc, virtio_fs_parameters, param, &result); 130 if (opt < 0) 131 return opt; 132 133 switch (opt) { 134 case OPT_DAX: 135 ctx->dax_mode = FUSE_DAX_ALWAYS; 136 break; 137 case OPT_DAX_ENUM: 138 ctx->dax_mode = result.uint_32; 139 break; 140 default: 141 return -EINVAL; 142 } 143 144 return 0; 145 } 146 147 static void virtio_fs_free_fsc(struct fs_context *fsc) 148 { 149 struct fuse_fs_context *ctx = fsc->fs_private; 150 151 kfree(ctx); 152 } 153 154 static inline struct virtio_fs_vq *vq_to_fsvq(struct virtqueue *vq) 155 { 156 struct virtio_fs *fs = vq->vdev->priv; 157 158 return &fs->vqs[vq->index]; 159 } 160 161 /* Should be called with fsvq->lock held. */ 162 static inline void inc_in_flight_req(struct virtio_fs_vq *fsvq) 163 { 164 fsvq->in_flight++; 165 } 166 167 /* Should be called with fsvq->lock held. */ 168 static inline void dec_in_flight_req(struct virtio_fs_vq *fsvq) 169 { 170 WARN_ON(fsvq->in_flight <= 0); 171 fsvq->in_flight--; 172 if (!fsvq->in_flight) 173 complete(&fsvq->in_flight_zero); 174 } 175 176 static ssize_t tag_show(struct kobject *kobj, 177 struct kobj_attribute *attr, char *buf) 178 { 179 struct virtio_fs *fs = container_of(kobj, struct virtio_fs, kobj); 180 181 return sysfs_emit(buf, "%s\n", fs->tag); 182 } 183 184 static struct kobj_attribute virtio_fs_tag_attr = __ATTR_RO(tag); 185 186 static struct attribute *virtio_fs_attrs[] = { 187 &virtio_fs_tag_attr.attr, 188 NULL 189 }; 190 ATTRIBUTE_GROUPS(virtio_fs); 191 192 static void virtio_fs_ktype_release(struct kobject *kobj) 193 { 194 struct virtio_fs *vfs = container_of(kobj, struct virtio_fs, kobj); 195 196 kfree(vfs->mq_map); 197 kfree(vfs->vqs); 198 kfree(vfs); 199 } 200 201 static const struct kobj_type virtio_fs_ktype = { 202 .release = virtio_fs_ktype_release, 203 .sysfs_ops = &kobj_sysfs_ops, 204 .default_groups = virtio_fs_groups, 205 }; 206 207 static struct virtio_fs_vq *virtio_fs_kobj_to_vq(struct virtio_fs *fs, 208 struct kobject *kobj) 209 { 210 int i; 211 212 for (i = 0; i < fs->nvqs; i++) { 213 if (kobj == fs->vqs[i].kobj) 214 return &fs->vqs[i]; 215 } 216 return NULL; 217 } 218 219 static ssize_t name_show(struct kobject *kobj, 220 struct kobj_attribute *attr, char *buf) 221 { 222 struct virtio_fs *fs = container_of(kobj->parent->parent, struct virtio_fs, kobj); 223 struct virtio_fs_vq *fsvq = virtio_fs_kobj_to_vq(fs, kobj); 224 225 if (!fsvq) 226 return -EINVAL; 227 return sysfs_emit(buf, "%s\n", fsvq->name); 228 } 229 230 static struct kobj_attribute virtio_fs_vq_name_attr = __ATTR_RO(name); 231 232 static ssize_t cpu_list_show(struct kobject *kobj, 233 struct kobj_attribute *attr, char *buf) 234 { 235 struct virtio_fs *fs = container_of(kobj->parent->parent, struct virtio_fs, kobj); 236 struct virtio_fs_vq *fsvq = virtio_fs_kobj_to_vq(fs, kobj); 237 unsigned int cpu, qid; 238 const size_t size = PAGE_SIZE - 1; 239 bool first = true; 240 int ret = 0, pos = 0; 241 242 if (!fsvq) 243 return -EINVAL; 244 245 qid = fsvq->vq->index; 246 for (cpu = 0; cpu < nr_cpu_ids; cpu++) { 247 if (qid < VQ_REQUEST || (fs->mq_map[cpu] == qid)) { 248 if (first) 249 ret = snprintf(buf + pos, size - pos, "%u", cpu); 250 else 251 ret = snprintf(buf + pos, size - pos, ", %u", cpu); 252 253 if (ret >= size - pos) 254 break; 255 first = false; 256 pos += ret; 257 } 258 } 259 ret = snprintf(buf + pos, size + 1 - pos, "\n"); 260 return pos + ret; 261 } 262 263 static struct kobj_attribute virtio_fs_vq_cpu_list_attr = __ATTR_RO(cpu_list); 264 265 static struct attribute *virtio_fs_vq_attrs[] = { 266 &virtio_fs_vq_name_attr.attr, 267 &virtio_fs_vq_cpu_list_attr.attr, 268 NULL 269 }; 270 271 static struct attribute_group virtio_fs_vq_attr_group = { 272 .attrs = virtio_fs_vq_attrs, 273 }; 274 275 /* Make sure virtiofs_mutex is held */ 276 static void virtio_fs_put_locked(struct virtio_fs *fs) 277 { 278 lockdep_assert_held(&virtio_fs_mutex); 279 280 kobject_put(&fs->kobj); 281 } 282 283 static void virtio_fs_put(struct virtio_fs *fs) 284 { 285 mutex_lock(&virtio_fs_mutex); 286 virtio_fs_put_locked(fs); 287 mutex_unlock(&virtio_fs_mutex); 288 } 289 290 static void virtio_fs_fiq_release(struct fuse_iqueue *fiq) 291 { 292 struct virtio_fs *vfs = fiq->priv; 293 294 virtio_fs_put(vfs); 295 } 296 297 static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq) 298 { 299 WARN_ON(fsvq->in_flight < 0); 300 301 /* Wait for in flight requests to finish.*/ 302 spin_lock(&fsvq->lock); 303 if (fsvq->in_flight) { 304 /* We are holding virtio_fs_mutex. There should not be any 305 * waiters waiting for completion. 306 */ 307 reinit_completion(&fsvq->in_flight_zero); 308 spin_unlock(&fsvq->lock); 309 wait_for_completion(&fsvq->in_flight_zero); 310 } else { 311 spin_unlock(&fsvq->lock); 312 } 313 314 flush_work(&fsvq->done_work); 315 flush_work(&fsvq->dispatch_work); 316 } 317 318 static void virtio_fs_drain_all_queues_locked(struct virtio_fs *fs) 319 { 320 struct virtio_fs_vq *fsvq; 321 int i; 322 323 for (i = 0; i < fs->nvqs; i++) { 324 fsvq = &fs->vqs[i]; 325 virtio_fs_drain_queue(fsvq); 326 } 327 } 328 329 static void virtio_fs_drain_all_queues(struct virtio_fs *fs) 330 { 331 /* Provides mutual exclusion between ->remove and ->kill_sb 332 * paths. We don't want both of these draining queue at the 333 * same time. Current completion logic reinits completion 334 * and that means there should not be any other thread 335 * doing reinit or waiting for completion already. 336 */ 337 mutex_lock(&virtio_fs_mutex); 338 virtio_fs_drain_all_queues_locked(fs); 339 mutex_unlock(&virtio_fs_mutex); 340 } 341 342 static void virtio_fs_start_all_queues(struct virtio_fs *fs) 343 { 344 struct virtio_fs_vq *fsvq; 345 int i; 346 347 for (i = 0; i < fs->nvqs; i++) { 348 fsvq = &fs->vqs[i]; 349 spin_lock(&fsvq->lock); 350 fsvq->connected = true; 351 spin_unlock(&fsvq->lock); 352 } 353 } 354 355 static void virtio_fs_delete_queues_sysfs(struct virtio_fs *fs) 356 { 357 struct virtio_fs_vq *fsvq; 358 int i; 359 360 for (i = 0; i < fs->nvqs; i++) { 361 fsvq = &fs->vqs[i]; 362 kobject_put(fsvq->kobj); 363 } 364 } 365 366 static int virtio_fs_add_queues_sysfs(struct virtio_fs *fs) 367 { 368 struct virtio_fs_vq *fsvq; 369 char buff[12]; 370 int i, j, ret; 371 372 for (i = 0; i < fs->nvqs; i++) { 373 fsvq = &fs->vqs[i]; 374 375 sprintf(buff, "%d", i); 376 fsvq->kobj = kobject_create_and_add(buff, fs->mqs_kobj); 377 if (!fsvq->kobj) { 378 ret = -ENOMEM; 379 goto out_del; 380 } 381 382 ret = sysfs_create_group(fsvq->kobj, &virtio_fs_vq_attr_group); 383 if (ret) { 384 kobject_put(fsvq->kobj); 385 goto out_del; 386 } 387 } 388 389 return 0; 390 391 out_del: 392 for (j = 0; j < i; j++) { 393 fsvq = &fs->vqs[j]; 394 kobject_put(fsvq->kobj); 395 } 396 return ret; 397 } 398 399 /* Add a new instance to the list or return -EEXIST if tag name exists*/ 400 static int virtio_fs_add_instance(struct virtio_device *vdev, 401 struct virtio_fs *fs) 402 { 403 struct virtio_fs *fs2; 404 int ret; 405 406 mutex_lock(&virtio_fs_mutex); 407 408 list_for_each_entry(fs2, &virtio_fs_instances, list) { 409 if (strcmp(fs->tag, fs2->tag) == 0) { 410 mutex_unlock(&virtio_fs_mutex); 411 return -EEXIST; 412 } 413 } 414 415 /* Use the virtio_device's index as a unique identifier, there is no 416 * need to allocate our own identifiers because the virtio_fs instance 417 * is only visible to userspace as long as the underlying virtio_device 418 * exists. 419 */ 420 fs->kobj.kset = virtio_fs_kset; 421 ret = kobject_add(&fs->kobj, NULL, "%d", vdev->index); 422 if (ret < 0) 423 goto out_unlock; 424 425 fs->mqs_kobj = kobject_create_and_add("mqs", &fs->kobj); 426 if (!fs->mqs_kobj) { 427 ret = -ENOMEM; 428 goto out_del; 429 } 430 431 ret = sysfs_create_link(&fs->kobj, &vdev->dev.kobj, "device"); 432 if (ret < 0) 433 goto out_put; 434 435 ret = virtio_fs_add_queues_sysfs(fs); 436 if (ret) 437 goto out_remove; 438 439 list_add_tail(&fs->list, &virtio_fs_instances); 440 441 mutex_unlock(&virtio_fs_mutex); 442 443 kobject_uevent(&fs->kobj, KOBJ_ADD); 444 445 return 0; 446 447 out_remove: 448 sysfs_remove_link(&fs->kobj, "device"); 449 out_put: 450 kobject_put(fs->mqs_kobj); 451 out_del: 452 kobject_del(&fs->kobj); 453 out_unlock: 454 mutex_unlock(&virtio_fs_mutex); 455 return ret; 456 } 457 458 /* Return the virtio_fs with a given tag, or NULL */ 459 static struct virtio_fs *virtio_fs_find_instance(const char *tag) 460 { 461 struct virtio_fs *fs; 462 463 mutex_lock(&virtio_fs_mutex); 464 465 list_for_each_entry(fs, &virtio_fs_instances, list) { 466 if (strcmp(fs->tag, tag) == 0) { 467 kobject_get(&fs->kobj); 468 goto found; 469 } 470 } 471 472 fs = NULL; /* not found */ 473 474 found: 475 mutex_unlock(&virtio_fs_mutex); 476 477 return fs; 478 } 479 480 static void virtio_fs_free_devs(struct virtio_fs *fs) 481 { 482 unsigned int i; 483 484 for (i = 0; i < fs->nvqs; i++) { 485 struct virtio_fs_vq *fsvq = &fs->vqs[i]; 486 487 if (!fsvq->fud) 488 continue; 489 490 fuse_dev_put(fsvq->fud); 491 fsvq->fud = NULL; 492 } 493 } 494 495 /* Read filesystem name from virtio config into fs->tag (must kfree()). */ 496 static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs) 497 { 498 char tag_buf[sizeof_field(struct virtio_fs_config, tag)]; 499 char *end; 500 size_t len; 501 502 virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag), 503 &tag_buf, sizeof(tag_buf)); 504 end = memchr(tag_buf, '\0', sizeof(tag_buf)); 505 if (end == tag_buf) 506 return -EINVAL; /* empty tag */ 507 if (!end) 508 end = &tag_buf[sizeof(tag_buf)]; 509 510 len = end - tag_buf; 511 fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL); 512 if (!fs->tag) 513 return -ENOMEM; 514 memcpy(fs->tag, tag_buf, len); 515 fs->tag[len] = '\0'; 516 517 /* While the VIRTIO specification allows any character, newlines are 518 * awkward on mount(8) command-lines and cause problems in the sysfs 519 * "tag" attr and uevent TAG= properties. Forbid them. 520 */ 521 if (strchr(fs->tag, '\n')) { 522 dev_dbg(&vdev->dev, "refusing virtiofs tag with newline character\n"); 523 return -EINVAL; 524 } 525 526 dev_info(&vdev->dev, "discovered new tag: %s\n", fs->tag); 527 return 0; 528 } 529 530 /* Work function for hiprio completion */ 531 static void virtio_fs_hiprio_done_work(struct work_struct *work) 532 { 533 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 534 done_work); 535 struct virtqueue *vq = fsvq->vq; 536 537 /* Free completed FUSE_FORGET requests */ 538 spin_lock(&fsvq->lock); 539 do { 540 unsigned int len; 541 void *req; 542 543 virtqueue_disable_cb(vq); 544 545 while ((req = virtqueue_get_buf(vq, &len)) != NULL) { 546 kfree(req); 547 dec_in_flight_req(fsvq); 548 } 549 } while (!virtqueue_enable_cb(vq)); 550 551 if (!list_empty(&fsvq->queued_reqs)) 552 schedule_work(&fsvq->dispatch_work); 553 554 spin_unlock(&fsvq->lock); 555 } 556 557 static void virtio_fs_request_dispatch_work(struct work_struct *work) 558 { 559 struct fuse_req *req; 560 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 561 dispatch_work); 562 int ret; 563 564 pr_debug("virtio-fs: worker %s called.\n", __func__); 565 while (1) { 566 spin_lock(&fsvq->lock); 567 req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req, 568 list); 569 if (!req) { 570 spin_unlock(&fsvq->lock); 571 break; 572 } 573 574 list_del_init(&req->list); 575 spin_unlock(&fsvq->lock); 576 fuse_request_end(req); 577 } 578 579 /* Dispatch pending requests */ 580 while (1) { 581 unsigned int flags; 582 583 spin_lock(&fsvq->lock); 584 req = list_first_entry_or_null(&fsvq->queued_reqs, 585 struct fuse_req, list); 586 if (!req) { 587 spin_unlock(&fsvq->lock); 588 return; 589 } 590 list_del_init(&req->list); 591 spin_unlock(&fsvq->lock); 592 593 flags = memalloc_nofs_save(); 594 ret = virtio_fs_enqueue_req(fsvq, req, true, GFP_KERNEL); 595 memalloc_nofs_restore(flags); 596 if (ret < 0) { 597 if (ret == -ENOSPC) { 598 spin_lock(&fsvq->lock); 599 list_add_tail(&req->list, &fsvq->queued_reqs); 600 spin_unlock(&fsvq->lock); 601 return; 602 } 603 req->out.h.error = ret; 604 spin_lock(&fsvq->lock); 605 dec_in_flight_req(fsvq); 606 spin_unlock(&fsvq->lock); 607 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", 608 ret); 609 fuse_request_end(req); 610 } 611 } 612 } 613 614 /* 615 * Returns 1 if queue is full and sender should wait a bit before sending 616 * next request, 0 otherwise. 617 */ 618 static int send_forget_request(struct virtio_fs_vq *fsvq, 619 struct virtio_fs_forget *forget, 620 bool in_flight) 621 { 622 struct scatterlist sg; 623 struct virtqueue *vq; 624 int ret = 0; 625 bool notify; 626 struct virtio_fs_forget_req *req = &forget->req; 627 628 spin_lock(&fsvq->lock); 629 if (!fsvq->connected) { 630 if (in_flight) 631 dec_in_flight_req(fsvq); 632 kfree(forget); 633 goto out; 634 } 635 636 sg_init_one(&sg, req, sizeof(*req)); 637 vq = fsvq->vq; 638 dev_dbg(&vq->vdev->dev, "%s\n", __func__); 639 640 ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC); 641 if (ret < 0) { 642 if (ret == -ENOSPC) { 643 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n", 644 ret); 645 list_add_tail(&forget->list, &fsvq->queued_reqs); 646 if (!in_flight) 647 inc_in_flight_req(fsvq); 648 /* Queue is full */ 649 ret = 1; 650 } else { 651 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n", 652 ret); 653 kfree(forget); 654 if (in_flight) 655 dec_in_flight_req(fsvq); 656 } 657 goto out; 658 } 659 660 if (!in_flight) 661 inc_in_flight_req(fsvq); 662 notify = virtqueue_kick_prepare(vq); 663 spin_unlock(&fsvq->lock); 664 665 if (notify) 666 virtqueue_notify(vq); 667 return ret; 668 out: 669 spin_unlock(&fsvq->lock); 670 return ret; 671 } 672 673 static void virtio_fs_hiprio_dispatch_work(struct work_struct *work) 674 { 675 struct virtio_fs_forget *forget; 676 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 677 dispatch_work); 678 pr_debug("virtio-fs: worker %s called.\n", __func__); 679 while (1) { 680 spin_lock(&fsvq->lock); 681 forget = list_first_entry_or_null(&fsvq->queued_reqs, 682 struct virtio_fs_forget, list); 683 if (!forget) { 684 spin_unlock(&fsvq->lock); 685 return; 686 } 687 688 list_del(&forget->list); 689 spin_unlock(&fsvq->lock); 690 if (send_forget_request(fsvq, forget, true)) 691 return; 692 } 693 } 694 695 /* Allocate and copy args into req->argbuf */ 696 static int copy_args_to_argbuf(struct fuse_req *req, gfp_t gfp) 697 { 698 struct fuse_args *args = req->args; 699 unsigned int offset = 0; 700 unsigned int num_in; 701 unsigned int num_out; 702 unsigned int len; 703 unsigned int i; 704 705 num_in = args->in_numargs - args->in_pages; 706 num_out = args->out_numargs - args->out_pages; 707 len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) + 708 fuse_len_args(num_out, args->out_args); 709 710 req->argbuf = kmalloc(len, gfp); 711 if (!req->argbuf) 712 return -ENOMEM; 713 714 for (i = 0; i < num_in; i++) { 715 memcpy(req->argbuf + offset, 716 args->in_args[i].value, 717 args->in_args[i].size); 718 offset += args->in_args[i].size; 719 } 720 721 return 0; 722 } 723 724 /* Copy args out of and free req->argbuf */ 725 static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req) 726 { 727 unsigned int remaining; 728 unsigned int offset; 729 unsigned int num_in; 730 unsigned int num_out; 731 unsigned int i; 732 733 remaining = req->out.h.len - sizeof(req->out.h); 734 num_in = args->in_numargs - args->in_pages; 735 num_out = args->out_numargs - args->out_pages; 736 offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args); 737 738 for (i = 0; i < num_out; i++) { 739 unsigned int argsize = args->out_args[i].size; 740 741 if (args->out_argvar && 742 i == args->out_numargs - 1 && 743 argsize > remaining) { 744 argsize = remaining; 745 } 746 747 memcpy(args->out_args[i].value, req->argbuf + offset, argsize); 748 offset += argsize; 749 750 if (i != args->out_numargs - 1) 751 remaining -= argsize; 752 } 753 754 /* Store the actual size of the variable-length arg */ 755 if (args->out_argvar) 756 args->out_args[args->out_numargs - 1].size = remaining; 757 758 kfree(req->argbuf); 759 req->argbuf = NULL; 760 } 761 762 /* Verify that the server properly follows the FUSE protocol */ 763 static bool virtio_fs_verify_response(struct fuse_req *req, unsigned int len) 764 { 765 struct fuse_out_header *oh = &req->out.h; 766 767 if (len < sizeof(*oh)) { 768 pr_warn("virtio-fs: response too short (%u)\n", len); 769 return false; 770 } 771 if (oh->len != len) { 772 pr_warn("virtio-fs: oh.len mismatch (%u != %u)\n", oh->len, len); 773 return false; 774 } 775 if (oh->unique != req->in.h.unique) { 776 pr_warn("virtio-fs: oh.unique mismatch (%llu != %llu)\n", 777 oh->unique, req->in.h.unique); 778 return false; 779 } 780 return true; 781 } 782 783 /* Work function for request completion */ 784 static void virtio_fs_request_complete(struct fuse_req *req, 785 struct virtio_fs_vq *fsvq) 786 { 787 struct fuse_args *args; 788 struct fuse_args_pages *ap; 789 unsigned int len, i, thislen; 790 struct folio *folio; 791 792 args = req->args; 793 copy_args_from_argbuf(args, req); 794 795 if (args->out_pages && args->page_zeroing) { 796 len = args->out_args[args->out_numargs - 1].size; 797 ap = container_of(args, typeof(*ap), args); 798 for (i = 0; i < ap->num_folios; i++) { 799 thislen = ap->descs[i].length; 800 if (len < thislen) { 801 WARN_ON(ap->descs[i].offset); 802 folio = ap->folios[i]; 803 folio_zero_segment(folio, len, thislen); 804 len = 0; 805 } else { 806 len -= thislen; 807 } 808 } 809 } 810 811 clear_bit(FR_SENT, &req->flags); 812 813 fuse_request_end(req); 814 spin_lock(&fsvq->lock); 815 dec_in_flight_req(fsvq); 816 spin_unlock(&fsvq->lock); 817 } 818 819 static void virtio_fs_complete_req_work(struct work_struct *work) 820 { 821 struct virtio_fs_req_work *w = 822 container_of(work, typeof(*w), done_work); 823 824 virtio_fs_request_complete(w->req, w->fsvq); 825 kfree(w); 826 } 827 828 static void virtio_fs_requests_done_work(struct work_struct *work) 829 { 830 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 831 done_work); 832 struct fuse_pqueue *fpq = &fsvq->fud->pq; 833 struct virtqueue *vq = fsvq->vq; 834 struct fuse_req *req; 835 struct fuse_req *next; 836 unsigned int len; 837 LIST_HEAD(reqs); 838 839 /* Collect completed requests off the virtqueue */ 840 spin_lock(&fsvq->lock); 841 do { 842 virtqueue_disable_cb(vq); 843 844 while ((req = virtqueue_get_buf(vq, &len)) != NULL) { 845 if (!virtio_fs_verify_response(req, len)) { 846 req->out.h.error = -EIO; 847 req->out.h.len = sizeof(struct fuse_out_header); 848 } 849 spin_lock(&fpq->lock); 850 list_move_tail(&req->list, &reqs); 851 spin_unlock(&fpq->lock); 852 } 853 } while (!virtqueue_enable_cb(vq)); 854 spin_unlock(&fsvq->lock); 855 856 /* End requests */ 857 list_for_each_entry_safe(req, next, &reqs, list) { 858 list_del_init(&req->list); 859 860 /* blocking async request completes in a worker context */ 861 if (req->args->may_block) { 862 struct virtio_fs_req_work *w; 863 864 w = kzalloc_obj(*w, GFP_NOFS | __GFP_NOFAIL); 865 INIT_WORK(&w->done_work, virtio_fs_complete_req_work); 866 w->fsvq = fsvq; 867 w->req = req; 868 schedule_work(&w->done_work); 869 } else { 870 virtio_fs_request_complete(req, fsvq); 871 } 872 } 873 874 /* Try to push previously queued requests, as the queue might no longer be full */ 875 spin_lock(&fsvq->lock); 876 if (!list_empty(&fsvq->queued_reqs)) 877 schedule_work(&fsvq->dispatch_work); 878 spin_unlock(&fsvq->lock); 879 } 880 881 static void virtio_fs_map_queues(struct virtio_device *vdev, struct virtio_fs *fs) 882 { 883 const struct cpumask *mask, *masks; 884 unsigned int q, cpu, nr_masks; 885 886 /* First attempt to map using existing transport layer affinities 887 * e.g. PCIe MSI-X 888 */ 889 if (!vdev->config->get_vq_affinity) 890 goto fallback; 891 892 for (q = 0; q < fs->num_request_queues; q++) { 893 mask = vdev->config->get_vq_affinity(vdev, VQ_REQUEST + q); 894 if (!mask) 895 goto fallback; 896 897 for_each_cpu(cpu, mask) 898 fs->mq_map[cpu] = q + VQ_REQUEST; 899 } 900 901 return; 902 fallback: 903 /* Attempt to map evenly in groups over the CPUs */ 904 masks = group_cpus_evenly(fs->num_request_queues, &nr_masks); 905 /* If even this fails we default to all CPUs use first request queue */ 906 if (!masks) { 907 for_each_possible_cpu(cpu) 908 fs->mq_map[cpu] = VQ_REQUEST; 909 return; 910 } 911 912 for (q = 0; q < fs->num_request_queues; q++) { 913 for_each_cpu(cpu, &masks[q % nr_masks]) 914 fs->mq_map[cpu] = q + VQ_REQUEST; 915 } 916 kfree(masks); 917 } 918 919 /* Virtqueue interrupt handler */ 920 static void virtio_fs_vq_done(struct virtqueue *vq) 921 { 922 struct virtio_fs_vq *fsvq = vq_to_fsvq(vq); 923 924 dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name); 925 926 schedule_work(&fsvq->done_work); 927 } 928 929 static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name, 930 int vq_type) 931 { 932 strscpy(fsvq->name, name, VQ_NAME_LEN); 933 spin_lock_init(&fsvq->lock); 934 INIT_LIST_HEAD(&fsvq->queued_reqs); 935 INIT_LIST_HEAD(&fsvq->end_reqs); 936 init_completion(&fsvq->in_flight_zero); 937 938 if (vq_type == VQ_REQUEST) { 939 INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work); 940 INIT_WORK(&fsvq->dispatch_work, 941 virtio_fs_request_dispatch_work); 942 } else { 943 INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work); 944 INIT_WORK(&fsvq->dispatch_work, 945 virtio_fs_hiprio_dispatch_work); 946 } 947 } 948 949 /* Initialize virtqueues */ 950 static int virtio_fs_setup_vqs(struct virtio_device *vdev, 951 struct virtio_fs *fs) 952 { 953 struct virtqueue_info *vqs_info; 954 struct virtqueue **vqs; 955 /* Specify pre_vectors to ensure that the queues before the 956 * request queues (e.g. hiprio) don't claim any of the CPUs in 957 * the multi-queue mapping and interrupt affinities 958 */ 959 struct irq_affinity desc = { .pre_vectors = VQ_REQUEST }; 960 unsigned int i; 961 int ret = 0; 962 963 virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues, 964 &fs->num_request_queues); 965 if (fs->num_request_queues == 0) 966 return -EINVAL; 967 968 /* Truncate nr of request queues to nr_cpu_id */ 969 fs->num_request_queues = min_t(unsigned int, fs->num_request_queues, 970 nr_cpu_ids); 971 fs->nvqs = VQ_REQUEST + fs->num_request_queues; 972 fs->vqs = kzalloc_objs(fs->vqs[VQ_HIPRIO], fs->nvqs); 973 if (!fs->vqs) 974 return -ENOMEM; 975 976 vqs = kmalloc_objs(vqs[VQ_HIPRIO], fs->nvqs); 977 fs->mq_map = kcalloc_node(nr_cpu_ids, sizeof(*fs->mq_map), GFP_KERNEL, 978 dev_to_node(&vdev->dev)); 979 vqs_info = kzalloc_objs(*vqs_info, fs->nvqs); 980 if (!vqs || !vqs_info || !fs->mq_map) { 981 ret = -ENOMEM; 982 goto out; 983 } 984 985 /* Initialize the hiprio/forget request virtqueue */ 986 vqs_info[VQ_HIPRIO].callback = virtio_fs_vq_done; 987 virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO); 988 vqs_info[VQ_HIPRIO].name = fs->vqs[VQ_HIPRIO].name; 989 990 /* Initialize the requests virtqueues */ 991 for (i = VQ_REQUEST; i < fs->nvqs; i++) { 992 char vq_name[VQ_NAME_LEN]; 993 994 snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST); 995 virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST); 996 vqs_info[i].callback = virtio_fs_vq_done; 997 vqs_info[i].name = fs->vqs[i].name; 998 } 999 1000 ret = virtio_find_vqs(vdev, fs->nvqs, vqs, vqs_info, &desc); 1001 if (ret < 0) 1002 goto out; 1003 1004 for (i = 0; i < fs->nvqs; i++) 1005 fs->vqs[i].vq = vqs[i]; 1006 1007 virtio_fs_start_all_queues(fs); 1008 out: 1009 kfree(vqs_info); 1010 kfree(vqs); 1011 if (ret) { 1012 kfree(fs->vqs); 1013 fs->vqs = NULL; 1014 kfree(fs->mq_map); 1015 fs->mq_map = NULL; 1016 } 1017 return ret; 1018 } 1019 1020 /* Free virtqueues (device must already be reset) */ 1021 static void virtio_fs_cleanup_vqs(struct virtio_device *vdev) 1022 { 1023 vdev->config->del_vqs(vdev); 1024 } 1025 1026 /* Map a window offset to a page frame number. The window offset will have 1027 * been produced by .iomap_next(), which maps a file offset to a window offset. 1028 */ 1029 static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1030 long nr_pages, enum dax_access_mode mode, 1031 void **kaddr, unsigned long *pfn) 1032 { 1033 struct virtio_fs *fs = dax_get_private(dax_dev); 1034 phys_addr_t offset = PFN_PHYS(pgoff); 1035 size_t max_nr_pages = fs->window_len / PAGE_SIZE - pgoff; 1036 1037 if (kaddr) 1038 *kaddr = fs->window_kaddr + offset; 1039 if (pfn) 1040 *pfn = PHYS_PFN(fs->window_phys_addr + offset); 1041 return nr_pages > max_nr_pages ? max_nr_pages : nr_pages; 1042 } 1043 1044 static int virtio_fs_zero_page_range(struct dax_device *dax_dev, 1045 pgoff_t pgoff, size_t nr_pages) 1046 { 1047 long rc; 1048 void *kaddr; 1049 1050 rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS, &kaddr, 1051 NULL); 1052 if (rc < 0) 1053 return dax_mem2blk_err(rc); 1054 1055 memset(kaddr, 0, nr_pages << PAGE_SHIFT); 1056 dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT); 1057 return 0; 1058 } 1059 1060 static const struct dax_operations virtio_fs_dax_ops = { 1061 .direct_access = virtio_fs_direct_access, 1062 .zero_page_range = virtio_fs_zero_page_range, 1063 }; 1064 1065 static void virtio_fs_cleanup_dax(void *data) 1066 { 1067 struct dax_device *dax_dev = data; 1068 1069 kill_dax(dax_dev); 1070 put_dax(dax_dev); 1071 } 1072 1073 DEFINE_FREE(cleanup_dax, struct dax_dev *, if (!IS_ERR_OR_NULL(_T)) virtio_fs_cleanup_dax(_T)) 1074 1075 static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs) 1076 { 1077 struct dax_device *dax_dev __free(cleanup_dax) = NULL; 1078 struct virtio_shm_region cache_reg; 1079 struct dev_pagemap *pgmap; 1080 bool have_cache; 1081 1082 if (!IS_ENABLED(CONFIG_FUSE_DAX)) 1083 return 0; 1084 1085 dax_dev = alloc_dax(fs, &virtio_fs_dax_ops); 1086 if (IS_ERR(dax_dev)) { 1087 int rc = PTR_ERR(dax_dev); 1088 return rc == -EOPNOTSUPP ? 0 : rc; 1089 } 1090 1091 /* Get cache region */ 1092 have_cache = virtio_get_shm_region(vdev, &cache_reg, 1093 (u8)VIRTIO_FS_SHMCAP_ID_CACHE); 1094 if (!have_cache) { 1095 dev_notice(&vdev->dev, "%s: No cache capability\n", __func__); 1096 return 0; 1097 } 1098 1099 if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len, 1100 dev_name(&vdev->dev))) { 1101 dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n", 1102 cache_reg.addr, cache_reg.len); 1103 return -EBUSY; 1104 } 1105 1106 dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len, 1107 cache_reg.addr); 1108 1109 pgmap = devm_kzalloc(&vdev->dev, sizeof(*pgmap), GFP_KERNEL); 1110 if (!pgmap) 1111 return -ENOMEM; 1112 1113 pgmap->type = MEMORY_DEVICE_FS_DAX; 1114 1115 /* Ideally we would directly use the PCI BAR resource but 1116 * devm_memremap_pages() wants its own copy in pgmap. So 1117 * initialize a struct resource from scratch (only the start 1118 * and end fields will be used). 1119 */ 1120 pgmap->range = (struct range) { 1121 .start = (phys_addr_t) cache_reg.addr, 1122 .end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1, 1123 }; 1124 pgmap->nr_range = 1; 1125 1126 fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap); 1127 if (IS_ERR(fs->window_kaddr)) 1128 return PTR_ERR(fs->window_kaddr); 1129 1130 fs->window_phys_addr = (phys_addr_t) cache_reg.addr; 1131 fs->window_len = (phys_addr_t) cache_reg.len; 1132 1133 dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n", 1134 __func__, fs->window_kaddr, cache_reg.addr, cache_reg.len); 1135 1136 fs->dax_dev = no_free_ptr(dax_dev); 1137 return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax, 1138 fs->dax_dev); 1139 } 1140 1141 static int virtio_fs_probe(struct virtio_device *vdev) 1142 { 1143 struct virtio_fs *fs; 1144 int ret; 1145 1146 fs = kzalloc_obj(*fs); 1147 if (!fs) 1148 return -ENOMEM; 1149 kobject_init(&fs->kobj, &virtio_fs_ktype); 1150 vdev->priv = fs; 1151 1152 ret = virtio_fs_read_tag(vdev, fs); 1153 if (ret < 0) 1154 goto out; 1155 1156 ret = virtio_fs_setup_vqs(vdev, fs); 1157 if (ret < 0) 1158 goto out; 1159 1160 virtio_fs_map_queues(vdev, fs); 1161 1162 ret = virtio_fs_setup_dax(vdev, fs); 1163 if (ret < 0) 1164 goto out_vqs; 1165 1166 /* Bring the device online in case the filesystem is mounted and 1167 * requests need to be sent before we return. 1168 */ 1169 virtio_device_ready(vdev); 1170 1171 ret = virtio_fs_add_instance(vdev, fs); 1172 if (ret < 0) 1173 goto out_vqs; 1174 1175 return 0; 1176 1177 out_vqs: 1178 virtio_reset_device(vdev); 1179 virtio_fs_cleanup_vqs(vdev); 1180 1181 out: 1182 vdev->priv = NULL; 1183 kobject_put(&fs->kobj); 1184 return ret; 1185 } 1186 1187 static void virtio_fs_stop_all_queues(struct virtio_fs *fs) 1188 { 1189 struct virtio_fs_vq *fsvq; 1190 int i; 1191 1192 for (i = 0; i < fs->nvqs; i++) { 1193 fsvq = &fs->vqs[i]; 1194 spin_lock(&fsvq->lock); 1195 fsvq->connected = false; 1196 spin_unlock(&fsvq->lock); 1197 } 1198 } 1199 1200 static void virtio_fs_remove(struct virtio_device *vdev) 1201 { 1202 struct virtio_fs *fs = vdev->priv; 1203 1204 mutex_lock(&virtio_fs_mutex); 1205 /* This device is going away. No one should get new reference */ 1206 list_del_init(&fs->list); 1207 virtio_fs_delete_queues_sysfs(fs); 1208 sysfs_remove_link(&fs->kobj, "device"); 1209 kobject_put(fs->mqs_kobj); 1210 kobject_del(&fs->kobj); 1211 virtio_fs_stop_all_queues(fs); 1212 virtio_fs_drain_all_queues_locked(fs); 1213 virtio_reset_device(vdev); 1214 virtio_fs_cleanup_vqs(vdev); 1215 1216 vdev->priv = NULL; 1217 /* Put device reference on virtio_fs object */ 1218 virtio_fs_put_locked(fs); 1219 mutex_unlock(&virtio_fs_mutex); 1220 } 1221 1222 #ifdef CONFIG_PM_SLEEP 1223 static int virtio_fs_freeze(struct virtio_device *vdev) 1224 { 1225 /* TODO need to save state here */ 1226 pr_warn("virtio-fs: suspend/resume not yet supported\n"); 1227 return -EOPNOTSUPP; 1228 } 1229 1230 static int virtio_fs_restore(struct virtio_device *vdev) 1231 { 1232 /* TODO need to restore state here */ 1233 return 0; 1234 } 1235 #endif /* CONFIG_PM_SLEEP */ 1236 1237 static const struct virtio_device_id id_table[] = { 1238 { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID }, 1239 {}, 1240 }; 1241 1242 static const unsigned int feature_table[] = {}; 1243 1244 static struct virtio_driver virtio_fs_driver = { 1245 .driver.name = KBUILD_MODNAME, 1246 .id_table = id_table, 1247 .feature_table = feature_table, 1248 .feature_table_size = ARRAY_SIZE(feature_table), 1249 .probe = virtio_fs_probe, 1250 .remove = virtio_fs_remove, 1251 #ifdef CONFIG_PM_SLEEP 1252 .freeze = virtio_fs_freeze, 1253 .restore = virtio_fs_restore, 1254 #endif 1255 }; 1256 1257 static void virtio_fs_send_forget(struct fuse_iqueue *fiq, struct fuse_forget_link *link) 1258 { 1259 struct virtio_fs_forget *forget; 1260 struct virtio_fs_forget_req *req; 1261 struct virtio_fs *fs = fiq->priv; 1262 struct virtio_fs_vq *fsvq = &fs->vqs[VQ_HIPRIO]; 1263 u64 unique = fuse_get_unique(fiq); 1264 1265 /* Allocate a buffer for the request */ 1266 forget = kmalloc_obj(*forget, GFP_NOFS | __GFP_NOFAIL); 1267 req = &forget->req; 1268 1269 req->ih = (struct fuse_in_header){ 1270 .opcode = FUSE_FORGET, 1271 .nodeid = link->forget_one.nodeid, 1272 .unique = unique, 1273 .len = sizeof(*req), 1274 }; 1275 req->arg = (struct fuse_forget_in){ 1276 .nlookup = link->forget_one.nlookup, 1277 }; 1278 1279 send_forget_request(fsvq, forget, false); 1280 kfree(link); 1281 } 1282 1283 static void virtio_fs_send_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req) 1284 { 1285 /* 1286 * TODO interrupts. 1287 * 1288 * Normal fs operations on a local filesystems aren't interruptible. 1289 * Exceptions are blocking lock operations; for example fcntl(F_SETLKW) 1290 * with shared lock between host and guest. 1291 */ 1292 } 1293 1294 /* Count number of scatter-gather elements required */ 1295 static unsigned int sg_count_fuse_folios(struct fuse_folio_desc *folio_descs, 1296 unsigned int num_folios, 1297 unsigned int total_len) 1298 { 1299 unsigned int i; 1300 unsigned int this_len; 1301 1302 for (i = 0; i < num_folios && total_len; i++) { 1303 this_len = min(folio_descs[i].length, total_len); 1304 total_len -= this_len; 1305 } 1306 1307 return i; 1308 } 1309 1310 /* Return the number of scatter-gather list elements required */ 1311 static unsigned int sg_count_fuse_req(struct fuse_req *req) 1312 { 1313 struct fuse_args *args = req->args; 1314 struct fuse_args_pages *ap = container_of(args, typeof(*ap), args); 1315 unsigned int size, total_sgs = 1 /* fuse_in_header */; 1316 1317 if (args->in_numargs - args->in_pages) 1318 total_sgs += 1; 1319 1320 if (args->in_pages) { 1321 size = args->in_args[args->in_numargs - 1].size; 1322 total_sgs += sg_count_fuse_folios(ap->descs, ap->num_folios, 1323 size); 1324 } 1325 1326 if (!test_bit(FR_ISREPLY, &req->flags)) 1327 return total_sgs; 1328 1329 total_sgs += 1 /* fuse_out_header */; 1330 1331 if (args->out_numargs - args->out_pages) 1332 total_sgs += 1; 1333 1334 if (args->out_pages) { 1335 size = args->out_args[args->out_numargs - 1].size; 1336 total_sgs += sg_count_fuse_folios(ap->descs, ap->num_folios, 1337 size); 1338 } 1339 1340 return total_sgs; 1341 } 1342 1343 /* Add folios to scatter-gather list and return number of elements used */ 1344 static unsigned int sg_init_fuse_folios(struct scatterlist *sg, 1345 struct folio **folios, 1346 struct fuse_folio_desc *folio_descs, 1347 unsigned int num_folios, 1348 unsigned int total_len) 1349 { 1350 unsigned int i; 1351 unsigned int this_len; 1352 1353 for (i = 0; i < num_folios && total_len; i++) { 1354 sg_init_table(&sg[i], 1); 1355 this_len = min(folio_descs[i].length, total_len); 1356 sg_set_folio(&sg[i], folios[i], this_len, folio_descs[i].offset); 1357 total_len -= this_len; 1358 } 1359 1360 return i; 1361 } 1362 1363 /* Add args to scatter-gather list and return number of elements used */ 1364 static unsigned int sg_init_fuse_args(struct scatterlist *sg, 1365 struct fuse_req *req, 1366 struct fuse_arg *args, 1367 unsigned int numargs, 1368 bool argpages, 1369 void *argbuf, 1370 unsigned int *len_used) 1371 { 1372 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args); 1373 unsigned int total_sgs = 0; 1374 unsigned int len; 1375 1376 len = fuse_len_args(numargs - argpages, args); 1377 if (len) 1378 sg_init_one(&sg[total_sgs++], argbuf, len); 1379 1380 if (argpages) 1381 total_sgs += sg_init_fuse_folios(&sg[total_sgs], 1382 ap->folios, ap->descs, 1383 ap->num_folios, 1384 args[numargs - 1].size); 1385 1386 if (len_used) 1387 *len_used = len; 1388 1389 return total_sgs; 1390 } 1391 1392 /* Add a request to a virtqueue and kick the device */ 1393 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq, 1394 struct fuse_req *req, bool in_flight, 1395 gfp_t gfp) 1396 { 1397 /* requests need at least 4 elements */ 1398 struct scatterlist *stack_sgs[6]; 1399 struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)]; 1400 struct scatterlist **sgs = stack_sgs; 1401 struct scatterlist *sg = stack_sg; 1402 struct virtqueue *vq; 1403 struct fuse_args *args = req->args; 1404 unsigned int argbuf_used = 0; 1405 unsigned int out_sgs = 0; 1406 unsigned int in_sgs = 0; 1407 unsigned int total_sgs; 1408 unsigned int i, hash; 1409 int ret; 1410 bool notify; 1411 struct fuse_pqueue *fpq; 1412 1413 /* Does the sglist fit on the stack? */ 1414 total_sgs = sg_count_fuse_req(req); 1415 if (total_sgs > ARRAY_SIZE(stack_sgs)) { 1416 sgs = kmalloc_objs(sgs[0], total_sgs, gfp); 1417 sg = kmalloc_objs(sg[0], total_sgs, gfp); 1418 if (!sgs || !sg) { 1419 ret = -ENOMEM; 1420 goto out; 1421 } 1422 } 1423 1424 /* Use a bounce buffer since stack args cannot be mapped */ 1425 ret = copy_args_to_argbuf(req, gfp); 1426 if (ret < 0) 1427 goto out; 1428 1429 /* Request elements */ 1430 sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h)); 1431 out_sgs += sg_init_fuse_args(&sg[out_sgs], req, 1432 (struct fuse_arg *)args->in_args, 1433 args->in_numargs, args->in_pages, 1434 req->argbuf, &argbuf_used); 1435 1436 /* Reply elements */ 1437 if (test_bit(FR_ISREPLY, &req->flags)) { 1438 sg_init_one(&sg[out_sgs + in_sgs++], 1439 &req->out.h, sizeof(req->out.h)); 1440 in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req, 1441 args->out_args, args->out_numargs, 1442 args->out_pages, 1443 req->argbuf + argbuf_used, NULL); 1444 } 1445 1446 WARN_ON(out_sgs + in_sgs != total_sgs); 1447 1448 for (i = 0; i < total_sgs; i++) 1449 sgs[i] = &sg[i]; 1450 1451 spin_lock(&fsvq->lock); 1452 1453 if (!fsvq->connected) { 1454 spin_unlock(&fsvq->lock); 1455 ret = -ENOTCONN; 1456 goto out; 1457 } 1458 1459 vq = fsvq->vq; 1460 ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC); 1461 if (ret < 0) { 1462 spin_unlock(&fsvq->lock); 1463 goto out; 1464 } 1465 1466 /* Request successfully sent. */ 1467 fpq = &fsvq->fud->pq; 1468 hash = fuse_req_hash(req->in.h.unique); 1469 spin_lock(&fpq->lock); 1470 list_add_tail(&req->list, &fpq->processing[hash]); 1471 spin_unlock(&fpq->lock); 1472 set_bit(FR_SENT, &req->flags); 1473 /* matches barrier in request_wait_answer() */ 1474 smp_mb__after_atomic(); 1475 1476 if (!in_flight) 1477 inc_in_flight_req(fsvq); 1478 notify = virtqueue_kick_prepare(vq); 1479 1480 spin_unlock(&fsvq->lock); 1481 1482 if (notify) 1483 virtqueue_notify(vq); 1484 1485 out: 1486 if (ret < 0 && req->argbuf) { 1487 kfree(req->argbuf); 1488 req->argbuf = NULL; 1489 } 1490 if (sgs != stack_sgs) { 1491 kfree(sgs); 1492 kfree(sg); 1493 } 1494 1495 return ret; 1496 } 1497 1498 static void virtio_fs_send_req(struct fuse_iqueue *fiq, struct fuse_req *req) 1499 { 1500 unsigned int queue_id; 1501 struct virtio_fs *fs; 1502 struct virtio_fs_vq *fsvq; 1503 int ret; 1504 1505 fuse_request_assign_unique(fiq, req); 1506 1507 clear_bit(FR_PENDING, &req->flags); 1508 1509 fs = fiq->priv; 1510 queue_id = fs->mq_map[raw_smp_processor_id()]; 1511 1512 pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u queue_id %u\n", 1513 __func__, req->in.h.opcode, req->in.h.unique, 1514 req->in.h.nodeid, req->in.h.len, 1515 fuse_len_args(req->args->out_numargs, req->args->out_args), 1516 queue_id); 1517 1518 fsvq = &fs->vqs[queue_id]; 1519 ret = virtio_fs_enqueue_req(fsvq, req, false, GFP_ATOMIC); 1520 if (ret < 0) { 1521 if (ret == -ENOSPC) { 1522 /* 1523 * Virtqueue full. Retry submission from worker 1524 * context as we might be holding fc->chan->bg_lock. 1525 */ 1526 spin_lock(&fsvq->lock); 1527 list_add_tail(&req->list, &fsvq->queued_reqs); 1528 inc_in_flight_req(fsvq); 1529 spin_unlock(&fsvq->lock); 1530 return; 1531 } 1532 req->out.h.error = ret; 1533 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret); 1534 1535 /* Can't end request in submission context. Use a worker */ 1536 spin_lock(&fsvq->lock); 1537 list_add_tail(&req->list, &fsvq->end_reqs); 1538 schedule_work(&fsvq->dispatch_work); 1539 spin_unlock(&fsvq->lock); 1540 return; 1541 } 1542 } 1543 1544 static const struct fuse_iqueue_ops virtio_fs_fiq_ops = { 1545 .send_forget = virtio_fs_send_forget, 1546 .send_interrupt = virtio_fs_send_interrupt, 1547 .send_req = virtio_fs_send_req, 1548 .release = virtio_fs_fiq_release, 1549 }; 1550 1551 static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx) 1552 { 1553 ctx->rootmode = S_IFDIR; 1554 ctx->default_permissions = 1; 1555 ctx->allow_other = 1; 1556 ctx->max_read = UINT_MAX; 1557 ctx->blksize = 512; 1558 ctx->destroy = true; 1559 ctx->no_control = true; 1560 ctx->no_force_umount = true; 1561 } 1562 1563 static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc) 1564 { 1565 struct fuse_mount *fm = get_fuse_mount_super(sb); 1566 struct fuse_conn *fc = fm->fc; 1567 struct virtio_fs *fs = fc->chan->iq.priv; 1568 struct fuse_fs_context *ctx = fsc->fs_private; 1569 unsigned int i; 1570 int err; 1571 1572 virtio_fs_ctx_set_defaults(ctx); 1573 mutex_lock(&virtio_fs_mutex); 1574 1575 /* After holding mutex, make sure virtiofs device is still there. 1576 * Though we are holding a reference to it, drive ->remove might 1577 * still have cleaned up virtual queues. In that case bail out. 1578 */ 1579 err = -EINVAL; 1580 if (list_empty(&fs->list)) { 1581 pr_info("virtio-fs: tag <%s> not found\n", fs->tag); 1582 goto err; 1583 } 1584 1585 err = -ENOMEM; 1586 /* Allocate fuse_dev for hiprio and notification queues */ 1587 for (i = 0; i < fs->nvqs; i++) { 1588 struct virtio_fs_vq *fsvq = &fs->vqs[i]; 1589 1590 fsvq->fud = fuse_dev_alloc(); 1591 if (!fsvq->fud) 1592 goto err_free_fuse_devs; 1593 } 1594 1595 if (ctx->dax_mode != FUSE_DAX_NEVER) { 1596 if (ctx->dax_mode == FUSE_DAX_ALWAYS && !fs->dax_dev) { 1597 err = -EINVAL; 1598 pr_err("virtio-fs: dax can't be enabled as filesystem" 1599 " device does not support it.\n"); 1600 goto err_free_fuse_devs; 1601 } 1602 ctx->dax_dev = fs->dax_dev; 1603 } 1604 err = fuse_fill_super_common(sb, ctx); 1605 if (err < 0) 1606 goto err_free_fuse_devs; 1607 1608 for (i = 0; i < fs->nvqs; i++) { 1609 struct virtio_fs_vq *fsvq = &fs->vqs[i]; 1610 1611 fuse_dev_install(fsvq->fud, fc->chan); 1612 } 1613 1614 /* Previous unmount will stop all queues. Start these again */ 1615 virtio_fs_start_all_queues(fs); 1616 fuse_send_init(fm); 1617 mutex_unlock(&virtio_fs_mutex); 1618 return 0; 1619 1620 err_free_fuse_devs: 1621 virtio_fs_free_devs(fs); 1622 err: 1623 mutex_unlock(&virtio_fs_mutex); 1624 return err; 1625 } 1626 1627 static void virtio_fs_conn_destroy(struct fuse_mount *fm) 1628 { 1629 struct fuse_conn *fc = fm->fc; 1630 struct virtio_fs *vfs = fc->chan->iq.priv; 1631 struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO]; 1632 1633 /* Stop dax worker. Soon evict_inodes() will be called which 1634 * will free all memory ranges belonging to all inodes. 1635 */ 1636 if (IS_ENABLED(CONFIG_FUSE_DAX)) 1637 fuse_dax_cancel_work(fc); 1638 1639 /* Stop forget queue. Soon destroy will be sent */ 1640 spin_lock(&fsvq->lock); 1641 fsvq->connected = false; 1642 spin_unlock(&fsvq->lock); 1643 virtio_fs_drain_all_queues(vfs); 1644 1645 fuse_conn_destroy(fm); 1646 1647 /* fuse_conn_destroy() must have sent destroy. Stop all queues 1648 * and drain one more time and free fuse devices. Freeing fuse 1649 * devices will drop their reference on fuse_conn and that in 1650 * turn will drop its reference on virtio_fs object. 1651 */ 1652 virtio_fs_stop_all_queues(vfs); 1653 virtio_fs_drain_all_queues(vfs); 1654 virtio_fs_free_devs(vfs); 1655 } 1656 1657 static void virtio_kill_sb(struct super_block *sb) 1658 { 1659 struct fuse_mount *fm = get_fuse_mount_super(sb); 1660 bool last; 1661 1662 /* If mount failed, we can still be called without any fc */ 1663 if (sb->s_root) { 1664 last = fuse_mount_remove(fm); 1665 if (last) 1666 virtio_fs_conn_destroy(fm); 1667 } 1668 kill_anon_super(sb); 1669 fuse_mount_destroy(fm); 1670 } 1671 1672 static int virtio_fs_test_super(struct super_block *sb, 1673 struct fs_context *fsc) 1674 { 1675 struct fuse_mount *fsc_fm = fsc->s_fs_info; 1676 struct fuse_mount *sb_fm = get_fuse_mount_super(sb); 1677 1678 return fsc_fm->fc->chan->iq.priv == sb_fm->fc->chan->iq.priv; 1679 } 1680 1681 static int virtio_fs_get_tree(struct fs_context *fsc) 1682 { 1683 struct virtio_fs *fs; 1684 struct super_block *sb; 1685 struct fuse_conn *fc = NULL; 1686 struct fuse_mount *fm; 1687 unsigned int virtqueue_size; 1688 struct fuse_chan *fch __free(fuse_chan_free) = fuse_chan_new(); 1689 int err = -EIO; 1690 1691 if (!fch) 1692 return -ENOMEM; 1693 1694 if (!fsc->source) 1695 return invalf(fsc, "No source specified"); 1696 1697 /* This gets a reference on virtio_fs object. This ptr gets installed 1698 * in chan->iq->priv. Once fuse_conn is going away, it calls ->put() 1699 * to drop the reference to this object. 1700 */ 1701 fs = virtio_fs_find_instance(fsc->source); 1702 if (!fs) { 1703 pr_info("virtio-fs: tag <%s> not found\n", fsc->source); 1704 return -EINVAL; 1705 } 1706 1707 virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq); 1708 if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD)) 1709 goto out_err; 1710 1711 err = -ENOMEM; 1712 fc = kzalloc_obj(struct fuse_conn); 1713 if (!fc) 1714 goto out_err; 1715 1716 fm = kzalloc_obj(struct fuse_mount); 1717 if (!fm) 1718 goto out_err; 1719 1720 fuse_iqueue_init(&fch->iq, &virtio_fs_fiq_ops, fs); 1721 fuse_conn_init(fc, fm, fsc->user_ns, no_free_ptr(fch)); 1722 1723 fc->release = fuse_free_conn; 1724 fc->delete_stale = true; 1725 fc->auto_submounts = true; 1726 fc->sync_fs = true; 1727 fc->use_pages_for_kvec_io = true; 1728 1729 /* Tell FUSE to split requests that exceed the virtqueue's size */ 1730 fc->max_pages_limit = min_t(unsigned int, fc->max_pages_limit, 1731 virtqueue_size - FUSE_HEADER_OVERHEAD); 1732 1733 fsc->s_fs_info = fm; 1734 sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc); 1735 if (fsc->s_fs_info) 1736 fuse_mount_destroy(fm); 1737 if (IS_ERR(sb)) 1738 return PTR_ERR(sb); 1739 1740 if (!sb->s_root) { 1741 err = virtio_fs_fill_super(sb, fsc); 1742 if (err) { 1743 deactivate_locked_super(sb); 1744 return err; 1745 } 1746 1747 sb->s_flags |= SB_ACTIVE; 1748 } 1749 1750 WARN_ON(fsc->root); 1751 fsc->root = dget(sb->s_root); 1752 return 0; 1753 1754 out_err: 1755 kfree(fc); 1756 virtio_fs_put(fs); 1757 return err; 1758 } 1759 1760 static const struct fs_context_operations virtio_fs_context_ops = { 1761 .free = virtio_fs_free_fsc, 1762 .parse_param = virtio_fs_parse_param, 1763 .get_tree = virtio_fs_get_tree, 1764 }; 1765 1766 static int virtio_fs_init_fs_context(struct fs_context *fsc) 1767 { 1768 struct fuse_fs_context *ctx; 1769 1770 if (fsc->purpose == FS_CONTEXT_FOR_SUBMOUNT) 1771 return fuse_init_fs_context_submount(fsc); 1772 1773 ctx = kzalloc_obj(struct fuse_fs_context); 1774 if (!ctx) 1775 return -ENOMEM; 1776 fsc->fs_private = ctx; 1777 fsc->ops = &virtio_fs_context_ops; 1778 return 0; 1779 } 1780 1781 static struct file_system_type virtio_fs_type = { 1782 .owner = THIS_MODULE, 1783 .name = "virtiofs", 1784 .init_fs_context = virtio_fs_init_fs_context, 1785 .kill_sb = virtio_kill_sb, 1786 .fs_flags = FS_ALLOW_IDMAP, 1787 }; 1788 1789 static int virtio_fs_uevent(const struct kobject *kobj, struct kobj_uevent_env *env) 1790 { 1791 const struct virtio_fs *fs = container_of(kobj, struct virtio_fs, kobj); 1792 1793 add_uevent_var(env, "TAG=%s", fs->tag); 1794 return 0; 1795 } 1796 1797 static const struct kset_uevent_ops virtio_fs_uevent_ops = { 1798 .uevent = virtio_fs_uevent, 1799 }; 1800 1801 static int __init virtio_fs_sysfs_init(void) 1802 { 1803 virtio_fs_kset = kset_create_and_add("virtiofs", &virtio_fs_uevent_ops, 1804 fs_kobj); 1805 if (!virtio_fs_kset) 1806 return -ENOMEM; 1807 return 0; 1808 } 1809 1810 static void virtio_fs_sysfs_exit(void) 1811 { 1812 kset_unregister(virtio_fs_kset); 1813 virtio_fs_kset = NULL; 1814 } 1815 1816 static int __init virtio_fs_init(void) 1817 { 1818 int ret; 1819 1820 ret = virtio_fs_sysfs_init(); 1821 if (ret < 0) 1822 return ret; 1823 1824 ret = register_virtio_driver(&virtio_fs_driver); 1825 if (ret < 0) 1826 goto sysfs_exit; 1827 1828 ret = register_filesystem(&virtio_fs_type); 1829 if (ret < 0) 1830 goto unregister_virtio_driver; 1831 1832 return 0; 1833 1834 unregister_virtio_driver: 1835 unregister_virtio_driver(&virtio_fs_driver); 1836 sysfs_exit: 1837 virtio_fs_sysfs_exit(); 1838 return ret; 1839 } 1840 module_init(virtio_fs_init); 1841 1842 static void __exit virtio_fs_exit(void) 1843 { 1844 unregister_filesystem(&virtio_fs_type); 1845 unregister_virtio_driver(&virtio_fs_driver); 1846 virtio_fs_sysfs_exit(); 1847 } 1848 module_exit(virtio_fs_exit); 1849 1850 MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>"); 1851 MODULE_DESCRIPTION("Virtio Filesystem"); 1852 MODULE_LICENSE("GPL"); 1853 MODULE_ALIAS_FS(KBUILD_MODNAME); 1854 MODULE_DEVICE_TABLE(virtio, id_table); 1855