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 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58 #include <sys/cdefs.h> 59 __FBSDID("$FreeBSD$"); 60 61 #include <sys/types.h> 62 #include <sys/module.h> 63 #include <sys/systm.h> 64 #include <sys/errno.h> 65 #include <sys/param.h> 66 #include <sys/kernel.h> 67 #include <sys/conf.h> 68 #include <sys/uio.h> 69 #include <sys/malloc.h> 70 #include <sys/queue.h> 71 #include <sys/lock.h> 72 #include <sys/sx.h> 73 #include <sys/mutex.h> 74 #include <sys/rwlock.h> 75 #include <sys/proc.h> 76 #include <sys/mount.h> 77 #include <sys/vnode.h> 78 #include <sys/stat.h> 79 #include <sys/unistd.h> 80 #include <sys/filedesc.h> 81 #include <sys/file.h> 82 #include <sys/fcntl.h> 83 #include <sys/bio.h> 84 #include <sys/buf.h> 85 #include <sys/sysctl.h> 86 87 #include <vm/vm.h> 88 #include <vm/vm_extern.h> 89 #include <vm/pmap.h> 90 #include <vm/vm_map.h> 91 #include <vm/vm_page.h> 92 #include <vm/vm_object.h> 93 94 #include "fuse.h" 95 #include "fuse_file.h" 96 #include "fuse_node.h" 97 #include "fuse_internal.h" 98 #include "fuse_ipc.h" 99 #include "fuse_io.h" 100 101 #define FUSE_DEBUG_MODULE IO 102 #include "fuse_debug.h" 103 104 105 static int 106 fuse_read_directbackend(struct vnode *vp, struct uio *uio, 107 struct ucred *cred, struct fuse_filehandle *fufh); 108 static int 109 fuse_read_biobackend(struct vnode *vp, struct uio *uio, 110 struct ucred *cred, struct fuse_filehandle *fufh); 111 static int 112 fuse_write_directbackend(struct vnode *vp, struct uio *uio, 113 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag); 114 static int 115 fuse_write_biobackend(struct vnode *vp, struct uio *uio, 116 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag); 117 118 int 119 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag, 120 struct ucred *cred) 121 { 122 struct fuse_filehandle *fufh; 123 int err, directio; 124 125 MPASS(vp->v_type == VREG || vp->v_type == VDIR); 126 127 err = fuse_filehandle_getrw(vp, 128 (uio->uio_rw == UIO_READ) ? FUFH_RDONLY : FUFH_WRONLY, &fufh); 129 if (err) { 130 printf("FUSE: io dispatch: filehandles are closed\n"); 131 return err; 132 } 133 /* 134 * Ideally, when the daemon asks for direct io at open time, the 135 * standard file flag should be set according to this, so that would 136 * just change the default mode, which later on could be changed via 137 * fcntl(2). 138 * But this doesn't work, the O_DIRECT flag gets cleared at some point 139 * (don't know where). So to make any use of the Fuse direct_io option, 140 * we hardwire it into the file's private data (similarly to Linux, 141 * btw.). 142 */ 143 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp)); 144 145 switch (uio->uio_rw) { 146 case UIO_READ: 147 if (directio) { 148 FS_DEBUG("direct read of vnode %ju via file handle %ju\n", 149 (uintmax_t)VTOILLU(vp), (uintmax_t)fufh->fh_id); 150 err = fuse_read_directbackend(vp, uio, cred, fufh); 151 } else { 152 FS_DEBUG("buffered read of vnode %ju\n", 153 (uintmax_t)VTOILLU(vp)); 154 err = fuse_read_biobackend(vp, uio, cred, fufh); 155 } 156 break; 157 case UIO_WRITE: 158 /* 159 * Kludge: simulate write-through caching via write-around 160 * caching. Same effect, as far as never caching dirty data, 161 * but slightly pessimal in that newly written data is not 162 * cached. 163 */ 164 if (directio || fuse_data_cache_mode == FUSE_CACHE_WT) { 165 FS_DEBUG("direct write of vnode %ju via file handle %ju\n", 166 (uintmax_t)VTOILLU(vp), (uintmax_t)fufh->fh_id); 167 err = fuse_write_directbackend(vp, uio, cred, fufh, ioflag); 168 } else { 169 FS_DEBUG("buffered write of vnode %ju\n", 170 (uintmax_t)VTOILLU(vp)); 171 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag); 172 } 173 break; 174 default: 175 panic("uninterpreted mode passed to fuse_io_dispatch"); 176 } 177 178 return (err); 179 } 180 181 static int 182 fuse_read_biobackend(struct vnode *vp, struct uio *uio, 183 struct ucred *cred, struct fuse_filehandle *fufh) 184 { 185 struct buf *bp; 186 daddr_t lbn; 187 int bcount; 188 int err = 0, n = 0, on = 0; 189 off_t filesize; 190 191 const int biosize = fuse_iosize(vp); 192 193 FS_DEBUG("resid=%zx offset=%jx fsize=%jx\n", 194 uio->uio_resid, uio->uio_offset, VTOFUD(vp)->filesize); 195 196 if (uio->uio_resid == 0) 197 return (0); 198 if (uio->uio_offset < 0) 199 return (EINVAL); 200 201 bcount = MIN(MAXBSIZE, biosize); 202 filesize = VTOFUD(vp)->filesize; 203 204 do { 205 if (fuse_isdeadfs(vp)) { 206 err = ENXIO; 207 break; 208 } 209 lbn = uio->uio_offset / biosize; 210 on = uio->uio_offset & (biosize - 1); 211 212 FS_DEBUG2G("biosize %d, lbn %d, on %d\n", biosize, (int)lbn, on); 213 214 /* 215 * Obtain the buffer cache block. Figure out the buffer size 216 * when we are at EOF. If we are modifying the size of the 217 * buffer based on an EOF condition we need to hold 218 * nfs_rslock() through obtaining the buffer to prevent 219 * a potential writer-appender from messing with n_size. 220 * Otherwise we may accidentally truncate the buffer and 221 * lose dirty data. 222 * 223 * Note that bcount is *not* DEV_BSIZE aligned. 224 */ 225 if ((off_t)lbn * biosize >= filesize) { 226 bcount = 0; 227 } else if ((off_t)(lbn + 1) * biosize > filesize) { 228 bcount = filesize - (off_t)lbn *biosize; 229 } 230 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0); 231 232 if (!bp) 233 return (EINTR); 234 235 /* 236 * If B_CACHE is not set, we must issue the read. If this 237 * fails, we return an error. 238 */ 239 240 if ((bp->b_flags & B_CACHE) == 0) { 241 bp->b_iocmd = BIO_READ; 242 vfs_busy_pages(bp, 0); 243 err = fuse_io_strategy(vp, bp); 244 if (err) { 245 brelse(bp); 246 return (err); 247 } 248 } 249 /* 250 * on is the offset into the current bp. Figure out how many 251 * bytes we can copy out of the bp. Note that bcount is 252 * NOT DEV_BSIZE aligned. 253 * 254 * Then figure out how many bytes we can copy into the uio. 255 */ 256 257 n = 0; 258 if (on < bcount) 259 n = MIN((unsigned)(bcount - on), uio->uio_resid); 260 if (n > 0) { 261 FS_DEBUG2G("feeding buffeater with %d bytes of buffer %p," 262 " saying %d was asked for\n", 263 n, bp->b_data + on, n + (int)bp->b_resid); 264 err = uiomove(bp->b_data + on, n, uio); 265 } 266 brelse(bp); 267 FS_DEBUG2G("end of turn, err %d, uio->uio_resid %zd, n %d\n", 268 err, uio->uio_resid, n); 269 } while (err == 0 && uio->uio_resid > 0 && n > 0); 270 271 return (err); 272 } 273 274 static int 275 fuse_read_directbackend(struct vnode *vp, struct uio *uio, 276 struct ucred *cred, struct fuse_filehandle *fufh) 277 { 278 struct fuse_dispatcher fdi; 279 struct fuse_read_in *fri; 280 int err = 0; 281 282 if (uio->uio_resid == 0) 283 return (0); 284 285 fdisp_init(&fdi, 0); 286 287 /* 288 * XXX In "normal" case we use an intermediate kernel buffer for 289 * transmitting data from daemon's context to ours. Eventually, we should 290 * get rid of this. Anyway, if the target uio lives in sysspace (we are 291 * called from pageops), and the input data doesn't need kernel-side 292 * processing (we are not called from readdir) we can already invoke 293 * an optimized, "peer-to-peer" I/O routine. 294 */ 295 while (uio->uio_resid > 0) { 296 fdi.iosize = sizeof(*fri); 297 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred); 298 fri = fdi.indata; 299 fri->fh = fufh->fh_id; 300 fri->offset = uio->uio_offset; 301 fri->size = MIN(uio->uio_resid, 302 fuse_get_mpdata(vp->v_mount)->max_read); 303 304 FS_DEBUG2G("fri->fh %ju, fri->offset %ju, fri->size %ju\n", 305 (uintmax_t)fri->fh, (uintmax_t)fri->offset, 306 (uintmax_t)fri->size); 307 308 if ((err = fdisp_wait_answ(&fdi))) 309 goto out; 310 311 FS_DEBUG2G("complete: got iosize=%d, requested fri.size=%zd; " 312 "resid=%zd offset=%ju\n", 313 fri->size, fdi.iosize, uio->uio_resid, 314 (uintmax_t)uio->uio_offset); 315 316 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio))) 317 break; 318 if (fdi.iosize < fri->size) 319 break; 320 } 321 322 out: 323 fdisp_destroy(&fdi); 324 return (err); 325 } 326 327 static int 328 fuse_write_directbackend(struct vnode *vp, struct uio *uio, 329 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag) 330 { 331 struct fuse_vnode_data *fvdat = VTOFUD(vp); 332 struct fuse_write_in *fwi; 333 struct fuse_dispatcher fdi; 334 size_t chunksize; 335 int diff; 336 int err = 0; 337 338 if (uio->uio_resid == 0) 339 return (0); 340 if (ioflag & IO_APPEND) 341 uio_setoffset(uio, fvdat->filesize); 342 343 fdisp_init(&fdi, 0); 344 345 while (uio->uio_resid > 0) { 346 chunksize = MIN(uio->uio_resid, 347 fuse_get_mpdata(vp->v_mount)->max_write); 348 349 fdi.iosize = sizeof(*fwi) + chunksize; 350 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred); 351 352 fwi = fdi.indata; 353 fwi->fh = fufh->fh_id; 354 fwi->offset = uio->uio_offset; 355 fwi->size = chunksize; 356 357 if ((err = uiomove((char *)fdi.indata + sizeof(*fwi), 358 chunksize, uio))) 359 break; 360 361 if ((err = fdisp_wait_answ(&fdi))) 362 break; 363 364 diff = chunksize - ((struct fuse_write_out *)fdi.answ)->size; 365 if (diff < 0) { 366 err = EINVAL; 367 break; 368 } 369 uio->uio_resid += diff; 370 uio->uio_offset -= diff; 371 if (uio->uio_offset > fvdat->filesize && 372 fuse_data_cache_mode != FUSE_CACHE_UC) { 373 fuse_vnode_setsize(vp, cred, uio->uio_offset); 374 fvdat->flag &= ~FN_SIZECHANGE; 375 } 376 } 377 378 fdisp_destroy(&fdi); 379 380 return (err); 381 } 382 383 static int 384 fuse_write_biobackend(struct vnode *vp, struct uio *uio, 385 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag) 386 { 387 struct fuse_vnode_data *fvdat = VTOFUD(vp); 388 struct buf *bp; 389 daddr_t lbn; 390 int bcount; 391 int n, on, err = 0; 392 393 const int biosize = fuse_iosize(vp); 394 395 KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode")); 396 FS_DEBUG("resid=%zx offset=%jx fsize=%jx\n", 397 uio->uio_resid, uio->uio_offset, fvdat->filesize); 398 if (vp->v_type != VREG) 399 return (EIO); 400 if (uio->uio_offset < 0) 401 return (EINVAL); 402 if (uio->uio_resid == 0) 403 return (0); 404 if (ioflag & IO_APPEND) 405 uio_setoffset(uio, fvdat->filesize); 406 407 /* 408 * Find all of this file's B_NEEDCOMMIT buffers. If our writes 409 * would exceed the local maximum per-file write commit size when 410 * combined with those, we must decide whether to flush, 411 * go synchronous, or return err. We don't bother checking 412 * IO_UNIT -- we just make all writes atomic anyway, as there's 413 * no point optimizing for something that really won't ever happen. 414 */ 415 do { 416 if (fuse_isdeadfs(vp)) { 417 err = ENXIO; 418 break; 419 } 420 lbn = uio->uio_offset / biosize; 421 on = uio->uio_offset & (biosize - 1); 422 n = MIN((unsigned)(biosize - on), uio->uio_resid); 423 424 FS_DEBUG2G("lbn %ju, on %d, n %d, uio offset %ju, uio resid %zd\n", 425 (uintmax_t)lbn, on, n, 426 (uintmax_t)uio->uio_offset, uio->uio_resid); 427 428 again: 429 /* 430 * Handle direct append and file extension cases, calculate 431 * unaligned buffer size. 432 */ 433 if (uio->uio_offset == fvdat->filesize && n) { 434 /* 435 * Get the buffer (in its pre-append state to maintain 436 * B_CACHE if it was previously set). Resize the 437 * nfsnode after we have locked the buffer to prevent 438 * readers from reading garbage. 439 */ 440 bcount = on; 441 FS_DEBUG("getting block from OS, bcount %d\n", bcount); 442 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0); 443 444 if (bp != NULL) { 445 long save; 446 447 err = fuse_vnode_setsize(vp, cred, 448 uio->uio_offset + n); 449 if (err) { 450 brelse(bp); 451 break; 452 } 453 save = bp->b_flags & B_CACHE; 454 bcount += n; 455 allocbuf(bp, bcount); 456 bp->b_flags |= save; 457 } 458 } else { 459 /* 460 * Obtain the locked cache block first, and then 461 * adjust the file's size as appropriate. 462 */ 463 bcount = on + n; 464 if ((off_t)lbn * biosize + bcount < fvdat->filesize) { 465 if ((off_t)(lbn + 1) * biosize < fvdat->filesize) 466 bcount = biosize; 467 else 468 bcount = fvdat->filesize - 469 (off_t)lbn *biosize; 470 } 471 FS_DEBUG("getting block from OS, bcount %d\n", bcount); 472 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0); 473 if (bp && uio->uio_offset + n > fvdat->filesize) { 474 err = fuse_vnode_setsize(vp, cred, 475 uio->uio_offset + n); 476 if (err) { 477 brelse(bp); 478 break; 479 } 480 } 481 } 482 483 if (!bp) { 484 err = EINTR; 485 break; 486 } 487 /* 488 * Issue a READ if B_CACHE is not set. In special-append 489 * mode, B_CACHE is based on the buffer prior to the write 490 * op and is typically set, avoiding the read. If a read 491 * is required in special append mode, the server will 492 * probably send us a short-read since we extended the file 493 * on our end, resulting in b_resid == 0 and, thusly, 494 * B_CACHE getting set. 495 * 496 * We can also avoid issuing the read if the write covers 497 * the entire buffer. We have to make sure the buffer state 498 * is reasonable in this case since we will not be initiating 499 * I/O. See the comments in kern/vfs_bio.c's getblk() for 500 * more information. 501 * 502 * B_CACHE may also be set due to the buffer being cached 503 * normally. 504 */ 505 506 if (on == 0 && n == bcount) { 507 bp->b_flags |= B_CACHE; 508 bp->b_flags &= ~B_INVAL; 509 bp->b_ioflags &= ~BIO_ERROR; 510 } 511 if ((bp->b_flags & B_CACHE) == 0) { 512 bp->b_iocmd = BIO_READ; 513 vfs_busy_pages(bp, 0); 514 fuse_io_strategy(vp, bp); 515 if ((err = bp->b_error)) { 516 brelse(bp); 517 break; 518 } 519 } 520 if (bp->b_wcred == NOCRED) 521 bp->b_wcred = crhold(cred); 522 523 /* 524 * If dirtyend exceeds file size, chop it down. This should 525 * not normally occur but there is an append race where it 526 * might occur XXX, so we log it. 527 * 528 * If the chopping creates a reverse-indexed or degenerate 529 * situation with dirtyoff/end, we 0 both of them. 530 */ 531 532 if (bp->b_dirtyend > bcount) { 533 FS_DEBUG("FUSE append race @%lx:%d\n", 534 (long)bp->b_blkno * biosize, 535 bp->b_dirtyend - bcount); 536 bp->b_dirtyend = bcount; 537 } 538 if (bp->b_dirtyoff >= bp->b_dirtyend) 539 bp->b_dirtyoff = bp->b_dirtyend = 0; 540 541 /* 542 * If the new write will leave a contiguous dirty 543 * area, just update the b_dirtyoff and b_dirtyend, 544 * otherwise force a write rpc of the old dirty area. 545 * 546 * While it is possible to merge discontiguous writes due to 547 * our having a B_CACHE buffer ( and thus valid read data 548 * for the hole), we don't because it could lead to 549 * significant cache coherency problems with multiple clients, 550 * especially if locking is implemented later on. 551 * 552 * as an optimization we could theoretically maintain 553 * a linked list of discontinuous areas, but we would still 554 * have to commit them separately so there isn't much 555 * advantage to it except perhaps a bit of asynchronization. 556 */ 557 558 if (bp->b_dirtyend > 0 && 559 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 560 /* 561 * Yes, we mean it. Write out everything to "storage" 562 * immediately, without hesitation. (Apart from other 563 * reasons: the only way to know if a write is valid 564 * if its actually written out.) 565 */ 566 bwrite(bp); 567 if (bp->b_error == EINTR) { 568 err = EINTR; 569 break; 570 } 571 goto again; 572 } 573 err = uiomove((char *)bp->b_data + on, n, uio); 574 575 /* 576 * Since this block is being modified, it must be written 577 * again and not just committed. Since write clustering does 578 * not work for the stage 1 data write, only the stage 2 579 * commit rpc, we have to clear B_CLUSTEROK as well. 580 */ 581 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 582 583 if (err) { 584 bp->b_ioflags |= BIO_ERROR; 585 bp->b_error = err; 586 brelse(bp); 587 break; 588 } 589 /* 590 * Only update dirtyoff/dirtyend if not a degenerate 591 * condition. 592 */ 593 if (n) { 594 if (bp->b_dirtyend > 0) { 595 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff); 596 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend); 597 } else { 598 bp->b_dirtyoff = on; 599 bp->b_dirtyend = on + n; 600 } 601 vfs_bio_set_valid(bp, on, n); 602 } 603 err = bwrite(bp); 604 if (err) 605 break; 606 } while (uio->uio_resid > 0 && n > 0); 607 608 if (fuse_sync_resize && (fvdat->flag & FN_SIZECHANGE) != 0) 609 fuse_vnode_savesize(vp, cred); 610 611 return (err); 612 } 613 614 int 615 fuse_io_strategy(struct vnode *vp, struct buf *bp) 616 { 617 struct fuse_filehandle *fufh; 618 struct fuse_vnode_data *fvdat = VTOFUD(vp); 619 struct ucred *cred; 620 struct uio *uiop; 621 struct uio uio; 622 struct iovec io; 623 int error = 0; 624 625 const int biosize = fuse_iosize(vp); 626 627 MPASS(vp->v_type == VREG || vp->v_type == VDIR); 628 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE); 629 FS_DEBUG("inode=%ju offset=%jd resid=%ld\n", 630 (uintmax_t)VTOI(vp), (intmax_t)(((off_t)bp->b_blkno) * biosize), 631 bp->b_bcount); 632 633 error = fuse_filehandle_getrw(vp, 634 (bp->b_iocmd == BIO_READ) ? FUFH_RDONLY : FUFH_WRONLY, &fufh); 635 if (error) { 636 printf("FUSE: strategy: filehandles are closed\n"); 637 bp->b_ioflags |= BIO_ERROR; 638 bp->b_error = error; 639 return (error); 640 } 641 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred; 642 643 uiop = &uio; 644 uiop->uio_iov = &io; 645 uiop->uio_iovcnt = 1; 646 uiop->uio_segflg = UIO_SYSSPACE; 647 uiop->uio_td = curthread; 648 649 /* 650 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We 651 * do this here so we do not have to do it in all the code that 652 * calls us. 653 */ 654 bp->b_flags &= ~B_INVAL; 655 bp->b_ioflags &= ~BIO_ERROR; 656 657 KASSERT(!(bp->b_flags & B_DONE), 658 ("fuse_io_strategy: bp %p already marked done", bp)); 659 if (bp->b_iocmd == BIO_READ) { 660 io.iov_len = uiop->uio_resid = bp->b_bcount; 661 io.iov_base = bp->b_data; 662 uiop->uio_rw = UIO_READ; 663 664 uiop->uio_offset = ((off_t)bp->b_blkno) * biosize; 665 error = fuse_read_directbackend(vp, uiop, cred, fufh); 666 667 /* XXXCEM: Potentially invalid access to cached_attrs here */ 668 if ((!error && uiop->uio_resid) || 669 (fsess_opt_brokenio(vnode_mount(vp)) && error == EIO && 670 uiop->uio_offset < fvdat->filesize && fvdat->filesize > 0 && 671 uiop->uio_offset >= fvdat->cached_attrs.va_size)) { 672 /* 673 * If we had a short read with no error, we must have 674 * hit a file hole. We should zero-fill the remainder. 675 * This can also occur if the server hits the file EOF. 676 * 677 * Holes used to be able to occur due to pending 678 * writes, but that is not possible any longer. 679 */ 680 int nread = bp->b_bcount - uiop->uio_resid; 681 int left = uiop->uio_resid; 682 683 if (error != 0) { 684 printf("FUSE: Fix broken io: offset %ju, " 685 " resid %zd, file size %ju/%ju\n", 686 (uintmax_t)uiop->uio_offset, 687 uiop->uio_resid, fvdat->filesize, 688 fvdat->cached_attrs.va_size); 689 error = 0; 690 } 691 if (left > 0) 692 bzero((char *)bp->b_data + nread, left); 693 uiop->uio_resid = 0; 694 } 695 if (error) { 696 bp->b_ioflags |= BIO_ERROR; 697 bp->b_error = error; 698 } 699 } else { 700 /* 701 * If we only need to commit, try to commit 702 */ 703 if (bp->b_flags & B_NEEDCOMMIT) { 704 FS_DEBUG("write: B_NEEDCOMMIT flags set\n"); 705 } 706 /* 707 * Setup for actual write 708 */ 709 if ((off_t)bp->b_blkno * biosize + bp->b_dirtyend > 710 fvdat->filesize) 711 bp->b_dirtyend = fvdat->filesize - 712 (off_t)bp->b_blkno * biosize; 713 714 if (bp->b_dirtyend > bp->b_dirtyoff) { 715 io.iov_len = uiop->uio_resid = bp->b_dirtyend 716 - bp->b_dirtyoff; 717 uiop->uio_offset = (off_t)bp->b_blkno * biosize 718 + bp->b_dirtyoff; 719 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 720 uiop->uio_rw = UIO_WRITE; 721 722 error = fuse_write_directbackend(vp, uiop, cred, fufh, 0); 723 724 if (error == EINTR || error == ETIMEDOUT 725 || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 726 727 bp->b_flags &= ~(B_INVAL | B_NOCACHE); 728 if ((bp->b_flags & B_PAGING) == 0) { 729 bdirty(bp); 730 bp->b_flags &= ~B_DONE; 731 } 732 if ((error == EINTR || error == ETIMEDOUT) && 733 (bp->b_flags & B_ASYNC) == 0) 734 bp->b_flags |= B_EINTR; 735 } else { 736 if (error) { 737 bp->b_ioflags |= BIO_ERROR; 738 bp->b_flags |= B_INVAL; 739 bp->b_error = error; 740 } 741 bp->b_dirtyoff = bp->b_dirtyend = 0; 742 } 743 } else { 744 bp->b_resid = 0; 745 bufdone(bp); 746 return (0); 747 } 748 } 749 bp->b_resid = uiop->uio_resid; 750 bufdone(bp); 751 return (error); 752 } 753 754 int 755 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td) 756 { 757 struct vop_fsync_args a = { 758 .a_vp = vp, 759 .a_waitfor = waitfor, 760 .a_td = td, 761 }; 762 763 return (vop_stdfsync(&a)); 764 } 765 766 /* 767 * Flush and invalidate all dirty buffers. If another process is already 768 * doing the flush, just wait for completion. 769 */ 770 int 771 fuse_io_invalbuf(struct vnode *vp, struct thread *td) 772 { 773 struct fuse_vnode_data *fvdat = VTOFUD(vp); 774 int error = 0; 775 776 if (vp->v_iflag & VI_DOOMED) 777 return 0; 778 779 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf"); 780 781 while (fvdat->flag & FN_FLUSHINPROG) { 782 struct proc *p = td->td_proc; 783 784 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) 785 return EIO; 786 fvdat->flag |= FN_FLUSHWANT; 787 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz); 788 error = 0; 789 if (p != NULL) { 790 PROC_LOCK(p); 791 if (SIGNOTEMPTY(p->p_siglist) || 792 SIGNOTEMPTY(td->td_siglist)) 793 error = EINTR; 794 PROC_UNLOCK(p); 795 } 796 if (error == EINTR) 797 return EINTR; 798 } 799 fvdat->flag |= FN_FLUSHINPROG; 800 801 if (vp->v_bufobj.bo_object != NULL) { 802 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object); 803 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC); 804 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object); 805 } 806 error = vinvalbuf(vp, V_SAVE, PCATCH, 0); 807 while (error) { 808 if (error == ERESTART || error == EINTR) { 809 fvdat->flag &= ~FN_FLUSHINPROG; 810 if (fvdat->flag & FN_FLUSHWANT) { 811 fvdat->flag &= ~FN_FLUSHWANT; 812 wakeup(&fvdat->flag); 813 } 814 return EINTR; 815 } 816 error = vinvalbuf(vp, V_SAVE, PCATCH, 0); 817 } 818 fvdat->flag &= ~FN_FLUSHINPROG; 819 if (fvdat->flag & FN_FLUSHWANT) { 820 fvdat->flag &= ~FN_FLUSHWANT; 821 wakeup(&fvdat->flag); 822 } 823 return (error); 824 } 825