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