2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Google Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * Copyright (C) 2005 Csaba Henk.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
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>
69 #include <sys/malloc.h>
70 #include <sys/queue.h>
73 #include <sys/mutex.h>
74 #include <sys/rwlock.h>
76 #include <sys/mount.h>
77 #include <sys/vnode.h>
79 #include <sys/unistd.h>
80 #include <sys/filedesc.h>
82 #include <sys/fcntl.h>
85 #include <sys/sysctl.h>
88 #include <vm/vm_extern.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_object.h>
95 #include "fuse_file.h"
96 #include "fuse_node.h"
97 #include "fuse_internal.h"
101 SDT_PROVIDER_DECLARE(fusefs);
104 * arg0: verbosity. Higher numbers give more verbose messages
105 * arg1: Textual message
107 SDT_PROBE_DEFINE2(fusefs, , io, trace, "int", "char*");
110 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
111 struct ucred *cred, struct fuse_filehandle *fufh);
113 fuse_read_biobackend(struct vnode *vp, struct uio *uio,
114 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid);
116 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
117 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag);
119 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
120 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid);
122 SDT_PROBE_DEFINE5(fusefs, , io, io_dispatch, "struct vnode*", "struct uio*",
123 "int", "struct ucred*", "struct fuse_filehandle*");
125 fuse_io_dispatch(struct vnode *vp, struct uio *uio, int ioflag,
126 struct ucred *cred, pid_t pid)
128 struct fuse_filehandle *fufh;
132 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
134 fflag = (uio->uio_rw == UIO_READ) ? FREAD : FWRITE;
135 err = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
137 printf("FUSE: io dispatch: filehandles are closed\n");
140 SDT_PROBE5(fusefs, , io, io_dispatch, vp, uio, ioflag, cred, fufh);
143 * Ideally, when the daemon asks for direct io at open time, the
144 * standard file flag should be set according to this, so that would
145 * just change the default mode, which later on could be changed via
147 * But this doesn't work, the O_DIRECT flag gets cleared at some point
148 * (don't know where). So to make any use of the Fuse direct_io option,
149 * we hardwire it into the file's private data (similarly to Linux,
152 directio = (ioflag & IO_DIRECT) || !fsess_opt_datacache(vnode_mount(vp));
154 switch (uio->uio_rw) {
157 SDT_PROBE2(fusefs, , io, trace, 1,
158 "direct read of vnode");
159 err = fuse_read_directbackend(vp, uio, cred, fufh);
161 SDT_PROBE2(fusefs, , io, trace, 1,
162 "buffered read of vnode");
163 err = fuse_read_biobackend(vp, uio, cred, fufh, pid);
168 * Kludge: simulate write-through caching via write-around
169 * caching. Same effect, as far as never caching dirty data,
170 * but slightly pessimal in that newly written data is not
173 if (directio || fuse_data_cache_mode == FUSE_CACHE_WT) {
176 SDT_PROBE2(fusefs, , io, trace, 1,
177 "direct write of vnode");
178 start = uio->uio_offset;
179 end = start + uio->uio_resid;
180 v_inval_buf_range(vp, start, end, fuse_iosize(vp));
181 err = fuse_write_directbackend(vp, uio, cred, fufh,
184 SDT_PROBE2(fusefs, , io, trace, 1,
185 "buffered write of vnode");
186 err = fuse_write_biobackend(vp, uio, cred, fufh, ioflag,
191 panic("uninterpreted mode passed to fuse_io_dispatch");
197 SDT_PROBE_DEFINE3(fusefs, , io, read_bio_backend_start, "int", "int", "int");
198 SDT_PROBE_DEFINE2(fusefs, , io, read_bio_backend_feed, "int", "int");
199 SDT_PROBE_DEFINE3(fusefs, , io, read_bio_backend_end, "int", "ssize_t", "int");
201 fuse_read_biobackend(struct vnode *vp, struct uio *uio,
202 struct ucred *cred, struct fuse_filehandle *fufh, pid_t pid)
207 int err = 0, n = 0, on = 0;
210 const int biosize = fuse_iosize(vp);
212 if (uio->uio_resid == 0)
214 if (uio->uio_offset < 0)
218 filesize = VTOFUD(vp)->filesize;
221 if (fuse_isdeadfs(vp)) {
225 lbn = uio->uio_offset / biosize;
226 on = uio->uio_offset & (biosize - 1);
228 SDT_PROBE3(fusefs, , io, read_bio_backend_start,
229 biosize, (int)lbn, on);
232 * Obtain the buffer cache block. Figure out the buffer size
233 * when we are at EOF. If we are modifying the size of the
234 * buffer based on an EOF condition we need to hold
235 * nfs_rslock() through obtaining the buffer to prevent
236 * a potential writer-appender from messing with n_size.
237 * Otherwise we may accidentally truncate the buffer and
240 * Note that bcount is *not* DEV_BSIZE aligned.
242 if ((off_t)lbn * biosize >= filesize) {
244 } else if ((off_t)(lbn + 1) * biosize > filesize) {
245 bcount = filesize - (off_t)lbn *biosize;
247 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
253 * If B_CACHE is not set, we must issue the read. If this
254 * fails, we return an error.
257 if ((bp->b_flags & B_CACHE) == 0) {
258 bp->b_iocmd = BIO_READ;
259 vfs_busy_pages(bp, 0);
260 err = fuse_io_strategy(vp, bp);
267 * on is the offset into the current bp. Figure out how many
268 * bytes we can copy out of the bp. Note that bcount is
269 * NOT DEV_BSIZE aligned.
271 * Then figure out how many bytes we can copy into the uio.
276 n = MIN((unsigned)(bcount - on), uio->uio_resid);
278 SDT_PROBE2(fusefs, , io, read_bio_backend_feed,
279 n, n + (int)bp->b_resid);
280 err = uiomove(bp->b_data + on, n, uio);
283 SDT_PROBE3(fusefs, , io, read_bio_backend_end, err,
285 } while (err == 0 && uio->uio_resid > 0 && n > 0);
290 SDT_PROBE_DEFINE1(fusefs, , io, read_directbackend_start,
291 "struct fuse_read_in*");
292 SDT_PROBE_DEFINE2(fusefs, , io, read_directbackend_complete,
293 "struct fuse_dispatcher*", "struct uio*");
296 fuse_read_directbackend(struct vnode *vp, struct uio *uio,
297 struct ucred *cred, struct fuse_filehandle *fufh)
299 struct fuse_dispatcher fdi;
300 struct fuse_read_in *fri;
303 if (uio->uio_resid == 0)
309 * XXX In "normal" case we use an intermediate kernel buffer for
310 * transmitting data from daemon's context to ours. Eventually, we should
311 * get rid of this. Anyway, if the target uio lives in sysspace (we are
312 * called from pageops), and the input data doesn't need kernel-side
313 * processing (we are not called from readdir) we can already invoke
314 * an optimized, "peer-to-peer" I/O routine.
316 while (uio->uio_resid > 0) {
317 fdi.iosize = sizeof(*fri);
318 fdisp_make_vp(&fdi, FUSE_READ, vp, uio->uio_td, cred);
320 fri->fh = fufh->fh_id;
321 fri->offset = uio->uio_offset;
322 fri->size = MIN(uio->uio_resid,
323 fuse_get_mpdata(vp->v_mount)->max_read);
325 SDT_PROBE1(fusefs, , io, read_directbackend_start, fri);
327 if ((err = fdisp_wait_answ(&fdi)))
330 SDT_PROBE2(fusefs, , io, read_directbackend_complete,
333 if ((err = uiomove(fdi.answ, MIN(fri->size, fdi.iosize), uio)))
335 if (fdi.iosize < fri->size)
345 fuse_write_directbackend(struct vnode *vp, struct uio *uio,
346 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag)
348 struct fuse_vnode_data *fvdat = VTOFUD(vp);
349 struct fuse_write_in *fwi;
350 struct fuse_write_out *fwo;
351 struct fuse_dispatcher fdi;
354 off_t as_written_offset;
357 bool direct_io = fufh->fuse_open_flags & FOPEN_DIRECT_IO;
359 if (uio->uio_resid == 0)
361 if (ioflag & IO_APPEND)
362 uio_setoffset(uio, fvdat->filesize);
366 while (uio->uio_resid > 0) {
367 chunksize = MIN(uio->uio_resid,
368 fuse_get_mpdata(vp->v_mount)->max_write);
370 fdi.iosize = sizeof(*fwi) + chunksize;
371 fdisp_make_vp(&fdi, FUSE_WRITE, vp, uio->uio_td, cred);
374 fwi->fh = fufh->fh_id;
375 fwi->offset = uio->uio_offset;
376 fwi->size = chunksize;
377 fwi_data = (char *)fdi.indata + sizeof(*fwi);
379 if ((err = uiomove(fwi_data, chunksize, uio)))
383 err = fdisp_wait_answ(&fdi);
384 if (err == ERESTART || err == EINTR || err == EWOULDBLOCK) {
386 * Rewind the uio so dofilewrite will know it's
389 uio->uio_resid += fwi->size;
390 uio->uio_offset -= fwi->size;
392 * Change ERESTART into EINTR because we can't rewind
393 * uio->uio_iov. Basically, once uiomove(9) has been
394 * called, it's impossible to restart a syscall.
403 fwo = ((struct fuse_write_out *)fdi.answ);
405 /* Adjust the uio in the case of short writes */
406 diff = fwi->size - fwo->size;
407 as_written_offset = uio->uio_offset - diff;
409 if (as_written_offset - diff > fvdat->filesize &&
410 fuse_data_cache_mode != FUSE_CACHE_UC) {
411 fuse_vnode_setsize(vp, cred, as_written_offset);
412 fvdat->flag &= ~FN_SIZECHANGE;
416 printf("WARNING: misbehaving FUSE filesystem "
417 "wrote more data than we provided it\n");
420 } else if (diff > 0) {
423 printf("WARNING: misbehaving FUSE filesystem: "
424 "short writes are only allowed with "
427 if (ioflag & IO_DIRECT) {
429 uio->uio_resid += diff;
430 uio->uio_offset -= diff;
433 /* Resend the unwritten portion of data */
434 fdi.iosize = sizeof(*fwi) + diff;
435 /* Refresh fdi without clearing data buffer */
436 fdisp_refresh_vp(&fdi, FUSE_WRITE, vp,
439 MPASS2(fwi == fdi.indata, "FUSE dispatcher "
440 "reallocated despite no increase in "
442 void *src = (char*)fwi_data + fwo->size;
443 memmove(fwi_data, src, diff);
444 fwi->fh = fufh->fh_id;
445 fwi->offset = as_written_offset;
457 SDT_PROBE_DEFINE6(fusefs, , io, write_biobackend_start, "int64_t", "int", "int",
458 "struct uio*", "int", "bool");
459 SDT_PROBE_DEFINE2(fusefs, , io, write_biobackend_append_race, "long", "int");
462 fuse_write_biobackend(struct vnode *vp, struct uio *uio,
463 struct ucred *cred, struct fuse_filehandle *fufh, int ioflag, pid_t pid)
465 struct fuse_vnode_data *fvdat = VTOFUD(vp);
471 const int biosize = fuse_iosize(vp);
473 KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode"));
474 if (vp->v_type != VREG)
476 if (uio->uio_offset < 0)
478 if (uio->uio_resid == 0)
480 if (ioflag & IO_APPEND)
481 uio_setoffset(uio, fvdat->filesize);
484 * Find all of this file's B_NEEDCOMMIT buffers. If our writes
485 * would exceed the local maximum per-file write commit size when
486 * combined with those, we must decide whether to flush,
487 * go synchronous, or return err. We don't bother checking
488 * IO_UNIT -- we just make all writes atomic anyway, as there's
489 * no point optimizing for something that really won't ever happen.
492 if (fuse_isdeadfs(vp)) {
496 lbn = uio->uio_offset / biosize;
497 on = uio->uio_offset & (biosize - 1);
498 n = MIN((unsigned)(biosize - on), uio->uio_resid);
502 * Handle direct append and file extension cases, calculate
503 * unaligned buffer size.
505 if (uio->uio_offset == fvdat->filesize && n) {
507 * Get the buffer (in its pre-append state to maintain
508 * B_CACHE if it was previously set). Resize the
509 * nfsnode after we have locked the buffer to prevent
510 * readers from reading garbage.
513 SDT_PROBE6(fusefs, , io, write_biobackend_start,
514 lbn, on, n, uio, bcount, true);
515 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
520 err = fuse_vnode_setsize(vp, cred,
521 uio->uio_offset + n);
526 save = bp->b_flags & B_CACHE;
528 allocbuf(bp, bcount);
533 * Obtain the locked cache block first, and then
534 * adjust the file's size as appropriate.
537 if ((off_t)lbn * biosize + bcount < fvdat->filesize) {
538 if ((off_t)(lbn + 1) * biosize < fvdat->filesize)
541 bcount = fvdat->filesize -
544 SDT_PROBE6(fusefs, , io, write_biobackend_start,
545 lbn, on, n, uio, bcount, false);
546 bp = getblk(vp, lbn, bcount, PCATCH, 0, 0);
547 if (bp && uio->uio_offset + n > fvdat->filesize) {
548 err = fuse_vnode_setsize(vp, cred,
549 uio->uio_offset + n);
562 * Issue a READ if B_CACHE is not set. In special-append
563 * mode, B_CACHE is based on the buffer prior to the write
564 * op and is typically set, avoiding the read. If a read
565 * is required in special append mode, the server will
566 * probably send us a short-read since we extended the file
567 * on our end, resulting in b_resid == 0 and, thusly,
568 * B_CACHE getting set.
570 * We can also avoid issuing the read if the write covers
571 * the entire buffer. We have to make sure the buffer state
572 * is reasonable in this case since we will not be initiating
573 * I/O. See the comments in kern/vfs_bio.c's getblk() for
576 * B_CACHE may also be set due to the buffer being cached
580 if (on == 0 && n == bcount) {
581 bp->b_flags |= B_CACHE;
582 bp->b_flags &= ~B_INVAL;
583 bp->b_ioflags &= ~BIO_ERROR;
585 if ((bp->b_flags & B_CACHE) == 0) {
586 bp->b_iocmd = BIO_READ;
587 vfs_busy_pages(bp, 0);
588 fuse_io_strategy(vp, bp);
589 if ((err = bp->b_error)) {
594 if (bp->b_wcred == NOCRED)
595 bp->b_wcred = crhold(cred);
598 * If dirtyend exceeds file size, chop it down. This should
599 * not normally occur but there is an append race where it
600 * might occur XXX, so we log it.
602 * If the chopping creates a reverse-indexed or degenerate
603 * situation with dirtyoff/end, we 0 both of them.
606 if (bp->b_dirtyend > bcount) {
607 SDT_PROBE2(fusefs, , io, write_biobackend_append_race,
608 (long)bp->b_blkno * biosize,
609 bp->b_dirtyend - bcount);
610 bp->b_dirtyend = bcount;
612 if (bp->b_dirtyoff >= bp->b_dirtyend)
613 bp->b_dirtyoff = bp->b_dirtyend = 0;
616 * If the new write will leave a contiguous dirty
617 * area, just update the b_dirtyoff and b_dirtyend,
618 * otherwise force a write rpc of the old dirty area.
620 * While it is possible to merge discontiguous writes due to
621 * our having a B_CACHE buffer ( and thus valid read data
622 * for the hole), we don't because it could lead to
623 * significant cache coherency problems with multiple clients,
624 * especially if locking is implemented later on.
626 * as an optimization we could theoretically maintain
627 * a linked list of discontinuous areas, but we would still
628 * have to commit them separately so there isn't much
629 * advantage to it except perhaps a bit of asynchronization.
632 if (bp->b_dirtyend > 0 &&
633 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
635 * Yes, we mean it. Write out everything to "storage"
636 * immediately, without hesitation. (Apart from other
637 * reasons: the only way to know if a write is valid
638 * if its actually written out.)
641 if (bp->b_error == EINTR) {
647 err = uiomove((char *)bp->b_data + on, n, uio);
650 * Since this block is being modified, it must be written
651 * again and not just committed. Since write clustering does
652 * not work for the stage 1 data write, only the stage 2
653 * commit rpc, we have to clear B_CLUSTEROK as well.
655 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
658 bp->b_ioflags |= BIO_ERROR;
664 * Only update dirtyoff/dirtyend if not a degenerate
668 if (bp->b_dirtyend > 0) {
669 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff);
670 bp->b_dirtyend = MAX((on + n), bp->b_dirtyend);
673 bp->b_dirtyend = on + n;
675 vfs_bio_set_valid(bp, on, n);
680 } while (uio->uio_resid > 0 && n > 0);
682 if (fuse_sync_resize && (fvdat->flag & FN_SIZECHANGE) != 0)
683 fuse_vnode_savesize(vp, cred, pid);
689 fuse_io_strategy(struct vnode *vp, struct buf *bp)
691 struct fuse_filehandle *fufh;
692 struct fuse_vnode_data *fvdat = VTOFUD(vp);
699 /* We don't know the true pid when we're dealing with the cache */
702 const int biosize = fuse_iosize(vp);
704 MPASS(vp->v_type == VREG || vp->v_type == VDIR);
705 MPASS(bp->b_iocmd == BIO_READ || bp->b_iocmd == BIO_WRITE);
707 fflag = bp->b_iocmd == BIO_READ ? FREAD : FWRITE;
708 cred = bp->b_iocmd == BIO_READ ? bp->b_rcred : bp->b_wcred;
709 error = fuse_filehandle_getrw(vp, fflag, &fufh, cred, pid);
710 if (bp->b_iocmd == BIO_READ && error == EBADF) {
712 * This may be a read-modify-write operation on a cached file
713 * opened O_WRONLY. The FUSE protocol allows this.
715 * TODO: eliminate this hacky check once the FUFH table is gone
717 error = fuse_filehandle_get(vp, FWRITE, &fufh, cred, pid);
720 printf("FUSE: strategy: filehandles are closed\n");
721 bp->b_ioflags |= BIO_ERROR;
729 uiop->uio_iovcnt = 1;
730 uiop->uio_segflg = UIO_SYSSPACE;
731 uiop->uio_td = curthread;
734 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
735 * do this here so we do not have to do it in all the code that
738 bp->b_flags &= ~B_INVAL;
739 bp->b_ioflags &= ~BIO_ERROR;
741 KASSERT(!(bp->b_flags & B_DONE),
742 ("fuse_io_strategy: bp %p already marked done", bp));
743 if (bp->b_iocmd == BIO_READ) {
744 io.iov_len = uiop->uio_resid = bp->b_bcount;
745 io.iov_base = bp->b_data;
746 uiop->uio_rw = UIO_READ;
748 uiop->uio_offset = ((off_t)bp->b_blkno) * biosize;
749 error = fuse_read_directbackend(vp, uiop, cred, fufh);
751 /* XXXCEM: Potentially invalid access to cached_attrs here */
752 if ((!error && uiop->uio_resid) ||
753 (fsess_opt_brokenio(vnode_mount(vp)) && error == EIO &&
754 uiop->uio_offset < fvdat->filesize && fvdat->filesize > 0 &&
755 uiop->uio_offset >= fvdat->cached_attrs.va_size)) {
757 * If we had a short read with no error, we must have
758 * hit a file hole. We should zero-fill the remainder.
759 * This can also occur if the server hits the file EOF.
761 * Holes used to be able to occur due to pending
762 * writes, but that is not possible any longer.
764 int nread = bp->b_bcount - uiop->uio_resid;
765 int left = uiop->uio_resid;
768 printf("FUSE: Fix broken io: offset %ju, "
769 " resid %zd, file size %ju/%ju\n",
770 (uintmax_t)uiop->uio_offset,
771 uiop->uio_resid, fvdat->filesize,
772 fvdat->cached_attrs.va_size);
776 bzero((char *)bp->b_data + nread, left);
780 bp->b_ioflags |= BIO_ERROR;
785 * If we only need to commit, try to commit
787 if (bp->b_flags & B_NEEDCOMMIT) {
788 SDT_PROBE2(fusefs, , io, trace, 1,
789 "write: B_NEEDCOMMIT flags set");
792 * Setup for actual write
794 if ((off_t)bp->b_blkno * biosize + bp->b_dirtyend >
796 bp->b_dirtyend = fvdat->filesize -
797 (off_t)bp->b_blkno * biosize;
799 if (bp->b_dirtyend > bp->b_dirtyoff) {
800 io.iov_len = uiop->uio_resid = bp->b_dirtyend
802 uiop->uio_offset = (off_t)bp->b_blkno * biosize
804 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
805 uiop->uio_rw = UIO_WRITE;
807 error = fuse_write_directbackend(vp, uiop, cred, fufh, 0);
809 if (error == EINTR || error == ETIMEDOUT
810 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
812 bp->b_flags &= ~(B_INVAL | B_NOCACHE);
813 if ((bp->b_flags & B_PAGING) == 0) {
815 bp->b_flags &= ~B_DONE;
817 if ((error == EINTR || error == ETIMEDOUT) &&
818 (bp->b_flags & B_ASYNC) == 0)
819 bp->b_flags |= B_EINTR;
822 bp->b_ioflags |= BIO_ERROR;
823 bp->b_flags |= B_INVAL;
826 bp->b_dirtyoff = bp->b_dirtyend = 0;
834 bp->b_resid = uiop->uio_resid;
840 fuse_io_flushbuf(struct vnode *vp, int waitfor, struct thread *td)
842 struct vop_fsync_args a = {
844 .a_waitfor = waitfor,
848 return (vop_stdfsync(&a));
852 * Flush and invalidate all dirty buffers. If another process is already
853 * doing the flush, just wait for completion.
856 fuse_io_invalbuf(struct vnode *vp, struct thread *td)
858 struct fuse_vnode_data *fvdat = VTOFUD(vp);
861 if (vp->v_iflag & VI_DOOMED)
864 ASSERT_VOP_ELOCKED(vp, "fuse_io_invalbuf");
866 while (fvdat->flag & FN_FLUSHINPROG) {
867 struct proc *p = td->td_proc;
869 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF)
871 fvdat->flag |= FN_FLUSHWANT;
872 tsleep(&fvdat->flag, PRIBIO + 2, "fusevinv", 2 * hz);
876 if (SIGNOTEMPTY(p->p_siglist) ||
877 SIGNOTEMPTY(td->td_siglist))
884 fvdat->flag |= FN_FLUSHINPROG;
886 if (vp->v_bufobj.bo_object != NULL) {
887 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
888 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
889 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
891 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
893 if (error == ERESTART || error == EINTR) {
894 fvdat->flag &= ~FN_FLUSHINPROG;
895 if (fvdat->flag & FN_FLUSHWANT) {
896 fvdat->flag &= ~FN_FLUSHWANT;
897 wakeup(&fvdat->flag);
901 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
903 fvdat->flag &= ~FN_FLUSHINPROG;
904 if (fvdat->flag & FN_FLUSHWANT) {
905 fvdat->flag &= ~FN_FLUSHWANT;
906 wakeup(&fvdat->flag);