1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2007-2009 Google Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following disclaimer 15 * in the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Google Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Copyright (C) 2005 Csaba Henk. 34 * All rights reserved. 35 * 36 * Copyright (c) 2019 The FreeBSD Foundation 37 * 38 * Portions of this software were developed by BFF Storage Systems, LLC under 39 * sponsorship from the FreeBSD Foundation. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #include <sys/cdefs.h> 64 __FBSDID("$FreeBSD$"); 65 66 #include <sys/types.h> 67 #include <sys/param.h> 68 #include <sys/module.h> 69 #include <sys/systm.h> 70 #include <sys/errno.h> 71 #include <sys/param.h> 72 #include <sys/kernel.h> 73 #include <sys/conf.h> 74 #include <sys/uio.h> 75 #include <sys/malloc.h> 76 #include <sys/queue.h> 77 #include <sys/lock.h> 78 #include <sys/sx.h> 79 #include <sys/mutex.h> 80 #include <sys/rwlock.h> 81 #include <sys/priv.h> 82 #include <sys/proc.h> 83 #include <sys/mount.h> 84 #include <sys/vnode.h> 85 #include <sys/stat.h> 86 #include <sys/unistd.h> 87 #include <sys/filedesc.h> 88 #include <sys/file.h> 89 #include <sys/fcntl.h> 90 #include <sys/bio.h> 91 #include <sys/buf.h> 92 #include <sys/sysctl.h> 93 #include <sys/vmmeter.h> 94 95 #include <vm/vm.h> 96 #include <vm/vm_extern.h> 97 #include <vm/pmap.h> 98 #include <vm/vm_map.h> 99 #include <vm/vm_page.h> 100 #include <vm/vm_object.h> 101 102 #include "fuse.h" 103 #include "fuse_file.h" 104 #include "fuse_node.h" 105 #include "fuse_internal.h" 106 #include "fuse_ipc.h" 107 #include "fuse_io.h" 108 109 /* 110 * Set in a struct buf to indicate that the write came from the buffer cache 111 * and the originating cred and pid are no longer known. 112 */ 113 #define B_FUSEFS_WRITE_CACHE B_FS_FLAG1 114 115 SDT_PROVIDER_DECLARE(fusefs); 116 /* 117 * Fuse trace probe: 118 * arg0: verbosity. Higher numbers give more verbose messages 119 * arg1: Textual message 120 */ 121 SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*"); 122 123 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_start, "int", "int", "int", "int"); 124 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "struct buf*"); 125 SDT_PROBE_DEFINE4(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int", 126 "struct buf*"); 127 int 128 fuse_read_biobackend(struct vnode *vp, struct uio *uio, int ioflag, 129 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid) 130 { 131 struct buf *bp; 132 struct mount *mp; 133 struct fuse_data *data; 134 daddr_t lbn, nextlbn; 135 int bcount, nextsize; 136 int err, n = 0, on = 0, seqcount; 137 off_t filesize; 138 139 const int biosize = fuse_iosize(vp); 140 mp = vnode_mount(vp); 141 data = fuse_get_mpdata(mp); 142 143 if (uio->uio_offset < 0) 144 return (EINVAL); 145 146 seqcount = ioflag >> IO_SEQSHIFT; 147 148 err = fuse_vnode_size(vp, &filesize, cred, curthread); 149 if (err) 150 return err; 151 152 for (err = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { 153 if (fuse_isdeadfs(vp)) { 154 err = ENXIO; 155 break; 156 } 157 if (filesize - uio->uio_offset <= 0) 158 break; 159 lbn = uio->uio_offset / biosize; 160 on = uio->uio_offset & (biosize - 1); 161 162 if ((off_t)lbn * biosize >= filesize) { 163 bcount = 0; 164 } else if ((off_t)(lbn + 1) * biosize > filesize) { 165 bcount = filesize - (off_t)lbn *biosize; 166 } else { 167 bcount = biosize; 168 } 169 nextlbn = lbn + 1; 170 nextsize = MIN(biosize, filesize - nextlbn * biosize); 171 172 SDT_PROBE4(fusefs, , io, read_bio_backend_start, 173 biosize, (int)lbn, on, bcount); 174 175 if (bcount < biosize) { 176 /* If near EOF, don't do readahead */ 177 err = bread(vp, lbn, bcount, NOCRED, &bp); 178 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 179 /* Try clustered read */ 180 long totread = uio->uio_resid + on; 181 seqcount = MIN(seqcount, 182 data->max_readahead_blocks + 1); 183 err = cluster_read(vp, filesize, lbn, bcount, NOCRED, 184 totread, seqcount, 0, &bp); 185 } else if (seqcount > 1 && data->max_readahead_blocks >= 1) { 186 /* Try non-clustered readahead */ 187 err = breadn(vp, lbn, bcount, &nextlbn, &nextsize, 1, 188 NOCRED, &bp); 189 } else { 190 /* Just read what was requested */ 191 err = bread(vp, lbn, bcount, NOCRED, &bp); 192 } 193 194 if (err) { 195 brelse(bp); 196 bp = NULL; 197 break; 198 } 199 200 /* 201 * on is the offset into the current bp. Figure out how many 202 * bytes we can copy out of the bp. Note that bcount is 203 * NOT DEV_BSIZE aligned. 204 * 205 * Then figure out how many bytes we can copy into the uio. 206 */ 207 208 n = 0; 209 if (on < bcount - bp->b_resid) 210 n = MIN((unsigned)(bcount - bp->b_resid - on), 211 uio->uio_resid); 212 if (n > 0) { 213 SDT_PROBE2(fusefs, , io, read_bio_backend_feed, n, bp); 214 err = uiomove(bp->b_data + on, n, uio); 215 } 216 vfs_bio_brelse(bp, ioflag); 217 SDT_PROBE4(fusefs, , io, read_bio_backend_end, err, 218 uio->uio_resid, n, bp); 219 if (bp->b_resid > 0) { 220 /* Short read indicates EOF */ 221 break; 222 } 223 } 224 225 return (err); 226 } 227 228 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start, 229 "struct fuse_read_in*"); 230 SDT_PROBE_DEFINE3(fusefs, , io, read_directbackend_complete, 231 "struct fuse_dispatcher*", "struct fuse_read_in*", "struct uio*"); 232 233 int 234 fuse_read_directbackend(struct vnode *vp, struct uio *uio, 235 struct ucred *cred, struct fuse_filehandle *fufh) 236 { 237 struct fuse_data *data; 238 struct fuse_dispatcher fdi; 239 struct fuse_read_in *fri; 240 int err = 0; 241 242 data = fuse_get_mpdata(vp->v_mount); 243 244 if (uio->uio_resid == 0) 245 return (0); 246 247 fdisp_init(&fdi, 0); 248 249 /* 250 * XXX In "normal" case we use an intermediate kernel buffer for 251 * transmitting data from daemon's context to ours. Eventually, we should 252 * get rid of this. Anyway, if the target uio lives in sysspace (we are 253 * called from pageops), and the input data doesn't need kernel-side 254 * processing (we are not called from readdir) we can already invoke 255 * an optimized, "peer-to-peer" I/O routine. 256 */ 257 while (uio->uio_resid > 0) { 258 fdi.iosize = sizeof(*fri); 259 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred); 260 fri = fdi.indata; 261 fri->fh = fufh->fh_id; 262 fri->offset = uio->uio_offset; 263 fri->size = MIN(uio->uio_resid, 264 fuse_get_mpdata(vp->v_mount)->max_read); 265 if (fuse_libabi_geq(data, 7, 9)) { 266 /* See comment regarding FUSE_WRITE_LOCKOWNER */ 267 fri->read_flags = 0; 268 fri->flags = fufh_type_2_fflags(fufh->fufh_type); 269 } 270 271 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri); 272 273 if ((err = fdisp_wait_answ(&fdi))) 274 goto out; 275 276 SDT_PROBE3(fusefs, , io, read_directbackend_complete, 277 &fdi, fri, uio); 278 279 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio))) 280 break; 281 if (fdi.iosize < fri->size) { 282 /* 283 * Short read. Should only happen at EOF or with 284 * direct io. 285 */ 286 break; 287 } 288 } 289 290 out: 291 fdisp_destroy(&fdi); 292 return (err); 293 } 294 295 int 296 fuse_write_directbackend(struct vnode *vp, struct uio *uio, 297 struct ucred *cred, struct fuse_filehandle *fufh, off_t filesize, 298 int ioflag, bool pages) 299 { 300 struct fuse_vnode_data *fvdat = VTOFUD(vp); 301 struct fuse_data *data; 302 struct fuse_write_in *fwi; 303 struct fuse_write_out *fwo; 304 struct fuse_dispatcher fdi; 305 size_t chunksize; 306 void *fwi_data; 307 off_t as_written_offset; 308 int diff; 309 int err = 0; 310 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO; 311 bool wrote_anything = false; 312 uint32_t write_flags; 313 314 data = fuse_get_mpdata(vp->v_mount); 315 316 /* 317 * Don't set FUSE_WRITE_LOCKOWNER in write_flags. It can't be set 318 * accurately when using POSIX AIO, libfuse doesn't use it, and I'm not 319 * aware of any file systems that do. It was an attempt to add 320 * Linux-style mandatory locking to the FUSE protocol, but mandatory 321 * locking is deprecated even on Linux. See Linux commit 322 * f33321141b273d60cbb3a8f56a5489baad82ba5e . 323 */ 324 /* 325 * Set FUSE_WRITE_CACHE whenever we don't know the uid, gid, and/or pid 326 * that originated a write. For example when writing from the 327 * writeback cache. I don't know of a single file system that cares, 328 * but the protocol says we're supposed to do this. 329 */ 330 write_flags = !pages && ( 331 (ioflag & IO_DIRECT) || 332 !fsess_opt_datacache(vnode_mount(vp)) || 333 !fsess_opt_writeback(vnode_mount(vp))) ? 0 : FUSE_WRITE_CACHE; 334 335 if (uio->uio_resid == 0) 336 return (0); 337 338 if (ioflag & IO_APPEND) 339 uio_setoffset(uio, filesize); 340 341 err = vn_rlimit_fsize(vp, uio, uio->uio_td); 342 if (err != 0) 343 return (err); 344 345 fdisp_init(&fdi, 0); 346 347 while (uio->uio_resid > 0) { 348 size_t sizeof_fwi; 349 350 if (fuse_libabi_geq(data, 7, 9)) { 351 sizeof_fwi = sizeof(*fwi); 352 } else { 353 sizeof_fwi = FUSE_COMPAT_WRITE_IN_SIZE; 354 } 355 356 chunksize = MIN(uio->uio_resid, data->max_write); 357 358 fdi.iosize = sizeof_fwi + chunksize; 359 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred); 360 361 fwi = fdi.indata; 362 fwi->fh = fufh->fh_id; 363 fwi->offset = uio->uio_offset; 364 fwi->size = chunksize; 365 fwi->write_flags = write_flags; 366 if (fuse_libabi_geq(data, 7, 9)) { 367 fwi->flags = fufh_type_2_fflags(fufh->fufh_type); 368 } 369 fwi_data = (char *)fdi.indata + sizeof_fwi; 370 371 if ((err = uiomove(fwi_data, chunksize, uio))) 372 break; 373 374 retry: 375 err = fdisp_wait_answ(&fdi); 376 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) { 377 /* 378 * Rewind the uio so dofilewrite will know it's 379 * incomplete 380 */ 381 uio->uio_resid += fwi->size; 382 uio->uio_offset -= fwi->size; 383 /* 384 * Change ERESTART into EINTR because we can't rewind 385 * uio->uio_iov. Basically, once uiomove(9) has been 386 * called, it's impossible to restart a syscall. 387 */ 388 if (err == ERESTART) 389 err = EINTR; 390 break; 391 } else if (err) { 392 break; 393 } else { 394 wrote_anything = true; 395 } 396 397 fwo = ((struct fuse_write_out *)fdi.answ); 398 399 if (fwo->size > fwi->size) { 400 fuse_warn(data, FSESS_WARN_WROTE_LONG, 401 "wrote more data than we provided it."); 402 /* This is bonkers. Clear attr cache. */ 403 fvdat->flag &= ~FN_SIZECHANGE; 404 fuse_vnode_clear_attr_cache(vp); 405 err = EINVAL; 406 break; 407 } 408 409 /* Adjust the uio in the case of short writes */ 410 diff = fwi->size - fwo->size; 411 412 as_written_offset = uio->uio_offset - diff; 413 414 if (as_written_offset - diff > filesize) { 415 fuse_vnode_setsize(vp, as_written_offset, false); 416 getnanouptime(&fvdat->last_local_modify); 417 } 418 if (as_written_offset - diff >= filesize) 419 fvdat->flag &= ~FN_SIZECHANGE; 420 421 if (diff > 0) { 422 /* Short write */ 423 if (!direct_io) { 424 fuse_warn(data, FSESS_WARN_SHORT_WRITE, 425 "short writes are only allowed with " 426 "direct_io."); 427 } 428 if (ioflag & IO_DIRECT) { 429 /* Return early */ 430 uio->uio_resid += diff; 431 uio->uio_offset -= diff; 432 break; 433 } else { 434 /* Resend the unwritten portion of data */ 435 fdi.iosize = sizeof_fwi + diff; 436 /* Refresh fdi without clearing data buffer */ 437 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp, 438 uio->uio_td, cred); 439 fwi = fdi.indata; 440 MPASS2(fwi == fdi.indata, "FUSE dispatcher " 441 "reallocated despite no increase in " 442 "size?"); 443 void *src = (char*)fwi_data + fwo->size; 444 memmove(fwi_data, src, diff); 445 fwi->fh = fufh->fh_id; 446 fwi->offset = as_written_offset; 447 fwi->size = diff; 448 fwi->write_flags = write_flags; 449 goto retry; 450 } 451 } 452 } 453 454 fdisp_destroy(&fdi); 455 456 if (wrote_anything) 457 fuse_vnode_undirty_cached_timestamps(vp, false); 458 459 return (err); 460 } 461 462 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int", 463 "struct uio*", "int", "bool"); 464 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int"); 465 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*"); 466 467 int 468 fuse_write_biobackend(struct vnode *vp, struct uio *uio, 469 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid) 470 { 471 struct fuse_vnode_data *fvdat = VTOFUD(vp); 472 struct buf *bp; 473 daddr_t lbn; 474 off_t filesize; 475 int bcount; 476 int n, on, seqcount, err = 0; 477 478 const int biosize = fuse_iosize(vp); 479 480 seqcount = ioflag >> IO_SEQSHIFT; 481 482 KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode")); 483 if (vp->v_type != VREG) 484 return (EIO); 485 if (uio->uio_offset < 0) 486 return (EINVAL); 487 if (uio->uio_resid == 0) 488 return (0); 489 490 err = fuse_vnode_size(vp, &filesize, cred, curthread); 491 if (err) 492 return err; 493 494 if (ioflag & IO_APPEND) 495 uio_setoffset(uio, filesize); 496 497 err = vn_rlimit_fsize(vp, uio, uio->uio_td); 498 if (err != 0) 499 return (err); 500 501 do { 502 bool direct_append, extending; 503 504 if (fuse_isdeadfs(vp)) { 505 err = ENXIO; 506 break; 507 } 508 lbn = uio->uio_offset / biosize; 509 on = uio->uio_offset & (biosize - 1); 510 n = MIN((unsigned)(biosize - on), uio->uio_resid); 511 512 again: 513 /* Get or create a buffer for the write */ 514 direct_append = uio->uio_offset == filesize && n; 515 if (uio->uio_offset + n < filesize) { 516 extending = false; 517 if ((off_t)(lbn + 1) * biosize < filesize) { 518 /* Not the file's last block */ 519 bcount = biosize; 520 } else { 521 /* The file's last block */ 522 bcount = filesize - (off_t)lbn * biosize; 523 } 524 } else { 525 extending = true; 526 bcount = on + n; 527 } 528 if (direct_append) { 529 /* 530 * Take care to preserve the buffer's B_CACHE state so 531 * as not to cause an unnecessary read. 532 */ 533 bp = getblk(vp, lbn, on, PCATCH, 0, 0); 534 if (bp != NULL) { 535 uint32_t save = bp->b_flags & B_CACHE; 536 allocbuf(bp, bcount); 537 bp->b_flags |= save; 538 } 539 } else { 540 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0); 541 } 542 if (!bp) { 543 err = EINTR; 544 break; 545 } 546 if (extending) { 547 /* 548 * Extend file _after_ locking buffer so we won't race 549 * with other readers 550 */ 551 err = fuse_vnode_setsize(vp, uio->uio_offset + n, false); 552 filesize = uio->uio_offset + n; 553 getnanouptime(&fvdat->last_local_modify); 554 fvdat->flag |= FN_SIZECHANGE; 555 if (err) { 556 brelse(bp); 557 break; 558 } 559 } 560 561 SDT_PROBE6(fusefs, , io, write_biobackend_start, 562 lbn, on, n, uio, bcount, direct_append); 563 /* 564 * Issue a READ if B_CACHE is not set. In special-append 565 * mode, B_CACHE is based on the buffer prior to the write 566 * op and is typically set, avoiding the read. If a read 567 * is required in special append mode, the server will 568 * probably send us a short-read since we extended the file 569 * on our end, resulting in b_resid == 0 and, thusly, 570 * B_CACHE getting set. 571 * 572 * We can also avoid issuing the read if the write covers 573 * the entire buffer. We have to make sure the buffer state 574 * is reasonable in this case since we will not be initiating 575 * I/O. See the comments in kern/vfs_bio.c's getblk() for 576 * more information. 577 * 578 * B_CACHE may also be set due to the buffer being cached 579 * normally. 580 */ 581 582 if (on == 0 && n == bcount) { 583 bp->b_flags |= B_CACHE; 584 bp->b_flags &= ~B_INVAL; 585 bp->b_ioflags &= ~BIO_ERROR; 586 } 587 if ((bp->b_flags & B_CACHE) == 0) { 588 bp->b_iocmd = BIO_READ; 589 vfs_busy_pages(bp, 0); 590 fuse_io_strategy(vp, bp); 591 if ((err = bp->b_error)) { 592 brelse(bp); 593 break; 594 } 595 if (bp->b_resid > 0) { 596 /* 597 * Short read indicates EOF. Update file size 598 * from the server and try again. 599 */ 600 SDT_PROBE2(fusefs, , io, trace, 1, 601 "Short read during a RMW"); 602 brelse(bp); 603 err = fuse_vnode_size(vp, &filesize, cred, 604 curthread); 605 if (err) 606 break; 607 else 608 goto again; 609 } 610 } 611 if (bp->b_wcred == NOCRED) 612 bp->b_wcred = crhold(cred); 613 614 /* 615 * If dirtyend exceeds file size, chop it down. This should 616 * not normally occur but there is an append race where it 617 * might occur XXX, so we log it. 618 * 619 * If the chopping creates a reverse-indexed or degenerate 620 * situation with dirtyoff/end, we 0 both of them. 621 */ 622 if (bp->b_dirtyend > bcount) { 623 SDT_PROBE2(fusefs, , io, write_biobackend_append_race, 624 (long)bp->b_blkno * biosize, 625 bp->b_dirtyend - bcount); 626 bp->b_dirtyend = bcount; 627 } 628 if (bp->b_dirtyoff >= bp->b_dirtyend) 629 bp->b_dirtyoff = bp->b_dirtyend = 0; 630 631 /* 632 * If the new write will leave a contiguous dirty 633 * area, just update the b_dirtyoff and b_dirtyend, 634 * otherwise force a write rpc of the old dirty area. 635 * 636 * While it is possible to merge discontiguous writes due to 637 * our having a B_CACHE buffer ( and thus valid read data 638 * for the hole), we don't because it could lead to 639 * significant cache coherency problems with multiple clients, 640 * especially if locking is implemented later on. 641 * 642 * as an optimization we could theoretically maintain 643 * a linked list of discontinuous areas, but we would still 644 * have to commit them separately so there isn't much 645 * advantage to it except perhaps a bit of asynchronization. 646 */ 647 648 if (bp->b_dirtyend > 0 && 649 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 650 /* 651 * Yes, we mean it. Write out everything to "storage" 652 * immediately, without hesitation. (Apart from other 653 * reasons: the only way to know if a write is valid 654 * if its actually written out.) 655 */ 656 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp); 657 bwrite(bp); 658 if (bp->b_error == EINTR) { 659 err = EINTR; 660 break; 661 } 662 goto again; 663 } 664 err = uiomove((char *)bp->b_data + on, n, uio); 665 666 if (err) { 667 bp->b_ioflags |= BIO_ERROR; 668 bp->b_error = err; 669 brelse(bp); 670 break; 671 /* TODO: vfs_bio_clrbuf like ffs_write does? */ 672 } 673 /* 674 * Only update dirtyoff/dirtyend if not a degenerate 675 * condition. 676 */ 677 if (n) { 678 if (bp->b_dirtyend > 0) { 679 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff); 680 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend); 681 } else { 682 bp->b_dirtyoff = on; 683 bp->b_dirtyend = on + n; 684 } 685 vfs_bio_set_valid(bp, on, n); 686 } 687 688 vfs_bio_set_flags(bp, ioflag); 689 690 bp->b_flags |= B_FUSEFS_WRITE_CACHE; 691 if (ioflag & IO_SYNC) { 692 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp); 693 if (!(ioflag & IO_VMIO)) 694 bp->b_flags &= ~B_FUSEFS_WRITE_CACHE; 695 err = bwrite(bp); 696 } else if (vm_page_count_severe() || 697 buf_dirty_count_severe() || 698 (ioflag & IO_ASYNC)) { 699 bp->b_flags |= B_CLUSTEROK; 700 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp); 701 bawrite(bp); 702 } else if (on == 0 && n == bcount) { 703 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) { 704 bp->b_flags |= B_CLUSTEROK; 705 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 706 4, bp); 707 cluster_write(vp, &fvdat->clusterw, bp, 708 filesize, seqcount, 0); 709 } else { 710 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 711 5, bp); 712 bawrite(bp); 713 } 714 } else if (ioflag & IO_DIRECT) { 715 bp->b_flags |= B_CLUSTEROK; 716 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp); 717 bawrite(bp); 718 } else { 719 bp->b_flags &= ~B_CLUSTEROK; 720 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp); 721 bdwrite(bp); 722 } 723 if (err) 724 break; 725 } while (uio->uio_resid > 0 && n > 0); 726 727 return (err); 728 } 729 730 int 731 fuse_io_strategy(struct vnode *vp, struct buf *bp) 732 { 733 struct fuse_vnode_data *fvdat = VTOFUD(vp); 734 struct fuse_filehandle *fufh; 735 struct ucred *cred; 736 struct uio *uiop; 737 struct uio uio; 738 struct iovec io; 739 off_t filesize; 740 int error = 0; 741 int fflag; 742 /* We don't know the true pid when we're dealing with the cache */ 743 pid_t pid = 0; 744 745 const int biosize = fuse_iosize(vp); 746 747 MPASS(vp->v_type == VREG || vp->v_type == VDIR); 748 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE); 749 750 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE; 751 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred; 752 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid); 753 if (bp->b_iocmd == BIO_READ && error == EBADF) { 754 /* 755 * This may be a read-modify-write operation on a cached file 756 * opened O_WRONLY. The FUSE protocol allows this. 757 */ 758 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid); 759 } 760 if (error) { 761 printf("FUSE: strategy: filehandles are closed\n"); 762 bp->b_ioflags |= BIO_ERROR; 763 bp->b_error = error; 764 bufdone(bp); 765 return (error); 766 } 767 768 uiop = &uio; 769 uiop->uio_iov = &io; 770 uiop->uio_iovcnt = 1; 771 uiop->uio_segflg = UIO_SYSSPACE; 772 uiop->uio_td = curthread; 773 774 /* 775 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We 776 * do this here so we do not have to do it in all the code that 777 * calls us. 778 */ 779 bp->b_flags &= ~B_INVAL; 780 bp->b_ioflags &= ~BIO_ERROR; 781 782 KASSERT(!(bp->b_flags & B_DONE), 783 ("fuse_io_strategy: bp %p already marked done", bp)); 784 if (bp->b_iocmd == BIO_READ) { 785 ssize_t left; 786 787 io.iov_len = uiop->uio_resid = bp->b_bcount; 788 io.iov_base = bp->b_data; 789 uiop->uio_rw = UIO_READ; 790 791 uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize; 792 error = fuse_read_directbackend(vp, uiop, cred, fufh); 793 /* 794 * Store the amount we failed to read in the buffer's private 795 * field, so callers can truncate the file if necessary' 796 */ 797 798 if (!error && uiop->uio_resid) { 799 int nread = bp->b_bcount - uiop->uio_resid; 800 left = uiop->uio_resid; 801 bzero((char *)bp->b_data + nread, left); 802 803 if ((fvdat->flag & FN_SIZECHANGE) == 0) { 804 /* 805 * A short read with no error, when not using 806 * direct io, and when no writes are cached, 807 * indicates EOF caused by a server-side 808 * truncation. Clear the attr cache so we'll 809 * pick up the new file size and timestamps. 810 * 811 * We must still bzero the remaining buffer so 812 * uninitialized data doesn't get exposed by a 813 * future truncate that extends the file. 814 * 815 * To prevent lock order problems, we must 816 * truncate the file upstack, not here. 817 */ 818 SDT_PROBE2(fusefs, , io, trace, 1, 819 "Short read of a clean file"); 820 fuse_vnode_clear_attr_cache(vp); 821 } else { 822 /* 823 * If dirty writes _are_ cached beyond EOF, 824 * that indicates a newly created hole that the 825 * server doesn't know about. Those don't pose 826 * any problem. 827 * XXX: we don't currently track whether dirty 828 * writes are cached beyond EOF, before EOF, or 829 * both. 830 */ 831 SDT_PROBE2(fusefs, , io, trace, 1, 832 "Short read of a dirty file"); 833 uiop->uio_resid = 0; 834 } 835 } 836 if (error) { 837 bp->b_ioflags |= BIO_ERROR; 838 bp->b_error = error; 839 } 840 } else { 841 /* 842 * Setup for actual write 843 */ 844 /* 845 * If the file's size is cached, use that value, even if the 846 * cache is expired. At this point we're already committed to 847 * writing something. If the FUSE server has changed the 848 * file's size behind our back, it's too late for us to do 849 * anything about it. In particular, we can't invalidate any 850 * part of the file's buffers because VOP_STRATEGY is called 851 * with them already locked. 852 */ 853 filesize = fvdat->cached_attrs.va_size; 854 /* filesize must've been cached by fuse_vnop_open. */ 855 KASSERT(filesize != VNOVAL, ("filesize should've been cached")); 856 857 if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize) 858 bp->b_dirtyend = filesize - 859 (off_t)bp->b_lblkno * biosize; 860 861 if (bp->b_dirtyend > bp->b_dirtyoff) { 862 io.iov_len = uiop->uio_resid = bp->b_dirtyend 863 - bp->b_dirtyoff; 864 uiop->uio_offset = (off_t)bp->b_lblkno * biosize 865 + bp->b_dirtyoff; 866 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 867 uiop->uio_rw = UIO_WRITE; 868 869 bool pages = bp->b_flags & B_FUSEFS_WRITE_CACHE; 870 error = fuse_write_directbackend(vp, uiop, cred, fufh, 871 filesize, 0, pages); 872 873 if (error == EINTR || error == ETIMEDOUT) { 874 bp->b_flags &= ~(B_INVAL | B_NOCACHE); 875 if ((bp->b_flags & B_PAGING) == 0) { 876 bdirty(bp); 877 bp->b_flags &= ~B_DONE; 878 } 879 if ((error == EINTR || error == ETIMEDOUT) && 880 (bp->b_flags & B_ASYNC) == 0) 881 bp->b_flags |= B_EINTR; 882 } else { 883 if (error) { 884 bp->b_ioflags |= BIO_ERROR; 885 bp->b_flags |= B_INVAL; 886 bp->b_error = error; 887 } 888 bp->b_dirtyoff = bp->b_dirtyend = 0; 889 } 890 } else { 891 bp->b_resid = 0; 892 bufdone(bp); 893 return (0); 894 } 895 } 896 bp->b_resid = uiop->uio_resid; 897 bufdone(bp); 898 return (error); 899 } 900 901 int 902 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td) 903 { 904 905 return (vn_fsync_buf(vp, waitfor)); 906 } 907 908 /* 909 * Flush and invalidate all dirty buffers. If another process is already 910 * doing the flush, just wait for completion. 911 */ 912 int 913 fuse_io_invalbuf(struct vnode *vp, struct thread *td) 914 { 915 struct fuse_vnode_data *fvdat = VTOFUD(vp); 916 int error = 0; 917 918 if (VN_IS_DOOMED(vp)) 919 return 0; 920 921 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf"); 922 923 while (fvdat->flag & FN_FLUSHINPROG) { 924 struct proc *p = td->td_proc; 925 926 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) 927 return EIO; 928 fvdat->flag |= FN_FLUSHWANT; 929 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz); 930 error = 0; 931 if (p != NULL) { 932 PROC_LOCK(p); 933 if (SIGNOTEMPTY(p->p_siglist) || 934 SIGNOTEMPTY(td->td_siglist)) 935 error = EINTR; 936 PROC_UNLOCK(p); 937 } 938 if (error == EINTR) 939 return EINTR; 940 } 941 fvdat->flag |= FN_FLUSHINPROG; 942 943 if (vp->v_bufobj.bo_object != NULL) { 944 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object); 945 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC); 946 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object); 947 } 948 error = vinvalbuf(vp, V_SAVE, PCATCH, 0); 949 while (error) { 950 if (error == ERESTART || error == EINTR) { 951 fvdat->flag &= ~FN_FLUSHINPROG; 952 if (fvdat->flag & FN_FLUSHWANT) { 953 fvdat->flag &= ~FN_FLUSHWANT; 954 wakeup(&fvdat->flag); 955 } 956 return EINTR; 957 } 958 error = vinvalbuf(vp, V_SAVE, PCATCH, 0); 959 } 960 fvdat->flag &= ~FN_FLUSHINPROG; 961 if (fvdat->flag & FN_FLUSHWANT) { 962 fvdat->flag &= ~FN_FLUSHWANT; 963 wakeup(&fvdat->flag); 964 } 965 return (error); 966 } 967