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