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, fuse_inode_backing(fi)); 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 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read, 849 struct fuse_args_pages *ap) 850 { 851 struct fuse_conn *fc = get_fuse_conn(inode); 852 853 /* 854 * If writeback_cache is enabled, a short read means there's a hole in 855 * the file. Some data after the hole is in page cache, but has not 856 * reached the client fs yet. So the hole is not present there. 857 */ 858 if (!fc->writeback_cache) { 859 loff_t pos = folio_pos(ap->folios[0]) + num_read; 860 fuse_read_update_size(inode, pos, attr_ver); 861 } 862 } 863 864 static int fuse_do_readfolio(struct file *file, struct folio *folio, 865 size_t off, size_t len) 866 { 867 struct inode *inode = folio->mapping->host; 868 struct fuse_mount *fm = get_fuse_mount(inode); 869 loff_t pos = folio_pos(folio) + off; 870 struct fuse_folio_desc desc = { 871 .offset = off, 872 .length = len, 873 }; 874 struct fuse_io_args ia = { 875 .ap.args.page_zeroing = true, 876 .ap.args.out_pages = true, 877 .ap.num_folios = 1, 878 .ap.folios = &folio, 879 .ap.descs = &desc, 880 }; 881 ssize_t res; 882 u64 attr_ver; 883 884 attr_ver = fuse_get_attr_version(fm->fc); 885 886 /* 887 * Don't overflow end offset. 888 * 889 * Ask the server for len - 1 bytes. desc.length still holds the full 890 * length. When the reply comes back, it will be one byte shorter than 891 * desc.length and fuse_copy_folios() will zero that last byte. 892 * 893 * For this reason, desc.length must not be decremented too. The caller 894 * reports the full length to iomap_finish_folio_read(), which marks 895 * every block it covers uptodate. Shortening the descriptor would 896 * suppress zeroing and leave the last byte holding stale data. 897 */ 898 if (pos + (len - 1) == LLONG_MAX) 899 len--; 900 901 fuse_read_args_fill(&ia, file, pos, len, FUSE_READ); 902 res = fuse_simple_request(fm, &ia.ap.args); 903 if (res < 0) 904 return res; 905 /* 906 * Short read means EOF. If file size is larger, truncate it 907 */ 908 if (res < len) 909 fuse_short_read(inode, attr_ver, res, &ia.ap); 910 911 return 0; 912 } 913 914 static int fuse_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 915 unsigned int flags, struct iomap *iomap, 916 struct iomap *srcmap) 917 { 918 iomap->type = IOMAP_MAPPED; 919 iomap->length = length; 920 iomap->offset = offset; 921 return 0; 922 } 923 924 static DEFINE_IOMAP_ITER_NEXT(fuse_iomap_next, fuse_iomap_begin); 925 926 static const struct iomap_ops fuse_iomap_ops = { 927 .iomap_next = fuse_iomap_next, 928 }; 929 930 struct fuse_fill_read_data { 931 struct file *file; 932 933 /* Fields below are used if sending the read request asynchronously */ 934 struct fuse_conn *fc; 935 struct fuse_io_args *ia; 936 unsigned int nr_bytes; 937 }; 938 939 /* forward declarations */ 940 static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos, 941 unsigned len, struct fuse_args_pages *ap, 942 unsigned cur_bytes, bool write); 943 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file, 944 unsigned int count, bool async); 945 946 static int fuse_handle_readahead(struct folio *folio, 947 struct readahead_control *rac, 948 struct fuse_fill_read_data *data, loff_t pos, 949 size_t len) 950 { 951 struct fuse_io_args *ia = data->ia; 952 size_t off = offset_in_folio(folio, pos); 953 struct fuse_conn *fc = data->fc; 954 struct fuse_args_pages *ap; 955 unsigned int nr_pages; 956 957 if (ia && fuse_folios_need_send(fc, pos, len, &ia->ap, data->nr_bytes, 958 false)) { 959 fuse_send_readpages(ia, data->file, data->nr_bytes, 960 fc->async_read); 961 data->nr_bytes = 0; 962 data->ia = NULL; 963 ia = NULL; 964 } 965 if (!ia) { 966 if (fuse_chan_num_background(fc->chan) >= fc->congestion_threshold && 967 rac->ra->async_size >= readahead_count(rac)) 968 /* 969 * Congested and only async pages left, so skip the 970 * rest. 971 */ 972 return -EAGAIN; 973 974 nr_pages = min(fc->max_pages, readahead_count(rac)); 975 data->ia = fuse_io_alloc(NULL, nr_pages); 976 if (!data->ia) 977 return -ENOMEM; 978 ia = data->ia; 979 } 980 folio_get(folio); 981 ap = &ia->ap; 982 ap->folios[ap->num_folios] = folio; 983 ap->descs[ap->num_folios].offset = off; 984 ap->descs[ap->num_folios].length = len; 985 data->nr_bytes += len; 986 ap->num_folios++; 987 988 return 0; 989 } 990 991 static int fuse_iomap_read_folio_range_async(const struct iomap_iter *iter, 992 struct iomap_read_folio_ctx *ctx, 993 size_t len) 994 { 995 struct fuse_fill_read_data *data = ctx->read_ctx; 996 struct folio *folio = ctx->cur_folio; 997 loff_t pos = iter->pos; 998 size_t off = offset_in_folio(folio, pos); 999 struct file *file = data->file; 1000 int ret; 1001 1002 if (ctx->rac) { 1003 ret = fuse_handle_readahead(folio, ctx->rac, data, pos, len); 1004 } else { 1005 /* 1006 * for non-readahead read requests, do reads synchronously 1007 * since it's not guaranteed that the server can handle 1008 * out-of-order reads 1009 */ 1010 ret = fuse_do_readfolio(file, folio, off, len); 1011 if (!ret) 1012 iomap_finish_folio_read(folio, off, len, ret); 1013 } 1014 return ret; 1015 } 1016 1017 static const struct iomap_read_ops fuse_iomap_read_ops = { 1018 .read_folio_range = fuse_iomap_read_folio_range_async, 1019 }; 1020 1021 static int fuse_read_folio(struct file *file, struct folio *folio) 1022 { 1023 struct inode *inode = folio->mapping->host; 1024 struct fuse_fill_read_data data = { 1025 .file = file, 1026 }; 1027 struct iomap_read_folio_ctx ctx = { 1028 .cur_folio = folio, 1029 .ops = &fuse_iomap_read_ops, 1030 .read_ctx = &data, 1031 1032 }; 1033 1034 if (fuse_is_bad(inode)) { 1035 folio_unlock(folio); 1036 return -EIO; 1037 } 1038 1039 iomap_read_folio(&fuse_iomap_ops, &ctx, NULL); 1040 fuse_invalidate_atime(inode); 1041 return 0; 1042 } 1043 1044 static int fuse_iomap_read_folio_range(const struct iomap_iter *iter, 1045 struct folio *folio, loff_t pos, 1046 size_t len) 1047 { 1048 struct file *file = iter->private; 1049 size_t off = offset_in_folio(folio, pos); 1050 1051 return fuse_do_readfolio(file, folio, off, len); 1052 } 1053 1054 static void fuse_readpages_end(struct fuse_args *args, int err) 1055 { 1056 int i; 1057 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args); 1058 struct fuse_args_pages *ap = &ia->ap; 1059 size_t count = ia->read.in.size; 1060 size_t num_read = args->out_args[0].size; 1061 struct address_space *mapping; 1062 struct inode *inode; 1063 1064 WARN_ON_ONCE(!ap->num_folios); 1065 mapping = ap->folios[0]->mapping; 1066 inode = mapping->host; 1067 1068 /* 1069 * Short read means EOF. If file size is larger, truncate it 1070 */ 1071 if (!err && num_read < count) 1072 fuse_short_read(inode, ia->read.attr_ver, num_read, ap); 1073 1074 fuse_invalidate_atime(inode); 1075 1076 for (i = 0; i < ap->num_folios; i++) { 1077 iomap_finish_folio_read(ap->folios[i], ap->descs[i].offset, 1078 ap->descs[i].length, err); 1079 folio_put(ap->folios[i]); 1080 } 1081 if (ia->ff) 1082 fuse_file_put(ia->ff, false); 1083 1084 fuse_io_free(ia); 1085 } 1086 1087 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file, 1088 unsigned int count, bool async) 1089 { 1090 struct fuse_file *ff = file->private_data; 1091 struct fuse_mount *fm = ff->fm; 1092 struct fuse_args_pages *ap = &ia->ap; 1093 loff_t pos = folio_pos(ap->folios[0]); 1094 ssize_t res; 1095 int err; 1096 1097 ap->args.out_pages = true; 1098 ap->args.page_zeroing = true; 1099 ap->args.page_replace = true; 1100 1101 /* 1102 * Don't overflow end offset. 1103 * 1104 * Ask the server for count - 1 bytes. The reply is then one byte 1105 * shorter than what the descriptor lengths add up to, so 1106 * fuse_copy_folios() zeroes the last byte when it walks the folios. 1107 * 1108 * ap->descs[] must not be decremented here. It is what 1109 * fuse_readpages_end() reports back to iomap_finish_folio_read(), and 1110 * iomap has already accounted the full descriptor length, so shortening 1111 * it would leave ifs->read_bytes_pending nonzero and the folio locked. 1112 */ 1113 if (pos + (count - 1) == LLONG_MAX) 1114 count--; 1115 WARN_ON((loff_t) (pos + count) < 0); 1116 1117 fuse_read_args_fill(ia, file, pos, count, FUSE_READ); 1118 ia->read.attr_ver = fuse_get_attr_version(fm->fc); 1119 if (async) { 1120 ia->ff = fuse_file_get(ff); 1121 ap->args.end = fuse_readpages_end; 1122 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL); 1123 if (!err) 1124 return; 1125 } else { 1126 res = fuse_simple_request(fm, &ap->args); 1127 err = res < 0 ? res : 0; 1128 } 1129 fuse_readpages_end(&ap->args, err); 1130 } 1131 1132 static void fuse_readahead(struct readahead_control *rac) 1133 { 1134 struct inode *inode = rac->mapping->host; 1135 struct fuse_conn *fc = get_fuse_conn(inode); 1136 struct fuse_fill_read_data data = { 1137 .file = rac->file, 1138 .fc = fc, 1139 }; 1140 struct iomap_read_folio_ctx ctx = { 1141 .ops = &fuse_iomap_read_ops, 1142 .rac = rac, 1143 .read_ctx = &data 1144 }; 1145 1146 if (fuse_is_bad(inode)) 1147 return; 1148 1149 iomap_readahead(&fuse_iomap_ops, &ctx, NULL); 1150 if (data.ia) 1151 fuse_send_readpages(data.ia, data.file, data.nr_bytes, 1152 fc->async_read); 1153 } 1154 1155 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to) 1156 { 1157 struct inode *inode = iocb->ki_filp->f_mapping->host; 1158 struct fuse_conn *fc = get_fuse_conn(inode); 1159 1160 /* 1161 * In auto invalidate mode, always update attributes on read. 1162 * Otherwise, only update if we attempt to read past EOF (to ensure 1163 * i_size is up to date). 1164 */ 1165 if (fc->auto_inval_data || 1166 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) { 1167 int err; 1168 err = fuse_update_attributes(inode, iocb->ki_filp, STATX_SIZE); 1169 if (err) 1170 return err; 1171 } 1172 1173 return generic_file_read_iter(iocb, to); 1174 } 1175 1176 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff, 1177 loff_t pos, size_t count) 1178 { 1179 struct fuse_args *args = &ia->ap.args; 1180 1181 ia->write.in.fh = ff->fh; 1182 ia->write.in.offset = pos; 1183 ia->write.in.size = count; 1184 args->opcode = FUSE_WRITE; 1185 args->nodeid = ff->nodeid; 1186 args->in_numargs = 2; 1187 if (ff->fm->fc->minor < 9) 1188 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; 1189 else 1190 args->in_args[0].size = sizeof(ia->write.in); 1191 args->in_args[0].value = &ia->write.in; 1192 args->in_args[1].size = count; 1193 args->out_numargs = 1; 1194 args->out_args[0].size = sizeof(ia->write.out); 1195 args->out_args[0].value = &ia->write.out; 1196 args->zero_copy = ff->open_flags & FOPEN_IO_URING_ZERO_COPY; 1197 } 1198 1199 static unsigned int fuse_write_flags(struct kiocb *iocb) 1200 { 1201 unsigned int flags = iocb->ki_filp->f_flags; 1202 1203 if (iocb_is_dsync(iocb)) 1204 flags |= O_DSYNC; 1205 if (iocb->ki_flags & IOCB_SYNC) 1206 flags |= O_SYNC; 1207 1208 return flags; 1209 } 1210 1211 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos, 1212 size_t count, fl_owner_t owner) 1213 { 1214 struct kiocb *iocb = ia->io->iocb; 1215 struct file *file = iocb->ki_filp; 1216 struct fuse_file *ff = file->private_data; 1217 struct fuse_mount *fm = ff->fm; 1218 struct fuse_write_in *inarg = &ia->write.in; 1219 ssize_t err; 1220 1221 fuse_write_args_fill(ia, ff, pos, count); 1222 inarg->flags = fuse_write_flags(iocb); 1223 if (owner != NULL) { 1224 inarg->write_flags |= FUSE_WRITE_LOCKOWNER; 1225 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner); 1226 } 1227 1228 if (ia->io->async) 1229 return fuse_async_req_send(fm, ia, count); 1230 1231 err = fuse_simple_request(fm, &ia->ap.args); 1232 if (!err && ia->write.out.size > count) 1233 err = -EIO; 1234 1235 return err ?: ia->write.out.size; 1236 } 1237 1238 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written) 1239 { 1240 struct fuse_conn *fc = get_fuse_conn(inode); 1241 struct fuse_inode *fi = get_fuse_inode(inode); 1242 bool ret = false; 1243 1244 spin_lock(&fi->lock); 1245 fi->attr_version = atomic64_inc_return(&fc->attr_version); 1246 if (written > 0 && pos > inode->i_size) { 1247 i_size_write(inode, pos); 1248 ret = true; 1249 } 1250 spin_unlock(&fi->lock); 1251 1252 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE); 1253 1254 return ret; 1255 } 1256 1257 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia, 1258 struct kiocb *iocb, struct inode *inode, 1259 loff_t pos, size_t count) 1260 { 1261 struct fuse_args_pages *ap = &ia->ap; 1262 struct file *file = iocb->ki_filp; 1263 struct fuse_file *ff = file->private_data; 1264 struct fuse_mount *fm = ff->fm; 1265 unsigned int i; 1266 int err; 1267 1268 for (i = 0; i < ap->num_folios; i++) 1269 folio_wait_writeback(ap->folios[i]); 1270 1271 fuse_write_args_fill(ia, ff, pos, count); 1272 ia->write.in.flags = fuse_write_flags(iocb); 1273 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID)) 1274 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID; 1275 1276 err = fuse_simple_request(fm, &ap->args); 1277 if (!err && ia->write.out.size > count) 1278 err = -EIO; 1279 1280 for (i = 0; i < ap->num_folios; i++) { 1281 struct folio *folio = ap->folios[i]; 1282 1283 if (ia->write.folio_locked && (i == ap->num_folios - 1)) 1284 folio_unlock(folio); 1285 folio_put(folio); 1286 } 1287 1288 return err; 1289 } 1290 1291 static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia, 1292 struct address_space *mapping, 1293 struct iov_iter *ii, loff_t pos, 1294 unsigned int max_folios) 1295 { 1296 struct fuse_args_pages *ap = &ia->ap; 1297 struct fuse_conn *fc = get_fuse_conn(mapping->host); 1298 size_t count = 0; 1299 unsigned int num; 1300 int err = 0; 1301 1302 num = min(iov_iter_count(ii), fc->max_write); 1303 1304 ap->args.in_pages = true; 1305 1306 while (num && ap->num_folios < max_folios) { 1307 size_t tmp; 1308 struct folio *folio; 1309 pgoff_t index = pos >> PAGE_SHIFT; 1310 unsigned int bytes; 1311 unsigned int folio_offset; 1312 1313 again: 1314 folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN, 1315 mapping_gfp_mask(mapping)); 1316 if (IS_ERR(folio)) { 1317 err = PTR_ERR(folio); 1318 break; 1319 } 1320 1321 if (mapping_writably_mapped(mapping)) 1322 flush_dcache_folio(folio); 1323 1324 folio_offset = offset_in_folio(folio, pos); 1325 bytes = min(folio_size(folio) - folio_offset, num); 1326 1327 tmp = copy_folio_from_iter_atomic(folio, folio_offset, bytes, ii); 1328 flush_dcache_folio(folio); 1329 1330 if (!tmp) { 1331 folio_unlock(folio); 1332 folio_put(folio); 1333 1334 /* 1335 * Ensure forward progress by faulting in 1336 * while not holding the folio lock: 1337 */ 1338 if (fault_in_iov_iter_readable(ii, bytes)) { 1339 err = -EFAULT; 1340 break; 1341 } 1342 1343 goto again; 1344 } 1345 1346 ap->folios[ap->num_folios] = folio; 1347 ap->descs[ap->num_folios].offset = folio_offset; 1348 ap->descs[ap->num_folios].length = tmp; 1349 ap->num_folios++; 1350 1351 count += tmp; 1352 pos += tmp; 1353 num -= tmp; 1354 1355 /* If we copied full folio, mark it uptodate */ 1356 if (tmp == folio_size(folio)) 1357 iomap_folio_mark_uptodate(folio); 1358 1359 if (folio_test_uptodate(folio)) { 1360 folio_unlock(folio); 1361 } else { 1362 ia->write.folio_locked = true; 1363 break; 1364 } 1365 if (!fc->big_writes) 1366 break; 1367 if (folio_offset + tmp != folio_size(folio)) 1368 break; 1369 } 1370 1371 return count > 0 ? count : err; 1372 } 1373 1374 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len, 1375 unsigned int max_pages) 1376 { 1377 unsigned int pages = ((pos + len - 1) >> PAGE_SHIFT) - 1378 (pos >> PAGE_SHIFT) + 1; 1379 1380 return min(pages, max_pages); 1381 } 1382 1383 static ssize_t fuse_perform_write(struct kiocb *iocb, struct iov_iter *ii) 1384 { 1385 struct address_space *mapping = iocb->ki_filp->f_mapping; 1386 struct inode *inode = mapping->host; 1387 struct fuse_conn *fc = get_fuse_conn(inode); 1388 struct fuse_inode *fi = get_fuse_inode(inode); 1389 loff_t pos = iocb->ki_pos; 1390 loff_t old_size = i_size_read(inode); 1391 int err = 0; 1392 ssize_t res = 0; 1393 1394 if (pos > old_size) 1395 truncate_pagecache_range(inode, old_size, pos - 1); 1396 1397 if (inode->i_size < pos + iov_iter_count(ii)) 1398 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1399 1400 do { 1401 ssize_t count; 1402 struct fuse_io_args ia = {}; 1403 struct fuse_args_pages *ap = &ia.ap; 1404 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii), 1405 fc->max_pages); 1406 1407 ap->folios = fuse_folios_alloc(nr_pages, GFP_KERNEL, &ap->descs); 1408 if (!ap->folios) { 1409 err = -ENOMEM; 1410 break; 1411 } 1412 1413 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages); 1414 if (count <= 0) { 1415 err = count; 1416 } else { 1417 err = fuse_send_write_pages(&ia, iocb, inode, 1418 pos, count); 1419 if (!err) { 1420 size_t num_written = ia.write.out.size; 1421 1422 res += num_written; 1423 pos += num_written; 1424 1425 /* break out of the loop on short write */ 1426 if (num_written != count) 1427 err = -EIO; 1428 } 1429 } 1430 kfree(ap->folios); 1431 } while (!err && iov_iter_count(ii)); 1432 1433 fuse_write_update_attr(inode, pos, res); 1434 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 1435 1436 if (!res) 1437 return err; 1438 iocb->ki_pos += res; 1439 return res; 1440 } 1441 1442 static bool fuse_io_past_eof(struct kiocb *iocb, struct iov_iter *iter) 1443 { 1444 struct inode *inode = file_inode(iocb->ki_filp); 1445 1446 return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode); 1447 } 1448 1449 /* 1450 * @return true if an exclusive lock for direct IO writes is needed 1451 */ 1452 static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from) 1453 { 1454 struct file *file = iocb->ki_filp; 1455 struct fuse_file *ff = file->private_data; 1456 struct inode *inode = file_inode(iocb->ki_filp); 1457 struct fuse_inode *fi = get_fuse_inode(inode); 1458 1459 /* Server side has to advise that it supports parallel dio writes. */ 1460 if (!(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES)) 1461 return true; 1462 1463 /* 1464 * Append will need to know the eventual EOF - always needs an 1465 * exclusive lock. 1466 */ 1467 if (iocb->ki_flags & IOCB_APPEND) 1468 return true; 1469 1470 /* shared locks are not allowed with parallel page cache IO */ 1471 if (test_bit(FUSE_I_CACHE_IO_MODE, &fi->state)) 1472 return true; 1473 1474 /* Parallel dio beyond EOF is not supported, at least for now. */ 1475 if (fuse_io_past_eof(iocb, from)) 1476 return true; 1477 1478 return false; 1479 } 1480 1481 static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from, 1482 bool *exclusive) 1483 { 1484 struct inode *inode = file_inode(iocb->ki_filp); 1485 struct fuse_inode *fi = get_fuse_inode(inode); 1486 1487 *exclusive = fuse_dio_wr_exclusive_lock(iocb, from); 1488 if (*exclusive) { 1489 inode_lock(inode); 1490 } else { 1491 inode_lock_shared(inode); 1492 /* 1493 * New parallal dio allowed only if inode is not in caching 1494 * mode and denies new opens in caching mode. This check 1495 * should be performed only after taking shared inode lock. 1496 * Previous past eof check was without inode lock and might 1497 * have raced, so check it again. 1498 */ 1499 if (fuse_io_past_eof(iocb, from) || 1500 fuse_inode_uncached_io_start(fi, NULL) != 0) { 1501 inode_unlock_shared(inode); 1502 inode_lock(inode); 1503 *exclusive = true; 1504 } 1505 } 1506 } 1507 1508 static void fuse_dio_unlock(struct kiocb *iocb, bool exclusive) 1509 { 1510 struct inode *inode = file_inode(iocb->ki_filp); 1511 struct fuse_inode *fi = get_fuse_inode(inode); 1512 1513 if (exclusive) { 1514 inode_unlock(inode); 1515 } else { 1516 /* Allow opens in caching mode after last parallel dio end */ 1517 fuse_inode_uncached_io_end(fi); 1518 inode_unlock_shared(inode); 1519 } 1520 } 1521 1522 static const struct iomap_write_ops fuse_iomap_write_ops = { 1523 .read_folio_range = fuse_iomap_read_folio_range, 1524 }; 1525 1526 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) 1527 { 1528 struct file *file = iocb->ki_filp; 1529 const struct mnt_idmap *idmap = file_mnt_idmap(file); 1530 struct address_space *mapping = file->f_mapping; 1531 ssize_t written = 0; 1532 struct inode *inode = mapping->host; 1533 ssize_t err, count; 1534 struct fuse_conn *fc = get_fuse_conn(inode); 1535 bool writeback = false; 1536 1537 if (fc->writeback_cache) { 1538 /* Update size (EOF optimization) and mode (SUID clearing) */ 1539 err = fuse_update_attributes(mapping->host, file, 1540 STATX_SIZE | STATX_MODE); 1541 if (err) 1542 return err; 1543 1544 if (!fc->handle_killpriv_v2 || 1545 !setattr_should_drop_suidgid(idmap, file_inode(file))) 1546 writeback = true; 1547 } 1548 1549 inode_lock(inode); 1550 1551 err = count = generic_write_checks(iocb, from); 1552 if (err <= 0) 1553 goto out; 1554 1555 task_io_account_write(count); 1556 1557 err = kiocb_modified(iocb); 1558 if (err) 1559 goto out; 1560 1561 if (iocb->ki_flags & IOCB_DIRECT) { 1562 written = generic_file_direct_write(iocb, from); 1563 if (written < 0 || !iov_iter_count(from)) 1564 goto out; 1565 written = direct_write_fallback(iocb, from, written, 1566 fuse_perform_write(iocb, from)); 1567 } else if (writeback) { 1568 /* 1569 * Use iomap so that we can do granular uptodate reads 1570 * and granular dirty tracking for large folios. 1571 */ 1572 written = iomap_file_buffered_write(iocb, from, 1573 &fuse_iomap_ops, 1574 &fuse_iomap_write_ops, 1575 file); 1576 } else { 1577 written = fuse_perform_write(iocb, from); 1578 } 1579 out: 1580 inode_unlock(inode); 1581 if (written > 0) 1582 written = generic_write_sync(iocb, written); 1583 1584 return written ? written : err; 1585 } 1586 1587 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) 1588 { 1589 return (unsigned long)iter_iov(ii)->iov_base + ii->iov_offset; 1590 } 1591 1592 static inline size_t fuse_get_frag_size(const struct iov_iter *ii, 1593 size_t max_size) 1594 { 1595 return min(iov_iter_single_seg_count(ii), max_size); 1596 } 1597 1598 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, 1599 size_t *nbytesp, int write, 1600 unsigned int max_pages, 1601 bool use_pages_for_kvec_io) 1602 { 1603 bool flush_or_invalidate = false; 1604 unsigned int nr_pages = 0; 1605 size_t nbytes = 0; /* # bytes already packed in req */ 1606 ssize_t ret = 0; 1607 1608 /* Special case for kernel I/O: can copy directly into the buffer. 1609 * However if the implementation of fuse_conn requires pages instead of 1610 * pointer (e.g., virtio-fs), use iov_iter_extract_pages() instead. 1611 */ 1612 if (iov_iter_is_kvec(ii)) { 1613 void *user_addr = (void *)fuse_get_user_addr(ii); 1614 1615 if (!use_pages_for_kvec_io) { 1616 size_t frag_size = fuse_get_frag_size(ii, *nbytesp); 1617 1618 if (write) 1619 ap->args.in_args[1].value = user_addr; 1620 else 1621 ap->args.out_args[0].value = user_addr; 1622 1623 iov_iter_advance(ii, frag_size); 1624 *nbytesp = frag_size; 1625 return 0; 1626 } 1627 1628 if (is_vmalloc_addr(user_addr)) { 1629 ap->args.vmap_base = user_addr; 1630 flush_or_invalidate = true; 1631 } 1632 } 1633 1634 /* 1635 * Until there is support for iov_iter_extract_folios(), we have to 1636 * manually extract pages using iov_iter_extract_pages() and then 1637 * copy that to a folios array. 1638 */ 1639 struct page **pages = kzalloc_objs(struct page *, max_pages); 1640 if (!pages) { 1641 ret = -ENOMEM; 1642 goto out; 1643 } 1644 1645 while (nbytes < *nbytesp && nr_pages < max_pages) { 1646 unsigned nfolios, i; 1647 size_t start; 1648 1649 ret = iov_iter_extract_pages(ii, &pages, 1650 *nbytesp - nbytes, 1651 max_pages - nr_pages, 1652 0, &start); 1653 if (ret < 0) 1654 break; 1655 1656 nbytes += ret; 1657 1658 nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE); 1659 1660 for (i = 0; i < nfolios; i++) { 1661 struct folio *folio = page_folio(pages[i]); 1662 unsigned int offset = start + 1663 (folio_page_idx(folio, pages[i]) << PAGE_SHIFT); 1664 unsigned int len = umin(ret, PAGE_SIZE - start); 1665 1666 ap->descs[ap->num_folios].offset = offset; 1667 ap->descs[ap->num_folios].length = len; 1668 ap->folios[ap->num_folios] = folio; 1669 start = 0; 1670 ret -= len; 1671 ap->num_folios++; 1672 } 1673 1674 nr_pages += nfolios; 1675 } 1676 kfree(pages); 1677 1678 if (write && flush_or_invalidate) 1679 flush_kernel_vmap_range(ap->args.vmap_base, nbytes); 1680 1681 ap->args.invalidate_vmap = !write && flush_or_invalidate; 1682 ap->args.is_pinned = iov_iter_extract_will_pin(ii); 1683 ap->args.user_pages = true; 1684 if (write) 1685 ap->args.in_pages = true; 1686 else 1687 ap->args.out_pages = true; 1688 1689 out: 1690 *nbytesp = nbytes; 1691 1692 return ret < 0 ? ret : 0; 1693 } 1694 1695 ssize_t fuse_do_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, 1696 loff_t *ppos, int flags) 1697 { 1698 int write = flags & FUSE_DIO_WRITE; 1699 int cuse = flags & FUSE_DIO_CUSE; 1700 struct file *file = io->iocb->ki_filp; 1701 struct address_space *mapping = file->f_mapping; 1702 struct inode *inode = mapping->host; 1703 struct fuse_file *ff = file->private_data; 1704 struct fuse_conn *fc = ff->fm->fc; 1705 size_t nmax = write ? fc->max_write : fc->max_read; 1706 loff_t pos = *ppos; 1707 size_t count = iov_iter_count(iter); 1708 pgoff_t idx_from = pos >> PAGE_SHIFT; 1709 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT; 1710 ssize_t res = 0; 1711 int err = 0; 1712 struct fuse_io_args *ia; 1713 unsigned int max_pages; 1714 bool fopen_direct_io = ff->open_flags & FOPEN_DIRECT_IO; 1715 1716 max_pages = iov_iter_npages(iter, fc->max_pages); 1717 ia = fuse_io_alloc(io, max_pages); 1718 if (!ia) 1719 return -ENOMEM; 1720 1721 if (fopen_direct_io) { 1722 res = filemap_write_and_wait_range(mapping, pos, pos + count - 1); 1723 if (res) { 1724 fuse_io_free(ia); 1725 return res; 1726 } 1727 } 1728 if (!cuse && filemap_range_has_writeback(mapping, pos, (pos + count - 1))) { 1729 if (!write) 1730 inode_lock(inode); 1731 fuse_sync_writes(inode); 1732 if (!write) 1733 inode_unlock(inode); 1734 } 1735 1736 if (fopen_direct_io && write) { 1737 res = invalidate_inode_pages2_range(mapping, idx_from, idx_to); 1738 if (res) { 1739 fuse_io_free(ia); 1740 return res; 1741 } 1742 } 1743 1744 io->should_dirty = !write && user_backed_iter(iter); 1745 while (count) { 1746 ssize_t nres; 1747 fl_owner_t owner = current->files; 1748 size_t nbytes = min(count, nmax); 1749 1750 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write, 1751 max_pages, fc->use_pages_for_kvec_io); 1752 if (err && !nbytes) 1753 break; 1754 1755 if (write) { 1756 if (!capable(CAP_FSETID)) 1757 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID; 1758 1759 nres = fuse_send_write(ia, pos, nbytes, owner); 1760 } else { 1761 nres = fuse_send_read(ia, pos, nbytes, owner); 1762 } 1763 1764 if (!io->async || nres < 0) { 1765 fuse_release_user_pages(&ia->ap, nres, io->should_dirty); 1766 fuse_io_free(ia); 1767 } 1768 ia = NULL; 1769 if (nres < 0) { 1770 iov_iter_revert(iter, nbytes); 1771 err = nres; 1772 break; 1773 } 1774 WARN_ON(nres > nbytes); 1775 1776 count -= nres; 1777 res += nres; 1778 pos += nres; 1779 if (nres != nbytes) { 1780 iov_iter_revert(iter, nbytes - nres); 1781 break; 1782 } 1783 if (count) { 1784 max_pages = iov_iter_npages(iter, fc->max_pages); 1785 ia = fuse_io_alloc(io, max_pages); 1786 if (!ia) 1787 break; 1788 } 1789 } 1790 if (ia) 1791 fuse_io_free(ia); 1792 if (res > 0) 1793 *ppos = pos; 1794 1795 return res > 0 ? res : err; 1796 } 1797 EXPORT_SYMBOL_GPL(fuse_do_direct_io); 1798 1799 static ssize_t __fuse_direct_read(struct fuse_io_priv *io, 1800 struct iov_iter *iter, 1801 loff_t *ppos) 1802 { 1803 ssize_t res; 1804 struct inode *inode = file_inode(io->iocb->ki_filp); 1805 1806 res = fuse_do_direct_io(io, iter, ppos, 0); 1807 1808 fuse_invalidate_atime(inode); 1809 1810 return res; 1811 } 1812 1813 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter); 1814 1815 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) 1816 { 1817 ssize_t res; 1818 1819 if (!is_sync_kiocb(iocb)) { 1820 res = fuse_direct_IO(iocb, to); 1821 } else { 1822 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 1823 1824 res = __fuse_direct_read(&io, to, &iocb->ki_pos); 1825 } 1826 1827 return res; 1828 } 1829 1830 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) 1831 { 1832 struct inode *inode = file_inode(iocb->ki_filp); 1833 struct address_space *mapping = inode->i_mapping; 1834 ssize_t res; 1835 bool exclusive; 1836 1837 fuse_dio_lock(iocb, from, &exclusive); 1838 res = generic_write_checks(iocb, from); 1839 if (res > 0) { 1840 loff_t pos = iocb->ki_pos; 1841 1842 task_io_account_write(res); 1843 if (!is_sync_kiocb(iocb)) { 1844 res = fuse_direct_IO(iocb, from); 1845 } else { 1846 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 1847 1848 res = fuse_do_direct_io(&io, from, &iocb->ki_pos, 1849 FUSE_DIO_WRITE); 1850 fuse_write_update_attr(inode, iocb->ki_pos, res); 1851 } 1852 if (res > 0 && mapping->nrpages) { 1853 /* 1854 * As in generic_file_direct_write(), invalidate after 1855 * write, to invalidate read-ahead cache that may have 1856 * competed with the write. 1857 */ 1858 invalidate_inode_pages2_range(mapping, 1859 pos >> PAGE_SHIFT, 1860 (pos + res - 1) >> PAGE_SHIFT); 1861 } 1862 } 1863 fuse_dio_unlock(iocb, exclusive); 1864 1865 return res; 1866 } 1867 1868 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 1869 { 1870 struct file *file = iocb->ki_filp; 1871 struct fuse_file *ff = file->private_data; 1872 struct inode *inode = file_inode(file); 1873 1874 if (fuse_is_bad(inode)) 1875 return -EIO; 1876 1877 if (FUSE_IS_VDAX(inode)) 1878 return fuse_vdax_read_iter(iocb, to); 1879 1880 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1881 if (ff->open_flags & FOPEN_DIRECT_IO) 1882 return fuse_direct_read_iter(iocb, to); 1883 else if (fuse_is_passthrough(ff)) 1884 return fuse_passthrough_read_iter(iocb, to); 1885 else 1886 return fuse_cache_read_iter(iocb, to); 1887 } 1888 1889 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 1890 { 1891 struct file *file = iocb->ki_filp; 1892 struct fuse_file *ff = file->private_data; 1893 struct inode *inode = file_inode(file); 1894 1895 if (fuse_is_bad(inode)) 1896 return -EIO; 1897 1898 if (FUSE_IS_VDAX(inode)) 1899 return fuse_vdax_write_iter(iocb, from); 1900 1901 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1902 if (ff->open_flags & FOPEN_DIRECT_IO) 1903 return fuse_direct_write_iter(iocb, from); 1904 else if (fuse_is_passthrough(ff)) 1905 return fuse_passthrough_write_iter(iocb, from); 1906 else 1907 return fuse_cache_write_iter(iocb, from); 1908 } 1909 1910 static ssize_t fuse_splice_read(struct file *in, loff_t *ppos, 1911 struct pipe_inode_info *pipe, size_t len, 1912 unsigned int flags) 1913 { 1914 struct fuse_file *ff = in->private_data; 1915 1916 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1917 1918 if (ff->open_flags & FOPEN_DIRECT_IO) 1919 return copy_splice_read(in, ppos, pipe, len, flags); 1920 else if (fuse_is_passthrough(ff)) 1921 return fuse_passthrough_splice_read(in, ppos, pipe, len, flags); 1922 else 1923 return filemap_splice_read(in, ppos, pipe, len, flags); 1924 } 1925 1926 static ssize_t fuse_splice_write(struct pipe_inode_info *pipe, struct file *out, 1927 loff_t *ppos, size_t len, unsigned int flags) 1928 { 1929 struct fuse_file *ff = out->private_data; 1930 1931 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ 1932 if (fuse_is_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO)) 1933 return fuse_passthrough_splice_write(pipe, out, ppos, len, flags); 1934 else 1935 return iter_file_splice_write(pipe, out, ppos, len, flags); 1936 } 1937 1938 static void fuse_writepage_free(struct fuse_writepage_args *wpa) 1939 { 1940 struct fuse_args_pages *ap = &wpa->ia.ap; 1941 1942 if (wpa->bucket) 1943 fuse_sync_bucket_dec(wpa->bucket); 1944 1945 fuse_file_put(wpa->ia.ff, false); 1946 1947 kfree(ap->folios); 1948 kfree(wpa); 1949 } 1950 1951 static void fuse_writepage_finish(struct fuse_writepage_args *wpa) 1952 { 1953 struct fuse_args_pages *ap = &wpa->ia.ap; 1954 struct inode *inode = wpa->inode; 1955 struct fuse_inode *fi = get_fuse_inode(inode); 1956 int i; 1957 1958 for (i = 0; i < ap->num_folios; i++) 1959 /* 1960 * Benchmarks showed that ending writeback within the 1961 * scope of the fi->lock alleviates xarray lock 1962 * contention and noticeably improves performance. 1963 */ 1964 iomap_finish_folio_write(inode, ap->folios[i], 1965 ap->descs[i].length); 1966 1967 wake_up(&fi->page_waitq); 1968 } 1969 1970 /* Called under fi->lock, may release and reacquire it */ 1971 static void fuse_send_writepage(struct fuse_mount *fm, 1972 struct fuse_writepage_args *wpa, loff_t size) 1973 __releases(fi->lock) 1974 __acquires(fi->lock) 1975 { 1976 struct fuse_inode *fi = get_fuse_inode(wpa->inode); 1977 struct fuse_args_pages *ap = &wpa->ia.ap; 1978 struct fuse_write_in *inarg = &wpa->ia.write.in; 1979 struct fuse_args *args = &ap->args; 1980 __u64 data_size = 0; 1981 int err, i; 1982 1983 for (i = 0; i < ap->num_folios; i++) 1984 data_size += ap->descs[i].length; 1985 1986 fi->writectr++; 1987 if (inarg->offset + data_size <= size) { 1988 inarg->size = data_size; 1989 } else if (inarg->offset < size) { 1990 inarg->size = size - inarg->offset; 1991 } else { 1992 /* Got truncated off completely */ 1993 goto out_free; 1994 } 1995 1996 args->in_args[1].size = inarg->size; 1997 args->force = true; 1998 args->nocreds = true; 1999 2000 err = fuse_simple_background(fm, args, GFP_ATOMIC); 2001 if (err == -ENOMEM) { 2002 spin_unlock(&fi->lock); 2003 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL); 2004 spin_lock(&fi->lock); 2005 } 2006 2007 /* Fails on broken connection only */ 2008 if (unlikely(err)) 2009 goto out_free; 2010 2011 return; 2012 2013 out_free: 2014 fi->writectr--; 2015 fuse_writepage_finish(wpa); 2016 spin_unlock(&fi->lock); 2017 fuse_writepage_free(wpa); 2018 spin_lock(&fi->lock); 2019 } 2020 2021 /* 2022 * If fi->writectr is positive (no truncate or fsync going on) send 2023 * all queued writepage requests. 2024 * 2025 * Called with fi->lock 2026 */ 2027 void fuse_flush_writepages(struct inode *inode) 2028 __releases(fi->lock) 2029 __acquires(fi->lock) 2030 { 2031 struct fuse_mount *fm = get_fuse_mount(inode); 2032 struct fuse_inode *fi = get_fuse_inode(inode); 2033 loff_t crop = i_size_read(inode); 2034 struct fuse_writepage_args *wpa; 2035 2036 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { 2037 wpa = list_entry(fi->queued_writes.next, 2038 struct fuse_writepage_args, queue_entry); 2039 list_del_init(&wpa->queue_entry); 2040 fuse_send_writepage(fm, wpa, crop); 2041 } 2042 } 2043 2044 static void fuse_writepage_end(struct fuse_args *args, int error) 2045 { 2046 struct fuse_writepage_args *wpa = 2047 container_of(args, typeof(*wpa), ia.ap.args); 2048 struct inode *inode = wpa->inode; 2049 struct fuse_inode *fi = get_fuse_inode(inode); 2050 struct fuse_conn *fc = get_fuse_conn(inode); 2051 2052 mapping_set_error(inode->i_mapping, error); 2053 /* 2054 * A writeback finished and this might have updated mtime/ctime on 2055 * server making local mtime/ctime stale. Hence invalidate attrs. 2056 * Do this only if writeback_cache is not enabled. If writeback_cache 2057 * is enabled, we trust local ctime/mtime. 2058 */ 2059 if (!fc->writeback_cache) 2060 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY); 2061 spin_lock(&fi->lock); 2062 fi->writectr--; 2063 fuse_writepage_finish(wpa); 2064 spin_unlock(&fi->lock); 2065 fuse_writepage_free(wpa); 2066 } 2067 2068 static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi) 2069 { 2070 struct fuse_file *ff; 2071 2072 spin_lock(&fi->lock); 2073 ff = list_first_entry_or_null(&fi->write_files, struct fuse_file, 2074 write_entry); 2075 if (ff) 2076 fuse_file_get(ff); 2077 spin_unlock(&fi->lock); 2078 2079 return ff; 2080 } 2081 2082 static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi) 2083 { 2084 struct fuse_file *ff = __fuse_write_file_get(fi); 2085 WARN_ON(!ff); 2086 return ff; 2087 } 2088 2089 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc) 2090 { 2091 struct fuse_inode *fi = get_fuse_inode(inode); 2092 struct fuse_file *ff; 2093 int err; 2094 2095 ff = __fuse_write_file_get(fi); 2096 err = fuse_flush_times(inode, ff); 2097 if (ff) 2098 fuse_file_put(ff, false); 2099 2100 return err; 2101 } 2102 2103 static struct fuse_writepage_args *fuse_writepage_args_alloc(void) 2104 { 2105 struct fuse_writepage_args *wpa; 2106 struct fuse_args_pages *ap; 2107 2108 wpa = kzalloc_obj(*wpa, GFP_NOFS); 2109 if (wpa) { 2110 ap = &wpa->ia.ap; 2111 ap->num_folios = 0; 2112 ap->folios = fuse_folios_alloc(1, GFP_NOFS, &ap->descs); 2113 if (!ap->folios) { 2114 kfree(wpa); 2115 wpa = NULL; 2116 } 2117 } 2118 return wpa; 2119 2120 } 2121 2122 static void fuse_writepage_add_to_bucket(struct fuse_conn *fc, 2123 struct fuse_writepage_args *wpa) 2124 { 2125 if (!fc->sync_fs) 2126 return; 2127 2128 rcu_read_lock(); 2129 /* Prevent resurrection of dead bucket in unlikely race with syncfs */ 2130 do { 2131 wpa->bucket = rcu_dereference(fc->curr_bucket); 2132 } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count))); 2133 rcu_read_unlock(); 2134 } 2135 2136 static void fuse_writepage_args_page_fill(struct fuse_writepage_args *wpa, struct folio *folio, 2137 uint32_t folio_index, loff_t offset, unsigned len) 2138 { 2139 struct fuse_args_pages *ap = &wpa->ia.ap; 2140 2141 ap->folios[folio_index] = folio; 2142 ap->descs[folio_index].offset = offset; 2143 ap->descs[folio_index].length = len; 2144 } 2145 2146 static struct fuse_writepage_args *fuse_writepage_args_setup(struct folio *folio, 2147 size_t offset, 2148 struct fuse_file *ff) 2149 { 2150 struct inode *inode = folio->mapping->host; 2151 struct fuse_conn *fc = get_fuse_conn(inode); 2152 struct fuse_writepage_args *wpa; 2153 struct fuse_args_pages *ap; 2154 2155 wpa = fuse_writepage_args_alloc(); 2156 if (!wpa) 2157 return NULL; 2158 2159 fuse_writepage_add_to_bucket(fc, wpa); 2160 fuse_write_args_fill(&wpa->ia, ff, folio_pos(folio) + offset, 0); 2161 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE; 2162 wpa->inode = inode; 2163 wpa->ia.ff = ff; 2164 2165 ap = &wpa->ia.ap; 2166 ap->args.in_pages = true; 2167 ap->args.end = fuse_writepage_end; 2168 2169 return wpa; 2170 } 2171 2172 struct fuse_fill_wb_data { 2173 struct fuse_writepage_args *wpa; 2174 struct fuse_file *ff; 2175 unsigned int max_folios; 2176 /* 2177 * nr_bytes won't overflow since fuse_folios_need_send() caps 2178 * wb requests to never exceed fc->max_pages (which has an upper bound 2179 * of U16_MAX). 2180 */ 2181 unsigned int nr_bytes; 2182 }; 2183 2184 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data, 2185 unsigned int max_pages) 2186 { 2187 struct fuse_args_pages *ap = &data->wpa->ia.ap; 2188 struct folio **folios; 2189 struct fuse_folio_desc *descs; 2190 unsigned int nfolios = min_t(unsigned int, 2191 max_t(unsigned int, data->max_folios * 2, 2192 FUSE_DEFAULT_MAX_PAGES_PER_REQ), 2193 max_pages); 2194 WARN_ON(nfolios <= data->max_folios); 2195 2196 folios = fuse_folios_alloc(nfolios, GFP_NOFS, &descs); 2197 if (!folios) 2198 return false; 2199 2200 memcpy(folios, ap->folios, sizeof(struct folio *) * ap->num_folios); 2201 memcpy(descs, ap->descs, sizeof(struct fuse_folio_desc) * ap->num_folios); 2202 kfree(ap->folios); 2203 ap->folios = folios; 2204 ap->descs = descs; 2205 data->max_folios = nfolios; 2206 2207 return true; 2208 } 2209 2210 static void fuse_writepages_send(struct inode *inode, 2211 struct fuse_fill_wb_data *data) 2212 { 2213 struct fuse_writepage_args *wpa = data->wpa; 2214 struct fuse_inode *fi = get_fuse_inode(inode); 2215 2216 spin_lock(&fi->lock); 2217 list_add_tail(&wpa->queue_entry, &fi->queued_writes); 2218 fuse_flush_writepages(inode); 2219 spin_unlock(&fi->lock); 2220 } 2221 2222 static bool fuse_folios_need_send(struct fuse_conn *fc, loff_t pos, 2223 unsigned len, struct fuse_args_pages *ap, 2224 unsigned cur_bytes, bool write) 2225 { 2226 struct folio *prev_folio; 2227 struct fuse_folio_desc prev_desc; 2228 unsigned bytes = cur_bytes + len; 2229 loff_t prev_pos; 2230 size_t max_bytes = write ? fc->max_write : fc->max_read; 2231 2232 WARN_ON(!ap->num_folios); 2233 2234 /* Reached max pages or max folio slots */ 2235 if (ap->num_folios >= fc->max_pages) 2236 return true; 2237 2238 if (DIV_ROUND_UP(bytes, PAGE_SIZE) > fc->max_pages) 2239 return true; 2240 2241 if (bytes > max_bytes) 2242 return true; 2243 2244 /* Discontinuity */ 2245 prev_folio = ap->folios[ap->num_folios - 1]; 2246 prev_desc = ap->descs[ap->num_folios - 1]; 2247 prev_pos = folio_pos(prev_folio) + prev_desc.offset + prev_desc.length; 2248 if (prev_pos != pos) 2249 return true; 2250 2251 return false; 2252 } 2253 2254 static ssize_t fuse_iomap_writeback_range(struct iomap_writepage_ctx *wpc, 2255 struct folio *folio, u64 pos, 2256 unsigned len, u64 end_pos) 2257 { 2258 struct fuse_fill_wb_data *data = wpc->wb_ctx; 2259 struct fuse_writepage_args *wpa = data->wpa; 2260 struct fuse_args_pages *ap = &wpa->ia.ap; 2261 struct inode *inode = wpc->inode; 2262 struct fuse_inode *fi = get_fuse_inode(inode); 2263 struct fuse_conn *fc = get_fuse_conn(inode); 2264 loff_t offset = offset_in_folio(folio, pos); 2265 2266 WARN_ON_ONCE(!data); 2267 2268 if (!data->ff) { 2269 data->ff = fuse_write_file_get(fi); 2270 if (!data->ff) 2271 return -EIO; 2272 } 2273 2274 if (wpa) { 2275 bool send = fuse_folios_need_send(fc, pos, len, ap, 2276 data->nr_bytes, true); 2277 2278 if (!send) { 2279 /* 2280 * Need to grow the pages array? If so, did the 2281 * expansion fail? 2282 */ 2283 send = (ap->num_folios == data->max_folios) && 2284 !fuse_pages_realloc(data, fc->max_pages); 2285 } 2286 2287 if (send) { 2288 fuse_writepages_send(inode, data); 2289 data->wpa = NULL; 2290 data->nr_bytes = 0; 2291 } 2292 } 2293 2294 if (data->wpa == NULL) { 2295 wpa = fuse_writepage_args_setup(folio, offset, data->ff); 2296 if (!wpa) 2297 return -ENOMEM; 2298 fuse_file_get(wpa->ia.ff); 2299 data->max_folios = 1; 2300 ap = &wpa->ia.ap; 2301 } 2302 2303 fuse_writepage_args_page_fill(wpa, folio, ap->num_folios, 2304 offset, len); 2305 data->nr_bytes += len; 2306 2307 ap->num_folios++; 2308 if (!data->wpa) 2309 data->wpa = wpa; 2310 2311 return len; 2312 } 2313 2314 static int fuse_iomap_writeback_submit(struct iomap_writepage_ctx *wpc, 2315 int error) 2316 { 2317 struct fuse_fill_wb_data *data = wpc->wb_ctx; 2318 2319 WARN_ON_ONCE(!data); 2320 2321 if (data->wpa) { 2322 WARN_ON(!data->wpa->ia.ap.num_folios); 2323 fuse_writepages_send(wpc->inode, data); 2324 } 2325 2326 if (data->ff) 2327 fuse_file_put(data->ff, false); 2328 2329 return error; 2330 } 2331 2332 static const struct iomap_writeback_ops fuse_writeback_ops = { 2333 .writeback_range = fuse_iomap_writeback_range, 2334 .writeback_submit = fuse_iomap_writeback_submit, 2335 }; 2336 2337 static int fuse_writepages(struct address_space *mapping, 2338 struct writeback_control *wbc) 2339 { 2340 struct inode *inode = mapping->host; 2341 struct fuse_conn *fc = get_fuse_conn(inode); 2342 struct fuse_fill_wb_data data = {}; 2343 struct iomap_writepage_ctx wpc = { 2344 .inode = inode, 2345 .iomap.type = IOMAP_MAPPED, 2346 .wbc = wbc, 2347 .ops = &fuse_writeback_ops, 2348 .wb_ctx = &data, 2349 }; 2350 2351 if (fuse_is_bad(inode)) 2352 return -EIO; 2353 2354 if (wbc->sync_mode == WB_SYNC_NONE && 2355 fuse_chan_num_background(fc->chan) >= fc->congestion_threshold) 2356 return 0; 2357 2358 return iomap_writepages(&wpc); 2359 } 2360 2361 static int fuse_launder_folio(struct folio *folio) 2362 { 2363 int err = 0; 2364 struct fuse_fill_wb_data data = {}; 2365 struct iomap_writepage_ctx wpc = { 2366 .inode = folio->mapping->host, 2367 .iomap.type = IOMAP_MAPPED, 2368 .ops = &fuse_writeback_ops, 2369 .wb_ctx = &data, 2370 }; 2371 2372 if (fuse_is_bad(wpc.inode)) 2373 return -EIO; 2374 2375 if (folio_clear_dirty_for_io(folio)) { 2376 err = iomap_writeback_folio(&wpc, folio); 2377 err = fuse_iomap_writeback_submit(&wpc, err); 2378 if (!err) 2379 folio_wait_writeback(folio); 2380 } 2381 return err; 2382 } 2383 2384 /* 2385 * Write back dirty data/metadata now (there may not be any suitable 2386 * open files later for data) 2387 */ 2388 static void fuse_vma_close(struct vm_area_struct *vma) 2389 { 2390 int err; 2391 2392 err = write_inode_now(vma->vm_file->f_mapping->host, 1); 2393 mapping_set_error(vma->vm_file->f_mapping, err); 2394 } 2395 2396 /* 2397 * Wait for writeback against this page to complete before allowing it 2398 * to be marked dirty again, and hence written back again, possibly 2399 * before the previous writepage completed. 2400 * 2401 * Block here, instead of in ->writepage(), so that the userspace fs 2402 * can only block processes actually operating on the filesystem. 2403 * 2404 * Otherwise unprivileged userspace fs would be able to block 2405 * unrelated: 2406 * 2407 * - page migration 2408 * - sync(2) 2409 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER 2410 */ 2411 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf) 2412 { 2413 struct folio *folio = page_folio(vmf->page); 2414 struct inode *inode = file_inode(vmf->vma->vm_file); 2415 2416 file_update_time(vmf->vma->vm_file); 2417 folio_lock(folio); 2418 if (folio->mapping != inode->i_mapping) { 2419 folio_unlock(folio); 2420 return VM_FAULT_NOPAGE; 2421 } 2422 2423 folio_wait_writeback(folio); 2424 return VM_FAULT_LOCKED; 2425 } 2426 2427 static const struct vm_operations_struct fuse_file_vm_ops = { 2428 .close = fuse_vma_close, 2429 .fault = filemap_fault, 2430 .map_pages = filemap_map_pages, 2431 .page_mkwrite = fuse_page_mkwrite, 2432 }; 2433 2434 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) 2435 { 2436 struct fuse_file *ff = file->private_data; 2437 struct fuse_conn *fc = ff->fm->fc; 2438 struct inode *inode = file_inode(file); 2439 2440 /* DAX mmap is superior to direct_io mmap */ 2441 if (FUSE_IS_VDAX(inode)) 2442 return fuse_vdax_mmap(file, vma); 2443 2444 /* 2445 * If inode is in passthrough io mode, because it has some file open 2446 * in passthrough mode, either mmap to backing file or fail mmap, 2447 * because mixing cached mmap and passthrough io mode is not allowed. 2448 */ 2449 if (fuse_is_passthrough(ff)) 2450 return fuse_passthrough_mmap(file, vma); 2451 else if (fuse_inode_backing(get_fuse_inode(inode))) 2452 return -ENODEV; 2453 2454 /* 2455 * FOPEN_DIRECT_IO handling is special compared to O_DIRECT, 2456 * as does not allow MAP_SHARED mmap without FUSE_DIRECT_IO_ALLOW_MMAP. 2457 */ 2458 if (ff->open_flags & FOPEN_DIRECT_IO) { 2459 /* 2460 * Can't provide the coherency needed for MAP_SHARED 2461 * if FUSE_DIRECT_IO_ALLOW_MMAP isn't set. 2462 */ 2463 if ((vma->vm_flags & VM_MAYSHARE) && !fc->direct_io_allow_mmap) 2464 return -ENODEV; 2465 2466 invalidate_inode_pages2(file->f_mapping); 2467 2468 if (!(vma->vm_flags & VM_MAYSHARE)) { 2469 /* MAP_PRIVATE */ 2470 return generic_file_mmap(file, vma); 2471 } 2472 2473 /* 2474 * First mmap of direct_io file enters caching inode io mode. 2475 * Also waits for parallel dio writers to go into serial mode 2476 * (exclusive instead of shared lock). 2477 * After first mmap, the inode stays in caching io mode until 2478 * the direct_io file release. 2479 */ 2480 if (!fuse_file_cached_io_open(inode, ff)) 2481 return -ENODEV; 2482 } 2483 2484 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) 2485 fuse_link_write_file(file); 2486 2487 file_accessed(file); 2488 vma->vm_ops = &fuse_file_vm_ops; 2489 return 0; 2490 } 2491 2492 static int convert_fuse_file_lock(struct fuse_conn *fc, 2493 const struct fuse_file_lock *ffl, 2494 struct file_lock *fl) 2495 { 2496 switch (ffl->type) { 2497 case F_UNLCK: 2498 break; 2499 2500 case F_RDLCK: 2501 case F_WRLCK: 2502 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || 2503 ffl->end < ffl->start) 2504 return -EIO; 2505 2506 fl->fl_start = ffl->start; 2507 fl->fl_end = ffl->end; 2508 2509 /* 2510 * Convert pid into init's pid namespace. The locks API will 2511 * translate it into the caller's pid namespace. 2512 */ 2513 rcu_read_lock(); 2514 fl->c.flc_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns); 2515 rcu_read_unlock(); 2516 break; 2517 2518 default: 2519 return -EIO; 2520 } 2521 fl->c.flc_type = ffl->type; 2522 return 0; 2523 } 2524 2525 static void fuse_lk_fill(struct fuse_args *args, struct file *file, 2526 const struct file_lock *fl, int opcode, pid_t pid, 2527 int flock, struct fuse_lk_in *inarg) 2528 { 2529 struct inode *inode = file_inode(file); 2530 struct fuse_conn *fc = get_fuse_conn(inode); 2531 struct fuse_file *ff = file->private_data; 2532 2533 memset(inarg, 0, sizeof(*inarg)); 2534 inarg->fh = ff->fh; 2535 inarg->owner = fuse_lock_owner_id(fc, fl->c.flc_owner); 2536 inarg->lk.start = fl->fl_start; 2537 inarg->lk.end = fl->fl_end; 2538 inarg->lk.type = fl->c.flc_type; 2539 inarg->lk.pid = pid; 2540 if (flock) 2541 inarg->lk_flags |= FUSE_LK_FLOCK; 2542 args->opcode = opcode; 2543 args->nodeid = get_node_id(inode); 2544 args->in_numargs = 1; 2545 args->in_args[0].size = sizeof(*inarg); 2546 args->in_args[0].value = inarg; 2547 } 2548 2549 static int fuse_getlk(struct file *file, struct file_lock *fl) 2550 { 2551 struct inode *inode = file_inode(file); 2552 struct fuse_mount *fm = get_fuse_mount(inode); 2553 FUSE_ARGS(args); 2554 struct fuse_lk_in inarg; 2555 struct fuse_lk_out outarg; 2556 int err; 2557 2558 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg); 2559 args.out_numargs = 1; 2560 args.out_args[0].size = sizeof(outarg); 2561 args.out_args[0].value = &outarg; 2562 err = fuse_simple_request(fm, &args); 2563 if (!err) 2564 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl); 2565 2566 return err; 2567 } 2568 2569 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) 2570 { 2571 struct inode *inode = file_inode(file); 2572 struct fuse_mount *fm = get_fuse_mount(inode); 2573 FUSE_ARGS(args); 2574 struct fuse_lk_in inarg; 2575 int opcode = (fl->c.flc_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; 2576 struct pid *pid = fl->c.flc_type != F_UNLCK ? task_tgid(current) : NULL; 2577 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns); 2578 int err; 2579 2580 if (fl->fl_lmops && fl->fl_lmops->lm_grant) { 2581 /* NLM needs asynchronous locks, which we don't support yet */ 2582 return -ENOLCK; 2583 } 2584 2585 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg); 2586 err = fuse_simple_request(fm, &args); 2587 2588 /* locking is restartable */ 2589 if (err == -EINTR) 2590 err = -ERESTARTSYS; 2591 2592 return err; 2593 } 2594 2595 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) 2596 { 2597 struct inode *inode = file_inode(file); 2598 struct fuse_conn *fc = get_fuse_conn(inode); 2599 int err; 2600 2601 if (cmd == F_CANCELLK) { 2602 err = 0; 2603 } else if (cmd == F_GETLK) { 2604 if (fc->no_lock) { 2605 posix_test_lock(file, fl); 2606 err = 0; 2607 } else 2608 err = fuse_getlk(file, fl); 2609 } else { 2610 if (fc->no_lock) 2611 err = posix_lock_file(file, fl, NULL); 2612 else 2613 err = fuse_setlk(file, fl, 0); 2614 } 2615 return err; 2616 } 2617 2618 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) 2619 { 2620 struct inode *inode = file_inode(file); 2621 struct fuse_conn *fc = get_fuse_conn(inode); 2622 int err; 2623 2624 if (fc->no_flock) { 2625 err = locks_lock_file_wait(file, fl); 2626 } else { 2627 struct fuse_file *ff = file->private_data; 2628 2629 /* emulate flock with POSIX locks */ 2630 err = fuse_setlk(file, fl, 1); 2631 if (!err) 2632 ff->flock = true; 2633 } 2634 2635 return err; 2636 } 2637 2638 static sector_t fuse_bmap(struct address_space *mapping, sector_t block) 2639 { 2640 struct inode *inode = mapping->host; 2641 struct fuse_mount *fm = get_fuse_mount(inode); 2642 FUSE_ARGS(args); 2643 struct fuse_bmap_in inarg; 2644 struct fuse_bmap_out outarg; 2645 int err; 2646 2647 if (!inode->i_sb->s_bdev || fm->fc->no_bmap) 2648 return 0; 2649 2650 memset(&inarg, 0, sizeof(inarg)); 2651 inarg.block = block; 2652 inarg.blocksize = inode->i_sb->s_blocksize; 2653 args.opcode = FUSE_BMAP; 2654 args.nodeid = get_node_id(inode); 2655 args.in_numargs = 1; 2656 args.in_args[0].size = sizeof(inarg); 2657 args.in_args[0].value = &inarg; 2658 args.out_numargs = 1; 2659 args.out_args[0].size = sizeof(outarg); 2660 args.out_args[0].value = &outarg; 2661 err = fuse_simple_request(fm, &args); 2662 if (err == -ENOSYS) 2663 fm->fc->no_bmap = 1; 2664 2665 return err ? 0 : outarg.block; 2666 } 2667 2668 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) 2669 { 2670 struct inode *inode = file->f_mapping->host; 2671 struct fuse_mount *fm = get_fuse_mount(inode); 2672 struct fuse_file *ff = file->private_data; 2673 FUSE_ARGS(args); 2674 struct fuse_lseek_in inarg = { 2675 .fh = ff->fh, 2676 .offset = offset, 2677 .whence = whence 2678 }; 2679 struct fuse_lseek_out outarg; 2680 int err; 2681 2682 if (fm->fc->no_lseek) 2683 goto fallback; 2684 2685 args.opcode = FUSE_LSEEK; 2686 args.nodeid = ff->nodeid; 2687 args.in_numargs = 1; 2688 args.in_args[0].size = sizeof(inarg); 2689 args.in_args[0].value = &inarg; 2690 args.out_numargs = 1; 2691 args.out_args[0].size = sizeof(outarg); 2692 args.out_args[0].value = &outarg; 2693 err = fuse_simple_request(fm, &args); 2694 if (err) { 2695 if (err == -ENOSYS) { 2696 fm->fc->no_lseek = 1; 2697 goto fallback; 2698 } 2699 return err; 2700 } 2701 2702 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes); 2703 2704 fallback: 2705 err = fuse_update_attributes(inode, file, STATX_SIZE); 2706 if (!err) 2707 return generic_file_llseek(file, offset, whence); 2708 else 2709 return err; 2710 } 2711 2712 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) 2713 { 2714 loff_t retval; 2715 struct inode *inode = file_inode(file); 2716 2717 switch (whence) { 2718 case SEEK_SET: 2719 case SEEK_CUR: 2720 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ 2721 retval = generic_file_llseek(file, offset, whence); 2722 break; 2723 case SEEK_END: 2724 inode_lock(inode); 2725 retval = fuse_update_attributes(inode, file, STATX_SIZE); 2726 if (!retval) 2727 retval = generic_file_llseek(file, offset, whence); 2728 inode_unlock(inode); 2729 break; 2730 case SEEK_HOLE: 2731 case SEEK_DATA: 2732 inode_lock(inode); 2733 retval = fuse_lseek(file, offset, whence); 2734 inode_unlock(inode); 2735 break; 2736 default: 2737 retval = -EINVAL; 2738 } 2739 2740 return retval; 2741 } 2742 2743 static void fuse_do_truncate(struct file *file) 2744 { 2745 struct inode *inode = file->f_mapping->host; 2746 struct iattr attr; 2747 2748 attr.ia_valid = ATTR_SIZE; 2749 attr.ia_size = i_size_read(inode); 2750 2751 attr.ia_file = file; 2752 attr.ia_valid |= ATTR_FILE; 2753 2754 fuse_do_setattr(file_mnt_idmap(file), file_dentry(file), &attr, file); 2755 } 2756 2757 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off) 2758 { 2759 return round_up(off, fc->max_pages << PAGE_SHIFT); 2760 } 2761 2762 static ssize_t 2763 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 2764 { 2765 DECLARE_COMPLETION_ONSTACK(wait); 2766 ssize_t ret = 0; 2767 struct file *file = iocb->ki_filp; 2768 struct fuse_file *ff = file->private_data; 2769 loff_t pos = 0; 2770 struct inode *inode; 2771 loff_t i_size; 2772 size_t count = iov_iter_count(iter), shortened = 0; 2773 loff_t offset = iocb->ki_pos; 2774 struct fuse_io_priv *io; 2775 bool async = ff->fm->fc->async_dio; 2776 2777 pos = offset; 2778 inode = file->f_mapping->host; 2779 i_size = i_size_read(inode); 2780 2781 if ((iov_iter_rw(iter) == READ) && (offset >= i_size)) 2782 return 0; 2783 2784 if ((iov_iter_rw(iter) == WRITE) && async && !inode->i_sb->s_dio_done_wq) { 2785 ret = sb_init_dio_done_wq(inode->i_sb); 2786 if (ret < 0) 2787 return ret; 2788 } 2789 2790 io = kmalloc_obj(struct fuse_io_priv); 2791 if (!io) 2792 return -ENOMEM; 2793 spin_lock_init(&io->lock); 2794 kref_init(&io->refcnt); 2795 io->reqs = 1; 2796 io->bytes = -1; 2797 io->size = 0; 2798 io->offset = offset; 2799 io->write = (iov_iter_rw(iter) == WRITE); 2800 io->err = 0; 2801 /* 2802 * By default, we want to optimize all I/Os with async request 2803 * submission to the client filesystem if supported. 2804 */ 2805 io->async = async; 2806 io->iocb = iocb; 2807 io->blocking = is_sync_kiocb(iocb); 2808 2809 /* optimization for short read */ 2810 if (io->async && !io->write && offset + count > i_size) { 2811 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset)); 2812 shortened = count - iov_iter_count(iter); 2813 count -= shortened; 2814 } 2815 2816 /* 2817 * We cannot asynchronously extend the size of a file. 2818 * In such case the aio will behave exactly like sync io. 2819 */ 2820 if ((offset + count > i_size) && io->write) 2821 io->blocking = true; 2822 2823 if (io->async && io->blocking) { 2824 /* 2825 * Additional reference to keep io around after 2826 * calling fuse_aio_complete() 2827 */ 2828 kref_get(&io->refcnt); 2829 io->done = &wait; 2830 } 2831 2832 if (iov_iter_rw(iter) == WRITE) { 2833 ret = fuse_do_direct_io(io, iter, &pos, FUSE_DIO_WRITE); 2834 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE); 2835 } else { 2836 ret = __fuse_direct_read(io, iter, &pos); 2837 } 2838 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened); 2839 2840 if (io->async) { 2841 bool blocking = io->blocking; 2842 2843 fuse_aio_complete(io, ret < 0 ? ret : 0, -1); 2844 2845 /* we have a non-extending, async request, so return */ 2846 if (!blocking) 2847 return -EIOCBQUEUED; 2848 2849 wait_for_completion(&wait); 2850 ret = fuse_get_res_by_io(io); 2851 } 2852 2853 kref_put(&io->refcnt, fuse_io_release); 2854 2855 if (iov_iter_rw(iter) == WRITE) { 2856 fuse_write_update_attr(inode, pos, ret); 2857 /* For extending writes we already hold exclusive lock */ 2858 if (ret < 0 && offset + count > i_size) 2859 fuse_do_truncate(file); 2860 } 2861 2862 return ret; 2863 } 2864 2865 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end) 2866 { 2867 int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX); 2868 2869 if (!err) 2870 fuse_sync_writes(inode); 2871 2872 return err; 2873 } 2874 2875 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, 2876 loff_t length) 2877 { 2878 struct fuse_file *ff = file->private_data; 2879 struct inode *inode = file_inode(file); 2880 struct fuse_inode *fi = get_fuse_inode(inode); 2881 struct fuse_mount *fm = ff->fm; 2882 FUSE_ARGS(args); 2883 struct fuse_fallocate_in inarg = { 2884 .fh = ff->fh, 2885 .offset = offset, 2886 .length = length, 2887 .mode = mode 2888 }; 2889 int err; 2890 bool block_faults = FUSE_IS_VDAX(inode) && 2891 (!(mode & FALLOC_FL_KEEP_SIZE) || 2892 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))); 2893 2894 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 2895 FALLOC_FL_ZERO_RANGE)) 2896 return -EOPNOTSUPP; 2897 2898 if (fm->fc->no_fallocate) 2899 return -EOPNOTSUPP; 2900 2901 /* No size extend/hole punch for DAX yet */ 2902 if (IS_DAX(inode) && !FUSE_IS_VDAX(inode)) 2903 return -EOPNOTSUPP; 2904 2905 inode_lock(inode); 2906 if (block_faults) { 2907 filemap_invalidate_lock(inode->i_mapping); 2908 err = fuse_vdax_break_layouts(inode, 0, -1); 2909 if (err) 2910 goto out; 2911 } 2912 2913 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) { 2914 loff_t endbyte = offset + length - 1; 2915 2916 err = fuse_writeback_range(inode, offset, endbyte); 2917 if (err) 2918 goto out; 2919 } 2920 2921 if (!(mode & FALLOC_FL_KEEP_SIZE) && 2922 offset + length > i_size_read(inode)) { 2923 err = inode_newsize_ok(inode, offset + length); 2924 if (err) 2925 goto out; 2926 } 2927 2928 err = file_modified(file); 2929 if (err) 2930 goto out; 2931 2932 if (!(mode & FALLOC_FL_KEEP_SIZE)) 2933 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2934 2935 args.opcode = FUSE_FALLOCATE; 2936 args.nodeid = ff->nodeid; 2937 args.in_numargs = 1; 2938 args.in_args[0].size = sizeof(inarg); 2939 args.in_args[0].value = &inarg; 2940 err = fuse_simple_request(fm, &args); 2941 if (err == -ENOSYS) { 2942 fm->fc->no_fallocate = 1; 2943 err = -EOPNOTSUPP; 2944 } 2945 if (err) 2946 goto out; 2947 2948 /* we could have extended the file */ 2949 if (!(mode & FALLOC_FL_KEEP_SIZE)) { 2950 loff_t oldsize = i_size_read(inode); 2951 2952 if (offset + length > oldsize) 2953 truncate_pagecache_range(inode, oldsize, 2954 offset + length - 1); 2955 if (fuse_write_update_attr(inode, offset + length, length)) 2956 file_update_time(file); 2957 } 2958 2959 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) 2960 truncate_pagecache_range(inode, offset, offset + length - 1); 2961 2962 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE); 2963 2964 out: 2965 if (!(mode & FALLOC_FL_KEEP_SIZE)) 2966 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); 2967 2968 if (block_faults) 2969 filemap_invalidate_unlock(inode->i_mapping); 2970 2971 inode_unlock(inode); 2972 2973 fuse_flush_time_update(inode); 2974 2975 return err; 2976 } 2977 2978 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in, 2979 struct file *file_out, loff_t pos_out, 2980 size_t len, unsigned int flags) 2981 { 2982 struct fuse_file *ff_in = file_in->private_data; 2983 struct fuse_file *ff_out = file_out->private_data; 2984 struct inode *inode_in = file_inode(file_in); 2985 struct inode *inode_out = file_inode(file_out); 2986 struct fuse_inode *fi_out = get_fuse_inode(inode_out); 2987 struct fuse_mount *fm = ff_in->fm; 2988 struct fuse_conn *fc = fm->fc; 2989 FUSE_ARGS(args); 2990 struct fuse_copy_file_range_in inarg = { 2991 .fh_in = ff_in->fh, 2992 .off_in = pos_in, 2993 .nodeid_out = ff_out->nodeid, 2994 .fh_out = ff_out->fh, 2995 .off_out = pos_out, 2996 .len = len, 2997 .flags = flags 2998 }; 2999 struct fuse_write_out outarg; 3000 struct fuse_copy_file_range_out outarg_64; 3001 u64 bytes_copied; 3002 ssize_t err; 3003 /* mark unstable when write-back is not used, and file_out gets 3004 * extended */ 3005 bool is_unstable = (!fc->writeback_cache) && 3006 ((pos_out + len) > inode_out->i_size); 3007 3008 if (fc->no_copy_file_range) 3009 return -EOPNOTSUPP; 3010 3011 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) 3012 return -EXDEV; 3013 3014 inode_lock(inode_in); 3015 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1); 3016 inode_unlock(inode_in); 3017 if (err) 3018 return err; 3019 3020 inode_lock(inode_out); 3021 3022 err = file_modified(file_out); 3023 if (err) 3024 goto out; 3025 3026 /* 3027 * Write out dirty pages in the destination file before sending the COPY 3028 * request to userspace. After the request is completed, drop the 3029 * folios covering the copied range from the cache, since these contain 3030 * stale data at that point. 3031 * 3032 * This should be mostly correct, but if the COPY writes to a partial 3033 * folio (at the start or end) and the parts not covered by the COPY are 3034 * written through a memory map after calling fuse_writeback_range(), 3035 * then that folio is laundered before it is dropped, so the memory map 3036 * modifications are written back over the range the server just copied 3037 * into and the copied data is lost. 3038 * 3039 * It is unlikely that someone would rely on such mixed style 3040 * modifications. Yet this does give less guarantees than if the 3041 * copying was performed with write(2). 3042 * 3043 * To fix this a mapping->invalidate_lock could be used to prevent new 3044 * faults while the copy is ongoing. 3045 */ 3046 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1); 3047 if (err) 3048 goto out; 3049 3050 if (is_unstable) 3051 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); 3052 3053 args.opcode = FUSE_COPY_FILE_RANGE_64; 3054 args.nodeid = ff_in->nodeid; 3055 args.in_numargs = 1; 3056 args.in_args[0].size = sizeof(inarg); 3057 args.in_args[0].value = &inarg; 3058 args.out_numargs = 1; 3059 args.out_args[0].size = sizeof(outarg_64); 3060 args.out_args[0].value = &outarg_64; 3061 if (fc->no_copy_file_range_64) { 3062 fallback: 3063 /* Fall back to old op that can't handle large copy length */ 3064 args.opcode = FUSE_COPY_FILE_RANGE; 3065 args.out_args[0].size = sizeof(outarg); 3066 args.out_args[0].value = &outarg; 3067 inarg.len = len = min_t(size_t, len, UINT_MAX & PAGE_MASK); 3068 } 3069 err = fuse_simple_request(fm, &args); 3070 if (err == -ENOSYS) { 3071 if (args.opcode == FUSE_COPY_FILE_RANGE) { 3072 fc->no_copy_file_range = 1; 3073 err = -EOPNOTSUPP; 3074 } else { 3075 fc->no_copy_file_range_64 = 1; 3076 goto fallback; 3077 } 3078 } 3079 if (err) 3080 goto out; 3081 3082 bytes_copied = args.opcode == FUSE_COPY_FILE_RANGE ? 3083 outarg.size : outarg_64.bytes_copied; 3084 3085 if (bytes_copied > len) { 3086 err = -EIO; 3087 goto out; 3088 } 3089 3090 if (bytes_copied) 3091 invalidate_inode_pages2_range(inode_out->i_mapping, 3092 pos_out >> PAGE_SHIFT, 3093 (pos_out + bytes_copied - 1) >> PAGE_SHIFT); 3094 3095 file_update_time(file_out); 3096 fuse_write_update_attr(inode_out, pos_out + bytes_copied, bytes_copied); 3097 3098 err = bytes_copied; 3099 out: 3100 if (is_unstable) 3101 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); 3102 3103 inode_unlock(inode_out); 3104 file_accessed(file_in); 3105 3106 fuse_flush_time_update(inode_out); 3107 3108 return err; 3109 } 3110 3111 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off, 3112 struct file *dst_file, loff_t dst_off, 3113 size_t len, unsigned int flags) 3114 { 3115 ssize_t ret; 3116 3117 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off, 3118 len, flags); 3119 3120 if (ret == -EOPNOTSUPP || ret == -EXDEV) 3121 ret = splice_copy_file_range(src_file, src_off, dst_file, 3122 dst_off, len); 3123 return ret; 3124 } 3125 3126 static int fuse_file_fadvise(struct file *file, loff_t offset, loff_t len, int advice) 3127 { 3128 struct fuse_file *ff = file->private_data; 3129 3130 if (fuse_is_passthrough(ff)) 3131 return fuse_passthrough_fadvise(file, offset, len, advice); 3132 3133 return generic_fadvise(file, offset, len, advice); 3134 } 3135 3136 static const struct file_operations fuse_file_operations = { 3137 .llseek = fuse_file_llseek, 3138 .read_iter = fuse_file_read_iter, 3139 .write_iter = fuse_file_write_iter, 3140 .mmap = fuse_file_mmap, 3141 .open = fuse_open, 3142 .flush = fuse_flush, 3143 .release = fuse_release, 3144 .fsync = fuse_fsync, 3145 .lock = fuse_file_lock, 3146 .get_unmapped_area = thp_get_unmapped_area, 3147 .flock = fuse_file_flock, 3148 .splice_read = fuse_splice_read, 3149 .splice_write = fuse_splice_write, 3150 .unlocked_ioctl = fuse_file_ioctl, 3151 .compat_ioctl = fuse_file_compat_ioctl, 3152 .poll = fuse_file_poll, 3153 .fallocate = fuse_file_fallocate, 3154 .copy_file_range = fuse_copy_file_range, 3155 .setlease = generic_setlease, 3156 .fadvise = fuse_file_fadvise, 3157 }; 3158 3159 static const struct address_space_operations fuse_file_aops = { 3160 .read_folio = fuse_read_folio, 3161 .readahead = fuse_readahead, 3162 .writepages = fuse_writepages, 3163 .launder_folio = fuse_launder_folio, 3164 .dirty_folio = iomap_dirty_folio, 3165 .release_folio = iomap_release_folio, 3166 .invalidate_folio = iomap_invalidate_folio, 3167 .is_partially_uptodate = iomap_is_partially_uptodate, 3168 .migrate_folio = filemap_migrate_folio, 3169 .bmap = fuse_bmap, 3170 .direct_IO = fuse_direct_IO, 3171 }; 3172 3173 void fuse_init_file_inode(struct inode *inode, unsigned int flags) 3174 { 3175 struct fuse_inode *fi = get_fuse_inode(inode); 3176 struct fuse_conn *fc = get_fuse_conn(inode); 3177 unsigned int max_folio_pages; 3178 3179 inode->i_fop = &fuse_file_operations; 3180 inode->i_data.a_ops = &fuse_file_aops; 3181 if (fc->writeback_cache) 3182 mapping_set_writeback_may_deadlock_on_reclaim(&inode->i_data); 3183 3184 INIT_LIST_HEAD(&fi->write_files); 3185 INIT_LIST_HEAD(&fi->queued_writes); 3186 fi->writectr = 0; 3187 fi->iocachectr = 0; 3188 init_waitqueue_head(&fi->page_waitq); 3189 init_waitqueue_head(&fi->direct_io_waitq); 3190 3191 if (IS_ENABLED(CONFIG_FUSE_VDAX)) 3192 fuse_vdax_inode_init(inode, flags); 3193 3194 if (FUSE_IS_VDAX(inode)) 3195 return; 3196 3197 /* 3198 * A folio is never split across requests so cap the order so that one 3199 * always fits in a single request. 3200 */ 3201 max_folio_pages = min3(fc->max_write >> PAGE_SHIFT, 3202 fc->max_read >> PAGE_SHIFT, fc->max_pages); 3203 3204 if (max_folio_pages) 3205 mapping_set_folio_order_range(inode->i_mapping, 0, 3206 ilog2(max_folio_pages)); 3207 } 3208