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