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 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 342 return (EFBIG); 343 344 fdisp_init(&fdi, 0); 345 346 while (uio->uio_resid > 0) { 347 size_t sizeof_fwi; 348 349 if (fuse_libabi_geq(data, 7, 9)) { 350 sizeof_fwi = sizeof(*fwi); 351 } else { 352 sizeof_fwi = FUSE_COMPAT_WRITE_IN_SIZE; 353 } 354 355 chunksize = MIN(uio->uio_resid, data->max_write); 356 357 fdi.iosize = sizeof_fwi + chunksize; 358 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred); 359 360 fwi = fdi.indata; 361 fwi->fh = fufh->fh_id; 362 fwi->offset = uio->uio_offset; 363 fwi->size = chunksize; 364 fwi->write_flags = write_flags; 365 if (fuse_libabi_geq(data, 7, 9)) { 366 fwi->flags = fufh_type_2_fflags(fufh->fufh_type); 367 } 368 fwi_data = (char *)fdi.indata + sizeof_fwi; 369 370 if ((err = uiomove(fwi_data, chunksize, uio))) 371 break; 372 373 retry: 374 err = fdisp_wait_answ(&fdi); 375 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) { 376 /* 377 * Rewind the uio so dofilewrite will know it's 378 * incomplete 379 */ 380 uio->uio_resid += fwi->size; 381 uio->uio_offset -= fwi->size; 382 /* 383 * Change ERESTART into EINTR because we can't rewind 384 * uio->uio_iov. Basically, once uiomove(9) has been 385 * called, it's impossible to restart a syscall. 386 */ 387 if (err == ERESTART) 388 err = EINTR; 389 break; 390 } else if (err) { 391 break; 392 } else { 393 wrote_anything = true; 394 } 395 396 fwo = ((struct fuse_write_out *)fdi.answ); 397 398 /* Adjust the uio in the case of short writes */ 399 diff = fwi->size - fwo->size; 400 as_written_offset = uio->uio_offset - diff; 401 402 if (as_written_offset - diff > filesize) { 403 fuse_vnode_setsize(vp, as_written_offset, false); 404 getnanouptime(&fvdat->last_local_modify); 405 } 406 if (as_written_offset - diff >= filesize) 407 fvdat->flag &= ~FN_SIZECHANGE; 408 409 if (diff < 0) { 410 fuse_warn(data, FSESS_WARN_WROTE_LONG, 411 "wrote more data than we provided it."); 412 err = EINVAL; 413 break; 414 } else if (diff > 0) { 415 /* Short write */ 416 if (!direct_io) { 417 fuse_warn(data, FSESS_WARN_SHORT_WRITE, 418 "short writes are only allowed with " 419 "direct_io."); 420 } 421 if (ioflag & IO_DIRECT) { 422 /* Return early */ 423 uio->uio_resid += diff; 424 uio->uio_offset -= diff; 425 break; 426 } else { 427 /* Resend the unwritten portion of data */ 428 fdi.iosize = sizeof_fwi + diff; 429 /* Refresh fdi without clearing data buffer */ 430 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp, 431 uio->uio_td, cred); 432 fwi = fdi.indata; 433 MPASS2(fwi == fdi.indata, "FUSE dispatcher " 434 "reallocated despite no increase in " 435 "size?"); 436 void *src = (char*)fwi_data + fwo->size; 437 memmove(fwi_data, src, diff); 438 fwi->fh = fufh->fh_id; 439 fwi->offset = as_written_offset; 440 fwi->size = diff; 441 fwi->write_flags = write_flags; 442 goto retry; 443 } 444 } 445 } 446 447 fdisp_destroy(&fdi); 448 449 if (wrote_anything) 450 fuse_vnode_undirty_cached_timestamps(vp, false); 451 452 return (err); 453 } 454 455 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int", 456 "struct uio*", "int", "bool"); 457 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int"); 458 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_issue, "int", "struct buf*"); 459 460 int 461 fuse_write_biobackend(struct vnode *vp, struct uio *uio, 462 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid) 463 { 464 struct fuse_vnode_data *fvdat = VTOFUD(vp); 465 struct buf *bp; 466 daddr_t lbn; 467 off_t filesize; 468 int bcount; 469 int n, on, seqcount, err = 0; 470 471 const int biosize = fuse_iosize(vp); 472 473 seqcount = ioflag >> IO_SEQSHIFT; 474 475 KASSERT(uio->uio_rw == UIO_WRITE, ("fuse_write_biobackend mode")); 476 if (vp->v_type != VREG) 477 return (EIO); 478 if (uio->uio_offset < 0) 479 return (EINVAL); 480 if (uio->uio_resid == 0) 481 return (0); 482 483 err = fuse_vnode_size(vp, &filesize, cred, curthread); 484 if (err) 485 return err; 486 487 if (ioflag & IO_APPEND) 488 uio_setoffset(uio, filesize); 489 490 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 491 return (EFBIG); 492 493 do { 494 bool direct_append, extending; 495 496 if (fuse_isdeadfs(vp)) { 497 err = ENXIO; 498 break; 499 } 500 lbn = uio->uio_offset / biosize; 501 on = uio->uio_offset & (biosize - 1); 502 n = MIN((unsigned)(biosize - on), uio->uio_resid); 503 504 again: 505 /* Get or create a buffer for the write */ 506 direct_append = uio->uio_offset == filesize && n; 507 if (uio->uio_offset + n < filesize) { 508 extending = false; 509 if ((off_t)(lbn + 1) * biosize < filesize) { 510 /* Not the file's last block */ 511 bcount = biosize; 512 } else { 513 /* The file's last block */ 514 bcount = filesize - (off_t)lbn * biosize; 515 } 516 } else { 517 extending = true; 518 bcount = on + n; 519 } 520 if (direct_append) { 521 /* 522 * Take care to preserve the buffer's B_CACHE state so 523 * as not to cause an unnecessary read. 524 */ 525 bp = getblk(vp, lbn, on, PCATCH, 0, 0); 526 if (bp != NULL) { 527 uint32_t save = bp->b_flags & B_CACHE; 528 allocbuf(bp, bcount); 529 bp->b_flags |= save; 530 } 531 } else { 532 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0); 533 } 534 if (!bp) { 535 err = EINTR; 536 break; 537 } 538 if (extending) { 539 /* 540 * Extend file _after_ locking buffer so we won't race 541 * with other readers 542 */ 543 err = fuse_vnode_setsize(vp, uio->uio_offset + n, false); 544 filesize = uio->uio_offset + n; 545 getnanouptime(&fvdat->last_local_modify); 546 fvdat->flag |= FN_SIZECHANGE; 547 if (err) { 548 brelse(bp); 549 break; 550 } 551 } 552 553 SDT_PROBE6(fusefs, , io, write_biobackend_start, 554 lbn, on, n, uio, bcount, direct_append); 555 /* 556 * Issue a READ if B_CACHE is not set. In special-append 557 * mode, B_CACHE is based on the buffer prior to the write 558 * op and is typically set, avoiding the read. If a read 559 * is required in special append mode, the server will 560 * probably send us a short-read since we extended the file 561 * on our end, resulting in b_resid == 0 and, thusly, 562 * B_CACHE getting set. 563 * 564 * We can also avoid issuing the read if the write covers 565 * the entire buffer. We have to make sure the buffer state 566 * is reasonable in this case since we will not be initiating 567 * I/O. See the comments in kern/vfs_bio.c's getblk() for 568 * more information. 569 * 570 * B_CACHE may also be set due to the buffer being cached 571 * normally. 572 */ 573 574 if (on == 0 && n == bcount) { 575 bp->b_flags |= B_CACHE; 576 bp->b_flags &= ~B_INVAL; 577 bp->b_ioflags &= ~BIO_ERROR; 578 } 579 if ((bp->b_flags & B_CACHE) == 0) { 580 bp->b_iocmd = BIO_READ; 581 vfs_busy_pages(bp, 0); 582 fuse_io_strategy(vp, bp); 583 if ((err = bp->b_error)) { 584 brelse(bp); 585 break; 586 } 587 if (bp->b_resid > 0) { 588 /* 589 * Short read indicates EOF. Update file size 590 * from the server and try again. 591 */ 592 SDT_PROBE2(fusefs, , io, trace, 1, 593 "Short read during a RMW"); 594 brelse(bp); 595 err = fuse_vnode_size(vp, &filesize, cred, 596 curthread); 597 if (err) 598 break; 599 else 600 goto again; 601 } 602 } 603 if (bp->b_wcred == NOCRED) 604 bp->b_wcred = crhold(cred); 605 606 /* 607 * If dirtyend exceeds file size, chop it down. This should 608 * not normally occur but there is an append race where it 609 * might occur XXX, so we log it. 610 * 611 * If the chopping creates a reverse-indexed or degenerate 612 * situation with dirtyoff/end, we 0 both of them. 613 */ 614 if (bp->b_dirtyend > bcount) { 615 SDT_PROBE2(fusefs, , io, write_biobackend_append_race, 616 (long)bp->b_blkno * biosize, 617 bp->b_dirtyend - bcount); 618 bp->b_dirtyend = bcount; 619 } 620 if (bp->b_dirtyoff >= bp->b_dirtyend) 621 bp->b_dirtyoff = bp->b_dirtyend = 0; 622 623 /* 624 * If the new write will leave a contiguous dirty 625 * area, just update the b_dirtyoff and b_dirtyend, 626 * otherwise force a write rpc of the old dirty area. 627 * 628 * While it is possible to merge discontiguous writes due to 629 * our having a B_CACHE buffer ( and thus valid read data 630 * for the hole), we don't because it could lead to 631 * significant cache coherency problems with multiple clients, 632 * especially if locking is implemented later on. 633 * 634 * as an optimization we could theoretically maintain 635 * a linked list of discontinuous areas, but we would still 636 * have to commit them separately so there isn't much 637 * advantage to it except perhaps a bit of asynchronization. 638 */ 639 640 if (bp->b_dirtyend > 0 && 641 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 642 /* 643 * Yes, we mean it. Write out everything to "storage" 644 * immediately, without hesitation. (Apart from other 645 * reasons: the only way to know if a write is valid 646 * if its actually written out.) 647 */ 648 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 0, bp); 649 bwrite(bp); 650 if (bp->b_error == EINTR) { 651 err = EINTR; 652 break; 653 } 654 goto again; 655 } 656 err = uiomove((char *)bp->b_data + on, n, uio); 657 658 if (err) { 659 bp->b_ioflags |= BIO_ERROR; 660 bp->b_error = err; 661 brelse(bp); 662 break; 663 /* TODO: vfs_bio_clrbuf like ffs_write does? */ 664 } 665 /* 666 * Only update dirtyoff/dirtyend if not a degenerate 667 * condition. 668 */ 669 if (n) { 670 if (bp->b_dirtyend > 0) { 671 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff); 672 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend); 673 } else { 674 bp->b_dirtyoff = on; 675 bp->b_dirtyend = on + n; 676 } 677 vfs_bio_set_valid(bp, on, n); 678 } 679 680 vfs_bio_set_flags(bp, ioflag); 681 682 bp->b_flags |= B_FUSEFS_WRITE_CACHE; 683 if (ioflag & IO_SYNC) { 684 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 2, bp); 685 if (!(ioflag & IO_VMIO)) 686 bp->b_flags &= ~B_FUSEFS_WRITE_CACHE; 687 err = bwrite(bp); 688 } else if (vm_page_count_severe() || 689 buf_dirty_count_severe() || 690 (ioflag & IO_ASYNC)) { 691 bp->b_flags |= B_CLUSTEROK; 692 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 3, bp); 693 bawrite(bp); 694 } else if (on == 0 && n == bcount) { 695 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) { 696 bp->b_flags |= B_CLUSTEROK; 697 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 698 4, bp); 699 cluster_write(vp, &fvdat->clusterw, bp, 700 filesize, seqcount, 0); 701 } else { 702 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 703 5, bp); 704 bawrite(bp); 705 } 706 } else if (ioflag & IO_DIRECT) { 707 bp->b_flags |= B_CLUSTEROK; 708 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 6, bp); 709 bawrite(bp); 710 } else { 711 bp->b_flags &= ~B_CLUSTEROK; 712 SDT_PROBE2(fusefs, , io, write_biobackend_issue, 7, bp); 713 bdwrite(bp); 714 } 715 if (err) 716 break; 717 } while (uio->uio_resid > 0 && n > 0); 718 719 return (err); 720 } 721 722 int 723 fuse_io_strategy(struct vnode *vp, struct buf *bp) 724 { 725 struct fuse_vnode_data *fvdat = VTOFUD(vp); 726 struct fuse_filehandle *fufh; 727 struct ucred *cred; 728 struct uio *uiop; 729 struct uio uio; 730 struct iovec io; 731 off_t filesize; 732 int error = 0; 733 int fflag; 734 /* We don't know the true pid when we're dealing with the cache */ 735 pid_t pid = 0; 736 737 const int biosize = fuse_iosize(vp); 738 739 MPASS(vp->v_type == VREG || vp->v_type == VDIR); 740 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE); 741 742 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE; 743 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred; 744 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid); 745 if (bp->b_iocmd == BIO_READ && error == EBADF) { 746 /* 747 * This may be a read-modify-write operation on a cached file 748 * opened O_WRONLY. The FUSE protocol allows this. 749 */ 750 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid); 751 } 752 if (error) { 753 printf("FUSE: strategy: filehandles are closed\n"); 754 bp->b_ioflags |= BIO_ERROR; 755 bp->b_error = error; 756 bufdone(bp); 757 return (error); 758 } 759 760 uiop = &uio; 761 uiop->uio_iov = &io; 762 uiop->uio_iovcnt = 1; 763 uiop->uio_segflg = UIO_SYSSPACE; 764 uiop->uio_td = curthread; 765 766 /* 767 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We 768 * do this here so we do not have to do it in all the code that 769 * calls us. 770 */ 771 bp->b_flags &= ~B_INVAL; 772 bp->b_ioflags &= ~BIO_ERROR; 773 774 KASSERT(!(bp->b_flags & B_DONE), 775 ("fuse_io_strategy: bp %p already marked done", bp)); 776 if (bp->b_iocmd == BIO_READ) { 777 ssize_t left; 778 779 io.iov_len = uiop->uio_resid = bp->b_bcount; 780 io.iov_base = bp->b_data; 781 uiop->uio_rw = UIO_READ; 782 783 uiop->uio_offset = ((off_t)bp->b_lblkno) * biosize; 784 error = fuse_read_directbackend(vp, uiop, cred, fufh); 785 /* 786 * Store the amount we failed to read in the buffer's private 787 * field, so callers can truncate the file if necessary' 788 */ 789 790 if (!error && uiop->uio_resid) { 791 int nread = bp->b_bcount - uiop->uio_resid; 792 left = uiop->uio_resid; 793 bzero((char *)bp->b_data + nread, left); 794 795 if ((fvdat->flag & FN_SIZECHANGE) == 0) { 796 /* 797 * A short read with no error, when not using 798 * direct io, and when no writes are cached, 799 * indicates EOF caused by a server-side 800 * truncation. Clear the attr cache so we'll 801 * pick up the new file size and timestamps. 802 * 803 * We must still bzero the remaining buffer so 804 * uninitialized data doesn't get exposed by a 805 * future truncate that extends the file. 806 * 807 * To prevent lock order problems, we must 808 * truncate the file upstack, not here. 809 */ 810 SDT_PROBE2(fusefs, , io, trace, 1, 811 "Short read of a clean file"); 812 fuse_vnode_clear_attr_cache(vp); 813 } else { 814 /* 815 * If dirty writes _are_ cached beyond EOF, 816 * that indicates a newly created hole that the 817 * server doesn't know about. Those don't pose 818 * any problem. 819 * XXX: we don't currently track whether dirty 820 * writes are cached beyond EOF, before EOF, or 821 * both. 822 */ 823 SDT_PROBE2(fusefs, , io, trace, 1, 824 "Short read of a dirty file"); 825 uiop->uio_resid = 0; 826 } 827 } 828 if (error) { 829 bp->b_ioflags |= BIO_ERROR; 830 bp->b_error = error; 831 } 832 } else { 833 /* 834 * Setup for actual write 835 */ 836 /* 837 * If the file's size is cached, use that value, even if the 838 * cache is expired. At this point we're already committed to 839 * writing something. If the FUSE server has changed the 840 * file's size behind our back, it's too late for us to do 841 * anything about it. In particular, we can't invalidate any 842 * part of the file's buffers because VOP_STRATEGY is called 843 * with them already locked. 844 */ 845 filesize = fvdat->cached_attrs.va_size; 846 /* filesize must've been cached by fuse_vnop_open. */ 847 KASSERT(filesize != VNOVAL, ("filesize should've been cached")); 848 849 if ((off_t)bp->b_lblkno * biosize + bp->b_dirtyend > filesize) 850 bp->b_dirtyend = filesize - 851 (off_t)bp->b_lblkno * biosize; 852 853 if (bp->b_dirtyend > bp->b_dirtyoff) { 854 io.iov_len = uiop->uio_resid = bp->b_dirtyend 855 - bp->b_dirtyoff; 856 uiop->uio_offset = (off_t)bp->b_lblkno * biosize 857 + bp->b_dirtyoff; 858 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 859 uiop->uio_rw = UIO_WRITE; 860 861 bool pages = bp->b_flags & B_FUSEFS_WRITE_CACHE; 862 error = fuse_write_directbackend(vp, uiop, cred, fufh, 863 filesize, 0, pages); 864 865 if (error == EINTR || error == ETIMEDOUT) { 866 bp->b_flags &= ~(B_INVAL | B_NOCACHE); 867 if ((bp->b_flags & B_PAGING) == 0) { 868 bdirty(bp); 869 bp->b_flags &= ~B_DONE; 870 } 871 if ((error == EINTR || error == ETIMEDOUT) && 872 (bp->b_flags & B_ASYNC) == 0) 873 bp->b_flags |= B_EINTR; 874 } else { 875 if (error) { 876 bp->b_ioflags |= BIO_ERROR; 877 bp->b_flags |= B_INVAL; 878 bp->b_error = error; 879 } 880 bp->b_dirtyoff = bp->b_dirtyend = 0; 881 } 882 } else { 883 bp->b_resid = 0; 884 bufdone(bp); 885 return (0); 886 } 887 } 888 bp->b_resid = uiop->uio_resid; 889 bufdone(bp); 890 return (error); 891 } 892 893 int 894 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td) 895 { 896 897 return (vn_fsync_buf(vp, waitfor)); 898 } 899 900 /* 901 * Flush and invalidate all dirty buffers. If another process is already 902 * doing the flush, just wait for completion. 903 */ 904 int 905 fuse_io_invalbuf(struct vnode *vp, struct thread *td) 906 { 907 struct fuse_vnode_data *fvdat = VTOFUD(vp); 908 int error = 0; 909 910 if (VN_IS_DOOMED(vp)) 911 return 0; 912 913 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf"); 914 915 while (fvdat->flag & FN_FLUSHINPROG) { 916 struct proc *p = td->td_proc; 917 918 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) 919 return EIO; 920 fvdat->flag |= FN_FLUSHWANT; 921 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz); 922 error = 0; 923 if (p != NULL) { 924 PROC_LOCK(p); 925 if (SIGNOTEMPTY(p->p_siglist) || 926 SIGNOTEMPTY(td->td_siglist)) 927 error = EINTR; 928 PROC_UNLOCK(p); 929 } 930 if (error == EINTR) 931 return EINTR; 932 } 933 fvdat->flag |= FN_FLUSHINPROG; 934 935 if (vp->v_bufobj.bo_object != NULL) { 936 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object); 937 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC); 938 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object); 939 } 940 error = vinvalbuf(vp, V_SAVE, PCATCH, 0); 941 while (error) { 942 if (error == ERESTART || error == EINTR) { 943 fvdat->flag &= ~FN_FLUSHINPROG; 944 if (fvdat->flag & FN_FLUSHWANT) { 945 fvdat->flag &= ~FN_FLUSHWANT; 946 wakeup(&fvdat->flag); 947 } 948 return EINTR; 949 } 950 error = vinvalbuf(vp, V_SAVE, PCATCH, 0); 951 } 952 fvdat->flag &= ~FN_FLUSHINPROG; 953 if (fvdat->flag & FN_FLUSHWANT) { 954 fvdat->flag &= ~FN_FLUSHWANT; 955 wakeup(&fvdat->flag); 956 } 957 return (error); 958 } 959