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_VDAX_ALWAYS }, 106 {"never", FUSE_VDAX_NEVER }, 107 {"inode", FUSE_VDAX_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->vdax_mode = FUSE_VDAX_ALWAYS; 136 break; 137 case OPT_DAX_ENUM: 138 ctx->vdax_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 * Callers hold virtio_fs_mutex, which also keeps the virtqueues alive. 460 */ 461 static struct virtio_fs *virtio_fs_find_instance(const char *tag) 462 { 463 struct virtio_fs *fs; 464 465 lockdep_assert_held(&virtio_fs_mutex); 466 467 list_for_each_entry(fs, &virtio_fs_instances, list) { 468 if (strcmp(fs->tag, tag) == 0) { 469 kobject_get(&fs->kobj); 470 return fs; 471 } 472 } 473 474 return NULL; /* not found */ 475 } 476 477 static void virtio_fs_free_devs(struct virtio_fs *fs) 478 { 479 unsigned int i; 480 481 for (i = 0; i < fs->nvqs; i++) { 482 struct virtio_fs_vq *fsvq = &fs->vqs[i]; 483 484 if (!fsvq->fud) 485 continue; 486 487 fuse_dev_put(fsvq->fud); 488 fsvq->fud = NULL; 489 } 490 } 491 492 /* Read filesystem name from virtio config into fs->tag (must kfree()). */ 493 static int virtio_fs_read_tag(struct virtio_device *vdev, struct virtio_fs *fs) 494 { 495 char tag_buf[sizeof_field(struct virtio_fs_config, tag)]; 496 char *end; 497 size_t len; 498 499 virtio_cread_bytes(vdev, offsetof(struct virtio_fs_config, tag), 500 &tag_buf, sizeof(tag_buf)); 501 end = memchr(tag_buf, '\0', sizeof(tag_buf)); 502 if (end == tag_buf) 503 return -EINVAL; /* empty tag */ 504 if (!end) 505 end = &tag_buf[sizeof(tag_buf)]; 506 507 len = end - tag_buf; 508 fs->tag = devm_kmalloc(&vdev->dev, len + 1, GFP_KERNEL); 509 if (!fs->tag) 510 return -ENOMEM; 511 memcpy(fs->tag, tag_buf, len); 512 fs->tag[len] = '\0'; 513 514 /* While the VIRTIO specification allows any character, newlines are 515 * awkward on mount(8) command-lines and cause problems in the sysfs 516 * "tag" attr and uevent TAG= properties. Forbid them. 517 */ 518 if (strchr(fs->tag, '\n')) { 519 dev_dbg(&vdev->dev, "refusing virtiofs tag with newline character\n"); 520 return -EINVAL; 521 } 522 523 dev_info(&vdev->dev, "discovered new tag: %s\n", fs->tag); 524 return 0; 525 } 526 527 /* Work function for hiprio completion */ 528 static void virtio_fs_hiprio_done_work(struct work_struct *work) 529 { 530 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 531 done_work); 532 struct virtqueue *vq = fsvq->vq; 533 534 /* Free completed FUSE_FORGET requests */ 535 spin_lock(&fsvq->lock); 536 do { 537 unsigned int len; 538 void *req; 539 540 virtqueue_disable_cb(vq); 541 542 while ((req = virtqueue_get_buf(vq, &len)) != NULL) { 543 kfree(req); 544 dec_in_flight_req(fsvq); 545 } 546 } while (!virtqueue_enable_cb(vq)); 547 548 if (!list_empty(&fsvq->queued_reqs)) 549 schedule_work(&fsvq->dispatch_work); 550 551 spin_unlock(&fsvq->lock); 552 } 553 554 static void virtio_fs_request_dispatch_work(struct work_struct *work) 555 { 556 struct fuse_req *req; 557 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 558 dispatch_work); 559 int ret; 560 561 pr_debug("virtio-fs: worker %s called.\n", __func__); 562 while (1) { 563 spin_lock(&fsvq->lock); 564 req = list_first_entry_or_null(&fsvq->end_reqs, struct fuse_req, 565 list); 566 if (!req) { 567 spin_unlock(&fsvq->lock); 568 break; 569 } 570 571 list_del_init(&req->list); 572 spin_unlock(&fsvq->lock); 573 fuse_request_end(req); 574 } 575 576 /* Dispatch pending requests */ 577 while (1) { 578 unsigned int flags; 579 580 spin_lock(&fsvq->lock); 581 req = list_first_entry_or_null(&fsvq->queued_reqs, 582 struct fuse_req, list); 583 if (!req) { 584 spin_unlock(&fsvq->lock); 585 return; 586 } 587 list_del_init(&req->list); 588 spin_unlock(&fsvq->lock); 589 590 flags = memalloc_nofs_save(); 591 ret = virtio_fs_enqueue_req(fsvq, req, true, GFP_KERNEL); 592 memalloc_nofs_restore(flags); 593 if (ret < 0) { 594 if (ret == -ENOSPC) { 595 spin_lock(&fsvq->lock); 596 list_add_tail(&req->list, &fsvq->queued_reqs); 597 spin_unlock(&fsvq->lock); 598 return; 599 } 600 req->out.h.error = ret; 601 spin_lock(&fsvq->lock); 602 dec_in_flight_req(fsvq); 603 spin_unlock(&fsvq->lock); 604 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", 605 ret); 606 fuse_request_end(req); 607 } 608 } 609 } 610 611 /* 612 * Returns 1 if queue is full and sender should wait a bit before sending 613 * next request, 0 otherwise. 614 */ 615 static int send_forget_request(struct virtio_fs_vq *fsvq, 616 struct virtio_fs_forget *forget, 617 bool in_flight) 618 { 619 struct scatterlist sg; 620 struct virtqueue *vq; 621 int ret = 0; 622 bool notify; 623 struct virtio_fs_forget_req *req = &forget->req; 624 625 spin_lock(&fsvq->lock); 626 if (!fsvq->connected) { 627 if (in_flight) 628 dec_in_flight_req(fsvq); 629 kfree(forget); 630 goto out; 631 } 632 633 sg_init_one(&sg, req, sizeof(*req)); 634 vq = fsvq->vq; 635 dev_dbg(&vq->vdev->dev, "%s\n", __func__); 636 637 ret = virtqueue_add_outbuf(vq, &sg, 1, forget, GFP_ATOMIC); 638 if (ret < 0) { 639 if (ret == -ENOSPC) { 640 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Will try later\n", 641 ret); 642 list_add_tail(&forget->list, &fsvq->queued_reqs); 643 if (!in_flight) 644 inc_in_flight_req(fsvq); 645 /* Queue is full */ 646 ret = 1; 647 } else { 648 pr_debug("virtio-fs: Could not queue FORGET: err=%d. Dropping it.\n", 649 ret); 650 kfree(forget); 651 if (in_flight) 652 dec_in_flight_req(fsvq); 653 } 654 goto out; 655 } 656 657 if (!in_flight) 658 inc_in_flight_req(fsvq); 659 notify = virtqueue_kick_prepare(vq); 660 spin_unlock(&fsvq->lock); 661 662 if (notify) 663 virtqueue_notify(vq); 664 return ret; 665 out: 666 spin_unlock(&fsvq->lock); 667 return ret; 668 } 669 670 static void virtio_fs_hiprio_dispatch_work(struct work_struct *work) 671 { 672 struct virtio_fs_forget *forget; 673 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 674 dispatch_work); 675 pr_debug("virtio-fs: worker %s called.\n", __func__); 676 while (1) { 677 spin_lock(&fsvq->lock); 678 forget = list_first_entry_or_null(&fsvq->queued_reqs, 679 struct virtio_fs_forget, list); 680 if (!forget) { 681 spin_unlock(&fsvq->lock); 682 return; 683 } 684 685 list_del(&forget->list); 686 spin_unlock(&fsvq->lock); 687 if (send_forget_request(fsvq, forget, true)) 688 return; 689 } 690 } 691 692 /* Allocate and copy args into req->argbuf */ 693 static int copy_args_to_argbuf(struct fuse_req *req, gfp_t gfp) 694 { 695 struct fuse_args *args = req->args; 696 unsigned int offset = 0; 697 unsigned int num_in; 698 unsigned int num_out; 699 unsigned int len; 700 unsigned int i; 701 702 num_in = args->in_numargs - args->in_pages; 703 num_out = args->out_numargs - args->out_pages; 704 len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) + 705 fuse_len_args(num_out, args->out_args); 706 707 req->argbuf = kmalloc(len, gfp); 708 if (!req->argbuf) 709 return -ENOMEM; 710 711 for (i = 0; i < num_in; i++) { 712 memcpy(req->argbuf + offset, 713 args->in_args[i].value, 714 args->in_args[i].size); 715 offset += args->in_args[i].size; 716 } 717 718 return 0; 719 } 720 721 /* Copy args out of and free req->argbuf */ 722 static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req) 723 { 724 unsigned int remaining; 725 unsigned int offset; 726 unsigned int num_in; 727 unsigned int num_out; 728 unsigned int i; 729 730 /* fuse_request_end() returns this error; there are no args to copy. */ 731 if (req->out.h.error) 732 goto out; 733 734 remaining = req->out.h.len - sizeof(req->out.h); 735 num_in = args->in_numargs - args->in_pages; 736 num_out = args->out_numargs - args->out_pages; 737 offset = fuse_len_args(num_in, (struct fuse_arg *)args->in_args); 738 739 for (i = 0; i < num_out; i++) { 740 unsigned int argsize = args->out_args[i].size; 741 742 if (args->out_argvar && 743 i == args->out_numargs - 1 && 744 argsize > remaining) { 745 argsize = remaining; 746 } 747 748 memcpy(args->out_args[i].value, req->argbuf + offset, argsize); 749 offset += argsize; 750 751 if (i != args->out_numargs - 1) 752 remaining -= argsize; 753 } 754 755 /* Store the actual size of the variable-length arg */ 756 if (args->out_argvar) 757 args->out_args[args->out_numargs - 1].size = remaining; 758 759 out: 760 kfree(req->argbuf); 761 req->argbuf = NULL; 762 } 763 764 /* Verify that the server properly follows the FUSE protocol */ 765 static bool virtio_fs_verify_response(struct fuse_req *req, unsigned int len) 766 { 767 struct fuse_args *args = req->args; 768 struct fuse_out_header *oh = &req->out.h; 769 unsigned int expected; 770 771 if (len < sizeof(*oh)) { 772 pr_warn("virtio-fs: response too short (%u)\n", len); 773 return false; 774 } 775 if (oh->len != len) { 776 pr_warn("virtio-fs: oh.len mismatch (%u != %u)\n", oh->len, len); 777 return false; 778 } 779 if (oh->unique != req->in.h.unique) { 780 pr_warn("virtio-fs: oh.unique mismatch (%llu != %llu)\n", 781 oh->unique, req->in.h.unique); 782 return false; 783 } 784 if (oh->error <= -ERESTARTSYS || oh->error > 0) { 785 pr_warn("virtio-fs: invalid error value (%d)\n", oh->error); 786 return false; 787 } 788 789 if (oh->error) { 790 if (len != sizeof(*oh)) { 791 pr_warn("virtio-fs: error response too long (%u)\n", len); 792 return false; 793 } 794 return true; 795 } 796 797 expected = sizeof(*oh) + 798 fuse_len_args(args->out_numargs, args->out_args); 799 if (len > expected || 800 (len < expected && 801 (!args->out_argvar || 802 expected - len > args->out_args[args->out_numargs - 1].size))) { 803 pr_warn("virtio-fs: invalid response length (%u, expected %u)\n", 804 len, expected); 805 return false; 806 } 807 return true; 808 } 809 810 /* Work function for request completion */ 811 static void virtio_fs_request_complete(struct fuse_req *req, 812 struct virtio_fs_vq *fsvq) 813 { 814 struct fuse_args *args; 815 struct fuse_args_pages *ap; 816 unsigned int len, i, thislen; 817 struct folio *folio; 818 819 args = req->args; 820 copy_args_from_argbuf(args, req); 821 822 if (args->out_pages && args->page_zeroing) { 823 len = args->out_args[args->out_numargs - 1].size; 824 ap = container_of(args, typeof(*ap), args); 825 for (i = 0; i < ap->num_folios; i++) { 826 thislen = ap->descs[i].length; 827 if (len < thislen) { 828 unsigned int offset = ap->descs[i].offset; 829 830 folio = ap->folios[i]; 831 folio_zero_segment(folio, offset + len, 832 offset + thislen); 833 len = 0; 834 } else { 835 len -= thislen; 836 } 837 } 838 } 839 840 clear_bit(FR_SENT, &req->flags); 841 842 fuse_request_end(req); 843 spin_lock(&fsvq->lock); 844 dec_in_flight_req(fsvq); 845 spin_unlock(&fsvq->lock); 846 } 847 848 static void virtio_fs_complete_req_work(struct work_struct *work) 849 { 850 struct virtio_fs_req_work *w = 851 container_of(work, typeof(*w), done_work); 852 853 virtio_fs_request_complete(w->req, w->fsvq); 854 kfree(w); 855 } 856 857 static void virtio_fs_requests_done_work(struct work_struct *work) 858 { 859 struct virtio_fs_vq *fsvq = container_of(work, struct virtio_fs_vq, 860 done_work); 861 struct fuse_pqueue *fpq = &fsvq->fud->pq; 862 struct virtqueue *vq = fsvq->vq; 863 struct fuse_req *req; 864 struct fuse_req *next; 865 unsigned int len; 866 LIST_HEAD(reqs); 867 868 /* Collect completed requests off the virtqueue */ 869 spin_lock(&fsvq->lock); 870 do { 871 virtqueue_disable_cb(vq); 872 873 while ((req = virtqueue_get_buf(vq, &len)) != NULL) { 874 if (!virtio_fs_verify_response(req, len)) { 875 req->out.h.error = -EIO; 876 req->out.h.len = sizeof(struct fuse_out_header); 877 } 878 spin_lock(&fpq->lock); 879 list_move_tail(&req->list, &reqs); 880 spin_unlock(&fpq->lock); 881 } 882 } while (!virtqueue_enable_cb(vq)); 883 spin_unlock(&fsvq->lock); 884 885 /* End requests */ 886 list_for_each_entry_safe(req, next, &reqs, list) { 887 list_del_init(&req->list); 888 889 /* blocking async request completes in a worker context */ 890 if (req->args->may_block) { 891 struct virtio_fs_req_work *w; 892 893 w = kzalloc_obj(*w, GFP_NOFS | __GFP_NOFAIL); 894 INIT_WORK(&w->done_work, virtio_fs_complete_req_work); 895 w->fsvq = fsvq; 896 w->req = req; 897 schedule_work(&w->done_work); 898 } else { 899 virtio_fs_request_complete(req, fsvq); 900 } 901 } 902 903 /* Try to push previously queued requests, as the queue might no longer be full */ 904 spin_lock(&fsvq->lock); 905 if (!list_empty(&fsvq->queued_reqs)) 906 schedule_work(&fsvq->dispatch_work); 907 spin_unlock(&fsvq->lock); 908 } 909 910 static void virtio_fs_map_queues(struct virtio_device *vdev, struct virtio_fs *fs) 911 { 912 const struct cpumask *mask, *masks; 913 unsigned int q, cpu, nr_masks; 914 915 /* First attempt to map using existing transport layer affinities 916 * e.g. PCIe MSI-X 917 */ 918 if (!vdev->config->get_vq_affinity) 919 goto fallback; 920 921 for (q = 0; q < fs->num_request_queues; q++) { 922 mask = vdev->config->get_vq_affinity(vdev, VQ_REQUEST + q); 923 if (!mask) 924 goto fallback; 925 926 for_each_cpu(cpu, mask) 927 fs->mq_map[cpu] = q + VQ_REQUEST; 928 } 929 930 return; 931 fallback: 932 /* Attempt to map evenly in groups over the CPUs */ 933 masks = group_cpus_evenly(fs->num_request_queues, &nr_masks); 934 /* If even this fails we default to all CPUs use first request queue */ 935 if (!masks) { 936 for_each_possible_cpu(cpu) 937 fs->mq_map[cpu] = VQ_REQUEST; 938 return; 939 } 940 941 for (q = 0; q < fs->num_request_queues; q++) { 942 for_each_cpu(cpu, &masks[q % nr_masks]) 943 fs->mq_map[cpu] = q + VQ_REQUEST; 944 } 945 kfree(masks); 946 } 947 948 /* Virtqueue interrupt handler */ 949 static void virtio_fs_vq_done(struct virtqueue *vq) 950 { 951 struct virtio_fs_vq *fsvq = vq_to_fsvq(vq); 952 953 dev_dbg(&vq->vdev->dev, "%s %s\n", __func__, fsvq->name); 954 955 schedule_work(&fsvq->done_work); 956 } 957 958 static void virtio_fs_init_vq(struct virtio_fs_vq *fsvq, char *name, 959 int vq_type) 960 { 961 strscpy(fsvq->name, name, VQ_NAME_LEN); 962 spin_lock_init(&fsvq->lock); 963 INIT_LIST_HEAD(&fsvq->queued_reqs); 964 INIT_LIST_HEAD(&fsvq->end_reqs); 965 init_completion(&fsvq->in_flight_zero); 966 967 if (vq_type == VQ_REQUEST) { 968 INIT_WORK(&fsvq->done_work, virtio_fs_requests_done_work); 969 INIT_WORK(&fsvq->dispatch_work, 970 virtio_fs_request_dispatch_work); 971 } else { 972 INIT_WORK(&fsvq->done_work, virtio_fs_hiprio_done_work); 973 INIT_WORK(&fsvq->dispatch_work, 974 virtio_fs_hiprio_dispatch_work); 975 } 976 } 977 978 /* Initialize virtqueues */ 979 static int virtio_fs_setup_vqs(struct virtio_device *vdev, 980 struct virtio_fs *fs) 981 { 982 struct virtqueue_info *vqs_info; 983 struct virtqueue **vqs; 984 /* Specify pre_vectors to ensure that the queues before the 985 * request queues (e.g. hiprio) don't claim any of the CPUs in 986 * the multi-queue mapping and interrupt affinities 987 */ 988 struct irq_affinity desc = { .pre_vectors = VQ_REQUEST }; 989 unsigned int i; 990 int ret = 0; 991 992 virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues, 993 &fs->num_request_queues); 994 if (fs->num_request_queues == 0) 995 return -EINVAL; 996 997 /* Truncate nr of request queues to nr_cpu_id */ 998 fs->num_request_queues = min_t(unsigned int, fs->num_request_queues, 999 nr_cpu_ids); 1000 fs->nvqs = VQ_REQUEST + fs->num_request_queues; 1001 fs->vqs = kzalloc_objs(fs->vqs[VQ_HIPRIO], fs->nvqs); 1002 if (!fs->vqs) 1003 return -ENOMEM; 1004 1005 vqs = kmalloc_objs(vqs[VQ_HIPRIO], fs->nvqs); 1006 fs->mq_map = kcalloc_node(nr_cpu_ids, sizeof(*fs->mq_map), GFP_KERNEL, 1007 dev_to_node(&vdev->dev)); 1008 vqs_info = kzalloc_objs(*vqs_info, fs->nvqs); 1009 if (!vqs || !vqs_info || !fs->mq_map) { 1010 ret = -ENOMEM; 1011 goto out; 1012 } 1013 1014 /* Initialize the hiprio/forget request virtqueue */ 1015 vqs_info[VQ_HIPRIO].callback = virtio_fs_vq_done; 1016 virtio_fs_init_vq(&fs->vqs[VQ_HIPRIO], "hiprio", VQ_HIPRIO); 1017 vqs_info[VQ_HIPRIO].name = fs->vqs[VQ_HIPRIO].name; 1018 1019 /* Initialize the requests virtqueues */ 1020 for (i = VQ_REQUEST; i < fs->nvqs; i++) { 1021 char vq_name[VQ_NAME_LEN]; 1022 1023 snprintf(vq_name, VQ_NAME_LEN, "requests.%u", i - VQ_REQUEST); 1024 virtio_fs_init_vq(&fs->vqs[i], vq_name, VQ_REQUEST); 1025 vqs_info[i].callback = virtio_fs_vq_done; 1026 vqs_info[i].name = fs->vqs[i].name; 1027 } 1028 1029 ret = virtio_find_vqs(vdev, fs->nvqs, vqs, vqs_info, &desc); 1030 if (ret < 0) 1031 goto out; 1032 1033 for (i = 0; i < fs->nvqs; i++) 1034 fs->vqs[i].vq = vqs[i]; 1035 1036 virtio_fs_start_all_queues(fs); 1037 out: 1038 kfree(vqs_info); 1039 kfree(vqs); 1040 if (ret) { 1041 kfree(fs->vqs); 1042 fs->vqs = NULL; 1043 kfree(fs->mq_map); 1044 fs->mq_map = NULL; 1045 } 1046 return ret; 1047 } 1048 1049 /* Free virtqueues (device must already be reset) */ 1050 static void virtio_fs_cleanup_vqs(struct virtio_device *vdev) 1051 { 1052 vdev->config->del_vqs(vdev); 1053 } 1054 1055 /* Map a window offset to a page frame number. The window offset will have 1056 * been produced by fuse_iomap_next(), which maps a file offset to a window 1057 * offset. 1058 */ 1059 static long virtio_fs_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, 1060 long nr_pages, enum dax_access_mode mode, 1061 void **kaddr, unsigned long *pfn) 1062 { 1063 struct virtio_fs *fs = dax_get_private(dax_dev); 1064 phys_addr_t offset = PFN_PHYS(pgoff); 1065 size_t max_nr_pages = fs->window_len / PAGE_SIZE - pgoff; 1066 1067 if (kaddr) 1068 *kaddr = fs->window_kaddr + offset; 1069 if (pfn) 1070 *pfn = PHYS_PFN(fs->window_phys_addr + offset); 1071 return nr_pages > max_nr_pages ? max_nr_pages : nr_pages; 1072 } 1073 1074 static int virtio_fs_zero_page_range(struct dax_device *dax_dev, 1075 pgoff_t pgoff, size_t nr_pages) 1076 { 1077 long rc; 1078 void *kaddr; 1079 1080 rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS, &kaddr, 1081 NULL); 1082 if (rc < 0) 1083 return dax_mem2blk_err(rc); 1084 1085 memset(kaddr, 0, nr_pages << PAGE_SHIFT); 1086 dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT); 1087 return 0; 1088 } 1089 1090 static const struct dax_operations virtio_fs_dax_ops = { 1091 .direct_access = virtio_fs_direct_access, 1092 .zero_page_range = virtio_fs_zero_page_range, 1093 }; 1094 1095 static void virtio_fs_cleanup_dax(void *data) 1096 { 1097 struct dax_device *dax_dev = data; 1098 1099 kill_dax(dax_dev); 1100 put_dax(dax_dev); 1101 } 1102 1103 DEFINE_FREE(cleanup_dax, struct dax_dev *, if (!IS_ERR_OR_NULL(_T)) virtio_fs_cleanup_dax(_T)) 1104 1105 static int virtio_fs_setup_dax(struct virtio_device *vdev, struct virtio_fs *fs) 1106 { 1107 struct dax_device *dax_dev __free(cleanup_dax) = NULL; 1108 struct virtio_shm_region cache_reg; 1109 struct dev_pagemap *pgmap; 1110 bool have_cache; 1111 1112 if (!IS_ENABLED(CONFIG_FUSE_VDAX)) 1113 return 0; 1114 1115 dax_dev = alloc_dax(fs, &virtio_fs_dax_ops); 1116 if (IS_ERR(dax_dev)) { 1117 int rc = PTR_ERR(dax_dev); 1118 return rc == -EOPNOTSUPP ? 0 : rc; 1119 } 1120 1121 /* Get cache region */ 1122 have_cache = virtio_get_shm_region(vdev, &cache_reg, 1123 (u8)VIRTIO_FS_SHMCAP_ID_CACHE); 1124 if (!have_cache) { 1125 dev_notice(&vdev->dev, "%s: No cache capability\n", __func__); 1126 return 0; 1127 } 1128 1129 if (!devm_request_mem_region(&vdev->dev, cache_reg.addr, cache_reg.len, 1130 dev_name(&vdev->dev))) { 1131 dev_warn(&vdev->dev, "could not reserve region addr=0x%llx len=0x%llx\n", 1132 cache_reg.addr, cache_reg.len); 1133 return -EBUSY; 1134 } 1135 1136 dev_notice(&vdev->dev, "Cache len: 0x%llx @ 0x%llx\n", cache_reg.len, 1137 cache_reg.addr); 1138 1139 pgmap = devm_kzalloc_obj(&vdev->dev, *pgmap); 1140 if (!pgmap) 1141 return -ENOMEM; 1142 1143 pgmap->type = MEMORY_DEVICE_FS_DAX; 1144 1145 /* Ideally we would directly use the PCI BAR resource but 1146 * devm_memremap_pages() wants its own copy in pgmap. So 1147 * initialize a struct resource from scratch (only the start 1148 * and end fields will be used). 1149 */ 1150 pgmap->range = (struct range) { 1151 .start = (phys_addr_t) cache_reg.addr, 1152 .end = (phys_addr_t) cache_reg.addr + cache_reg.len - 1, 1153 }; 1154 pgmap->nr_range = 1; 1155 1156 fs->window_kaddr = devm_memremap_pages(&vdev->dev, pgmap); 1157 if (IS_ERR(fs->window_kaddr)) 1158 return PTR_ERR(fs->window_kaddr); 1159 1160 fs->window_phys_addr = (phys_addr_t) cache_reg.addr; 1161 fs->window_len = (phys_addr_t) cache_reg.len; 1162 1163 dev_dbg(&vdev->dev, "%s: window kaddr 0x%px phys_addr 0x%llx len 0x%llx\n", 1164 __func__, fs->window_kaddr, cache_reg.addr, cache_reg.len); 1165 1166 fs->dax_dev = no_free_ptr(dax_dev); 1167 return devm_add_action_or_reset(&vdev->dev, virtio_fs_cleanup_dax, 1168 fs->dax_dev); 1169 } 1170 1171 static int virtio_fs_probe(struct virtio_device *vdev) 1172 { 1173 struct virtio_fs *fs; 1174 int ret; 1175 1176 fs = kzalloc_obj(*fs); 1177 if (!fs) 1178 return -ENOMEM; 1179 kobject_init(&fs->kobj, &virtio_fs_ktype); 1180 vdev->priv = fs; 1181 1182 ret = virtio_fs_read_tag(vdev, fs); 1183 if (ret < 0) 1184 goto out; 1185 1186 ret = virtio_fs_setup_vqs(vdev, fs); 1187 if (ret < 0) 1188 goto out; 1189 1190 virtio_fs_map_queues(vdev, fs); 1191 1192 ret = virtio_fs_setup_dax(vdev, fs); 1193 if (ret < 0) 1194 goto out_vqs; 1195 1196 /* Bring the device online in case the filesystem is mounted and 1197 * requests need to be sent before we return. 1198 */ 1199 virtio_device_ready(vdev); 1200 1201 ret = virtio_fs_add_instance(vdev, fs); 1202 if (ret < 0) 1203 goto out_vqs; 1204 1205 return 0; 1206 1207 out_vqs: 1208 virtio_reset_device(vdev); 1209 virtio_fs_cleanup_vqs(vdev); 1210 1211 out: 1212 vdev->priv = NULL; 1213 kobject_put(&fs->kobj); 1214 return ret; 1215 } 1216 1217 static void virtio_fs_stop_all_queues(struct virtio_fs *fs) 1218 { 1219 struct virtio_fs_vq *fsvq; 1220 int i; 1221 1222 for (i = 0; i < fs->nvqs; i++) { 1223 fsvq = &fs->vqs[i]; 1224 spin_lock(&fsvq->lock); 1225 fsvq->connected = false; 1226 spin_unlock(&fsvq->lock); 1227 } 1228 } 1229 1230 static void virtio_fs_remove(struct virtio_device *vdev) 1231 { 1232 struct virtio_fs *fs = vdev->priv; 1233 1234 mutex_lock(&virtio_fs_mutex); 1235 /* This device is going away. No one should get new reference */ 1236 list_del_init(&fs->list); 1237 virtio_fs_delete_queues_sysfs(fs); 1238 sysfs_remove_link(&fs->kobj, "device"); 1239 kobject_put(fs->mqs_kobj); 1240 kobject_del(&fs->kobj); 1241 virtio_fs_stop_all_queues(fs); 1242 virtio_fs_drain_all_queues_locked(fs); 1243 virtio_reset_device(vdev); 1244 virtio_fs_cleanup_vqs(vdev); 1245 1246 vdev->priv = NULL; 1247 /* Put device reference on virtio_fs object */ 1248 virtio_fs_put_locked(fs); 1249 mutex_unlock(&virtio_fs_mutex); 1250 } 1251 1252 #ifdef CONFIG_PM_SLEEP 1253 static int virtio_fs_freeze(struct virtio_device *vdev) 1254 { 1255 /* TODO need to save state here */ 1256 pr_warn("virtio-fs: suspend/resume not yet supported\n"); 1257 return -EOPNOTSUPP; 1258 } 1259 1260 static int virtio_fs_restore(struct virtio_device *vdev) 1261 { 1262 /* TODO need to restore state here */ 1263 return 0; 1264 } 1265 #endif /* CONFIG_PM_SLEEP */ 1266 1267 static const struct virtio_device_id id_table[] = { 1268 { VIRTIO_ID_FS, VIRTIO_DEV_ANY_ID }, 1269 {}, 1270 }; 1271 1272 static const unsigned int feature_table[] = {}; 1273 1274 static struct virtio_driver virtio_fs_driver = { 1275 .driver.name = KBUILD_MODNAME, 1276 .id_table = id_table, 1277 .feature_table = feature_table, 1278 .feature_table_size = ARRAY_SIZE(feature_table), 1279 .probe = virtio_fs_probe, 1280 .remove = virtio_fs_remove, 1281 #ifdef CONFIG_PM_SLEEP 1282 .freeze = virtio_fs_freeze, 1283 .restore = virtio_fs_restore, 1284 #endif 1285 }; 1286 1287 static void virtio_fs_send_forget(struct fuse_iqueue *fiq, struct fuse_forget_link *link) 1288 { 1289 struct virtio_fs_forget *forget; 1290 struct virtio_fs_forget_req *req; 1291 struct virtio_fs *fs = fiq->priv; 1292 struct virtio_fs_vq *fsvq = &fs->vqs[VQ_HIPRIO]; 1293 u64 unique = fuse_get_unique(fiq); 1294 1295 /* Allocate a buffer for the request */ 1296 forget = kmalloc_obj(*forget, GFP_NOFS | __GFP_NOFAIL); 1297 req = &forget->req; 1298 1299 req->ih = (struct fuse_in_header){ 1300 .opcode = FUSE_FORGET, 1301 .nodeid = link->forget_one.nodeid, 1302 .unique = unique, 1303 .len = sizeof(*req), 1304 }; 1305 req->arg = (struct fuse_forget_in){ 1306 .nlookup = link->forget_one.nlookup, 1307 }; 1308 1309 send_forget_request(fsvq, forget, false); 1310 kfree(link); 1311 } 1312 1313 static void virtio_fs_send_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req) 1314 { 1315 /* 1316 * TODO interrupts. 1317 * 1318 * Normal fs operations on a local filesystems aren't interruptible. 1319 * Exceptions are blocking lock operations; for example fcntl(F_SETLKW) 1320 * with shared lock between host and guest. 1321 */ 1322 } 1323 1324 /* Count number of scatter-gather elements required */ 1325 static unsigned int sg_count_fuse_folios(struct fuse_folio_desc *folio_descs, 1326 unsigned int num_folios, 1327 unsigned int total_len) 1328 { 1329 unsigned int i; 1330 unsigned int this_len; 1331 1332 for (i = 0; i < num_folios && total_len; i++) { 1333 this_len = min(folio_descs[i].length, total_len); 1334 total_len -= this_len; 1335 } 1336 1337 return i; 1338 } 1339 1340 /* Return the number of scatter-gather list elements required */ 1341 static unsigned int sg_count_fuse_req(struct fuse_req *req) 1342 { 1343 struct fuse_args *args = req->args; 1344 struct fuse_args_pages *ap = container_of(args, typeof(*ap), args); 1345 unsigned int size, total_sgs = 1 /* fuse_in_header */; 1346 1347 if (args->in_numargs - args->in_pages) 1348 total_sgs += 1; 1349 1350 if (args->in_pages) { 1351 size = args->in_args[args->in_numargs - 1].size; 1352 total_sgs += sg_count_fuse_folios(ap->descs, ap->num_folios, 1353 size); 1354 } 1355 1356 if (!test_bit(FR_ISREPLY, &req->flags)) 1357 return total_sgs; 1358 1359 total_sgs += 1 /* fuse_out_header */; 1360 1361 if (args->out_numargs - args->out_pages) 1362 total_sgs += 1; 1363 1364 if (args->out_pages) { 1365 size = args->out_args[args->out_numargs - 1].size; 1366 total_sgs += sg_count_fuse_folios(ap->descs, ap->num_folios, 1367 size); 1368 } 1369 1370 return total_sgs; 1371 } 1372 1373 /* Add folios to scatter-gather list and return number of elements used */ 1374 static unsigned int sg_init_fuse_folios(struct scatterlist *sg, 1375 struct folio **folios, 1376 struct fuse_folio_desc *folio_descs, 1377 unsigned int num_folios, 1378 unsigned int total_len) 1379 { 1380 unsigned int i; 1381 unsigned int this_len; 1382 1383 for (i = 0; i < num_folios && total_len; i++) { 1384 sg_init_table(&sg[i], 1); 1385 this_len = min(folio_descs[i].length, total_len); 1386 sg_set_folio(&sg[i], folios[i], this_len, folio_descs[i].offset); 1387 total_len -= this_len; 1388 } 1389 1390 return i; 1391 } 1392 1393 /* Add args to scatter-gather list and return number of elements used */ 1394 static unsigned int sg_init_fuse_args(struct scatterlist *sg, 1395 struct fuse_req *req, 1396 struct fuse_arg *args, 1397 unsigned int numargs, 1398 bool argpages, 1399 void *argbuf, 1400 unsigned int *len_used) 1401 { 1402 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args); 1403 unsigned int total_sgs = 0; 1404 unsigned int len; 1405 1406 len = fuse_len_args(numargs - argpages, args); 1407 if (len) 1408 sg_init_one(&sg[total_sgs++], argbuf, len); 1409 1410 if (argpages) 1411 total_sgs += sg_init_fuse_folios(&sg[total_sgs], 1412 ap->folios, ap->descs, 1413 ap->num_folios, 1414 args[numargs - 1].size); 1415 1416 if (len_used) 1417 *len_used = len; 1418 1419 return total_sgs; 1420 } 1421 1422 /* Add a request to a virtqueue and kick the device */ 1423 static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq, 1424 struct fuse_req *req, bool in_flight, 1425 gfp_t gfp) 1426 { 1427 /* requests need at least 4 elements */ 1428 struct scatterlist *stack_sgs[6]; 1429 struct scatterlist stack_sg[ARRAY_SIZE(stack_sgs)]; 1430 struct scatterlist **sgs = stack_sgs; 1431 struct scatterlist *sg = stack_sg; 1432 struct virtqueue *vq; 1433 struct fuse_args *args = req->args; 1434 unsigned int argbuf_used = 0; 1435 unsigned int out_sgs = 0; 1436 unsigned int in_sgs = 0; 1437 unsigned int total_sgs; 1438 unsigned int i, hash; 1439 int ret; 1440 bool notify; 1441 struct fuse_pqueue *fpq; 1442 1443 /* Does the sglist fit on the stack? */ 1444 total_sgs = sg_count_fuse_req(req); 1445 if (total_sgs > ARRAY_SIZE(stack_sgs)) { 1446 sgs = kmalloc_objs(sgs[0], total_sgs, gfp); 1447 sg = kmalloc_objs(sg[0], total_sgs, gfp); 1448 if (!sgs || !sg) { 1449 ret = -ENOMEM; 1450 goto out; 1451 } 1452 } 1453 1454 /* Use a bounce buffer since stack args cannot be mapped */ 1455 ret = copy_args_to_argbuf(req, gfp); 1456 if (ret < 0) 1457 goto out; 1458 1459 /* Request elements */ 1460 sg_init_one(&sg[out_sgs++], &req->in.h, sizeof(req->in.h)); 1461 out_sgs += sg_init_fuse_args(&sg[out_sgs], req, 1462 (struct fuse_arg *)args->in_args, 1463 args->in_numargs, args->in_pages, 1464 req->argbuf, &argbuf_used); 1465 1466 /* Reply elements */ 1467 if (test_bit(FR_ISREPLY, &req->flags)) { 1468 sg_init_one(&sg[out_sgs + in_sgs++], 1469 &req->out.h, sizeof(req->out.h)); 1470 in_sgs += sg_init_fuse_args(&sg[out_sgs + in_sgs], req, 1471 args->out_args, args->out_numargs, 1472 args->out_pages, 1473 req->argbuf + argbuf_used, NULL); 1474 } 1475 1476 WARN_ON(out_sgs + in_sgs != total_sgs); 1477 1478 for (i = 0; i < total_sgs; i++) 1479 sgs[i] = &sg[i]; 1480 1481 spin_lock(&fsvq->lock); 1482 1483 if (!fsvq->connected) { 1484 spin_unlock(&fsvq->lock); 1485 ret = -ENOTCONN; 1486 goto out; 1487 } 1488 1489 vq = fsvq->vq; 1490 ret = virtqueue_add_sgs(vq, sgs, out_sgs, in_sgs, req, GFP_ATOMIC); 1491 if (ret < 0) { 1492 spin_unlock(&fsvq->lock); 1493 goto out; 1494 } 1495 1496 /* Request successfully sent. */ 1497 fpq = &fsvq->fud->pq; 1498 hash = fuse_req_hash(req->in.h.unique); 1499 spin_lock(&fpq->lock); 1500 list_add_tail(&req->list, &fpq->processing[hash]); 1501 spin_unlock(&fpq->lock); 1502 set_bit(FR_SENT, &req->flags); 1503 /* matches barrier in request_wait_answer() */ 1504 smp_mb__after_atomic(); 1505 1506 if (!in_flight) 1507 inc_in_flight_req(fsvq); 1508 notify = virtqueue_kick_prepare(vq); 1509 1510 spin_unlock(&fsvq->lock); 1511 1512 if (notify) 1513 virtqueue_notify(vq); 1514 1515 out: 1516 if (ret < 0 && req->argbuf) { 1517 kfree(req->argbuf); 1518 req->argbuf = NULL; 1519 } 1520 if (sgs != stack_sgs) { 1521 kfree(sgs); 1522 kfree(sg); 1523 } 1524 1525 return ret; 1526 } 1527 1528 static void virtio_fs_send_req(struct fuse_iqueue *fiq, struct fuse_req *req) 1529 { 1530 unsigned int queue_id; 1531 struct virtio_fs *fs; 1532 struct virtio_fs_vq *fsvq; 1533 int ret; 1534 1535 fuse_request_assign_unique(fiq, req); 1536 1537 clear_bit(FR_PENDING, &req->flags); 1538 1539 fs = fiq->priv; 1540 queue_id = fs->mq_map[raw_smp_processor_id()]; 1541 1542 pr_debug("%s: opcode %u unique %#llx nodeid %#llx in.len %u out.len %u queue_id %u\n", 1543 __func__, req->in.h.opcode, req->in.h.unique, 1544 req->in.h.nodeid, req->in.h.len, 1545 fuse_len_args(req->args->out_numargs, req->args->out_args), 1546 queue_id); 1547 1548 fsvq = &fs->vqs[queue_id]; 1549 ret = virtio_fs_enqueue_req(fsvq, req, false, GFP_ATOMIC); 1550 if (ret < 0) { 1551 if (ret == -ENOSPC) { 1552 /* 1553 * Virtqueue full. Retry submission from worker 1554 * context as we might be holding fc->chan->bg_lock. 1555 */ 1556 spin_lock(&fsvq->lock); 1557 list_add_tail(&req->list, &fsvq->queued_reqs); 1558 inc_in_flight_req(fsvq); 1559 spin_unlock(&fsvq->lock); 1560 return; 1561 } 1562 req->out.h.error = ret; 1563 pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret); 1564 1565 /* Can't end request in submission context. Use a worker */ 1566 spin_lock(&fsvq->lock); 1567 list_add_tail(&req->list, &fsvq->end_reqs); 1568 schedule_work(&fsvq->dispatch_work); 1569 spin_unlock(&fsvq->lock); 1570 return; 1571 } 1572 } 1573 1574 static const struct fuse_iqueue_ops virtio_fs_fiq_ops = { 1575 .send_forget = virtio_fs_send_forget, 1576 .send_interrupt = virtio_fs_send_interrupt, 1577 .send_req = virtio_fs_send_req, 1578 .release = virtio_fs_fiq_release, 1579 }; 1580 1581 static inline void virtio_fs_ctx_set_defaults(struct fuse_fs_context *ctx) 1582 { 1583 ctx->rootmode = S_IFDIR; 1584 ctx->default_permissions = 1; 1585 ctx->allow_other = 1; 1586 ctx->max_read = UINT_MAX; 1587 ctx->blksize = 512; 1588 ctx->destroy = true; 1589 ctx->no_control = true; 1590 ctx->no_force_umount = true; 1591 } 1592 1593 static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc) 1594 { 1595 struct fuse_mount *fm = get_fuse_mount_super(sb); 1596 struct fuse_conn *fc = fm->fc; 1597 struct virtio_fs *fs = fc->chan->iq.priv; 1598 struct fuse_fs_context *ctx = fsc->fs_private; 1599 unsigned int i; 1600 int err; 1601 1602 virtio_fs_ctx_set_defaults(ctx); 1603 mutex_lock(&virtio_fs_mutex); 1604 1605 /* After holding mutex, make sure virtiofs device is still there. 1606 * Though we are holding a reference to it, drive ->remove might 1607 * still have cleaned up virtual queues. In that case bail out. 1608 */ 1609 err = -EINVAL; 1610 if (list_empty(&fs->list)) { 1611 pr_info("virtio-fs: tag <%s> not found\n", fs->tag); 1612 goto err; 1613 } 1614 1615 err = -ENOMEM; 1616 /* Allocate fuse_dev for hiprio and notification queues */ 1617 for (i = 0; i < fs->nvqs; i++) { 1618 struct virtio_fs_vq *fsvq = &fs->vqs[i]; 1619 1620 fsvq->fud = fuse_dev_alloc(); 1621 if (!fsvq->fud) 1622 goto err_free_fuse_devs; 1623 } 1624 1625 if (ctx->vdax_mode != FUSE_VDAX_NEVER) { 1626 if (ctx->vdax_mode == FUSE_VDAX_ALWAYS && !fs->dax_dev) { 1627 err = -EINVAL; 1628 pr_err("virtio-fs: dax can't be enabled as filesystem" 1629 " device does not support it.\n"); 1630 goto err_free_fuse_devs; 1631 } 1632 ctx->vdax_dev = fs->dax_dev; 1633 } 1634 err = fuse_fill_super_common(sb, ctx); 1635 if (err < 0) 1636 goto err_free_fuse_devs; 1637 1638 for (i = 0; i < fs->nvqs; i++) { 1639 struct virtio_fs_vq *fsvq = &fs->vqs[i]; 1640 1641 fuse_dev_install(fsvq->fud, fc->chan); 1642 } 1643 1644 /* Previous unmount will stop all queues. Start these again */ 1645 virtio_fs_start_all_queues(fs); 1646 fuse_send_init(fm); 1647 mutex_unlock(&virtio_fs_mutex); 1648 return 0; 1649 1650 err_free_fuse_devs: 1651 virtio_fs_free_devs(fs); 1652 err: 1653 mutex_unlock(&virtio_fs_mutex); 1654 return err; 1655 } 1656 1657 static void virtio_fs_conn_destroy(struct fuse_mount *fm) 1658 { 1659 struct fuse_conn *fc = fm->fc; 1660 struct virtio_fs *vfs = fc->chan->iq.priv; 1661 struct virtio_fs_vq *fsvq = &vfs->vqs[VQ_HIPRIO]; 1662 1663 /* Stop dax worker. Soon evict_inodes() will be called which 1664 * will free all memory ranges belonging to all inodes. 1665 */ 1666 if (IS_ENABLED(CONFIG_FUSE_VDAX)) 1667 fuse_vdax_cancel_work(fc); 1668 1669 /* Stop forget queue. Soon destroy will be sent */ 1670 spin_lock(&fsvq->lock); 1671 fsvq->connected = false; 1672 spin_unlock(&fsvq->lock); 1673 virtio_fs_drain_all_queues(vfs); 1674 1675 fuse_conn_destroy(fm); 1676 1677 /* fuse_conn_destroy() must have sent destroy. Stop all queues 1678 * and drain one more time and free fuse devices. Freeing fuse 1679 * devices will drop their reference on fuse_conn and that in 1680 * turn will drop its reference on virtio_fs object. 1681 */ 1682 virtio_fs_stop_all_queues(vfs); 1683 virtio_fs_drain_all_queues(vfs); 1684 virtio_fs_free_devs(vfs); 1685 } 1686 1687 static void virtio_kill_sb(struct super_block *sb) 1688 { 1689 struct fuse_mount *fm = get_fuse_mount_super(sb); 1690 bool last; 1691 1692 /* If mount failed, we can still be called without any fc */ 1693 if (sb->s_root) { 1694 last = fuse_mount_remove(fm); 1695 if (last) 1696 virtio_fs_conn_destroy(fm); 1697 } 1698 kill_anon_super(sb); 1699 fuse_mount_destroy(fm); 1700 } 1701 1702 static int virtio_fs_test_super(struct super_block *sb, 1703 struct fs_context *fsc) 1704 { 1705 struct fuse_mount *fsc_fm = fsc->s_fs_info; 1706 struct fuse_mount *sb_fm = get_fuse_mount_super(sb); 1707 1708 return fsc_fm->fc->chan->iq.priv == sb_fm->fc->chan->iq.priv; 1709 } 1710 1711 static int virtio_fs_get_tree(struct fs_context *fsc) 1712 { 1713 struct virtio_fs *fs; 1714 struct super_block *sb; 1715 struct fuse_conn *fc = NULL; 1716 struct fuse_mount *fm; 1717 unsigned int virtqueue_size; 1718 struct fuse_chan *fch __free(fuse_chan_free) = fuse_chan_new(); 1719 int err = -EIO; 1720 1721 if (!fch) 1722 return -ENOMEM; 1723 1724 if (!fsc->source) 1725 return invalf(fsc, "No source specified"); 1726 1727 /* This gets a reference on virtio_fs object. This ptr gets installed 1728 * in chan->iq->priv. Once fuse_conn is going away, it calls ->put() 1729 * to drop the reference to this object. 1730 */ 1731 mutex_lock(&virtio_fs_mutex); 1732 fs = virtio_fs_find_instance(fsc->source); 1733 if (fs) 1734 virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq); 1735 mutex_unlock(&virtio_fs_mutex); 1736 1737 if (!fs) { 1738 pr_info("virtio-fs: tag <%s> not found\n", fsc->source); 1739 return -EINVAL; 1740 } 1741 1742 if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD)) 1743 goto out_err; 1744 1745 err = -ENOMEM; 1746 fc = kzalloc_obj(struct fuse_conn); 1747 if (!fc) 1748 goto out_err; 1749 1750 fm = kzalloc_obj(struct fuse_mount); 1751 if (!fm) 1752 goto out_err; 1753 1754 fuse_iqueue_init(&fch->iq, &virtio_fs_fiq_ops, fs); 1755 fuse_conn_init(fc, fm, fsc->user_ns, no_free_ptr(fch)); 1756 1757 fc->release = fuse_free_conn; 1758 fc->delete_stale = true; 1759 fc->auto_submounts = true; 1760 fc->sync_fs = true; 1761 fc->use_pages_for_kvec_io = true; 1762 1763 /* Tell FUSE to split requests that exceed the virtqueue's size */ 1764 fc->max_pages_limit = min_t(unsigned int, fc->max_pages_limit, 1765 virtqueue_size - FUSE_HEADER_OVERHEAD); 1766 1767 fsc->s_fs_info = fm; 1768 sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc); 1769 if (fsc->s_fs_info) 1770 fuse_mount_destroy(fm); 1771 if (IS_ERR(sb)) 1772 return PTR_ERR(sb); 1773 1774 if (!sb->s_root) { 1775 err = virtio_fs_fill_super(sb, fsc); 1776 if (err) { 1777 deactivate_locked_super(sb); 1778 return err; 1779 } 1780 1781 sb->s_flags |= SB_ACTIVE; 1782 } 1783 1784 WARN_ON(fsc->root); 1785 fsc->root = dget(sb->s_root); 1786 return 0; 1787 1788 out_err: 1789 kfree(fc); 1790 virtio_fs_put(fs); 1791 return err; 1792 } 1793 1794 static const struct fs_context_operations virtio_fs_context_ops = { 1795 .free = virtio_fs_free_fsc, 1796 .parse_param = virtio_fs_parse_param, 1797 .get_tree = virtio_fs_get_tree, 1798 }; 1799 1800 static int virtio_fs_init_fs_context(struct fs_context *fsc) 1801 { 1802 struct fuse_fs_context *ctx; 1803 1804 if (fsc->purpose == FS_CONTEXT_FOR_SUBMOUNT) 1805 return fuse_init_fs_context_submount(fsc); 1806 1807 ctx = kzalloc_obj(struct fuse_fs_context); 1808 if (!ctx) 1809 return -ENOMEM; 1810 fsc->fs_private = ctx; 1811 fsc->ops = &virtio_fs_context_ops; 1812 return 0; 1813 } 1814 1815 static struct file_system_type virtio_fs_type = { 1816 .owner = THIS_MODULE, 1817 .name = "virtiofs", 1818 .init_fs_context = virtio_fs_init_fs_context, 1819 .kill_sb = virtio_kill_sb, 1820 .fs_flags = FS_ALLOW_IDMAP, 1821 }; 1822 1823 static int virtio_fs_uevent(const struct kobject *kobj, struct kobj_uevent_env *env) 1824 { 1825 const struct virtio_fs *fs = container_of(kobj, struct virtio_fs, kobj); 1826 1827 add_uevent_var(env, "TAG=%s", fs->tag); 1828 return 0; 1829 } 1830 1831 static const struct kset_uevent_ops virtio_fs_uevent_ops = { 1832 .uevent = virtio_fs_uevent, 1833 }; 1834 1835 static int __init virtio_fs_sysfs_init(void) 1836 { 1837 virtio_fs_kset = kset_create_and_add("virtiofs", &virtio_fs_uevent_ops, 1838 fs_kobj); 1839 if (!virtio_fs_kset) 1840 return -ENOMEM; 1841 return 0; 1842 } 1843 1844 static void virtio_fs_sysfs_exit(void) 1845 { 1846 kset_unregister(virtio_fs_kset); 1847 virtio_fs_kset = NULL; 1848 } 1849 1850 static int __init virtio_fs_init(void) 1851 { 1852 int ret; 1853 1854 ret = virtio_fs_sysfs_init(); 1855 if (ret < 0) 1856 return ret; 1857 1858 ret = register_virtio_driver(&virtio_fs_driver); 1859 if (ret < 0) 1860 goto sysfs_exit; 1861 1862 ret = register_filesystem(&virtio_fs_type); 1863 if (ret < 0) 1864 goto unregister_virtio_driver; 1865 1866 return 0; 1867 1868 unregister_virtio_driver: 1869 unregister_virtio_driver(&virtio_fs_driver); 1870 sysfs_exit: 1871 virtio_fs_sysfs_exit(); 1872 return ret; 1873 } 1874 module_init(virtio_fs_init); 1875 1876 static void __exit virtio_fs_exit(void) 1877 { 1878 unregister_filesystem(&virtio_fs_type); 1879 unregister_virtio_driver(&virtio_fs_driver); 1880 virtio_fs_sysfs_exit(); 1881 } 1882 module_exit(virtio_fs_exit); 1883 1884 MODULE_AUTHOR("Stefan Hajnoczi <stefanha@redhat.com>"); 1885 MODULE_DESCRIPTION("Virtio Filesystem"); 1886 MODULE_LICENSE("GPL"); 1887 MODULE_ALIAS_FS(KBUILD_MODNAME); 1888 MODULE_DEVICE_TABLE(virtio, id_table); 1889