1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 FUSE: Filesystem in Userspace 4 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> 5 */ 6 7 #include "fuse_i.h" 8 #include "dev.h" 9 10 #include <linux/pagemap.h> 11 #include <linux/slab.h> 12 #include <linux/kernel.h> 13 #include <linux/sched.h> 14 #include <linux/sched/signal.h> 15 #include <linux/module.h> 16 #include <linux/swap.h> 17 #include <linux/falloc.h> 18 #include <linux/uio.h> 19 #include <linux/fs.h> 20 #include <linux/filelock.h> 21 #include <linux/splice.h> 22 #include <linux/task_io_accounting_ops.h> 23 #include <linux/iomap.h> 24 25 static int fuse_send_open(struct fuse_mount *fm, u64 nodeid, 26 unsigned int open_flags, int opcode, 27 struct fuse_open_out *outargp) 28 { 29 struct fuse_open_in inarg; 30 FUSE_ARGS(args); 31 int res; 32 33 memset(&inarg, 0, sizeof(inarg)); 34 inarg.flags = open_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); 35 if (!fm->fc->atomic_o_trunc) 36 inarg.flags &= ~O_TRUNC; 37 38 if (fm->fc->handle_killpriv_v2 && 39 (inarg.flags & O_TRUNC) && !capable(CAP_FSETID)) { 40 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID; 41 } 42 43 args.opcode = opcode; 44 args.nodeid = nodeid; 45 args.in_numargs = 1; 46 args.in_args[0].size = sizeof(inarg); 47 args.in_args[0].value = &inarg; 48 args.out_numargs = 1; 49 args.out_argvar = true; /* compat */ 50 args.out_args[0].size = sizeof(*outargp); 51 args.out_args[0].value = outargp; 52 53 res = fuse_simple_request(fm, &args); 54 55 return res < 0 ? res : 0; 56 } 57 58 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm, bool release) 59 { 60 struct fuse_file *ff; 61 62 ff = kzalloc_obj(struct fuse_file, GFP_KERNEL_ACCOUNT); 63 if (unlikely(!ff)) 64 return NULL; 65 66 ff->fm = fm; 67 if (release) { 68 ff->args = kzalloc_obj(*ff->args, GFP_KERNEL_ACCOUNT); 69 if (!ff->args) { 70 kfree(ff); 71 return NULL; 72 } 73 } 74 75 INIT_LIST_HEAD(&ff->write_entry); 76 refcount_set(&ff->count, 1); 77 RB_CLEAR_NODE(&ff->polled_node); 78 init_waitqueue_head(&ff->poll_wait); 79 80 ff->kh = atomic64_inc_return(&fm->fc->khctr); 81 82 return ff; 83 } 84 85 void fuse_file_free(struct fuse_file *ff) 86 { 87 kfree(ff->args); 88 kfree(ff); 89 } 90 91 static struct fuse_file *fuse_file_get(struct fuse_file *ff) 92 { 93 refcount_inc(&ff->count); 94 return ff; 95 } 96 97 static void fuse_release_end(struct fuse_args *args, int error) 98 { 99 struct fuse_release_args *ra = container_of(args, typeof(*ra), args); 100 101 iput(ra->inode); 102 kfree(ra); 103 } 104 105 static void fuse_file_put(struct fuse_file *ff, bool sync) 106 { 107 if (refcount_dec_and_test(&ff->count)) { 108 struct fuse_release_args *ra = &ff->args->release_args; 109 struct fuse_args *args = (ra ? &ra->args : NULL); 110 111 if (ra && ra->inode) 112 fuse_file_io_release(ff, ra->inode); 113 114 if (!args) { 115 /* Do nothing when server does not implement 'opendir' */ 116 } else if (args->opcode == FUSE_RELEASE && ff->fm->fc->no_open) { 117 fuse_release_end(args, 0); 118 } else if (sync) { 119 fuse_simple_request(ff->fm, args); 120 fuse_release_end(args, 0); 121 } else { 122 /* 123 * DAX inodes may need to issue a number of synchronous 124 * request for clearing the mappings. 125 */ 126 if (ra && ra->inode && FUSE_IS_VDAX(ra->inode)) 127 args->may_block = true; 128 args->end = fuse_release_end; 129 if (fuse_simple_background(ff->fm, args, 130 GFP_KERNEL | __GFP_NOFAIL)) 131 fuse_release_end(args, -ENOTCONN); 132 } 133 kfree(ff); 134 } 135 } 136 137 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, 138 unsigned int open_flags, bool isdir) 139 { 140 struct fuse_conn *fc = fm->fc; 141 struct fuse_file *ff; 142 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; 143 bool open = isdir ? !fc->no_opendir : !fc->no_open; 144 bool release = !isdir || open; 145 146 /* 147 * ff->args->release_args still needs to be allocated (so we can hold an 148 * inode reference while there are pending inflight file operations when 149 * ->release() is called, see fuse_prepare_release()) even if 150 * fc->no_open is set else it becomes possible for reclaim to deadlock 151 * if while servicing the readahead request the server triggers reclaim 152 * and reclaim evicts the inode of the file being read ahead. 153 */ 154 ff = fuse_file_alloc(fm, release); 155 if (!ff) 156 return ERR_PTR(-ENOMEM); 157 158 ff->fh = 0; 159 /* Default for no-open */ 160 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0); 161 if (open) { 162 /* Store outarg for fuse_finish_open() */ 163 struct fuse_open_out *outargp = &ff->args->open_outarg; 164 int err; 165 166 err = fuse_send_open(fm, nodeid, open_flags, opcode, outargp); 167 if (!err) { 168 ff->fh = outargp->fh; 169 ff->open_flags = outargp->open_flags; 170 } else if (err != -ENOSYS) { 171 fuse_file_free(ff); 172 return ERR_PTR(err); 173 } else { 174 if (isdir) { 175 /* No release needed */ 176 kfree(ff->args); 177 ff->args = NULL; 178 fc->no_opendir = 1; 179 } else { 180 fc->no_open = 1; 181 } 182 } 183 } 184 185 if (isdir) 186 ff->open_flags &= ~FOPEN_DIRECT_IO; 187 188 ff->nodeid = nodeid; 189 190 return ff; 191 } 192 193 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file, 194 bool isdir) 195 { 196 struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir); 197 198 if (!IS_ERR(ff)) 199 file->private_data = ff; 200 201 return PTR_ERR_OR_ZERO(ff); 202 } 203 EXPORT_SYMBOL_GPL(fuse_do_open); 204 205 static void fuse_link_write_file(struct file *file) 206 { 207 struct inode *inode = file_inode(file); 208 struct fuse_inode *fi = get_fuse_inode(inode); 209 struct fuse_file *ff = file->private_data; 210 /* 211 * file may be written through mmap, so chain it onto the 212 * inodes's write_file list 213 */ 214 spin_lock(&fi->lock); 215 if (list_empty(&ff->write_entry)) 216 list_add(&ff->write_entry, &fi->write_files); 217 spin_unlock(&fi->lock); 218 } 219 220 int fuse_finish_open(struct inode *inode, struct file *file) 221 { 222 struct fuse_file *ff = file->private_data; 223 struct fuse_conn *fc = get_fuse_conn(inode); 224 int err; 225 226 err = fuse_file_io_open(file, inode); 227 if (err) 228 return err; 229 230 if (ff->open_flags & FOPEN_STREAM) 231 stream_open(inode, file); 232 else if (ff->open_flags & FOPEN_NONSEEKABLE) 233 nonseekable_open(inode, file); 234 235 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache) 236 fuse_link_write_file(file); 237 238 return 0; 239 } 240 241 static void fuse_truncate_update_attr(struct inode *inode, struct file *file) 242 { 243 struct fuse_conn *fc = get_fuse_conn(inode); 244 struct fuse_inode *fi = get_fuse_inode(inode); 245 246 spin_lock(&fi->lock); 247 fi->attr_version = atomic64_inc_return(&fc->attr_version); 248 i_size_write(inode, 0); 249 spin_unlock(&fi->lock); 250 file_update_time(file); 251 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE); 252 } 253 254 static int fuse_open(struct inode *inode, struct file *file) 255 { 256 struct fuse_mount *fm = get_fuse_mount(inode); 257 struct fuse_inode *fi = get_fuse_inode(inode); 258 struct fuse_conn *fc = fm->fc; 259 struct fuse_file *ff; 260 int err; 261 bool is_truncate = (file->f_flags & O_TRUNC) && fc->atomic_o_trunc; 262 bool is_wb_truncate = is_truncate && fc->writeback_cache; 263 bool vdax_truncate = is_truncate && FUSE_IS_VDAX(inode); 264 265 if (fuse_is_bad(inode)) 266 return -EIO; 267 268 /* No truncate for DAX yet */ 269 if ((file->f_flags & O_TRUNC) && IS_DAX(inode) && !FUSE_IS_VDAX(inode)) 270 return -EINVAL; 271 272 err = generic_file_open(inode, file); 273 if (err) 274 return err; 275 276 if (is_wb_truncate || vdax_truncate) 277 inode_lock(inode); 278 279 if (vdax_truncate) { 280 filemap_invalidate_lock(inode->i_mapping); 281 err = fuse_vdax_break_layouts(inode, 0, -1); 282 if (err) 283 goto out_unlock; 284 } 285 286 if (is_wb_truncate || vdax_truncate) 287 fuse_set_nowrite(inode); 288 289 err = fuse_do_open(fm, get_node_id(inode), file, false); 290 if (!err) { 291 ff = file->private_data; 292 err = fuse_finish_open(inode, file); 293 if (err) 294 fuse_sync_release(fi, ff, file->f_flags); 295 else if (is_truncate) 296 fuse_truncate_update_attr(inode, file); 297 } 298 299 if (is_wb_truncate || vdax_truncate) 300 fuse_release_nowrite(inode); 301 if (!err) { 302 if (is_truncate) 303 truncate_pagecache(inode, 0); 304 else if (!(ff->open_flags & FOPEN_KEEP_CACHE) && !IS_DAX(inode)) 305 invalidate_inode_pages2(inode->i_mapping); 306 } 307 out_unlock: 308 if (vdax_truncate) 309 filemap_invalidate_unlock(inode->i_mapping); 310 if (is_wb_truncate || vdax_truncate) 311 inode_unlock(inode); 312 313 return err; 314 } 315 316 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff, 317 unsigned int flags, int opcode, bool sync) 318 { 319 struct fuse_conn *fc = ff->fm->fc; 320 struct fuse_release_args *ra = &ff->args->release_args; 321 322 if (fuse_is_passthrough(ff)) 323 fuse_passthrough_release(ff, fi ? fuse_inode_backing(fi) : NULL); 324 325 /* Inode is NULL on error path of fuse_create_open() */ 326 if (likely(fi)) { 327 spin_lock(&fi->lock); 328 list_del(&ff->write_entry); 329 spin_unlock(&fi->lock); 330 } 331 spin_lock(&fc->lock); 332 if (!RB_EMPTY_NODE(&ff->polled_node)) 333 rb_erase(&ff->polled_node, &fc->polled_files); 334 spin_unlock(&fc->lock); 335 336 wake_up_interruptible_all(&ff->poll_wait); 337 338 if (!ra) 339 return; 340 341 /* ff->args was used for open outarg */ 342 memset(ff->args, 0, sizeof(*ff->args)); 343 ra->inarg.fh = ff->fh; 344 ra->inarg.flags = flags; 345 ra->args.in_numargs = 1; 346 ra->args.in_args[0].size = sizeof(struct fuse_release_in); 347 ra->args.in_args[0].value = &ra->inarg; 348 ra->args.opcode = opcode; 349 ra->args.nodeid = ff->nodeid; 350 ra->args.force = true; 351 ra->args.nocreds = true; 352 353 /* 354 * Hold inode until release is finished. 355 * From fuse_sync_release() the refcount is 1 and everything's 356 * synchronous, so we are fine with not doing igrab() here. 357 */ 358 ra->inode = sync ? NULL : igrab(&fi->inode); 359 } 360 361 void fuse_file_release(struct inode *inode, struct fuse_file *ff, 362 unsigned int open_flags, fl_owner_t id, bool isdir) 363 { 364 struct fuse_inode *fi = get_fuse_inode(inode); 365 struct fuse_release_args *ra = &ff->args->release_args; 366 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; 367 368 fuse_prepare_release(fi, ff, open_flags, opcode, false); 369 370 if (ra && ff->flock) { 371 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; 372 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id); 373 } 374 375 /* 376 * Normally this will send the RELEASE request, however if 377 * some asynchronous READ or WRITE requests are outstanding, 378 * the sending will be delayed. 379 * 380 * Make the release synchronous if this is a fuseblk mount, 381 * synchronous RELEASE is allowed (and desirable) in this case 382 * because the server can be trusted not to screw up. 383 * 384 * Always use the asynchronous file put because the current thread 385 * might be the fuse server. This can happen if a process starts some 386 * aio and closes the fd before the aio completes. Since aio takes its 387 * own ref to the file, the IO completion has to drop the ref, which is 388 * how the fuse server can end up closing its clients' files. 389 * 390 * Exception is virtio-fs, which is not affected by the above (server is 391 * on host, cannot close open files in guest). Virtio-fs needs sync 392 * release, because the num_waiting mechanism to wait for all requests 393 * before commencing with fs shutdown doesn't work if submounts are 394 * used. 395 */ 396 fuse_file_put(ff, ff->fm->fc->auto_submounts); 397 } 398 399 void fuse_release_common(struct file *file, bool isdir) 400 { 401 fuse_file_release(file_inode(file), file->private_data, file->f_flags, 402 (fl_owner_t) file, isdir); 403 } 404 405 static int fuse_release(struct inode *inode, struct file *file) 406 { 407 struct fuse_conn *fc = get_fuse_conn(inode); 408 409 /* 410 * Dirty pages might remain despite write_inode_now() call from 411 * fuse_flush() due to writes racing with the close. 412 */ 413 if (fc->writeback_cache) 414 write_inode_now(inode, 1); 415 416 fuse_release_common(file, false); 417 418 /* return value is ignored by VFS */ 419 return 0; 420 } 421 422 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, 423 unsigned int flags) 424 { 425 WARN_ON(refcount_read(&ff->count) > 1); 426 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE, true); 427 fuse_file_put(ff, true); 428 } 429 EXPORT_SYMBOL_GPL(fuse_sync_release); 430 431 /* 432 * Scramble the ID space with XTEA, so that the value of the files_struct 433 * pointer is not exposed to userspace. 434 */ 435 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) 436 { 437 u32 *k = fc->scramble_key; 438 u64 v = (unsigned long) id; 439 u32 v0 = v; 440 u32 v1 = v >> 32; 441 u32 sum = 0; 442 int i; 443 444 for (i = 0; i < 32; i++) { 445 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); 446 sum += 0x9E3779B9; 447 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); 448 } 449 450 return (u64) v0 + ((u64) v1 << 32); 451 } 452 453 struct fuse_writepage_args { 454 struct fuse_io_args ia; 455 struct list_head queue_entry; 456 struct inode *inode; 457 struct fuse_sync_bucket *bucket; 458 }; 459 460 /* 461 * Wait for all pending writepages on the inode to finish. 462 * 463 * This is currently done by blocking further writes with FUSE_NOWRITE 464 * and waiting for all sent writes to complete. 465 * 466 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage 467 * could conflict with truncation. 468 */ 469 static void fuse_sync_writes(struct inode *inode) 470 { 471 fuse_set_nowrite(inode); 472 fuse_release_nowrite(inode); 473 } 474 475 static int fuse_flush(struct file *file, fl_owner_t id) 476 { 477 struct inode *inode = file_inode(file); 478 struct fuse_mount *fm = get_fuse_mount(inode); 479 struct fuse_file *ff = file->private_data; 480 struct fuse_flush_in inarg; 481 FUSE_ARGS(args); 482 int err; 483 484 if (fuse_is_bad(inode)) 485 return -EIO; 486 487 if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache) 488 return 0; 489 490 err = write_inode_now(inode, 1); 491 if (err) 492 return err; 493 494 err = filemap_check_errors(file->f_mapping); 495 if (err) 496 return err; 497 498 err = 0; 499 if (fm->fc->no_flush) 500 goto inval_attr_out; 501 502 memset(&inarg, 0, sizeof(inarg)); 503 inarg.fh = ff->fh; 504 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id); 505 args.opcode = FUSE_FLUSH; 506 args.nodeid = get_node_id(inode); 507 args.in_numargs = 1; 508 args.in_args[0].size = sizeof(inarg); 509 args.in_args[0].value = &inarg; 510 args.force = true; 511 512 err = fuse_simple_request(fm, &args); 513 if (err == -ENOSYS) { 514 fm->fc->no_flush = 1; 515 err = 0; 516 } 517 518 inval_attr_out: 519 /* 520 * In memory i_blocks is not maintained by fuse, if writeback cache is 521 * enabled, i_blocks from cached attr may not be accurate. 522 */ 523 if (!err && fm->fc->writeback_cache) 524 fuse_invalidate_attr_mask(inode, STATX_BLOCKS); 525 return err; 526 } 527 528 int fuse_fsync_common(struct file *file, loff_t start, loff_t end, 529 int datasync, int opcode) 530 { 531 struct inode *inode = file->f_mapping->host; 532 struct fuse_mount *fm = get_fuse_mount(inode); 533 struct fuse_file *ff = file->private_data; 534 FUSE_ARGS(args); 535 struct fuse_fsync_in inarg; 536 537 memset(&inarg, 0, sizeof(inarg)); 538 inarg.fh = ff->fh; 539 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0; 540 args.opcode = opcode; 541 args.nodeid = get_node_id(inode); 542 args.in_numargs = 1; 543 args.in_args[0].size = sizeof(inarg); 544 args.in_args[0].value = &inarg; 545 return fuse_simple_request(fm, &args); 546 } 547 548 static int fuse_fsync(struct file *file, loff_t start, loff_t end, 549 int datasync) 550 { 551 struct inode *inode = file->f_mapping->host; 552 struct fuse_conn *fc = get_fuse_conn(inode); 553 int err; 554 555 if (fuse_is_bad(inode)) 556 return -EIO; 557 558 if (IS_DAX(inode) && !fuse_inode_vdax(inode)) { 559 /* 560 * Note: the DAX fault path calls this via dax_finish_sync_fault() 561 * and taking inode lock in that context is prohibited. 562 * 563 * FIXME: need to flush CPU caches for the DAX memory range 564 * FIXME: need to sync metadata to the server 565 */ 566 return 0; 567 } 568 569 inode_lock(inode); 570 571 /* 572 * Start writeback against all dirty pages of the inode, then 573 * wait for all outstanding writes, before sending the FSYNC 574 * request. 575 */ 576 err = file_write_and_wait_range(file, start, end); 577 if (err) 578 goto out; 579 580 fuse_sync_writes(inode); 581 582 /* 583 * Due to implementation of fuse writeback 584 * file_write_and_wait_range() does not catch errors. 585 * We have to do this directly after fuse_sync_writes() 586 */ 587 err = file_check_and_advance_wb_err(file); 588 if (err) 589 goto out; 590 591 err = sync_inode_metadata(inode, 1); 592 if (err) 593 goto out; 594 595 if (fc->no_fsync) 596 goto out; 597 598 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC); 599 if (err == -ENOSYS) { 600 fc->no_fsync = 1; 601 err = 0; 602 } 603 out: 604 inode_unlock(inode); 605 606 return err; 607 } 608 609 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos, 610 size_t count, int opcode) 611 { 612 struct fuse_file *ff = file->private_data; 613 struct fuse_args *args = &ia->ap.args; 614 615 ia->read.in.fh = ff->fh; 616 ia->read.in.offset = pos; 617 ia->read.in.size = count; 618 ia->read.in.flags = file->f_flags; 619 args->opcode = opcode; 620 args->nodeid = ff->nodeid; 621 args->in_numargs = 1; 622 args->in_args[0].size = sizeof(ia->read.in); 623 args->in_args[0].value = &ia->read.in; 624 args->out_argvar = true; 625 args->out_numargs = 1; 626 args->out_args[0].size = count; 627 args->zero_copy = ff->open_flags & FOPEN_IO_URING_ZERO_COPY; 628 } 629 630 static void fuse_release_user_pages(struct fuse_args_pages *ap, ssize_t nres, 631 bool should_dirty) 632 { 633 unsigned int i; 634 635 for (i = 0; i < ap->num_folios; i++) { 636 if (should_dirty) 637 folio_mark_dirty_lock(ap->folios[i]); 638 if (ap->args.is_pinned) 639 unpin_folio(ap->folios[i]); 640 } 641 642 if (nres > 0 && ap->args.invalidate_vmap) 643 invalidate_kernel_vmap_range(ap->args.vmap_base, nres); 644 } 645 646 static void fuse_io_release(struct kref *kref) 647 { 648 kfree(container_of(kref, struct fuse_io_priv, refcnt)); 649 } 650 651 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io) 652 { 653 if (io->err) 654 return io->err; 655 656 if (io->bytes >= 0 && io->write) 657 return -EIO; 658 659 return io->bytes < 0 ? io->size : io->bytes; 660 } 661 662 static void fuse_aio_invalidate_worker(struct work_struct *work) 663 { 664 struct fuse_io_priv *io = container_of(work, struct fuse_io_priv, work); 665 struct address_space *mapping = io->iocb->ki_filp->f_mapping; 666 ssize_t res = fuse_get_res_by_io(io); 667 pgoff_t start = io->offset >> PAGE_SHIFT; 668 pgoff_t end = (io->offset + res - 1) >> PAGE_SHIFT; 669 670 invalidate_inode_pages2_range(mapping, start, end); 671 io->iocb->ki_complete(io->iocb, res); 672 kref_put(&io->refcnt, fuse_io_release); 673 } 674 675 /* 676 * In case of short read, the caller sets 'pos' to the position of 677 * actual end of fuse request in IO request. Otherwise, if bytes_requested 678 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1. 679 * 680 * An example: 681 * User requested DIO read of 64K. It was split into two 32K fuse requests, 682 * both submitted asynchronously. The first of them was ACKed by userspace as 683 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The 684 * second request was ACKed as short, e.g. only 1K was read, resulting in 685 * pos == 33K. 686 * 687 * Thus, when all fuse requests are completed, the minimal non-negative 'pos' 688 * will be equal to the length of the longest contiguous fragment of 689 * transferred data starting from the beginning of IO request. 690 */ 691 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos) 692 { 693 int left; 694 695 spin_lock(&io->lock); 696 if (err) 697 io->err = io->err ? : err; 698 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes)) 699 io->bytes = pos; 700 701 left = --io->reqs; 702 if (!left && io->blocking) 703 complete(io->done); 704 spin_unlock(&io->lock); 705 706 if (!left && !io->blocking) { 707 struct inode *inode = file_inode(io->iocb->ki_filp); 708 struct address_space *mapping = io->iocb->ki_filp->f_mapping; 709 ssize_t res = fuse_get_res_by_io(io); 710 711 if (res >= 0 && io->write) { 712 struct fuse_conn *fc = get_fuse_conn(inode); 713 struct fuse_inode *fi = get_fuse_inode(inode); 714 715 spin_lock(&fi->lock); 716 fi->attr_version = atomic64_inc_return(&fc->attr_version); 717 spin_unlock(&fi->lock); 718 } 719 720 if (io->write && res > 0 && mapping->nrpages) { 721 /* 722 * As in generic_file_direct_write(), invalidate after the 723 * write, to invalidate read-ahead cache that may have competed 724 * with the write. 725 */ 726 INIT_WORK(&io->work, fuse_aio_invalidate_worker); 727 queue_work(inode->i_sb->s_dio_done_wq, &io->work); 728 return; 729 } 730 731 io->iocb->ki_complete(io->iocb, res); 732 } 733 734 kref_put(&io->refcnt, fuse_io_release); 735 } 736 737 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io, 738 unsigned int nfolios) 739 { 740 struct fuse_io_args *ia; 741 742 ia = kzalloc_obj(*ia); 743 if (ia) { 744 ia->io = io; 745 ia->ap.folios = fuse_folios_alloc(nfolios, GFP_KERNEL, 746 &ia->ap.descs); 747 if (!ia->ap.folios) { 748 kfree(ia); 749 ia = NULL; 750 } 751 } 752 return ia; 753 } 754 755 static void fuse_io_free(struct fuse_io_args *ia) 756 { 757 kfree(ia->ap.folios); 758 kfree(ia); 759 } 760 761 static void fuse_aio_complete_req(struct fuse_args *args, int err) 762 { 763 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args); 764 struct fuse_io_priv *io = ia->io; 765 ssize_t pos = -1; 766 size_t nres; 767 768 if (err) { 769 /* Nothing */ 770 } else if (io->write) { 771 if (ia->write.out.size > ia->write.in.size) { 772 err = -EIO; 773 } else { 774 nres = ia->write.out.size; 775 if (ia->write.in.size != ia->write.out.size) 776 pos = ia->write.in.offset - io->offset + 777 ia->write.out.size; 778 } 779 } else { 780 u32 outsize = args->out_args[0].size; 781 782 nres = outsize; 783 if (ia->read.in.size != outsize) 784 pos = ia->read.in.offset - io->offset + outsize; 785 } 786 787 fuse_release_user_pages(&ia->ap, err ?: nres, io->should_dirty); 788 789 fuse_aio_complete(io, err, pos); 790 fuse_io_free(ia); 791 } 792 793 static ssize_t fuse_async_req_send(struct fuse_mount *fm, 794 struct fuse_io_args *ia, size_t num_bytes) 795 { 796 ssize_t err; 797 struct fuse_io_priv *io = ia->io; 798 799 spin_lock(&io->lock); 800 kref_get(&io->refcnt); 801 io->size += num_bytes; 802 io->reqs++; 803 spin_unlock(&io->lock); 804 805 ia->ap.args.end = fuse_aio_complete_req; 806 ia->ap.args.may_block = io->should_dirty; 807 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL); 808 if (err) 809 fuse_aio_complete_req(&ia->ap.args, err); 810 811 return num_bytes; 812 } 813 814 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count, 815 fl_owner_t owner) 816 { 817 struct file *file = ia->io->iocb->ki_filp; 818 struct fuse_file *ff = file->private_data; 819 struct fuse_mount *fm = ff->fm; 820 821 fuse_read_args_fill(ia, file, pos, count, FUSE_READ); 822 if (owner != NULL) { 823 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER; 824 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner); 825 } 826 827 if (ia->io->async) 828 return fuse_async_req_send(fm, ia, count); 829 830 return fuse_simple_request(fm, &ia->ap.args); 831 } 832 833 static void fuse_read_update_size(struct inode *inode, loff_t size, 834 u64 attr_ver) 835 { 836 struct fuse_conn *fc = get_fuse_conn(inode); 837 struct fuse_inode *fi = get_fuse_inode(inode); 838 839 spin_lock(&fi->lock); 840 if (attr_ver >= fi->attr_version && size < inode->i_size && 841 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { 842 fi->attr_version = atomic64_inc_return(&fc->attr_version); 843 i_size_write(inode, size); 844 } 845 spin_unlock(&fi->lock); 846 } 847 848 /* 849 * Position of the first byte covered by the folios in @ap. 850 * 851 * The first folio may be read partially, so the descriptor offset is part of 852 * the position. 853 */ 854 static loff_t fuse_folios_pos(struct fuse_args_pages *ap) 855 { 856 return folio_pos(ap->folios[0]) + ap->descs[0].offset; 857 } 858 859 static void fuse_short_read(struct inode *inode, struct fuse_io_args *ia, 860 size_t num_read) 861 { 862 struct fuse_conn *fc = get_fuse_conn(inode); 863 864 /* 865 * If writeback_cache is enabled, a short read means there's a hole in 866 * the file. Some data after the hole is in page cache, but has not 867 * reached the client fs yet. So the hole is not present there. 868 */ 869 if (!fc->writeback_cache) { 870 /* 871 * Derive EOF from the offset the request was sent with, which 872 * is not necessarily the start of the first folio. 873 */ 874 loff_t pos = ia->read.in.offset + num_read; 875 876 fuse_read_update_size(inode, pos, ia->read.attr_ver); 877 } 878 } 879 880 static int fuse_do_readfolio(struct file *file, struct folio *folio, 881 size_t off, size_t len) 882 { 883 struct inode *inode = folio->mapping->host; 884 struct fuse_mount *fm = get_fuse_mount(inode); 885 struct fuse_folio_desc desc = { 886 .offset = off, 887 .length = len, 888 }; 889 struct fuse_io_args ia = { 890 .ap.args.page_zeroing = true, 891 .ap.args.out_pages = true, 892 .ap.num_folios = 1, 893 .ap.folios = &folio, 894 .ap.descs = &desc, 895 }; 896 loff_t pos = fuse_folios_pos(&ia.ap); 897 ssize_t res; 898 899 ia.read.attr_ver = fuse_get_attr_version(fm->fc); 900 901 /* 902 * Don't overflow end offset. 903 * 904 * Ask the server for len - 1 bytes. desc.length still holds the full 905 * length. When the reply comes back, it will be one byte shorter than 906 * desc.length and fuse_copy_folios() will zero that last byte. 907 * 908 * For this reason, desc.length must not be decremented too. The caller 909 * reports the full length to iomap_finish_folio_read(), which marks 910 * every block it covers uptodate. Shortening the descriptor would 911 * suppress zeroing and leave the last byte holding stale data. 912 */ 913 if (pos + (len - 1) == LLONG_MAX) 914 len--; 915 916 fuse_read_args_fill(&ia, file, pos, len, FUSE_READ); 917 res = fuse_simple_request(fm, &ia.ap.args); 918 if (res < 0) 919 return res; 920 /* 921 * Short read means EOF. If file size is larger, truncate it 922 */ 923 if (res < len) 924 fuse_short_read(inode, &ia, res); 925 926 return 0; 927 } 928 929 static int fuse_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 930 unsigned int flags, struct iomap *iomap, 931 struct iomap *srcmap) 932 { 933 iomap->type = IOMAP_MAPPED; 934 iomap->length = length; 935 iomap->offset = offset; 936 return 0; 937 } 938 939 static DEFINE_IOMAP_ITER_NEXT(fuse_iomap_next, fuse_iomap_begin); 940 941 struct fuse_fill_read_data { 942 struct file *file; 943 944 /* Fields below are used if sending the read request asynchronously */ 945 struct fuse_conn *fc; 946 struct fuse_io_args *ia; 947 unsigned int nr_bytes; 948 }; 949 950 /* forward declarations */ 951 static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos, 952 unsigned len, struct fuse_args_pages *ap, 953 unsigned cur_bytes, bool write); 954 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file, 955 unsigned int count, bool async); 956 957 static int fuse_handle_readahead(struct folio *folio, 958 struct readahead_control *rac, 959 struct fuse_fill_read_data *data, loff_t pos, 960 size_t len) 961 { 962 struct fuse_io_args *ia = data->ia; 963 size_t off = offset_in_folio(folio, pos); 964 struct fuse_conn *fc = data->fc; 965 struct fuse_args_pages *ap; 966 unsigned int nr_pages; 967 968 if (ia && fuse_folios_need_send(fc, pos, len, &ia->ap, data->nr_bytes, 969 false)) { 970 fuse_send_readpages(ia, data->file, data->nr_bytes, 971 fc->async_read); 972 data->nr_bytes = 0; 973 data->ia = NULL; 974 ia = NULL; 975 } 976 if (!ia) { 977 if (fuse_chan_num_background(fc->chan) >= fc->congestion_threshold && 978 rac->ra->async_size >= readahead_count(rac)) 979 /* 980 * Congested and only async pages left, so skip the 981 * rest. 982 */ 983 return -EAGAIN; 984 985 nr_pages = min(fc->max_pages, readahead_count(rac)); 986 data->ia = fuse_io_alloc(NULL, nr_pages); 987 if (!data->ia) 988 return -ENOMEM; 989 ia = data->ia; 990 } 991 folio_get(folio); 992 ap = &ia->ap; 993 ap->folios[ap->num_folios] = folio; 994 ap->descs[ap->num_folios].offset = off; 995 ap->descs[ap->num_folios].length = len; 996 data->nr_bytes += len; 997 ap->num_folios++; 998 999 return 0; 1000 } 1001 1002 static int fuse_iomap_read_folio_range_async(const struct iomap_iter *iter, 1003 struct iomap_read_folio_ctx *ctx, 1004 size_t len) 1005 { 1006 struct fuse_fill_read_data *data = ctx->read_ctx; 1007 struct folio *folio = ctx->cur_folio; 1008 loff_t pos = iter->pos; 1009 size_t off = offset_in_folio(folio, pos); 1010 struct file *file = data->file; 1011 int ret; 1012 1013 if (ctx->rac) { 1014 ret = fuse_handle_readahead(folio, ctx->rac, data, pos, len); 1015 } else { 1016 /* 1017 * for non-readahead read requests, do reads synchronously 1018 * since it's not guaranteed that the server can handle 1019 * out-of-order reads 1020 */ 1021 ret = fuse_do_readfolio(file, folio, off, len); 1022 if (!ret) 1023 iomap_finish_folio_read(folio, off, len, ret); 1024 } 1025 return ret; 1026 } 1027 1028 static const struct iomap_read_ops fuse_iomap_read_ops = { 1029 .read_folio_range = fuse_iomap_read_folio_range_async, 1030 }; 1031 1032 static int fuse_read_folio(struct file *file, struct folio *folio) 1033 { 1034 struct inode *inode = folio->mapping->host; 1035 struct fuse_fill_read_data data = { 1036 .file = file, 1037 }; 1038 struct iomap_read_folio_ctx ctx = { 1039 .cur_folio = folio, 1040 .ops = &fuse_iomap_read_ops, 1041 .read_ctx = &data, 1042 1043 }; 1044 1045 if (fuse_is_bad(inode)) { 1046 folio_unlock(folio); 1047 return -EIO; 1048 } 1049 1050 iomap_read_folio(fuse_iomap_next, &ctx, NULL); 1051 fuse_invalidate_atime(inode); 1052 return 0; 1053 } 1054 1055 static int fuse_iomap_read_folio_range(const struct iomap_iter *iter, 1056 struct folio *folio, loff_t pos, 1057 size_t len) 1058 { 1059 struct file *file = iter->private; 1060 size_t off = offset_in_folio(folio, pos); 1061 1062 return fuse_do_readfolio(file, folio, off, len); 1063 } 1064 1065 static void fuse_readpages_end(struct fuse_args *args, int err) 1066 { 1067 int i; 1068 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args); 1069 struct fuse_args_pages *ap = &ia->ap; 1070 size_t count = ia->read.in.size; 1071 size_t num_read = args->out_args[0].size; 1072 struct address_space *mapping; 1073 struct inode *inode; 1074 1075 WARN_ON_ONCE(!ap->num_folios); 1076 mapping = ap->folios[0]->mapping; 1077 inode = mapping->host; 1078 1079 /* 1080 * Short read means EOF. If file size is larger, truncate it 1081 */ 1082 if (!err && num_read < count) 1083 fuse_short_read(inode, ia, num_read); 1084 1085 fuse_invalidate_atime(inode); 1086 1087 for (i = 0; i < ap->num_folios; i++) { 1088 iomap_finish_folio_read(ap->folios[i], ap->descs[i].offset, 1089 ap->descs[i].length, err); 1090 folio_put(ap->folios[i]); 1091 } 1092 if (ia->ff) 1093 fuse_file_put(ia->ff, false); 1094 1095 fuse_io_free(ia); 1096 } 1097 1098 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file, 1099 unsigned int count, bool async) 1100 { 1101 struct fuse_file *ff = file->private_data; 1102 struct fuse_mount *fm = ff->fm; 1103 struct fuse_args_pages *ap = &ia->ap; 1104 loff_t pos = fuse_folios_pos(ap); 1105 ssize_t res; 1106 int err; 1107 1108 ap->args.out_pages = true; 1109 ap->args.page_zeroing = true; 1110 1111 /* 1112 * Don't overflow end offset. 1113 * 1114 * Ask the server for count - 1 bytes. The reply is then one byte 1115 * shorter than what the descriptor lengths add up to, so 1116 * fuse_copy_folios() zeroes the last byte when it walks the folios. 1117 * 1118 * ap->descs[] must not be decremented here. It is what 1119 * fuse_readpages_end() reports back to iomap_finish_folio_read(), and 1120 * iomap has already accounted the full descriptor length, so shortening 1121 * it would leave ifs->read_bytes_pending nonzero and the folio locked. 1122 */ 1123 if (pos + (count - 1) == LLONG_MAX) 1124 count--; 1125 WARN_ON((loff_t) (pos + count) < 0); 1126 1127 fuse_read_args_fill(ia, file, pos, count, FUSE_READ); 1128 ia->read.attr_ver = fuse_get_attr_version(fm->fc); 1129 if (async) { 1130 ia->ff = fuse_file_get(ff); 1131 ap->args.end = fuse_readpages_end; 1132 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL); 1133 if (!err) 1134 return; 1135 } else { 1136 res = fuse_simple_request(fm, &ap->args); 1137 err = res < 0 ? res : 0; 1138 } 1139 fuse_readpages_end(&ap->args, err); 1140 } 1141 1142 static void fuse_readahead(struct readahead_control *rac) 1143 { 1144 struct inode *inode = rac->mapping->host; 1145 struct fuse_conn *fc = get_fuse_conn(inode); 1146 struct fuse_fill_read_data data = { 1147 .file = rac->file, 1148 .fc = fc, 1149 }; 1150 struct iomap_read_folio_ctx ctx = { 1151 .ops = &fuse_iomap_read_ops, 1152 .rac = rac, 1153 .read_ctx = &data 1154 }; 1155 1156 if (fuse_is_bad(inode)) 1157 return; 1158 1159 iomap_readahead(fuse_iomap_next, &ctx, NULL); 1160 if (data.ia) 1161 fuse_send_readpages(data.ia, data.file, data.nr_bytes, 1162 fc->async_read); 1163 } 1164 1165 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to) 1166 { 1167 struct inode *inode = iocb->ki_filp->f_mapping->host; 1168 struct fuse_conn *fc = get_fuse_conn(inode); 1169 1170 /* 1171 * In auto invalidate mode, always update attributes on read. 1172 * Otherwise, only update if we attempt to read past EOF (to ensure 1173 * i_size is up to date). 1174 */ 1175 if (fc->auto_inval_data || 1176 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) { 1177 int err; 1178 err = fuse_update_attributes(inode, iocb->ki_filp, STATX_SIZE); 1179 if (err) 1180 return err; 1181 } 1182 1183 return generic_file_read_iter(iocb, to); 1184 } 1185 1186 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff, 1187 loff_t pos, size_t count) 1188 { 1189 struct fuse_args *args = &ia->ap.args; 1190 1191 ia->write.in.fh = ff->fh; 1192 ia->write.in.offset = pos; 1193 ia->write.in.size = count; 1194 args->opcode = FUSE_WRITE; 1195 args->nodeid = ff->nodeid; 1196 args->in_numargs = 2; 1197 if (ff->fm->fc->minor < 9) 1198 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; 1199 else 1200 args->in_args[0].size = sizeof(ia->write.in); 1201 args->in_args[0].value = &ia->write.in; 1202 args->in_args[1].size = count; 1203 args->out_numargs = 1; 1204 args->out_args[0].size = sizeof(ia->write.out); 1205 args->out_args[0].value = &ia->write.out; 1206 args->zero_copy = ff->open_flags & FOPEN_IO_URING_ZERO_COPY; 1207 } 1208 1209 static unsigned int fuse_write_flags(struct kiocb *iocb) 1210 { 1211 unsigned int flags = iocb->ki_filp->f_flags; 1212 1213 if (iocb_is_dsync(iocb)) 1214 flags |= O_DSYNC; 1215 if (iocb->ki_flags & IOCB_SYNC) 1216 flags |= O_SYNC; 1217 1218 return flags; 1219 } 1220 1221 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos, 1222 size_t count, fl_owner_t owner) 1223 { 1224 struct kiocb *iocb = ia->io->iocb; 1225 struct file *file = iocb->ki_filp; 1226 struct fuse_file *ff = file->private_data; 1227 struct fuse_mount *fm = ff->fm; 1228 struct fuse_write_in *inarg = &ia->write.in; 1229 ssize_t err; 1230 1231 fuse_write_args_fill(ia, ff, pos, count); 1232 inarg->flags = fuse_write_flags(iocb); 1233 if (owner != NULL) { 1234 inarg->write_flags |= FUSE_WRITE_LOCKOWNER; 1235 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner); 1236 } 1237 1238 if (ia->io->async) 1239 return fuse_async_req_send(fm, ia, count); 1240 1241 err = fuse_simple_request(fm, &ia->ap.args); 1242 if (!err && ia->write.out.size > count) 1243 err = -EIO; 1244 1245 return err ?: ia->write.out.size; 1246 } 1247 1248 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written) 1249 { 1250 struct fuse_conn *fc = get_fuse_conn(inode); 1251 struct fuse_inode *fi = get_fuse_inode(inode); 1252 bool ret = false; 1253 1254 spin_lock(&fi->lock); 1255 fi->attr_version = atomic64_inc_return(&fc->attr_version); 1256 if (written > 0 && pos > inode->i_size) { 1257 i_size_write(inode, pos); 1258 ret = true; 1259 } 1260 spin_unlock(&fi->lock); 1261 1262 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE); 1263 1264 return ret; 1265 } 1266 1267 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia, 1268 struct kiocb *iocb, struct inode *inode, 1269 loff_t pos, size_t count) 1270 { 1271 struct fuse_args_pages *ap = &ia->ap; 1272 struct file *file = iocb->ki_filp; 1273 struct fuse_file *ff = file->private_data; 1274 struct fuse_mount *fm = ff->fm; 1275 unsigned int i; 1276 int err; 1277 1278 for (i = 0; i < ap->num_folios; i++) 1279 folio_wait_writeback(ap->folios[i]); 1280 1281 fuse_write_args_fill(ia, ff, pos, count); 1282 ia->write.in.flags = fuse_write_flags(iocb); 1283 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID)) 1284 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID; 1285 1286 err = fuse_simple_request(fm, &ap->args); 1287 if (!err && ia->write.out.size > count) 1288 err = -EIO; 1289 1290 for (i = 0; i < ap->num_folios; i++) { 1291 struct folio *folio = ap->folios[i]; 1292 1293 if (ia->write.folio_locked && (i == ap->num_folios - 1)) 1294 folio_unlock(folio); 1295 folio_put(folio); 1296 } 1297 1298 return err; 1299 } 1300 1301 static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia, 1302 struct address_space *mapping, 1303 struct iov_iter *ii, loff_t pos, 1304 unsigned int max_folios) 1305 { 1306 struct fuse_args_pages *ap = &ia->ap; 1307 struct fuse_conn *fc = get_fuse_conn(mapping->host); 1308 size_t count = 0; 1309 unsigned int num; 1310 int err = 0; 1311 1312 num = min(iov_iter_count(ii), fc->max_write); 1313 1314 ap->args.in_pages = true; 1315 1316 while (num && ap->num_folios < max_folios) { 1317 size_t tmp; 1318 struct folio *folio; 1319 pgoff_t index = pos >> PAGE_SHIFT; 1320 unsigned int bytes; 1321 unsigned int folio_offset; 1322 1323 again: 1324 folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN, 1325 mapping_gfp_mask(mapping)); 1326 if (IS_ERR(folio)) { 1327 err = PTR_ERR(folio); 1328 break; 1329 } 1330 1331 if (mapping_writably_mapped(mapping)) 1332 flush_dcache_folio(folio); 1333 1334 folio_offset = offset_in_folio(folio, pos); 1335 bytes = min(folio_size(folio) - folio_offset, num); 1336 1337 tmp = copy_folio_from_iter_atomic(folio, folio_offset, bytes, ii); 1338 flush_dcache_folio(folio); 1339 1340 if (!tmp) { 1341 folio_unlock(folio); 1342 folio_put(folio); 1343 1344 /* 1345 * Ensure forward progress by faulting in 1346 * while not holding the folio lock: 1347 */ 1348 if (fault_in_iov_iter_readable(ii, bytes)) { 1349 err = -EFAULT; 1350 break; 1351 } 1352 1353 goto again; 1354 } 1355 1356 ap->folios[ap->num_folios] = folio; 1357 ap->descs[ap->num_folios].offset = folio_offset; 1358 ap->descs[ap->num_folios].length = tmp; 1359 ap->num_folios++; 1360 1361 count += tmp; 1362 pos += tmp; 1363 num -= tmp; 1364 1365 /* If we copied full folio, mark it uptodate */ 1366 if (tmp == folio_size(folio)) 1367 iomap_folio_mark_uptodate(folio); 1368 1369 if (folio_test_uptodate(folio)) { 1370 folio_unlock(folio); 1371 } else { 1372 ia->write.folio_locked = true; 1373 break; 1374 } 1375 if (!fc->big_writes) 1376 break; 1377 if (folio_offset + tmp != folio_size(folio)) 1378 break; 1379 } 1380 1381 return count > 0 ? count : err; 1382 } 1383 1384 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len, 1385 unsigned int max_pages) 1386 { 1387 unsigned int pages = ((pos + len - 1) >> PAGE_SHIFT) - 1388 (pos >> PAGE_SHIFT) + 1; 1389 1390 return min(pages, max_pages); 1391 } 1392 1393 static ssize_t fuse_perform_write(struct kiocb *iocb, struct iov_iter *ii) 1394 { 1395 struct address_space *mapping = iocb->ki_filp->f_mapping; 1396 struct inode *inode = mapping->host; 1397 struct fuse_conn *fc = get_fuse_conn(inode); 1398 struct fuse_inode *fi = get_fuse_inode(inode); 1399 loff_t pos = iocb->ki_pos; 1400 loff_t old_size = i_size_read(inode); 1401 int err = 0; 1402 ssize_t res = 0; 1403 1404 if (pos > old_size) 1405 truncate_pagecache_range(inode, old_size, pos - 1); 1406 1407 if (inode->i_size < pos + iov_iter_count(ii)) 1408 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1409 1410 do { 1411 ssize_t count; 1412 struct fuse_io_args ia = {}; 1413 struct fuse_args_pages *ap = &ia.ap; 1414 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii), 1415 fc->max_pages); 1416 1417 ap->folios = fuse_folios_alloc(nr_pages, GFP_KERNEL, &ap->descs); 1418 if (!ap->folios) { 1419 err = -ENOMEM; 1420 break; 1421 } 1422 1423 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages); 1424 if (count <= 0) { 1425 err = count; 1426 } else { 1427 err = fuse_send_write_pages(&ia, iocb, inode, 1428 pos, count); 1429 if (!err) { 1430 size_t num_written = ia.write.out.size; 1431 1432 res += num_written; 1433 pos += num_written; 1434 1435 /* break out of the loop on short write */ 1436 if (num_written != count) 1437 err = -EIO; 1438 } 1439 } 1440 kfree(ap->folios); 1441 } while (!err && iov_iter_count(ii)); 1442 1443 fuse_write_update_attr(inode, pos, res); 1444 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1445 1446 if (!res) 1447 return err; 1448 iocb->ki_pos += res; 1449 return res; 1450 } 1451 1452 static bool fuse_io_past_eof(struct kiocb *iocb, struct iov_iter *iter) 1453 { 1454 struct inode *inode = file_inode(iocb->ki_filp); 1455 1456 return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode); 1457 } 1458 1459 /* 1460 * @return true if an exclusive lock for direct IO writes is needed 1461 */ 1462 static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from) 1463 { 1464 struct file *file = iocb->ki_filp; 1465 struct fuse_file *ff = file->private_data; 1466 struct inode *inode = file_inode(iocb->ki_filp); 1467 struct fuse_inode *fi = get_fuse_inode(inode); 1468 1469 /* Server side has to advise that it supports parallel dio writes. */ 1470 if (!(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES)) 1471 return true; 1472 1473 /* 1474 * Append will need to know the eventual EOF - always needs an 1475 * exclusive lock. 1476 */ 1477 if (iocb->ki_flags & IOCB_APPEND) 1478 return true; 1479 1480 /* shared locks are not allowed with parallel page cache IO */ 1481 if (test_bit(FUSE_I_CACHE_IO_MODE, &fi->state)) 1482 return true; 1483 1484 /* Parallel dio beyond EOF is not supported, at least for now. */ 1485 if (fuse_io_past_eof(iocb, from)) 1486 return true; 1487 1488 return false; 1489 } 1490 1491 static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from, 1492 bool *exclusive) 1493 { 1494 struct inode *inode = file_inode(iocb->ki_filp); 1495 struct fuse_inode *fi = get_fuse_inode(inode); 1496 1497 *exclusive = fuse_dio_wr_exclusive_lock(iocb, from); 1498 if (*exclusive) { 1499 inode_lock(inode); 1500 } else { 1501 inode_lock_shared(inode); 1502 /* 1503 * New parallal dio allowed only if inode is not in caching 1504 * mode and denies new opens in caching mode. This check 1505 * should be performed only after taking shared inode lock. 1506 * Previous past eof check was without inode lock and might 1507 * have raced, so check it again. 1508 */ 1509 if (fuse_io_past_eof(iocb, from) || 1510 fuse_inode_uncached_io_start(fi, NULL) != 0) { 1511 inode_unlock_shared(inode); 1512 inode_lock(inode); 1513 *exclusive = true; 1514 } 1515 } 1516 } 1517 1518 static void fuse_dio_unlock(struct kiocb *iocb, bool exclusive) 1519 { 1520 struct inode *inode = file_inode(iocb->ki_filp); 1521 struct fuse_inode *fi = get_fuse_inode(inode); 1522 1523 if (exclusive) { 1524 inode_unlock(inode); 1525 } else { 1526 /* Allow opens in caching mode after last parallel dio end */ 1527 fuse_inode_uncached_io_end(fi); 1528 inode_unlock_shared(inode); 1529 } 1530 } 1531 1532 static const struct iomap_write_ops fuse_iomap_write_ops = { 1533 .read_folio_range = fuse_iomap_read_folio_range, 1534 }; 1535 1536 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) 1537 { 1538 struct file *file = iocb->ki_filp; 1539 const struct mnt_idmap *idmap = file_mnt_idmap(file); 1540 struct address_space *mapping = file->f_mapping; 1541 ssize_t written = 0; 1542 struct inode *inode = mapping->host; 1543 ssize_t err, count; 1544 struct fuse_conn *fc = get_fuse_conn(inode); 1545 bool writeback = false; 1546 1547 if (fc->writeback_cache) { 1548 /* Update size (EOF optimization) and mode (SUID clearing) */ 1549 err = fuse_update_attributes(mapping->host, file, 1550 STATX_SIZE | STATX_MODE); 1551 if (err) 1552 return err; 1553 1554 if (!fc->handle_killpriv_v2 || 1555 !setattr_should_drop_suidgid(idmap, file_inode(file))) 1556 writeback = true; 1557 } 1558 1559 inode_lock(inode); 1560 1561 err = count = generic_write_checks(iocb, from); 1562 if (err <= 0) 1563 goto out; 1564 1565 task_io_account_write(count); 1566 1567 err = kiocb_modified(iocb); 1568 if (err) 1569 goto out; 1570 1571 if (iocb->ki_flags & IOCB_DIRECT) { 1572 written = generic_file_direct_write(iocb, from); 1573 if (written < 0 || !iov_iter_count(from)) 1574 goto out; 1575 written = direct_write_fallback(iocb, from, written, 1576 fuse_perform_write(iocb, from)); 1577 } else if (writeback) { 1578 loff_t old_size = i_size_read(inode); 1579 1580 if (iocb->ki_pos > old_size) 1581 truncate_pagecache_range(inode, old_size, iocb->ki_pos - 1); 1582 1583 /* 1584 * Use iomap so that we can do granular uptodate reads 1585 * and granular dirty tracking for large folios. 1586 */ 1587 written = iomap_file_buffered_write(iocb, from, 1588 fuse_iomap_next, 1589 &fuse_iomap_write_ops, 1590 file); 1591 } else { 1592 written = fuse_perform_write(iocb, from); 1593 } 1594 out: 1595 inode_unlock(inode); 1596 if (written > 0) 1597 written = generic_write_sync(iocb, written); 1598 1599 return written ? written : err; 1600 } 1601 1602 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) 1603 { 1604 return (unsigned long)iter_iov(ii)->iov_base + ii->iov_offset; 1605 } 1606 1607 static inline size_t fuse_get_frag_size(const struct iov_iter *ii, 1608 size_t max_size) 1609 { 1610 return min(iov_iter_single_seg_count(ii), max_size); 1611 } 1612 1613 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, 1614 size_t *nbytesp, int write, 1615 unsigned int max_pages, 1616 bool use_pages_for_kvec_io) 1617 { 1618 bool flush_or_invalidate = false; 1619 unsigned int nr_pages = 0; 1620 size_t nbytes = 0; /* # bytes already packed in req */ 1621 ssize_t ret = 0; 1622 1623 /* Special case for kernel I/O: can copy directly into the buffer. 1624 * However if the implementation of fuse_conn requires pages instead of 1625 * pointer (e.g., virtio-fs), use iov_iter_extract_pages() instead. 1626 */ 1627 if (iov_iter_is_kvec(ii)) { 1628 void *user_addr = (void *)fuse_get_user_addr(ii); 1629 1630 if (!use_pages_for_kvec_io) { 1631 size_t frag_size = fuse_get_frag_size(ii, *nbytesp); 1632 1633 if (write) 1634 ap->args.in_args[1].value = user_addr; 1635 else 1636 ap->args.out_args[0].value = user_addr; 1637 1638 iov_iter_advance(ii, frag_size); 1639 *nbytesp = frag_size; 1640 return 0; 1641 } 1642 1643 if (is_vmalloc_addr(user_addr)) { 1644 ap->args.vmap_base = user_addr; 1645 flush_or_invalidate = true; 1646 } 1647 } 1648 1649 /* 1650 * Until there is support for iov_iter_extract_folios(), we have to 1651 * manually extract pages using iov_iter_extract_pages() and then 1652 * copy that to a folios array. 1653 */ 1654 struct page **pages = kzalloc_objs(struct page *, max_pages); 1655 if (!pages) { 1656 ret = -ENOMEM; 1657 goto out; 1658 } 1659 1660 while (nbytes < *nbytesp && nr_pages < max_pages) { 1661 unsigned nfolios, i; 1662 size_t start; 1663 1664 ret = iov_iter_extract_pages(ii, &pages, 1665 *nbytesp - nbytes, 1666 max_pages - nr_pages, 1667 0, &start); 1668 if (ret < 0) 1669 break; 1670 1671 nbytes += ret; 1672 1673 nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE); 1674 1675 for (i = 0; i < nfolios; i++) { 1676 struct folio *folio = page_folio(pages[i]); 1677 unsigned int offset = start + 1678 (folio_page_idx(folio, pages[i]) << PAGE_SHIFT); 1679 unsigned int len = umin(ret, PAGE_SIZE - start); 1680 1681 ap->descs[ap->num_folios].offset = offset; 1682 ap->descs[ap->num_folios].length = len; 1683 ap->folios[ap->num_folios] = folio; 1684 start = 0; 1685 ret -= len; 1686 ap->num_folios++; 1687 } 1688 1689 nr_pages += nfolios; 1690 } 1691 kfree(pages); 1692 1693 if (write && flush_or_invalidate) 1694 flush_kernel_vmap_range(ap->args.vmap_base, nbytes); 1695 1696 ap->args.invalidate_vmap = !write && flush_or_invalidate; 1697 ap->args.is_pinned = iov_iter_extract_will_pin(ii); 1698 ap->args.user_pages = true; 1699 if (write) 1700 ap->args.in_pages = true; 1701 else 1702 ap->args.out_pages = true; 1703 1704 out: 1705 *nbytesp = nbytes; 1706 1707 return ret < 0 ? ret : 0; 1708 } 1709 1710 ssize_t fuse_do_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, 1711 loff_t *ppos, int flags) 1712 { 1713 int write = flags & FUSE_DIO_WRITE; 1714 int cuse = flags & FUSE_DIO_CUSE; 1715 struct file *file = io->iocb->ki_filp; 1716 struct address_space *mapping = file->f_mapping; 1717 struct inode *inode = mapping->host; 1718 struct fuse_file *ff = file->private_data; 1719 struct fuse_conn *fc = ff->fm->fc; 1720 size_t nmax = write ? fc->max_write : fc->max_read; 1721 loff_t pos = *ppos; 1722 size_t count = iov_iter_count(iter); 1723 pgoff_t idx_from = pos >> PAGE_SHIFT; 1724 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT; 1725 ssize_t res = 0; 1726 int err = 0; 1727 struct fuse_io_args *ia; 1728 unsigned int max_pages; 1729 bool fopen_direct_io = ff->open_flags & FOPEN_DIRECT_IO; 1730 1731 max_pages = iov_iter_npages(iter, fc->max_pages); 1732 ia = fuse_io_alloc(io, max_pages); 1733 if (!ia) 1734 return -ENOMEM; 1735 1736 if (fopen_direct_io) { 1737 res = filemap_write_and_wait_range(mapping, pos, pos + count - 1); 1738 if (res) { 1739 fuse_io_free(ia); 1740 return res; 1741 } 1742 } 1743 if (!cuse && filemap_range_has_writeback(mapping, pos, (pos + count - 1))) { 1744 if (!write) 1745 inode_lock(inode); 1746 fuse_sync_writes(inode); 1747 if (!write) 1748 inode_unlock(inode); 1749 } 1750 1751 if (fopen_direct_io && write) { 1752 res = invalidate_inode_pages2_range(mapping, idx_from, idx_to); 1753 if (res) { 1754 fuse_io_free(ia); 1755 return res; 1756 } 1757 } 1758 1759 io->should_dirty = !write && user_backed_iter(iter); 1760 while (count) { 1761 ssize_t nres; 1762 fl_owner_t owner = current->files; 1763 size_t nbytes = min(count, nmax); 1764 1765 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write, 1766 max_pages, fc->use_pages_for_kvec_io); 1767 if (err && !nbytes) 1768 break; 1769 1770 if (write) { 1771 if (!capable(CAP_FSETID)) 1772 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID; 1773 1774 nres = fuse_send_write(ia, pos, nbytes, owner); 1775 } else { 1776 nres = fuse_send_read(ia, pos, nbytes, owner); 1777 } 1778 1779 if (!io->async || nres < 0) { 1780 fuse_release_user_pages(&ia->ap, nres, io->should_dirty); 1781 fuse_io_free(ia); 1782 } 1783 ia = NULL; 1784 if (nres < 0) { 1785 iov_iter_revert(iter, nbytes); 1786 err = nres; 1787 break; 1788 } 1789 WARN_ON(nres > nbytes); 1790 1791 count -= nres; 1792 res += nres; 1793 pos += nres; 1794 if (nres != nbytes) { 1795 iov_iter_revert(iter, nbytes - nres); 1796 break; 1797 } 1798 if (count) { 1799 max_pages = iov_iter_npages(iter, fc->max_pages); 1800 ia = fuse_io_alloc(io, max_pages); 1801 if (!ia) 1802 break; 1803 } 1804 } 1805 if (ia) 1806 fuse_io_free(ia); 1807 if (res > 0) 1808 *ppos = pos; 1809 1810 return res > 0 ? res : err; 1811 } 1812 EXPORT_SYMBOL_GPL(fuse_do_direct_io); 1813 1814 static ssize_t __fuse_direct_read(struct fuse_io_priv *io, 1815 struct iov_iter *iter, 1816 loff_t *ppos) 1817 { 1818 ssize_t res; 1819 struct inode *inode = file_inode(io->iocb->ki_filp); 1820 1821 res = fuse_do_direct_io(io, iter, ppos, 0); 1822 1823 fuse_invalidate_atime(inode); 1824 1825 return res; 1826 } 1827 1828 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter); 1829 1830 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) 1831 { 1832 ssize_t res; 1833 1834 if (!is_sync_kiocb(iocb)) { 1835 res = fuse_direct_IO(iocb, to); 1836 } else { 1837 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 1838 1839 res = __fuse_direct_read(&io, to, &iocb->ki_pos); 1840 } 1841 1842 return res; 1843 } 1844 1845 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) 1846 { 1847 struct inode *inode = file_inode(iocb->ki_filp); 1848 struct address_space *mapping = inode->i_mapping; 1849 ssize_t res; 1850 bool exclusive; 1851 1852 fuse_dio_lock(iocb, from, &exclusive); 1853 res = generic_write_checks(iocb, from); 1854 if (res > 0) { 1855 loff_t pos = iocb->ki_pos; 1856 1857 task_io_account_write(res); 1858 if (!is_sync_kiocb(iocb)) { 1859 res = fuse_direct_IO(iocb, from); 1860 } else { 1861 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 1862 1863 res = fuse_do_direct_io(&io, from, &iocb->ki_pos, 1864 FUSE_DIO_WRITE); 1865 fuse_write_update_attr(inode, iocb->ki_pos, res); 1866 } 1867 if (res > 0 && mapping->nrpages) { 1868 /* 1869 * As in generic_file_direct_write(), invalidate after 1870 * write, to invalidate read-ahead cache that may have 1871 * competed with the write. 1872 */ 1873 invalidate_inode_pages2_range(mapping, 1874 pos >> PAGE_SHIFT, 1875 (pos + res - 1) >> PAGE_SHIFT); 1876 } 1877 } 1878 fuse_dio_unlock(iocb, exclusive); 1879 1880 return res; 1881 } 1882 1883 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 1884 { 1885 struct file *file = iocb->ki_filp; 1886 struct fuse_file *ff = file->private_data; 1887 struct inode *inode = file_inode(file); 1888 1889 if (fuse_is_bad(inode)) 1890 return -EIO; 1891 1892 if (FUSE_IS_VDAX(inode)) 1893 return fuse_vdax_read_iter(iocb, to); 1894 1895 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1896 if (ff->open_flags & FOPEN_DIRECT_IO) 1897 return fuse_direct_read_iter(iocb, to); 1898 else if (fuse_is_passthrough(ff)) 1899 return fuse_passthrough_read_iter(iocb, to); 1900 else 1901 return fuse_cache_read_iter(iocb, to); 1902 } 1903 1904 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 1905 { 1906 struct file *file = iocb->ki_filp; 1907 struct fuse_file *ff = file->private_data; 1908 struct inode *inode = file_inode(file); 1909 1910 if (fuse_is_bad(inode)) 1911 return -EIO; 1912 1913 if (FUSE_IS_VDAX(inode)) 1914 return fuse_vdax_write_iter(iocb, from); 1915 1916 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1917 if (ff->open_flags & FOPEN_DIRECT_IO) 1918 return fuse_direct_write_iter(iocb, from); 1919 else if (fuse_is_passthrough(ff)) 1920 return fuse_passthrough_write_iter(iocb, from); 1921 else 1922 return fuse_cache_write_iter(iocb, from); 1923 } 1924 1925 static ssize_t fuse_splice_read(struct file *in, loff_t *ppos, 1926 struct pipe_inode_info *pipe, size_t len, 1927 unsigned int flags) 1928 { 1929 struct fuse_file *ff = in->private_data; 1930 1931 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1932 1933 if (ff->open_flags & FOPEN_DIRECT_IO) 1934 return copy_splice_read(in, ppos, pipe, len, flags); 1935 else if (fuse_is_passthrough(ff)) 1936 return fuse_passthrough_splice_read(in, ppos, pipe, len, flags); 1937 else 1938 return filemap_splice_read(in, ppos, pipe, len, flags); 1939 } 1940 1941 static ssize_t fuse_splice_write(struct pipe_inode_info *pipe, struct file *out, 1942 loff_t *ppos, size_t len, unsigned int flags) 1943 { 1944 struct fuse_file *ff = out->private_data; 1945 1946 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1947 if (fuse_is_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO)) 1948 return fuse_passthrough_splice_write(pipe, out, ppos, len, flags); 1949 else 1950 return iter_file_splice_write(pipe, out, ppos, len, flags); 1951 } 1952 1953 static void fuse_writepage_free(struct fuse_writepage_args *wpa) 1954 { 1955 struct fuse_args_pages *ap = &wpa->ia.ap; 1956 1957 if (wpa->bucket) 1958 fuse_sync_bucket_dec(wpa->bucket); 1959 1960 fuse_file_put(wpa->ia.ff, false); 1961 1962 kfree(ap->folios); 1963 kfree(wpa); 1964 } 1965 1966 static void fuse_writepage_finish(struct fuse_writepage_args *wpa) 1967 { 1968 struct fuse_args_pages *ap = &wpa->ia.ap; 1969 struct inode *inode = wpa->inode; 1970 struct fuse_inode *fi = get_fuse_inode(inode); 1971 int i; 1972 1973 for (i = 0; i < ap->num_folios; i++) 1974 /* 1975 * Benchmarks showed that ending writeback within the 1976 * scope of the fi->lock alleviates xarray lock 1977 * contention and noticeably improves performance. 1978 */ 1979 iomap_finish_folio_write(inode, ap->folios[i], 1980 ap->descs[i].length); 1981 1982 wake_up(&fi->page_waitq); 1983 } 1984 1985 /* Called under fi->lock, may release and reacquire it */ 1986 static void fuse_send_writepage(struct fuse_mount *fm, 1987 struct fuse_writepage_args *wpa, loff_t size) 1988 __releases(fi->lock) 1989 __acquires(fi->lock) 1990 { 1991 struct fuse_inode *fi = get_fuse_inode(wpa->inode); 1992 struct fuse_args_pages *ap = &wpa->ia.ap; 1993 struct fuse_write_in *inarg = &wpa->ia.write.in; 1994 struct fuse_args *args = &ap->args; 1995 __u64 data_size = 0; 1996 int err, i; 1997 1998 for (i = 0; i < ap->num_folios; i++) 1999 data_size += ap->descs[i].length; 2000 2001 fi->writectr++; 2002 if (inarg->offset + data_size <= size) { 2003 inarg->size = data_size; 2004 } else if (inarg->offset < size) { 2005 inarg->size = size - inarg->offset; 2006 } else { 2007 /* Got truncated off completely */ 2008 goto out_free; 2009 } 2010 2011 args->in_args[1].size = inarg->size; 2012 args->force = true; 2013 args->nocreds = true; 2014 2015 err = fuse_simple_background(fm, args, GFP_ATOMIC); 2016 if (err == -ENOMEM) { 2017 spin_unlock(&fi->lock); 2018 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL); 2019 spin_lock(&fi->lock); 2020 } 2021 2022 /* Fails on broken connection only */ 2023 if (unlikely(err)) 2024 goto out_free; 2025 2026 return; 2027 2028 out_free: 2029 fi->writectr--; 2030 fuse_writepage_finish(wpa); 2031 spin_unlock(&fi->lock); 2032 fuse_writepage_free(wpa); 2033 spin_lock(&fi->lock); 2034 } 2035 2036 /* 2037 * If fi->writectr is positive (no truncate or fsync going on) send 2038 * all queued writepage requests. 2039 * 2040 * Called with fi->lock 2041 */ 2042 void fuse_flush_writepages(struct inode *inode) 2043 __releases(fi->lock) 2044 __acquires(fi->lock) 2045 { 2046 struct fuse_mount *fm = get_fuse_mount(inode); 2047 struct fuse_inode *fi = get_fuse_inode(inode); 2048 loff_t crop = i_size_read(inode); 2049 struct fuse_writepage_args *wpa; 2050 2051 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { 2052 wpa = list_entry(fi->queued_writes.next, 2053 struct fuse_writepage_args, queue_entry); 2054 list_del_init(&wpa->queue_entry); 2055 fuse_send_writepage(fm, wpa, crop); 2056 } 2057 } 2058 2059 static void fuse_writepage_end(struct fuse_args *args, int error) 2060 { 2061 struct fuse_writepage_args *wpa = 2062 container_of(args, typeof(*wpa), ia.ap.args); 2063 struct inode *inode = wpa->inode; 2064 struct fuse_inode *fi = get_fuse_inode(inode); 2065 struct fuse_conn *fc = get_fuse_conn(inode); 2066 2067 mapping_set_error(inode->i_mapping, error); 2068 /* 2069 * A writeback finished and this might have updated mtime/ctime on 2070 * server making local mtime/ctime stale. Hence invalidate attrs. 2071 * Do this only if writeback_cache is not enabled. If writeback_cache 2072 * is enabled, we trust local ctime/mtime. 2073 */ 2074 if (!fc->writeback_cache) 2075 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY); 2076 spin_lock(&fi->lock); 2077 fi->writectr--; 2078 fuse_writepage_finish(wpa); 2079 spin_unlock(&fi->lock); 2080 fuse_writepage_free(wpa); 2081 } 2082 2083 static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi) 2084 { 2085 struct fuse_file *ff; 2086 2087 spin_lock(&fi->lock); 2088 ff = list_first_entry_or_null(&fi->write_files, struct fuse_file, 2089 write_entry); 2090 if (ff) 2091 fuse_file_get(ff); 2092 spin_unlock(&fi->lock); 2093 2094 return ff; 2095 } 2096 2097 static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi) 2098 { 2099 struct fuse_file *ff = __fuse_write_file_get(fi); 2100 WARN_ON(!ff); 2101 return ff; 2102 } 2103 2104 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc) 2105 { 2106 struct fuse_inode *fi = get_fuse_inode(inode); 2107 struct fuse_file *ff; 2108 int err; 2109 2110 ff = __fuse_write_file_get(fi); 2111 err = fuse_flush_times(inode, ff); 2112 if (ff) 2113 fuse_file_put(ff, false); 2114 2115 return err; 2116 } 2117 2118 static struct fuse_writepage_args *fuse_writepage_args_alloc(void) 2119 { 2120 struct fuse_writepage_args *wpa; 2121 struct fuse_args_pages *ap; 2122 2123 wpa = kzalloc_obj(*wpa, GFP_NOFS); 2124 if (wpa) { 2125 ap = &wpa->ia.ap; 2126 ap->num_folios = 0; 2127 ap->folios = fuse_folios_alloc(1, GFP_NOFS, &ap->descs); 2128 if (!ap->folios) { 2129 kfree(wpa); 2130 wpa = NULL; 2131 } 2132 } 2133 return wpa; 2134 2135 } 2136 2137 static void fuse_writepage_add_to_bucket(struct fuse_conn *fc, 2138 struct fuse_writepage_args *wpa) 2139 { 2140 if (!fc->sync_fs) 2141 return; 2142 2143 rcu_read_lock(); 2144 /* Prevent resurrection of dead bucket in unlikely race with syncfs */ 2145 do { 2146 wpa->bucket = rcu_dereference(fc->curr_bucket); 2147 } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count))); 2148 rcu_read_unlock(); 2149 } 2150 2151 static void fuse_writepage_args_page_fill(struct fuse_writepage_args *wpa, struct folio *folio, 2152 uint32_t folio_index, loff_t offset, unsigned len) 2153 { 2154 struct fuse_args_pages *ap = &wpa->ia.ap; 2155 2156 ap->folios[folio_index] = folio; 2157 ap->descs[folio_index].offset = offset; 2158 ap->descs[folio_index].length = len; 2159 } 2160 2161 static struct fuse_writepage_args *fuse_writepage_args_setup(struct folio *folio, 2162 size_t offset, 2163 struct fuse_file *ff) 2164 { 2165 struct inode *inode = folio->mapping->host; 2166 struct fuse_conn *fc = get_fuse_conn(inode); 2167 struct fuse_writepage_args *wpa; 2168 struct fuse_args_pages *ap; 2169 2170 wpa = fuse_writepage_args_alloc(); 2171 if (!wpa) 2172 return NULL; 2173 2174 fuse_writepage_add_to_bucket(fc, wpa); 2175 fuse_write_args_fill(&wpa->ia, ff, folio_pos(folio) + offset, 0); 2176 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE; 2177 wpa->inode = inode; 2178 wpa->ia.ff = ff; 2179 2180 ap = &wpa->ia.ap; 2181 ap->args.in_pages = true; 2182 ap->args.end = fuse_writepage_end; 2183 2184 return wpa; 2185 } 2186 2187 struct fuse_fill_wb_data { 2188 struct fuse_writepage_args *wpa; 2189 struct fuse_file *ff; 2190 unsigned int max_folios; 2191 /* 2192 * nr_bytes won't overflow since fuse_folios_need_send() caps 2193 * wb requests to never exceed fc->max_pages (which has an upper bound 2194 * of U16_MAX). 2195 */ 2196 unsigned int nr_bytes; 2197 }; 2198 2199 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data, 2200 unsigned int max_pages) 2201 { 2202 struct fuse_args_pages *ap = &data->wpa->ia.ap; 2203 struct folio **folios; 2204 struct fuse_folio_desc *descs; 2205 unsigned int nfolios = min_t(unsigned int, 2206 max_t(unsigned int, data->max_folios * 2, 2207 FUSE_DEFAULT_MAX_PAGES_PER_REQ), 2208 max_pages); 2209 WARN_ON(nfolios <= data->max_folios); 2210 2211 folios = fuse_folios_alloc(nfolios, GFP_NOFS, &descs); 2212 if (!folios) 2213 return false; 2214 2215 memcpy(folios, ap->folios, sizeof(struct folio *) * ap->num_folios); 2216 memcpy(descs, ap->descs, sizeof(struct fuse_folio_desc) * ap->num_folios); 2217 kfree(ap->folios); 2218 ap->folios = folios; 2219 ap->descs = descs; 2220 data->max_folios = nfolios; 2221 2222 return true; 2223 } 2224 2225 static void fuse_writepages_send(struct inode *inode, 2226 struct fuse_fill_wb_data *data) 2227 { 2228 struct fuse_writepage_args *wpa = data->wpa; 2229 struct fuse_inode *fi = get_fuse_inode(inode); 2230 2231 spin_lock(&fi->lock); 2232 list_add_tail(&wpa->queue_entry, &fi->queued_writes); 2233 fuse_flush_writepages(inode); 2234 spin_unlock(&fi->lock); 2235 } 2236 2237 static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos, 2238 unsigned len, struct fuse_args_pages *ap, 2239 unsigned cur_bytes, bool write) 2240 { 2241 struct folio *prev_folio; 2242 struct fuse_folio_desc prev_desc; 2243 unsigned bytes = cur_bytes + len; 2244 loff_t prev_pos; 2245 size_t max_bytes = write ? fc->max_write : fc->max_read; 2246 2247 WARN_ON(!ap->num_folios); 2248 2249 /* Reached max pages or max folio slots */ 2250 if (ap->num_folios >= fc->max_pages) 2251 return true; 2252 2253 if (DIV_ROUND_UP(bytes, PAGE_SIZE) > fc->max_pages) 2254 return true; 2255 2256 if (bytes > max_bytes) 2257 return true; 2258 2259 /* Discontinuity */ 2260 prev_folio = ap->folios[ap->num_folios - 1]; 2261 prev_desc = ap->descs[ap->num_folios - 1]; 2262 prev_pos = folio_pos(prev_folio) + prev_desc.offset + prev_desc.length; 2263 if (prev_pos != pos) 2264 return true; 2265 2266 return false; 2267 } 2268 2269 static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, 2270 struct folio *folio, u64 pos, 2271 unsigned len, u64 end_pos) 2272 { 2273 struct fuse_fill_wb_data *data = wpc->wb_ctx; 2274 struct fuse_writepage_args *wpa = data->wpa; 2275 struct fuse_args_pages *ap = wpa ? &wpa->ia.ap : NULL; 2276 struct inode *inode = wpc->inode; 2277 struct fuse_inode *fi = get_fuse_inode(inode); 2278 struct fuse_conn *fc = get_fuse_conn(inode); 2279 loff_t offset = offset_in_folio(folio, pos); 2280 2281 WARN_ON_ONCE(!data); 2282 2283 if (!data->ff) { 2284 data->ff = fuse_write_file_get(fi); 2285 if (!data->ff) 2286 return -EIO; 2287 } 2288 2289 if (wpa) { 2290 bool send = fuse_folios_need_send(fc, pos, len, ap, 2291 data->nr_bytes, true); 2292 2293 if (!send) { 2294 /* 2295 * Need to grow the pages array? If so, did the 2296 * expansion fail? 2297 */ 2298 send = (ap->num_folios == data->max_folios) && 2299 !fuse_pages_realloc(data, fc->max_pages); 2300 } 2301 2302 if (send) { 2303 fuse_writepages_send(inode, data); 2304 data->wpa = NULL; 2305 data->nr_bytes = 0; 2306 } 2307 } 2308 2309 if (data->wpa == NULL) { 2310 wpa = fuse_writepage_args_setup(folio, offset, data->ff); 2311 if (!wpa) 2312 return -ENOMEM; 2313 fuse_file_get(wpa->ia.ff); 2314 data->max_folios = 1; 2315 ap = &wpa->ia.ap; 2316 } 2317 2318 fuse_writepage_args_page_fill(wpa, folio, ap->num_folios, 2319 offset, len); 2320 data->nr_bytes += len; 2321 2322 ap->num_folios++; 2323 if (!data->wpa) 2324 data->wpa = wpa; 2325 2326 return len; 2327 } 2328 2329 static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, 2330 int error) 2331 { 2332 struct fuse_fill_wb_data *data = wpc->wb_ctx; 2333 2334 WARN_ON_ONCE(!data); 2335 2336 if (data->wpa) { 2337 WARN_ON(!data->wpa->ia.ap.num_folios); 2338 fuse_writepages_send(wpc->inode, data); 2339 } 2340 2341 if (data->ff) 2342 fuse_file_put(data->ff, false); 2343 2344 return error; 2345 } 2346 2347 static const struct iomap_writeback_ops fuse_writeback_ops = { 2348 .writeback_range = fuse_iomap_writeback_range, 2349 .writeback_submit = fuse_iomap_writeback_submit, 2350 }; 2351 2352 static int fuse_writepages(struct address_space *mapping, 2353 struct writeback_control *wbc) 2354 { 2355 struct inode *inode = mapping->host; 2356 struct fuse_conn *fc = get_fuse_conn(inode); 2357 struct fuse_fill_wb_data data = {}; 2358 struct iomap_writepage_ctx wpc = { 2359 .inode = inode, 2360 .iomap.type = IOMAP_MAPPED, 2361 .wbc = wbc, 2362 .ops = &fuse_writeback_ops, 2363 .wb_ctx = &data, 2364 }; 2365 2366 if (fuse_is_bad(inode)) 2367 return -EIO; 2368 2369 if (wbc->sync_mode == WB_SYNC_NONE && 2370 fuse_chan_num_background(fc->chan) >= fc->congestion_threshold) 2371 return 0; 2372 2373 return iomap_writepages(&wpc); 2374 } 2375 2376 static int fuse_launder_folio(struct folio *folio) 2377 { 2378 int err = 0; 2379 struct fuse_fill_wb_data data = {}; 2380 struct iomap_writepage_ctx wpc = { 2381 .inode = folio->mapping->host, 2382 .iomap.type = IOMAP_MAPPED, 2383 .ops = &fuse_writeback_ops, 2384 .wb_ctx = &data, 2385 }; 2386 2387 if (fuse_is_bad(wpc.inode)) 2388 return -EIO; 2389 2390 if (folio_clear_dirty_for_io(folio)) { 2391 err = iomap_writeback_folio(&wpc, folio); 2392 err = fuse_iomap_writeback_submit(&wpc, err); 2393 if (!err) 2394 folio_wait_writeback(folio); 2395 } 2396 return err; 2397 } 2398 2399 /* 2400 * Write back dirty data/metadata now (there may not be any suitable 2401 * open files later for data) 2402 */ 2403 static void fuse_vma_close(struct vm_area_struct *vma) 2404 { 2405 int err; 2406 2407 err = write_inode_now(vma->vm_file->f_mapping->host, 1); 2408 mapping_set_error(vma->vm_file->f_mapping, err); 2409 } 2410 2411 /* 2412 * Wait for writeback against this page to complete before allowing it 2413 * to be marked dirty again, and hence written back again, possibly 2414 * before the previous writepage completed. 2415 * 2416 * Block here, instead of in ->writepage(), so that the userspace fs 2417 * can only block processes actually operating on the filesystem. 2418 * 2419 * Otherwise unprivileged userspace fs would be able to block 2420 * unrelated: 2421 * 2422 * - page migration 2423 * - sync(2) 2424 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER 2425 */ 2426 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf) 2427 { 2428 struct folio *folio = page_folio(vmf->page); 2429 struct inode *inode = file_inode(vmf->vma->vm_file); 2430 2431 file_update_time(vmf->vma->vm_file); 2432 folio_lock(folio); 2433 if (folio->mapping != inode->i_mapping) { 2434 folio_unlock(folio); 2435 return VM_FAULT_NOPAGE; 2436 } 2437 2438 folio_wait_writeback(folio); 2439 return VM_FAULT_LOCKED; 2440 } 2441 2442 static const struct vm_operations_struct fuse_file_vm_ops = { 2443 .close = fuse_vma_close, 2444 .fault = filemap_fault, 2445 .map_pages = filemap_map_pages, 2446 .page_mkwrite = fuse_page_mkwrite, 2447 }; 2448 2449 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) 2450 { 2451 struct fuse_file *ff = file->private_data; 2452 struct fuse_conn *fc = ff->fm->fc; 2453 struct inode *inode = file_inode(file); 2454 2455 /* DAX mmap is superior to direct_io mmap */ 2456 if (FUSE_IS_VDAX(inode)) 2457 return fuse_vdax_mmap(file, vma); 2458 2459 /* 2460 * If inode is in passthrough io mode, because it has some file open 2461 * in passthrough mode, either mmap to backing file or fail mmap, 2462 * because mixing cached mmap and passthrough io mode is not allowed. 2463 */ 2464 if (fuse_is_passthrough(ff)) 2465 return fuse_passthrough_mmap(file, vma); 2466 else if (fuse_inode_backing(get_fuse_inode(inode))) 2467 return -ENODEV; 2468 2469 /* 2470 * FOPEN_DIRECT_IO handling is special compared to O_DIRECT, 2471 * as does not allow MAP_SHARED mmap without FUSE_DIRECT_IO_ALLOW_MMAP. 2472 */ 2473 if (ff->open_flags & FOPEN_DIRECT_IO) { 2474 /* 2475 * Can't provide the coherency needed for MAP_SHARED 2476 * if FUSE_DIRECT_IO_ALLOW_MMAP isn't set. 2477 */ 2478 if ((vma->vm_flags & VM_MAYSHARE) && !fc->direct_io_allow_mmap) 2479 return -ENODEV; 2480 2481 invalidate_inode_pages2(file->f_mapping); 2482 2483 if (!(vma->vm_flags & VM_MAYSHARE)) { 2484 /* MAP_PRIVATE */ 2485 return generic_file_mmap(file, vma); 2486 } 2487 2488 /* 2489 * First mmap of direct_io file enters caching inode io mode. 2490 * Also waits for parallel dio writers to go into serial mode 2491 * (exclusive instead of shared lock). 2492 * After first mmap, the inode stays in caching io mode until 2493 * the direct_io file release. 2494 */ 2495 if (!fuse_file_cached_io_open(inode, ff)) 2496 return -ENODEV; 2497 } 2498 2499 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 2500 fuse_link_write_file(file); 2501 2502 file_accessed(file); 2503 vma->vm_ops = &fuse_file_vm_ops; 2504 return 0; 2505 } 2506 2507 static int convert_fuse_file_lock(struct fuse_conn *fc, 2508 const struct fuse_file_lock *ffl, 2509 struct file_lock *fl) 2510 { 2511 switch (ffl->type) { 2512 case F_UNLCK: 2513 break; 2514 2515 case F_RDLCK: 2516 case F_WRLCK: 2517 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || 2518 ffl->end < ffl->start) 2519 return -EIO; 2520 2521 fl->fl_start = ffl->start; 2522 fl->fl_end = ffl->end; 2523 2524 /* 2525 * Convert pid into init's pid namespace. The locks API will 2526 * translate it into the caller's pid namespace. 2527 */ 2528 rcu_read_lock(); 2529 fl->c.flc_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns); 2530 rcu_read_unlock(); 2531 break; 2532 2533 default: 2534 return -EIO; 2535 } 2536 fl->c.flc_type = ffl->type; 2537 return 0; 2538 } 2539 2540 static void fuse_lk_fill(struct fuse_args *args, struct file *file, 2541 const struct file_lock *fl, int opcode, pid_t pid, 2542 int flock, struct fuse_lk_in *inarg) 2543 { 2544 struct inode *inode = file_inode(file); 2545 struct fuse_conn *fc = get_fuse_conn(inode); 2546 struct fuse_file *ff = file->private_data; 2547 2548 memset(inarg, 0, sizeof(*inarg)); 2549 inarg->fh = ff->fh; 2550 inarg->owner = fuse_lock_owner_id(fc, fl->c.flc_owner); 2551 inarg->lk.start = fl->fl_start; 2552 inarg->lk.end = fl->fl_end; 2553 inarg->lk.type = fl->c.flc_type; 2554 inarg->lk.pid = pid; 2555 if (flock) 2556 inarg->lk_flags |= FUSE_LK_FLOCK; 2557 args->opcode = opcode; 2558 args->nodeid = get_node_id(inode); 2559 args->in_numargs = 1; 2560 args->in_args[0].size = sizeof(*inarg); 2561 args->in_args[0].value = inarg; 2562 } 2563 2564 static int fuse_getlk(struct file *file, struct file_lock *fl) 2565 { 2566 struct inode *inode = file_inode(file); 2567 struct fuse_mount *fm = get_fuse_mount(inode); 2568 FUSE_ARGS(args); 2569 struct fuse_lk_in inarg; 2570 struct fuse_lk_out outarg; 2571 int err; 2572 2573 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg); 2574 args.out_numargs = 1; 2575 args.out_args[0].size = sizeof(outarg); 2576 args.out_args[0].value = &outarg; 2577 err = fuse_simple_request(fm, &args); 2578 if (!err) 2579 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl); 2580 2581 return err; 2582 } 2583 2584 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) 2585 { 2586 struct inode *inode = file_inode(file); 2587 struct fuse_mount *fm = get_fuse_mount(inode); 2588 FUSE_ARGS(args); 2589 struct fuse_lk_in inarg; 2590 int opcode = (fl->c.flc_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; 2591 struct pid *pid = fl->c.flc_type != F_UNLCK ? task_tgid(current) : NULL; 2592 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns); 2593 int err; 2594 2595 if (fl->fl_lmops && fl->fl_lmops->lm_grant) { 2596 /* NLM needs asynchronous locks, which we don't support yet */ 2597 return -ENOLCK; 2598 } 2599 2600 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg); 2601 err = fuse_simple_request(fm, &args); 2602 2603 /* locking is restartable */ 2604 if (err == -EINTR) 2605 err = -ERESTARTSYS; 2606 2607 return err; 2608 } 2609 2610 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) 2611 { 2612 struct inode *inode = file_inode(file); 2613 struct fuse_conn *fc = get_fuse_conn(inode); 2614 int err; 2615 2616 if (cmd == F_CANCELLK) { 2617 err = 0; 2618 } else if (cmd == F_GETLK) { 2619 if (fc->no_lock) { 2620 posix_test_lock(file, fl); 2621 err = 0; 2622 } else 2623 err = fuse_getlk(file, fl); 2624 } else { 2625 if (fc->no_lock) 2626 err = posix_lock_file(file, fl, NULL); 2627 else 2628 err = fuse_setlk(file, fl, 0); 2629 } 2630 return err; 2631 } 2632 2633 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) 2634 { 2635 struct inode *inode = file_inode(file); 2636 struct fuse_conn *fc = get_fuse_conn(inode); 2637 int err; 2638 2639 if (fc->no_flock) { 2640 err = locks_lock_file_wait(file, fl); 2641 } else { 2642 struct fuse_file *ff = file->private_data; 2643 2644 /* emulate flock with POSIX locks */ 2645 err = fuse_setlk(file, fl, 1); 2646 if (!err) 2647 ff->flock = true; 2648 } 2649 2650 return err; 2651 } 2652 2653 static sector_t fuse_bmap(struct address_space *mapping, sector_t block) 2654 { 2655 struct inode *inode = mapping->host; 2656 struct fuse_mount *fm = get_fuse_mount(inode); 2657 FUSE_ARGS(args); 2658 struct fuse_bmap_in inarg; 2659 struct fuse_bmap_out outarg; 2660 int err; 2661 2662 if (!inode->i_sb->s_bdev || fm->fc->no_bmap) 2663 return 0; 2664 2665 memset(&inarg, 0, sizeof(inarg)); 2666 inarg.block = block; 2667 inarg.blocksize = inode->i_sb->s_blocksize; 2668 args.opcode = FUSE_BMAP; 2669 args.nodeid = get_node_id(inode); 2670 args.in_numargs = 1; 2671 args.in_args[0].size = sizeof(inarg); 2672 args.in_args[0].value = &inarg; 2673 args.out_numargs = 1; 2674 args.out_args[0].size = sizeof(outarg); 2675 args.out_args[0].value = &outarg; 2676 err = fuse_simple_request(fm, &args); 2677 if (err == -ENOSYS) 2678 fm->fc->no_bmap = 1; 2679 2680 return err ? 0 : outarg.block; 2681 } 2682 2683 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) 2684 { 2685 struct inode *inode = file->f_mapping->host; 2686 struct fuse_mount *fm = get_fuse_mount(inode); 2687 struct fuse_file *ff = file->private_data; 2688 FUSE_ARGS(args); 2689 struct fuse_lseek_in inarg = { 2690 .fh = ff->fh, 2691 .offset = offset, 2692 .whence = whence 2693 }; 2694 struct fuse_lseek_out outarg; 2695 int err; 2696 2697 if (fm->fc->no_lseek) 2698 goto fallback; 2699 2700 args.opcode = FUSE_LSEEK; 2701 args.nodeid = ff->nodeid; 2702 args.in_numargs = 1; 2703 args.in_args[0].size = sizeof(inarg); 2704 args.in_args[0].value = &inarg; 2705 args.out_numargs = 1; 2706 args.out_args[0].size = sizeof(outarg); 2707 args.out_args[0].value = &outarg; 2708 err = fuse_simple_request(fm, &args); 2709 if (err) { 2710 if (err == -ENOSYS) { 2711 fm->fc->no_lseek = 1; 2712 goto fallback; 2713 } 2714 return err; 2715 } 2716 2717 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes); 2718 2719 fallback: 2720 err = fuse_update_attributes(inode, file, STATX_SIZE); 2721 if (!err) 2722 return generic_file_llseek(file, offset, whence); 2723 else 2724 return err; 2725 } 2726 2727 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) 2728 { 2729 loff_t retval; 2730 struct inode *inode = file_inode(file); 2731 2732 switch (whence) { 2733 case SEEK_SET: 2734 case SEEK_CUR: 2735 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ 2736 retval = generic_file_llseek(file, offset, whence); 2737 break; 2738 case SEEK_END: 2739 inode_lock(inode); 2740 retval = fuse_update_attributes(inode, file, STATX_SIZE); 2741 if (!retval) 2742 retval = generic_file_llseek(file, offset, whence); 2743 inode_unlock(inode); 2744 break; 2745 case SEEK_HOLE: 2746 case SEEK_DATA: 2747 inode_lock(inode); 2748 retval = fuse_lseek(file, offset, whence); 2749 inode_unlock(inode); 2750 break; 2751 default: 2752 retval = -EINVAL; 2753 } 2754 2755 return retval; 2756 } 2757 2758 static void fuse_do_truncate(struct file *file) 2759 { 2760 struct inode *inode = file->f_mapping->host; 2761 struct iattr attr; 2762 2763 attr.ia_valid = ATTR_SIZE; 2764 attr.ia_size = i_size_read(inode); 2765 2766 attr.ia_file = file; 2767 attr.ia_valid |= ATTR_FILE; 2768 2769 fuse_do_setattr(file_mnt_idmap(file), file_dentry(file), &attr, file); 2770 } 2771 2772 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off) 2773 { 2774 return round_up(off, fc->max_pages << PAGE_SHIFT); 2775 } 2776 2777 static ssize_t 2778 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 2779 { 2780 DECLARE_COMPLETION_ONSTACK(wait); 2781 ssize_t ret = 0; 2782 struct file *file = iocb->ki_filp; 2783 struct fuse_file *ff = file->private_data; 2784 loff_t pos = 0; 2785 struct inode *inode; 2786 loff_t i_size; 2787 size_t count = iov_iter_count(iter), shortened = 0; 2788 loff_t offset = iocb->ki_pos; 2789 struct fuse_io_priv *io; 2790 bool async = ff->fm->fc->async_dio; 2791 2792 pos = offset; 2793 inode = file->f_mapping->host; 2794 i_size = i_size_read(inode); 2795 2796 if ((iov_iter_rw(iter) == READ) && (offset >= i_size)) 2797 return 0; 2798 2799 if ((iov_iter_rw(iter) == WRITE) && async && !inode->i_sb->s_dio_done_wq) { 2800 ret = sb_init_dio_done_wq(inode->i_sb); 2801 if (ret < 0) 2802 return ret; 2803 } 2804 2805 io = kmalloc_obj(struct fuse_io_priv); 2806 if (!io) 2807 return -ENOMEM; 2808 spin_lock_init(&io->lock); 2809 kref_init(&io->refcnt); 2810 io->reqs = 1; 2811 io->bytes = -1; 2812 io->size = 0; 2813 io->offset = offset; 2814 io->write = (iov_iter_rw(iter) == WRITE); 2815 io->err = 0; 2816 /* 2817 * By default, we want to optimize all I/Os with async request 2818 * submission to the client filesystem if supported. 2819 */ 2820 io->async = async; 2821 io->iocb = iocb; 2822 io->blocking = is_sync_kiocb(iocb); 2823 2824 /* optimization for short read */ 2825 if (io->async && !io->write && offset + count > i_size) { 2826 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset)); 2827 shortened = count - iov_iter_count(iter); 2828 count -= shortened; 2829 } 2830 2831 /* 2832 * We cannot asynchronously extend the size of a file. 2833 * In such case the aio will behave exactly like sync io. 2834 */ 2835 if ((offset + count > i_size) && io->write) 2836 io->blocking = true; 2837 2838 if (io->async && io->blocking) { 2839 /* 2840 * Additional reference to keep io around after 2841 * calling fuse_aio_complete() 2842 */ 2843 kref_get(&io->refcnt); 2844 io->done = &wait; 2845 } 2846 2847 if (iov_iter_rw(iter) == WRITE) { 2848 ret = fuse_do_direct_io(io, iter, &pos, FUSE_DIO_WRITE); 2849 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE); 2850 } else { 2851 ret = __fuse_direct_read(io, iter, &pos); 2852 } 2853 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened); 2854 2855 if (io->async) { 2856 bool blocking = io->blocking; 2857 2858 fuse_aio_complete(io, ret < 0 ? ret : 0, -1); 2859 2860 /* we have a non-extending, async request, so return */ 2861 if (!blocking) 2862 return -EIOCBQUEUED; 2863 2864 wait_for_completion(&wait); 2865 ret = fuse_get_res_by_io(io); 2866 } 2867 2868 kref_put(&io->refcnt, fuse_io_release); 2869 2870 if (iov_iter_rw(iter) == WRITE) { 2871 fuse_write_update_attr(inode, pos, ret); 2872 /* For extending writes we already hold exclusive lock */ 2873 if (ret < 0 && offset + count > i_size) 2874 fuse_do_truncate(file); 2875 } 2876 2877 return ret; 2878 } 2879 2880 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end) 2881 { 2882 int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX); 2883 2884 if (!err) 2885 fuse_sync_writes(inode); 2886 2887 return err; 2888 } 2889 2890 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, 2891 loff_t length) 2892 { 2893 struct fuse_file *ff = file->private_data; 2894 struct inode *inode = file_inode(file); 2895 struct fuse_inode *fi = get_fuse_inode(inode); 2896 struct fuse_mount *fm = ff->fm; 2897 FUSE_ARGS(args); 2898 struct fuse_fallocate_in inarg = { 2899 .fh = ff->fh, 2900 .offset = offset, 2901 .length = length, 2902 .mode = mode 2903 }; 2904 int err; 2905 bool block_faults = FUSE_IS_VDAX(inode) && 2906 (!(mode & FALLOC_FL_KEEP_SIZE) || 2907 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))); 2908 2909 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 2910 FALLOC_FL_ZERO_RANGE)) 2911 return -EOPNOTSUPP; 2912 2913 if (fm->fc->no_fallocate) 2914 return -EOPNOTSUPP; 2915 2916 /* No size extend/hole punch for DAX yet */ 2917 if (IS_DAX(inode) && !FUSE_IS_VDAX(inode)) 2918 return -EOPNOTSUPP; 2919 2920 inode_lock(inode); 2921 if (block_faults) { 2922 filemap_invalidate_lock(inode->i_mapping); 2923 err = fuse_vdax_break_layouts(inode, 0, -1); 2924 if (err) 2925 goto out; 2926 } 2927 2928 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) { 2929 loff_t endbyte = offset + length - 1; 2930 2931 err = fuse_writeback_range(inode, offset, endbyte); 2932 if (err) 2933 goto out; 2934 } 2935 2936 if (!(mode & FALLOC_FL_KEEP_SIZE) && 2937 offset + length > i_size_read(inode)) { 2938 err = inode_newsize_ok(inode, offset + length); 2939 if (err) 2940 goto out; 2941 } 2942 2943 err = file_modified(file); 2944 if (err) 2945 goto out; 2946 2947 if (!(mode & FALLOC_FL_KEEP_SIZE)) 2948 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2949 2950 args.opcode = FUSE_FALLOCATE; 2951 args.nodeid = ff->nodeid; 2952 args.in_numargs = 1; 2953 args.in_args[0].size = sizeof(inarg); 2954 args.in_args[0].value = &inarg; 2955 err = fuse_simple_request(fm, &args); 2956 if (err == -ENOSYS) { 2957 fm->fc->no_fallocate = 1; 2958 err = -EOPNOTSUPP; 2959 } 2960 if (err) 2961 goto out; 2962 2963 /* we could have extended the file */ 2964 if (!(mode & FALLOC_FL_KEEP_SIZE)) { 2965 loff_t oldsize = i_size_read(inode); 2966 2967 if (offset + length > oldsize) 2968 truncate_pagecache_range(inode, oldsize, 2969 offset + length - 1); 2970 if (fuse_write_update_attr(inode, offset + length, length)) 2971 file_update_time(file); 2972 } 2973 2974 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) 2975 truncate_pagecache_range(inode, offset, offset + length - 1); 2976 2977 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE); 2978 2979 out: 2980 if (!(mode & FALLOC_FL_KEEP_SIZE)) 2981 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2982 2983 if (block_faults) 2984 filemap_invalidate_unlock(inode->i_mapping); 2985 2986 inode_unlock(inode); 2987 2988 fuse_flush_time_update(inode); 2989 2990 return err; 2991 } 2992 2993 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in, 2994 struct file *file_out, loff_t pos_out, 2995 size_t len, unsigned int flags) 2996 { 2997 struct fuse_file *ff_in = file_in->private_data; 2998 struct fuse_file *ff_out = file_out->private_data; 2999 struct inode *inode_in = file_inode(file_in); 3000 struct inode *inode_out = file_inode(file_out); 3001 struct fuse_inode *fi_out = get_fuse_inode(inode_out); 3002 struct fuse_mount *fm = ff_in->fm; 3003 struct fuse_conn *fc = fm->fc; 3004 FUSE_ARGS(args); 3005 struct fuse_copy_file_range_in inarg = { 3006 .fh_in = ff_in->fh, 3007 .off_in = pos_in, 3008 .nodeid_out = ff_out->nodeid, 3009 .fh_out = ff_out->fh, 3010 .off_out = pos_out, 3011 .len = len, 3012 .flags = flags 3013 }; 3014 struct fuse_write_out outarg; 3015 struct fuse_copy_file_range_out outarg_64; 3016 u64 bytes_copied; 3017 loff_t old_size; 3018 ssize_t err; 3019 /* mark unstable when write-back is not used, and file_out gets 3020 * extended */ 3021 bool is_unstable = (!fc->writeback_cache) && 3022 ((pos_out + len) > inode_out->i_size); 3023 3024 if (fc->no_copy_file_range) 3025 return -EOPNOTSUPP; 3026 3027 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) 3028 return -EXDEV; 3029 3030 inode_lock(inode_in); 3031 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1); 3032 inode_unlock(inode_in); 3033 if (err) 3034 return err; 3035 3036 inode_lock(inode_out); 3037 3038 err = file_modified(file_out); 3039 if (err) 3040 goto out; 3041 3042 /* 3043 * Copying to an offset past EOF extends the file over a hole. Drop the 3044 * cache over the newly exposed range before the new size is published. 3045 */ 3046 old_size = i_size_read(inode_out); 3047 if (pos_out > old_size) 3048 truncate_pagecache_range(inode_out, old_size, pos_out - 1); 3049 3050 /* 3051 * Write out dirty pages in the destination file before sending the COPY 3052 * request to userspace. After the request is completed, drop the 3053 * folios covering the copied range from the cache, since these contain 3054 * stale data at that point. 3055 * 3056 * This should be mostly correct, but if the COPY writes to a partial 3057 * folio (at the start or end) and the parts not covered by the COPY are 3058 * written through a memory map after calling fuse_writeback_range(), 3059 * then that folio is laundered before it is dropped, so the memory map 3060 * modifications are written back over the range the server just copied 3061 * into and the copied data is lost. 3062 * 3063 * It is unlikely that someone would rely on such mixed style 3064 * modifications. Yet this does give less guarantees than if the 3065 * copying was performed with write(2). 3066 * 3067 * To fix this a mapping->invalidate_lock could be used to prevent new 3068 * faults while the copy is ongoing. 3069 */ 3070 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1); 3071 if (err) 3072 goto out; 3073 3074 if (is_unstable) 3075 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); 3076 3077 args.opcode = FUSE_COPY_FILE_RANGE_64; 3078 args.nodeid = ff_in->nodeid; 3079 args.in_numargs = 1; 3080 args.in_args[0].size = sizeof(inarg); 3081 args.in_args[0].value = &inarg; 3082 args.out_numargs = 1; 3083 args.out_args[0].size = sizeof(outarg_64); 3084 args.out_args[0].value = &outarg_64; 3085 if (fc->no_copy_file_range_64) { 3086 fallback: 3087 /* Fall back to old op that can't handle large copy length */ 3088 args.opcode = FUSE_COPY_FILE_RANGE; 3089 args.out_args[0].size = sizeof(outarg); 3090 args.out_args[0].value = &outarg; 3091 inarg.len = len = min_t(size_t, len, UINT_MAX & PAGE_MASK); 3092 } 3093 err = fuse_simple_request(fm, &args); 3094 if (err == -ENOSYS) { 3095 if (args.opcode == FUSE_COPY_FILE_RANGE) { 3096 fc->no_copy_file_range = 1; 3097 err = -EOPNOTSUPP; 3098 } else { 3099 fc->no_copy_file_range_64 = 1; 3100 goto fallback; 3101 } 3102 } 3103 if (err) 3104 goto out; 3105 3106 bytes_copied = args.opcode == FUSE_COPY_FILE_RANGE ? 3107 outarg.size : outarg_64.bytes_copied; 3108 3109 if (bytes_copied > len) { 3110 err = -EIO; 3111 goto out; 3112 } 3113 3114 if (bytes_copied) 3115 invalidate_inode_pages2_range(inode_out->i_mapping, 3116 pos_out >> PAGE_SHIFT, 3117 (pos_out + bytes_copied - 1) >> PAGE_SHIFT); 3118 3119 file_update_time(file_out); 3120 fuse_write_update_attr(inode_out, pos_out + bytes_copied, bytes_copied); 3121 3122 err = bytes_copied; 3123 out: 3124 if (is_unstable) 3125 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); 3126 3127 inode_unlock(inode_out); 3128 file_accessed(file_in); 3129 3130 fuse_flush_time_update(inode_out); 3131 3132 return err; 3133 } 3134 3135 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off, 3136 struct file *dst_file, loff_t dst_off, 3137 size_t len, unsigned int flags) 3138 { 3139 ssize_t ret; 3140 3141 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off, 3142 len, flags); 3143 3144 if (ret == -EOPNOTSUPP || ret == -EXDEV) 3145 ret = splice_copy_file_range(src_file, src_off, dst_file, 3146 dst_off, len); 3147 return ret; 3148 } 3149 3150 static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice) 3151 { 3152 struct fuse_file *ff = file->private_data; 3153 3154 if (fuse_is_passthrough(ff)) 3155 return fuse_passthrough_fadvise(file, offset, len, advice); 3156 3157 return generic_fadvise(file, offset, len, advice); 3158 } 3159 3160 static const struct file_operations fuse_file_operations = { 3161 .llseek = fuse_file_llseek, 3162 .read_iter = fuse_file_read_iter, 3163 .write_iter = fuse_file_write_iter, 3164 .mmap = fuse_file_mmap, 3165 .open = fuse_open, 3166 .flush = fuse_flush, 3167 .release = fuse_release, 3168 .fsync = fuse_fsync, 3169 .lock = fuse_file_lock, 3170 .get_unmapped_area = thp_get_unmapped_area, 3171 .flock = fuse_file_flock, 3172 .splice_read = fuse_splice_read, 3173 .splice_write = fuse_splice_write, 3174 .unlocked_ioctl = fuse_file_ioctl, 3175 .compat_ioctl = fuse_file_compat_ioctl, 3176 .poll = fuse_file_poll, 3177 .fallocate = fuse_file_fallocate, 3178 .copy_file_range = fuse_copy_file_range, 3179 .setlease = generic_setlease, 3180 .fadvise = fuse_file_fadvise, 3181 }; 3182 3183 static const struct address_space_operations fuse_file_aops = { 3184 .read_folio = fuse_read_folio, 3185 .readahead = fuse_readahead, 3186 .writepages = fuse_writepages, 3187 .launder_folio = fuse_launder_folio, 3188 .dirty_folio = iomap_dirty_folio, 3189 .release_folio = iomap_release_folio, 3190 .invalidate_folio = iomap_invalidate_folio, 3191 .is_partially_uptodate = iomap_is_partially_uptodate, 3192 .migrate_folio = filemap_migrate_folio, 3193 .bmap = fuse_bmap, 3194 .direct_IO = fuse_direct_IO, 3195 }; 3196 3197 void fuse_init_file_inode(struct inode *inode, unsigned int flags) 3198 { 3199 struct fuse_inode *fi = get_fuse_inode(inode); 3200 struct fuse_conn *fc = get_fuse_conn(inode); 3201 unsigned int max_folio_pages; 3202 3203 inode->i_fop = &fuse_file_operations; 3204 inode->i_data.a_ops = &fuse_file_aops; 3205 if (fc->writeback_cache) 3206 mapping_set_writeback_may_deadlock_on_reclaim(&inode->i_data); 3207 3208 INIT_LIST_HEAD(&fi->write_files); 3209 INIT_LIST_HEAD(&fi->queued_writes); 3210 fi->writectr = 0; 3211 fi->iocachectr = 0; 3212 init_waitqueue_head(&fi->page_waitq); 3213 init_waitqueue_head(&fi->direct_io_waitq); 3214 3215 if (IS_ENABLED(CONFIG_FUSE_VDAX)) 3216 fuse_vdax_inode_init(inode, flags); 3217 3218 if (FUSE_IS_VDAX(inode)) 3219 return; 3220 3221 /* 3222 * A folio is never split across requests so cap the order so that one 3223 * always fits in a single request. 3224 */ 3225 max_folio_pages = min3(fc->max_write >> PAGE_SHIFT, 3226 fc->max_read >> PAGE_SHIFT, fc->max_pages); 3227 3228 if (max_folio_pages) 3229 mapping_set_folio_order_range(inode->i_mapping, 0, 3230 ilog2(max_folio_pages)); 3231 } 3232