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